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.13 1995/04/24 06:01:54 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"
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
100 void check_access __P((FILE *, char *));
102 static void network_phase __P((int));
103 static int login __P((char *, char *, char **, int *));
104 static void logout __P((void));
105 static int null_login __P((int));
106 static int get_upap_passwd __P((void));
107 static int have_upap_secret __P((void));
108 static int have_chap_secret __P((char *, char *));
109 static int scan_authfile __P((FILE *, char *, char *, char *,
110 struct wordlist **, char *));
111 static void free_wordlist __P((struct wordlist *));
113 extern char *crypt __P((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)
133 if (phase == PHASE_DEAD)
138 syslog(LOG_NOTICE, "Connection terminated.");
142 * 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;
203 * Proceed to the network phase.
209 phase = PHASE_NETWORK;
215 * The peer has failed to authenticate himself using `protocol'.
218 auth_peer_fail(unit, protocol)
222 * Authentication failure: take the link down
225 phase = PHASE_TERMINATE;
229 * The peer has been successfully authenticated using `protocol'.
232 auth_peer_success(unit, protocol)
245 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
251 * If there is no more authentication still to be done,
252 * proceed to the network phase.
254 if ((auth_pending[unit] &= ~bit) == 0) {
255 phase = PHASE_NETWORK;
262 * We have failed to authenticate ourselves to the peer using `protocol'.
265 auth_withpeer_fail(unit, protocol)
269 * We've failed to authenticate ourselves to our peer.
270 * He'll probably take the link down, and there's not much
271 * we can do except wait for that.
276 * We have successfully authenticated ourselves with the peer using `protocol'.
279 auth_withpeer_success(unit, protocol)
292 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
298 * If there is no more authentication still being done,
299 * proceed to the network phase.
301 if ((auth_pending[unit] &= ~bit) == 0)
307 * check_auth_options - called to check authentication options.
312 lcp_options *wo = &lcp_wantoptions[0];
313 lcp_options *ao = &lcp_allowoptions[0];
315 /* Default our_name to hostname, and user to our_name */
316 if (our_name[0] == 0 || usehostname)
317 strcpy(our_name, hostname);
319 strcpy(user, our_name);
321 /* If authentication is required, ask peer for CHAP or PAP. */
322 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
328 * Check whether we have appropriate secrets to use
329 * to authenticate ourselves and/or the peer.
331 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
333 if (wo->neg_upap && !uselogin && !have_upap_secret())
335 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
337 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
340 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
342 pppd: peer authentication required but no authentication files accessible\n");
350 * check_passwd - Check the user name and passwd against the PAP secrets
351 * file. If requested, also check against the system password database,
352 * and login the user if OK.
355 * UPAP_AUTHNAK: Authentication failed.
356 * UPAP_AUTHACK: Authentication succeeded.
357 * In either case, msg points to an appropriate message.
360 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
372 struct wordlist *addrs;
373 char passwd[256], user[256];
374 char secret[MAXWORDLEN];
375 static int attempts = 0;
378 * Make copies of apasswd and auser, then null-terminate them.
380 BCOPY(apasswd, passwd, passwdlen);
381 passwd[passwdlen] = '\0';
382 BCOPY(auser, user, userlen);
383 user[userlen] = '\0';
386 * Open the file of upap secrets and scan for a suitable secret
387 * for authenticating this user.
389 filename = _PATH_UPAPFILE;
392 f = fopen(filename, "r");
395 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
400 check_access(f, filename);
401 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
402 || (secret[0] != 0 && strcmp(passwd, secret) != 0
403 && strcmp(crypt(passwd, secret), secret) != 0)) {
404 syslog(LOG_WARNING, "upap authentication failure for %s", user);
410 if (uselogin && ret == UPAP_AUTHACK) {
411 ret = login(user, passwd, msg, msglen);
412 if (ret == UPAP_AUTHNAK) {
413 syslog(LOG_WARNING, "upap login failure for %s", user);
417 if (ret == UPAP_AUTHNAK) {
418 *msg = "Login incorrect";
419 *msglen = strlen(*msg);
421 * Frustrate passwd stealer programs.
422 * Allow 10 tries, but start backing off after 3 (stolen from login).
423 * On 10'th, drop the connection.
425 if (attempts++ >= 10) {
426 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
427 attempts, devnam, user);
431 sleep((u_int) (attempts - 3) * 5);
433 free_wordlist(addrs);
436 attempts = 0; /* Reset count */
438 *msglen = strlen(*msg);
439 if (addresses[unit] != NULL)
440 free_wordlist(addresses[unit]);
441 addresses[unit] = addrs;
449 * login - Check the user name and password against the system
450 * password database, and login the user if OK.
453 * UPAP_AUTHNAK: Login failed.
454 * UPAP_AUTHACK: Login succeeded.
455 * In either case, msg points to an appropriate message.
458 login(user, passwd, msg, msglen)
470 struct spwd *getspnam();
473 if ((pw = getpwnam(user)) == NULL) {
474 return (UPAP_AUTHNAK);
478 if ((spwd = getspnam(user)) == NULL) {
481 pw->pw_passwd = spwd->sp_pwdp;
486 * XXX If no passwd, let them login without one.
488 if (pw->pw_passwd == '\0') {
489 return (UPAP_AUTHACK);
493 if ((pw->pw_passwd && pw->pw_passwd[0] == '@'
494 && pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL))
495 || !valid (passwd, pw)) {
496 return (UPAP_AUTHNAK);
499 epasswd = crypt(passwd, pw->pw_passwd);
500 if (strcmp(epasswd, pw->pw_passwd)) {
501 return (UPAP_AUTHNAK);
505 syslog(LOG_INFO, "user %s logged in", user);
508 * Write a wtmp entry for this user.
510 tty = strrchr(devnam, '/');
515 logwtmp(tty, user, ""); /* Add wtmp login entry */
518 return (UPAP_AUTHACK);
522 * logout - Logout the user.
529 tty = strrchr(devnam, '/');
534 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
540 * null_login - Check if a username of "" and a password of "" are
541 * acceptable, and iff so, set the list of acceptable IP addresses
551 struct wordlist *addrs;
552 char secret[MAXWORDLEN];
555 * Open the file of upap secrets and scan for a suitable secret.
556 * We don't accept a wildcard client.
558 filename = _PATH_UPAPFILE;
560 f = fopen(filename, "r");
563 check_access(f, filename);
565 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
566 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
569 if (addresses[unit] != NULL)
570 free_wordlist(addresses[unit]);
571 addresses[unit] = addrs;
580 * get_upap_passwd - get a password for authenticating ourselves with
581 * our peer using PAP. Returns 1 on success, 0 if no suitable password
589 struct wordlist *addrs;
590 char secret[MAXWORDLEN];
592 filename = _PATH_UPAPFILE;
594 f = fopen(filename, "r");
597 check_access(f, filename);
598 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
600 strncpy(passwd, secret, MAXSECRETLEN);
601 passwd[MAXSECRETLEN-1] = 0;
607 * have_upap_secret - check whether we have a PAP file with any
608 * secrets that we could possibly use for authenticating the peer.
617 filename = _PATH_UPAPFILE;
618 f = fopen(filename, "r");
622 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
632 * have_chap_secret - check whether we have a CHAP file with a
633 * secret that we could possibly use for authenticating `client'
634 * on `server'. Either can be the null string, meaning we don't
635 * know the identity yet.
638 have_chap_secret(client, server)
646 filename = _PATH_CHAPFILE;
647 f = fopen(filename, "r");
653 else if (server[0] == 0)
656 ret = scan_authfile(f, client, server, NULL, NULL, filename);
666 * get_secret - open the CHAP secret file and return the secret
667 * for authenticating the given client on the given server.
668 * (We could be either client or server).
671 get_secret(unit, client, server, secret, secret_len, save_addrs)
682 struct wordlist *addrs;
683 char secbuf[MAXWORDLEN];
685 filename = _PATH_CHAPFILE;
689 f = fopen(filename, "r");
691 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
694 check_access(f, filename);
696 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
702 if (addresses[unit] != NULL)
703 free_wordlist(addresses[unit]);
704 addresses[unit] = addrs;
707 len = strlen(secbuf);
708 if (len > MAXSECRETLEN) {
709 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
712 BCOPY(secbuf, secret, len);
719 * auth_ip_addr - check whether the peer is authorized to use
720 * a given IP address. Returns 1 if authorized, 0 otherwise.
723 auth_ip_addr(unit, addr)
729 struct wordlist *addrs;
731 /* don't allow loopback or multicast address */
732 if (bad_ip_adrs(addr))
735 if ((addrs = addresses[unit]) == NULL)
736 return 1; /* no restriction */
738 for (; addrs != NULL; addrs = addrs->next) {
739 /* "-" means no addresses authorized */
740 if (strcmp(addrs->word, "-") == 0)
742 if ((a = inet_addr(addrs->word)) == -1) {
743 if ((hp = gethostbyname(addrs->word)) == NULL) {
744 syslog(LOG_WARNING, "unknown host %s in auth. address list",
748 a = *(u_int32_t *)hp->h_addr;
753 return 0; /* not in list => can't have it */
757 * bad_ip_adrs - return 1 if the IP address is one we don't want
758 * to use, such as an address in the loopback net or a multicast address.
759 * addr is in network byte order.
766 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
767 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
771 * check_access - complain if a secret file has too-liberal permissions.
774 check_access(f, filename)
780 if (fstat(fileno(f), &sbuf) < 0) {
781 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
782 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
783 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
789 * scan_authfile - Scan an authorization file for a secret suitable
790 * for authenticating `client' on `server'. The return value is -1
791 * if no secret is found, otherwise >= 0. The return value has
792 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
793 * NONWILD_SERVER set if the secret didn't have "*" for the server.
794 * Any following words on the line (i.e. address authorization
795 * info) are placed in a wordlist and returned in *addrs.
798 scan_authfile(f, client, server, secret, addrs, filename)
803 struct wordlist **addrs;
807 int got_flag, best_flag;
809 struct wordlist *ap, *addr_list, *addr_last;
810 char word[MAXWORDLEN];
811 char atfile[MAXWORDLEN];
816 if (!getword(f, word, &newline, filename))
817 return -1; /* file is empty??? */
822 * Skip until we find a word at the start of a line.
824 while (!newline && getword(f, word, &newline, filename))
827 break; /* got to end of file */
830 * Got a client - check if it's a match or a wildcard.
833 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
838 got_flag = NONWILD_CLIENT;
841 * Now get a server and check if it matches.
843 if (!getword(f, word, &newline, filename))
847 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
850 got_flag |= NONWILD_SERVER;
853 * Got some sort of a match - see if it's better than what
856 if (got_flag <= best_flag)
862 if (!getword(f, word, &newline, filename))
868 * Special syntax: @filename means read secret from file.
870 if (word[0] == '@') {
871 strcpy(atfile, word+1);
872 if ((sf = fopen(atfile, "r")) == NULL) {
873 syslog(LOG_WARNING, "can't open indirect secret file %s",
877 check_access(sf, atfile);
878 if (!getword(sf, word, &xxx, atfile)) {
879 syslog(LOG_WARNING, "no secret in indirect secret file %s",
887 strcpy(secret, word);
889 best_flag = got_flag;
892 * Now read address authorization info and make a wordlist.
895 free_wordlist(addr_list);
896 addr_list = addr_last = NULL;
898 if (!getword(f, word, &newline, filename) || newline)
900 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
903 novm("authorized addresses");
905 strcpy(ap->word, word);
906 if (addr_list == NULL)
909 addr_last->next = ap;
918 else if (addr_list != NULL)
919 free_wordlist(addr_list);
925 * free_wordlist - release memory allocated for a wordlist.
931 struct wordlist *next;