diff options
-rw-r--r-- | source3/utils/net_usershare.c | 212 |
1 files changed, 151 insertions, 61 deletions
diff --git a/source3/utils/net_usershare.c b/source3/utils/net_usershare.c index 19fa8fad2f..f95b1bdc6e 100644 --- a/source3/utils/net_usershare.c +++ b/source3/utils/net_usershare.c @@ -127,12 +127,17 @@ int net_usershare_usage(int argc, const char **argv) /*************************************************************************** ***************************************************************************/ -static void get_basepath(pstring basepath) +static char *get_basepath(TALLOC_CTX *ctx) { - pstrcpy(basepath, lp_usershare_path()); + 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; } /*************************************************************************** @@ -141,7 +146,7 @@ static void get_basepath(pstring basepath) static int net_usershare_delete(int argc, const char **argv) { - pstring us_path; + char *us_path; char *sharename; if (argc != 1) { @@ -161,9 +166,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. " @@ -196,9 +206,11 @@ static int get_share_list(TALLOC_CTX *ctx, const char *wcard, bool only_ours) 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", @@ -208,7 +220,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 .. */ @@ -222,9 +234,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", @@ -246,13 +263,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; } @@ -299,10 +318,10 @@ static int info_fn(struct file_list *fl, void *priv) int fd = -1; int numlines = 0; SEC_DESC *psd = NULL; - pstring basepath; + char *basepath; char *sharepath = NULL; char *comment = NULL; - pstring acl_str; + char *acl_str; int num_aces; char sep_str[2]; enum usershare_err us_err; @@ -311,9 +330,16 @@ static int info_fn(struct file_list *fl, void *priv) 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); @@ -366,7 +392,10 @@ static int info_fn(struct file_list *fl, void *priv) 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++) { const char *domain; @@ -377,30 +406,53 @@ static int info_fn(struct file_list *fl, void *priv) if (NT_STATUS_IS_OK(ntstatus)) { if (domain && *domain) { - pstrcat(acl_str, domain); - pstrcat(acl_str, sep_str); + acl_str = talloc_asprintf_append(acl_str, + "%s%s", + domain, + sep_str); + if (!acl_str) { + return -1; + } } - pstrcat(acl_str,name); + acl_str = talloc_asprintf_append(acl_str, + "%s", + name); + if (!acl_str) { + return -1; + } + } else { fstring sidstr; sid_to_string(sidstr, &psd->dacl->aces[num_aces].trustee); - pstrcat(acl_str,sidstr); + 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->aces[num_aces].type == SEC_ACE_TYPE_ACCESS_DENIED) { - pstrcat(acl_str, "D,"); + acl_str = talloc_asprintf_append(acl_str, "D,"); + if (!acl_str) { + return -1; + } } else { if (psd->dacl->aces[num_aces].access_mask & GENERIC_ALL_ACCESS) { - pstrcat(acl_str, "F,"); + 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 ); @@ -466,10 +518,14 @@ static int count_num_usershares(void) { SMB_STRUCT_DIR *dp; SMB_STRUCT_DIRENT *de; - pstring basepath; int num_usershares = 0; + TALLOC_CTX *ctx = talloc_tos(); + char *basepath = get_basepath(ctx); + + if (!basepath) { + return -1; + } - get_basepath(basepath); dp = sys_opendir(basepath); if (!dp) { d_fprintf(stderr, "count_num_usershares: cannot open usershare directory %s. Error %s\n", @@ -479,7 +535,7 @@ static int count_num_usershares(void) while((de = sys_readdir(dp)) != 0) { SMB_STRUCT_STAT sbuf; - pstring path; + char *path; const char *n = de->d_name; /* Ignore . and .. */ @@ -493,9 +549,14 @@ static int count_num_usershares(void) d_fprintf(stderr, "count_num_usershares: 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, "count_num_usershares: can't lstat file %s. Error was %s\n", @@ -521,12 +582,12 @@ static int count_num_usershares(void) static int net_usershare_add(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; @@ -573,6 +634,7 @@ static int net_usershare_add(int argc, const char **argv) arg_acl = "S-1-1-0:R"; } if (!strnequal(argv[4], "guest_ok=", 9)) { + TALLOC_FREE(ctx); return net_usershare_add_usage(argc, argv); } switch (argv[4][9]) { @@ -584,7 +646,8 @@ static int net_usershare_add(int argc, const char **argv) case 'N': guest_ok = False; break; - default: + default: + TALLOC_FREE(ctx); return net_usershare_add_usage(argc, argv); } break; @@ -595,6 +658,7 @@ static int net_usershare_add(int argc, const char **argv) 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; } @@ -603,6 +667,7 @@ static int net_usershare_add(int argc, const char **argv) 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; } @@ -611,21 +676,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; } @@ -635,6 +720,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; } @@ -642,6 +728,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; } @@ -655,6 +742,7 @@ static int net_usershare_add(int argc, const char **argv) "\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; } @@ -663,10 +751,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; @@ -682,7 +772,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; } @@ -698,7 +788,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; } @@ -706,7 +796,7 @@ 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; } @@ -714,7 +804,7 @@ static int net_usershare_add(int argc, const char **argv) /* Get the name */ if ((name = talloc_strndup(ctx, pacl, pcolon - pacl)) == NULL) { d_fprintf(stderr, "talloc_strndup failed\n"); - talloc_destroy(ctx); + TALLOC_FREE(ctx); SAFE_FREE(sharename); return -1; } @@ -729,7 +819,7 @@ static int net_usershare_add(int argc, const char **argv) } else { d_fprintf(stderr, "\n"); } - talloc_destroy(ctx); + TALLOC_FREE(ctx); SAFE_FREE(sharename); return -1; } @@ -749,7 +839,7 @@ static int net_usershare_add(int argc, const char **argv) d_fprintf(stderr, "net usershare add: guest_ok=y requested " "but the \"usershare allow guests\" parameter is not enabled " "by this server.\n"); - talloc_destroy(ctx); + TALLOC_FREE(ctx); SAFE_FREE(sharename); return -1; } @@ -760,7 +850,7 @@ static int net_usershare_add(int argc, const char **argv) 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; } @@ -769,7 +859,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; } @@ -778,7 +868,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; } @@ -786,15 +876,15 @@ 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; } @@ -810,7 +900,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; } @@ -820,14 +910,13 @@ 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) { const char *my_argv[2]; @@ -837,6 +926,7 @@ static int net_usershare_add(int argc, const char **argv) } SAFE_FREE(sharename); + TALLOC_FREE(ctx); return 0; } |