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.7 1994/08/09 06:29:14 paulus Exp $";
44 #include <sys/types.h>
48 #include <netinet/in.h>
49 #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)
82 extern char devname[];
83 extern char our_name[];
84 extern char remote_name[];
85 extern char hostname[];
87 extern int usehostname;
88 extern int auth_required;
90 /* Records which authentication operations haven't completed yet. */
91 static int auth_pending[NPPP];
93 static struct wordlist *addresses[NPPP];
95 /* Bits in auth_pending[] */
96 #define UPAP_WITHPEER 1
98 #define CHAP_WITHPEER 4
102 void check_access __ARGS((FILE *, char *));
104 static void network_phase __ARGS((int));
105 static int login __ARGS((char *, char *, char **, int *));
106 static void logout __ARGS((void));
107 static int null_login __ARGS((int));
108 static int get_upap_passwd __ARGS((void));
109 static int have_upap_secret __ARGS((void));
110 static int have_chap_secret __ARGS((char *, char *));
111 static int scan_authfile __ARGS((FILE *, char *, char *, char *,
112 struct wordlist **, char *));
113 static void free_wordlist __ARGS((struct wordlist *));
115 extern char *crypt __ARGS((char *, char *));
118 * An Open on LCP has requested a change from Dead to Establish phase.
119 * Do what's necessary to bring the physical layer up.
128 * LCP has terminated the link; go to the Dead phase and take the
129 * physical layer down.
132 link_terminated(unit)
135 if (phase == PHASE_DEAD)
140 syslog(LOG_NOTICE, "Connection terminated.");
144 * LCP has gone down; it will either die or try to re-establish.
152 phase = PHASE_TERMINATE;
156 * The link is established.
157 * Proceed to the Dead, Authenticate or Network phase as appropriate.
160 link_established(unit)
164 lcp_options *wo = &lcp_wantoptions[unit];
165 lcp_options *go = &lcp_gotoptions[unit];
166 lcp_options *ho = &lcp_hisoptions[unit];
168 if (auth_required && !(go->neg_chap || go->neg_upap)) {
170 * We wanted the peer to authenticate itself, and it refused:
171 * treat it as though it authenticated with PAP using a username
172 * of "" and a password of "". If that's not OK, boot it out.
174 if (wo->neg_upap && !null_login(unit)) {
175 syslog(LOG_WARNING, "peer refused to authenticate");
177 phase = PHASE_TERMINATE;
182 phase = PHASE_AUTHENTICATE;
185 ChapAuthPeer(unit, our_name, go->chap_mdtype);
187 } else if (go->neg_upap) {
192 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
193 auth |= CHAP_WITHPEER;
194 } else if (ho->neg_upap) {
195 upap_authwithpeer(unit, user, passwd);
196 auth |= UPAP_WITHPEER;
198 auth_pending[unit] = auth;
205 * Proceed to the network phase.
211 phase = PHASE_NETWORK;
217 * The peer has failed to authenticate himself using `protocol'.
220 auth_peer_fail(unit, protocol)
224 * Authentication failure: take the link down
227 phase = PHASE_TERMINATE;
231 * The peer has been successfully authenticated using `protocol'.
234 auth_peer_success(unit, protocol)
247 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
253 * If there is no more authentication still to be done,
254 * proceed to the network phase.
256 if ((auth_pending[unit] &= ~bit) == 0) {
257 phase = PHASE_NETWORK;
264 * We have failed to authenticate ourselves to the peer using `protocol'.
267 auth_withpeer_fail(unit, protocol)
271 * We've failed to authenticate ourselves to our peer.
272 * He'll probably take the link down, and there's not much
273 * we can do except wait for that.
278 * We have successfully authenticated ourselves with the peer using `protocol'.
281 auth_withpeer_success(unit, protocol)
294 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
300 * If there is no more authentication still being done,
301 * proceed to the network phase.
303 if ((auth_pending[unit] &= ~bit) == 0)
309 * check_auth_options - called to check authentication options.
314 lcp_options *wo = &lcp_wantoptions[0];
315 lcp_options *ao = &lcp_allowoptions[0];
317 /* Default our_name to hostname, and user to our_name */
318 if (our_name[0] == 0 || usehostname)
319 strcpy(our_name, hostname);
321 strcpy(user, our_name);
323 /* If authentication is required, ask peer for CHAP or PAP. */
324 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
330 * Check whether we have appropriate secrets to use
331 * to authenticate ourselves and/or the peer.
333 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
335 if (wo->neg_upap && !uselogin && !have_upap_secret())
337 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
339 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
342 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
344 pppd: peer authentication required but no authentication files accessible\n");
352 * check_passwd - Check the user name and passwd against the PAP secrets
353 * file. If requested, also check against the system password database,
354 * and login the user if OK.
357 * UPAP_AUTHNAK: Authentication failed.
358 * UPAP_AUTHACK: Authentication succeeded.
359 * In either case, msg points to an appropriate message.
362 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
374 struct wordlist *addrs;
375 char passwd[256], user[256];
376 char secret[MAXWORDLEN];
377 static int attempts = 0;
380 * Make copies of apasswd and auser, then null-terminate them.
382 BCOPY(apasswd, passwd, passwdlen);
383 passwd[passwdlen] = '\0';
384 BCOPY(auser, user, userlen);
385 user[userlen] = '\0';
388 * Open the file of upap secrets and scan for a suitable secret
389 * for authenticating this user.
391 filename = _PATH_UPAPFILE;
394 f = fopen(filename, "r");
397 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
402 check_access(f, filename);
403 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
404 || (secret[0] != 0 && strcmp(passwd, secret) != 0
405 && strcmp(crypt(passwd, secret), secret) != 0)) {
406 syslog(LOG_WARNING, "upap authentication failure for %s", user);
412 if (uselogin && ret == UPAP_AUTHACK) {
413 ret = login(user, passwd, msg, msglen);
414 if (ret == UPAP_AUTHNAK) {
415 syslog(LOG_WARNING, "upap login failure for %s", user);
419 if (ret == UPAP_AUTHNAK) {
420 *msg = "Login incorrect";
421 *msglen = strlen(*msg);
423 * Frustrate passwd stealer programs.
424 * Allow 10 tries, but start backing off after 3 (stolen from login).
425 * On 10'th, drop the connection.
427 if (attempts++ >= 10) {
428 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
429 attempts, devname, user);
433 sleep((u_int) (attempts - 3) * 5);
435 free_wordlist(addrs);
438 attempts = 0; /* Reset count */
440 *msglen = strlen(*msg);
441 if (addresses[unit] != NULL)
442 free_wordlist(addresses[unit]);
443 addresses[unit] = addrs;
451 * login - Check the user name and password against the system
452 * password database, and login the user if OK.
455 * UPAP_AUTHNAK: Login failed.
456 * UPAP_AUTHACK: Login succeeded.
457 * In either case, msg points to an appropriate message.
460 login(user, passwd, msg, msglen)
470 if ((pw = getpwnam(user)) == NULL) {
471 return (UPAP_AUTHNAK);
475 * XXX If no passwd, let them login without one.
477 if (pw->pw_passwd == '\0') {
478 return (UPAP_AUTHACK);
481 epasswd = crypt(passwd, pw->pw_passwd);
482 if (strcmp(epasswd, pw->pw_passwd)) {
483 return (UPAP_AUTHNAK);
486 syslog(LOG_INFO, "user %s logged in", user);
489 * Write a wtmp entry for this user.
491 tty = strrchr(devname, '/');
496 logwtmp(tty, user, ""); /* Add wtmp login entry */
499 return (UPAP_AUTHACK);
503 * logout - Logout the user.
510 tty = strrchr(devname, '/');
515 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
521 * null_login - Check if a username of "" and a password of "" are
522 * acceptable, and iff so, set the list of acceptable IP addresses
532 struct wordlist *addrs;
533 char secret[MAXWORDLEN];
536 * Open the file of upap secrets and scan for a suitable secret.
537 * We don't accept a wildcard client.
539 filename = _PATH_UPAPFILE;
541 f = fopen(filename, "r");
544 check_access(f, filename);
546 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
547 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
550 if (addresses[unit] != NULL)
551 free_wordlist(addresses[unit]);
552 addresses[unit] = addrs;
561 * get_upap_passwd - get a password for authenticating ourselves with
562 * our peer using PAP. Returns 1 on success, 0 if no suitable password
570 struct wordlist *addrs;
571 char secret[MAXWORDLEN];
573 filename = _PATH_UPAPFILE;
575 f = fopen(filename, "r");
578 check_access(f, filename);
579 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
581 strncpy(passwd, secret, MAXSECRETLEN);
582 passwd[MAXSECRETLEN-1] = 0;
588 * have_upap_secret - check whether we have a PAP file with any
589 * secrets that we could possibly use for authenticating the peer.
598 filename = _PATH_UPAPFILE;
599 f = fopen(filename, "r");
603 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
613 * have_chap_secret - check whether we have a CHAP file with a
614 * secret that we could possibly use for authenticating `client'
615 * on `server'. Either can be the null string, meaning we don't
616 * know the identity yet.
619 have_chap_secret(client, server)
627 filename = _PATH_CHAPFILE;
628 f = fopen(filename, "r");
634 else if (server[0] == 0)
637 ret = scan_authfile(f, client, server, NULL, NULL, filename);
647 * get_secret - open the CHAP secret file and return the secret
648 * for authenticating the given client on the given server.
649 * (We could be either client or server).
652 get_secret(unit, client, server, secret, secret_len, save_addrs)
663 struct wordlist *addrs;
664 char secbuf[MAXWORDLEN];
666 filename = _PATH_CHAPFILE;
670 f = fopen(filename, "r");
672 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
675 check_access(f, filename);
677 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
683 if (addresses[unit] != NULL)
684 free_wordlist(addresses[unit]);
685 addresses[unit] = addrs;
688 len = strlen(secbuf);
689 if (len > MAXSECRETLEN) {
690 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
693 BCOPY(secbuf, secret, len);
700 * auth_ip_addr - check whether the peer is authorized to use
701 * a given IP address. Returns 1 if authorized, 0 otherwise.
704 auth_ip_addr(unit, addr)
710 struct wordlist *addrs;
712 /* don't allow loopback or multicast address */
713 if (bad_ip_adrs(addr))
716 if ((addrs = addresses[unit]) == NULL)
717 return 1; /* no restriction */
719 for (; addrs != NULL; addrs = addrs->next) {
720 /* "-" means no addresses authorized */
721 if (strcmp(addrs->word, "-") == 0)
723 if ((a = inet_addr(addrs->word)) == -1) {
724 if ((hp = gethostbyname(addrs->word)) == NULL) {
725 syslog(LOG_WARNING, "unknown host %s in auth. address list",
729 a = *(u_long *)hp->h_addr;
734 return 0; /* not in list => can't have it */
738 * bad_ip_adrs - return 1 if the IP address is one we don't want
739 * to use, such as an address in the loopback net or a multicast address.
740 * addr is in network byte order.
747 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
748 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
752 * check_access - complain if a secret file has too-liberal permissions.
755 check_access(f, filename)
761 if (fstat(fileno(f), &sbuf) < 0) {
762 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
763 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
764 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
770 * scan_authfile - Scan an authorization file for a secret suitable
771 * for authenticating `client' on `server'. The return value is -1
772 * if no secret is found, otherwise >= 0. The return value has
773 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
774 * NONWILD_SERVER set if the secret didn't have "*" for the server.
775 * Any following words on the line (i.e. address authorization
776 * info) are placed in a wordlist and returned in *addrs.
779 scan_authfile(f, client, server, secret, addrs, filename)
784 struct wordlist **addrs;
788 int got_flag, best_flag;
790 struct wordlist *ap, *addr_list, *addr_last;
791 char word[MAXWORDLEN];
792 char atfile[MAXWORDLEN];
797 if (!getword(f, word, &newline, filename))
798 return -1; /* file is empty??? */
803 * Skip until we find a word at the start of a line.
805 while (!newline && getword(f, word, &newline, filename))
808 break; /* got to end of file */
811 * Got a client - check if it's a match or a wildcard.
814 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
819 got_flag = NONWILD_CLIENT;
822 * Now get a server and check if it matches.
824 if (!getword(f, word, &newline, filename))
828 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
831 got_flag |= NONWILD_SERVER;
834 * Got some sort of a match - see if it's better than what
837 if (got_flag <= best_flag)
843 if (!getword(f, word, &newline, filename))
849 * Special syntax: @filename means read secret from file.
851 if (word[0] == '@') {
852 strcpy(atfile, word+1);
853 if ((sf = fopen(atfile, "r")) == NULL) {
854 syslog(LOG_WARNING, "can't open indirect secret file %s",
858 check_access(sf, atfile);
859 if (!getword(sf, word, &xxx, atfile)) {
860 syslog(LOG_WARNING, "no secret in indirect secret file %s",
868 strcpy(secret, word);
870 best_flag = got_flag;
873 * Now read address authorization info and make a wordlist.
876 free_wordlist(addr_list);
877 addr_list = addr_last = NULL;
879 if (!getword(f, word, &newline, filename) || newline)
881 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
884 novm("authorized addresses");
886 strcpy(ap->word, word);
887 if (addr_list == NULL)
890 addr_last->next = ap;
899 else if (addr_list != NULL)
900 free_wordlist(addr_list);
906 * free_wordlist - release memory allocated for a wordlist.
912 struct wordlist *next;