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.4 1994/05/18 05:59:43 paulus Exp $";
44 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
58 #include "pathnames.h"
63 /* why alloca.h doesn't define what alloca() returns is a mystery */
64 /* char *alloca __ARGS((int)); */
68 /* Used for storing a sequence of words. Usually malloced. */
70 struct wordlist *next;
74 /* Bits in scan_authfile return value */
75 #define NONWILD_SERVER 1
76 #define NONWILD_CLIENT 2
78 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
85 extern char devname[];
86 extern char our_name[];
87 extern char remote_name[];
88 extern char hostname[];
90 extern int usehostname;
91 extern int auth_required;
93 /* Records which authentication operations haven't completed yet. */
94 static int auth_pending[NPPP];
96 static struct wordlist *addresses[NPPP];
98 /* Bits in auth_pending[] */
99 #define UPAP_WITHPEER 1
101 #define CHAP_WITHPEER 4
105 void check_access __ARGS((FILE *, char *));
107 static int login __ARGS((char *, char *, char **, int *));
108 static void logout __ARGS((void));
109 static int null_login __ARGS((int));
110 static int get_upap_passwd __ARGS((void));
111 static int have_upap_secret __ARGS((void));
112 static int have_chap_secret __ARGS((char *, char *));
113 static int scan_authfile __ARGS((FILE *, char *, char *, char *,
114 struct wordlist **, char *));
115 static void free_wordlist __ARGS((struct wordlist *));
117 extern char *crypt __ARGS((char *, char *));
120 * An Open on LCP has requested a change from Dead to Establish phase.
121 * Do what's necessary to bring the physical layer up.
130 * LCP has terminated the link; go to the Dead phase and take the
131 * physical layer down.
134 link_terminated(unit)
140 syslog(LOG_NOTICE, "Connection terminated.");
144 * LCP has gone down; it will either die or try to re-establish.
150 phase = PHASE_TERMINATE;
154 * The link is established.
155 * Proceed to the Dead, Authenticate or Network phase as appropriate.
158 link_established(unit)
162 lcp_options *wo = &lcp_wantoptions[unit];
163 lcp_options *go = &lcp_gotoptions[unit];
164 lcp_options *ho = &lcp_hisoptions[unit];
166 if (auth_required && !(go->neg_chap || go->neg_upap)) {
168 * We wanted the peer to authenticate itself, and it refused:
169 * treat it as though it authenticated with PAP using a username
170 * of "" and a password of "". If that's not OK, boot it out.
172 if (wo->neg_upap && !null_login(unit)) {
173 syslog(LOG_WARNING, "peer refused to authenticate");
175 phase = PHASE_TERMINATE;
180 phase = PHASE_AUTHENTICATE;
183 ChapAuthPeer(unit, our_name, go->chap_mdtype);
185 } else if (go->neg_upap) {
190 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
191 auth |= CHAP_WITHPEER;
192 } else if (ho->neg_upap) {
193 upap_authwithpeer(unit, user, passwd);
194 auth |= UPAP_WITHPEER;
196 auth_pending[unit] = auth;
199 phase = PHASE_NETWORK;
205 * The peer has failed to authenticate himself using `protocol'.
208 auth_peer_fail(unit, protocol)
212 * Authentication failure: take the link down
215 phase = PHASE_TERMINATE;
219 * The peer has been successfully authenticated using `protocol'.
222 auth_peer_success(unit, protocol)
235 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
241 * If there is no more authentication still to be done,
242 * proceed to the network phase.
244 if ((auth_pending[unit] &= ~bit) == 0) {
245 phase = PHASE_NETWORK;
251 * We have failed to authenticate ourselves to the peer using `protocol'.
254 auth_withpeer_fail(unit, protocol)
258 * We've failed to authenticate ourselves to our peer.
259 * He'll probably take the link down, and there's not much
260 * we can do except wait for that.
265 * We have successfully authenticated ourselves with the peer using `protocol'.
268 auth_withpeer_success(unit, protocol)
281 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
286 * If there is no more authentication still being done,
287 * proceed to the network phase.
289 if ((auth_pending[unit] &= ~bit) == 0) {
290 phase = PHASE_NETWORK;
297 * check_auth_options - called to check authentication options.
302 lcp_options *wo = &lcp_wantoptions[0];
303 lcp_options *ao = &lcp_allowoptions[0];
305 /* Default our_name to hostname, and user to our_name */
306 if (our_name[0] == 0 || usehostname)
307 strcpy(our_name, hostname);
309 strcpy(user, our_name);
311 /* If authentication is required, ask peer for CHAP or PAP. */
312 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
318 * Check whether we have appropriate secrets to use
319 * to authenticate ourselves and/or the peer.
321 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
323 if (wo->neg_upap && !uselogin && !have_upap_secret())
325 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
327 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
330 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
332 pppd: peer authentication required but no authentication files accessible\n");
340 * check_passwd - Check the user name and passwd against the PAP secrets
341 * file. If requested, also check against the system password database,
342 * and login the user if OK.
345 * UPAP_AUTHNAK: Authentication failed.
346 * UPAP_AUTHACK: Authentication succeeded.
347 * In either case, msg points to an appropriate message.
350 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
362 struct wordlist *addrs;
363 char passwd[256], user[256];
364 char secret[MAXWORDLEN];
365 static int attempts = 0;
368 * Make copies of apasswd and auser, then null-terminate them.
370 BCOPY(apasswd, passwd, passwdlen);
371 passwd[passwdlen] = '\0';
372 BCOPY(auser, user, userlen);
373 user[userlen] = '\0';
376 * Open the file of upap secrets and scan for a suitable secret
377 * for authenticating this user.
379 filename = _PATH_UPAPFILE;
382 f = fopen(filename, "r");
385 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
390 check_access(f, filename);
391 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
392 || (secret[0] != 0 && strcmp(passwd, secret) != 0
393 && strcmp(crypt(passwd, secret), secret) != 0)) {
394 syslog(LOG_WARNING, "upap authentication failure for %s", user);
400 if (uselogin && ret == UPAP_AUTHACK) {
401 ret = login(user, passwd, msg, msglen);
402 if (ret == UPAP_AUTHNAK) {
403 syslog(LOG_WARNING, "upap login failure for %s", user);
407 if (ret == UPAP_AUTHNAK) {
408 *msg = "Login incorrect";
409 *msglen = strlen(*msg);
411 * Frustrate passwd stealer programs.
412 * Allow 10 tries, but start backing off after 3 (stolen from login).
413 * On 10'th, drop the connection.
415 if (attempts++ >= 10) {
416 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
417 attempts, devname, user);
421 sleep((u_int) (attempts - 3) * 5);
423 free_wordlist(addrs);
426 attempts = 0; /* Reset count */
428 *msglen = strlen(*msg);
429 if (addresses[unit] != NULL)
430 free_wordlist(addresses[unit]);
431 addresses[unit] = addrs;
439 * login - Check the user name and password against the system
440 * password database, and login the user if OK.
443 * UPAP_AUTHNAK: Login failed.
444 * UPAP_AUTHACK: Login succeeded.
445 * In either case, msg points to an appropriate message.
448 login(user, passwd, msg, msglen)
458 if ((pw = getpwnam(user)) == NULL) {
459 return (UPAP_AUTHNAK);
463 * XXX If no passwd, let them login without one.
465 if (pw->pw_passwd == '\0') {
466 return (UPAP_AUTHACK);
469 epasswd = crypt(passwd, pw->pw_passwd);
470 if (strcmp(epasswd, pw->pw_passwd)) {
471 return (UPAP_AUTHNAK);
474 syslog(LOG_INFO, "user %s logged in", user);
477 * Write a wtmp entry for this user.
479 tty = strrchr(devname, '/');
484 logwtmp(tty, user, ""); /* Add wtmp login entry */
487 return (UPAP_AUTHACK);
491 * logout - Logout the user.
498 tty = strrchr(devname, '/');
503 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
509 * null_login - Check if a username of "" and a password of "" are
510 * acceptable, and iff so, set the list of acceptable IP addresses
520 struct wordlist *addrs;
521 char secret[MAXWORDLEN];
524 * Open the file of upap secrets and scan for a suitable secret.
525 * We don't accept a wildcard client.
527 filename = _PATH_UPAPFILE;
529 f = fopen(filename, "r");
532 check_access(f, filename);
534 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
535 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
538 if (addresses[unit] != NULL)
539 free_wordlist(addresses[unit]);
540 addresses[unit] = addrs;
549 * get_upap_passwd - get a password for authenticating ourselves with
550 * our peer using PAP. Returns 1 on success, 0 if no suitable password
558 struct wordlist *addrs;
559 char secret[MAXWORDLEN];
561 filename = _PATH_UPAPFILE;
563 f = fopen(filename, "r");
566 check_access(f, filename);
567 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
569 strncpy(passwd, secret, MAXSECRETLEN);
570 passwd[MAXSECRETLEN-1] = 0;
576 * have_upap_secret - check whether we have a PAP file with any
577 * secrets that we could possibly use for authenticating the peer.
586 filename = _PATH_UPAPFILE;
587 f = fopen(filename, "r");
591 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
601 * have_chap_secret - check whether we have a CHAP file with a
602 * secret that we could possibly use for authenticating `client'
603 * on `server'. Either can be the null string, meaning we don't
604 * know the identity yet.
607 have_chap_secret(client, server)
615 filename = _PATH_CHAPFILE;
616 f = fopen(filename, "r");
622 else if (server[0] == 0)
625 ret = scan_authfile(f, client, server, NULL, NULL, filename);
635 * get_secret - open the CHAP secret file and return the secret
636 * for authenticating the given client on the given server.
637 * (We could be either client or server).
640 get_secret(unit, client, server, secret, secret_len, save_addrs)
650 struct wordlist *addrs;
651 char secbuf[MAXWORDLEN];
653 filename = _PATH_CHAPFILE;
657 f = fopen(filename, "r");
659 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
662 check_access(f, filename);
664 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
670 if (addresses[unit] != NULL)
671 free_wordlist(addresses[unit]);
672 addresses[unit] = addrs;
675 len = strlen(secbuf);
676 if (len > MAXSECRETLEN) {
677 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
680 BCOPY(secbuf, secret, len);
687 * auth_ip_addr - check whether the peer is authorized to use
688 * a given IP address. Returns 1 if authorized, 0 otherwise.
691 auth_ip_addr(unit, addr)
697 struct wordlist *addrs;
699 /* don't allow loopback or multicast address */
700 if (bad_ip_adrs(addr))
703 if ((addrs = addresses[unit]) == NULL)
704 return 1; /* no restriction */
706 for (; addrs != NULL; addrs = addrs->next) {
707 /* "-" means no addresses authorized */
708 if (strcmp(addrs->word, "-") == 0)
710 if ((a = inet_addr(addrs->word)) == -1) {
711 if ((hp = gethostbyname(addrs->word)) == NULL) {
712 syslog(LOG_WARNING, "unknown host %s in auth. address list",
716 a = *(u_long *)hp->h_addr;
721 return 0; /* not in list => can't have it */
725 * bad_ip_adrs - return 1 if the IP address is one we don't want
726 * to use, such as an address in the loopback net or a multicast address.
727 * addr is in network byte order.
734 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
735 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
739 * check_access - complain if a secret file has too-liberal permissions.
742 check_access(f, filename)
748 if (fstat(fileno(f), &sbuf) < 0) {
749 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
750 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
751 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
757 * scan_authfile - Scan an authorization file for a secret suitable
758 * for authenticating `client' on `server'. The return value is -1
759 * if no secret is found, otherwise >= 0. The return value has
760 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
761 * NONWILD_SERVER set if the secret didn't have "*" for the server.
762 * Any following words on the line (i.e. address authorization
763 * info) are placed in a wordlist and returned in *addrs.
766 scan_authfile(f, client, server, secret, addrs, filename)
771 struct wordlist **addrs;
775 int got_flag, best_flag;
777 struct wordlist *ap, *addr_list, *addr_last;
778 char word[MAXWORDLEN];
779 char atfile[MAXWORDLEN];
784 if (!getword(f, word, &newline, filename))
785 return -1; /* file is empty??? */
790 * Skip until we find a word at the start of a line.
792 while (!newline && getword(f, word, &newline, filename))
795 break; /* got to end of file */
798 * Got a client - check if it's a match or a wildcard.
801 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
806 got_flag = NONWILD_CLIENT;
809 * Now get a server and check if it matches.
811 if (!getword(f, word, &newline, filename))
815 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
818 got_flag |= NONWILD_SERVER;
821 * Got some sort of a match - see if it's better than what
824 if (got_flag <= best_flag)
830 if (!getword(f, word, &newline, filename))
836 * Special syntax: @filename means read secret from file.
838 if (word[0] == '@') {
839 strcpy(atfile, word+1);
840 if ((sf = fopen(atfile, "r")) == NULL) {
841 syslog(LOG_WARNING, "can't open indirect secret file %s",
845 check_access(sf, atfile);
846 if (!getword(sf, word, &xxx, atfile)) {
847 syslog(LOG_WARNING, "no secret in indirect secret file %s",
855 strcpy(secret, word);
857 best_flag = got_flag;
860 * Now read address authorization info and make a wordlist.
863 free_wordlist(addr_list);
866 if (!getword(f, word, &newline, filename) || newline)
868 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
871 novm("authorized addresses");
873 strcpy(ap->word, word);
874 if (addr_list == NULL)
877 addr_last->next = ap;
886 else if (addr_list != NULL)
887 free_wordlist(addr_list);
893 * free_wordlist - release memory allocated for a wordlist.
899 struct wordlist *next;