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.6 1994/05/25 06:25:05 paulus Exp $";
44 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
58 #include "pathnames.h"
64 /* Used for storing a sequence of words. Usually malloced. */
66 struct wordlist *next;
70 /* Bits in scan_authfile return value */
71 #define NONWILD_SERVER 1
72 #define NONWILD_CLIENT 2
74 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
81 extern char devname[];
82 extern char our_name[];
83 extern char remote_name[];
84 extern char hostname[];
86 extern int usehostname;
87 extern int auth_required;
89 /* Records which authentication operations haven't completed yet. */
90 static int auth_pending[NPPP];
92 static struct wordlist *addresses[NPPP];
94 /* Bits in auth_pending[] */
95 #define UPAP_WITHPEER 1
97 #define CHAP_WITHPEER 4
101 void check_access __ARGS((FILE *, char *));
103 static int login __ARGS((char *, char *, char **, int *));
104 static void logout __ARGS((void));
105 static int null_login __ARGS((int));
106 static int get_upap_passwd __ARGS((void));
107 static int have_upap_secret __ARGS((void));
108 static int have_chap_secret __ARGS((char *, char *));
109 static int scan_authfile __ARGS((FILE *, char *, char *, char *,
110 struct wordlist **, char *));
111 static void free_wordlist __ARGS((struct wordlist *));
113 extern char *crypt __ARGS((char *, char *));
116 * An Open on LCP has requested a change from Dead to Establish phase.
117 * Do what's necessary to bring the physical layer up.
126 * LCP has terminated the link; go to the Dead phase and take the
127 * physical layer down.
130 link_terminated(unit)
136 syslog(LOG_NOTICE, "Connection terminated.");
140 * LCP has gone down; it will either die or try to re-establish.
146 phase = PHASE_TERMINATE;
150 * The link is established.
151 * Proceed to the Dead, Authenticate or Network phase as appropriate.
154 link_established(unit)
158 lcp_options *wo = &lcp_wantoptions[unit];
159 lcp_options *go = &lcp_gotoptions[unit];
160 lcp_options *ho = &lcp_hisoptions[unit];
162 if (auth_required && !(go->neg_chap || go->neg_upap)) {
164 * We wanted the peer to authenticate itself, and it refused:
165 * treat it as though it authenticated with PAP using a username
166 * of "" and a password of "". If that's not OK, boot it out.
168 if (wo->neg_upap && !null_login(unit)) {
169 syslog(LOG_WARNING, "peer refused to authenticate");
171 phase = PHASE_TERMINATE;
176 phase = PHASE_AUTHENTICATE;
179 ChapAuthPeer(unit, our_name, go->chap_mdtype);
181 } else if (go->neg_upap) {
186 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
187 auth |= CHAP_WITHPEER;
188 } else if (ho->neg_upap) {
189 upap_authwithpeer(unit, user, passwd);
190 auth |= UPAP_WITHPEER;
192 auth_pending[unit] = auth;
195 phase = PHASE_NETWORK;
201 * The peer has failed to authenticate himself using `protocol'.
204 auth_peer_fail(unit, protocol)
208 * Authentication failure: take the link down
211 phase = PHASE_TERMINATE;
215 * The peer has been successfully authenticated using `protocol'.
218 auth_peer_success(unit, protocol)
231 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
237 * If there is no more authentication still to be done,
238 * proceed to the network phase.
240 if ((auth_pending[unit] &= ~bit) == 0) {
241 phase = PHASE_NETWORK;
247 * We have failed to authenticate ourselves to the peer using `protocol'.
250 auth_withpeer_fail(unit, protocol)
254 * We've failed to authenticate ourselves to our peer.
255 * He'll probably take the link down, and there's not much
256 * we can do except wait for that.
261 * We have successfully authenticated ourselves with the peer using `protocol'.
264 auth_withpeer_success(unit, protocol)
277 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
283 * If there is no more authentication still being done,
284 * proceed to the network phase.
286 if ((auth_pending[unit] &= ~bit) == 0) {
287 phase = PHASE_NETWORK;
294 * check_auth_options - called to check authentication options.
299 lcp_options *wo = &lcp_wantoptions[0];
300 lcp_options *ao = &lcp_allowoptions[0];
302 /* Default our_name to hostname, and user to our_name */
303 if (our_name[0] == 0 || usehostname)
304 strcpy(our_name, hostname);
306 strcpy(user, our_name);
308 /* If authentication is required, ask peer for CHAP or PAP. */
309 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
315 * Check whether we have appropriate secrets to use
316 * to authenticate ourselves and/or the peer.
318 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
320 if (wo->neg_upap && !uselogin && !have_upap_secret())
322 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
324 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
327 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
329 pppd: peer authentication required but no authentication files accessible\n");
337 * check_passwd - Check the user name and passwd against the PAP secrets
338 * file. If requested, also check against the system password database,
339 * and login the user if OK.
342 * UPAP_AUTHNAK: Authentication failed.
343 * UPAP_AUTHACK: Authentication succeeded.
344 * In either case, msg points to an appropriate message.
347 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
359 struct wordlist *addrs;
360 char passwd[256], user[256];
361 char secret[MAXWORDLEN];
362 static int attempts = 0;
365 * Make copies of apasswd and auser, then null-terminate them.
367 BCOPY(apasswd, passwd, passwdlen);
368 passwd[passwdlen] = '\0';
369 BCOPY(auser, user, userlen);
370 user[userlen] = '\0';
373 * Open the file of upap secrets and scan for a suitable secret
374 * for authenticating this user.
376 filename = _PATH_UPAPFILE;
379 f = fopen(filename, "r");
382 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
387 check_access(f, filename);
388 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
389 || (secret[0] != 0 && strcmp(passwd, secret) != 0
390 && strcmp(crypt(passwd, secret), secret) != 0)) {
391 syslog(LOG_WARNING, "upap authentication failure for %s", user);
397 if (uselogin && ret == UPAP_AUTHACK) {
398 ret = login(user, passwd, msg, msglen);
399 if (ret == UPAP_AUTHNAK) {
400 syslog(LOG_WARNING, "upap login failure for %s", user);
404 if (ret == UPAP_AUTHNAK) {
405 *msg = "Login incorrect";
406 *msglen = strlen(*msg);
408 * Frustrate passwd stealer programs.
409 * Allow 10 tries, but start backing off after 3 (stolen from login).
410 * On 10'th, drop the connection.
412 if (attempts++ >= 10) {
413 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
414 attempts, devname, user);
418 sleep((u_int) (attempts - 3) * 5);
420 free_wordlist(addrs);
423 attempts = 0; /* Reset count */
425 *msglen = strlen(*msg);
426 if (addresses[unit] != NULL)
427 free_wordlist(addresses[unit]);
428 addresses[unit] = addrs;
436 * login - Check the user name and password against the system
437 * password database, and login the user if OK.
440 * UPAP_AUTHNAK: Login failed.
441 * UPAP_AUTHACK: Login succeeded.
442 * In either case, msg points to an appropriate message.
445 login(user, passwd, msg, msglen)
455 if ((pw = getpwnam(user)) == NULL) {
456 return (UPAP_AUTHNAK);
460 * XXX If no passwd, let them login without one.
462 if (pw->pw_passwd == '\0') {
463 return (UPAP_AUTHACK);
466 epasswd = crypt(passwd, pw->pw_passwd);
467 if (strcmp(epasswd, pw->pw_passwd)) {
468 return (UPAP_AUTHNAK);
471 syslog(LOG_INFO, "user %s logged in", user);
474 * Write a wtmp entry for this user.
476 tty = strrchr(devname, '/');
481 logwtmp(tty, user, ""); /* Add wtmp login entry */
484 return (UPAP_AUTHACK);
488 * logout - Logout the user.
495 tty = strrchr(devname, '/');
500 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
506 * null_login - Check if a username of "" and a password of "" are
507 * acceptable, and iff so, set the list of acceptable IP addresses
517 struct wordlist *addrs;
518 char secret[MAXWORDLEN];
521 * Open the file of upap secrets and scan for a suitable secret.
522 * We don't accept a wildcard client.
524 filename = _PATH_UPAPFILE;
526 f = fopen(filename, "r");
529 check_access(f, filename);
531 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
532 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
535 if (addresses[unit] != NULL)
536 free_wordlist(addresses[unit]);
537 addresses[unit] = addrs;
546 * get_upap_passwd - get a password for authenticating ourselves with
547 * our peer using PAP. Returns 1 on success, 0 if no suitable password
555 struct wordlist *addrs;
556 char secret[MAXWORDLEN];
558 filename = _PATH_UPAPFILE;
560 f = fopen(filename, "r");
563 check_access(f, filename);
564 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
566 strncpy(passwd, secret, MAXSECRETLEN);
567 passwd[MAXSECRETLEN-1] = 0;
573 * have_upap_secret - check whether we have a PAP file with any
574 * secrets that we could possibly use for authenticating the peer.
583 filename = _PATH_UPAPFILE;
584 f = fopen(filename, "r");
588 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
598 * have_chap_secret - check whether we have a CHAP file with a
599 * secret that we could possibly use for authenticating `client'
600 * on `server'. Either can be the null string, meaning we don't
601 * know the identity yet.
604 have_chap_secret(client, server)
612 filename = _PATH_CHAPFILE;
613 f = fopen(filename, "r");
619 else if (server[0] == 0)
622 ret = scan_authfile(f, client, server, NULL, NULL, filename);
632 * get_secret - open the CHAP secret file and return the secret
633 * for authenticating the given client on the given server.
634 * (We could be either client or server).
637 get_secret(unit, client, server, secret, secret_len, save_addrs)
648 struct wordlist *addrs;
649 char secbuf[MAXWORDLEN];
651 filename = _PATH_CHAPFILE;
655 f = fopen(filename, "r");
657 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
660 check_access(f, filename);
662 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
668 if (addresses[unit] != NULL)
669 free_wordlist(addresses[unit]);
670 addresses[unit] = addrs;
673 len = strlen(secbuf);
674 if (len > MAXSECRETLEN) {
675 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
678 BCOPY(secbuf, secret, len);
685 * auth_ip_addr - check whether the peer is authorized to use
686 * a given IP address. Returns 1 if authorized, 0 otherwise.
689 auth_ip_addr(unit, addr)
695 struct wordlist *addrs;
697 /* don't allow loopback or multicast address */
698 if (bad_ip_adrs(addr))
701 if ((addrs = addresses[unit]) == NULL)
702 return 1; /* no restriction */
704 for (; addrs != NULL; addrs = addrs->next) {
705 /* "-" means no addresses authorized */
706 if (strcmp(addrs->word, "-") == 0)
708 if ((a = inet_addr(addrs->word)) == -1) {
709 if ((hp = gethostbyname(addrs->word)) == NULL) {
710 syslog(LOG_WARNING, "unknown host %s in auth. address list",
714 a = *(u_long *)hp->h_addr;
719 return 0; /* not in list => can't have it */
723 * bad_ip_adrs - return 1 if the IP address is one we don't want
724 * to use, such as an address in the loopback net or a multicast address.
725 * addr is in network byte order.
732 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
733 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
737 * check_access - complain if a secret file has too-liberal permissions.
740 check_access(f, filename)
746 if (fstat(fileno(f), &sbuf) < 0) {
747 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
748 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
749 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
755 * scan_authfile - Scan an authorization file for a secret suitable
756 * for authenticating `client' on `server'. The return value is -1
757 * if no secret is found, otherwise >= 0. The return value has
758 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
759 * NONWILD_SERVER set if the secret didn't have "*" for the server.
760 * Any following words on the line (i.e. address authorization
761 * info) are placed in a wordlist and returned in *addrs.
764 scan_authfile(f, client, server, secret, addrs, filename)
769 struct wordlist **addrs;
773 int got_flag, best_flag;
775 struct wordlist *ap, *addr_list, *addr_last;
776 char word[MAXWORDLEN];
777 char atfile[MAXWORDLEN];
782 if (!getword(f, word, &newline, filename))
783 return -1; /* file is empty??? */
788 * Skip until we find a word at the start of a line.
790 while (!newline && getword(f, word, &newline, filename))
793 break; /* got to end of file */
796 * Got a client - check if it's a match or a wildcard.
799 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
804 got_flag = NONWILD_CLIENT;
807 * Now get a server and check if it matches.
809 if (!getword(f, word, &newline, filename))
813 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
816 got_flag |= NONWILD_SERVER;
819 * Got some sort of a match - see if it's better than what
822 if (got_flag <= best_flag)
828 if (!getword(f, word, &newline, filename))
834 * Special syntax: @filename means read secret from file.
836 if (word[0] == '@') {
837 strcpy(atfile, word+1);
838 if ((sf = fopen(atfile, "r")) == NULL) {
839 syslog(LOG_WARNING, "can't open indirect secret file %s",
843 check_access(sf, atfile);
844 if (!getword(sf, word, &xxx, atfile)) {
845 syslog(LOG_WARNING, "no secret in indirect secret file %s",
853 strcpy(secret, word);
855 best_flag = got_flag;
858 * Now read address authorization info and make a wordlist.
861 free_wordlist(addr_list);
862 addr_list = addr_last = NULL;
864 if (!getword(f, word, &newline, filename) || newline)
866 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
869 novm("authorized addresses");
871 strcpy(ap->word, word);
872 if (addr_list == NULL)
875 addr_last->next = ap;
884 else if (addr_list != NULL)
885 free_wordlist(addr_list);
891 * free_wordlist - release memory allocated for a wordlist.
897 struct wordlist *next;