this now works as a add|delete share command :-)
[Samba.git] / source / lib / smbrun.c
blob62378503e0f7abe64e3bbae5a6549c7b0fdd51d9
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
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.
22 #include "includes.h"
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().
31 ****************************************************************************/
33 static int setup_out_fd(void)
35 int fd;
36 pstring path;
38 slprintf(path, sizeof(path)-1, "%s/smb.XXXXXX", tmpdir());
40 /* now create the file */
41 fd = smb_mkstemp(path);
43 if (fd == -1) {
44 DEBUG(0,("setup_out_fd: Failed to create file %s. (%s)\n",
45 path, strerror(errno) ));
46 return -1;
49 DEBUG(10,("setup_out_fd: Created tmp file %s\n", path ));
51 /* Ensure file only kept around by open fd. */
52 unlink(path);
53 return fd;
56 /****************************************************************************
57 run a command being careful about uid/gid handling and putting the output in
58 outfd (or discard it if outfd is NULL).
59 ****************************************************************************/
61 int smbrun(char *cmd, int *outfd)
63 pid_t pid;
64 uid_t uid = current_user.uid;
65 gid_t gid = current_user.gid;
68 * Lose any kernel oplock capabilities we may have.
70 oplock_set_capability(False, False);
72 /* point our stdout at the file we want output to go into */
74 if (outfd && ((*outfd = setup_out_fd()) == -1)) {
75 return -1;
78 /* in this method we will exec /bin/sh with the correct
79 arguments, after first setting stdout to point at the file */
82 * We need to temporarily stop CatchChild from eating
83 * SIGCLD signals as it also eats the exit status code. JRA.
86 CatchChildLeaveStatus();
88 if ((pid=sys_fork()) < 0) {
89 DEBUG(0,("smbrun: fork failed with error %s\n", strerror(errno) ));
90 CatchChild();
91 if (outfd) {
92 close(*outfd);
93 *outfd = -1;
95 return errno;
98 if (pid) {
100 * Parent.
102 int status=0;
103 pid_t wpid;
106 /* the parent just waits for the child to exit */
107 while((wpid = sys_waitpid(pid,&status,0)) < 0) {
108 if(errno == EINTR) {
109 errno = 0;
110 continue;
112 break;
115 CatchChild();
117 if (wpid != pid) {
118 DEBUG(2,("waitpid(%d) : %s\n",(int)pid,strerror(errno)));
119 if (outfd) {
120 close(*outfd);
121 *outfd = -1;
123 return -1;
126 /* Reset the seek pointer. */
127 if (outfd) {
128 sys_lseek(*outfd, 0, SEEK_SET);
131 #if defined(WIFEXITED) && defined(WEXITSTATUS)
132 if (WIFEXITED(status)) {
133 return WEXITSTATUS(status);
135 #endif
137 return status;
140 CatchChild();
142 /* we are in the child. we exec /bin/sh to do the work for us. we
143 don't directly exec the command we want because it may be a
144 pipeline or anything else the config file specifies */
146 /* point our stdout at the file we want output to go into */
147 if (outfd) {
148 close(1);
149 if (dup2(*outfd,1) != 1) {
150 DEBUG(2,("Failed to create stdout file descriptor\n"));
151 close(*outfd);
152 exit(80);
156 /* now completely lose our privileges. This is a fairly paranoid
157 way of doing it, but it does work on all systems that I know of */
159 become_user_permanently(uid, gid);
161 if (getuid() != uid || geteuid() != uid ||
162 getgid() != gid || getegid() != gid) {
163 /* we failed to lose our privileges - do not execute
164 the command */
165 exit(81); /* we can't print stuff at this stage,
166 instead use exit codes for debugging */
169 #ifndef __INSURE__
170 /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
171 2 point to /dev/null from the startup code */
173 int fd;
174 for (fd=3;fd<256;fd++) close(fd);
176 #endif
178 execl("/bin/sh","sh","-c",cmd,NULL);
180 /* not reached */
181 exit(82);
182 return 1;