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.
35 #define RCSID "$Id: auth.c,v 1.57 1999/08/13 06:46:10 paulus Exp $"
44 #include <sys/types.h>
46 #include <sys/socket.h>
49 #if defined(_PATH_LASTLOG) && defined(_linux_)
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
58 #include <security/pam_appl.h>
64 #define PW_PPP PW_LOGIN
77 #include "pathnames.h"
79 static const char rcsid[] = RCSID;
81 /* Bits in scan_authfile return value */
82 #define NONWILD_SERVER 1
83 #define NONWILD_CLIENT 2
85 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
87 /* The name by which the peer authenticated itself to us. */
88 char peer_authname[MAXNAMELEN];
90 /* Records which authentication operations haven't completed yet. */
91 static int auth_pending[NUM_PPP];
93 /* Set if we have successfully called plogin() */
96 /* List of addresses which the peer may use. */
97 static struct permitted_ip *addresses[NUM_PPP];
99 /* Extra options to apply, from the secrets file entry for the peer. */
100 static struct wordlist *extra_options;
102 /* Number of network protocols which we have opened. */
103 static int num_np_open;
105 /* Number of network protocols which have come up. */
106 static int num_np_up;
108 /* Set if we got the contents of passwd[] from the pap-secrets file. */
109 static int passwd_from_file;
112 * This is used to ensure that we don't start an auth-up/down
113 * script while one is already running.
120 static enum script_state auth_state = s_down;
121 static enum script_state auth_script_state = s_down;
122 static pid_t auth_script_pid = 0;
127 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
128 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
129 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
130 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
131 bool usehostname = 0; /* Use hostname for our_name */
132 bool auth_required = 0; /* Always require authentication from peer */
133 bool allow_any_ip = 0; /* Allow peer to use any IP address */
134 bool explicit_remote = 0; /* User specified explicit remote name */
135 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
137 /* Bits in auth_pending[] */
138 #define PAP_WITHPEER 1
140 #define CHAP_WITHPEER 4
143 extern char *crypt __P((const char *, const char *));
145 /* Prototypes for procedures local to this file. */
147 static void network_phase __P((int));
148 static void check_idle __P((void *));
149 static void connect_time_expired __P((void *));
150 static int plogin __P((char *, char *, char **, int *));
151 static void plogout __P((void));
152 static int null_login __P((int));
153 static int get_pap_passwd __P((char *));
154 static int have_pap_secret __P((int *));
155 static int have_chap_secret __P((char *, char *, int, int *));
156 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
157 static int scan_authfile __P((FILE *, char *, char *, char *,
158 struct wordlist **, char *));
159 static void free_wordlist __P((struct wordlist *));
160 static void auth_script __P((char *));
161 static void auth_script_done __P((void *));
162 static void set_allowed_addrs __P((int, struct wordlist *));
163 static int some_ip_ok __P((struct wordlist *));
164 static int setupapfile __P((char **));
165 static int privgroup __P((char **));
166 static void check_access __P((FILE *, char *));
169 * Authentication-related options.
171 option_t auth_options[] = {
172 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
173 "Require PAP authentication from peer", 1, &auth_required },
174 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
175 "Require PAP authentication from peer", 1, &auth_required },
176 { "refuse-pap", o_bool, &refuse_pap,
177 "Don't agree to auth to peer with PAP", 1 },
178 { "-pap", o_bool, &refuse_pap,
179 "Don't allow PAP authentication with peer", 1 },
180 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
181 "Require CHAP authentication from peer", 1, &auth_required },
182 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
183 "Require CHAP authentication from peer", 1, &auth_required },
184 { "refuse-chap", o_bool, &refuse_chap,
185 "Don't agree to auth to peer with CHAP", 1 },
186 { "-chap", o_bool, &refuse_chap,
187 "Don't allow CHAP authentication with peer", 1 },
188 { "name", o_string, our_name,
189 "Set local name for authentication",
190 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
191 { "user", o_string, user,
192 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
193 { "usehostname", o_bool, &usehostname,
194 "Must use hostname for authentication", 1 },
195 { "remotename", o_string, remote_name,
196 "Set remote name for authentication", OPT_STATIC,
197 &explicit_remote, MAXNAMELEN },
198 { "auth", o_bool, &auth_required,
199 "Require authentication from peer", 1 },
200 { "noauth", o_bool, &auth_required,
201 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
202 { "login", o_bool, &uselogin,
203 "Use system password database for PAP", 1 },
204 { "papcrypt", o_bool, &cryptpap,
205 "PAP passwords are encrypted", 1 },
206 { "+ua", o_special, setupapfile,
207 "Get PAP user and password from file" },
208 { "password", o_string, passwd,
209 "Password for authenticating us to the peer", OPT_STATIC,
210 NULL, MAXSECRETLEN },
211 { "privgroup", o_special, privgroup,
212 "Allow group members to use privileged options", OPT_PRIV },
217 * setupapfile - specifies UPAP info for authenticating with peer.
226 lcp_allowoptions[0].neg_upap = 1;
228 /* open user info file */
230 ufile = fopen(*argv, "r");
233 option_error("unable to open user login data file %s", *argv);
236 check_access(ufile, *argv);
239 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
240 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
241 option_error("unable to read user login data file %s", *argv);
246 /* get rid of newlines */
248 if (l > 0 && user[l-1] == '\n')
251 if (l > 0 && passwd[l-1] == '\n')
259 * privgroup - allow members of the group to have privileged access.
270 option_error("group %s is unknown", *argv);
273 for (i = 0; i < ngroups; ++i) {
274 if (groups[i] == g->gr_gid) {
284 * An Open on LCP has requested a change from Dead to Establish phase.
285 * Do what's necessary to bring the physical layer up.
294 * LCP has terminated the link; go to the Dead phase and take the
295 * physical layer down.
298 link_terminated(unit)
301 if (phase == PHASE_DEAD)
306 notice("Connection terminated.");
310 * LCP has gone down; it will either die or try to re-establish.
317 struct protent *protp;
320 if (auth_script_state == s_up && auth_script_pid == 0) {
321 auth_script_state = s_down;
322 auth_script(_PATH_AUTHDOWN);
324 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
325 if (!protp->enabled_flag)
327 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
328 (*protp->lowerdown)(unit);
329 if (protp->protocol < 0xC000 && protp->close != NULL)
330 (*protp->close)(unit, "LCP down");
334 if (phase != PHASE_DEAD)
335 phase = PHASE_TERMINATE;
339 * The link is established.
340 * Proceed to the Dead, Authenticate or Network phase as appropriate.
343 link_established(unit)
347 lcp_options *wo = &lcp_wantoptions[unit];
348 lcp_options *go = &lcp_gotoptions[unit];
349 lcp_options *ho = &lcp_hisoptions[unit];
351 struct protent *protp;
354 * Tell higher-level protocols that LCP is up.
356 for (i = 0; (protp = protocols[i]) != NULL; ++i)
357 if (protp->protocol != PPP_LCP && protp->enabled_flag
358 && protp->lowerup != NULL)
359 (*protp->lowerup)(unit);
361 if (auth_required && !(go->neg_chap || go->neg_upap)) {
363 * We wanted the peer to authenticate itself, and it refused:
364 * treat it as though it authenticated with PAP using a username
365 * of "" and a password of "". If that's not OK, boot it out.
367 if (!wo->neg_upap || !null_login(unit)) {
368 warn("peer refused to authenticate: terminating link");
369 lcp_close(unit, "peer refused to authenticate");
370 status = EXIT_PEER_AUTH_FAILED;
375 phase = PHASE_AUTHENTICATE;
378 ChapAuthPeer(unit, our_name, go->chap_mdtype);
380 } else if (go->neg_upap) {
385 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
386 auth |= CHAP_WITHPEER;
387 } else if (ho->neg_upap) {
388 if (passwd[0] == 0) {
389 passwd_from_file = 1;
390 if (!get_pap_passwd(passwd))
391 error("No secret found for PAP login");
393 upap_authwithpeer(unit, user, passwd);
394 auth |= PAP_WITHPEER;
396 auth_pending[unit] = auth;
403 * Proceed to the network phase.
409 lcp_options *go = &lcp_gotoptions[unit];
412 * If the peer had to authenticate, run the auth-up script now.
414 if (go->neg_chap || go->neg_upap) {
416 if (auth_script_state == s_down && auth_script_pid == 0) {
417 auth_script_state = s_up;
418 auth_script(_PATH_AUTHUP);
424 * If we negotiated callback, do it now.
427 phase = PHASE_CALLBACK;
428 (*cbcp_protent.open)(unit);
434 * Process extra options from the secrets file
437 options_from_list(extra_options, 1);
438 free_wordlist(extra_options);
448 struct protent *protp;
450 phase = PHASE_NETWORK;
453 set_filters(&pass_filter, &active_filter);
455 for (i = 0; (protp = protocols[i]) != NULL; ++i)
456 if (protp->protocol < 0xC000 && protp->enabled_flag
457 && protp->open != NULL) {
459 if (protp->protocol != PPP_CCP)
463 if (num_np_open == 0)
465 lcp_close(0, "No network protocols running");
469 * The peer has failed to authenticate himself using `protocol'.
472 auth_peer_fail(unit, protocol)
476 * Authentication failure: take the link down
478 lcp_close(unit, "Authentication failed");
479 status = EXIT_PEER_AUTH_FAILED;
483 * The peer has been successfully authenticated using `protocol'.
486 auth_peer_success(unit, protocol, name, namelen)
501 warn("auth_peer_success: unknown protocol %x", protocol);
506 * Save the authenticated name of the peer for later.
508 if (namelen > sizeof(peer_authname) - 1)
509 namelen = sizeof(peer_authname) - 1;
510 BCOPY(name, peer_authname, namelen);
511 peer_authname[namelen] = 0;
512 script_setenv("PEERNAME", peer_authname);
515 * If there is no more authentication still to be done,
516 * proceed to the network (or callback) phase.
518 if ((auth_pending[unit] &= ~bit) == 0)
523 * We have failed to authenticate ourselves to the peer using `protocol'.
526 auth_withpeer_fail(unit, protocol)
529 if (passwd_from_file)
530 BZERO(passwd, MAXSECRETLEN);
532 * We've failed to authenticate ourselves to our peer.
533 * Some servers keep sending CHAP challenges, but there
534 * is no point in persisting without any way to get updated
535 * authentication secrets.
537 lcp_close(unit, "Failed to authenticate ourselves to peer");
538 status = EXIT_AUTH_TOPEER_FAILED;
542 * We have successfully authenticated ourselves with the peer using `protocol'.
545 auth_withpeer_success(unit, protocol)
555 if (passwd_from_file)
556 BZERO(passwd, MAXSECRETLEN);
560 warn("auth_withpeer_success: unknown protocol %x", protocol);
565 * If there is no more authentication still being done,
566 * proceed to the network (or callback) phase.
568 if ((auth_pending[unit] &= ~bit) == 0)
574 * np_up - a network protocol has come up.
580 if (num_np_up == 0) {
582 * At this point we consider that the link has come up successfully.
587 if (idle_time_limit > 0)
588 TIMEOUT(check_idle, NULL, idle_time_limit);
591 * Set a timeout to close the connection once the maximum
592 * connect time has expired.
595 TIMEOUT(connect_time_expired, 0, maxconnect);
598 * Detach now, if the updetach option was given.
600 if (updetach && !nodetach)
607 * np_down - a network protocol has gone down.
613 if (--num_np_up == 0 && idle_time_limit > 0) {
614 UNTIMEOUT(check_idle, NULL);
619 * np_finished - a network protocol has finished using the link.
622 np_finished(unit, proto)
625 if (--num_np_open <= 0) {
626 /* no further use for the link: shut up shop. */
627 lcp_close(0, "No network protocols running");
632 * check_idle - check whether the link has been idle for long
633 * enough that we can shut it down.
639 struct ppp_idle idle;
642 if (!get_idle_time(0, &idle))
644 itime = MIN(idle.xmit_idle, idle.recv_idle);
645 if (itime >= idle_time_limit) {
646 /* link is idle: shut it down. */
647 notice("Terminating connection due to lack of activity.");
648 lcp_close(0, "Link inactive");
650 status = EXIT_IDLE_TIMEOUT;
652 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
657 * connect_time_expired - log a message and close the connection.
660 connect_time_expired(arg)
663 info("Connect time expired");
664 lcp_close(0, "Connect time expired"); /* Close connection */
665 status = EXIT_CONNECT_TIME;
669 * auth_check_options - called to check authentication options.
674 lcp_options *wo = &lcp_wantoptions[0];
678 /* Default our_name to hostname, and user to our_name */
679 if (our_name[0] == 0 || usehostname)
680 strlcpy(our_name, hostname, sizeof(our_name));
682 strlcpy(user, our_name, sizeof(user));
685 * If we have a default route, require the peer to authenticate
686 * unless the noauth option was given.
688 if (!auth_required && !allow_any_ip && have_route_to(0))
691 /* If authentication is required, ask peer for CHAP or PAP. */
693 if (!wo->neg_chap && !wo->neg_upap) {
703 * Check whether we have appropriate secrets to use
704 * to authenticate the peer.
707 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
708 if (!can_auth && wo->neg_chap) {
709 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
710 our_name, 1, &lacks_ip);
713 if (auth_required && !can_auth) {
716 "The remote system (%s) is required to authenticate itself but I",
720 "The remote system is required to authenticate itself but I");
724 "couldn't find any suitable secret (password) for it to use to do so.");
727 "couldn't find any secret (password) which would let it use an IP address.");
734 * auth_reset - called when LCP is starting negotiations to recheck
735 * authentication options, i.e. whether we have appropriate secrets
736 * to use for authenticating ourselves and/or the peer.
742 lcp_options *go = &lcp_gotoptions[unit];
743 lcp_options *ao = &lcp_allowoptions[0];
745 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
746 ao->neg_chap = !refuse_chap
748 || have_chap_secret(user, (explicit_remote? remote_name: NULL),
751 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
754 if (!have_chap_secret((explicit_remote? remote_name: NULL),
762 * check_passwd - Check the user name and passwd against the PAP secrets
763 * file. If requested, also check against the system password database,
764 * and login the user if OK.
767 * UPAP_AUTHNAK: Authentication failed.
768 * UPAP_AUTHACK: Authentication succeeded.
769 * In either case, msg points to an appropriate message.
772 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
784 struct wordlist *addrs;
785 char passwd[256], user[256];
786 char secret[MAXWORDLEN];
787 static int attempts = 0;
790 * Make copies of apasswd and auser, then null-terminate them.
792 BCOPY(apasswd, passwd, passwdlen);
793 passwd[passwdlen] = '\0';
794 BCOPY(auser, user, userlen);
795 user[userlen] = '\0';
799 * Open the file of pap secrets and scan for a suitable secret
800 * for authenticating this user.
802 filename = _PATH_UPAPFILE;
805 f = fopen(filename, "r");
807 error("Can't open PAP password file %s: %m", filename);
810 check_access(f, filename);
811 if (scan_authfile(f, user, our_name, secret, &addrs, filename) < 0) {
812 warn("no PAP secret found for %s", user);
813 } else if (secret[0] != 0) {
814 /* password given in pap-secrets - must match */
815 if ((!cryptpap && strcmp(passwd, secret) == 0)
816 || strcmp(crypt(passwd, secret), secret) == 0)
819 warn("PAP authentication failure for %s", user);
820 } else if (uselogin) {
821 /* empty password in pap-secrets and login option */
822 ret = plogin(user, passwd, msg, msglen);
823 if (ret == UPAP_AUTHNAK)
824 warn("PAP login failure for %s", user);
826 /* empty password in pap-secrets and login option not used */
832 if (ret == UPAP_AUTHNAK) {
833 if (*msg == (char *) 0)
834 *msg = "Login incorrect";
835 *msglen = strlen(*msg);
837 * XXX can we ever get here more than once??
838 * Frustrate passwd stealer programs.
839 * Allow 10 tries, but start backing off after 3 (stolen from login).
840 * On 10'th, drop the connection.
842 if (attempts++ >= 10) {
843 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
844 lcp_close(unit, "login failed");
847 sleep((u_int) (attempts - 3) * 5);
849 free_wordlist(addrs);
852 attempts = 0; /* Reset count */
853 if (*msg == (char *) 0)
855 *msglen = strlen(*msg);
856 set_allowed_addrs(unit, addrs);
859 BZERO(passwd, sizeof(passwd));
860 BZERO(secret, sizeof(secret));
866 * This function is needed for PAM.
870 /* Static variables used to communicate between the conversation function
871 * and the server_login function
873 static char *PAM_username;
874 static char *PAM_password;
875 static int PAM_error = 0;
876 static pam_handle_t *pamh = NULL;
878 /* PAM conversation function
879 * Here we assume (for now, at least) that echo on means login name, and
880 * echo off means password.
883 static int PAM_conv (int num_msg, const struct pam_message **msg,
884 struct pam_response **resp, void *appdata_ptr)
887 struct pam_response *reply = NULL;
889 #define COPY_STRING(s) (s) ? strdup(s) : NULL
891 reply = malloc(sizeof(struct pam_response) * num_msg);
892 if (!reply) return PAM_CONV_ERR;
894 for (replies = 0; replies < num_msg; replies++) {
895 switch (msg[replies]->msg_style) {
896 case PAM_PROMPT_ECHO_ON:
897 reply[replies].resp_retcode = PAM_SUCCESS;
898 reply[replies].resp = COPY_STRING(PAM_username);
901 case PAM_PROMPT_ECHO_OFF:
902 reply[replies].resp_retcode = PAM_SUCCESS;
903 reply[replies].resp = COPY_STRING(PAM_password);
909 /* ignore it, but pam still wants a NULL response... */
910 reply[replies].resp_retcode = PAM_SUCCESS;
911 reply[replies].resp = NULL;
914 /* Must be an error of some sort... */
924 static struct pam_conv PAM_conversation = {
931 * plogin - Check the user name and password against the system
932 * password database, and login the user if OK.
935 * UPAP_AUTHNAK: Login failed.
936 * UPAP_AUTHACK: Login succeeded.
937 * In either case, msg points to an appropriate message.
941 plogin(user, passwd, msg, msglen)
952 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
953 if (pam_error != PAM_SUCCESS) {
954 *msg = (char *) pam_strerror (pamh, pam_error);
959 * Define the fields for the credential validation
963 PAM_password = passwd;
965 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
970 pam_error = pam_authenticate (pamh, PAM_SILENT);
971 if (pam_error == PAM_SUCCESS && !PAM_error) {
972 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
973 if (pam_error == PAM_SUCCESS)
974 pam_open_session (pamh, PAM_SILENT);
977 *msg = (char *) pam_strerror (pamh, pam_error);
982 reopen_log(); /* apparently the PAM stuff does closelog() */
985 if (pam_error != PAM_SUCCESS)
987 #else /* #ifdef USE_PAM */
990 * Use the non-PAM methods directly
995 struct spwd *getspnam();
997 struct passwd *pw = getpwnam(user);
1001 return (UPAP_AUTHNAK);
1004 spwd = getspnam(user);
1007 /* check the age of the password entry */
1008 long now = time(NULL) / 86400L;
1010 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1011 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1012 && spwd->sp_lstchg >= 0
1013 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1014 warn("Password for %s has expired", user);
1015 return (UPAP_AUTHNAK);
1017 pw->pw_passwd = spwd->sp_pwdp;
1022 * If no passwd, don't let them login.
1024 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1025 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1026 return (UPAP_AUTHNAK);
1028 #endif /* #ifdef USE_PAM */
1031 * Write a wtmp entry for this user.
1035 if (strncmp(tty, "/dev/", 5) == 0)
1037 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1039 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1040 if (pw != (struct passwd *)NULL) {
1044 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1045 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1046 memset((void *)&ll, 0, sizeof(ll));
1047 (void)time(&ll.ll_time);
1048 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1049 (void)write(fd, (char *)&ll, sizeof(ll));
1053 #endif /* _PATH_LASTLOG and not USE_PAM */
1055 info("user %s logged in", user);
1058 return (UPAP_AUTHACK);
1062 * plogout - Logout the user.
1071 pam_error = pam_close_session (pamh, PAM_SILENT);
1072 pam_end (pamh, pam_error);
1075 /* Apparently the pam stuff does closelog(). */
1077 #else /* ! USE_PAM */
1081 if (strncmp(tty, "/dev/", 5) == 0)
1083 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1084 #endif /* ! USE_PAM */
1090 * null_login - Check if a username of "" and a password of "" are
1091 * acceptable, and iff so, set the list of acceptable IP addresses
1101 struct wordlist *addrs;
1102 char secret[MAXWORDLEN];
1105 * Open the file of pap secrets and scan for a suitable secret.
1106 * We don't accept a wildcard client.
1108 filename = _PATH_UPAPFILE;
1110 f = fopen(filename, "r");
1113 check_access(f, filename);
1115 i = scan_authfile(f, "", our_name, secret, &addrs, filename);
1116 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1117 BZERO(secret, sizeof(secret));
1120 set_allowed_addrs(unit, addrs);
1122 free_wordlist(addrs);
1130 * get_pap_passwd - get a password for authenticating ourselves with
1131 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1133 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1136 get_pap_passwd(passwd)
1142 struct wordlist *addrs;
1143 char secret[MAXWORDLEN];
1145 filename = _PATH_UPAPFILE;
1147 f = fopen(filename, "r");
1150 check_access(f, filename);
1151 ret = scan_authfile(f, user,
1152 (remote_name[0]? remote_name: NULL),
1153 secret, NULL, filename);
1158 strlcpy(passwd, secret, MAXSECRETLEN);
1159 BZERO(secret, sizeof(secret));
1165 * have_pap_secret - check whether we have a PAP file with any
1166 * secrets that we could possibly use for authenticating the peer.
1169 have_pap_secret(lacks_ipp)
1175 struct wordlist *addrs;
1177 filename = _PATH_UPAPFILE;
1178 f = fopen(filename, "r");
1182 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1183 NULL, &addrs, filename);
1185 if (ret >= 0 && !some_ip_ok(addrs)) {
1191 free_wordlist(addrs);
1198 * have_chap_secret - check whether we have a CHAP file with a
1199 * secret that we could possibly use for authenticating `client'
1200 * on `server'. Either can be the null string, meaning we don't
1201 * know the identity yet.
1204 have_chap_secret(client, server, need_ip, lacks_ipp)
1213 struct wordlist *addrs;
1215 filename = _PATH_CHAPFILE;
1216 f = fopen(filename, "r");
1220 if (client != NULL && client[0] == 0)
1222 else if (server != NULL && server[0] == 0)
1225 ret = scan_authfile(f, client, server, NULL, &addrs, filename);
1227 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1233 free_wordlist(addrs);
1240 * get_secret - open the CHAP secret file and return the secret
1241 * for authenticating the given client on the given server.
1242 * (We could be either client or server).
1245 get_secret(unit, client, server, secret, secret_len, am_server)
1256 struct wordlist *addrs;
1257 char secbuf[MAXWORDLEN];
1259 if (!am_server && passwd[0] != 0) {
1260 strlcpy(secbuf, passwd, sizeof(secbuf));
1262 filename = _PATH_CHAPFILE;
1266 f = fopen(filename, "r");
1268 error("Can't open chap secret file %s: %m", filename);
1271 check_access(f, filename);
1273 ret = scan_authfile(f, client, server, secbuf, &addrs, filename);
1279 set_allowed_addrs(unit, addrs);
1282 len = strlen(secbuf);
1283 if (len > MAXSECRETLEN) {
1284 error("Secret for %s on %s is too long", client, server);
1287 BCOPY(secbuf, secret, len);
1288 BZERO(secbuf, sizeof(secbuf));
1295 * set_allowed_addrs() - set the list of allowed addresses.
1296 * Also looks for `--' indicating options to apply for this peer
1297 * and leaves the following words in extra_options.
1300 set_allowed_addrs(unit, addrs)
1302 struct wordlist *addrs;
1305 struct wordlist *ap, **pap;
1306 struct permitted_ip *ip;
1307 char *ptr_word, *ptr_mask;
1310 u_int32_t a, mask, ah, offset;
1311 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1312 u_int32_t suggested_ip = 0;
1314 if (addresses[unit] != NULL)
1315 free(addresses[unit]);
1316 addresses[unit] = NULL;
1317 if (extra_options != NULL)
1318 free_wordlist(extra_options);
1319 extra_options = NULL;
1322 * Count the number of IP addresses given, and chop off
1323 * any extra options for this peer.
1325 for (n = 0, pap = &addrs; (ap = *pap) != NULL; pap = &ap->next, ++n) {
1326 if (strcmp(ap->word, "--") == 0) {
1327 /* rest are options */
1329 extra_options = ap->next;
1336 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1341 for (ap = addrs; ap != NULL; ap = ap->next) {
1342 /* "-" means no addresses authorized, "*" means any address allowed */
1343 ptr_word = ap->word;
1344 if (strcmp(ptr_word, "-") == 0)
1346 if (strcmp(ptr_word, "*") == 0) {
1348 ip[n].base = ip[n].mask = 0;
1354 if (*ptr_word == '!') {
1359 mask = ~ (u_int32_t) 0;
1361 ptr_mask = strchr (ptr_word, '/');
1362 if (ptr_mask != NULL) {
1366 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1367 if (bit_count <= 0 || bit_count > 32) {
1368 warn("invalid address length %v in auth. address list",
1372 bit_count = 32 - bit_count; /* # bits in host part */
1374 offset = ifunit + 1;
1378 warn("invalid address length syntax: %v", ptr_mask+1);
1385 hp = gethostbyname(ptr_word);
1386 if (hp != NULL && hp->h_addrtype == AF_INET) {
1387 a = *(u_int32_t *)hp->h_addr;
1389 np = getnetbyname (ptr_word);
1390 if (np != NULL && np->n_addrtype == AF_INET) {
1391 a = htonl (*(u_int32_t *)np->n_net);
1392 if (ptr_mask == NULL) {
1393 /* calculate appropriate mask for net */
1396 mask = IN_CLASSA_NET;
1397 else if (IN_CLASSB(ah))
1398 mask = IN_CLASSB_NET;
1399 else if (IN_CLASSC(ah))
1400 mask = IN_CLASSC_NET;
1403 a = inet_addr (ptr_word);
1407 if (ptr_mask != NULL)
1410 if (a == (u_int32_t)-1L) {
1411 warn("unknown host %s in auth. address list", ap->word);
1415 if (offset >= ~mask) {
1416 warn("interface unit %d too large for subnet %v",
1420 a = htonl((ntohl(a) & mask) + offset);
1421 mask = ~(u_int32_t)0;
1423 ip[n].mask = htonl(mask);
1424 ip[n].base = a & ip[n].mask;
1426 if (~mask == 0 && suggested_ip == 0)
1430 ip[n].permit = 0; /* make the last entry forbid all addresses */
1431 ip[n].base = 0; /* to terminate the list */
1434 addresses[unit] = ip;
1437 * If the address given for the peer isn't authorized, or if
1438 * the user hasn't given one, AND there is an authorized address
1439 * which is a single host, then use that if we find one.
1441 if (suggested_ip != 0
1442 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
1443 wo->hisaddr = suggested_ip;
1447 * auth_ip_addr - check whether the peer is authorized to use
1448 * a given IP address. Returns 1 if authorized, 0 otherwise.
1451 auth_ip_addr(unit, addr)
1455 if (addresses[unit] == NULL) {
1457 return 0; /* no addresses authorized */
1458 return allow_any_ip || !have_route_to(addr);
1460 return ip_addr_check(addr, addresses[unit]);
1464 ip_addr_check(addr, addrs)
1466 struct permitted_ip *addrs;
1468 /* don't allow loopback or multicast address */
1469 if (bad_ip_adrs(addr))
1473 if ((addr & addrs->mask) == addrs->base)
1474 return addrs->permit;
1478 * bad_ip_adrs - return 1 if the IP address is one we don't want
1479 * to use, such as an address in the loopback net or a multicast address.
1480 * addr is in network byte order.
1487 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1488 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1492 * some_ip_ok - check a wordlist to see if it authorizes any
1497 struct wordlist *addrs;
1499 for (; addrs != 0; addrs = addrs->next) {
1500 if (strcmp(addrs->word, "-") == 0)
1502 if (addrs->word[0] != '!')
1503 return 1; /* some IP address is allowed */
1509 * check_access - complain if a secret file has too-liberal permissions.
1512 check_access(f, filename)
1518 if (fstat(fileno(f), &sbuf) < 0) {
1519 warn("cannot stat secret file %s: %m", filename);
1520 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1521 warn("Warning - secret file %s has world and/or group access",
1528 * scan_authfile - Scan an authorization file for a secret suitable
1529 * for authenticating `client' on `server'. The return value is -1
1530 * if no secret is found, otherwise >= 0. The return value has
1531 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1532 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1533 * Any following words on the line (i.e. address authorization
1534 * info) are placed in a wordlist and returned in *addrs.
1535 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1538 scan_authfile(f, client, server, secret, addrs, filename)
1543 struct wordlist **addrs;
1547 int got_flag, best_flag;
1549 struct wordlist *ap, *addr_list, *alist, *alast;
1550 char word[MAXWORDLEN];
1551 char atfile[MAXWORDLEN];
1552 char lsecret[MAXWORDLEN];
1557 if (!getword(f, word, &newline, filename))
1558 return -1; /* file is empty??? */
1563 * Skip until we find a word at the start of a line.
1565 while (!newline && getword(f, word, &newline, filename))
1568 break; /* got to end of file */
1571 * Got a client - check if it's a match or a wildcard.
1574 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1579 got_flag = NONWILD_CLIENT;
1582 * Now get a server and check if it matches.
1584 if (!getword(f, word, &newline, filename))
1588 if (!ISWILD(word)) {
1589 if (server != NULL && strcmp(word, server) != 0)
1591 got_flag |= NONWILD_SERVER;
1595 * Got some sort of a match - see if it's better than what
1598 if (got_flag <= best_flag)
1604 if (!getword(f, word, &newline, filename))
1610 * Special syntax: @filename means read secret from file.
1612 if (word[0] == '@') {
1613 strlcpy(atfile, word+1, sizeof(atfile));
1614 if ((sf = fopen(atfile, "r")) == NULL) {
1615 warn("can't open indirect secret file %s", atfile);
1618 check_access(sf, atfile);
1619 if (!getword(sf, word, &xxx, atfile)) {
1620 warn("no secret in indirect secret file %s", atfile);
1627 strlcpy(lsecret, word, sizeof(lsecret));
1630 * Now read address authorization info and make a wordlist.
1632 alist = alast = NULL;
1634 if (!getword(f, word, &newline, filename) || newline)
1636 ap = (struct wordlist *) malloc(sizeof(struct wordlist));
1638 novm("authorized addresses");
1640 ap->word = strdup(word);
1641 if (ap->word == NULL)
1642 novm("authorized address");
1651 * This is the best so far; remember it.
1653 best_flag = got_flag;
1655 free_wordlist(addr_list);
1658 strlcpy(secret, lsecret, MAXWORDLEN);
1666 else if (addr_list != NULL)
1667 free_wordlist(addr_list);
1673 * free_wordlist - release memory allocated for a wordlist.
1677 struct wordlist *wp;
1679 struct wordlist *next;
1681 while (wp != NULL) {
1689 * auth_script_done - called when the auth-up or auth-down script
1693 auth_script_done(arg)
1696 auth_script_pid = 0;
1697 switch (auth_script_state) {
1699 if (auth_state == s_down) {
1700 auth_script_state = s_down;
1701 auth_script(_PATH_AUTHDOWN);
1705 if (auth_state == s_up) {
1706 auth_script_state = s_up;
1707 auth_script(_PATH_AUTHUP);
1714 * auth_script - execute a script with arguments
1715 * interface-name peer-name real-user tty speed
1727 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1728 user_name = pw->pw_name;
1730 slprintf(struid, sizeof(struid), "%d", getuid());
1733 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1737 argv[2] = peer_authname;
1738 argv[3] = user_name;
1743 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);