xref: /openbsd-src/usr.sbin/smtpd/mail.mda.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*
2  * Copyright (c) 2017 Gilles Chehade <gilles@poolp.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/wait.h>
20 
21 #include <ctype.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <netdb.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sysexits.h>
31 #include <unistd.h>
32 
33 int
34 main(int argc, char *argv[])
35 {
36 	int ch;
37 	int ret;
38 
39 	if (! geteuid())
40 		errx(1, "mail.mda: may not be executed as root");
41 
42 	while ((ch = getopt(argc, argv, "")) != -1) {
43 		switch (ch) {
44 		default:
45 			break;
46 		}
47 	}
48 	argc -= optind;
49 	argv += optind;
50 
51 	if (argc == 0)
52 		errx(1, "mail.mda: command required");
53 
54 	if (argc > 1)
55 		errx(1, "mail.mda: only one command is supported");
56 
57 	/* could not obtain a shell or could not obtain wait status,
58 	 * tempfail */
59 	if ((ret = system(argv[0])) == -1)
60 		errx(EX_TEMPFAIL, "%s", strerror(errno));
61 
62 	/* not exited properly but we have no details,
63 	 * tempfail */
64 	if (! WIFEXITED(ret))
65 		exit(EX_TEMPFAIL);
66 
67 	exit(WEXITSTATUS(ret));
68 }
69