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.1 1993/11/11 03:54:25 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 get_upap_passwd __ARGS((void));
110 static int have_upap_secret __ARGS((void));
111 static int have_chap_secret __ARGS((char *, char *));
112 static int scan_authfile __ARGS((FILE *, char *, char *, char *,
113 struct wordlist **, char *));
114 static void free_wordlist __ARGS((struct wordlist *));
116 extern char *crypt __ARGS((char *, char *));
119 * An Open on LCP has requested a change from Dead to Establish phase.
120 * Do what's necessary to bring the physical layer up.
129 * LCP has terminated the link; go to the Dead phase and take the
130 * physical layer down.
133 link_terminated(unit)
138 if (lcp_wantoptions[unit].restart) {
146 * The link is established.
147 * Proceed to the Dead, Authenticate or Network phase as appropriate.
150 link_established(unit)
154 lcp_options *wo = &lcp_wantoptions[unit];
155 lcp_options *go = &lcp_gotoptions[unit];
156 lcp_options *ho = &lcp_hisoptions[unit];
158 if (auth_required && !(go->neg_chap || go->neg_upap)) {
160 * We wanted the peer to authenticate himself, and he refused:
161 * tell him to go away.
163 syslog(LOG_WARNING, "peer refused to authenticate");
170 ChapAuthPeer(unit, our_name, go->chap_mdtype);
172 } else if (go->neg_upap) {
177 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
178 auth |= CHAP_WITHPEER;
179 } else if (ho->neg_upap) {
180 upap_authwithpeer(unit, user, passwd);
181 auth |= UPAP_WITHPEER;
183 auth_pending[unit] = auth;
190 * The peer has failed to authenticate himself using `protocol'.
193 auth_peer_fail(unit, protocol)
197 * Authentication failure: take the link down
203 * The peer has been successfully authenticated using `protocol'.
206 auth_peer_success(unit, protocol)
219 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
225 * If there is no more authentication still to be done,
226 * proceed to the network phase.
228 if ((auth_pending[unit] &= ~bit) == 0)
233 * We have failed to authenticate ourselves to the peer using `protocol'.
236 auth_withpeer_fail(unit, protocol)
240 * We've failed to authenticate ourselves to our peer.
241 * He'll probably take the link down, and there's not much
242 * we can do except wait for that.
247 * We have successfully authenticated ourselves with the peer using `protocol'.
250 auth_withpeer_success(unit, protocol)
263 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
268 * If there is no more authentication still being done,
269 * proceed to the network phase.
271 if ((auth_pending[unit] &= ~bit) == 0)
277 * check_auth_options - called to check authentication options.
282 lcp_options *wo = &lcp_wantoptions[0];
283 lcp_options *ao = &lcp_allowoptions[0];
285 /* Default our_name to hostname, and user to our_name */
286 if (our_name[0] == 0 || usehostname)
287 strcpy(our_name, hostname);
289 strcpy(user, our_name);
291 /* If authentication is required, ask peer for CHAP or PAP. */
292 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
298 * Check whether we have appropriate secrets to use
299 * to authenticate ourselves and/or the peer.
301 if (ao->neg_upap && passwd[0] == 0 && !get_upap_passwd())
303 if (wo->neg_upap && !uselogin && !have_upap_secret())
305 if (ao->neg_chap && !have_chap_secret(our_name, remote_name))
307 if (wo->neg_chap && !have_chap_secret(remote_name, our_name))
310 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
312 pppd: peer authentication required but no authentication files accessible\n");
320 * check_passwd - Check the user name and passwd against the PAP secrets
321 * file. If requested, also check against the system password database,
322 * and login the user if OK.
325 * UPAP_AUTHNAK: Authentication failed.
326 * UPAP_AUTHACK: Authentication succeeded.
327 * In either case, msg points to an appropriate message.
330 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
342 struct wordlist *addrs;
343 char passwd[256], user[256];
344 char secret[MAXWORDLEN];
345 static int attempts = 0;
348 * Make copies of apasswd and auser, then null-terminate them.
350 BCOPY(apasswd, passwd, passwdlen);
351 passwd[passwdlen] = '\0';
352 BCOPY(auser, user, userlen);
353 user[userlen] = '\0';
356 * Open the file of upap secrets and scan for a suitable secret
357 * for authenticating this user.
359 filename = _PATH_UPAPFILE;
362 f = fopen(filename, "r");
365 syslog(LOG_ERR, "Can't open upap password file %s: %m", filename);
370 check_access(f, filename);
371 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0
372 || (secret[0] != 0 && strcmp(passwd, secret) != 0
373 && strcmp(crypt(passwd, secret), secret) != 0)) {
374 syslog(LOG_WARNING, "upap authentication failure for %s", user);
380 if (uselogin && ret == UPAP_AUTHACK) {
381 ret = login(user, passwd, msg, msglen);
382 if (ret == UPAP_AUTHNAK) {
383 syslog(LOG_WARNING, "upap login failure for %s", user);
387 if (ret == UPAP_AUTHNAK) {
388 *msg = "Login incorrect";
389 *msglen = strlen(*msg);
391 * Frustrate passwd stealer programs.
392 * Allow 10 tries, but start backing off after 3 (stolen from login).
393 * On 10'th, drop the connection.
395 if (attempts++ >= 10) {
396 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
397 attempts, devname, user);
401 sleep((u_int) (attempts - 3) * 5);
403 free_wordlist(addrs);
406 attempts = 0; /* Reset count */
408 *msglen = strlen(*msg);
409 if (addresses[unit] != NULL)
410 free_wordlist(addresses[unit]);
411 addresses[unit] = addrs;
419 * login - Check the user name and password against the system
420 * password database, and login the user if OK.
423 * UPAP_AUTHNAK: Login failed.
424 * UPAP_AUTHACK: Login succeeded.
425 * In either case, msg points to an appropriate message.
428 login(user, passwd, msg, msglen)
438 if ((pw = getpwnam(user)) == NULL) {
439 return (UPAP_AUTHNAK);
443 * XXX If no passwd, let them login without one.
445 if (pw->pw_passwd == '\0') {
446 return (UPAP_AUTHACK);
449 epasswd = crypt(passwd, pw->pw_passwd);
450 if (strcmp(epasswd, pw->pw_passwd)) {
451 return (UPAP_AUTHNAK);
454 syslog(LOG_INFO, "user %s logged in", user);
457 * Write a wtmp entry for this user.
459 tty = strrchr(devname, '/');
464 logwtmp(tty, user, ""); /* Add wtmp login entry */
467 return (UPAP_AUTHACK);
471 * logout - Logout the user.
478 tty = strrchr(devname, '/');
483 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
489 * get_upap_passwd - get a password for authenticating ourselves with
490 * our peer using PAP. Returns 1 on success, 0 if no suitable password
498 struct wordlist *addrs;
499 char secret[MAXWORDLEN];
501 filename = _PATH_UPAPFILE;
503 f = fopen(filename, "r");
506 check_access(f, filename);
507 if (scan_authfile(f, user, remote_name, secret, NULL, filename) < 0)
509 strncpy(passwd, secret, MAXSECRETLEN);
510 passwd[MAXSECRETLEN-1] = 0;
516 * have_upap_secret - check whether we have a PAP file with any
517 * secrets that we could possibly use for authenticating the peer.
526 filename = _PATH_UPAPFILE;
527 f = fopen(filename, "r");
531 ret = scan_authfile(f, NULL, our_name, NULL, NULL, filename);
541 * have_chap_secret - check whether we have a CHAP file with a
542 * secret that we could possibly use for authenticating `client'
543 * on `server'. Either can be the null string, meaning we don't
544 * know the identity yet.
547 have_chap_secret(client, server)
555 filename = _PATH_CHAPFILE;
556 f = fopen(filename, "r");
562 else if (server[0] == 0)
565 ret = scan_authfile(f, client, server, NULL, NULL, filename);
575 * get_secret - open the CHAP secret file and return the secret
576 * for authenticating the given client on the given server.
577 * (We could be either client or server).
580 get_secret(unit, client, server, secret, secret_len, save_addrs)
590 struct wordlist *addrs;
591 char secbuf[MAXWORDLEN];
593 filename = _PATH_CHAPFILE;
597 f = fopen(filename, "r");
599 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
602 check_access(f, filename);
604 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
610 if (addresses[unit] != NULL)
611 free_wordlist(addresses[unit]);
612 addresses[unit] = addrs;
615 len = strlen(secbuf);
616 if (len > MAXSECRETLEN) {
617 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
620 BCOPY(secbuf, secret, len);
627 * auth_ip_addr - check whether the peer is authorized to use
628 * a given IP address. Returns 1 if authorized, 0 otherwise.
631 auth_ip_addr(unit, addr)
637 struct wordlist *addrs;
639 if ((addrs = addresses[unit]) == NULL)
640 return 1; /* no restriction */
642 for (; addrs != NULL; addrs = addrs->next) {
643 /* "-" means no addresses authorized */
644 if (strcmp(addrs->word, "-") == 0)
646 if ((a = inet_addr(addrs->word)) == -1) {
647 if ((hp = gethostbyname(addrs->word)) == NULL) {
648 syslog(LOG_WARNING, "unknown host %s in auth. address list",
652 a = *(u_long *)hp->h_addr;
657 return 0; /* not in list => can't have it */
661 * check_access - complain if a secret file has too-liberal permissions.
664 check_access(f, filename)
670 if (fstat(fileno(f), &sbuf) < 0) {
671 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
672 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
673 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
679 * scan_authfile - Scan an authorization file for a secret suitable
680 * for authenticating `client' on `server'. The return value is -1
681 * if no secret is found, otherwise >= 0. The return value has
682 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
683 * NONWILD_SERVER set if the secret didn't have "*" for the server.
684 * Any following words on the line (i.e. address authorization
685 * info) are placed in a wordlist and returned in *addrs.
688 scan_authfile(f, client, server, secret, addrs, filename)
693 struct wordlist **addrs;
697 int got_flag, best_flag;
699 struct wordlist *ap, *addr_list, *addr_last;
700 char word[MAXWORDLEN];
701 char atfile[MAXWORDLEN];
706 if (!getword(f, word, &newline, filename))
707 return -1; /* file is empty??? */
712 * Skip until we find a word at the start of a line.
714 while (!newline && getword(f, word, &newline, filename))
717 break; /* got to end of file */
720 * Got a client - check if it's a match or a wildcard.
723 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
728 got_flag = NONWILD_CLIENT;
731 * Now get a server and check if it matches.
733 if (!getword(f, word, &newline, filename))
737 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
740 got_flag |= NONWILD_SERVER;
743 * Got some sort of a match - see if it's better than what
746 if (got_flag <= best_flag)
752 if (!getword(f, word, &newline, filename))
758 * Special syntax: @filename means read secret from file.
760 if (word[0] == '@') {
761 strcpy(atfile, word+1);
762 if ((sf = fopen(atfile, "r")) == NULL) {
763 syslog(LOG_WARNING, "can't open indirect secret file %s",
767 check_access(sf, atfile);
768 if (!getword(sf, word, &xxx, atfile)) {
769 syslog(LOG_WARNING, "no secret in indirect secret file %s",
777 strcpy(secret, word);
779 best_flag = got_flag;
782 * Now read address authorization info and make a wordlist.
785 free_wordlist(addr_list);
788 if (!getword(f, word, &newline, filename) || newline)
790 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
793 novm("authorized addresses");
795 strcpy(ap->word, word);
796 if (addr_list == NULL)
799 addr_last->next = ap;
808 else if (addr_list != NULL)
809 free_wordlist(addr_list);
815 * free_wordlist - release memory allocated for a wordlist.
821 struct wordlist *next;