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/>.
22 #include "system/passwd.h"
23 #include "utils/net.h"
24 #include "../libcli/security/security.h"
27 const char *us_errstr;
28 enum usershare_err us_err;
31 {N_("Malformed usershare file"), USERSHARE_MALFORMED_FILE},
32 {N_("Bad version number"), USERSHARE_BAD_VERSION},
33 {N_("Malformed path entry"), USERSHARE_MALFORMED_PATH},
34 {N_("Malformed comment entryfile"), USERSHARE_MALFORMED_COMMENT_DEF},
35 {N_("Malformed acl definition"), USERSHARE_MALFORMED_ACL_DEF},
36 {N_("Acl parse error"), USERSHARE_ACL_ERR},
37 {N_("Path not absolute"), USERSHARE_PATH_NOT_ABSOLUTE},
38 {N_("Path is denied"), USERSHARE_PATH_IS_DENIED},
39 {N_("Path not allowed"), USERSHARE_PATH_NOT_ALLOWED},
40 {N_("Path is not a directory"), USERSHARE_PATH_NOT_DIRECTORY},
41 {N_("System error"), USERSHARE_POSIX_ERR},
42 {N_("Malformed sharename definition"), USERSHARE_MALFORMED_SHARENAME_DEF},
43 {N_("Bad sharename (doesn't match filename)"), USERSHARE_BAD_SHARENAME},
44 {NULL,(enum usershare_err)-1}
47 static const char *get_us_error_code(enum usershare_err us_err)
52 while (us_errs[idx].us_errstr != NULL) {
53 if (us_errs[idx].us_err == us_err) {
54 return us_errs[idx].us_errstr;
59 result = talloc_asprintf(talloc_tos(), _("Usershare error code (0x%x)"),
60 (unsigned int)us_err);
61 SMB_ASSERT(result != NULL);
65 /* The help subsystem for the USERSHARE subcommand */
67 static int net_usershare_add_usage(struct net_context *c, int argc, const char **argv)
69 char chr = *lp_winbind_separator();
71 "net usershare add [-l|--long] <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>]\n"
72 "\tAdds the specified share name for this user.\n"
73 "\t<sharename> is the new share name.\n"
74 "\t<path> is the path on the filesystem to export.\n"
75 "\t<comment> is the optional comment for the new share.\n"
76 "\t<acl> is an optional share acl in the format \"DOMAIN%cname:X,DOMAIN%cname:X,....\"\n"
77 "\t<guest_ok=y> if present sets \"guest ok = yes\" on this usershare.\n"
78 "\t\t\"X\" represents a permission and can be any one of the characters f, r or d\n"
79 "\t\twhere \"f\" means full control, \"r\" means read-only, \"d\" means deny access.\n"
80 "\t\tname may be a domain user or group. For local users use the local server name "
81 "instead of \"DOMAIN\"\n"
82 "\t\tThe default acl is \"Everyone:r\" which allows everyone read-only access.\n"
83 "\tAdd -l or --long to print the info on the newly added share.\n"),
88 static int net_usershare_delete_usage(struct net_context *c, int argc, const char **argv)
91 "net usershare delete <sharename>\n"
92 "\tdeletes the specified share name for this user.\n"));
96 static int net_usershare_info_usage(struct net_context *c, int argc, const char **argv)
99 "net usershare info [-l|--long] [wildcard sharename]\n"
100 "\tPrints out the path, comment and acl elements of shares that match the wildcard.\n"
101 "\tBy default only gives info on shares owned by the current user\n"
102 "\tAdd -l or --long to apply this to all shares\n"
103 "\tOmit the sharename or use a wildcard of '*' to see all shares\n"));
107 static int net_usershare_list_usage(struct net_context *c, int argc, const char **argv)
110 "net usershare list [-l|--long] [wildcard sharename]\n"
111 "\tLists the names of all shares that match the wildcard.\n"
112 "\tBy default only lists shares owned by the current user\n"
113 "\tAdd -l or --long to apply this to all shares\n"
114 "\tOmit the sharename or use a wildcard of '*' to see all shares\n"));
118 int net_usershare_usage(struct net_context *c, int argc, const char **argv)
120 d_printf(_("net usershare add <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>] to "
121 "add or change a user defined share.\n"
122 "net usershare delete <sharename> to delete a user defined share.\n"
123 "net usershare info [-l|--long] [wildcard sharename] to print info about a user defined share.\n"
124 "net usershare list [-l|--long] [wildcard sharename] to list user defined shares.\n"
125 "net usershare help\n"
126 "\nType \"net usershare help <option>\" to get more information on that option\n\n"));
128 net_common_flags_usage(c, argc, argv);
132 /***************************************************************************
133 ***************************************************************************/
135 static char *get_basepath(TALLOC_CTX *ctx)
137 char *basepath = talloc_strdup(ctx, lp_usershare_path());
142 if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
143 basepath[strlen(basepath)-1] = '\0';
148 /***************************************************************************
149 Delete a single userlevel share.
150 ***************************************************************************/
152 static int net_usershare_delete(struct net_context *c, int argc, const char **argv)
157 if (argc != 1 || c->display_usage) {
158 return net_usershare_delete_usage(c, argc, argv);
161 if ((sharename = strlower_talloc(talloc_tos(), argv[0])) == NULL) {
162 d_fprintf(stderr, _("strlower_talloc failed\n"));
166 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
167 d_fprintf(stderr, _("net usershare delete: share name %s contains "
168 "invalid characters (any of %s)\n"),
169 sharename, INVALID_SHARENAME_CHARS);
170 TALLOC_FREE(sharename);
174 us_path = talloc_asprintf(talloc_tos(),
179 TALLOC_FREE(sharename);
183 if (unlink(us_path) != 0) {
184 d_fprintf(stderr, _("net usershare delete: unable to remove usershare %s. "
186 us_path, strerror(errno));
187 TALLOC_FREE(sharename);
190 TALLOC_FREE(sharename);
194 /***************************************************************************
195 Data structures to handle a list of usershare files.
196 ***************************************************************************/
199 struct file_list *next, *prev;
200 const char *pathname;
203 static struct file_list *flist;
205 /***************************************************************************
206 ***************************************************************************/
208 static int get_share_list(TALLOC_CTX *ctx, const char *wcard, bool only_ours)
211 SMB_STRUCT_DIRENT *de;
212 uid_t myuid = geteuid();
213 struct file_list *fl = NULL;
214 char *basepath = get_basepath(ctx);
219 dp = sys_opendir(basepath);
222 _("get_share_list: cannot open usershare directory %s. "
224 basepath, strerror(errno) );
228 while((de = sys_readdir(dp)) != 0) {
229 SMB_STRUCT_STAT sbuf;
231 const char *n = de->d_name;
233 /* Ignore . and .. */
235 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
240 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
242 _("get_share_list: ignoring bad share "
246 path = talloc_asprintf(ctx,
255 if (sys_lstat(path, &sbuf, false) != 0) {
257 _("get_share_list: can't lstat file %s. Error "
259 path, strerror(errno) );
263 if (!S_ISREG(sbuf.st_ex_mode)) {
265 _("get_share_list: file %s is not a regular "
266 "file. Ignoring.\n"),
271 if (only_ours && sbuf.st_ex_uid != myuid) {
275 if (!unix_wild_match(wcard, n)) {
279 /* (Finally) - add to list. */
280 fl = TALLOC_P(ctx, struct file_list);
285 fl->pathname = talloc_strdup(ctx, n);
291 DLIST_ADD(flist, fl);
298 enum us_priv_op { US_LIST_OP, US_INFO_OP};
300 struct us_priv_info {
303 struct net_context *c;
306 /***************************************************************************
307 Call a function for every share on the list.
308 ***************************************************************************/
310 static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
312 struct file_list *fl;
315 for (fl = flist; fl; fl = fl->next) {
316 ret = (*fn)(fl, priv);
322 /***************************************************************************
324 ***************************************************************************/
326 static int info_fn(struct file_list *fl, void *priv)
328 SMB_STRUCT_STAT sbuf;
330 struct us_priv_info *pi = (struct us_priv_info *)priv;
331 TALLOC_CTX *ctx = pi->ctx;
332 struct net_context *c = pi->c;
335 struct security_descriptor *psd = NULL;
337 char *sharepath = NULL;
338 char *comment = NULL;
339 char *cp_sharename = NULL;
343 enum usershare_err us_err;
344 bool guest_ok = false;
346 sep_str[0] = *lp_winbind_separator();
349 basepath = get_basepath(ctx);
353 basepath = talloc_asprintf_append(basepath,
361 fd = sys_open(basepath, O_RDONLY|O_NOFOLLOW, 0);
363 fd = sys_open(basepath, O_RDONLY, 0);
367 d_fprintf(stderr, _("info_fn: unable to open %s. %s\n"),
368 basepath, strerror(errno) );
373 if (sys_fstat(fd, &sbuf, false) != 0) {
375 _("info_fn: can't fstat file %s. Error was %s\n"),
376 basepath, strerror(errno) );
381 if (!S_ISREG(sbuf.st_ex_mode)) {
383 _("info_fn: file %s is not a regular file. Ignoring.\n"),
389 lines = fd_lines_load(fd, &numlines, 10240, NULL);
396 /* Ensure it's well formed. */
397 us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
406 if (us_err != USERSHARE_OK) {
408 _("info_fn: file %s is not a well formed usershare "
411 d_fprintf(stderr, _("info_fn: Error was %s.\n"),
412 get_us_error_code(us_err) );
416 acl_str = talloc_strdup(ctx, "usershare_acl=");
421 for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
426 ntstatus = net_lookup_name_from_sid(c, ctx,
427 &psd->dacl->aces[num_aces].trustee,
430 if (NT_STATUS_IS_OK(ntstatus)) {
431 if (domain && *domain) {
432 acl_str = talloc_asprintf_append(acl_str,
440 acl_str = talloc_asprintf_append(acl_str,
449 sid_to_fstring(sidstr,
450 &psd->dacl->aces[num_aces].trustee);
451 acl_str = talloc_asprintf_append(acl_str,
458 acl_str = talloc_asprintf_append(acl_str, ":");
463 if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
464 acl_str = talloc_asprintf_append(acl_str, "D,");
469 if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
470 acl_str = talloc_asprintf_append(acl_str, "F,");
472 acl_str = talloc_asprintf_append(acl_str, "R,");
480 /* NOTE: This is smb.conf-like output. Do not translate. */
481 if (pi->op == US_INFO_OP) {
482 d_printf("[%s]\n", cp_sharename );
483 d_printf("path=%s\n", sharepath );
484 d_printf("comment=%s\n", comment);
485 d_printf("%s\n", acl_str);
486 d_printf("guest_ok=%c\n\n", guest_ok ? 'y' : 'n');
487 } else if (pi->op == US_LIST_OP) {
488 d_printf("%s\n", cp_sharename);
494 /***************************************************************************
495 Print out info (internal detail) on userlevel shares.
496 ***************************************************************************/
498 static int net_usershare_info(struct net_context *c, int argc, const char **argv)
501 bool only_ours = true;
503 struct us_priv_info pi;
508 if (c->display_usage)
509 return net_usershare_info_usage(c, argc, argv);
511 if (c->opt_long_list_entries) {
519 fstrcpy(wcard, argv[0]);
522 return net_usershare_info_usage(c, argc, argv);
527 ctx = talloc_init("share_info");
528 ret = get_share_list(ctx, wcard, only_ours);
537 ret = process_share_list(info_fn, &pi);
542 /***************************************************************************
543 Count the current total number of usershares.
544 ***************************************************************************/
546 static int count_num_usershares(void)
549 SMB_STRUCT_DIRENT *de;
550 int num_usershares = 0;
551 TALLOC_CTX *ctx = talloc_tos();
552 char *basepath = get_basepath(ctx);
558 dp = sys_opendir(basepath);
561 _("count_num_usershares: cannot open usershare "
562 "directory %s. Error %s\n"),
563 basepath, strerror(errno) );
567 while((de = sys_readdir(dp)) != 0) {
568 SMB_STRUCT_STAT sbuf;
570 const char *n = de->d_name;
572 /* Ignore . and .. */
574 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
579 if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
581 _("count_num_usershares: ignoring bad share "
585 path = talloc_asprintf(ctx,
594 if (sys_lstat(path, &sbuf, false) != 0) {
596 _("count_num_usershares: can't lstat file %s. "
598 path, strerror(errno) );
602 if (!S_ISREG(sbuf.st_ex_mode)) {
604 _("count_num_usershares: file %s is not a "
605 "regular file. Ignoring.\n"),
613 return num_usershares;
616 /***************************************************************************
617 Add a single userlevel share.
618 ***************************************************************************/
620 static int net_usershare_add(struct net_context *c, int argc, const char **argv)
622 TALLOC_CTX *ctx = talloc_stackframe();
623 SMB_STRUCT_STAT sbuf;
624 SMB_STRUCT_STAT lsbuf;
626 const char *cp_sharename;
630 const char *us_comment;
639 uid_t myeuid = geteuid();
640 bool guest_ok = false;
644 arg_acl = "S-1-1-0:R";
646 if (c->display_usage)
647 return net_usershare_add_usage(c, argc, argv);
653 return net_usershare_add_usage(c, argc, argv);
655 cp_sharename = argv[0];
656 sharename = strlower_talloc(ctx, argv[0]);
660 cp_sharename = argv[0];
661 sharename = strlower_talloc(ctx, argv[0]);
663 us_comment = argv[2];
666 cp_sharename = argv[0];
667 sharename = strlower_talloc(ctx, argv[0]);
669 us_comment = argv[2];
673 cp_sharename = argv[0];
674 sharename = strlower_talloc(ctx, argv[0]);
676 us_comment = argv[2];
678 if (strlen(arg_acl) == 0) {
679 arg_acl = "S-1-1-0:R";
681 if (!strnequal(argv[4], "guest_ok=", 9)) {
683 return net_usershare_add_usage(c, argc, argv);
685 switch (argv[4][9]) {
696 return net_usershare_add_usage(c, argc, argv);
701 /* Ensure we're under the "usershare max shares" number. Advisory only. */
702 num_usershares = count_num_usershares();
703 if (num_usershares >= lp_usershare_max_shares()) {
705 _("net usershare add: maximum number of allowed "
706 "usershares (%d) reached\n"),
707 lp_usershare_max_shares() );
712 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
713 d_fprintf(stderr, _("net usershare add: share name %s contains "
714 "invalid characters (any of %s)\n"),
715 sharename, INVALID_SHARENAME_CHARS);
720 /* Disallow shares the same as users. */
721 if (getpwnam(sharename)) {
723 _("net usershare add: share name %s is already a valid "
724 "system user name\n"),
730 /* Construct the full path for the usershare file. */
731 full_path = get_basepath(ctx);
736 full_path_tmp = talloc_asprintf(ctx,
739 if (!full_path_tmp) {
744 full_path = talloc_asprintf_append(full_path,
752 /* The path *must* be absolute. */
753 if (us_path[0] != '/') {
755 _("net usershare add: path %s is not an absolute "
762 /* Check the directory to be shared exists. */
763 if (sys_stat(us_path, &sbuf, false) != 0) {
765 _("net usershare add: cannot stat path %s to ensure "
766 "this is a directory. Error was %s\n"),
767 us_path, strerror(errno) );
772 if (!S_ISDIR(sbuf.st_ex_mode)) {
774 _("net usershare add: path %s is not a directory.\n"),
780 /* If we're not root, check if we're restricted to sharing out directories
783 if ((myeuid != 0) && lp_usershare_owner_only() && (myeuid != sbuf.st_ex_uid)) {
784 d_fprintf(stderr, _("net usershare add: cannot share path %s as "
785 "we are restricted to only sharing directories we own.\n"
786 "\tAsk the administrator to add the line \"usershare owner only = false\" \n"
787 "\tto the [global] section of the smb.conf to allow this.\n"),
793 /* No validation needed on comment. Now go through and validate the
794 acl string. Convert names to SID's as needed. Then run it through
795 parse_usershare_acl to ensure it's valid. */
797 /* Start off the string we'll append to. */
798 us_acl = talloc_strdup(ctx, "");
807 /* Add the number of ',' characters to get the number of aces. */
808 num_aces += count_chars(pacl,',');
810 for (i = 0; i < num_aces; i++) {
812 const char *pcolon = strchr_m(pacl, ':');
815 if (pcolon == NULL) {
817 _("net usershare add: malformed acl %s "
833 _("net usershare add: malformed acl %s "
834 "(access control must be 'r', 'f', "
841 if (pcolon[2] != ',' && pcolon[2] != '\0') {
843 _("net usershare add: malformed terminating "
844 "character for acl %s\n"),
851 if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
852 d_fprintf(stderr, _("talloc_strndup failed\n"));
856 if (!string_to_sid(&sid, name)) {
857 /* Convert to a SID */
858 NTSTATUS ntstatus = net_lookup_sid_from_name(c, ctx, name, &sid);
859 if (!NT_STATUS_IS_OK(ntstatus)) {
861 _("net usershare add: cannot convert "
862 "name \"%s\" to a SID. %s."),
863 name, get_friendly_nt_error_msg(ntstatus) );
864 if (NT_STATUS_EQUAL(ntstatus, NT_STATUS_CONNECTION_REFUSED)) {
866 _(" Maybe smbd is not running.\n"));
868 d_fprintf(stderr, "\n");
874 us_acl = talloc_asprintf_append(
875 us_acl, "%s:%c,", sid_string_tos(&sid), pcolon[1]);
877 /* Move to the next ACL entry. */
878 if (pcolon[2] == ',') {
883 /* Remove the last ',' */
884 us_acl[strlen(us_acl)-1] = '\0';
886 if (guest_ok && !lp_usershare_allow_guests()) {
887 d_fprintf(stderr, _("net usershare add: guest_ok=y requested "
888 "but the \"usershare allow guests\" parameter is not "
889 "enabled by this server.\n"));
894 /* Create a temporary filename for this share. */
895 tmpfd = mkstemp(full_path_tmp);
899 _("net usershare add: cannot create tmp file %s\n"),
905 /* Ensure we opened the file we thought we did. */
906 if (sys_lstat(full_path_tmp, &lsbuf, false) != 0) {
908 _("net usershare add: cannot lstat tmp file %s\n"),
915 /* Check this is the same as the file we opened. */
916 if (sys_fstat(tmpfd, &sbuf, false) != 0) {
918 _("net usershare add: cannot fstat tmp file %s\n"),
925 if (!S_ISREG(sbuf.st_ex_mode) || sbuf.st_ex_dev != lsbuf.st_ex_dev || sbuf.st_ex_ino != lsbuf.st_ex_ino) {
927 _("net usershare add: tmp file %s is not a regular "
935 if (fchmod(tmpfd, 0644) == -1) {
937 _("net usershare add: failed to fchmod tmp file %s "
945 /* Create the in-memory image of the file. */
946 file_img = talloc_strdup(ctx, "#VERSION 2\npath=");
947 file_img = talloc_asprintf_append(file_img,
948 "%s\ncomment=%s\nusershare_acl=%s\n"
949 "guest_ok=%c\nsharename=%s\n",
953 guest_ok ? 'y' : 'n',
956 to_write = strlen(file_img);
958 if (write(tmpfd, file_img, to_write) != to_write) {
960 _("net usershare add: failed to write %u bytes to "
961 "file %s. Error was %s\n"),
962 (unsigned int)to_write, full_path_tmp, strerror(errno));
963 unlink(full_path_tmp);
969 /* Attempt to replace any existing share by this name. */
970 if (rename(full_path_tmp, full_path) != 0) {
971 unlink(full_path_tmp);
973 _("net usershare add: failed to add share %s. Error "
975 sharename, strerror(errno));
983 if (c->opt_long_list_entries) {
984 const char *my_argv[2];
985 my_argv[0] = sharename;
987 net_usershare_info(c, 1, my_argv);
995 /***************************************************************************
997 ***************************************************************************/
999 static int list_fn(struct file_list *fl, void *priv)
1001 d_printf("%s\n", fl->pathname);
1006 /***************************************************************************
1007 List userlevel shares.
1008 ***************************************************************************/
1010 static int net_usershare_list(struct net_context *c, int argc,
1014 bool only_ours = true;
1016 struct us_priv_info pi;
1019 fstrcpy(wcard, "*");
1021 if (c->display_usage)
1022 return net_usershare_list_usage(c, argc, argv);
1024 if (c->opt_long_list_entries) {
1032 fstrcpy(wcard, argv[0]);
1035 return net_usershare_list_usage(c, argc, argv);
1040 ctx = talloc_init("share_list");
1041 ret = get_share_list(ctx, wcard, only_ours);
1050 ret = process_share_list(info_fn, &pi);
1051 talloc_destroy(ctx);
1055 /***************************************************************************
1056 Entry-point for all the USERSHARE functions.
1057 ***************************************************************************/
1059 int net_usershare(struct net_context *c, int argc, const char **argv)
1063 struct functable func[] = {
1067 NET_TRANSPORT_LOCAL,
1068 N_("Add/modify user defined share"),
1069 N_("net usershare add\n"
1070 " Add/modify user defined share")
1074 net_usershare_delete,
1075 NET_TRANSPORT_LOCAL,
1076 N_("Delete user defined share"),
1077 N_("net usershare delete\n"
1078 " Delete user defined share")
1083 NET_TRANSPORT_LOCAL,
1084 N_("Display information about a user defined share"),
1085 N_("net usershare info\n"
1086 " Display information about a user defined share")
1091 NET_TRANSPORT_LOCAL,
1092 N_("List user defined shares"),
1093 N_("net usershare list\n"
1094 " List user defined shares")
1096 {NULL, NULL, 0, NULL, NULL}
1099 if (lp_usershare_max_shares() == 0) {
1101 _("net usershare: usershares are currently "
1106 dp = sys_opendir(lp_usershare_path());
1110 _("net usershare: cannot open usershare directory %s. "
1112 lp_usershare_path(), strerror(err) );
1113 if (err == EACCES) {
1115 _("You do not have permission to create a "
1116 "usershare. Ask your administrator to grant "
1117 "you permissions to create a share.\n"));
1118 } else if (err == ENOENT) {
1120 _("Please ask your system administrator to "
1121 "enable user sharing.\n"));
1127 return net_run_function(c, argc, argv, "net usershare", func);