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.
34 * $FreeBSD: src/usr.sbin/pppd/auth.c,v 1.24.2.2 2002/03/12 08:55:22 maxim Exp $
35 * $DragonFly: src/usr.sbin/pppd/auth.c,v 1.5 2005/11/24 23:42:54 swildner Exp $
46 #include <sys/types.h>
48 #include <sys/socket.h>
52 #if defined(_PATH_LASTLOG) && defined(_linux_)
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
64 #include <security/pam_appl.h>
70 #define PW_PPP PW_LOGIN
83 #include "pathnames.h"
85 /* Used for storing a sequence of words. Usually malloced. */
87 struct wordlist *next;
91 /* Bits in scan_authfile return value */
92 #define NONWILD_SERVER 1
93 #define NONWILD_CLIENT 2
95 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
100 /* The name by which the peer authenticated itself to us. */
101 char peer_authname[MAXNAMELEN];
103 /* Records which authentication operations haven't completed yet. */
104 static int auth_pending[NUM_PPP];
106 /* Set if we have successfully called plogin() */
107 static int logged_in;
109 /* Set if not wild or blank */
110 static int non_wildclient;
112 /* Set if we have run the /etc/ppp/auth-up script. */
113 static int did_authup;
115 /* List of addresses which the peer may use. */
116 static struct wordlist *addresses[NUM_PPP];
118 /* Number of network protocols which we have opened. */
119 static int num_np_open;
121 /* Number of network protocols which have come up. */
122 static int num_np_up;
124 /* Set if we got the contents of passwd[] from the pap-secrets file. */
125 static int passwd_from_file;
127 /* Bits in auth_pending[] */
128 #define PAP_WITHPEER 1
130 #define CHAP_WITHPEER 4
133 extern char *crypt(const char *, const char *);
135 /* Prototypes for procedures local to this file. */
137 static void network_phase(int);
138 static void check_idle(void *);
139 static void connect_time_expired(void *);
140 static int plogin(char *, char *, char **, int *);
141 static void plogout(void);
142 static int null_login(int);
143 static int get_pap_passwd(char *);
144 static int have_pap_secret(void);
145 static int have_chap_secret(char *, char *, u_int32_t);
146 static int ip_addr_check(u_int32_t, struct wordlist *);
147 static int scan_authfile(FILE *, char *, char *, u_int32_t, char *,
148 struct wordlist **, char *);
149 static void free_wordlist(struct wordlist *);
150 static void auth_set_ip_addr(int);
151 static void auth_script(char *);
152 static void set_allowed_addrs(int, struct wordlist *);
155 * An Open on LCP has requested a change from Dead to Establish phase.
156 * Do what's necessary to bring the physical layer up.
159 link_required(int unit)
164 * LCP has terminated the link; go to the Dead phase and take the
165 * physical layer down.
168 link_terminated(int unit)
170 extern time_t etime, stime;
173 if (phase == PHASE_DEAD)
179 minutes = (etime-stime)/60;
180 syslog(LOG_NOTICE, "Connection terminated, connected for %d minutes\n",
181 minutes > 1 ? minutes : 1);
185 * LCP has gone down; it will either die or try to re-establish.
191 struct protent *protp;
194 auth_script(_PATH_AUTHDOWN);
197 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
198 if (!protp->enabled_flag)
200 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
201 (*protp->lowerdown)(unit);
202 if (protp->protocol < 0xC000 && protp->close != NULL)
203 (*protp->close)(unit, "LCP down");
207 if (phase != PHASE_DEAD)
208 phase = PHASE_TERMINATE;
212 * The link is established.
213 * Proceed to the Dead, Authenticate or Network phase as appropriate.
216 link_established(int unit)
219 lcp_options *wo = &lcp_wantoptions[unit];
220 lcp_options *go = &lcp_gotoptions[unit];
221 lcp_options *ho = &lcp_hisoptions[unit];
223 struct protent *protp;
226 * Tell higher-level protocols that LCP is up.
228 for (i = 0; (protp = protocols[i]) != NULL; ++i)
229 if (protp->protocol != PPP_LCP && protp->enabled_flag
230 && protp->lowerup != NULL)
231 (*protp->lowerup)(unit);
233 if (auth_required && !(go->neg_chap || go->neg_upap)) {
235 * We wanted the peer to authenticate itself, and it refused:
236 * treat it as though it authenticated with PAP using a username
237 * of "" and a password of "". If that's not OK, boot it out.
239 if (!wo->neg_upap || !null_login(unit)) {
240 syslog(LOG_WARNING, "peer refused to authenticate");
241 lcp_close(unit, "peer refused to authenticate");
246 phase = PHASE_AUTHENTICATE;
249 ChapAuthPeer(unit, our_name, go->chap_mdtype);
251 } else if (go->neg_upap) {
256 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
257 auth |= CHAP_WITHPEER;
258 } else if (ho->neg_upap) {
259 if (passwd[0] == 0) {
260 passwd_from_file = 1;
261 if (!get_pap_passwd(passwd))
262 syslog(LOG_ERR, "No secret found for PAP login");
264 upap_authwithpeer(unit, user, passwd);
265 auth |= PAP_WITHPEER;
267 auth_pending[unit] = auth;
274 * Proceed to the network phase.
277 network_phase(int unit)
280 struct protent *protp;
281 lcp_options *go = &lcp_gotoptions[unit];
284 * If the peer had to authenticate, run the auth-up script now.
286 if ((go->neg_chap || go->neg_upap) && !did_authup) {
287 auth_script(_PATH_AUTHUP);
293 * If we negotiated callback, do it now.
296 phase = PHASE_CALLBACK;
297 (*cbcp_protent.open)(unit);
302 phase = PHASE_NETWORK;
305 set_filters(&pass_filter, &active_filter);
307 for (i = 0; (protp = protocols[i]) != NULL; ++i)
308 if (protp->protocol < 0xC000 && protp->enabled_flag
309 && protp->open != NULL) {
310 (*protp->open)(unit);
311 if (protp->protocol != PPP_CCP)
315 if (num_np_open == 0)
317 lcp_close(0, "No network protocols running");
321 * The peer has failed to authenticate himself using `protocol'.
324 auth_peer_fail(int unit, int protocol)
327 * Authentication failure: take the link down
329 lcp_close(unit, "Authentication failed");
333 * The peer has been successfully authenticated using `protocol'.
336 auth_peer_success(int unit, int protocol, char *name, int namelen)
348 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
354 * Save the authenticated name of the peer for later.
356 if (namelen > sizeof(peer_authname) - 1)
357 namelen = sizeof(peer_authname) - 1;
358 BCOPY(name, peer_authname, namelen);
359 peer_authname[namelen] = 0;
362 * If we have overridden addresses based on auth info
363 * then set that information now before continuing.
365 auth_set_ip_addr(unit);
367 script_setenv("PEERNAME", peer_authname);
370 * If there is no more authentication still to be done,
371 * proceed to the network (or callback) phase.
373 if ((auth_pending[unit] &= ~bit) == 0)
378 * We have failed to authenticate ourselves to the peer using `protocol'.
381 auth_withpeer_fail(int unit, int protocol)
383 if (passwd_from_file)
384 BZERO(passwd, MAXSECRETLEN);
386 * We've failed to authenticate ourselves to our peer.
387 * He'll probably take the link down, and there's not much
388 * we can do except wait for that.
393 * We have successfully authenticated ourselves with the peer using `protocol'.
396 auth_withpeer_success(int unit, int protocol)
405 if (passwd_from_file)
406 BZERO(passwd, MAXSECRETLEN);
410 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
416 * If we have overridden addresses based on auth info
417 * then set that information now before continuing.
419 auth_set_ip_addr(unit);
422 * If there is no more authentication still being done,
423 * proceed to the network (or callback) phase.
425 if ((auth_pending[unit] &= ~bit) == 0)
431 * np_up - a network protocol has come up.
434 np_up(int unit, int proto)
436 if (num_np_up == 0) {
438 * At this point we consider that the link has come up successfully.
442 if (idle_time_limit > 0)
443 TIMEOUT(check_idle, NULL, idle_time_limit);
446 * Set a timeout to close the connection once the maximum
447 * connect time has expired.
450 TIMEOUT(connect_time_expired, 0, maxconnect);
453 * Detach now, if the updetach option was given.
462 * np_down - a network protocol has gone down.
465 np_down(int unit, int proto)
467 if (--num_np_up == 0 && idle_time_limit > 0) {
468 UNTIMEOUT(check_idle, NULL);
473 * np_finished - a network protocol has finished using the link.
476 np_finished(int unit, int proto)
478 if (--num_np_open <= 0) {
479 /* no further use for the link: shut up shop. */
480 lcp_close(0, "No network protocols running");
485 * check_idle - check whether the link has been idle for long
486 * enough that we can shut it down.
489 check_idle(void *arg)
491 struct ppp_idle idle;
494 if (!get_idle_time(0, &idle))
496 itime = MIN(idle.xmit_idle, idle.recv_idle);
497 if (itime >= idle_time_limit) {
498 /* link is idle: shut it down. */
499 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
500 lcp_close(0, "Link inactive");
502 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
507 * connect_time_expired - log a message and close the connection.
510 connect_time_expired(void *arg)
512 syslog(LOG_INFO, "Connect time expired");
513 lcp_close(0, "Connect time expired"); /* Close connection */
517 * auth_check_options - called to check authentication options.
520 auth_check_options(void)
522 lcp_options *wo = &lcp_wantoptions[0];
524 ipcp_options *ipwo = &ipcp_wantoptions[0];
527 /* Default our_name to hostname, and user to our_name */
528 if (our_name[0] == 0 || usehostname)
529 strcpy(our_name, hostname);
531 strcpy(user, our_name);
533 /* If authentication is required, ask peer for CHAP or PAP. */
534 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
540 * Check whether we have appropriate secrets to use
541 * to authenticate the peer.
543 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
544 if (!can_auth && wo->neg_chap) {
545 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
546 can_auth = have_chap_secret(remote_name, our_name, remote);
549 if (auth_required && !can_auth) {
550 option_error("peer authentication required but no suitable secret(s) found\n");
551 if (remote_name[0] == 0)
552 option_error("for authenticating any peer to us (%s)\n", our_name);
554 option_error("for authenticating peer %s to us (%s)\n",
555 remote_name, our_name);
560 * Check whether the user tried to override certain values
563 if (!auth_required && auth_req_info.priv > 0) {
564 if (!default_device && devnam_info.priv == 0) {
565 option_error("can't override device name when noauth option used");
568 if ((connector != NULL && connector_info.priv == 0)
569 || (disconnector != NULL && disconnector_info.priv == 0)
570 || (welcomer != NULL && welcomer_info.priv == 0)) {
571 option_error("can't override connect, disconnect or welcome");
572 option_error("option values when noauth option used");
579 * auth_reset - called when LCP is starting negotiations to recheck
580 * authentication options, i.e. whether we have appropriate secrets
581 * to use for authenticating ourselves and/or the peer.
586 lcp_options *go = &lcp_gotoptions[unit];
587 lcp_options *ao = &lcp_allowoptions[0];
588 ipcp_options *ipwo = &ipcp_wantoptions[0];
591 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
592 ao->neg_chap = !refuse_chap
593 && have_chap_secret(user, remote_name, (u_int32_t)0);
595 if (go->neg_upap && !uselogin && !have_pap_secret())
598 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
599 if (!have_chap_secret(remote_name, our_name, remote))
606 * check_passwd - Check the user name and passwd against the PAP secrets
607 * file. If requested, also check against the system password database,
608 * and login the user if OK.
611 * UPAP_AUTHNAK: Authentication failed.
612 * UPAP_AUTHACK: Authentication succeeded.
613 * In either case, msg points to an appropriate message.
616 check_passwd(int unit, char *auser, int userlen, char *apasswd, int passwdlen,
617 char **msg, int *msglen)
622 struct wordlist *addrs;
624 ipcp_options *ipwo = &ipcp_wantoptions[unit];
625 char passwd[256], user[256];
626 char secret[MAXWORDLEN];
627 static int attempts = 0;
631 * Make copies of apasswd and auser, then null-terminate them.
633 len = MIN(passwdlen, sizeof(passwd) - 1);
634 BCOPY(apasswd, passwd, len);
636 len = MIN(userlen, sizeof(user) - 1);
637 BCOPY(auser, user, len);
642 * Open the file of pap secrets and scan for a suitable secret
643 * for authenticating this user.
645 filename = _PATH_UPAPFILE;
648 f = fopen(filename, "r");
650 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
654 check_access(f, filename);
655 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
656 if (scan_authfile(f, user, our_name, remote,
657 secret, &addrs, filename) < 0) {
658 warn("no PAP secret found for %s", user);
660 if (secret[0] != 0) {
661 /* password given in pap-secrets - must match */
662 if ((cryptpap || strcmp(passwd, secret) != 0)
663 && strcmp(crypt(passwd, secret), secret) != 0) {
665 warn("PAP authentication failure for %s", user);
672 if (uselogin && ret == UPAP_AUTHACK) {
673 ret = plogin(user, passwd, msg, msglen);
674 if (ret == UPAP_AUTHNAK) {
675 syslog(LOG_WARNING, "PAP login failure for %s", user);
679 if (ret == UPAP_AUTHNAK) {
681 *msg = "Login incorrect";
682 *msglen = strlen(*msg);
684 * Frustrate passwd stealer programs.
685 * Allow 10 tries, but start backing off after 3 (stolen from login).
686 * On 10'th, drop the connection.
688 if (attempts++ >= 10) {
689 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
690 attempts, devnam, user);
694 sleep((u_int) (attempts - 3) * 5);
696 free_wordlist(addrs);
699 attempts = 0; /* Reset count */
702 *msglen = strlen(*msg);
703 set_allowed_addrs(unit, addrs);
706 BZERO(passwd, sizeof(passwd));
707 BZERO(secret, sizeof(secret));
713 * Check if an "entry" is in the file "fname" - used by ppplogin.
714 * Taken from libexec/ftpd/ftpd.c
715 * Returns: 0 if not found, 1 if found, 2 if file can't be opened for reading.
718 checkfile(char *fname, char *name)
722 char *p, line[BUFSIZ];
724 if ((fd = fopen(fname, "r")) != NULL) {
725 while (fgets(line, sizeof(line), fd) != NULL)
726 if ((p = strchr(line, '\n')) != NULL) {
730 if (strcmp(line, name) == 0) {
743 * This function is needed for PAM.
747 static char *PAM_username = "";
748 static char *PAM_password = "";
750 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
751 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
753 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
756 static int pam_conv (int num_msg,
757 const struct pam_message **msg,
758 struct pam_response **resp,
761 int count = 0, replies = 0;
762 struct pam_response *reply = NULL;
765 for (count = 0; count < num_msg; count++)
767 size += sizeof (struct pam_response);
768 reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
772 switch (msg[count]->msg_style)
774 case PAM_PROMPT_ECHO_ON:
775 reply[replies].resp_retcode = PAM_SUCCESS;
776 reply[replies++].resp = strdup(PAM_username); /* never NULL */
779 case PAM_PROMPT_ECHO_OFF:
780 reply[replies].resp_retcode = PAM_SUCCESS;
781 reply[replies++].resp = strdup(PAM_password); /* never NULL */
785 reply[replies].resp_retcode = PAM_SUCCESS;
786 reply[replies++].resp = NULL;
806 * plogin - Check the user name and password against the system
807 * password database, and login the user if OK.
810 * UPAP_AUTHNAK: Login failed.
811 * UPAP_AUTHACK: Login succeeded.
812 * In either case, msg points to an appropriate message.
814 * UPAP_AUTHACK should only be returned *after* wtmp and utmp are updated.
818 plogin(char *user, char *passwd, char **msg, int *msglen)
823 struct pam_conv pam_conversation;
827 * Fill the pam_conversion structure
829 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
830 pam_conversation.conv = &pam_conv;
832 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
834 if (pam_error != PAM_SUCCESS) {
835 *msg = MY_PAM_STRERROR (pam_error);
839 * Define the fields for the credintial validation
841 pam_set_item (pamh, PAM_TTY, devnam);
843 PAM_password = passwd;
847 pam_error = pam_authenticate (pamh, PAM_SILENT);
848 if (pam_error == PAM_SUCCESS) {
849 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
851 /* start a session for this user. Session closed when link ends. */
852 if (pam_error == PAM_SUCCESS)
853 pam_open_session (pamh, PAM_SILENT);
856 *msg = MY_PAM_STRERROR (pam_error);
863 pam_end (pamh, pam_error);
865 if (pam_error != PAM_SUCCESS)
868 * Use the non-PAM methods directly
870 #else /* #ifdef USE_PAM */
879 struct spwd *getspnam();
885 return (UPAP_AUTHNAK);
888 * Check that the user is not listed in /etc/ppp/ppp.deny
889 * and that the user's shell is listed in /etc/ppp/ppp.shells
890 * if /etc/ppp/ppp.shells exists.
893 if (checkfile(_PATH_PPPDENY, user) == 1) {
894 syslog(LOG_WARNING, "upap user %s: login denied in %s",
895 user, _PATH_PPPDENY);
896 return (UPAP_AUTHNAK);
899 if (checkfile(_PATH_PPPSHELLS, pw->pw_shell) == 0) {
900 syslog(LOG_WARNING, "upap user %s: shell %s not in %s",
901 user, pw->pw_shell, _PATH_PPPSHELLS);
902 return (UPAP_AUTHNAK);
906 spwd = getspnam(user);
909 /* check the age of the password entry */
910 long now = time(NULL) / 86400L;
912 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
913 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
914 && spwd->sp_lstchg >= 0
915 && now >= spwd->sp_lstchg + spwd->sp_max)) {
916 syslog(LOG_WARNING, "Password for %s has expired", user);
917 return (UPAP_AUTHNAK);
919 pw->pw_passwd = spwd->sp_pwdp;
924 * If no passwd, don't let them login.
926 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
927 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
928 return (UPAP_AUTHNAK);
931 gettimeofday(&tp, NULL);
932 if (tp.tv_sec >= pw->pw_expire) {
933 syslog(LOG_INFO, "pap user %s account expired", user);
934 return (UPAP_AUTHNAK);
938 /* These functions are not enabled for PAM. The reason for this is that */
939 /* there is not necessarily a "passwd" entry for this user. That is */
940 /* real purpose of 'PAM' -- to virtualize the account data from the */
941 /* application. If you want to do the same thing, write the entry in */
942 /* the 'session' hook. */
944 /* Log in wtmp and utmp using login() */
947 if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
950 if (logout(tty)) /* Already entered (by login?) */
951 logwtmp(tty, "", "");
953 #if defined(_PATH_LASTLOG)
958 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
959 lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
960 memset((void *)&ll, 0, sizeof(ll));
962 strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
963 write(fd, (char *)&ll, sizeof(ll));
969 memset((void *)&utmp, 0, sizeof(utmp));
971 strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
972 strncpy(utmp.ut_host, ":PPP", sizeof(utmp.ut_host));
973 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
974 login(&utmp); /* This logs us in wtmp too */
976 #endif /* #ifdef USE_PAM */
978 syslog(LOG_INFO, "user %s logged in", user);
981 return (UPAP_AUTHACK);
985 * plogout - Logout the user.
991 struct pam_conv pam_conversation;
995 * Fill the pam_conversion structure. The PAM specification states that the
996 * session must be able to be closed by a totally different handle from which
997 * it was created. Hold the PAM group to their own specification!
999 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
1000 pam_conversation.conv = &pam_conv;
1002 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
1003 if (pam_error == PAM_SUCCESS) {
1004 pam_set_item (pamh, PAM_TTY, devnam);
1005 pam_close_session (pamh, PAM_SILENT);
1006 pam_end (pamh, PAM_SUCCESS);
1013 if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
1015 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
1016 logout(tty); /* Wipe out utmp */
1024 * null_login - Check if a username of "" and a password of "" are
1025 * acceptable, and iff so, set the list of acceptable IP addresses
1029 null_login(int unit)
1034 struct wordlist *addrs;
1035 char secret[MAXWORDLEN];
1038 * Open the file of pap secrets and scan for a suitable secret.
1039 * We don't accept a wildcard client.
1041 filename = _PATH_UPAPFILE;
1043 f = fopen(filename, "r");
1046 check_access(f, filename);
1048 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
1049 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1050 BZERO(secret, sizeof(secret));
1053 set_allowed_addrs(unit, addrs);
1055 free_wordlist(addrs);
1063 * get_pap_passwd - get a password for authenticating ourselves with
1064 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1068 get_pap_passwd(char *passwd)
1073 struct wordlist *addrs;
1074 char secret[MAXWORDLEN];
1076 filename = _PATH_UPAPFILE;
1078 f = fopen(filename, "r");
1081 check_access(f, filename);
1082 ret = scan_authfile(f, user,
1083 remote_name[0]? remote_name: NULL,
1084 (u_int32_t)0, secret, NULL, filename);
1088 if (passwd != NULL) {
1089 strncpy(passwd, secret, MAXSECRETLEN);
1090 passwd[MAXSECRETLEN-1] = 0;
1092 BZERO(secret, sizeof(secret));
1098 * have_pap_secret - check whether we have a PAP file with any
1099 * secrets that we could possibly use for authenticating the peer.
1102 have_pap_secret(void)
1107 ipcp_options *ipwo = &ipcp_wantoptions[0];
1110 filename = _PATH_UPAPFILE;
1111 f = fopen(filename, "r");
1115 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
1116 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
1126 * have_chap_secret - check whether we have a CHAP file with a
1127 * secret that we could possibly use for authenticating `client'
1128 * on `server'. Either can be the null string, meaning we don't
1129 * know the identity yet.
1132 have_chap_secret(char *client, char *server, u_int32_t remote)
1138 filename = _PATH_CHAPFILE;
1139 f = fopen(filename, "r");
1145 else if (server[0] == 0)
1148 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1158 * get_secret - open the CHAP secret file and return the secret
1159 * for authenticating the given client on the given server.
1160 * (We could be either client or server).
1163 get_secret(int unit, char *client, char *server, char *secret, int *secret_len,
1169 struct wordlist *addrs;
1170 char secbuf[MAXWORDLEN];
1172 filename = _PATH_CHAPFILE;
1176 f = fopen(filename, "r");
1178 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1181 check_access(f, filename);
1183 ret = scan_authfile(f, client, server, (u_int32_t)0,
1184 secbuf, &addrs, filename);
1190 set_allowed_addrs(unit, addrs);
1192 len = strlen(secbuf);
1193 if (len > MAXSECRETLEN) {
1194 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1197 BCOPY(secbuf, secret, len);
1198 BZERO(secbuf, sizeof(secbuf));
1205 * set_allowed_addrs() - set the list of allowed addresses.
1208 set_allowed_addrs(int unit, struct wordlist *addrs)
1210 if (addresses[unit] != NULL)
1211 free_wordlist(addresses[unit]);
1212 addresses[unit] = addrs;
1215 * If there's only one authorized address we might as well
1216 * ask our peer for that one right away
1218 if (addrs != NULL && addrs->next == NULL) {
1219 char *p = addrs->word;
1220 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1224 if (*p != '!' && *p != '-' && strchr(p, '/') == NULL) {
1225 hp = gethostbyname(p);
1226 if (hp != NULL && hp->h_addrtype == AF_INET)
1227 a = *(u_int32_t *)hp->h_addr;
1230 if (a != (u_int32_t) -1)
1237 auth_set_ip_addr(int unit)
1239 struct wordlist *addrs;
1241 if (non_wildclient && (addrs = addresses[unit]) != NULL) {
1242 for (; addrs != NULL; addrs = addrs->next) {
1243 /* Look for address overrides, and set them if we have any */
1244 if (strchr(addrs->word, ':') != NULL) {
1245 if (setipaddr(addrs->word))
1253 * auth_ip_addr - check whether the peer is authorized to use
1254 * a given IP address. Returns 1 if authorized, 0 otherwise.
1257 auth_ip_addr(int unit, u_int32_t addr)
1259 return ip_addr_check(addr, addresses[unit]);
1263 ip_addr_check(u_int32_t addr, struct wordlist *addrs)
1266 u_int32_t a, mask, ah;
1268 char *ptr_word, *ptr_mask;
1272 /* don't allow loopback or multicast address */
1273 if (bad_ip_adrs(addr))
1277 return !auth_required; /* no addresses authorized */
1280 for (; addrs != NULL; addrs = addrs->next) {
1282 /* "-" means no addresses authorized, "*" means any address allowed */
1283 ptr_word = addrs->word;
1284 if (strcmp(ptr_word, "-") == 0)
1286 if (strcmp(ptr_word, "*") == 0)
1290 * A colon in the string means that we wish to force a specific
1291 * local:remote address, but we ignore these for now.
1293 if (strchr(addrs->word, ':') != NULL)
1298 if (*ptr_word == '!') {
1303 mask = ~ (u_int32_t) 0;
1304 ptr_mask = strchr (ptr_word, '/');
1305 if (ptr_mask != NULL) {
1308 bit_count = (int) strtol (ptr_mask+1, NULL, 10);
1309 if (bit_count <= 0 || bit_count > 32) {
1310 syslog (LOG_WARNING,
1311 "invalid address length %s in auth. address list",
1316 mask <<= 32 - bit_count;
1319 hp = gethostbyname(ptr_word);
1320 if (hp != NULL && hp->h_addrtype == AF_INET) {
1321 a = *(u_int32_t *)hp->h_addr;
1323 np = getnetbyname (ptr_word);
1324 if (np != NULL && np->n_addrtype == AF_INET) {
1325 a = htonl (*(u_int32_t *)np->n_net);
1326 if (ptr_mask == NULL) {
1327 /* calculate appropriate mask for net */
1330 mask = IN_CLASSA_NET;
1331 else if (IN_CLASSB(ah))
1332 mask = IN_CLASSB_NET;
1333 else if (IN_CLASSC(ah))
1334 mask = IN_CLASSC_NET;
1337 a = inet_addr (ptr_word);
1341 if (ptr_mask != NULL)
1344 if (a == (u_int32_t)-1L)
1345 syslog (LOG_WARNING,
1346 "unknown host %s in auth. address list",
1349 /* Here a and addr are in network byte order,
1350 and mask is in host order. */
1351 if (((addr ^ a) & htonl(mask)) == 0)
1355 return x == y; /* not in list => can't have it */
1359 * bad_ip_adrs - return 1 if the IP address is one we don't want
1360 * to use, such as an address in the loopback net or a multicast address.
1361 * addr is in network byte order.
1364 bad_ip_adrs(u_int32_t addr)
1367 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1368 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1372 * check_access - complain if a secret file has too-liberal permissions.
1375 check_access(FILE *f, char *filename)
1379 if (fstat(fileno(f), &sbuf) < 0) {
1380 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1381 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1382 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1388 * scan_authfile - Scan an authorization file for a secret suitable
1389 * for authenticating `client' on `server'. The return value is -1
1390 * if no secret is found, otherwise >= 0. The return value has
1391 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1392 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1393 * Any following words on the line (i.e. address authorization
1394 * info) are placed in a wordlist and returned in *addrs.
1397 scan_authfile(FILE *f, char *client, char *server, u_int32_t ipaddr,
1398 char *secret, struct wordlist **addrs, char *filename)
1401 int got_flag, best_flag;
1403 struct wordlist *ap, *addr_list, *alist, *alast;
1404 char word[MAXWORDLEN];
1405 char atfile[MAXWORDLEN];
1406 char lsecret[MAXWORDLEN];
1411 if (!getword(f, word, &newline, filename))
1412 return -1; /* file is empty??? */
1417 * Skip until we find a word at the start of a line.
1419 while (!newline && getword(f, word, &newline, filename))
1422 break; /* got to end of file */
1425 * Got a client - check if it's a match or a wildcard.
1428 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1433 got_flag = NONWILD_CLIENT;
1436 * Now get a server and check if it matches.
1438 if (!getword(f, word, &newline, filename))
1442 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1445 got_flag |= NONWILD_SERVER;
1448 * Got some sort of a match - see if it's better than what
1451 if (got_flag <= best_flag)
1457 if (!getword(f, word, &newline, filename))
1463 * Special syntax: @filename means read secret from file.
1465 if (word[0] == '@') {
1466 strcpy(atfile, word+1);
1467 if ((sf = fopen(atfile, "r")) == NULL) {
1468 syslog(LOG_WARNING, "can't open indirect secret file %s",
1472 check_access(sf, atfile);
1473 if (!getword(sf, word, &xxx, atfile)) {
1474 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1482 strcpy(lsecret, word);
1485 * Now read address authorization info and make a wordlist.
1487 alist = alast = NULL;
1489 if (!getword(f, word, &newline, filename) || newline)
1491 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1494 novm("authorized addresses");
1496 strcpy(ap->word, word);
1505 * Check if the given IP address is allowed by the wordlist.
1507 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1508 free_wordlist(alist);
1513 * This is the best so far; remember it.
1515 best_flag = got_flag;
1517 free_wordlist(addr_list);
1520 strcpy(secret, lsecret);
1528 else if (addr_list != NULL)
1529 free_wordlist(addr_list);
1531 non_wildclient = (best_flag & NONWILD_CLIENT) && client != NULL &&
1537 * free_wordlist - release memory allocated for a wordlist.
1540 free_wordlist(struct wordlist *wp)
1542 struct wordlist *next;
1544 while (wp != NULL) {
1552 * auth_script - execute a script with arguments
1553 * interface-name peer-name real-user tty speed
1556 auth_script(char *script)
1564 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1565 user_name = pw->pw_name;
1567 sprintf(struid, "%d", getuid());
1570 sprintf(strspeed, "%d", baud_rate);
1574 argv[2] = peer_authname;
1575 argv[3] = user_name;
1580 run_program(script, argv, 0);