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.23 1996/05/28 00:47:01 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
64 #include <shadow/pwauth.h>
66 #define PW_PPP PW_LOGIN
76 #include "pathnames.h"
78 #if defined(sun) && defined(sparc)
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 /* Records which authentication operations haven't completed yet. */
98 static int auth_pending[NUM_PPP];
100 /* Set if we have successfully called login() */
101 static int logged_in;
103 /* List of addresses which the peer may use. */
104 static struct wordlist *addresses[NUM_PPP];
106 /* Number of network protocols which we have opened. */
107 static int num_np_open;
109 /* Number of network protocols which have come up. */
110 static int num_np_up;
112 /* Bits in auth_pending[] */
113 #define UPAP_WITHPEER 1
115 #define CHAP_WITHPEER 4
118 /* Prototypes for procedures local to this file. */
120 static void network_phase __P((int));
121 static void check_idle __P((caddr_t));
122 static int login __P((char *, char *, char **, int *));
123 static void logout __P((void));
124 static int null_login __P((int));
125 static int get_upap_passwd __P((void));
126 static int have_upap_secret __P((void));
127 static int have_chap_secret __P((char *, char *, u_int32_t));
128 static int ip_addr_check __P((u_int32_t, struct wordlist *));
129 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
130 struct wordlist **, char *));
131 static void free_wordlist __P((struct wordlist *));
134 * An Open on LCP has requested a change from Dead to Establish phase.
135 * Do what's necessary to bring the physical layer up.
144 * LCP has terminated the link; go to the Dead phase and take the
145 * physical layer down.
148 link_terminated(unit)
151 if (phase == PHASE_DEAD)
156 syslog(LOG_NOTICE, "Connection terminated.");
160 * LCP has gone down; it will either die or try to re-establish.
167 struct protent *protp;
169 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
170 if (!protp->enabled_flag)
172 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
173 (*protp->lowerdown)(unit);
174 if (protp->protocol < 0xC000 && protp->close != NULL)
175 (*protp->close)(unit, "LCP down");
179 phase = PHASE_TERMINATE;
183 * The link is established.
184 * Proceed to the Dead, Authenticate or Network phase as appropriate.
187 link_established(unit)
191 lcp_options *wo = &lcp_wantoptions[unit];
192 lcp_options *go = &lcp_gotoptions[unit];
193 lcp_options *ho = &lcp_hisoptions[unit];
195 struct protent *protp;
198 * Tell higher-level protocols that LCP is up.
200 for (i = 0; (protp = protocols[i]) != NULL; ++i)
201 if (protp->protocol != PPP_LCP && protp->enabled_flag
202 && protp->lowerup != NULL)
203 (*protp->lowerup)(unit);
205 if (auth_required && !(go->neg_chap || go->neg_upap)) {
207 * We wanted the peer to authenticate itself, and it refused:
208 * treat it as though it authenticated with PAP using a username
209 * of "" and a password of "". If that's not OK, boot it out.
211 if (!wo->neg_upap || !null_login(unit)) {
212 syslog(LOG_WARNING, "peer refused to authenticate");
213 lcp_close(unit, "peer refused to authenticate");
214 phase = PHASE_TERMINATE;
219 phase = PHASE_AUTHENTICATE;
222 ChapAuthPeer(unit, our_name, go->chap_mdtype);
224 } else if (go->neg_upap) {
229 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
230 auth |= CHAP_WITHPEER;
231 } else if (ho->neg_upap) {
232 upap_authwithpeer(unit, user, passwd);
233 auth |= UPAP_WITHPEER;
235 auth_pending[unit] = auth;
242 * Proceed to the network phase.
249 struct protent *protp;
251 phase = PHASE_NETWORK;
254 set_filters(&pass_filter, &active_filter);
256 for (i = 0; (protp = protocols[i]) != NULL; ++i)
257 if (protp->protocol < 0xC000 && protp->enabled_flag
258 && protp->open != NULL) {
259 (*protp->open)(unit);
260 if (protp->protocol != PPP_CCP)
266 * The peer has failed to authenticate himself using `protocol'.
269 auth_peer_fail(unit, protocol)
273 * Authentication failure: take the link down
275 lcp_close(unit, "Authentication failed");
276 phase = PHASE_TERMINATE;
280 * The peer has been successfully authenticated using `protocol'.
283 auth_peer_success(unit, protocol)
296 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
302 * If there is no more authentication still to be done,
303 * proceed to the network phase.
305 if ((auth_pending[unit] &= ~bit) == 0)
310 * We have failed to authenticate ourselves to the peer using `protocol'.
313 auth_withpeer_fail(unit, protocol)
317 * We've failed to authenticate ourselves to our peer.
318 * He'll probably take the link down, and there's not much
319 * we can do except wait for that.
324 * We have successfully authenticated ourselves with the peer using `protocol'.
327 auth_withpeer_success(unit, protocol)
340 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
346 * If there is no more authentication still being done,
347 * proceed to the network phase.
349 if ((auth_pending[unit] &= ~bit) == 0)
355 * np_up - a network protocol has come up.
361 if (num_np_up == 0 && idle_time_limit > 0) {
362 TIMEOUT(check_idle, NULL, idle_time_limit);
368 * np_down - a network protocol has gone down.
374 if (--num_np_up == 0 && idle_time_limit > 0) {
375 UNTIMEOUT(check_idle, NULL);
380 * np_finished - a network protocol has finished using the link.
383 np_finished(unit, proto)
386 if (--num_np_open <= 0) {
387 /* no further use for the link: shut up shop. */
388 lcp_close(0, "No network protocols running");
393 * check_idle - check whether the link has been idle for long
394 * enough that we can shut it down.
400 struct ppp_idle idle;
403 if (!get_idle_time(0, &idle))
405 itime = MIN(idle.xmit_idle, idle.recv_idle);
406 if (itime >= idle_time_limit) {
407 /* link is idle: shut it down. */
408 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
409 lcp_close(0, "Link inactive");
411 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
416 * auth_check_options - called to check authentication options.
421 lcp_options *wo = &lcp_wantoptions[0];
422 lcp_options *ao = &lcp_allowoptions[0];
423 ipcp_options *ipwo = &ipcp_wantoptions[0];
426 /* Default our_name to hostname, and user to our_name */
427 if (our_name[0] == 0 || usehostname)
428 strcpy(our_name, hostname);
430 strcpy(user, our_name);
432 /* If authentication is required, ask peer for CHAP or PAP. */
433 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
439 * Check whether we have appropriate secrets to use
440 * to authenticate ourselves and/or the peer.
442 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
444 if (wo->neg_upap && !uselogin && !have_upap_secret())
446 if (ao->neg_chap && !have_chap_secret(our_name, remote_name, (u_int32_t)0))
449 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
450 if (!have_chap_secret(remote_name, our_name, remote))
454 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
456 pppd: peer authentication required but no suitable secret(s) found\n");
464 * check_passwd - Check the user name and passwd against the PAP secrets
465 * file. If requested, also check against the system password database,
466 * and login the user if OK.
469 * UPAP_AUTHNAK: Authentication failed.
470 * UPAP_AUTHACK: Authentication succeeded.
471 * In either case, msg points to an appropriate message.
474 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
486 struct wordlist *addrs;
488 ipcp_options *ipwo = &ipcp_wantoptions[unit];
489 char passwd[256], user[256];
490 char secret[MAXWORDLEN];
491 static int attempts = 0;
494 * Make copies of apasswd and auser, then null-terminate them.
496 BCOPY(apasswd, passwd, passwdlen);
497 passwd[passwdlen] = '\0';
498 BCOPY(auser, user, userlen);
499 user[userlen] = '\0';
502 * Open the file of upap secrets and scan for a suitable secret
503 * for authenticating this user.
505 filename = _PATH_UPAPFILE;
508 f = fopen(filename, "r");
511 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
516 check_access(f, filename);
517 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
518 if (scan_authfile(f, user, our_name, remote,
519 secret, &addrs, filename) < 0
520 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
521 && strcmp(crypt(passwd, secret), secret) != 0)) {
522 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
528 if (uselogin && ret == UPAP_AUTHACK) {
529 ret = login(user, passwd, msg, msglen);
530 if (ret == UPAP_AUTHNAK) {
531 syslog(LOG_WARNING, "PAP login failure for %s", user);
535 if (ret == UPAP_AUTHNAK) {
536 *msg = "Login incorrect";
537 *msglen = strlen(*msg);
539 * Frustrate passwd stealer programs.
540 * Allow 10 tries, but start backing off after 3 (stolen from login).
541 * On 10'th, drop the connection.
543 if (attempts++ >= 10) {
544 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
545 attempts, devnam, user);
549 sleep((u_int) (attempts - 3) * 5);
551 free_wordlist(addrs);
554 attempts = 0; /* Reset count */
556 *msglen = strlen(*msg);
557 if (addresses[unit] != NULL)
558 free_wordlist(addresses[unit]);
559 addresses[unit] = addrs;
567 * This function was lifted from the shadow-3.3.2 version by John Haugh II.
568 * It is included because the function was not in the standard libshadow
569 * library. If it is included in the library then I can remove it from here.
572 #define DAY (24L*3600L)
574 * isexpired - determine if account is expired yet
576 * isexpired calculates the expiration date based on the
577 * password expiration criteria.
588 clock = time ((time_t *) 0) / DAY;
591 * Quick and easy - there is an expired account field
592 * along with an inactive account field. Do the expired
593 * one first since it is worse.
596 if (sp->sp_expire > 0 && sp->sp_expire < clock)
599 if (sp->sp_inact > 0 && sp->sp_lstchg > 0 && sp->sp_max > 0 &&
600 sp->sp_inact + sp->sp_lstchg + sp->sp_max < clock)
604 * The last and max fields must be present for an account
605 * to have an expired password. A maximum of >10000 days
606 * is considered to be infinite.
609 if (sp->sp_lstchg == -1 ||
610 sp->sp_max == -1 || sp->sp_max >= 10000L)
614 * Calculate today's day and the day on which the password
615 * is going to expire. If that date has already passed,
616 * the password has expired.
619 if (sp->sp_lstchg + sp->sp_max < clock)
627 * login - Check the user name and password against the system
628 * password database, and login the user if OK.
631 * UPAP_AUTHNAK: Login failed.
632 * UPAP_AUTHACK: Login succeeded.
633 * In either case, msg points to an appropriate message.
636 login(user, passwd, msg, msglen)
648 struct spwd *getspnam();
651 if ((pw = getpwnam(user)) == NULL) {
652 return (UPAP_AUTHNAK);
656 if ((spwd = getspnam(user)) == NULL) {
659 pw->pw_passwd = spwd->sp_pwdp;
664 * XXX If no passwd, let them login without one.
666 if (pw->pw_passwd == '\0') {
667 return (UPAP_AUTHACK);
672 if (pw->pw_passwd[0] == '@') {
673 if (pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL)) {
674 return (UPAP_AUTHNAK);
677 epasswd = pw_encrypt(passwd, pw->pw_passwd);
678 if (strcmp(epasswd, pw->pw_passwd)) {
679 return (UPAP_AUTHNAK);
682 /* check the age of the password entry */
683 if (spwd && (isexpired (pw, spwd) != 0)) {
684 return (UPAP_AUTHNAK);
688 epasswd = crypt(passwd, pw->pw_passwd);
689 if (strcmp(epasswd, pw->pw_passwd)) {
690 return (UPAP_AUTHNAK);
694 syslog(LOG_INFO, "user %s logged in", user);
697 * Write a wtmp entry for this user.
700 if (strncmp(tty, "/dev/", 5) == 0)
702 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
705 return (UPAP_AUTHACK);
709 * logout - Logout the user.
717 if (strncmp(tty, "/dev/", 5) == 0)
719 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
725 * null_login - Check if a username of "" and a password of "" are
726 * acceptable, and iff so, set the list of acceptable IP addresses
736 struct wordlist *addrs;
737 char secret[MAXWORDLEN];
740 * Open the file of upap secrets and scan for a suitable secret.
741 * We don't accept a wildcard client.
743 filename = _PATH_UPAPFILE;
745 f = fopen(filename, "r");
748 check_access(f, filename);
750 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
751 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
754 if (addresses[unit] != NULL)
755 free_wordlist(addresses[unit]);
756 addresses[unit] = addrs;
765 * get_upap_passwd - get a password for authenticating ourselves with
766 * our peer using PAP. Returns 1 on success, 0 if no suitable password
774 struct wordlist *addrs;
775 char secret[MAXWORDLEN];
777 filename = _PATH_UPAPFILE;
779 f = fopen(filename, "r");
782 check_access(f, filename);
783 if (scan_authfile(f, user, remote_name, (u_int32_t)0,
784 secret, NULL, filename) < 0)
786 strncpy(passwd, secret, MAXSECRETLEN);
787 passwd[MAXSECRETLEN-1] = 0;
793 * have_upap_secret - check whether we have a PAP file with any
794 * secrets that we could possibly use for authenticating the peer.
802 ipcp_options *ipwo = &ipcp_wantoptions[0];
805 filename = _PATH_UPAPFILE;
806 f = fopen(filename, "r");
810 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
811 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
821 * have_chap_secret - check whether we have a CHAP file with a
822 * secret that we could possibly use for authenticating `client'
823 * on `server'. Either can be the null string, meaning we don't
824 * know the identity yet.
827 have_chap_secret(client, server, remote)
836 filename = _PATH_CHAPFILE;
837 f = fopen(filename, "r");
843 else if (server[0] == 0)
846 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
856 * get_secret - open the CHAP secret file and return the secret
857 * for authenticating the given client on the given server.
858 * (We could be either client or server).
861 get_secret(unit, client, server, secret, secret_len, save_addrs)
872 struct wordlist *addrs;
873 char secbuf[MAXWORDLEN];
875 filename = _PATH_CHAPFILE;
879 f = fopen(filename, "r");
881 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
884 check_access(f, filename);
886 ret = scan_authfile(f, client, server, (u_int32_t)0,
887 secbuf, &addrs, filename);
893 if (addresses[unit] != NULL)
894 free_wordlist(addresses[unit]);
895 addresses[unit] = addrs;
898 len = strlen(secbuf);
899 if (len > MAXSECRETLEN) {
900 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
903 BCOPY(secbuf, secret, len);
910 * auth_ip_addr - check whether the peer is authorized to use
911 * a given IP address. Returns 1 if authorized, 0 otherwise.
914 auth_ip_addr(unit, addr)
918 return ip_addr_check(addr, addresses[unit]);
922 ip_addr_check(addr, addrs)
924 struct wordlist *addrs;
926 u_int32_t a, mask, ah;
928 char *ptr_word, *ptr_mask;
932 /* don't allow loopback or multicast address */
933 if (bad_ip_adrs(addr))
937 return 1; /* no restriction */
939 for (; addrs != NULL; addrs = addrs->next) {
940 /* "-" means no addresses authorized */
941 ptr_word = addrs->word;
942 if (strcmp(ptr_word, "-") == 0)
946 if (*ptr_word == '!') {
951 mask = ~ (u_int32_t) 0;
952 ptr_mask = strchr (ptr_word, '/');
953 if (ptr_mask != NULL) {
956 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
957 if (bit_count <= 0 || bit_count > 32) {
959 "invalid address length %s in auth. address list",
964 mask <<= 32 - bit_count;
967 hp = gethostbyname(ptr_word);
968 if (hp != NULL && hp->h_addrtype == AF_INET) {
969 a = *(u_int32_t *)hp->h_addr;
970 mask = ~ (u_int32_t) 0; /* are we sure we want this? */
972 np = getnetbyname (ptr_word);
973 if (np != NULL && np->n_addrtype == AF_INET)
974 a = htonl (*(u_int32_t *)np->n_net);
976 a = inet_addr (ptr_word);
977 if (ptr_mask == NULL) {
978 /* calculate appropriate mask for net */
981 mask = IN_CLASSA_NET;
982 else if (IN_CLASSB(ah))
983 mask = IN_CLASSB_NET;
984 else if (IN_CLASSC(ah))
985 mask = IN_CLASSC_NET;
989 if (ptr_mask != NULL)
994 "unknown host %s in auth. address list",
997 if (((addr ^ a) & htonl(mask)) == 0)
1000 return 0; /* not in list => can't have it */
1004 * bad_ip_adrs - return 1 if the IP address is one we don't want
1005 * to use, such as an address in the loopback net or a multicast address.
1006 * addr is in network byte order.
1013 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1014 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1018 * check_access - complain if a secret file has too-liberal permissions.
1021 check_access(f, filename)
1027 if (fstat(fileno(f), &sbuf) < 0) {
1028 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1029 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1030 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1036 * scan_authfile - Scan an authorization file for a secret suitable
1037 * for authenticating `client' on `server'. The return value is -1
1038 * if no secret is found, otherwise >= 0. The return value has
1039 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1040 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1041 * Any following words on the line (i.e. address authorization
1042 * info) are placed in a wordlist and returned in *addrs.
1045 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1051 struct wordlist **addrs;
1055 int got_flag, best_flag;
1057 struct wordlist *ap, *addr_list, *alist, *alast;
1058 char word[MAXWORDLEN];
1059 char atfile[MAXWORDLEN];
1060 char lsecret[MAXWORDLEN];
1065 if (!getword(f, word, &newline, filename))
1066 return -1; /* file is empty??? */
1071 * Skip until we find a word at the start of a line.
1073 while (!newline && getword(f, word, &newline, filename))
1076 break; /* got to end of file */
1079 * Got a client - check if it's a match or a wildcard.
1082 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1087 got_flag = NONWILD_CLIENT;
1090 * Now get a server and check if it matches.
1092 if (!getword(f, word, &newline, filename))
1096 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1099 got_flag |= NONWILD_SERVER;
1102 * Got some sort of a match - see if it's better than what
1105 if (got_flag <= best_flag)
1111 if (!getword(f, word, &newline, filename))
1117 * Special syntax: @filename means read secret from file.
1119 if (word[0] == '@') {
1120 strcpy(atfile, word+1);
1121 if ((sf = fopen(atfile, "r")) == NULL) {
1122 syslog(LOG_WARNING, "can't open indirect secret file %s",
1126 check_access(sf, atfile);
1127 if (!getword(sf, word, &xxx, atfile)) {
1128 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1136 strcpy(lsecret, word);
1139 * Now read address authorization info and make a wordlist.
1141 alist = alast = NULL;
1143 if (!getword(f, word, &newline, filename) || newline)
1145 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1148 novm("authorized addresses");
1150 strcpy(ap->word, word);
1159 * Check if the given IP address is allowed by the wordlist.
1161 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1162 free_wordlist(alist);
1167 * This is the best so far; remember it.
1169 best_flag = got_flag;
1171 free_wordlist(addr_list);
1174 strcpy(secret, lsecret);
1182 else if (addr_list != NULL)
1183 free_wordlist(addr_list);
1189 * free_wordlist - release memory allocated for a wordlist.
1193 struct wordlist *wp;
1195 struct wordlist *next;
1197 while (wp != NULL) {