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.69 2001/03/12 22:50:01 paulus Exp $"
44 #include <sys/types.h>
46 #include <sys/socket.h>
50 #if defined(_PATH_LASTLOG) && defined(_linux_)
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
62 #include <security/pam_appl.h>
68 #define PW_PPP PW_LOGIN
81 #include "pathnames.h"
83 static const char rcsid[] = RCSID;
85 /* Bits in scan_authfile return value */
86 #define NONWILD_SERVER 1
87 #define NONWILD_CLIENT 2
89 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
91 /* The name by which the peer authenticated itself to us. */
92 char peer_authname[MAXNAMELEN];
94 /* Records which authentication operations haven't completed yet. */
95 static int auth_pending[NUM_PPP];
97 /* Set if we have successfully called plogin() */
100 /* List of addresses which the peer may use. */
101 static struct permitted_ip *addresses[NUM_PPP];
103 /* Wordlist giving addresses which the peer may use
104 without authenticating itself. */
105 static struct wordlist *noauth_addrs;
107 /* Extra options to apply, from the secrets file entry for the peer. */
108 static struct wordlist *extra_options;
110 /* Number of network protocols which we have opened. */
111 static int num_np_open;
113 /* Number of network protocols which have come up. */
114 static int num_np_up;
116 /* Set if we got the contents of passwd[] from the pap-secrets file. */
117 static int passwd_from_file;
119 /* Set if we require authentication only because we have a default route. */
120 static bool default_auth;
122 /* Hook to enable a plugin to control the idle time limit */
123 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
125 /* Hook for a plugin to say whether we can possibly authenticate any peer */
126 int (*pap_check_hook) __P((void)) = NULL;
128 /* Hook for a plugin to check the PAP user and password */
129 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
130 struct wordlist **paddrs,
131 struct wordlist **popts)) = NULL;
133 /* Hook for a plugin to know about the PAP user logout */
134 void (*pap_logout_hook) __P((void)) = NULL;
136 /* Hook for a plugin to get the PAP password for authenticating us */
137 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
140 * This is used to ensure that we don't start an auth-up/down
141 * script while one is already running.
148 static enum script_state auth_state = s_down;
149 static enum script_state auth_script_state = s_down;
150 static pid_t auth_script_pid = 0;
152 static int used_login; /* peer authenticated against login database */
157 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
158 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
159 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
160 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
161 bool usehostname = 0; /* Use hostname for our_name */
162 bool auth_required = 0; /* Always require authentication from peer */
163 bool allow_any_ip = 0; /* Allow peer to use any IP address */
164 bool explicit_remote = 0; /* User specified explicit remote name */
165 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
167 static char *uafname; /* name of most recent +ua file */
169 /* Bits in auth_pending[] */
170 #define PAP_WITHPEER 1
172 #define CHAP_WITHPEER 4
175 extern char *crypt __P((const char *, const char *));
177 /* Prototypes for procedures local to this file. */
179 static void network_phase __P((int));
180 static void check_idle __P((void *));
182 static void check_link __P((void *));
183 static void check_lan_link __P((void *));
184 static void connect_time_expired __P((void *));
185 static int plogin __P((char *, char *, char **));
186 static void plogout __P((void));
187 static int null_login __P((int));
188 static int get_pap_passwd __P((char *));
189 static int have_pap_secret __P((int *));
190 static int have_chap_secret __P((char *, char *, int, int *));
191 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
192 static int scan_authfile __P((FILE *, char *, char *, char *,
193 struct wordlist **, struct wordlist **,
195 static void free_wordlist __P((struct wordlist *));
196 static void auth_script __P((char *));
197 static void auth_script_done __P((void *));
198 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
199 static int some_ip_ok __P((struct wordlist *));
200 static int setupapfile __P((char **));
201 static int privgroup __P((char **));
202 static int set_noauth_addr __P((char **));
203 static void check_access __P((FILE *, char *));
204 static int wordlist_count __P((struct wordlist *));
207 * Authentication-related options.
209 option_t auth_options[] = {
210 { "auth", o_bool, &auth_required,
211 "Require authentication from peer", OPT_PRIO | 1 },
212 { "noauth", o_bool, &auth_required,
213 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
215 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
216 "Require PAP authentication from peer",
217 OPT_PRIOSUB | 1, &auth_required },
218 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
219 "Require PAP authentication from peer",
220 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
221 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
222 "Require CHAP authentication from peer",
223 OPT_PRIOSUB | 1, &auth_required },
224 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
225 "Require CHAP authentication from peer",
226 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
228 { "refuse-pap", o_bool, &refuse_pap,
229 "Don't agree to auth to peer with PAP", 1 },
230 { "-pap", o_bool, &refuse_pap,
231 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
233 { "refuse-chap", o_bool, &refuse_chap,
234 "Don't agree to auth to peer with CHAP", 1 },
235 { "-chap", o_bool, &refuse_chap,
236 "Don't allow CHAP authentication with peer", OPT_ALIAS | 1 },
238 { "name", o_string, our_name,
239 "Set local name for authentication",
240 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
242 { "+ua", o_special, (void *)setupapfile,
243 "Get PAP user and password from file",
244 OPT_PRIO | OPT_A2STRVAL, &uafname },
246 { "user", o_string, user,
247 "Set name for auth with peer", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN },
249 { "password", o_string, passwd,
250 "Password for authenticating us to the peer",
251 OPT_PRIO | OPT_STATIC | OPT_HIDE, NULL, MAXSECRETLEN },
253 { "usehostname", o_bool, &usehostname,
254 "Must use hostname for authentication", 1 },
256 { "remotename", o_string, remote_name,
257 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
258 &explicit_remote, MAXNAMELEN },
260 { "login", o_bool, &uselogin,
261 "Use system password database for PAP", 1 },
263 { "papcrypt", o_bool, &cryptpap,
264 "PAP passwords are encrypted", 1 },
266 { "privgroup", o_special, (void *)privgroup,
267 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
269 { "allow-ip", o_special, (void *)set_noauth_addr,
270 "Set IP address(es) which can be used without authentication",
271 OPT_PRIV | OPT_A2LIST },
277 * setupapfile - specifies UPAP info for authenticating with peer.
285 char u[MAXNAMELEN], p[MAXSECRETLEN];
288 lcp_allowoptions[0].neg_upap = 1;
290 /* open user info file */
291 fname = strdup(*argv);
293 novm("+ua file name");
295 ufile = fopen(fname, "r");
298 option_error("unable to open user login data file %s", fname);
301 check_access(ufile, fname);
305 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
306 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL){
307 option_error("unable to read user login data file %s", fname);
312 /* get rid of newlines */
314 if (l > 0 && u[l-1] == '\n')
317 if (l > 0 && p[l-1] == '\n')
320 if (override_value("user", option_priority, fname))
321 strlcpy(user, u, sizeof(user));
322 if (override_value("passwd", option_priority, fname))
323 strlcpy(passwd, p, sizeof(passwd));
330 * privgroup - allow members of the group to have privileged access.
341 option_error("group %s is unknown", *argv);
344 for (i = 0; i < ngroups; ++i) {
345 if (groups[i] == g->gr_gid) {
355 * set_noauth_addr - set address(es) that can be used without authentication.
356 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
359 set_noauth_addr(argv)
363 int l = strlen(addr) + 1;
366 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
368 novm("allow-ip argument");
369 wp->word = (char *) (wp + 1);
370 wp->next = noauth_addrs;
371 BCOPY(addr, wp->word, l);
378 * An Open on LCP has requested a change from Dead to Establish phase.
379 * Do what's necessary to bring the physical layer up.
388 * LCP has terminated the link; go to the Dead phase and take the
389 * physical layer down.
392 link_terminated(unit)
395 if (phase == PHASE_DEAD)
397 if (pap_logout_hook) {
403 new_phase(PHASE_DEAD);
404 notice("Connection terminated.");
408 * LCP has gone down; it will either die or try to re-establish.
415 struct protent *protp;
418 if (auth_script_state == s_up && auth_script_pid == 0) {
419 update_link_stats(unit);
420 auth_script_state = s_down;
421 auth_script(_PATH_AUTHDOWN);
423 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
424 if (!protp->enabled_flag)
426 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
427 (*protp->lowerdown)(unit);
428 if (protp->protocol < 0xC000 && protp->close != NULL)
429 (*protp->close)(unit, "LCP down");
433 if (phase != PHASE_DEAD)
434 new_phase(PHASE_TERMINATE);
438 * The link is established.
439 * Proceed to the Dead, Authenticate or Network phase as appropriate.
442 link_established(unit)
446 lcp_options *wo = &lcp_wantoptions[unit];
447 lcp_options *go = &lcp_gotoptions[unit];
448 lcp_options *ho = &lcp_hisoptions[unit];
450 struct protent *protp;
453 * Tell higher-level protocols that LCP is up.
455 for (i = 0; (protp = protocols[i]) != NULL; ++i)
456 if (protp->protocol != PPP_LCP && protp->enabled_flag
457 && protp->lowerup != NULL)
458 (*protp->lowerup)(unit);
460 if (auth_required && !(go->neg_chap || go->neg_upap)) {
462 * We wanted the peer to authenticate itself, and it refused:
463 * if we have some address(es) it can use without auth, fine,
464 * otherwise treat it as though it authenticated with PAP using
465 * a username * of "" and a password of "". If that's not OK,
468 if (noauth_addrs != NULL) {
469 set_allowed_addrs(unit, NULL, NULL);
470 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
471 warn("peer refused to authenticate: terminating link");
472 lcp_close(unit, "peer refused to authenticate");
473 status = EXIT_PEER_AUTH_FAILED;
478 new_phase(PHASE_AUTHENTICATE);
482 ChapAuthPeer(unit, our_name, go->chap_mdtype);
484 } else if (go->neg_upap) {
489 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
490 auth |= CHAP_WITHPEER;
491 } else if (ho->neg_upap) {
492 if (passwd[0] == 0) {
493 passwd_from_file = 1;
494 if (!get_pap_passwd(passwd))
495 error("No secret found for PAP login");
497 upap_authwithpeer(unit, user, passwd);
498 auth |= PAP_WITHPEER;
500 auth_pending[unit] = auth;
507 * Proceed to the network phase.
513 lcp_options *go = &lcp_gotoptions[unit];
516 * If the peer had to authenticate, run the auth-up script now.
518 if (go->neg_chap || go->neg_upap) {
520 if (auth_script_state == s_down && auth_script_pid == 0) {
521 auth_script_state = s_up;
522 auth_script(_PATH_AUTHUP);
528 * If we negotiated callback, do it now.
531 new_phase(PHASE_CALLBACK);
532 (*cbcp_protent.open)(unit);
538 * Process extra options from the secrets file
543 options_from_list(extra_options, 1);
544 free_wordlist(extra_options);
555 struct protent *protp;
557 new_phase(PHASE_NETWORK);
559 #ifdef HAVE_MULTILINK
561 if (mp_join_bundle()) {
562 if (updetach && !nodetach)
567 #endif /* HAVE_MULTILINK */
571 set_filters(&pass_filter, &active_filter);
573 for (i = 0; (protp = protocols[i]) != NULL; ++i)
574 if (protp->protocol < 0xC000 && protp->enabled_flag
575 && protp->open != NULL) {
577 if (protp->protocol != PPP_CCP)
581 if (num_np_open == 0)
583 lcp_close(0, "No network protocols running");
587 * The peer has failed to authenticate himself using `protocol'.
590 auth_peer_fail(unit, protocol)
594 * Authentication failure: take the link down
596 lcp_close(unit, "Authentication failed");
597 status = EXIT_PEER_AUTH_FAILED;
601 * The peer has been successfully authenticated using `protocol'.
604 auth_peer_success(unit, protocol, name, namelen)
619 warn("auth_peer_success: unknown protocol %x", protocol);
624 * Save the authenticated name of the peer for later.
626 if (namelen > sizeof(peer_authname) - 1)
627 namelen = sizeof(peer_authname) - 1;
628 BCOPY(name, peer_authname, namelen);
629 peer_authname[namelen] = 0;
630 script_setenv("PEERNAME", peer_authname, 0);
633 * If there is no more authentication still to be done,
634 * proceed to the network (or callback) phase.
636 if ((auth_pending[unit] &= ~bit) == 0)
641 * We have failed to authenticate ourselves to the peer using `protocol'.
644 auth_withpeer_fail(unit, protocol)
647 if (passwd_from_file)
648 BZERO(passwd, MAXSECRETLEN);
650 * We've failed to authenticate ourselves to our peer.
651 * Some servers keep sending CHAP challenges, but there
652 * is no point in persisting without any way to get updated
653 * authentication secrets.
655 lcp_close(unit, "Failed to authenticate ourselves to peer");
656 status = EXIT_AUTH_TOPEER_FAILED;
658 if ((strlen(user) > 0) || autoscanP2) {
659 printf("PPP: Authenication failed.\n");
660 create_msg(BCM_PPPOE_AUTH_FAILED);
661 syslog(LOG_ERR,"User name and password authentication failed.\n");
667 * We have successfully authenticated ourselves with the peer using `protocol'.
670 auth_withpeer_success(unit, protocol)
680 if (passwd_from_file)
681 BZERO(passwd, MAXSECRETLEN);
685 warn("auth_withpeer_success: unknown protocol %x", protocol);
690 * If there is no more authentication still being done,
691 * proceed to the network (or callback) phase.
693 if ((auth_pending[unit] &= ~bit) == 0)
699 * np_up - a network protocol has come up.
707 if (num_np_up == 0) {
709 * At this point we consider that the link has come up successfully.
713 new_phase(PHASE_RUNNING);
715 if (idle_time_hook != 0)
716 tlim = (*idle_time_hook)(NULL);
718 tlim = idle_time_limit;
720 TIMEOUT(check_idle, NULL, tlim);
723 TIMEOUT(check_link, NULL, 3);
727 TIMEOUT(check_lan_link, NULL, 5);
730 * Set a timeout to close the connection once the maximum
731 * connect time has expired.
734 TIMEOUT(connect_time_expired, 0, maxconnect);
737 * Detach now, if the updetach option was given.
739 if (updetach && !nodetach)
746 * np_down - a network protocol has gone down.
752 if (--num_np_up == 0) {
753 UNTIMEOUT(check_idle, NULL);
755 UNTIMEOUT(check_link, NULL);
757 UNTIMEOUT(check_lan_link, NULL);
758 new_phase(PHASE_NETWORK);
763 * np_finished - a network protocol has finished using the link.
766 np_finished(unit, proto)
769 if (--num_np_open <= 0) {
770 /* no further use for the link: shut up shop. */
771 lcp_close(0, "No network protocols running");
776 * check_idle - check whether the link has been idle for long
777 * enough that we can shut it down.
783 struct ppp_idle idle;
787 if (!get_idle_time(0, &idle))
789 if (idle_time_hook != 0) {
790 tlim = idle_time_hook(&idle);
792 tlim = idle_time_limit - idle.xmit_idle;
793 itime = MIN(idle.xmit_idle, idle.recv_idle);
794 tlim = idle_time_limit - itime;
797 /* link is idle: shut it down. */
798 notice("Terminating connection due to lack of activity.");
799 lcp_close(0, "Link inactive");
801 status = EXIT_IDLE_TIMEOUT;
804 sprintf(oldsession, "%s", "");
806 TIMEOUT(check_idle, NULL, tlim);
815 if ((fd=fopen("/var/run/wanup", "r"))) {
826 sprintf(path, "%s/%s/%s", "/proc/var/fyi/wan", session_path, "wanup");
827 //printf("=== %s ===\n", path);
829 if (fd=fopen(path, "r")) {
832 //printf("data=%x\n", data[0]);
833 if (strstr(data, "1"))
850 /* link is idle: shut it down. */
851 notice("Terminating connection due to link down.");
852 lcp_close(0, "Link down");
854 status = EXIT_IDLE_TIMEOUT;
856 UNTIMEOUT(check_link, NULL);
857 TIMEOUT(check_link, NULL, 3);
861 int lan_pending_cur=0;
862 int lan_pending_max=36;
869 if ((fd=fopen("/var/run/ethup", "r"))) {
874 else if ((fd=fopen("/var/run/usbup", "r"))) {
890 if (!lan_link_up()) {
892 //printf("LAN link down %d ....\n", lan_pending_cur);
893 if (lan_pending_cur >= lan_pending_max) {
894 /* link is idle: shut it down. */
896 notice("Terminating connection due to link down.");
897 lcp_close(0, "Link down");
899 status = EXIT_IDLE_TIMEOUT;
901 UNTIMEOUT(check_lan_link, NULL);
902 TIMEOUT(check_lan_link, NULL, 5);
904 //printf("LAN link up ....\n");
906 UNTIMEOUT(check_lan_link, NULL);
907 TIMEOUT(check_lan_link, NULL, 5);
912 * connect_time_expired - log a message and close the connection.
915 connect_time_expired(arg)
918 info("Connect time expired");
919 lcp_close(0, "Connect time expired"); /* Close connection */
920 status = EXIT_CONNECT_TIME;
924 * auth_check_options - called to check authentication options.
929 lcp_options *wo = &lcp_wantoptions[0];
933 /* Default our_name to hostname, and user to our_name */
934 if (our_name[0] == 0 || usehostname)
935 strlcpy(our_name, hostname, sizeof(our_name));
937 strlcpy(user, our_name, sizeof(user));
940 * If we have a default route, require the peer to authenticate
941 * unless the noauth option was given or the real user is root.
943 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
948 /* If authentication is required, ask peer for CHAP or PAP. */
951 if (!wo->neg_chap && !wo->neg_upap) {
961 * Check whether we have appropriate secrets to use
962 * to authenticate the peer.
965 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
966 if (!can_auth && wo->neg_chap) {
967 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
968 our_name, 1, &lacks_ip);
971 if (auth_required && !can_auth && noauth_addrs == NULL) {
974 "By default the remote system is required to authenticate itself");
976 "(because this system has a default route to the internet)");
977 } else if (explicit_remote)
979 "The remote system (%s) is required to authenticate itself",
983 "The remote system is required to authenticate itself");
985 "but I couldn't find any suitable secret (password) for it to use to do so.");
988 "(None of the available passwords would let it use an IP address.)");
995 * auth_reset - called when LCP is starting negotiations to recheck
996 * authentication options, i.e. whether we have appropriate secrets
997 * to use for authenticating ourselves and/or the peer.
1003 lcp_options *go = &lcp_gotoptions[unit];
1004 lcp_options *ao = &lcp_allowoptions[0];
1006 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1007 ao->neg_chap = !refuse_chap
1009 || have_chap_secret(user, (explicit_remote? remote_name: NULL),
1012 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1015 if (!have_chap_secret((explicit_remote? remote_name: NULL),
1023 * check_passwd - Check the user name and passwd against the PAP secrets
1024 * file. If requested, also check against the system password database,
1025 * and login the user if OK.
1028 * UPAP_AUTHNAK: Authentication failed.
1029 * UPAP_AUTHACK: Authentication succeeded.
1030 * In either case, msg points to an appropriate message.
1033 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1044 struct wordlist *addrs = NULL, *opts = NULL;
1045 char passwd[256], user[256];
1046 char secret[MAXWORDLEN];
1047 static int attempts = 0;
1050 * Make copies of apasswd and auser, then null-terminate them.
1051 * If there are unprintable characters in the password, make
1054 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1055 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1059 * Check if a plugin wants to handle this.
1061 if (pap_auth_hook) {
1062 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1065 set_allowed_addrs(unit, addrs, opts);
1066 BZERO(passwd, sizeof(passwd));
1068 free_wordlist(addrs);
1069 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1074 * Open the file of pap secrets and scan for a suitable secret
1075 * for authenticating this user.
1077 filename = _PATH_UPAPFILE;
1078 addrs = opts = NULL;
1080 f = fopen(filename, "r");
1082 error("Can't open PAP password file %s: %m", filename);
1085 check_access(f, filename);
1086 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
1087 warn("no PAP secret found for %s", user);
1090 * If the secret is "@login", it means to check
1091 * the password against the login database.
1093 int login_secret = strcmp(secret, "@login") == 0;
1095 if (uselogin || login_secret) {
1096 /* login option or secret is @login */
1097 ret = plogin(user, passwd, msg);
1098 if (ret == UPAP_AUTHNAK)
1099 warn("PAP login failure for %s", user);
1103 if (secret[0] != 0 && !login_secret) {
1104 /* password given in pap-secrets - must match */
1105 if ((cryptpap || strcmp(passwd, secret) != 0)
1106 && strcmp(crypt(passwd, secret), secret) != 0) {
1108 warn("PAP authentication failure for %s", user);
1115 if (ret == UPAP_AUTHNAK) {
1117 *msg = "Login incorrect";
1119 * XXX can we ever get here more than once??
1120 * Frustrate passwd stealer programs.
1121 * Allow 10 tries, but start backing off after 3 (stolen from login).
1122 * On 10'th, drop the connection.
1124 if (attempts++ >= 10) {
1125 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1126 lcp_close(unit, "login failed");
1129 sleep((u_int) (attempts - 3) * 5);
1131 free_wordlist(opts);
1134 attempts = 0; /* Reset count */
1137 set_allowed_addrs(unit, addrs, opts);
1141 free_wordlist(addrs);
1142 BZERO(passwd, sizeof(passwd));
1143 BZERO(secret, sizeof(secret));
1149 * This function is needed for PAM.
1153 /* Static variables used to communicate between the conversation function
1154 * and the server_login function
1156 static char *PAM_username;
1157 static char *PAM_password;
1158 static int PAM_error = 0;
1159 static pam_handle_t *pamh = NULL;
1161 /* PAM conversation function
1162 * Here we assume (for now, at least) that echo on means login name, and
1163 * echo off means password.
1166 static int PAM_conv (int num_msg, const struct pam_message **msg,
1167 struct pam_response **resp, void *appdata_ptr)
1170 struct pam_response *reply = NULL;
1172 #define COPY_STRING(s) (s) ? strdup(s) : NULL
1174 reply = malloc(sizeof(struct pam_response) * num_msg);
1175 if (!reply) return PAM_CONV_ERR;
1177 for (replies = 0; replies < num_msg; replies++) {
1178 switch (msg[replies]->msg_style) {
1179 case PAM_PROMPT_ECHO_ON:
1180 reply[replies].resp_retcode = PAM_SUCCESS;
1181 reply[replies].resp = COPY_STRING(PAM_username);
1182 /* PAM frees resp */
1184 case PAM_PROMPT_ECHO_OFF:
1185 reply[replies].resp_retcode = PAM_SUCCESS;
1186 reply[replies].resp = COPY_STRING(PAM_password);
1187 /* PAM frees resp */
1192 /* ignore it, but pam still wants a NULL response... */
1193 reply[replies].resp_retcode = PAM_SUCCESS;
1194 reply[replies].resp = NULL;
1197 /* Must be an error of some sort... */
1200 return PAM_CONV_ERR;
1207 static struct pam_conv PAM_conversation = {
1211 #endif /* USE_PAM */
1214 * plogin - Check the user name and password against the system
1215 * password database, and login the user if OK.
1218 * UPAP_AUTHNAK: Login failed.
1219 * UPAP_AUTHACK: Login succeeded.
1220 * In either case, msg points to an appropriate message.
1224 plogin(user, passwd, msg)
1234 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1235 if (pam_error != PAM_SUCCESS) {
1236 *msg = (char *) pam_strerror (pamh, pam_error);
1238 return UPAP_AUTHNAK;
1241 * Define the fields for the credential validation
1244 PAM_username = user;
1245 PAM_password = passwd;
1247 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1252 pam_error = pam_authenticate (pamh, PAM_SILENT);
1253 if (pam_error == PAM_SUCCESS && !PAM_error) {
1254 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1255 if (pam_error == PAM_SUCCESS)
1256 pam_open_session (pamh, PAM_SILENT);
1259 *msg = (char *) pam_strerror (pamh, pam_error);
1264 reopen_log(); /* apparently the PAM stuff does closelog() */
1265 PAM_username = NULL;
1266 PAM_password = NULL;
1267 if (pam_error != PAM_SUCCESS)
1268 return UPAP_AUTHNAK;
1269 #else /* #ifdef USE_PAM */
1272 * Use the non-PAM methods directly
1277 struct spwd *getspnam();
1279 struct passwd *pw = getpwnam(user);
1283 return (UPAP_AUTHNAK);
1286 spwd = getspnam(user);
1289 /* check the age of the password entry */
1290 long now = time(NULL) / 86400L;
1292 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1293 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1294 && spwd->sp_lstchg >= 0
1295 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1296 warn("Password for %s has expired", user);
1297 return (UPAP_AUTHNAK);
1299 pw->pw_passwd = spwd->sp_pwdp;
1304 * If no passwd, don't let them login.
1306 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1307 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1308 return (UPAP_AUTHNAK);
1310 #endif /* #ifdef USE_PAM */
1313 * Write a wtmp entry for this user.
1317 if (strncmp(tty, "/dev/", 5) == 0)
1319 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1321 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1322 if (pw != (struct passwd *)NULL) {
1326 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1327 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1328 memset((void *)&ll, 0, sizeof(ll));
1329 (void)time(&ll.ll_time);
1330 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1331 (void)write(fd, (char *)&ll, sizeof(ll));
1335 #endif /* _PATH_LASTLOG and not USE_PAM */
1337 info("user %s logged in", user);
1340 return (UPAP_AUTHACK);
1344 * plogout - Logout the user.
1353 pam_error = pam_close_session (pamh, PAM_SILENT);
1354 pam_end (pamh, pam_error);
1357 /* Apparently the pam stuff does closelog(). */
1359 #else /* ! USE_PAM */
1363 if (strncmp(tty, "/dev/", 5) == 0)
1365 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1366 #endif /* ! USE_PAM */
1372 * null_login - Check if a username of "" and a password of "" are
1373 * acceptable, and iff so, set the list of acceptable IP addresses
1383 struct wordlist *addrs, *opts;
1384 char secret[MAXWORDLEN];
1387 * Open the file of pap secrets and scan for a suitable secret.
1389 filename = _PATH_UPAPFILE;
1391 f = fopen(filename, "r");
1394 check_access(f, filename);
1396 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename);
1397 ret = i >= 0 && secret[0] == 0;
1398 BZERO(secret, sizeof(secret));
1401 set_allowed_addrs(unit, addrs, opts);
1403 free_wordlist(opts);
1405 free_wordlist(addrs);
1413 * get_pap_passwd - get a password for authenticating ourselves with
1414 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1416 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1419 get_pap_passwd(passwd)
1425 char secret[MAXWORDLEN];
1428 * Check whether a plugin wants to supply this.
1430 if (pap_passwd_hook) {
1431 ret = (*pap_passwd_hook)(user, passwd);
1436 filename = _PATH_UPAPFILE;
1437 f = fopen(filename, "r");
1440 check_access(f, filename);
1441 ret = scan_authfile(f, user,
1442 (remote_name[0]? remote_name: NULL),
1443 secret, NULL, NULL, filename);
1448 strlcpy(passwd, secret, MAXSECRETLEN);
1449 BZERO(secret, sizeof(secret));
1455 * have_pap_secret - check whether we have a PAP file with any
1456 * secrets that we could possibly use for authenticating the peer.
1459 have_pap_secret(lacks_ipp)
1465 struct wordlist *addrs;
1467 /* let the plugin decide, if there is one */
1468 if (pap_check_hook) {
1469 ret = (*pap_check_hook)();
1474 filename = _PATH_UPAPFILE;
1475 f = fopen(filename, "r");
1479 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1480 NULL, &addrs, NULL, filename);
1482 if (ret >= 0 && !some_ip_ok(addrs)) {
1488 free_wordlist(addrs);
1495 * have_chap_secret - check whether we have a CHAP file with a
1496 * secret that we could possibly use for authenticating `client'
1497 * on `server'. Either can be the null string, meaning we don't
1498 * know the identity yet.
1501 have_chap_secret(client, server, need_ip, lacks_ipp)
1510 struct wordlist *addrs;
1512 filename = _PATH_CHAPFILE;
1513 f = fopen(filename, "r");
1517 if (client != NULL && client[0] == 0)
1519 else if (server != NULL && server[0] == 0)
1522 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename);
1524 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1530 free_wordlist(addrs);
1537 * get_secret - open the CHAP secret file and return the secret
1538 * for authenticating the given client on the given server.
1539 * (We could be either client or server).
1542 get_secret(unit, client, server, secret, secret_len, am_server)
1553 struct wordlist *addrs, *opts;
1554 char secbuf[MAXWORDLEN];
1556 if (!am_server && passwd[0] != 0) {
1557 strlcpy(secbuf, passwd, sizeof(secbuf));
1559 filename = _PATH_CHAPFILE;
1563 f = fopen(filename, "r");
1565 error("Can't open chap secret file %s: %m", filename);
1568 check_access(f, filename);
1570 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename);
1576 set_allowed_addrs(unit, addrs, opts);
1578 free_wordlist(opts);
1580 free_wordlist(addrs);
1583 len = strlen(secbuf);
1584 if (len > MAXSECRETLEN) {
1585 error("Secret for %s on %s is too long", client, server);
1588 BCOPY(secbuf, secret, len);
1589 BZERO(secbuf, sizeof(secbuf));
1596 * set_allowed_addrs() - set the list of allowed addresses.
1597 * Also looks for `--' indicating options to apply for this peer
1598 * and leaves the following words in extra_options.
1601 set_allowed_addrs(unit, addrs, opts)
1603 struct wordlist *addrs;
1604 struct wordlist *opts;
1607 struct wordlist *ap, **plink;
1608 struct permitted_ip *ip;
1609 char *ptr_word, *ptr_mask;
1612 u_int32_t a, mask, ah, offset;
1613 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1614 u_int32_t suggested_ip = 0;
1616 if (addresses[unit] != NULL)
1617 free(addresses[unit]);
1618 addresses[unit] = NULL;
1619 if (extra_options != NULL)
1620 free_wordlist(extra_options);
1621 extra_options = opts;
1624 * Count the number of IP addresses given.
1626 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1629 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1633 /* temporarily append the noauth_addrs list to addrs */
1634 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1636 *plink = noauth_addrs;
1639 for (ap = addrs; ap != NULL; ap = ap->next) {
1640 /* "-" means no addresses authorized, "*" means any address allowed */
1641 ptr_word = ap->word;
1642 if (strcmp(ptr_word, "-") == 0)
1644 if (strcmp(ptr_word, "*") == 0) {
1646 ip[n].base = ip[n].mask = 0;
1652 if (*ptr_word == '!') {
1657 mask = ~ (u_int32_t) 0;
1659 ptr_mask = strchr (ptr_word, '/');
1660 if (ptr_mask != NULL) {
1664 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1665 if (bit_count <= 0 || bit_count > 32) {
1666 warn("invalid address length %v in auth. address list",
1670 bit_count = 32 - bit_count; /* # bits in host part */
1672 offset = ifunit + 1;
1676 warn("invalid address length syntax: %v", ptr_mask+1);
1683 hp = gethostbyname(ptr_word);
1684 if (hp != NULL && hp->h_addrtype == AF_INET) {
1685 a = *(u_int32_t *)hp->h_addr;
1687 np = getnetbyname (ptr_word);
1688 if (np != NULL && np->n_addrtype == AF_INET) {
1689 a = htonl (*(u_int32_t *)np->n_net);
1690 if (ptr_mask == NULL) {
1691 /* calculate appropriate mask for net */
1694 mask = IN_CLASSA_NET;
1695 else if (IN_CLASSB(ah))
1696 mask = IN_CLASSB_NET;
1697 else if (IN_CLASSC(ah))
1698 mask = IN_CLASSC_NET;
1701 a = inet_addr (ptr_word);
1705 if (ptr_mask != NULL)
1708 if (a == (u_int32_t)-1L) {
1709 warn("unknown host %s in auth. address list", ap->word);
1713 if (offset >= ~mask) {
1714 warn("interface unit %d too large for subnet %v",
1718 a = htonl((ntohl(a) & mask) + offset);
1719 mask = ~(u_int32_t)0;
1721 ip[n].mask = htonl(mask);
1722 ip[n].base = a & ip[n].mask;
1724 if (~mask == 0 && suggested_ip == 0)
1729 ip[n].permit = 0; /* make the last entry forbid all addresses */
1730 ip[n].base = 0; /* to terminate the list */
1733 addresses[unit] = ip;
1736 * If the address given for the peer isn't authorized, or if
1737 * the user hasn't given one, AND there is an authorized address
1738 * which is a single host, then use that if we find one.
1740 if (suggested_ip != 0
1741 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
1742 wo->hisaddr = suggested_ip;
1744 * Do we insist on this address? No, if there are other
1745 * addresses authorized than the suggested one.
1748 wo->accept_remote = 1;
1753 * auth_ip_addr - check whether the peer is authorized to use
1754 * a given IP address. Returns 1 if authorized, 0 otherwise.
1757 auth_ip_addr(unit, addr)
1763 /* don't allow loopback or multicast address */
1764 if (bad_ip_adrs(addr))
1767 if (addresses[unit] != NULL) {
1768 ok = ip_addr_check(addr, addresses[unit]);
1773 return 0; /* no addresses authorized */
1774 return allow_any_ip || privileged || !have_route_to(addr);
1778 ip_addr_check(addr, addrs)
1780 struct permitted_ip *addrs;
1783 if ((addr & addrs->mask) == addrs->base)
1784 return addrs->permit;
1788 * bad_ip_adrs - return 1 if the IP address is one we don't want
1789 * to use, such as an address in the loopback net or a multicast address.
1790 * addr is in network byte order.
1797 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1798 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1802 * some_ip_ok - check a wordlist to see if it authorizes any
1807 struct wordlist *addrs;
1809 for (; addrs != 0; addrs = addrs->next) {
1810 if (addrs->word[0] == '-')
1812 if (addrs->word[0] != '!')
1813 return 1; /* some IP address is allowed */
1819 * check_access - complain if a secret file has too-liberal permissions.
1822 check_access(f, filename)
1828 if (fstat(fileno(f), &sbuf) < 0) {
1829 warn("cannot stat secret file %s: %m", filename);
1830 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1831 warn("Warning - secret file %s has world and/or group access",
1838 * scan_authfile - Scan an authorization file for a secret suitable
1839 * for authenticating `client' on `server'. The return value is -1
1840 * if no secret is found, otherwise >= 0. The return value has
1841 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1842 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1843 * Any following words on the line up to a "--" (i.e. address authorization
1844 * info) are placed in a wordlist and returned in *addrs. Any
1845 * following words (extra options) are placed in a wordlist and
1846 * returned in *opts.
1847 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1850 scan_authfile(f, client, server, secret, addrs, opts, filename)
1855 struct wordlist **addrs;
1856 struct wordlist **opts;
1860 int got_flag, best_flag;
1862 struct wordlist *ap, *addr_list, *alist, **app;
1863 char word[MAXWORDLEN];
1864 char atfile[MAXWORDLEN];
1865 char lsecret[MAXWORDLEN];
1872 if (!getword(f, word, &newline, filename))
1873 return -1; /* file is empty??? */
1878 * Skip until we find a word at the start of a line.
1880 while (!newline && getword(f, word, &newline, filename))
1883 break; /* got to end of file */
1886 * Got a client - check if it's a match or a wildcard.
1889 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1894 got_flag = NONWILD_CLIENT;
1897 * Now get a server and check if it matches.
1899 if (!getword(f, word, &newline, filename))
1903 if (!ISWILD(word)) {
1904 if (server != NULL && strcmp(word, server) != 0)
1906 got_flag |= NONWILD_SERVER;
1910 * Got some sort of a match - see if it's better than what
1913 if (got_flag <= best_flag)
1919 if (!getword(f, word, &newline, filename))
1924 if (secret != NULL) {
1926 * Special syntax: @/pathname means read secret from file.
1928 if (word[0] == '@' && word[1] == '/') {
1929 strlcpy(atfile, word+1, sizeof(atfile));
1930 if ((sf = fopen(atfile, "r")) == NULL) {
1931 warn("can't open indirect secret file %s", atfile);
1934 check_access(sf, atfile);
1935 if (!getword(sf, word, &xxx, atfile)) {
1936 warn("no secret in indirect secret file %s", atfile);
1942 strlcpy(lsecret, word, sizeof(lsecret));
1946 * Now read address authorization info and make a wordlist.
1950 if (!getword(f, word, &newline, filename) || newline)
1952 ap = (struct wordlist *)
1953 malloc(sizeof(struct wordlist) + strlen(word) + 1);
1955 novm("authorized addresses");
1956 ap->word = (char *) (ap + 1);
1957 strcpy(ap->word, word);
1964 * This is the best so far; remember it.
1966 best_flag = got_flag;
1968 free_wordlist(addr_list);
1971 strlcpy(secret, lsecret, MAXWORDLEN);
1977 /* scan for a -- word indicating the start of options */
1978 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
1979 if (strcmp(ap->word, "--") == 0)
1981 /* ap = start of options */
1983 ap = ap->next; /* first option */
1984 free(*app); /* free the "--" word */
1985 *app = NULL; /* terminate addr list */
1989 else if (ap != NULL)
1993 else if (addr_list != NULL)
1994 free_wordlist(addr_list);
2000 * wordlist_count - return the number of items in a wordlist
2004 struct wordlist *wp;
2008 for (n = 0; wp != NULL; wp = wp->next)
2014 * free_wordlist - release memory allocated for a wordlist.
2018 struct wordlist *wp;
2020 struct wordlist *next;
2022 while (wp != NULL) {
2030 * auth_script_done - called when the auth-up or auth-down script
2034 auth_script_done(arg)
2037 auth_script_pid = 0;
2038 switch (auth_script_state) {
2040 if (auth_state == s_down) {
2041 auth_script_state = s_down;
2042 auth_script(_PATH_AUTHDOWN);
2046 if (auth_state == s_up) {
2047 auth_script_state = s_up;
2048 auth_script(_PATH_AUTHUP);
2055 * auth_script - execute a script with arguments
2056 * interface-name peer-name real-user tty speed
2068 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2069 user_name = pw->pw_name;
2071 slprintf(struid, sizeof(struid), "%d", getuid());
2074 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2078 argv[2] = peer_authname;
2079 argv[3] = user_name;
2084 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);