s4: load the domain level of the current provision and create a provision with the...
[Samba/eduardoll.git] / source3 / utils / net_usershare.c
blob7a688c1330bffcf019d23bc380df1468880b2d4f
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) Jeremy Allison (jra@samba.org) 2005
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.
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.
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/>.
21 #include "includes.h"
22 #include "utils/net.h"
24 struct {
25 const char *us_errstr;
26 enum usershare_err us_err;
27 } us_errs [] = {
28 {"",USERSHARE_OK},
29 {N_("Malformed usershare file"), USERSHARE_MALFORMED_FILE},
30 {N_("Bad version number"), USERSHARE_BAD_VERSION},
31 {N_("Malformed path entry"), USERSHARE_MALFORMED_PATH},
32 {N_("Malformed comment entryfile"), USERSHARE_MALFORMED_COMMENT_DEF},
33 {N_("Malformed acl definition"), USERSHARE_MALFORMED_ACL_DEF},
34 {N_("Acl parse error"), USERSHARE_ACL_ERR},
35 {N_("Path not absolute"), USERSHARE_PATH_NOT_ABSOLUTE},
36 {N_("Path is denied"), USERSHARE_PATH_IS_DENIED},
37 {N_("Path not allowed"), USERSHARE_PATH_NOT_ALLOWED},
38 {N_("Path is not a directory"), USERSHARE_PATH_NOT_DIRECTORY},
39 {N_("System error"), USERSHARE_POSIX_ERR},
40 {NULL,(enum usershare_err)-1}
43 static const char *get_us_error_code(enum usershare_err us_err)
45 char *result;
46 int idx = 0;
48 while (us_errs[idx].us_errstr != NULL) {
49 if (us_errs[idx].us_err == us_err) {
50 return us_errs[idx].us_errstr;
52 idx++;
55 result = talloc_asprintf(talloc_tos(), _("Usershare error code (0x%x)"),
56 (unsigned int)us_err);
57 SMB_ASSERT(result != NULL);
58 return result;
61 /* The help subsystem for the USERSHARE subcommand */
63 static int net_usershare_add_usage(struct net_context *c, int argc, const char **argv)
65 char chr = *lp_winbind_separator();
66 d_printf(_(
67 "net usershare add [-l|--long] <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>]\n"
68 "\tAdds the specified share name for this user.\n"
69 "\t<sharename> is the new share name.\n"
70 "\t<path> is the path on the filesystem to export.\n"
71 "\t<comment> is the optional comment for the new share.\n"
72 "\t<acl> is an optional share acl in the format \"DOMAIN%cname:X,DOMAIN%cname:X,....\"\n"
73 "\t<guest_ok=y> if present sets \"guest ok = yes\" on this usershare.\n"
74 "\t\t\"X\" represents a permission and can be any one of the characters f, r or d\n"
75 "\t\twhere \"f\" means full control, \"r\" means read-only, \"d\" means deny access.\n"
76 "\t\tname may be a domain user or group. For local users use the local server name "
77 "instead of \"DOMAIN\"\n"
78 "\t\tThe default acl is \"Everyone:r\" which allows everyone read-only access.\n"
79 "\tAdd -l or --long to print the info on the newly added share.\n"),
80 chr, chr );
81 return -1;
84 static int net_usershare_delete_usage(struct net_context *c, int argc, const char **argv)
86 d_printf(_(
87 "net usershare delete <sharename>\n"
88 "\tdeletes the specified share name for this user.\n"));
89 return -1;
92 static int net_usershare_info_usage(struct net_context *c, int argc, const char **argv)
94 d_printf(_(
95 "net usershare info [-l|--long] [wildcard sharename]\n"
96 "\tPrints out the path, comment and acl elements of shares that match the wildcard.\n"
97 "\tBy default only gives info on shares owned by the current user\n"
98 "\tAdd -l or --long to apply this to all shares\n"
99 "\tOmit the sharename or use a wildcard of '*' to see all shares\n"));
100 return -1;
103 static int net_usershare_list_usage(struct net_context *c, int argc, const char **argv)
105 d_printf(_(
106 "net usershare list [-l|--long] [wildcard sharename]\n"
107 "\tLists the names of all shares that match the wildcard.\n"
108 "\tBy default only lists shares owned by the current user\n"
109 "\tAdd -l or --long to apply this to all shares\n"
110 "\tOmit the sharename or use a wildcard of '*' to see all shares\n"));
111 return -1;
114 int net_usershare_usage(struct net_context *c, int argc, const char **argv)
116 d_printf(_("net usershare add <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>] to "
117 "add or change a user defined share.\n"
118 "net usershare delete <sharename> to delete a user defined share.\n"
119 "net usershare info [-l|--long] [wildcard sharename] to print info about a user defined share.\n"
120 "net usershare list [-l|--long] [wildcard sharename] to list user defined shares.\n"
121 "net usershare help\n"
122 "\nType \"net usershare help <option>\" to get more information on that option\n\n"));
124 net_common_flags_usage(c, argc, argv);
125 return -1;
128 /***************************************************************************
129 ***************************************************************************/
131 static char *get_basepath(TALLOC_CTX *ctx)
133 char *basepath = talloc_strdup(ctx, lp_usershare_path());
135 if (!basepath) {
136 return NULL;
138 if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
139 basepath[strlen(basepath)-1] = '\0';
141 return basepath;
144 /***************************************************************************
145 Delete a single userlevel share.
146 ***************************************************************************/
148 static int net_usershare_delete(struct net_context *c, int argc, const char **argv)
150 char *us_path;
151 char *sharename;
153 if (argc != 1 || c->display_usage) {
154 return net_usershare_delete_usage(c, argc, argv);
157 if ((sharename = strlower_talloc(talloc_tos(), argv[0])) == NULL) {
158 d_fprintf(stderr, _("strlower_talloc failed\n"));
159 return -1;
162 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
163 d_fprintf(stderr, _("net usershare delete: share name %s contains "
164 "invalid characters (any of %s)\n"),
165 sharename, INVALID_SHARENAME_CHARS);
166 TALLOC_FREE(sharename);
167 return -1;
170 us_path = talloc_asprintf(talloc_tos(),
171 "%s/%s",
172 lp_usershare_path(),
173 sharename);
174 if (!us_path) {
175 TALLOC_FREE(sharename);
176 return -1;
179 if (unlink(us_path) != 0) {
180 d_fprintf(stderr, _("net usershare delete: unable to remove usershare %s. "
181 "Error was %s\n"),
182 us_path, strerror(errno));
183 TALLOC_FREE(sharename);
184 return -1;
186 TALLOC_FREE(sharename);
187 return 0;
190 /***************************************************************************
191 Data structures to handle a list of usershare files.
192 ***************************************************************************/
194 struct file_list {
195 struct file_list *next, *prev;
196 const char *pathname;
199 static struct file_list *flist;
201 /***************************************************************************
202 ***************************************************************************/
204 static int get_share_list(TALLOC_CTX *ctx, const char *wcard, bool only_ours)
206 SMB_STRUCT_DIR *dp;
207 SMB_STRUCT_DIRENT *de;
208 uid_t myuid = geteuid();
209 struct file_list *fl = NULL;
210 char *basepath = get_basepath(ctx);
212 if (!basepath) {
213 return -1;
215 dp = sys_opendir(basepath);
216 if (!dp) {
217 d_fprintf(stderr,
218 _("get_share_list: cannot open usershare directory %s. "
219 "Error %s\n"),
220 basepath, strerror(errno) );
221 return -1;
224 while((de = sys_readdir(dp)) != 0) {
225 SMB_STRUCT_STAT sbuf;
226 char *path;
227 const char *n = de->d_name;
229 /* Ignore . and .. */
230 if (*n == '.') {
231 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
232 continue;
236 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
237 d_fprintf(stderr,
238 _("get_share_list: ignoring bad share "
239 "name %s\n"), n);
240 continue;
242 path = talloc_asprintf(ctx,
243 "%s/%s",
244 basepath,
246 if (!path) {
247 sys_closedir(dp);
248 return -1;
251 if (sys_lstat(path, &sbuf, false) != 0) {
252 d_fprintf(stderr,
253 _("get_share_list: can't lstat file %s. Error "
254 "was %s\n"),
255 path, strerror(errno) );
256 continue;
259 if (!S_ISREG(sbuf.st_ex_mode)) {
260 d_fprintf(stderr,
261 _("get_share_list: file %s is not a regular "
262 "file. Ignoring.\n"),
263 path );
264 continue;
267 if (only_ours && sbuf.st_ex_uid != myuid) {
268 continue;
271 if (!unix_wild_match(wcard, n)) {
272 continue;
275 /* (Finally) - add to list. */
276 fl = TALLOC_P(ctx, struct file_list);
277 if (!fl) {
278 sys_closedir(dp);
279 return -1;
281 fl->pathname = talloc_strdup(ctx, n);
282 if (!fl->pathname) {
283 sys_closedir(dp);
284 return -1;
287 DLIST_ADD(flist, fl);
290 sys_closedir(dp);
291 return 0;
294 enum us_priv_op { US_LIST_OP, US_INFO_OP};
296 struct us_priv_info {
297 TALLOC_CTX *ctx;
298 enum us_priv_op op;
299 struct net_context *c;
302 /***************************************************************************
303 Call a function for every share on the list.
304 ***************************************************************************/
306 static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
308 struct file_list *fl;
309 int ret = 0;
311 for (fl = flist; fl; fl = fl->next) {
312 ret = (*fn)(fl, priv);
315 return ret;
318 /***************************************************************************
319 Info function.
320 ***************************************************************************/
322 static int info_fn(struct file_list *fl, void *priv)
324 SMB_STRUCT_STAT sbuf;
325 char **lines = NULL;
326 struct us_priv_info *pi = (struct us_priv_info *)priv;
327 TALLOC_CTX *ctx = pi->ctx;
328 struct net_context *c = pi->c;
329 int fd = -1;
330 int numlines = 0;
331 SEC_DESC *psd = NULL;
332 char *basepath;
333 char *sharepath = NULL;
334 char *comment = NULL;
335 char *acl_str;
336 int num_aces;
337 char sep_str[2];
338 enum usershare_err us_err;
339 bool guest_ok = false;
341 sep_str[0] = *lp_winbind_separator();
342 sep_str[1] = '\0';
344 basepath = get_basepath(ctx);
345 if (!basepath) {
346 return -1;
348 basepath = talloc_asprintf_append(basepath,
349 "/%s",
350 fl->pathname);
351 if (!basepath) {
352 return -1;
355 #ifdef O_NOFOLLOW
356 fd = sys_open(basepath, O_RDONLY|O_NOFOLLOW, 0);
357 #else
358 fd = sys_open(basepath, O_RDONLY, 0);
359 #endif
361 if (fd == -1) {
362 d_fprintf(stderr, _("info_fn: unable to open %s. %s\n"),
363 basepath, strerror(errno) );
364 return -1;
367 /* Paranoia... */
368 if (sys_fstat(fd, &sbuf, false) != 0) {
369 d_fprintf(stderr,
370 _("info_fn: can't fstat file %s. Error was %s\n"),
371 basepath, strerror(errno) );
372 close(fd);
373 return -1;
376 if (!S_ISREG(sbuf.st_ex_mode)) {
377 d_fprintf(stderr,
378 _("info_fn: file %s is not a regular file. Ignoring.\n"),
379 basepath );
380 close(fd);
381 return -1;
384 lines = fd_lines_load(fd, &numlines, 10240, NULL);
385 close(fd);
387 if (lines == NULL) {
388 return -1;
391 /* Ensure it's well formed. */
392 us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
393 &sharepath,
394 &comment,
395 &psd,
396 &guest_ok);
398 TALLOC_FREE(lines);
400 if (us_err != USERSHARE_OK) {
401 d_fprintf(stderr,
402 _("info_fn: file %s is not a well formed usershare "
403 "file.\n"),
404 basepath );
405 d_fprintf(stderr, _("info_fn: Error was %s.\n"),
406 get_us_error_code(us_err) );
407 return -1;
410 acl_str = talloc_strdup(ctx, "usershare_acl=");
411 if (!acl_str) {
412 return -1;
415 for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
416 const char *domain;
417 const char *name;
418 NTSTATUS ntstatus;
420 ntstatus = net_lookup_name_from_sid(c, ctx,
421 &psd->dacl->aces[num_aces].trustee,
422 &domain, &name);
424 if (NT_STATUS_IS_OK(ntstatus)) {
425 if (domain && *domain) {
426 acl_str = talloc_asprintf_append(acl_str,
427 "%s%s",
428 domain,
429 sep_str);
430 if (!acl_str) {
431 return -1;
434 acl_str = talloc_asprintf_append(acl_str,
435 "%s",
436 name);
437 if (!acl_str) {
438 return -1;
441 } else {
442 fstring sidstr;
443 sid_to_fstring(sidstr,
444 &psd->dacl->aces[num_aces].trustee);
445 acl_str = talloc_asprintf_append(acl_str,
446 "%s",
447 sidstr);
448 if (!acl_str) {
449 return -1;
452 acl_str = talloc_asprintf_append(acl_str, ":");
453 if (!acl_str) {
454 return -1;
457 if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
458 acl_str = talloc_asprintf_append(acl_str, "D,");
459 if (!acl_str) {
460 return -1;
462 } else {
463 if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
464 acl_str = talloc_asprintf_append(acl_str, "F,");
465 } else {
466 acl_str = talloc_asprintf_append(acl_str, "R,");
468 if (!acl_str) {
469 return -1;
474 /* NOTE: This is smb.conf-like output. Do not translate. */
475 if (pi->op == US_INFO_OP) {
476 d_printf("[%s]\n", fl->pathname );
477 d_printf("path=%s\n", sharepath );
478 d_printf("comment=%s\n", comment);
479 d_printf("%s\n", acl_str);
480 d_printf("guest_ok=%c\n\n", guest_ok ? 'y' : 'n');
481 } else if (pi->op == US_LIST_OP) {
482 d_printf("%s\n", fl->pathname);
485 return 0;
488 /***************************************************************************
489 Print out info (internal detail) on userlevel shares.
490 ***************************************************************************/
492 static int net_usershare_info(struct net_context *c, int argc, const char **argv)
494 fstring wcard;
495 bool only_ours = true;
496 int ret = -1;
497 struct us_priv_info pi;
498 TALLOC_CTX *ctx;
500 fstrcpy(wcard, "*");
502 if (c->display_usage)
503 return net_usershare_info_usage(c, argc, argv);
505 if (c->opt_long_list_entries) {
506 only_ours = false;
509 switch (argc) {
510 case 0:
511 break;
512 case 1:
513 fstrcpy(wcard, argv[0]);
514 break;
515 default:
516 return net_usershare_info_usage(c, argc, argv);
519 strlower_m(wcard);
521 ctx = talloc_init("share_info");
522 ret = get_share_list(ctx, wcard, only_ours);
523 if (ret) {
524 return ret;
527 pi.ctx = ctx;
528 pi.op = US_INFO_OP;
529 pi.c = c;
531 ret = process_share_list(info_fn, &pi);
532 talloc_destroy(ctx);
533 return ret;
536 /***************************************************************************
537 Count the current total number of usershares.
538 ***************************************************************************/
540 static int count_num_usershares(void)
542 SMB_STRUCT_DIR *dp;
543 SMB_STRUCT_DIRENT *de;
544 int num_usershares = 0;
545 TALLOC_CTX *ctx = talloc_tos();
546 char *basepath = get_basepath(ctx);
548 if (!basepath) {
549 return -1;
552 dp = sys_opendir(basepath);
553 if (!dp) {
554 d_fprintf(stderr,
555 _("count_num_usershares: cannot open usershare "
556 "directory %s. Error %s\n"),
557 basepath, strerror(errno) );
558 return -1;
561 while((de = sys_readdir(dp)) != 0) {
562 SMB_STRUCT_STAT sbuf;
563 char *path;
564 const char *n = de->d_name;
566 /* Ignore . and .. */
567 if (*n == '.') {
568 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
569 continue;
573 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
574 d_fprintf(stderr,
575 _("count_num_usershares: ignoring bad share "
576 "name %s\n"), n);
577 continue;
579 path = talloc_asprintf(ctx,
580 "%s/%s",
581 basepath,
583 if (!path) {
584 sys_closedir(dp);
585 return -1;
588 if (sys_lstat(path, &sbuf, false) != 0) {
589 d_fprintf(stderr,
590 _("count_num_usershares: can't lstat file %s. "
591 "Error was %s\n"),
592 path, strerror(errno) );
593 continue;
596 if (!S_ISREG(sbuf.st_ex_mode)) {
597 d_fprintf(stderr,
598 _("count_num_usershares: file %s is not a "
599 "regular file. Ignoring.\n"),
600 path );
601 continue;
603 num_usershares++;
606 sys_closedir(dp);
607 return num_usershares;
610 /***************************************************************************
611 Add a single userlevel share.
612 ***************************************************************************/
614 static int net_usershare_add(struct net_context *c, int argc, const char **argv)
616 TALLOC_CTX *ctx = talloc_stackframe();
617 SMB_STRUCT_STAT sbuf;
618 SMB_STRUCT_STAT lsbuf;
619 char *sharename;
620 char *full_path;
621 char *full_path_tmp;
622 const char *us_path;
623 const char *us_comment;
624 const char *arg_acl;
625 char *us_acl;
626 char *file_img;
627 int num_aces = 0;
628 int i;
629 int tmpfd;
630 const char *pacl;
631 size_t to_write;
632 uid_t myeuid = geteuid();
633 bool guest_ok = false;
634 int num_usershares;
636 us_comment = "";
637 arg_acl = "S-1-1-0:R";
639 if (c->display_usage)
640 return net_usershare_add_usage(c, argc, argv);
642 switch (argc) {
643 case 0:
644 case 1:
645 default:
646 return net_usershare_add_usage(c, argc, argv);
647 case 2:
648 sharename = strlower_talloc(ctx, argv[0]);
649 us_path = argv[1];
650 break;
651 case 3:
652 sharename = strlower_talloc(ctx, argv[0]);
653 us_path = argv[1];
654 us_comment = argv[2];
655 break;
656 case 4:
657 sharename = strlower_talloc(ctx, argv[0]);
658 us_path = argv[1];
659 us_comment = argv[2];
660 arg_acl = argv[3];
661 break;
662 case 5:
663 sharename = strlower_talloc(ctx, argv[0]);
664 us_path = argv[1];
665 us_comment = argv[2];
666 arg_acl = argv[3];
667 if (strlen(arg_acl) == 0) {
668 arg_acl = "S-1-1-0:R";
670 if (!strnequal(argv[4], "guest_ok=", 9)) {
671 TALLOC_FREE(ctx);
672 return net_usershare_add_usage(c, argc, argv);
674 switch (argv[4][9]) {
675 case 'y':
676 case 'Y':
677 guest_ok = true;
678 break;
679 case 'n':
680 case 'N':
681 guest_ok = false;
682 break;
683 default:
684 TALLOC_FREE(ctx);
685 return net_usershare_add_usage(c, argc, argv);
687 break;
690 /* Ensure we're under the "usershare max shares" number. Advisory only. */
691 num_usershares = count_num_usershares();
692 if (num_usershares >= lp_usershare_max_shares()) {
693 d_fprintf(stderr,
694 _("net usershare add: maximum number of allowed "
695 "usershares (%d) reached\n"),
696 lp_usershare_max_shares() );
697 TALLOC_FREE(ctx);
698 return -1;
701 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
702 d_fprintf(stderr, _("net usershare add: share name %s contains "
703 "invalid characters (any of %s)\n"),
704 sharename, INVALID_SHARENAME_CHARS);
705 TALLOC_FREE(ctx);
706 return -1;
709 /* Disallow shares the same as users. */
710 if (getpwnam(sharename)) {
711 d_fprintf(stderr,
712 _("net usershare add: share name %s is already a valid "
713 "system user name\n"),
714 sharename );
715 TALLOC_FREE(ctx);
716 return -1;
719 /* Construct the full path for the usershare file. */
720 full_path = get_basepath(ctx);
721 if (!full_path) {
722 TALLOC_FREE(ctx);
723 return -1;
725 full_path_tmp = talloc_asprintf(ctx,
726 "%s/:tmpXXXXXX",
727 full_path);
728 if (!full_path_tmp) {
729 TALLOC_FREE(ctx);
730 return -1;
733 full_path = talloc_asprintf_append(full_path,
734 "/%s",
735 sharename);
736 if (!full_path) {
737 TALLOC_FREE(ctx);
738 return -1;
741 /* The path *must* be absolute. */
742 if (us_path[0] != '/') {
743 d_fprintf(stderr,
744 _("net usershare add: path %s is not an absolute "
745 "path.\n"),
746 us_path);
747 TALLOC_FREE(ctx);
748 return -1;
751 /* Check the directory to be shared exists. */
752 if (sys_stat(us_path, &sbuf, false) != 0) {
753 d_fprintf(stderr,
754 _("net usershare add: cannot stat path %s to ensure "
755 "this is a directory. Error was %s\n"),
756 us_path, strerror(errno) );
757 TALLOC_FREE(ctx);
758 return -1;
761 if (!S_ISDIR(sbuf.st_ex_mode)) {
762 d_fprintf(stderr,
763 _("net usershare add: path %s is not a directory.\n"),
764 us_path );
765 TALLOC_FREE(ctx);
766 return -1;
769 /* If we're not root, check if we're restricted to sharing out directories
770 that we own only. */
772 if ((myeuid != 0) && lp_usershare_owner_only() && (myeuid != sbuf.st_ex_uid)) {
773 d_fprintf(stderr, _("net usershare add: cannot share path %s as "
774 "we are restricted to only sharing directories we own.\n"
775 "\tAsk the administrator to add the line \"usershare owner only = false\" \n"
776 "\tto the [global] section of the smb.conf to allow this.\n"),
777 us_path );
778 TALLOC_FREE(ctx);
779 return -1;
782 /* No validation needed on comment. Now go through and validate the
783 acl string. Convert names to SID's as needed. Then run it through
784 parse_usershare_acl to ensure it's valid. */
786 /* Start off the string we'll append to. */
787 us_acl = talloc_strdup(ctx, "");
788 if (!us_acl) {
789 TALLOC_FREE(ctx);
790 return -1;
793 pacl = arg_acl;
794 num_aces = 1;
796 /* Add the number of ',' characters to get the number of aces. */
797 num_aces += count_chars(pacl,',');
799 for (i = 0; i < num_aces; i++) {
800 DOM_SID sid;
801 const char *pcolon = strchr_m(pacl, ':');
802 const char *name;
804 if (pcolon == NULL) {
805 d_fprintf(stderr,
806 _("net usershare add: malformed acl %s "
807 "(missing ':').\n"),
808 pacl );
809 TALLOC_FREE(ctx);
810 return -1;
813 switch(pcolon[1]) {
814 case 'f':
815 case 'F':
816 case 'd':
817 case 'r':
818 case 'R':
819 break;
820 default:
821 d_fprintf(stderr,
822 _("net usershare add: malformed acl %s "
823 "(access control must be 'r', 'f', "
824 "or 'd')\n"),
825 pacl );
826 TALLOC_FREE(ctx);
827 return -1;
830 if (pcolon[2] != ',' && pcolon[2] != '\0') {
831 d_fprintf(stderr,
832 _("net usershare add: malformed terminating "
833 "character for acl %s\n"),
834 pacl );
835 TALLOC_FREE(ctx);
836 return -1;
839 /* Get the name */
840 if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
841 d_fprintf(stderr, _("talloc_strndup failed\n"));
842 TALLOC_FREE(ctx);
843 return -1;
845 if (!string_to_sid(&sid, name)) {
846 /* Convert to a SID */
847 NTSTATUS ntstatus = net_lookup_sid_from_name(c, ctx, name, &sid);
848 if (!NT_STATUS_IS_OK(ntstatus)) {
849 d_fprintf(stderr,
850 _("net usershare add: cannot convert "
851 "name \"%s\" to a SID. %s."),
852 name, get_friendly_nt_error_msg(ntstatus) );
853 if (NT_STATUS_EQUAL(ntstatus, NT_STATUS_CONNECTION_REFUSED)) {
854 d_fprintf(stderr,
855 _(" Maybe smbd is not running.\n"));
856 } else {
857 d_fprintf(stderr, "\n");
859 TALLOC_FREE(ctx);
860 return -1;
863 us_acl = talloc_asprintf_append(
864 us_acl, "%s:%c,", sid_string_tos(&sid), pcolon[1]);
866 /* Move to the next ACL entry. */
867 if (pcolon[2] == ',') {
868 pacl = &pcolon[3];
872 /* Remove the last ',' */
873 us_acl[strlen(us_acl)-1] = '\0';
875 if (guest_ok && !lp_usershare_allow_guests()) {
876 d_fprintf(stderr, _("net usershare add: guest_ok=y requested "
877 "but the \"usershare allow guests\" parameter is not "
878 "enabled by this server.\n"));
879 TALLOC_FREE(ctx);
880 return -1;
883 /* Create a temporary filename for this share. */
884 tmpfd = mkstemp(full_path_tmp);
886 if (tmpfd == -1) {
887 d_fprintf(stderr,
888 _("net usershare add: cannot create tmp file %s\n"),
889 full_path_tmp );
890 TALLOC_FREE(ctx);
891 return -1;
894 /* Ensure we opened the file we thought we did. */
895 if (sys_lstat(full_path_tmp, &lsbuf, false) != 0) {
896 d_fprintf(stderr,
897 _("net usershare add: cannot lstat tmp file %s\n"),
898 full_path_tmp );
899 TALLOC_FREE(ctx);
900 return -1;
903 /* Check this is the same as the file we opened. */
904 if (sys_fstat(tmpfd, &sbuf, false) != 0) {
905 d_fprintf(stderr,
906 _("net usershare add: cannot fstat tmp file %s\n"),
907 full_path_tmp );
908 TALLOC_FREE(ctx);
909 return -1;
912 if (!S_ISREG(sbuf.st_ex_mode) || sbuf.st_ex_dev != lsbuf.st_ex_dev || sbuf.st_ex_ino != lsbuf.st_ex_ino) {
913 d_fprintf(stderr,
914 _("net usershare add: tmp file %s is not a regular "
915 "file ?\n"),
916 full_path_tmp );
917 TALLOC_FREE(ctx);
918 return -1;
921 if (fchmod(tmpfd, 0644) == -1) {
922 d_fprintf(stderr,
923 _("net usershare add: failed to fchmod tmp file %s "
924 "to 0644n"),
925 full_path_tmp );
926 TALLOC_FREE(ctx);
927 return -1;
930 /* Create the in-memory image of the file. */
931 file_img = talloc_strdup(ctx, "#VERSION 2\npath=");
932 file_img = talloc_asprintf_append(file_img, "%s\ncomment=%s\nusershare_acl=%s\nguest_ok=%c\n",
933 us_path, us_comment, us_acl, guest_ok ? 'y' : 'n');
935 to_write = strlen(file_img);
937 if (write(tmpfd, file_img, to_write) != to_write) {
938 d_fprintf(stderr,
939 _("net usershare add: failed to write %u bytes to "
940 "file %s. Error was %s\n"),
941 (unsigned int)to_write, full_path_tmp, strerror(errno));
942 unlink(full_path_tmp);
943 TALLOC_FREE(ctx);
944 return -1;
947 /* Attempt to replace any existing share by this name. */
948 if (rename(full_path_tmp, full_path) != 0) {
949 unlink(full_path_tmp);
950 d_fprintf(stderr,
951 _("net usershare add: failed to add share %s. Error "
952 "was %s\n"),
953 sharename, strerror(errno));
954 TALLOC_FREE(ctx);
955 close(tmpfd);
956 return -1;
959 close(tmpfd);
961 if (c->opt_long_list_entries) {
962 const char *my_argv[2];
963 my_argv[0] = sharename;
964 my_argv[1] = NULL;
965 net_usershare_info(c, 1, my_argv);
968 TALLOC_FREE(ctx);
969 return 0;
972 #if 0
973 /***************************************************************************
974 List function.
975 ***************************************************************************/
977 static int list_fn(struct file_list *fl, void *priv)
979 d_printf("%s\n", fl->pathname);
980 return 0;
982 #endif
984 /***************************************************************************
985 List userlevel shares.
986 ***************************************************************************/
988 static int net_usershare_list(struct net_context *c, int argc,
989 const char **argv)
991 fstring wcard;
992 bool only_ours = true;
993 int ret = -1;
994 struct us_priv_info pi;
995 TALLOC_CTX *ctx;
997 fstrcpy(wcard, "*");
999 if (c->display_usage)
1000 return net_usershare_list_usage(c, argc, argv);
1002 if (c->opt_long_list_entries) {
1003 only_ours = false;
1006 switch (argc) {
1007 case 0:
1008 break;
1009 case 1:
1010 fstrcpy(wcard, argv[0]);
1011 break;
1012 default:
1013 return net_usershare_list_usage(c, argc, argv);
1016 strlower_m(wcard);
1018 ctx = talloc_init("share_list");
1019 ret = get_share_list(ctx, wcard, only_ours);
1020 if (ret) {
1021 return ret;
1024 pi.ctx = ctx;
1025 pi.op = US_LIST_OP;
1026 pi.c = c;
1028 ret = process_share_list(info_fn, &pi);
1029 talloc_destroy(ctx);
1030 return ret;
1033 /***************************************************************************
1034 Entry-point for all the USERSHARE functions.
1035 ***************************************************************************/
1037 int net_usershare(struct net_context *c, int argc, const char **argv)
1039 SMB_STRUCT_DIR *dp;
1041 struct functable func[] = {
1043 "add",
1044 net_usershare_add,
1045 NET_TRANSPORT_LOCAL,
1046 N_("Add/modify user defined share"),
1047 N_("net usershare add\n"
1048 " Add/modify user defined share")
1051 "delete",
1052 net_usershare_delete,
1053 NET_TRANSPORT_LOCAL,
1054 N_("Delete user defined share"),
1055 N_("net usershare delete\n"
1056 " Delete user defined share")
1059 "info",
1060 net_usershare_info,
1061 NET_TRANSPORT_LOCAL,
1062 N_("Display information about a user defined share"),
1063 N_("net usershare info\n"
1064 " Display information about a user defined share")
1067 "list",
1068 net_usershare_list,
1069 NET_TRANSPORT_LOCAL,
1070 N_("List user defined shares"),
1071 N_("net usershare list\n"
1072 " List user defined shares")
1074 {NULL, NULL, 0, NULL, NULL}
1077 if (lp_usershare_max_shares() == 0) {
1078 d_fprintf(stderr,
1079 _("net usershare: usershares are currently "
1080 "disabled\n"));
1081 return -1;
1084 dp = sys_opendir(lp_usershare_path());
1085 if (!dp) {
1086 int err = errno;
1087 d_fprintf(stderr,
1088 _("net usershare: cannot open usershare directory %s. "
1089 "Error %s\n"),
1090 lp_usershare_path(), strerror(err) );
1091 if (err == EACCES) {
1092 d_fprintf(stderr,
1093 _("You do not have permission to create a "
1094 "usershare. Ask your administrator to grant "
1095 "you permissions to create a share.\n"));
1096 } else if (err == ENOENT) {
1097 d_fprintf(stderr,
1098 _("Please ask your system administrator to "
1099 "enable user sharing.\n"));
1101 return -1;
1103 sys_closedir(dp);
1105 return net_run_function(c, argc, argv, "net usershare", func);