xref: /openbsd/usr.sbin/cron/popen.c (revision 891d7ab6)
1 /*	$OpenBSD: popen.c,v 1.21 2009/10/27 23:59:51 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software written by Ken Arnold and
8  * published in UNIX Review, Vol. 6, No. 8.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 /* this came out of the ftpd sources; it's been modified to avoid the
37  * globbing stuff since we don't need it.  also execvp instead of execv.
38  */
39 
40 #include "cron.h"
41 
42 #define MAX_ARGV	100
43 #define MAX_GARGV	1000
44 
45 /*
46  * Special version of popen which avoids call to shell.  This ensures noone
47  * may create a pipe to a hidden program as a side effect of a list or dir
48  * command.
49  */
50 static PID_T *pids;
51 static int fds;
52 
53 FILE *
54 cron_popen(char *program, char *type, struct passwd *pw) {
55 	char *cp;
56 	FILE *iop;
57 	int argc, pdes[2];
58 	PID_T pid;
59 	char *argv[MAX_ARGV];
60 
61 	if ((*type != 'r' && *type != 'w') || type[1] != '\0')
62 		return (NULL);
63 
64 	if (!pids) {
65 		if ((fds = sysconf(_SC_OPEN_MAX)) <= 0)
66 			return (NULL);
67 		if (!(pids = calloc(fds, sizeof(PID_T))))
68 			return (NULL);
69 	}
70 	if (pipe(pdes) < 0)
71 		return (NULL);
72 
73 	/* break up string into pieces */
74 	for (argc = 0, cp = program; argc < MAX_ARGV - 1; cp = NULL)
75 		if (!(argv[argc++] = strtok(cp, " \t\n")))
76 			break;
77 	argv[MAX_ARGV-1] = NULL;
78 
79 	switch (pid = fork()) {
80 	case -1:			/* error */
81 		(void)close(pdes[0]);
82 		(void)close(pdes[1]);
83 		return (NULL);
84 		/* NOTREACHED */
85 	case 0:				/* child */
86 		if (pw) {
87 #ifdef LOGIN_CAP
88 			if (setusercontext(0, pw, pw->pw_uid, LOGIN_SETALL) < 0) {
89 				fprintf(stderr,
90 				    "setusercontext failed for %s\n",
91 				    pw->pw_name);
92 				_exit(ERROR_EXIT);
93 			}
94 #else
95 			if (setgid(pw->pw_gid) < 0 ||
96 			    initgroups(pw->pw_name, pw->pw_gid) < 0) {
97 				fprintf(stderr,
98 				    "unable to set groups for %s\n",
99 				    pw->pw_name);
100 				_exit(1);
101 			}
102 #if (defined(BSD)) && (BSD >= 199103)
103 			setlogin(pw->pw_name);
104 #endif /* BSD */
105 			if (setuid(pw->pw_uid)) {
106 				fprintf(stderr,
107 				    "unable to set uid for %s\n",
108 				    pw->pw_name);
109 				_exit(1);
110 			}
111 #endif /* LOGIN_CAP */
112 		}
113 		if (*type == 'r') {
114 			if (pdes[1] != STDOUT) {
115 				dup2(pdes[1], STDOUT);
116 				(void)close(pdes[1]);
117 			}
118 			dup2(STDOUT, STDERR);	/* stderr too! */
119 			(void)close(pdes[0]);
120 		} else {
121 			if (pdes[0] != STDIN) {
122 				dup2(pdes[0], STDIN);
123 				(void)close(pdes[0]);
124 			}
125 			(void)close(pdes[1]);
126 		}
127 		execvp(argv[0], argv);
128 		_exit(1);
129 	}
130 
131 	/* parent; assume fdopen can't fail...  */
132 	if (*type == 'r') {
133 		iop = fdopen(pdes[0], type);
134 		(void)close(pdes[1]);
135 	} else {
136 		iop = fdopen(pdes[1], type);
137 		(void)close(pdes[0]);
138 	}
139 	pids[fileno(iop)] = pid;
140 
141 	return (iop);
142 }
143 
144 int
145 cron_pclose(FILE *iop) {
146 	int fdes;
147 	PID_T pid;
148 	WAIT_T status;
149 	sigset_t sigset, osigset;
150 
151 	/*
152 	 * pclose returns -1 if stream is not associated with a
153 	 * `popened' command, or, if already `pclosed'.
154 	 */
155 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
156 		return (-1);
157 	(void)fclose(iop);
158 	sigemptyset(&sigset);
159 	sigaddset(&sigset, SIGINT);
160 	sigaddset(&sigset, SIGQUIT);
161 	sigaddset(&sigset, SIGHUP);
162 	sigprocmask(SIG_BLOCK, &sigset, &osigset);
163 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
164 		continue;
165 	sigprocmask(SIG_SETMASK, &osigset, NULL);
166 	pids[fdes] = 0;
167 	if (pid < 0)
168 		return (pid);
169 	if (WIFEXITED(status))
170 		return (WEXITSTATUS(status));
171 	return (1);
172 }
173