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.21 1996/01/01 22:53:04 paulus Exp $";
45 #include <sys/types.h>
47 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
55 #include <shadow/pwauth.h>
57 #define PW_PPP PW_LOGIN
65 #include "pathnames.h"
67 #if defined(sun) && defined(sparc)
71 /* Used for storing a sequence of words. Usually malloced. */
73 struct wordlist *next;
77 /* Bits in scan_authfile return value */
78 #define NONWILD_SERVER 1
79 #define NONWILD_CLIENT 2
81 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
86 /* Records which authentication operations haven't completed yet. */
87 static int auth_pending[NUM_PPP];
89 /* Set if we have successfully called login() */
92 /* List of addresses which the peer may use. */
93 static struct wordlist *addresses[NUM_PPP];
95 /* Number of network protocols which we have opened. */
96 static int num_np_open;
98 /* Number of network protocols which have come up. */
101 /* Bits in auth_pending[] */
102 #define UPAP_WITHPEER 1
104 #define CHAP_WITHPEER 4
107 /* Prototypes for procedures local to this file. */
109 static void network_phase __P((int));
110 static void check_idle __P((caddr_t));
111 static int login __P((char *, char *, char **, int *));
112 static void logout __P((void));
113 static int null_login __P((int));
114 static int get_upap_passwd __P((void));
115 static int have_upap_secret __P((void));
116 static int have_chap_secret __P((char *, char *));
117 static int scan_authfile __P((FILE *, char *, char *, char *,
118 struct wordlist **, char *));
119 static void free_wordlist __P((struct wordlist *));
121 extern char *crypt __P((char *, char *));
124 * An Open on LCP has requested a change from Dead to Establish phase.
125 * Do what's necessary to bring the physical layer up.
134 * LCP has terminated the link; go to the Dead phase and take the
135 * physical layer down.
138 link_terminated(unit)
141 if (phase == PHASE_DEAD)
146 syslog(LOG_NOTICE, "Connection terminated.");
150 * LCP has gone down; it will either die or try to re-establish.
157 struct protent *protp;
159 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
160 if (!protp->enabled_flag)
162 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
163 (*protp->lowerdown)(unit);
164 if (protp->protocol < 0xC000 && protp->close != NULL)
165 (*protp->close)(unit);
169 phase = PHASE_TERMINATE;
173 * The link is established.
174 * Proceed to the Dead, Authenticate or Network phase as appropriate.
177 link_established(unit)
181 lcp_options *wo = &lcp_wantoptions[unit];
182 lcp_options *go = &lcp_gotoptions[unit];
183 lcp_options *ho = &lcp_hisoptions[unit];
185 struct protent *protp;
188 * Tell higher-level protocols that LCP is up.
190 for (i = 0; (protp = protocols[i]) != NULL; ++i)
191 if (protp->protocol != PPP_LCP && protp->enabled_flag
192 && protp->lowerup != NULL)
193 (*protp->lowerup)(unit);
195 if (auth_required && !(go->neg_chap || go->neg_upap)) {
197 * We wanted the peer to authenticate itself, and it refused:
198 * treat it as though it authenticated with PAP using a username
199 * of "" and a password of "". If that's not OK, boot it out.
201 if (!wo->neg_upap || !null_login(unit)) {
202 syslog(LOG_WARNING, "peer refused to authenticate");
203 lcp_close(unit, "peer refused to authenticate");
204 phase = PHASE_TERMINATE;
209 phase = PHASE_AUTHENTICATE;
212 ChapAuthPeer(unit, our_name, go->chap_mdtype);
214 } else if (go->neg_upap) {
219 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
220 auth |= CHAP_WITHPEER;
221 } else if (ho->neg_upap) {
222 upap_authwithpeer(unit, user, passwd);
223 auth |= UPAP_WITHPEER;
225 auth_pending[unit] = auth;
232 * Proceed to the network phase.
239 struct protent *protp;
241 phase = PHASE_NETWORK;
242 for (i = 0; (protp = protocols[i]) != NULL; ++i)
243 if (protp->protocol < 0xC000 && protp->enabled_flag
244 && protp->open != NULL) {
245 (*protp->open)(unit);
246 if (protp->protocol != PPP_CCP)
252 * The peer has failed to authenticate himself using `protocol'.
255 auth_peer_fail(unit, protocol)
259 * Authentication failure: take the link down
261 lcp_close(unit, "Authentication failed");
262 phase = PHASE_TERMINATE;
266 * The peer has been successfully authenticated using `protocol'.
269 auth_peer_success(unit, protocol)
282 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
288 * If there is no more authentication still to be done,
289 * proceed to the network phase.
291 if ((auth_pending[unit] &= ~bit) == 0)
296 * We have failed to authenticate ourselves to the peer using `protocol'.
299 auth_withpeer_fail(unit, protocol)
303 * We've failed to authenticate ourselves to our peer.
304 * He'll probably take the link down, and there's not much
305 * we can do except wait for that.
310 * We have successfully authenticated ourselves with the peer using `protocol'.
313 auth_withpeer_success(unit, protocol)
326 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
332 * If there is no more authentication still being done,
333 * proceed to the network phase.
335 if ((auth_pending[unit] &= ~bit) == 0)
341 * np_up - a network protocol has come up.
347 if (num_np_up == 0 && idle_time_limit > 0) {
348 TIMEOUT(check_idle, NULL, idle_time_limit);
354 * np_down - a network protocol has gone down.
360 if (--num_np_up == 0 && idle_time_limit > 0) {
361 UNTIMEOUT(check_idle, NULL);
366 * np_finished - a network protocol has finished using the link.
369 np_finished(unit, proto)
372 if (--num_np_open <= 0) {
373 /* no further use for the link: shut up shop. */
374 lcp_close(0, "No network protocols running");
379 * check_idle - check whether the link has been idle for long
380 * enough that we can shut it down.
386 struct ppp_idle idle;
389 if (!get_idle_time(0, &idle))
391 itime = MIN(idle.xmit_idle, idle.recv_idle);
392 if (itime >= idle_time_limit) {
393 /* link is idle: shut it down. */
394 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
395 lcp_close(0, "Link inactive");
397 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
402 * auth_check_options - called to check authentication options.
407 lcp_options *wo = &lcp_wantoptions[0];
408 lcp_options *ao = &lcp_allowoptions[0];
410 /* Default our_name to hostname, and user to our_name */
411 if (our_name[0] == 0 || usehostname)
412 strcpy(our_name, hostname);
414 strcpy(user, our_name);
416 /* If authentication is required, ask peer for CHAP or PAP. */
417 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
423 * Check whether we have appropriate secrets to use
424 * to authenticate ourselves and/or the peer.
426 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
428 if (wo->neg_upap && !uselogin && !have_upap_secret())
430 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
432 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
435 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
437 pppd: peer authentication required but no authentication files accessible\n");
445 * check_passwd - Check the user name and passwd against the PAP secrets
446 * file. If requested, also check against the system password database,
447 * and login the user if OK.
450 * UPAP_AUTHNAK: Authentication failed.
451 * UPAP_AUTHACK: Authentication succeeded.
452 * In either case, msg points to an appropriate message.
455 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
467 struct wordlist *addrs;
468 char passwd[256], user[256];
469 char secret[MAXWORDLEN];
470 static int attempts = 0;
473 * Make copies of apasswd and auser, then null-terminate them.
475 BCOPY(apasswd, passwd, passwdlen);
476 passwd[passwdlen] = '\0';
477 BCOPY(auser, user, userlen);
478 user[userlen] = '\0';
481 * Open the file of upap secrets and scan for a suitable secret
482 * for authenticating this user.
484 filename = _PATH_UPAPFILE;
487 f = fopen(filename, "r");
490 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
495 check_access(f, filename);
496 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
497 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
498 && strcmp(crypt(passwd, secret), secret) != 0)) {
499 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
505 if (uselogin && ret == UPAP_AUTHACK) {
506 ret = login(user, passwd, msg, msglen);
507 if (ret == UPAP_AUTHNAK) {
508 syslog(LOG_WARNING, "PAP login failure for %s", user);
512 if (ret == UPAP_AUTHNAK) {
513 *msg = "Login incorrect";
514 *msglen = strlen(*msg);
516 * Frustrate passwd stealer programs.
517 * Allow 10 tries, but start backing off after 3 (stolen from login).
518 * On 10'th, drop the connection.
520 if (attempts++ >= 10) {
521 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
522 attempts, devnam, user);
526 sleep((u_int) (attempts - 3) * 5);
528 free_wordlist(addrs);
531 attempts = 0; /* Reset count */
533 *msglen = strlen(*msg);
534 if (addresses[unit] != NULL)
535 free_wordlist(addresses[unit]);
536 addresses[unit] = addrs;
544 * login - Check the user name and password against the system
545 * password database, and login the user if OK.
548 * UPAP_AUTHNAK: Login failed.
549 * UPAP_AUTHACK: Login succeeded.
550 * In either case, msg points to an appropriate message.
553 login(user, passwd, msg, msglen)
565 struct spwd *getspnam();
568 if ((pw = getpwnam(user)) == NULL) {
569 return (UPAP_AUTHNAK);
573 if ((spwd = getspnam(user)) == NULL) {
576 pw->pw_passwd = spwd->sp_pwdp;
581 * XXX If no passwd, let them login without one.
583 if (pw->pw_passwd == '\0') {
584 return (UPAP_AUTHACK);
588 if ((pw->pw_passwd && pw->pw_passwd[0] == '@'
589 && pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL))
590 || !valid (passwd, pw)) {
591 return (UPAP_AUTHNAK);
594 epasswd = crypt(passwd, pw->pw_passwd);
595 if (strcmp(epasswd, pw->pw_passwd)) {
596 return (UPAP_AUTHNAK);
600 syslog(LOG_INFO, "user %s logged in", user);
603 * Write a wtmp entry for this user.
606 if (strncmp(tty, "/dev/", 5) == 0)
608 logwtmp(tty, user, ""); /* Add wtmp login entry */
611 return (UPAP_AUTHACK);
615 * logout - Logout the user.
623 if (strncmp(tty, "/dev/", 5) == 0)
625 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
631 * null_login - Check if a username of "" and a password of "" are
632 * acceptable, and iff so, set the list of acceptable IP addresses
642 struct wordlist *addrs;
643 char secret[MAXWORDLEN];
646 * Open the file of upap secrets and scan for a suitable secret.
647 * We don't accept a wildcard client.
649 filename = _PATH_UPAPFILE;
651 f = fopen(filename, "r");
654 check_access(f, filename);
656 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
657 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
660 if (addresses[unit] != NULL)
661 free_wordlist(addresses[unit]);
662 addresses[unit] = addrs;
671 * get_upap_passwd - get a password for authenticating ourselves with
672 * our peer using PAP. Returns 1 on success, 0 if no suitable password
680 struct wordlist *addrs;
681 char secret[MAXWORDLEN];
683 filename = _PATH_UPAPFILE;
685 f = fopen(filename, "r");
688 check_access(f, filename);
689 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
691 strncpy(passwd, secret, MAXSECRETLEN);
692 passwd[MAXSECRETLEN-1] = 0;
698 * have_upap_secret - check whether we have a PAP file with any
699 * secrets that we could possibly use for authenticating the peer.
708 filename = _PATH_UPAPFILE;
709 f = fopen(filename, "r");
713 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
723 * have_chap_secret - check whether we have a CHAP file with a
724 * secret that we could possibly use for authenticating `client'
725 * on `server'. Either can be the null string, meaning we don't
726 * know the identity yet.
729 have_chap_secret(client, server)
737 filename = _PATH_CHAPFILE;
738 f = fopen(filename, "r");
744 else if (server[0] == 0)
747 ret = scan_authfile(f, client, server, NULL, NULL, filename);
757 * get_secret - open the CHAP secret file and return the secret
758 * for authenticating the given client on the given server.
759 * (We could be either client or server).
762 get_secret(unit, client, server, secret, secret_len, save_addrs)
773 struct wordlist *addrs;
774 char secbuf[MAXWORDLEN];
776 filename = _PATH_CHAPFILE;
780 f = fopen(filename, "r");
782 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
785 check_access(f, filename);
787 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
793 if (addresses[unit] != NULL)
794 free_wordlist(addresses[unit]);
795 addresses[unit] = addrs;
798 len = strlen(secbuf);
799 if (len > MAXSECRETLEN) {
800 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
803 BCOPY(secbuf, secret, len);
810 * auth_ip_addr - check whether the peer is authorized to use
811 * a given IP address. Returns 1 if authorized, 0 otherwise.
814 auth_ip_addr(unit, addr)
820 char *ptr_word, *ptr_mask;
823 struct wordlist *addrs;
825 /* don't allow loopback or multicast address */
826 if (bad_ip_adrs(addr))
829 if ((addrs = addresses[unit]) == NULL)
830 return 1; /* no restriction */
832 for (; addrs != NULL; addrs = addrs->next) {
833 /* "-" means no addresses authorized */
834 ptr_word = addrs->word;
835 if (strcmp(ptr_word, "-") == 0)
839 if (*ptr_word == '!') {
844 mask = ~ (u_int32_t) 0;
845 ptr_mask = strchr (ptr_word, '/');
846 if (ptr_mask != NULL) {
849 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
850 if (bit_count <= 0 || bit_count > 32) {
852 "invalid address length %s in auth. address list",
857 mask <<= 32 - bit_count;
860 hp = gethostbyname(ptr_word);
861 if (hp != NULL && hp->h_addrtype == AF_INET) {
862 a = *(u_int32_t *)hp->h_addr;
863 mask = ~ (u_int32_t) 0; /* are we sure we want this? */
865 np = getnetbyname (ptr_word);
866 if (np != NULL && np->n_addrtype == AF_INET)
867 a = htonl (*(u_int32_t *)np->n_net);
869 a = inet_addr (ptr_word);
872 if (ptr_mask != NULL)
877 "unknown host %s in auth. address list",
880 if (((addr ^ a) & mask) == 0)
883 return 0; /* not in list => can't have it */
887 * bad_ip_adrs - return 1 if the IP address is one we don't want
888 * to use, such as an address in the loopback net or a multicast address.
889 * addr is in network byte order.
896 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
897 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
901 * check_access - complain if a secret file has too-liberal permissions.
904 check_access(f, filename)
910 if (fstat(fileno(f), &sbuf) < 0) {
911 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
912 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
913 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
919 * scan_authfile - Scan an authorization file for a secret suitable
920 * for authenticating `client' on `server'. The return value is -1
921 * if no secret is found, otherwise >= 0. The return value has
922 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
923 * NONWILD_SERVER set if the secret didn't have "*" for the server.
924 * Any following words on the line (i.e. address authorization
925 * info) are placed in a wordlist and returned in *addrs.
928 scan_authfile(f, client, server, secret, addrs, filename)
933 struct wordlist **addrs;
937 int got_flag, best_flag;
939 struct wordlist *ap, *addr_list, *addr_last;
940 char word[MAXWORDLEN];
941 char atfile[MAXWORDLEN];
946 if (!getword(f, word, &newline, filename))
947 return -1; /* file is empty??? */
952 * Skip until we find a word at the start of a line.
954 while (!newline && getword(f, word, &newline, filename))
957 break; /* got to end of file */
960 * Got a client - check if it's a match or a wildcard.
963 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
968 got_flag = NONWILD_CLIENT;
971 * Now get a server and check if it matches.
973 if (!getword(f, word, &newline, filename))
977 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
980 got_flag |= NONWILD_SERVER;
983 * Got some sort of a match - see if it's better than what
986 if (got_flag <= best_flag)
992 if (!getword(f, word, &newline, filename))
998 * Special syntax: @filename means read secret from file.
1000 if (word[0] == '@') {
1001 strcpy(atfile, word+1);
1002 if ((sf = fopen(atfile, "r")) == NULL) {
1003 syslog(LOG_WARNING, "can't open indirect secret file %s",
1007 check_access(sf, atfile);
1008 if (!getword(sf, word, &xxx, atfile)) {
1009 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1017 strcpy(secret, word);
1019 best_flag = got_flag;
1022 * Now read address authorization info and make a wordlist.
1025 free_wordlist(addr_list);
1026 addr_list = addr_last = NULL;
1028 if (!getword(f, word, &newline, filename) || newline)
1030 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1033 novm("authorized addresses");
1035 strcpy(ap->word, word);
1036 if (addr_list == NULL)
1039 addr_last->next = ap;
1048 else if (addr_list != NULL)
1049 free_wordlist(addr_list);
1055 * free_wordlist - release memory allocated for a wordlist.
1059 struct wordlist *wp;
1061 struct wordlist *next;
1063 while (wp != NULL) {