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.12 1994/10/24 04:31:11 paulus Exp $";
45 #include <sys/types.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
59 #include "pathnames.h"
65 /* Used for storing a sequence of words. Usually malloced. */
67 struct wordlist *next;
71 /* Bits in scan_authfile return value */
72 #define NONWILD_SERVER 1
73 #define NONWILD_CLIENT 2
75 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
80 /* Records which authentication operations haven't completed yet. */
81 static int auth_pending[NUM_PPP];
83 static struct wordlist *addresses[NUM_PPP];
85 /* Bits in auth_pending[] */
86 #define UPAP_WITHPEER 1
88 #define CHAP_WITHPEER 4
92 void check_access __P((FILE *, char *));
94 static void network_phase __P((int));
95 static int login __P((char *, char *, char **, int *));
96 static void logout __P((void));
97 static int null_login __P((int));
98 static int get_upap_passwd __P((void));
99 static int have_upap_secret __P((void));
100 static int have_chap_secret __P((char *, char *));
101 static int scan_authfile __P((FILE *, char *, char *, char *,
102 struct wordlist **, char *));
103 static void free_wordlist __P((struct wordlist *));
105 extern char *crypt __P((char *, char *));
108 * An Open on LCP has requested a change from Dead to Establish phase.
109 * Do what's necessary to bring the physical layer up.
118 * LCP has terminated the link; go to the Dead phase and take the
119 * physical layer down.
122 link_terminated(unit)
125 if (phase == PHASE_DEAD)
130 syslog(LOG_NOTICE, "Connection terminated.");
134 * LCP has gone down; it will either die or try to re-establish.
142 phase = PHASE_TERMINATE;
146 * The link is established.
147 * Proceed to the Dead, Authenticate or Network phase as appropriate.
150 link_established(unit)
154 lcp_options *wo = &lcp_wantoptions[unit];
155 lcp_options *go = &lcp_gotoptions[unit];
156 lcp_options *ho = &lcp_hisoptions[unit];
158 if (auth_required && !(go->neg_chap || go->neg_upap)) {
160 * We wanted the peer to authenticate itself, and it refused:
161 * treat it as though it authenticated with PAP using a username
162 * of "" and a password of "". If that's not OK, boot it out.
164 if (wo->neg_upap && !null_login(unit)) {
165 syslog(LOG_WARNING, "peer refused to authenticate");
167 phase = PHASE_TERMINATE;
172 phase = PHASE_AUTHENTICATE;
175 ChapAuthPeer(unit, our_name, go->chap_mdtype);
177 } else if (go->neg_upap) {
182 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
183 auth |= CHAP_WITHPEER;
184 } else if (ho->neg_upap) {
185 upap_authwithpeer(unit, user, passwd);
186 auth |= UPAP_WITHPEER;
188 auth_pending[unit] = auth;
195 * Proceed to the network phase.
201 phase = PHASE_NETWORK;
207 * The peer has failed to authenticate himself using `protocol'.
210 auth_peer_fail(unit, protocol)
214 * Authentication failure: take the link down
217 phase = PHASE_TERMINATE;
221 * The peer has been successfully authenticated using `protocol'.
224 auth_peer_success(unit, protocol)
237 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
243 * If there is no more authentication still to be done,
244 * proceed to the network phase.
246 if ((auth_pending[unit] &= ~bit) == 0) {
247 phase = PHASE_NETWORK;
254 * We have failed to authenticate ourselves to the peer using `protocol'.
257 auth_withpeer_fail(unit, protocol)
261 * We've failed to authenticate ourselves to our peer.
262 * He'll probably take the link down, and there's not much
263 * we can do except wait for that.
268 * We have successfully authenticated ourselves with the peer using `protocol'.
271 auth_withpeer_success(unit, protocol)
284 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
290 * If there is no more authentication still being done,
291 * proceed to the network phase.
293 if ((auth_pending[unit] &= ~bit) == 0)
299 * check_auth_options - called to check authentication options.
304 lcp_options *wo = &lcp_wantoptions[0];
305 lcp_options *ao = &lcp_allowoptions[0];
307 /* Default our_name to hostname, and user to our_name */
308 if (our_name[0] == 0 || usehostname)
309 strcpy(our_name, hostname);
311 strcpy(user, our_name);
313 /* If authentication is required, ask peer for CHAP or PAP. */
314 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
320 * Check whether we have appropriate secrets to use
321 * to authenticate ourselves and/or the peer.
323 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
325 if (wo->neg_upap && !uselogin && !have_upap_secret())
327 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
329 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
332 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
334 pppd: peer authentication required but no authentication files accessible\n");
342 * check_passwd - Check the user name and passwd against the PAP secrets
343 * file. If requested, also check against the system password database,
344 * and login the user if OK.
347 * UPAP_AUTHNAK: Authentication failed.
348 * UPAP_AUTHACK: Authentication succeeded.
349 * In either case, msg points to an appropriate message.
352 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
364 struct wordlist *addrs;
365 char passwd[256], user[256];
366 char secret[MAXWORDLEN];
367 static int attempts = 0;
370 * Make copies of apasswd and auser, then null-terminate them.
372 BCOPY(apasswd, passwd, passwdlen);
373 passwd[passwdlen] = '\0';
374 BCOPY(auser, user, userlen);
375 user[userlen] = '\0';
378 * Open the file of upap secrets and scan for a suitable secret
379 * for authenticating this user.
381 filename = _PATH_UPAPFILE;
384 f = fopen(filename, "r");
387 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
392 check_access(f, filename);
393 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
394 || (secret[0] != 0 && strcmp(passwd, secret) != 0
395 && strcmp(crypt(passwd, secret), secret) != 0)) {
396 syslog(LOG_WARNING, "upap authentication failure for %s", user);
402 if (uselogin && ret == UPAP_AUTHACK) {
403 ret = login(user, passwd, msg, msglen);
404 if (ret == UPAP_AUTHNAK) {
405 syslog(LOG_WARNING, "upap login failure for %s", user);
409 if (ret == UPAP_AUTHNAK) {
410 *msg = "Login incorrect";
411 *msglen = strlen(*msg);
413 * Frustrate passwd stealer programs.
414 * Allow 10 tries, but start backing off after 3 (stolen from login).
415 * On 10'th, drop the connection.
417 if (attempts++ >= 10) {
418 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
419 attempts, devnam, user);
423 sleep((u_int) (attempts - 3) * 5);
425 free_wordlist(addrs);
428 attempts = 0; /* Reset count */
430 *msglen = strlen(*msg);
431 if (addresses[unit] != NULL)
432 free_wordlist(addresses[unit]);
433 addresses[unit] = addrs;
441 * login - Check the user name and password against the system
442 * password database, and login the user if OK.
445 * UPAP_AUTHNAK: Login failed.
446 * UPAP_AUTHACK: Login succeeded.
447 * In either case, msg points to an appropriate message.
450 login(user, passwd, msg, msglen)
460 if ((pw = getpwnam(user)) == NULL) {
461 return (UPAP_AUTHNAK);
465 * XXX If no passwd, let them login without one.
467 if (pw->pw_passwd == '\0') {
468 return (UPAP_AUTHACK);
471 epasswd = crypt(passwd, pw->pw_passwd);
472 if (strcmp(epasswd, pw->pw_passwd)) {
473 return (UPAP_AUTHNAK);
476 syslog(LOG_INFO, "user %s logged in", user);
479 * Write a wtmp entry for this user.
481 tty = strrchr(devnam, '/');
486 logwtmp(tty, user, ""); /* Add wtmp login entry */
489 return (UPAP_AUTHACK);
493 * logout - Logout the user.
500 tty = strrchr(devnam, '/');
505 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
511 * null_login - Check if a username of "" and a password of "" are
512 * acceptable, and iff so, set the list of acceptable IP addresses
522 struct wordlist *addrs;
523 char secret[MAXWORDLEN];
526 * Open the file of upap secrets and scan for a suitable secret.
527 * We don't accept a wildcard client.
529 filename = _PATH_UPAPFILE;
531 f = fopen(filename, "r");
534 check_access(f, filename);
536 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
537 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
540 if (addresses[unit] != NULL)
541 free_wordlist(addresses[unit]);
542 addresses[unit] = addrs;
551 * get_upap_passwd - get a password for authenticating ourselves with
552 * our peer using PAP. Returns 1 on success, 0 if no suitable password
560 struct wordlist *addrs;
561 char secret[MAXWORDLEN];
563 filename = _PATH_UPAPFILE;
565 f = fopen(filename, "r");
568 check_access(f, filename);
569 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
571 strncpy(passwd, secret, MAXSECRETLEN);
572 passwd[MAXSECRETLEN-1] = 0;
578 * have_upap_secret - check whether we have a PAP file with any
579 * secrets that we could possibly use for authenticating the peer.
588 filename = _PATH_UPAPFILE;
589 f = fopen(filename, "r");
593 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
603 * have_chap_secret - check whether we have a CHAP file with a
604 * secret that we could possibly use for authenticating `client'
605 * on `server'. Either can be the null string, meaning we don't
606 * know the identity yet.
609 have_chap_secret(client, server)
617 filename = _PATH_CHAPFILE;
618 f = fopen(filename, "r");
624 else if (server[0] == 0)
627 ret = scan_authfile(f, client, server, NULL, NULL, filename);
637 * get_secret - open the CHAP secret file and return the secret
638 * for authenticating the given client on the given server.
639 * (We could be either client or server).
642 get_secret(unit, client, server, secret, secret_len, save_addrs)
653 struct wordlist *addrs;
654 char secbuf[MAXWORDLEN];
656 filename = _PATH_CHAPFILE;
660 f = fopen(filename, "r");
662 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
665 check_access(f, filename);
667 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
673 if (addresses[unit] != NULL)
674 free_wordlist(addresses[unit]);
675 addresses[unit] = addrs;
678 len = strlen(secbuf);
679 if (len > MAXSECRETLEN) {
680 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
683 BCOPY(secbuf, secret, len);
690 * auth_ip_addr - check whether the peer is authorized to use
691 * a given IP address. Returns 1 if authorized, 0 otherwise.
694 auth_ip_addr(unit, addr)
700 struct wordlist *addrs;
702 /* don't allow loopback or multicast address */
703 if (bad_ip_adrs(addr))
706 if ((addrs = addresses[unit]) == NULL)
707 return 1; /* no restriction */
709 for (; addrs != NULL; addrs = addrs->next) {
710 /* "-" means no addresses authorized */
711 if (strcmp(addrs->word, "-") == 0)
713 if ((a = inet_addr(addrs->word)) == -1) {
714 if ((hp = gethostbyname(addrs->word)) == NULL) {
715 syslog(LOG_WARNING, "unknown host %s in auth. address list",
719 a = *(u_int32_t *)hp->h_addr;
724 return 0; /* not in list => can't have it */
728 * bad_ip_adrs - return 1 if the IP address is one we don't want
729 * to use, such as an address in the loopback net or a multicast address.
730 * addr is in network byte order.
737 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
738 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
742 * check_access - complain if a secret file has too-liberal permissions.
745 check_access(f, filename)
751 if (fstat(fileno(f), &sbuf) < 0) {
752 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
753 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
754 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
760 * scan_authfile - Scan an authorization file for a secret suitable
761 * for authenticating `client' on `server'. The return value is -1
762 * if no secret is found, otherwise >= 0. The return value has
763 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
764 * NONWILD_SERVER set if the secret didn't have "*" for the server.
765 * Any following words on the line (i.e. address authorization
766 * info) are placed in a wordlist and returned in *addrs.
769 scan_authfile(f, client, server, secret, addrs, filename)
774 struct wordlist **addrs;
778 int got_flag, best_flag;
780 struct wordlist *ap, *addr_list, *addr_last;
781 char word[MAXWORDLEN];
782 char atfile[MAXWORDLEN];
787 if (!getword(f, word, &newline, filename))
788 return -1; /* file is empty??? */
793 * Skip until we find a word at the start of a line.
795 while (!newline && getword(f, word, &newline, filename))
798 break; /* got to end of file */
801 * Got a client - check if it's a match or a wildcard.
804 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
809 got_flag = NONWILD_CLIENT;
812 * Now get a server and check if it matches.
814 if (!getword(f, word, &newline, filename))
818 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
821 got_flag |= NONWILD_SERVER;
824 * Got some sort of a match - see if it's better than what
827 if (got_flag <= best_flag)
833 if (!getword(f, word, &newline, filename))
839 * Special syntax: @filename means read secret from file.
841 if (word[0] == '@') {
842 strcpy(atfile, word+1);
843 if ((sf = fopen(atfile, "r")) == NULL) {
844 syslog(LOG_WARNING, "can't open indirect secret file %s",
848 check_access(sf, atfile);
849 if (!getword(sf, word, &xxx, atfile)) {
850 syslog(LOG_WARNING, "no secret in indirect secret file %s",
858 strcpy(secret, word);
860 best_flag = got_flag;
863 * Now read address authorization info and make a wordlist.
866 free_wordlist(addr_list);
867 addr_list = addr_last = NULL;
869 if (!getword(f, word, &newline, filename) || newline)
871 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
874 novm("authorized addresses");
876 strcpy(ap->word, word);
877 if (addr_list == NULL)
880 addr_last->next = ap;
889 else if (addr_list != NULL)
890 free_wordlist(addr_list);
896 * free_wordlist - release memory allocated for a wordlist.
902 struct wordlist *next;