first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[nivanova/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(). 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)
34 {  
35   int fd;
36   SMB_STRUCT_STAT st;
37   mode_t mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH;
38   int flags = O_RDWR|O_CREAT|O_TRUNC|O_EXCL;
39
40   close(1);
41
42   if (shared) {
43         /* become root - unprivileged users can't delete these files */
44         gain_root_privilege();
45         gain_root_group_privilege();
46   }
47
48   if(sys_stat(outfile, &st) == 0) {
49     /* Check we're not deleting a device file. */ 
50     if(st.st_mode & S_IFREG)
51       unlink(outfile);
52     else
53       flags = O_RDWR;
54   }
55   /* now create the file */
56   fd = sys_open(outfile,flags,mode);
57
58   if (fd == -1) return False;
59
60   if (fd != 1) {
61     if (dup2(fd,1) != 0) {
62       DEBUG(2,("Failed to create stdout file descriptor\n"));
63       close(fd);
64       return False;
65     }
66     close(fd);
67   }
68   return True;
69 }
70
71
72 /****************************************************************************
73 run a command being careful about uid/gid handling and putting the output in
74 outfile (or discard it if outfile is NULL).
75
76 if shared is True then ensure the file will be writeable by all users
77 but created such that its owned by root. This overcomes a security hole.
78
79 if shared is not set then open the file with O_EXCL set
80 ****************************************************************************/
81 int smbrun(char *cmd,char *outfile,BOOL shared)
82 {
83         int fd;
84         pid_t pid;
85         uid_t uid = current_user.uid;
86         gid_t gid = current_user.gid;
87
88     /*
89      * Lose any kernel oplock capabilities we may have.
90      */
91     set_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
92     set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
93
94 #ifndef HAVE_EXECL
95         int ret;
96         pstring syscmd;  
97         char *path = lp_smbrun();
98         
99         /* in the old method we use system() to execute smbrun which then
100            executes the command (using system() again!). This involves lots
101            of shell launches and is very slow. It also suffers from a
102            potential security hole */
103         if (!file_exist(path,NULL)) {
104                 DEBUG(0,("SMBRUN ERROR: Can't find %s. Installation problem?\n",path));
105                 return(1);
106         }
107
108         slprintf(syscmd,sizeof(syscmd)-1,"%s %d %d \"(%s 2>&1) > %s\"",
109                  path,(int)uid,(int)gid,cmd,
110                  outfile?outfile:"/dev/null");
111         
112         DEBUG(5,("smbrun - running %s ",syscmd));
113         ret = system(syscmd);
114         DEBUG(5,("gave %d\n",ret));
115         return(ret);
116 #else
117         /* in this newer method we will exec /bin/sh with the correct
118            arguments, after first setting stdout to point at the file */
119
120         /*
121          * We need to temporarily stop CatchChild from eating
122          * SIGCLD signals as it also eats the exit status code. JRA.
123          */
124
125         CatchChildLeaveStatus();
126                                         
127         if ((pid=fork()) < 0) {
128                 DEBUG(0,("smbrun: fork failed with error %s\n", strerror(errno) ));
129                 CatchChild(); 
130                 return errno;
131     }
132
133         if (pid) {
134                 /*
135                  * Parent.
136                  */
137                 int status=0;
138                 pid_t wpid;
139
140                 
141                 /* the parent just waits for the child to exit */
142                 while((wpid = sys_waitpid(pid,&status,0)) < 0) {
143                         if(errno == EINTR) {
144                                 errno = 0;
145                                 continue;
146                         }
147                         break;
148                 }
149
150                 CatchChild(); 
151
152                 if (wpid != pid) {
153                         DEBUG(2,("waitpid(%d) : %s\n",(int)pid,strerror(errno)));
154                         return -1;
155                 }
156 #if defined(WIFEXITED) && defined(WEXITSTATUS)
157                 if (WIFEXITED(status)) {
158                         return WEXITSTATUS(status);
159                 }
160 #endif
161                 return status;
162         }
163         
164         CatchChild(); 
165         
166         /* we are in the child. we exec /bin/sh to do the work for us. we
167            don't directly exec the command we want because it may be a
168            pipeline or anything else the config file specifies */
169         
170         /* point our stdout at the file we want output to go into */
171         if (outfile && !setup_stdout_file(outfile,shared)) {
172                 exit(80);
173         }
174         
175         /* now completely lose our privileges. This is a fairly paranoid
176            way of doing it, but it does work on all systems that I know of */
177
178         become_user_permanently(uid, gid);
179
180         if (getuid() != uid || geteuid() != uid ||
181             getgid() != gid || getegid() != gid) {
182                 /* we failed to lose our privileges - do not execute
183                    the command */
184                 exit(81); /* we can't print stuff at this stage,
185                              instead use exit codes for debugging */
186         }
187         
188         /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
189            2 point to /dev/null from the startup code */
190         for (fd=3;fd<256;fd++) close(fd);
191         
192         execl("/bin/sh","sh","-c",cmd,NULL);  
193         
194         /* not reached */
195         exit(82);
196 #endif
197         return 1;
198 }