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