Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / cron / cron / popen.c
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  * @(#)popen.c  5.7 (Berkeley) 2/14/89
21  * $FreeBSD: src/usr.sbin/cron/cron/popen.c,v 1.7.2.3 2000/12/11 01:03:31 obrien Exp $
22  * $DragonFly: src/usr.sbin/cron/cron/popen.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
23  */
24
25 /* this came out of the ftpd sources; it's been modified to avoid the
26  * globbing stuff since we don't need it.  also execvp instead of execv.
27  */
28
29 #include "cron.h"
30 #include <sys/signal.h>
31 #include <fcntl.h>
32 #include <paths.h>
33 #if defined(SYSLOG)
34 # include <syslog.h>
35 #endif
36 #if defined(LOGIN_CAP)
37 # include <login_cap.h>
38 #endif
39
40
41 #define MAX_ARGS 100
42 #define WANT_GLOBBING 0
43
44 /*
45  * Special version of popen which avoids call to shell.  This insures noone
46  * may create a pipe to a hidden program as a side effect of a list or dir
47  * command.
48  */
49 static PID_T *pids;
50 static int fds;
51
52 FILE *
53 cron_popen(program, type, e)
54         char *program, *type;
55         entry *e;
56 {
57         register char *cp;
58         FILE *iop;
59         int argc, pdes[2];
60         PID_T pid;
61         char *usernm;
62         char *argv[MAX_ARGS + 1];
63 # if defined(LOGIN_CAP)
64         struct passwd   *pwd;
65         login_cap_t *lc;
66 # endif
67 #if WANT_GLOBBING
68         char **pop, *vv[2];
69         int gargc;
70         char *gargv[1000];
71         extern char **glob(), **copyblk();
72 #endif
73
74         if ((*type != 'r' && *type != 'w') || type[1])
75                 return(NULL);
76
77         if (!pids) {
78                 if ((fds = getdtablesize()) <= 0)
79                         return(NULL);
80                 if (!(pids = (PID_T *)malloc((u_int)(fds * sizeof(PID_T)))))
81                         return(NULL);
82                 bzero((char *)pids, fds * sizeof(PID_T));
83         }
84         if (pipe(pdes) < 0)
85                 return(NULL);
86
87         /* break up string into pieces */
88         for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL)
89                 if (!(argv[argc++] = strtok(cp, " \t\n")))
90                         break;
91         argv[MAX_ARGS] = NULL;
92
93 #if WANT_GLOBBING
94         /* glob each piece */
95         gargv[0] = argv[0];
96         for (gargc = argc = 1; argv[argc]; argc++) {
97                 if (!(pop = glob(argv[argc]))) {        /* globbing failed */
98                         vv[0] = argv[argc];
99                         vv[1] = NULL;
100                         pop = copyblk(vv);
101                 }
102                 argv[argc] = (char *)pop;               /* save to free later */
103                 while (*pop && gargc < 1000)
104                         gargv[gargc++] = *pop++;
105         }
106         gargv[gargc] = NULL;
107 #endif
108
109         iop = NULL;
110         switch(pid = vfork()) {
111         case -1:                        /* error */
112                 (void)close(pdes[0]);
113                 (void)close(pdes[1]);
114                 goto pfree;
115                 /* NOTREACHED */
116         case 0:                         /* child */
117                 if (e != NULL) {
118 #ifdef SYSLOG
119                         closelog();
120 #endif
121
122                         /* get new pgrp, void tty, etc.
123                          */
124                         (void) setsid();
125                 }
126                 if (*type == 'r') {
127                         /* Do not share our parent's stdin */
128                         (void)close(0);
129                         (void)open(_PATH_DEVNULL, O_RDWR);
130                         if (pdes[1] != 1) {
131                                 dup2(pdes[1], 1);
132                                 dup2(pdes[1], 2);       /* stderr, too! */
133                                 (void)close(pdes[1]);
134                         }
135                         (void)close(pdes[0]);
136                 } else {
137                         if (pdes[0] != 0) {
138                                 dup2(pdes[0], 0);
139                                 (void)close(pdes[0]);
140                         }
141                         /* Hack: stdout gets revoked */
142                         (void)close(1);
143                         (void)open(_PATH_DEVNULL, O_RDWR);
144                         (void)close(2);
145                         (void)open(_PATH_DEVNULL, O_RDWR);
146                         (void)close(pdes[1]);
147                 }
148 # if defined(LOGIN_CAP)
149                 if (e != NULL) {
150                         /* Set user's entire context, but skip the environment
151                          * as cron provides a separate interface for this
152                          */
153                         usernm = env_get("LOGNAME", e->envp);
154                         if ((pwd = getpwnam(usernm)) == NULL)
155                                 pwd = getpwuid(e->uid);
156                         lc = NULL;
157                         if (pwd != NULL) {
158                                 pwd->pw_gid = e->gid;
159                                 if (e->class != NULL)
160                                         lc = login_getclass(e->class);
161                         }
162                         if (pwd &&
163                             setusercontext(lc, pwd, e->uid,
164                                     LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
165                                 (void) endpwent();
166                         else {
167                                 /* fall back to the old method */
168                                 (void) endpwent();
169 # endif
170                                 /* set our directory, uid and gid.  Set gid first,
171                                  * since once we set uid, we've lost root privledges.
172                                  */
173                                 setgid(e->gid);
174 # if defined(BSD)
175                                 initgroups(usernm, e->gid);
176 # endif
177                                 setlogin(usernm);
178                                 setuid(e->uid);         /* we aren't root after this..*/
179 #if defined(LOGIN_CAP)
180                         }
181                         if (lc != NULL)
182                                 login_close(lc);
183 #endif
184                         chdir(env_get("HOME", e->envp));
185                 }
186 #if WANT_GLOBBING
187                 execvp(gargv[0], gargv);
188 #else
189                 execvp(argv[0], argv);
190 #endif
191                 _exit(1);
192         }
193         /* parent; assume fdopen can't fail...  */
194         if (*type == 'r') {
195                 iop = fdopen(pdes[0], type);
196                 (void)close(pdes[1]);
197         } else {
198                 iop = fdopen(pdes[1], type);
199                 (void)close(pdes[0]);
200         }
201         pids[fileno(iop)] = pid;
202
203 pfree:
204 #if WANT_GLOBBING
205         for (argc = 1; argv[argc] != NULL; argc++) {
206 /*              blkfree((char **)argv[argc]);   */
207                 free((char *)argv[argc]);
208         }
209 #endif
210         return(iop);
211 }
212
213 int
214 cron_pclose(iop)
215         FILE *iop;
216 {
217         register int fdes;
218         int omask;
219         WAIT_T stat_loc;
220         PID_T pid;
221
222         /*
223          * pclose returns -1 if stream is not associated with a
224          * `popened' command, or, if already `pclosed'.
225          */
226         if (pids == 0 || pids[fdes = fileno(iop)] == 0)
227                 return(-1);
228         (void)fclose(iop);
229         omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
230         while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1)
231                 ;
232         (void)sigsetmask(omask);
233         pids[fdes] = 0;
234         return (pid == -1 ? -1 : WEXITSTATUS(stat_loc));
235 }