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.60 1999/12/23 01:25:13 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 /* Wordlist giving addresses which the peer may use
100 without authenticating itself. */
101 static struct wordlist *noauth_addrs;
103 /* Extra options to apply, from the secrets file entry for the peer. */
104 static struct wordlist *extra_options;
106 /* Number of network protocols which we have opened. */
107 static int num_np_open;
109 /* Number of network protocols which have come up. */
110 static int num_np_up;
112 /* Set if we got the contents of passwd[] from the pap-secrets file. */
113 static int passwd_from_file;
115 /* Set if we require authentication only because we have a default route. */
116 static bool default_auth;
118 /* Hook to enable a plugin to control the idle time limit */
119 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
121 /* Hook for a plugin to say whether we can possibly authenticate any peer */
122 int (*pap_check_hook) __P((void)) = NULL;
124 /* Hook for a plugin to check the PAP user and password */
125 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
126 struct wordlist **paddrs,
127 struct wordlist **popts)) = NULL;
129 /* Hook for a plugin to know about the PAP user logout */
130 void (*pap_logout_hook) __P((void)) = NULL;
132 /* Hook for a plugin to get the PAP password for authenticating us */
133 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
136 * This is used to ensure that we don't start an auth-up/down
137 * script while one is already running.
144 static enum script_state auth_state = s_down;
145 static enum script_state auth_script_state = s_down;
146 static pid_t auth_script_pid = 0;
151 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
152 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
153 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
154 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
155 bool usehostname = 0; /* Use hostname for our_name */
156 bool auth_required = 0; /* Always require authentication from peer */
157 bool allow_any_ip = 0; /* Allow peer to use any IP address */
158 bool explicit_remote = 0; /* User specified explicit remote name */
159 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
161 /* Bits in auth_pending[] */
162 #define PAP_WITHPEER 1
164 #define CHAP_WITHPEER 4
167 extern char *crypt __P((const char *, const char *));
169 /* Prototypes for procedures local to this file. */
171 static void network_phase __P((int));
172 static void check_idle __P((void *));
173 static void connect_time_expired __P((void *));
174 static int plogin __P((char *, char *, char **));
175 static void plogout __P((void));
176 static int null_login __P((int));
177 static int get_pap_passwd __P((char *));
178 static int have_pap_secret __P((int *));
179 static int have_chap_secret __P((char *, char *, int, int *));
180 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
181 static int scan_authfile __P((FILE *, char *, char *, char *,
182 struct wordlist **, struct wordlist **,
184 static void free_wordlist __P((struct wordlist *));
185 static void auth_script __P((char *));
186 static void auth_script_done __P((void *));
187 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
188 static int some_ip_ok __P((struct wordlist *));
189 static int setupapfile __P((char **));
190 static int privgroup __P((char **));
191 static int set_noauth_addr __P((char **));
192 static void check_access __P((FILE *, char *));
195 * Authentication-related options.
197 option_t auth_options[] = {
198 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
199 "Require PAP authentication from peer", 1, &auth_required },
200 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
201 "Require PAP authentication from peer", 1, &auth_required },
202 { "refuse-pap", o_bool, &refuse_pap,
203 "Don't agree to auth to peer with PAP", 1 },
204 { "-pap", o_bool, &refuse_pap,
205 "Don't allow PAP authentication with peer", 1 },
206 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
207 "Require CHAP authentication from peer", 1, &auth_required },
208 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
209 "Require CHAP authentication from peer", 1, &auth_required },
210 { "refuse-chap", o_bool, &refuse_chap,
211 "Don't agree to auth to peer with CHAP", 1 },
212 { "-chap", o_bool, &refuse_chap,
213 "Don't allow CHAP authentication with peer", 1 },
214 { "name", o_string, our_name,
215 "Set local name for authentication",
216 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
217 { "user", o_string, user,
218 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
219 { "usehostname", o_bool, &usehostname,
220 "Must use hostname for authentication", 1 },
221 { "remotename", o_string, remote_name,
222 "Set remote name for authentication", OPT_STATIC,
223 &explicit_remote, MAXNAMELEN },
224 { "auth", o_bool, &auth_required,
225 "Require authentication from peer", 1 },
226 { "noauth", o_bool, &auth_required,
227 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
228 { "login", o_bool, &uselogin,
229 "Use system password database for PAP", 1 },
230 { "papcrypt", o_bool, &cryptpap,
231 "PAP passwords are encrypted", 1 },
232 { "+ua", o_special, setupapfile,
233 "Get PAP user and password from file" },
234 { "password", o_string, passwd,
235 "Password for authenticating us to the peer", OPT_STATIC,
236 NULL, MAXSECRETLEN },
237 { "privgroup", o_special, privgroup,
238 "Allow group members to use privileged options", OPT_PRIV },
239 { "allow-ip", o_special, set_noauth_addr,
240 "Set IP address(es) which can be used without authentication",
246 * setupapfile - specifies UPAP info for authenticating with peer.
255 lcp_allowoptions[0].neg_upap = 1;
257 /* open user info file */
259 ufile = fopen(*argv, "r");
262 option_error("unable to open user login data file %s", *argv);
265 check_access(ufile, *argv);
268 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
269 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
270 option_error("unable to read user login data file %s", *argv);
275 /* get rid of newlines */
277 if (l > 0 && user[l-1] == '\n')
280 if (l > 0 && passwd[l-1] == '\n')
288 * privgroup - allow members of the group to have privileged access.
299 option_error("group %s is unknown", *argv);
302 for (i = 0; i < ngroups; ++i) {
303 if (groups[i] == g->gr_gid) {
313 * set_noauth_addr - set address(es) that can be used without authentication.
314 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
317 set_noauth_addr(argv)
321 int l = strlen(addr);
324 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l + 1);
326 novm("allow-ip argument");
327 wp->word = (char *) (wp + 1);
328 wp->next = noauth_addrs;
329 BCOPY(addr, wp->word, l);
336 * An Open on LCP has requested a change from Dead to Establish phase.
337 * Do what's necessary to bring the physical layer up.
346 * LCP has terminated the link; go to the Dead phase and take the
347 * physical layer down.
350 link_terminated(unit)
353 if (phase == PHASE_DEAD)
355 if (pap_logout_hook) {
361 new_phase(PHASE_DEAD);
362 notice("Connection terminated.");
366 * LCP has gone down; it will either die or try to re-establish.
373 struct protent *protp;
376 if (auth_script_state == s_up && auth_script_pid == 0) {
377 update_link_stats(unit);
378 auth_script_state = s_down;
379 auth_script(_PATH_AUTHDOWN);
381 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
382 if (!protp->enabled_flag)
384 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
385 (*protp->lowerdown)(unit);
386 if (protp->protocol < 0xC000 && protp->close != NULL)
387 (*protp->close)(unit, "LCP down");
391 if (phase != PHASE_DEAD)
392 new_phase(PHASE_TERMINATE);
396 * The link is established.
397 * Proceed to the Dead, Authenticate or Network phase as appropriate.
400 link_established(unit)
404 lcp_options *wo = &lcp_wantoptions[unit];
405 lcp_options *go = &lcp_gotoptions[unit];
406 lcp_options *ho = &lcp_hisoptions[unit];
408 struct protent *protp;
411 * Tell higher-level protocols that LCP is up.
413 for (i = 0; (protp = protocols[i]) != NULL; ++i)
414 if (protp->protocol != PPP_LCP && protp->enabled_flag
415 && protp->lowerup != NULL)
416 (*protp->lowerup)(unit);
418 if (auth_required && !(go->neg_chap || go->neg_upap)) {
420 * We wanted the peer to authenticate itself, and it refused:
421 * if we have some address(es) it can use without auth, fine,
422 * otherwise treat it as though it authenticated with PAP using
423 * a username * of "" and a password of "". If that's not OK,
426 if (noauth_addrs != NULL) {
427 set_allowed_addrs(unit, noauth_addrs, NULL);
428 } else if (!wo->neg_upap || !null_login(unit)) {
429 warn("peer refused to authenticate: terminating link");
430 lcp_close(unit, "peer refused to authenticate");
431 status = EXIT_PEER_AUTH_FAILED;
436 new_phase(PHASE_AUTHENTICATE);
439 ChapAuthPeer(unit, our_name, go->chap_mdtype);
441 } else if (go->neg_upap) {
446 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
447 auth |= CHAP_WITHPEER;
448 } else if (ho->neg_upap) {
449 if (passwd[0] == 0) {
450 passwd_from_file = 1;
451 if (!get_pap_passwd(passwd))
452 error("No secret found for PAP login");
454 upap_authwithpeer(unit, user, passwd);
455 auth |= PAP_WITHPEER;
457 auth_pending[unit] = auth;
464 * Proceed to the network phase.
470 lcp_options *go = &lcp_gotoptions[unit];
473 * If the peer had to authenticate, run the auth-up script now.
475 if (go->neg_chap || go->neg_upap) {
477 if (auth_script_state == s_down && auth_script_pid == 0) {
478 auth_script_state = s_up;
479 auth_script(_PATH_AUTHUP);
485 * If we negotiated callback, do it now.
488 new_phase(PHASE_CALLBACK);
489 (*cbcp_protent.open)(unit);
495 * Process extra options from the secrets file
498 options_from_list(extra_options, 1);
499 free_wordlist(extra_options);
509 struct protent *protp;
511 new_phase(PHASE_NETWORK);
514 set_filters(&pass_filter, &active_filter);
516 for (i = 0; (protp = protocols[i]) != NULL; ++i)
517 if (protp->protocol < 0xC000 && protp->enabled_flag
518 && protp->open != NULL) {
520 if (protp->protocol != PPP_CCP)
524 if (num_np_open == 0)
526 lcp_close(0, "No network protocols running");
530 * The peer has failed to authenticate himself using `protocol'.
533 auth_peer_fail(unit, protocol)
537 * Authentication failure: take the link down
539 lcp_close(unit, "Authentication failed");
540 status = EXIT_PEER_AUTH_FAILED;
544 * The peer has been successfully authenticated using `protocol'.
547 auth_peer_success(unit, protocol, name, namelen)
562 warn("auth_peer_success: unknown protocol %x", protocol);
567 * Save the authenticated name of the peer for later.
569 if (namelen > sizeof(peer_authname) - 1)
570 namelen = sizeof(peer_authname) - 1;
571 BCOPY(name, peer_authname, namelen);
572 peer_authname[namelen] = 0;
573 script_setenv("PEERNAME", peer_authname);
576 * If there is no more authentication still to be done,
577 * proceed to the network (or callback) phase.
579 if ((auth_pending[unit] &= ~bit) == 0)
584 * We have failed to authenticate ourselves to the peer using `protocol'.
587 auth_withpeer_fail(unit, protocol)
590 if (passwd_from_file)
591 BZERO(passwd, MAXSECRETLEN);
593 * We've failed to authenticate ourselves to our peer.
594 * Some servers keep sending CHAP challenges, but there
595 * is no point in persisting without any way to get updated
596 * authentication secrets.
598 lcp_close(unit, "Failed to authenticate ourselves to peer");
599 status = EXIT_AUTH_TOPEER_FAILED;
603 * We have successfully authenticated ourselves with the peer using `protocol'.
606 auth_withpeer_success(unit, protocol)
616 if (passwd_from_file)
617 BZERO(passwd, MAXSECRETLEN);
621 warn("auth_withpeer_success: unknown protocol %x", protocol);
626 * If there is no more authentication still being done,
627 * proceed to the network (or callback) phase.
629 if ((auth_pending[unit] &= ~bit) == 0)
635 * np_up - a network protocol has come up.
643 if (num_np_up == 0) {
645 * At this point we consider that the link has come up successfully.
649 new_phase(PHASE_RUNNING);
651 if (idle_time_hook != 0)
652 tlim = (*idle_time_hook)(NULL);
654 tlim = idle_time_limit;
656 TIMEOUT(check_idle, NULL, tlim);
659 * Set a timeout to close the connection once the maximum
660 * connect time has expired.
663 TIMEOUT(connect_time_expired, 0, maxconnect);
666 * Detach now, if the updetach option was given.
668 if (updetach && !nodetach)
675 * np_down - a network protocol has gone down.
681 if (--num_np_up == 0) {
682 UNTIMEOUT(check_idle, NULL);
683 new_phase(PHASE_NETWORK);
688 * np_finished - a network protocol has finished using the link.
691 np_finished(unit, proto)
694 if (--num_np_open <= 0) {
695 /* no further use for the link: shut up shop. */
696 lcp_close(0, "No network protocols running");
701 * check_idle - check whether the link has been idle for long
702 * enough that we can shut it down.
708 struct ppp_idle idle;
712 if (!get_idle_time(0, &idle))
714 if (idle_time_hook != 0) {
715 tlim = idle_time_hook(&idle);
717 itime = MIN(idle.xmit_idle, idle.recv_idle);
718 tlim = idle_time_limit - itime;
721 /* link is idle: shut it down. */
722 notice("Terminating connection due to lack of activity.");
723 lcp_close(0, "Link inactive");
725 status = EXIT_IDLE_TIMEOUT;
727 TIMEOUT(check_idle, NULL, tlim);
732 * connect_time_expired - log a message and close the connection.
735 connect_time_expired(arg)
738 info("Connect time expired");
739 lcp_close(0, "Connect time expired"); /* Close connection */
740 status = EXIT_CONNECT_TIME;
744 * auth_check_options - called to check authentication options.
749 lcp_options *wo = &lcp_wantoptions[0];
753 /* Default our_name to hostname, and user to our_name */
754 if (our_name[0] == 0 || usehostname)
755 strlcpy(our_name, hostname, sizeof(our_name));
757 strlcpy(user, our_name, sizeof(user));
760 * If we have a default route, require the peer to authenticate
761 * unless the noauth option was given or the real user is root.
763 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
768 /* If authentication is required, ask peer for CHAP or PAP. */
770 if (!wo->neg_chap && !wo->neg_upap) {
780 * Check whether we have appropriate secrets to use
781 * to authenticate the peer.
784 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
785 if (!can_auth && wo->neg_chap) {
786 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
787 our_name, 1, &lacks_ip);
790 if (auth_required && !can_auth && noauth_addrs == NULL) {
793 "By default the remote system is required to authenticate itself");
795 "(because this system has a default route to the internet)");
796 } else if (explicit_remote)
798 "The remote system (%s) is required to authenticate itself",
802 "The remote system is required to authenticate itself");
804 "but I couldn't find any suitable secret (password) for it to use to do so.");
807 "(None of the available passwords would let it use an IP address.)");
814 * auth_reset - called when LCP is starting negotiations to recheck
815 * authentication options, i.e. whether we have appropriate secrets
816 * to use for authenticating ourselves and/or the peer.
822 lcp_options *go = &lcp_gotoptions[unit];
823 lcp_options *ao = &lcp_allowoptions[0];
825 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
826 ao->neg_chap = !refuse_chap
828 || have_chap_secret(user, (explicit_remote? remote_name: NULL),
831 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
834 if (!have_chap_secret((explicit_remote? remote_name: NULL),
842 * check_passwd - Check the user name and passwd against the PAP secrets
843 * file. If requested, also check against the system password database,
844 * and login the user if OK.
847 * UPAP_AUTHNAK: Authentication failed.
848 * UPAP_AUTHACK: Authentication succeeded.
849 * In either case, msg points to an appropriate message.
852 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
863 struct wordlist *addrs = NULL, *opts = NULL;
864 char passwd[256], user[256];
865 char secret[MAXWORDLEN];
866 static int attempts = 0;
869 * Make copies of apasswd and auser, then null-terminate them.
870 * If there are unprintable characters in the password, make
873 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
874 slprintf(user, sizeof(user), "%.*v", userlen, auser);
878 * Check if a plugin wants to handle this.
881 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
884 set_allowed_addrs(unit, addrs, opts);
885 BZERO(passwd, sizeof(passwd));
887 free_wordlist(addrs);
888 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
893 * Open the file of pap secrets and scan for a suitable secret
894 * for authenticating this user.
896 filename = _PATH_UPAPFILE;
899 f = fopen(filename, "r");
901 error("Can't open PAP password file %s: %m", filename);
904 check_access(f, filename);
905 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
906 warn("no PAP secret found for %s", user);
907 } else if (secret[0] != 0) {
908 /* password given in pap-secrets - must match */
909 if ((!cryptpap && strcmp(passwd, secret) == 0)
910 || strcmp(crypt(passwd, secret), secret) == 0)
913 warn("PAP authentication failure for %s", user);
914 } else if (uselogin) {
915 /* empty password in pap-secrets and login option */
916 ret = plogin(user, passwd, msg);
917 if (ret == UPAP_AUTHNAK)
918 warn("PAP login failure for %s", user);
920 /* empty password in pap-secrets and login option not used */
926 if (ret == UPAP_AUTHNAK) {
928 *msg = "Login incorrect";
930 * XXX can we ever get here more than once??
931 * Frustrate passwd stealer programs.
932 * Allow 10 tries, but start backing off after 3 (stolen from login).
933 * On 10'th, drop the connection.
935 if (attempts++ >= 10) {
936 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
937 lcp_close(unit, "login failed");
940 sleep((u_int) (attempts - 3) * 5);
945 attempts = 0; /* Reset count */
948 set_allowed_addrs(unit, addrs, opts);
952 free_wordlist(addrs);
953 BZERO(passwd, sizeof(passwd));
954 BZERO(secret, sizeof(secret));
960 * This function is needed for PAM.
964 /* Static variables used to communicate between the conversation function
965 * and the server_login function
967 static char *PAM_username;
968 static char *PAM_password;
969 static int PAM_error = 0;
970 static pam_handle_t *pamh = NULL;
972 /* PAM conversation function
973 * Here we assume (for now, at least) that echo on means login name, and
974 * echo off means password.
977 static int PAM_conv (int num_msg, const struct pam_message **msg,
978 struct pam_response **resp, void *appdata_ptr)
981 struct pam_response *reply = NULL;
983 #define COPY_STRING(s) (s) ? strdup(s) : NULL
985 reply = malloc(sizeof(struct pam_response) * num_msg);
986 if (!reply) return PAM_CONV_ERR;
988 for (replies = 0; replies < num_msg; replies++) {
989 switch (msg[replies]->msg_style) {
990 case PAM_PROMPT_ECHO_ON:
991 reply[replies].resp_retcode = PAM_SUCCESS;
992 reply[replies].resp = COPY_STRING(PAM_username);
995 case PAM_PROMPT_ECHO_OFF:
996 reply[replies].resp_retcode = PAM_SUCCESS;
997 reply[replies].resp = COPY_STRING(PAM_password);
1003 /* ignore it, but pam still wants a NULL response... */
1004 reply[replies].resp_retcode = PAM_SUCCESS;
1005 reply[replies].resp = NULL;
1008 /* Must be an error of some sort... */
1011 return PAM_CONV_ERR;
1018 static struct pam_conv PAM_conversation = {
1022 #endif /* USE_PAM */
1025 * plogin - Check the user name and password against the system
1026 * password database, and login the user if OK.
1029 * UPAP_AUTHNAK: Login failed.
1030 * UPAP_AUTHACK: Login succeeded.
1031 * In either case, msg points to an appropriate message.
1035 plogin(user, passwd, msg)
1045 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1046 if (pam_error != PAM_SUCCESS) {
1047 *msg = (char *) pam_strerror (pamh, pam_error);
1049 return UPAP_AUTHNAK;
1052 * Define the fields for the credential validation
1055 PAM_username = user;
1056 PAM_password = passwd;
1058 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1063 pam_error = pam_authenticate (pamh, PAM_SILENT);
1064 if (pam_error == PAM_SUCCESS && !PAM_error) {
1065 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1066 if (pam_error == PAM_SUCCESS)
1067 pam_open_session (pamh, PAM_SILENT);
1070 *msg = (char *) pam_strerror (pamh, pam_error);
1075 reopen_log(); /* apparently the PAM stuff does closelog() */
1076 PAM_username = NULL;
1077 PAM_password = NULL;
1078 if (pam_error != PAM_SUCCESS)
1079 return UPAP_AUTHNAK;
1080 #else /* #ifdef USE_PAM */
1083 * Use the non-PAM methods directly
1088 struct spwd *getspnam();
1090 struct passwd *pw = getpwnam(user);
1094 return (UPAP_AUTHNAK);
1097 spwd = getspnam(user);
1100 /* check the age of the password entry */
1101 long now = time(NULL) / 86400L;
1103 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1104 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1105 && spwd->sp_lstchg >= 0
1106 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1107 warn("Password for %s has expired", user);
1108 return (UPAP_AUTHNAK);
1110 pw->pw_passwd = spwd->sp_pwdp;
1115 * If no passwd, don't let them login.
1117 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1118 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1119 return (UPAP_AUTHNAK);
1121 #endif /* #ifdef USE_PAM */
1124 * Write a wtmp entry for this user.
1128 if (strncmp(tty, "/dev/", 5) == 0)
1130 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1132 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1133 if (pw != (struct passwd *)NULL) {
1137 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1138 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1139 memset((void *)&ll, 0, sizeof(ll));
1140 (void)time(&ll.ll_time);
1141 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1142 (void)write(fd, (char *)&ll, sizeof(ll));
1146 #endif /* _PATH_LASTLOG and not USE_PAM */
1148 info("user %s logged in", user);
1151 return (UPAP_AUTHACK);
1155 * plogout - Logout the user.
1164 pam_error = pam_close_session (pamh, PAM_SILENT);
1165 pam_end (pamh, pam_error);
1168 /* Apparently the pam stuff does closelog(). */
1170 #else /* ! USE_PAM */
1174 if (strncmp(tty, "/dev/", 5) == 0)
1176 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1177 #endif /* ! USE_PAM */
1183 * null_login - Check if a username of "" and a password of "" are
1184 * acceptable, and iff so, set the list of acceptable IP addresses
1194 struct wordlist *addrs, *opts;
1195 char secret[MAXWORDLEN];
1198 * Open the file of pap secrets and scan for a suitable secret.
1200 filename = _PATH_UPAPFILE;
1202 f = fopen(filename, "r");
1205 check_access(f, filename);
1207 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename);
1208 ret = i >= 0 && secret[0] == 0;
1209 BZERO(secret, sizeof(secret));
1212 set_allowed_addrs(unit, addrs, opts);
1214 free_wordlist(opts);
1216 free_wordlist(addrs);
1224 * get_pap_passwd - get a password for authenticating ourselves with
1225 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1227 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1230 get_pap_passwd(passwd)
1236 char secret[MAXWORDLEN];
1239 * Check whether a plugin wants to supply this.
1241 if (pap_passwd_hook) {
1242 ret = (*pap_passwd_hook)(user, passwd);
1247 filename = _PATH_UPAPFILE;
1248 f = fopen(filename, "r");
1251 check_access(f, filename);
1252 ret = scan_authfile(f, user,
1253 (remote_name[0]? remote_name: NULL),
1254 secret, NULL, NULL, filename);
1259 strlcpy(passwd, secret, MAXSECRETLEN);
1260 BZERO(secret, sizeof(secret));
1266 * have_pap_secret - check whether we have a PAP file with any
1267 * secrets that we could possibly use for authenticating the peer.
1270 have_pap_secret(lacks_ipp)
1276 struct wordlist *addrs;
1278 /* let the plugin decide, if there is one */
1279 if (pap_check_hook) {
1280 ret = (*pap_check_hook)();
1285 filename = _PATH_UPAPFILE;
1286 f = fopen(filename, "r");
1290 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1291 NULL, &addrs, NULL, filename);
1293 if (ret >= 0 && !some_ip_ok(addrs)) {
1299 free_wordlist(addrs);
1306 * have_chap_secret - check whether we have a CHAP file with a
1307 * secret that we could possibly use for authenticating `client'
1308 * on `server'. Either can be the null string, meaning we don't
1309 * know the identity yet.
1312 have_chap_secret(client, server, need_ip, lacks_ipp)
1321 struct wordlist *addrs;
1323 filename = _PATH_CHAPFILE;
1324 f = fopen(filename, "r");
1328 if (client != NULL && client[0] == 0)
1330 else if (server != NULL && server[0] == 0)
1333 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename);
1335 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1341 free_wordlist(addrs);
1348 * get_secret - open the CHAP secret file and return the secret
1349 * for authenticating the given client on the given server.
1350 * (We could be either client or server).
1353 get_secret(unit, client, server, secret, secret_len, am_server)
1364 struct wordlist *addrs, *opts;
1365 char secbuf[MAXWORDLEN];
1367 if (!am_server && passwd[0] != 0) {
1368 strlcpy(secbuf, passwd, sizeof(secbuf));
1370 filename = _PATH_CHAPFILE;
1374 f = fopen(filename, "r");
1376 error("Can't open chap secret file %s: %m", filename);
1379 check_access(f, filename);
1381 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename);
1387 set_allowed_addrs(unit, addrs, opts);
1389 free_wordlist(opts);
1391 free_wordlist(addrs);
1394 len = strlen(secbuf);
1395 if (len > MAXSECRETLEN) {
1396 error("Secret for %s on %s is too long", client, server);
1399 BCOPY(secbuf, secret, len);
1400 BZERO(secbuf, sizeof(secbuf));
1407 * set_allowed_addrs() - set the list of allowed addresses.
1408 * Also looks for `--' indicating options to apply for this peer
1409 * and leaves the following words in extra_options.
1412 set_allowed_addrs(unit, addrs, opts)
1414 struct wordlist *addrs;
1415 struct wordlist *opts;
1418 struct wordlist *ap, **pap;
1419 struct permitted_ip *ip;
1420 char *ptr_word, *ptr_mask;
1423 u_int32_t a, mask, ah, offset;
1424 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1425 u_int32_t suggested_ip = 0;
1427 if (addresses[unit] != NULL)
1428 free(addresses[unit]);
1429 addresses[unit] = NULL;
1430 if (extra_options != NULL)
1431 free_wordlist(extra_options);
1432 extra_options = opts;
1435 * Count the number of IP addresses given.
1437 for (n = 0, pap = &addrs; (ap = *pap) != NULL; pap = &ap->next)
1441 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1446 for (ap = addrs; ap != NULL; ap = ap->next) {
1447 /* "-" means no addresses authorized, "*" means any address allowed */
1448 ptr_word = ap->word;
1449 if (strcmp(ptr_word, "-") == 0)
1451 if (strcmp(ptr_word, "*") == 0) {
1453 ip[n].base = ip[n].mask = 0;
1459 if (*ptr_word == '!') {
1464 mask = ~ (u_int32_t) 0;
1466 ptr_mask = strchr (ptr_word, '/');
1467 if (ptr_mask != NULL) {
1471 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1472 if (bit_count <= 0 || bit_count > 32) {
1473 warn("invalid address length %v in auth. address list",
1477 bit_count = 32 - bit_count; /* # bits in host part */
1479 offset = ifunit + 1;
1483 warn("invalid address length syntax: %v", ptr_mask+1);
1490 hp = gethostbyname(ptr_word);
1491 if (hp != NULL && hp->h_addrtype == AF_INET) {
1492 a = *(u_int32_t *)hp->h_addr;
1494 np = getnetbyname (ptr_word);
1495 if (np != NULL && np->n_addrtype == AF_INET) {
1496 a = htonl (*(u_int32_t *)np->n_net);
1497 if (ptr_mask == NULL) {
1498 /* calculate appropriate mask for net */
1501 mask = IN_CLASSA_NET;
1502 else if (IN_CLASSB(ah))
1503 mask = IN_CLASSB_NET;
1504 else if (IN_CLASSC(ah))
1505 mask = IN_CLASSC_NET;
1508 a = inet_addr (ptr_word);
1512 if (ptr_mask != NULL)
1515 if (a == (u_int32_t)-1L) {
1516 warn("unknown host %s in auth. address list", ap->word);
1520 if (offset >= ~mask) {
1521 warn("interface unit %d too large for subnet %v",
1525 a = htonl((ntohl(a) & mask) + offset);
1526 mask = ~(u_int32_t)0;
1528 ip[n].mask = htonl(mask);
1529 ip[n].base = a & ip[n].mask;
1531 if (~mask == 0 && suggested_ip == 0)
1535 ip[n].permit = 0; /* make the last entry forbid all addresses */
1536 ip[n].base = 0; /* to terminate the list */
1539 addresses[unit] = ip;
1542 * If the address given for the peer isn't authorized, or if
1543 * the user hasn't given one, AND there is an authorized address
1544 * which is a single host, then use that if we find one.
1546 if (suggested_ip != 0
1547 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
1548 wo->hisaddr = suggested_ip;
1552 * auth_ip_addr - check whether the peer is authorized to use
1553 * a given IP address. Returns 1 if authorized, 0 otherwise.
1556 auth_ip_addr(unit, addr)
1562 /* don't allow loopback or multicast address */
1563 if (bad_ip_adrs(addr))
1566 if (addresses[unit] != NULL) {
1567 ok = ip_addr_check(addr, addresses[unit]);
1572 return 0; /* no addresses authorized */
1573 return allow_any_ip || !have_route_to(addr);
1577 ip_addr_check(addr, addrs)
1579 struct permitted_ip *addrs;
1582 if ((addr & addrs->mask) == addrs->base)
1583 return addrs->permit;
1587 * bad_ip_adrs - return 1 if the IP address is one we don't want
1588 * to use, such as an address in the loopback net or a multicast address.
1589 * addr is in network byte order.
1596 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1597 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1601 * some_ip_ok - check a wordlist to see if it authorizes any
1606 struct wordlist *addrs;
1608 for (; addrs != 0; addrs = addrs->next) {
1609 if (addrs->word[0] == '-')
1611 if (addrs->word[0] != '!')
1612 return 1; /* some IP address is allowed */
1618 * check_access - complain if a secret file has too-liberal permissions.
1621 check_access(f, filename)
1627 if (fstat(fileno(f), &sbuf) < 0) {
1628 warn("cannot stat secret file %s: %m", filename);
1629 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1630 warn("Warning - secret file %s has world and/or group access",
1637 * scan_authfile - Scan an authorization file for a secret suitable
1638 * for authenticating `client' on `server'. The return value is -1
1639 * if no secret is found, otherwise >= 0. The return value has
1640 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1641 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1642 * Any following words on the line up to a "--" (i.e. address authorization
1643 * info) are placed in a wordlist and returned in *addrs. Any
1644 * following words (extra options) are placed in a wordlist and
1645 * returned in *opts.
1646 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1649 scan_authfile(f, client, server, secret, addrs, opts, filename)
1654 struct wordlist **addrs;
1655 struct wordlist **opts;
1659 int got_flag, best_flag;
1661 struct wordlist *ap, *addr_list, *alist, **app;
1662 char word[MAXWORDLEN];
1663 char atfile[MAXWORDLEN];
1664 char lsecret[MAXWORDLEN];
1671 if (!getword(f, word, &newline, filename))
1672 return -1; /* file is empty??? */
1677 * Skip until we find a word at the start of a line.
1679 while (!newline && getword(f, word, &newline, filename))
1682 break; /* got to end of file */
1685 * Got a client - check if it's a match or a wildcard.
1688 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1693 got_flag = NONWILD_CLIENT;
1696 * Now get a server and check if it matches.
1698 if (!getword(f, word, &newline, filename))
1702 if (!ISWILD(word)) {
1703 if (server != NULL && strcmp(word, server) != 0)
1705 got_flag |= NONWILD_SERVER;
1709 * Got some sort of a match - see if it's better than what
1712 if (got_flag <= best_flag)
1718 if (!getword(f, word, &newline, filename))
1724 * Special syntax: @filename means read secret from file.
1726 if (word[0] == '@') {
1727 strlcpy(atfile, word+1, sizeof(atfile));
1728 if ((sf = fopen(atfile, "r")) == NULL) {
1729 warn("can't open indirect secret file %s", atfile);
1732 check_access(sf, atfile);
1733 if (!getword(sf, word, &xxx, atfile)) {
1734 warn("no secret in indirect secret file %s", atfile);
1741 strlcpy(lsecret, word, sizeof(lsecret));
1744 * Now read address authorization info and make a wordlist.
1748 if (!getword(f, word, &newline, filename) || newline)
1750 ap = (struct wordlist *) malloc(sizeof(struct wordlist));
1752 novm("authorized addresses");
1753 ap->word = strdup(word);
1754 if (ap->word == NULL)
1755 novm("authorized addresses");
1762 * This is the best so far; remember it.
1764 best_flag = got_flag;
1766 free_wordlist(addr_list);
1769 strlcpy(secret, lsecret, MAXWORDLEN);
1775 /* scan for a -- word indicating the start of options */
1776 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
1777 if (strcmp(ap->word, "--") == 0)
1779 /* ap = start of options */
1781 ap = ap->next; /* first option */
1782 free(*app); /* free the "--" word */
1783 *app = NULL; /* terminate addr list */
1787 else if (ap != NULL)
1791 else if (addr_list != NULL)
1792 free_wordlist(addr_list);
1798 * free_wordlist - release memory allocated for a wordlist.
1802 struct wordlist *wp;
1804 struct wordlist *next;
1806 while (wp != NULL) {
1814 * auth_script_done - called when the auth-up or auth-down script
1818 auth_script_done(arg)
1821 auth_script_pid = 0;
1822 switch (auth_script_state) {
1824 if (auth_state == s_down) {
1825 auth_script_state = s_down;
1826 auth_script(_PATH_AUTHDOWN);
1830 if (auth_state == s_up) {
1831 auth_script_state = s_up;
1832 auth_script(_PATH_AUTHUP);
1839 * auth_script - execute a script with arguments
1840 * interface-name peer-name real-user tty speed
1852 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1853 user_name = pw->pw_name;
1855 slprintf(struid, sizeof(struid), "%d", getuid());
1858 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1862 argv[2] = peer_authname;
1863 argv[3] = user_name;
1868 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);