GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: libexec/ftpd/popen.c Lines: 0 72 0.0 %
Date: 2017-11-13 Branches: 0 49 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: popen.c,v 1.26 2016/02/29 17:50:34 jca Exp $	*/
2
/*	$NetBSD: popen.c,v 1.5 1995/04/11 02:45:00 cgd Exp $	*/
3
4
/*
5
 * Copyright (c) 1988, 1993, 1994
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * This code is derived from software written by Ken Arnold and
9
 * published in UNIX Review, Vol. 6, No. 8.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
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
#include <sys/types.h>
38
#include <sys/wait.h>
39
40
#include <errno.h>
41
#include <glob.h>
42
#include <signal.h>
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include <syslog.h>
47
#include <unistd.h>
48
49
#include <netinet/in.h>
50
51
#include "monitor.h"
52
#include "extern.h"
53
54
/*
55
 * Special version of popen which avoids call to shell.  This ensures noone
56
 * may create a pipe to a hidden program as a side effect of a list or dir
57
 * command.
58
 */
59
#define MAX_ARGV	100
60
#define MAX_GARGV	1000
61
62
FILE *
63
ftpd_popen(char *program, char *type, pid_t *pidptr)
64
{
65
	char *cp;
66
	FILE *iop;
67
	int argc, gargc, pdes[2];
68
	pid_t pid;
69
	char **pop, *argv[MAX_ARGV], *gargv[MAX_GARGV];
70
71
	if ((*type != 'r' && *type != 'w') || type[1])
72
		return (NULL);
73
74
	if (pipe(pdes) < 0)
75
		return (NULL);
76
77
	/* break up string into pieces */
78
	for (argc = 0, cp = program;argc < MAX_ARGV-1; cp = NULL)
79
		if (!(argv[argc++] = strtok(cp, " \t\n")))
80
			break;
81
	argv[MAX_ARGV-1] = NULL;
82
83
	/* glob each piece */
84
	gargv[0] = argv[0];
85
	for (gargc = argc = 1; argv[argc]; argc++) {
86
		glob_t gl;
87
88
		memset(&gl, 0, sizeof(gl));
89
		if (glob(argv[argc],
90
		    GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE|GLOB_LIMIT,
91
		    NULL, &gl)) {
92
			if (gargc < MAX_GARGV-1) {
93
				gargv[gargc++] = strdup(argv[argc]);
94
				if (gargv[gargc -1] == NULL)
95
					fatal ("Out of memory.");
96
			}
97
98
		} else if (gl.gl_pathc > 0) {
99
			for (pop = gl.gl_pathv; *pop && gargc < MAX_GARGV-1; pop++) {
100
				gargv[gargc++] = strdup(*pop);
101
				if (gargv[gargc - 1] == NULL)
102
					fatal ("Out of memory.");
103
			}
104
		}
105
		globfree(&gl);
106
	}
107
	gargv[gargc] = NULL;
108
109
	iop = NULL;
110
111
	switch (pid = fork()) {
112
	case -1:			/* error */
113
		(void)close(pdes[0]);
114
		(void)close(pdes[1]);
115
		goto pfree;
116
		/* NOTREACHED */
117
	case 0:				/* child */
118
		if (*type == 'r') {
119
			if (pdes[1] != STDOUT_FILENO) {
120
				dup2(pdes[1], STDOUT_FILENO);
121
				(void)close(pdes[1]);
122
			}
123
			dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */
124
			(void)close(pdes[0]);
125
		} else {
126
			if (pdes[0] != STDIN_FILENO) {
127
				dup2(pdes[0], STDIN_FILENO);
128
				(void)close(pdes[0]);
129
			}
130
			(void)close(pdes[1]);
131
		}
132
		closelog();
133
134
		if (strcmp(gargv[0], "/bin/ls") == 0) {
135
			extern int optreset;
136
			extern int ls_main(int, char **);
137
138
			/* reset getopt for ls_main */
139
			optreset = optind = 1;
140
			exit(ls_main(gargc, gargv));
141
		}
142
143
		execv(gargv[0], gargv);
144
		_exit(1);
145
	}
146
	/* parent; assume fdopen can't fail...  */
147
	if (*type == 'r') {
148
		iop = fdopen(pdes[0], type);
149
		(void)close(pdes[1]);
150
	} else {
151
		iop = fdopen(pdes[1], type);
152
		(void)close(pdes[0]);
153
	}
154
	*pidptr = pid;
155
156
pfree:	for (argc = 1; gargv[argc] != NULL; argc++)
157
		free(gargv[argc]);
158
159
	return (iop);
160
}
161
162
int
163
ftpd_pclose(FILE *iop, pid_t pid)
164
{
165
	int status;
166
	pid_t rv;
167
	sigset_t sigset, osigset;
168
169
	(void)fclose(iop);
170
	sigemptyset(&sigset);
171
	sigaddset(&sigset, SIGINT);
172
	sigaddset(&sigset, SIGQUIT);
173
	sigaddset(&sigset, SIGHUP);
174
	sigprocmask(SIG_BLOCK, &sigset, &osigset);
175
	while ((rv = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
176
		continue;
177
	sigprocmask(SIG_SETMASK, &osigset, NULL);
178
	if (rv < 0)
179
		return (-1);
180
	if (WIFEXITED(status))
181
		return (WEXITSTATUS(status));
182
	return (1);
183
}