1dcb0790bSMark Johnston /*-
2dcb0790bSMark Johnston * Copyright (c) 2018 Christian S.J. Peron
3dcb0790bSMark Johnston * All rights reserved.
4dcb0790bSMark Johnston *
5dcb0790bSMark Johnston * Redistribution and use in source and binary forms, with or without
6dcb0790bSMark Johnston * modification, are permitted provided that the following conditions
7dcb0790bSMark Johnston * are met:
8dcb0790bSMark Johnston * 1. Redistributions of source code must retain the above copyright
9dcb0790bSMark Johnston * notice, this list of conditions and the following disclaimer.
10dcb0790bSMark Johnston * 2. Redistributions in binary form must reproduce the above copyright
11dcb0790bSMark Johnston * notice, this list of conditions and the following disclaimer in the
12dcb0790bSMark Johnston * documentation and/or other materials provided with the distribution.
13dcb0790bSMark Johnston *
14dcb0790bSMark Johnston * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15dcb0790bSMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16dcb0790bSMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17dcb0790bSMark Johnston * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18dcb0790bSMark Johnston * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19dcb0790bSMark Johnston * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20dcb0790bSMark Johnston * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21dcb0790bSMark Johnston * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22dcb0790bSMark Johnston * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23dcb0790bSMark Johnston * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24dcb0790bSMark Johnston * SUCH DAMAGE.
25dcb0790bSMark Johnston */
26a9be8f99SMark Johnston
27dcb0790bSMark Johnston #include <sys/types.h>
28dcb0790bSMark Johnston #include <sys/socket.h>
29dcb0790bSMark Johnston
30dcb0790bSMark Johnston #include <bsm/audit.h>
31dcb0790bSMark Johnston #include <bsm/libbsm.h>
32dcb0790bSMark Johnston
33dcb0790bSMark Johnston #include <netinet/in.h>
34dcb0790bSMark Johnston
35a9be8f99SMark Johnston #include <err.h>
36a9be8f99SMark Johnston #include <netdb.h>
37dcb0790bSMark Johnston #include <pwd.h>
38*1238610aSMark Johnston #include <stdbool.h>
39a9be8f99SMark Johnston #include <stdio.h>
40a9be8f99SMark Johnston #include <stdlib.h>
41dcb0790bSMark Johnston #include <string.h>
42dcb0790bSMark Johnston #include <unistd.h>
43dcb0790bSMark Johnston
44dcb0790bSMark Johnston static void
usage(char * prog)45dcb0790bSMark Johnston usage(char *prog)
46dcb0790bSMark Johnston {
47dcb0790bSMark Johnston (void)fprintf(stderr,
48*1238610aSMark Johnston "usage: %s [-46U] [-a auid] [-m mask] [-p port] [-s source] command ...\n",
49dcb0790bSMark Johnston prog);
50dcb0790bSMark Johnston exit(1);
51dcb0790bSMark Johnston }
52dcb0790bSMark Johnston
53dcb0790bSMark Johnston int
main(int argc,char * argv[])54dcb0790bSMark Johnston main(int argc, char *argv [])
55dcb0790bSMark Johnston {
56dcb0790bSMark Johnston struct sockaddr_in6 *sin6;
57dcb0790bSMark Johnston struct sockaddr_in *sin;
58dcb0790bSMark Johnston struct addrinfo hints;
59dcb0790bSMark Johnston auditinfo_addr_t aia;
60551191e1SMark Johnston char *aflag, *mflag, *sflag, *prog;
61*1238610aSMark Johnston dev_t term_port;
62*1238610aSMark Johnston uint32_t term_type;
63dcb0790bSMark Johnston int ch, error;
64*1238610aSMark Johnston bool Uflag;
65dcb0790bSMark Johnston
66a9be8f99SMark Johnston aflag = mflag = sflag = NULL;
67*1238610aSMark Johnston Uflag = false;
68a9be8f99SMark Johnston
69dcb0790bSMark Johnston prog = argv[0];
70dcb0790bSMark Johnston bzero(&aia, sizeof(aia));
71dcb0790bSMark Johnston bzero(&hints, sizeof(hints));
72*1238610aSMark Johnston term_type = AU_IPv4;
73dcb0790bSMark Johnston hints.ai_family = PF_UNSPEC;
74*1238610aSMark Johnston while ((ch = getopt(argc, argv, "46a:m:p:s:U")) != -1)
75dcb0790bSMark Johnston switch (ch) {
76dcb0790bSMark Johnston case '4':
77dcb0790bSMark Johnston hints.ai_family = PF_INET;
78dcb0790bSMark Johnston break;
79dcb0790bSMark Johnston case '6':
80dcb0790bSMark Johnston hints.ai_family = PF_INET6;
81dcb0790bSMark Johnston break;
82dcb0790bSMark Johnston case 'a':
83dcb0790bSMark Johnston aflag = optarg;
84dcb0790bSMark Johnston break;
85dcb0790bSMark Johnston case 'm':
86dcb0790bSMark Johnston mflag = optarg;
87dcb0790bSMark Johnston break;
88dcb0790bSMark Johnston case 'p':
89*1238610aSMark Johnston term_port = htons(atoi(optarg));
90dcb0790bSMark Johnston break;
91a9be8f99SMark Johnston case 's':
92a9be8f99SMark Johnston sflag = optarg;
93a9be8f99SMark Johnston break;
94*1238610aSMark Johnston case 'U':
95*1238610aSMark Johnston Uflag = true;
96*1238610aSMark Johnston break;
97dcb0790bSMark Johnston default:
98dcb0790bSMark Johnston usage(prog);
99dcb0790bSMark Johnston /* NOT REACHED */
100dcb0790bSMark Johnston }
101dcb0790bSMark Johnston argc -= optind;
102dcb0790bSMark Johnston argv += optind;
103dcb0790bSMark Johnston if (argc == 0)
104dcb0790bSMark Johnston usage(prog);
105*1238610aSMark Johnston
106*1238610aSMark Johnston if (Uflag) {
107*1238610aSMark Johnston if (getaudit_addr(&aia, sizeof(aia)) < 0)
108*1238610aSMark Johnston err(1, "getaudit_addr");
109*1238610aSMark Johnston }
110dcb0790bSMark Johnston if (aflag) {
111*1238610aSMark Johnston struct passwd *pwd;
112*1238610aSMark Johnston
113dcb0790bSMark Johnston pwd = getpwnam(aflag);
114dcb0790bSMark Johnston if (pwd == NULL) {
115551191e1SMark Johnston char *r;
116551191e1SMark Johnston
117dcb0790bSMark Johnston aia.ai_auid = strtoul(aflag, &r, 10);
118551191e1SMark Johnston if (*r != '\0')
119dcb0790bSMark Johnston errx(1, "%s: invalid user", aflag);
120dcb0790bSMark Johnston } else
121dcb0790bSMark Johnston aia.ai_auid = pwd->pw_uid;
122dcb0790bSMark Johnston }
123dcb0790bSMark Johnston if (mflag) {
124dcb0790bSMark Johnston if (getauditflagsbin(mflag, &aia.ai_mask) < 0)
125dcb0790bSMark Johnston err(1, "getauditflagsbin");
126dcb0790bSMark Johnston }
127dcb0790bSMark Johnston if (sflag) {
128*1238610aSMark Johnston struct addrinfo *res;
129*1238610aSMark Johnston
130dcb0790bSMark Johnston error = getaddrinfo(sflag, NULL, &hints, &res);
131dcb0790bSMark Johnston if (error)
132dcb0790bSMark Johnston errx(1, "%s", gai_strerror(error));
133dcb0790bSMark Johnston switch (res->ai_family) {
134dcb0790bSMark Johnston case PF_INET6:
135a9be8f99SMark Johnston sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
136dcb0790bSMark Johnston bcopy(&sin6->sin6_addr.s6_addr,
137dcb0790bSMark Johnston &aia.ai_termid.at_addr[0],
138dcb0790bSMark Johnston sizeof(struct in6_addr));
139*1238610aSMark Johnston term_type = AU_IPv6;
140dcb0790bSMark Johnston break;
141dcb0790bSMark Johnston case PF_INET:
142a9be8f99SMark Johnston sin = (struct sockaddr_in *)(void *)res->ai_addr;
143dcb0790bSMark Johnston bcopy(&sin->sin_addr.s_addr,
144dcb0790bSMark Johnston &aia.ai_termid.at_addr[0],
145dcb0790bSMark Johnston sizeof(struct in_addr));
146*1238610aSMark Johnston term_type = AU_IPv4;
147dcb0790bSMark Johnston break;
148dcb0790bSMark Johnston }
149dcb0790bSMark Johnston }
150*1238610aSMark Johnston if (!Uflag || sflag) {
151*1238610aSMark Johnston aia.ai_termid.at_port = term_port;
152*1238610aSMark Johnston aia.ai_termid.at_type = term_type;
153dcb0790bSMark Johnston }
154*1238610aSMark Johnston if (setaudit_addr(&aia, sizeof(aia)) < 0)
155*1238610aSMark Johnston err(1, "setaudit_addr");
156dcb0790bSMark Johnston (void)execvp(*argv, argv);
157dcb0790bSMark Johnston err(1, "%s", *argv);
158dcb0790bSMark Johnston }
159