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.58 1999/09/11 12:08:56 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 /* Hook to enable a plugin to control the idle time limit */
116 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
118 /* Hook for a plugin to say whether we can possibly authenticate any peer */
119 int (*pap_check_hook) __P((void)) = NULL;
121 /* Hook for a plugin to check the PAP user and password */
122 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
123 struct wordlist **paddrs,
124 struct wordlist **popts)) = NULL;
126 /* Hook for a plugin to get the PAP password for authenticating us */
127 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
130 * This is used to ensure that we don't start an auth-up/down
131 * script while one is already running.
138 static enum script_state auth_state = s_down;
139 static enum script_state auth_script_state = s_down;
140 static pid_t auth_script_pid = 0;
145 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
146 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
147 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
148 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
149 bool usehostname = 0; /* Use hostname for our_name */
150 bool auth_required = 0; /* Always require authentication from peer */
151 bool allow_any_ip = 0; /* Allow peer to use any IP address */
152 bool explicit_remote = 0; /* User specified explicit remote name */
153 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
155 /* Bits in auth_pending[] */
156 #define PAP_WITHPEER 1
158 #define CHAP_WITHPEER 4
161 extern char *crypt __P((const char *, const char *));
163 /* Prototypes for procedures local to this file. */
165 static void network_phase __P((int));
166 static void check_idle __P((void *));
167 static void connect_time_expired __P((void *));
168 static int plogin __P((char *, char *, char **));
169 static void plogout __P((void));
170 static int null_login __P((int));
171 static int get_pap_passwd __P((char *));
172 static int have_pap_secret __P((int *));
173 static int have_chap_secret __P((char *, char *, int, int *));
174 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
175 static int scan_authfile __P((FILE *, char *, char *, char *,
176 struct wordlist **, struct wordlist **,
178 static void free_wordlist __P((struct wordlist *));
179 static void auth_script __P((char *));
180 static void auth_script_done __P((void *));
181 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
182 static int some_ip_ok __P((struct wordlist *));
183 static int setupapfile __P((char **));
184 static int privgroup __P((char **));
185 static int set_noauth_addr __P((char **));
186 static void check_access __P((FILE *, char *));
189 * Authentication-related options.
191 option_t auth_options[] = {
192 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
193 "Require PAP authentication from peer", 1, &auth_required },
194 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
195 "Require PAP authentication from peer", 1, &auth_required },
196 { "refuse-pap", o_bool, &refuse_pap,
197 "Don't agree to auth to peer with PAP", 1 },
198 { "-pap", o_bool, &refuse_pap,
199 "Don't allow PAP authentication with peer", 1 },
200 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
201 "Require CHAP authentication from peer", 1, &auth_required },
202 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
203 "Require CHAP authentication from peer", 1, &auth_required },
204 { "refuse-chap", o_bool, &refuse_chap,
205 "Don't agree to auth to peer with CHAP", 1 },
206 { "-chap", o_bool, &refuse_chap,
207 "Don't allow CHAP authentication with peer", 1 },
208 { "name", o_string, our_name,
209 "Set local name for authentication",
210 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
211 { "user", o_string, user,
212 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
213 { "usehostname", o_bool, &usehostname,
214 "Must use hostname for authentication", 1 },
215 { "remotename", o_string, remote_name,
216 "Set remote name for authentication", OPT_STATIC,
217 &explicit_remote, MAXNAMELEN },
218 { "auth", o_bool, &auth_required,
219 "Require authentication from peer", 1 },
220 { "noauth", o_bool, &auth_required,
221 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
222 { "login", o_bool, &uselogin,
223 "Use system password database for PAP", 1 },
224 { "papcrypt", o_bool, &cryptpap,
225 "PAP passwords are encrypted", 1 },
226 { "+ua", o_special, setupapfile,
227 "Get PAP user and password from file" },
228 { "password", o_string, passwd,
229 "Password for authenticating us to the peer", OPT_STATIC,
230 NULL, MAXSECRETLEN },
231 { "privgroup", o_special, privgroup,
232 "Allow group members to use privileged options", OPT_PRIV },
233 { "allow-ip", o_special, set_noauth_addr,
234 "Set IP address(es) which can be used without authentication",
240 * setupapfile - specifies UPAP info for authenticating with peer.
249 lcp_allowoptions[0].neg_upap = 1;
251 /* open user info file */
253 ufile = fopen(*argv, "r");
256 option_error("unable to open user login data file %s", *argv);
259 check_access(ufile, *argv);
262 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
263 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
264 option_error("unable to read user login data file %s", *argv);
269 /* get rid of newlines */
271 if (l > 0 && user[l-1] == '\n')
274 if (l > 0 && passwd[l-1] == '\n')
282 * privgroup - allow members of the group to have privileged access.
293 option_error("group %s is unknown", *argv);
296 for (i = 0; i < ngroups; ++i) {
297 if (groups[i] == g->gr_gid) {
307 * set_noauth_addr - set address(es) that can be used without authentication.
308 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
311 set_noauth_addr(argv)
315 int l = strlen(addr);
318 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l + 1);
320 novm("allow-ip argument");
321 wp->word = (char *) (wp + 1);
322 wp->next = noauth_addrs;
323 BCOPY(addr, wp->word, l);
330 * An Open on LCP has requested a change from Dead to Establish phase.
331 * Do what's necessary to bring the physical layer up.
340 * LCP has terminated the link; go to the Dead phase and take the
341 * physical layer down.
344 link_terminated(unit)
347 if (phase == PHASE_DEAD)
351 new_phase(PHASE_DEAD);
352 notice("Connection terminated.");
356 * LCP has gone down; it will either die or try to re-establish.
363 struct protent *protp;
366 if (auth_script_state == s_up && auth_script_pid == 0) {
367 update_link_stats(unit);
368 auth_script_state = s_down;
369 auth_script(_PATH_AUTHDOWN);
371 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
372 if (!protp->enabled_flag)
374 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
375 (*protp->lowerdown)(unit);
376 if (protp->protocol < 0xC000 && protp->close != NULL)
377 (*protp->close)(unit, "LCP down");
381 if (phase != PHASE_DEAD)
382 new_phase(PHASE_TERMINATE);
386 * The link is established.
387 * Proceed to the Dead, Authenticate or Network phase as appropriate.
390 link_established(unit)
394 lcp_options *wo = &lcp_wantoptions[unit];
395 lcp_options *go = &lcp_gotoptions[unit];
396 lcp_options *ho = &lcp_hisoptions[unit];
398 struct protent *protp;
401 * Tell higher-level protocols that LCP is up.
403 for (i = 0; (protp = protocols[i]) != NULL; ++i)
404 if (protp->protocol != PPP_LCP && protp->enabled_flag
405 && protp->lowerup != NULL)
406 (*protp->lowerup)(unit);
408 if (auth_required && !(go->neg_chap || go->neg_upap)) {
410 * We wanted the peer to authenticate itself, and it refused:
411 * if we have some address(es) it can use without auth, fine,
412 * otherwise treat it as though it authenticated with PAP using
413 * a username * of "" and a password of "". If that's not OK,
416 if (noauth_addrs != NULL) {
417 set_allowed_addrs(unit, noauth_addrs, NULL);
418 } else if (!wo->neg_upap || !null_login(unit)) {
419 warn("peer refused to authenticate: terminating link");
420 lcp_close(unit, "peer refused to authenticate");
421 status = EXIT_PEER_AUTH_FAILED;
426 new_phase(PHASE_AUTHENTICATE);
429 ChapAuthPeer(unit, our_name, go->chap_mdtype);
431 } else if (go->neg_upap) {
436 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
437 auth |= CHAP_WITHPEER;
438 } else if (ho->neg_upap) {
439 if (passwd[0] == 0) {
440 passwd_from_file = 1;
441 if (!get_pap_passwd(passwd))
442 error("No secret found for PAP login");
444 upap_authwithpeer(unit, user, passwd);
445 auth |= PAP_WITHPEER;
447 auth_pending[unit] = auth;
454 * Proceed to the network phase.
460 lcp_options *go = &lcp_gotoptions[unit];
463 * If the peer had to authenticate, run the auth-up script now.
465 if (go->neg_chap || go->neg_upap) {
467 if (auth_script_state == s_down && auth_script_pid == 0) {
468 auth_script_state = s_up;
469 auth_script(_PATH_AUTHUP);
475 * If we negotiated callback, do it now.
478 new_phase(PHASE_CALLBACK);
479 (*cbcp_protent.open)(unit);
485 * Process extra options from the secrets file
488 options_from_list(extra_options, 1);
489 free_wordlist(extra_options);
499 struct protent *protp;
501 new_phase(PHASE_NETWORK);
504 set_filters(&pass_filter, &active_filter);
506 for (i = 0; (protp = protocols[i]) != NULL; ++i)
507 if (protp->protocol < 0xC000 && protp->enabled_flag
508 && protp->open != NULL) {
510 if (protp->protocol != PPP_CCP)
514 if (num_np_open == 0)
516 lcp_close(0, "No network protocols running");
520 * The peer has failed to authenticate himself using `protocol'.
523 auth_peer_fail(unit, protocol)
527 * Authentication failure: take the link down
529 lcp_close(unit, "Authentication failed");
530 status = EXIT_PEER_AUTH_FAILED;
534 * The peer has been successfully authenticated using `protocol'.
537 auth_peer_success(unit, protocol, name, namelen)
552 warn("auth_peer_success: unknown protocol %x", protocol);
557 * Save the authenticated name of the peer for later.
559 if (namelen > sizeof(peer_authname) - 1)
560 namelen = sizeof(peer_authname) - 1;
561 BCOPY(name, peer_authname, namelen);
562 peer_authname[namelen] = 0;
563 script_setenv("PEERNAME", peer_authname);
566 * If there is no more authentication still to be done,
567 * proceed to the network (or callback) phase.
569 if ((auth_pending[unit] &= ~bit) == 0)
574 * We have failed to authenticate ourselves to the peer using `protocol'.
577 auth_withpeer_fail(unit, protocol)
580 if (passwd_from_file)
581 BZERO(passwd, MAXSECRETLEN);
583 * We've failed to authenticate ourselves to our peer.
584 * Some servers keep sending CHAP challenges, but there
585 * is no point in persisting without any way to get updated
586 * authentication secrets.
588 lcp_close(unit, "Failed to authenticate ourselves to peer");
589 status = EXIT_AUTH_TOPEER_FAILED;
593 * We have successfully authenticated ourselves with the peer using `protocol'.
596 auth_withpeer_success(unit, protocol)
606 if (passwd_from_file)
607 BZERO(passwd, MAXSECRETLEN);
611 warn("auth_withpeer_success: unknown protocol %x", protocol);
616 * If there is no more authentication still being done,
617 * proceed to the network (or callback) phase.
619 if ((auth_pending[unit] &= ~bit) == 0)
625 * np_up - a network protocol has come up.
633 if (num_np_up == 0) {
635 * At this point we consider that the link has come up successfully.
639 new_phase(PHASE_RUNNING);
641 if (idle_time_hook != 0)
642 tlim = (*idle_time_hook)(NULL);
644 tlim = idle_time_limit;
646 TIMEOUT(check_idle, NULL, tlim);
649 * Set a timeout to close the connection once the maximum
650 * connect time has expired.
653 TIMEOUT(connect_time_expired, 0, maxconnect);
656 * Detach now, if the updetach option was given.
658 if (updetach && !nodetach)
665 * np_down - a network protocol has gone down.
671 if (--num_np_up == 0) {
672 UNTIMEOUT(check_idle, NULL);
673 new_phase(PHASE_NETWORK);
678 * np_finished - a network protocol has finished using the link.
681 np_finished(unit, proto)
684 if (--num_np_open <= 0) {
685 /* no further use for the link: shut up shop. */
686 lcp_close(0, "No network protocols running");
691 * check_idle - check whether the link has been idle for long
692 * enough that we can shut it down.
698 struct ppp_idle idle;
702 if (!get_idle_time(0, &idle))
704 if (idle_time_hook != 0) {
705 tlim = idle_time_hook(&idle);
707 itime = MIN(idle.xmit_idle, idle.recv_idle);
708 tlim = idle_time_limit - itime;
711 /* link is idle: shut it down. */
712 notice("Terminating connection due to lack of activity.");
713 lcp_close(0, "Link inactive");
715 status = EXIT_IDLE_TIMEOUT;
717 TIMEOUT(check_idle, NULL, tlim);
722 * connect_time_expired - log a message and close the connection.
725 connect_time_expired(arg)
728 info("Connect time expired");
729 lcp_close(0, "Connect time expired"); /* Close connection */
730 status = EXIT_CONNECT_TIME;
734 * auth_check_options - called to check authentication options.
739 lcp_options *wo = &lcp_wantoptions[0];
743 /* Default our_name to hostname, and user to our_name */
744 if (our_name[0] == 0 || usehostname)
745 strlcpy(our_name, hostname, sizeof(our_name));
747 strlcpy(user, our_name, sizeof(user));
750 * If we have a default route, require the peer to authenticate
751 * unless the noauth option was given.
753 if (!auth_required && !allow_any_ip && have_route_to(0))
756 /* If authentication is required, ask peer for CHAP or PAP. */
758 if (!wo->neg_chap && !wo->neg_upap) {
768 * Check whether we have appropriate secrets to use
769 * to authenticate the peer.
772 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
773 if (!can_auth && wo->neg_chap) {
774 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
775 our_name, 1, &lacks_ip);
778 if (auth_required && !can_auth && noauth_addrs == NULL) {
781 "The remote system (%s) is required to authenticate itself but I",
785 "The remote system is required to authenticate itself but I");
789 "couldn't find any suitable secret (password) for it to use to do so.");
792 "couldn't find any secret (password) which would let it use an IP address.");
799 * auth_reset - called when LCP is starting negotiations to recheck
800 * authentication options, i.e. whether we have appropriate secrets
801 * to use for authenticating ourselves and/or the peer.
807 lcp_options *go = &lcp_gotoptions[unit];
808 lcp_options *ao = &lcp_allowoptions[0];
810 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
811 ao->neg_chap = !refuse_chap
813 || have_chap_secret(user, (explicit_remote? remote_name: NULL),
816 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
819 if (!have_chap_secret((explicit_remote? remote_name: NULL),
827 * check_passwd - Check the user name and passwd against the PAP secrets
828 * file. If requested, also check against the system password database,
829 * and login the user if OK.
832 * UPAP_AUTHNAK: Authentication failed.
833 * UPAP_AUTHACK: Authentication succeeded.
834 * In either case, msg points to an appropriate message.
837 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
848 struct wordlist *addrs = NULL, *opts = NULL;
849 char passwd[256], user[256];
850 char secret[MAXWORDLEN];
851 static int attempts = 0;
854 * Make copies of apasswd and auser, then null-terminate them.
855 * If there are unprintable characters in the password, make
858 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
859 slprintf(user, sizeof(user), "%.*v", userlen, auser);
863 * Check if a plugin wants to handle this.
866 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
869 set_allowed_addrs(unit, addrs, opts);
870 BZERO(passwd, sizeof(passwd));
871 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
876 * Open the file of pap secrets and scan for a suitable secret
877 * for authenticating this user.
879 filename = _PATH_UPAPFILE;
882 f = fopen(filename, "r");
884 error("Can't open PAP password file %s: %m", filename);
887 check_access(f, filename);
888 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
889 warn("no PAP secret found for %s", user);
890 } else if (secret[0] != 0) {
891 /* password given in pap-secrets - must match */
892 if ((!cryptpap && strcmp(passwd, secret) == 0)
893 || strcmp(crypt(passwd, secret), secret) == 0)
896 warn("PAP authentication failure for %s", user);
897 } else if (uselogin) {
898 /* empty password in pap-secrets and login option */
899 ret = plogin(user, passwd, msg);
900 if (ret == UPAP_AUTHNAK)
901 warn("PAP login failure for %s", user);
903 /* empty password in pap-secrets and login option not used */
909 if (ret == UPAP_AUTHNAK) {
911 *msg = "Login incorrect";
913 * XXX can we ever get here more than once??
914 * Frustrate passwd stealer programs.
915 * Allow 10 tries, but start backing off after 3 (stolen from login).
916 * On 10'th, drop the connection.
918 if (attempts++ >= 10) {
919 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
920 lcp_close(unit, "login failed");
923 sleep((u_int) (attempts - 3) * 5);
925 free_wordlist(addrs);
930 attempts = 0; /* Reset count */
933 set_allowed_addrs(unit, addrs, opts);
936 BZERO(passwd, sizeof(passwd));
937 BZERO(secret, sizeof(secret));
943 * This function is needed for PAM.
947 /* Static variables used to communicate between the conversation function
948 * and the server_login function
950 static char *PAM_username;
951 static char *PAM_password;
952 static int PAM_error = 0;
953 static pam_handle_t *pamh = NULL;
955 /* PAM conversation function
956 * Here we assume (for now, at least) that echo on means login name, and
957 * echo off means password.
960 static int PAM_conv (int num_msg, const struct pam_message **msg,
961 struct pam_response **resp, void *appdata_ptr)
964 struct pam_response *reply = NULL;
966 #define COPY_STRING(s) (s) ? strdup(s) : NULL
968 reply = malloc(sizeof(struct pam_response) * num_msg);
969 if (!reply) return PAM_CONV_ERR;
971 for (replies = 0; replies < num_msg; replies++) {
972 switch (msg[replies]->msg_style) {
973 case PAM_PROMPT_ECHO_ON:
974 reply[replies].resp_retcode = PAM_SUCCESS;
975 reply[replies].resp = COPY_STRING(PAM_username);
978 case PAM_PROMPT_ECHO_OFF:
979 reply[replies].resp_retcode = PAM_SUCCESS;
980 reply[replies].resp = COPY_STRING(PAM_password);
986 /* ignore it, but pam still wants a NULL response... */
987 reply[replies].resp_retcode = PAM_SUCCESS;
988 reply[replies].resp = NULL;
991 /* Must be an error of some sort... */
1001 static struct pam_conv PAM_conversation = {
1005 #endif /* USE_PAM */
1008 * plogin - Check the user name and password against the system
1009 * password database, and login the user if OK.
1012 * UPAP_AUTHNAK: Login failed.
1013 * UPAP_AUTHACK: Login succeeded.
1014 * In either case, msg points to an appropriate message.
1018 plogin(user, passwd, msg)
1028 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1029 if (pam_error != PAM_SUCCESS) {
1030 *msg = (char *) pam_strerror (pamh, pam_error);
1032 return UPAP_AUTHNAK;
1035 * Define the fields for the credential validation
1038 PAM_username = user;
1039 PAM_password = passwd;
1041 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1046 pam_error = pam_authenticate (pamh, PAM_SILENT);
1047 if (pam_error == PAM_SUCCESS && !PAM_error) {
1048 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1049 if (pam_error == PAM_SUCCESS)
1050 pam_open_session (pamh, PAM_SILENT);
1053 *msg = (char *) pam_strerror (pamh, pam_error);
1058 reopen_log(); /* apparently the PAM stuff does closelog() */
1059 PAM_username = NULL;
1060 PAM_password = NULL;
1061 if (pam_error != PAM_SUCCESS)
1062 return UPAP_AUTHNAK;
1063 #else /* #ifdef USE_PAM */
1066 * Use the non-PAM methods directly
1071 struct spwd *getspnam();
1073 struct passwd *pw = getpwnam(user);
1077 return (UPAP_AUTHNAK);
1080 spwd = getspnam(user);
1083 /* check the age of the password entry */
1084 long now = time(NULL) / 86400L;
1086 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1087 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1088 && spwd->sp_lstchg >= 0
1089 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1090 warn("Password for %s has expired", user);
1091 return (UPAP_AUTHNAK);
1093 pw->pw_passwd = spwd->sp_pwdp;
1098 * If no passwd, don't let them login.
1100 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1101 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1102 return (UPAP_AUTHNAK);
1104 #endif /* #ifdef USE_PAM */
1107 * Write a wtmp entry for this user.
1111 if (strncmp(tty, "/dev/", 5) == 0)
1113 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1115 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1116 if (pw != (struct passwd *)NULL) {
1120 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1121 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1122 memset((void *)&ll, 0, sizeof(ll));
1123 (void)time(&ll.ll_time);
1124 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1125 (void)write(fd, (char *)&ll, sizeof(ll));
1129 #endif /* _PATH_LASTLOG and not USE_PAM */
1131 info("user %s logged in", user);
1134 return (UPAP_AUTHACK);
1138 * plogout - Logout the user.
1147 pam_error = pam_close_session (pamh, PAM_SILENT);
1148 pam_end (pamh, pam_error);
1151 /* Apparently the pam stuff does closelog(). */
1153 #else /* ! USE_PAM */
1157 if (strncmp(tty, "/dev/", 5) == 0)
1159 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1160 #endif /* ! USE_PAM */
1166 * null_login - Check if a username of "" and a password of "" are
1167 * acceptable, and iff so, set the list of acceptable IP addresses
1177 struct wordlist *addrs, *opts;
1178 char secret[MAXWORDLEN];
1181 * Open the file of pap secrets and scan for a suitable secret.
1183 filename = _PATH_UPAPFILE;
1185 f = fopen(filename, "r");
1188 check_access(f, filename);
1190 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename);
1191 ret = i >= 0 && secret[0] == 0;
1192 BZERO(secret, sizeof(secret));
1195 set_allowed_addrs(unit, addrs, opts);
1197 free_wordlist(addrs);
1198 free_wordlist(opts);
1207 * get_pap_passwd - get a password for authenticating ourselves with
1208 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1210 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1213 get_pap_passwd(passwd)
1219 struct wordlist *addrs;
1220 char secret[MAXWORDLEN];
1223 * Check whether a plugin wants to supply this.
1225 if (pap_passwd_hook) {
1226 ret = (*pap_passwd_hook)(user, passwd);
1231 filename = _PATH_UPAPFILE;
1233 f = fopen(filename, "r");
1236 check_access(f, filename);
1237 ret = scan_authfile(f, user,
1238 (remote_name[0]? remote_name: NULL),
1239 secret, NULL, NULL, filename);
1244 strlcpy(passwd, secret, MAXSECRETLEN);
1245 BZERO(secret, sizeof(secret));
1251 * have_pap_secret - check whether we have a PAP file with any
1252 * secrets that we could possibly use for authenticating the peer.
1255 have_pap_secret(lacks_ipp)
1261 struct wordlist *addrs;
1263 /* let the plugin decide, if there is one */
1264 if (pap_check_hook) {
1265 ret = (*pap_check_hook)();
1270 filename = _PATH_UPAPFILE;
1271 f = fopen(filename, "r");
1275 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1276 NULL, &addrs, NULL, filename);
1278 if (ret >= 0 && !some_ip_ok(addrs)) {
1284 free_wordlist(addrs);
1291 * have_chap_secret - check whether we have a CHAP file with a
1292 * secret that we could possibly use for authenticating `client'
1293 * on `server'. Either can be the null string, meaning we don't
1294 * know the identity yet.
1297 have_chap_secret(client, server, need_ip, lacks_ipp)
1306 struct wordlist *addrs;
1308 filename = _PATH_CHAPFILE;
1309 f = fopen(filename, "r");
1313 if (client != NULL && client[0] == 0)
1315 else if (server != NULL && server[0] == 0)
1318 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename);
1320 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1326 free_wordlist(addrs);
1333 * get_secret - open the CHAP secret file and return the secret
1334 * for authenticating the given client on the given server.
1335 * (We could be either client or server).
1338 get_secret(unit, client, server, secret, secret_len, am_server)
1349 struct wordlist *addrs, *opts;
1350 char secbuf[MAXWORDLEN];
1352 if (!am_server && passwd[0] != 0) {
1353 strlcpy(secbuf, passwd, sizeof(secbuf));
1355 filename = _PATH_CHAPFILE;
1359 f = fopen(filename, "r");
1361 error("Can't open chap secret file %s: %m", filename);
1364 check_access(f, filename);
1366 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename);
1372 set_allowed_addrs(unit, addrs, opts);
1374 free_wordlist(addrs);
1375 free_wordlist(opts);
1379 len = strlen(secbuf);
1380 if (len > MAXSECRETLEN) {
1381 error("Secret for %s on %s is too long", client, server);
1384 BCOPY(secbuf, secret, len);
1385 BZERO(secbuf, sizeof(secbuf));
1392 * set_allowed_addrs() - set the list of allowed addresses.
1393 * Also looks for `--' indicating options to apply for this peer
1394 * and leaves the following words in extra_options.
1397 set_allowed_addrs(unit, addrs, opts)
1399 struct wordlist *addrs;
1400 struct wordlist *opts;
1403 struct wordlist *ap, **pap;
1404 struct permitted_ip *ip;
1405 char *ptr_word, *ptr_mask;
1408 u_int32_t a, mask, ah, offset;
1409 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1410 u_int32_t suggested_ip = 0;
1412 if (addresses[unit] != NULL)
1413 free(addresses[unit]);
1414 addresses[unit] = NULL;
1415 if (extra_options != NULL)
1416 free_wordlist(extra_options);
1417 extra_options = opts;
1420 * Count the number of IP addresses given.
1422 for (n = 0, pap = &addrs; (ap = *pap) != NULL; pap = &ap->next)
1426 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1431 for (ap = addrs; ap != NULL; ap = ap->next) {
1432 /* "-" means no addresses authorized, "*" means any address allowed */
1433 ptr_word = ap->word;
1434 if (strcmp(ptr_word, "-") == 0)
1436 if (strcmp(ptr_word, "*") == 0) {
1438 ip[n].base = ip[n].mask = 0;
1444 if (*ptr_word == '!') {
1449 mask = ~ (u_int32_t) 0;
1451 ptr_mask = strchr (ptr_word, '/');
1452 if (ptr_mask != NULL) {
1456 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1457 if (bit_count <= 0 || bit_count > 32) {
1458 warn("invalid address length %v in auth. address list",
1462 bit_count = 32 - bit_count; /* # bits in host part */
1464 offset = ifunit + 1;
1468 warn("invalid address length syntax: %v", ptr_mask+1);
1475 hp = gethostbyname(ptr_word);
1476 if (hp != NULL && hp->h_addrtype == AF_INET) {
1477 a = *(u_int32_t *)hp->h_addr;
1479 np = getnetbyname (ptr_word);
1480 if (np != NULL && np->n_addrtype == AF_INET) {
1481 a = htonl (*(u_int32_t *)np->n_net);
1482 if (ptr_mask == NULL) {
1483 /* calculate appropriate mask for net */
1486 mask = IN_CLASSA_NET;
1487 else if (IN_CLASSB(ah))
1488 mask = IN_CLASSB_NET;
1489 else if (IN_CLASSC(ah))
1490 mask = IN_CLASSC_NET;
1493 a = inet_addr (ptr_word);
1497 if (ptr_mask != NULL)
1500 if (a == (u_int32_t)-1L) {
1501 warn("unknown host %s in auth. address list", ap->word);
1505 if (offset >= ~mask) {
1506 warn("interface unit %d too large for subnet %v",
1510 a = htonl((ntohl(a) & mask) + offset);
1511 mask = ~(u_int32_t)0;
1513 ip[n].mask = htonl(mask);
1514 ip[n].base = a & ip[n].mask;
1516 if (~mask == 0 && suggested_ip == 0)
1520 ip[n].permit = 0; /* make the last entry forbid all addresses */
1521 ip[n].base = 0; /* to terminate the list */
1524 addresses[unit] = ip;
1527 * If the address given for the peer isn't authorized, or if
1528 * the user hasn't given one, AND there is an authorized address
1529 * which is a single host, then use that if we find one.
1531 if (suggested_ip != 0
1532 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
1533 wo->hisaddr = suggested_ip;
1537 * auth_ip_addr - check whether the peer is authorized to use
1538 * a given IP address. Returns 1 if authorized, 0 otherwise.
1541 auth_ip_addr(unit, addr)
1547 /* don't allow loopback or multicast address */
1548 if (bad_ip_adrs(addr))
1551 if (addresses[unit] != NULL) {
1552 ok = ip_addr_check(addr, addresses[unit]);
1557 return 0; /* no addresses authorized */
1558 return allow_any_ip || !have_route_to(addr);
1562 ip_addr_check(addr, addrs)
1564 struct permitted_ip *addrs;
1567 if ((addr & addrs->mask) == addrs->base)
1568 return addrs->permit;
1572 * bad_ip_adrs - return 1 if the IP address is one we don't want
1573 * to use, such as an address in the loopback net or a multicast address.
1574 * addr is in network byte order.
1581 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1582 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1586 * some_ip_ok - check a wordlist to see if it authorizes any
1591 struct wordlist *addrs;
1593 for (; addrs != 0; addrs = addrs->next) {
1594 if (addrs->word[0] == '-')
1596 if (addrs->word[0] != '!')
1597 return 1; /* some IP address is allowed */
1603 * check_access - complain if a secret file has too-liberal permissions.
1606 check_access(f, filename)
1612 if (fstat(fileno(f), &sbuf) < 0) {
1613 warn("cannot stat secret file %s: %m", filename);
1614 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1615 warn("Warning - secret file %s has world and/or group access",
1622 * scan_authfile - Scan an authorization file for a secret suitable
1623 * for authenticating `client' on `server'. The return value is -1
1624 * if no secret is found, otherwise >= 0. The return value has
1625 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1626 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1627 * Any following words on the line up to a "--" (i.e. address authorization
1628 * info) are placed in a wordlist and returned in *addrs. Any
1629 * following words (extra options) are placed in a wordlist and
1630 * returned in *opts.
1631 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1634 scan_authfile(f, client, server, secret, addrs, opts, filename)
1639 struct wordlist **addrs;
1640 struct wordlist **opts;
1644 int got_flag, best_flag;
1646 struct wordlist *ap, *addr_list, *alist, **app;
1647 char word[MAXWORDLEN];
1648 char atfile[MAXWORDLEN];
1649 char lsecret[MAXWORDLEN];
1656 if (!getword(f, word, &newline, filename))
1657 return -1; /* file is empty??? */
1662 * Skip until we find a word at the start of a line.
1664 while (!newline && getword(f, word, &newline, filename))
1667 break; /* got to end of file */
1670 * Got a client - check if it's a match or a wildcard.
1673 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1678 got_flag = NONWILD_CLIENT;
1681 * Now get a server and check if it matches.
1683 if (!getword(f, word, &newline, filename))
1687 if (!ISWILD(word)) {
1688 if (server != NULL && strcmp(word, server) != 0)
1690 got_flag |= NONWILD_SERVER;
1694 * Got some sort of a match - see if it's better than what
1697 if (got_flag <= best_flag)
1703 if (!getword(f, word, &newline, filename))
1709 * Special syntax: @filename means read secret from file.
1711 if (word[0] == '@') {
1712 strlcpy(atfile, word+1, sizeof(atfile));
1713 if ((sf = fopen(atfile, "r")) == NULL) {
1714 warn("can't open indirect secret file %s", atfile);
1717 check_access(sf, atfile);
1718 if (!getword(sf, word, &xxx, atfile)) {
1719 warn("no secret in indirect secret file %s", atfile);
1726 strlcpy(lsecret, word, sizeof(lsecret));
1729 * Now read address authorization info and make a wordlist.
1733 if (!getword(f, word, &newline, filename) || newline)
1735 ap = (struct wordlist *) malloc(sizeof(struct wordlist));
1737 novm("authorized addresses");
1738 ap->word = strdup(word);
1739 if (ap->word == NULL)
1740 novm("authorized addresses");
1747 * This is the best so far; remember it.
1749 best_flag = got_flag;
1751 free_wordlist(addr_list);
1754 strlcpy(secret, lsecret, MAXWORDLEN);
1760 /* scan for a -- word indicating the start of options */
1761 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
1762 if (strcmp(ap->word, "--") == 0)
1764 /* ap = start of options */
1766 ap = ap->next; /* first option */
1767 free(*app); /* free the "--" word */
1768 *app = NULL; /* terminate addr list */
1772 else if (ap != NULL)
1776 else if (addr_list != NULL)
1777 free_wordlist(addr_list);
1783 * free_wordlist - release memory allocated for a wordlist.
1787 struct wordlist *wp;
1789 struct wordlist *next;
1791 while (wp != NULL) {
1799 * auth_script_done - called when the auth-up or auth-down script
1803 auth_script_done(arg)
1806 auth_script_pid = 0;
1807 switch (auth_script_state) {
1809 if (auth_state == s_down) {
1810 auth_script_state = s_down;
1811 auth_script(_PATH_AUTHDOWN);
1815 if (auth_state == s_up) {
1816 auth_script_state = s_up;
1817 auth_script(_PATH_AUTHUP);
1824 * auth_script - execute a script with arguments
1825 * interface-name peer-name real-user tty speed
1837 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1838 user_name = pw->pw_name;
1840 slprintf(struid, sizeof(struid), "%d", getuid());
1843 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1847 argv[2] = peer_authname;
1848 argv[3] = user_name;
1853 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);