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 /****************************************************************************
28 This is a utility function of smbrun().
29 ****************************************************************************/
31 static int setup_out_fd(void)
36 slprintf(path
, sizeof(path
)-1, "%s/smb.XXXXXX", tmpdir());
38 /* now create the file */
39 fd
= smb_mkstemp(path
);
42 DEBUG(0,("setup_out_fd: Failed to create file %s. (%s)\n",
43 path
, strerror(errno
) ));
47 DEBUG(10,("setup_out_fd: Created tmp file %s\n", path
));
49 /* Ensure file only kept around by open fd. */
54 /****************************************************************************
55 run a command being careful about uid/gid handling and putting the output in
56 outfd (or discard it if outfd is NULL).
57 ****************************************************************************/
59 int smbrun(char *cmd
, int *outfd
)
62 uid_t uid
= current_user
.uid
;
63 gid_t gid
= current_user
.gid
;
66 * Lose any kernel oplock capabilities we may have.
68 oplock_set_capability(False
, False
);
70 /* point our stdout at the file we want output to go into */
72 if (outfd
&& ((*outfd
= setup_out_fd()) == -1)) {
76 /* in this method we will exec /bin/sh with the correct
77 arguments, after first setting stdout to point at the file */
80 * We need to temporarily stop CatchChild from eating
81 * SIGCLD signals as it also eats the exit status code. JRA.
84 CatchChildLeaveStatus();
86 if ((pid
=sys_fork()) < 0) {
87 DEBUG(0,("smbrun: fork failed with error %s\n", strerror(errno
) ));
104 /* the parent just waits for the child to exit */
105 while((wpid
= sys_waitpid(pid
,&status
,0)) < 0) {
116 DEBUG(2,("waitpid(%d) : %s\n",(int)pid
,strerror(errno
)));
124 /* Reset the seek pointer. */
126 sys_lseek(*outfd
, 0, SEEK_SET
);
129 #if defined(WIFEXITED) && defined(WEXITSTATUS)
130 if (WIFEXITED(status
)) {
131 return WEXITSTATUS(status
);
140 /* we are in the child. we exec /bin/sh to do the work for us. we
141 don't directly exec the command we want because it may be a
142 pipeline or anything else the config file specifies */
144 /* point our stdout at the file we want output to go into */
147 if (dup2(*outfd
,1) != 1) {
148 DEBUG(2,("Failed to create stdout file descriptor\n"));
154 /* now completely lose our privileges. This is a fairly paranoid
155 way of doing it, but it does work on all systems that I know of */
157 become_user_permanently(uid
, gid
);
159 if (getuid() != uid
|| geteuid() != uid
||
160 getgid() != gid
|| getegid() != gid
) {
161 /* we failed to lose our privileges - do not execute
163 exit(81); /* we can't print stuff at this stage,
164 instead use exit codes for debugging */
168 /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
169 2 point to /dev/null from the startup code */
172 for (fd
=3;fd
<256;fd
++) close(fd
);
176 execl("/bin/sh","sh","-c",cmd
,NULL
);