Line data Source code
1 : /*
2 : Samba Unix/Linux SMB client library
3 : Distributed SMB/CIFS Server Management Utility
4 :
5 : Copyright (C) Jeremy Allison (jra@samba.org) 2005
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "system/passwd.h"
23 : #include "system/filesys.h"
24 : #include "utils/net.h"
25 : #include "../libcli/security/security.h"
26 : #include "lib/util/string_wrappers.h"
27 :
28 : struct {
29 : const char *us_errstr;
30 : enum usershare_err us_err;
31 : } us_errs [] = {
32 : {"",USERSHARE_OK},
33 : {N_("Malformed usershare file"), USERSHARE_MALFORMED_FILE},
34 : {N_("Bad version number"), USERSHARE_BAD_VERSION},
35 : {N_("Malformed path entry"), USERSHARE_MALFORMED_PATH},
36 : {N_("Malformed comment entryfile"), USERSHARE_MALFORMED_COMMENT_DEF},
37 : {N_("Malformed acl definition"), USERSHARE_MALFORMED_ACL_DEF},
38 : {N_("Acl parse error"), USERSHARE_ACL_ERR},
39 : {N_("Path not absolute"), USERSHARE_PATH_NOT_ABSOLUTE},
40 : {N_("Path is denied"), USERSHARE_PATH_IS_DENIED},
41 : {N_("Path not allowed"), USERSHARE_PATH_NOT_ALLOWED},
42 : {N_("Path is not a directory"), USERSHARE_PATH_NOT_DIRECTORY},
43 : {N_("System error"), USERSHARE_POSIX_ERR},
44 : {N_("Malformed sharename definition"), USERSHARE_MALFORMED_SHARENAME_DEF},
45 : {N_("Bad sharename (doesn't match filename)"), USERSHARE_BAD_SHARENAME},
46 : {NULL,(enum usershare_err)-1}
47 : };
48 :
49 0 : static const char *get_us_error_code(enum usershare_err us_err)
50 : {
51 : char *result;
52 0 : int idx = 0;
53 :
54 0 : while (us_errs[idx].us_errstr != NULL) {
55 0 : if (us_errs[idx].us_err == us_err) {
56 0 : return us_errs[idx].us_errstr;
57 : }
58 0 : idx++;
59 : }
60 :
61 0 : result = talloc_asprintf(talloc_tos(), _("Usershare error code (0x%x)"),
62 : (unsigned int)us_err);
63 0 : SMB_ASSERT(result != NULL);
64 0 : return result;
65 : }
66 :
67 : /* The help subsystem for the USERSHARE subcommand */
68 :
69 0 : static int net_usershare_add_usage(struct net_context *c, int argc, const char **argv)
70 : {
71 0 : char chr = *lp_winbind_separator();
72 0 : d_printf(_(
73 : "net usershare add [--long] <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>]\n"
74 : "\tAdds the specified share name for this user.\n"
75 : "\t<sharename> is the new share name.\n"
76 : "\t<path> is the path on the filesystem to export.\n"
77 : "\t<comment> is the optional comment for the new share.\n"
78 : "\t<acl> is an optional share acl in the format \"DOMAIN%cname:X,DOMAIN%cname:X,....\"\n"
79 : "\t<guest_ok=y> if present sets \"guest ok = yes\" on this usershare.\n"
80 : "\t\t\"X\" represents a permission and can be any one of the characters f, r or d\n"
81 : "\t\twhere \"f\" means full control, \"r\" means read-only, \"d\" means deny access.\n"
82 : "\t\tname may be a domain user or group. For local users use the local server name "
83 : "instead of \"DOMAIN\"\n"
84 : "\t\tThe default acl is \"Everyone:r\" which allows everyone read-only access.\n"
85 : "\tAdd --long to print the info on the newly added share.\n"),
86 : chr, chr );
87 0 : return -1;
88 : }
89 :
90 0 : static int net_usershare_delete_usage(struct net_context *c, int argc, const char **argv)
91 : {
92 0 : d_printf(_(
93 : "net usershare delete <sharename>\n"
94 : "\tdeletes the specified share name for this user.\n"));
95 0 : return -1;
96 : }
97 :
98 0 : static int net_usershare_info_usage(struct net_context *c, int argc, const char **argv)
99 : {
100 0 : d_printf(_(
101 : "net usershare info [--long] [wildcard sharename]\n"
102 : "\tPrints out the path, comment and acl elements of shares that match the wildcard.\n"
103 : "\tBy default only gives info on shares owned by the current user\n"
104 : "\tAdd --long to apply this to all shares\n"
105 : "\tOmit the sharename or use a wildcard of '*' to see all shares\n"));
106 0 : return -1;
107 : }
108 :
109 0 : static int net_usershare_list_usage(struct net_context *c, int argc, const char **argv)
110 : {
111 0 : d_printf(_(
112 : "net usershare list [--long] [wildcard sharename]\n"
113 : "\tLists the names of all shares that match the wildcard.\n"
114 : "\tBy default only lists shares owned by the current user\n"
115 : "\tAdd --long to apply this to all shares\n"
116 : "\tOmit the sharename or use a wildcard of '*' to see all shares\n"));
117 0 : return -1;
118 : }
119 :
120 0 : int net_usershare_usage(struct net_context *c, int argc, const char **argv)
121 : {
122 0 : d_printf(_("net usershare add <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>] to "
123 : "add or change a user defined share.\n"
124 : "net usershare delete <sharename> to delete a user defined share.\n"
125 : "net usershare info [--long] [wildcard sharename] to print info about a user defined share.\n"
126 : "net usershare list [--long] [wildcard sharename] to list user defined shares.\n"
127 : "net usershare help\n"
128 : "\nType \"net usershare help <option>\" to get more information on that option\n\n"));
129 :
130 0 : net_common_flags_usage(c, argc, argv);
131 0 : return -1;
132 : }
133 :
134 : /***************************************************************************
135 : ***************************************************************************/
136 :
137 0 : static char *get_basepath(TALLOC_CTX *ctx)
138 : {
139 0 : const struct loadparm_substitution *lp_sub =
140 0 : loadparm_s3_global_substitution();
141 0 : char *basepath = lp_usershare_path(ctx, lp_sub);
142 :
143 0 : if (!basepath) {
144 0 : return NULL;
145 : }
146 0 : if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
147 0 : basepath[strlen(basepath)-1] = '\0';
148 : }
149 0 : return basepath;
150 : }
151 :
152 : /***************************************************************************
153 : Delete a single userlevel share.
154 : ***************************************************************************/
155 :
156 0 : static int net_usershare_delete(struct net_context *c, int argc, const char **argv)
157 : {
158 0 : const struct loadparm_substitution *lp_sub =
159 0 : loadparm_s3_global_substitution();
160 : char *us_path;
161 : char *sharename;
162 :
163 0 : if (argc != 1 || c->display_usage) {
164 0 : return net_usershare_delete_usage(c, argc, argv);
165 : }
166 :
167 0 : if ((sharename = strlower_talloc(talloc_tos(), argv[0])) == NULL) {
168 0 : d_fprintf(stderr, _("strlower_talloc failed\n"));
169 0 : return -1;
170 : }
171 :
172 0 : if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
173 0 : d_fprintf(stderr, _("net usershare delete: share name %s contains "
174 : "invalid characters (any of %s)\n"),
175 : sharename, INVALID_SHARENAME_CHARS);
176 0 : TALLOC_FREE(sharename);
177 0 : return -1;
178 : }
179 :
180 0 : us_path = talloc_asprintf(talloc_tos(),
181 : "%s/%s",
182 : lp_usershare_path(talloc_tos(), lp_sub),
183 : sharename);
184 0 : if (!us_path) {
185 0 : TALLOC_FREE(sharename);
186 0 : return -1;
187 : }
188 :
189 0 : if (unlink(us_path) != 0) {
190 0 : d_fprintf(stderr, _("net usershare delete: unable to remove usershare %s. "
191 : "Error was %s\n"),
192 0 : us_path, strerror(errno));
193 0 : TALLOC_FREE(sharename);
194 0 : return -1;
195 : }
196 0 : TALLOC_FREE(sharename);
197 0 : return 0;
198 : }
199 :
200 : /***************************************************************************
201 : Data structures to handle a list of usershare files.
202 : ***************************************************************************/
203 :
204 : struct file_list {
205 : struct file_list *next, *prev;
206 : const char *pathname;
207 : };
208 :
209 : static struct file_list *flist;
210 :
211 : /***************************************************************************
212 : ***************************************************************************/
213 :
214 0 : static int get_share_list(TALLOC_CTX *ctx, const char *wcard, bool only_ours)
215 : {
216 : DIR *dp;
217 : struct dirent *de;
218 0 : uid_t myuid = geteuid();
219 0 : struct file_list *fl = NULL;
220 0 : char *basepath = get_basepath(ctx);
221 :
222 0 : if (!basepath) {
223 0 : return -1;
224 : }
225 0 : dp = opendir(basepath);
226 0 : if (!dp) {
227 0 : d_fprintf(stderr,
228 0 : _("get_share_list: cannot open usershare directory %s. "
229 : "Error %s\n"),
230 0 : basepath, strerror(errno) );
231 0 : return -1;
232 : }
233 :
234 0 : while((de = readdir(dp)) != 0) {
235 : SMB_STRUCT_STAT sbuf;
236 : char *path;
237 0 : const char *n = de->d_name;
238 :
239 : /* Ignore . and .. */
240 0 : if (*n == '.') {
241 0 : if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
242 0 : continue;
243 : }
244 : }
245 :
246 0 : if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
247 0 : d_fprintf(stderr,
248 0 : _("get_share_list: ignoring bad share "
249 : "name %s\n"), n);
250 0 : continue;
251 : }
252 0 : path = talloc_asprintf(ctx,
253 : "%s/%s",
254 : basepath,
255 : n);
256 0 : if (!path) {
257 0 : closedir(dp);
258 0 : return -1;
259 : }
260 :
261 0 : if (sys_lstat(path, &sbuf, false) != 0) {
262 0 : d_fprintf(stderr,
263 0 : _("get_share_list: can't lstat file %s. Error "
264 : "was %s\n"),
265 0 : path, strerror(errno) );
266 0 : continue;
267 : }
268 :
269 0 : if (!S_ISREG(sbuf.st_ex_mode)) {
270 0 : d_fprintf(stderr,
271 0 : _("get_share_list: file %s is not a regular "
272 : "file. Ignoring.\n"),
273 : path );
274 0 : continue;
275 : }
276 :
277 0 : if (only_ours && sbuf.st_ex_uid != myuid) {
278 0 : continue;
279 : }
280 :
281 0 : if (!unix_wild_match(wcard, n)) {
282 0 : continue;
283 : }
284 :
285 : /* (Finally) - add to list. */
286 0 : fl = talloc(ctx, struct file_list);
287 0 : if (!fl) {
288 0 : closedir(dp);
289 0 : return -1;
290 : }
291 0 : fl->pathname = talloc_strdup(ctx, n);
292 0 : if (!fl->pathname) {
293 0 : closedir(dp);
294 0 : return -1;
295 : }
296 :
297 0 : DLIST_ADD(flist, fl);
298 : }
299 :
300 0 : closedir(dp);
301 0 : return 0;
302 : }
303 :
304 : enum us_priv_op { US_LIST_OP, US_INFO_OP};
305 :
306 : struct us_priv_info {
307 : TALLOC_CTX *ctx;
308 : enum us_priv_op op;
309 : struct net_context *c;
310 : };
311 :
312 : /***************************************************************************
313 : Call a function for every share on the list.
314 : ***************************************************************************/
315 :
316 0 : static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
317 : {
318 : struct file_list *fl;
319 0 : int ret = 0;
320 :
321 0 : for (fl = flist; fl; fl = fl->next) {
322 0 : ret = (*fn)(fl, priv);
323 : }
324 :
325 0 : return ret;
326 : }
327 :
328 : /***************************************************************************
329 : Info function.
330 : ***************************************************************************/
331 :
332 0 : static int info_fn(struct file_list *fl, void *priv)
333 : {
334 : SMB_STRUCT_STAT sbuf;
335 0 : char **lines = NULL;
336 0 : struct us_priv_info *pi = (struct us_priv_info *)priv;
337 0 : TALLOC_CTX *ctx = pi->ctx;
338 0 : struct net_context *c = pi->c;
339 0 : int fd = -1;
340 0 : int numlines = 0;
341 0 : struct security_descriptor *psd = NULL;
342 : char *basepath;
343 0 : char *sharepath = NULL;
344 0 : char *comment = NULL;
345 0 : char *cp_sharename = NULL;
346 : char *acl_str;
347 : int num_aces;
348 : char sep_str[2];
349 : enum usershare_err us_err;
350 0 : bool guest_ok = false;
351 :
352 0 : sep_str[0] = *lp_winbind_separator();
353 0 : sep_str[1] = '\0';
354 :
355 0 : basepath = get_basepath(ctx);
356 0 : if (!basepath) {
357 0 : return -1;
358 : }
359 0 : basepath = talloc_asprintf_append(basepath,
360 : "/%s",
361 : fl->pathname);
362 0 : if (!basepath) {
363 0 : return -1;
364 : }
365 :
366 : #ifdef O_NOFOLLOW
367 0 : fd = open(basepath, O_RDONLY|O_NOFOLLOW, 0);
368 : #else
369 : fd = open(basepath, O_RDONLY, 0);
370 : #endif
371 :
372 0 : if (fd == -1) {
373 0 : d_fprintf(stderr, _("info_fn: unable to open %s. %s\n"),
374 0 : basepath, strerror(errno) );
375 0 : return -1;
376 : }
377 :
378 : /* Paranoia... */
379 0 : if (sys_fstat(fd, &sbuf, false) != 0) {
380 0 : d_fprintf(stderr,
381 0 : _("info_fn: can't fstat file %s. Error was %s\n"),
382 0 : basepath, strerror(errno) );
383 0 : close(fd);
384 0 : return -1;
385 : }
386 :
387 0 : if (!S_ISREG(sbuf.st_ex_mode)) {
388 0 : d_fprintf(stderr,
389 0 : _("info_fn: file %s is not a regular file. Ignoring.\n"),
390 : basepath );
391 0 : close(fd);
392 0 : return -1;
393 : }
394 :
395 0 : lines = fd_lines_load(fd, &numlines, 10240, NULL);
396 0 : close(fd);
397 :
398 0 : if (lines == NULL) {
399 0 : return -1;
400 : }
401 :
402 : /* Ensure it's well formed. */
403 0 : us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
404 : &sharepath,
405 : &comment,
406 : &cp_sharename,
407 : &psd,
408 : &guest_ok);
409 :
410 0 : TALLOC_FREE(lines);
411 :
412 0 : if (us_err != USERSHARE_OK) {
413 0 : d_fprintf(stderr,
414 0 : _("info_fn: file %s is not a well formed usershare "
415 : "file.\n"),
416 : basepath );
417 0 : d_fprintf(stderr, _("info_fn: Error was %s.\n"),
418 : get_us_error_code(us_err) );
419 0 : return -1;
420 : }
421 :
422 0 : acl_str = talloc_strdup(ctx, "usershare_acl=");
423 0 : if (!acl_str) {
424 0 : return -1;
425 : }
426 :
427 0 : for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
428 : const char *domain;
429 : const char *name;
430 : NTSTATUS ntstatus;
431 :
432 0 : ntstatus = net_lookup_name_from_sid(c, ctx,
433 0 : &psd->dacl->aces[num_aces].trustee,
434 : &domain, &name);
435 :
436 0 : if (NT_STATUS_IS_OK(ntstatus)) {
437 0 : if (domain && *domain) {
438 0 : acl_str = talloc_asprintf_append(acl_str,
439 : "%s%s",
440 : domain,
441 : sep_str);
442 0 : if (!acl_str) {
443 0 : return -1;
444 : }
445 : }
446 0 : acl_str = talloc_asprintf_append(acl_str,
447 : "%s",
448 : name);
449 0 : if (!acl_str) {
450 0 : return -1;
451 : }
452 :
453 : } else {
454 : struct dom_sid_buf sidstr;
455 :
456 0 : acl_str = talloc_asprintf_append(
457 : acl_str,
458 : "%s",
459 : dom_sid_str_buf(
460 0 : &psd->dacl->aces[num_aces].trustee,
461 : &sidstr));
462 0 : if (!acl_str) {
463 0 : return -1;
464 : }
465 : }
466 0 : acl_str = talloc_asprintf_append(acl_str, ":");
467 0 : if (!acl_str) {
468 0 : return -1;
469 : }
470 :
471 0 : if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
472 0 : acl_str = talloc_asprintf_append(acl_str, "D,");
473 0 : if (!acl_str) {
474 0 : return -1;
475 : }
476 : } else {
477 0 : if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
478 0 : acl_str = talloc_asprintf_append(acl_str, "F,");
479 : } else {
480 0 : acl_str = talloc_asprintf_append(acl_str, "R,");
481 : }
482 0 : if (!acl_str) {
483 0 : return -1;
484 : }
485 : }
486 : }
487 :
488 : /* NOTE: This is smb.conf-like output. Do not translate. */
489 0 : if (pi->op == US_INFO_OP) {
490 0 : d_printf("[%s]\n", cp_sharename );
491 0 : d_printf("path=%s\n", sharepath );
492 0 : d_printf("comment=%s\n", comment);
493 0 : d_printf("%s\n", acl_str);
494 0 : d_printf("guest_ok=%c\n\n", guest_ok ? 'y' : 'n');
495 0 : } else if (pi->op == US_LIST_OP) {
496 0 : d_printf("%s\n", cp_sharename);
497 : }
498 :
499 0 : return 0;
500 : }
501 :
502 : /***************************************************************************
503 : Print out info (internal detail) on userlevel shares.
504 : ***************************************************************************/
505 :
506 0 : static int net_usershare_info(struct net_context *c, int argc, const char **argv)
507 : {
508 : fstring wcard;
509 0 : bool only_ours = true;
510 0 : int ret = -1;
511 : struct us_priv_info pi;
512 : TALLOC_CTX *ctx;
513 :
514 0 : fstrcpy(wcard, "*");
515 :
516 0 : if (c->display_usage)
517 0 : return net_usershare_info_usage(c, argc, argv);
518 :
519 0 : if (c->opt_long_list_entries) {
520 0 : only_ours = false;
521 : }
522 :
523 0 : switch (argc) {
524 0 : case 0:
525 0 : break;
526 0 : case 1:
527 0 : fstrcpy(wcard, argv[0]);
528 0 : break;
529 0 : default:
530 0 : return net_usershare_info_usage(c, argc, argv);
531 : }
532 :
533 0 : if (!strlower_m(wcard)) {
534 0 : return -1;
535 : }
536 :
537 0 : ctx = talloc_init("share_info");
538 0 : ret = get_share_list(ctx, wcard, only_ours);
539 0 : if (ret) {
540 0 : return ret;
541 : }
542 :
543 0 : pi.ctx = ctx;
544 0 : pi.op = US_INFO_OP;
545 0 : pi.c = c;
546 :
547 0 : ret = process_share_list(info_fn, &pi);
548 0 : talloc_destroy(ctx);
549 0 : return ret;
550 : }
551 :
552 : /***************************************************************************
553 : Count the current total number of usershares.
554 : ***************************************************************************/
555 :
556 0 : static int count_num_usershares(void)
557 : {
558 : DIR *dp;
559 : struct dirent *de;
560 0 : int num_usershares = 0;
561 0 : TALLOC_CTX *ctx = talloc_tos();
562 0 : char *basepath = get_basepath(ctx);
563 :
564 0 : if (!basepath) {
565 0 : return -1;
566 : }
567 :
568 0 : dp = opendir(basepath);
569 0 : if (!dp) {
570 0 : d_fprintf(stderr,
571 0 : _("count_num_usershares: cannot open usershare "
572 : "directory %s. Error %s\n"),
573 0 : basepath, strerror(errno) );
574 0 : return -1;
575 : }
576 :
577 0 : while((de = readdir(dp)) != 0) {
578 : SMB_STRUCT_STAT sbuf;
579 : char *path;
580 0 : const char *n = de->d_name;
581 :
582 : /* Ignore . and .. */
583 0 : if (*n == '.') {
584 0 : if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
585 0 : continue;
586 : }
587 : }
588 :
589 0 : if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
590 0 : d_fprintf(stderr,
591 0 : _("count_num_usershares: ignoring bad share "
592 : "name %s\n"), n);
593 0 : continue;
594 : }
595 0 : path = talloc_asprintf(ctx,
596 : "%s/%s",
597 : basepath,
598 : n);
599 0 : if (!path) {
600 0 : closedir(dp);
601 0 : return -1;
602 : }
603 :
604 0 : if (sys_lstat(path, &sbuf, false) != 0) {
605 0 : d_fprintf(stderr,
606 0 : _("count_num_usershares: can't lstat file %s. "
607 : "Error was %s\n"),
608 0 : path, strerror(errno) );
609 0 : continue;
610 : }
611 :
612 0 : if (!S_ISREG(sbuf.st_ex_mode)) {
613 0 : d_fprintf(stderr,
614 0 : _("count_num_usershares: file %s is not a "
615 : "regular file. Ignoring.\n"),
616 : path );
617 0 : continue;
618 : }
619 0 : num_usershares++;
620 : }
621 :
622 0 : closedir(dp);
623 0 : return num_usershares;
624 : }
625 :
626 : /***************************************************************************
627 : Add a single userlevel share.
628 : ***************************************************************************/
629 :
630 0 : static int net_usershare_add(struct net_context *c, int argc, const char **argv)
631 : {
632 0 : TALLOC_CTX *ctx = talloc_stackframe();
633 : SMB_STRUCT_STAT sbuf;
634 : SMB_STRUCT_STAT lsbuf;
635 : char *sharename;
636 : const char *cp_sharename;
637 : char *full_path;
638 : char *full_path_tmp;
639 : const char *us_path;
640 : const char *us_comment;
641 : const char *arg_acl;
642 : char *us_acl;
643 : char *file_img;
644 0 : int num_aces = 0;
645 : int i;
646 : int tmpfd;
647 : const char *pacl;
648 : size_t to_write;
649 0 : uid_t myeuid = geteuid();
650 0 : bool guest_ok = false;
651 : int num_usershares;
652 : mode_t mask;
653 :
654 0 : us_comment = "";
655 0 : arg_acl = "S-1-1-0:R";
656 :
657 0 : if (c->display_usage) {
658 0 : TALLOC_FREE(ctx);
659 0 : return net_usershare_add_usage(c, argc, argv);
660 : }
661 :
662 0 : switch (argc) {
663 0 : case 0:
664 : case 1:
665 : default:
666 0 : TALLOC_FREE(ctx);
667 0 : return net_usershare_add_usage(c, argc, argv);
668 0 : case 2:
669 0 : cp_sharename = argv[0];
670 0 : sharename = strlower_talloc(ctx, argv[0]);
671 0 : us_path = argv[1];
672 0 : break;
673 0 : case 3:
674 0 : cp_sharename = argv[0];
675 0 : sharename = strlower_talloc(ctx, argv[0]);
676 0 : us_path = argv[1];
677 0 : us_comment = argv[2];
678 0 : break;
679 0 : case 4:
680 0 : cp_sharename = argv[0];
681 0 : sharename = strlower_talloc(ctx, argv[0]);
682 0 : us_path = argv[1];
683 0 : us_comment = argv[2];
684 0 : arg_acl = argv[3];
685 0 : break;
686 0 : case 5:
687 0 : cp_sharename = argv[0];
688 0 : sharename = strlower_talloc(ctx, argv[0]);
689 0 : us_path = argv[1];
690 0 : us_comment = argv[2];
691 0 : arg_acl = argv[3];
692 0 : if (strlen(arg_acl) == 0) {
693 0 : arg_acl = "S-1-1-0:R";
694 : }
695 0 : if (!strnequal(argv[4], "guest_ok=", 9)) {
696 0 : TALLOC_FREE(ctx);
697 0 : return net_usershare_add_usage(c, argc, argv);
698 : }
699 0 : switch (argv[4][9]) {
700 0 : case 'y':
701 : case 'Y':
702 0 : guest_ok = true;
703 0 : break;
704 0 : case 'n':
705 : case 'N':
706 0 : guest_ok = false;
707 0 : break;
708 0 : default:
709 0 : TALLOC_FREE(ctx);
710 0 : return net_usershare_add_usage(c, argc, argv);
711 : }
712 0 : break;
713 : }
714 :
715 : /* Ensure we're under the "usershare max shares" number. Advisory only. */
716 0 : num_usershares = count_num_usershares();
717 0 : if (num_usershares >= lp_usershare_max_shares()) {
718 0 : d_fprintf(stderr,
719 0 : _("net usershare add: maximum number of allowed "
720 : "usershares (%d) reached\n"),
721 : lp_usershare_max_shares() );
722 0 : TALLOC_FREE(ctx);
723 0 : return -1;
724 : }
725 :
726 0 : if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
727 0 : d_fprintf(stderr, _("net usershare add: share name %s contains "
728 : "invalid characters (any of %s)\n"),
729 : sharename, INVALID_SHARENAME_CHARS);
730 0 : TALLOC_FREE(ctx);
731 0 : return -1;
732 : }
733 :
734 : /* Disallow shares the same as users. */
735 0 : if (getpwnam(sharename)) {
736 0 : d_fprintf(stderr,
737 0 : _("net usershare add: share name %s is already a valid "
738 : "system user name\n"),
739 : sharename );
740 0 : TALLOC_FREE(ctx);
741 0 : return -1;
742 : }
743 :
744 : /* Construct the full path for the usershare file. */
745 0 : full_path = get_basepath(ctx);
746 0 : if (!full_path) {
747 0 : TALLOC_FREE(ctx);
748 0 : return -1;
749 : }
750 0 : full_path_tmp = talloc_asprintf(ctx,
751 : "%s/:tmpXXXXXX",
752 : full_path);
753 0 : if (!full_path_tmp) {
754 0 : TALLOC_FREE(ctx);
755 0 : return -1;
756 : }
757 :
758 0 : full_path = talloc_asprintf_append(full_path,
759 : "/%s",
760 : sharename);
761 0 : if (!full_path) {
762 0 : TALLOC_FREE(ctx);
763 0 : return -1;
764 : }
765 :
766 : /* The path *must* be absolute. */
767 0 : if (us_path[0] != '/') {
768 0 : d_fprintf(stderr,
769 0 : _("net usershare add: path %s is not an absolute "
770 : "path.\n"),
771 : us_path);
772 0 : TALLOC_FREE(ctx);
773 0 : return -1;
774 : }
775 :
776 : /* Check the directory to be shared exists. */
777 0 : if (sys_stat(us_path, &sbuf, false) != 0) {
778 0 : d_fprintf(stderr,
779 0 : _("net usershare add: cannot stat path %s to ensure "
780 : "this is a directory. Error was %s\n"),
781 0 : us_path, strerror(errno) );
782 0 : TALLOC_FREE(ctx);
783 0 : return -1;
784 : }
785 :
786 0 : if (!S_ISDIR(sbuf.st_ex_mode)) {
787 0 : d_fprintf(stderr,
788 0 : _("net usershare add: path %s is not a directory.\n"),
789 : us_path );
790 0 : TALLOC_FREE(ctx);
791 0 : return -1;
792 : }
793 :
794 : /* If we're not root, check if we're restricted to sharing out directories
795 : that we own only. */
796 :
797 0 : if ((myeuid != 0) && lp_usershare_owner_only() && (myeuid != sbuf.st_ex_uid)) {
798 0 : d_fprintf(stderr, _("net usershare add: cannot share path %s as "
799 : "we are restricted to only sharing directories we own.\n"
800 : "\tAsk the administrator to add the line \"usershare owner only = false\" \n"
801 : "\tto the [global] section of the smb.conf to allow this.\n"),
802 : us_path );
803 0 : TALLOC_FREE(ctx);
804 0 : return -1;
805 : }
806 :
807 : /* No validation needed on comment. Now go through and validate the
808 : acl string. Convert names to SID's as needed. Then run it through
809 : parse_usershare_acl to ensure it's valid. */
810 :
811 : /* Start off the string we'll append to. */
812 0 : us_acl = talloc_strdup(ctx, "");
813 0 : if (!us_acl) {
814 0 : TALLOC_FREE(ctx);
815 0 : return -1;
816 : }
817 :
818 0 : pacl = arg_acl;
819 0 : num_aces = 1;
820 :
821 : /* Add the number of ',' characters to get the number of aces. */
822 0 : num_aces += count_chars(pacl,',');
823 :
824 0 : for (i = 0; i < num_aces; i++) {
825 : struct dom_sid sid;
826 : struct dom_sid_buf buf;
827 0 : const char *pcolon = strchr_m(pacl, ':');
828 : const char *name;
829 :
830 0 : if (pcolon == NULL) {
831 0 : d_fprintf(stderr,
832 0 : _("net usershare add: malformed acl %s "
833 : "(missing ':').\n"),
834 : pacl );
835 0 : TALLOC_FREE(ctx);
836 0 : return -1;
837 : }
838 :
839 0 : switch(pcolon[1]) {
840 0 : case 'f':
841 : case 'F':
842 : case 'd':
843 : case 'r':
844 : case 'R':
845 0 : break;
846 0 : default:
847 0 : d_fprintf(stderr,
848 0 : _("net usershare add: malformed acl %s "
849 : "(access control must be 'r', 'f', "
850 : "or 'd')\n"),
851 : pacl );
852 0 : TALLOC_FREE(ctx);
853 0 : return -1;
854 : }
855 :
856 0 : if (pcolon[2] != ',' && pcolon[2] != '\0') {
857 0 : d_fprintf(stderr,
858 0 : _("net usershare add: malformed terminating "
859 : "character for acl %s\n"),
860 : pacl );
861 0 : TALLOC_FREE(ctx);
862 0 : return -1;
863 : }
864 :
865 : /* Get the name */
866 0 : if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
867 0 : d_fprintf(stderr, _("talloc_strndup failed\n"));
868 0 : TALLOC_FREE(ctx);
869 0 : return -1;
870 : }
871 0 : if (!string_to_sid(&sid, name)) {
872 : /* Convert to a SID */
873 0 : NTSTATUS ntstatus = net_lookup_sid_from_name(c, ctx, name, &sid);
874 0 : if (!NT_STATUS_IS_OK(ntstatus)) {
875 0 : d_fprintf(stderr,
876 0 : _("net usershare add: cannot convert "
877 : "name \"%s\" to a SID. %s."),
878 : name, get_friendly_nt_error_msg(ntstatus) );
879 0 : if (NT_STATUS_EQUAL(ntstatus, NT_STATUS_CONNECTION_REFUSED)) {
880 0 : d_fprintf(stderr,
881 0 : _(" Maybe smbd is not running.\n"));
882 : } else {
883 0 : d_fprintf(stderr, "\n");
884 : }
885 0 : TALLOC_FREE(ctx);
886 0 : return -1;
887 : }
888 : }
889 0 : us_acl = talloc_asprintf_append(
890 : us_acl,
891 : "%s:%c,",
892 : dom_sid_str_buf(&sid, &buf),
893 0 : pcolon[1]);
894 :
895 : /* Move to the next ACL entry. */
896 0 : if (pcolon[2] == ',') {
897 0 : pacl = &pcolon[3];
898 : }
899 : }
900 :
901 : /* Remove the last ',' */
902 0 : us_acl[strlen(us_acl)-1] = '\0';
903 :
904 0 : if (guest_ok && !lp_usershare_allow_guests()) {
905 0 : d_fprintf(stderr, _("net usershare add: guest_ok=y requested "
906 : "but the \"usershare allow guests\" parameter is not "
907 : "enabled by this server.\n"));
908 0 : TALLOC_FREE(ctx);
909 0 : return -1;
910 : }
911 :
912 : /* Create a temporary filename for this share. */
913 0 : mask = umask(S_IRWXO | S_IRWXG);
914 0 : tmpfd = mkstemp(full_path_tmp);
915 0 : umask(mask);
916 :
917 0 : if (tmpfd == -1) {
918 0 : d_fprintf(stderr,
919 0 : _("net usershare add: cannot create tmp file %s\n"),
920 : full_path_tmp );
921 0 : TALLOC_FREE(ctx);
922 0 : return -1;
923 : }
924 :
925 : /* Ensure we opened the file we thought we did. */
926 0 : if (sys_lstat(full_path_tmp, &lsbuf, false) != 0) {
927 0 : d_fprintf(stderr,
928 0 : _("net usershare add: cannot lstat tmp file %s\n"),
929 : full_path_tmp );
930 0 : TALLOC_FREE(ctx);
931 0 : close(tmpfd);
932 0 : return -1;
933 : }
934 :
935 : /* Check this is the same as the file we opened. */
936 0 : if (sys_fstat(tmpfd, &sbuf, false) != 0) {
937 0 : d_fprintf(stderr,
938 0 : _("net usershare add: cannot fstat tmp file %s\n"),
939 : full_path_tmp );
940 0 : TALLOC_FREE(ctx);
941 0 : close(tmpfd);
942 0 : return -1;
943 : }
944 :
945 0 : if (!S_ISREG(sbuf.st_ex_mode) || sbuf.st_ex_dev != lsbuf.st_ex_dev || sbuf.st_ex_ino != lsbuf.st_ex_ino) {
946 0 : d_fprintf(stderr,
947 0 : _("net usershare add: tmp file %s is not a regular "
948 : "file ?\n"),
949 : full_path_tmp );
950 0 : TALLOC_FREE(ctx);
951 0 : close(tmpfd);
952 0 : return -1;
953 : }
954 :
955 0 : if (fchmod(tmpfd, 0644) == -1) {
956 0 : d_fprintf(stderr,
957 0 : _("net usershare add: failed to fchmod tmp file %s "
958 : "to 0644n"),
959 : full_path_tmp );
960 0 : TALLOC_FREE(ctx);
961 0 : close(tmpfd);
962 0 : return -1;
963 : }
964 :
965 : /* Create the in-memory image of the file. */
966 0 : file_img = talloc_strdup(ctx, "#VERSION 2\npath=");
967 0 : file_img = talloc_asprintf_append(file_img,
968 : "%s\ncomment=%s\nusershare_acl=%s\n"
969 : "guest_ok=%c\nsharename=%s\n",
970 : us_path,
971 : us_comment,
972 : us_acl,
973 : guest_ok ? 'y' : 'n',
974 : cp_sharename);
975 :
976 0 : to_write = strlen(file_img);
977 :
978 0 : if (write(tmpfd, file_img, to_write) != to_write) {
979 0 : d_fprintf(stderr,
980 0 : _("net usershare add: failed to write %u bytes to "
981 : "file %s. Error was %s\n"),
982 0 : (unsigned int)to_write, full_path_tmp, strerror(errno));
983 0 : unlink(full_path_tmp);
984 0 : TALLOC_FREE(ctx);
985 0 : close(tmpfd);
986 0 : return -1;
987 : }
988 :
989 : /* Attempt to replace any existing share by this name. */
990 0 : if (rename(full_path_tmp, full_path) != 0) {
991 0 : unlink(full_path_tmp);
992 0 : d_fprintf(stderr,
993 0 : _("net usershare add: failed to add share %s. Error "
994 : "was %s\n"),
995 0 : sharename, strerror(errno));
996 0 : TALLOC_FREE(ctx);
997 0 : close(tmpfd);
998 0 : return -1;
999 : }
1000 :
1001 0 : close(tmpfd);
1002 :
1003 0 : if (c->opt_long_list_entries) {
1004 : const char *my_argv[2];
1005 0 : my_argv[0] = sharename;
1006 0 : my_argv[1] = NULL;
1007 0 : net_usershare_info(c, 1, my_argv);
1008 : }
1009 :
1010 0 : TALLOC_FREE(ctx);
1011 0 : return 0;
1012 : }
1013 :
1014 : #if 0
1015 : /***************************************************************************
1016 : List function.
1017 : ***************************************************************************/
1018 :
1019 : static int list_fn(struct file_list *fl, void *priv)
1020 : {
1021 : d_printf("%s\n", fl->pathname);
1022 : return 0;
1023 : }
1024 : #endif
1025 :
1026 : /***************************************************************************
1027 : List userlevel shares.
1028 : ***************************************************************************/
1029 :
1030 0 : static int net_usershare_list(struct net_context *c, int argc,
1031 : const char **argv)
1032 : {
1033 : fstring wcard;
1034 0 : bool only_ours = true;
1035 0 : int ret = -1;
1036 : struct us_priv_info pi;
1037 : TALLOC_CTX *ctx;
1038 :
1039 0 : fstrcpy(wcard, "*");
1040 :
1041 0 : if (c->display_usage)
1042 0 : return net_usershare_list_usage(c, argc, argv);
1043 :
1044 0 : if (c->opt_long_list_entries) {
1045 0 : only_ours = false;
1046 : }
1047 :
1048 0 : switch (argc) {
1049 0 : case 0:
1050 0 : break;
1051 0 : case 1:
1052 0 : fstrcpy(wcard, argv[0]);
1053 0 : break;
1054 0 : default:
1055 0 : return net_usershare_list_usage(c, argc, argv);
1056 : }
1057 :
1058 0 : if (!strlower_m(wcard)) {
1059 0 : return -1;
1060 : }
1061 :
1062 0 : ctx = talloc_init("share_list");
1063 0 : ret = get_share_list(ctx, wcard, only_ours);
1064 0 : if (ret) {
1065 0 : return ret;
1066 : }
1067 :
1068 0 : pi.ctx = ctx;
1069 0 : pi.op = US_LIST_OP;
1070 0 : pi.c = c;
1071 :
1072 0 : ret = process_share_list(info_fn, &pi);
1073 0 : talloc_destroy(ctx);
1074 0 : return ret;
1075 : }
1076 :
1077 : /***************************************************************************
1078 : Entry-point for all the USERSHARE functions.
1079 : ***************************************************************************/
1080 :
1081 0 : int net_usershare(struct net_context *c, int argc, const char **argv)
1082 : {
1083 0 : const struct loadparm_substitution *lp_sub =
1084 0 : loadparm_s3_global_substitution();
1085 : DIR *dp;
1086 :
1087 0 : struct functable func[] = {
1088 : {
1089 : "add",
1090 : net_usershare_add,
1091 : NET_TRANSPORT_LOCAL,
1092 : N_("Add/modify user defined share"),
1093 : N_("net usershare add\n"
1094 : " Add/modify user defined share")
1095 : },
1096 : {
1097 : "delete",
1098 : net_usershare_delete,
1099 : NET_TRANSPORT_LOCAL,
1100 : N_("Delete user defined share"),
1101 : N_("net usershare delete\n"
1102 : " Delete user defined share")
1103 : },
1104 : {
1105 : "info",
1106 : net_usershare_info,
1107 : NET_TRANSPORT_LOCAL,
1108 : N_("Display information about a user defined share"),
1109 : N_("net usershare info\n"
1110 : " Display information about a user defined share")
1111 : },
1112 : {
1113 : "list",
1114 : net_usershare_list,
1115 : NET_TRANSPORT_LOCAL,
1116 : N_("List user defined shares"),
1117 : N_("net usershare list\n"
1118 : " List user defined shares")
1119 : },
1120 : {NULL, NULL, 0, NULL, NULL}
1121 : };
1122 :
1123 0 : if (lp_usershare_max_shares() == 0) {
1124 0 : d_fprintf(stderr,
1125 0 : _("net usershare: usershares are currently "
1126 : "disabled\n"));
1127 0 : return -1;
1128 : }
1129 :
1130 0 : dp = opendir(lp_usershare_path(talloc_tos(), lp_sub));
1131 0 : if (!dp) {
1132 0 : int err = errno;
1133 0 : d_fprintf(stderr,
1134 0 : _("net usershare: cannot open usershare directory %s. "
1135 : "Error %s\n"),
1136 : lp_usershare_path(talloc_tos(), lp_sub), strerror(err) );
1137 0 : if (err == EACCES) {
1138 0 : d_fprintf(stderr,
1139 0 : _("You do not have permission to create a "
1140 : "usershare. Ask your administrator to grant "
1141 : "you permissions to create a share.\n"));
1142 0 : } else if (err == ENOENT) {
1143 0 : d_fprintf(stderr,
1144 0 : _("Please ask your system administrator to "
1145 : "enable user sharing.\n"));
1146 : }
1147 0 : return -1;
1148 : }
1149 0 : closedir(dp);
1150 :
1151 0 : return net_run_function(c, argc, argv, "net usershare", func);
1152 : }
|