xref: /netbsd-src/external/bsd/cron/dist/popen.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: popen.c,v 1.3 2011/07/17 01:16:46 christos 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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /* this came out of the ftpd sources; it's been modified to avoid the
38  * globbing stuff since we don't need it.  also execvp instead of execv.
39  */
40 
41 #include <sys/cdefs.h>
42 #ifndef lint
43 #if 0
44 static sccsid[] = "@(#)popen.c	8.3 (Berkeley) 4/6/94";
45 static char rcsid[] = "Id: popen.c,v 1.6 2003/02/16 04:40:01 vixie Exp";
46 #else
47 __RCSID("$NetBSD: popen.c,v 1.3 2011/07/17 01:16:46 christos Exp $");
48 #endif
49 #endif /* not lint */
50 
51 #include "cron.h"
52 
53 #define MAX_ARGV	100
54 #define MAX_GARGV	1000
55 
56 /*
57  * Special version of popen which avoids call to shell.  This ensures noone
58  * may create a pipe to a hidden program as a side effect of a list or dir
59  * command.
60  */
61 static PID_T *pids;
62 static long fds;
63 
64 FILE *
65 cron_popen(char *program, const char *type, struct passwd *pw) {
66 	char *cp;
67 	FILE *iop;
68 	int argc, pdes[2];
69 	PID_T pid;
70 	char *argv[MAX_ARGV];
71 
72 	if ((*type != 'r' && *type != 'w') || type[1] != '\0')
73 		return (NULL);
74 
75 	if (!pids) {
76 		size_t len;
77 		if ((fds = sysconf(_SC_OPEN_MAX)) <= 0)
78 			return (NULL);
79 		len = fds * sizeof(*pids);
80 		if ((pids = malloc(len)) == NULL)
81 			return (NULL);
82 		(void)memset(pids, 0, len);
83 	}
84 	if (pipe(pdes) < 0)
85 		return (NULL);
86 
87 	/* break up string into pieces */
88 	for (argc = 0, cp = program; argc < MAX_ARGV - 1; cp = NULL)
89 		if (!(argv[argc++] = strtok(cp, " \t\n")))
90 			break;
91 	argv[MAX_ARGV-1] = NULL;
92 
93 	switch (pid = vfork()) {
94 	case -1:			/* error */
95 		(void)close(pdes[0]);
96 		(void)close(pdes[1]);
97 		return (NULL);
98 		/* NOTREACHED */
99 	case 0:				/* child */
100 		if (pw) {
101 			if (setsid() == -1)
102 				warn("setsid() failed for %s", pw->pw_name);
103 #ifdef LOGIN_CAP
104 			if (setusercontext(0, pw, pw->pw_uid, LOGIN_SETALL) < 0)
105 			{
106 				warn("setusercontext() failed for %s",
107 				    pw->pw_name);
108 				_exit(ERROR_EXIT);
109 			}
110 #else
111 			if (setgid(pw->pw_gid) < 0 ||
112 			    initgroups(pw->pw_name, pw->pw_gid) < 0) {
113 				warn("unable to set groups for %s",
114 				    pw->pw_name);
115 				_exit(ERROR_EXIT);
116 			}
117 #if (defined(BSD)) && (BSD >= 199103)
118 			if (setlogin(pw->pw_name) < 0) {
119 				warn("setlogin() failed for %s",
120 				    pw->pw_name);
121 				_exit(ERROR_EXIT);
122 			}
123 #endif /* BSD */
124 			if (setuid(pw->pw_uid)) {
125 				warn("unable to set uid for %s", pw->pw_name);
126 				_exit(ERROR_EXIT);
127 			}
128 #endif /* LOGIN_CAP */
129 		}
130 		if (*type == 'r') {
131 			if (pdes[1] != STDOUT) {
132 				(void)dup2(pdes[1], STDOUT);
133 				(void)close(pdes[1]);
134 			}
135 			(void)dup2(STDOUT, STDERR);	/* stderr too! */
136 			(void)close(pdes[0]);
137 		} else {
138 			if (pdes[0] != STDIN) {
139 				(void)dup2(pdes[0], STDIN);
140 				(void)close(pdes[0]);
141 			}
142 			(void)close(pdes[1]);
143 		}
144 		(void)execvp(argv[0], argv);
145 		_exit(ERROR_EXIT);
146 	}
147 
148 	/* parent; assume fdopen can't fail...  */
149 	if (*type == 'r') {
150 		iop = fdopen(pdes[0], type);
151 		(void)close(pdes[1]);
152 	} else {
153 		iop = fdopen(pdes[1], type);
154 		(void)close(pdes[0]);
155 	}
156 	pids[fileno(iop)] = pid;
157 
158 	return (iop);
159 }
160 
161 int
162 cron_pclose(FILE *iop) {
163 	int fdes;
164 	PID_T pid;
165 	WAIT_T status;
166 	sigset_t sset, osset;
167 
168 	/*
169 	 * pclose returns -1 if stream is not associated with a
170 	 * `popened' command, or, if already `pclosed'.
171 	 */
172 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
173 		return (-1);
174 	(void)fclose(iop);
175 	(void)sigemptyset(&sset);
176 	(void)sigaddset(&sset, SIGINT);
177 	(void)sigaddset(&sset, SIGQUIT);
178 	(void)sigaddset(&sset, SIGHUP);
179 	(void)sigprocmask(SIG_BLOCK, &sset, &osset);
180 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
181 		continue;
182 	(void)sigprocmask(SIG_SETMASK, &osset, NULL);
183 	pids[fdes] = 0;
184 	if (pid < 0)
185 		return (pid);
186 	if (WIFEXITED(status))
187 		return (WEXITSTATUS(status));
188 	else
189 		return WTERMSIG(status);
190 }
191