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.22 1996/04/04 03:31:27 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
56 #include <shadow/pwauth.h>
58 #define PW_PPP PW_LOGIN
68 #include "pathnames.h"
70 #if defined(sun) && defined(sparc)
74 /* Used for storing a sequence of words. Usually malloced. */
76 struct wordlist *next;
80 /* Bits in scan_authfile return value */
81 #define NONWILD_SERVER 1
82 #define NONWILD_CLIENT 2
84 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
89 /* Records which authentication operations haven't completed yet. */
90 static int auth_pending[NUM_PPP];
92 /* Set if we have successfully called login() */
95 /* List of addresses which the peer may use. */
96 static struct wordlist *addresses[NUM_PPP];
98 /* Number of network protocols which we have opened. */
99 static int num_np_open;
101 /* Number of network protocols which have come up. */
102 static int num_np_up;
104 /* Bits in auth_pending[] */
105 #define UPAP_WITHPEER 1
107 #define CHAP_WITHPEER 4
110 /* Prototypes for procedures local to this file. */
112 static void network_phase __P((int));
113 static void check_idle __P((caddr_t));
114 static int login __P((char *, char *, char **, int *));
115 static void logout __P((void));
116 static int null_login __P((int));
117 static int get_upap_passwd __P((void));
118 static int have_upap_secret __P((void));
119 static int have_chap_secret __P((char *, char *, u_int32_t));
120 static int ip_addr_check __P((u_int32_t, struct wordlist *));
121 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
122 struct wordlist **, char *));
123 static void free_wordlist __P((struct wordlist *));
126 * An Open on LCP has requested a change from Dead to Establish phase.
127 * Do what's necessary to bring the physical layer up.
136 * LCP has terminated the link; go to the Dead phase and take the
137 * physical layer down.
140 link_terminated(unit)
143 if (phase == PHASE_DEAD)
148 syslog(LOG_NOTICE, "Connection terminated.");
152 * LCP has gone down; it will either die or try to re-establish.
159 struct protent *protp;
161 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
162 if (!protp->enabled_flag)
164 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
165 (*protp->lowerdown)(unit);
166 if (protp->protocol < 0xC000 && protp->close != NULL)
167 (*protp->close)(unit, "LCP down");
171 phase = PHASE_TERMINATE;
175 * The link is established.
176 * Proceed to the Dead, Authenticate or Network phase as appropriate.
179 link_established(unit)
183 lcp_options *wo = &lcp_wantoptions[unit];
184 lcp_options *go = &lcp_gotoptions[unit];
185 lcp_options *ho = &lcp_hisoptions[unit];
187 struct protent *protp;
190 * Tell higher-level protocols that LCP is up.
192 for (i = 0; (protp = protocols[i]) != NULL; ++i)
193 if (protp->protocol != PPP_LCP && protp->enabled_flag
194 && protp->lowerup != NULL)
195 (*protp->lowerup)(unit);
197 if (auth_required && !(go->neg_chap || go->neg_upap)) {
199 * We wanted the peer to authenticate itself, and it refused:
200 * treat it as though it authenticated with PAP using a username
201 * of "" and a password of "". If that's not OK, boot it out.
203 if (!wo->neg_upap || !null_login(unit)) {
204 syslog(LOG_WARNING, "peer refused to authenticate");
205 lcp_close(unit, "peer refused to authenticate");
206 phase = PHASE_TERMINATE;
211 phase = PHASE_AUTHENTICATE;
214 ChapAuthPeer(unit, our_name, go->chap_mdtype);
216 } else if (go->neg_upap) {
221 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
222 auth |= CHAP_WITHPEER;
223 } else if (ho->neg_upap) {
224 upap_authwithpeer(unit, user, passwd);
225 auth |= UPAP_WITHPEER;
227 auth_pending[unit] = auth;
234 * Proceed to the network phase.
241 struct protent *protp;
243 phase = PHASE_NETWORK;
245 set_filters(&pass_filter, &active_filter);
246 for (i = 0; (protp = protocols[i]) != NULL; ++i)
247 if (protp->protocol < 0xC000 && protp->enabled_flag
248 && protp->open != NULL) {
249 (*protp->open)(unit);
250 if (protp->protocol != PPP_CCP)
256 * The peer has failed to authenticate himself using `protocol'.
259 auth_peer_fail(unit, protocol)
263 * Authentication failure: take the link down
265 lcp_close(unit, "Authentication failed");
266 phase = PHASE_TERMINATE;
270 * The peer has been successfully authenticated using `protocol'.
273 auth_peer_success(unit, protocol)
286 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
292 * If there is no more authentication still to be done,
293 * proceed to the network phase.
295 if ((auth_pending[unit] &= ~bit) == 0)
300 * We have failed to authenticate ourselves to the peer using `protocol'.
303 auth_withpeer_fail(unit, protocol)
307 * We've failed to authenticate ourselves to our peer.
308 * He'll probably take the link down, and there's not much
309 * we can do except wait for that.
314 * We have successfully authenticated ourselves with the peer using `protocol'.
317 auth_withpeer_success(unit, protocol)
330 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
336 * If there is no more authentication still being done,
337 * proceed to the network phase.
339 if ((auth_pending[unit] &= ~bit) == 0)
345 * np_up - a network protocol has come up.
351 if (num_np_up == 0 && idle_time_limit > 0) {
352 TIMEOUT(check_idle, NULL, idle_time_limit);
358 * np_down - a network protocol has gone down.
364 if (--num_np_up == 0 && idle_time_limit > 0) {
365 UNTIMEOUT(check_idle, NULL);
370 * np_finished - a network protocol has finished using the link.
373 np_finished(unit, proto)
376 if (--num_np_open <= 0) {
377 /* no further use for the link: shut up shop. */
378 lcp_close(0, "No network protocols running");
383 * check_idle - check whether the link has been idle for long
384 * enough that we can shut it down.
390 struct ppp_idle idle;
393 if (!get_idle_time(0, &idle))
395 itime = MIN(idle.xmit_idle, idle.recv_idle);
396 if (itime >= idle_time_limit) {
397 /* link is idle: shut it down. */
398 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
399 lcp_close(0, "Link inactive");
401 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
406 * auth_check_options - called to check authentication options.
411 lcp_options *wo = &lcp_wantoptions[0];
412 lcp_options *ao = &lcp_allowoptions[0];
413 ipcp_options *ipwo = &ipcp_wantoptions[0];
416 /* Default our_name to hostname, and user to our_name */
417 if (our_name[0] == 0 || usehostname)
418 strcpy(our_name, hostname);
420 strcpy(user, our_name);
422 /* If authentication is required, ask peer for CHAP or PAP. */
423 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
429 * Check whether we have appropriate secrets to use
430 * to authenticate ourselves and/or the peer.
432 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
434 if (wo->neg_upap && !uselogin && !have_upap_secret())
436 if (ao->neg_chap && !have_chap_secret(our_name, remote_name, (u_int32_t)0))
439 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
440 if (!have_chap_secret(remote_name, our_name, remote))
444 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
446 pppd: peer authentication required but no suitable secret(s) found\n");
454 * check_passwd - Check the user name and passwd against the PAP secrets
455 * file. If requested, also check against the system password database,
456 * and login the user if OK.
459 * UPAP_AUTHNAK: Authentication failed.
460 * UPAP_AUTHACK: Authentication succeeded.
461 * In either case, msg points to an appropriate message.
464 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
476 struct wordlist *addrs;
478 ipcp_options *ipwo = &ipcp_wantoptions[unit];
479 char passwd[256], user[256];
480 char secret[MAXWORDLEN];
481 static int attempts = 0;
484 * Make copies of apasswd and auser, then null-terminate them.
486 BCOPY(apasswd, passwd, passwdlen);
487 passwd[passwdlen] = '\0';
488 BCOPY(auser, user, userlen);
489 user[userlen] = '\0';
492 * Open the file of upap secrets and scan for a suitable secret
493 * for authenticating this user.
495 filename = _PATH_UPAPFILE;
498 f = fopen(filename, "r");
501 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
506 check_access(f, filename);
507 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
508 if (scan_authfile(f, user, our_name, remote,
509 secret, &addrs, filename) < 0
510 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
511 && strcmp(crypt(passwd, secret), secret) != 0)) {
512 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
518 if (uselogin && ret == UPAP_AUTHACK) {
519 ret = login(user, passwd, msg, msglen);
520 if (ret == UPAP_AUTHNAK) {
521 syslog(LOG_WARNING, "PAP login failure for %s", user);
525 if (ret == UPAP_AUTHNAK) {
526 *msg = "Login incorrect";
527 *msglen = strlen(*msg);
529 * Frustrate passwd stealer programs.
530 * Allow 10 tries, but start backing off after 3 (stolen from login).
531 * On 10'th, drop the connection.
533 if (attempts++ >= 10) {
534 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
535 attempts, devnam, user);
539 sleep((u_int) (attempts - 3) * 5);
541 free_wordlist(addrs);
544 attempts = 0; /* Reset count */
546 *msglen = strlen(*msg);
547 if (addresses[unit] != NULL)
548 free_wordlist(addresses[unit]);
549 addresses[unit] = addrs;
557 * login - Check the user name and password against the system
558 * password database, and login the user if OK.
561 * UPAP_AUTHNAK: Login failed.
562 * UPAP_AUTHACK: Login succeeded.
563 * In either case, msg points to an appropriate message.
566 login(user, passwd, msg, msglen)
578 struct spwd *getspnam();
581 if ((pw = getpwnam(user)) == NULL) {
582 return (UPAP_AUTHNAK);
586 if ((spwd = getspnam(user)) == NULL) {
589 pw->pw_passwd = spwd->sp_pwdp;
594 * XXX If no passwd, let them login without one.
596 if (pw->pw_passwd == '\0') {
597 return (UPAP_AUTHACK);
601 if ((pw->pw_passwd && pw->pw_passwd[0] == '@'
602 && pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL))
603 || !valid (passwd, pw)) {
604 return (UPAP_AUTHNAK);
607 epasswd = crypt(passwd, pw->pw_passwd);
608 if (strcmp(epasswd, pw->pw_passwd)) {
609 return (UPAP_AUTHNAK);
613 syslog(LOG_INFO, "user %s logged in", user);
616 * Write a wtmp entry for this user.
619 if (strncmp(tty, "/dev/", 5) == 0)
621 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
624 return (UPAP_AUTHACK);
628 * logout - Logout the user.
636 if (strncmp(tty, "/dev/", 5) == 0)
638 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
644 * null_login - Check if a username of "" and a password of "" are
645 * acceptable, and iff so, set the list of acceptable IP addresses
655 struct wordlist *addrs;
656 char secret[MAXWORDLEN];
659 * Open the file of upap secrets and scan for a suitable secret.
660 * We don't accept a wildcard client.
662 filename = _PATH_UPAPFILE;
664 f = fopen(filename, "r");
667 check_access(f, filename);
669 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
670 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
673 if (addresses[unit] != NULL)
674 free_wordlist(addresses[unit]);
675 addresses[unit] = addrs;
684 * get_upap_passwd - get a password for authenticating ourselves with
685 * our peer using PAP. Returns 1 on success, 0 if no suitable password
693 struct wordlist *addrs;
694 char secret[MAXWORDLEN];
696 filename = _PATH_UPAPFILE;
698 f = fopen(filename, "r");
701 check_access(f, filename);
702 if (scan_authfile(f, user, remote_name, (u_int32_t)0,
703 secret, NULL, filename) < 0)
705 strncpy(passwd, secret, MAXSECRETLEN);
706 passwd[MAXSECRETLEN-1] = 0;
712 * have_upap_secret - check whether we have a PAP file with any
713 * secrets that we could possibly use for authenticating the peer.
721 ipcp_options *ipwo = &ipcp_wantoptions[0];
724 filename = _PATH_UPAPFILE;
725 f = fopen(filename, "r");
729 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
730 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
740 * have_chap_secret - check whether we have a CHAP file with a
741 * secret that we could possibly use for authenticating `client'
742 * on `server'. Either can be the null string, meaning we don't
743 * know the identity yet.
746 have_chap_secret(client, server, remote)
755 filename = _PATH_CHAPFILE;
756 f = fopen(filename, "r");
762 else if (server[0] == 0)
765 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
775 * get_secret - open the CHAP secret file and return the secret
776 * for authenticating the given client on the given server.
777 * (We could be either client or server).
780 get_secret(unit, client, server, secret, secret_len, save_addrs)
791 struct wordlist *addrs;
792 char secbuf[MAXWORDLEN];
794 filename = _PATH_CHAPFILE;
798 f = fopen(filename, "r");
800 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
803 check_access(f, filename);
805 ret = scan_authfile(f, client, server, (u_int32_t)0,
806 secbuf, &addrs, filename);
812 if (addresses[unit] != NULL)
813 free_wordlist(addresses[unit]);
814 addresses[unit] = addrs;
817 len = strlen(secbuf);
818 if (len > MAXSECRETLEN) {
819 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
822 BCOPY(secbuf, secret, len);
829 * auth_ip_addr - check whether the peer is authorized to use
830 * a given IP address. Returns 1 if authorized, 0 otherwise.
833 auth_ip_addr(unit, addr)
837 return ip_addr_check(addr, addresses[unit]);
841 ip_addr_check(addr, addrs)
843 struct wordlist *addrs;
845 u_int32_t a, mask, ah;
847 char *ptr_word, *ptr_mask;
851 /* don't allow loopback or multicast address */
852 if (bad_ip_adrs(addr))
856 return 1; /* no restriction */
858 for (; addrs != NULL; addrs = addrs->next) {
859 /* "-" means no addresses authorized */
860 ptr_word = addrs->word;
861 if (strcmp(ptr_word, "-") == 0)
865 if (*ptr_word == '!') {
870 mask = ~ (u_int32_t) 0;
871 ptr_mask = strchr (ptr_word, '/');
872 if (ptr_mask != NULL) {
875 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
876 if (bit_count <= 0 || bit_count > 32) {
878 "invalid address length %s in auth. address list",
883 mask <<= 32 - bit_count;
886 hp = gethostbyname(ptr_word);
887 if (hp != NULL && hp->h_addrtype == AF_INET) {
888 a = *(u_int32_t *)hp->h_addr;
889 mask = ~ (u_int32_t) 0; /* are we sure we want this? */
891 np = getnetbyname (ptr_word);
892 if (np != NULL && np->n_addrtype == AF_INET)
893 a = htonl (*(u_int32_t *)np->n_net);
895 a = inet_addr (ptr_word);
896 if (ptr_mask == NULL) {
897 /* calculate appropriate mask for net */
900 mask = IN_CLASSA_NET;
901 else if (IN_CLASSB(ah))
902 mask = IN_CLASSB_NET;
903 else if (IN_CLASSC(ah))
904 mask = IN_CLASSC_NET;
908 if (ptr_mask != NULL)
913 "unknown host %s in auth. address list",
916 if (((addr ^ a) & htonl(mask)) == 0)
919 return 0; /* not in list => can't have it */
923 * bad_ip_adrs - return 1 if the IP address is one we don't want
924 * to use, such as an address in the loopback net or a multicast address.
925 * addr is in network byte order.
932 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
933 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
937 * check_access - complain if a secret file has too-liberal permissions.
940 check_access(f, filename)
946 if (fstat(fileno(f), &sbuf) < 0) {
947 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
948 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
949 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
955 * scan_authfile - Scan an authorization file for a secret suitable
956 * for authenticating `client' on `server'. The return value is -1
957 * if no secret is found, otherwise >= 0. The return value has
958 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
959 * NONWILD_SERVER set if the secret didn't have "*" for the server.
960 * Any following words on the line (i.e. address authorization
961 * info) are placed in a wordlist and returned in *addrs.
964 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
970 struct wordlist **addrs;
974 int got_flag, best_flag;
976 struct wordlist *ap, *addr_list, *alist, *alast;
977 char word[MAXWORDLEN];
978 char atfile[MAXWORDLEN];
979 char lsecret[MAXWORDLEN];
984 if (!getword(f, word, &newline, filename))
985 return -1; /* file is empty??? */
990 * Skip until we find a word at the start of a line.
992 while (!newline && getword(f, word, &newline, filename))
995 break; /* got to end of file */
998 * Got a client - check if it's a match or a wildcard.
1001 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1006 got_flag = NONWILD_CLIENT;
1009 * Now get a server and check if it matches.
1011 if (!getword(f, word, &newline, filename))
1015 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1018 got_flag |= NONWILD_SERVER;
1021 * Got some sort of a match - see if it's better than what
1024 if (got_flag <= best_flag)
1030 if (!getword(f, word, &newline, filename))
1036 * Special syntax: @filename means read secret from file.
1038 if (word[0] == '@') {
1039 strcpy(atfile, word+1);
1040 if ((sf = fopen(atfile, "r")) == NULL) {
1041 syslog(LOG_WARNING, "can't open indirect secret file %s",
1045 check_access(sf, atfile);
1046 if (!getword(sf, word, &xxx, atfile)) {
1047 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1055 strcpy(lsecret, word);
1058 * Now read address authorization info and make a wordlist.
1060 alist = alast = NULL;
1062 if (!getword(f, word, &newline, filename) || newline)
1064 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1067 novm("authorized addresses");
1069 strcpy(ap->word, word);
1078 * Check if the given IP address is allowed by the wordlist.
1080 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1081 free_wordlist(alist);
1086 * This is the best so far; remember it.
1088 best_flag = got_flag;
1090 free_wordlist(addr_list);
1093 strcpy(secret, lsecret);
1101 else if (addr_list != NULL)
1102 free_wordlist(addr_list);
1108 * free_wordlist - release memory allocated for a wordlist.
1112 struct wordlist *wp;
1114 struct wordlist *next;
1116 while (wp != NULL) {