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.10 1994/09/21 06:47:37 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)
79 /* Records which authentication operations haven't completed yet. */
80 static int auth_pending[N_PPP];
82 static struct wordlist *addresses[N_PPP];
84 /* Bits in auth_pending[] */
85 #define UPAP_WITHPEER 1
87 #define CHAP_WITHPEER 4
91 void check_access __P((FILE *, char *));
93 static void network_phase __P((int));
94 static int login __P((char *, char *, char **, int *));
95 static void logout __P((void));
96 static int null_login __P((int));
97 static int get_upap_passwd __P((void));
98 static int have_upap_secret __P((void));
99 static int have_chap_secret __P((char *, char *));
100 static int scan_authfile __P((FILE *, char *, char *, char *,
101 struct wordlist **, char *));
102 static void free_wordlist __P((struct wordlist *));
104 extern char *crypt __P((char *, char *));
107 * An Open on LCP has requested a change from Dead to Establish phase.
108 * Do what's necessary to bring the physical layer up.
117 * LCP has terminated the link; go to the Dead phase and take the
118 * physical layer down.
121 link_terminated(unit)
124 if (phase == PHASE_DEAD)
129 syslog(LOG_NOTICE, "Connection terminated.");
133 * LCP has gone down; it will either die or try to re-establish.
141 phase = PHASE_TERMINATE;
145 * The link is established.
146 * Proceed to the Dead, Authenticate or Network phase as appropriate.
149 link_established(unit)
153 lcp_options *wo = &lcp_wantoptions[unit];
154 lcp_options *go = &lcp_gotoptions[unit];
155 lcp_options *ho = &lcp_hisoptions[unit];
157 if (auth_required && !(go->neg_chap || go->neg_upap)) {
159 * We wanted the peer to authenticate itself, and it refused:
160 * treat it as though it authenticated with PAP using a username
161 * of "" and a password of "". If that's not OK, boot it out.
163 if (wo->neg_upap && !null_login(unit)) {
164 syslog(LOG_WARNING, "peer refused to authenticate");
166 phase = PHASE_TERMINATE;
171 phase = PHASE_AUTHENTICATE;
174 ChapAuthPeer(unit, our_name, go->chap_mdtype);
176 } else if (go->neg_upap) {
181 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
182 auth |= CHAP_WITHPEER;
183 } else if (ho->neg_upap) {
184 upap_authwithpeer(unit, user, passwd);
185 auth |= UPAP_WITHPEER;
187 auth_pending[unit] = auth;
194 * Proceed to the network phase.
200 phase = PHASE_NETWORK;
206 * The peer has failed to authenticate himself using `protocol'.
209 auth_peer_fail(unit, protocol)
213 * Authentication failure: take the link down
216 phase = PHASE_TERMINATE;
220 * The peer has been successfully authenticated using `protocol'.
223 auth_peer_success(unit, protocol)
236 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
242 * If there is no more authentication still to be done,
243 * proceed to the network phase.
245 if ((auth_pending[unit] &= ~bit) == 0) {
246 phase = PHASE_NETWORK;
253 * We have failed to authenticate ourselves to the peer using `protocol'.
256 auth_withpeer_fail(unit, protocol)
260 * We've failed to authenticate ourselves to our peer.
261 * He'll probably take the link down, and there's not much
262 * we can do except wait for that.
267 * We have successfully authenticated ourselves with the peer using `protocol'.
270 auth_withpeer_success(unit, protocol)
283 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
289 * If there is no more authentication still being done,
290 * proceed to the network phase.
292 if ((auth_pending[unit] &= ~bit) == 0)
298 * check_auth_options - called to check authentication options.
303 lcp_options *wo = &lcp_wantoptions[0];
304 lcp_options *ao = &lcp_allowoptions[0];
306 /* Default our_name to hostname, and user to our_name */
307 if (our_name[0] == 0 || usehostname)
308 strcpy(our_name, hostname);
310 strcpy(user, our_name);
312 /* If authentication is required, ask peer for CHAP or PAP. */
313 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
319 * Check whether we have appropriate secrets to use
320 * to authenticate ourselves and/or the peer.
322 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
324 if (wo->neg_upap && !uselogin && !have_upap_secret())
326 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
328 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
331 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
333 pppd: peer authentication required but no authentication files accessible\n");
341 * check_passwd - Check the user name and passwd against the PAP secrets
342 * file. If requested, also check against the system password database,
343 * and login the user if OK.
346 * UPAP_AUTHNAK: Authentication failed.
347 * UPAP_AUTHACK: Authentication succeeded.
348 * In either case, msg points to an appropriate message.
351 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
363 struct wordlist *addrs;
364 char passwd[256], user[256];
365 char secret[MAXWORDLEN];
366 static int attempts = 0;
369 * Make copies of apasswd and auser, then null-terminate them.
371 BCOPY(apasswd, passwd, passwdlen);
372 passwd[passwdlen] = '\0';
373 BCOPY(auser, user, userlen);
374 user[userlen] = '\0';
377 * Open the file of upap secrets and scan for a suitable secret
378 * for authenticating this user.
380 filename = _PATH_UPAPFILE;
383 f = fopen(filename, "r");
386 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
391 check_access(f, filename);
392 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
393 || (secret[0] != 0 && strcmp(passwd, secret) != 0
394 && strcmp(crypt(passwd, secret), secret) != 0)) {
395 syslog(LOG_WARNING, "upap authentication failure for %s", user);
401 if (uselogin && ret == UPAP_AUTHACK) {
402 ret = login(user, passwd, msg, msglen);
403 if (ret == UPAP_AUTHNAK) {
404 syslog(LOG_WARNING, "upap login failure for %s", user);
408 if (ret == UPAP_AUTHNAK) {
409 *msg = "Login incorrect";
410 *msglen = strlen(*msg);
412 * Frustrate passwd stealer programs.
413 * Allow 10 tries, but start backing off after 3 (stolen from login).
414 * On 10'th, drop the connection.
416 if (attempts++ >= 10) {
417 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
418 attempts, devnam, user);
422 sleep((u_int) (attempts - 3) * 5);
424 free_wordlist(addrs);
427 attempts = 0; /* Reset count */
429 *msglen = strlen(*msg);
430 if (addresses[unit] != NULL)
431 free_wordlist(addresses[unit]);
432 addresses[unit] = addrs;
440 * login - Check the user name and password against the system
441 * password database, and login the user if OK.
444 * UPAP_AUTHNAK: Login failed.
445 * UPAP_AUTHACK: Login succeeded.
446 * In either case, msg points to an appropriate message.
449 login(user, passwd, msg, msglen)
459 if ((pw = getpwnam(user)) == NULL) {
460 return (UPAP_AUTHNAK);
464 * XXX If no passwd, let them login without one.
466 if (pw->pw_passwd == '\0') {
467 return (UPAP_AUTHACK);
470 epasswd = crypt(passwd, pw->pw_passwd);
471 if (strcmp(epasswd, pw->pw_passwd)) {
472 return (UPAP_AUTHNAK);
475 syslog(LOG_INFO, "user %s logged in", user);
478 * Write a wtmp entry for this user.
480 tty = strrchr(devnam, '/');
485 logwtmp(tty, user, ""); /* Add wtmp login entry */
488 return (UPAP_AUTHACK);
492 * logout - Logout the user.
499 tty = strrchr(devnam, '/');
504 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
510 * null_login - Check if a username of "" and a password of "" are
511 * acceptable, and iff so, set the list of acceptable IP addresses
521 struct wordlist *addrs;
522 char secret[MAXWORDLEN];
525 * Open the file of upap secrets and scan for a suitable secret.
526 * We don't accept a wildcard client.
528 filename = _PATH_UPAPFILE;
530 f = fopen(filename, "r");
533 check_access(f, filename);
535 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
536 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
539 if (addresses[unit] != NULL)
540 free_wordlist(addresses[unit]);
541 addresses[unit] = addrs;
550 * get_upap_passwd - get a password for authenticating ourselves with
551 * our peer using PAP. Returns 1 on success, 0 if no suitable password
559 struct wordlist *addrs;
560 char secret[MAXWORDLEN];
562 filename = _PATH_UPAPFILE;
564 f = fopen(filename, "r");
567 check_access(f, filename);
568 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
570 strncpy(passwd, secret, MAXSECRETLEN);
571 passwd[MAXSECRETLEN-1] = 0;
577 * have_upap_secret - check whether we have a PAP file with any
578 * secrets that we could possibly use for authenticating the peer.
587 filename = _PATH_UPAPFILE;
588 f = fopen(filename, "r");
592 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
602 * have_chap_secret - check whether we have a CHAP file with a
603 * secret that we could possibly use for authenticating `client'
604 * on `server'. Either can be the null string, meaning we don't
605 * know the identity yet.
608 have_chap_secret(client, server)
616 filename = _PATH_CHAPFILE;
617 f = fopen(filename, "r");
623 else if (server[0] == 0)
626 ret = scan_authfile(f, client, server, NULL, NULL, filename);
636 * get_secret - open the CHAP secret file and return the secret
637 * for authenticating the given client on the given server.
638 * (We could be either client or server).
641 get_secret(unit, client, server, secret, secret_len, save_addrs)
652 struct wordlist *addrs;
653 char secbuf[MAXWORDLEN];
655 filename = _PATH_CHAPFILE;
659 f = fopen(filename, "r");
661 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
664 check_access(f, filename);
666 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
672 if (addresses[unit] != NULL)
673 free_wordlist(addresses[unit]);
674 addresses[unit] = addrs;
677 len = strlen(secbuf);
678 if (len > MAXSECRETLEN) {
679 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
682 BCOPY(secbuf, secret, len);
689 * auth_ip_addr - check whether the peer is authorized to use
690 * a given IP address. Returns 1 if authorized, 0 otherwise.
693 auth_ip_addr(unit, addr)
699 struct wordlist *addrs;
701 /* don't allow loopback or multicast address */
702 if (bad_ip_adrs(addr))
705 if ((addrs = addresses[unit]) == NULL)
706 return 1; /* no restriction */
708 for (; addrs != NULL; addrs = addrs->next) {
709 /* "-" means no addresses authorized */
710 if (strcmp(addrs->word, "-") == 0)
712 if ((a = inet_addr(addrs->word)) == -1) {
713 if ((hp = gethostbyname(addrs->word)) == NULL) {
714 syslog(LOG_WARNING, "unknown host %s in auth. address list",
718 a = *(u_int32_t *)hp->h_addr;
723 return 0; /* not in list => can't have it */
727 * bad_ip_adrs - return 1 if the IP address is one we don't want
728 * to use, such as an address in the loopback net or a multicast address.
729 * addr is in network byte order.
736 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
737 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
741 * check_access - complain if a secret file has too-liberal permissions.
744 check_access(f, filename)
750 if (fstat(fileno(f), &sbuf) < 0) {
751 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
752 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
753 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
759 * scan_authfile - Scan an authorization file for a secret suitable
760 * for authenticating `client' on `server'. The return value is -1
761 * if no secret is found, otherwise >= 0. The return value has
762 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
763 * NONWILD_SERVER set if the secret didn't have "*" for the server.
764 * Any following words on the line (i.e. address authorization
765 * info) are placed in a wordlist and returned in *addrs.
768 scan_authfile(f, client, server, secret, addrs, filename)
773 struct wordlist **addrs;
777 int got_flag, best_flag;
779 struct wordlist *ap, *addr_list, *addr_last;
780 char word[MAXWORDLEN];
781 char atfile[MAXWORDLEN];
786 if (!getword(f, word, &newline, filename))
787 return -1; /* file is empty??? */
792 * Skip until we find a word at the start of a line.
794 while (!newline && getword(f, word, &newline, filename))
797 break; /* got to end of file */
800 * Got a client - check if it's a match or a wildcard.
803 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
808 got_flag = NONWILD_CLIENT;
811 * Now get a server and check if it matches.
813 if (!getword(f, word, &newline, filename))
817 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
820 got_flag |= NONWILD_SERVER;
823 * Got some sort of a match - see if it's better than what
826 if (got_flag <= best_flag)
832 if (!getword(f, word, &newline, filename))
838 * Special syntax: @filename means read secret from file.
840 if (word[0] == '@') {
841 strcpy(atfile, word+1);
842 if ((sf = fopen(atfile, "r")) == NULL) {
843 syslog(LOG_WARNING, "can't open indirect secret file %s",
847 check_access(sf, atfile);
848 if (!getword(sf, word, &xxx, atfile)) {
849 syslog(LOG_WARNING, "no secret in indirect secret file %s",
857 strcpy(secret, word);
859 best_flag = got_flag;
862 * Now read address authorization info and make a wordlist.
865 free_wordlist(addr_list);
866 addr_list = addr_last = NULL;
868 if (!getword(f, word, &newline, filename) || newline)
870 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
873 novm("authorized addresses");
875 strcpy(ap->word, word);
876 if (addr_list == NULL)
879 addr_last->next = ap;
888 else if (addr_list != NULL)
889 free_wordlist(addr_list);
895 * free_wordlist - release memory allocated for a wordlist.
901 struct wordlist *next;