From 5f6fe3bda69a9d6c38eaa0a34970eae7533d138b Mon Sep 17 00:00:00 2001 From: Aliaksei Karaliou Date: Thu, 27 Dec 2018 04:18:28 -0500 Subject: [PATCH 01/16] s3:util: Move popen wrappers to lib/util When linked into Samba3 libraries, sys_popen()/sys_pclose() cannot be used in lower level libraries because of circular dependencies. This patch moves them into common samba-util library. Extra fix needed to enable easy back-port of code for: BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Aliaksei Karaliou Reviewed-by: Garming Sam Reviewed-by: Andrew Bartlett (cherry picked from commit 65ea3f2a461c0eeca7d14c4258eea52e19f0ed06) --- {source3/lib => lib/util}/sys_popen.c | 2 +- {source3/lib => lib/util}/sys_popen.h | 0 lib/util/wscript_build | 4 ++-- source3/lib/util_file.c | 2 +- source3/wscript_build | 1 - 5 files changed, 4 insertions(+), 5 deletions(-) rename {source3/lib => lib/util}/sys_popen.c (99%) rename {source3/lib => lib/util}/sys_popen.h (100%) diff --git a/source3/lib/sys_popen.c b/lib/util/sys_popen.c similarity index 99% rename from source3/lib/sys_popen.c rename to lib/util/sys_popen.c index 607e599cf3f..709f1822f33 100644 --- a/source3/lib/sys_popen.c +++ b/lib/util/sys_popen.c @@ -21,7 +21,7 @@ #include "system/wait.h" #include "system/filesys.h" #include -#include "lib/sys_popen.h" +#include "lib/util/sys_popen.h" #include "lib/util/debug.h" /************************************************************************** diff --git a/source3/lib/sys_popen.h b/lib/util/sys_popen.h similarity index 100% rename from source3/lib/sys_popen.h rename to lib/util/sys_popen.h diff --git a/lib/util/wscript_build b/lib/util/wscript_build index 8fc402062fb..6134ca66641 100644 --- a/lib/util/wscript_build +++ b/lib/util/wscript_build @@ -68,14 +68,14 @@ bld.SAMBA_LIBRARY('talloc_report', ) bld.SAMBA_SUBSYSTEM('samba-util-core', - source='''data_blob.c util_file.c time.c + source='''data_blob.c util_file.c sys_popen.c time.c signal.c util.c idtree.c fault.c substitute.c util_process.c util_strlist.c strv_util.c bitmap.c select.c pidfile.c become_daemon.c mkdir_p.c''', deps='''time-basic samba-debug socket-blocking talloc tevent execinfo pthread strv tini''', - public_deps='systemd systemd-daemon', + public_deps='systemd systemd-daemon sys_rw', local_include=False) bld.SAMBA_LIBRARY('iov_buf', diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index d3139e55b57..94e1118225f 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -22,7 +22,7 @@ #include "lib/util/debug.h" #include "lib/util/samba_util.h" #include "lib/util/sys_rw.h" -#include "lib/sys_popen.h" +#include "lib/util/sys_popen.h" #include "lib/async_req/async_sock.h" #include "lib/util/tevent_unix.h" diff --git a/source3/wscript_build b/source3/wscript_build index bbcfc72a714..6be87ba8139 100644 --- a/source3/wscript_build +++ b/source3/wscript_build @@ -312,7 +312,6 @@ bld.SAMBA3_SUBSYSTEM('samba3util', lib/util_sock.c lib/util_tsock.c lib/util_transfer_file.c - lib/sys_popen.c ''', deps=''' CHARSET3 -- 2.22.0.rc1.257.g3120a18244-goog From 0196bce57395b85588ca3b7716eb78b363b4c735 Mon Sep 17 00:00:00 2001 From: Aliaksei Karaliou Date: Thu, 27 Dec 2018 04:25:47 -0500 Subject: [PATCH 02/16] s3:util: Move static file_pload() function to lib/util file_pload() is static private function in Samba3 library, however it does not have any special dependencies and might be widely used as common function, so moving it into common samba-util library. Extra fix needed to enable easy back-port of code for: BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Aliaksei Karaliou Reviewed-by: Garming Sam Reviewed-by: Andrew Bartlett (cherry picked from commit d21fc7d8b86b0cddc619ffe528d9cd93eeedbb0b) --- lib/util/samba_util.h | 5 +++++ lib/util/util_file.c | 48 +++++++++++++++++++++++++++++++++++++++++ source3/lib/util_file.c | 47 ---------------------------------------- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 7b96a595d43..fff5478cd6d 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -394,6 +394,11 @@ _PUBLIC_ int fdprintf(int fd, const char *format, ...) PRINTF_ATTRIBUTE(2,3); */ bool file_compare(const char *path1, const char *path2); +/* + load from a pipe into memory. + */ +char *file_pload(const char *syscmd, size_t *size); + /* The following definitions come from lib/util/util.c */ diff --git a/lib/util/util_file.c b/lib/util/util_file.c index 926eda240f6..90d39f7cdd3 100644 --- a/lib/util/util_file.c +++ b/lib/util/util_file.c @@ -24,6 +24,8 @@ #include "system/filesys.h" #include #include "lib/util/samba_util.h" +#include "lib/util/sys_popen.h" +#include "lib/util/sys_rw.h" #include "lib/util/debug.h" /** @@ -362,3 +364,49 @@ bool file_compare(const char *path1, const char *path2) talloc_free(mem_ctx); return true; } + + +/** + Load from a pipe into memory. +**/ +char *file_pload(const char *syscmd, size_t *size) +{ + int fd, n; + char *p; + char buf[1024]; + size_t total; + + fd = sys_popen(syscmd); + if (fd == -1) { + return NULL; + } + + p = NULL; + total = 0; + + while ((n = sys_read(fd, buf, sizeof(buf))) > 0) { + p = talloc_realloc(NULL, p, char, total + n + 1); + if (!p) { + DEBUG(0,("file_pload: failed to expand buffer!\n")); + close(fd); + return NULL; + } + memcpy(p+total, buf, n); + total += n; + } + + if (p) { + p[total] = 0; + } + + /* FIXME: Perhaps ought to check that the command completed + * successfully (returned 0); if not the data may be + * truncated. */ + sys_pclose(fd); + + if (size) { + *size = total; + } + + return p; +} diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 94e1118225f..cfbcf278caa 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -151,53 +151,6 @@ int file_pload_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, return 0; } -/** - Load from a pipe into memory. -**/ - -static char *file_pload(const char *syscmd, size_t *size) -{ - int fd, n; - char *p; - char buf[1024]; - size_t total; - - fd = sys_popen(syscmd); - if (fd == -1) { - return NULL; - } - - p = NULL; - total = 0; - - while ((n = sys_read(fd, buf, sizeof(buf))) > 0) { - p = talloc_realloc(NULL, p, char, total + n + 1); - if (!p) { - DEBUG(0,("file_pload: failed to expand buffer!\n")); - close(fd); - return NULL; - } - memcpy(p+total, buf, n); - total += n; - } - - if (p) { - p[total] = 0; - } - - /* FIXME: Perhaps ought to check that the command completed - * successfully (returned 0); if not the data may be - * truncated. */ - sys_pclose(fd); - - if (size) { - *size = total; - } - - return p; -} - - /** Load a pipe into memory and return an array of pointers to lines in the data -- 2.22.0.rc1.257.g3120a18244-goog From 03f6e21846746c4b98e1ac0703147ae5882ef4c3 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 May 2019 21:45:21 -0700 Subject: [PATCH 03/16] lib: popen: Prepare to remove sys_popen(). Add sys_popenv(char * const argl[]) that uses a NULL terminated vector array of args. Change sys_popen() to split up its command string and call sys_popenv(). Once all callers are converted to sys_popenv() we can remove sys_popen(). BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit f20538de041eed1cadbabe2149b2b7cfcb779cb5) --- lib/util/sys_popen.c | 57 ++++++++++++++++++++++++++++---------------- lib/util/sys_popen.h | 1 + 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/lib/util/sys_popen.c b/lib/util/sys_popen.c index 709f1822f33..65040508c81 100644 --- a/lib/util/sys_popen.c +++ b/lib/util/sys_popen.c @@ -110,14 +110,19 @@ typedef struct _popen_list static popen_list *popen_chain; -int sys_popen(const char *command) +int sys_popenv(char * const argl[]) { int parent_end, child_end; int pipe_fds[2]; popen_list *entry = NULL; - char **argl = NULL; + const char *command = argl[0]; int ret; + if (argl == NULL) { + errno = EINVAL; + return -1; + } + if (!*command) { errno = EINVAL; return -1; @@ -125,8 +130,8 @@ int sys_popen(const char *command) ret = pipe(pipe_fds); if (ret < 0) { - DEBUG(0, ("sys_popen: error opening pipe: %s\n", - strerror(errno))); + DBG_ERR("error opening pipe: %s\n", + strerror(errno)); return -1; } @@ -135,24 +140,14 @@ int sys_popen(const char *command) entry = talloc_zero(NULL, popen_list); if (entry == NULL) { - DEBUG(0, ("sys_popen: malloc failed\n")); - goto err_exit; - } - - /* - * Extract the command and args into a NULL terminated array. - */ - - argl = extract_args(NULL, command); - if (argl == NULL) { - DEBUG(0, ("sys_popen: extract_args() failed: %s\n", strerror(errno))); + DBG_ERR("talloc failed\n"); goto err_exit; } entry->child_pid = fork(); if (entry->child_pid == -1) { - DEBUG(0, ("sys_popen: fork failed: %s\n", strerror(errno))); + DBG_ERR("fork failed: %s\n", strerror(errno)); goto err_exit; } @@ -182,8 +177,8 @@ int sys_popen(const char *command) ret = execv(argl[0], argl); if (ret == -1) { - DEBUG(0, ("sys_popen: ERROR executing command " - "'%s': %s\n", command, strerror(errno))); + DBG_ERR("ERROR executing command " + "'%s': %s\n", command, strerror(errno)); } _exit (127); } @@ -193,7 +188,6 @@ int sys_popen(const char *command) */ close (child_end); - TALLOC_FREE(argl); /* Link into popen_chain. */ entry->next = popen_chain; @@ -205,12 +199,35 @@ int sys_popen(const char *command) err_exit: TALLOC_FREE(entry); - TALLOC_FREE(argl); close(pipe_fds[0]); close(pipe_fds[1]); return -1; } +int sys_popen(const char *command) +{ + char **argl = NULL; + int ret; + + if (!*command) { + errno = EINVAL; + return -1; + } + + /* + * Extract the command and args into a NULL terminated array. + */ + + argl = extract_args(NULL, command); + if (argl == NULL) { + DBG_ERR("extract_args() failed: %s\n", strerror(errno)); + return -1; + } + ret = sys_popenv(argl); + TALLOC_FREE(argl); + return ret; +} + /************************************************************************** Wrapper for pclose. Modified from the glibc sources. ****************************************************************************/ diff --git a/lib/util/sys_popen.h b/lib/util/sys_popen.h index 6807d3c5061..80ea70efa75 100644 --- a/lib/util/sys_popen.h +++ b/lib/util/sys_popen.h @@ -21,6 +21,7 @@ #define __LIB_SYS_POPEN_H__ int sys_popen(const char *command); +int sys_popenv(char * const argl[]); int sys_pclose(int fd); #endif -- 2.22.0.rc1.257.g3120a18244-goog From 4dd3c058a1241d8f2e0b4c935a9932ef280b3756 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 May 2019 21:56:13 -0700 Subject: [PATCH 04/16] lib: util: Add file_ploadv(). Not yet used. Duplicate code to file_pload() except uses vectored argument list. file_pload() will be removed once all callers are converted. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 5c34fa0b85e4d9a3c5fd4fa0b39af4772ec023db) --- lib/util/samba_util.h | 1 + lib/util/util_file.c | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index fff5478cd6d..a4817f40ad9 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -398,6 +398,7 @@ bool file_compare(const char *path1, const char *path2); load from a pipe into memory. */ char *file_pload(const char *syscmd, size_t *size); +char *file_ploadv(char * const argl[], size_t *size); /* The following definitions come from lib/util/util.c */ diff --git a/lib/util/util_file.c b/lib/util/util_file.c index 90d39f7cdd3..af709dc52d0 100644 --- a/lib/util/util_file.c +++ b/lib/util/util_file.c @@ -410,3 +410,49 @@ char *file_pload(const char *syscmd, size_t *size) return p; } + +/** + Load from a pipe into memory. +**/ +char *file_ploadv(char * const argl[], size_t *size) +{ + int fd, n; + char *p = NULL; + char buf[1024]; + size_t total; + + fd = sys_popenv(argl); + if (fd == -1) { + return NULL; + } + + total = 0; + + while ((n = sys_read(fd, buf, sizeof(buf))) > 0) { + p = talloc_realloc(NULL, p, char, total + n + 1); + if (p == NULL) { + DBG_ERR("failed to expand buffer!\n"); + close(fd); + return NULL; + } + memcpy(p+total, buf, n); + total += n; + } + + if (p != NULL) { + p[total] = 0; + } + + /* + * FIXME: Perhaps ought to check that the command completed + * successfully (returned 0); if not the data may be + * truncated. + */ + sys_pclose(fd); + + if (size) { + *size = total; + } + + return p; +} -- 2.22.0.rc1.257.g3120a18244-goog From 64ed89b6ed344625117c6acba6a1673f5f429169 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 May 2019 22:10:51 -0700 Subject: [PATCH 05/16] s3: lib: util: Add file_lines_ploadv(). Not yet used. Duplicate code to file_lines_pload() except uses vectored argument list. file_lines_pload() will be removed once all callers are converted. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 3b19412baedfffb7adc2a79471f5b17990259c31) --- source3/lib/util_file.c | 15 +++++++++++++++ source3/lib/util_file.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index cfbcf278caa..eeb749be36c 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -170,3 +170,18 @@ char **file_lines_pload(TALLOC_CTX *mem_ctx, const char *syscmd, return file_lines_parse(p, size, numlines, mem_ctx); } + +char **file_lines_ploadv(TALLOC_CTX *mem_ctx, + char * const argl[], + int *numlines) +{ + char *p = NULL; + size_t size; + + p = file_ploadv(argl, &size); + if (!p) { + return NULL; + } + + return file_lines_parse(p, size, numlines, mem_ctx); +} diff --git a/source3/lib/util_file.h b/source3/lib/util_file.h index 9175ed0dcee..85bcf06f38c 100644 --- a/source3/lib/util_file.h +++ b/source3/lib/util_file.h @@ -31,5 +31,8 @@ int file_pload_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, char **file_lines_pload(TALLOC_CTX *mem_ctx, const char *syscmd, int *numlines); +char **file_lines_ploadv(TALLOC_CTX *mem_ctx, + char * const argl[], + int *numlines); #endif -- 2.22.0.rc1.257.g3120a18244-goog From 567938eaa573365cb70f81aafee510bf306e0c71 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 May 2019 22:11:49 -0700 Subject: [PATCH 06/16] s3: smbd: Convert dfree code to use file_lines_ploadv(). BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit d6453e1ffd92c7754862389a933a9fd9089ce518) --- source3/smbd/dfree.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/source3/smbd/dfree.c b/source3/smbd/dfree.c index 05f6d6988c1..5c6c0fb00ae 100644 --- a/source3/smbd/dfree.c +++ b/source3/smbd/dfree.c @@ -60,7 +60,7 @@ uint64_t sys_disk_free(connection_struct *conn, struct smb_filename *fname, uint64_t dsize_q = 0; const char *dfree_command; static bool dfree_broken = false; - const char *path = fname->base_name; + char *path = fname->base_name; (*dfree) = (*dsize) = 0; (*bsize) = 512; @@ -73,20 +73,31 @@ uint64_t sys_disk_free(connection_struct *conn, struct smb_filename *fname, if (dfree_command && *dfree_command) { const char *p; char **lines = NULL; - char *syscmd = NULL; + char **argl = NULL; - syscmd = talloc_asprintf(talloc_tos(), - "%s %s", - dfree_command, - path); + argl = talloc_zero_array(talloc_tos(), + char *, + 3); + if (argl == NULL) { + return (uint64_t)-1; + } - if (!syscmd) { + argl[0] = talloc_strdup(argl, dfree_command); + if (argl[0] == NULL) { + TALLOC_FREE(argl); return (uint64_t)-1; } + argl[1] = path; + argl[2] = NULL; + + DBG_NOTICE("Running command '%s %s'\n", + dfree_command, + path); + + lines = file_lines_ploadv(talloc_tos(), argl, NULL); - DEBUG (3, ("disk_free: Running command '%s'\n", syscmd)); + TALLOC_FREE(argl); - lines = file_lines_pload(talloc_tos(), syscmd, NULL); if (lines != NULL) { char *line = lines[0]; @@ -114,9 +125,9 @@ uint64_t sys_disk_free(connection_struct *conn, struct smb_filename *fname, goto dfree_done; } - DEBUG (0, ("disk_free: file_lines_load() failed for " - "command '%s'. Error was : %s\n", - syscmd, strerror(errno) )); + DBG_ERR("file_lines_load() failed for " + "command '%s %s'. Error was : %s\n", + dfree_command, path, strerror(errno)); } if (SMB_VFS_DISK_FREE(conn, fname, bsize, dfree, dsize) == -- 2.22.0.rc1.257.g3120a18244-goog From eef9163bac6e8e048bb0c6f4513463a2b3deecea Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 May 2019 22:18:40 -0700 Subject: [PATCH 07/16] s3: smbd: Convert print_svid code to use file_lines_ploadv(). BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit f9ccf1cc3df13138a1a4b645c8190238ce011f04) --- source3/printing/print_svid.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/source3/printing/print_svid.c b/source3/printing/print_svid.c index f041ef482ad..d20fdad88e3 100644 --- a/source3/printing/print_svid.c +++ b/source3/printing/print_svid.c @@ -41,6 +41,7 @@ bool sysv_cache_reload(struct pcap_cache **_pcache) char **lines; int i; struct pcap_cache *pcache = NULL; + char **argl = NULL; #if defined(HPUX) DEBUG(5, ("reloading hpux printcap cache\n")); @@ -48,7 +49,23 @@ bool sysv_cache_reload(struct pcap_cache **_pcache) DEBUG(5, ("reloading sysv printcap cache\n")); #endif - lines = file_lines_pload(talloc_tos(), "/usr/bin/lpstat -v", NULL); + argl = talloc_zero_array(talloc_tos(), char *, 3); + if (argl == NULL) { + return false; + } + argl[0] = talloc_strdup(argl, "/usr/bin/lpstat"); + if (argl[0] == NULL) { + TALLOC_FREE(argl); + return false; + } + argl[1] = talloc_strdup(argl, "-v"); + if (argl[1] == NULL) { + TALLOC_FREE(argl); + return false; + } + argl[2] = NULL; + + lines = file_lines_ploadv(talloc_tos(), argl, NULL); if (lines == NULL) { #if defined(HPUX) @@ -59,7 +76,14 @@ bool sysv_cache_reload(struct pcap_cache **_pcache) */ char **scheduler; - scheduler = file_lines_pload("/usr/bin/lpstat -r", NULL); + + argl[1] = talloc_strdup(argl, "-r"); + if (argl[1] == NULL) { + TALLOC_FREE(argl); + return false; + } + scheduler = file_lines_ploadv(talloc_tos(), argl, NULL); + TALLOC_FREE(argl); if(!strcmp(*scheduler,"scheduler is running")){ DEBUG(3,("No Printers found!!!\n")); TALLOC_FREE(scheduler); @@ -75,6 +99,7 @@ bool sysv_cache_reload(struct pcap_cache **_pcache) return False; #endif } + TALLOC_FREE(argl); for (i = 0; lines[i]; i++) { char *name, *tmp; -- 2.22.0.rc1.257.g3120a18244-goog From fb10a243f7250fc2e9136aeab25ca0cabbaab74d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:08:15 -0700 Subject: [PATCH 08/16] s3: smbd: Convert sysquotas.c code to use file_lines_ploadv(). BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 4a63e3b9659c8715d436c66dee8bf420e2ea89fb) --- source3/lib/sysquotas.c | 126 ++++++++++++++++++++++++++++++++++------ 1 file changed, 107 insertions(+), 19 deletions(-) diff --git a/source3/lib/sysquotas.c b/source3/lib/sysquotas.c index 9b2d37b8375..ed1fb3c3823 100644 --- a/source3/lib/sysquotas.c +++ b/source3/lib/sysquotas.c @@ -248,8 +248,8 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t if (get_quota_command && *get_quota_command) { const char *p; char *p2; - char *syscmd = NULL; int _id = -1; + char **argl = NULL; switch(qtype) { case SMB_USER_QUOTA_TYPE: @@ -265,15 +265,40 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t return -1; } - if (asprintf(&syscmd, "%s %s %d %d", - get_quota_command, path, qtype, _id) < 0) { + argl = talloc_zero_array(talloc_tos(), char *, 5); + if (argl == NULL) { return -1; } + argl[0] = talloc_strdup(argl, get_quota_command); + if (argl[0] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[1] = talloc_strdup(argl, path); + if (argl[1] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[2] = talloc_asprintf(argl, "%d", qtype); + if (argl[2] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[3] = talloc_asprintf(argl, "%d", _id); + if (argl[3] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[4] = NULL; - DEBUG (3, ("get_quota: Running command %s\n", syscmd)); + DBG_NOTICE("Running command %s %s %d %d\n", + get_quota_command, + path, + qtype, + _id); - lines = file_lines_pload(talloc_tos(), syscmd, NULL); - SAFE_FREE(syscmd); + lines = file_lines_ploadv(talloc_tos(), argl, NULL); + TALLOC_FREE(argl); if (lines) { char *line = lines[0]; @@ -391,8 +416,8 @@ static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t set_quota_command = lp_set_quota_command(talloc_tos()); if (set_quota_command && *set_quota_command) { char **lines = NULL; - char *syscmd = NULL; int _id = -1; + char **argl = NULL; switch(qtype) { case SMB_USER_QUOTA_TYPE: @@ -407,21 +432,84 @@ static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t return -1; } - if (asprintf(&syscmd, - "%s %s %d %d " - "%u %llu %llu " - "%llu %llu %llu ", - set_quota_command, path, qtype, _id, dp->qflags, - (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit, - (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit, - (long long unsigned)dp->bsize) < 0) { + argl = talloc_zero_array(talloc_tos(), char *, 11); + if (argl == NULL) { return -1; } + argl[0] = talloc_strdup(argl, set_quota_command); + if (argl[0] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[1] = talloc_strdup(argl, path); + if (argl[1] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[2] = talloc_asprintf(argl, "%d", qtype); + if (argl[2] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[3] = talloc_asprintf(argl, "%d", _id); + if (argl[3] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[4] = talloc_asprintf(argl, "%u", dp->qflags); + if (argl[4] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[5] = talloc_asprintf(argl, "%llu", + (long long unsigned)dp->softlimit); + if (argl[5] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[6] = talloc_asprintf(argl, "%llu", + (long long unsigned)dp->hardlimit); + if (argl[6] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[7] = talloc_asprintf(argl, "%llu", + (long long unsigned)dp->isoftlimit); + if (argl[7] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[8] = talloc_asprintf(argl, "%llu", + (long long unsigned)dp->ihardlimit); + if (argl[8] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[9] = talloc_asprintf(argl, "%llu", + (long long unsigned)dp->bsize); + if (argl[9] == NULL) { + TALLOC_FREE(argl); + return -1; + } + argl[10] = NULL; - DBG_NOTICE("set_quota: Running command %s\n", syscmd); - - lines = file_lines_pload(talloc_tos(), syscmd, NULL); - SAFE_FREE(syscmd); + DBG_NOTICE("Running command " + "%s %s %d %d " + "%u %llu %llu " + "%llu %llu %llu ", + set_quota_command, + path, + qtype, + _id, + dp->qflags, + (long long unsigned)dp->softlimit, + (long long unsigned)dp->hardlimit, + (long long unsigned)dp->isoftlimit, + (long long unsigned)dp->ihardlimit, + (long long unsigned)dp->bsize); + + lines = file_lines_ploadv(talloc_tos(), argl, NULL); + TALLOC_FREE(argl); if (lines) { char *line = lines[0]; -- 2.22.0.rc1.257.g3120a18244-goog From badd41b1d9a54b5c78ce6de934fa2b2be4b28853 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:10:40 -0700 Subject: [PATCH 09/16] s3: lib: Remove file_lines_pload(). No longer used. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 02bc0ce9d22117b464bae47c5d09c45b4f7c2272) --- source3/lib/util_file.c | 14 -------------- source3/lib/util_file.h | 3 --- 2 files changed, 17 deletions(-) diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index eeb749be36c..8f91f7d00fb 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -157,20 +157,6 @@ int file_pload_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, must be freed with TALLOC_FREE. **/ -char **file_lines_pload(TALLOC_CTX *mem_ctx, const char *syscmd, - int *numlines) -{ - char *p; - size_t size; - - p = file_pload(syscmd, &size); - if (!p) { - return NULL; - } - - return file_lines_parse(p, size, numlines, mem_ctx); -} - char **file_lines_ploadv(TALLOC_CTX *mem_ctx, char * const argl[], int *numlines) diff --git a/source3/lib/util_file.h b/source3/lib/util_file.h index 85bcf06f38c..9cf00aae893 100644 --- a/source3/lib/util_file.h +++ b/source3/lib/util_file.h @@ -28,9 +28,6 @@ struct tevent_req *file_pload_send(TALLOC_CTX *mem_ctx, const char *syscmd, size_t maxsize); int file_pload_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, uint8_t **buf); - -char **file_lines_pload(TALLOC_CTX *mem_ctx, const char *syscmd, - int *numlines); char **file_lines_ploadv(TALLOC_CTX *mem_ctx, char * const argl[], int *numlines); -- 2.22.0.rc1.257.g3120a18244-goog From b40eaf0552cbea1222b23e6848cb85c9072a2f75 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:14:53 -0700 Subject: [PATCH 10/16] lib: util: Remove file_pload() No longer used. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit c5729ae44219ec81008040d4d50f0f5fdf254201) --- lib/util/samba_util.h | 1 - lib/util/util_file.c | 46 ------------------------------------------- 2 files changed, 47 deletions(-) diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index a4817f40ad9..48417a38e0f 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -397,7 +397,6 @@ bool file_compare(const char *path1, const char *path2); /* load from a pipe into memory. */ -char *file_pload(const char *syscmd, size_t *size); char *file_ploadv(char * const argl[], size_t *size); /* The following definitions come from lib/util/util.c */ diff --git a/lib/util/util_file.c b/lib/util/util_file.c index af709dc52d0..102eac46131 100644 --- a/lib/util/util_file.c +++ b/lib/util/util_file.c @@ -365,52 +365,6 @@ bool file_compare(const char *path1, const char *path2) return true; } - -/** - Load from a pipe into memory. -**/ -char *file_pload(const char *syscmd, size_t *size) -{ - int fd, n; - char *p; - char buf[1024]; - size_t total; - - fd = sys_popen(syscmd); - if (fd == -1) { - return NULL; - } - - p = NULL; - total = 0; - - while ((n = sys_read(fd, buf, sizeof(buf))) > 0) { - p = talloc_realloc(NULL, p, char, total + n + 1); - if (!p) { - DEBUG(0,("file_pload: failed to expand buffer!\n")); - close(fd); - return NULL; - } - memcpy(p+total, buf, n); - total += n; - } - - if (p) { - p[total] = 0; - } - - /* FIXME: Perhaps ought to check that the command completed - * successfully (returned 0); if not the data may be - * truncated. */ - sys_pclose(fd); - - if (size) { - *size = total; - } - - return p; -} - /** Load from a pipe into memory. **/ -- 2.22.0.rc1.257.g3120a18244-goog From 245a3b4f094f0efc18872a76749c46f7e4b674a6 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:18:19 -0700 Subject: [PATCH 11/16] s3: lib: Add file_ploadv_send(). Not yet used. Preparing to remove file_pload_send() with this safer alternative. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 61054e53f53e5884902b566b1f9b454a3ff4741f) --- source3/lib/util_file.c | 29 +++++++++++++++++++++++++++++ source3/lib/util_file.h | 3 +++ 2 files changed, 32 insertions(+) diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 8f91f7d00fb..6ee2d58b532 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -65,6 +65,35 @@ struct tevent_req *file_pload_send(TALLOC_CTX *mem_ctx, return req; } +struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + char * const argl[], size_t maxsize) +{ + struct tevent_req *req = NULL, *subreq = NULL; + struct file_pload_state *state = NULL; + + req = tevent_req_create(mem_ctx, &state, struct file_pload_state); + if (req == NULL) { + return NULL; + } + state->ev = ev; + state->maxsize = maxsize; + + state->fd = sys_popenv(argl); + if (state->fd == -1) { + tevent_req_error(req, errno); + return tevent_req_post(req, ev); + } + talloc_set_destructor(state, file_pload_state_destructor); + + subreq = wait_for_read_send(state, state->ev, state->fd, false); + if (tevent_req_nomem(subreq, req)) { + return tevent_req_post(req, ev); + } + tevent_req_set_callback(subreq, file_pload_readable, req); + return req; +} + static int file_pload_state_destructor(struct file_pload_state *s) { if (s->fd != -1) { diff --git a/source3/lib/util_file.h b/source3/lib/util_file.h index 9cf00aae893..fe2782fd349 100644 --- a/source3/lib/util_file.h +++ b/source3/lib/util_file.h @@ -26,6 +26,9 @@ struct tevent_req *file_pload_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, const char *syscmd, size_t maxsize); +struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + char * const argl[], size_t maxsize); int file_pload_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, uint8_t **buf); char **file_lines_ploadv(TALLOC_CTX *mem_ctx, -- 2.22.0.rc1.257.g3120a18244-goog From 0544b8cc1c8d84f0828b16200d8c84b3f8b31e72 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:25:01 -0700 Subject: [PATCH 12/16] s3: winbind: Convert idmap to use file_ploadv_send(). BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 449d49946b295f574e1fed83b5a5ffbf1c1b1e30) --- source3/winbindd/idmap_script.c | 53 ++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/source3/winbindd/idmap_script.c b/source3/winbindd/idmap_script.c index 7b7f8844c36..269f047e1f4 100644 --- a/source3/winbindd/idmap_script.c +++ b/source3/winbindd/idmap_script.c @@ -63,7 +63,7 @@ struct idmap_script_context { */ struct idmap_script_xid2sid_state { - const char *syscmd; + char **argl; size_t idx; uint8_t *out; }; @@ -101,13 +101,32 @@ static struct tevent_req *idmap_script_xid2sid_send( return tevent_req_post(req, ev); } - state->syscmd = talloc_asprintf(state, "%s IDTOSID %cID %lu", script, key, - (unsigned long)xid.id); - if (tevent_req_nomem(state->syscmd, req)) { + state->argl = talloc_zero_array(state, + char *, + 5); + if (tevent_req_nomem(state->argl, req)) { return tevent_req_post(req, ev); } + state->argl[0] = talloc_strdup(state->argl, script); + if (tevent_req_nomem(state->argl[0], req)) { + return tevent_req_post(req, ev); + } + state->argl[1] = talloc_strdup(state->argl, "IDTOSID"); + if (tevent_req_nomem(state->argl[1], req)) { + return tevent_req_post(req, ev); + } + state->argl[2] = talloc_asprintf(state->argl, "%cID", key); + if (tevent_req_nomem(state->argl[2], req)) { + return tevent_req_post(req, ev); + } + state->argl[3] = talloc_asprintf(state->argl, "%lu", + (unsigned long)xid.id); + if (tevent_req_nomem(state->argl[3], req)) { + return tevent_req_post(req, ev); + } + state->argl[4] = NULL; - subreq = file_pload_send(state, ev, state->syscmd, 1024); + subreq = file_ploadv_send(state, ev, state->argl, 1024); if (tevent_req_nomem(subreq, req)) { return tevent_req_post(req, ev); } @@ -320,7 +339,7 @@ static NTSTATUS idmap_script_unixids_to_sids(struct idmap_domain *dom, } struct idmap_script_sid2xid_state { - const char *syscmd; + char **argl; size_t idx; uint8_t *out; }; @@ -344,13 +363,27 @@ static struct tevent_req *idmap_script_sid2xid_send( dom_sid_string_buf(sid, sidbuf, sizeof(sidbuf)); - state->syscmd = talloc_asprintf(state, "%s SIDTOID %s", - script, sidbuf); - if (tevent_req_nomem(state->syscmd, req)) { + state->argl = talloc_zero_array(state, + char *, + 4); + if (tevent_req_nomem(state->argl, req)) { + return tevent_req_post(req, ev); + } + state->argl[0] = talloc_strdup(state->argl, script); + if (tevent_req_nomem(state->argl[0], req)) { + return tevent_req_post(req, ev); + } + state->argl[1] = talloc_strdup(state->argl, "SIDTOID"); + if (tevent_req_nomem(state->argl[1], req)) { + return tevent_req_post(req, ev); + } + state->argl[2] = talloc_asprintf(state->argl, "%s", sidbuf); + if (tevent_req_nomem(state->argl[2], req)) { return tevent_req_post(req, ev); } + state->argl[3] = NULL; - subreq = file_pload_send(state, ev, state->syscmd, 1024); + subreq = file_ploadv_send(state, ev, state->argl, 1024); if (tevent_req_nomem(subreq, req)) { return tevent_req_post(req, ev); } -- 2.22.0.rc1.257.g3120a18244-goog From 6566affd5b6fa1c54437372293de03c8e1ce359a Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:32:05 -0700 Subject: [PATCH 13/16] s3: lib: Remove file_pload_send(). No longer used. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit d5c363d65d771c792523f2f3e526c90514212fc2) --- source3/lib/util_file.c | 29 ----------------------------- source3/lib/util_file.h | 3 --- 2 files changed, 32 deletions(-) diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 6ee2d58b532..770609d0b11 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -36,35 +36,6 @@ struct file_pload_state { static int file_pload_state_destructor(struct file_pload_state *s); static void file_pload_readable(struct tevent_req *subreq); -struct tevent_req *file_pload_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - const char *syscmd, size_t maxsize) -{ - struct tevent_req *req, *subreq; - struct file_pload_state *state; - - req = tevent_req_create(mem_ctx, &state, struct file_pload_state); - if (req == NULL) { - return NULL; - } - state->ev = ev; - state->maxsize = maxsize; - - state->fd = sys_popen(syscmd); - if (state->fd == -1) { - tevent_req_error(req, errno); - return tevent_req_post(req, ev); - } - talloc_set_destructor(state, file_pload_state_destructor); - - subreq = wait_for_read_send(state, state->ev, state->fd, false); - if (tevent_req_nomem(subreq, req)) { - return tevent_req_post(req, ev); - } - tevent_req_set_callback(subreq, file_pload_readable, req); - return req; -} - struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, char * const argl[], size_t maxsize) diff --git a/source3/lib/util_file.h b/source3/lib/util_file.h index fe2782fd349..97fcf89996d 100644 --- a/source3/lib/util_file.h +++ b/source3/lib/util_file.h @@ -23,9 +23,6 @@ #include "replace.h" #include -struct tevent_req *file_pload_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - const char *syscmd, size_t maxsize); struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, char * const argl[], size_t maxsize); -- 2.22.0.rc1.257.g3120a18244-goog From 7306c00a4557841424e5dd71c76b91f4274448f5 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Fri, 24 May 2019 19:08:10 +0200 Subject: [PATCH 14/16] s3: lib: Rename all uses of file_pload_XXX -> file_ploadv_XXX. Keep naming consistent across all usage. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit dbfa3cd186428c02589aa9093e868554b4c695d5) --- source3/lib/util_file.c | 30 +++++++++++++++--------------- source3/lib/util_file.h | 2 +- source3/winbindd/idmap_script.c | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 770609d0b11..1c468ea5501 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -26,24 +26,24 @@ #include "lib/async_req/async_sock.h" #include "lib/util/tevent_unix.h" -struct file_pload_state { +struct file_ploadv_state { struct tevent_context *ev; size_t maxsize; int fd; uint8_t *buf; }; -static int file_pload_state_destructor(struct file_pload_state *s); -static void file_pload_readable(struct tevent_req *subreq); +static int file_ploadv_state_destructor(struct file_ploadv_state *s); +static void file_ploadv_readable(struct tevent_req *subreq); struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, char * const argl[], size_t maxsize) { struct tevent_req *req = NULL, *subreq = NULL; - struct file_pload_state *state = NULL; + struct file_ploadv_state *state = NULL; - req = tevent_req_create(mem_ctx, &state, struct file_pload_state); + req = tevent_req_create(mem_ctx, &state, struct file_ploadv_state); if (req == NULL) { return NULL; } @@ -55,17 +55,17 @@ struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx, tevent_req_error(req, errno); return tevent_req_post(req, ev); } - talloc_set_destructor(state, file_pload_state_destructor); + talloc_set_destructor(state, file_ploadv_state_destructor); subreq = wait_for_read_send(state, state->ev, state->fd, false); if (tevent_req_nomem(subreq, req)) { return tevent_req_post(req, ev); } - tevent_req_set_callback(subreq, file_pload_readable, req); + tevent_req_set_callback(subreq, file_ploadv_readable, req); return req; } -static int file_pload_state_destructor(struct file_pload_state *s) +static int file_ploadv_state_destructor(struct file_ploadv_state *s) { if (s->fd != -1) { sys_pclose(s->fd); @@ -74,12 +74,12 @@ static int file_pload_state_destructor(struct file_pload_state *s) return 0; } -static void file_pload_readable(struct tevent_req *subreq) +static void file_ploadv_readable(struct tevent_req *subreq) { struct tevent_req *req = tevent_req_callback_data( subreq, struct tevent_req); - struct file_pload_state *state = tevent_req_data( - req, struct file_pload_state); + struct file_ploadv_state *state = tevent_req_data( + req, struct file_ploadv_state); uint8_t buf[1024]; uint8_t *tmp; ssize_t nread; @@ -131,14 +131,14 @@ static void file_pload_readable(struct tevent_req *subreq) if (tevent_req_nomem(subreq, req)) { return; } - tevent_req_set_callback(subreq, file_pload_readable, req); + tevent_req_set_callback(subreq, file_ploadv_readable, req); } -int file_pload_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, +int file_ploadv_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, uint8_t **buf) { - struct file_pload_state *state = tevent_req_data( - req, struct file_pload_state); + struct file_ploadv_state *state = tevent_req_data( + req, struct file_ploadv_state); int err; if (tevent_req_is_unix_error(req, &err)) { diff --git a/source3/lib/util_file.h b/source3/lib/util_file.h index 97fcf89996d..1aef5a22b44 100644 --- a/source3/lib/util_file.h +++ b/source3/lib/util_file.h @@ -26,7 +26,7 @@ struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, char * const argl[], size_t maxsize); -int file_pload_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, +int file_ploadv_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, uint8_t **buf); char **file_lines_ploadv(TALLOC_CTX *mem_ctx, char * const argl[], diff --git a/source3/winbindd/idmap_script.c b/source3/winbindd/idmap_script.c index 269f047e1f4..fd6c19e8f62 100644 --- a/source3/winbindd/idmap_script.c +++ b/source3/winbindd/idmap_script.c @@ -142,7 +142,7 @@ static void idmap_script_xid2sid_done(struct tevent_req *subreq) req, struct idmap_script_xid2sid_state); int ret; - ret = file_pload_recv(subreq, state, &state->out); + ret = file_ploadv_recv(subreq, state, &state->out); TALLOC_FREE(subreq); if (tevent_req_error(req, ret)) { return; @@ -399,7 +399,7 @@ static void idmap_script_sid2xid_done(struct tevent_req *subreq) req, struct idmap_script_sid2xid_state); int ret; - ret = file_pload_recv(subreq, state, &state->out); + ret = file_ploadv_recv(subreq, state, &state->out); TALLOC_FREE(subreq); if (tevent_req_error(req, ret)) { return; -- 2.22.0.rc1.257.g3120a18244-goog From 643037cd86c8637f09be80faa2598cfc6242abc4 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:40:26 -0700 Subject: [PATCH 15/16] lib: util: Finally remove possibilities of using sys_popen() unsafely. All code now uses sys_popenv() which is much harder to use incorrectly. Remove the extract_args() function that was the cause of possible issues. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 9fa95d5b45369acfdd38923e8618e94e5d04b07e) --- lib/util/sys_popen.c | 95 -------------------------------------------- lib/util/sys_popen.h | 1 - 2 files changed, 96 deletions(-) diff --git a/lib/util/sys_popen.c b/lib/util/sys_popen.c index 65040508c81..659e99ba736 100644 --- a/lib/util/sys_popen.c +++ b/lib/util/sys_popen.c @@ -24,77 +24,6 @@ #include "lib/util/sys_popen.h" #include "lib/util/debug.h" -/************************************************************************** - Extract a command into an arg list. -****************************************************************************/ - -static char **extract_args(TALLOC_CTX *mem_ctx, const char *command) -{ - char *trunc_cmd; - char *saveptr; - char *ptr; - int argcl; - char **argl = NULL; - int i; - - if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) { - DEBUG(0, ("talloc failed\n")); - goto nomem; - } - - if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) { - TALLOC_FREE(trunc_cmd); - errno = EINVAL; - return NULL; - } - - /* - * Count the args. - */ - - for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr)) - argcl++; - - TALLOC_FREE(trunc_cmd); - - if (!(argl = talloc_array(mem_ctx, char *, argcl + 1))) { - goto nomem; - } - - /* - * Now do the extraction. - */ - - if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) { - goto nomem; - } - - ptr = strtok_r(trunc_cmd, " \t", &saveptr); - i = 0; - - if (!(argl[i++] = talloc_strdup(argl, ptr))) { - goto nomem; - } - - while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) { - - if (!(argl[i++] = talloc_strdup(argl, ptr))) { - goto nomem; - } - } - - argl[i++] = NULL; - TALLOC_FREE(trunc_cmd); - return argl; - - nomem: - DEBUG(0, ("talloc failed\n")); - TALLOC_FREE(trunc_cmd); - TALLOC_FREE(argl); - errno = ENOMEM; - return NULL; -} - /************************************************************************** Wrapper for popen. Safer as it doesn't search a path. Modified from the glibc sources. @@ -204,30 +133,6 @@ err_exit: return -1; } -int sys_popen(const char *command) -{ - char **argl = NULL; - int ret; - - if (!*command) { - errno = EINVAL; - return -1; - } - - /* - * Extract the command and args into a NULL terminated array. - */ - - argl = extract_args(NULL, command); - if (argl == NULL) { - DBG_ERR("extract_args() failed: %s\n", strerror(errno)); - return -1; - } - ret = sys_popenv(argl); - TALLOC_FREE(argl); - return ret; -} - /************************************************************************** Wrapper for pclose. Modified from the glibc sources. ****************************************************************************/ diff --git a/lib/util/sys_popen.h b/lib/util/sys_popen.h index 80ea70efa75..be437483626 100644 --- a/lib/util/sys_popen.h +++ b/lib/util/sys_popen.h @@ -20,7 +20,6 @@ #ifndef __LIB_SYS_POPEN_H__ #define __LIB_SYS_POPEN_H__ -int sys_popen(const char *command); int sys_popenv(char * const argl[]); int sys_pclose(int fd); -- 2.22.0.rc1.257.g3120a18244-goog From dcae2cb8464fe1aedcd8ae0b947b854a4a774a2b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 18 May 2019 11:41:56 -0700 Subject: [PATCH 16/16] docs: dfree command. Correct usage of dfree scripts. Add quotes to the sample scripts to prevent incorrect parameter usage. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme Autobuild-User(master): Jeremy Allison Autobuild-Date(master): Fri May 24 20:12:02 UTC 2019 on sn-devel-184 (cherry picked from commit 77eabd74e9a28b1e6decf0890a9ef4c83fa002d4) --- docs-xml/smbdotconf/misc/dfreecommand.xml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs-xml/smbdotconf/misc/dfreecommand.xml b/docs-xml/smbdotconf/misc/dfreecommand.xml index a678bb44adf..a1eed4948a6 100644 --- a/docs-xml/smbdotconf/misc/dfreecommand.xml +++ b/docs-xml/smbdotconf/misc/dfreecommand.xml @@ -40,14 +40,16 @@ Where the script dfree (which must be made executable) could be: #!/bin/sh -df $1 | tail -1 | awk '{print $(NF-4),$(NF-2)}' +df "$1" | tail -1 | awk '{print $(NF-4),$(NF-2)}' or perhaps (on Sys V based systems): #!/bin/sh -/usr/bin/df -k $1 | tail -1 | awk '{print $3" "$5}' +/usr/bin/df -k "$1" | tail -1 | awk '{print $3" "$5}' Note that you may have to replace the command names with full path names on some systems. + Also note the arguments passed into the script should be quoted inside the script in case they + contain special characters such as spaces or newlines. -- 2.22.0.rc1.257.g3120a18244-goog