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[] = "$FreeBSD$";
47 #include <sys/types.h>
49 #include <sys/socket.h>
52 #if defined(_PATH_LASTLOG) && defined(_linux_)
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
63 #include <security/pam_appl.h>
69 #define PW_PPP PW_LOGIN
82 #include "pathnames.h"
84 /* Used for storing a sequence of words. Usually malloced. */
86 struct wordlist *next;
90 /* Bits in scan_authfile return value */
91 #define NONWILD_SERVER 1
92 #define NONWILD_CLIENT 2
94 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
99 /* The name by which the peer authenticated itself to us. */
100 char peer_authname[MAXNAMELEN];
102 /* Records which authentication operations haven't completed yet. */
103 static int auth_pending[NUM_PPP];
105 /* Set if we have successfully called plogin() */
106 static int logged_in;
108 /* Set if not wild or blank */
109 static int non_wildclient;
111 /* Set if we have run the /etc/ppp/auth-up script. */
112 static int did_authup;
114 /* List of addresses which the peer may use. */
115 static struct wordlist *addresses[NUM_PPP];
117 /* Number of network protocols which we have opened. */
118 static int num_np_open;
120 /* Number of network protocols which have come up. */
121 static int num_np_up;
123 /* Set if we got the contents of passwd[] from the pap-secrets file. */
124 static int passwd_from_file;
126 /* Bits in auth_pending[] */
127 #define PAP_WITHPEER 1
129 #define CHAP_WITHPEER 4
132 extern char *crypt __P((const char *, const char *));
134 /* Prototypes for procedures local to this file. */
136 static void network_phase __P((int));
137 static void check_idle __P((void *));
138 static void connect_time_expired __P((void *));
139 static int plogin __P((char *, char *, char **, int *));
140 static void plogout __P((void));
141 static int null_login __P((int));
142 static int get_pap_passwd __P((char *));
143 static int have_pap_secret __P((void));
144 static int have_chap_secret __P((char *, char *, u_int32_t));
145 static int ip_addr_check __P((u_int32_t, struct wordlist *));
146 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
147 struct wordlist **, char *));
148 static void free_wordlist __P((struct wordlist *));
149 static void auth_set_ip_addr __P((int));
150 static void auth_script __P((char *));
151 static void set_allowed_addrs __P((int, struct wordlist *));
154 * An Open on LCP has requested a change from Dead to Establish phase.
155 * Do what's necessary to bring the physical layer up.
164 * LCP has terminated the link; go to the Dead phase and take the
165 * physical layer down.
168 link_terminated(unit)
171 extern time_t etime, stime;
174 if (phase == PHASE_DEAD)
179 etime = time((time_t *) NULL);
180 minutes = (etime-stime)/60;
181 syslog(LOG_NOTICE, "Connection terminated, connected for %d minutes\n",
182 minutes > 1 ? minutes : 1);
186 * LCP has gone down; it will either die or try to re-establish.
193 struct protent *protp;
196 auth_script(_PATH_AUTHDOWN);
199 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
200 if (!protp->enabled_flag)
202 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
203 (*protp->lowerdown)(unit);
204 if (protp->protocol < 0xC000 && protp->close != NULL)
205 (*protp->close)(unit, "LCP down");
209 if (phase != PHASE_DEAD)
210 phase = PHASE_TERMINATE;
214 * The link is established.
215 * Proceed to the Dead, Authenticate or Network phase as appropriate.
218 link_established(unit)
222 lcp_options *wo = &lcp_wantoptions[unit];
223 lcp_options *go = &lcp_gotoptions[unit];
224 lcp_options *ho = &lcp_hisoptions[unit];
226 struct protent *protp;
229 * Tell higher-level protocols that LCP is up.
231 for (i = 0; (protp = protocols[i]) != NULL; ++i)
232 if (protp->protocol != PPP_LCP && protp->enabled_flag
233 && protp->lowerup != NULL)
234 (*protp->lowerup)(unit);
236 if (auth_required && !(go->neg_chap || go->neg_upap)) {
238 * We wanted the peer to authenticate itself, and it refused:
239 * treat it as though it authenticated with PAP using a username
240 * of "" and a password of "". If that's not OK, boot it out.
242 if (!wo->neg_upap || !null_login(unit)) {
243 syslog(LOG_WARNING, "peer refused to authenticate");
244 lcp_close(unit, "peer refused to authenticate");
249 phase = PHASE_AUTHENTICATE;
252 ChapAuthPeer(unit, our_name, go->chap_mdtype);
254 } else if (go->neg_upap) {
259 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
260 auth |= CHAP_WITHPEER;
261 } else if (ho->neg_upap) {
262 if (passwd[0] == 0) {
263 passwd_from_file = 1;
264 if (!get_pap_passwd(passwd))
265 syslog(LOG_ERR, "No secret found for PAP login");
267 upap_authwithpeer(unit, user, passwd);
268 auth |= PAP_WITHPEER;
270 auth_pending[unit] = auth;
277 * Proceed to the network phase.
284 struct protent *protp;
285 lcp_options *go = &lcp_gotoptions[unit];
288 * If the peer had to authenticate, run the auth-up script now.
290 if ((go->neg_chap || go->neg_upap) && !did_authup) {
291 auth_script(_PATH_AUTHUP);
297 * If we negotiated callback, do it now.
300 phase = PHASE_CALLBACK;
301 (*cbcp_protent.open)(unit);
306 phase = PHASE_NETWORK;
309 set_filters(&pass_filter, &active_filter);
311 for (i = 0; (protp = protocols[i]) != NULL; ++i)
312 if (protp->protocol < 0xC000 && protp->enabled_flag
313 && protp->open != NULL) {
314 (*protp->open)(unit);
315 if (protp->protocol != PPP_CCP)
319 if (num_np_open == 0)
321 lcp_close(0, "No network protocols running");
325 * The peer has failed to authenticate himself using `protocol'.
328 auth_peer_fail(unit, protocol)
332 * Authentication failure: take the link down
334 lcp_close(unit, "Authentication failed");
338 * The peer has been successfully authenticated using `protocol'.
341 auth_peer_success(unit, protocol, name, namelen)
356 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
362 * Save the authenticated name of the peer for later.
364 if (namelen > sizeof(peer_authname) - 1)
365 namelen = sizeof(peer_authname) - 1;
366 BCOPY(name, peer_authname, namelen);
367 peer_authname[namelen] = 0;
370 * If we have overridden addresses based on auth info
371 * then set that information now before continuing.
373 auth_set_ip_addr(unit);
375 script_setenv("PEERNAME", peer_authname);
378 * If there is no more authentication still to be done,
379 * proceed to the network (or callback) phase.
381 if ((auth_pending[unit] &= ~bit) == 0)
386 * We have failed to authenticate ourselves to the peer using `protocol'.
389 auth_withpeer_fail(unit, protocol)
392 if (passwd_from_file)
393 BZERO(passwd, MAXSECRETLEN);
395 * We've failed to authenticate ourselves to our peer.
396 * He'll probably take the link down, and there's not much
397 * we can do except wait for that.
402 * We have successfully authenticated ourselves with the peer using `protocol'.
405 auth_withpeer_success(unit, protocol)
415 if (passwd_from_file)
416 BZERO(passwd, MAXSECRETLEN);
420 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
426 * If we have overridden addresses based on auth info
427 * then set that information now before continuing.
429 auth_set_ip_addr(unit);
432 * If there is no more authentication still being done,
433 * proceed to the network (or callback) phase.
435 if ((auth_pending[unit] &= ~bit) == 0)
441 * np_up - a network protocol has come up.
447 if (num_np_up == 0) {
449 * At this point we consider that the link has come up successfully.
453 if (idle_time_limit > 0)
454 TIMEOUT(check_idle, NULL, idle_time_limit);
457 * Set a timeout to close the connection once the maximum
458 * connect time has expired.
461 TIMEOUT(connect_time_expired, 0, maxconnect);
464 * Detach now, if the updetach option was given.
473 * np_down - a network protocol has gone down.
479 if (--num_np_up == 0 && idle_time_limit > 0) {
480 UNTIMEOUT(check_idle, NULL);
485 * np_finished - a network protocol has finished using the link.
488 np_finished(unit, proto)
491 if (--num_np_open <= 0) {
492 /* no further use for the link: shut up shop. */
493 lcp_close(0, "No network protocols running");
498 * check_idle - check whether the link has been idle for long
499 * enough that we can shut it down.
505 struct ppp_idle idle;
508 if (!get_idle_time(0, &idle))
510 itime = MIN(idle.xmit_idle, idle.recv_idle);
511 if (itime >= idle_time_limit) {
512 /* link is idle: shut it down. */
513 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
514 lcp_close(0, "Link inactive");
516 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
521 * connect_time_expired - log a message and close the connection.
524 connect_time_expired(arg)
527 syslog(LOG_INFO, "Connect time expired");
528 lcp_close(0, "Connect time expired"); /* Close connection */
532 * auth_check_options - called to check authentication options.
537 lcp_options *wo = &lcp_wantoptions[0];
539 ipcp_options *ipwo = &ipcp_wantoptions[0];
542 /* Default our_name to hostname, and user to our_name */
543 if (our_name[0] == 0 || usehostname)
544 strcpy(our_name, hostname);
546 strcpy(user, our_name);
548 /* If authentication is required, ask peer for CHAP or PAP. */
549 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
555 * Check whether we have appropriate secrets to use
556 * to authenticate the peer.
558 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
559 if (!can_auth && wo->neg_chap) {
560 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
561 can_auth = have_chap_secret(remote_name, our_name, remote);
564 if (auth_required && !can_auth) {
565 option_error("peer authentication required but no suitable secret(s) found\n");
566 if (remote_name[0] == 0)
567 option_error("for authenticating any peer to us (%s)\n", our_name);
569 option_error("for authenticating peer %s to us (%s)\n",
570 remote_name, our_name);
575 * Check whether the user tried to override certain values
578 if (!auth_required && auth_req_info.priv > 0) {
579 if (!default_device && devnam_info.priv == 0) {
580 option_error("can't override device name when noauth option used");
583 if ((connector != NULL && connector_info.priv == 0)
584 || (disconnector != NULL && disconnector_info.priv == 0)
585 || (welcomer != NULL && welcomer_info.priv == 0)) {
586 option_error("can't override connect, disconnect or welcome");
587 option_error("option values when noauth option used");
594 * auth_reset - called when LCP is starting negotiations to recheck
595 * authentication options, i.e. whether we have appropriate secrets
596 * to use for authenticating ourselves and/or the peer.
602 lcp_options *go = &lcp_gotoptions[unit];
603 lcp_options *ao = &lcp_allowoptions[0];
604 ipcp_options *ipwo = &ipcp_wantoptions[0];
607 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
608 ao->neg_chap = !refuse_chap
609 && have_chap_secret(user, remote_name, (u_int32_t)0);
611 if (go->neg_upap && !uselogin && !have_pap_secret())
614 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
615 if (!have_chap_secret(remote_name, our_name, remote))
622 * check_passwd - Check the user name and passwd against the PAP secrets
623 * file. If requested, also check against the system password database,
624 * and login the user if OK.
627 * UPAP_AUTHNAK: Authentication failed.
628 * UPAP_AUTHACK: Authentication succeeded.
629 * In either case, msg points to an appropriate message.
632 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
644 struct wordlist *addrs;
646 ipcp_options *ipwo = &ipcp_wantoptions[unit];
647 char passwd[256], user[256];
648 char secret[MAXWORDLEN];
649 static int attempts = 0;
653 * Make copies of apasswd and auser, then null-terminate them.
655 len = MIN(passwdlen, sizeof(passwd) - 1);
656 BCOPY(apasswd, passwd, len);
658 len = MIN(userlen, sizeof(user) - 1);
659 BCOPY(auser, user, len);
664 * Open the file of pap secrets and scan for a suitable secret
665 * for authenticating this user.
667 filename = _PATH_UPAPFILE;
670 f = fopen(filename, "r");
672 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
676 check_access(f, filename);
677 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
678 if (scan_authfile(f, user, our_name, remote,
679 secret, &addrs, filename) < 0
680 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
681 && strcmp(crypt(passwd, secret), secret) != 0)) {
682 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
688 if (uselogin && ret == UPAP_AUTHACK) {
689 ret = plogin(user, passwd, msg, msglen);
690 if (ret == UPAP_AUTHNAK) {
691 syslog(LOG_WARNING, "PAP login failure for %s", user);
695 if (ret == UPAP_AUTHNAK) {
696 if (*msg == (char *) 0)
697 *msg = "Login incorrect";
698 *msglen = strlen(*msg);
700 * Frustrate passwd stealer programs.
701 * Allow 10 tries, but start backing off after 3 (stolen from login).
702 * On 10'th, drop the connection.
704 if (attempts++ >= 10) {
705 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
706 attempts, devnam, user);
710 sleep((u_int) (attempts - 3) * 5);
712 free_wordlist(addrs);
715 attempts = 0; /* Reset count */
716 if (*msg == (char *) 0)
718 *msglen = strlen(*msg);
719 set_allowed_addrs(unit, addrs);
722 BZERO(passwd, sizeof(passwd));
723 BZERO(secret, sizeof(secret));
729 * Check if an "entry" is in the file "fname" - used by ppplogin.
730 * Taken from libexec/ftpd/ftpd.c
731 * Returns: 0 if not found, 1 if found, 2 if file can't be opened for reading.
734 checkfile(fname, name)
740 char *p, line[BUFSIZ];
742 if ((fd = fopen(fname, "r")) != NULL) {
743 while (fgets(line, sizeof(line), fd) != NULL)
744 if ((p = strchr(line, '\n')) != NULL) {
748 if (strcmp(line, name) == 0) {
761 * This function is needed for PAM.
765 static char *PAM_username = "";
766 static char *PAM_password = "";
768 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
769 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
771 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
774 static int pam_conv (int num_msg,
775 const struct pam_message **msg,
776 struct pam_response **resp,
779 int count = 0, replies = 0;
780 struct pam_response *reply = NULL;
783 for (count = 0; count < num_msg; count++)
785 size += sizeof (struct pam_response);
786 reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
790 switch (msg[count]->msg_style)
792 case PAM_PROMPT_ECHO_ON:
793 reply[replies].resp_retcode = PAM_SUCCESS;
794 reply[replies++].resp = strdup(PAM_username); /* never NULL */
797 case PAM_PROMPT_ECHO_OFF:
798 reply[replies].resp_retcode = PAM_SUCCESS;
799 reply[replies++].resp = strdup(PAM_password); /* never NULL */
803 reply[replies].resp_retcode = PAM_SUCCESS;
804 reply[replies++].resp = NULL;
824 * plogin - Check the user name and password against the system
825 * password database, and login the user if OK.
828 * UPAP_AUTHNAK: Login failed.
829 * UPAP_AUTHACK: Login succeeded.
830 * In either case, msg points to an appropriate message.
832 * UPAP_AUTHACK should only be returned *after* wtmp and utmp are updated.
836 plogin(user, passwd, msg, msglen)
845 struct pam_conv pam_conversation;
849 * Fill the pam_conversion structure
851 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
852 pam_conversation.conv = &pam_conv;
854 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
856 if (pam_error != PAM_SUCCESS) {
857 *msg = MY_PAM_STRERROR (pam_error);
861 * Define the fields for the credintial validation
863 (void) pam_set_item (pamh, PAM_TTY, devnam);
865 PAM_password = passwd;
869 pam_error = pam_authenticate (pamh, PAM_SILENT);
870 if (pam_error == PAM_SUCCESS) {
871 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
873 /* start a session for this user. Session closed when link ends. */
874 if (pam_error == PAM_SUCCESS)
875 (void) pam_open_session (pamh, PAM_SILENT);
878 *msg = MY_PAM_STRERROR (pam_error);
885 (void) pam_end (pamh, pam_error);
887 if (pam_error != PAM_SUCCESS)
890 * Use the non-PAM methods directly
892 #else /* #ifdef USE_PAM */
901 struct spwd *getspnam();
907 return (UPAP_AUTHNAK);
910 * Check that the user is not listed in /etc/ppp/ppp.deny
911 * and that the user's shell is listed in /etc/ppp/ppp.shells
912 * if /etc/ppp/ppp.shells exists.
915 if (checkfile(_PATH_PPPDENY, user) == 1) {
916 syslog(LOG_WARNING, "upap user %s: login denied in %s",
917 user, _PATH_PPPDENY);
918 return (UPAP_AUTHNAK);
921 if (checkfile(_PATH_PPPSHELLS, pw->pw_shell) == 0) {
922 syslog(LOG_WARNING, "upap user %s: shell %s not in %s",
923 user, pw->pw_shell, _PATH_PPPSHELLS);
924 return (UPAP_AUTHNAK);
928 spwd = getspnam(user);
931 /* check the age of the password entry */
932 long now = time(NULL) / 86400L;
934 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
935 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
936 && spwd->sp_lstchg >= 0
937 && now >= spwd->sp_lstchg + spwd->sp_max)) {
938 syslog(LOG_WARNING, "Password for %s has expired", user);
939 return (UPAP_AUTHNAK);
941 pw->pw_passwd = spwd->sp_pwdp;
946 * If no passwd, don't let them login.
948 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
949 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
950 return (UPAP_AUTHNAK);
953 (void)gettimeofday(&tp, (struct timezone *)NULL);
954 if (tp.tv_sec >= pw->pw_expire) {
955 syslog(LOG_INFO, "pap user %s account expired", user);
956 return (UPAP_AUTHNAK);
960 /* These functions are not enabled for PAM. The reason for this is that */
961 /* there is not necessarily a "passwd" entry for this user. That is */
962 /* real purpose of 'PAM' -- to virtualize the account data from the */
963 /* application. If you want to do the same thing, write the entry in */
964 /* the 'session' hook. */
966 /* Log in wtmp and utmp using login() */
969 if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
972 if (logout(tty)) /* Already entered (by login?) */
973 logwtmp(tty, "", "");
975 #if defined(_PATH_LASTLOG)
980 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
981 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
982 memset((void *)&ll, 0, sizeof(ll));
983 (void)time(&ll.ll_time);
984 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
985 (void)write(fd, (char *)&ll, sizeof(ll));
991 memset((void *)&utmp, 0, sizeof(utmp));
992 (void)time(&utmp.ut_time);
993 (void)strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
994 (void)strncpy(utmp.ut_host, ":PPP", sizeof(utmp.ut_host));
995 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
996 login(&utmp); /* This logs us in wtmp too */
998 #endif /* #ifdef USE_PAM */
1000 syslog(LOG_INFO, "user %s logged in", user);
1003 return (UPAP_AUTHACK);
1007 * plogout - Logout the user.
1013 struct pam_conv pam_conversation;
1017 * Fill the pam_conversion structure. The PAM specification states that the
1018 * session must be able to be closed by a totally different handle from which
1019 * it was created. Hold the PAM group to their own specification!
1021 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
1022 pam_conversation.conv = &pam_conv;
1024 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
1025 if (pam_error == PAM_SUCCESS) {
1026 (void) pam_set_item (pamh, PAM_TTY, devnam);
1027 (void) pam_close_session (pamh, PAM_SILENT);
1028 (void) pam_end (pamh, PAM_SUCCESS);
1035 if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
1037 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
1038 logout(tty); /* Wipe out utmp */
1046 * null_login - Check if a username of "" and a password of "" are
1047 * acceptable, and iff so, set the list of acceptable IP addresses
1057 struct wordlist *addrs;
1058 char secret[MAXWORDLEN];
1061 * Open the file of pap secrets and scan for a suitable secret.
1062 * We don't accept a wildcard client.
1064 filename = _PATH_UPAPFILE;
1066 f = fopen(filename, "r");
1069 check_access(f, filename);
1071 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
1072 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1073 BZERO(secret, sizeof(secret));
1076 set_allowed_addrs(unit, addrs);
1078 free_wordlist(addrs);
1086 * get_pap_passwd - get a password for authenticating ourselves with
1087 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1091 get_pap_passwd(passwd)
1097 struct wordlist *addrs;
1098 char secret[MAXWORDLEN];
1100 filename = _PATH_UPAPFILE;
1102 f = fopen(filename, "r");
1105 check_access(f, filename);
1106 ret = scan_authfile(f, user,
1107 remote_name[0]? remote_name: NULL,
1108 (u_int32_t)0, secret, NULL, filename);
1112 if (passwd != NULL) {
1113 strncpy(passwd, secret, MAXSECRETLEN);
1114 passwd[MAXSECRETLEN-1] = 0;
1116 BZERO(secret, sizeof(secret));
1122 * have_pap_secret - check whether we have a PAP file with any
1123 * secrets that we could possibly use for authenticating the peer.
1131 ipcp_options *ipwo = &ipcp_wantoptions[0];
1134 filename = _PATH_UPAPFILE;
1135 f = fopen(filename, "r");
1139 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
1140 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
1150 * have_chap_secret - check whether we have a CHAP file with a
1151 * secret that we could possibly use for authenticating `client'
1152 * on `server'. Either can be the null string, meaning we don't
1153 * know the identity yet.
1156 have_chap_secret(client, server, remote)
1165 filename = _PATH_CHAPFILE;
1166 f = fopen(filename, "r");
1172 else if (server[0] == 0)
1175 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1185 * get_secret - open the CHAP secret file and return the secret
1186 * for authenticating the given client on the given server.
1187 * (We could be either client or server).
1190 get_secret(unit, client, server, secret, secret_len, save_addrs)
1201 struct wordlist *addrs;
1202 char secbuf[MAXWORDLEN];
1204 filename = _PATH_CHAPFILE;
1208 f = fopen(filename, "r");
1210 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1213 check_access(f, filename);
1215 ret = scan_authfile(f, client, server, (u_int32_t)0,
1216 secbuf, &addrs, filename);
1222 set_allowed_addrs(unit, addrs);
1224 len = strlen(secbuf);
1225 if (len > MAXSECRETLEN) {
1226 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1229 BCOPY(secbuf, secret, len);
1230 BZERO(secbuf, sizeof(secbuf));
1237 * set_allowed_addrs() - set the list of allowed addresses.
1240 set_allowed_addrs(unit, addrs)
1242 struct wordlist *addrs;
1244 if (addresses[unit] != NULL)
1245 free_wordlist(addresses[unit]);
1246 addresses[unit] = addrs;
1249 * If there's only one authorized address we might as well
1250 * ask our peer for that one right away
1252 if (addrs != NULL && addrs->next == NULL) {
1253 char *p = addrs->word;
1254 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1258 if (*p != '!' && *p != '-' && strchr(p, '/') == NULL) {
1259 hp = gethostbyname(p);
1260 if (hp != NULL && hp->h_addrtype == AF_INET)
1261 a = *(u_int32_t *)hp->h_addr;
1264 if (a != (u_int32_t) -1)
1271 auth_set_ip_addr(unit)
1274 struct wordlist *addrs;
1276 if (non_wildclient && (addrs = addresses[unit]) != NULL) {
1277 for (; addrs != NULL; addrs = addrs->next) {
1278 /* Look for address overrides, and set them if we have any */
1279 if (strchr(addrs->word, ':') != NULL) {
1280 if (setipaddr(addrs->word))
1288 * auth_ip_addr - check whether the peer is authorized to use
1289 * a given IP address. Returns 1 if authorized, 0 otherwise.
1292 auth_ip_addr(unit, addr)
1296 return ip_addr_check(addr, addresses[unit]);
1300 ip_addr_check(addr, addrs)
1302 struct wordlist *addrs;
1305 u_int32_t a, mask, ah;
1307 char *ptr_word, *ptr_mask;
1311 /* don't allow loopback or multicast address */
1312 if (bad_ip_adrs(addr))
1316 return !auth_required; /* no addresses authorized */
1319 for (; addrs != NULL; addrs = addrs->next) {
1321 /* "-" means no addresses authorized, "*" means any address allowed */
1322 ptr_word = addrs->word;
1323 if (strcmp(ptr_word, "-") == 0)
1325 if (strcmp(ptr_word, "*") == 0)
1329 * A colon in the string means that we wish to force a specific
1330 * local:remote address, but we ignore these for now.
1332 if (strchr(addrs->word, ':') != NULL)
1337 if (*ptr_word == '!') {
1342 mask = ~ (u_int32_t) 0;
1343 ptr_mask = strchr (ptr_word, '/');
1344 if (ptr_mask != NULL) {
1347 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1348 if (bit_count <= 0 || bit_count > 32) {
1349 syslog (LOG_WARNING,
1350 "invalid address length %s in auth. address list",
1355 mask <<= 32 - bit_count;
1358 hp = gethostbyname(ptr_word);
1359 if (hp != NULL && hp->h_addrtype == AF_INET) {
1360 a = *(u_int32_t *)hp->h_addr;
1362 np = getnetbyname (ptr_word);
1363 if (np != NULL && np->n_addrtype == AF_INET) {
1364 a = htonl (*(u_int32_t *)np->n_net);
1365 if (ptr_mask == NULL) {
1366 /* calculate appropriate mask for net */
1369 mask = IN_CLASSA_NET;
1370 else if (IN_CLASSB(ah))
1371 mask = IN_CLASSB_NET;
1372 else if (IN_CLASSC(ah))
1373 mask = IN_CLASSC_NET;
1376 a = inet_addr (ptr_word);
1380 if (ptr_mask != NULL)
1383 if (a == (u_int32_t)-1L)
1384 syslog (LOG_WARNING,
1385 "unknown host %s in auth. address list",
1388 /* Here a and addr are in network byte order,
1389 and mask is in host order. */
1390 if (((addr ^ a) & htonl(mask)) == 0)
1394 return x == y; /* not in list => can't have it */
1398 * bad_ip_adrs - return 1 if the IP address is one we don't want
1399 * to use, such as an address in the loopback net or a multicast address.
1400 * addr is in network byte order.
1407 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1408 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1412 * check_access - complain if a secret file has too-liberal permissions.
1415 check_access(f, filename)
1421 if (fstat(fileno(f), &sbuf) < 0) {
1422 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1423 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1424 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1430 * scan_authfile - Scan an authorization file for a secret suitable
1431 * for authenticating `client' on `server'. The return value is -1
1432 * if no secret is found, otherwise >= 0. The return value has
1433 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1434 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1435 * Any following words on the line (i.e. address authorization
1436 * info) are placed in a wordlist and returned in *addrs.
1439 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1445 struct wordlist **addrs;
1449 int got_flag, best_flag;
1451 struct wordlist *ap, *addr_list, *alist, *alast;
1452 char word[MAXWORDLEN];
1453 char atfile[MAXWORDLEN];
1454 char lsecret[MAXWORDLEN];
1459 if (!getword(f, word, &newline, filename))
1460 return -1; /* file is empty??? */
1465 * Skip until we find a word at the start of a line.
1467 while (!newline && getword(f, word, &newline, filename))
1470 break; /* got to end of file */
1473 * Got a client - check if it's a match or a wildcard.
1476 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1481 got_flag = NONWILD_CLIENT;
1484 * Now get a server and check if it matches.
1486 if (!getword(f, word, &newline, filename))
1490 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1493 got_flag |= NONWILD_SERVER;
1496 * Got some sort of a match - see if it's better than what
1499 if (got_flag <= best_flag)
1505 if (!getword(f, word, &newline, filename))
1511 * Special syntax: @filename means read secret from file.
1513 if (word[0] == '@') {
1514 strcpy(atfile, word+1);
1515 if ((sf = fopen(atfile, "r")) == NULL) {
1516 syslog(LOG_WARNING, "can't open indirect secret file %s",
1520 check_access(sf, atfile);
1521 if (!getword(sf, word, &xxx, atfile)) {
1522 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1530 strcpy(lsecret, word);
1533 * Now read address authorization info and make a wordlist.
1535 alist = alast = NULL;
1537 if (!getword(f, word, &newline, filename) || newline)
1539 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1542 novm("authorized addresses");
1544 strcpy(ap->word, word);
1553 * Check if the given IP address is allowed by the wordlist.
1555 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1556 free_wordlist(alist);
1561 * This is the best so far; remember it.
1563 best_flag = got_flag;
1565 free_wordlist(addr_list);
1568 strcpy(secret, lsecret);
1576 else if (addr_list != NULL)
1577 free_wordlist(addr_list);
1579 non_wildclient = (best_flag & NONWILD_CLIENT) && client != NULL &&
1585 * free_wordlist - release memory allocated for a wordlist.
1589 struct wordlist *wp;
1591 struct wordlist *next;
1593 while (wp != NULL) {
1601 * auth_script - execute a script with arguments
1602 * interface-name peer-name real-user tty speed
1614 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1615 user_name = pw->pw_name;
1617 sprintf(struid, "%d", getuid());
1620 sprintf(strspeed, "%d", baud_rate);
1624 argv[2] = peer_authname;
1625 argv[3] = user_name;
1630 run_program(script, argv, 0);