2 Unix SMB/Netbios implementation.
4 run a command as a specified user
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* need to move this from here!! need some sleep ... */
25 struct current_user current_user;
27 extern int DEBUGLEVEL;
29 /****************************************************************************
30 This is a utility function of smbrun(). It must be called only from
31 the child as it may leave the caller in a privileged state.
32 ****************************************************************************/
33 static BOOL setup_stdout_file(char *outfile,BOOL shared)
36 mode_t mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH;
37 int flags = O_RDWR|O_CREAT|O_EXCL;
42 /* become root - unprivileged users can't delete these files */
43 gain_root_privilege();
44 gain_root_group_privilege();
49 /* now create the file */
50 fd = sys_open(outfile,flags,mode);
52 if (fd == -1) return False;
55 if (dup2(fd,1) != 1) {
56 DEBUG(2,("Failed to create stdout file descriptor\n"));
66 /****************************************************************************
67 run a command being careful about uid/gid handling and putting the output in
68 outfile (or discard it if outfile is NULL).
70 if shared is True then ensure the file will be writeable by all users
71 but created such that its owned by root. This overcomes a security hole.
73 if shared is not set then open the file with O_EXCL set
74 ****************************************************************************/
75 int smbrun(char *cmd,char *outfile,BOOL shared)
79 uid_t uid = current_user.uid;
80 gid_t gid = current_user.gid;
83 * Lose any kernel oplock capabilities we may have.
85 oplock_set_capability(False, False);
91 char *path = lp_smbrun();
93 /* in the old method we use system() to execute smbrun which then
94 executes the command (using system() again!). This involves lots
95 of shell launches and is very slow. It also suffers from a
96 potential security hole */
97 if (!file_exist(path,NULL)) {
98 DEBUG(0,("SMBRUN ERROR: Can't find %s. Installation problem?\n",path));
102 slprintf(syscmd,sizeof(syscmd)-1,"%s %d %d \"(%s 2>&1) > %s\"",
103 path,(int)uid,(int)gid,cmd,
104 outfile?outfile:"/dev/null");
106 DEBUG(5,("smbrun - running %s ",syscmd));
107 ret = system(syscmd);
108 DEBUG(5,("gave %d\n",ret));
112 /* in this newer method we will exec /bin/sh with the correct
113 arguments, after first setting stdout to point at the file */
116 * We need to temporarily stop CatchChild from eating
117 * SIGCLD signals as it also eats the exit status code. JRA.
120 CatchChildLeaveStatus();
122 if ((pid=sys_fork()) < 0) {
123 DEBUG(0,("smbrun: fork failed with error %s\n", strerror(errno) ));
136 /* the parent just waits for the child to exit */
137 while((wpid = sys_waitpid(pid,&status,0)) < 0) {
148 DEBUG(2,("waitpid(%d) : %s\n",(int)pid,strerror(errno)));
151 #if defined(WIFEXITED) && defined(WEXITSTATUS)
152 if (WIFEXITED(status)) {
153 return WEXITSTATUS(status);
161 /* we are in the child. we exec /bin/sh to do the work for us. we
162 don't directly exec the command we want because it may be a
163 pipeline or anything else the config file specifies */
165 /* point our stdout at the file we want output to go into */
166 if (outfile && !setup_stdout_file(outfile,shared)) {
170 /* now completely lose our privileges. This is a fairly paranoid
171 way of doing it, but it does work on all systems that I know of */
173 become_user_permanently(uid, gid);
175 if (getuid() != uid || geteuid() != uid ||
176 getgid() != gid || getegid() != gid) {
177 /* we failed to lose our privileges - do not execute
179 exit(81); /* we can't print stuff at this stage,
180 instead use exit codes for debugging */
184 /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
185 2 point to /dev/null from the startup code */
186 for (fd=3;fd<256;fd++) close(fd);
189 execl("/bin/sh","sh","-c",cmd,NULL);