As Andrew suggested, make smbrun return a fd for a deleted file which can then
[vlendec/samba-autobuild/.git] / source3 / lib / smbrun.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    run a command as a specified user
5    Copyright (C) Andrew Tridgell 1992-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /* need to move this from here!! need some sleep ... */
25 struct current_user current_user;
26
27 extern int DEBUGLEVEL;
28
29 /****************************************************************************
30 This is a utility function of smbrun().
31 ****************************************************************************/
32
33 static BOOL setup_out_fd(char *template)
34 {  
35         int fd;
36         pstring path;
37
38         pstrcpy( path, template);
39         pstrcat( path, generate_random_str(17));
40         pstrcat( path, ".XXXXXX");
41
42         /* now create the file */
43         fd = smb_mkstemp(path);
44
45         if (fd == -1) {
46                 DEBUG(0,("setup_out_fd: Failed to create file %s. (%s)\n",
47                         path, strerror(errno) ));
48                 return -1;
49         }
50
51         DEBUG(10,("setup_out_fd: Created tmp file %s\n", path ));
52
53         /* Ensure file only kept around by open fd. */
54         unlink(path);
55         return fd;
56 }
57
58 /****************************************************************************
59 run a command being careful about uid/gid handling and putting the output in
60 outfd (or discard it if outfd is NULL).
61 ****************************************************************************/
62
63 int smbrun(char *cmd, int *outfd, char *template)
64 {
65         pid_t pid;
66         uid_t uid = current_user.uid;
67         gid_t gid = current_user.gid;
68         
69         /*
70          * Lose any kernel oplock capabilities we may have.
71          */
72         oplock_set_capability(False, False);
73
74         /* point our stdout at the file we want output to go into */
75
76         if (outfd && ((*outfd = setup_out_fd(template)) == -1)) {
77                 return -1;
78         }
79
80         /* in this method we will exec /bin/sh with the correct
81            arguments, after first setting stdout to point at the file */
82
83         /*
84          * We need to temporarily stop CatchChild from eating
85          * SIGCLD signals as it also eats the exit status code. JRA.
86          */
87
88         CatchChildLeaveStatus();
89                                         
90         if ((pid=sys_fork()) < 0) {
91                 DEBUG(0,("smbrun: fork failed with error %s\n", strerror(errno) ));
92                 CatchChild(); 
93                 if (outfd) {
94                         close(*outfd);
95                         *outfd = -1;
96                 }
97                 return errno;
98     }
99
100         if (pid) {
101                 /*
102                  * Parent.
103                  */
104                 int status=0;
105                 pid_t wpid;
106
107                 
108                 /* the parent just waits for the child to exit */
109                 while((wpid = sys_waitpid(pid,&status,0)) < 0) {
110                         if(errno == EINTR) {
111                                 errno = 0;
112                                 continue;
113                         }
114                         break;
115                 }
116
117                 CatchChild(); 
118
119                 if (wpid != pid) {
120                         DEBUG(2,("waitpid(%d) : %s\n",(int)pid,strerror(errno)));
121                         if (outfd) {
122                                 close(*outfd);
123                                 *outfd = -1;
124                         }
125                         return -1;
126                 }
127
128                 /* Reset the seek pointer. */
129                 if (outfd) {
130                         sys_lseek(*outfd, 0, SEEK_SET);
131                 }
132
133 #if defined(WIFEXITED) && defined(WEXITSTATUS)
134                 if (WIFEXITED(status)) {
135                         return WEXITSTATUS(status);
136                 }
137 #endif
138
139                 return status;
140         }
141         
142         CatchChild(); 
143         
144         /* we are in the child. we exec /bin/sh to do the work for us. we
145            don't directly exec the command we want because it may be a
146            pipeline or anything else the config file specifies */
147         
148         /* point our stdout at the file we want output to go into */
149         if (outfd) {
150                 close(1);
151                 if (dup2(*outfd,1) != 1) {
152                         DEBUG(2,("Failed to create stdout file descriptor\n"));
153                         close(*outfd);
154                         exit(80);
155                 }
156         }
157
158         /* now completely lose our privileges. This is a fairly paranoid
159            way of doing it, but it does work on all systems that I know of */
160
161         become_user_permanently(uid, gid);
162
163         if (getuid() != uid || geteuid() != uid ||
164             getgid() != gid || getegid() != gid) {
165                 /* we failed to lose our privileges - do not execute
166                    the command */
167                 exit(81); /* we can't print stuff at this stage,
168                              instead use exit codes for debugging */
169         }
170         
171 #ifndef __INSURE__
172         /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
173            2 point to /dev/null from the startup code */
174         {
175         int fd;
176         for (fd=3;fd<256;fd++) close(fd);
177         }
178 #endif
179
180         execl("/bin/sh","sh","-c",cmd,NULL);  
181         
182         /* not reached */
183         exit(82);
184         return 1;
185 }