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 "utils/net.h"
24 const char *us_errstr;
25 enum usershare_err us_err;
28 {"Malformed usershare file", USERSHARE_MALFORMED_FILE},
29 {"Bad version number", USERSHARE_BAD_VERSION},
30 {"Malformed path entry", USERSHARE_MALFORMED_PATH},
31 {"Malformed comment entryfile", USERSHARE_MALFORMED_COMMENT_DEF},
32 {"Malformed acl definition", USERSHARE_MALFORMED_ACL_DEF},
33 {"Acl parse error", USERSHARE_ACL_ERR},
34 {"Path not absolute", USERSHARE_PATH_NOT_ABSOLUTE},
35 {"Path is denied", USERSHARE_PATH_IS_DENIED},
36 {"Path not allowed", USERSHARE_PATH_NOT_ALLOWED},
37 {"Path is not a directory", USERSHARE_PATH_NOT_DIRECTORY},
38 {"System error", USERSHARE_POSIX_ERR},
39 {NULL,(enum usershare_err)-1}
42 static const char *get_us_error_code(enum usershare_err us_err)
47 while (us_errs[idx].us_errstr != NULL) {
48 if (us_errs[idx].us_err == us_err) {
49 return us_errs[idx].us_errstr;
54 result = talloc_asprintf(talloc_tos(), "Usershare error code (0x%x)",
55 (unsigned int)us_err);
56 SMB_ASSERT(result != NULL);
60 /* The help subsystem for the USERSHARE subcommand */
62 static int net_usershare_add_usage(int argc, const char **argv)
64 char c = *lp_winbind_separator();
66 "net usershare add [-l|--long] <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>]\n"
67 "\tAdds the specified share name for this user.\n"
68 "\t<sharename> is the new share name.\n"
69 "\t<path> is the path on the filesystem to export.\n"
70 "\t<comment> is the optional comment for the new share.\n"
71 "\t<acl> is an optional share acl in the format \"DOMAIN%cname:X,DOMAIN%cname:X,....\"\n"
72 "\t<guest_ok=y> if present sets \"guest ok = yes\" on this usershare.\n"
73 "\t\t\"X\" represents a permission and can be any one of the characters f, r or d\n"
74 "\t\twhere \"f\" means full control, \"r\" means read-only, \"d\" means deny access.\n"
75 "\t\tname may be a domain user or group. For local users use the local server name "
76 "instead of \"DOMAIN\"\n"
77 "\t\tThe default acl is \"Everyone:r\" which allows everyone read-only access.\n"
78 "\tAdd -l or --long to print the info on the newly added share.\n",
83 static int net_usershare_delete_usage(int argc, const char **argv)
86 "net usershare delete <sharename>\n"\
87 "\tdeletes the specified share name for this user.\n");
91 static int net_usershare_info_usage(int argc, const char **argv)
94 "net usershare info [-l|--long] [wildcard sharename]\n"\
95 "\tPrints out the path, comment and acl elements of shares that match the wildcard.\n"
96 "\tBy default only gives info on shares owned by the current user\n"
97 "\tAdd -l or --long to apply this to all shares\n"
98 "\tOmit the sharename or use a wildcard of '*' to see all shares\n");
102 static int net_usershare_list_usage(int argc, const char **argv)
105 "net usershare list [-l|--long] [wildcard sharename]\n"\
106 "\tLists the names of all shares that match the wildcard.\n"
107 "\tBy default only lists shares owned by the current user\n"
108 "\tAdd -l or --long to apply this to all shares\n"
109 "\tOmit the sharename or use a wildcard of '*' to see all shares\n");
113 int net_usershare_usage(int argc, const char **argv)
115 d_printf("net usershare add <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>] to "
116 "add or change a user defined share.\n"
117 "net usershare delete <sharename> to delete a user defined share.\n"
118 "net usershare info [-l|--long] [wildcard sharename] to print info about a user defined share.\n"
119 "net usershare list [-l|--long] [wildcard sharename] to list user defined shares.\n"
120 "net usershare help\n"\
121 "\nType \"net usershare help <option>\" to get more information on that option\n\n");
123 net_common_flags_usage(argc, argv);
127 /***************************************************************************
128 ***************************************************************************/
130 static char *get_basepath(TALLOC_CTX *ctx)
132 char *basepath = talloc_strdup(ctx, lp_usershare_path());
137 if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
138 basepath[strlen(basepath)-1] = '\0';
143 /***************************************************************************
144 Delete a single userlevel share.
145 ***************************************************************************/
147 static int net_usershare_delete(int argc, const char **argv)
153 return net_usershare_delete_usage(argc, argv);
156 if ((sharename = strdup_lower(argv[0])) == NULL) {
157 d_fprintf(stderr, "strdup failed\n");
161 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
162 d_fprintf(stderr, "net usershare delete: share name %s contains "
163 "invalid characters (any of %s)\n",
164 sharename, INVALID_SHARENAME_CHARS);
165 SAFE_FREE(sharename);
169 us_path = talloc_asprintf(talloc_tos(),
174 SAFE_FREE(sharename);
178 if (unlink(us_path) != 0) {
179 d_fprintf(stderr, "net usershare delete: unable to remove usershare %s. "
181 us_path, strerror(errno));
182 SAFE_FREE(sharename);
185 SAFE_FREE(sharename);
189 /***************************************************************************
190 Data structures to handle a list of usershare files.
191 ***************************************************************************/
194 struct file_list *next, *prev;
195 const char *pathname;
198 static struct file_list *flist;
200 /***************************************************************************
201 ***************************************************************************/
203 static int get_share_list(TALLOC_CTX *ctx, const char *wcard, bool only_ours)
206 SMB_STRUCT_DIRENT *de;
207 uid_t myuid = geteuid();
208 struct file_list *fl = NULL;
209 char *basepath = get_basepath(ctx);
214 dp = sys_opendir(basepath);
216 d_fprintf(stderr, "get_share_list: cannot open usershare directory %s. Error %s\n",
217 basepath, strerror(errno) );
221 while((de = sys_readdir(dp)) != 0) {
222 SMB_STRUCT_STAT sbuf;
224 const char *n = de->d_name;
226 /* Ignore . and .. */
228 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
233 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
234 d_fprintf(stderr, "get_share_list: ignoring bad share name %s\n",n);
237 path = talloc_asprintf(ctx,
246 if (sys_lstat(path, &sbuf) != 0) {
247 d_fprintf(stderr, "get_share_list: can't lstat file %s. Error was %s\n",
248 path, strerror(errno) );
252 if (!S_ISREG(sbuf.st_mode)) {
253 d_fprintf(stderr, "get_share_list: file %s is not a regular file. Ignoring.\n",
258 if (only_ours && sbuf.st_uid != myuid) {
262 if (!unix_wild_match(wcard, n)) {
266 /* (Finally) - add to list. */
267 fl = TALLOC_P(ctx, struct file_list);
272 fl->pathname = talloc_strdup(ctx, n);
278 DLIST_ADD(flist, fl);
285 enum us_priv_op { US_LIST_OP, US_INFO_OP};
287 struct us_priv_info {
292 /***************************************************************************
293 Call a function for every share on the list.
294 ***************************************************************************/
296 static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
298 struct file_list *fl;
301 for (fl = flist; fl; fl = fl->next) {
302 ret = (*fn)(fl, priv);
308 /***************************************************************************
310 ***************************************************************************/
312 static int info_fn(struct file_list *fl, void *priv)
314 SMB_STRUCT_STAT sbuf;
316 struct us_priv_info *pi = (struct us_priv_info *)priv;
317 TALLOC_CTX *ctx = pi->ctx;
320 SEC_DESC *psd = NULL;
322 char *sharepath = NULL;
323 char *comment = NULL;
327 enum usershare_err us_err;
328 bool guest_ok = False;
330 sep_str[0] = *lp_winbind_separator();
333 basepath = get_basepath(ctx);
337 basepath = talloc_asprintf_append(basepath,
345 fd = sys_open(basepath, O_RDONLY|O_NOFOLLOW, 0);
347 fd = sys_open(basepath, O_RDONLY, 0);
351 d_fprintf(stderr, "info_fn: unable to open %s. %s\n",
352 basepath, strerror(errno) );
357 if (sys_fstat(fd, &sbuf) != 0) {
358 d_fprintf(stderr, "info_fn: can't fstat file %s. Error was %s\n",
359 basepath, strerror(errno) );
364 if (!S_ISREG(sbuf.st_mode)) {
365 d_fprintf(stderr, "info_fn: file %s is not a regular file. Ignoring.\n",
371 lines = fd_lines_load(fd, &numlines, 10240);
378 /* Ensure it's well formed. */
379 us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
385 file_lines_free(lines);
387 if (us_err != USERSHARE_OK) {
388 d_fprintf(stderr, "info_fn: file %s is not a well formed usershare file.\n",
390 d_fprintf(stderr, "info_fn: Error was %s.\n",
391 get_us_error_code(us_err) );
395 acl_str = talloc_strdup(ctx, "usershare_acl=");
400 for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
405 ntstatus = net_lookup_name_from_sid(ctx, &psd->dacl->aces[num_aces].trustee, &domain, &name);
407 if (NT_STATUS_IS_OK(ntstatus)) {
408 if (domain && *domain) {
409 acl_str = talloc_asprintf_append(acl_str,
417 acl_str = talloc_asprintf_append(acl_str,
426 sid_to_fstring(sidstr,
427 &psd->dacl->aces[num_aces].trustee);
428 acl_str = talloc_asprintf_append(acl_str,
435 acl_str = talloc_asprintf_append(acl_str, ":");
440 if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
441 acl_str = talloc_asprintf_append(acl_str, "D,");
446 if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
447 acl_str = talloc_asprintf_append(acl_str, "F,");
449 acl_str = talloc_asprintf_append(acl_str, "R,");
457 if (pi->op == US_INFO_OP) {
458 d_printf("[%s]\n", fl->pathname );
459 d_printf("path=%s\n", sharepath );
460 d_printf("comment=%s\n", comment);
461 d_printf("%s\n", acl_str);
462 d_printf("guest_ok=%c\n\n", guest_ok ? 'y' : 'n');
463 } else if (pi->op == US_LIST_OP) {
464 d_printf("%s\n", fl->pathname);
470 /***************************************************************************
471 Print out info (internal detail) on userlevel shares.
472 ***************************************************************************/
474 static int net_usershare_info(int argc, const char **argv)
477 bool only_ours = True;
479 struct us_priv_info pi;
484 if (opt_long_list_entries) {
492 fstrcpy(wcard, argv[0]);
495 return net_usershare_info_usage(argc, argv);
500 ctx = talloc_init("share_info");
501 ret = get_share_list(ctx, wcard, only_ours);
509 ret = process_share_list(info_fn, &pi);
514 /***************************************************************************
515 Count the current total number of usershares.
516 ***************************************************************************/
518 static int count_num_usershares(void)
521 SMB_STRUCT_DIRENT *de;
522 int num_usershares = 0;
523 TALLOC_CTX *ctx = talloc_tos();
524 char *basepath = get_basepath(ctx);
530 dp = sys_opendir(basepath);
532 d_fprintf(stderr, "count_num_usershares: cannot open usershare directory %s. Error %s\n",
533 basepath, strerror(errno) );
537 while((de = sys_readdir(dp)) != 0) {
538 SMB_STRUCT_STAT sbuf;
540 const char *n = de->d_name;
542 /* Ignore . and .. */
544 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
549 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
550 d_fprintf(stderr, "count_num_usershares: ignoring bad share name %s\n",n);
553 path = talloc_asprintf(ctx,
562 if (sys_lstat(path, &sbuf) != 0) {
563 d_fprintf(stderr, "count_num_usershares: can't lstat file %s. Error was %s\n",
564 path, strerror(errno) );
568 if (!S_ISREG(sbuf.st_mode)) {
569 d_fprintf(stderr, "count_num_usershares: file %s is not a regular file. Ignoring.\n",
577 return num_usershares;
580 /***************************************************************************
581 Add a single userlevel share.
582 ***************************************************************************/
584 static int net_usershare_add(int argc, const char **argv)
586 TALLOC_CTX *ctx = talloc_stackframe();
587 SMB_STRUCT_STAT sbuf;
588 SMB_STRUCT_STAT lsbuf;
593 const char *us_comment;
602 uid_t myeuid = geteuid();
603 bool guest_ok = False;
607 arg_acl = "S-1-1-0:R";
613 return net_usershare_add_usage(argc, argv);
615 sharename = strdup_lower(argv[0]);
619 sharename = strdup_lower(argv[0]);
621 us_comment = argv[2];
624 sharename = strdup_lower(argv[0]);
626 us_comment = argv[2];
630 sharename = strdup_lower(argv[0]);
632 us_comment = argv[2];
634 if (strlen(arg_acl) == 0) {
635 arg_acl = "S-1-1-0:R";
637 if (!strnequal(argv[4], "guest_ok=", 9)) {
639 return net_usershare_add_usage(argc, argv);
641 switch (argv[4][9]) {
652 return net_usershare_add_usage(argc, argv);
657 /* Ensure we're under the "usershare max shares" number. Advisory only. */
658 num_usershares = count_num_usershares();
659 if (num_usershares >= lp_usershare_max_shares()) {
660 d_fprintf(stderr, "net usershare add: maximum number of allowed usershares (%d) reached\n",
661 lp_usershare_max_shares() );
663 SAFE_FREE(sharename);
667 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
668 d_fprintf(stderr, "net usershare add: share name %s contains "
669 "invalid characters (any of %s)\n",
670 sharename, INVALID_SHARENAME_CHARS);
672 SAFE_FREE(sharename);
676 /* Disallow shares the same as users. */
677 if (getpwnam(sharename)) {
678 d_fprintf(stderr, "net usershare add: share name %s is already a valid system user name\n",
681 SAFE_FREE(sharename);
685 /* Construct the full path for the usershare file. */
686 full_path = get_basepath(ctx);
689 SAFE_FREE(sharename);
692 full_path_tmp = talloc_asprintf(ctx,
695 if (!full_path_tmp) {
697 SAFE_FREE(sharename);
701 full_path = talloc_asprintf_append(full_path,
706 SAFE_FREE(sharename);
710 /* The path *must* be absolute. */
711 if (us_path[0] != '/') {
712 d_fprintf(stderr,"net usershare add: path %s is not an absolute path.\n",
715 SAFE_FREE(sharename);
719 /* Check the directory to be shared exists. */
720 if (sys_stat(us_path, &sbuf) != 0) {
721 d_fprintf(stderr, "net usershare add: cannot stat path %s to ensure "
722 "this is a directory. Error was %s\n",
723 us_path, strerror(errno) );
725 SAFE_FREE(sharename);
729 if (!S_ISDIR(sbuf.st_mode)) {
730 d_fprintf(stderr, "net usershare add: path %s is not a directory.\n",
733 SAFE_FREE(sharename);
737 /* If we're not root, check if we're restricted to sharing out directories
740 if ((myeuid != 0) && lp_usershare_owner_only() && (myeuid != sbuf.st_uid)) {
741 d_fprintf(stderr, "net usershare add: cannot share path %s as "
742 "we are restricted to only sharing directories we own.\n"
743 "\tAsk the administrator to add the line \"usershare owner only = False\" \n"
744 "\tto the [global] section of the smb.conf to allow this.\n",
747 SAFE_FREE(sharename);
751 /* No validation needed on comment. Now go through and validate the
752 acl string. Convert names to SID's as needed. Then run it through
753 parse_usershare_acl to ensure it's valid. */
755 /* Start off the string we'll append to. */
756 us_acl = talloc_strdup(ctx, "");
765 /* Add the number of ',' characters to get the number of aces. */
766 num_aces += count_chars(pacl,',');
768 for (i = 0; i < num_aces; i++) {
770 const char *pcolon = strchr_m(pacl, ':');
773 if (pcolon == NULL) {
774 d_fprintf(stderr, "net usershare add: malformed acl %s (missing ':').\n",
777 SAFE_FREE(sharename);
789 d_fprintf(stderr, "net usershare add: malformed acl %s "
790 "(access control must be 'r', 'f', or 'd')\n",
793 SAFE_FREE(sharename);
797 if (pcolon[2] != ',' && pcolon[2] != '\0') {
798 d_fprintf(stderr, "net usershare add: malformed terminating character for acl %s\n",
801 SAFE_FREE(sharename);
806 if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
807 d_fprintf(stderr, "talloc_strndup failed\n");
809 SAFE_FREE(sharename);
812 if (!string_to_sid(&sid, name)) {
813 /* Convert to a SID */
814 NTSTATUS ntstatus = net_lookup_sid_from_name(ctx, name, &sid);
815 if (!NT_STATUS_IS_OK(ntstatus)) {
816 d_fprintf(stderr, "net usershare add: cannot convert name \"%s\" to a SID. %s.",
817 name, get_friendly_nt_error_msg(ntstatus) );
818 if (NT_STATUS_EQUAL(ntstatus, NT_STATUS_CONNECTION_REFUSED)) {
819 d_fprintf(stderr, " Maybe smbd is not running.\n");
821 d_fprintf(stderr, "\n");
824 SAFE_FREE(sharename);
828 us_acl = talloc_asprintf_append(
829 us_acl, "%s:%c,", sid_string_tos(&sid), pcolon[1]);
831 /* Move to the next ACL entry. */
832 if (pcolon[2] == ',') {
837 /* Remove the last ',' */
838 us_acl[strlen(us_acl)-1] = '\0';
840 if (guest_ok && !lp_usershare_allow_guests()) {
841 d_fprintf(stderr, "net usershare add: guest_ok=y requested "
842 "but the \"usershare allow guests\" parameter is not enabled "
843 "by this server.\n");
845 SAFE_FREE(sharename);
849 /* Create a temporary filename for this share. */
850 tmpfd = smb_mkstemp(full_path_tmp);
853 d_fprintf(stderr, "net usershare add: cannot create tmp file %s\n",
856 SAFE_FREE(sharename);
860 /* Ensure we opened the file we thought we did. */
861 if (sys_lstat(full_path_tmp, &lsbuf) != 0) {
862 d_fprintf(stderr, "net usershare add: cannot lstat tmp file %s\n",
865 SAFE_FREE(sharename);
869 /* Check this is the same as the file we opened. */
870 if (sys_fstat(tmpfd, &sbuf) != 0) {
871 d_fprintf(stderr, "net usershare add: cannot fstat tmp file %s\n",
874 SAFE_FREE(sharename);
878 if (!S_ISREG(sbuf.st_mode) || sbuf.st_dev != lsbuf.st_dev || sbuf.st_ino != lsbuf.st_ino) {
879 d_fprintf(stderr, "net usershare add: tmp file %s is not a regular file ?\n",
882 SAFE_FREE(sharename);
886 if (fchmod(tmpfd, 0644) == -1) {
887 d_fprintf(stderr, "net usershare add: failed to fchmod tmp file %s to 0644n",
890 SAFE_FREE(sharename);
894 /* Create the in-memory image of the file. */
895 file_img = talloc_strdup(ctx, "#VERSION 2\npath=");
896 file_img = talloc_asprintf_append(file_img, "%s\ncomment=%s\nusershare_acl=%s\nguest_ok=%c\n",
897 us_path, us_comment, us_acl, guest_ok ? 'y' : 'n');
899 to_write = strlen(file_img);
901 if (write(tmpfd, file_img, to_write) != to_write) {
902 d_fprintf(stderr, "net usershare add: failed to write %u bytes to file %s. Error was %s\n",
903 (unsigned int)to_write, full_path_tmp, strerror(errno));
904 unlink(full_path_tmp);
906 SAFE_FREE(sharename);
910 /* Attempt to replace any existing share by this name. */
911 if (rename(full_path_tmp, full_path) != 0) {
912 unlink(full_path_tmp);
913 d_fprintf(stderr, "net usershare add: failed to add share %s. Error was %s\n",
914 sharename, strerror(errno));
917 SAFE_FREE(sharename);
923 if (opt_long_list_entries) {
924 const char *my_argv[2];
925 my_argv[0] = sharename;
927 net_usershare_info(1, my_argv);
930 SAFE_FREE(sharename);
936 /***************************************************************************
938 ***************************************************************************/
940 static int list_fn(struct file_list *fl, void *priv)
942 d_printf("%s\n", fl->pathname);
947 /***************************************************************************
948 List userlevel shares.
949 ***************************************************************************/
951 static int net_usershare_list(int argc, const char **argv)
954 bool only_ours = True;
956 struct us_priv_info pi;
961 if (opt_long_list_entries) {
969 fstrcpy(wcard, argv[0]);
972 return net_usershare_list_usage(argc, argv);
977 ctx = talloc_init("share_list");
978 ret = get_share_list(ctx, wcard, only_ours);
986 ret = process_share_list(info_fn, &pi);
991 /***************************************************************************
992 Handle "net usershare help *" subcommands.
993 ***************************************************************************/
995 int net_usershare_help(int argc, const char **argv)
997 struct functable func[] = {
998 {"ADD", net_usershare_add_usage},
999 {"DELETE", net_usershare_delete_usage},
1000 {"INFO", net_usershare_info_usage},
1001 {"LIST", net_usershare_list_usage},
1004 return net_run_function(argc, argv, func, net_usershare_usage);
1007 /***************************************************************************
1008 Entry-point for all the USERSHARE functions.
1009 ***************************************************************************/
1011 int net_usershare(int argc, const char **argv)
1015 struct functable func[] = {
1016 {"ADD", net_usershare_add},
1017 {"DELETE", net_usershare_delete},
1018 {"INFO", net_usershare_info},
1019 {"LIST", net_usershare_list},
1020 {"HELP", net_usershare_help},
1024 if (lp_usershare_max_shares() == 0) {
1025 d_fprintf(stderr, "net usershare: usershares are currently disabled\n");
1029 dp = sys_opendir(lp_usershare_path());
1032 d_fprintf(stderr, "net usershare: cannot open usershare directory %s. Error %s\n",
1033 lp_usershare_path(), strerror(err) );
1034 if (err == EACCES) {
1035 d_fprintf(stderr, "You do not have permission to create a usershare. Ask your "
1036 "administrator to grant you permissions to create a share.\n");
1037 } else if (err == ENOENT) {
1038 d_fprintf(stderr, "Please ask your system administrator to "
1039 "enable user sharing.\n");
1045 return net_run_function(argc, argv, func, net_usershare_usage);