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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 modified for netatalk dgautheron@magic.fr
25 #endif /* HAVE_CONFIG_H */
29 #include <sys/types.h>
30 /* #define __USE_GNU 1 */
35 #include <sys/param.h>
41 #define USE_SETRESUID 1
49 #include <atalk/logger.h>
51 /**************************************************************************n
52 Find a suitable temporary directory. The result should be copied immediately
53 as it may be overwritten by a subsequent call.
54 ****************************************************************************/
56 static const char *tmpdir(void)
60 if ((p = getenv("TMPDIR")))
65 /****************************************************************************
66 This is a utility function of afprun().
67 ****************************************************************************/
69 static int setup_out_fd(void)
72 char path[MAXPATHLEN +1];
74 snprintf(path, sizeof(path)-1, "%s/afp.XXXXXX", tmpdir());
76 /* now create the file */
80 LOG(log_error, logtype_afpd, "setup_out_fd: Failed to create file %s. (%s)",path, strerror(errno) );
84 /* Ensure file only kept around by open fd. */
89 /****************************************************************************
90 Gain root privilege before doing something.
91 We want to end up with ruid==euid==0
92 ****************************************************************************/
93 static void gain_root_privilege(void)
98 /****************************************************************************
99 Ensure our real and effective groups are zero.
100 we want to end up with rgid==egid==0
101 ****************************************************************************/
102 static void gain_root_group_privilege(void)
107 /****************************************************************************
108 Become the specified uid and gid - permanently !
109 there should be no way back if possible
110 ****************************************************************************/
111 static void become_user_permanently(uid_t uid, gid_t gid)
114 * First - gain root privilege. We do this to ensure
115 * we can lose it again.
118 gain_root_privilege();
119 gain_root_group_privilege();
122 setresgid(gid,gid,gid);
124 setresuid(uid,uid,uid);
144 setgidx(ID_REAL, gid);
145 setgidx(ID_EFFECTIVE, gid);
147 setuidx(ID_REAL, uid);
148 setuidx(ID_EFFECTIVE, uid);
153 /****************************************************************************
154 run a command being careful about uid/gid handling and putting the output in
155 outfd (or discard it if outfd is NULL).
156 ****************************************************************************/
158 int afprun(int root, char *cmd, int *outfd)
161 uid_t uid = geteuid();
162 gid_t gid = getegid();
164 /* point our stdout at the file we want output to go into */
165 if (outfd && ((*outfd = setup_out_fd()) == -1)) {
168 LOG(log_debug, logtype_afpd, "running %s as user %d", cmd, root?0:uid);
169 /* in this method we will exec /bin/sh with the correct
170 arguments, after first setting stdout to point at the file */
172 if ((pid=fork()) < 0) {
173 LOG(log_error, logtype_afpd, "afprun: fork failed with error %s", strerror(errno) );
188 /* the parent just waits for the child to exit */
189 while((wpid = waitpid(pid,&status,0)) < 0) {
190 if (errno == EINTR) {
197 LOG(log_error, logtype_afpd, "waitpid(%d) : %s",(int)pid, strerror(errno) );
204 /* Reset the seek pointer. */
206 lseek(*outfd, 0, SEEK_SET);
209 #if defined(WIFEXITED) && defined(WEXITSTATUS)
210 if (WIFEXITED(status)) {
211 return WEXITSTATUS(status);
217 /* we are in the child. we exec /bin/sh to do the work for us. we
218 don't directly exec the command we want because it may be a
219 pipeline or anything else the config file specifies */
221 /* point our stdout at the file we want output to go into */
224 if (dup2(*outfd,1) != 1) {
225 LOG(log_error, logtype_afpd, "Failed to create stdout file descriptor");
231 if (chdir("/") < 0) {
232 LOG(log_error, logtype_afpd, "afprun: can't change directory to \"/\" %s", strerror(errno) );
236 /* now completely lose our privileges. This is a fairly paranoid
237 way of doing it, but it does work on all systems that I know of */
239 become_user_permanently(0, 0);
243 become_user_permanently(uid, gid);
245 if (getuid() != uid || geteuid() != uid || getgid() != gid || getegid() != gid) {
246 /* we failed to lose our privileges - do not execute the command */
247 exit(81); /* we can't print stuff at this stage, instead use exit codes for debugging */
250 /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
251 2 point to /dev/null from the startup code */
254 for (fd=3;fd<256;fd++) close(fd);
257 execl("/bin/sh","sh","-c",cmd,NULL);