net: Rename functable3 to functable, get rid of old functables
[kamenim/samba.git] / source3 / utils / net_usershare.c
index c00f239b99ab8b1ce5027a111f2cecd1eda48bb5..8f263c636ccd13f95502014c526af39bc95fe130 100644 (file)
@@ -1,22 +1,22 @@
-/* 
-   Samba Unix/Linux SMB client library 
-   Distributed SMB/CIFS Server Management Utility 
+/*
+   Samba Unix/Linux SMB client library
+   Distributed SMB/CIFS Server Management Utility
 
    Copyright (C) Jeremy Allison (jra@samba.org) 2005
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include "includes.h"
 #include "utils/net.h"
@@ -42,7 +42,7 @@ struct {
 
 static const char *get_us_error_code(enum usershare_err us_err)
 {
-       static pstring out;
+       char *result;
        int idx = 0;
 
        while (us_errs[idx].us_errstr != NULL) {
@@ -52,44 +52,47 @@ static const char *get_us_error_code(enum usershare_err us_err)
                idx++;
        }
 
-       slprintf(out, sizeof(out), "Usershare error code (0x%x)", (unsigned int)us_err);
-       return out;
+       result = talloc_asprintf(talloc_tos(), "Usershare error code (0x%x)",
+                                (unsigned int)us_err);
+       SMB_ASSERT(result != NULL);
+       return result;
 }
 
 /* The help subsystem for the USERSHARE subcommand */
 
-static int net_usershare_add_usage(int argc, const char **argv)
+static int net_usershare_add_usage(struct net_context *c, int argc, const char **argv)
 {
-       char c = *lp_winbind_separator();
+       char chr = *lp_winbind_separator();
        d_printf(
-               "net usershare add [-l|--long] <sharename> <path> [<comment>] [<acl>]\n"
+               "net usershare add [-l|--long] <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>]\n"
                "\tAdds the specified share name for this user.\n"
                "\t<sharename> is the new share name.\n"
                "\t<path> is the path on the filesystem to export.\n"
                "\t<comment> is the optional comment for the new share.\n"
                "\t<acl> is an optional share acl in the format \"DOMAIN%cname:X,DOMAIN%cname:X,....\"\n"
+               "\t<guest_ok=y> if present sets \"guest ok = yes\" on this usershare.\n"
                "\t\t\"X\" represents a permission and can be any one of the characters f, r or d\n"
                "\t\twhere \"f\" means full control, \"r\" means read-only, \"d\" means deny access.\n"
                "\t\tname may be a domain user or group. For local users use the local server name "
                "instead of \"DOMAIN\"\n"
                "\t\tThe default acl is \"Everyone:r\" which allows everyone read-only access.\n"
                "\tAdd -l or --long to print the info on the newly added share.\n",
-               c, c );
+               chr, chr );
        return -1;
 }
 
-static int net_usershare_delete_usage(int argc, const char **argv)
+static int net_usershare_delete_usage(struct net_context *c, int argc, const char **argv)
 {
        d_printf(
-               "net usershare delete <sharename>\n"\
+               "net usershare delete <sharename>\n"
                "\tdeletes the specified share name for this user.\n");
        return -1;
 }
 
