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.43 1999/03/06 11:28:10 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)
96 /* The name by which the peer authenticated itself to us. */
97 char peer_authname[MAXNAMELEN];
99 /* Records which authentication operations haven't completed yet. */
100 static int auth_pending[NUM_PPP];
102 /* Set if we have successfully called plogin() */
103 static int logged_in;
105 /* Set if we have run the /etc/ppp/auth-up script. */
106 static int did_authup;
107 static pid_t authup_pid; /* process ID of auth-up/down script */
109 /* List of addresses which the peer may use. */
110 static struct wordlist *addresses[NUM_PPP];
112 /* Number of network protocols which we have opened. */
113 static int num_np_open;
115 /* Number of network protocols which have come up. */
116 static int num_np_up;
118 /* Set if we got the contents of passwd[] from the pap-secrets file. */
119 static int passwd_from_file;
124 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
125 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
126 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
127 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
128 bool usehostname = 0; /* Use hostname for our_name */
129 bool auth_required = 0; /* Always require authentication from peer */
130 bool allow_any_ip = 0; /* Allow peer to use any IP address */
132 /* Bits in auth_pending[] */
133 #define PAP_WITHPEER 1
135 #define CHAP_WITHPEER 4
138 extern char *crypt __P((const char *, const char *));
140 /* Prototypes for procedures local to this file. */
142 static void network_phase __P((int));
143 static void check_idle __P((void *));
144 static void connect_time_expired __P((void *));
145 static int plogin __P((char *, char *, char **, int *));
146 static void plogout __P((void));
147 static int null_login __P((int));
148 static int get_pap_passwd __P((char *));
149 static int have_pap_secret __P((void));
150 static int have_chap_secret __P((char *, char *, u_int32_t));
151 static int ip_addr_check __P((u_int32_t, struct wordlist *));
152 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
153 struct wordlist **, char *));
154 static void free_wordlist __P((struct wordlist *));
155 static void auth_script __P((char *));
156 static void set_allowed_addrs __P((int, struct wordlist *));
157 static int setupapfile __P((char **));
160 * Authentication-related options.
162 option_t auth_options[] = {
163 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
164 "Require PAP authentication from peer", 1, &auth_required },
165 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
166 "Require PAP authentication from peer", 1, &auth_required },
167 { "refuse-pap", o_bool, &refuse_pap,
168 "Don't agree to auth to peer with PAP", 1 },
169 { "-pap", o_bool, &refuse_pap,
170 "Don't allow PAP authentication with peer", 1 },
171 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
172 "Require CHAP authentication from peer", 1, &auth_required },
173 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
174 "Require CHAP authentication from peer", 1, &auth_required },
175 { "refuse-chap", o_bool, &refuse_chap,
176 "Don't agree to auth to peer with CHAP", 1 },
177 { "-chap", o_bool, &refuse_chap,
178 "Don't allow CHAP authentication with peer", 1 },
179 { "name", o_string, our_name,
180 "Set local name for authentication",
181 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
182 { "user", o_string, user,
183 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
184 { "usehostname", o_bool, &usehostname,
185 "Must use hostname for authentication", 1 },
186 { "remotename", o_string, remote_name,
187 "Set remote name for authentication", OPT_STATIC, NULL, MAXNAMELEN },
188 { "auth", o_bool, &auth_required,
189 "Require authentication from peer", 1 },
190 { "noauth", o_bool, &auth_required,
191 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
192 { "login", o_bool, &uselogin,
193 "Use system password database for PAP", 1 },
194 { "papcrypt", o_bool, &cryptpap,
195 "PAP passwords are encrypted", 1 },
196 { "+ua", o_special, setupapfile,
197 "Get PAP user and password from file" },
202 * setupapfile - specifies UPAP info for authenticating with peer.
211 lcp_allowoptions[0].neg_upap = 1;
213 /* open user info file */
215 ufile = fopen(*argv, "r");
218 option_error("unable to open user login data file %s", *argv);
221 #if 0 /* check done by setting effective UID above */
222 if (!readable(fileno(ufile))) {
223 option_error("%s: access denied", *argv);
227 check_access(ufile, *argv);
230 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
231 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
232 option_error("unable to read user login data file %s", *argv);
237 /* get rid of newlines */
239 if (l > 0 && user[l-1] == '\n')
242 if (l > 0 && passwd[l-1] == '\n')
250 * An Open on LCP has requested a change from Dead to Establish phase.
251 * Do what's necessary to bring the physical layer up.
260 * LCP has terminated the link; go to the Dead phase and take the
261 * physical layer down.
264 link_terminated(unit)
267 if (phase == PHASE_DEAD)
272 syslog(LOG_NOTICE, "Connection terminated.");
276 * LCP has gone down; it will either die or try to re-establish.
283 struct protent *protp;
286 auth_script(_PATH_AUTHDOWN);
289 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
290 if (!protp->enabled_flag)
292 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
293 (*protp->lowerdown)(unit);
294 if (protp->protocol < 0xC000 && protp->close != NULL)
295 (*protp->close)(unit, "LCP down");
299 if (phase != PHASE_DEAD)
300 phase = PHASE_TERMINATE;
304 * The link is established.
305 * Proceed to the Dead, Authenticate or Network phase as appropriate.
308 link_established(unit)
312 lcp_options *wo = &lcp_wantoptions[unit];
313 lcp_options *go = &lcp_gotoptions[unit];
314 lcp_options *ho = &lcp_hisoptions[unit];
316 struct protent *protp;
319 * Tell higher-level protocols that LCP is up.
321 for (i = 0; (protp = protocols[i]) != NULL; ++i)
322 if (protp->protocol != PPP_LCP && protp->enabled_flag
323 && protp->lowerup != NULL)
324 (*protp->lowerup)(unit);
326 if (auth_required && !(go->neg_chap || go->neg_upap)) {
328 * We wanted the peer to authenticate itself, and it refused:
329 * treat it as though it authenticated with PAP using a username
330 * of "" and a password of "". If that's not OK, boot it out.
332 if (!wo->neg_upap || !null_login(unit)) {
333 syslog(LOG_WARNING, "peer refused to authenticate");
334 lcp_close(unit, "peer refused to authenticate");
339 phase = PHASE_AUTHENTICATE;
342 ChapAuthPeer(unit, our_name, go->chap_mdtype);
344 } else if (go->neg_upap) {
349 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
350 auth |= CHAP_WITHPEER;
351 } else if (ho->neg_upap) {
352 if (passwd[0] == 0) {
353 passwd_from_file = 1;
354 if (!get_pap_passwd(passwd))
355 syslog(LOG_ERR, "No secret found for PAP login");
357 upap_authwithpeer(unit, user, passwd);
358 auth |= PAP_WITHPEER;
360 auth_pending[unit] = auth;
367 * Proceed to the network phase.
374 struct protent *protp;
375 lcp_options *go = &lcp_gotoptions[unit];
378 * If the peer had to authenticate, run the auth-up script now.
380 if ((go->neg_chap || go->neg_upap) && !did_authup) {
381 auth_script(_PATH_AUTHUP);
387 * If we negotiated callback, do it now.
390 phase = PHASE_CALLBACK;
391 (*cbcp_protent.open)(unit);
396 phase = PHASE_NETWORK;
399 set_filters(&pass_filter, &active_filter);
401 for (i = 0; (protp = protocols[i]) != NULL; ++i)
402 if (protp->protocol < 0xC000 && protp->enabled_flag
403 && protp->open != NULL) {
404 (*protp->open)(unit);
405 if (protp->protocol != PPP_CCP)
409 if (num_np_open == 0)
411 lcp_close(0, "No network protocols running");
415 * The peer has failed to authenticate himself using `protocol'.
418 auth_peer_fail(unit, protocol)
422 * Authentication failure: take the link down
424 lcp_close(unit, "Authentication failed");
428 * The peer has been successfully authenticated using `protocol'.
431 auth_peer_success(unit, protocol, name, namelen)
446 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
452 * Save the authenticated name of the peer for later.
454 if (namelen > sizeof(peer_authname) - 1)
455 namelen = sizeof(peer_authname) - 1;
456 BCOPY(name, peer_authname, namelen);
457 peer_authname[namelen] = 0;
458 script_setenv("PEERNAME", peer_authname);
461 * If there is no more authentication still to be done,
462 * proceed to the network (or callback) phase.
464 if ((auth_pending[unit] &= ~bit) == 0)
469 * We have failed to authenticate ourselves to the peer using `protocol'.
472 auth_withpeer_fail(unit, protocol)
475 if (passwd_from_file)
476 BZERO(passwd, MAXSECRETLEN);
478 * We've failed to authenticate ourselves to our peer.
479 * He'll probably take the link down, and there's not much
480 * we can do except wait for that.
485 * We have successfully authenticated ourselves with the peer using `protocol'.
488 auth_withpeer_success(unit, protocol)
498 if (passwd_from_file)
499 BZERO(passwd, MAXSECRETLEN);
503 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
509 * If there is no more authentication still being done,
510 * proceed to the network (or callback) phase.
512 if ((auth_pending[unit] &= ~bit) == 0)
518 * np_up - a network protocol has come up.
524 if (num_np_up == 0) {
526 * At this point we consider that the link has come up successfully.
530 if (idle_time_limit > 0)
531 TIMEOUT(check_idle, NULL, idle_time_limit);
534 * Set a timeout to close the connection once the maximum
535 * connect time has expired.
538 TIMEOUT(connect_time_expired, 0, maxconnect);
541 * Detach now, if the updetach option was given.
543 if (updetach && !nodetach)
550 * np_down - a network protocol has gone down.
556 if (--num_np_up == 0 && idle_time_limit > 0) {
557 UNTIMEOUT(check_idle, NULL);
562 * np_finished - a network protocol has finished using the link.
565 np_finished(unit, proto)
568 if (--num_np_open <= 0) {
569 /* no further use for the link: shut up shop. */
570 lcp_close(0, "No network protocols running");
575 * check_idle - check whether the link has been idle for long
576 * enough that we can shut it down.
582 struct ppp_idle idle;
585 if (!get_idle_time(0, &idle))
587 itime = MIN(idle.xmit_idle, idle.recv_idle);
588 if (itime >= idle_time_limit) {
589 /* link is idle: shut it down. */
590 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
591 lcp_close(0, "Link inactive");
593 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
598 * connect_time_expired - log a message and close the connection.
601 connect_time_expired(arg)
604 syslog(LOG_INFO, "Connect time expired");
605 lcp_close(0, "Connect time expired"); /* Close connection */
609 * auth_check_options - called to check authentication options.
614 lcp_options *wo = &lcp_wantoptions[0];
616 ipcp_options *ipwo = &ipcp_wantoptions[0];
619 /* Default our_name to hostname, and user to our_name */
620 if (our_name[0] == 0 || usehostname)
621 strcpy(our_name, hostname);
623 strcpy(user, our_name);
625 /* If authentication is required, ask peer for CHAP or PAP. */
627 if (!wo->neg_chap && !wo->neg_upap) {
637 * If we have a default route, require the peer to authenticate
638 * unless the noauth option was given.
640 if (!auth_required && !allow_any_ip && have_route_to(0))
644 * Check whether we have appropriate secrets to use
645 * to authenticate the peer.
647 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
648 if (!can_auth && wo->neg_chap) {
649 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
650 can_auth = have_chap_secret(remote_name, our_name, remote);
653 if (auth_required && !can_auth) {
654 option_error("peer authentication required but no suitable secret(s) found\n");
655 if (remote_name[0] == 0)
656 option_error("for authenticating any peer to us (%s)\n", our_name);
658 option_error("for authenticating peer %s to us (%s)\n",
659 remote_name, our_name);
664 * Check whether the user tried to override certain values
668 if ((connector != NULL && connector_info.priv == 0)
669 || (disconnector != NULL && disconnector_info.priv == 0)
670 || (welcomer != NULL && welcomer_info.priv == 0)) {
671 option_error("connect, disconnect and welcome options");
672 option_error("are privileged when noauth option is used");
679 * auth_reset - called when LCP is starting negotiations to recheck
680 * authentication options, i.e. whether we have appropriate secrets
681 * to use for authenticating ourselves and/or the peer.
687 lcp_options *go = &lcp_gotoptions[unit];
688 lcp_options *ao = &lcp_allowoptions[0];
689 ipcp_options *ipwo = &ipcp_wantoptions[0];
692 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
693 ao->neg_chap = !refuse_chap
694 && have_chap_secret(user, remote_name, (u_int32_t)0);
696 if (go->neg_upap && !uselogin && !have_pap_secret())
699 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
700 if (!have_chap_secret(remote_name, our_name, remote))
707 * check_passwd - Check the user name and passwd against the PAP secrets
708 * file. If requested, also check against the system password database,
709 * and login the user if OK.
712 * UPAP_AUTHNAK: Authentication failed.
713 * UPAP_AUTHACK: Authentication succeeded.
714 * In either case, msg points to an appropriate message.
717 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
729 struct wordlist *addrs;
731 ipcp_options *ipwo = &ipcp_wantoptions[unit];
732 char passwd[256], user[256];
733 char secret[MAXWORDLEN];
734 static int attempts = 0;
737 * Make copies of apasswd and auser, then null-terminate them.
739 BCOPY(apasswd, passwd, passwdlen);
740 passwd[passwdlen] = '\0';
741 BCOPY(auser, user, userlen);
742 user[userlen] = '\0';
746 * Open the file of pap secrets and scan for a suitable secret
747 * for authenticating this user.
749 filename = _PATH_UPAPFILE;
752 f = fopen(filename, "r");
754 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
758 check_access(f, filename);
759 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
760 if (scan_authfile(f, user, our_name, remote,
761 secret, &addrs, filename) < 0
762 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
763 && strcmp(crypt(passwd, secret), secret) != 0)) {
764 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
770 if (uselogin && ret == UPAP_AUTHACK) {
771 ret = plogin(user, passwd, msg, msglen);
772 if (ret == UPAP_AUTHNAK) {
773 syslog(LOG_WARNING, "PAP login failure for %s", user);
777 if (ret == UPAP_AUTHNAK) {
778 if (*msg == (char *) 0)
779 *msg = "Login incorrect";
780 *msglen = strlen(*msg);
782 * Frustrate passwd stealer programs.
783 * Allow 10 tries, but start backing off after 3 (stolen from login).
784 * On 10'th, drop the connection.
786 if (attempts++ >= 10) {
787 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
788 attempts, devnam, user);
792 sleep((u_int) (attempts - 3) * 5);
794 free_wordlist(addrs);
797 attempts = 0; /* Reset count */
798 if (*msg == (char *) 0)
800 *msglen = strlen(*msg);
801 set_allowed_addrs(unit, addrs);
804 BZERO(passwd, sizeof(passwd));
805 BZERO(secret, sizeof(secret));
811 * This function is needed for PAM.
815 static char *PAM_username = "";
816 static char *PAM_password = "";
818 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
819 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
821 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
824 static int pam_conv (int num_msg,
825 const struct pam_message **msg,
826 struct pam_response **resp,
829 int count = 0, replies = 0;
830 struct pam_response *reply = NULL;
833 for (count = 0; count < num_msg; count++)
835 size += sizeof (struct pam_response);
836 reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
840 switch (msg[count]->msg_style)
842 case PAM_PROMPT_ECHO_ON:
843 reply[replies].resp_retcode = PAM_SUCCESS;
844 reply[replies++].resp = strdup(PAM_username); /* never NULL */
847 case PAM_PROMPT_ECHO_OFF:
848 reply[replies].resp_retcode = PAM_SUCCESS;
849 reply[replies++].resp = strdup(PAM_password); /* never NULL */
853 reply[replies].resp_retcode = PAM_SUCCESS;
854 reply[replies++].resp = NULL;
874 * plogin - Check the user name and password against the system
875 * password database, and login the user if OK.
878 * UPAP_AUTHNAK: Login failed.
879 * UPAP_AUTHACK: Login succeeded.
880 * In either case, msg points to an appropriate message.
884 plogin(user, passwd, msg, msglen)
893 struct pam_conv pam_conversation;
897 * Fill the pam_conversion structure
899 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
900 pam_conversation.conv = &pam_conv;
902 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
904 if (pam_error != PAM_SUCCESS) {
905 *msg = MY_PAM_STRERROR (pam_error);
909 * Define the fields for the credintial validation
911 (void) pam_set_item (pamh, PAM_TTY, devnam);
913 PAM_password = passwd;
917 pam_error = pam_authenticate (pamh, PAM_SILENT);
918 if (pam_error == PAM_SUCCESS) {
919 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
921 /* start a session for this user. Session closed when link ends. */
922 if (pam_error == PAM_SUCCESS)
923 (void) pam_open_session (pamh, PAM_SILENT);
926 *msg = MY_PAM_STRERROR (pam_error);
933 (void) pam_end (pamh, pam_error);
935 if (pam_error != PAM_SUCCESS)
938 * Use the non-PAM methods directly
940 #else /* #ifdef USE_PAM */
947 struct spwd *getspnam();
953 return (UPAP_AUTHNAK);
957 spwd = getspnam(user);
960 /* check the age of the password entry */
961 long now = time(NULL) / 86400L;
963 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
964 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
965 && spwd->sp_lstchg >= 0
966 && now >= spwd->sp_lstchg + spwd->sp_max)) {
967 syslog(LOG_WARNING, "Password for %s has expired", user);
968 return (UPAP_AUTHNAK);
970 pw->pw_passwd = spwd->sp_pwdp;
975 * If no passwd, don't let them login.
977 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
978 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
979 return (UPAP_AUTHNAK);
981 /* These functions are not enabled for PAM. The reason for this is that */
982 /* there is not necessarily a "passwd" entry for this user. That is */
983 /* real purpose of 'PAM' -- to virtualize the account data from the */
984 /* application. If you want to do the same thing, write the entry in */
985 /* the 'session' hook. */
988 * Write a wtmp entry for this user.
992 if (strncmp(tty, "/dev/", 5) == 0)
994 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
996 #if defined(_PATH_LASTLOG)
1001 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1002 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1003 memset((void *)&ll, 0, sizeof(ll));
1004 (void)time(&ll.ll_time);
1005 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1006 (void)write(fd, (char *)&ll, sizeof(ll));
1012 #endif /* #ifdef USE_PAM */
1014 syslog(LOG_INFO, "user %s logged in", user);
1017 return (UPAP_AUTHACK);
1021 * plogout - Logout the user.
1027 struct pam_conv pam_conversation;
1031 * Fill the pam_conversion structure. The PAM specification states that the
1032 * session must be able to be closed by a totally different handle from which
1033 * it was created. Hold the PAM group to their own specification!
1035 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
1036 pam_conversation.conv = &pam_conv;
1038 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
1039 if (pam_error == PAM_SUCCESS) {
1040 (void) pam_set_item (pamh, PAM_TTY, devnam);
1041 (void) pam_close_session (pamh, PAM_SILENT);
1042 (void) pam_end (pamh, PAM_SUCCESS);
1049 if (strncmp(tty, "/dev/", 5) == 0)
1051 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1059 * null_login - Check if a username of "" and a password of "" are
1060 * acceptable, and iff so, set the list of acceptable IP addresses
1070 struct wordlist *addrs;
1071 char secret[MAXWORDLEN];
1074 * Open the file of pap secrets and scan for a suitable secret.
1075 * We don't accept a wildcard client.
1077 filename = _PATH_UPAPFILE;
1079 f = fopen(filename, "r");
1082 check_access(f, filename);
1084 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
1085 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1086 BZERO(secret, sizeof(secret));
1089 set_allowed_addrs(unit, addrs);
1091 free_wordlist(addrs);
1099 * get_pap_passwd - get a password for authenticating ourselves with
1100 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1104 get_pap_passwd(passwd)
1110 struct wordlist *addrs;
1111 char secret[MAXWORDLEN];
1113 filename = _PATH_UPAPFILE;
1115 f = fopen(filename, "r");
1118 check_access(f, filename);
1119 ret = scan_authfile(f, user,
1120 remote_name[0]? remote_name: NULL,
1121 (u_int32_t)0, secret, NULL, filename);
1125 if (passwd != NULL) {
1126 strncpy(passwd, secret, MAXSECRETLEN);
1127 passwd[MAXSECRETLEN-1] = 0;
1129 BZERO(secret, sizeof(secret));
1135 * have_pap_secret - check whether we have a PAP file with any
1136 * secrets that we could possibly use for authenticating the peer.
1144 ipcp_options *ipwo = &ipcp_wantoptions[0];
1147 filename = _PATH_UPAPFILE;
1148 f = fopen(filename, "r");
1152 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
1153 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
1163 * have_chap_secret - check whether we have a CHAP file with a
1164 * secret that we could possibly use for authenticating `client'
1165 * on `server'. Either can be the null string, meaning we don't
1166 * know the identity yet.
1169 have_chap_secret(client, server, remote)
1178 filename = _PATH_CHAPFILE;
1179 f = fopen(filename, "r");
1185 else if (server[0] == 0)
1188 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1198 * get_secret - open the CHAP secret file and return the secret
1199 * for authenticating the given client on the given server.
1200 * (We could be either client or server).
1203 get_secret(unit, client, server, secret, secret_len, save_addrs)
1214 struct wordlist *addrs;
1215 char secbuf[MAXWORDLEN];
1217 filename = _PATH_CHAPFILE;
1221 f = fopen(filename, "r");
1223 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1226 check_access(f, filename);
1228 ret = scan_authfile(f, client, server, (u_int32_t)0,
1229 secbuf, &addrs, filename);
1235 set_allowed_addrs(unit, addrs);
1237 len = strlen(secbuf);
1238 if (len > MAXSECRETLEN) {
1239 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1242 BCOPY(secbuf, secret, len);
1243 BZERO(secbuf, sizeof(secbuf));
1250 * set_allowed_addrs() - set the list of allowed addresses.
1253 set_allowed_addrs(unit, addrs)
1255 struct wordlist *addrs;
1257 if (addresses[unit] != NULL)
1258 free_wordlist(addresses[unit]);
1259 addresses[unit] = addrs;
1262 * If there's only one authorized address we might as well
1263 * ask our peer for that one right away
1265 if (addrs != NULL && addrs->next == NULL) {
1266 char *p = addrs->word;
1267 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1271 if (*p != '!' && *p != '-' && *p != '*' && strchr(p, '/') == NULL) {
1272 hp = gethostbyname(p);
1273 if (hp != NULL && hp->h_addrtype == AF_INET)
1274 a = *(u_int32_t *)hp->h_addr;
1277 if (a != (u_int32_t) -1)
1284 * auth_ip_addr - check whether the peer is authorized to use
1285 * a given IP address. Returns 1 if authorized, 0 otherwise.
1288 auth_ip_addr(unit, addr)
1292 return ip_addr_check(addr, addresses[unit]);
1296 ip_addr_check(addr, addrs)
1298 struct wordlist *addrs;
1300 u_int32_t a, mask, ah;
1302 char *ptr_word, *ptr_mask;
1306 /* don't allow loopback or multicast address */
1307 if (bad_ip_adrs(addr))
1310 if (addrs == NULL) {
1312 return 0; /* no addresses authorized */
1313 return allow_any_ip || !have_route_to(addr);
1316 for (; addrs != NULL; addrs = addrs->next) {
1317 /* "-" means no addresses authorized, "*" means any address allowed */
1318 ptr_word = addrs->word;
1319 if (strcmp(ptr_word, "-") == 0)
1321 if (strcmp(ptr_word, "*") == 0)
1325 if (*ptr_word == '!') {
1330 mask = ~ (u_int32_t) 0;
1331 ptr_mask = strchr (ptr_word, '/');
1332 if (ptr_mask != NULL) {
1335 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1336 if (bit_count <= 0 || bit_count > 32) {
1337 syslog (LOG_WARNING,
1338 "invalid address length %s in auth. address list",
1343 mask <<= 32 - bit_count;
1346 hp = gethostbyname(ptr_word);
1347 if (hp != NULL && hp->h_addrtype == AF_INET) {
1348 a = *(u_int32_t *)hp->h_addr;
1350 np = getnetbyname (ptr_word);
1351 if (np != NULL && np->n_addrtype == AF_INET) {
1352 a = htonl (*(u_int32_t *)np->n_net);
1353 if (ptr_mask == NULL) {
1354 /* calculate appropriate mask for net */
1357 mask = IN_CLASSA_NET;
1358 else if (IN_CLASSB(ah))
1359 mask = IN_CLASSB_NET;
1360 else if (IN_CLASSC(ah))
1361 mask = IN_CLASSC_NET;
1364 a = inet_addr (ptr_word);
1368 if (ptr_mask != NULL)
1371 if (a == (u_int32_t)-1L)
1372 syslog (LOG_WARNING,
1373 "unknown host %s in auth. address list",
1376 /* Here a and addr are in network byte order,
1377 and mask is in host order. */
1378 if (((addr ^ a) & htonl(mask)) == 0)
1381 return 0; /* not in list => can't have it */
1385 * bad_ip_adrs - return 1 if the IP address is one we don't want
1386 * to use, such as an address in the loopback net or a multicast address.
1387 * addr is in network byte order.
1394 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1395 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1399 * check_access - complain if a secret file has too-liberal permissions.
1402 check_access(f, filename)
1408 if (fstat(fileno(f), &sbuf) < 0) {
1409 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1410 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1411 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1417 * scan_authfile - Scan an authorization file for a secret suitable
1418 * for authenticating `client' on `server'. The return value is -1
1419 * if no secret is found, otherwise >= 0. The return value has
1420 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1421 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1422 * Any following words on the line (i.e. address authorization
1423 * info) are placed in a wordlist and returned in *addrs.
1426 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1432 struct wordlist **addrs;
1436 int got_flag, best_flag;
1438 struct wordlist *ap, *addr_list, *alist, *alast;
1439 char word[MAXWORDLEN];
1440 char atfile[MAXWORDLEN];
1441 char lsecret[MAXWORDLEN];
1446 if (!getword(f, word, &newline, filename))
1447 return -1; /* file is empty??? */
1452 * Skip until we find a word at the start of a line.
1454 while (!newline && getword(f, word, &newline, filename))
1457 break; /* got to end of file */
1460 * Got a client - check if it's a match or a wildcard.
1463 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1468 got_flag = NONWILD_CLIENT;
1471 * Now get a server and check if it matches.
1473 if (!getword(f, word, &newline, filename))
1477 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1480 got_flag |= NONWILD_SERVER;
1483 * Got some sort of a match - see if it's better than what
1486 if (got_flag <= best_flag)
1492 if (!getword(f, word, &newline, filename))
1498 * Special syntax: @filename means read secret from file.
1500 if (word[0] == '@') {
1501 strcpy(atfile, word+1);
1502 if ((sf = fopen(atfile, "r")) == NULL) {
1503 syslog(LOG_WARNING, "can't open indirect secret file %s",
1507 check_access(sf, atfile);
1508 if (!getword(sf, word, &xxx, atfile)) {
1509 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1517 strcpy(lsecret, word);
1520 * Now read address authorization info and make a wordlist.
1522 alist = alast = NULL;
1524 if (!getword(f, word, &newline, filename) || newline)
1526 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1529 novm("authorized addresses");
1531 strcpy(ap->word, word);
1540 * Check if the given IP address is allowed by the wordlist.
1541 * XXX accepts this entry even if it has no allowed IP addresses
1542 * if they didn't specify a remote IP address. XXX
1544 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1545 free_wordlist(alist);
1550 * This is the best so far; remember it.
1552 best_flag = got_flag;
1554 free_wordlist(addr_list);
1557 strcpy(secret, lsecret);
1565 else if (addr_list != NULL)
1566 free_wordlist(addr_list);
1572 * free_wordlist - release memory allocated for a wordlist.
1576 struct wordlist *wp;
1578 struct wordlist *next;
1580 while (wp != NULL) {
1588 * auth_script - execute a script with arguments
1589 * interface-name peer-name real-user tty speed
1601 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1602 user_name = pw->pw_name;
1604 sprintf(struid, "%d", getuid());
1607 sprintf(strspeed, "%d", baud_rate);
1611 argv[2] = peer_authname;
1612 argv[3] = user_name;
1617 authup_pid = run_program(script, argv, 0, NULL, NULL);