RIP BOOL. Convert BOOL -> bool. I found a few interesting
[samba.git] / source3 / utils / net_usershare.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4
5    Copyright (C) Jeremy Allison (jra@samba.org) 2005
6
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.
11    
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.
16    
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/>.  */
19
20 #include "includes.h"
21 #include "utils/net.h"
22
23 struct {
24         const char *us_errstr;
25         enum usershare_err us_err;
26 } us_errs [] = {
27         {"",USERSHARE_OK},
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}
40 };
41
42 static const char *get_us_error_code(enum usershare_err us_err)
43 {
44         static pstring out;
45         int idx = 0;
46
47         while (us_errs[idx].us_errstr != NULL) {
48                 if (us_errs[idx].us_err == us_err) {
49                         return us_errs[idx].us_errstr;
50                 }
51                 idx++;
52         }
53
54         slprintf(out, sizeof(out), "Usershare error code (0x%x)", (unsigned int)us_err);
55         return out;
56 }
57
58 /* The help subsystem for the USERSHARE subcommand */
59
60 static int net_usershare_add_usage(int argc, const char **argv)
61 {
62         char c = *lp_winbind_separator();
63         d_printf(
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",
77                 c, c );
78         return -1;
79 }
80
81 static int net_usershare_delete_usage(int argc, const char **argv)
82 {
83         d_printf(
84                 "net usershare delete <sharename>\n"\
85                 "\tdeletes the specified share name for this user.\n");
86         return -1;
87 }
88
89 static int net_usershare_info_usage(int argc, const char **argv)
90 {
91         d_printf(
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");
97         return -1;
98 }
99
100 static int net_usershare_list_usage(int argc, const char **argv)
101 {
102         d_printf(
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");
108         return -1;
109 }
110
111 int net_usershare_usage(int argc, const char **argv)
112 {
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");
120
121         net_common_flags_usage(argc, argv);
122         return -1;
123 }
124
125 /***************************************************************************
126 ***************************************************************************/
127
128 static void get_basepath(pstring basepath)
129 {
130         pstrcpy(basepath, lp_usershare_path());
131         if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
132                 basepath[strlen(basepath)-1] = '\0';
133         }
134 }
135
136 /***************************************************************************
137  Delete a single userlevel share.
138 ***************************************************************************/
139
140 static int net_usershare_delete(int argc, const char **argv)
141 {
142         pstring us_path;
143         char *sharename;
144
145         if (argc != 1) {
146                 return net_usershare_delete_usage(argc, argv);
147         }
148
149         if ((sharename = strdup_lower(argv[0])) == NULL) {
150                 d_fprintf(stderr, "strdup failed\n");
151                 return -1;
152         }
153
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);
159                 return -1;
160         }
161
162         pstrcpy(us_path, lp_usershare_path());
163         pstrcat(us_path, "/");
164         pstrcat(us_path, sharename);
165
166         if (unlink(us_path) != 0) {
167                 d_fprintf(stderr, "net usershare delete: unable to remove usershare %s. "
168                         "Error was %s\n",
169                         us_path, strerror(errno));
170                 SAFE_FREE(sharename);
171                 return -1;
172         }
173         SAFE_FREE(sharename);
174         return 0;
175 }
176
177 /***************************************************************************
178  Data structures to handle a list of usershare files.
179 ***************************************************************************/
180
181 struct file_list {
182         struct file_list *next, *prev;
183         const char *pathname;
184 };
185
186 static struct file_list *flist;
187
188 /***************************************************************************
189 ***************************************************************************/
190
191 static int get_share_list(TALLOC_CTX *ctx, const char *wcard, bool only_ours)
192 {
193         SMB_STRUCT_DIR *dp;
194         SMB_STRUCT_DIRENT *de;
195         uid_t myuid = geteuid();
196         struct file_list *fl = NULL;
197         pstring basepath;
198
199         get_basepath(basepath);
200         dp = sys_opendir(basepath);
201         if (!dp) {
202                 d_fprintf(stderr, "get_share_list: cannot open usershare directory %s. Error %s\n",
203                         basepath, strerror(errno) );
204                 return -1;
205         }
206
207         while((de = sys_readdir(dp)) != 0) {
208                 SMB_STRUCT_STAT sbuf;
209                 pstring path;
210                 const char *n = de->d_name;
211
212                 /* Ignore . and .. */
213                 if (*n == '.') {
214                         if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
215                                 continue;
216                         }
217                 }
218
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);
221                         continue;
222                 }
223                 pstrcpy(path, basepath);
224                 pstrcat(path, "/");
225                 pstrcat(path, n);
226
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) );
230                         continue;
231                 }
232
233                 if (!S_ISREG(sbuf.st_mode)) {
234                         d_fprintf(stderr, "get_share_list: file %s is not a regular file. Ignoring.\n",
235                                 path );
236                         continue;
237                 }
238
239                 if (only_ours && sbuf.st_uid != myuid) {
240                         continue;
241                 }
242
243                 if (!unix_wild_match(wcard, n)) {
244                         continue;
245                 }
246
247                 /* (Finally) - add to list. */ 
248                 fl = TALLOC_P(ctx, struct file_list);
249                 if (!fl) {
250                         return -1;
251                 }
252                 fl->pathname = talloc_strdup(ctx, n);
253                 if (!fl->pathname) {
254                         return -1;
255                 }
256
257                 DLIST_ADD(flist, fl);
258         }
259
260         sys_closedir(dp);
261         return 0;
262 }
263
264 enum us_priv_op { US_LIST_OP, US_INFO_OP};
265
266 struct us_priv_info {
267         TALLOC_CTX *ctx;
268         enum us_priv_op op;
269 };
270
271 /***************************************************************************
272  Call a function for every share on the list.
273 ***************************************************************************/
274
275 static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
276 {
277         struct file_list *fl;
278         int ret = 0;
279
280         for (fl = flist; fl; fl = fl->next) {
281                 ret = (*fn)(fl, priv);
282         }
283
284         return ret;
285 }
286
287 /***************************************************************************
288  Info function.
289 ***************************************************************************/
290
291 static int info_fn(struct file_list *fl, void *priv)
292 {
293         SMB_STRUCT_STAT sbuf;
294         char **lines = NULL;
295         struct us_priv_info *pi = (struct us_priv_info *)priv;
296         TALLOC_CTX *ctx = pi->ctx;
297         int fd = -1;
298         int numlines = 0;
299         SEC_DESC *psd = NULL;
300         pstring basepath;
301         pstring sharepath;
302         pstring comment;
303         pstring acl_str;
304         int num_aces;
305         char sep_str[2];
306         enum usershare_err us_err;
307         bool guest_ok = False;
308
309         sep_str[0] = *lp_winbind_separator();
310         sep_str[1] = '\0';
311
312         get_basepath(basepath);
313         pstrcat(basepath, "/");
314         pstrcat(basepath, fl->pathname);
315
316 #ifdef O_NOFOLLOW
317         fd = sys_open(basepath, O_RDONLY|O_NOFOLLOW, 0);
318 #else
319         fd = sys_open(basepath, O_RDONLY, 0);
320 #endif
321
322         if (fd == -1) {
323                 d_fprintf(stderr, "info_fn: unable to open %s. %s\n",
324                         basepath, strerror(errno) );
325                 return -1;
326         }
327
328         /* Paranoia... */
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) );
332                 close(fd);
333                 return -1;
334         }
335
336         if (!S_ISREG(sbuf.st_mode)) {
337                 d_fprintf(stderr, "info_fn: file %s is not a regular file. Ignoring.\n",
338                         basepath );
339                 close(fd);
340                 return -1;
341         }
342
343         lines = fd_lines_load(fd, &numlines, 10240);
344         close(fd);
345
346         if (lines == NULL) {
347                 return -1;
348         }
349
350         /* Ensure it's well formed. */
351         us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
352                                 sharepath,
353                                 comment,
354                                 &psd,
355                                 &guest_ok);
356
357         file_lines_free(lines);
358
359         if (us_err != USERSHARE_OK) {
360                 d_fprintf(stderr, "info_fn: file %s is not a well formed usershare file.\n",
361                         basepath );
362                 d_fprintf(stderr, "info_fn: Error was %s.\n",
363                         get_us_error_code(us_err) );
364                 return -1;
365         }
366
367         pstrcpy(acl_str, "usershare_acl=");
368
369         for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
370                 const char *domain;
371                 const char *name;
372                 NTSTATUS ntstatus;
373
374                 ntstatus = net_lookup_name_from_sid(ctx, &psd->dacl->aces[num_aces].trustee, &domain, &name);
375
376                 if (NT_STATUS_IS_OK(ntstatus)) {
377                         if (domain && *domain) {
378                                 pstrcat(acl_str, domain);
379                                 pstrcat(acl_str, sep_str);
380                         }
381                         pstrcat(acl_str,name);
382                 } else {
383                         fstring sidstr;
384                         sid_to_string(sidstr, &psd->dacl->aces[num_aces].trustee);
385                         pstrcat(acl_str,sidstr);
386                 }
387                 pstrcat(acl_str, ":");
388
389                 if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
390                         pstrcat(acl_str, "D,");
391                 } else {
392                         if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
393                                 pstrcat(acl_str, "F,");
394                         } else {
395                                 pstrcat(acl_str, "R,");
396                         }
397                 }
398         }
399
400         acl_str[strlen(acl_str)-1] = '\0';
401
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);
410         }
411
412         return 0;
413 }
414
415 /***************************************************************************
416  Print out info (internal detail) on userlevel shares.
417 ***************************************************************************/
418
419 static int net_usershare_info(int argc, const char **argv)
420 {
421         fstring wcard;
422         bool only_ours = True;
423         int ret = -1;
424         struct us_priv_info pi;
425         TALLOC_CTX *ctx;
426
427         fstrcpy(wcard, "*");
428
429         if (opt_long_list_entries) {
430                 only_ours = False;
431         }
432
433         switch (argc) {
434                 case 0:
435                         break;
436                 case 1:
437                         fstrcpy(wcard, argv[0]);
438                         break;
439                 default:
440                         return net_usershare_info_usage(argc, argv);
441         }
442
443         strlower_m(wcard);
444
445         ctx = talloc_init("share_info");
446         ret = get_share_list(ctx, wcard, only_ours);
447         if (ret) {
448                 return ret;
449         }
450
451         pi.ctx = ctx;
452         pi.op = US_INFO_OP;
453
454         ret = process_share_list(info_fn, &pi);
455         talloc_destroy(ctx);
456         return ret;
457 }
458
459 /***************************************************************************
460  Count the current total number of usershares.
461 ***************************************************************************/
462
463 static int count_num_usershares(void)
464 {
465         SMB_STRUCT_DIR *dp;
466         SMB_STRUCT_DIRENT *de;
467         pstring basepath;
468         int num_usershares = 0;
469
470         get_basepath(basepath);
471         dp = sys_opendir(basepath);
472         if (!dp) {
473                 d_fprintf(stderr, "count_num_usershares: cannot open usershare directory %s. Error %s\n",
474                         basepath, strerror(errno) );
475                 return -1;
476         }
477
478         while((de = sys_readdir(dp)) != 0) {
479                 SMB_STRUCT_STAT sbuf;
480                 pstring path;
481                 const char *n = de->d_name;
482
483                 /* Ignore . and .. */
484                 if (*n == '.') {
485                         if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
486                                 continue;
487                         }
488                 }
489
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);
492                         continue;
493                 }
494                 pstrcpy(path, basepath);
495                 pstrcat(path, "/");
496                 pstrcat(path, n);
497
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) );
501                         continue;
502                 }
503
504                 if (!S_ISREG(sbuf.st_mode)) {
505                         d_fprintf(stderr, "count_num_usershares: file %s is not a regular file. Ignoring.\n",
506                                 path );
507                         continue;
508                 }
509                 num_usershares++;
510         }
511
512         sys_closedir(dp);
513         return num_usershares;
514 }
515
516 /***************************************************************************
517  Add a single userlevel share.
518 ***************************************************************************/
519
520 static int net_usershare_add(int argc, const char **argv)
521 {
522         TALLOC_CTX *ctx = NULL;
523         SMB_STRUCT_STAT sbuf;
524         SMB_STRUCT_STAT lsbuf;
525         char *sharename;
526         pstring full_path;
527         pstring full_path_tmp;
528         const char *us_path;
529         const char *us_comment;
530         const char *arg_acl;
531         char *us_acl;
532         char *file_img;
533         int num_aces = 0;
534         int i;
535         int tmpfd;
536         const char *pacl;
537         size_t to_write;
538         uid_t myeuid = geteuid();
539         bool guest_ok = False;
540         int num_usershares;
541
542         us_comment = "";
543         arg_acl = "S-1-1-0:R";
544
545         switch (argc) {
546                 case 0:
547                 case 1:
548                 default:
549                         return net_usershare_add_usage(argc, argv);
550                 case 2:
551                         sharename = strdup_lower(argv[0]);
552                         us_path = argv[1];
553                         break;
554                 case 3:
555                         sharename = strdup_lower(argv[0]);
556                         us_path = argv[1];
557                         us_comment = argv[2];
558                         break;
559                 case 4:
560                         sharename = strdup_lower(argv[0]);
561                         us_path = argv[1];
562                         us_comment = argv[2];
563                         arg_acl = argv[3];
564                         break;
565                 case 5:
566                         sharename = strdup_lower(argv[0]);
567                         us_path = argv[1];
568                         us_comment = argv[2];
569                         arg_acl = argv[3];
570                         if (strlen(arg_acl) == 0) {
571                                 arg_acl = "S-1-1-0:R";
572                         }
573                         if (!strnequal(argv[4], "guest_ok=", 9)) {
574                                 return net_usershare_add_usage(argc, argv);
575                         }
576                         switch (argv[4][9]) {
577                                 case 'y':
578                                 case 'Y':
579                                         guest_ok = True;
580                                         break;
581                                 case 'n':
582                                 case 'N':
583                                         guest_ok = False;
584                                         break;
585                                 default: 
586                                         return net_usershare_add_usage(argc, argv);
587                         }
588                         break;
589         }
590
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);
597                 return -1;
598         }
599
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);
605                 return -1;
606         }
607
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",
611                         sharename );
612                 SAFE_FREE(sharename);
613                 return -1;
614         }
615
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");
622
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",
626                         us_path);
627                 SAFE_FREE(sharename);
628                 return -1;
629         }
630
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);
637                 return -1;
638         }
639
640         if (!S_ISDIR(sbuf.st_mode)) {
641                 d_fprintf(stderr, "net usershare add: path %s is not a directory.\n",
642                         us_path );
643                 SAFE_FREE(sharename);
644                 return -1;
645         }
646
647         /* If we're not root, check if we're restricted to sharing out directories
648            that we own only. */
649
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",
655                         us_path );
656                 SAFE_FREE(sharename);
657                 return -1;
658         }
659
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. */
663
664         ctx = talloc_init("share_info");
665
666         /* Start off the string we'll append to. */
667         us_acl = talloc_strdup(ctx, "");
668
669         pacl = arg_acl;
670         num_aces = 1;
671
672         /* Add the number of ',' characters to get the number of aces. */
673         num_aces += count_chars(pacl,',');
674
675         for (i = 0; i < num_aces; i++) {
676                 DOM_SID sid;
677                 const char *pcolon = strchr_m(pacl, ':');
678                 const char *name;
679
680                 if (pcolon == NULL) {
681                         d_fprintf(stderr, "net usershare add: malformed acl %s (missing ':').\n",
682                                 pacl );
683                         talloc_destroy(ctx);
684                         SAFE_FREE(sharename);
685                         return -1;
686                 }
687
688                 switch(pcolon[1]) {
689                         case 'f':
690                         case 'F':
691                         case 'd':
692                         case 'r':
693                         case 'R':
694                                 break;
695                         default:
696                                 d_fprintf(stderr, "net usershare add: malformed acl %s "
697                                         "(access control must be 'r', 'f', or 'd')\n",
698                                         pacl );
699                                 talloc_destroy(ctx);
700                                 SAFE_FREE(sharename);
701                                 return -1;
702                 }
703
704                 if (pcolon[2] != ',' && pcolon[2] != '\0') {
705                         d_fprintf(stderr, "net usershare add: malformed terminating character for acl %s\n",
706                                 pacl );
707                         talloc_destroy(ctx);
708                         SAFE_FREE(sharename);
709                         return -1;
710                 }
711
712                 /* Get the name */
713                 if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
714                         d_fprintf(stderr, "talloc_strndup failed\n");
715                         talloc_destroy(ctx);
716                         SAFE_FREE(sharename);
717                         return -1;
718                 }
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");
727                                 } else {
728                                         d_fprintf(stderr, "\n");
729                                 }
730                                 talloc_destroy(ctx);
731                                 SAFE_FREE(sharename);
732                                 return -1;
733                         }
734                 }
735                 us_acl = talloc_asprintf_append(us_acl, "%s:%c,", sid_string_static(&sid), pcolon[1]);
736
737                 /* Move to the next ACL entry. */
738                 if (pcolon[2] == ',') {
739                         pacl = &pcolon[3];
740                 }
741         }
742
743         /* Remove the last ',' */
744         us_acl[strlen(us_acl)-1] = '\0';
745
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");
750                 talloc_destroy(ctx);
751                 SAFE_FREE(sharename);
752                 return -1;
753         }
754
755         /* Create a temporary filename for this share. */
756         tmpfd = smb_mkstemp(full_path_tmp);
757
758         if (tmpfd == -1) {
759                 d_fprintf(stderr, "net usershare add: cannot create tmp file %s\n",
760                                 full_path_tmp );
761                 talloc_destroy(ctx);
762                 SAFE_FREE(sharename);
763                 return -1;
764         }
765
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",
769                                 full_path_tmp );
770                 talloc_destroy(ctx);
771                 SAFE_FREE(sharename);
772                 return -1;
773         }
774
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",
778                                 full_path_tmp );
779                 talloc_destroy(ctx);
780                 SAFE_FREE(sharename);
781                 return -1;
782         }
783
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",
786                                 full_path_tmp );
787                 talloc_destroy(ctx);
788                 SAFE_FREE(sharename);
789                 return -1;
790         }
791         
792         if (fchmod(tmpfd, 0644) == -1) {
793                 d_fprintf(stderr, "net usershare add: failed to fchmod tmp file %s to 0644n",
794                                 full_path_tmp );
795                 talloc_destroy(ctx);
796                 SAFE_FREE(sharename);
797                 return -1;
798         }
799
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');
804
805         to_write = strlen(file_img);
806
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);
811                 talloc_destroy(ctx);
812                 SAFE_FREE(sharename);
813                 return -1;
814         }
815
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));
821                 talloc_destroy(ctx);
822                 close(tmpfd);
823                 SAFE_FREE(sharename);
824                 return -1;
825         }
826
827         close(tmpfd);
828         talloc_destroy(ctx);
829
830         if (opt_long_list_entries) {
831                 const char *my_argv[2];
832                 my_argv[0] = sharename;
833                 my_argv[1] = NULL;
834                 net_usershare_info(1, my_argv);
835         }
836
837         SAFE_FREE(sharename);
838         return 0;
839 }
840
841 #if 0
842 /***************************************************************************
843  List function.
844 ***************************************************************************/
845
846 static int list_fn(struct file_list *fl, void *priv)
847 {
848         d_printf("%s\n", fl->pathname);
849         return 0;
850 }
851 #endif
852
853 /***************************************************************************
854  List userlevel shares.
855 ***************************************************************************/
856
857 static int net_usershare_list(int argc, const char **argv)
858 {
859         fstring wcard;
860         bool only_ours = True;
861         int ret = -1;
862         struct us_priv_info pi;
863         TALLOC_CTX *ctx;
864
865         fstrcpy(wcard, "*");
866
867         if (opt_long_list_entries) {
868                 only_ours = False;
869         }
870
871         switch (argc) {
872                 case 0:
873                         break;
874                 case 1:
875                         fstrcpy(wcard, argv[0]);
876                         break;
877                 default:
878                         return net_usershare_list_usage(argc, argv);
879         }
880
881         strlower_m(wcard);
882
883         ctx = talloc_init("share_list");
884         ret = get_share_list(ctx, wcard, only_ours);
885         if (ret) {
886                 return ret;
887         }
888
889         pi.ctx = ctx;
890         pi.op = US_LIST_OP;
891
892         ret = process_share_list(info_fn, &pi);
893         talloc_destroy(ctx);
894         return ret;
895 }
896
897 /***************************************************************************
898  Handle "net usershare help *" subcommands.
899 ***************************************************************************/
900
901 int net_usershare_help(int argc, const char **argv)
902 {
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},
908                 {NULL, NULL}};
909
910         return net_run_function(argc, argv, func, net_usershare_usage);
911 }
912
913 /***************************************************************************
914  Entry-point for all the USERSHARE functions.
915 ***************************************************************************/
916
917 int net_usershare(int argc, const char **argv)
918 {
919         SMB_STRUCT_DIR *dp;
920
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},
927                 {NULL, NULL}
928         };
929         
930         if (lp_usershare_max_shares() == 0) {
931                 d_fprintf(stderr, "net usershare: usershares are currently disabled\n");
932                 return -1;
933         }
934
935         dp = sys_opendir(lp_usershare_path());
936         if (!dp) {
937                 int err = errno;
938                 d_fprintf(stderr, "net usershare: cannot open usershare directory %s. Error %s\n",
939                         lp_usershare_path(), strerror(err) );
940                 if (err == EACCES) {
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");
946                 }
947                 return -1;
948         }
949         sys_closedir(dp);
950
951         return net_run_function(argc, argv, func, net_usershare_usage);
952 }