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.19 1995/12/11 05:17:42 paulus Exp $";
45 #include <sys/types.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
54 #include <shadow/pwauth.h>
56 #define PW_PPP PW_LOGIN
67 #include "pathnames.h"
69 #if defined(sun) && defined(sparc)
73 /* Used for storing a sequence of words. Usually malloced. */
75 struct wordlist *next;
79 /* Bits in scan_authfile return value */
80 #define NONWILD_SERVER 1
81 #define NONWILD_CLIENT 2
83 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
88 /* Records which authentication operations haven't completed yet. */
89 static int auth_pending[NUM_PPP];
91 static struct wordlist *addresses[NUM_PPP];
93 /* Bits in auth_pending[] */
94 #define UPAP_WITHPEER 1
96 #define CHAP_WITHPEER 4
99 /* Prototypes for procedures local to this file. */
101 static void network_phase __P((int));
102 static int login __P((char *, char *, char **, int *));
103 static void logout __P((void));
104 static int null_login __P((int));
105 static int get_upap_passwd __P((void));
106 static int have_upap_secret __P((void));
107 static int have_chap_secret __P((char *, char *));
108 static int scan_authfile __P((FILE *, char *, char *, char *,
109 struct wordlist **, char *));
110 static void free_wordlist __P((struct wordlist *));
112 extern char *crypt __P((char *, char *));
115 * An Open on LCP has requested a change from Dead to Establish phase.
116 * Do what's necessary to bring the physical layer up.
125 * LCP has terminated the link; go to the Dead phase and take the
126 * physical layer down.
129 link_terminated(unit)
132 if (phase == PHASE_DEAD)
137 syslog(LOG_NOTICE, "Connection terminated.");
141 * LCP has gone down; it will either die or try to re-establish.
149 phase = PHASE_TERMINATE;
153 * The link is established.
154 * Proceed to the Dead, Authenticate or Network phase as appropriate.
157 link_established(unit)
161 lcp_options *wo = &lcp_wantoptions[unit];
162 lcp_options *go = &lcp_gotoptions[unit];
163 lcp_options *ho = &lcp_hisoptions[unit];
165 if (auth_required && !(go->neg_chap || go->neg_upap)) {
167 * We wanted the peer to authenticate itself, and it refused:
168 * treat it as though it authenticated with PAP using a username
169 * of "" and a password of "". If that's not OK, boot it out.
171 if (!wo->neg_upap || !null_login(unit)) {
172 syslog(LOG_WARNING, "peer refused to authenticate");
174 phase = PHASE_TERMINATE;
179 phase = PHASE_AUTHENTICATE;
182 ChapAuthPeer(unit, our_name, go->chap_mdtype);
184 } else if (go->neg_upap) {
189 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
190 auth |= CHAP_WITHPEER;
191 } else if (ho->neg_upap) {
192 upap_authwithpeer(unit, user, passwd);
193 auth |= UPAP_WITHPEER;
195 auth_pending[unit] = auth;
202 * Proceed to the network phase.
208 phase = PHASE_NETWORK;
214 * The peer has failed to authenticate himself using `protocol'.
217 auth_peer_fail(unit, protocol)
221 * Authentication failure: take the link down
224 phase = PHASE_TERMINATE;
228 * The peer has been successfully authenticated using `protocol'.
231 auth_peer_success(unit, protocol)
244 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
250 * If there is no more authentication still to be done,
251 * proceed to the network phase.
253 if ((auth_pending[unit] &= ~bit) == 0) {
254 phase = PHASE_NETWORK;
261 * We have failed to authenticate ourselves to the peer using `protocol'.
264 auth_withpeer_fail(unit, protocol)
268 * We've failed to authenticate ourselves to our peer.
269 * He'll probably take the link down, and there's not much
270 * we can do except wait for that.
275 * We have successfully authenticated ourselves with the peer using `protocol'.
278 auth_withpeer_success(unit, protocol)
291 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
297 * If there is no more authentication still being done,
298 * proceed to the network phase.
300 if ((auth_pending[unit] &= ~bit) == 0)
306 * check_auth_options - called to check authentication options.
311 lcp_options *wo = &lcp_wantoptions[0];
312 lcp_options *ao = &lcp_allowoptions[0];
314 /* Default our_name to hostname, and user to our_name */
315 if (our_name[0] == 0 || usehostname)
316 strcpy(our_name, hostname);
318 strcpy(user, our_name);
320 /* If authentication is required, ask peer for CHAP or PAP. */
321 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
327 * Check whether we have appropriate secrets to use
328 * to authenticate ourselves and/or the peer.
330 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
332 if (wo->neg_upap && !uselogin && !have_upap_secret())
334 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
336 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
339 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
341 pppd: peer authentication required but no authentication files accessible\n");
349 * check_passwd - Check the user name and passwd against the PAP secrets
350 * file. If requested, also check against the system password database,
351 * and login the user if OK.
354 * UPAP_AUTHNAK: Authentication failed.
355 * UPAP_AUTHACK: Authentication succeeded.
356 * In either case, msg points to an appropriate message.
359 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
371 struct wordlist *addrs;
372 char passwd[256], user[256];
373 char secret[MAXWORDLEN];
374 static int attempts = 0;
377 * Make copies of apasswd and auser, then null-terminate them.
379 BCOPY(apasswd, passwd, passwdlen);
380 passwd[passwdlen] = '\0';
381 BCOPY(auser, user, userlen);
382 user[userlen] = '\0';
385 * Open the file of upap secrets and scan for a suitable secret
386 * for authenticating this user.
388 filename = _PATH_UPAPFILE;
391 f = fopen(filename, "r");
394 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
399 check_access(f, filename);
400 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
401 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
402 && strcmp(crypt(passwd, secret), secret) != 0)) {
403 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
409 if (uselogin && ret == UPAP_AUTHACK) {
410 ret = login(user, passwd, msg, msglen);
411 if (ret == UPAP_AUTHNAK) {
412 syslog(LOG_WARNING, "PAP login failure for %s", user);
416 if (ret == UPAP_AUTHNAK) {
417 *msg = "Login incorrect";
418 *msglen = strlen(*msg);
420 * Frustrate passwd stealer programs.
421 * Allow 10 tries, but start backing off after 3 (stolen from login).
422 * On 10'th, drop the connection.
424 if (attempts++ >= 10) {
425 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
426 attempts, devnam, user);
430 sleep((u_int) (attempts - 3) * 5);
432 free_wordlist(addrs);
435 attempts = 0; /* Reset count */
437 *msglen = strlen(*msg);
438 if (addresses[unit] != NULL)
439 free_wordlist(addresses[unit]);
440 addresses[unit] = addrs;
448 * login - Check the user name and password against the system
449 * password database, and login the user if OK.
452 * UPAP_AUTHNAK: Login failed.
453 * UPAP_AUTHACK: Login succeeded.
454 * In either case, msg points to an appropriate message.
457 login(user, passwd, msg, msglen)
469 struct spwd *getspnam();
472 if ((pw = getpwnam(user)) == NULL) {
473 return (UPAP_AUTHNAK);
477 if ((spwd = getspnam(user)) == NULL) {
480 pw->pw_passwd = spwd->sp_pwdp;
485 * XXX If no passwd, let them login without one.
487 if (pw->pw_passwd == '\0') {
488 return (UPAP_AUTHACK);
492 if ((pw->pw_passwd && pw->pw_passwd[0] == '@'
493 && pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL))
494 || !valid (passwd, pw)) {
495 return (UPAP_AUTHNAK);
498 epasswd = crypt(passwd, pw->pw_passwd);
499 if (strcmp(epasswd, pw->pw_passwd)) {
500 return (UPAP_AUTHNAK);
504 syslog(LOG_INFO, "user %s logged in", user);
507 * Write a wtmp entry for this user.
510 if (strncmp(tty, "/dev/", 5) == 0)
512 logwtmp(tty, user, ""); /* Add wtmp login entry */
515 return (UPAP_AUTHACK);
519 * logout - Logout the user.
527 if (strncmp(tty, "/dev/", 5) == 0)
529 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
535 * null_login - Check if a username of "" and a password of "" are
536 * acceptable, and iff so, set the list of acceptable IP addresses
546 struct wordlist *addrs;
547 char secret[MAXWORDLEN];
550 * Open the file of upap secrets and scan for a suitable secret.
551 * We don't accept a wildcard client.
553 filename = _PATH_UPAPFILE;
555 f = fopen(filename, "r");
558 check_access(f, filename);
560 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
561 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
564 if (addresses[unit] != NULL)
565 free_wordlist(addresses[unit]);
566 addresses[unit] = addrs;
575 * get_upap_passwd - get a password for authenticating ourselves with
576 * our peer using PAP. Returns 1 on success, 0 if no suitable password
584 struct wordlist *addrs;
585 char secret[MAXWORDLEN];
587 filename = _PATH_UPAPFILE;
589 f = fopen(filename, "r");
592 check_access(f, filename);
593 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
595 strncpy(passwd, secret, MAXSECRETLEN);
596 passwd[MAXSECRETLEN-1] = 0;
602 * have_upap_secret - check whether we have a PAP file with any
603 * secrets that we could possibly use for authenticating the peer.
612 filename = _PATH_UPAPFILE;
613 f = fopen(filename, "r");
617 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
627 * have_chap_secret - check whether we have a CHAP file with a
628 * secret that we could possibly use for authenticating `client'
629 * on `server'. Either can be the null string, meaning we don't
630 * know the identity yet.
633 have_chap_secret(client, server)
641 filename = _PATH_CHAPFILE;
642 f = fopen(filename, "r");
648 else if (server[0] == 0)
651 ret = scan_authfile(f, client, server, NULL, NULL, filename);
661 * get_secret - open the CHAP secret file and return the secret
662 * for authenticating the given client on the given server.
663 * (We could be either client or server).
666 get_secret(unit, client, server, secret, secret_len, save_addrs)
677 struct wordlist *addrs;
678 char secbuf[MAXWORDLEN];
680 filename = _PATH_CHAPFILE;
684 f = fopen(filename, "r");
686 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
689 check_access(f, filename);
691 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
697 if (addresses[unit] != NULL)
698 free_wordlist(addresses[unit]);
699 addresses[unit] = addrs;
702 len = strlen(secbuf);
703 if (len > MAXSECRETLEN) {
704 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
707 BCOPY(secbuf, secret, len);
714 * auth_ip_addr - check whether the peer is authorized to use
715 * a given IP address. Returns 1 if authorized, 0 otherwise.
718 auth_ip_addr(unit, addr)
724 struct wordlist *addrs;
726 /* don't allow loopback or multicast address */
727 if (bad_ip_adrs(addr))
730 if ((addrs = addresses[unit]) == NULL)
731 return 1; /* no restriction */
733 for (; addrs != NULL; addrs = addrs->next) {
734 /* "-" means no addresses authorized */
735 if (strcmp(addrs->word, "-") == 0)
737 if ((a = inet_addr(addrs->word)) == -1) {
738 if ((hp = gethostbyname(addrs->word)) == NULL) {
739 syslog(LOG_WARNING, "unknown host %s in auth. address list",
743 a = *(u_int32_t *)hp->h_addr;
748 return 0; /* not in list => can't have it */
752 * bad_ip_adrs - return 1 if the IP address is one we don't want
753 * to use, such as an address in the loopback net or a multicast address.
754 * addr is in network byte order.
761 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
762 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
766 * check_access - complain if a secret file has too-liberal permissions.
769 check_access(f, filename)
775 if (fstat(fileno(f), &sbuf) < 0) {
776 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
777 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
778 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
784 * scan_authfile - Scan an authorization file for a secret suitable
785 * for authenticating `client' on `server'. The return value is -1
786 * if no secret is found, otherwise >= 0. The return value has
787 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
788 * NONWILD_SERVER set if the secret didn't have "*" for the server.
789 * Any following words on the line (i.e. address authorization
790 * info) are placed in a wordlist and returned in *addrs.
793 scan_authfile(f, client, server, secret, addrs, filename)
798 struct wordlist **addrs;
802 int got_flag, best_flag;
804 struct wordlist *ap, *addr_list, *addr_last;
805 char word[MAXWORDLEN];
806 char atfile[MAXWORDLEN];
811 if (!getword(f, word, &newline, filename))
812 return -1; /* file is empty??? */
817 * Skip until we find a word at the start of a line.
819 while (!newline && getword(f, word, &newline, filename))
822 break; /* got to end of file */
825 * Got a client - check if it's a match or a wildcard.
828 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
833 got_flag = NONWILD_CLIENT;
836 * Now get a server and check if it matches.
838 if (!getword(f, word, &newline, filename))
842 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
845 got_flag |= NONWILD_SERVER;
848 * Got some sort of a match - see if it's better than what
851 if (got_flag <= best_flag)
857 if (!getword(f, word, &newline, filename))
863 * Special syntax: @filename means read secret from file.
865 if (word[0] == '@') {
866 strcpy(atfile, word+1);
867 if ((sf = fopen(atfile, "r")) == NULL) {
868 syslog(LOG_WARNING, "can't open indirect secret file %s",
872 check_access(sf, atfile);
873 if (!getword(sf, word, &xxx, atfile)) {
874 syslog(LOG_WARNING, "no secret in indirect secret file %s",
882 strcpy(secret, word);
884 best_flag = got_flag;
887 * Now read address authorization info and make a wordlist.
890 free_wordlist(addr_list);
891 addr_list = addr_last = NULL;
893 if (!getword(f, word, &newline, filename) || newline)
895 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
898 novm("authorized addresses");
900 strcpy(ap->word, word);
901 if (addr_list == NULL)
904 addr_last->next = ap;
913 else if (addr_list != NULL)
914 free_wordlist(addr_list);
920 * free_wordlist - release memory allocated for a wordlist.
926 struct wordlist *next;