authpf.c revision 1.2 1 1.2 itojun /* $NetBSD: authpf.c,v 1.2 2004/06/25 00:06:09 itojun Exp $ */
2 1.1 itojun /* $OpenBSD: authpf.c,v 1.75 2004/01/29 01:55:10 deraadt Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1998 - 2002 Bob Beck (beck (at) openbsd.org).
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun *
16 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 itojun * SUCH DAMAGE.
27 1.1 itojun */
28 1.1 itojun
29 1.1 itojun #include <sys/types.h>
30 1.1 itojun #include <sys/file.h>
31 1.1 itojun #include <sys/ioctl.h>
32 1.1 itojun #include <sys/socket.h>
33 1.1 itojun #include <sys/time.h>
34 1.2 itojun #include <sys/param.h>
35 1.1 itojun
36 1.1 itojun #include <net/if.h>
37 1.2 itojun #include <netinet/in.h>
38 1.1 itojun #include <net/pfvar.h>
39 1.1 itojun #include <arpa/inet.h>
40 1.1 itojun
41 1.1 itojun #include <err.h>
42 1.1 itojun #include <errno.h>
43 1.1 itojun #include <pwd.h>
44 1.1 itojun #include <signal.h>
45 1.1 itojun #include <stdio.h>
46 1.1 itojun #include <stdlib.h>
47 1.1 itojun #include <string.h>
48 1.1 itojun #include <syslog.h>
49 1.1 itojun #include <unistd.h>
50 1.1 itojun
51 1.1 itojun #include <pfctl_parser.h>
52 1.1 itojun #include <pfctl.h>
53 1.1 itojun
54 1.1 itojun #include "pathnames.h"
55 1.1 itojun
56 1.1 itojun extern int symset(const char *, const char *, int);
57 1.1 itojun
58 1.1 itojun static int read_config(FILE *);
59 1.1 itojun static void print_message(char *);
60 1.1 itojun static int allowed_luser(char *);
61 1.1 itojun static int check_luser(char *, char *);
62 1.1 itojun static int remove_stale_rulesets(void);
63 1.1 itojun static int change_filter(int, const char *, const char *);
64 1.1 itojun static void authpf_kill_states(void);
65 1.1 itojun
66 1.1 itojun int dev; /* pf device */
67 1.1 itojun char anchorname[PF_ANCHOR_NAME_SIZE] = "authpf";
68 1.1 itojun char rulesetname[PF_RULESET_NAME_SIZE];
69 1.1 itojun
70 1.1 itojun FILE *pidfp;
71 1.1 itojun char *infile; /* file name printed by yyerror() in parse.y */
72 1.1 itojun char luser[MAXLOGNAME]; /* username */
73 1.1 itojun char ipsrc[256]; /* ip as a string */
74 1.1 itojun char pidfile[MAXPATHLEN]; /* we save pid in this file. */
75 1.1 itojun
76 1.1 itojun struct timeval Tstart, Tend; /* start and end times of session */
77 1.1 itojun
78 1.1 itojun volatile sig_atomic_t want_death;
79 1.1 itojun static void need_death(int signo);
80 1.1 itojun static __dead void do_death(int);
81 1.1 itojun
82 1.1 itojun /*
83 1.1 itojun * User shell for authenticating gateways. Sole purpose is to allow
84 1.1 itojun * a user to ssh to a gateway, and have the gateway modify packet
85 1.1 itojun * filters to allow access, then remove access when the user finishes
86 1.1 itojun * up. Meant to be used only from ssh(1) connections.
87 1.1 itojun */
88 1.1 itojun int
89 1.1 itojun main(int argc, char *argv[])
90 1.1 itojun {
91 1.1 itojun int lockcnt = 0, n, pidfd;
92 1.1 itojun FILE *config;
93 1.1 itojun struct in_addr ina;
94 1.1 itojun struct passwd *pw;
95 1.1 itojun char *cp;
96 1.1 itojun uid_t uid;
97 1.1 itojun
98 1.1 itojun config = fopen(PATH_CONFFILE, "r");
99 1.1 itojun
100 1.1 itojun if ((cp = getenv("SSH_TTY")) == NULL) {
101 1.1 itojun syslog(LOG_ERR, "non-interactive session connection for authpf");
102 1.1 itojun exit(1);
103 1.1 itojun }
104 1.1 itojun
105 1.1 itojun if ((cp = getenv("SSH_CLIENT")) == NULL) {
106 1.1 itojun syslog(LOG_ERR, "cannot determine connection source");
107 1.1 itojun exit(1);
108 1.1 itojun }
109 1.1 itojun
110 1.1 itojun if (strlcpy(ipsrc, cp, sizeof(ipsrc)) >= sizeof(ipsrc)) {
111 1.1 itojun syslog(LOG_ERR, "SSH_CLIENT variable too long");
112 1.1 itojun exit(1);
113 1.1 itojun }
114 1.1 itojun cp = strchr(ipsrc, ' ');
115 1.1 itojun if (!cp) {
116 1.1 itojun syslog(LOG_ERR, "corrupt SSH_CLIENT variable %s", ipsrc);
117 1.1 itojun exit(1);
118 1.1 itojun }
119 1.1 itojun *cp = '\0';
120 1.1 itojun if (inet_pton(AF_INET, ipsrc, &ina) != 1) {
121 1.1 itojun syslog(LOG_ERR,
122 1.1 itojun "cannot determine IP from SSH_CLIENT %s", ipsrc);
123 1.1 itojun exit(1);
124 1.1 itojun }
125 1.1 itojun /* open the pf device */
126 1.1 itojun dev = open(PATH_DEVFILE, O_RDWR);
127 1.1 itojun if (dev == -1) {
128 1.1 itojun syslog(LOG_ERR, "cannot open packet filter device (%m)");
129 1.1 itojun goto die;
130 1.1 itojun }
131 1.1 itojun
132 1.1 itojun uid = getuid();
133 1.1 itojun pw = getpwuid(uid);
134 1.1 itojun if (pw == NULL) {
135 1.1 itojun syslog(LOG_ERR, "cannot find user for uid %u", uid);
136 1.1 itojun goto die;
137 1.1 itojun }
138 1.1 itojun if (strcmp(pw->pw_shell, PATH_AUTHPF_SHELL)) {
139 1.1 itojun syslog(LOG_ERR, "wrong shell for user %s, uid %u",
140 1.1 itojun pw->pw_name, pw->pw_uid);
141 1.1 itojun goto die;
142 1.1 itojun }
143 1.1 itojun
144 1.1 itojun /*
145 1.1 itojun * Paranoia, but this data _does_ come from outside authpf, and
146 1.1 itojun * truncation would be bad.
147 1.1 itojun */
148 1.1 itojun if (strlcpy(luser, pw->pw_name, sizeof(luser)) >= sizeof(luser)) {
149 1.1 itojun syslog(LOG_ERR, "username too long: %s", pw->pw_name);
150 1.1 itojun goto die;
151 1.1 itojun }
152 1.1 itojun
153 1.1 itojun if ((n = snprintf(rulesetname, sizeof(rulesetname), "%s(%ld)",
154 1.1 itojun luser, (long)getpid())) < 0 || n >= sizeof(rulesetname)) {
155 1.1 itojun syslog(LOG_INFO, "%s(%ld) too large, ruleset name will be %ld",
156 1.1 itojun luser, (long)getpid(), (long)getpid());
157 1.1 itojun if ((n = snprintf(rulesetname, sizeof(rulesetname), "%ld",
158 1.1 itojun (long)getpid())) < 0 || n >= sizeof(rulesetname)) {
159 1.1 itojun syslog(LOG_ERR, "pid too large for ruleset name");
160 1.1 itojun goto die;
161 1.1 itojun }
162 1.1 itojun }
163 1.1 itojun
164 1.1 itojun
165 1.1 itojun /* Make our entry in /var/authpf as /var/authpf/ipaddr */
166 1.1 itojun n = snprintf(pidfile, sizeof(pidfile), "%s/%s", PATH_PIDFILE, ipsrc);
167 1.1 itojun if (n < 0 || (u_int)n >= sizeof(pidfile)) {
168 1.1 itojun syslog(LOG_ERR, "path to pidfile too long");
169 1.1 itojun goto die;
170 1.1 itojun }
171 1.1 itojun
172 1.1 itojun /*
173 1.1 itojun * If someone else is already using this ip, then this person
174 1.1 itojun * wants to switch users - so kill the old process and exit
175 1.1 itojun * as well.
176 1.1 itojun *
177 1.1 itojun * Note, we could print a message and tell them to log out, but the
178 1.1 itojun * usual case of this is that someone has left themselves logged in,
179 1.1 itojun * with the authenticated connection iconized and someone else walks
180 1.1 itojun * up to use and automatically logs in before using. If this just
181 1.1 itojun * gets rid of the old one silently, the new user never knows they
182 1.1 itojun * could have used someone else's old authentication. If we
183 1.1 itojun * tell them to log out before switching users it is an invitation
184 1.1 itojun * for abuse.
185 1.1 itojun */
186 1.1 itojun
187 1.1 itojun do {
188 1.1 itojun int save_errno, otherpid = -1;
189 1.1 itojun char otherluser[MAXLOGNAME];
190 1.1 itojun
191 1.1 itojun if ((pidfd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1 ||
192 1.1 itojun (pidfp = fdopen(pidfd, "r+")) == NULL) {
193 1.1 itojun if (pidfd != -1)
194 1.1 itojun close(pidfd);
195 1.1 itojun syslog(LOG_ERR, "cannot open or create %s: %s", pidfile,
196 1.1 itojun strerror(errno));
197 1.1 itojun goto die;
198 1.1 itojun }
199 1.1 itojun
200 1.1 itojun if (flock(fileno(pidfp), LOCK_EX|LOCK_NB) == 0)
201 1.1 itojun break;
202 1.1 itojun save_errno = errno;
203 1.1 itojun
204 1.1 itojun /* Mark our pid, and username to our file. */
205 1.1 itojun
206 1.1 itojun rewind(pidfp);
207 1.1 itojun /* 31 == MAXLOGNAME - 1 */
208 1.1 itojun if (fscanf(pidfp, "%d\n%31s\n", &otherpid, otherluser) != 2)
209 1.1 itojun otherpid = -1;
210 1.1 itojun syslog(LOG_DEBUG, "tried to lock %s, in use by pid %d: %s",
211 1.1 itojun pidfile, otherpid, strerror(save_errno));
212 1.1 itojun
213 1.1 itojun if (otherpid > 0) {
214 1.1 itojun syslog(LOG_INFO,
215 1.1 itojun "killing prior auth (pid %d) of %s by user %s",
216 1.1 itojun otherpid, ipsrc, otherluser);
217 1.1 itojun if (kill((pid_t) otherpid, SIGTERM) == -1) {
218 1.1 itojun syslog(LOG_INFO,
219 1.1 itojun "could not kill process %d: (%m)",
220 1.1 itojun otherpid);
221 1.1 itojun }
222 1.1 itojun }
223 1.1 itojun
224 1.1 itojun /*
225 1.1 itojun * we try to kill the previous process and acquire the lock
226 1.1 itojun * for 10 seconds, trying once a second. if we can't after
227 1.1 itojun * 10 attempts we log an error and give up
228 1.1 itojun */
229 1.1 itojun if (++lockcnt > 10) {
230 1.1 itojun syslog(LOG_ERR, "cannot kill previous authpf (pid %d)",
231 1.1 itojun otherpid);
232 1.1 itojun goto dogdeath;
233 1.1 itojun }
234 1.1 itojun sleep(1);
235 1.1 itojun
236 1.1 itojun /* re-open, and try again. The previous authpf process
237 1.1 itojun * we killed above should unlink the file and release
238 1.1 itojun * it's lock, giving us a chance to get it now
239 1.1 itojun */
240 1.1 itojun fclose(pidfp);
241 1.1 itojun } while (1);
242 1.1 itojun
243 1.1 itojun /* revoke privs */
244 1.1 itojun seteuid(getuid());
245 1.1 itojun setuid(getuid());
246 1.1 itojun
247 1.1 itojun openlog("authpf", LOG_PID | LOG_NDELAY, LOG_DAEMON);
248 1.1 itojun
249 1.1 itojun if (!check_luser(PATH_BAN_DIR, luser) || !allowed_luser(luser)) {
250 1.1 itojun syslog(LOG_INFO, "user %s prohibited", luser);
251 1.1 itojun do_death(0);
252 1.1 itojun }
253 1.1 itojun
254 1.1 itojun if (config == NULL || read_config(config)) {
255 1.1 itojun syslog(LOG_INFO, "bad or nonexistent %s", PATH_CONFFILE);
256 1.1 itojun do_death(0);
257 1.1 itojun }
258 1.1 itojun
259 1.1 itojun if (remove_stale_rulesets()) {
260 1.1 itojun syslog(LOG_INFO, "error removing stale rulesets");
261 1.1 itojun do_death(0);
262 1.1 itojun }
263 1.1 itojun
264 1.1 itojun /* We appear to be making headway, so actually mark our pid */
265 1.1 itojun rewind(pidfp);
266 1.1 itojun fprintf(pidfp, "%ld\n%s\n", (long)getpid(), luser);
267 1.1 itojun fflush(pidfp);
268 1.1 itojun (void) ftruncate(fileno(pidfp), ftell(pidfp));
269 1.1 itojun
270 1.1 itojun if (change_filter(1, luser, ipsrc) == -1) {
271 1.1 itojun printf("Unable to modify filters\r\n");
272 1.1 itojun do_death(0);
273 1.1 itojun }
274 1.1 itojun
275 1.1 itojun signal(SIGTERM, need_death);
276 1.1 itojun signal(SIGINT, need_death);
277 1.1 itojun signal(SIGALRM, need_death);
278 1.1 itojun signal(SIGPIPE, need_death);
279 1.1 itojun signal(SIGHUP, need_death);
280 1.1 itojun signal(SIGSTOP, need_death);
281 1.1 itojun signal(SIGTSTP, need_death);
282 1.1 itojun while (1) {
283 1.1 itojun printf("\r\nHello %s, ", luser);
284 1.1 itojun printf("You are authenticated from host \"%s\"\r\n", ipsrc);
285 1.1 itojun setproctitle("%s@%s", luser, ipsrc);
286 1.1 itojun print_message(PATH_MESSAGE);
287 1.1 itojun while (1) {
288 1.1 itojun sleep(10);
289 1.1 itojun if (want_death)
290 1.1 itojun do_death(1);
291 1.1 itojun }
292 1.1 itojun }
293 1.1 itojun
294 1.1 itojun /* NOTREACHED */
295 1.1 itojun dogdeath:
296 1.1 itojun printf("\r\n\r\nSorry, this service is currently unavailable due to ");
297 1.1 itojun printf("technical difficulties\r\n\r\n");
298 1.1 itojun print_message(PATH_PROBLEM);
299 1.1 itojun printf("\r\nYour authentication process (pid %ld) was unable to run\n",
300 1.1 itojun (long)getpid());
301 1.1 itojun sleep(180); /* them lusers read reaaaaal slow */
302 1.1 itojun die:
303 1.1 itojun do_death(0);
304 1.2 itojun return (0);
305 1.1 itojun }
306 1.1 itojun
307 1.1 itojun /*
308 1.1 itojun * reads config file in PATH_CONFFILE to set optional behaviours up
309 1.1 itojun */
310 1.1 itojun static int
311 1.1 itojun read_config(FILE *f)
312 1.1 itojun {
313 1.1 itojun char buf[1024];
314 1.1 itojun int i = 0;
315 1.1 itojun
316 1.1 itojun do {
317 1.1 itojun char **ap;
318 1.1 itojun char *pair[4], *cp, *tp;
319 1.1 itojun int len;
320 1.1 itojun
321 1.1 itojun if (fgets(buf, sizeof(buf), f) == NULL) {
322 1.1 itojun fclose(f);
323 1.1 itojun return (0);
324 1.1 itojun }
325 1.1 itojun i++;
326 1.1 itojun len = strlen(buf);
327 1.1 itojun if (buf[len - 1] != '\n' && !feof(f)) {
328 1.1 itojun syslog(LOG_ERR, "line %d too long in %s", i,
329 1.1 itojun PATH_CONFFILE);
330 1.1 itojun return (1);
331 1.1 itojun }
332 1.1 itojun buf[len - 1] = '\0';
333 1.1 itojun
334 1.1 itojun for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
335 1.1 itojun ; /* nothing */
336 1.1 itojun
337 1.1 itojun if (!*cp || *cp == '#' || *cp == '\n')
338 1.1 itojun continue;
339 1.1 itojun
340 1.1 itojun for (ap = pair; ap < &pair[3] &&
341 1.1 itojun (*ap = strsep(&cp, "=")) != NULL; ) {
342 1.1 itojun if (**ap != '\0')
343 1.1 itojun ap++;
344 1.1 itojun }
345 1.1 itojun if (ap != &pair[2])
346 1.1 itojun goto parse_error;
347 1.1 itojun
348 1.1 itojun tp = pair[1] + strlen(pair[1]);
349 1.1 itojun while ((*tp == ' ' || *tp == '\t') && tp >= pair[1])
350 1.1 itojun *tp-- = '\0';
351 1.1 itojun
352 1.1 itojun if (strcasecmp(pair[0], "anchor") == 0) {
353 1.1 itojun if (!pair[1][0] || strlcpy(anchorname, pair[1],
354 1.1 itojun sizeof(anchorname)) >= sizeof(anchorname))
355 1.1 itojun goto parse_error;
356 1.1 itojun }
357 1.1 itojun } while (!feof(f) && !ferror(f));
358 1.1 itojun fclose(f);
359 1.1 itojun return (0);
360 1.1 itojun
361 1.1 itojun parse_error:
362 1.1 itojun fclose(f);
363 1.1 itojun syslog(LOG_ERR, "parse error, line %d of %s", i, PATH_CONFFILE);
364 1.1 itojun return (1);
365 1.1 itojun }
366 1.1 itojun
367 1.1 itojun
368 1.1 itojun /*
369 1.1 itojun * splatter a file to stdout - max line length of 1024,
370 1.1 itojun * used for spitting message files at users to tell them
371 1.1 itojun * they've been bad or we're unavailable.
372 1.1 itojun */
373 1.1 itojun static void
374 1.1 itojun print_message(char *filename)
375 1.1 itojun {
376 1.1 itojun char buf[1024];
377 1.1 itojun FILE *f;
378 1.1 itojun
379 1.1 itojun if ((f = fopen(filename, "r")) == NULL)
380 1.1 itojun return; /* fail silently, we don't care if it isn't there */
381 1.1 itojun
382 1.1 itojun do {
383 1.1 itojun if (fgets(buf, sizeof(buf), f) == NULL) {
384 1.1 itojun fflush(stdout);
385 1.1 itojun fclose(f);
386 1.1 itojun return;
387 1.1 itojun }
388 1.1 itojun } while (fputs(buf, stdout) != EOF && !feof(f));
389 1.1 itojun fflush(stdout);
390 1.1 itojun fclose(f);
391 1.1 itojun }
392 1.1 itojun
393 1.1 itojun /*
394 1.1 itojun * allowed_luser checks to see if user "luser" is allowed to
395 1.1 itojun * use this gateway by virtue of being listed in an allowed
396 1.1 itojun * users file, namely /etc/authpf/authpf.allow .
397 1.1 itojun *
398 1.1 itojun * If /etc/authpf/authpf.allow does not exist, then we assume that
399 1.1 itojun * all users who are allowed in by sshd(8) are permitted to
400 1.1 itojun * use this gateway. If /etc/authpf/authpf.allow does exist, then a
401 1.1 itojun * user must be listed if the connection is to continue, else
402 1.1 itojun * the session terminates in the same manner as being banned.
403 1.1 itojun */
404 1.1 itojun static int
405 1.1 itojun allowed_luser(char *luser)
406 1.1 itojun {
407 1.1 itojun char *buf, *lbuf;
408 1.1 itojun int matched;
409 1.1 itojun size_t len;
410 1.1 itojun FILE *f;
411 1.1 itojun
412 1.1 itojun if ((f = fopen(PATH_ALLOWFILE, "r")) == NULL) {
413 1.1 itojun if (errno == ENOENT) {
414 1.1 itojun /*
415 1.1 itojun * allowfile doesn't exist, thus this gateway
416 1.1 itojun * isn't restricted to certain users...
417 1.1 itojun */
418 1.1 itojun return (1);
419 1.1 itojun }
420 1.1 itojun
421 1.1 itojun /*
422 1.1 itojun * luser may in fact be allowed, but we can't open
423 1.1 itojun * the file even though it's there. probably a config
424 1.1 itojun * problem.
425 1.1 itojun */
426 1.1 itojun syslog(LOG_ERR, "cannot open allowed users file %s (%s)",
427 1.1 itojun PATH_ALLOWFILE, strerror(errno));
428 1.1 itojun return (0);
429 1.1 itojun } else {
430 1.1 itojun /*
431 1.1 itojun * /etc/authpf/authpf.allow exists, thus we do a linear
432 1.1 itojun * search to see if they are allowed.
433 1.1 itojun * also, if username "*" exists, then this is a
434 1.1 itojun * "public" gateway, such as it is, so let
435 1.1 itojun * everyone use it.
436 1.1 itojun */
437 1.1 itojun lbuf = NULL;
438 1.1 itojun while ((buf = fgetln(f, &len))) {
439 1.1 itojun if (buf[len - 1] == '\n')
440 1.1 itojun buf[len - 1] = '\0';
441 1.1 itojun else {
442 1.1 itojun if ((lbuf = (char *)malloc(len + 1)) == NULL)
443 1.1 itojun err(1, NULL);
444 1.1 itojun memcpy(lbuf, buf, len);
445 1.1 itojun lbuf[len] = '\0';
446 1.1 itojun buf = lbuf;
447 1.1 itojun }
448 1.1 itojun
449 1.1 itojun matched = strcmp(luser, buf) == 0 || strcmp("*", buf) == 0;
450 1.1 itojun
451 1.1 itojun if (lbuf != NULL) {
452 1.1 itojun free(lbuf);
453 1.1 itojun lbuf = NULL;
454 1.1 itojun }
455 1.1 itojun
456 1.1 itojun if (matched)
457 1.1 itojun return (1); /* matched an allowed username */
458 1.1 itojun }
459 1.1 itojun syslog(LOG_INFO, "denied access to %s: not listed in %s",
460 1.1 itojun luser, PATH_ALLOWFILE);
461 1.1 itojun
462 1.1 itojun /* reuse buf */
463 1.1 itojun buf = "\n\nSorry, you are not allowed to use this facility!\n";
464 1.1 itojun fputs(buf, stdout);
465 1.1 itojun }
466 1.1 itojun fflush(stdout);
467 1.1 itojun return (0);
468 1.1 itojun }
469 1.1 itojun
470 1.1 itojun /*
471 1.1 itojun * check_luser checks to see if user "luser" has been banned
472 1.1 itojun * from using us by virtue of having an file of the same name
473 1.1 itojun * in the "luserdir" directory.
474 1.1 itojun *
475 1.1 itojun * If the user has been banned, we copy the contents of the file
476 1.1 itojun * to the user's screen. (useful for telling the user what to
477 1.1 itojun * do to get un-banned, or just to tell them they aren't
478 1.1 itojun * going to be un-banned.)
479 1.1 itojun */
480 1.1 itojun static int
481 1.1 itojun check_luser(char *luserdir, char *luser)
482 1.1 itojun {
483 1.1 itojun FILE *f;
484 1.1 itojun int n;
485 1.1 itojun char tmp[MAXPATHLEN];
486 1.1 itojun
487 1.1 itojun n = snprintf(tmp, sizeof(tmp), "%s/%s", luserdir, luser);
488 1.1 itojun if (n < 0 || (u_int)n >= sizeof(tmp)) {
489 1.1 itojun syslog(LOG_ERR, "provided banned directory line too long (%s)",
490 1.1 itojun luserdir);
491 1.1 itojun return (0);
492 1.1 itojun }
493 1.1 itojun if ((f = fopen(tmp, "r")) == NULL) {
494 1.1 itojun if (errno == ENOENT) {
495 1.1 itojun /*
496 1.1 itojun * file or dir doesn't exist, so therefore
497 1.1 itojun * this luser isn't banned.. all is well
498 1.1 itojun */
499 1.1 itojun return (1);
500 1.1 itojun } else {
501 1.1 itojun /*
502 1.1 itojun * luser may in fact be banned, but we can't open the
503 1.1 itojun * file even though it's there. probably a config
504 1.1 itojun * problem.
505 1.1 itojun */
506 1.1 itojun syslog(LOG_ERR, "cannot open banned file %s (%s)",
507 1.1 itojun tmp, strerror(errno));
508 1.1 itojun return (0);
509 1.1 itojun }
510 1.1 itojun } else {
511 1.1 itojun /*
512 1.1 itojun * luser is banned - spit the file at them to
513 1.1 itojun * tell what they can do and where they can go.
514 1.1 itojun */
515 1.1 itojun syslog(LOG_INFO, "denied access to %s: %s exists",
516 1.1 itojun luser, tmp);
517 1.1 itojun
518 1.1 itojun /* reuse tmp */
519 1.1 itojun strlcpy(tmp, "\n\n-**- Sorry, you have been banned! -**-\n\n",
520 1.1 itojun sizeof(tmp));
521 1.1 itojun while (fputs(tmp, stdout) != EOF && !feof(f)) {
522 1.1 itojun if (fgets(tmp, sizeof(tmp), f) == NULL) {
523 1.1 itojun fflush(stdout);
524 1.1 itojun return (0);
525 1.1 itojun }
526 1.1 itojun }
527 1.1 itojun }
528 1.1 itojun fflush(stdout);
529 1.1 itojun return (0);
530 1.1 itojun }
531 1.1 itojun
532 1.1 itojun /*
533 1.1 itojun * Search for rulesets left by other authpf processes (either because they
534 1.1 itojun * died ungracefully or were terminated) and remove them.
535 1.1 itojun */
536 1.1 itojun static int
537 1.1 itojun remove_stale_rulesets(void)
538 1.1 itojun {
539 1.1 itojun struct pfioc_ruleset prs;
540 1.1 itojun const int action[PF_RULESET_MAX] = { PF_SCRUB,
541 1.1 itojun PF_PASS, PF_NAT, PF_BINAT, PF_RDR };
542 1.1 itojun u_int32_t nr, mnr;
543 1.1 itojun
544 1.1 itojun memset(&prs, 0, sizeof(prs));
545 1.1 itojun strlcpy(prs.anchor, anchorname, sizeof(prs.anchor));
546 1.1 itojun if (ioctl(dev, DIOCGETRULESETS, &prs)) {
547 1.1 itojun if (errno == EINVAL)
548 1.1 itojun return (0);
549 1.1 itojun else
550 1.1 itojun return (1);
551 1.1 itojun }
552 1.1 itojun
553 1.1 itojun mnr = prs.nr;
554 1.1 itojun nr = 0;
555 1.1 itojun while (nr < mnr) {
556 1.1 itojun char *s, *t;
557 1.1 itojun pid_t pid;
558 1.1 itojun
559 1.1 itojun prs.nr = nr;
560 1.1 itojun if (ioctl(dev, DIOCGETRULESET, &prs))
561 1.1 itojun return (1);
562 1.1 itojun errno = 0;
563 1.1 itojun if ((t = strchr(prs.name, '(')) == NULL)
564 1.1 itojun t = prs.name;
565 1.1 itojun else
566 1.1 itojun t++;
567 1.1 itojun pid = strtoul(t, &s, 10);
568 1.1 itojun if (!prs.name[0] || errno ||
569 1.1 itojun (*s && (t == prs.name || *s != ')')))
570 1.1 itojun return (1);
571 1.1 itojun if (kill(pid, 0) && errno != EPERM) {
572 1.1 itojun int i;
573 1.1 itojun
574 1.1 itojun for (i = 0; i < PF_RULESET_MAX; ++i) {
575 1.1 itojun struct pfioc_rule pr;
576 1.1 itojun
577 1.1 itojun memset(&pr, 0, sizeof(pr));
578 1.1 itojun memcpy(pr.anchor, prs.anchor, sizeof(pr.anchor));
579 1.1 itojun memcpy(pr.ruleset, prs.name, sizeof(pr.ruleset));
580 1.1 itojun pr.rule.action = action[i];
581 1.1 itojun if ((ioctl(dev, DIOCBEGINRULES, &pr) ||
582 1.1 itojun ioctl(dev, DIOCCOMMITRULES, &pr)) &&
583 1.1 itojun errno != EINVAL)
584 1.1 itojun return (1);
585 1.1 itojun }
586 1.1 itojun mnr--;
587 1.1 itojun } else
588 1.1 itojun nr++;
589 1.1 itojun }
590 1.1 itojun return (0);
591 1.1 itojun }
592 1.1 itojun
593 1.1 itojun /*
594 1.1 itojun * Add/remove filter entries for user "luser" from ip "ipsrc"
595 1.1 itojun */
596 1.1 itojun static int
597 1.1 itojun change_filter(int add, const char *luser, const char *ipsrc)
598 1.1 itojun {
599 1.1 itojun char fn[MAXPATHLEN];
600 1.1 itojun FILE *f = NULL;
601 1.1 itojun struct pfctl pf;
602 1.1 itojun struct pfr_buffer t;
603 1.1 itojun int i;
604 1.1 itojun
605 1.1 itojun if (luser == NULL || !luser[0] || ipsrc == NULL || !ipsrc[0]) {
606 1.1 itojun syslog(LOG_ERR, "invalid luser/ipsrc");
607 1.1 itojun goto error;
608 1.1 itojun }
609 1.1 itojun
610 1.1 itojun if (add) {
611 1.1 itojun if ((i = snprintf(fn, sizeof(fn), "%s/%s/authpf.rules",
612 1.1 itojun PATH_USER_DIR, luser)) < 0 || i >= sizeof(fn)) {
613 1.1 itojun syslog(LOG_ERR, "user rule path too long");
614 1.1 itojun goto error;
615 1.1 itojun }
616 1.1 itojun if ((f = fopen(fn, "r")) == NULL && errno != ENOENT) {
617 1.1 itojun syslog(LOG_ERR, "cannot open %s (%m)", fn);
618 1.1 itojun goto error;
619 1.1 itojun }
620 1.1 itojun if (f == NULL) {
621 1.1 itojun if (strlcpy(fn, PATH_PFRULES, sizeof(fn)) >=
622 1.1 itojun sizeof(fn)) {
623 1.1 itojun syslog(LOG_ERR, "rule path too long");
624 1.1 itojun goto error;
625 1.1 itojun }
626 1.1 itojun if ((f = fopen(fn, "r")) == NULL) {
627 1.1 itojun syslog(LOG_ERR, "cannot open %s (%m)", fn);
628 1.1 itojun goto error;
629 1.1 itojun }
630 1.1 itojun }
631 1.1 itojun }
632 1.1 itojun
633 1.1 itojun if (pfctl_load_fingerprints(dev, 0)) {
634 1.1 itojun syslog(LOG_ERR, "unable to load kernel's OS fingerprints");
635 1.1 itojun goto error;
636 1.1 itojun }
637 1.1 itojun bzero(&t, sizeof(t));
638 1.1 itojun t.pfrb_type = PFRB_TRANS;
639 1.1 itojun memset(&pf, 0, sizeof(pf));
640 1.1 itojun for (i = 0; i < PF_RULESET_MAX; ++i) {
641 1.1 itojun if (pfctl_add_trans(&t, i, anchorname, rulesetname)) {
642 1.1 itojun syslog(LOG_ERR, "pfctl_add_trans %m");
643 1.1 itojun goto error;
644 1.1 itojun }
645 1.1 itojun }
646 1.1 itojun if (pfctl_trans(dev, &t, DIOCXBEGIN, 0)) {
647 1.1 itojun syslog(LOG_ERR, "DIOCXBEGIN (%s) %m", add?"add":"remove");
648 1.1 itojun goto error;
649 1.1 itojun }
650 1.1 itojun
651 1.1 itojun if (add) {
652 1.1 itojun if (symset("user_ip", ipsrc, 0) ||
653 1.1 itojun symset("user_id", luser, 0)) {
654 1.1 itojun syslog(LOG_ERR, "symset");
655 1.1 itojun goto error;
656 1.1 itojun }
657 1.1 itojun
658 1.1 itojun pf.dev = dev;
659 1.1 itojun pf.trans = &t;
660 1.1 itojun pf.anchor = anchorname;
661 1.1 itojun pf.ruleset = rulesetname;
662 1.1 itojun
663 1.1 itojun infile = fn;
664 1.1 itojun if (parse_rules(f, &pf) < 0) {
665 1.1 itojun syslog(LOG_ERR, "syntax error in rule file: "
666 1.1 itojun "authpf rules not loaded");
667 1.1 itojun goto error;
668 1.1 itojun }
669 1.1 itojun
670 1.1 itojun infile = NULL;
671 1.1 itojun fclose(f);
672 1.1 itojun f = NULL;
673 1.1 itojun }
674 1.1 itojun
675 1.1 itojun if (pfctl_trans(dev, &t, DIOCXCOMMIT, 0)) {
676 1.1 itojun syslog(LOG_ERR, "DIOCXCOMMIT (%s) %m", add?"add":"remove");
677 1.1 itojun goto error;
678 1.1 itojun }
679 1.1 itojun
680 1.1 itojun if (add) {
681 1.1 itojun gettimeofday(&Tstart, NULL);
682 1.1 itojun syslog(LOG_INFO, "allowing %s, user %s", ipsrc, luser);
683 1.1 itojun } else {
684 1.1 itojun gettimeofday(&Tend, NULL);
685 1.1 itojun syslog(LOG_INFO, "removed %s, user %s - duration %ld seconds",
686 1.1 itojun ipsrc, luser, Tend.tv_sec - Tstart.tv_sec);
687 1.1 itojun }
688 1.1 itojun return (0);
689 1.1 itojun
690 1.1 itojun error:
691 1.1 itojun if (f != NULL)
692 1.1 itojun fclose(f);
693 1.1 itojun if (pfctl_trans(dev, &t, DIOCXROLLBACK, 0))
694 1.1 itojun syslog(LOG_ERR, "DIOCXROLLBACK (%s) %m", add?"add":"remove");
695 1.1 itojun
696 1.1 itojun infile = NULL;
697 1.1 itojun return (-1);
698 1.1 itojun }
699 1.1 itojun
700 1.1 itojun /*
701 1.1 itojun * This is to kill off states that would otherwise be left behind stateful
702 1.1 itojun * rules. This means we don't need to allow in more traffic than we really
703 1.1 itojun * want to, since we don't have to worry about any luser sessions lasting
704 1.1 itojun * longer than their ssh session. This function is based on
705 1.1 itojun * pfctl_kill_states from pfctl.
706 1.1 itojun */
707 1.1 itojun static void
708 1.1 itojun authpf_kill_states(void)
709 1.1 itojun {
710 1.1 itojun struct pfioc_state_kill psk;
711 1.1 itojun struct in_addr target;
712 1.1 itojun
713 1.1 itojun memset(&psk, 0, sizeof(psk));
714 1.1 itojun psk.psk_af = AF_INET;
715 1.1 itojun
716 1.1 itojun inet_pton(AF_INET, ipsrc, &target);
717 1.1 itojun
718 1.1 itojun /* Kill all states from ipsrc */
719 1.1 itojun psk.psk_src.addr.v.a.addr.v4 = target;
720 1.1 itojun memset(&psk.psk_src.addr.v.a.mask, 0xff,
721 1.1 itojun sizeof(psk.psk_src.addr.v.a.mask));
722 1.1 itojun if (ioctl(dev, DIOCKILLSTATES, &psk))
723 1.1 itojun syslog(LOG_ERR, "DIOCKILLSTATES failed (%m)");
724 1.1 itojun
725 1.1 itojun /* Kill all states to ipsrc */
726 1.1 itojun psk.psk_af = AF_INET;
727 1.1 itojun memset(&psk.psk_src, 0, sizeof(psk.psk_src));
728 1.1 itojun psk.psk_dst.addr.v.a.addr.v4 = target;
729 1.1 itojun memset(&psk.psk_dst.addr.v.a.mask, 0xff,
730 1.1 itojun sizeof(psk.psk_dst.addr.v.a.mask));
731 1.1 itojun if (ioctl(dev, DIOCKILLSTATES, &psk))
732 1.1 itojun syslog(LOG_ERR, "DIOCKILLSTATES failed (%m)");
733 1.1 itojun }
734 1.1 itojun
735 1.1 itojun /* signal handler that makes us go away properly */
736 1.1 itojun static void
737 1.1 itojun need_death(int signo)
738 1.1 itojun {
739 1.1 itojun want_death = 1;
740 1.1 itojun }
741 1.1 itojun
742 1.1 itojun /*
743 1.1 itojun * function that removes our stuff when we go away.
744 1.1 itojun */
745 1.1 itojun static __dead void
746 1.1 itojun do_death(int active)
747 1.1 itojun {
748 1.1 itojun int ret = 0;
749 1.1 itojun
750 1.1 itojun if (active) {
751 1.1 itojun change_filter(0, luser, ipsrc);
752 1.1 itojun authpf_kill_states();
753 1.1 itojun remove_stale_rulesets();
754 1.1 itojun }
755 1.1 itojun if (pidfp)
756 1.1 itojun ftruncate(fileno(pidfp), 0);
757 1.1 itojun if (pidfile[0])
758 1.1 itojun if (unlink(pidfile) == -1)
759 1.1 itojun syslog(LOG_ERR, "cannot unlink %s (%m)", pidfile);
760 1.1 itojun exit(ret);
761 1.1 itojun }
762 1.1 itojun
763 1.1 itojun /*
764 1.1 itojun * callbacks for parse_rules(void)
765 1.1 itojun */
766 1.1 itojun
767 1.1 itojun int
768 1.1 itojun pfctl_add_rule(struct pfctl *pf, struct pf_rule *r)
769 1.1 itojun {
770 1.1 itojun u_int8_t rs_num;
771 1.1 itojun struct pfioc_rule pr;
772 1.1 itojun
773 1.1 itojun switch (r->action) {
774 1.1 itojun case PF_PASS:
775 1.1 itojun case PF_DROP:
776 1.1 itojun rs_num = PF_RULESET_FILTER;
777 1.1 itojun break;
778 1.1 itojun case PF_SCRUB:
779 1.1 itojun rs_num = PF_RULESET_SCRUB;
780 1.1 itojun break;
781 1.1 itojun case PF_NAT:
782 1.1 itojun case PF_NONAT:
783 1.1 itojun rs_num = PF_RULESET_NAT;
784 1.1 itojun break;
785 1.1 itojun case PF_RDR:
786 1.1 itojun case PF_NORDR:
787 1.1 itojun rs_num = PF_RULESET_RDR;
788 1.1 itojun break;
789 1.1 itojun case PF_BINAT:
790 1.1 itojun case PF_NOBINAT:
791 1.1 itojun rs_num = PF_RULESET_BINAT;
792 1.1 itojun break;
793 1.1 itojun default:
794 1.1 itojun syslog(LOG_ERR, "invalid rule action %d", r->action);
795 1.1 itojun return (1);
796 1.1 itojun }
797 1.1 itojun
798 1.1 itojun bzero(&pr, sizeof(pr));
799 1.1 itojun strlcpy(pr.anchor, pf->anchor, sizeof(pr.anchor));
800 1.1 itojun strlcpy(pr.ruleset, pf->ruleset, sizeof(pr.ruleset));
801 1.1 itojun if (pfctl_add_pool(pf, &r->rpool, r->af))
802 1.1 itojun return (1);
803 1.1 itojun pr.ticket = pfctl_get_ticket(pf->trans, rs_num, pf->anchor,
804 1.1 itojun pf->ruleset);
805 1.1 itojun pr.pool_ticket = pf->paddr.ticket;
806 1.1 itojun memcpy(&pr.rule, r, sizeof(pr.rule));
807 1.1 itojun if (ioctl(pf->dev, DIOCADDRULE, &pr)) {
808 1.1 itojun syslog(LOG_ERR, "DIOCADDRULE %m");
809 1.1 itojun return (1);
810 1.1 itojun }
811 1.1 itojun pfctl_clear_pool(&r->rpool);
812 1.1 itojun return (0);
813 1.1 itojun }
814 1.1 itojun
815 1.1 itojun int
816 1.1 itojun pfctl_add_pool(struct pfctl *pf, struct pf_pool *p, sa_family_t af)
817 1.1 itojun {
818 1.1 itojun struct pf_pooladdr *pa;
819 1.1 itojun
820 1.1 itojun if (ioctl(pf->dev, DIOCBEGINADDRS, &pf->paddr)) {
821 1.1 itojun syslog(LOG_ERR, "DIOCBEGINADDRS %m");
822 1.1 itojun return (1);
823 1.1 itojun }
824 1.1 itojun pf->paddr.af = af;
825 1.1 itojun TAILQ_FOREACH(pa, &p->list, entries) {
826 1.1 itojun memcpy(&pf->paddr.addr, pa, sizeof(struct pf_pooladdr));
827 1.1 itojun if (ioctl(pf->dev, DIOCADDADDR, &pf->paddr)) {
828 1.1 itojun syslog(LOG_ERR, "DIOCADDADDR %m");
829 1.1 itojun return (1);
830 1.1 itojun }
831 1.1 itojun }
832 1.1 itojun return (0);
833 1.1 itojun }
834 1.1 itojun
835 1.1 itojun void
836 1.1 itojun pfctl_clear_pool(struct pf_pool *pool)
837 1.1 itojun {
838 1.1 itojun struct pf_pooladdr *pa;
839 1.1 itojun
840 1.1 itojun while ((pa = TAILQ_FIRST(&pool->list)) != NULL) {
841 1.1 itojun TAILQ_REMOVE(&pool->list, pa, entries);
842 1.1 itojun free(pa);
843 1.1 itojun }
844 1.1 itojun }
845 1.1 itojun
846 1.1 itojun int
847 1.1 itojun pfctl_add_altq(struct pfctl *pf, struct pf_altq *a)
848 1.1 itojun {
849 1.1 itojun fprintf(stderr, "altq rules not supported in authpf\n");
850 1.1 itojun return (1);
851 1.1 itojun }
852 1.1 itojun
853 1.1 itojun int
854 1.1 itojun pfctl_set_optimization(struct pfctl *pf, const char *opt)
855 1.1 itojun {
856 1.1 itojun fprintf(stderr, "set optimization not supported in authpf\n");
857 1.1 itojun return (1);
858 1.1 itojun }
859 1.1 itojun
860 1.1 itojun int
861 1.1 itojun pfctl_set_logif(struct pfctl *pf, char *ifname)
862 1.1 itojun {
863 1.1 itojun fprintf(stderr, "set loginterface not supported in authpf\n");
864 1.1 itojun return (1);
865 1.1 itojun }
866 1.1 itojun
867 1.1 itojun int
868 1.1 itojun pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
869 1.1 itojun {
870 1.1 itojun fprintf(stderr, "set hostid not supported in authpf\n");
871 1.1 itojun return (1);
872 1.1 itojun }
873 1.1 itojun
874 1.1 itojun int
875 1.1 itojun pfctl_set_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
876 1.1 itojun {
877 1.1 itojun fprintf(stderr, "set timeout not supported in authpf\n");
878 1.1 itojun return (1);
879 1.1 itojun }
880 1.1 itojun
881 1.1 itojun int
882 1.1 itojun pfctl_set_limit(struct pfctl *pf, const char *opt, unsigned int limit)
883 1.1 itojun {
884 1.1 itojun fprintf(stderr, "set limit not supported in authpf\n");
885 1.1 itojun return (1);
886 1.1 itojun }
887 1.1 itojun
888 1.1 itojun int
889 1.1 itojun pfctl_set_debug(struct pfctl *pf, char *d)
890 1.1 itojun {
891 1.1 itojun fprintf(stderr, "set debug not supported in authpf\n");
892 1.1 itojun return (1);
893 1.1 itojun }
894 1.1 itojun
895 1.1 itojun int
896 1.1 itojun pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
897 1.1 itojun const char *ruleset, struct pfr_buffer *ab, u_int32_t ticket)
898 1.1 itojun {
899 1.1 itojun fprintf(stderr, "table definitions not yet supported in authpf\n");
900 1.1 itojun return (1);
901 1.1 itojun }
902 1.1 itojun
903 1.1 itojun int
904 1.1 itojun pfctl_rules(int dev, char *filename, int opts, char *anchorname,
905 1.1 itojun char *rulesetname, struct pfr_buffer *t)
906 1.1 itojun {
907 1.1 itojun /* never called, no anchors inside anchors, but we need the stub */
908 1.1 itojun fprintf(stderr, "load anchor not supported from authpf\n");
909 1.1 itojun return (1);
910 1.1 itojun }
911 1.1 itojun
912 1.1 itojun void
913 1.1 itojun pfctl_print_title(char *title)
914 1.1 itojun {
915 1.1 itojun }
916