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 slprintf(out, sizeof(out), "Usershare error code (0x%x)", (unsigned int)us_err);
58 /* The help subsystem for the USERSHARE subcommand */
60 static int net_usershare_add_usage(int argc, const char **argv)
62 char c = *lp_winbind_separator();
64 "net usershare add [-l|--long] <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>]\n"
65 "\tAdds the specified share name for this user.\n"
66 "\t<sharename> is the new share name.\n"
67 "\t<path> is the path on the filesystem to export.\n"
68 "\t<comment> is the optional comment for the new share.\n"
69 "\t<acl> is an optional share acl in the format \"DOMAIN%cname:X,DOMAIN%cname:X,....\"\n"
70 "\t<guest_ok=y> if present sets \"guest ok = yes\" on this usershare.\n"
71 "\t\t\"X\" represents a permission and can be any one of the characters f, r or d\n"
72 "\t\twhere \"f\" means full control, \"r\" means read-only, \"d\" means deny access.\n"
73 "\t\tname may be a domain user or group. For local users use the local server name "
74 "instead of \"DOMAIN\"\n"
75 "\t\tThe default acl is \"Everyone:r\" which allows everyone read-only access.\n"
76 "\tAdd -l or --long to print the info on the newly added share.\n",
81 static int net_usershare_delete_usage(int argc, const char **argv)
84 "net usershare delete <sharename>\n"\
85 "\tdeletes the specified share name for this user.\n");
89 static int net_usershare_info_usage(int argc, const char **argv)
92 "net usershare info [-l|--long] [wildcard sharename]\n"\
93 "\tPrints out the path, comment and acl elements of shares that match the wildcard.\n"
94 "\tBy default only gives info on shares owned by the current user\n"
95 "\tAdd -l or --long to apply this to all shares\n"
96 "\tOmit the sharename or use a wildcard of '*' to see all shares\n");
100 static int net_usershare_list_usage(int argc, const char **argv)
103 "net usershare list [-l|--long] [wildcard sharename]\n"\
104 "\tLists the names of all shares that match the wildcard.\n"
105 "\tBy default only lists shares owned by the current user\n"
106 "\tAdd -l or --long to apply this to all shares\n"
107 "\tOmit the sharename or use a wildcard of '*' to see all shares\n");
111 int net_usershare_usage(int argc, const char **argv)
113 d_printf("net usershare add <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>] to "
114 "add or change a user defined share.\n"
115 "net usershare delete <sharename> to delete a user defined share.\n"
116 "net usershare info [-l|--long] [wildcard sharename] to print info about a user defined share.\n"
117 "net usershare list [-l|--long] [wildcard sharename] to list user defined shares.\n"
118 "net usershare help\n"\
119 "\nType \"net usershare help <option>\" to get more information on that option\n\n");
121 net_common_flags_usage(argc, argv);
125 /***************************************************************************
126 ***************************************************************************/
128 static void get_basepath(pstring basepath)
130 pstrcpy(basepath, lp_usershare_path());
131 if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
132 basepath[strlen(basepath)-1] = '\0';
136 /***************************************************************************
137 Delete a single userlevel share.
138 ***************************************************************************/
140 static int net_usershare_delete(int argc, const char **argv)
146 return net_usershare_delete_usage(argc, argv);
149 if ((sharename = strdup_lower(argv[0])) == NULL) {
150 d_fprintf(stderr, "strdup failed\n");
154 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
155 d_fprintf(stderr, "net usershare delete: share name %s contains "
156 "invalid characters (any of %s)\n",
157 sharename, INVALID_SHARENAME_CHARS);
158 SAFE_FREE(sharename);
162 pstrcpy(us_path, lp_usershare_path());
163 pstrcat(us_path, "/");
164 pstrcat(us_path, sharename);
166 if (unlink(us_path) != 0) {
167 d_fprintf(stderr, "net usershare delete: unable to remove usershare %s. "
169 us_path, strerror(errno));
170 SAFE_FREE(sharename);
173 SAFE_FREE(sharename);
177 /***************************************************************************
178 Data structures to handle a list of usershare files.
179 ***************************************************************************/
182 struct file_list *next, *prev;
183 const char *pathname;
186 static struct file_list *flist;
188 /***************************************************************************
189 ***************************************************************************/
191 static int get_share_list(TALLOC_CTX *ctx, const char *wcard, BOOL only_ours)
194 SMB_STRUCT_DIRENT *de;
195 uid_t myuid = geteuid();
196 struct file_list *fl = NULL;
199 get_basepath(basepath);
200 dp = sys_opendir(basepath);
202 d_fprintf(stderr, "get_share_list: cannot open usershare directory %s. Error %s\n",
203 basepath, strerror(errno) );
207 while((de = sys_readdir(dp)) != 0) {
208 SMB_STRUCT_STAT sbuf;
210 const char *n = de->d_name;
212 /* Ignore . and .. */
214 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
219 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
220 d_fprintf(stderr, "get_share_list: ignoring bad share name %s\n",n);
223 pstrcpy(path, basepath);
227 if (sys_lstat(path, &sbuf) != 0) {
228 d_fprintf(stderr, "get_share_list: can't lstat file %s. Error was %s\n",
229 path, strerror(errno) );
233 if (!S_ISREG(sbuf.st_mode)) {
234 d_fprintf(stderr, "get_share_list: file %s is not a regular file. Ignoring.\n",
239 if (only_ours && sbuf.st_uid != myuid) {
243 if (!unix_wild_match(wcard, n)) {
247 /* (Finally) - add to list. */
248 fl = TALLOC_P(ctx, struct file_list);
252 fl->pathname = talloc_strdup(ctx, n);
257 DLIST_ADD(flist, fl);
264 enum us_priv_op { US_LIST_OP, US_INFO_OP};
266 struct us_priv_info {
271 /***************************************************************************
272 Call a function for every share on the list.
273 ***************************************************************************/
275 static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
277 struct file_list *fl;
280 for (fl = flist; fl; fl = fl->next) {
281 ret = (*fn)(fl, priv);
287 /***************************************************************************
289 ***************************************************************************/
291 static int info_fn(struct file_list *fl, void *priv)
293 SMB_STRUCT_STAT sbuf;
295 struct us_priv_info *pi = (struct us_priv_info *)priv;
296 TALLOC_CTX *ctx = pi->ctx;
299 SEC_DESC *psd = NULL;
306 enum usershare_err us_err;
307 BOOL guest_ok = False;
309 sep_str[0] = *lp_winbind_separator();
312 get_basepath(basepath);
313 pstrcat(basepath, "/");
314 pstrcat(basepath, fl->pathname);
317 fd = sys_open(basepath, O_RDONLY|O_NOFOLLOW, 0);
319 fd = sys_open(basepath, O_RDONLY, 0);
323 d_fprintf(stderr, "info_fn: unable to open %s. %s\n",
324 basepath, strerror(errno) );
329 if (sys_fstat(fd, &sbuf) != 0) {
330 d_fprintf(stderr, "info_fn: can't fstat file %s. Error was %s\n",
331 basepath, strerror(errno) );
336 if (!S_ISREG(sbuf.st_mode)) {
337 d_fprintf(stderr, "info_fn: file %s is not a regular file. Ignoring.\n",
343 lines = fd_lines_load(fd, &numlines, 10240);
350 /* Ensure it's well formed. */
351 us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
357 file_lines_free(lines);
359 if (us_err != USERSHARE_OK) {
360 d_fprintf(stderr, "info_fn: file %s is not a well formed usershare file.\n",
362 d_fprintf(stderr, "info_fn: Error was %s.\n",
363 get_us_error_code(us_err) );
367 pstrcpy(acl_str, "usershare_acl=");
369 for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
374 ntstatus = net_lookup_name_from_sid(ctx, &psd->dacl->aces[num_aces].trustee, &domain, &name);
376 if (NT_STATUS_IS_OK(ntstatus)) {
377 if (domain && *domain) {
378 pstrcat(acl_str, domain);
379 pstrcat(acl_str, sep_str);
381 pstrcat(acl_str,name);
384 sid_to_string(sidstr, &psd->dacl->aces[num_aces].trustee);
385 pstrcat(acl_str,sidstr);
387 pstrcat(acl_str, ":");
389 if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
390 pstrcat(acl_str, "D,");
392 if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
393 pstrcat(acl_str, "F,");
395 pstrcat(acl_str, "R,");
400 acl_str[strlen(acl_str)-1] = '\0';
402 if (pi->op == US_INFO_OP) {
403 d_printf("[%s]\n", fl->pathname );
404 d_printf("path=%s\n", sharepath );
405 d_printf("comment=%s\n", comment);
406 d_printf("%s\n", acl_str);
407 d_printf("guest_ok=%c\n\n", guest_ok ? 'y' : 'n');
408 } else if (pi->op == US_LIST_OP) {
409 d_printf("%s\n", fl->pathname);
415 /***************************************************************************
416 Print out info (internal detail) on userlevel shares.
417 ***************************************************************************/
419 static int net_usershare_info(int argc, const char **argv)
422 BOOL only_ours = True;
424 struct us_priv_info pi;
429 if (opt_long_list_entries) {
437 fstrcpy(wcard, argv[0]);
440 return net_usershare_info_usage(argc, argv);
445 ctx = talloc_init("share_info");
446 ret = get_share_list(ctx, wcard, only_ours);
454 ret = process_share_list(info_fn, &pi);
459 /***************************************************************************
460 Count the current total number of usershares.
461 ***************************************************************************/
463 static int count_num_usershares(void)
466 SMB_STRUCT_DIRENT *de;
468 int num_usershares = 0;
470 get_basepath(basepath);
471 dp = sys_opendir(basepath);
473 d_fprintf(stderr, "count_num_usershares: cannot open usershare directory %s. Error %s\n",
474 basepath, strerror(errno) );
478 while((de = sys_readdir(dp)) != 0) {
479 SMB_STRUCT_STAT sbuf;
481 const char *n = de->d_name;
483 /* Ignore . and .. */
485 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
490 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
491 d_fprintf(stderr, "count_num_usershares: ignoring bad share name %s\n",n);
494 pstrcpy(path, basepath);
498 if (sys_lstat(path, &sbuf) != 0) {
499 d_fprintf(stderr, "count_num_usershares: can't lstat file %s. Error was %s\n",
500 path, strerror(errno) );
504 if (!S_ISREG(sbuf.st_mode)) {
505 d_fprintf(stderr, "count_num_usershares: file %s is not a regular file. Ignoring.\n",
513 return num_usershares;
516 /***************************************************************************
517 Add a single userlevel share.
518 ***************************************************************************/
520 static int net_usershare_add(int argc, const char **argv)
522 TALLOC_CTX *ctx = NULL;
523 SMB_STRUCT_STAT sbuf;
524 SMB_STRUCT_STAT lsbuf;
527 pstring full_path_tmp;
529 const char *us_comment;
538 uid_t myeuid = geteuid();
539 BOOL guest_ok = False;
543 arg_acl = "S-1-1-0:R";
549 return net_usershare_add_usage(argc, argv);
551 sharename = strdup_lower(argv[0]);
555 sharename = strdup_lower(argv[0]);
557 us_comment = argv[2];
560 sharename = strdup_lower(argv[0]);
562 us_comment = argv[2];
566 sharename = strdup_lower(argv[0]);
568 us_comment = argv[2];
570 if (strlen(arg_acl) == 0) {
571 arg_acl = "S-1-1-0:R";
573 if (!strnequal(argv[4], "guest_ok=", 9)) {
574 return net_usershare_add_usage(argc, argv);
576 switch (argv[4][9]) {
586 return net_usershare_add_usage(argc, argv);
591 /* Ensure we're under the "usershare max shares" number. Advisory only. */
592 num_usershares = count_num_usershares();
593 if (num_usershares >= lp_usershare_max_shares()) {
594 d_fprintf(stderr, "net usershare add: maximum number of allowed usershares (%d) reached\n",
595 lp_usershare_max_shares() );
596 SAFE_FREE(sharename);
600 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
601 d_fprintf(stderr, "net usershare add: share name %s contains "
602 "invalid characters (any of %s)\n",
603 sharename, INVALID_SHARENAME_CHARS);
604 SAFE_FREE(sharename);
608 /* Disallow shares the same as users. */
609 if (getpwnam(sharename)) {
610 d_fprintf(stderr, "net usershare add: share name %s is already a valid system user name\n",
612 SAFE_FREE(sharename);
616 /* Construct the full path for the usershare file. */
617 get_basepath(full_path);
618 pstrcat(full_path, "/");
619 pstrcpy(full_path_tmp, full_path);
620 pstrcat(full_path, sharename);
621 pstrcat(full_path_tmp, ":tmpXXXXXX");
623 /* The path *must* be absolute. */
624 if (us_path[0] != '/') {
625 d_fprintf(stderr,"net usershare add: path %s is not an absolute path.\n",
627 SAFE_FREE(sharename);
631 /* Check the directory to be shared exists. */
632 if (sys_stat(us_path, &sbuf) != 0) {
633 d_fprintf(stderr, "net usershare add: cannot stat path %s to ensure "
634 "this is a directory. Error was %s\n",
635 us_path, strerror(errno) );
636 SAFE_FREE(sharename);
640 if (!S_ISDIR(sbuf.st_mode)) {
641 d_fprintf(stderr, "net usershare add: path %s is not a directory.\n",
643 SAFE_FREE(sharename);
647 /* If we're not root, check if we're restricted to sharing out directories
650 if ((myeuid != 0) && lp_usershare_owner_only() && (myeuid != sbuf.st_uid)) {
651 d_fprintf(stderr, "net usershare add: cannot share path %s as "
652 "we are restricted to only sharing directories we own.\n"
653 "\tAsk the administrator to add the line \"usershare owner only = False\" \n"
654 "\tto the [global] section of the smb.conf to allow this.\n",
656 SAFE_FREE(sharename);
660 /* No validation needed on comment. Now go through and validate the
661 acl string. Convert names to SID's as needed. Then run it through
662 parse_usershare_acl to ensure it's valid. */
664 ctx = talloc_init("share_info");
666 /* Start off the string we'll append to. */
667 us_acl = talloc_strdup(ctx, "");
672 /* Add the number of ',' characters to get the number of aces. */
673 num_aces += count_chars(pacl,',');
675 for (i = 0; i < num_aces; i++) {
677 const char *pcolon = strchr_m(pacl, ':');
680 if (pcolon == NULL) {
681 d_fprintf(stderr, "net usershare add: malformed acl %s (missing ':').\n",
684 SAFE_FREE(sharename);
696 d_fprintf(stderr, "net usershare add: malformed acl %s "
697 "(access control must be 'r', 'f', or 'd')\n",
700 SAFE_FREE(sharename);
704 if (pcolon[2] != ',' && pcolon[2] != '\0') {
705 d_fprintf(stderr, "net usershare add: malformed terminating character for acl %s\n",
708 SAFE_FREE(sharename);
713 if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
714 d_fprintf(stderr, "talloc_strndup failed\n");
716 SAFE_FREE(sharename);
719 if (!string_to_sid(&sid, name)) {
720 /* Convert to a SID */
721 NTSTATUS ntstatus = net_lookup_sid_from_name(ctx, name, &sid);
722 if (!NT_STATUS_IS_OK(ntstatus)) {
723 d_fprintf(stderr, "net usershare add: cannot convert name \"%s\" to a SID. %s.",
724 name, get_friendly_nt_error_msg(ntstatus) );
725 if (NT_STATUS_EQUAL(ntstatus, NT_STATUS_CONNECTION_REFUSED)) {
726 d_fprintf(stderr, " Maybe smbd is not running.\n");
728 d_fprintf(stderr, "\n");
731 SAFE_FREE(sharename);
735 us_acl = talloc_asprintf_append(us_acl, "%s:%c,", sid_string_static(&sid), pcolon[1]);
737 /* Move to the next ACL entry. */
738 if (pcolon[2] == ',') {
743 /* Remove the last ',' */
744 us_acl[strlen(us_acl)-1] = '\0';
746 if (guest_ok && !lp_usershare_allow_guests()) {
747 d_fprintf(stderr, "net usershare add: guest_ok=y requested "
748 "but the \"usershare allow guests\" parameter is not enabled "
749 "by this server.\n");
751 SAFE_FREE(sharename);
755 /* Create a temporary filename for this share. */
756 tmpfd = smb_mkstemp(full_path_tmp);
759 d_fprintf(stderr, "net usershare add: cannot create tmp file %s\n",
762 SAFE_FREE(sharename);
766 /* Ensure we opened the file we thought we did. */
767 if (sys_lstat(full_path_tmp, &lsbuf) != 0) {
768 d_fprintf(stderr, "net usershare add: cannot lstat tmp file %s\n",
771 SAFE_FREE(sharename);
775 /* Check this is the same as the file we opened. */
776 if (sys_fstat(tmpfd, &sbuf) != 0) {
777 d_fprintf(stderr, "net usershare add: cannot fstat tmp file %s\n",
780 SAFE_FREE(sharename);
784 if (!S_ISREG(sbuf.st_mode) || sbuf.st_dev != lsbuf.st_dev || sbuf.st_ino != lsbuf.st_ino) {
785 d_fprintf(stderr, "net usershare add: tmp file %s is not a regular file ?\n",
788 SAFE_FREE(sharename);
792 if (fchmod(tmpfd, 0644) == -1) {
793 d_fprintf(stderr, "net usershare add: failed to fchmod tmp file %s to 0644n",
796 SAFE_FREE(sharename);
800 /* Create the in-memory image of the file. */
801 file_img = talloc_strdup(ctx, "#VERSION 2\npath=");
802 file_img = talloc_asprintf_append(file_img, "%s\ncomment=%s\nusershare_acl=%s\nguest_ok=%c\n",
803 us_path, us_comment, us_acl, guest_ok ? 'y' : 'n');
805 to_write = strlen(file_img);
807 if (write(tmpfd, file_img, to_write) != to_write) {
808 d_fprintf(stderr, "net usershare add: failed to write %u bytes to file %s. Error was %s\n",
809 (unsigned int)to_write, full_path_tmp, strerror(errno));
810 unlink(full_path_tmp);
812 SAFE_FREE(sharename);
816 /* Attempt to replace any existing share by this name. */
817 if (rename(full_path_tmp, full_path) != 0) {
818 unlink(full_path_tmp);
819 d_fprintf(stderr, "net usershare add: failed to add share %s. Error was %s\n",
820 sharename, strerror(errno));
823 SAFE_FREE(sharename);
830 if (opt_long_list_entries) {
831 const char *my_argv[2];
832 my_argv[0] = sharename;
834 net_usershare_info(1, my_argv);
837 SAFE_FREE(sharename);
842 /***************************************************************************
844 ***************************************************************************/
846 static int list_fn(struct file_list *fl, void *priv)
848 d_printf("%s\n", fl->pathname);
853 /***************************************************************************
854 List userlevel shares.
855 ***************************************************************************/
857 static int net_usershare_list(int argc, const char **argv)
860 BOOL only_ours = True;
862 struct us_priv_info pi;
867 if (opt_long_list_entries) {
875 fstrcpy(wcard, argv[0]);
878 return net_usershare_list_usage(argc, argv);
883 ctx = talloc_init("share_list");
884 ret = get_share_list(ctx, wcard, only_ours);
892 ret = process_share_list(info_fn, &pi);
897 /***************************************************************************
898 Handle "net usershare help *" subcommands.
899 ***************************************************************************/
901 int net_usershare_help(int argc, const char **argv)
903 struct functable func[] = {
904 {"ADD", net_usershare_add_usage},
905 {"DELETE", net_usershare_delete_usage},
906 {"INFO", net_usershare_info_usage},
907 {"LIST", net_usershare_list_usage},
910 return net_run_function(argc, argv, func, net_usershare_usage);
913 /***************************************************************************
914 Entry-point for all the USERSHARE functions.
915 ***************************************************************************/
917 int net_usershare(int argc, const char **argv)
921 struct functable func[] = {
922 {"ADD", net_usershare_add},
923 {"DELETE", net_usershare_delete},
924 {"INFO", net_usershare_info},
925 {"LIST", net_usershare_list},
926 {"HELP", net_usershare_help},
930 if (lp_usershare_max_shares() == 0) {
931 d_fprintf(stderr, "net usershare: usershares are currently disabled\n");
935 dp = sys_opendir(lp_usershare_path());
938 d_fprintf(stderr, "net usershare: cannot open usershare directory %s. Error %s\n",
939 lp_usershare_path(), strerror(err) );
941 d_fprintf(stderr, "You do not have permission to create a usershare. Ask your "
942 "administrator to grant you permissions to create a share.\n");
943 } else if (err == ENOENT) {
944 d_fprintf(stderr, "Please ask your system administrator to "
945 "enable user sharing.\n");
951 return net_run_function(argc, argv, func, net_usershare_usage);