2 * auth.c - PPP authentication and phase control.
4 * Copyright (c) 1993 The Australian National University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the Australian National University. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * Copyright (c) 1989 Carnegie Mellon University.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that the above copyright notice and this paragraph are
24 * duplicated in all such forms and that any documentation,
25 * advertising materials, and other materials related to such
26 * distribution and use acknowledge that the software was developed
27 * by Carnegie Mellon University. The name of the
28 * University may not be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
32 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
36 static char rcsid[] = "$Id: auth.c,v 1.55 1999/07/23 06:55:05 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #if defined(_PATH_LASTLOG) && defined(_linux_)
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
60 #include <security/pam_appl.h>
66 #define PW_PPP PW_LOGIN
79 #include "pathnames.h"
81 /* Bits in scan_authfile return value */
82 #define NONWILD_SERVER 1
83 #define NONWILD_CLIENT 2
85 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
87 /* The name by which the peer authenticated itself to us. */
88 char peer_authname[MAXNAMELEN];
90 /* Records which authentication operations haven't completed yet. */
91 static int auth_pending[NUM_PPP];
93 /* Set if we have successfully called plogin() */
96 /* List of addresses which the peer may use. */
97 static struct permitted_ip *addresses[NUM_PPP];
99 /* Extra options to apply, from the secrets file entry for the peer. */
100 static struct wordlist *extra_options;
102 /* Number of network protocols which we have opened. */
103 static int num_np_open;
105 /* Number of network protocols which have come up. */
106 static int num_np_up;
108 /* Set if we got the contents of passwd[] from the pap-secrets file. */
109 static int passwd_from_file;
112 * This is used to ensure that we don't start an auth-up/down
113 * script while one is already running.
120 static enum script_state auth_state = s_down;
121 static enum script_state auth_script_state = s_down;
122 static pid_t auth_script_pid = 0;
127 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
128 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
129 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
130 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
131 bool usehostname = 0; /* Use hostname for our_name */
132 bool auth_required = 0; /* Always require authentication from peer */
133 bool allow_any_ip = 0; /* Allow peer to use any IP address */
134 bool explicit_remote = 0; /* User specified explicit remote name */
135 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
137 /* Bits in auth_pending[] */
138 #define PAP_WITHPEER 1
140 #define CHAP_WITHPEER 4
143 extern char *crypt __P((const char *, const char *));
145 /* Prototypes for procedures local to this file. */
147 static void network_phase __P((int));
148 static void check_idle __P((void *));
149 static void connect_time_expired __P((void *));
150 static int plogin __P((char *, char *, char **, int *));
151 static void plogout __P((void));
152 static int null_login __P((int));
153 static int get_pap_passwd __P((char *));
154 static int have_pap_secret __P((int *));
155 static int have_chap_secret __P((char *, char *, int, int *));
156 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
157 static int scan_authfile __P((FILE *, char *, char *, char *,
158 struct wordlist **, char *));
159 static void free_wordlist __P((struct wordlist *));
160 static void auth_script __P((char *));
161 static void auth_script_done __P((void *));
162 static void set_allowed_addrs __P((int, struct wordlist *));
163 static int some_ip_ok __P((struct wordlist *));
164 static int setupapfile __P((char **));
165 static int privgroup __P((char **));
166 static void check_access __P((FILE *, char *));
169 * Authentication-related options.
171 option_t auth_options[] = {
172 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
173 "Require PAP authentication from peer", 1, &auth_required },
174 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
175 "Require PAP authentication from peer", 1, &auth_required },
176 { "refuse-pap", o_bool, &refuse_pap,
177 "Don't agree to auth to peer with PAP", 1 },
178 { "-pap", o_bool, &refuse_pap,
179 "Don't allow PAP authentication with peer", 1 },
180 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
181 "Require CHAP authentication from peer", 1, &auth_required },
182 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
183 "Require CHAP authentication from peer", 1, &auth_required },
184 { "refuse-chap", o_bool, &refuse_chap,
185 "Don't agree to auth to peer with CHAP", 1 },
186 { "-chap", o_bool, &refuse_chap,
187 "Don't allow CHAP authentication with peer", 1 },
188 { "name", o_string, our_name,
189 "Set local name for authentication",
190 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
191 { "user", o_string, user,
192 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
193 { "usehostname", o_bool, &usehostname,
194 "Must use hostname for authentication", 1 },
195 { "remotename", o_string, remote_name,
196 "Set remote name for authentication", OPT_STATIC,
197 &explicit_remote, MAXNAMELEN },
198 { "auth", o_bool, &auth_required,
199 "Require authentication from peer", 1 },
200 { "noauth", o_bool, &auth_required,
201 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
202 { "login", o_bool, &uselogin,
203 "Use system password database for PAP", 1 },
204 { "papcrypt", o_bool, &cryptpap,
205 "PAP passwords are encrypted", 1 },
206 { "+ua", o_special, setupapfile,
207 "Get PAP user and password from file" },
208 { "privgroup", o_special, privgroup,
209 "Allow group members to use privileged options", OPT_PRIV },
214 * setupapfile - specifies UPAP info for authenticating with peer.
223 lcp_allowoptions[0].neg_upap = 1;
225 /* open user info file */
227 ufile = fopen(*argv, "r");
230 option_error("unable to open user login data file %s", *argv);
233 check_access(ufile, *argv);
236 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
237 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
238 option_error("unable to read user login data file %s", *argv);
243 /* get rid of newlines */
245 if (l > 0 && user[l-1] == '\n')
248 if (l > 0 && passwd[l-1] == '\n')
256 * privgroup - allow members of the group to have privileged access.
267 option_error("group %s is unknown", *argv);
270 for (i = 0; i < ngroups; ++i) {
271 if (groups[i] == g->gr_gid) {
281 * An Open on LCP has requested a change from Dead to Establish phase.
282 * Do what's necessary to bring the physical layer up.
291 * LCP has terminated the link; go to the Dead phase and take the
292 * physical layer down.
295 link_terminated(unit)
298 if (phase == PHASE_DEAD)
303 notice("Connection terminated.");
307 * LCP has gone down; it will either die or try to re-establish.
314 struct protent *protp;
317 if (auth_script_state == s_up && auth_script_pid == 0) {
318 auth_script_state = s_down;
319 auth_script(_PATH_AUTHDOWN);
321 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
322 if (!protp->enabled_flag)
324 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
325 (*protp->lowerdown)(unit);
326 if (protp->protocol < 0xC000 && protp->close != NULL)
327 (*protp->close)(unit, "LCP down");
331 if (phase != PHASE_DEAD)
332 phase = PHASE_TERMINATE;
336 * The link is established.
337 * Proceed to the Dead, Authenticate or Network phase as appropriate.
340 link_established(unit)
344 lcp_options *wo = &lcp_wantoptions[unit];
345 lcp_options *go = &lcp_gotoptions[unit];
346 lcp_options *ho = &lcp_hisoptions[unit];
348 struct protent *protp;
351 * Tell higher-level protocols that LCP is up.
353 for (i = 0; (protp = protocols[i]) != NULL; ++i)
354 if (protp->protocol != PPP_LCP && protp->enabled_flag
355 && protp->lowerup != NULL)
356 (*protp->lowerup)(unit);
358 if (auth_required && !(go->neg_chap || go->neg_upap)) {
360 * We wanted the peer to authenticate itself, and it refused:
361 * treat it as though it authenticated with PAP using a username
362 * of "" and a password of "". If that's not OK, boot it out.
364 if (!wo->neg_upap || !null_login(unit)) {
365 warn("peer refused to authenticate: terminating link");
366 lcp_close(unit, "peer refused to authenticate");
367 status = EXIT_PEER_AUTH_FAILED;
372 phase = PHASE_AUTHENTICATE;
375 ChapAuthPeer(unit, our_name, go->chap_mdtype);
377 } else if (go->neg_upap) {
382 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
383 auth |= CHAP_WITHPEER;
384 } else if (ho->neg_upap) {
385 if (passwd[0] == 0) {
386 passwd_from_file = 1;
387 if (!get_pap_passwd(passwd))
388 error("No secret found for PAP login");
390 upap_authwithpeer(unit, user, passwd);
391 auth |= PAP_WITHPEER;
393 auth_pending[unit] = auth;
400 * Proceed to the network phase.
406 lcp_options *go = &lcp_gotoptions[unit];
409 * If the peer had to authenticate, run the auth-up script now.
411 if (go->neg_chap || go->neg_upap) {
413 if (auth_script_state == s_down && auth_script_pid == 0) {
414 auth_script_state = s_up;
415 auth_script(_PATH_AUTHUP);
421 * If we negotiated callback, do it now.
424 phase = PHASE_CALLBACK;
425 (*cbcp_protent.open)(unit);
431 * Process extra options from the secrets file
434 options_from_list(extra_options, 1);
435 free_wordlist(extra_options);
445 struct protent *protp;
447 phase = PHASE_NETWORK;
450 set_filters(&pass_filter, &active_filter);
452 for (i = 0; (protp = protocols[i]) != NULL; ++i)
453 if (protp->protocol < 0xC000 && protp->enabled_flag
454 && protp->open != NULL) {
456 if (protp->protocol != PPP_CCP)
460 if (num_np_open == 0)
462 lcp_close(0, "No network protocols running");
466 * The peer has failed to authenticate himself using `protocol'.
469 auth_peer_fail(unit, protocol)
473 * Authentication failure: take the link down
475 lcp_close(unit, "Authentication failed");
476 status = EXIT_PEER_AUTH_FAILED;
480 * The peer has been successfully authenticated using `protocol'.
483 auth_peer_success(unit, protocol, name, namelen)
498 warn("auth_peer_success: unknown protocol %x", protocol);
503 * Save the authenticated name of the peer for later.
505 if (namelen > sizeof(peer_authname) - 1)
506 namelen = sizeof(peer_authname) - 1;
507 BCOPY(name, peer_authname, namelen);
508 peer_authname[namelen] = 0;
509 script_setenv("PEERNAME", peer_authname);
512 * If there is no more authentication still to be done,
513 * proceed to the network (or callback) phase.
515 if ((auth_pending[unit] &= ~bit) == 0)
520 * We have failed to authenticate ourselves to the peer using `protocol'.
523 auth_withpeer_fail(unit, protocol)
526 if (passwd_from_file)
527 BZERO(passwd, MAXSECRETLEN);
529 * We've failed to authenticate ourselves to our peer.
530 * He'll probably take the link down, and there's not much
531 * we can do except wait for that.
536 * We have successfully authenticated ourselves with the peer using `protocol'.
539 auth_withpeer_success(unit, protocol)
549 if (passwd_from_file)
550 BZERO(passwd, MAXSECRETLEN);
554 warn("auth_withpeer_success: unknown protocol %x", protocol);
559 * If there is no more authentication still being done,
560 * proceed to the network (or callback) phase.
562 if ((auth_pending[unit] &= ~bit) == 0)
568 * np_up - a network protocol has come up.
574 if (num_np_up == 0) {
576 * At this point we consider that the link has come up successfully.
580 if (idle_time_limit > 0)
581 TIMEOUT(check_idle, NULL, idle_time_limit);
584 * Set a timeout to close the connection once the maximum
585 * connect time has expired.
588 TIMEOUT(connect_time_expired, 0, maxconnect);
591 * Detach now, if the updetach option was given.
593 if (updetach && !nodetach)
600 * np_down - a network protocol has gone down.
606 if (--num_np_up == 0 && idle_time_limit > 0) {
607 UNTIMEOUT(check_idle, NULL);
612 * np_finished - a network protocol has finished using the link.
615 np_finished(unit, proto)
618 if (--num_np_open <= 0) {
619 /* no further use for the link: shut up shop. */
620 lcp_close(0, "No network protocols running");
625 * check_idle - check whether the link has been idle for long
626 * enough that we can shut it down.
632 struct ppp_idle idle;
635 if (!get_idle_time(0, &idle))
637 itime = MIN(idle.xmit_idle, idle.recv_idle);
638 if (itime >= idle_time_limit) {
639 /* link is idle: shut it down. */
640 notice("Terminating connection due to lack of activity.");
641 lcp_close(0, "Link inactive");
643 status = EXIT_IDLE_TIMEOUT;
645 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
650 * connect_time_expired - log a message and close the connection.
653 connect_time_expired(arg)
656 info("Connect time expired");
657 lcp_close(0, "Connect time expired"); /* Close connection */
658 status = EXIT_CONNECT_TIME;
662 * auth_check_options - called to check authentication options.
667 lcp_options *wo = &lcp_wantoptions[0];
671 /* Default our_name to hostname, and user to our_name */
672 if (our_name[0] == 0 || usehostname)
673 strlcpy(our_name, hostname, sizeof(our_name));
675 strlcpy(user, our_name, sizeof(user));
678 * If we have a default route, require the peer to authenticate
679 * unless the noauth option was given.
681 if (!auth_required && !allow_any_ip && have_route_to(0))
684 /* If authentication is required, ask peer for CHAP or PAP. */
686 if (!wo->neg_chap && !wo->neg_upap) {
696 * Check whether we have appropriate secrets to use
697 * to authenticate the peer.
700 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
701 if (!can_auth && wo->neg_chap) {
702 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
703 our_name, 1, &lacks_ip);
706 if (auth_required && !can_auth) {
709 "The remote system (%s) is required to authenticate itself but I",
713 "The remote system is required to authenticate itself but I");
717 "couldn't find any suitable secret (password) for it to use to do so.");
720 "couldn't find any secret (password) which would let it use an IP address.");
727 * auth_reset - called when LCP is starting negotiations to recheck
728 * authentication options, i.e. whether we have appropriate secrets
729 * to use for authenticating ourselves and/or the peer.
735 lcp_options *go = &lcp_gotoptions[unit];
736 lcp_options *ao = &lcp_allowoptions[0];
738 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
739 ao->neg_chap = !refuse_chap
740 && have_chap_secret(user, (explicit_remote? remote_name: NULL),
743 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
746 if (!have_chap_secret((explicit_remote? remote_name: NULL),
754 * check_passwd - Check the user name and passwd against the PAP secrets
755 * file. If requested, also check against the system password database,
756 * and login the user if OK.
759 * UPAP_AUTHNAK: Authentication failed.
760 * UPAP_AUTHACK: Authentication succeeded.
761 * In either case, msg points to an appropriate message.
764 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
776 struct wordlist *addrs;
777 char passwd[256], user[256];
778 char secret[MAXWORDLEN];
779 static int attempts = 0;
782 * Make copies of apasswd and auser, then null-terminate them.
784 BCOPY(apasswd, passwd, passwdlen);
785 passwd[passwdlen] = '\0';
786 BCOPY(auser, user, userlen);
787 user[userlen] = '\0';
791 * Open the file of pap secrets and scan for a suitable secret
792 * for authenticating this user.
794 filename = _PATH_UPAPFILE;
797 f = fopen(filename, "r");
799 error("Can't open PAP password file %s: %m", filename);
803 check_access(f, filename);
804 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
805 || (!uselogin && secret[0] != 0
806 && (cryptpap || strcmp(passwd, secret) != 0)
807 && strcmp(crypt(passwd, secret), secret) != 0)) {
808 warn("PAP authentication failure for %s", user);
814 if (uselogin && ret == UPAP_AUTHACK) {
815 ret = plogin(user, passwd, msg, msglen);
816 if (ret == UPAP_AUTHNAK) {
817 warn("PAP login failure for %s", user);
821 if (ret == UPAP_AUTHNAK) {
822 if (*msg == (char *) 0)
823 *msg = "Login incorrect";
824 *msglen = strlen(*msg);
826 * XXX can we ever get here more than once??
827 * Frustrate passwd stealer programs.
828 * Allow 10 tries, but start backing off after 3 (stolen from login).
829 * On 10'th, drop the connection.
831 if (attempts++ >= 10) {
832 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
833 lcp_close(unit, "login failed");
836 sleep((u_int) (attempts - 3) * 5);
838 free_wordlist(addrs);
841 attempts = 0; /* Reset count */
842 if (*msg == (char *) 0)
844 *msglen = strlen(*msg);
845 set_allowed_addrs(unit, addrs);
848 BZERO(passwd, sizeof(passwd));
849 BZERO(secret, sizeof(secret));
855 * This function is needed for PAM.
859 /* Static variables used to communicate between the conversation function
860 * and the server_login function
862 static char *PAM_username;
863 static char *PAM_password;
864 static int PAM_error = 0;
865 static pam_handle_t *pamh = NULL;
867 /* PAM conversation function
868 * Here we assume (for now, at least) that echo on means login name, and
869 * echo off means password.
872 static int PAM_conv (int num_msg, const struct pam_message **msg,
873 struct pam_response **resp, void *appdata_ptr)
876 struct pam_response *reply = NULL;
878 #define COPY_STRING(s) (s) ? strdup(s) : NULL
880 reply = malloc(sizeof(struct pam_response) * num_msg);
881 if (!reply) return PAM_CONV_ERR;
883 for (replies = 0; replies < num_msg; replies++) {
884 switch (msg[replies]->msg_style) {
885 case PAM_PROMPT_ECHO_ON:
886 reply[replies].resp_retcode = PAM_SUCCESS;
887 reply[replies].resp = COPY_STRING(PAM_username);
890 case PAM_PROMPT_ECHO_OFF:
891 reply[replies].resp_retcode = PAM_SUCCESS;
892 reply[replies].resp = COPY_STRING(PAM_password);
898 /* ignore it, but pam still wants a NULL response... */
899 reply[replies].resp_retcode = PAM_SUCCESS;
900 reply[replies].resp = NULL;
903 /* Must be an error of some sort... */
913 static struct pam_conv PAM_conversation = {
920 * plogin - Check the user name and password against the system
921 * password database, and login the user if OK.
924 * UPAP_AUTHNAK: Login failed.
925 * UPAP_AUTHACK: Login succeeded.
926 * In either case, msg points to an appropriate message.
930 plogin(user, passwd, msg, msglen)
941 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
942 if (pam_error != PAM_SUCCESS) {
943 *msg = (char *) pam_strerror (pamh, pam_error);
948 * Define the fields for the credential validation
952 PAM_password = passwd;
954 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
959 pam_error = pam_authenticate (pamh, PAM_SILENT);
960 if (pam_error == PAM_SUCCESS && !PAM_error) {
961 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
962 if (pam_error == PAM_SUCCESS)
963 pam_open_session (pamh, PAM_SILENT);
966 *msg = (char *) pam_strerror (pamh, pam_error);
971 reopen_log(); /* apparently the PAM stuff does closelog() */
974 if (pam_error != PAM_SUCCESS)
976 #else /* #ifdef USE_PAM */
979 * Use the non-PAM methods directly
984 struct spwd *getspnam();
986 struct passwd *pw = getpwnam(user);
990 return (UPAP_AUTHNAK);
993 spwd = getspnam(user);
996 /* check the age of the password entry */
997 long now = time(NULL) / 86400L;
999 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1000 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1001 && spwd->sp_lstchg >= 0
1002 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1003 warn("Password for %s has expired", user);
1004 return (UPAP_AUTHNAK);
1006 pw->pw_passwd = spwd->sp_pwdp;
1011 * If no passwd, don't let them login.
1013 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1014 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1015 return (UPAP_AUTHNAK);
1017 #endif /* #ifdef USE_PAM */
1020 * Write a wtmp entry for this user.
1024 if (strncmp(tty, "/dev/", 5) == 0)
1026 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1028 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1029 if (pw != (struct passwd *)NULL) {
1033 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1034 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1035 memset((void *)&ll, 0, sizeof(ll));
1036 (void)time(&ll.ll_time);
1037 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1038 (void)write(fd, (char *)&ll, sizeof(ll));
1042 #endif /* _PATH_LASTLOG and not USE_PAM */
1044 info("user %s logged in", user);
1047 return (UPAP_AUTHACK);
1051 * plogout - Logout the user.
1060 pam_error = pam_close_session (pamh, PAM_SILENT);
1061 pam_end (pamh, pam_error);
1064 /* Apparently the pam stuff does closelog(). */
1066 #else /* ! USE_PAM */
1070 if (strncmp(tty, "/dev/", 5) == 0)
1072 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1073 #endif /* ! USE_PAM */
1079 * null_login - Check if a username of "" and a password of "" are
1080 * acceptable, and iff so, set the list of acceptable IP addresses
1090 struct wordlist *addrs;
1091 char secret[MAXWORDLEN];
1094 * Open the file of pap secrets and scan for a suitable secret.
1095 * We don't accept a wildcard client.
1097 filename = _PATH_UPAPFILE;
1099 f = fopen(filename, "r");
1102 check_access(f, filename);
1104 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
1105 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1106 BZERO(secret, sizeof(secret));
1109 set_allowed_addrs(unit, addrs);
1111 free_wordlist(addrs);
1119 * get_pap_passwd - get a password for authenticating ourselves with
1120 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1122 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1125 get_pap_passwd(passwd)
1131 struct wordlist *addrs;
1132 char secret[MAXWORDLEN];
1134 filename = _PATH_UPAPFILE;
1136 f = fopen(filename, "r");
1139 check_access(f, filename);
1140 ret = scan_authfile(f, user,
1141 (remote_name[0]? remote_name: NULL),
1142 secret, NULL, filename);
1147 strlcpy(passwd, secret, MAXSECRETLEN);
1148 BZERO(secret, sizeof(secret));
1154 * have_pap_secret - check whether we have a PAP file with any
1155 * secrets that we could possibly use for authenticating the peer.
1158 have_pap_secret(lacks_ipp)
1164 struct wordlist *addrs;
1166 filename = _PATH_UPAPFILE;
1167 f = fopen(filename, "r");
1171 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1172 NULL, &addrs, filename);
1174 if (ret >= 0 && !some_ip_ok(addrs)) {
1180 free_wordlist(addrs);
1187 * have_chap_secret - check whether we have a CHAP file with a
1188 * secret that we could possibly use for authenticating `client'
1189 * on `server'. Either can be the null string, meaning we don't
1190 * know the identity yet.
1193 have_chap_secret(client, server, need_ip, lacks_ipp)
1202 struct wordlist *addrs;
1204 filename = _PATH_CHAPFILE;
1205 f = fopen(filename, "r");
1209 if (client != NULL && client[0] == 0)
1211 else if (server != NULL && server[0] == 0)
1214 ret = scan_authfile(f, client, server, NULL, &addrs, filename);
1216 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1222 free_wordlist(addrs);
1229 * get_secret - open the CHAP secret file and return the secret
1230 * for authenticating the given client on the given server.
1231 * (We could be either client or server).
1234 get_secret(unit, client, server, secret, secret_len, save_addrs)
1245 struct wordlist *addrs;
1246 char secbuf[MAXWORDLEN];
1248 filename = _PATH_CHAPFILE;
1252 f = fopen(filename, "r");
1254 error("Can't open chap secret file %s: %m", filename);
1257 check_access(f, filename);
1259 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
1265 set_allowed_addrs(unit, addrs);
1267 len = strlen(secbuf);
1268 if (len > MAXSECRETLEN) {
1269 error("Secret for %s on %s is too long", client, server);
1272 BCOPY(secbuf, secret, len);
1273 BZERO(secbuf, sizeof(secbuf));
1280 * set_allowed_addrs() - set the list of allowed addresses.
1281 * Also looks for `--' indicating options to apply for this peer
1282 * and leaves the following words in extra_options.
1285 set_allowed_addrs(unit, addrs)
1287 struct wordlist *addrs;
1290 struct wordlist *ap, **pap;
1291 struct permitted_ip *ip;
1292 char *ptr_word, *ptr_mask;
1295 u_int32_t a, mask, ah;
1296 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1297 u_int32_t suggested_ip = 0;
1299 if (addresses[unit] != NULL)
1300 free(addresses[unit]);
1301 addresses[unit] = NULL;
1302 if (extra_options != NULL)
1303 free_wordlist(extra_options);
1304 extra_options = NULL;
1307 * Count the number of IP addresses given, and chop off
1308 * any extra options for this peer.
1310 for (n = 0, pap = &addrs; (ap = *pap) != NULL; pap = &ap->next, ++n) {
1311 if (strcmp(ap->word, "--") == 0) {
1312 /* rest are options */
1314 extra_options = ap->next;
1321 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1326 for (ap = addrs; ap != NULL; ap = ap->next) {
1327 /* "-" means no addresses authorized, "*" means any address allowed */
1328 ptr_word = ap->word;
1329 if (strcmp(ptr_word, "-") == 0)
1331 if (strcmp(ptr_word, "*") == 0) {
1333 ip[n].base = ip[n].mask = 0;
1339 if (*ptr_word == '!') {
1344 mask = ~ (u_int32_t) 0;
1345 ptr_mask = strchr (ptr_word, '/');
1346 if (ptr_mask != NULL) {
1349 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1350 if (bit_count <= 0 || bit_count > 32) {
1351 warn("invalid address length %v in auth. address list",
1356 mask <<= 32 - bit_count;
1359 hp = gethostbyname(ptr_word);
1360 if (hp != NULL && hp->h_addrtype == AF_INET) {
1361 a = *(u_int32_t *)hp->h_addr;
1363 np = getnetbyname (ptr_word);
1364 if (np != NULL && np->n_addrtype == AF_INET) {
1365 a = htonl (*(u_int32_t *)np->n_net);
1366 if (ptr_mask == NULL) {
1367 /* calculate appropriate mask for net */
1370 mask = IN_CLASSA_NET;
1371 else if (IN_CLASSB(ah))
1372 mask = IN_CLASSB_NET;
1373 else if (IN_CLASSC(ah))
1374 mask = IN_CLASSC_NET;
1377 a = inet_addr (ptr_word);
1381 if (ptr_mask != NULL)
1384 if (a == (u_int32_t)-1L)
1385 warn("unknown host %s in auth. address list", ap->word);
1387 ip[n].mask = htonl(mask);
1388 ip[n].base = a & ip[n].mask;
1390 if (~mask == 0 && suggested_ip == 0)
1395 ip[n].permit = 0; /* make the last entry forbid all addresses */
1396 ip[n].base = 0; /* to terminate the list */
1399 addresses[unit] = ip;
1402 * If the address given for the peer isn't authorized, or if
1403 * the user hasn't given one, AND there is an authorized address
1404 * which is a single host, then use that if we find one.
1406 if (suggested_ip != 0
1407 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
1408 wo->hisaddr = suggested_ip;
1412 * auth_ip_addr - check whether the peer is authorized to use
1413 * a given IP address. Returns 1 if authorized, 0 otherwise.
1416 auth_ip_addr(unit, addr)
1420 if (addresses[unit] == NULL) {
1422 return 0; /* no addresses authorized */
1423 return allow_any_ip || !have_route_to(addr);
1425 return ip_addr_check(addr, addresses[unit]);
1429 ip_addr_check(addr, addrs)
1431 struct permitted_ip *addrs;
1433 /* don't allow loopback or multicast address */
1434 if (bad_ip_adrs(addr))
1438 if ((addr & addrs->mask) == addrs->base)
1439 return addrs->permit;
1443 * bad_ip_adrs - return 1 if the IP address is one we don't want
1444 * to use, such as an address in the loopback net or a multicast address.
1445 * addr is in network byte order.
1452 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1453 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1457 * some_ip_ok - check a wordlist to see if it authorizes any
1462 struct wordlist *addrs;
1464 for (; addrs != 0; addrs = addrs->next) {
1465 if (strcmp(addrs->word, "-") == 0)
1467 if (addrs->word[0] != '!')
1468 return 1; /* some IP address is allowed */
1474 * check_access - complain if a secret file has too-liberal permissions.
1477 check_access(f, filename)
1483 if (fstat(fileno(f), &sbuf) < 0) {
1484 warn("cannot stat secret file %s: %m", filename);
1485 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1486 warn("Warning - secret file %s has world and/or group access",
1493 * scan_authfile - Scan an authorization file for a secret suitable
1494 * for authenticating `client' on `server'. The return value is -1
1495 * if no secret is found, otherwise >= 0. The return value has
1496 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1497 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1498 * Any following words on the line (i.e. address authorization
1499 * info) are placed in a wordlist and returned in *addrs.
1500 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1503 scan_authfile(f, client, server, secret, addrs, filename)
1508 struct wordlist **addrs;
1512 int got_flag, best_flag;
1514 struct wordlist *ap, *addr_list, *alist, *alast;
1515 char word[MAXWORDLEN];
1516 char atfile[MAXWORDLEN];
1517 char lsecret[MAXWORDLEN];
1522 if (!getword(f, word, &newline, filename))
1523 return -1; /* file is empty??? */
1528 * Skip until we find a word at the start of a line.
1530 while (!newline && getword(f, word, &newline, filename))
1533 break; /* got to end of file */
1536 * Got a client - check if it's a match or a wildcard.
1539 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1544 got_flag = NONWILD_CLIENT;
1547 * Now get a server and check if it matches.
1549 if (!getword(f, word, &newline, filename))
1553 if (!ISWILD(word)) {
1554 if (server != NULL && strcmp(word, server) != 0)
1556 got_flag |= NONWILD_SERVER;
1560 * Got some sort of a match - see if it's better than what
1563 if (got_flag <= best_flag)
1569 if (!getword(f, word, &newline, filename))
1575 * Special syntax: @filename means read secret from file.
1577 if (word[0] == '@') {
1578 strlcpy(atfile, word+1, sizeof(atfile));
1579 if ((sf = fopen(atfile, "r")) == NULL) {
1580 warn("can't open indirect secret file %s", atfile);
1583 check_access(sf, atfile);
1584 if (!getword(sf, word, &xxx, atfile)) {
1585 warn("no secret in indirect secret file %s", atfile);
1592 strlcpy(lsecret, word, sizeof(lsecret));
1595 * Now read address authorization info and make a wordlist.
1597 alist = alast = NULL;
1599 if (!getword(f, word, &newline, filename) || newline)
1601 ap = (struct wordlist *) malloc(sizeof(struct wordlist));
1603 novm("authorized addresses");
1605 ap->word = strdup(word);
1606 if (ap->word == NULL)
1607 novm("authorized address");
1616 * This is the best so far; remember it.
1618 best_flag = got_flag;
1620 free_wordlist(addr_list);
1623 strlcpy(secret, lsecret, MAXWORDLEN);
1631 else if (addr_list != NULL)
1632 free_wordlist(addr_list);
1638 * free_wordlist - release memory allocated for a wordlist.
1642 struct wordlist *wp;
1644 struct wordlist *next;
1646 while (wp != NULL) {
1654 * auth_script_done - called when the auth-up or auth-down script
1658 auth_script_done(arg)
1661 auth_script_pid = 0;
1662 switch (auth_script_state) {
1664 if (auth_state == s_down) {
1665 auth_script_state = s_down;
1666 auth_script(_PATH_AUTHDOWN);
1670 if (auth_state == s_up) {
1671 auth_script_state = s_up;
1672 auth_script(_PATH_AUTHUP);
1679 * auth_script - execute a script with arguments
1680 * interface-name peer-name real-user tty speed
1692 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1693 user_name = pw->pw_name;
1695 slprintf(struid, sizeof(struid), "%d", getuid());
1698 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1702 argv[2] = peer_authname;
1703 argv[3] = user_name;
1708 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);