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.15.2.1 1995/06/01 07:01:21 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(sparc) && !defined(NeXT)
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 /* Set if we have successfully called login() */
94 /* List of addresses which the peer may use. */
95 static struct wordlist *addresses[NUM_PPP];
97 /* Number of network protocols which we have opened. */
98 static int num_np_open;
100 /* Number of network protocols which have come up. */
101 static int num_np_up;
103 /* Bits in auth_pending[] */
104 #define UPAP_WITHPEER 1
106 #define CHAP_WITHPEER 4
110 void check_access __P((FILE *, char *));
112 static void network_phase __P((int));
113 static void check_idle __P((caddr_t));
114 static int login __P((char *, char *, char **, int *));
115 static void logout __P((void));
116 static int null_login __P((int));
117 static int get_upap_passwd __P((void));
118 static int have_upap_secret __P((void));
119 static int have_chap_secret __P((char *, char *));
120 static int scan_authfile __P((FILE *, char *, char *, char *,
121 struct wordlist **, char *));
122 static void free_wordlist __P((struct wordlist *));
124 extern char *crypt __P((char *, char *));
127 * An Open on LCP has requested a change from Dead to Establish phase.
128 * Do what's necessary to bring the physical layer up.
137 * LCP has terminated the link; go to the Dead phase and take the
138 * physical layer down.
141 link_terminated(unit)
144 if (phase == PHASE_DEAD)
149 syslog(LOG_NOTICE, "Connection terminated.");
153 * LCP has gone down; it will either die or try to re-establish.
161 phase = PHASE_TERMINATE;
165 * The link is established.
166 * Proceed to the Dead, Authenticate or Network phase as appropriate.
169 link_established(unit)
173 lcp_options *wo = &lcp_wantoptions[unit];
174 lcp_options *go = &lcp_gotoptions[unit];
175 lcp_options *ho = &lcp_hisoptions[unit];
177 if (auth_required && !(go->neg_chap || go->neg_upap)) {
179 * We wanted the peer to authenticate itself, and it refused:
180 * treat it as though it authenticated with PAP using a username
181 * of "" and a password of "". If that's not OK, boot it out.
183 if (!wo->neg_upap || !null_login(unit)) {
184 syslog(LOG_WARNING, "peer refused to authenticate");
186 phase = PHASE_TERMINATE;
191 phase = PHASE_AUTHENTICATE;
194 ChapAuthPeer(unit, our_name, go->chap_mdtype);
196 } else if (go->neg_upap) {
201 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
202 auth |= CHAP_WITHPEER;
203 } else if (ho->neg_upap) {
204 upap_authwithpeer(unit, user, passwd);
205 auth |= UPAP_WITHPEER;
207 auth_pending[unit] = auth;
214 * Proceed to the network phase.
220 phase = PHASE_NETWORK;
227 * The peer has failed to authenticate himself using `protocol'.
230 auth_peer_fail(unit, protocol)
234 * Authentication failure: take the link down
237 phase = PHASE_TERMINATE;
241 * The peer has been successfully authenticated using `protocol'.
244 auth_peer_success(unit, protocol)
257 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
263 * If there is no more authentication still to be done,
264 * proceed to the network phase.
266 if ((auth_pending[unit] &= ~bit) == 0) {
267 phase = PHASE_NETWORK;
274 * We have failed to authenticate ourselves to the peer using `protocol'.
277 auth_withpeer_fail(unit, protocol)
281 * We've failed to authenticate ourselves to our peer.
282 * He'll probably take the link down, and there's not much
283 * we can do except wait for that.
288 * We have successfully authenticated ourselves with the peer using `protocol'.
291 auth_withpeer_success(unit, protocol)
304 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
310 * If there is no more authentication still being done,
311 * proceed to the network phase.
313 if ((auth_pending[unit] &= ~bit) == 0)
319 * np_up - a network protocol has come up.
325 if (num_np_up == 0 && idle_time_limit > 0) {
326 TIMEOUT(check_idle, NULL, idle_time_limit);
332 * np_down - a network protocol has gone down.
338 if (--num_np_up == 0 && idle_time_limit > 0) {
339 UNTIMEOUT(check_idle, NULL);
344 * np_finished - a network protocol has finished using the link.
347 np_finished(unit, proto)
350 if (--num_np_open <= 0) {
351 /* no further use for the link: shut up shop. */
357 * check_idle - check whether the link has been idle for long
358 * enough that we can shut it down.
364 struct ppp_idle idle;
367 if (!get_idle_time(0, &idle))
369 itime = MIN(idle.xmit_idle, idle.recv_idle);
370 if (itime >= idle_time_limit) {
371 /* link is idle: shut it down. */
372 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
375 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
380 * check_auth_options - called to check authentication options.
385 lcp_options *wo = &lcp_wantoptions[0];
386 lcp_options *ao = &lcp_allowoptions[0];
388 /* Default our_name to hostname, and user to our_name */
389 if (our_name[0] == 0 || usehostname)
390 strcpy(our_name, hostname);
392 strcpy(user, our_name);
394 /* If authentication is required, ask peer for CHAP or PAP. */
395 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
401 * Check whether we have appropriate secrets to use
402 * to authenticate ourselves and/or the peer.
404 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
406 if (wo->neg_upap && !uselogin && !have_upap_secret())
408 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
410 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
413 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
415 pppd: peer authentication required but no authentication files accessible\n");
423 * check_passwd - Check the user name and passwd against the PAP secrets
424 * file. If requested, also check against the system password database,
425 * and login the user if OK.
428 * UPAP_AUTHNAK: Authentication failed.
429 * UPAP_AUTHACK: Authentication succeeded.
430 * In either case, msg points to an appropriate message.
433 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
445 struct wordlist *addrs;
446 char passwd[256], user[256];
447 char secret[MAXWORDLEN];
448 static int attempts = 0;
451 * Make copies of apasswd and auser, then null-terminate them.
453 BCOPY(apasswd, passwd, passwdlen);
454 passwd[passwdlen] = '\0';
455 BCOPY(auser, user, userlen);
456 user[userlen] = '\0';
459 * Open the file of upap secrets and scan for a suitable secret
460 * for authenticating this user.
462 filename = _PATH_UPAPFILE;
465 f = fopen(filename, "r");
468 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
473 check_access(f, filename);
474 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
475 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
476 && strcmp(crypt(passwd, secret), secret) != 0)) {
477 syslog(LOG_WARNING, "upap authentication failure for %s", user);
483 if (uselogin && ret == UPAP_AUTHACK) {
484 ret = login(user, passwd, msg, msglen);
485 if (ret == UPAP_AUTHNAK) {
486 syslog(LOG_WARNING, "upap login failure for %s", user);
490 if (ret == UPAP_AUTHNAK) {
491 *msg = "Login incorrect";
492 *msglen = strlen(*msg);
494 * Frustrate passwd stealer programs.
495 * Allow 10 tries, but start backing off after 3 (stolen from login).
496 * On 10'th, drop the connection.
498 if (attempts++ >= 10) {
499 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
500 attempts, devnam, user);
504 sleep((u_int) (attempts - 3) * 5);
506 free_wordlist(addrs);
509 attempts = 0; /* Reset count */
511 *msglen = strlen(*msg);
512 if (addresses[unit] != NULL)
513 free_wordlist(addresses[unit]);
514 addresses[unit] = addrs;
522 * login - Check the user name and password against the system
523 * password database, and login the user if OK.
526 * UPAP_AUTHNAK: Login failed.
527 * UPAP_AUTHACK: Login succeeded.
528 * In either case, msg points to an appropriate message.
531 login(user, passwd, msg, msglen)
543 struct spwd *getspnam();
546 if ((pw = getpwnam(user)) == NULL) {
547 return (UPAP_AUTHNAK);
551 if ((spwd = getspnam(user)) == NULL) {
554 pw->pw_passwd = spwd->sp_pwdp;
559 * XXX If no passwd, let them login without one.
561 if (pw->pw_passwd == '\0') {
562 return (UPAP_AUTHACK);
566 if ((pw->pw_passwd && pw->pw_passwd[0] == '@'
567 && pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL))
568 || !valid (passwd, pw)) {
569 return (UPAP_AUTHNAK);
572 epasswd = crypt(passwd, pw->pw_passwd);
573 if (strcmp(epasswd, pw->pw_passwd)) {
574 return (UPAP_AUTHNAK);
578 syslog(LOG_INFO, "user %s logged in", user);
581 * Write a wtmp entry for this user.
583 tty = strrchr(devnam, '/');
588 logwtmp(tty, user, ""); /* Add wtmp login entry */
591 return (UPAP_AUTHACK);
595 * logout - Logout the user.
602 tty = strrchr(devnam, '/');
607 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
613 * null_login - Check if a username of "" and a password of "" are
614 * acceptable, and iff so, set the list of acceptable IP addresses
624 struct wordlist *addrs;
625 char secret[MAXWORDLEN];
628 * Open the file of upap secrets and scan for a suitable secret.
629 * We don't accept a wildcard client.
631 filename = _PATH_UPAPFILE;
633 f = fopen(filename, "r");
636 check_access(f, filename);
638 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
639 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
642 if (addresses[unit] != NULL)
643 free_wordlist(addresses[unit]);
644 addresses[unit] = addrs;
653 * get_upap_passwd - get a password for authenticating ourselves with
654 * our peer using PAP. Returns 1 on success, 0 if no suitable password
662 struct wordlist *addrs;
663 char secret[MAXWORDLEN];
665 filename = _PATH_UPAPFILE;
667 f = fopen(filename, "r");
670 check_access(f, filename);
671 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
673 strncpy(passwd, secret, MAXSECRETLEN);
674 passwd[MAXSECRETLEN-1] = 0;
680 * have_upap_secret - check whether we have a PAP file with any
681 * secrets that we could possibly use for authenticating the peer.
690 filename = _PATH_UPAPFILE;
691 f = fopen(filename, "r");
695 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
705 * have_chap_secret - check whether we have a CHAP file with a
706 * secret that we could possibly use for authenticating `client'
707 * on `server'. Either can be the null string, meaning we don't
708 * know the identity yet.
711 have_chap_secret(client, server)
719 filename = _PATH_CHAPFILE;
720 f = fopen(filename, "r");
726 else if (server[0] == 0)
729 ret = scan_authfile(f, client, server, NULL, NULL, filename);
739 * get_secret - open the CHAP secret file and return the secret
740 * for authenticating the given client on the given server.
741 * (We could be either client or server).
744 get_secret(unit, client, server, secret, secret_len, save_addrs)
755 struct wordlist *addrs;
756 char secbuf[MAXWORDLEN];
758 filename = _PATH_CHAPFILE;
762 f = fopen(filename, "r");
764 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
767 check_access(f, filename);
769 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
775 if (addresses[unit] != NULL)
776 free_wordlist(addresses[unit]);
777 addresses[unit] = addrs;
780 len = strlen(secbuf);
781 if (len > MAXSECRETLEN) {
782 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
785 BCOPY(secbuf, secret, len);
792 * auth_ip_addr - check whether the peer is authorized to use
793 * a given IP address. Returns 1 if authorized, 0 otherwise.
796 auth_ip_addr(unit, addr)
802 struct wordlist *addrs;
804 /* don't allow loopback or multicast address */
805 if (bad_ip_adrs(addr))
808 if ((addrs = addresses[unit]) == NULL)
809 return 1; /* no restriction */
811 for (; addrs != NULL; addrs = addrs->next) {
812 /* "-" means no addresses authorized */
813 if (strcmp(addrs->word, "-") == 0)
815 if ((a = inet_addr(addrs->word)) == -1) {
816 if ((hp = gethostbyname(addrs->word)) == NULL) {
817 syslog(LOG_WARNING, "unknown host %s in auth. address list",
821 a = *(u_int32_t *)hp->h_addr;
826 return 0; /* not in list => can't have it */
830 * bad_ip_adrs - return 1 if the IP address is one we don't want
831 * to use, such as an address in the loopback net or a multicast address.
832 * addr is in network byte order.
839 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
840 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
844 * check_access - complain if a secret file has too-liberal permissions.
847 check_access(f, filename)
853 if (fstat(fileno(f), &sbuf) < 0) {
854 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
855 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
856 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
862 * scan_authfile - Scan an authorization file for a secret suitable
863 * for authenticating `client' on `server'. The return value is -1
864 * if no secret is found, otherwise >= 0. The return value has
865 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
866 * NONWILD_SERVER set if the secret didn't have "*" for the server.
867 * Any following words on the line (i.e. address authorization
868 * info) are placed in a wordlist and returned in *addrs.
871 scan_authfile(f, client, server, secret, addrs, filename)
876 struct wordlist **addrs;
880 int got_flag, best_flag;
882 struct wordlist *ap, *addr_list, *addr_last;
883 char word[MAXWORDLEN];
884 char atfile[MAXWORDLEN];
889 if (!getword(f, word, &newline, filename))
890 return -1; /* file is empty??? */
895 * Skip until we find a word at the start of a line.
897 while (!newline && getword(f, word, &newline, filename))
900 break; /* got to end of file */
903 * Got a client - check if it's a match or a wildcard.
906 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
911 got_flag = NONWILD_CLIENT;
914 * Now get a server and check if it matches.
916 if (!getword(f, word, &newline, filename))
920 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
923 got_flag |= NONWILD_SERVER;
926 * Got some sort of a match - see if it's better than what
929 if (got_flag <= best_flag)
935 if (!getword(f, word, &newline, filename))
941 * Special syntax: @filename means read secret from file.
943 if (word[0] == '@') {
944 strcpy(atfile, word+1);
945 if ((sf = fopen(atfile, "r")) == NULL) {
946 syslog(LOG_WARNING, "can't open indirect secret file %s",
950 check_access(sf, atfile);
951 if (!getword(sf, word, &xxx, atfile)) {
952 syslog(LOG_WARNING, "no secret in indirect secret file %s",
960 strcpy(secret, word);
962 best_flag = got_flag;
965 * Now read address authorization info and make a wordlist.
968 free_wordlist(addr_list);
969 addr_list = addr_last = NULL;
971 if (!getword(f, word, &newline, filename) || newline)
973 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
976 novm("authorized addresses");
978 strcpy(ap->word, word);
979 if (addr_list == NULL)
982 addr_last->next = ap;
991 else if (addr_list != NULL)
992 free_wordlist(addr_list);
998 * free_wordlist - release memory allocated for a wordlist.
1002 struct wordlist *wp;
1004 struct wordlist *next;
1006 while (wp != NULL) {