librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source3 / lib / smbrun.c
blob15a0c886e4779a81432f5edc0dfa1b79d22762f0
1 /*
2 Unix SMB/CIFS implementation.
3 run a command as a specified user
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
23 /* need to move this from here!! need some sleep ... */
24 struct current_user current_user;
26 /****************************************************************************
27 This is a utility function of smbrun().
28 ****************************************************************************/
30 static int setup_out_fd(void)
32 int fd;
33 TALLOC_CTX *ctx = talloc_stackframe();
34 char *path = NULL;
35 mode_t mask;
37 path = talloc_asprintf(ctx,
38 "%s/smb.XXXXXX",
39 tmpdir());
40 if (!path) {
41 TALLOC_FREE(ctx);
42 errno = ENOMEM;
43 return -1;
46 /* now create the file */
47 mask = umask(S_IRWXO | S_IRWXG);
48 fd = mkstemp(path);
49 umask(mask);
51 if (fd == -1) {
52 DEBUG(0,("setup_out_fd: Failed to create file %s. (%s)\n",
53 path, strerror(errno) ));
54 TALLOC_FREE(ctx);
55 return -1;
58 DEBUG(10,("setup_out_fd: Created tmp file %s\n", path ));
60 /* Ensure file only kept around by open fd. */
61 unlink(path);
62 TALLOC_FREE(ctx);
63 return fd;
66 /****************************************************************************
67 run a command being careful about uid/gid handling and putting the output in
68 outfd (or discard it if outfd is NULL).
69 ****************************************************************************/
71 static int smbrun_internal(const char *cmd, int *outfd, bool sanitize)
73 pid_t pid;
74 uid_t uid = current_user.ut.uid;
75 gid_t gid = current_user.ut.gid;
78 * Lose any elevated privileges.
80 drop_effective_capability(KERNEL_OPLOCK_CAPABILITY);
81 drop_effective_capability(DMAPI_ACCESS_CAPABILITY);
83 /* point our stdout at the file we want output to go into */
85 if (outfd && ((*outfd = setup_out_fd()) == -1)) {
86 return -1;
89 /* in this method we will exec /bin/sh with the correct
90 arguments, after first setting stdout to point at the file */
93 * We need to temporarily stop CatchChild from eating
94 * SIGCLD signals as it also eats the exit status code. JRA.
97 CatchChildLeaveStatus();
99 if ((pid=fork()) < 0) {
100 DEBUG(0,("smbrun: fork failed with error %s\n", strerror(errno) ));
101 CatchChild();
102 if (outfd) {
103 close(*outfd);
104 *outfd = -1;
106 return errno;
109 if (pid) {
111 * Parent.
113 int status=0;
114 pid_t wpid;
117 /* the parent just waits for the child to exit */
118 while((wpid = sys_waitpid(pid,&status,0)) < 0) {
119 if(errno == EINTR) {
120 errno = 0;
121 continue;
123 break;
126 CatchChild();
128 if (wpid != pid) {
129 DEBUG(2,("waitpid(%d) : %s\n",(int)pid,strerror(errno)));
130 if (outfd) {
131 close(*outfd);
132 *outfd = -1;
134 return -1;
137 /* Reset the seek pointer. */
138 if (outfd) {
139 lseek(*outfd, 0, SEEK_SET);
142 #if defined(WIFEXITED) && defined(WEXITSTATUS)
143 if (WIFEXITED(status)) {
144 return WEXITSTATUS(status);
146 #endif
148 return status;
151 CatchChild();
153 /* we are in the child. we exec /bin/sh to do the work for us. we
154 don't directly exec the command we want because it may be a
155 pipeline or anything else the config file specifies */
157 /* point our stdout at the file we want output to go into */
158 if (outfd) {
159 close(1);
160 if (dup2(*outfd,1) != 1) {
161 DEBUG(2,("Failed to create stdout file descriptor\n"));
162 close(*outfd);
163 exit(80);
167 /* now completely lose our privileges. This is a fairly paranoid
168 way of doing it, but it does work on all systems that I know of */
170 become_user_permanently(uid, gid);
172 if (!non_root_mode()) {
173 if (getuid() != uid || geteuid() != uid ||
174 getgid() != gid || getegid() != gid) {
175 /* we failed to lose our privileges - do not execute
176 the command */
177 exit(81); /* we can't print stuff at this stage,
178 instead use exit codes for debugging */
182 #ifndef __INSURE__
183 /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
184 2 point to /dev/null from the startup code */
186 int fd;
187 for (fd=3;fd<256;fd++) close(fd);
189 #endif
192 char *newcmd = NULL;
193 if (sanitize) {
194 newcmd = escape_shell_string(cmd);
195 if (!newcmd)
196 exit(82);
199 execl("/bin/sh","sh","-c",
200 newcmd ? (const char *)newcmd : cmd, NULL);
202 SAFE_FREE(newcmd);
205 /* not reached */
206 exit(83);
207 return 1;
210 /****************************************************************************
211 Use only in known safe shell calls (printing).
212 ****************************************************************************/
214 int smbrun_no_sanitize(const char *cmd, int *outfd)
216 return smbrun_internal(cmd, outfd, False);
219 /****************************************************************************
220 By default this now sanitizes shell expansion.
221 ****************************************************************************/
223 int smbrun(const char *cmd, int *outfd)
225 return smbrun_internal(cmd, outfd, True);
228 /****************************************************************************
229 run a command being careful about uid/gid handling and putting the output in
230 outfd (or discard it if outfd is NULL).
231 sends the provided secret to the child stdin.
232 ****************************************************************************/
234 int smbrunsecret(const char *cmd, const char *secret)
236 pid_t pid;
237 uid_t uid = current_user.ut.uid;
238 gid_t gid = current_user.ut.gid;
239 int ifd[2];
242 * Lose any elevated privileges.
244 drop_effective_capability(KERNEL_OPLOCK_CAPABILITY);
245 drop_effective_capability(DMAPI_ACCESS_CAPABILITY);
247 /* build up an input pipe */
248 if(pipe(ifd)) {
249 return -1;
252 /* in this method we will exec /bin/sh with the correct
253 arguments, after first setting stdout to point at the file */
256 * We need to temporarily stop CatchChild from eating
257 * SIGCLD signals as it also eats the exit status code. JRA.
260 CatchChildLeaveStatus();
262 if ((pid=fork()) < 0) {
263 DEBUG(0, ("smbrunsecret: fork failed with error %s\n", strerror(errno)));
264 CatchChild();
265 return errno;
268 if (pid) {
270 * Parent.
272 int status = 0;
273 pid_t wpid;
274 size_t towrite;
275 ssize_t wrote;
277 close(ifd[0]);
278 /* send the secret */
279 towrite = strlen(secret);
280 wrote = write(ifd[1], secret, towrite);
281 if ( wrote != towrite ) {
282 DEBUG(0,("smbrunsecret: wrote %ld of %lu bytes\n",(long)wrote,(unsigned long)towrite));
284 fsync(ifd[1]);
285 close(ifd[1]);
287 /* the parent just waits for the child to exit */
288 while((wpid = sys_waitpid(pid, &status, 0)) < 0) {
289 if(errno == EINTR) {
290 errno = 0;
291 continue;
293 break;
296 CatchChild();
298 if (wpid != pid) {
299 DEBUG(2, ("waitpid(%d) : %s\n", (int)pid, strerror(errno)));
300 return -1;
303 #if defined(WIFEXITED) && defined(WEXITSTATUS)
304 if (WIFEXITED(status)) {
305 return WEXITSTATUS(status);
307 #endif
309 return status;
312 CatchChild();
314 /* we are in the child. we exec /bin/sh to do the work for us. we
315 don't directly exec the command we want because it may be a
316 pipeline or anything else the config file specifies */
318 close(ifd[1]);
319 close(0);
320 if (dup2(ifd[0], 0) != 0) {
321 DEBUG(2,("Failed to create stdin file descriptor\n"));
322 close(ifd[0]);
323 exit(80);
326 /* now completely lose our privileges. This is a fairly paranoid
327 way of doing it, but it does work on all systems that I know of */
329 become_user_permanently(uid, gid);
331 if (!non_root_mode()) {
332 if (getuid() != uid || geteuid() != uid ||
333 getgid() != gid || getegid() != gid) {
334 /* we failed to lose our privileges - do not execute
335 the command */
336 exit(81); /* we can't print stuff at this stage,
337 instead use exit codes for debugging */
341 #ifndef __INSURE__
342 /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
343 2 point to /dev/null from the startup code */
345 int fd;
346 for (fd = 3; fd < 256; fd++) close(fd);
348 #endif
350 execl("/bin/sh", "sh", "-c", cmd, NULL);
352 /* not reached */
353 exit(82);
354 return 1;