From 7accec7f78f40849b50ad291fb56b04f3fd39dc2 Mon Sep 17 00:00:00 2001 From: "Trever L. Adams" Date: Wed, 12 Oct 2016 09:55:15 -0600 Subject: [PATCH] Update smbrun to allow for settings environment variables. Signed-off-by: Trever L. Adams Reviewed-by: David Disseldorp Reviewed-by: Jeremy Allison Autobuild-User(master): Jeremy Allison Autobuild-Date(master): Thu Oct 13 04:26:26 CEST 2016 on sn-devel-144 --- source3/auth/auth_util.c | 4 ++-- source3/auth/user_util.c | 2 +- source3/groupdb/mapping.c | 10 +++++----- source3/include/proto.h | 4 ++-- source3/lib/smbrun.c | 21 ++++++++++++++------- source3/libnet/libnet_dssync_passdb.c | 2 +- source3/libnet/libnet_samsync_passdb.c | 2 +- source3/modules/vfs_shell_snap.c | 6 +++--- source3/modules/vfs_tsmsm.c | 3 ++- source3/nmbd/nmbd_winsserver.c | 2 +- source3/passdb/pdb_interface.c | 4 ++-- source3/passdb/pdb_ldap.c | 2 +- source3/passdb/pdb_smbpasswd.c | 2 +- source3/passdb/pdb_tdb.c | 2 +- source3/printing/print_generic.c | 2 +- source3/rpc_server/spoolss/srv_spoolss_nt.c | 10 ++++++---- source3/rpc_server/srvsvc/srv_srvsvc_nt.c | 9 ++++++--- source3/rpc_server/winreg/srv_winreg_nt.c | 4 ++-- source3/services/svc_rcinit.c | 6 +++--- source3/smbd/close.c | 2 +- source3/smbd/message.c | 2 +- source3/smbd/service.c | 8 ++++---- 22 files changed, 61 insertions(+), 48 deletions(-) diff --git a/source3/auth/auth_util.c b/source3/auth/auth_util.c index 663c0bc7431..5473fa23a6c 100644 --- a/source3/auth/auth_util.c +++ b/source3/auth/auth_util.c @@ -78,7 +78,7 @@ static int _smb_create_user(const char *domain, const char *unix_username, const return -1; } } - ret = smbrun(add_script,NULL); + ret = smbrun(add_script, NULL, NULL); flush_pwnam_cache(); DEBUG(ret ? 0 : 3, ("smb_create_user: Running the command `%s' gave %d\n", @@ -434,7 +434,7 @@ static NTSTATUS log_nt_token(struct security_token *token) } DEBUG(8, ("running command: [%s]\n", command)); - if (smbrun(command, NULL) != 0) { + if (smbrun(command, NULL, NULL) != 0) { DEBUG(0, ("Could not log NT token\n")); TALLOC_FREE(frame); return NT_STATUS_ACCESS_DENIED; diff --git a/source3/auth/user_util.c b/source3/auth/user_util.c index ded2184051d..d5c61b22794 100644 --- a/source3/auth/user_util.c +++ b/source3/auth/user_util.c @@ -315,7 +315,7 @@ bool map_username(TALLOC_CTX *ctx, const char *user_in, char **p_user_out) } DEBUG(10,("Running [%s]\n", command)); - ret = smbrun(command, &fd); + ret = smbrun(command, &fd, NULL); DEBUGADD(10,("returned [%d]\n", ret)); TALLOC_FREE(command); diff --git a/source3/groupdb/mapping.c b/source3/groupdb/mapping.c index 4db53ab5eb0..ac70fe68c48 100644 --- a/source3/groupdb/mapping.c +++ b/source3/groupdb/mapping.c @@ -227,7 +227,7 @@ int smb_create_group(const char *unix_group, gid_t *new_gid) return -1; } - ret = smbrun(add_script, &fd); + ret = smbrun(add_script, &fd, NULL); DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret)); if (ret == 0) { smb_nscd_flush_group_cache(); @@ -282,7 +282,7 @@ int smb_delete_group(const char *unix_group) if (!del_script) { return -1; } - ret = smbrun(del_script,NULL); + ret = smbrun(del_script, NULL, NULL); DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret)); if (ret == 0) { smb_nscd_flush_group_cache(); @@ -322,7 +322,7 @@ int smb_set_primary_group(const char *unix_group, const char* unix_user) if (!add_script) { return -1; } - ret = smbrun(add_script,NULL); + ret = smbrun(add_script, NULL, NULL); flush_pwnam_cache(); DEBUG(ret ? 0 : 3,("smb_set_primary_group: " "Running the command `%s' gave %d\n",add_script,ret)); @@ -364,7 +364,7 @@ int smb_add_user_group(const char *unix_group, const char *unix_user) if (!add_script) { return -1; } - ret = smbrun(add_script,NULL); + ret = smbrun(add_script, NULL, NULL); DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret)); if (ret == 0) { smb_nscd_flush_group_cache(); @@ -404,7 +404,7 @@ int smb_delete_user_group(const char *unix_group, const char *unix_user) if (!del_script) { return -1; } - ret = smbrun(del_script,NULL); + ret = smbrun(del_script, NULL, NULL); DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret)); if (ret == 0) { smb_nscd_flush_group_cache(); diff --git a/source3/include/proto.h b/source3/include/proto.h index fe4217d93ef..0b0a2b59325 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -162,8 +162,8 @@ bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_d /* The following definitions come from lib/smbrun.c */ -int smbrun_no_sanitize(const char *cmd, int *outfd); -int smbrun(const char *cmd, int *outfd); +int smbrun_no_sanitize(const char *cmd, int *outfd, char * const *env); +int smbrun(const char *cmd, int *outfd, char * const *env); int smbrunsecret(const char *cmd, const char *secret); /* The following definitions come from lib/sock_exec.c */ diff --git a/source3/lib/smbrun.c b/source3/lib/smbrun.c index 63b0323c51c..3b7ed542ab2 100644 --- a/source3/lib/smbrun.c +++ b/source3/lib/smbrun.c @@ -68,7 +68,8 @@ run a command being careful about uid/gid handling and putting the output in outfd (or discard it if outfd is NULL). ****************************************************************************/ -static int smbrun_internal(const char *cmd, int *outfd, bool sanitize) +static int smbrun_internal(const char *cmd, int *outfd, bool sanitize, + char * const *env) { pid_t pid; uid_t uid = current_user.ut.uid; @@ -197,8 +198,14 @@ static int smbrun_internal(const char *cmd, int *outfd, bool sanitize) exit(82); } - execl("/bin/sh","sh","-c", - newcmd ? (const char *)newcmd : cmd, NULL); + if (env != NULL) { + execle("/bin/sh","sh","-c", + newcmd ? (const char *)newcmd : cmd, NULL, + env); + } else { + execl("/bin/sh","sh","-c", + newcmd ? (const char *)newcmd : cmd, NULL); + } SAFE_FREE(newcmd); } @@ -212,18 +219,18 @@ static int smbrun_internal(const char *cmd, int *outfd, bool sanitize) Use only in known safe shell calls (printing). ****************************************************************************/ -int smbrun_no_sanitize(const char *cmd, int *outfd) +int smbrun_no_sanitize(const char *cmd, int *outfd, char * const *env) { - return smbrun_internal(cmd, outfd, False); + return smbrun_internal(cmd, outfd, false, env); } /**************************************************************************** By default this now sanitizes shell expansion. ****************************************************************************/ -int smbrun(const char *cmd, int *outfd) +int smbrun(const char *cmd, int *outfd, char * const *env) { - return smbrun_internal(cmd, outfd, True); + return smbrun_internal(cmd, outfd, true, env); } /**************************************************************************** diff --git a/source3/libnet/libnet_dssync_passdb.c b/source3/libnet/libnet_dssync_passdb.c index 31c0616aae7..99e65c263ad 100644 --- a/source3/libnet/libnet_dssync_passdb.c +++ b/source3/libnet/libnet_dssync_passdb.c @@ -693,7 +693,7 @@ static NTSTATUS smb_create_user(TALLOC_CTX *mem_ctx, if (!add_script) { return NT_STATUS_NO_MEMORY; } - add_ret = smbrun(add_script, NULL); + add_ret = smbrun(add_script, NULL, NULL); DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' " "gave %d\n", add_script, add_ret)); if (add_ret == 0) { diff --git a/source3/libnet/libnet_samsync_passdb.c b/source3/libnet/libnet_samsync_passdb.c index 91482e69f2e..01373de0890 100644 --- a/source3/libnet/libnet_samsync_passdb.c +++ b/source3/libnet/libnet_samsync_passdb.c @@ -276,7 +276,7 @@ static NTSTATUS smb_create_user(TALLOC_CTX *mem_ctx, if (!add_script) { return NT_STATUS_NO_MEMORY; } - add_ret = smbrun(add_script, NULL); + add_ret = smbrun(add_script, NULL, NULL); DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' " "gave %d\n", add_script, add_ret)); if (add_ret == 0) { diff --git a/source3/modules/vfs_shell_snap.c b/source3/modules/vfs_shell_snap.c index a8c29254419..2273652b360 100644 --- a/source3/modules/vfs_shell_snap.c +++ b/source3/modules/vfs_shell_snap.c @@ -59,7 +59,7 @@ static NTSTATUS shell_snap_check_path(struct vfs_handle_struct *handle, goto err_tmp_free; } - ret = smbrun(cmd_run, NULL); + ret = smbrun(cmd_run, NULL, NULL); if (ret != 0) { DEBUG(0, ("%s failed with %d\n", cmd_run, ret)); status = NT_STATUS_NOT_SUPPORTED; @@ -116,7 +116,7 @@ static NTSTATUS shell_snap_create(struct vfs_handle_struct *handle, goto err_tmp_free; } - ret = smbrun(cmd_run, &fd); + ret = smbrun(cmd_run, &fd, NULL); talloc_free(cmd_run); if (ret != 0) { if (fd != -1) { @@ -178,7 +178,7 @@ static NTSTATUS shell_snap_delete(struct vfs_handle_struct *handle, return NT_STATUS_NO_MEMORY; } - ret = smbrun(cmd_run, NULL); + ret = smbrun(cmd_run, NULL, NULL); talloc_free(cmd_run); if (ret != 0) { return NT_STATUS_UNSUCCESSFUL; diff --git a/source3/modules/vfs_tsmsm.c b/source3/modules/vfs_tsmsm.c index b943515a8fc..aa0ae963ae4 100644 --- a/source3/modules/vfs_tsmsm.c +++ b/source3/modules/vfs_tsmsm.c @@ -521,7 +521,8 @@ static NTSTATUS tsmsm_set_offline(struct vfs_handle_struct *handle, return NT_STATUS_NO_MEMORY; } DEBUG(10, ("tsmsm_set_offline: Running [%s]\n", command)); - if((result = smbrun(command, NULL)) != 0) { + result = smbrun(command, NULL, NULL) + if(result != 0) { DEBUG(1,("tsmsm_set_offline: Running [%s] returned %d\n", command, result)); TALLOC_FREE(command); return NT_STATUS_INTERNAL_ERROR; diff --git a/source3/nmbd/nmbd_winsserver.c b/source3/nmbd/nmbd_winsserver.c index 98ec6250987..cdec10e08ca 100644 --- a/source3/nmbd/nmbd_winsserver.c +++ b/source3/nmbd/nmbd_winsserver.c @@ -501,7 +501,7 @@ static void wins_hook(const char *operation, struct name_record *namerec, int tt } DEBUG(3,("calling wins hook for %s\n", nmb_namestr(&namerec->name))); - smbrun(command, NULL); + smbrun(command, NULL, NULL); TALLOC_FREE(command); } diff --git a/source3/passdb/pdb_interface.c b/source3/passdb/pdb_interface.c index 5260320939e..36ae576eb20 100644 --- a/source3/passdb/pdb_interface.c +++ b/source3/passdb/pdb_interface.c @@ -485,7 +485,7 @@ static NTSTATUS pdb_default_create_user(struct pdb_methods *methods, if (!add_script) { return NT_STATUS_NO_MEMORY; } - add_ret = smbrun(add_script,NULL); + add_ret = smbrun(add_script, NULL, NULL); DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the command `%s' gave %d\n", add_script, add_ret)); if (add_ret == 0) { @@ -570,7 +570,7 @@ static int smb_delete_user(const char *unix_user) if (!del_script) { return -1; } - ret = smbrun(del_script,NULL); + ret = smbrun(del_script, NULL, NULL); flush_pwnam_cache(); if (ret == 0) { smb_nscd_flush_user_cache(); diff --git a/source3/passdb/pdb_ldap.c b/source3/passdb/pdb_ldap.c index 4383785a841..e3dd790b1fa 100644 --- a/source3/passdb/pdb_ldap.c +++ b/source3/passdb/pdb_ldap.c @@ -2013,7 +2013,7 @@ static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods, oldname_lower, true, true); - rc = smbrun(rename_script, NULL); + rc = smbrun(rename_script, NULL, NULL); DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n", rename_script, rc)); diff --git a/source3/passdb/pdb_smbpasswd.c b/source3/passdb/pdb_smbpasswd.c index 9250bea32fc..8a75fde644e 100644 --- a/source3/passdb/pdb_smbpasswd.c +++ b/source3/passdb/pdb_smbpasswd.c @@ -1501,7 +1501,7 @@ static NTSTATUS smbpasswd_rename_sam_account (struct pdb_methods *my_methods, goto done; } - rename_ret = smbrun(rename_script, NULL); + rename_ret = smbrun(rename_script, NULL, NULL); DEBUG(rename_ret ? 0 : 3,("Running the command `%s' gave %d\n", rename_script, rename_ret)); diff --git a/source3/passdb/pdb_tdb.c b/source3/passdb/pdb_tdb.c index 74a27920da6..6453c9f48b9 100644 --- a/source3/passdb/pdb_tdb.c +++ b/source3/passdb/pdb_tdb.c @@ -1083,7 +1083,7 @@ static NTSTATUS tdbsam_rename_sam_account(struct pdb_methods *my_methods, if (!rename_script) { goto cancel; } - rename_ret = smbrun(rename_script, NULL); + rename_ret = smbrun(rename_script, NULL, NULL); DEBUG(rename_ret ? 0 : 3,("Running the command `%s' gave %d\n", rename_script, rename_ret)); diff --git a/source3/printing/print_generic.c b/source3/printing/print_generic.c index e89eed7bcdd..d77fb21e3fd 100644 --- a/source3/printing/print_generic.c +++ b/source3/printing/print_generic.c @@ -85,7 +85,7 @@ static int print_run_command(int snum, const char* printername, bool do_sub, } } - ret = smbrun_no_sanitize(syscmd,outfd); + ret = smbrun_no_sanitize(syscmd, outfd, NULL); DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret)); diff --git a/source3/rpc_server/spoolss/srv_spoolss_nt.c b/source3/rpc_server/spoolss/srv_spoolss_nt.c index 99e5eb2add1..c8ff44960d0 100644 --- a/source3/rpc_server/spoolss/srv_spoolss_nt.c +++ b/source3/rpc_server/spoolss/srv_spoolss_nt.c @@ -401,7 +401,8 @@ static WERROR delete_printer_hook(TALLOC_CTX *ctx, struct security_token *token, if ( is_print_op ) become_root(); - if ( (ret = smbrun(command, NULL)) == 0 ) { + ret = smbrun(command, NULL, NULL); + if (ret == 0) { /* Tell everyone we updated smb.conf. */ message_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL); } @@ -6317,7 +6318,7 @@ static WERROR add_port_hook(TALLOC_CTX *ctx, struct security_token *token, const if ( is_print_op ) become_root(); - ret = smbrun(command, NULL); + ret = smbrun(command, NULL, NULL); if ( is_print_op ) unbecome_root(); @@ -6384,7 +6385,8 @@ static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token, if ( is_print_op ) become_root(); - if ( (ret = smbrun(command, &fd)) == 0 ) { + ret = smbrun(command, &fd, NULL); + if (ret == 0) { /* Tell everyone we updated smb.conf. */ message_send_all(msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL); } @@ -8142,7 +8144,7 @@ static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines) } DEBUG(10,("Running [%s]\n", command)); - ret = smbrun(command, &fd); + ret = smbrun(command, &fd, NULL); DEBUG(10,("Returned [%d]\n", ret)); TALLOC_FREE(command); if (ret != 0) { diff --git a/source3/rpc_server/srvsvc/srv_srvsvc_nt.c b/source3/rpc_server/srvsvc/srv_srvsvc_nt.c index a5236d82a5b..17172d27fe4 100644 --- a/source3/rpc_server/srvsvc/srv_srvsvc_nt.c +++ b/source3/rpc_server/srvsvc/srv_srvsvc_nt.c @@ -1904,7 +1904,8 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p, if (is_disk_op) become_root(); - if ( (ret = smbrun(command, NULL)) == 0 ) { + ret = smbrun(command, NULL, NULL); + if (ret == 0) { /* Tell everyone we updated smb.conf. */ message_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL); @@ -2106,7 +2107,8 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p, /* FIXME: use libnetconf here - gd */ - if ( (ret = smbrun(command, NULL)) == 0 ) { + ret = smbrun(command, NULL, NULL); + if (ret == 0) { /* Tell everyone we updated smb.conf. */ message_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL); @@ -2213,7 +2215,8 @@ WERROR _srvsvc_NetShareDel(struct pipes_struct *p, if ( is_disk_op ) become_root(); - if ( (ret = smbrun(command, NULL)) == 0 ) { + ret = smbrun(command, NULL, NULL); + if (ret == 0) { /* Tell everyone we updated smb.conf. */ message_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0, NULL); diff --git a/source3/rpc_server/winreg/srv_winreg_nt.c b/source3/rpc_server/winreg/srv_winreg_nt.c index 3cfd009931f..d9ee8d0602d 100644 --- a/source3/rpc_server/winreg/srv_winreg_nt.c +++ b/source3/rpc_server/winreg/srv_winreg_nt.c @@ -592,7 +592,7 @@ WERROR _winreg_InitiateSystemShutdownEx(struct pipes_struct *p, if ( can_shutdown ) become_root(); - ret = smbrun( shutdown_script, NULL ); + ret = smbrun(shutdown_script, NULL, NULL); if ( can_shutdown ) unbecome_root(); @@ -626,7 +626,7 @@ WERROR _winreg_AbortSystemShutdown(struct pipes_struct *p, if ( can_shutdown ) become_root(); - ret = smbrun( abort_shutdown_script, NULL ); + ret = smbrun(abort_shutdown_script, NULL, NULL); if ( can_shutdown ) unbecome_root(); diff --git a/source3/services/svc_rcinit.c b/source3/services/svc_rcinit.c index a47931d63b6..95442ce2efb 100644 --- a/source3/services/svc_rcinit.c +++ b/source3/services/svc_rcinit.c @@ -36,7 +36,7 @@ static WERROR rcinit_stop( const char *service, struct SERVICE_STATUS *status ) /* we've already performed the access check when the service was opened */ become_root(); - ret = smbrun( command , &fd ); + ret = smbrun(command, &fd, NULL); unbecome_root(); DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret)); @@ -70,7 +70,7 @@ static WERROR rcinit_start( const char *service ) /* we've already performed the access check when the service was opened */ become_root(); - ret = smbrun( command , &fd ); + ret = smbrun(command, &fd, NULL); unbecome_root(); DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret)); @@ -99,7 +99,7 @@ static WERROR rcinit_status( const char *service, struct SERVICE_STATUS *status is STOPPED */ become_root(); - ret = smbrun( command , &fd ); + ret = smbrun(command, &fd, NULL); unbecome_root(); DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret)); diff --git a/source3/smbd/close.c b/source3/smbd/close.c index 22bd361768b..bc468c7e55d 100644 --- a/source3/smbd/close.c +++ b/source3/smbd/close.c @@ -91,7 +91,7 @@ static NTSTATUS check_magic(struct files_struct *fsp) status = map_nt_error_from_unix(errno); goto out; } - ret = smbrun(p,&tmp_fd); + ret = smbrun(p, &tmp_fd, NULL); DEBUG(3,("Invoking magic command %s gave %d\n", p,ret)); diff --git a/source3/smbd/message.c b/source3/smbd/message.c index a1ead4a56fb..1c3976dd3e9 100644 --- a/source3/smbd/message.c +++ b/source3/smbd/message.c @@ -129,7 +129,7 @@ static void msg_deliver(struct msg_state *state) if (s == NULL) { goto done; } - smbrun(s,NULL); + smbrun(s, NULL, NULL); done: TALLOC_FREE(frame); diff --git a/source3/smbd/service.c b/source3/smbd/service.c index 5b54aecd12f..3308e9dce97 100644 --- a/source3/smbd/service.c +++ b/source3/smbd/service.c @@ -764,7 +764,7 @@ static NTSTATUS make_connection_snum(struct smbXsrv_connection *xconn, conn->session_info->info->domain_name, lp_root_preexec(talloc_tos(), snum)); DEBUG(5,("cmd=%s\n",cmd)); - ret = smbrun(cmd,NULL); + ret = smbrun(cmd, NULL, NULL); TALLOC_FREE(cmd); if (ret != 0 && lp_root_preexec_close(snum)) { DEBUG(1,("root preexec gave %d - failing " @@ -801,7 +801,7 @@ static NTSTATUS make_connection_snum(struct smbXsrv_connection *xconn, conn->session_info->unix_info->sanitized_username, conn->session_info->info->domain_name, lp_preexec(talloc_tos(), snum)); - ret = smbrun(cmd,NULL); + ret = smbrun(cmd, NULL, NULL); TALLOC_FREE(cmd); if (ret != 0 && lp_preexec_close(snum)) { DEBUG(1,("preexec gave %d - failing connection\n", @@ -1199,7 +1199,7 @@ void close_cnum(connection_struct *conn, uint64_t vuid) conn->session_info->unix_info->sanitized_username, conn->session_info->info->domain_name, lp_postexec(talloc_tos(), SNUM(conn))); - smbrun(cmd,NULL); + smbrun(cmd, NULL, NULL); TALLOC_FREE(cmd); change_to_root_user(); } @@ -1215,7 +1215,7 @@ void close_cnum(connection_struct *conn, uint64_t vuid) conn->session_info->unix_info->sanitized_username, conn->session_info->info->domain_name, lp_root_postexec(talloc_tos(), SNUM(conn))); - smbrun(cmd,NULL); + smbrun(cmd, NULL, NULL); TALLOC_FREE(cmd); }