1 /* $OpenBSD: authpf.c,v 1.104 2007/02/24 17:35:08 beck Exp $ */
2 /* $DragonFly: src/usr.sbin/authpf/authpf.c,v 1.2 2004/12/18 22:48:02 swildner Exp $ */
5 * Copyright (C) 1998 - 2007 Bob Beck (beck@openbsd.org).
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
39 #include <net/pf/pfvar.h>
40 #include <arpa/inet.h>
44 #include <login_cap.h>
53 #include "pathnames.h"
55 #define __dead __dead2
57 static int read_config(FILE *);
58 static void print_message(const char *);
59 static int allowed_luser(const char *);
60 static int check_luser(const char *, const char *);
61 static int remove_stale_rulesets(void);
62 static int change_filter(int, const char *, const char *);
63 static int change_table(int, const char *);
64 static void authpf_kill_states(void);
66 int dev_fd; /* pf device */
67 char anchorname[PF_ANCHOR_NAME_SIZE] = "authpf";
68 char rulesetname[MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 2];
69 char tablename[PF_TABLE_NAME_SIZE] = "authpf_users";
72 char luser[MAXLOGNAME]; /* username */
73 char ipsrc[256]; /* ip as a string */
74 char pidfile[MAXPATHLEN]; /* we save pid in this file. */
76 struct timeval Tstart, Tend; /* start and end times of session */
78 volatile sig_atomic_t want_death;
79 static void need_death(int signo);
80 static __dead void do_death(int);
83 * User shell for authenticating gateways. Sole purpose is to allow
84 * a user to ssh to a gateway, and have the gateway modify packet
85 * filters to allow access, then remove access when the user finishes
86 * up. Meant to be used only from ssh(1) connections.
89 main(int argc __unused, char **argv __unused)
91 int lockcnt = 0, n, pidfd;
101 config = fopen(PATH_CONFFILE, "r");
102 if (config == NULL) {
103 syslog(LOG_ERR, "can not open %s (%m)", PATH_CONFFILE);
107 if ((cp = getenv("SSH_TTY")) == NULL) {
108 syslog(LOG_ERR, "non-interactive session connection for authpf");
112 if ((cp = getenv("SSH_CLIENT")) == NULL) {
113 syslog(LOG_ERR, "cannot determine connection source");
117 if (strlcpy(ipsrc, cp, sizeof(ipsrc)) >= sizeof(ipsrc)) {
118 syslog(LOG_ERR, "SSH_CLIENT variable too long");
121 cp = strchr(ipsrc, ' ');
123 syslog(LOG_ERR, "corrupt SSH_CLIENT variable %s", ipsrc);
127 if (inet_pton(AF_INET, ipsrc, &ina) != 1 &&
128 inet_pton(AF_INET6, ipsrc, &ina) != 1) {
130 "cannot determine IP from SSH_CLIENT %s", ipsrc);
133 /* open the pf device */
134 dev_fd = open(PATH_DEVFILE, O_RDWR);
136 syslog(LOG_ERR, "cannot open packet filter device (%m)");
143 syslog(LOG_ERR, "cannot find user for uid %u", uid);
147 if ((lc = login_getclass(pw->pw_class)) != NULL)
148 shell = login_getcapstr(lc, "shell", pw->pw_shell,
151 shell = pw->pw_shell;
155 if (strcmp(shell, PATH_AUTHPF_SHELL)) {
156 syslog(LOG_ERR, "wrong shell for user %s, uid %u",
157 pw->pw_name, pw->pw_uid);
158 if (shell != pw->pw_shell)
163 if (shell != pw->pw_shell)
167 * Paranoia, but this data _does_ come from outside authpf, and
168 * truncation would be bad.
170 if (strlcpy(luser, pw->pw_name, sizeof(luser)) >= sizeof(luser)) {
171 syslog(LOG_ERR, "username too long: %s", pw->pw_name);
175 if ((n = snprintf(rulesetname, sizeof(rulesetname), "%s(%ld)",
176 luser, (long)getpid())) < 0 || n >= (int)sizeof(rulesetname)) {
177 syslog(LOG_INFO, "%s(%ld) too large, ruleset name will be %ld",
178 luser, (long)getpid(), (long)getpid());
179 if ((n = snprintf(rulesetname, sizeof(rulesetname), "%ld",
180 (long)getpid())) < 0 || n >= (int)sizeof(rulesetname)) {
181 syslog(LOG_ERR, "pid too large for ruleset name");
187 /* Make our entry in /var/authpf as /var/authpf/ipaddr */
188 n = snprintf(pidfile, sizeof(pidfile), "%s/%s", PATH_PIDFILE, ipsrc);
189 if (n < 0 || (u_int)n >= sizeof(pidfile)) {
190 syslog(LOG_ERR, "path to pidfile too long");
195 * If someone else is already using this ip, then this person
196 * wants to switch users - so kill the old process and exit
199 * Note, we could print a message and tell them to log out, but the
200 * usual case of this is that someone has left themselves logged in,
201 * with the authenticated connection iconized and someone else walks
202 * up to use and automatically logs in before using. If this just
203 * gets rid of the old one silently, the new user never knows they
204 * could have used someone else's old authentication. If we
205 * tell them to log out before switching users it is an invitation
210 int save_errno, otherpid = -1;
211 char otherluser[MAXLOGNAME];
213 if ((pidfd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1 ||
214 (pidfp = fdopen(pidfd, "r+")) == NULL) {
217 syslog(LOG_ERR, "cannot open or create %s: %s", pidfile,
222 if (flock(fileno(pidfp), LOCK_EX|LOCK_NB) == 0)
226 /* Mark our pid, and username to our file. */
229 /* 31 == MAXLOGNAME - 1 */
230 if (fscanf(pidfp, "%d\n%31s\n", &otherpid, otherluser) != 2)
232 syslog(LOG_DEBUG, "tried to lock %s, in use by pid %d: %s",
233 pidfile, otherpid, strerror(save_errno));
237 "killing prior auth (pid %d) of %s by user %s",
238 otherpid, ipsrc, otherluser);
239 if (kill((pid_t) otherpid, SIGTERM) == -1) {
241 "could not kill process %d: (%m)",
247 * we try to kill the previous process and acquire the lock
248 * for 10 seconds, trying once a second. if we can't after
249 * 10 attempts we log an error and give up
251 if (++lockcnt > 10) {
252 syslog(LOG_ERR, "cannot kill previous authpf (pid %d)",
260 /* re-open, and try again. The previous authpf process
261 * we killed above should unlink the file and release
262 * it's lock, giving us a chance to get it now
268 /* whack the group list */
270 if (setgroups(1, &gid) == -1) {
271 syslog(LOG_INFO, "setgroups: %s", strerror(errno));
277 if (setresuid(uid, uid, uid) == -1) {
278 syslog(LOG_INFO, "setresuid: %s", strerror(errno));
281 openlog("authpf", LOG_PID | LOG_NDELAY, LOG_DAEMON);
283 if (!check_luser(PATH_BAN_DIR, luser) || !allowed_luser(luser)) {
284 syslog(LOG_INFO, "user %s prohibited", luser);
288 if (read_config(config)) {
289 syslog(LOG_ERR, "invalid config file %s", PATH_CONFFILE);
293 if (remove_stale_rulesets()) {
294 syslog(LOG_INFO, "error removing stale rulesets");
298 /* We appear to be making headway, so actually mark our pid */
300 fprintf(pidfp, "%ld\n%s\n", (long)getpid(), luser);
302 ftruncate(fileno(pidfp), ftell(pidfp));
304 if (change_filter(1, luser, ipsrc) == -1) {
305 printf("Unable to modify filters\r\n");
308 if (change_table(1, ipsrc) == -1) {
309 printf("Unable to modify table\r\n");
310 change_filter(0, luser, ipsrc);
314 signal(SIGTERM, need_death);
315 signal(SIGINT, need_death);
316 signal(SIGALRM, need_death);
317 signal(SIGPIPE, need_death);
318 signal(SIGHUP, need_death);
319 signal(SIGQUIT, need_death);
320 signal(SIGTSTP, need_death);
322 printf("\r\nHello %s. ", luser);
323 printf("You are authenticated from host \"%s\"\r\n", ipsrc);
324 setproctitle("%s@%s", luser, ipsrc);
325 print_message(PATH_MESSAGE);
335 printf("\r\n\r\nSorry, this service is currently unavailable due to ");
336 printf("technical difficulties\r\n\r\n");
337 print_message(PATH_PROBLEM);
338 printf("\r\nYour authentication process (pid %ld) was unable to run\n",
340 sleep(180); /* them lusers read reaaaaal slow */
346 * reads config file in PATH_CONFFILE to set optional behaviours up
356 char *pair[4], *cp, *tp;
359 if (fgets(buf, sizeof(buf), f) == NULL) {
365 if (buf[len - 1] != '\n' && !feof(f)) {
366 syslog(LOG_ERR, "line %d too long in %s", i,
372 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
375 if (!*cp || *cp == '#' || *cp == '\n')
378 for (ap = pair; ap < &pair[3] &&
379 (*ap = strsep(&cp, "=")) != NULL; ) {
386 tp = pair[1] + strlen(pair[1]);
387 while ((*tp == ' ' || *tp == '\t') && tp >= pair[1])
390 if (strcasecmp(pair[0], "anchor") == 0) {
391 if (!pair[1][0] || strlcpy(anchorname, pair[1],
392 sizeof(anchorname)) >= sizeof(anchorname))
395 if (strcasecmp(pair[0], "table") == 0) {
396 if (!pair[1][0] || strlcpy(tablename, pair[1],
397 sizeof(tablename)) >= sizeof(tablename))
400 } while (!feof(f) && !ferror(f));
406 syslog(LOG_ERR, "parse error, line %d of %s", i, PATH_CONFFILE);
412 * splatter a file to stdout - max line length of 1024,
413 * used for spitting message files at users to tell them
414 * they've been bad or we're unavailable.
417 print_message(const char *filename)
422 if ((f = fopen(filename, "r")) == NULL)
423 return; /* fail silently, we don't care if it isn't there */
426 if (fgets(buf, sizeof(buf), f) == NULL) {
431 } while (fputs(buf, stdout) != EOF && !feof(f));
437 * allowed_luser checks to see if user "luser" is allowed to
438 * use this gateway by virtue of being listed in an allowed
439 * users file, namely /etc/authpf/authpf.allow .
441 * If /etc/authpf/authpf.allow does not exist, then we assume that
442 * all users who are allowed in by sshd(8) are permitted to
443 * use this gateway. If /etc/authpf/authpf.allow does exist, then a
444 * user must be listed if the connection is to continue, else
445 * the session terminates in the same manner as being banned.
448 allowed_luser(const char *user)
455 if ((f = fopen(PATH_ALLOWFILE, "r")) == NULL) {
456 if (errno == ENOENT) {
458 * allowfile doesn't exist, thus this gateway
459 * isn't restricted to certain users...
465 * user may in fact be allowed, but we can't open
466 * the file even though it's there. probably a config
469 syslog(LOG_ERR, "cannot open allowed users file %s (%s)",
470 PATH_ALLOWFILE, strerror(errno));
474 * /etc/authpf/authpf.allow exists, thus we do a linear
475 * search to see if they are allowed.
476 * also, if username "*" exists, then this is a
477 * "public" gateway, such as it is, so let
481 while ((buf = fgetln(f, &len))) {
482 if (buf[len - 1] == '\n')
485 if ((lbuf = (char *)malloc(len + 1)) == NULL)
487 memcpy(lbuf, buf, len);
492 matched = strcmp(user, buf) == 0 || strcmp("*", buf) == 0;
500 return (1); /* matched an allowed username */
502 syslog(LOG_INFO, "denied access to %s: not listed in %s",
503 user, PATH_ALLOWFILE);
505 fputs("\n\nSorry, you are not allowed to use this facility!\n",
513 * check_luser checks to see if user "luser" has been banned
514 * from using us by virtue of having an file of the same name
515 * in the "luserdir" directory.
517 * If the user has been banned, we copy the contents of the file
518 * to the user's screen. (useful for telling the user what to
519 * do to get un-banned, or just to tell them they aren't
520 * going to be un-banned.)
523 check_luser(const char *userdir, const char *user)
527 char tmp[MAXPATHLEN];
529 n = snprintf(tmp, sizeof(tmp), "%s/%s", userdir, user);
530 if (n < 0 || (u_int)n >= sizeof(tmp)) {
531 syslog(LOG_ERR, "provided banned directory line too long (%s)",
535 if ((f = fopen(tmp, "r")) == NULL) {
536 if (errno == ENOENT) {
538 * file or dir doesn't exist, so therefore
539 * this luser isn't banned.. all is well
544 * user may in fact be banned, but we can't open the
545 * file even though it's there. probably a config
548 syslog(LOG_ERR, "cannot open banned file %s (%s)",
549 tmp, strerror(errno));
554 * user is banned - spit the file at them to
555 * tell what they can do and where they can go.
557 syslog(LOG_INFO, "denied access to %s: %s exists",
561 strlcpy(tmp, "\n\n-**- Sorry, you have been banned! -**-\n\n",
563 while (fputs(tmp, stdout) != EOF && !feof(f)) {
564 if (fgets(tmp, sizeof(tmp), f) == NULL) {
577 * Search for rulesets left by other authpf processes (either because they
578 * died ungracefully or were terminated) and remove them.
581 remove_stale_rulesets(void)
583 struct pfioc_ruleset prs;
586 memset(&prs, 0, sizeof(prs));
587 strlcpy(prs.path, anchorname, sizeof(prs.path));
588 if (ioctl(dev_fd, DIOCGETRULESETS, &prs)) {
602 if (ioctl(dev_fd, DIOCGETRULESET, &prs))
605 if ((t = strchr(prs.name, '(')) == NULL)
609 pid = strtoul(t, &s, 10);
610 if (!prs.name[0] || errno ||
611 (*s && (t == prs.name || *s != ')')))
613 if (kill(pid, 0) && errno != EPERM) {
615 struct pfioc_trans_e t_e[PF_RULESET_MAX+1];
616 struct pfioc_trans t_local;
618 bzero(&t, sizeof(t_local));
619 bzero(t_e, sizeof(t_e));
620 t_local.size = PF_RULESET_MAX+1;
621 t_local.esize = sizeof(t_e[0]);
623 for (i = 0; i < PF_RULESET_MAX+1; ++i) {
625 snprintf(t_e[i].anchor, sizeof(t_e[i].anchor),
626 "%s/%s", anchorname, prs.name);
636 * Add/remove filter entries for user "luser" from ip "ipsrc"
639 change_filter(int add, const char *user, const char *his_ipsrc)
641 const char *pargv[13] = {
642 "pfctl", "-p", "/dev/pf", "-q", "-a", "anchor/ruleset",
643 "-D", "user_ip=X", "-D", "user_id=X", "-f",
646 char *fdpath = NULL, *userstr = NULL, *ipstr = NULL;
647 char *rsn = NULL, *fn = NULL;
652 if (user == NULL || !user[0] || his_ipsrc == NULL || !his_ipsrc[0]) {
653 syslog(LOG_ERR, "invalid user/ipsrc");
657 if (asprintf(&rsn, "%s/%s", anchorname, rulesetname) == -1)
659 if (asprintf(&fdpath, "/dev/fd/%d", dev_fd) == -1)
661 if (asprintf(&ipstr, "user_ip=%s", his_ipsrc) == -1)
663 if (asprintf(&userstr, "user_id=%s", user) == -1)
669 if (asprintf(&fn, "%s/%s/authpf.rules", PATH_USER_DIR, user)
672 if (stat(fn, &sb) == -1) {
674 if ((fn = strdup(PATH_PFRULES)) == NULL)
683 pargv[11] = "/dev/null";
687 switch (pid = fork()) {
689 syslog(LOG_ERR, "fork failed");
692 /* revoke group privs before exec */
694 if (setregid(gid, gid) == -1) {
697 execvp(PATH_PFCTL, __DECONST(char *const *, pargv));
698 warn("exec of %s failed", PATH_PFCTL);
705 syslog(LOG_ERR, "pfctl exited abnormally");
710 gettimeofday(&Tstart, NULL);
711 syslog(LOG_INFO, "allowing %s, user %s", his_ipsrc, user);
713 gettimeofday(&Tend, NULL);
714 syslog(LOG_INFO, "removed %s, user %s - duration %ld seconds",
715 his_ipsrc, user, Tend.tv_sec - Tstart.tv_sec);
719 syslog(LOG_ERR, "malloc failed");
730 * Add/remove this IP from the "authpf_users" table.
733 change_table(int add, const char *his_ipsrc)
735 struct pfioc_table io;
736 struct pfr_addr addr;
738 bzero(&io, sizeof(io));
739 strlcpy(io.pfrio_table.pfrt_name, tablename,
740 sizeof(io.pfrio_table.pfrt_name));
741 io.pfrio_buffer = &addr;
742 io.pfrio_esize = sizeof(addr);
745 bzero(&addr, sizeof(addr));
746 if (his_ipsrc == NULL || !his_ipsrc[0])
748 if (inet_pton(AF_INET, his_ipsrc, &addr.pfra_ip4addr) == 1) {
749 addr.pfra_af = AF_INET;
751 } else if (inet_pton(AF_INET6, his_ipsrc, &addr.pfra_ip6addr) == 1) {
752 addr.pfra_af = AF_INET6;
755 syslog(LOG_ERR, "invalid his_ipsrc");
759 if (ioctl(dev_fd, add ? DIOCRADDADDRS : DIOCRDELADDRS, &io) &&
761 syslog(LOG_ERR, "cannot %s %s from table %s: %s",
762 add ? "add" : "remove", his_ipsrc, tablename,
770 * This is to kill off states that would otherwise be left behind stateful
771 * rules. This means we don't need to allow in more traffic than we really
772 * want to, since we don't have to worry about any luser sessions lasting
773 * longer than their ssh session. This function is based on
774 * pfctl_kill_states from pfctl.
777 authpf_kill_states(void)
779 struct pfioc_state_kill psk;
780 struct pf_addr target;
782 memset(&psk, 0, sizeof(psk));
783 memset(&target, 0, sizeof(target));
785 if (inet_pton(AF_INET, ipsrc, &target.v4) == 1)
786 psk.psk_af = AF_INET;
787 else if (inet_pton(AF_INET6, ipsrc, &target.v6) == 1)
788 psk.psk_af = AF_INET6;
790 syslog(LOG_ERR, "inet_pton(%s) failed", ipsrc);
794 /* Kill all states from ipsrc */
795 memcpy(&psk.psk_src.addr.v.a.addr, &target,
796 sizeof(psk.psk_src.addr.v.a.addr));
797 memset(&psk.psk_src.addr.v.a.mask, 0xff,
798 sizeof(psk.psk_src.addr.v.a.mask));
799 if (ioctl(dev_fd, DIOCKILLSTATES, &psk))
800 syslog(LOG_ERR, "DIOCKILLSTATES failed (%m)");
802 /* Kill all states to ipsrc */
803 memset(&psk.psk_src, 0, sizeof(psk.psk_src));
804 memcpy(&psk.psk_dst.addr.v.a.addr, &target,
805 sizeof(psk.psk_dst.addr.v.a.addr));
806 memset(&psk.psk_dst.addr.v.a.mask, 0xff,
807 sizeof(psk.psk_dst.addr.v.a.mask));
808 if (ioctl(dev_fd, DIOCKILLSTATES, &psk))
809 syslog(LOG_ERR, "DIOCKILLSTATES failed (%m)");
812 /* signal handler that makes us go away properly */
814 need_death(int signo __unused)
820 * function that removes our stuff when we go away.
828 change_filter(0, luser, ipsrc);
829 change_table(0, ipsrc);
830 authpf_kill_states();
831 remove_stale_rulesets();
833 if (pidfile[0] && (pidfp != NULL))
834 if (unlink(pidfile) == -1)
835 syslog(LOG_ERR, "cannot unlink %s (%m)", pidfile);