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.59 1999/11/20 05:11:01 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 get the PAP password for authenticating us */
130 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
133 * This is used to ensure that we don't start an auth-up/down
134 * script while one is already running.
141 static enum script_state auth_state = s_down;
142 static enum script_state auth_script_state = s_down;
143 static pid_t auth_script_pid = 0;
148 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
149 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
150 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
151 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
152 bool usehostname = 0; /* Use hostname for our_name */
153 bool auth_required = 0; /* Always require authentication from peer */
154 bool allow_any_ip = 0; /* Allow peer to use any IP address */
155 bool explicit_remote = 0; /* User specified explicit remote name */
156 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
158 /* Bits in auth_pending[] */
159 #define PAP_WITHPEER 1
161 #define CHAP_WITHPEER 4
164 extern char *crypt __P((const char *, const char *));
166 /* Prototypes for procedures local to this file. */
168 static void network_phase __P((int));
169 static void check_idle __P((void *));
170 static void connect_time_expired __P((void *));
171 static int plogin __P((char *, char *, char **));
172 static void plogout __P((void));
173 static int null_login __P((int));
174 static int get_pap_passwd __P((char *));
175 static int have_pap_secret __P((int *));
176 static int have_chap_secret __P((char *, char *, int, int *));
177 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
178 static int scan_authfile __P((FILE *, char *, char *, char *,
179 struct wordlist **, struct wordlist **,
181 static void free_wordlist __P((struct wordlist *));
182 static void auth_script __P((char *));
183 static void auth_script_done __P((void *));
184 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
185 static int some_ip_ok __P((struct wordlist *));
186 static int setupapfile __P((char **));
187 static int privgroup __P((char **));
188 static int set_noauth_addr __P((char **));
189 static void check_access __P((FILE *, char *));
192 * Authentication-related options.
194 option_t auth_options[] = {
195 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
196 "Require PAP authentication from peer", 1, &auth_required },
197 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
198 "Require PAP authentication from peer", 1, &auth_required },
199 { "refuse-pap", o_bool, &refuse_pap,
200 "Don't agree to auth to peer with PAP", 1 },
201 { "-pap", o_bool, &refuse_pap,
202 "Don't allow PAP authentication with peer", 1 },
203 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
204 "Require CHAP authentication from peer", 1, &auth_required },
205 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
206 "Require CHAP authentication from peer", 1, &auth_required },
207 { "refuse-chap", o_bool, &refuse_chap,
208 "Don't agree to auth to peer with CHAP", 1 },
209 { "-chap", o_bool, &refuse_chap,
210 "Don't allow CHAP authentication with peer", 1 },
211 { "name", o_string, our_name,
212 "Set local name for authentication",
213 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
214 { "user", o_string, user,
215 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
216 { "usehostname", o_bool, &usehostname,
217 "Must use hostname for authentication", 1 },
218 { "remotename", o_string, remote_name,
219 "Set remote name for authentication", OPT_STATIC,
220 &explicit_remote, MAXNAMELEN },
221 { "auth", o_bool, &auth_required,
222 "Require authentication from peer", 1 },
223 { "noauth", o_bool, &auth_required,
224 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
225 { "login", o_bool, &uselogin,
226 "Use system password database for PAP", 1 },
227 { "papcrypt", o_bool, &cryptpap,
228 "PAP passwords are encrypted", 1 },
229 { "+ua", o_special, setupapfile,
230 "Get PAP user and password from file" },
231 { "password", o_string, passwd,
232 "Password for authenticating us to the peer", OPT_STATIC,
233 NULL, MAXSECRETLEN },
234 { "privgroup", o_special, privgroup,
235 "Allow group members to use privileged options", OPT_PRIV },
236 { "allow-ip", o_special, set_noauth_addr,
237 "Set IP address(es) which can be used without authentication",
243 * setupapfile - specifies UPAP info for authenticating with peer.
252 lcp_allowoptions[0].neg_upap = 1;
254 /* open user info file */
256 ufile = fopen(*argv, "r");
259 option_error("unable to open user login data file %s", *argv);
262 check_access(ufile, *argv);
265 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
266 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
267 option_error("unable to read user login data file %s", *argv);
272 /* get rid of newlines */
274 if (l > 0 && user[l-1] == '\n')
277 if (l > 0 && passwd[l-1] == '\n')
285 * privgroup - allow members of the group to have privileged access.
296 option_error("group %s is unknown", *argv);
299 for (i = 0; i < ngroups; ++i) {
300 if (groups[i] == g->gr_gid) {
310 * set_noauth_addr - set address(es) that can be used without authentication.
311 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
314 set_noauth_addr(argv)
318 int l = strlen(addr);
321 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l + 1);
323 novm("allow-ip argument");
324 wp->word = (char *) (wp + 1);
325 wp->next = noauth_addrs;
326 BCOPY(addr, wp->word, l);
333 * An Open on LCP has requested a change from Dead to Establish phase.
334 * Do what's necessary to bring the physical layer up.
343 * LCP has terminated the link; go to the Dead phase and take the
344 * physical layer down.
347 link_terminated(unit)
350 if (phase == PHASE_DEAD)
354 new_phase(PHASE_DEAD);
355 notice("Connection terminated.");
359 * LCP has gone down; it will either die or try to re-establish.
366 struct protent *protp;
369 if (auth_script_state == s_up && auth_script_pid == 0) {
370 update_link_stats(unit);
371 auth_script_state = s_down;
372 auth_script(_PATH_AUTHDOWN);
374 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
375 if (!protp->enabled_flag)
377 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
378 (*protp->lowerdown)(unit);
379 if (protp->protocol < 0xC000 && protp->close != NULL)
380 (*protp->close)(unit, "LCP down");
384 if (phase != PHASE_DEAD)
385 new_phase(PHASE_TERMINATE);
389 * The link is established.
390 * Proceed to the Dead, Authenticate or Network phase as appropriate.
393 link_established(unit)
397 lcp_options *wo = &lcp_wantoptions[unit];
398 lcp_options *go = &lcp_gotoptions[unit];
399 lcp_options *ho = &lcp_hisoptions[unit];
401 struct protent *protp;
404 * Tell higher-level protocols that LCP is up.
406 for (i = 0; (protp = protocols[i]) != NULL; ++i)
407 if (protp->protocol != PPP_LCP && protp->enabled_flag
408 && protp->lowerup != NULL)
409 (*protp->lowerup)(unit);
411 if (auth_required && !(go->neg_chap || go->neg_upap)) {
413 * We wanted the peer to authenticate itself, and it refused:
414 * if we have some address(es) it can use without auth, fine,
415 * otherwise treat it as though it authenticated with PAP using
416 * a username * of "" and a password of "". If that's not OK,
419 if (noauth_addrs != NULL) {
420 set_allowed_addrs(unit, noauth_addrs, NULL);
421 } else if (!wo->neg_upap || !null_login(unit)) {
422 warn("peer refused to authenticate: terminating link");
423 lcp_close(unit, "peer refused to authenticate");
424 status = EXIT_PEER_AUTH_FAILED;
429 new_phase(PHASE_AUTHENTICATE);
432 ChapAuthPeer(unit, our_name, go->chap_mdtype);
434 } else if (go->neg_upap) {
439 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
440 auth |= CHAP_WITHPEER;
441 } else if (ho->neg_upap) {
442 if (passwd[0] == 0) {
443 passwd_from_file = 1;
444 if (!get_pap_passwd(passwd))
445 error("No secret found for PAP login");
447 upap_authwithpeer(unit, user, passwd);
448 auth |= PAP_WITHPEER;
450 auth_pending[unit] = auth;
457 * Proceed to the network phase.
463 lcp_options *go = &lcp_gotoptions[unit];
466 * If the peer had to authenticate, run the auth-up script now.
468 if (go->neg_chap || go->neg_upap) {
470 if (auth_script_state == s_down && auth_script_pid == 0) {
471 auth_script_state = s_up;
472 auth_script(_PATH_AUTHUP);
478 * If we negotiated callback, do it now.
481 new_phase(PHASE_CALLBACK);
482 (*cbcp_protent.open)(unit);
488 * Process extra options from the secrets file
491 options_from_list(extra_options, 1);
492 free_wordlist(extra_options);
502 struct protent *protp;
504 new_phase(PHASE_NETWORK);
507 set_filters(&pass_filter, &active_filter);
509 for (i = 0; (protp = protocols[i]) != NULL; ++i)
510 if (protp->protocol < 0xC000 && protp->enabled_flag
511 && protp->open != NULL) {
513 if (protp->protocol != PPP_CCP)
517 if (num_np_open == 0)
519 lcp_close(0, "No network protocols running");
523 * The peer has failed to authenticate himself using `protocol'.
526 auth_peer_fail(unit, protocol)
530 * Authentication failure: take the link down
532 lcp_close(unit, "Authentication failed");
533 status = EXIT_PEER_AUTH_FAILED;
537 * The peer has been successfully authenticated using `protocol'.
540 auth_peer_success(unit, protocol, name, namelen)
555 warn("auth_peer_success: unknown protocol %x", protocol);
560 * Save the authenticated name of the peer for later.
562 if (namelen > sizeof(peer_authname) - 1)
563 namelen = sizeof(peer_authname) - 1;
564 BCOPY(name, peer_authname, namelen);
565 peer_authname[namelen] = 0;
566 script_setenv("PEERNAME", peer_authname);
569 * If there is no more authentication still to be done,
570 * proceed to the network (or callback) phase.
572 if ((auth_pending[unit] &= ~bit) == 0)
577 * We have failed to authenticate ourselves to the peer using `protocol'.
580 auth_withpeer_fail(unit, protocol)
583 if (passwd_from_file)
584 BZERO(passwd, MAXSECRETLEN);
586 * We've failed to authenticate ourselves to our peer.
587 * Some servers keep sending CHAP challenges, but there
588 * is no point in persisting without any way to get updated
589 * authentication secrets.
591 lcp_close(unit, "Failed to authenticate ourselves to peer");
592 status = EXIT_AUTH_TOPEER_FAILED;
596 * We have successfully authenticated ourselves with the peer using `protocol'.
599 auth_withpeer_success(unit, protocol)
609 if (passwd_from_file)
610 BZERO(passwd, MAXSECRETLEN);
614 warn("auth_withpeer_success: unknown protocol %x", protocol);
619 * If there is no more authentication still being done,
620 * proceed to the network (or callback) phase.
622 if ((auth_pending[unit] &= ~bit) == 0)
628 * np_up - a network protocol has come up.
636 if (num_np_up == 0) {
638 * At this point we consider that the link has come up successfully.
642 new_phase(PHASE_RUNNING);
644 if (idle_time_hook != 0)
645 tlim = (*idle_time_hook)(NULL);
647 tlim = idle_time_limit;
649 TIMEOUT(check_idle, NULL, tlim);
652 * Set a timeout to close the connection once the maximum
653 * connect time has expired.
656 TIMEOUT(connect_time_expired, 0, maxconnect);
659 * Detach now, if the updetach option was given.
661 if (updetach && !nodetach)
668 * np_down - a network protocol has gone down.
674 if (--num_np_up == 0) {
675 UNTIMEOUT(check_idle, NULL);
676 new_phase(PHASE_NETWORK);
681 * np_finished - a network protocol has finished using the link.
684 np_finished(unit, proto)
687 if (--num_np_open <= 0) {
688 /* no further use for the link: shut up shop. */
689 lcp_close(0, "No network protocols running");
694 * check_idle - check whether the link has been idle for long
695 * enough that we can shut it down.
701 struct ppp_idle idle;
705 if (!get_idle_time(0, &idle))
707 if (idle_time_hook != 0) {
708 tlim = idle_time_hook(&idle);
710 itime = MIN(idle.xmit_idle, idle.recv_idle);
711 tlim = idle_time_limit - itime;
714 /* link is idle: shut it down. */
715 notice("Terminating connection due to lack of activity.");
716 lcp_close(0, "Link inactive");
718 status = EXIT_IDLE_TIMEOUT;
720 TIMEOUT(check_idle, NULL, tlim);
725 * connect_time_expired - log a message and close the connection.
728 connect_time_expired(arg)
731 info("Connect time expired");
732 lcp_close(0, "Connect time expired"); /* Close connection */
733 status = EXIT_CONNECT_TIME;
737 * auth_check_options - called to check authentication options.
742 lcp_options *wo = &lcp_wantoptions[0];
746 /* Default our_name to hostname, and user to our_name */
747 if (our_name[0] == 0 || usehostname)
748 strlcpy(our_name, hostname, sizeof(our_name));
750 strlcpy(user, our_name, sizeof(user));
753 * If we have a default route, require the peer to authenticate
754 * unless the noauth option was given or the real user is root.
756 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
761 /* If authentication is required, ask peer for CHAP or PAP. */
763 if (!wo->neg_chap && !wo->neg_upap) {
773 * Check whether we have appropriate secrets to use
774 * to authenticate the peer.
777 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
778 if (!can_auth && wo->neg_chap) {
779 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
780 our_name, 1, &lacks_ip);
783 if (auth_required && !can_auth && noauth_addrs == NULL) {
786 "By default the remote system is required to authenticate itself");
788 "(because this system has a default route to the internet)");
789 } else if (explicit_remote)
791 "The remote system (%s) is required to authenticate itself",
795 "The remote system is required to authenticate itself");
797 "but I couldn't find any suitable secret (password) for it to use to do so.");
800 "(None of the available passwords would let it use an IP address.)");
807 * auth_reset - called when LCP is starting negotiations to recheck
808 * authentication options, i.e. whether we have appropriate secrets
809 * to use for authenticating ourselves and/or the peer.
815 lcp_options *go = &lcp_gotoptions[unit];
816 lcp_options *ao = &lcp_allowoptions[0];
818 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
819 ao->neg_chap = !refuse_chap
821 || have_chap_secret(user, (explicit_remote? remote_name: NULL),
824 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
827 if (!have_chap_secret((explicit_remote? remote_name: NULL),
835 * check_passwd - Check the user name and passwd against the PAP secrets
836 * file. If requested, also check against the system password database,
837 * and login the user if OK.
840 * UPAP_AUTHNAK: Authentication failed.
841 * UPAP_AUTHACK: Authentication succeeded.
842 * In either case, msg points to an appropriate message.
845 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
856 struct wordlist *addrs = NULL, *opts = NULL;
857 char passwd[256], user[256];
858 char secret[MAXWORDLEN];
859 static int attempts = 0;
862 * Make copies of apasswd and auser, then null-terminate them.
863 * If there are unprintable characters in the password, make
866 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
867 slprintf(user, sizeof(user), "%.*v", userlen, auser);
871 * Check if a plugin wants to handle this.
874 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
877 set_allowed_addrs(unit, addrs, opts);
878 BZERO(passwd, sizeof(passwd));
880 free_wordlist(addrs);
881 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
886 * Open the file of pap secrets and scan for a suitable secret
887 * for authenticating this user.
889 filename = _PATH_UPAPFILE;
892 f = fopen(filename, "r");
894 error("Can't open PAP password file %s: %m", filename);
897 check_access(f, filename);
898 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
899 warn("no PAP secret found for %s", user);
900 } else if (secret[0] != 0) {
901 /* password given in pap-secrets - must match */
902 if ((!cryptpap && strcmp(passwd, secret) == 0)
903 || strcmp(crypt(passwd, secret), secret) == 0)
906 warn("PAP authentication failure for %s", user);
907 } else if (uselogin) {
908 /* empty password in pap-secrets and login option */
909 ret = plogin(user, passwd, msg);
910 if (ret == UPAP_AUTHNAK)
911 warn("PAP login failure for %s", user);
913 /* empty password in pap-secrets and login option not used */
919 if (ret == UPAP_AUTHNAK) {
921 *msg = "Login incorrect";
923 * XXX can we ever get here more than once??
924 * Frustrate passwd stealer programs.
925 * Allow 10 tries, but start backing off after 3 (stolen from login).
926 * On 10'th, drop the connection.
928 if (attempts++ >= 10) {
929 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
930 lcp_close(unit, "login failed");
933 sleep((u_int) (attempts - 3) * 5);
938 attempts = 0; /* Reset count */
941 set_allowed_addrs(unit, addrs, opts);
945 free_wordlist(addrs);
946 BZERO(passwd, sizeof(passwd));
947 BZERO(secret, sizeof(secret));
953 * This function is needed for PAM.
957 /* Static variables used to communicate between the conversation function
958 * and the server_login function
960 static char *PAM_username;
961 static char *PAM_password;
962 static int PAM_error = 0;
963 static pam_handle_t *pamh = NULL;
965 /* PAM conversation function
966 * Here we assume (for now, at least) that echo on means login name, and
967 * echo off means password.
970 static int PAM_conv (int num_msg, const struct pam_message **msg,
971 struct pam_response **resp, void *appdata_ptr)
974 struct pam_response *reply = NULL;
976 #define COPY_STRING(s) (s) ? strdup(s) : NULL
978 reply = malloc(sizeof(struct pam_response) * num_msg);
979 if (!reply) return PAM_CONV_ERR;
981 for (replies = 0; replies < num_msg; replies++) {
982 switch (msg[replies]->msg_style) {
983 case PAM_PROMPT_ECHO_ON:
984 reply[replies].resp_retcode = PAM_SUCCESS;
985 reply[replies].resp = COPY_STRING(PAM_username);
988 case PAM_PROMPT_ECHO_OFF:
989 reply[replies].resp_retcode = PAM_SUCCESS;
990 reply[replies].resp = COPY_STRING(PAM_password);
996 /* ignore it, but pam still wants a NULL response... */
997 reply[replies].resp_retcode = PAM_SUCCESS;
998 reply[replies].resp = NULL;
1001 /* Must be an error of some sort... */
1004 return PAM_CONV_ERR;
1011 static struct pam_conv PAM_conversation = {
1015 #endif /* USE_PAM */
1018 * plogin - Check the user name and password against the system
1019 * password database, and login the user if OK.
1022 * UPAP_AUTHNAK: Login failed.
1023 * UPAP_AUTHACK: Login succeeded.
1024 * In either case, msg points to an appropriate message.
1028 plogin(user, passwd, msg)
1038 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1039 if (pam_error != PAM_SUCCESS) {
1040 *msg = (char *) pam_strerror (pamh, pam_error);
1042 return UPAP_AUTHNAK;
1045 * Define the fields for the credential validation
1048 PAM_username = user;
1049 PAM_password = passwd;
1051 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1056 pam_error = pam_authenticate (pamh, PAM_SILENT);
1057 if (pam_error == PAM_SUCCESS && !PAM_error) {
1058 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1059 if (pam_error == PAM_SUCCESS)
1060 pam_open_session (pamh, PAM_SILENT);
1063 *msg = (char *) pam_strerror (pamh, pam_error);
1068 reopen_log(); /* apparently the PAM stuff does closelog() */
1069 PAM_username = NULL;
1070 PAM_password = NULL;
1071 if (pam_error != PAM_SUCCESS)
1072 return UPAP_AUTHNAK;
1073 #else /* #ifdef USE_PAM */
1076 * Use the non-PAM methods directly
1081 struct spwd *getspnam();
1083 struct passwd *pw = getpwnam(user);
1087 return (UPAP_AUTHNAK);
1090 spwd = getspnam(user);
1093 /* check the age of the password entry */
1094 long now = time(NULL) / 86400L;
1096 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1097 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1098 && spwd->sp_lstchg >= 0
1099 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1100 warn("Password for %s has expired", user);
1101 return (UPAP_AUTHNAK);
1103 pw->pw_passwd = spwd->sp_pwdp;
1108 * If no passwd, don't let them login.
1110 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1111 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1112 return (UPAP_AUTHNAK);
1114 #endif /* #ifdef USE_PAM */
1117 * Write a wtmp entry for this user.
1121 if (strncmp(tty, "/dev/", 5) == 0)
1123 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1125 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1126 if (pw != (struct passwd *)NULL) {
1130 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1131 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1132 memset((void *)&ll, 0, sizeof(ll));
1133 (void)time(&ll.ll_time);
1134 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1135 (void)write(fd, (char *)&ll, sizeof(ll));
1139 #endif /* _PATH_LASTLOG and not USE_PAM */
1141 info("user %s logged in", user);
1144 return (UPAP_AUTHACK);
1148 * plogout - Logout the user.
1157 pam_error = pam_close_session (pamh, PAM_SILENT);
1158 pam_end (pamh, pam_error);
1161 /* Apparently the pam stuff does closelog(). */
1163 #else /* ! USE_PAM */
1167 if (strncmp(tty, "/dev/", 5) == 0)
1169 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1170 #endif /* ! USE_PAM */
1176 * null_login - Check if a username of "" and a password of "" are
1177 * acceptable, and iff so, set the list of acceptable IP addresses
1187 struct wordlist *addrs, *opts;
1188 char secret[MAXWORDLEN];
1191 * Open the file of pap secrets and scan for a suitable secret.
1193 filename = _PATH_UPAPFILE;
1195 f = fopen(filename, "r");
1198 check_access(f, filename);
1200 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename);
1201 ret = i >= 0 && secret[0] == 0;
1202 BZERO(secret, sizeof(secret));
1205 set_allowed_addrs(unit, addrs, opts);
1207 free_wordlist(opts);
1209 free_wordlist(addrs);
1217 * get_pap_passwd - get a password for authenticating ourselves with
1218 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1220 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1223 get_pap_passwd(passwd)
1229 char secret[MAXWORDLEN];
1232 * Check whether a plugin wants to supply this.
1234 if (pap_passwd_hook) {
1235 ret = (*pap_passwd_hook)(user, passwd);
1240 filename = _PATH_UPAPFILE;
1241 f = fopen(filename, "r");
1244 check_access(f, filename);
1245 ret = scan_authfile(f, user,
1246 (remote_name[0]? remote_name: NULL),
1247 secret, NULL, NULL, filename);
1252 strlcpy(passwd, secret, MAXSECRETLEN);
1253 BZERO(secret, sizeof(secret));
1259 * have_pap_secret - check whether we have a PAP file with any
1260 * secrets that we could possibly use for authenticating the peer.
1263 have_pap_secret(lacks_ipp)
1269 struct wordlist *addrs;
1271 /* let the plugin decide, if there is one */
1272 if (pap_check_hook) {
1273 ret = (*pap_check_hook)();
1278 filename = _PATH_UPAPFILE;
1279 f = fopen(filename, "r");
1283 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1284 NULL, &addrs, NULL, filename);
1286 if (ret >= 0 && !some_ip_ok(addrs)) {
1292 free_wordlist(addrs);
1299 * have_chap_secret - check whether we have a CHAP file with a
1300 * secret that we could possibly use for authenticating `client'
1301 * on `server'. Either can be the null string, meaning we don't
1302 * know the identity yet.
1305 have_chap_secret(client, server, need_ip, lacks_ipp)
1314 struct wordlist *addrs;
1316 filename = _PATH_CHAPFILE;
1317 f = fopen(filename, "r");
1321 if (client != NULL && client[0] == 0)
1323 else if (server != NULL && server[0] == 0)
1326 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename);
1328 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1334 free_wordlist(addrs);
1341 * get_secret - open the CHAP secret file and return the secret
1342 * for authenticating the given client on the given server.
1343 * (We could be either client or server).
1346 get_secret(unit, client, server, secret, secret_len, am_server)
1357 struct wordlist *addrs, *opts;
1358 char secbuf[MAXWORDLEN];
1360 if (!am_server && passwd[0] != 0) {
1361 strlcpy(secbuf, passwd, sizeof(secbuf));
1363 filename = _PATH_CHAPFILE;
1367 f = fopen(filename, "r");
1369 error("Can't open chap secret file %s: %m", filename);
1372 check_access(f, filename);
1374 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename);
1380 set_allowed_addrs(unit, addrs, opts);
1382 free_wordlist(opts);
1384 free_wordlist(addrs);
1387 len = strlen(secbuf);
1388 if (len > MAXSECRETLEN) {
1389 error("Secret for %s on %s is too long", client, server);
1392 BCOPY(secbuf, secret, len);
1393 BZERO(secbuf, sizeof(secbuf));
1400 * set_allowed_addrs() - set the list of allowed addresses.
1401 * Also looks for `--' indicating options to apply for this peer
1402 * and leaves the following words in extra_options.
1405 set_allowed_addrs(unit, addrs, opts)
1407 struct wordlist *addrs;
1408 struct wordlist *opts;
1411 struct wordlist *ap, **pap;
1412 struct permitted_ip *ip;
1413 char *ptr_word, *ptr_mask;
1416 u_int32_t a, mask, ah, offset;
1417 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1418 u_int32_t suggested_ip = 0;
1420 if (addresses[unit] != NULL)
1421 free(addresses[unit]);
1422 addresses[unit] = NULL;
1423 if (extra_options != NULL)
1424 free_wordlist(extra_options);
1425 extra_options = opts;
1428 * Count the number of IP addresses given.
1430 for (n = 0, pap = &addrs; (ap = *pap) != NULL; pap = &ap->next)
1434 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1439 for (ap = addrs; ap != NULL; ap = ap->next) {
1440 /* "-" means no addresses authorized, "*" means any address allowed */
1441 ptr_word = ap->word;
1442 if (strcmp(ptr_word, "-") == 0)
1444 if (strcmp(ptr_word, "*") == 0) {
1446 ip[n].base = ip[n].mask = 0;
1452 if (*ptr_word == '!') {
1457 mask = ~ (u_int32_t) 0;
1459 ptr_mask = strchr (ptr_word, '/');
1460 if (ptr_mask != NULL) {
1464 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1465 if (bit_count <= 0 || bit_count > 32) {
1466 warn("invalid address length %v in auth. address list",
1470 bit_count = 32 - bit_count; /* # bits in host part */
1472 offset = ifunit + 1;
1476 warn("invalid address length syntax: %v", ptr_mask+1);
1483 hp = gethostbyname(ptr_word);
1484 if (hp != NULL && hp->h_addrtype == AF_INET) {
1485 a = *(u_int32_t *)hp->h_addr;
1487 np = getnetbyname (ptr_word);
1488 if (np != NULL && np->n_addrtype == AF_INET) {
1489 a = htonl (*(u_int32_t *)np->n_net);
1490 if (ptr_mask == NULL) {
1491 /* calculate appropriate mask for net */
1494 mask = IN_CLASSA_NET;
1495 else if (IN_CLASSB(ah))
1496 mask = IN_CLASSB_NET;
1497 else if (IN_CLASSC(ah))
1498 mask = IN_CLASSC_NET;
1501 a = inet_addr (ptr_word);
1505 if (ptr_mask != NULL)
1508 if (a == (u_int32_t)-1L) {
1509 warn("unknown host %s in auth. address list", ap->word);
1513 if (offset >= ~mask) {
1514 warn("interface unit %d too large for subnet %v",
1518 a = htonl((ntohl(a) & mask) + offset);
1519 mask = ~(u_int32_t)0;
1521 ip[n].mask = htonl(mask);
1522 ip[n].base = a & ip[n].mask;
1524 if (~mask == 0 && suggested_ip == 0)
1528 ip[n].permit = 0; /* make the last entry forbid all addresses */
1529 ip[n].base = 0; /* to terminate the list */
1532 addresses[unit] = ip;
1535 * If the address given for the peer isn't authorized, or if
1536 * the user hasn't given one, AND there is an authorized address
1537 * which is a single host, then use that if we find one.
1539 if (suggested_ip != 0
1540 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
1541 wo->hisaddr = suggested_ip;
1545 * auth_ip_addr - check whether the peer is authorized to use
1546 * a given IP address. Returns 1 if authorized, 0 otherwise.
1549 auth_ip_addr(unit, addr)
1555 /* don't allow loopback or multicast address */
1556 if (bad_ip_adrs(addr))
1559 if (addresses[unit] != NULL) {
1560 ok = ip_addr_check(addr, addresses[unit]);
1565 return 0; /* no addresses authorized */
1566 return allow_any_ip || !have_route_to(addr);
1570 ip_addr_check(addr, addrs)
1572 struct permitted_ip *addrs;
1575 if ((addr & addrs->mask) == addrs->base)
1576 return addrs->permit;
1580 * bad_ip_adrs - return 1 if the IP address is one we don't want
1581 * to use, such as an address in the loopback net or a multicast address.
1582 * addr is in network byte order.
1589 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1590 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1594 * some_ip_ok - check a wordlist to see if it authorizes any
1599 struct wordlist *addrs;
1601 for (; addrs != 0; addrs = addrs->next) {
1602 if (addrs->word[0] == '-')
1604 if (addrs->word[0] != '!')
1605 return 1; /* some IP address is allowed */
1611 * check_access - complain if a secret file has too-liberal permissions.
1614 check_access(f, filename)
1620 if (fstat(fileno(f), &sbuf) < 0) {
1621 warn("cannot stat secret file %s: %m", filename);
1622 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1623 warn("Warning - secret file %s has world and/or group access",
1630 * scan_authfile - Scan an authorization file for a secret suitable
1631 * for authenticating `client' on `server'. The return value is -1
1632 * if no secret is found, otherwise >= 0. The return value has
1633 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1634 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1635 * Any following words on the line up to a "--" (i.e. address authorization
1636 * info) are placed in a wordlist and returned in *addrs. Any
1637 * following words (extra options) are placed in a wordlist and
1638 * returned in *opts.
1639 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1642 scan_authfile(f, client, server, secret, addrs, opts, filename)
1647 struct wordlist **addrs;
1648 struct wordlist **opts;
1652 int got_flag, best_flag;
1654 struct wordlist *ap, *addr_list, *alist, **app;
1655 char word[MAXWORDLEN];
1656 char atfile[MAXWORDLEN];
1657 char lsecret[MAXWORDLEN];
1664 if (!getword(f, word, &newline, filename))
1665 return -1; /* file is empty??? */
1670 * Skip until we find a word at the start of a line.
1672 while (!newline && getword(f, word, &newline, filename))
1675 break; /* got to end of file */
1678 * Got a client - check if it's a match or a wildcard.
1681 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1686 got_flag = NONWILD_CLIENT;
1689 * Now get a server and check if it matches.
1691 if (!getword(f, word, &newline, filename))
1695 if (!ISWILD(word)) {
1696 if (server != NULL && strcmp(word, server) != 0)
1698 got_flag |= NONWILD_SERVER;
1702 * Got some sort of a match - see if it's better than what
1705 if (got_flag <= best_flag)
1711 if (!getword(f, word, &newline, filename))
1717 * Special syntax: @filename means read secret from file.
1719 if (word[0] == '@') {
1720 strlcpy(atfile, word+1, sizeof(atfile));
1721 if ((sf = fopen(atfile, "r")) == NULL) {
1722 warn("can't open indirect secret file %s", atfile);
1725 check_access(sf, atfile);
1726 if (!getword(sf, word, &xxx, atfile)) {
1727 warn("no secret in indirect secret file %s", atfile);
1734 strlcpy(lsecret, word, sizeof(lsecret));
1737 * Now read address authorization info and make a wordlist.
1741 if (!getword(f, word, &newline, filename) || newline)
1743 ap = (struct wordlist *) malloc(sizeof(struct wordlist));
1745 novm("authorized addresses");
1746 ap->word = strdup(word);
1747 if (ap->word == NULL)
1748 novm("authorized addresses");
1755 * This is the best so far; remember it.
1757 best_flag = got_flag;
1759 free_wordlist(addr_list);
1762 strlcpy(secret, lsecret, MAXWORDLEN);
1768 /* scan for a -- word indicating the start of options */
1769 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
1770 if (strcmp(ap->word, "--") == 0)
1772 /* ap = start of options */
1774 ap = ap->next; /* first option */
1775 free(*app); /* free the "--" word */
1776 *app = NULL; /* terminate addr list */
1780 else if (ap != NULL)
1784 else if (addr_list != NULL)
1785 free_wordlist(addr_list);
1791 * free_wordlist - release memory allocated for a wordlist.
1795 struct wordlist *wp;
1797 struct wordlist *next;
1799 while (wp != NULL) {
1807 * auth_script_done - called when the auth-up or auth-down script
1811 auth_script_done(arg)
1814 auth_script_pid = 0;
1815 switch (auth_script_state) {
1817 if (auth_state == s_down) {
1818 auth_script_state = s_down;
1819 auth_script(_PATH_AUTHDOWN);
1823 if (auth_state == s_up) {
1824 auth_script_state = s_up;
1825 auth_script(_PATH_AUTHUP);
1832 * auth_script - execute a script with arguments
1833 * interface-name peer-name real-user tty speed
1845 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1846 user_name = pw->pw_name;
1848 slprintf(struid, sizeof(struid), "%d", getuid());
1851 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1855 argv[2] = peer_authname;
1856 argv[3] = user_name;
1861 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);