-static int net_usershare_info_usage(int argc, const char **argv)
+static int net_usershare_info_usage(struct net_context *c, int argc, const char **argv)
 {
        d_printf(
-               "net usershare info [-l|--long] [wildcard sharename]\n"\
+               "net usershare info [-l|--long] [wildcard sharename]\n"
                "\tPrints out the path, comment and acl elements of shares that match the wildcard.\n"
                "\tBy default only gives info on shares owned by the current user\n"
                "\tAdd -l or --long to apply this to all shares\n"
@@ -97,10 +100,10 @@ static int net_usershare_info_usage(int argc, const char **argv)
        return -1;
 }
 
-static int net_usershare_list_usage(int argc, const char **argv)
+static int net_usershare_list_usage(struct net_context *c, int argc, const char **argv)
 {
        d_printf(
-               "net usershare list [-l|--long] [wildcard sharename]\n"\
+               "net usershare list [-l|--long] [wildcard sharename]\n"
                "\tLists the names of all shares that match the wildcard.\n"
                "\tBy default only lists shares owned by the current user\n"
                "\tAdd -l or --long to apply this to all shares\n"
@@ -108,44 +111,53 @@ static int net_usershare_list_usage(int argc, const char **argv)
        return -1;
 }
 
-int net_usershare_usage(int argc, const char **argv)
+int net_usershare_usage(struct net_context *c, int argc, const char **argv)
 {
-       d_printf("net usershare add <sharename> <path> [<comment>] [<acl>] to add or change a user defined share.\n"
+       d_printf("net usershare add <sharename> <path> [<comment>] [<acl>] [<guest_ok=[y|n]>] to "
+                               "add or change a user defined share.\n"
                "net usershare delete <sharename> to delete a user defined share.\n"
                "net usershare info [-l|--long] [wildcard sharename] to print info about a user defined share.\n"
                "net usershare list [-l|--long] [wildcard sharename] to list user defined shares.\n"
-               "net usershare help\n"\
+               "net usershare help\n"
                "\nType \"net usershare help <option>\" to get more information on that option\n\n");
 
-       net_common_flags_usage(argc, argv);
+       net_common_flags_usage(c, argc, argv);
        return -1;
 }
 
 /***************************************************************************
 ***************************************************************************/
 
-static void get_basepath(pstring basepath)
+static char *get_basepath(TALLOC_CTX *ctx)
 {
-       pstrcpy(basepath, lp_usershare_path());
-       if (basepath[strlen(basepath)-1] == '/') {
+       char *basepath = talloc_strdup(ctx, lp_usershare_path());
+
+       if (!basepath) {
+               return NULL;
+       }
+       if ((basepath[0] != '\0') && (basepath[strlen(basepath)-1] == '/')) {
                basepath[strlen(basepath)-1] = '\0';
        }
+       return basepath;
 }
 
 /***************************************************************************
  Delete a single userlevel share.
 ***************************************************************************/
 
-static int net_usershare_delete(int argc, const char **argv)
+static int net_usershare_delete(struct net_context *c, int argc, const char **argv)
 {
-       pstring us_path;
+       char *us_path;
        char *sharename;
 
-       if (argc != 1) {
-               return net_usershare_delete_usage(argc, argv);
+       if (argc != 1 || c->display_usage) {
+               return net_usershare_delete_usage(c, argc, argv);
        }
 
-       sharename = strdup_lower(argv[0]);
+       if ((sharename = strdup_lower(argv[0])) == NULL) {
+               d_fprintf(stderr, "strdup failed\n");
+               return -1;
+       }
 
        if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
                d_fprintf(stderr, "net usershare delete: share name %s contains "
@@ -155,9 +167,14 @@ static int net_usershare_delete(int argc, const char **argv)
                return -1;
        }
 
-       pstrcpy(us_path, lp_usershare_path());
-       pstrcat(us_path, "/");
-       pstrcat(us_path, sharename);
+       us_path = talloc_asprintf(talloc_tos(),
+                               "%s/%s",
+                               lp_usershare_path(),
+                               sharename);
+       if (!us_path) {
+               SAFE_FREE(sharename);
+               return -1;
+       }
 
        if (unlink(us_path) != 0) {
                d_fprintf(stderr, "net usershare delete: unable to remove usershare %s. "
@@ -184,15 +201,17 @@ static struct file_list *flist;
 /***************************************************************************
 ***************************************************************************/
 
-static int get_share_list(TALLOC_CTX *ctx, const char *wcard, BOOL only_ours)
+static int get_share_list(TALLOC_CTX *ctx, const char *wcard, bool only_ours)
 {
        SMB_STRUCT_DIR *dp;
        SMB_STRUCT_DIRENT *de;
        uid_t myuid = geteuid();
        struct file_list *fl = NULL;
-       pstring basepath;
+       char *basepath = get_basepath(ctx);
 
-       get_basepath(basepath);
+       if (!basepath) {
+               return -1;
+       }
        dp = sys_opendir(basepath);
        if (!dp) {
                d_fprintf(stderr, "get_share_list: cannot open usershare directory %s. Error %s\n",
@@ -202,7 +221,7 @@ static int get_share_list(TALLOC_CTX *ctx, const char *wcard, BOOL only_ours)
 
        while((de = sys_readdir(dp)) != 0) {
                SMB_STRUCT_STAT sbuf;
-               pstring path;
+               char *path;
                const char *n = de->d_name;
 
                /* Ignore . and .. */
@@ -216,9 +235,14 @@ static int get_share_list(TALLOC_CTX *ctx, const char *wcard, BOOL only_ours)
                        d_fprintf(stderr, "get_share_list: ignoring bad share name %s\n",n);
                        continue;
                }
-               pstrcpy(path, basepath);
-               pstrcat(path, "/");
-               pstrcat(path, n);
+               path = talloc_asprintf(ctx,
+                                       "%s/%s",
+                                       basepath,
+                                       n);
+               if (!path) {
+                       sys_closedir(dp);
+                       return -1;
+               }
 
                if (sys_lstat(path, &sbuf) != 0) {
                        d_fprintf(stderr, "get_share_list: can't lstat file %s. Error was %s\n",
@@ -240,13 +264,15 @@ static int get_share_list(TALLOC_CTX *ctx, const char *wcard, BOOL only_ours)
                        continue;
                }
 
-               /* (Finally) - add to list. */ 
+               /* (Finally) - add to list. */
                fl = TALLOC_P(ctx, struct file_list);
                if (!fl) {
+                       sys_closedir(dp);
                        return -1;
                }
                fl->pathname = talloc_strdup(ctx, n);
                if (!fl->pathname) {
+                       sys_closedir(dp);
                        return -1;
                }
 
@@ -257,24 +283,25 @@ static int get_share_list(TALLOC_CTX *ctx, const char *wcard, BOOL only_ours)
        return 0;
 }
 
-enum priv_op { US_LIST_OP, US_INFO_OP};
+enum us_priv_op { US_LIST_OP, US_INFO_OP};
 
-struct priv_info {
+struct us_priv_info {
        TALLOC_CTX *ctx;
-       enum priv_op op;
+       enum us_priv_op op;
+       struct net_context *c;
 };
 
 /***************************************************************************
  Call a function for every share on the list.
 ***************************************************************************/
 
-static int process_share_list(int (*fn)(struct file_list *, void *), void *private)
+static int process_share_list(int (*fn)(struct file_list *, void *), void *priv)
 {
        struct file_list *fl;
        int ret = 0;
 
        for (fl = flist; fl; fl = fl->next) {
-               ret = (*fn)(fl, private);
+               ret = (*fn)(fl, priv);
        }
 
        return ret;
@@ -284,29 +311,38 @@ static int process_share_list(int (*fn)(struct file_list *, void *), void *priva
  Info function.
 ***************************************************************************/
 
-static int info_fn(struct file_list *fl, void *private)
+static int info_fn(struct file_list *fl, void *priv)
 {
        SMB_STRUCT_STAT sbuf;
        char **lines = NULL;
-       struct priv_info *pi = (struct priv_info *)private;
+       struct us_priv_info *pi = (struct us_priv_info *)priv;
        TALLOC_CTX *ctx = pi->ctx;
+       struct net_context *c = pi->c;
        int fd = -1;
        int numlines = 0;
        SEC_DESC *psd = NULL;
-       pstring basepath;
-       pstring sharepath;
-       pstring comment;
-       pstring acl_str;
+       char *basepath;
+       char *sharepath = NULL;
+       char *comment = NULL;
+       char *acl_str;
        int num_aces;
        char sep_str[2];
        enum usershare_err us_err;
+       bool guest_ok = false;
 
        sep_str[0] = *lp_winbind_separator();
        sep_str[1] = '\0';
 
-       get_basepath(basepath);
-       pstrcat(basepath, "/");
-       pstrcat(basepath, fl->pathname);
+       basepath = get_basepath(ctx);
+       if (!basepath) {
+               return -1;
+       }
+       basepath = talloc_asprintf_append(basepath,
+                       "/%s",
+                       fl->pathname);
+       if (!basepath) {
+               return -1;
+       }
 
 #ifdef O_NOFOLLOW
        fd = sys_open(basepath, O_RDONLY|O_NOFOLLOW, 0);
@@ -344,9 +380,12 @@ static int info_fn(struct file_list *fl, void *private)
 
        /* Ensure it's well formed. */
        us_err = parse_usershare_file(ctx, &sbuf, fl->pathname, -1, lines, numlines,
-                               sharepath,
-                               comment,
-                               &psd);
+                               &sharepath,
+                               &comment,
+                               &psd,
+                               &guest_ok);
+
+       file_lines_free(lines);
 
        if (us_err != USERSHARE_OK) {
                d_fprintf(stderr, "info_fn: file %s is not a well formed usershare file.\n",
@@ -356,46 +395,76 @@ static int info_fn(struct file_list *fl, void *private)
                return -1;
        }
 
-       pstrcpy(acl_str, "usershare_acl=");
+       acl_str = talloc_strdup(ctx, "usershare_acl=");
+       if (!acl_str) {
+               return -1;
+       }
 
        for (num_aces = 0; num_aces < psd->dacl->num_aces; num_aces++) {
-               char access_str[2];
                const char *domain;
                const char *name;
-
-               access_str[1] = '\0';
-
-               if (net_lookup_name_from_sid(ctx, &psd->dacl->ace[num_aces].trustee, &domain, &name)) {
-                       if (*domain) {
-                               pstrcat(acl_str, domain);
-                               pstrcat(acl_str, sep_str);
+               NTSTATUS ntstatus;
+
+               ntstatus = net_lookup_name_from_sid(c, ctx,
+                                                   &psd->dacl->aces[num_aces].trustee,
+                                                   &domain, &name);
+
+               if (NT_STATUS_IS_OK(ntstatus)) {
+                       if (domain && *domain) {
+                               acl_str = talloc_asprintf_append(acl_str,
+                                               "%s%s",
+                                               domain,
+                                               sep_str);
+                               if (!acl_str) {
+                                       return -1;
+                               }
+                       }
+                       acl_str = talloc_asprintf_append(acl_str,
+                                               "%s",
+                                               name);
+                       if (!acl_str) {
+                               return -1;
                        }
-                       pstrcat(acl_str,name);
+
                } else {
                        fstring sidstr;
-                       sid_to_string(sidstr, &psd->dacl->ace[num_aces].trustee);
-                       pstrcat(acl_str,sidstr);
+                       sid_to_fstring(sidstr,
+                                      &psd->dacl->aces[num_aces].trustee);
+                       acl_str = talloc_asprintf_append(acl_str,
+                                               "%s",
+                                               sidstr);
+                       if (!acl_str) {
+                               return -1;
+                       }
+               }
+               acl_str = talloc_asprintf_append(acl_str, ":");
+               if (!acl_str) {
+                       return -1;
                }
-               pstrcat(acl_str, ":");
 
-               if (psd->dacl->ace[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
-                       pstrcat(acl_str, "D,");
+               if (psd->dacl->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) {
+                       acl_str = talloc_asprintf_append(acl_str, "D,");
+                       if (!acl_str) {
+                               return -1;
+                       }
                } else {
-                       if (psd->dacl->ace[num_aces].info.mask & GENERIC_ALL_ACCESS) {
-                               pstrcat(acl_str, "F,");
+                       if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) {
+                               acl_str = talloc_asprintf_append(acl_str, "F,");
                        } else {
-                               pstrcat(acl_str, "R,");
+                               acl_str = talloc_asprintf_append(acl_str, "R,");
+                       }
+                       if (!acl_str) {
+                               return -1;
                        }
                }
        }
 
-       acl_str[strlen(acl_str)-1] = '\0';
-
        if (pi->op == US_INFO_OP) {
                d_printf("[%s]\n", fl->pathname );
                d_printf("path=%s\n", sharepath );
                d_printf("comment=%s\n", comment);
-               d_printf("%s\n\n", acl_str);
+               d_printf("%s\n", acl_str);
+               d_printf("guest_ok=%c\n\n", guest_ok ? 'y' : 'n');
        } else if (pi->op == US_LIST_OP) {
                d_printf("%s\n", fl->pathname);
        }
@@ -407,18 +476,21 @@ static int info_fn(struct file_list *fl, void *private)
  Print out info (internal detail) on userlevel shares.
 ***************************************************************************/
 
-static int net_usershare_info(int argc, const char **argv)
+static int net_usershare_info(struct net_context *c, int argc, const char **argv)
 {
        fstring wcard;
-       BOOL only_ours = True;
+       bool only_ours = true;
        int ret = -1;
-       struct priv_info pi;
+       struct us_priv_info pi;
        TALLOC_CTX *ctx;
 
        fstrcpy(wcard, "*");
 
-       if (opt_long_list_entries) {
-               only_ours = False;
+       if (c->display_usage)
+               return net_usershare_info_usage(c, argc, argv);
+
+       if (c->opt_long_list_entries) {
+               only_ours = false;
        }
 
        switch (argc) {
@@ -428,7 +500,7 @@ static int net_usershare_info(int argc, const char **argv)
                        fstrcpy(wcard, argv[0]);
                        break;
                default:
-                       return net_usershare_info_usage(argc, argv);
+                       return net_usershare_info_usage(c, argc, argv);
        }
 
        strlower_m(wcard);
@@ -441,24 +513,91 @@ static int net_usershare_info(int argc, const char **argv)
 
        pi.ctx = ctx;
        pi.op = US_INFO_OP;
+       pi.c = c;
 
        ret = process_share_list(info_fn, &pi);
        talloc_destroy(ctx);
        return ret;
 }
 
+/***************************************************************************
+ Count the current total number of usershares.
+***************************************************************************/
+
+static int count_num_usershares(void)
+{
+       SMB_STRUCT_DIR *dp;
+       SMB_STRUCT_DIRENT *de;
+       int num_usershares = 0;
+       TALLOC_CTX *ctx = talloc_tos();
+       char *basepath = get_basepath(ctx);
+
+       if (!basepath) {
+               return -1;
+       }
+
+       dp = sys_opendir(basepath);
+       if (!dp) {
+               d_fprintf(stderr, "count_num_usershares: cannot open usershare directory %s. Error %s\n",
+                       basepath, strerror(errno) );
+               return -1;
+       }
+
+       while((de = sys_readdir(dp)) != 0) {
+               SMB_STRUCT_STAT sbuf;
+               char *path;
+               const char *n = de->d_name;
+
+               /* Ignore . and .. */
+               if (*n == '.') {
+                       if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
+                               continue;
+                       }
+               }
+
+               if (!validate_net_name(n, INVALID_SHARENAME_CHARS, strlen(n))) {
+                       d_fprintf(stderr, "count_num_usershares: ignoring bad share name %s\n",n);
+                       continue;
+               }
+               path = talloc_asprintf(ctx,
+                               "%s/%s",
+                               basepath,
+                               n);
+               if (!path) {
+                       sys_closedir(dp);
+                       return -1;
+               }
+
+               if (sys_lstat(path, &sbuf) != 0) {
+                       d_fprintf(stderr, "count_num_usershares: can't lstat file %s. Error was %s\n",
+                               path, strerror(errno) );
+                       continue;
+               }
+
+               if (!S_ISREG(sbuf.st_mode)) {
+                       d_fprintf(stderr, "count_num_usershares: file %s is not a regular file. Ignoring.\n",
+                               path );
+                       continue;
+               }
+               num_usershares++;
+       }
+
+       sys_closedir(dp);
+       return num_usershares;
+}
+
 /***************************************************************************
  Add a single userlevel share.
 ***************************************************************************/
 
-static int net_usershare_add(int argc, const char **argv)
+static int net_usershare_add(struct net_context *c, int argc, const char **argv)
 {
-       TALLOC_CTX *ctx = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
        SMB_STRUCT_STAT sbuf;
        SMB_STRUCT_STAT lsbuf;
        char *sharename;
-       pstring full_path;
-       pstring full_path_tmp;
+       char *full_path;
+       char *full_path_tmp;
        const char *us_path;
        const char *us_comment;
        const char *arg_acl;
@@ -470,15 +609,20 @@ static int net_usershare_add(int argc, const char **argv)
        const char *pacl;
        size_t to_write;
        uid_t myeuid = geteuid();
+       bool guest_ok = false;
+       int num_usershares;
 
        us_comment = "";
        arg_acl = "S-1-1-0:R";
 
+       if (c->display_usage)
+               return net_usershare_add_usage(c, argc, argv);
+
        switch (argc) {
                case 0:
                case 1:
                default:
-                       return net_usershare_add_usage(argc, argv);
+                       return net_usershare_add_usage(c, argc, argv);
                case 2:
                        sharename = strdup_lower(argv[0]);
                        us_path = argv[1];
@@ -494,12 +638,49 @@ static int net_usershare_add(int argc, const char **argv)
                        us_comment = argv[2];
                        arg_acl = argv[3];
                        break;
+               case 5:
+                       sharename = strdup_lower(argv[0]);
+                       us_path = argv[1];
+                       us_comment = argv[2];
+                       arg_acl = argv[3];
+                       if (strlen(arg_acl) == 0) {
+                               arg_acl = "S-1-1-0:R";
+                       }
+                       if (!strnequal(argv[4], "guest_ok=", 9)) {
+                               TALLOC_FREE(ctx);
+                               return net_usershare_add_usage(c, argc, argv);
+                       }
+                       switch (argv[4][9]) {
+                               case 'y':
+                               case 'Y':
+                                       guest_ok = true;
+                                       break;
+                               case 'n':
+                               case 'N':
+                                       guest_ok = false;
+                                       break;
+                               default:
+                                       TALLOC_FREE(ctx);
+                                       return net_usershare_add_usage(c, argc, argv);
+                       }
+                       break;
+       }
+
+       /* Ensure we're under the "usershare max shares" number. Advisory only. */
+       num_usershares = count_num_usershares();
+       if (num_usershares >= lp_usershare_max_shares()) {
+               d_fprintf(stderr, "net usershare add: maximum number of allowed usershares (%d) reached\n",
+                       lp_usershare_max_shares() );
+               TALLOC_FREE(ctx);
+               SAFE_FREE(sharename);
+               return -1;
        }
 
        if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS, strlen(sharename))) {
                d_fprintf(stderr, "net usershare add: share name %s contains "
                         "invalid characters (any of %s)\n",
                         sharename, INVALID_SHARENAME_CHARS);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -508,21 +689,41 @@ static int net_usershare_add(int argc, const char **argv)
        if (getpwnam(sharename)) {
                d_fprintf(stderr, "net usershare add: share name %s is already a valid system user name\n",
                        sharename );
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
 
        /* Construct the full path for the usershare file. */
-       get_basepath(full_path);
-       pstrcat(full_path, "/");
-       pstrcpy(full_path_tmp, full_path);
-       pstrcat(full_path, sharename);
-       pstrcat(full_path_tmp, ":tmpXXXXXX");
+       full_path = get_basepath(ctx);
+       if (!full_path) {
+               TALLOC_FREE(ctx);
+               SAFE_FREE(sharename);
+               return -1;
+       }
+       full_path_tmp = talloc_asprintf(ctx,
+                       "%s/:tmpXXXXXX",
+                       full_path);
+       if (!full_path_tmp) {
+               TALLOC_FREE(ctx);
+               SAFE_FREE(sharename);
+               return -1;
+       }
+
+       full_path = talloc_asprintf_append(full_path,
+                                       "/%s",
+                                       sharename);
+       if (!full_path) {
+               TALLOC_FREE(ctx);
+               SAFE_FREE(sharename);
+               return -1;
+       }
 
        /* The path *must* be absolute. */
        if (us_path[0] != '/') {
                d_fprintf(stderr,"net usershare add: path %s is not an absolute path.\n",
                        us_path);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -532,6 +733,7 @@ static int net_usershare_add(int argc, const char **argv)
                d_fprintf(stderr, "net usershare add: cannot stat path %s to ensure "
                        "this is a directory. Error was %s\n",
                        us_path, strerror(errno) );
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -539,6 +741,7 @@ static int net_usershare_add(int argc, const char **argv)
        if (!S_ISDIR(sbuf.st_mode)) {
                d_fprintf(stderr, "net usershare add: path %s is not a directory.\n",
                        us_path );
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -548,8 +751,11 @@ static int net_usershare_add(int argc, const char **argv)
 
        if ((myeuid != 0) && lp_usershare_owner_only() && (myeuid != sbuf.st_uid)) {
                d_fprintf(stderr, "net usershare add: cannot share path %s as "
-                       "we are restricted to only sharing directories we own.\n",
+                       "we are restricted to only sharing directories we own.\n"
+                       "\tAsk the administrator to add the line \"usershare owner only = false\" \n"
+                       "\tto the [global] section of the smb.conf to allow this.\n",
                        us_path );
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -558,10 +764,12 @@ static int net_usershare_add(int argc, const char **argv)
           acl string. Convert names to SID's as needed. Then run it through
           parse_usershare_acl to ensure it's valid. */
 
-       ctx = talloc_init("share_info");
-
        /* Start off the string we'll append to. */
        us_acl = talloc_strdup(ctx, "");
+       if (!us_acl) {
+               TALLOC_FREE(ctx);
+               return -1;
+       }
 
        pacl = arg_acl;
        num_aces = 1;
@@ -577,7 +785,7 @@ static int net_usershare_add(int argc, const char **argv)
                if (pcolon == NULL) {
                        d_fprintf(stderr, "net usershare add: malformed acl %s (missing ':').\n",
                                pacl );
-                       talloc_destroy(ctx);
+                       TALLOC_FREE(ctx);
                        SAFE_FREE(sharename);
                        return -1;
                }
@@ -593,7 +801,7 @@ static int net_usershare_add(int argc, const char **argv)
                                d_fprintf(stderr, "net usershare add: malformed acl %s "
                                        "(access control must be 'r', 'f', or 'd')\n",
                                        pacl );
-                               talloc_destroy(ctx);
+                               TALLOC_FREE(ctx);
                                SAFE_FREE(sharename);
                                return -1;
                }
@@ -601,24 +809,36 @@ static int net_usershare_add(int argc, const char **argv)
                if (pcolon[2] != ',' && pcolon[2] != '\0') {
                        d_fprintf(stderr, "net usershare add: malformed terminating character for acl %s\n",
                                pacl );
-                       talloc_destroy(ctx);
+                       TALLOC_FREE(ctx);
                        SAFE_FREE(sharename);
                        return -1;
                }
 
                /* Get the name */
-               name = talloc_strndup(ctx, pacl, pcolon - pacl);
+               if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) {
+                       d_fprintf(stderr, "talloc_strndup failed\n");
+                       TALLOC_FREE(ctx);
+                       SAFE_FREE(sharename);
+                       return -1;
+               }
                if (!string_to_sid(&sid, name)) {
                        /* Convert to a SID */
-                       if (!net_lookup_sid_from_name(ctx, name, &sid)) {
-                               d_fprintf(stderr, "net usershare add: cannot convert name %s to a SID.\n",
-                                       name );
-                               talloc_destroy(ctx);
+                       NTSTATUS ntstatus = net_lookup_sid_from_name(c, ctx, name, &sid);
+                       if (!NT_STATUS_IS_OK(ntstatus)) {
+                               d_fprintf(stderr, "net usershare add: cannot convert name \"%s\" to a SID. %s.",
+                                       name, get_friendly_nt_error_msg(ntstatus) );
+                               if (NT_STATUS_EQUAL(ntstatus, NT_STATUS_CONNECTION_REFUSED)) {
+                                       d_fprintf(stderr,  " Maybe smbd is not running.\n");
+                               } else {
+                                       d_fprintf(stderr, "\n");
+                               }
+                               TALLOC_FREE(ctx);
                                SAFE_FREE(sharename);
                                return -1;
                        }
                }
-               us_acl = talloc_asprintf_append(us_acl, "%s:%c,", sid_string_static(&sid), pcolon[1]);
+               us_acl = talloc_asprintf_append(
+                       us_acl, "%s:%c,", sid_string_tos(&sid), pcolon[1]);
 
                /* Move to the next ACL entry. */
                if (pcolon[2] == ',') {
@@ -629,13 +849,22 @@ static int net_usershare_add(int argc, const char **argv)
        /* Remove the last ',' */
        us_acl[strlen(us_acl)-1] = '\0';
 
+       if (guest_ok && !lp_usershare_allow_guests()) {
+               d_fprintf(stderr, "net usershare add: guest_ok=y requested "
+                       "but the \"usershare allow guests\" parameter is not enabled "
+                       "by this server.\n");
+               TALLOC_FREE(ctx);
+               SAFE_FREE(sharename);
+               return -1;
+       }
+
        /* Create a temporary filename for this share. */
        tmpfd = smb_mkstemp(full_path_tmp);
 
        if (tmpfd == -1) {
                d_fprintf(stderr, "net usershare add: cannot create tmp file %s\n",
                                full_path_tmp );
-               talloc_destroy(ctx);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -644,7 +873,7 @@ static int net_usershare_add(int argc, const char **argv)
        if (sys_lstat(full_path_tmp, &lsbuf) != 0) {
                d_fprintf(stderr, "net usershare add: cannot lstat tmp file %s\n",
                                full_path_tmp );
-               talloc_destroy(ctx);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -653,7 +882,7 @@ static int net_usershare_add(int argc, const char **argv)
        if (sys_fstat(tmpfd, &sbuf) != 0) {
                d_fprintf(stderr, "net usershare add: cannot fstat tmp file %s\n",
                                full_path_tmp );
-               talloc_destroy(ctx);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -661,23 +890,23 @@ static int net_usershare_add(int argc, const char **argv)
        if (!S_ISREG(sbuf.st_mode) || sbuf.st_dev != lsbuf.st_dev || sbuf.st_ino != lsbuf.st_ino) {
                d_fprintf(stderr, "net usershare add: tmp file %s is not a regular file ?\n",
                                full_path_tmp );
-               talloc_destroy(ctx);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
-       
+
        if (fchmod(tmpfd, 0644) == -1) {
                d_fprintf(stderr, "net usershare add: failed to fchmod tmp file %s to 0644n",
                                full_path_tmp );
-               talloc_destroy(ctx);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
 
        /* Create the in-memory image of the file. */
-       file_img = talloc_strdup(ctx, "#VERSION 1\npath=");
-       file_img = talloc_asprintf_append(file_img, "%s\ncomment=%s\nusershare_acl=%s\n",
-                       us_path, us_comment, us_acl );
+       file_img = talloc_strdup(ctx, "#VERSION 2\npath=");
+       file_img = talloc_asprintf_append(file_img, "%s\ncomment=%s\nusershare_acl=%s\nguest_ok=%c\n",
+                       us_path, us_comment, us_acl, guest_ok ? 'y' : 'n');
 
        to_write = strlen(file_img);
 
@@ -685,7 +914,7 @@ static int net_usershare_add(int argc, const char **argv)
                d_fprintf(stderr, "net usershare add: failed to write %u bytes to file %s. Error was %s\n",
                        (unsigned int)to_write, full_path_tmp, strerror(errno));
                unlink(full_path_tmp);
-               talloc_destroy(ctx);
+               TALLOC_FREE(ctx);
                SAFE_FREE(sharename);
                return -1;
        }
@@ -695,23 +924,23 @@ static int net_usershare_add(int argc, const char **argv)
                unlink(full_path_tmp);
                d_fprintf(stderr, "net usershare add: failed to add share %s. Error was %s\n",
                        sharename, strerror(errno));
-               talloc_destroy(ctx);
+               TALLOC_FREE(ctx);
                close(tmpfd);
                SAFE_FREE(sharename);
                return -1;
        }
 
        close(tmpfd);
-       talloc_destroy(ctx);
 
-       if (opt_long_list_entries) {
+       if (c->opt_long_list_entries) {
                const char *my_argv[2];
                my_argv[0] = sharename;
                my_argv[1] = NULL;
-               net_usershare_info(1, argv);
+               net_usershare_info(c, 1, my_argv);
        }
 
        SAFE_FREE(sharename);
+       TALLOC_FREE(ctx);
        return 0;
 }
 
@@ -720,7 +949,7 @@ static int net_usershare_add(int argc, const char **argv)
  List function.
 ***************************************************************************/
 
-static int list_fn(struct file_list *fl, void *private)
+static int list_fn(struct file_list *fl, void *priv)
 {
        d_printf("%s\n", fl->pathname);
        return 0;
@@ -731,18 +960,22 @@ static int list_fn(struct file_list *fl, void *private)
  List userlevel shares.
 ***************************************************************************/
 
-static int net_usershare_list(int argc, const char **argv)
+static int net_usershare_list(struct net_context *c, int argc,
+                             const char **argv)
 {
        fstring wcard;
-       BOOL only_ours = True;
+       bool only_ours = true;
        int ret = -1;
-       struct priv_info pi;
+       struct us_priv_info pi;
        TALLOC_CTX *ctx;
 
        fstrcpy(wcard, "*");
 
-       if (opt_long_list_entries) {
-               only_ours = False;
+       if (c->display_usage)
+               return net_usershare_list_usage(c, argc, argv);
+
+       if (c->opt_long_list_entries) {
+               only_ours = false;
        }
 
        switch (argc) {
@@ -752,7 +985,7 @@ static int net_usershare_list(int argc, const char **argv)
                        fstrcpy(wcard, argv[0]);
                        break;
                default:
-                       return net_usershare_list_usage(argc, argv);
+                       return net_usershare_list_usage(c, argc, argv);
        }
 
        strlower_m(wcard);
@@ -771,39 +1004,50 @@ static int net_usershare_list(int argc, const char **argv)
        return ret;
 }
 
-/***************************************************************************
- Handle "net usershare help *" subcommands.
-***************************************************************************/
-
-int net_usershare_help(int argc, const char **argv)
-{
-       struct functable func[] = {
-               {"ADD", net_usershare_add_usage},
-               {"DELETE", net_usershare_delete_usage},
-               {"INFO", net_usershare_info_usage},
-               {"LIST", net_usershare_list_usage},
-               {NULL, NULL}};
-
-       return net_run_function(argc, argv, func, net_usershare_usage);
-}
-
 /***************************************************************************
  Entry-point for all the USERSHARE functions.
 ***************************************************************************/
 
-int net_usershare(int argc, const char **argv)
+int net_usershare(struct net_context *c, int argc, const char **argv)
 {
        SMB_STRUCT_DIR *dp;
 
        struct functable func[] = {
-               {"ADD", net_usershare_add},
-               {"DELETE", net_usershare_delete},
-               {"INFO", net_usershare_info},
-               {"LIST", net_usershare_list},
-               {"HELP", net_usershare_help},
-               {NULL, NULL}
+               {
+                       "add",
+                       net_usershare_add,
+                       NET_TRANSPORT_LOCAL,
+                       "Add/modify user defined share",
+                       "net usershare add\n"
+                       "    Add/modify user defined share"
+               },
+               {
+                       "delete",
+                       net_usershare_delete,
+                       NET_TRANSPORT_LOCAL,
+                       "Delete user defined share",
+                       "net usershare delete\n"
+                       "    Delete user defined share"
+               },
+               {
+                       "info",
+                       net_usershare_info,
+                       NET_TRANSPORT_LOCAL,
+                       "Display information about a user defined share",
+                       "net usershare info\n"
+                       "    Display information about a user defined share"
+               },
+               {
+                       "list",
+                       net_usershare_list,
+                       NET_TRANSPORT_LOCAL,
+                       "List user defined shares",
+                       "net usershare list\n"
+                       "    List user defined shares"
+               },
+               {NULL, NULL, 0, NULL, NULL}
        };
-       
+
        if (lp_usershare_max_shares() == 0) {
                d_fprintf(stderr, "net usershare: usershares are currently disabled\n");
                return -1;
@@ -825,5 +1069,5 @@ int net_usershare(int argc, const char **argv)
        }
        sys_closedir(dp);
 
-       return net_run_function(argc, argv, func, net_usershare_usage);
+       return net_run_function(c, argc, argv, "net usershare", func);
 }