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.20 1995/12/18 03:43:04 paulus Exp $";
45 #include <sys/types.h>
47 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
55 #include <shadow/pwauth.h>
57 #define PW_PPP PW_LOGIN
65 #include "pathnames.h"
67 #if defined(sun) && defined(sparc)
71 /* Used for storing a sequence of words. Usually malloced. */
73 struct wordlist *next;
77 /* Bits in scan_authfile return value */
78 #define NONWILD_SERVER 1
79 #define NONWILD_CLIENT 2
81 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
86 /* Records which authentication operations haven't completed yet. */
87 static int auth_pending[NUM_PPP];
89 static struct wordlist *addresses[NUM_PPP];
91 /* Bits in auth_pending[] */
92 #define UPAP_WITHPEER 1
94 #define CHAP_WITHPEER 4
97 /* Prototypes for procedures local to this file. */
99 static void network_phase __P((int));
100 static int login __P((char *, char *, char **, int *));
101 static void logout __P((void));
102 static int null_login __P((int));
103 static int get_upap_passwd __P((void));
104 static int have_upap_secret __P((void));
105 static int have_chap_secret __P((char *, char *));
106 static int scan_authfile __P((FILE *, char *, char *, char *,
107 struct wordlist **, char *));
108 static void free_wordlist __P((struct wordlist *));
110 extern char *crypt __P((char *, char *));
113 * An Open on LCP has requested a change from Dead to Establish phase.
114 * Do what's necessary to bring the physical layer up.
123 * LCP has terminated the link; go to the Dead phase and take the
124 * physical layer down.
127 link_terminated(unit)
130 if (phase == PHASE_DEAD)
135 syslog(LOG_NOTICE, "Connection terminated.");
139 * LCP has gone down; it will either die or try to re-establish.
146 struct protent *protp;
148 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
149 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
150 (*protp->lowerdown)(unit);
151 if (protp->protocol < 0xC000 && protp->close != NULL)
152 (*protp->close)(unit);
154 phase = PHASE_TERMINATE;
158 * The link is established.
159 * Proceed to the Dead, Authenticate or Network phase as appropriate.
162 link_established(unit)
166 lcp_options *wo = &lcp_wantoptions[unit];
167 lcp_options *go = &lcp_gotoptions[unit];
168 lcp_options *ho = &lcp_hisoptions[unit];
170 struct protent *protp;
173 * Tell higher-level protocols that LCP is up.
175 for (i = 0; (protp = protocols[i]) != NULL; ++i)
176 if (protp->protocol != PPP_LCP && protp->lowerup != NULL)
177 (*protp->lowerup)(unit);
179 if (auth_required && !(go->neg_chap || go->neg_upap)) {
181 * We wanted the peer to authenticate itself, and it refused:
182 * treat it as though it authenticated with PAP using a username
183 * of "" and a password of "". If that's not OK, boot it out.
185 if (!wo->neg_upap || !null_login(unit)) {
186 syslog(LOG_WARNING, "peer refused to authenticate");
187 lcp_close(unit, "peer refused to authenticate");
188 phase = PHASE_TERMINATE;
193 phase = PHASE_AUTHENTICATE;
196 ChapAuthPeer(unit, our_name, go->chap_mdtype);
198 } else if (go->neg_upap) {
203 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
204 auth |= CHAP_WITHPEER;
205 } else if (ho->neg_upap) {
206 upap_authwithpeer(unit, user, passwd);
207 auth |= UPAP_WITHPEER;
209 auth_pending[unit] = auth;
216 * Proceed to the network phase.
223 struct protent *protp;
225 phase = PHASE_NETWORK;
226 for (i = 0; (protp = protocols[i]) != NULL; ++i)
227 if (protp->protocol < 0xC000 && protp->open != NULL)
228 (*protp->open)(unit);
232 * The peer has failed to authenticate himself using `protocol'.
235 auth_peer_fail(unit, protocol)
239 * Authentication failure: take the link down
241 lcp_close(unit, "Authentication failed");
242 phase = PHASE_TERMINATE;
246 * The peer has been successfully authenticated using `protocol'.
249 auth_peer_success(unit, protocol)
262 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
268 * If there is no more authentication still to be done,
269 * proceed to the network phase.
271 if ((auth_pending[unit] &= ~bit) == 0)
276 * We have failed to authenticate ourselves to the peer using `protocol'.
279 auth_withpeer_fail(unit, protocol)
283 * We've failed to authenticate ourselves to our peer.
284 * He'll probably take the link down, and there's not much
285 * we can do except wait for that.
290 * We have successfully authenticated ourselves with the peer using `protocol'.
293 auth_withpeer_success(unit, protocol)
306 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
312 * If there is no more authentication still being done,
313 * proceed to the network phase.
315 if ((auth_pending[unit] &= ~bit) == 0)
321 * check_auth_options - called to check authentication options.
326 lcp_options *wo = &lcp_wantoptions[0];
327 lcp_options *ao = &lcp_allowoptions[0];
329 /* Default our_name to hostname, and user to our_name */
330 if (our_name[0] == 0 || usehostname)
331 strcpy(our_name, hostname);
333 strcpy(user, our_name);
335 /* If authentication is required, ask peer for CHAP or PAP. */
336 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
342 * Check whether we have appropriate secrets to use
343 * to authenticate ourselves and/or the peer.
345 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
347 if (wo->neg_upap && !uselogin && !have_upap_secret())
349 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
351 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
354 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
356 pppd: peer authentication required but no authentication files accessible\n");
364 * check_passwd - Check the user name and passwd against the PAP secrets
365 * file. If requested, also check against the system password database,
366 * and login the user if OK.
369 * UPAP_AUTHNAK: Authentication failed.
370 * UPAP_AUTHACK: Authentication succeeded.
371 * In either case, msg points to an appropriate message.
374 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
386 struct wordlist *addrs;
387 char passwd[256], user[256];
388 char secret[MAXWORDLEN];
389 static int attempts = 0;
392 * Make copies of apasswd and auser, then null-terminate them.
394 BCOPY(apasswd, passwd, passwdlen);
395 passwd[passwdlen] = '\0';
396 BCOPY(auser, user, userlen);
397 user[userlen] = '\0';
400 * Open the file of upap secrets and scan for a suitable secret
401 * for authenticating this user.
403 filename = _PATH_UPAPFILE;
406 f = fopen(filename, "r");
409 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
414 check_access(f, filename);
415 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
416 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
417 && strcmp(crypt(passwd, secret), secret) != 0)) {
418 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
424 if (uselogin && ret == UPAP_AUTHACK) {
425 ret = login(user, passwd, msg, msglen);
426 if (ret == UPAP_AUTHNAK) {
427 syslog(LOG_WARNING, "PAP login failure for %s", user);
431 if (ret == UPAP_AUTHNAK) {
432 *msg = "Login incorrect";
433 *msglen = strlen(*msg);
435 * Frustrate passwd stealer programs.
436 * Allow 10 tries, but start backing off after 3 (stolen from login).
437 * On 10'th, drop the connection.
439 if (attempts++ >= 10) {
440 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
441 attempts, devnam, user);
445 sleep((u_int) (attempts - 3) * 5);
447 free_wordlist(addrs);
450 attempts = 0; /* Reset count */
452 *msglen = strlen(*msg);
453 if (addresses[unit] != NULL)
454 free_wordlist(addresses[unit]);
455 addresses[unit] = addrs;
463 * login - Check the user name and password against the system
464 * password database, and login the user if OK.
467 * UPAP_AUTHNAK: Login failed.
468 * UPAP_AUTHACK: Login succeeded.
469 * In either case, msg points to an appropriate message.
472 login(user, passwd, msg, msglen)
484 struct spwd *getspnam();
487 if ((pw = getpwnam(user)) == NULL) {
488 return (UPAP_AUTHNAK);
492 if ((spwd = getspnam(user)) == NULL) {
495 pw->pw_passwd = spwd->sp_pwdp;
500 * XXX If no passwd, let them login without one.
502 if (pw->pw_passwd == '\0') {
503 return (UPAP_AUTHACK);
507 if ((pw->pw_passwd && pw->pw_passwd[0] == '@'
508 && pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL))
509 || !valid (passwd, pw)) {
510 return (UPAP_AUTHNAK);
513 epasswd = crypt(passwd, pw->pw_passwd);
514 if (strcmp(epasswd, pw->pw_passwd)) {
515 return (UPAP_AUTHNAK);
519 syslog(LOG_INFO, "user %s logged in", user);
522 * Write a wtmp entry for this user.
525 if (strncmp(tty, "/dev/", 5) == 0)
527 logwtmp(tty, user, ""); /* Add wtmp login entry */
530 return (UPAP_AUTHACK);
534 * logout - Logout the user.
542 if (strncmp(tty, "/dev/", 5) == 0)
544 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
550 * null_login - Check if a username of "" and a password of "" are
551 * acceptable, and iff so, set the list of acceptable IP addresses
561 struct wordlist *addrs;
562 char secret[MAXWORDLEN];
565 * Open the file of upap secrets and scan for a suitable secret.
566 * We don't accept a wildcard client.
568 filename = _PATH_UPAPFILE;
570 f = fopen(filename, "r");
573 check_access(f, filename);
575 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
576 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
579 if (addresses[unit] != NULL)
580 free_wordlist(addresses[unit]);
581 addresses[unit] = addrs;
590 * get_upap_passwd - get a password for authenticating ourselves with
591 * our peer using PAP. Returns 1 on success, 0 if no suitable password
599 struct wordlist *addrs;
600 char secret[MAXWORDLEN];
602 filename = _PATH_UPAPFILE;
604 f = fopen(filename, "r");
607 check_access(f, filename);
608 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
610 strncpy(passwd, secret, MAXSECRETLEN);
611 passwd[MAXSECRETLEN-1] = 0;
617 * have_upap_secret - check whether we have a PAP file with any
618 * secrets that we could possibly use for authenticating the peer.
627 filename = _PATH_UPAPFILE;
628 f = fopen(filename, "r");
632 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
642 * have_chap_secret - check whether we have a CHAP file with a
643 * secret that we could possibly use for authenticating `client'
644 * on `server'. Either can be the null string, meaning we don't
645 * know the identity yet.
648 have_chap_secret(client, server)
656 filename = _PATH_CHAPFILE;
657 f = fopen(filename, "r");
663 else if (server[0] == 0)
666 ret = scan_authfile(f, client, server, NULL, NULL, filename);
676 * get_secret - open the CHAP secret file and return the secret
677 * for authenticating the given client on the given server.
678 * (We could be either client or server).
681 get_secret(unit, client, server, secret, secret_len, save_addrs)
692 struct wordlist *addrs;
693 char secbuf[MAXWORDLEN];
695 filename = _PATH_CHAPFILE;
699 f = fopen(filename, "r");
701 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
704 check_access(f, filename);
706 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
712 if (addresses[unit] != NULL)
713 free_wordlist(addresses[unit]);
714 addresses[unit] = addrs;
717 len = strlen(secbuf);
718 if (len > MAXSECRETLEN) {
719 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
722 BCOPY(secbuf, secret, len);
729 * auth_ip_addr - check whether the peer is authorized to use
730 * a given IP address. Returns 1 if authorized, 0 otherwise.
733 auth_ip_addr(unit, addr)
739 char *ptr_word, *ptr_mask;
742 struct wordlist *addrs;
744 /* don't allow loopback or multicast address */
745 if (bad_ip_adrs(addr))
748 if ((addrs = addresses[unit]) == NULL)
749 return 1; /* no restriction */
751 for (; addrs != NULL; addrs = addrs->next) {
752 /* "-" means no addresses authorized */
753 ptr_word = addrs->word;
754 if (strcmp(ptr_word, "-") == 0)
758 if (*ptr_word == '!') {
763 mask = ~ (u_int32_t) 0;
764 ptr_mask = strchr (ptr_word, '/');
765 if (ptr_mask != NULL) {
768 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
769 if (bit_count <= 0 || bit_count > 32) {
771 "invalid address length %s in auth. address list",
776 mask <<= 32 - bit_count;
779 hp = gethostbyname(ptr_word);
780 if (hp != NULL && hp->h_addrtype == AF_INET) {
781 a = *(u_int32_t *)hp->h_addr;
782 mask = ~ (u_int32_t) 0; /* are we sure we want this? */
784 np = getnetbyname (ptr_word);
785 if (np != NULL && np->n_addrtype == AF_INET)
786 a = htonl (*(u_int32_t *)np->n_net);
788 a = inet_addr (ptr_word);
791 if (ptr_mask != NULL)
796 "unknown host %s in auth. address list",
799 if (((addr ^ a) & mask) == 0)
802 return 0; /* not in list => can't have it */
806 * bad_ip_adrs - return 1 if the IP address is one we don't want
807 * to use, such as an address in the loopback net or a multicast address.
808 * addr is in network byte order.
815 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
816 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
820 * check_access - complain if a secret file has too-liberal permissions.
823 check_access(f, filename)
829 if (fstat(fileno(f), &sbuf) < 0) {
830 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
831 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
832 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
838 * scan_authfile - Scan an authorization file for a secret suitable
839 * for authenticating `client' on `server'. The return value is -1
840 * if no secret is found, otherwise >= 0. The return value has
841 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
842 * NONWILD_SERVER set if the secret didn't have "*" for the server.
843 * Any following words on the line (i.e. address authorization
844 * info) are placed in a wordlist and returned in *addrs.
847 scan_authfile(f, client, server, secret, addrs, filename)
852 struct wordlist **addrs;
856 int got_flag, best_flag;
858 struct wordlist *ap, *addr_list, *addr_last;
859 char word[MAXWORDLEN];
860 char atfile[MAXWORDLEN];
865 if (!getword(f, word, &newline, filename))
866 return -1; /* file is empty??? */
871 * Skip until we find a word at the start of a line.
873 while (!newline && getword(f, word, &newline, filename))
876 break; /* got to end of file */
879 * Got a client - check if it's a match or a wildcard.
882 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
887 got_flag = NONWILD_CLIENT;
890 * Now get a server and check if it matches.
892 if (!getword(f, word, &newline, filename))
896 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
899 got_flag |= NONWILD_SERVER;
902 * Got some sort of a match - see if it's better than what
905 if (got_flag <= best_flag)
911 if (!getword(f, word, &newline, filename))
917 * Special syntax: @filename means read secret from file.
919 if (word[0] == '@') {
920 strcpy(atfile, word+1);
921 if ((sf = fopen(atfile, "r")) == NULL) {
922 syslog(LOG_WARNING, "can't open indirect secret file %s",
926 check_access(sf, atfile);
927 if (!getword(sf, word, &xxx, atfile)) {
928 syslog(LOG_WARNING, "no secret in indirect secret file %s",
936 strcpy(secret, word);
938 best_flag = got_flag;
941 * Now read address authorization info and make a wordlist.
944 free_wordlist(addr_list);
945 addr_list = addr_last = NULL;
947 if (!getword(f, word, &newline, filename) || newline)
949 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
952 novm("authorized addresses");
954 strcpy(ap->word, word);
955 if (addr_list == NULL)
958 addr_last->next = ap;
967 else if (addr_list != NULL)
968 free_wordlist(addr_list);
974 * free_wordlist - release memory allocated for a wordlist.
980 struct wordlist *next;