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.47 1999/03/19 01:17:51 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 /* Used for storing a sequence of words. Usually malloced. */
83 struct wordlist *next;
87 /* Bits in scan_authfile return value */
88 #define NONWILD_SERVER 1
89 #define NONWILD_CLIENT 2
91 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
93 /* The name by which the peer authenticated itself to us. */
94 char peer_authname[MAXNAMELEN];
96 /* Records which authentication operations haven't completed yet. */
97 static int auth_pending[NUM_PPP];
99 /* Set if we have successfully called plogin() */
100 static int logged_in;
102 /* List of addresses which the peer may use. */
103 static struct permitted_ip *addresses[NUM_PPP];
105 /* Number of network protocols which we have opened. */
106 static int num_np_open;
108 /* Number of network protocols which have come up. */
109 static int num_np_up;
111 /* Set if we got the contents of passwd[] from the pap-secrets file. */
112 static int passwd_from_file;
115 * This is used to ensure that we don't start an auth-up/down
116 * script while one is already running.
123 static enum script_state auth_state = s_down;
124 static enum script_state auth_script_state = s_down;
125 static pid_t auth_script_pid = 0;
130 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
131 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
132 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
133 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
134 bool usehostname = 0; /* Use hostname for our_name */
135 bool auth_required = 0; /* Always require authentication from peer */
136 bool allow_any_ip = 0; /* Allow peer to use any IP address */
137 bool explicit_remote = 0; /* User specified explicit remote name */
138 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
140 /* Bits in auth_pending[] */
141 #define PAP_WITHPEER 1
143 #define CHAP_WITHPEER 4
146 extern char *crypt __P((const char *, const char *));
148 /* Prototypes for procedures local to this file. */
150 static void network_phase __P((int));
151 static void check_idle __P((void *));
152 static void connect_time_expired __P((void *));
153 static int plogin __P((char *, char *, char **, int *));
154 static void plogout __P((void));
155 static int null_login __P((int));
156 static int get_pap_passwd __P((char *));
157 static int have_pap_secret __P((int *));
158 static int have_chap_secret __P((char *, char *, int, int *));
159 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
160 static int scan_authfile __P((FILE *, char *, char *, char *,
161 struct wordlist **, char *));
162 static void free_wordlist __P((struct wordlist *));
163 static void auth_script __P((char *));
164 static void auth_script_done __P((void *));
165 static void set_allowed_addrs __P((int, struct wordlist *));
166 static int some_ip_ok __P((struct wordlist *));
167 static int setupapfile __P((char **));
168 static int privgroup __P((char **));
169 static void check_access __P((FILE *, char *));
172 * Authentication-related options.
174 option_t auth_options[] = {
175 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
176 "Require PAP authentication from peer", 1, &auth_required },
177 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
178 "Require PAP authentication from peer", 1, &auth_required },
179 { "refuse-pap", o_bool, &refuse_pap,
180 "Don't agree to auth to peer with PAP", 1 },
181 { "-pap", o_bool, &refuse_pap,
182 "Don't allow PAP authentication with peer", 1 },
183 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
184 "Require CHAP authentication from peer", 1, &auth_required },
185 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
186 "Require CHAP authentication from peer", 1, &auth_required },
187 { "refuse-chap", o_bool, &refuse_chap,
188 "Don't agree to auth to peer with CHAP", 1 },
189 { "-chap", o_bool, &refuse_chap,
190 "Don't allow CHAP authentication with peer", 1 },
191 { "name", o_string, our_name,
192 "Set local name for authentication",
193 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
194 { "user", o_string, user,
195 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
196 { "usehostname", o_bool, &usehostname,
197 "Must use hostname for authentication", 1 },
198 { "remotename", o_string, remote_name,
199 "Set remote name for authentication", OPT_STATIC,
200 &explicit_remote, MAXNAMELEN },
201 { "auth", o_bool, &auth_required,
202 "Require authentication from peer", 1 },
203 { "noauth", o_bool, &auth_required,
204 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
205 { "login", o_bool, &uselogin,
206 "Use system password database for PAP", 1 },
207 { "papcrypt", o_bool, &cryptpap,
208 "PAP passwords are encrypted", 1 },
209 { "+ua", o_special, setupapfile,
210 "Get PAP user and password from file" },
211 { "privgroup", o_special, privgroup,
212 "Allow group members to use privileged options", OPT_PRIV },
217 * setupapfile - specifies UPAP info for authenticating with peer.
226 lcp_allowoptions[0].neg_upap = 1;
228 /* open user info file */
230 ufile = fopen(*argv, "r");
233 option_error("unable to open user login data file %s", *argv);
236 check_access(ufile, *argv);
239 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
240 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
241 option_error("unable to read user login data file %s", *argv);
246 /* get rid of newlines */
248 if (l > 0 && user[l-1] == '\n')
251 if (l > 0 && passwd[l-1] == '\n')
259 * privgroup - allow members of the group to have privileged access.
270 option_error("group %s is unknown", *argv);
273 for (i = 0; i < ngroups; ++i) {
274 if (groups[i] == g->gr_gid) {
284 * An Open on LCP has requested a change from Dead to Establish phase.
285 * Do what's necessary to bring the physical layer up.
294 * LCP has terminated the link; go to the Dead phase and take the
295 * physical layer down.
298 link_terminated(unit)
301 if (phase == PHASE_DEAD)
306 notice("Connection terminated.");
310 * LCP has gone down; it will either die or try to re-establish.
317 struct protent *protp;
320 if (auth_script_state == s_up && auth_script_pid == 0) {
321 auth_script_state = s_down;
322 auth_script(_PATH_AUTHDOWN);
324 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
325 if (!protp->enabled_flag)
327 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
328 (*protp->lowerdown)(unit);
329 if (protp->protocol < 0xC000 && protp->close != NULL)
330 (*protp->close)(unit, "LCP down");
334 if (phase != PHASE_DEAD)
335 phase = PHASE_TERMINATE;
339 * The link is established.
340 * Proceed to the Dead, Authenticate or Network phase as appropriate.
343 link_established(unit)
347 lcp_options *wo = &lcp_wantoptions[unit];
348 lcp_options *go = &lcp_gotoptions[unit];
349 lcp_options *ho = &lcp_hisoptions[unit];
351 struct protent *protp;
354 * Tell higher-level protocols that LCP is up.
356 for (i = 0; (protp = protocols[i]) != NULL; ++i)
357 if (protp->protocol != PPP_LCP && protp->enabled_flag
358 && protp->lowerup != NULL)
359 (*protp->lowerup)(unit);
361 if (auth_required && !(go->neg_chap || go->neg_upap)) {
363 * We wanted the peer to authenticate itself, and it refused:
364 * treat it as though it authenticated with PAP using a username
365 * of "" and a password of "". If that's not OK, boot it out.
367 if (!wo->neg_upap || !null_login(unit)) {
368 warn("peer refused to authenticate: terminating link");
369 lcp_close(unit, "peer refused to authenticate");
374 phase = PHASE_AUTHENTICATE;
377 ChapAuthPeer(unit, our_name, go->chap_mdtype);
379 } else if (go->neg_upap) {
384 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
385 auth |= CHAP_WITHPEER;
386 } else if (ho->neg_upap) {
387 if (passwd[0] == 0) {
388 passwd_from_file = 1;
389 if (!get_pap_passwd(passwd))
390 error("No secret found for PAP login");
392 upap_authwithpeer(unit, user, passwd);
393 auth |= PAP_WITHPEER;
395 auth_pending[unit] = auth;
402 * Proceed to the network phase.
409 struct protent *protp;
410 lcp_options *go = &lcp_gotoptions[unit];
413 * If the peer had to authenticate, run the auth-up script now.
415 if (go->neg_chap || go->neg_upap) {
417 if (auth_script_state == s_down && auth_script_pid == 0) {
418 auth_script_state = s_up;
419 auth_script(_PATH_AUTHUP);
425 * If we negotiated callback, do it now.
428 phase = PHASE_CALLBACK;
429 (*cbcp_protent.open)(unit);
434 phase = PHASE_NETWORK;
437 set_filters(&pass_filter, &active_filter);
439 for (i = 0; (protp = protocols[i]) != NULL; ++i)
440 if (protp->protocol < 0xC000 && protp->enabled_flag
441 && protp->open != NULL) {
442 (*protp->open)(unit);
443 if (protp->protocol != PPP_CCP)
447 if (num_np_open == 0)
449 lcp_close(0, "No network protocols running");
453 * The peer has failed to authenticate himself using `protocol'.
456 auth_peer_fail(unit, protocol)
460 * Authentication failure: take the link down
462 lcp_close(unit, "Authentication failed");
466 * The peer has been successfully authenticated using `protocol'.
469 auth_peer_success(unit, protocol, name, namelen)
484 warn("auth_peer_success: unknown protocol %x", protocol);
489 * Save the authenticated name of the peer for later.
491 if (namelen > sizeof(peer_authname) - 1)
492 namelen = sizeof(peer_authname) - 1;
493 BCOPY(name, peer_authname, namelen);
494 peer_authname[namelen] = 0;
495 script_setenv("PEERNAME", peer_authname);
498 * If there is no more authentication still to be done,
499 * proceed to the network (or callback) phase.
501 if ((auth_pending[unit] &= ~bit) == 0)
506 * We have failed to authenticate ourselves to the peer using `protocol'.
509 auth_withpeer_fail(unit, protocol)
512 if (passwd_from_file)
513 BZERO(passwd, MAXSECRETLEN);
515 * We've failed to authenticate ourselves to our peer.
516 * He'll probably take the link down, and there's not much
517 * we can do except wait for that.
522 * We have successfully authenticated ourselves with the peer using `protocol'.
525 auth_withpeer_success(unit, protocol)
535 if (passwd_from_file)
536 BZERO(passwd, MAXSECRETLEN);
540 warn("auth_withpeer_success: unknown protocol %x", protocol);
545 * If there is no more authentication still being done,
546 * proceed to the network (or callback) phase.
548 if ((auth_pending[unit] &= ~bit) == 0)
554 * np_up - a network protocol has come up.
560 if (num_np_up == 0) {
562 * At this point we consider that the link has come up successfully.
566 if (idle_time_limit > 0)
567 TIMEOUT(check_idle, NULL, idle_time_limit);
570 * Set a timeout to close the connection once the maximum
571 * connect time has expired.
574 TIMEOUT(connect_time_expired, 0, maxconnect);
577 * Detach now, if the updetach option was given.
579 if (updetach && !nodetach)
586 * np_down - a network protocol has gone down.
592 if (--num_np_up == 0 && idle_time_limit > 0) {
593 UNTIMEOUT(check_idle, NULL);
598 * np_finished - a network protocol has finished using the link.
601 np_finished(unit, proto)
604 if (--num_np_open <= 0) {
605 /* no further use for the link: shut up shop. */
606 lcp_close(0, "No network protocols running");
611 * check_idle - check whether the link has been idle for long
612 * enough that we can shut it down.
618 struct ppp_idle idle;
621 if (!get_idle_time(0, &idle))
623 itime = MIN(idle.xmit_idle, idle.recv_idle);
624 if (itime >= idle_time_limit) {
625 /* link is idle: shut it down. */
626 info("Terminating connection due to lack of activity.");
627 lcp_close(0, "Link inactive");
629 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
634 * connect_time_expired - log a message and close the connection.
637 connect_time_expired(arg)
640 info("Connect time expired");
641 lcp_close(0, "Connect time expired"); /* Close connection */
645 * auth_check_options - called to check authentication options.
650 lcp_options *wo = &lcp_wantoptions[0];
654 /* Default our_name to hostname, and user to our_name */
655 if (our_name[0] == 0 || usehostname)
656 strlcpy(our_name, hostname, sizeof(our_name));
658 strlcpy(user, our_name, sizeof(user));
661 * If we have a default route, require the peer to authenticate
662 * unless the noauth option was given.
664 if (!auth_required && !allow_any_ip && have_route_to(0))
667 /* If authentication is required, ask peer for CHAP or PAP. */
669 if (!wo->neg_chap && !wo->neg_upap) {
679 * Check whether we have appropriate secrets to use
680 * to authenticate the peer.
683 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
684 if (!can_auth && wo->neg_chap) {
685 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
686 our_name, 1, &lacks_ip);
689 if (auth_required && !can_auth) {
692 "The remote system (%s) is required to authenticate itself but I",
696 "The remote system is required to authenticate itself but I");
700 "couldn't find any suitable secret (password) for it to use to do so.");
703 "couldn't find any secret (password) which would let it use an IP address.");
710 * auth_reset - called when LCP is starting negotiations to recheck
711 * authentication options, i.e. whether we have appropriate secrets
712 * to use for authenticating ourselves and/or the peer.
718 lcp_options *go = &lcp_gotoptions[unit];
719 lcp_options *ao = &lcp_allowoptions[0];
721 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
722 ao->neg_chap = !refuse_chap
723 && have_chap_secret(user, (explicit_remote? remote_name: NULL),
726 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
729 if (!have_chap_secret((explicit_remote? remote_name: NULL),
737 * check_passwd - Check the user name and passwd against the PAP secrets
738 * file. If requested, also check against the system password database,
739 * and login the user if OK.
742 * UPAP_AUTHNAK: Authentication failed.
743 * UPAP_AUTHACK: Authentication succeeded.
744 * In either case, msg points to an appropriate message.
747 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
759 struct wordlist *addrs;
760 char passwd[256], user[256];
761 char secret[MAXWORDLEN];
762 static int attempts = 0;
765 * Make copies of apasswd and auser, then null-terminate them.
767 BCOPY(apasswd, passwd, passwdlen);
768 passwd[passwdlen] = '\0';
769 BCOPY(auser, user, userlen);
770 user[userlen] = '\0';
774 * Open the file of pap secrets and scan for a suitable secret
775 * for authenticating this user.
777 filename = _PATH_UPAPFILE;
780 f = fopen(filename, "r");
782 error("Can't open PAP password file %s: %m", filename);
786 check_access(f, filename);
787 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
788 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
789 && strcmp(crypt(passwd, secret), secret) != 0)) {
790 warn("PAP authentication failure for %s", user);
796 if (uselogin && ret == UPAP_AUTHACK) {
797 ret = plogin(user, passwd, msg, msglen);
798 if (ret == UPAP_AUTHNAK) {
799 warn("PAP login failure for %s", user);
803 if (ret == UPAP_AUTHNAK) {
804 if (*msg == (char *) 0)
805 *msg = "Login incorrect";
806 *msglen = strlen(*msg);
808 * XXX can we ever get here more than once??
809 * Frustrate passwd stealer programs.
810 * Allow 10 tries, but start backing off after 3 (stolen from login).
811 * On 10'th, drop the connection.
813 if (attempts++ >= 10) {
814 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
815 lcp_close(unit, "login failed");
818 sleep((u_int) (attempts - 3) * 5);
820 free_wordlist(addrs);
823 attempts = 0; /* Reset count */
824 if (*msg == (char *) 0)
826 *msglen = strlen(*msg);
827 set_allowed_addrs(unit, addrs);
830 BZERO(passwd, sizeof(passwd));
831 BZERO(secret, sizeof(secret));
837 * This function is needed for PAM.
841 static char *PAM_username = "";
842 static char *PAM_password = "";
844 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
845 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
847 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
850 static int pam_conv (int num_msg,
851 const struct pam_message **msg,
852 struct pam_response **resp,
855 int count = 0, replies = 0;
856 struct pam_response *reply = NULL;
859 for (count = 0; count < num_msg; count++)
861 size += sizeof (struct pam_response);
862 reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
866 switch (msg[count]->msg_style)
868 case PAM_PROMPT_ECHO_ON:
869 reply[replies].resp_retcode = PAM_SUCCESS;
870 reply[replies++].resp = strdup(PAM_username); /* never NULL */
873 case PAM_PROMPT_ECHO_OFF:
874 reply[replies].resp_retcode = PAM_SUCCESS;
875 reply[replies++].resp = strdup(PAM_password); /* never NULL */
879 reply[replies].resp_retcode = PAM_SUCCESS;
880 reply[replies++].resp = NULL;
900 * plogin - Check the user name and password against the system
901 * password database, and login the user if OK.
904 * UPAP_AUTHNAK: Login failed.
905 * UPAP_AUTHACK: Login succeeded.
906 * In either case, msg points to an appropriate message.
910 plogin(user, passwd, msg, msglen)
919 struct pam_conv pam_conversation;
923 * Fill the pam_conversion structure
925 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
926 pam_conversation.conv = &pam_conv;
928 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
930 if (pam_error != PAM_SUCCESS) {
931 *msg = MY_PAM_STRERROR (pam_error);
935 * Define the fields for the credintial validation
937 (void) pam_set_item (pamh, PAM_TTY, devnam);
939 PAM_password = passwd;
943 pam_error = pam_authenticate (pamh, PAM_SILENT);
944 if (pam_error == PAM_SUCCESS) {
945 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
947 /* start a session for this user. Session closed when link ends. */
948 if (pam_error == PAM_SUCCESS)
949 (void) pam_open_session (pamh, PAM_SILENT);
952 *msg = MY_PAM_STRERROR (pam_error);
959 (void) pam_end (pamh, pam_error);
961 if (pam_error != PAM_SUCCESS)
964 * Use the non-PAM methods directly
966 #else /* #ifdef USE_PAM */
973 struct spwd *getspnam();
980 return (UPAP_AUTHNAK);
983 spwd = getspnam(user);
988 /* check the age of the password entry */
989 now = time(NULL) / 86400L;
991 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
992 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
993 && spwd->sp_lstchg >= 0
994 && now >= spwd->sp_lstchg + spwd->sp_max)) {
995 warn("Password for %s has expired", user);
996 return (UPAP_AUTHNAK);
998 pw->pw_passwd = spwd->sp_pwdp;
1002 * If no passwd, don't let them login.
1004 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
1005 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1006 return (UPAP_AUTHNAK);
1008 /* These functions are not enabled for PAM. The reason for this is that */
1009 /* there is not necessarily a "passwd" entry for this user. That is */
1010 /* real purpose of 'PAM' -- to virtualize the account data from the */
1011 /* application. If you want to do the same thing, write the entry in */
1012 /* the 'session' hook. */
1015 * Write a wtmp entry for this user.
1019 if (strncmp(tty, "/dev/", 5) == 0)
1021 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1023 #if defined(_PATH_LASTLOG)
1028 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1029 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1030 memset((void *)&ll, 0, sizeof(ll));
1031 (void)time(&ll.ll_time);
1032 (void)strlcpy(ll.ll_line, tty, sizeof(ll.ll_line));
1033 (void)write(fd, (char *)&ll, sizeof(ll));
1039 #endif /* #ifdef USE_PAM */
1041 info("user %s logged in", user);
1044 return (UPAP_AUTHACK);
1048 * plogout - Logout the user.
1054 struct pam_conv pam_conversation;
1058 * Fill the pam_conversion structure. The PAM specification states that the
1059 * session must be able to be closed by a totally different handle from which
1060 * it was created. Hold the PAM group to their own specification!
1062 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
1063 pam_conversation.conv = &pam_conv;
1065 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
1066 if (pam_error == PAM_SUCCESS) {
1067 (void) pam_set_item (pamh, PAM_TTY, devnam);
1068 (void) pam_close_session (pamh, PAM_SILENT);
1069 (void) pam_end (pamh, PAM_SUCCESS);
1076 if (strncmp(tty, "/dev/", 5) == 0)
1078 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1086 * null_login - Check if a username of "" and a password of "" are
1087 * acceptable, and iff so, set the list of acceptable IP addresses
1097 struct wordlist *addrs;
1098 char secret[MAXWORDLEN];
1101 * Open the file of pap secrets and scan for a suitable secret.
1102 * We don't accept a wildcard client.
1104 filename = _PATH_UPAPFILE;
1106 f = fopen(filename, "r");
1109 check_access(f, filename);
1111 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
1112 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1113 BZERO(secret, sizeof(secret));
1116 set_allowed_addrs(unit, addrs);
1118 free_wordlist(addrs);
1126 * get_pap_passwd - get a password for authenticating ourselves with
1127 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1129 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1132 get_pap_passwd(passwd)
1138 struct wordlist *addrs;
1139 char secret[MAXWORDLEN];
1141 filename = _PATH_UPAPFILE;
1143 f = fopen(filename, "r");
1146 check_access(f, filename);
1147 ret = scan_authfile(f, user,
1148 (remote_name[0]? remote_name: NULL),
1149 secret, NULL, filename);
1154 strlcpy(passwd, secret, MAXSECRETLEN);
1155 BZERO(secret, sizeof(secret));
1161 * have_pap_secret - check whether we have a PAP file with any
1162 * secrets that we could possibly use for authenticating the peer.
1165 have_pap_secret(lacks_ipp)
1171 struct wordlist *addrs;
1173 filename = _PATH_UPAPFILE;
1174 f = fopen(filename, "r");
1178 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1179 NULL, &addrs, filename);
1181 if (ret >= 0 && !some_ip_ok(addrs)) {
1187 free_wordlist(addrs);
1194 * have_chap_secret - check whether we have a CHAP file with a
1195 * secret that we could possibly use for authenticating `client'
1196 * on `server'. Either can be the null string, meaning we don't
1197 * know the identity yet.
1200 have_chap_secret(client, server, need_ip, lacks_ipp)
1209 struct wordlist *addrs;
1211 filename = _PATH_CHAPFILE;
1212 f = fopen(filename, "r");
1216 if (client != NULL && client[0] == 0)
1218 else if (server != NULL && server[0] == 0)
1221 ret = scan_authfile(f, client, server, NULL, &addrs, filename);
1223 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1229 free_wordlist(addrs);
1236 * get_secret - open the CHAP secret file and return the secret
1237 * for authenticating the given client on the given server.
1238 * (We could be either client or server).
1241 get_secret(unit, client, server, secret, secret_len, save_addrs)
1252 struct wordlist *addrs;
1253 char secbuf[MAXWORDLEN];
1255 filename = _PATH_CHAPFILE;
1259 f = fopen(filename, "r");
1261 error("Can't open chap secret file %s: %m", filename);
1264 check_access(f, filename);
1266 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
1272 set_allowed_addrs(unit, addrs);
1274 len = strlen(secbuf);
1275 if (len > MAXSECRETLEN) {
1276 error("Secret for %s on %s is too long", client, server);
1279 BCOPY(secbuf, secret, len);
1280 BZERO(secbuf, sizeof(secbuf));
1287 * set_allowed_addrs() - set the list of allowed addresses.
1290 set_allowed_addrs(unit, addrs)
1292 struct wordlist *addrs;
1295 struct wordlist *ap;
1296 struct permitted_ip *ip;
1297 char *ptr_word, *ptr_mask;
1300 u_int32_t a, mask, ah;
1301 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1302 u_int32_t suggested_ip = 0;
1304 if (addresses[unit] != NULL)
1305 free(addresses[unit]);
1306 addresses[unit] = NULL;
1308 for (ap = addrs; ap != NULL; ap = ap->next)
1312 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1317 for (ap = addrs; ap != NULL; ap = ap->next) {
1318 /* "-" means no addresses authorized, "*" means any address allowed */
1319 ptr_word = ap->word;
1320 if (strcmp(ptr_word, "-") == 0)
1322 if (strcmp(ptr_word, "*") == 0) {
1324 ip[n].base = ip[n].mask = 0;
1330 if (*ptr_word == '!') {
1335 mask = ~ (u_int32_t) 0;
1336 ptr_mask = strchr (ptr_word, '/');
1337 if (ptr_mask != NULL) {
1340 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1341 if (bit_count <= 0 || bit_count > 32) {
1342 warn("invalid address length %v in auth. address list",
1347 mask <<= 32 - bit_count;
1350 hp = gethostbyname(ptr_word);
1351 if (hp != NULL && hp->h_addrtype == AF_INET) {
1352 a = *(u_int32_t *)hp->h_addr;
1354 np = getnetbyname (ptr_word);
1355 if (np != NULL && np->n_addrtype == AF_INET) {
1356 a = htonl (*(u_int32_t *)np->n_net);
1357 if (ptr_mask == NULL) {
1358 /* calculate appropriate mask for net */
1361 mask = IN_CLASSA_NET;
1362 else if (IN_CLASSB(ah))
1363 mask = IN_CLASSB_NET;
1364 else if (IN_CLASSC(ah))
1365 mask = IN_CLASSC_NET;
1368 a = inet_addr (ptr_word);
1372 if (ptr_mask != NULL)
1375 if (a == (u_int32_t)-1L)
1376 warn("unknown host %s in auth. address list", ap->word);
1378 ip[n].mask = htonl(mask);
1379 ip[n].base = a & ip[n].mask;
1381 if (~mask == 0 && suggested_ip == 0)
1386 ip[n].permit = 0; /* make the last entry forbid all addresses */
1387 ip[n].base = 0; /* to terminate the list */
1390 addresses[unit] = ip;
1393 * If the address given for the peer isn't authorized, or if
1394 * the user hasn't given one, AND there is an authorized address
1395 * which is a single host, then use that if we find one.
1397 if (suggested_ip != 0
1398 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
1399 wo->hisaddr = suggested_ip;
1403 * auth_ip_addr - check whether the peer is authorized to use
1404 * a given IP address. Returns 1 if authorized, 0 otherwise.
1407 auth_ip_addr(unit, addr)
1411 if (addresses[unit] == NULL) {
1413 return 0; /* no addresses authorized */
1414 return allow_any_ip || !have_route_to(addr);
1416 return ip_addr_check(addr, addresses[unit]);
1420 ip_addr_check(addr, addrs)
1422 struct permitted_ip *addrs;
1424 /* don't allow loopback or multicast address */
1425 if (bad_ip_adrs(addr))
1429 if ((addr & addrs->mask) == addrs->base)
1430 return addrs->permit;
1434 * bad_ip_adrs - return 1 if the IP address is one we don't want
1435 * to use, such as an address in the loopback net or a multicast address.
1436 * addr is in network byte order.
1443 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1444 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1448 * some_ip_ok - check a wordlist to see if it authorizes any
1453 struct wordlist *addrs;
1455 for (; addrs != 0; addrs = addrs->next) {
1456 if (strcmp(addrs->word, "-") == 0)
1458 if (addrs->word[0] != '!')
1459 return 1; /* some IP address is allowed */
1465 * check_access - complain if a secret file has too-liberal permissions.
1468 check_access(f, filename)
1474 if (fstat(fileno(f), &sbuf) < 0) {
1475 warn("cannot stat secret file %s: %m", filename);
1476 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1477 warn("Warning - secret file %s has world and/or group access",
1484 * scan_authfile - Scan an authorization file for a secret suitable
1485 * for authenticating `client' on `server'. The return value is -1
1486 * if no secret is found, otherwise >= 0. The return value has
1487 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1488 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1489 * Any following words on the line (i.e. address authorization
1490 * info) are placed in a wordlist and returned in *addrs.
1491 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1494 scan_authfile(f, client, server, secret, addrs, filename)
1499 struct wordlist **addrs;
1503 int got_flag, best_flag;
1505 struct wordlist *ap, *addr_list, *alist, *alast;
1506 char word[MAXWORDLEN];
1507 char atfile[MAXWORDLEN];
1508 char lsecret[MAXWORDLEN];
1513 if (!getword(f, word, &newline, filename))
1514 return -1; /* file is empty??? */
1519 * Skip until we find a word at the start of a line.
1521 while (!newline && getword(f, word, &newline, filename))
1524 break; /* got to end of file */
1527 * Got a client - check if it's a match or a wildcard.
1530 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1535 got_flag = NONWILD_CLIENT;
1538 * Now get a server and check if it matches.
1540 if (!getword(f, word, &newline, filename))
1544 if (!ISWILD(word)) {
1545 if (server != NULL && strcmp(word, server) != 0)
1547 got_flag |= NONWILD_SERVER;
1551 * Got some sort of a match - see if it's better than what
1554 if (got_flag <= best_flag)
1560 if (!getword(f, word, &newline, filename))
1566 * Special syntax: @filename means read secret from file.
1568 if (word[0] == '@') {
1569 strlcpy(atfile, word+1, sizeof(atfile));
1570 if ((sf = fopen(atfile, "r")) == NULL) {
1571 warn("can't open indirect secret file %s", atfile);
1574 check_access(sf, atfile);
1575 if (!getword(sf, word, &xxx, atfile)) {
1576 warn("no secret in indirect secret file %s", atfile);
1583 strlcpy(lsecret, word, sizeof(lsecret));
1586 * Now read address authorization info and make a wordlist.
1588 alist = alast = NULL;
1590 if (!getword(f, word, &newline, filename) || newline)
1592 ap = (struct wordlist *) malloc(sizeof(struct wordlist));
1594 novm("authorized addresses");
1596 ap->word = strdup(word);
1597 if (ap->word == NULL)
1598 novm("authorized address");
1607 * This is the best so far; remember it.
1609 best_flag = got_flag;
1611 free_wordlist(addr_list);
1614 strlcpy(secret, lsecret, MAXWORDLEN);
1622 else if (addr_list != NULL)
1623 free_wordlist(addr_list);
1629 * free_wordlist - release memory allocated for a wordlist.
1633 struct wordlist *wp;
1635 struct wordlist *next;
1637 while (wp != NULL) {
1645 * auth_script_done - called when the auth-up or auth-down script
1649 auth_script_done(arg)
1652 auth_script_pid = 0;
1653 switch (auth_script_state) {
1655 if (auth_state == s_down) {
1656 auth_script_state = s_down;
1657 auth_script(_PATH_AUTHDOWN);
1661 if (auth_state == s_up) {
1662 auth_script_state = s_up;
1663 auth_script(_PATH_AUTHUP);
1670 * auth_script - execute a script with arguments
1671 * interface-name peer-name real-user tty speed
1683 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1684 user_name = pw->pw_name;
1686 slprintf(struid, sizeof(struid), "%d", getuid());
1689 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1693 argv[2] = peer_authname;
1694 argv[3] = user_name;
1699 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);