1 /* 2 * Copyright (c) 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software written by Ken Arnold and 6 * published in UNIX Review, Vol. 6, No. 8. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the University of California, Berkeley. The name of the 14 * University may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 */ 21 22 /* this came out of the ftpd sources; it's been modified to avoid the 23 * globbing stuff since we don't need it. also execvp instead of execv. 24 */ 25 26 #ifndef lint 27 static const char rcsid[] = 28 "$Id: popen.c,v 1.3 1998/08/14 00:32:41 vixie Exp $"; 29 #if 0 30 static const char sccsid[] = "@(#)popen.c 5.7 (Berkeley) 2/14/89"; 31 #endif 32 #endif /* not lint */ 33 34 #include "cron.h" 35 #if defined(LOGIN_CAP) 36 # include <login_cap.h> 37 #endif 38 39 #define MAX_ARGS 100 40 #define WANT_GLOBBING 0 41 42 /* 43 * Special version of popen which avoids call to shell. This insures no one 44 * may create a pipe to a hidden program as a side effect of a list or dir 45 * command. 46 */ 47 static PID_T *pids; 48 static int fds; 49 50 FILE * 51 cron_popen(char *program, char *type, entry *e, PID_T *pidptr) 52 { 53 char *cp; 54 FILE *iop; 55 int argc, pdes[2]; 56 PID_T pid; 57 char *usernm; 58 char *argv[MAX_ARGS + 1]; 59 # if defined(LOGIN_CAP) 60 struct passwd *pwd; 61 login_cap_t *lc; 62 # endif 63 #if WANT_GLOBBING 64 char **pop, *vv[2]; 65 int gargc; 66 char *gargv[1000]; 67 extern char **glob(), **copyblk(); 68 #endif 69 70 if ((*type != 'r' && *type != 'w') || type[1] != '\0') 71 return (NULL); 72 73 if (!pids) { 74 if ((fds = sysconf(_SC_OPEN_MAX)) <= 0) 75 return (NULL); 76 if (!(pids = calloc(fds, sizeof(PID_T)))) 77 return (NULL); 78 } 79 if (pipe(pdes) < 0) 80 return (NULL); 81 82 /* break up string into pieces */ 83 for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL) 84 if (!(argv[argc++] = strtok(cp, " \t\n"))) 85 break; 86 argv[MAX_ARGS] = NULL; 87 88 #if WANT_GLOBBING 89 /* glob each piece */ 90 gargv[0] = argv[0]; 91 for (gargc = argc = 1; argv[argc]; argc++) { 92 if (!(pop = glob(argv[argc]))) { /* globbing failed */ 93 vv[0] = argv[argc]; 94 vv[1] = NULL; 95 pop = copyblk(vv); 96 } 97 argv[argc] = (char *)pop; /* save to free later */ 98 while (*pop && gargc < 1000) 99 gargv[gargc++] = *pop++; 100 } 101 gargv[gargc] = NULL; 102 #endif 103 104 iop = NULL; 105 switch(pid = fork()) { 106 case -1: /* error */ 107 (void)close(pdes[0]); 108 (void)close(pdes[1]); 109 goto pfree; 110 /* NOTREACHED */ 111 case 0: /* child */ 112 if (e != NULL) { 113 #ifdef SYSLOG 114 closelog(); 115 #endif 116 117 /* get new pgrp, void tty, etc. 118 */ 119 (void) setsid(); 120 } 121 if (*type == 'r') { 122 /* Do not share our parent's stdin */ 123 (void)close(0); 124 (void)open(_PATH_DEVNULL, O_RDWR); 125 if (pdes[1] != 1) { 126 dup2(pdes[1], 1); 127 dup2(pdes[1], 2); /* stderr, too! */ 128 (void)close(pdes[1]); 129 } 130 (void)close(pdes[0]); 131 } else { 132 if (pdes[0] != 0) { 133 dup2(pdes[0], 0); 134 (void)close(pdes[0]); 135 } 136 /* Hack: stdout gets revoked */ 137 (void)close(1); 138 (void)open(_PATH_DEVNULL, O_RDWR); 139 (void)close(2); 140 (void)open(_PATH_DEVNULL, O_RDWR); 141 (void)close(pdes[1]); 142 } 143 if (e != NULL) { 144 /* Set user's entire context, but skip the environment 145 * as cron provides a separate interface for this 146 */ 147 usernm = env_get("LOGNAME", e->envp); 148 # if defined(LOGIN_CAP) 149 if ((pwd = getpwnam(usernm)) == NULL) 150 pwd = getpwuid(e->uid); 151 lc = NULL; 152 if (pwd != NULL) { 153 pwd->pw_gid = e->gid; 154 if (e->class != NULL) 155 lc = login_getclass(e->class); 156 } 157 if (pwd && 158 setusercontext(lc, pwd, e->uid, 159 LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0) 160 (void) endpwent(); 161 else { 162 /* fall back to the old method */ 163 (void) endpwent(); 164 # endif 165 /* 166 * Set our directory, uid and gid. Set gid 167 * first since once we set uid, we've lost 168 * root privileges. 169 */ 170 if (setgid(e->gid) != 0) 171 _exit(ERROR_EXIT); 172 # if defined(BSD) 173 if (initgroups(usernm, e->gid) != 0) 174 _exit(ERROR_EXIT); 175 # endif 176 if (setlogin(usernm) != 0) 177 _exit(ERROR_EXIT); 178 if (setuid(e->uid) != 0) 179 _exit(ERROR_EXIT); 180 /* we aren't root after this..*/ 181 #if defined(LOGIN_CAP) 182 } 183 if (lc != NULL) 184 login_close(lc); 185 #endif 186 chdir(env_get("HOME", e->envp)); 187 } 188 #if WANT_GLOBBING 189 execvp(gargv[0], gargv); 190 #else 191 execvp(argv[0], argv); 192 #endif 193 _exit(1); 194 } 195 /* parent; assume fdopen can't fail... */ 196 if (*type == 'r') { 197 iop = fdopen(pdes[0], type); 198 (void)close(pdes[1]); 199 } else { 200 iop = fdopen(pdes[1], type); 201 (void)close(pdes[0]); 202 } 203 pids[fileno(iop)] = pid; 204 205 pfree: 206 #if WANT_GLOBBING 207 for (argc = 1; argv[argc] != NULL; argc++) { 208 /* blkfree((char **)argv[argc]); */ 209 free((char *)argv[argc]); 210 } 211 #endif 212 213 *pidptr = pid; 214 215 return (iop); 216 } 217 218 int 219 cron_pclose(FILE *iop) 220 { 221 int fdes; 222 int omask; 223 WAIT_T stat_loc; 224 PID_T pid; 225 226 /* 227 * pclose returns -1 if stream is not associated with a 228 * `popened' command, or, if already `pclosed'. 229 */ 230 if (pids == 0 || pids[fdes = fileno(iop)] == 0) 231 return (-1); 232 (void)fclose(iop); 233 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 234 while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1) 235 ; 236 (void)sigsetmask(omask); 237 pids[fdes] = 0; 238 return (pid == -1 ? -1 : WEXITSTATUS(stat_loc)); 239 } 240