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.45 1999/03/12 06:07:14 paulus Exp $";
47 #include <sys/types.h>
49 #include <sys/socket.h>
52 #if defined(_PATH_LASTLOG) && defined(_linux_)
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
61 #include <security/pam_appl.h>
67 #define PW_PPP PW_LOGIN
80 #include "pathnames.h"
82 /* Used for storing a sequence of words. Usually malloced. */
84 struct wordlist *next;
88 /* Bits in scan_authfile return value */
89 #define NONWILD_SERVER 1
90 #define NONWILD_CLIENT 2
92 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
97 /* The name by which the peer authenticated itself to us. */
98 char peer_authname[MAXNAMELEN];
100 /* Records which authentication operations haven't completed yet. */
101 static int auth_pending[NUM_PPP];
103 /* Set if we have successfully called plogin() */
104 static int logged_in;
106 /* Set if we have run the /etc/ppp/auth-up script. */
107 static int did_authup;
108 static pid_t authup_pid; /* process ID of auth-up/down script */
110 /* List of addresses which the peer may use. */
111 static struct wordlist *addresses[NUM_PPP];
113 /* Number of network protocols which we have opened. */
114 static int num_np_open;
116 /* Number of network protocols which have come up. */
117 static int num_np_up;
119 /* Set if we got the contents of passwd[] from the pap-secrets file. */
120 static int passwd_from_file;
125 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
126 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
127 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
128 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
129 bool usehostname = 0; /* Use hostname for our_name */
130 bool auth_required = 0; /* Always require authentication from peer */
131 bool allow_any_ip = 0; /* Allow peer to use any IP address */
133 /* Bits in auth_pending[] */
134 #define PAP_WITHPEER 1
136 #define CHAP_WITHPEER 4
139 extern char *crypt __P((const char *, const char *));
141 /* Prototypes for procedures local to this file. */
143 static void network_phase __P((int));
144 static void check_idle __P((void *));
145 static void connect_time_expired __P((void *));
146 static int plogin __P((char *, char *, char **, int *));
147 static void plogout __P((void));
148 static int null_login __P((int));
149 static int get_pap_passwd __P((char *));
150 static int have_pap_secret __P((void));
151 static int have_chap_secret __P((char *, char *, u_int32_t));
152 static int ip_addr_check __P((u_int32_t, struct wordlist *));
153 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
154 struct wordlist **, char *));
155 static void free_wordlist __P((struct wordlist *));
156 static void auth_script __P((char *));
157 static void set_allowed_addrs __P((int, struct wordlist *));
158 static int setupapfile __P((char **));
159 static int privgroup __P((char **));
162 * Authentication-related options.
164 option_t auth_options[] = {
165 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
166 "Require PAP authentication from peer", 1, &auth_required },
167 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
168 "Require PAP authentication from peer", 1, &auth_required },
169 { "refuse-pap", o_bool, &refuse_pap,
170 "Don't agree to auth to peer with PAP", 1 },
171 { "-pap", o_bool, &refuse_pap,
172 "Don't allow PAP authentication with peer", 1 },
173 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
174 "Require CHAP authentication from peer", 1, &auth_required },
175 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
176 "Require CHAP authentication from peer", 1, &auth_required },
177 { "refuse-chap", o_bool, &refuse_chap,
178 "Don't agree to auth to peer with CHAP", 1 },
179 { "-chap", o_bool, &refuse_chap,
180 "Don't allow CHAP authentication with peer", 1 },
181 { "name", o_string, our_name,
182 "Set local name for authentication",
183 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
184 { "user", o_string, user,
185 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
186 { "usehostname", o_bool, &usehostname,
187 "Must use hostname for authentication", 1 },
188 { "remotename", o_string, remote_name,
189 "Set remote name for authentication", OPT_STATIC, NULL, MAXNAMELEN },
190 { "auth", o_bool, &auth_required,
191 "Require authentication from peer", 1 },
192 { "noauth", o_bool, &auth_required,
193 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
194 { "login", o_bool, &uselogin,
195 "Use system password database for PAP", 1 },
196 { "papcrypt", o_bool, &cryptpap,
197 "PAP passwords are encrypted", 1 },
198 { "+ua", o_special, setupapfile,
199 "Get PAP user and password from file" },
200 { "privgroup", o_special, privgroup,
201 "Allow group members to use privileged options", OPT_PRIV },
206 * setupapfile - specifies UPAP info for authenticating with peer.
215 lcp_allowoptions[0].neg_upap = 1;
217 /* open user info file */
219 ufile = fopen(*argv, "r");
222 option_error("unable to open user login data file %s", *argv);
225 #if 0 /* check done by setting effective UID above */
226 if (!readable(fileno(ufile))) {
227 option_error("%s: access denied", *argv);
231 check_access(ufile, *argv);
234 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
235 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
236 option_error("unable to read user login data file %s", *argv);
241 /* get rid of newlines */
243 if (l > 0 && user[l-1] == '\n')
246 if (l > 0 && passwd[l-1] == '\n')
254 * privgroup - allow members of the group to have privileged access.
265 option_error("group %s is unknown", *argv);
268 for (i = 0; i < ngroups; ++i) {
269 if (groups[i] == g->gr_gid) {
279 * An Open on LCP has requested a change from Dead to Establish phase.
280 * Do what's necessary to bring the physical layer up.
289 * LCP has terminated the link; go to the Dead phase and take the
290 * physical layer down.
293 link_terminated(unit)
296 if (phase == PHASE_DEAD)
301 syslog(LOG_NOTICE, "Connection terminated.");
305 * LCP has gone down; it will either die or try to re-establish.
312 struct protent *protp;
315 auth_script(_PATH_AUTHDOWN);
318 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
319 if (!protp->enabled_flag)
321 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
322 (*protp->lowerdown)(unit);
323 if (protp->protocol < 0xC000 && protp->close != NULL)
324 (*protp->close)(unit, "LCP down");
328 if (phase != PHASE_DEAD)
329 phase = PHASE_TERMINATE;
333 * The link is established.
334 * Proceed to the Dead, Authenticate or Network phase as appropriate.
337 link_established(unit)
341 lcp_options *wo = &lcp_wantoptions[unit];
342 lcp_options *go = &lcp_gotoptions[unit];
343 lcp_options *ho = &lcp_hisoptions[unit];
345 struct protent *protp;
348 * Tell higher-level protocols that LCP is up.
350 for (i = 0; (protp = protocols[i]) != NULL; ++i)
351 if (protp->protocol != PPP_LCP && protp->enabled_flag
352 && protp->lowerup != NULL)
353 (*protp->lowerup)(unit);
355 if (auth_required && !(go->neg_chap || go->neg_upap)) {
357 * We wanted the peer to authenticate itself, and it refused:
358 * treat it as though it authenticated with PAP using a username
359 * of "" and a password of "". If that's not OK, boot it out.
361 if (!wo->neg_upap || !null_login(unit)) {
362 syslog(LOG_WARNING, "peer refused to authenticate");
363 lcp_close(unit, "peer refused to authenticate");
368 phase = PHASE_AUTHENTICATE;
371 ChapAuthPeer(unit, our_name, go->chap_mdtype);
373 } else if (go->neg_upap) {
378 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
379 auth |= CHAP_WITHPEER;
380 } else if (ho->neg_upap) {
381 if (passwd[0] == 0) {
382 passwd_from_file = 1;
383 if (!get_pap_passwd(passwd))
384 syslog(LOG_ERR, "No secret found for PAP login");
386 upap_authwithpeer(unit, user, passwd);
387 auth |= PAP_WITHPEER;
389 auth_pending[unit] = auth;
396 * Proceed to the network phase.
403 struct protent *protp;
404 lcp_options *go = &lcp_gotoptions[unit];
407 * If the peer had to authenticate, run the auth-up script now.
409 if ((go->neg_chap || go->neg_upap) && !did_authup) {
410 auth_script(_PATH_AUTHUP);
416 * If we negotiated callback, do it now.
419 phase = PHASE_CALLBACK;
420 (*cbcp_protent.open)(unit);
425 phase = PHASE_NETWORK;
428 set_filters(&pass_filter, &active_filter);
430 for (i = 0; (protp = protocols[i]) != NULL; ++i)
431 if (protp->protocol < 0xC000 && protp->enabled_flag
432 && protp->open != NULL) {
433 (*protp->open)(unit);
434 if (protp->protocol != PPP_CCP)
438 if (num_np_open == 0)
440 lcp_close(0, "No network protocols running");
444 * The peer has failed to authenticate himself using `protocol'.
447 auth_peer_fail(unit, protocol)
451 * Authentication failure: take the link down
453 lcp_close(unit, "Authentication failed");
457 * The peer has been successfully authenticated using `protocol'.
460 auth_peer_success(unit, protocol, name, namelen)
475 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
481 * Save the authenticated name of the peer for later.
483 if (namelen > sizeof(peer_authname) - 1)
484 namelen = sizeof(peer_authname) - 1;
485 BCOPY(name, peer_authname, namelen);
486 peer_authname[namelen] = 0;
487 script_setenv("PEERNAME", peer_authname);
490 * If there is no more authentication still to be done,
491 * proceed to the network (or callback) phase.
493 if ((auth_pending[unit] &= ~bit) == 0)
498 * We have failed to authenticate ourselves to the peer using `protocol'.
501 auth_withpeer_fail(unit, protocol)
504 if (passwd_from_file)
505 BZERO(passwd, MAXSECRETLEN);
507 * We've failed to authenticate ourselves to our peer.
508 * He'll probably take the link down, and there's not much
509 * we can do except wait for that.
514 * We have successfully authenticated ourselves with the peer using `protocol'.
517 auth_withpeer_success(unit, protocol)
527 if (passwd_from_file)
528 BZERO(passwd, MAXSECRETLEN);
532 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
538 * If there is no more authentication still being done,
539 * proceed to the network (or callback) phase.
541 if ((auth_pending[unit] &= ~bit) == 0)
547 * np_up - a network protocol has come up.
553 if (num_np_up == 0) {
555 * At this point we consider that the link has come up successfully.
559 if (idle_time_limit > 0)
560 TIMEOUT(check_idle, NULL, idle_time_limit);
563 * Set a timeout to close the connection once the maximum
564 * connect time has expired.
567 TIMEOUT(connect_time_expired, 0, maxconnect);
570 * Detach now, if the updetach option was given.
572 if (updetach && !nodetach)
579 * np_down - a network protocol has gone down.
585 if (--num_np_up == 0 && idle_time_limit > 0) {
586 UNTIMEOUT(check_idle, NULL);
591 * np_finished - a network protocol has finished using the link.
594 np_finished(unit, proto)
597 if (--num_np_open <= 0) {
598 /* no further use for the link: shut up shop. */
599 lcp_close(0, "No network protocols running");
604 * check_idle - check whether the link has been idle for long
605 * enough that we can shut it down.
611 struct ppp_idle idle;
614 if (!get_idle_time(0, &idle))
616 itime = MIN(idle.xmit_idle, idle.recv_idle);
617 if (itime >= idle_time_limit) {
618 /* link is idle: shut it down. */
619 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
620 lcp_close(0, "Link inactive");
622 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
627 * connect_time_expired - log a message and close the connection.
630 connect_time_expired(arg)
633 syslog(LOG_INFO, "Connect time expired");
634 lcp_close(0, "Connect time expired"); /* Close connection */
638 * auth_check_options - called to check authentication options.
643 lcp_options *wo = &lcp_wantoptions[0];
645 ipcp_options *ipwo = &ipcp_wantoptions[0];
648 /* Default our_name to hostname, and user to our_name */
649 if (our_name[0] == 0 || usehostname)
650 strlcpy(our_name, sizeof(our_name), hostname);
652 strlcpy(user, sizeof(user), our_name);
654 /* If authentication is required, ask peer for CHAP or PAP. */
656 if (!wo->neg_chap && !wo->neg_upap) {
666 * If we have a default route, require the peer to authenticate
667 * unless the noauth option was given.
669 if (!auth_required && !allow_any_ip && have_route_to(0))
673 * Check whether we have appropriate secrets to use
674 * to authenticate the peer.
676 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
677 if (!can_auth && wo->neg_chap) {
678 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
679 can_auth = have_chap_secret(remote_name, our_name, remote);
682 if (auth_required && !can_auth) {
683 option_error("peer authentication required but no suitable secret(s) found\n");
684 if (remote_name[0] == 0)
685 option_error("for authenticating any peer to us (%s)\n", our_name);
687 option_error("for authenticating peer %s to us (%s)\n",
688 remote_name, our_name);
694 * auth_reset - called when LCP is starting negotiations to recheck
695 * authentication options, i.e. whether we have appropriate secrets
696 * to use for authenticating ourselves and/or the peer.
702 lcp_options *go = &lcp_gotoptions[unit];
703 lcp_options *ao = &lcp_allowoptions[0];
704 ipcp_options *ipwo = &ipcp_wantoptions[0];
707 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
708 ao->neg_chap = !refuse_chap
709 && have_chap_secret(user, remote_name, (u_int32_t)0);
711 if (go->neg_upap && !uselogin && !have_pap_secret())
714 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
715 if (!have_chap_secret(remote_name, our_name, remote))
722 * check_passwd - Check the user name and passwd against the PAP secrets
723 * file. If requested, also check against the system password database,
724 * and login the user if OK.
727 * UPAP_AUTHNAK: Authentication failed.
728 * UPAP_AUTHACK: Authentication succeeded.
729 * In either case, msg points to an appropriate message.
732 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
744 struct wordlist *addrs;
746 ipcp_options *ipwo = &ipcp_wantoptions[unit];
747 char passwd[256], user[256];
748 char secret[MAXWORDLEN];
749 static int attempts = 0;
752 * Make copies of apasswd and auser, then null-terminate them.
754 BCOPY(apasswd, passwd, passwdlen);
755 passwd[passwdlen] = '\0';
756 BCOPY(auser, user, userlen);
757 user[userlen] = '\0';
761 * Open the file of pap secrets and scan for a suitable secret
762 * for authenticating this user.
764 filename = _PATH_UPAPFILE;
767 f = fopen(filename, "r");
769 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
773 check_access(f, filename);
774 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
775 if (scan_authfile(f, user, our_name, remote,
776 secret, &addrs, filename) < 0
777 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
778 && strcmp(crypt(passwd, secret), secret) != 0)) {
779 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
785 if (uselogin && ret == UPAP_AUTHACK) {
786 ret = plogin(user, passwd, msg, msglen);
787 if (ret == UPAP_AUTHNAK) {
788 syslog(LOG_WARNING, "PAP login failure for %s", user);
792 if (ret == UPAP_AUTHNAK) {
793 if (*msg == (char *) 0)
794 *msg = "Login incorrect";
795 *msglen = strlen(*msg);
797 * Frustrate passwd stealer programs.
798 * Allow 10 tries, but start backing off after 3 (stolen from login).
799 * On 10'th, drop the connection.
801 if (attempts++ >= 10) {
802 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
803 attempts, devnam, user);
807 sleep((u_int) (attempts - 3) * 5);
809 free_wordlist(addrs);
812 attempts = 0; /* Reset count */
813 if (*msg == (char *) 0)
815 *msglen = strlen(*msg);
816 set_allowed_addrs(unit, addrs);
819 BZERO(passwd, sizeof(passwd));
820 BZERO(secret, sizeof(secret));
826 * This function is needed for PAM.
830 static char *PAM_username = "";
831 static char *PAM_password = "";
833 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
834 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
836 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
839 static int pam_conv (int num_msg,
840 const struct pam_message **msg,
841 struct pam_response **resp,
844 int count = 0, replies = 0;
845 struct pam_response *reply = NULL;
848 for (count = 0; count < num_msg; count++)
850 size += sizeof (struct pam_response);
851 reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
855 switch (msg[count]->msg_style)
857 case PAM_PROMPT_ECHO_ON:
858 reply[replies].resp_retcode = PAM_SUCCESS;
859 reply[replies++].resp = strdup(PAM_username); /* never NULL */
862 case PAM_PROMPT_ECHO_OFF:
863 reply[replies].resp_retcode = PAM_SUCCESS;
864 reply[replies++].resp = strdup(PAM_password); /* never NULL */
868 reply[replies].resp_retcode = PAM_SUCCESS;
869 reply[replies++].resp = NULL;
889 * plogin - Check the user name and password against the system
890 * password database, and login the user if OK.
893 * UPAP_AUTHNAK: Login failed.
894 * UPAP_AUTHACK: Login succeeded.
895 * In either case, msg points to an appropriate message.
899 plogin(user, passwd, msg, msglen)
908 struct pam_conv pam_conversation;
912 * Fill the pam_conversion structure
914 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
915 pam_conversation.conv = &pam_conv;
917 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
919 if (pam_error != PAM_SUCCESS) {
920 *msg = MY_PAM_STRERROR (pam_error);
924 * Define the fields for the credintial validation
926 (void) pam_set_item (pamh, PAM_TTY, devnam);
928 PAM_password = passwd;
932 pam_error = pam_authenticate (pamh, PAM_SILENT);
933 if (pam_error == PAM_SUCCESS) {
934 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
936 /* start a session for this user. Session closed when link ends. */
937 if (pam_error == PAM_SUCCESS)
938 (void) pam_open_session (pamh, PAM_SILENT);
941 *msg = MY_PAM_STRERROR (pam_error);
948 (void) pam_end (pamh, pam_error);
950 if (pam_error != PAM_SUCCESS)
953 * Use the non-PAM methods directly
955 #else /* #ifdef USE_PAM */
962 struct spwd *getspnam();
968 return (UPAP_AUTHNAK);
972 spwd = getspnam(user);
975 /* check the age of the password entry */
976 long now = time(NULL) / 86400L;
978 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
979 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
980 && spwd->sp_lstchg >= 0
981 && now >= spwd->sp_lstchg + spwd->sp_max)) {
982 syslog(LOG_WARNING, "Password for %s has expired", user);
983 return (UPAP_AUTHNAK);
985 pw->pw_passwd = spwd->sp_pwdp;
990 * If no passwd, don't let them login.
992 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
993 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
994 return (UPAP_AUTHNAK);
996 /* These functions are not enabled for PAM. The reason for this is that */
997 /* there is not necessarily a "passwd" entry for this user. That is */
998 /* real purpose of 'PAM' -- to virtualize the account data from the */
999 /* application. If you want to do the same thing, write the entry in */
1000 /* the 'session' hook. */
1003 * Write a wtmp entry for this user.
1007 if (strncmp(tty, "/dev/", 5) == 0)
1009 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1011 #if defined(_PATH_LASTLOG)
1016 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1017 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1018 memset((void *)&ll, 0, sizeof(ll));
1019 (void)time(&ll.ll_time);
1020 (void)strlcpy(ll.ll_line, sizeof(ll.ll_line), tty);
1021 (void)write(fd, (char *)&ll, sizeof(ll));
1027 #endif /* #ifdef USE_PAM */
1029 syslog(LOG_INFO, "user %s logged in", user);
1032 return (UPAP_AUTHACK);
1036 * plogout - Logout the user.
1042 struct pam_conv pam_conversation;
1046 * Fill the pam_conversion structure. The PAM specification states that the
1047 * session must be able to be closed by a totally different handle from which
1048 * it was created. Hold the PAM group to their own specification!
1050 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
1051 pam_conversation.conv = &pam_conv;
1053 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
1054 if (pam_error == PAM_SUCCESS) {
1055 (void) pam_set_item (pamh, PAM_TTY, devnam);
1056 (void) pam_close_session (pamh, PAM_SILENT);
1057 (void) pam_end (pamh, PAM_SUCCESS);
1064 if (strncmp(tty, "/dev/", 5) == 0)
1066 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1074 * null_login - Check if a username of "" and a password of "" are
1075 * acceptable, and iff so, set the list of acceptable IP addresses
1085 struct wordlist *addrs;
1086 char secret[MAXWORDLEN];
1089 * Open the file of pap secrets and scan for a suitable secret.
1090 * We don't accept a wildcard client.
1092 filename = _PATH_UPAPFILE;
1094 f = fopen(filename, "r");
1097 check_access(f, filename);
1099 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
1100 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1101 BZERO(secret, sizeof(secret));
1104 set_allowed_addrs(unit, addrs);
1106 free_wordlist(addrs);
1114 * get_pap_passwd - get a password for authenticating ourselves with
1115 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1117 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1120 get_pap_passwd(passwd)
1126 struct wordlist *addrs;
1127 char secret[MAXWORDLEN];
1129 filename = _PATH_UPAPFILE;
1131 f = fopen(filename, "r");
1134 check_access(f, filename);
1135 ret = scan_authfile(f, user,
1136 remote_name[0]? remote_name: NULL,
1137 (u_int32_t)0, secret, NULL, filename);
1142 strlcpy(passwd, MAXSECRETLEN, secret);
1143 BZERO(secret, sizeof(secret));
1149 * have_pap_secret - check whether we have a PAP file with any
1150 * secrets that we could possibly use for authenticating the peer.
1158 ipcp_options *ipwo = &ipcp_wantoptions[0];
1161 filename = _PATH_UPAPFILE;
1162 f = fopen(filename, "r");
1166 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
1167 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
1177 * have_chap_secret - check whether we have a CHAP file with a
1178 * secret that we could possibly use for authenticating `client'
1179 * on `server'. Either can be the null string, meaning we don't
1180 * know the identity yet.
1183 have_chap_secret(client, server, remote)
1192 filename = _PATH_CHAPFILE;
1193 f = fopen(filename, "r");
1199 else if (server[0] == 0)
1202 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1212 * get_secret - open the CHAP secret file and return the secret
1213 * for authenticating the given client on the given server.
1214 * (We could be either client or server).
1217 get_secret(unit, client, server, secret, secret_len, save_addrs)
1228 struct wordlist *addrs;
1229 char secbuf[MAXWORDLEN];
1231 filename = _PATH_CHAPFILE;
1235 f = fopen(filename, "r");
1237 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1240 check_access(f, filename);
1242 ret = scan_authfile(f, client, server, (u_int32_t)0,
1243 secbuf, &addrs, filename);
1249 set_allowed_addrs(unit, addrs);
1251 len = strlen(secbuf);
1252 if (len > MAXSECRETLEN) {
1253 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1256 BCOPY(secbuf, secret, len);
1257 BZERO(secbuf, sizeof(secbuf));
1264 * set_allowed_addrs() - set the list of allowed addresses.
1267 set_allowed_addrs(unit, addrs)
1269 struct wordlist *addrs;
1271 if (addresses[unit] != NULL)
1272 free_wordlist(addresses[unit]);
1273 addresses[unit] = addrs;
1276 * If there's only one authorized address we might as well
1277 * ask our peer for that one right away
1279 if (addrs != NULL && addrs->next == NULL) {
1280 char *p = addrs->word;
1281 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1285 if (*p != '!' && *p != '-' && *p != '*' && strchr(p, '/') == NULL) {
1286 hp = gethostbyname(p);
1287 if (hp != NULL && hp->h_addrtype == AF_INET)
1288 a = *(u_int32_t *)hp->h_addr;
1291 if (a != (u_int32_t) -1)
1298 * auth_ip_addr - check whether the peer is authorized to use
1299 * a given IP address. Returns 1 if authorized, 0 otherwise.
1302 auth_ip_addr(unit, addr)
1307 if (addresses[unit] == NULL) {
1309 return 0; /* no addresses authorized */
1310 return allow_any_ip || !have_route_to(addr);
1312 return ip_addr_check(addr, addresses[unit]);
1316 ip_addr_check(addr, addrs)
1318 struct wordlist *addrs;
1320 u_int32_t a, mask, ah;
1322 char *ptr_word, *ptr_mask;
1326 /* don't allow loopback or multicast address */
1327 if (bad_ip_adrs(addr))
1331 return 0; /* no addresses authorized */
1333 for (; addrs != NULL; addrs = addrs->next) {
1334 /* "-" means no addresses authorized, "*" means any address allowed */
1335 ptr_word = addrs->word;
1336 if (strcmp(ptr_word, "-") == 0)
1338 if (strcmp(ptr_word, "*") == 0)
1342 if (*ptr_word == '!') {
1347 mask = ~ (u_int32_t) 0;
1348 ptr_mask = strchr (ptr_word, '/');
1349 if (ptr_mask != NULL) {
1352 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1353 if (bit_count <= 0 || bit_count > 32) {
1354 syslog (LOG_WARNING,
1355 "invalid address length %s in auth. address list",
1360 mask <<= 32 - bit_count;
1363 hp = gethostbyname(ptr_word);
1364 if (hp != NULL && hp->h_addrtype == AF_INET) {
1365 a = *(u_int32_t *)hp->h_addr;
1367 np = getnetbyname (ptr_word);
1368 if (np != NULL && np->n_addrtype == AF_INET) {
1369 a = htonl (*(u_int32_t *)np->n_net);
1370 if (ptr_mask == NULL) {
1371 /* calculate appropriate mask for net */
1374 mask = IN_CLASSA_NET;
1375 else if (IN_CLASSB(ah))
1376 mask = IN_CLASSB_NET;
1377 else if (IN_CLASSC(ah))
1378 mask = IN_CLASSC_NET;
1381 a = inet_addr (ptr_word);
1385 if (ptr_mask != NULL)
1388 if (a == (u_int32_t)-1L)
1389 syslog (LOG_WARNING,
1390 "unknown host %s in auth. address list",
1393 /* Here a and addr are in network byte order,
1394 and mask is in host order. */
1395 if (((addr ^ a) & htonl(mask)) == 0)
1398 return 0; /* not in list => can't have it */
1402 * bad_ip_adrs - return 1 if the IP address is one we don't want
1403 * to use, such as an address in the loopback net or a multicast address.
1404 * addr is in network byte order.
1411 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1412 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1416 * check_access - complain if a secret file has too-liberal permissions.
1419 check_access(f, filename)
1425 if (fstat(fileno(f), &sbuf) < 0) {
1426 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1427 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1428 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1434 * scan_authfile - Scan an authorization file for a secret suitable
1435 * for authenticating `client' on `server'. The return value is -1
1436 * if no secret is found, otherwise >= 0. The return value has
1437 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1438 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1439 * Any following words on the line (i.e. address authorization
1440 * info) are placed in a wordlist and returned in *addrs.
1441 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1444 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1450 struct wordlist **addrs;
1454 int got_flag, best_flag;
1456 struct wordlist *ap, *addr_list, *alist, *alast;
1457 char word[MAXWORDLEN];
1458 char atfile[MAXWORDLEN];
1459 char lsecret[MAXWORDLEN];
1464 if (!getword(f, word, &newline, filename))
1465 return -1; /* file is empty??? */
1470 * Skip until we find a word at the start of a line.
1472 while (!newline && getword(f, word, &newline, filename))
1475 break; /* got to end of file */
1478 * Got a client - check if it's a match or a wildcard.
1481 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1486 got_flag = NONWILD_CLIENT;
1489 * Now get a server and check if it matches.
1491 if (!getword(f, word, &newline, filename))
1495 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1498 got_flag |= NONWILD_SERVER;
1501 * Got some sort of a match - see if it's better than what
1504 if (got_flag <= best_flag)
1510 if (!getword(f, word, &newline, filename))
1516 * Special syntax: @filename means read secret from file.
1518 if (word[0] == '@') {
1519 strlcpy(atfile, sizeof(atfile), word+1);
1520 if ((sf = fopen(atfile, "r")) == NULL) {
1521 warn("can't open indirect secret file %s", atfile);
1524 check_access(sf, atfile);
1525 if (!getword(sf, word, &xxx, atfile)) {
1526 warn("no secret in indirect secret file %s", atfile);
1533 strlcpy(lsecret, sizeof(lsecret), word);
1536 * Now read address authorization info and make a wordlist.
1538 alist = alast = NULL;
1540 if (!getword(f, word, &newline, filename) || newline)
1542 ap = (struct wordlist *) malloc(sizeof(struct wordlist));
1544 novm("authorized addresses");
1546 ap->word = strdup(word);
1547 if (ap->word == NULL)
1548 novm("authorized address");
1557 * Check if the given IP address is allowed by the wordlist.
1558 * XXX accepts this entry even if it has no allowed IP addresses
1559 * if they didn't specify a remote IP address. XXX
1561 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1562 free_wordlist(alist);
1567 * This is the best so far; remember it.
1569 best_flag = got_flag;
1571 free_wordlist(addr_list);
1574 strlcpy(secret, MAXWORDLEN, lsecret);
1582 else if (addr_list != NULL)
1583 free_wordlist(addr_list);
1589 * free_wordlist - release memory allocated for a wordlist.
1593 struct wordlist *wp;
1595 struct wordlist *next;
1597 while (wp != NULL) {
1605 * auth_script - execute a script with arguments
1606 * interface-name peer-name real-user tty speed
1618 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1619 user_name = pw->pw_name;
1621 sprintf(struid, "%d", getuid());
1624 sprintf(strspeed, "%d", baud_rate);
1628 argv[2] = peer_authname;
1629 argv[3] = user_name;
1634 authup_pid = run_program(script, argv, 0, NULL, NULL);