Workaround for KB932762
[Samba.git] / source / utils / net_usershare.c
blobe867f4c9409a3532ca5a28d61d1f13f82a5eedb5
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 {"Malformed usershare file", USERSHARE_MALFORMED_FILE},
30 {"Bad version number", USERSHARE_BAD_VERSION},
31 {"Malformed path entry", USERSHARE_MALFORMED_PATH},
32 {"Malformed comment entryfile", USERSHARE_MALFORMED_COMMENT_DEF},
33 {"Malformed acl definition", USERSHARE_MALFORMED_ACL_DEF},
34 {"Acl parse error", USERSHARE_ACL_ERR},
35 {"Path not absolute", USERSHARE_PATH_NOT_ABSOLUTE},
36 {"Path is denied", USERSHARE_PATH_IS_DENIED},
37 {"Path not allowed", USERSHARE_PATH_NOT_ALLOWED},
38 {"Path is not a directory", USERSHARE_PATH_NOT_DIRECTORY},
39 {"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 static pstring out;
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 slprintf(out, sizeof(out), "Usershare error code (0x%x)", (unsigned int)us_err);
56 return out;
59 /* The help subsystem for the USERSHARE subcommand */
61 static int net_usershare_add_usage(int argc, const char **argv)
63 char c = *lp_winbind_separator();
64 d_printf(
65 "net usershare add [-l|--long] <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>]\n"
66 "\tAdds the specified share name for this user.\n"
67 "\t<sharename> is the new share name.\n"
68 "\t<path> is the path on the filesystem to export.\n"
69 "\t<comment> is the optional comment for the new share.\n"
70 "\t<acl> is an optional share acl in the format \"DOMAIN%cname:X,DOMAIN%cname:X,....\"\n"
71 "\t<guest_ok=y> if present sets \"guest ok = yes\" on this usershare.\n"
72 "\t\t\"X\" represents a permission and can be any one of the characters f, r or d\n"
73 "\t\twhere \"f\" means full control, \"r\" means read-only, \"d\" means deny access.\n"
74 "\t\tname may be a domain user or group. For local users use the local server name "
75 "instead of \"DOMAIN\"\n"
76 "\t\tThe default acl is \"Everyone:r\" which allows everyone read-only access.\n"
77 "\tAdd -l or --long to print the info on the newly added share.\n",
78 c, c );
79 return -1;
82 static int net_usershare_delete_usage(int argc, const char **argv)
84 d_printf(
85 "net usershare delete <sharename>\n"\
86 "\tdeletes the specified share name for this user.\n");
87 return -1;
90 static int net_usershare_info_usage(int argc, const char **argv)
92 d_printf(
93 "net usershare info [-l|--long] [wildcard sharename]\n"\
94 "\tPrints out the path, comment and acl elements of shares that match the wildcard.\n"
95 "\tBy default only gives info on shares owned by the current user\n"
96 "\tAdd -l or --long to apply this to all shares\n"
97 "\tOmit the sharename or use a wildcard of '*' to see all shares\n");
98 return -1;
101 static int net_usershare_list_usage(int argc, const char **argv)
103 d_printf(
104 "net usershare list [-l|--long] [wildcard sharename]\n"\
105 "\tLists the names of all shares that match the wildcard.\n"
106 "\tBy default only lists shares owned by the current user\n"
107 "\tAdd -l or --long to apply this to all shares\n"
108 "\tOmit the sharename or use a wildcard of '*' to see all shares\n");
109 return -1;
112 int net_usershare_usage(int argc, const char **argv)
114 d_printf("net usershare add <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>] to "
115 "add or change a user defined share.\n"
116 "net usershare delete <sharename> to delete a user defined share.\n"
117 "net usershare info [-l|--long] [wildcard sharename] to print info about a user defined share.\n"
118 "net usershare list [-l|--long] [wildcard sharename] to list user defined shares.\n"
119 "net usershare help\n"\
120 "\nType \"net usershare help <option>\" to get more information on that option\n\n");
122 net_common_flags_usage(argc, argv);
123 return -1;
126 /***************************************************************************
127 ***************************************************************************/
129 static void get_basepath(pstring basepath)
131 pstrcpy(basepath, lp_usershare_path());
132 if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
133 basepath[strlen(basepath)-1] = '\0';
137 /***************************************************************************
138 Delete a single userlevel share.
139 ***************************************************************************/
141 static int net_usershare_delete(int argc, const char **argv)
143 pstring us_path;
144 char *sharename;
146 if (argc != 1) {
147 return net_usershare_delete_usage(argc, argv);
150 if ((sharename = strdup_lower(argv[0])) == NULL) {
151 d_fprintf(stderr, "strdup failed\n");
152 return -1;
155 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
156 d_fprintf(stderr, "net usershare delete: share name %s contains "
157 "invalid characters (any of %s)\n",
158 sharename, INVALID_SHARENAME_CHARS);
159 SAFE_FREE(sharename);
160 return -1;
163 pstrcpy(us_path, lp_usershare_path());
164 pstrcat(us_path, "/");
165 pstrcat(us_path, sharename);
167 if (unlink(us_path) != 0) {
168 d_fprintf(stderr, "net usershare delete: unable to remove usershare %s. "
169 "Error was %s\n",
170 us_path, strerror(errno));
171 SAFE_FREE(sharename);
172 return -1;
174 SAFE_FREE(sharename);
175 return 0;
178 /***************************************************************************
179 Data structures to handle a list of usershare files.
180 ***************************************************************************/
182 struct file_list {
183 struct file_list *next, *prev;
184 const char *pathname;
187 static struct file_list *flist;
189 /***************************************************************************
190 ***************************************************************************/
192 static int get_share_list(TALLOC_CTX *ctx, const char *wcard, BOOL only_ours)
194 SMB_STRUCT_DIR *dp;
195 SMB_STRUCT_DIRENT *de;
196 uid_t myuid = geteuid();
197 struct file_list *fl = NULL;
198 pstring basepath;
200 get_basepath(basepath);
201 dp = sys_opendir(basepath);
202 if (!dp) {
203 d_fprintf(stderr, "get_share_list: cannot open usershare directory %s. Error %s\n",
204 basepath, strerror(errno) );
205 return -1;
208 while((de = sys_readdir(dp)) != 0) {
209 SMB_STRUCT_STAT sbuf;
210 pstring path;
211 const char *n = de->d_name;
213 /* Ignore . and .. */
214 if (*n == '.') {
215 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
216 continue;
220 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
221 d_fprintf(stderr, "get_share_list: ignoring bad share name %s\n",n);
222 continue;
224 pstrcpy(path, basepath);
225 pstrcat(path, "/");
226 pstrcat(path, n);
228 if (sys_lstat(path, &sbuf) != 0) {
229 d_fprintf(stderr, "get_share_list: can't lstat file %s. Error was %s\n",
230 path, strerror(errno) );
231 continue;
234 if (!S_ISREG(sbuf.st_mode)) {
235 d_fprintf(stderr, "get_share_list: file %s is not a regular file. Ignoring.\n",
236 path );
237 continue;
240 if (only_ours && sbuf.st_uid != myuid) {
241 continue;
244 if (!unix_wild_match(wcard, n)) {
245 continue;
248 /* (Finally) - add to list. */
249 fl = TALLOC_P(ctx, struct file_list);
250 if (!fl) {
251 return -1;
253 fl->pathname = talloc_strdup(ctx, n);
254 if (!fl->pathname) {
255 return -1;
258 DLIST_ADD(flist, fl);
261 sys_closedir(dp);
262 return 0;
265 enum us_priv_op { US_LIST_OP, US_INFO_OP};
267 struct us_priv_info {
268 TALLOC_CTX *ctx;
269 enum us_priv_op op;
272 /***************************************************************************
273 Call a function for every share on the list.
274 ***************************************************************************/
276 static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
278 struct file_list *fl;
279 int ret = 0;
281 for (fl = flist; fl; fl = fl->next) {
282 ret = (*fn)(fl, priv);
285 return ret;
288 /***************************************************************************
289 Info function.
290 ***************************************************************************/
292 static int info_fn(struct file_list *fl, void *priv)
294 SMB_STRUCT_STAT sbuf;
295 char **lines = NULL;
296 struct us_priv_info *pi = (struct us_priv_info *)priv;
297 TALLOC_CTX *ctx = pi->ctx;
298 int fd = -1;
299 int numlines = 0;
300 SEC_DESC *psd = NULL;
301 pstring basepath;
302 pstring sharepath;
303 pstring comment;
304 pstring acl_str;
305 int num_aces;
306 char sep_str[2];
307 enum usershare_err us_err;
308 BOOL guest_ok = False;
310 sep_str[0] = *lp_winbind_separator();
311 sep_str[1] = '\0';
313 get_basepath(basepath);
314 pstrcat(basepath, "/");
315 pstrcat(basepath, fl->pathname);
317 #ifdef O_NOFOLLOW
318 fd = sys_open(basepath, O_RDONLY|O_NOFOLLOW, 0);
319 #else
320 fd = sys_open(basepath, O_RDONLY, 0);
321 #endif
323 if (fd == -1) {
324 d_fprintf(stderr, "info_fn: unable to open %s. %s\n",
325 basepath, strerror(errno) );
326 return -1;
329 /* Paranoia... */
330 if (sys_fstat(fd, &sbuf) != 0) {
331 d_fprintf(stderr, "info_fn: can't fstat file %s. Error was %s\n",
332 basepath, strerror(errno) );
333 close(fd);
334 return -1;
337 if (!S_ISREG(sbuf.st_mode)) {
338 d_fprintf(stderr, "info_fn: file %s is not a regular file. Ignoring.\n",
339 basepath );
340 close(fd);
341 return -1;
344 lines = fd_lines_load(fd, &numlines, 10240);
345 close(fd);
347 if (lines == NULL) {
348 return -1;
351 /* Ensure it's well formed. */
352 us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
353 sharepath,
354 comment,
355 &psd,
356 &guest_ok);
358 file_lines_free(lines);
360 if (us_err != USERSHARE_OK) {
361 d_fprintf(stderr, "info_fn: file %s is not a well formed usershare file.\n",
362 basepath );
363 d_fprintf(stderr, "info_fn: Error was %s.\n",
364 get_us_error_code(us_err) );
365 return -1;
368 pstrcpy(acl_str, "usershare_acl=");
370 for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
371 const char *domain;
372 const char *name;
373 NTSTATUS ntstatus;
375 ntstatus = net_lookup_name_from_sid(ctx, &psd->dacl->aces[num_aces].trustee, &domain, &name);
377 if (NT_STATUS_IS_OK(ntstatus)) {
378 if (domain && *domain) {
379 pstrcat(acl_str, domain);
380 pstrcat(acl_str, sep_str);
382 pstrcat(acl_str,name);
383 } else {
384 fstring sidstr;
385 sid_to_string(sidstr, &psd->dacl->aces[num_aces].trustee);
386 pstrcat(acl_str,sidstr);
388 pstrcat(acl_str, ":");
390 if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
391 pstrcat(acl_str, "D,");
392 } else {
393 if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
394 pstrcat(acl_str, "F,");
395 } else {
396 pstrcat(acl_str, "R,");
401 acl_str[strlen(acl_str)-1] = '\0';
403 if (pi->op == US_INFO_OP) {
404 d_printf("[%s]\n", fl->pathname );
405 d_printf("path=%s\n", sharepath );
406 d_printf("comment=%s\n", comment);
407 d_printf("%s\n", acl_str);
408 d_printf("guest_ok=%c\n\n", guest_ok ? 'y' : 'n');
409 } else if (pi->op == US_LIST_OP) {
410 d_printf("%s\n", fl->pathname);
413 return 0;
416 /***************************************************************************
417 Print out info (internal detail) on userlevel shares.
418 ***************************************************************************/
420 static int net_usershare_info(int argc, const char **argv)
422 fstring wcard;
423 BOOL only_ours = True;
424 int ret = -1;
425 struct us_priv_info pi;
426 TALLOC_CTX *ctx;
428 fstrcpy(wcard, "*");
430 if (opt_long_list_entries) {
431 only_ours = False;
434 switch (argc) {
435 case 0:
436 break;
437 case 1:
438 fstrcpy(wcard, argv[0]);
439 break;
440 default:
441 return net_usershare_info_usage(argc, argv);
444 strlower_m(wcard);
446 ctx = talloc_init("share_info");
447 ret = get_share_list(ctx, wcard, only_ours);
448 if (ret) {
449 return ret;
452 pi.ctx = ctx;
453 pi.op = US_INFO_OP;
455 ret = process_share_list(info_fn, &pi);
456 talloc_destroy(ctx);
457 return ret;
460 /***************************************************************************
461 Count the current total number of usershares.
462 ***************************************************************************/
464 static int count_num_usershares(void)
466 SMB_STRUCT_DIR *dp;
467 SMB_STRUCT_DIRENT *de;
468 pstring basepath;
469 int num_usershares = 0;
471 get_basepath(basepath);
472 dp = sys_opendir(basepath);
473 if (!dp) {
474 d_fprintf(stderr, "count_num_usershares: cannot open usershare directory %s. Error %s\n",
475 basepath, strerror(errno) );
476 return -1;
479 while((de = sys_readdir(dp)) != 0) {
480 SMB_STRUCT_STAT sbuf;
481 pstring path;
482 const char *n = de->d_name;
484 /* Ignore . and .. */
485 if (*n == '.') {
486 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
487 continue;
491 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
492 d_fprintf(stderr, "count_num_usershares: ignoring bad share name %s\n",n);
493 continue;
495 pstrcpy(path, basepath);
496 pstrcat(path, "/");
497 pstrcat(path, n);
499 if (sys_lstat(path, &sbuf) != 0) {
500 d_fprintf(stderr, "count_num_usershares: can't lstat file %s. Error was %s\n",
501 path, strerror(errno) );
502 continue;
505 if (!S_ISREG(sbuf.st_mode)) {
506 d_fprintf(stderr, "count_num_usershares: file %s is not a regular file. Ignoring.\n",
507 path );
508 continue;
510 num_usershares++;
513 sys_closedir(dp);
514 return num_usershares;
517 /***************************************************************************
518 Add a single userlevel share.
519 ***************************************************************************/
521 static int net_usershare_add(int argc, const char **argv)
523 TALLOC_CTX *ctx = NULL;
524 SMB_STRUCT_STAT sbuf;
525 SMB_STRUCT_STAT lsbuf;
526 char *sharename;
527 pstring full_path;
528 pstring full_path_tmp;
529 const char *us_path;
530 const char *us_comment;
531 const char *arg_acl;
532 char *us_acl;
533 char *file_img;
534 int num_aces = 0;
535 int i;
536 int tmpfd;
537 const char *pacl;
538 size_t to_write;
539 uid_t myeuid = geteuid();
540 BOOL guest_ok = False;
541 int num_usershares;
543 us_comment = "";
544 arg_acl = "S-1-1-0:R";
546 switch (argc) {
547 case 0:
548 case 1:
549 default:
550 return net_usershare_add_usage(argc, argv);
551 case 2:
552 sharename = strdup_lower(argv[0]);
553 us_path = argv[1];
554 break;
555 case 3:
556 sharename = strdup_lower(argv[0]);
557 us_path = argv[1];
558 us_comment = argv[2];
559 break;
560 case 4:
561 sharename = strdup_lower(argv[0]);
562 us_path = argv[1];
563 us_comment = argv[2];
564 arg_acl = argv[3];
565 break;
566 case 5:
567 sharename = strdup_lower(argv[0]);
568 us_path = argv[1];
569 us_comment = argv[2];
570 arg_acl = argv[3];
571 if (strlen(arg_acl) == 0) {
572 arg_acl = "S-1-1-0:R";
574 if (!strnequal(argv[4], "guest_ok=", 9)) {
575 return net_usershare_add_usage(argc, argv);
577 switch (argv[4][9]) {
578 case 'y':
579 case 'Y':
580 guest_ok = True;
581 break;
582 case 'n':
583 case 'N':
584 guest_ok = False;
585 break;
586 default:
587 return net_usershare_add_usage(argc, argv);
589 break;
592 /* Ensure we're under the "usershare max shares" number. Advisory only. */
593 num_usershares = count_num_usershares();
594 if (num_usershares >= lp_usershare_max_shares()) {
595 d_fprintf(stderr, "net usershare add: maximum number of allowed usershares (%d) reached\n",
596 lp_usershare_max_shares() );
597 SAFE_FREE(sharename);
598 return -1;
601 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
602 d_fprintf(stderr, "net usershare add: share name %s contains "
603 "invalid characters (any of %s)\n",
604 sharename, INVALID_SHARENAME_CHARS);
605 SAFE_FREE(sharename);
606 return -1;
609 /* Disallow shares the same as users. */
610 if (getpwnam(sharename)) {
611 d_fprintf(stderr, "net usershare add: share name %s is already a valid system user name\n",
612 sharename );
613 SAFE_FREE(sharename);
614 return -1;
617 /* Construct the full path for the usershare file. */
618 get_basepath(full_path);
619 pstrcat(full_path, "/");
620 pstrcpy(full_path_tmp, full_path);
621 pstrcat(full_path, sharename);
622 pstrcat(full_path_tmp, ":tmpXXXXXX");
624 /* The path *must* be absolute. */
625 if (us_path[0] != '/') {
626 d_fprintf(stderr,"net usershare add: path %s is not an absolute path.\n",
627 us_path);
628 SAFE_FREE(sharename);
629 return -1;
632 /* Check the directory to be shared exists. */
633 if (sys_stat(us_path, &sbuf) != 0) {
634 d_fprintf(stderr, "net usershare add: cannot stat path %s to ensure "
635 "this is a directory. Error was %s\n",
636 us_path, strerror(errno) );
637 SAFE_FREE(sharename);
638 return -1;
641 if (!S_ISDIR(sbuf.st_mode)) {
642 d_fprintf(stderr, "net usershare add: path %s is not a directory.\n",
643 us_path );
644 SAFE_FREE(sharename);
645 return -1;
648 /* If we're not root, check if we're restricted to sharing out directories
649 that we own only. */
651 if ((myeuid != 0) && lp_usershare_owner_only() && (myeuid != sbuf.st_uid)) {
652 d_fprintf(stderr, "net usershare add: cannot share path %s as "
653 "we are restricted to only sharing directories we own.\n"
654 "\tAsk the administrator to add the line \"usershare owner only = False\" \n"
655 "\tto the [global] section of the smb.conf to allow this.\n",
656 us_path );
657 SAFE_FREE(sharename);
658 return -1;
661 /* No validation needed on comment. Now go through and validate the
662 acl string. Convert names to SID's as needed. Then run it through
663 parse_usershare_acl to ensure it's valid. */
665 ctx = talloc_init("share_info");
667 /* Start off the string we'll append to. */
668 us_acl = talloc_strdup(ctx, "");
670 pacl = arg_acl;
671 num_aces = 1;
673 /* Add the number of ',' characters to get the number of aces. */
674 num_aces += count_chars(pacl,',');
676 for (i = 0; i < num_aces; i++) {
677 DOM_SID sid;
678 const char *pcolon = strchr_m(pacl, ':');
679 const char *name;
681 if (pcolon == NULL) {
682 d_fprintf(stderr, "net usershare add: malformed acl %s (missing ':').\n",
683 pacl );
684 talloc_destroy(ctx);
685 SAFE_FREE(sharename);
686 return -1;
689 switch(pcolon[1]) {
690 case 'f':
691 case 'F':
692 case 'd':
693 case 'r':
694 case 'R':
695 break;
696 default:
697 d_fprintf(stderr, "net usershare add: malformed acl %s "
698 "(access control must be 'r', 'f', or 'd')\n",
699 pacl );
700 talloc_destroy(ctx);
701 SAFE_FREE(sharename);
702 return -1;
705 if (pcolon[2] != ',' && pcolon[2] != '\0') {
706 d_fprintf(stderr, "net usershare add: malformed terminating character for acl %s\n",
707 pacl );
708 talloc_destroy(ctx);
709 SAFE_FREE(sharename);
710 return -1;
713 /* Get the name */
714 if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
715 d_fprintf(stderr, "talloc_strndup failed\n");
716 talloc_destroy(ctx);
717 SAFE_FREE(sharename);
718 return -1;
720 if (!string_to_sid(&sid, name)) {
721 /* Convert to a SID */
722 NTSTATUS ntstatus = net_lookup_sid_from_name(ctx, name, &sid);
723 if (!NT_STATUS_IS_OK(ntstatus)) {
724 d_fprintf(stderr, "net usershare add: cannot convert name \"%s\" to a SID. %s.",
725 name, get_friendly_nt_error_msg(ntstatus) );
726 if (NT_STATUS_EQUAL(ntstatus, NT_STATUS_CONNECTION_REFUSED)) {
727 d_fprintf(stderr, " Maybe smbd is not running.\n");
728 } else {
729 d_fprintf(stderr, "\n");
731 talloc_destroy(ctx);
732 SAFE_FREE(sharename);
733 return -1;
736 us_acl = talloc_asprintf_append(us_acl, "%s:%c,", sid_string_static(&sid), pcolon[1]);
738 /* Move to the next ACL entry. */
739 if (pcolon[2] == ',') {
740 pacl = &pcolon[3];
744 /* Remove the last ',' */
745 us_acl[strlen(us_acl)-1] = '\0';
747 if (guest_ok && !lp_usershare_allow_guests()) {
748 d_fprintf(stderr, "net usershare add: guest_ok=y requested "
749 "but the \"usershare allow guests\" parameter is not enabled "
750 "by this server.\n");
751 talloc_destroy(ctx);
752 SAFE_FREE(sharename);
753 return -1;
756 /* Create a temporary filename for this share. */
757 tmpfd = smb_mkstemp(full_path_tmp);
759 if (tmpfd == -1) {
760 d_fprintf(stderr, "net usershare add: cannot create tmp file %s\n",
761 full_path_tmp );
762 talloc_destroy(ctx);
763 SAFE_FREE(sharename);
764 return -1;
767 /* Ensure we opened the file we thought we did. */
768 if (sys_lstat(full_path_tmp, &lsbuf) != 0) {
769 d_fprintf(stderr, "net usershare add: cannot lstat tmp file %s\n",
770 full_path_tmp );
771 talloc_destroy(ctx);
772 SAFE_FREE(sharename);
773 return -1;
776 /* Check this is the same as the file we opened. */
777 if (sys_fstat(tmpfd, &sbuf) != 0) {
778 d_fprintf(stderr, "net usershare add: cannot fstat tmp file %s\n",
779 full_path_tmp );
780 talloc_destroy(ctx);
781 SAFE_FREE(sharename);
782 return -1;
785 if (!S_ISREG(sbuf.st_mode) || sbuf.st_dev != lsbuf.st_dev || sbuf.st_ino != lsbuf.st_ino) {
786 d_fprintf(stderr, "net usershare add: tmp file %s is not a regular file ?\n",
787 full_path_tmp );
788 talloc_destroy(ctx);
789 SAFE_FREE(sharename);
790 return -1;
793 if (fchmod(tmpfd, 0644) == -1) {
794 d_fprintf(stderr, "net usershare add: failed to fchmod tmp file %s to 0644n",
795 full_path_tmp );
796 talloc_destroy(ctx);
797 SAFE_FREE(sharename);
798 return -1;
801 /* Create the in-memory image of the file. */
802 file_img = talloc_strdup(ctx, "#VERSION 2\npath=");
803 file_img = talloc_asprintf_append(file_img, "%s\ncomment=%s\nusershare_acl=%s\nguest_ok=%c\n",
804 us_path, us_comment, us_acl, guest_ok ? 'y' : 'n');
806 to_write = strlen(file_img);
808 if (write(tmpfd, file_img, to_write) != to_write) {
809 d_fprintf(stderr, "net usershare add: failed to write %u bytes to file %s. Error was %s\n",
810 (unsigned int)to_write, full_path_tmp, strerror(errno));
811 unlink(full_path_tmp);
812 talloc_destroy(ctx);
813 SAFE_FREE(sharename);
814 return -1;
817 /* Attempt to replace any existing share by this name. */
818 if (rename(full_path_tmp, full_path) != 0) {
819 unlink(full_path_tmp);
820 d_fprintf(stderr, "net usershare add: failed to add share %s. Error was %s\n",
821 sharename, strerror(errno));
822 talloc_destroy(ctx);
823 close(tmpfd);
824 SAFE_FREE(sharename);
825 return -1;
828 close(tmpfd);
829 talloc_destroy(ctx);
831 if (opt_long_list_entries) {
832 const char *my_argv[2];
833 my_argv[0] = sharename;
834 my_argv[1] = NULL;
835 net_usershare_info(1, my_argv);
838 SAFE_FREE(sharename);
839 return 0;
842 #if 0
843 /***************************************************************************
844 List function.
845 ***************************************************************************/
847 static int list_fn(struct file_list *fl, void *priv)
849 d_printf("%s\n", fl->pathname);
850 return 0;
852 #endif
854 /***************************************************************************
855 List userlevel shares.
856 ***************************************************************************/
858 static int net_usershare_list(int argc, const char **argv)
860 fstring wcard;
861 BOOL only_ours = True;
862 int ret = -1;
863 struct us_priv_info pi;
864 TALLOC_CTX *ctx;
866 fstrcpy(wcard, "*");
868 if (opt_long_list_entries) {
869 only_ours = False;
872 switch (argc) {
873 case 0:
874 break;
875 case 1:
876 fstrcpy(wcard, argv[0]);
877 break;
878 default:
879 return net_usershare_list_usage(argc, argv);
882 strlower_m(wcard);
884 ctx = talloc_init("share_list");
885 ret = get_share_list(ctx, wcard, only_ours);
886 if (ret) {
887 return ret;
890 pi.ctx = ctx;
891 pi.op = US_LIST_OP;
893 ret = process_share_list(info_fn, &pi);
894 talloc_destroy(ctx);
895 return ret;
898 /***************************************************************************
899 Handle "net usershare help *" subcommands.
900 ***************************************************************************/
902 int net_usershare_help(int argc, const char **argv)
904 struct functable func[] = {
905 {"ADD", net_usershare_add_usage},
906 {"DELETE", net_usershare_delete_usage},
907 {"INFO", net_usershare_info_usage},
908 {"LIST", net_usershare_list_usage},
909 {NULL, NULL}};
911 return net_run_function(argc, argv, func, net_usershare_usage);
914 /***************************************************************************
915 Entry-point for all the USERSHARE functions.
916 ***************************************************************************/
918 int net_usershare(int argc, const char **argv)
920 SMB_STRUCT_DIR *dp;
922 struct functable func[] = {
923 {"ADD", net_usershare_add},
924 {"DELETE", net_usershare_delete},
925 {"INFO", net_usershare_info},
926 {"LIST", net_usershare_list},
927 {"HELP", net_usershare_help},
928 {NULL, NULL}
931 if (lp_usershare_max_shares() == 0) {
932 d_fprintf(stderr, "net usershare: usershares are currently disabled\n");
933 return -1;
936 dp = sys_opendir(lp_usershare_path());
937 if (!dp) {
938 int err = errno;
939 d_fprintf(stderr, "net usershare: cannot open usershare directory %s. Error %s\n",
940 lp_usershare_path(), strerror(err) );
941 if (err == EACCES) {
942 d_fprintf(stderr, "You do not have permission to create a usershare. Ask your "
943 "administrator to grant you permissions to create a share.\n");
944 } else if (err == ENOENT) {
945 d_fprintf(stderr, "Please ask your system administrator to "
946 "enable user sharing.\n");
948 return -1;
950 sys_closedir(dp);
952 return net_run_function(argc, argv, func, net_usershare_usage);