xref: /netbsd-src/usr.bin/mail/mime_child.c (revision d13b4aaeef9692d65eb0c9bd1d06a577b46a058b)
1 /*	$NetBSD: mime_child.c,v 1.10 2019/12/14 20:21:43 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Anon Ymous.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #ifdef MIME_SUPPORT
34 
35 #include <sys/cdefs.h>
36 #ifndef __lint__
37 __RCSID("$NetBSD: mime_child.c,v 1.10 2019/12/14 20:21:43 christos Exp $");
38 #endif /* not __lint__ */
39 
40 #include <assert.h>
41 #include <fcntl.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 
47 #include "def.h"
48 #include "extern.h"
49 
50 #ifdef MIME_SUPPORT
51 #include "mime.h"
52 #include "mime_child.h"
53 #endif
54 
55 /*
56  * This module contains the core routines used by mime modules that
57  * need to fork children.  Nothing else in the mime modules should be
58  * doing a pipe(), fork(), or a call to any popen functions that do.
59  * These routines are tied to the file registry in popen.c and hence
60  * share much of popen code.
61  */
62 
63 #define READ 0
64 #define WRITE 1
65 
66 static int
get_cmd_flags(const char * cmd,const char ** cmd_start)67 get_cmd_flags(const char *cmd, const char **cmd_start)
68 {
69 	const char *cp;
70 	int flags;
71 
72 	flags = 0;
73 	for (cp = cmd; cp && *cp; cp++)
74 		switch (*cp) {
75 		case '+':
76 			flags |= CMD_FLAG_ALTERNATIVE;
77 			break;
78 		case '-':
79 			flags |= CMD_FLAG_NO_DECODE;
80 			break;
81 		case '!':
82 			flags |= CMD_FLAG_SHELLCMD;
83 			break;
84 		default:
85 			goto done;
86 		}
87  done:
88 	if (cmd_start)
89 		*cmd_start = cp;
90 
91 	return flags;
92 }
93 
94 
95 static int
prepare_pipe(sigset_t * nset,int p[2])96 prepare_pipe(sigset_t *nset, int p[2])
97 {
98 	if (pipe2(p, O_CLOEXEC) == -1)
99 		return -1;
100 
101 	/*
102 	 * We _must_ ignore SIGINT and SIGPIPE or the child
103 	 * will end up in our earlier handlers.
104 	 */
105 	(void)sigemptyset(nset);
106 	(void)sigaddset(nset, SIGINT);
107 	(void)sigaddset(nset, SIGPIPE);
108 	(void)sigaddset(nset, SIGHUP);
109 	(void)sigaddset(nset, SIGTSTP);
110 	(void)sigaddset(nset, SIGTTOU);
111 	(void)sigaddset(nset, SIGTTIN);
112 
113 	return 0;
114 }
115 
116 
117 PUBLIC int
mime_run_command(const char * cmd,FILE * fo)118 mime_run_command(const char *cmd, FILE *fo)
119 {
120 	sigset_t nset;
121 	FILE *nfo;
122 	pid_t pid;
123 	int p[2];
124 	int flags;
125 
126 	if (cmd == NULL)
127 		return 0;
128 
129 	flags = get_cmd_flags(cmd, &cmd);
130 	if (fo == NULL)		/* no output file, just return the flags! */
131 		return flags;
132 
133 	if ((flags & CMD_FLAG_SHELLCMD) != 0) {	/* run command under the shell */
134 		char *cp;
135 		char *shellcmd;
136 		if ((shellcmd = value(ENAME_SHELL)) == NULL)
137 			shellcmd = __UNCONST(_PATH_CSHELL);
138 		(void)sasprintf(&cp, "%s -c '%s'", shellcmd, cmd);
139 		cmd = cp;
140 	}
141 	if (prepare_pipe(&nset, p) != 0) {
142 		warn("mime_run_command: prepare_pipe");
143 		return flags;	/* XXX - this or -1? */
144 	}
145 	flush_files(fo, 0); /* flush fo, all registered files, and stdout */
146 
147 	switch (pid = start_command(cmd, &nset, p[READ], fileno(fo), NULL)) {
148 	case -1:	/* error */
149 		/* start_command already did a warn(). */
150 		warnx("mime_run_command: %s", cmd); /* tell a bit more */
151 		(void)close(p[READ]);
152 		(void)close(p[WRITE]);
153 		return flags;			/* XXX - this or -1? */
154 
155 	case 0:		/* child */
156 		assert(/*CONSTCOND*/ 0);	/* a real coding error! */
157 		/* NOTREACHED */
158 
159 	default:	/* parent */
160 		(void)close(p[READ]);
161 
162 		nfo = fdopen(p[WRITE], "we");
163 		if (nfo == NULL) {
164 			warn("mime_run_command: fdopen");
165 			(void)close(p[WRITE]);
166 			warn("fdopen");
167 			return flags;
168 		}
169 		register_file(nfo, 1, pid);
170 		return flags;
171 	}
172 }
173 
174 
175 PUBLIC void
mime_run_function(void (* fn)(FILE *,FILE *,void *),FILE * fo,void * cookie)176 mime_run_function(void (*fn)(FILE *, FILE *, void *), FILE *fo, void *cookie)
177 {
178 	sigset_t nset;
179 	FILE *nfo;
180 	pid_t pid;
181 	int p[2];
182 
183 	if (prepare_pipe(&nset, p) != 0) {
184 		warn("mime_run_function: pipe");
185 		return;
186 	}
187 	flush_files(fo, 0); /* flush fo, all registered files, and stdout */
188 
189 	switch (pid = fork()) {
190 	case -1:	/* error */
191 		warn("mime_run_function: fork");
192 		(void)close(p[READ]);
193 		(void)close(p[WRITE]);
194 		return;
195 
196 	case 0:		/* child */
197 		(void)close(p[WRITE]);
198 		prepare_child(&nset, p[READ], fileno(fo));
199 		fn(stdin, stdout, cookie);
200 		(void)fflush(stdout);
201 		_exit(0);
202 		/* NOTREACHED */
203 
204 	default:	/* parent */
205 		(void)close(p[READ]);
206 		nfo = fdopen(p[WRITE], "we");
207 		if (nfo == NULL) {
208 			warn("run_function: fdopen");
209 			(void)close(p[WRITE]);
210 			return;
211 		}
212 		register_file(nfo, 1, pid);
213 		return;
214 	}
215 }
216 
217 #endif /* MIME_SUPPORT */
218