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.75 2002/03/05 15:14:04 dfs 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
78 #include "pathnames.h"
80 static const char rcsid[] = RCSID;
82 /* Bits in scan_authfile return value */
83 #define NONWILD_SERVER 1
84 #define NONWILD_CLIENT 2
86 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
88 /* The name by which the peer authenticated itself to us. */
89 char peer_authname[MAXNAMELEN];
91 /* Records which authentication operations haven't completed yet. */
92 static int auth_pending[NUM_PPP];
94 /* Set if we have successfully called plogin() */
97 /* List of addresses which the peer may use. */
98 static struct permitted_ip *addresses[NUM_PPP];
100 /* Wordlist giving addresses which the peer may use
101 without authenticating itself. */
102 static struct wordlist *noauth_addrs;
104 /* Extra options to apply, from the secrets file entry for the peer. */
105 static struct wordlist *extra_options;
107 /* Number of network protocols which we have opened. */
108 static int num_np_open;
110 /* Number of network protocols which have come up. */
111 static int num_np_up;
113 /* Set if we got the contents of passwd[] from the pap-secrets file. */
114 static int passwd_from_file;
116 /* Set if we require authentication only because we have a default route. */
117 static bool default_auth;
119 /* Hook to enable a plugin to control the idle time limit */
120 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
122 /* Hook for a plugin to say whether we can possibly authenticate any peer */
123 int (*pap_check_hook) __P((void)) = NULL;
125 /* Hook for a plugin to check the PAP user and password */
126 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
127 struct wordlist **paddrs,
128 struct wordlist **popts)) = NULL;
130 /* Hook for a plugin to know about the PAP user logout */
131 void (*pap_logout_hook) __P((void)) = NULL;
133 /* Hook for a plugin to get the PAP password for authenticating us */
134 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
136 /* Hook for a plugin to say whether it is OK if the peer
137 refuses to authenticate. */
138 int (*null_auth_hook) __P((struct wordlist **paddrs,
139 struct wordlist **popts)) = NULL;
141 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
143 /* A notifier for when the peer has authenticated itself,
144 and we are proceeding to the network phase. */
145 struct notifier *auth_up_notifier = NULL;
147 /* A notifier for when the link goes down. */
148 struct notifier *link_down_notifier = NULL;
151 * This is used to ensure that we don't start an auth-up/down
152 * script while one is already running.
159 static enum script_state auth_state = s_down;
160 static enum script_state auth_script_state = s_down;
161 static pid_t auth_script_pid = 0;
163 static int used_login; /* peer authenticated against login database */
168 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
169 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
170 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
171 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
173 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
174 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
176 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
177 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
179 bool usehostname = 0; /* Use hostname for our_name */
180 bool auth_required = 0; /* Always require authentication from peer */
181 bool allow_any_ip = 0; /* Allow peer to use any IP address */
182 bool explicit_remote = 0; /* User specified explicit remote name */
183 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
185 static char *uafname; /* name of most recent +ua file */
187 /* Bits in auth_pending[] */
188 #define PAP_WITHPEER 1
190 #define CHAP_WITHPEER 4
193 extern char *crypt __P((const char *, const char *));
195 /* Prototypes for procedures local to this file. */
197 static void network_phase __P((int));
198 static void check_idle __P((void *));
199 static void connect_time_expired __P((void *));
200 static int plogin __P((char *, char *, char **));
201 static void plogout __P((void));
202 static int null_login __P((int));
203 static int get_pap_passwd __P((char *));
204 static int have_pap_secret __P((int *));
205 static int have_chap_secret __P((char *, char *, int, int *));
206 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
207 static int scan_authfile __P((FILE *, char *, char *, char *,
208 struct wordlist **, struct wordlist **,
210 static void free_wordlist __P((struct wordlist *));
211 static void auth_script __P((char *));
212 static void auth_script_done __P((void *));
213 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
214 static int some_ip_ok __P((struct wordlist *));
215 static int setupapfile __P((char **));
216 static int privgroup __P((char **));
217 static int set_noauth_addr __P((char **));
218 static void check_access __P((FILE *, char *));
219 static int wordlist_count __P((struct wordlist *));
222 * Authentication-related options.
224 option_t auth_options[] = {
225 { "auth", o_bool, &auth_required,
226 "Require authentication from peer", OPT_PRIO | 1 },
227 { "noauth", o_bool, &auth_required,
228 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV | OPT_A2COPY,
230 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
231 "Require PAP authentication from peer",
232 OPT_PRIOSUB | OPT_A2COPY | 1, &auth_required },
233 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
234 "Require PAP authentication from peer",
235 OPT_ALIAS | OPT_PRIOSUB | OPT_A2COPY | 1, &auth_required },
236 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
237 "Require CHAP authentication from peer",
238 OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MD5,
239 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
240 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
241 "Require CHAP authentication from peer",
242 OPT_ALIAS | OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MD5,
243 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
245 { "require-mschap", o_bool, &lcp_wantoptions[0].neg_chap,
246 "Require MS-CHAP authentication from peer",
247 OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MICROSOFT,
248 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
249 { "+mschap", o_bool, &lcp_wantoptions[0].neg_chap,
250 "Require MS-CHAP authentication from peer",
251 OPT_ALIAS | OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MICROSOFT,
252 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
253 { "require-mschap-v2", o_bool, &lcp_wantoptions[0].neg_chap,
254 "Require MS-CHAPv2 authentication from peer",
255 OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MICROSOFT_V2,
256 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
257 { "+mschap-v2", o_bool, &lcp_wantoptions[0].neg_chap,
258 "Require MS-CHAPv2 authentication from peer",
259 OPT_ALIAS | OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MICROSOFT_V2,
260 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
263 { "refuse-pap", o_bool, &refuse_pap,
264 "Don't agree to auth to peer with PAP", 1 },
265 { "-pap", o_bool, &refuse_pap,
266 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
267 { "refuse-chap", o_bool, &refuse_chap,
268 "Don't agree to auth to peer with CHAP", OPT_A2CLRB | MDTYPE_MD5,
269 &lcp_allowoptions[0].chap_mdtype },
270 { "-chap", o_bool, &refuse_chap,
271 "Don't allow CHAP authentication with peer",
272 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
273 &lcp_allowoptions[0].chap_mdtype },
275 { "refuse-mschap", o_bool, &refuse_mschap,
276 "Don't agree to auth to peer with MS-CHAP",
277 OPT_A2CLRB | MDTYPE_MICROSOFT, &lcp_allowoptions[0].chap_mdtype },
278 { "-mschap", o_bool, &refuse_mschap,
279 "Don't allow MS-CHAP authentication with peer",
280 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
281 &lcp_allowoptions[0].chap_mdtype },
282 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
283 "Don't agree to auth to peer with MS-CHAPv2",
284 OPT_A2CLRB | MDTYPE_MICROSOFT_V2, &lcp_allowoptions[0].chap_mdtype },
285 { "-mschap-v2", o_bool, &refuse_mschap_v2,
286 "Don't allow MS-CHAPv2 authentication with peer",
287 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
288 &lcp_allowoptions[0].chap_mdtype },
291 { "name", o_string, our_name,
292 "Set local name for authentication",
293 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
295 { "+ua", o_special, (void *)setupapfile,
296 "Get PAP user and password from file",
297 OPT_PRIO | OPT_A2STRVAL, &uafname },
299 { "user", o_string, user,
300 "Set name for auth with peer", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN },
302 { "password", o_string, passwd,
303 "Password for authenticating us to the peer",
304 OPT_PRIO | OPT_STATIC | OPT_HIDE, NULL, MAXSECRETLEN },
306 { "usehostname", o_bool, &usehostname,
307 "Must use hostname for authentication", 1 },
309 { "remotename", o_string, remote_name,
310 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
311 &explicit_remote, MAXNAMELEN },
313 { "login", o_bool, &uselogin,
314 "Use system password database for PAP", 1 },
316 { "papcrypt", o_bool, &cryptpap,
317 "PAP passwords are encrypted", 1 },
319 { "privgroup", o_special, (void *)privgroup,
320 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
322 { "allow-ip", o_special, (void *)set_noauth_addr,
323 "Set IP address(es) which can be used without authentication",
324 OPT_PRIV | OPT_A2LIST },
330 * setupapfile - specifies UPAP info for authenticating with peer.
338 char u[MAXNAMELEN], p[MAXSECRETLEN];
341 lcp_allowoptions[0].neg_upap = 1;
343 /* open user info file */
344 fname = strdup(*argv);
346 novm("+ua file name");
348 ufile = fopen(fname, "r");
351 option_error("unable to open user login data file %s", fname);
354 check_access(ufile, fname);
358 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
359 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL){
360 option_error("unable to read user login data file %s", fname);
365 /* get rid of newlines */
367 if (l > 0 && u[l-1] == '\n')
370 if (l > 0 && p[l-1] == '\n')
373 if (override_value("user", option_priority, fname))
374 strlcpy(user, u, sizeof(user));
375 if (override_value("passwd", option_priority, fname))
376 strlcpy(passwd, p, sizeof(passwd));
383 * privgroup - allow members of the group to have privileged access.
394 option_error("group %s is unknown", *argv);
397 for (i = 0; i < ngroups; ++i) {
398 if (groups[i] == g->gr_gid) {
408 * set_noauth_addr - set address(es) that can be used without authentication.
409 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
412 set_noauth_addr(argv)
416 int l = strlen(addr) + 1;
419 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
421 novm("allow-ip argument");
422 wp->word = (char *) (wp + 1);
423 wp->next = noauth_addrs;
424 BCOPY(addr, wp->word, l);
431 * An Open on LCP has requested a change from Dead to Establish phase.
432 * Do what's necessary to bring the physical layer up.
441 * LCP has terminated the link; go to the Dead phase and take the
442 * physical layer down.
445 link_terminated(unit)
448 if (phase == PHASE_DEAD)
450 if (pap_logout_hook) {
456 new_phase(PHASE_DEAD);
457 notice("Connection terminated.");
461 * LCP has gone down; it will either die or try to re-establish.
468 struct protent *protp;
470 notify(link_down_notifier, 0);
472 if (auth_script_state == s_up && auth_script_pid == 0) {
473 update_link_stats(unit);
474 auth_script_state = s_down;
475 auth_script(_PATH_AUTHDOWN);
477 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
478 if (!protp->enabled_flag)
480 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
481 (*protp->lowerdown)(unit);
482 if (protp->protocol < 0xC000 && protp->close != NULL)
483 (*protp->close)(unit, "LCP down");
487 if (phase != PHASE_DEAD)
488 new_phase(PHASE_ESTABLISH);
492 * The link is established.
493 * Proceed to the Dead, Authenticate or Network phase as appropriate.
496 link_established(unit)
500 lcp_options *wo = &lcp_wantoptions[unit];
501 lcp_options *go = &lcp_gotoptions[unit];
502 lcp_options *ho = &lcp_hisoptions[unit];
504 struct protent *protp;
507 * Tell higher-level protocols that LCP is up.
509 for (i = 0; (protp = protocols[i]) != NULL; ++i)
510 if (protp->protocol != PPP_LCP && protp->enabled_flag
511 && protp->lowerup != NULL)
512 (*protp->lowerup)(unit);
514 if (auth_required && !(go->neg_upap || go->neg_chap)) {
516 * We wanted the peer to authenticate itself, and it refused:
517 * if we have some address(es) it can use without auth, fine,
518 * otherwise treat it as though it authenticated with PAP using
519 * a username of "" and a password of "". If that's not OK,
522 if (noauth_addrs != NULL) {
523 set_allowed_addrs(unit, NULL, NULL);
524 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
525 warn("peer refused to authenticate: terminating link");
526 lcp_close(unit, "peer refused to authenticate");
527 status = EXIT_PEER_AUTH_FAILED;
532 new_phase(PHASE_AUTHENTICATE);
536 ChapAuthPeer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
538 } else if (go->neg_upap) {
543 ChapAuthWithPeer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
544 auth |= CHAP_WITHPEER;
545 } else if (ho->neg_upap) {
546 if (passwd[0] == 0) {
547 passwd_from_file = 1;
548 if (!get_pap_passwd(passwd))
549 error("No secret found for PAP login");
551 upap_authwithpeer(unit, user, passwd);
552 auth |= PAP_WITHPEER;
554 auth_pending[unit] = auth;
561 * Proceed to the network phase.
567 lcp_options *go = &lcp_gotoptions[unit];
570 * If the peer had to authenticate, run the auth-up script now.
572 if (go->neg_chap || go->neg_upap) {
573 notify(auth_up_notifier, 0);
575 if (auth_script_state == s_down && auth_script_pid == 0) {
576 auth_script_state = s_up;
577 auth_script(_PATH_AUTHUP);
583 * If we negotiated callback, do it now.
586 new_phase(PHASE_CALLBACK);
587 (*cbcp_protent.open)(unit);
593 * Process extra options from the secrets file
596 options_from_list(extra_options, 1);
597 free_wordlist(extra_options);
607 struct protent *protp;
609 new_phase(PHASE_NETWORK);
611 #ifdef HAVE_MULTILINK
613 if (mp_join_bundle()) {
614 if (updetach && !nodetach)
619 #endif /* HAVE_MULTILINK */
623 set_filters(&pass_filter, &active_filter);
625 for (i = 0; (protp = protocols[i]) != NULL; ++i)
626 if (protp->protocol < 0xC000 && protp->enabled_flag
627 && protp->open != NULL) {
629 if (protp->protocol != PPP_CCP)
633 if (num_np_open == 0)
635 lcp_close(0, "No network protocols running");
639 * The peer has failed to authenticate himself using `protocol'.
642 auth_peer_fail(unit, protocol)
646 * Authentication failure: take the link down
648 lcp_close(unit, "Authentication failed");
649 status = EXIT_PEER_AUTH_FAILED;
653 * The peer has been successfully authenticated using `protocol'.
656 auth_peer_success(unit, protocol, name, namelen)
671 warn("auth_peer_success: unknown protocol %x", protocol);
676 * Save the authenticated name of the peer for later.
678 if (namelen > sizeof(peer_authname) - 1)
679 namelen = sizeof(peer_authname) - 1;
680 BCOPY(name, peer_authname, namelen);
681 peer_authname[namelen] = 0;
682 script_setenv("PEERNAME", peer_authname, 0);
685 * If there is no more authentication still to be done,
686 * proceed to the network (or callback) phase.
688 if ((auth_pending[unit] &= ~bit) == 0)
693 * We have failed to authenticate ourselves to the peer using `protocol'.
696 auth_withpeer_fail(unit, protocol)
699 if (passwd_from_file)
700 BZERO(passwd, MAXSECRETLEN);
702 * We've failed to authenticate ourselves to our peer.
703 * Some servers keep sending CHAP challenges, but there
704 * is no point in persisting without any way to get updated
705 * authentication secrets.
707 lcp_close(unit, "Failed to authenticate ourselves to peer");
708 status = EXIT_AUTH_TOPEER_FAILED;
712 * We have successfully authenticated ourselves with the peer using `protocol'.
715 auth_withpeer_success(unit, protocol)
725 if (passwd_from_file)
726 BZERO(passwd, MAXSECRETLEN);
730 warn("auth_withpeer_success: unknown protocol %x", protocol);
735 * If there is no more authentication still being done,
736 * proceed to the network (or callback) phase.
738 if ((auth_pending[unit] &= ~bit) == 0)
744 * np_up - a network protocol has come up.
752 if (num_np_up == 0) {
754 * At this point we consider that the link has come up successfully.
758 new_phase(PHASE_RUNNING);
760 if (idle_time_hook != 0)
761 tlim = (*idle_time_hook)(NULL);
763 tlim = idle_time_limit;
765 TIMEOUT(check_idle, NULL, tlim);
768 * Set a timeout to close the connection once the maximum
769 * connect time has expired.
772 TIMEOUT(connect_time_expired, 0, maxconnect);
775 * Detach now, if the updetach option was given.
777 if (updetach && !nodetach)
784 * np_down - a network protocol has gone down.
790 if (--num_np_up == 0) {
791 UNTIMEOUT(check_idle, NULL);
792 UNTIMEOUT(connect_time_expired, NULL);
793 new_phase(PHASE_NETWORK);
798 * np_finished - a network protocol has finished using the link.
801 np_finished(unit, proto)
804 if (--num_np_open <= 0) {
805 /* no further use for the link: shut up shop. */
806 lcp_close(0, "No network protocols running");
811 * check_idle - check whether the link has been idle for long
812 * enough that we can shut it down.
818 struct ppp_idle idle;
822 if (!get_idle_time(0, &idle))
824 if (idle_time_hook != 0) {
825 tlim = idle_time_hook(&idle);
827 itime = MIN(idle.xmit_idle, idle.recv_idle);
828 tlim = idle_time_limit - itime;
831 /* link is idle: shut it down. */
832 notice("Terminating connection due to lack of activity.");
833 lcp_close(0, "Link inactive");
835 status = EXIT_IDLE_TIMEOUT;
837 TIMEOUT(check_idle, NULL, tlim);
842 * connect_time_expired - log a message and close the connection.
845 connect_time_expired(arg)
848 info("Connect time expired");
849 lcp_close(0, "Connect time expired"); /* Close connection */
850 status = EXIT_CONNECT_TIME;
854 * auth_check_options - called to check authentication options.
859 lcp_options *wo = &lcp_wantoptions[0];
863 /* Default our_name to hostname, and user to our_name */
864 if (our_name[0] == 0 || usehostname)
865 strlcpy(our_name, hostname, sizeof(our_name));
867 strlcpy(user, our_name, sizeof(user));
870 * If we have a default route, require the peer to authenticate
871 * unless the noauth option was given or the real user is root.
873 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
878 /* If authentication is required, ask peer for CHAP or PAP. */
881 if (!wo->neg_chap && !wo->neg_upap) {
882 wo->neg_chap = 1; wo->chap_mdtype = MDTYPE_ALL;
886 wo->neg_chap = 0; wo->chap_mdtype = MDTYPE_NONE;
891 * Check whether we have appropriate secrets to use
892 * to authenticate the peer.
895 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
896 if (!can_auth && (wo->neg_chap)) {
897 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
898 our_name, 1, &lacks_ip);
901 if (auth_required && !can_auth && noauth_addrs == NULL) {
904 "By default the remote system is required to authenticate itself");
906 "(because this system has a default route to the internet)");
907 } else if (explicit_remote)
909 "The remote system (%s) is required to authenticate itself",
913 "The remote system is required to authenticate itself");
915 "but I couldn't find any suitable secret (password) for it to use to do so.");
918 "(None of the available passwords would let it use an IP address.)");
925 * auth_reset - called when LCP is starting negotiations to recheck
926 * authentication options, i.e. whether we have appropriate secrets
927 * to use for authenticating ourselves and/or the peer.
933 lcp_options *go = &lcp_gotoptions[unit];
934 lcp_options *ao = &lcp_allowoptions[0];
936 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
937 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
939 || have_chap_secret(user, (explicit_remote? remote_name: NULL),
942 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
945 if (!have_chap_secret((explicit_remote? remote_name: NULL),
953 * check_passwd - Check the user name and passwd against the PAP secrets
954 * file. If requested, also check against the system password database,
955 * and login the user if OK.
958 * UPAP_AUTHNAK: Authentication failed.
959 * UPAP_AUTHACK: Authentication succeeded.
960 * In either case, msg points to an appropriate message.
963 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
974 struct wordlist *addrs = NULL, *opts = NULL;
975 char passwd[256], user[256];
976 char secret[MAXWORDLEN];
977 static int attempts = 0;
980 * Make copies of apasswd and auser, then null-terminate them.
981 * If there are unprintable characters in the password, make
984 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
985 slprintf(user, sizeof(user), "%.*v", userlen, auser);
989 * Check if a plugin wants to handle this.
992 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
995 set_allowed_addrs(unit, addrs, opts);
996 BZERO(passwd, sizeof(passwd));
998 free_wordlist(addrs);
1000 free_wordlist(opts);
1002 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1007 * Open the file of pap secrets and scan for a suitable secret
1008 * for authenticating this user.
1010 filename = _PATH_UPAPFILE;
1011 addrs = opts = NULL;
1013 f = fopen(filename, "r");
1015 error("Can't open PAP password file %s: %m", filename);
1018 check_access(f, filename);
1019 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
1020 warn("no PAP secret found for %s", user);
1023 * If the secret is "@login", it means to check
1024 * the password against the login database.
1026 int login_secret = strcmp(secret, "@login") == 0;
1028 if (uselogin || login_secret) {
1029 /* login option or secret is @login */
1030 ret = plogin(user, passwd, msg);
1031 if (ret == UPAP_AUTHNAK)
1032 warn("PAP login failure for %s", user);
1036 if (secret[0] != 0 && !login_secret) {
1037 /* password given in pap-secrets - must match */
1038 if ((cryptpap || strcmp(passwd, secret) != 0)
1039 && strcmp(crypt(passwd, secret), secret) != 0) {
1041 warn("PAP authentication failure for %s", user);
1048 if (ret == UPAP_AUTHNAK) {
1050 *msg = "Login incorrect";
1052 * XXX can we ever get here more than once??
1053 * Frustrate passwd stealer programs.
1054 * Allow 10 tries, but start backing off after 3 (stolen from login).
1055 * On 10'th, drop the connection.
1057 if (attempts++ >= 10) {
1058 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1059 lcp_close(unit, "login failed");
1062 sleep((u_int) (attempts - 3) * 5);
1064 free_wordlist(opts);
1067 attempts = 0; /* Reset count */
1070 set_allowed_addrs(unit, addrs, opts);
1074 free_wordlist(addrs);
1075 BZERO(passwd, sizeof(passwd));
1076 BZERO(secret, sizeof(secret));
1082 * This function is needed for PAM.
1086 /* Static variables used to communicate between the conversation function
1087 * and the server_login function
1089 static char *PAM_username;
1090 static char *PAM_password;
1091 static int PAM_error = 0;
1092 static pam_handle_t *pamh = NULL;
1094 /* PAM conversation function
1095 * Here we assume (for now, at least) that echo on means login name, and
1096 * echo off means password.
1099 static int PAM_conv (int num_msg, const struct pam_message **msg,
1100 struct pam_response **resp, void *appdata_ptr)
1103 struct pam_response *reply = NULL;
1105 #define COPY_STRING(s) (s) ? strdup(s) : NULL
1107 reply = malloc(sizeof(struct pam_response) * num_msg);
1108 if (!reply) return PAM_CONV_ERR;
1110 for (replies = 0; replies < num_msg; replies++) {
1111 switch (msg[replies]->msg_style) {
1112 case PAM_PROMPT_ECHO_ON:
1113 reply[replies].resp_retcode = PAM_SUCCESS;
1114 reply[replies].resp = COPY_STRING(PAM_username);
1115 /* PAM frees resp */
1117 case PAM_PROMPT_ECHO_OFF:
1118 reply[replies].resp_retcode = PAM_SUCCESS;
1119 reply[replies].resp = COPY_STRING(PAM_password);
1120 /* PAM frees resp */
1125 /* ignore it, but pam still wants a NULL response... */
1126 reply[replies].resp_retcode = PAM_SUCCESS;
1127 reply[replies].resp = NULL;
1130 /* Must be an error of some sort... */
1133 return PAM_CONV_ERR;
1140 static struct pam_conv PAM_conversation = {
1144 #endif /* USE_PAM */
1147 * plogin - Check the user name and password against the system
1148 * password database, and login the user if OK.
1151 * UPAP_AUTHNAK: Login failed.
1152 * UPAP_AUTHACK: Login succeeded.
1153 * In either case, msg points to an appropriate message.
1157 plogin(user, passwd, msg)
1167 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1168 if (pam_error != PAM_SUCCESS) {
1169 *msg = (char *) pam_strerror (pamh, pam_error);
1171 return UPAP_AUTHNAK;
1174 * Define the fields for the credential validation
1177 PAM_username = user;
1178 PAM_password = passwd;
1180 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1185 pam_error = pam_authenticate (pamh, PAM_SILENT);
1186 if (pam_error == PAM_SUCCESS && !PAM_error) {
1187 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1188 if (pam_error == PAM_SUCCESS)
1189 pam_error = pam_open_session (pamh, PAM_SILENT);
1192 *msg = (char *) pam_strerror (pamh, pam_error);
1197 reopen_log(); /* apparently the PAM stuff does closelog() */
1198 PAM_username = NULL;
1199 PAM_password = NULL;
1200 if (pam_error != PAM_SUCCESS)
1201 return UPAP_AUTHNAK;
1202 #else /* #ifdef USE_PAM */
1205 * Use the non-PAM methods directly
1210 struct spwd *getspnam();
1212 struct passwd *pw = getpwnam(user);
1216 return (UPAP_AUTHNAK);
1219 spwd = getspnam(user);
1222 /* check the age of the password entry */
1223 long now = time(NULL) / 86400L;
1225 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1226 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1227 && spwd->sp_lstchg >= 0
1228 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1229 warn("Password for %s has expired", user);
1230 return (UPAP_AUTHNAK);
1232 pw->pw_passwd = spwd->sp_pwdp;
1237 * If no passwd, don't let them login.
1239 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1240 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1241 return (UPAP_AUTHNAK);
1243 #endif /* #ifdef USE_PAM */
1246 * Write a wtmp entry for this user.
1250 if (strncmp(tty, "/dev/", 5) == 0)
1252 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1254 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1255 if (pw != (struct passwd *)NULL) {
1259 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1260 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1261 memset((void *)&ll, 0, sizeof(ll));
1262 (void)time(&ll.ll_time);
1263 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1264 (void)write(fd, (char *)&ll, sizeof(ll));
1268 #endif /* _PATH_LASTLOG and not USE_PAM */
1270 info("user %s logged in", user);
1273 return (UPAP_AUTHACK);
1277 * plogout - Logout the user.
1286 pam_error = pam_close_session (pamh, PAM_SILENT);
1287 pam_end (pamh, pam_error);
1290 /* Apparently the pam stuff does closelog(). */
1292 #else /* ! USE_PAM */
1296 if (strncmp(tty, "/dev/", 5) == 0)
1298 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1299 #endif /* ! USE_PAM */
1305 * null_login - Check if a username of "" and a password of "" are
1306 * acceptable, and iff so, set the list of acceptable IP addresses
1316 struct wordlist *addrs, *opts;
1317 char secret[MAXWORDLEN];
1320 * Check if a plugin wants to handle this.
1324 ret = (*null_auth_hook)(&addrs, &opts);
1327 * Open the file of pap secrets and scan for a suitable secret.
1330 filename = _PATH_UPAPFILE;
1332 f = fopen(filename, "r");
1335 check_access(f, filename);
1337 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename);
1338 ret = i >= 0 && secret[0] == 0;
1339 BZERO(secret, sizeof(secret));
1344 set_allowed_addrs(unit, addrs, opts);
1346 free_wordlist(opts);
1348 free_wordlist(addrs);
1355 * get_pap_passwd - get a password for authenticating ourselves with
1356 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1358 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1361 get_pap_passwd(passwd)
1367 char secret[MAXWORDLEN];
1370 * Check whether a plugin wants to supply this.
1372 if (pap_passwd_hook) {
1373 ret = (*pap_passwd_hook)(user, passwd);
1378 filename = _PATH_UPAPFILE;
1379 f = fopen(filename, "r");
1382 check_access(f, filename);
1383 ret = scan_authfile(f, user,
1384 (remote_name[0]? remote_name: NULL),
1385 secret, NULL, NULL, filename);
1390 strlcpy(passwd, secret, MAXSECRETLEN);
1391 BZERO(secret, sizeof(secret));
1397 * have_pap_secret - check whether we have a PAP file with any
1398 * secrets that we could possibly use for authenticating the peer.
1401 have_pap_secret(lacks_ipp)
1407 struct wordlist *addrs;
1409 /* let the plugin decide, if there is one */
1410 if (pap_check_hook) {
1411 ret = (*pap_check_hook)();
1416 filename = _PATH_UPAPFILE;
1417 f = fopen(filename, "r");
1421 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1422 NULL, &addrs, NULL, filename);
1424 if (ret >= 0 && !some_ip_ok(addrs)) {
1430 free_wordlist(addrs);
1437 * have_chap_secret - check whether we have a CHAP file with a
1438 * secret that we could possibly use for authenticating `client'
1439 * on `server'. Either can be the null string, meaning we don't
1440 * know the identity yet.
1443 have_chap_secret(client, server, need_ip, lacks_ipp)
1452 struct wordlist *addrs;
1454 if (chap_check_hook) {
1455 ret = (*chap_check_hook)();
1461 filename = _PATH_CHAPFILE;
1462 f = fopen(filename, "r");
1466 if (client != NULL && client[0] == 0)
1468 else if (server != NULL && server[0] == 0)
1471 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename);
1473 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1479 free_wordlist(addrs);
1486 * get_secret - open the CHAP secret file and return the secret
1487 * for authenticating the given client on the given server.
1488 * (We could be either client or server).
1491 get_secret(unit, client, server, secret, secret_len, am_server)
1502 struct wordlist *addrs, *opts;
1503 char secbuf[MAXWORDLEN];
1505 if (!am_server && passwd[0] != 0) {
1506 strlcpy(secbuf, passwd, sizeof(secbuf));
1507 } else if (!am_server && chap_passwd_hook) {
1508 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1509 error("Unable to obtain CHAP password for %s on %s from plugin",
1514 filename = _PATH_CHAPFILE;
1518 f = fopen(filename, "r");
1520 error("Can't open chap secret file %s: %m", filename);
1523 check_access(f, filename);
1525 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename);
1531 set_allowed_addrs(unit, addrs, opts);
1533 free_wordlist(opts);
1535 free_wordlist(addrs);
1538 len = strlen(secbuf);
1539 if (len > MAXSECRETLEN) {
1540 error("Secret for %s on %s is too long", client, server);
1543 BCOPY(secbuf, secret, len);
1544 BZERO(secbuf, sizeof(secbuf));
1551 * set_allowed_addrs() - set the list of allowed addresses.
1552 * Also looks for `--' indicating options to apply for this peer
1553 * and leaves the following words in extra_options.
1556 set_allowed_addrs(unit, addrs, opts)
1558 struct wordlist *addrs;
1559 struct wordlist *opts;
1562 struct wordlist *ap, **plink;
1563 struct permitted_ip *ip;
1564 char *ptr_word, *ptr_mask;
1567 u_int32_t a, mask, ah, offset;
1568 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1569 u_int32_t suggested_ip = 0;
1571 if (addresses[unit] != NULL)
1572 free(addresses[unit]);
1573 addresses[unit] = NULL;
1574 if (extra_options != NULL)
1575 free_wordlist(extra_options);
1576 extra_options = opts;
1579 * Count the number of IP addresses given.
1581 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1584 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1588 /* temporarily append the noauth_addrs list to addrs */
1589 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1591 *plink = noauth_addrs;
1594 for (ap = addrs; ap != NULL; ap = ap->next) {
1595 /* "-" means no addresses authorized, "*" means any address allowed */
1596 ptr_word = ap->word;
1597 if (strcmp(ptr_word, "-") == 0)
1599 if (strcmp(ptr_word, "*") == 0) {
1601 ip[n].base = ip[n].mask = 0;
1607 if (*ptr_word == '!') {
1612 mask = ~ (u_int32_t) 0;
1614 ptr_mask = strchr (ptr_word, '/');
1615 if (ptr_mask != NULL) {
1619 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1620 if (bit_count <= 0 || bit_count > 32) {
1621 warn("invalid address length %v in auth. address list",
1625 bit_count = 32 - bit_count; /* # bits in host part */
1627 offset = ifunit + 1;
1631 warn("invalid address length syntax: %v", ptr_mask+1);
1638 hp = gethostbyname(ptr_word);
1639 if (hp != NULL && hp->h_addrtype == AF_INET) {
1640 a = *(u_int32_t *)hp->h_addr;
1642 np = getnetbyname (ptr_word);
1643 if (np != NULL && np->n_addrtype == AF_INET) {
1644 a = htonl (*(u_int32_t *)np->n_net);
1645 if (ptr_mask == NULL) {
1646 /* calculate appropriate mask for net */
1649 mask = IN_CLASSA_NET;
1650 else if (IN_CLASSB(ah))
1651 mask = IN_CLASSB_NET;
1652 else if (IN_CLASSC(ah))
1653 mask = IN_CLASSC_NET;
1656 a = inet_addr (ptr_word);
1660 if (ptr_mask != NULL)
1663 if (a == (u_int32_t)-1L) {
1664 warn("unknown host %s in auth. address list", ap->word);
1668 if (offset >= ~mask) {
1669 warn("interface unit %d too large for subnet %v",
1673 a = htonl((ntohl(a) & mask) + offset);
1674 mask = ~(u_int32_t)0;
1676 ip[n].mask = htonl(mask);
1677 ip[n].base = a & ip[n].mask;
1679 if (~mask == 0 && suggested_ip == 0)
1684 ip[n].permit = 0; /* make the last entry forbid all addresses */
1685 ip[n].base = 0; /* to terminate the list */
1688 addresses[unit] = ip;
1691 * If the address given for the peer isn't authorized, or if
1692 * the user hasn't given one, AND there is an authorized address
1693 * which is a single host, then use that if we find one.
1695 if (suggested_ip != 0
1696 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
1697 wo->hisaddr = suggested_ip;
1699 * Do we insist on this address? No, if there are other
1700 * addresses authorized than the suggested one.
1703 wo->accept_remote = 1;
1708 * auth_ip_addr - check whether the peer is authorized to use
1709 * a given IP address. Returns 1 if authorized, 0 otherwise.
1712 auth_ip_addr(unit, addr)
1718 /* don't allow loopback or multicast address */
1719 if (bad_ip_adrs(addr))
1722 if (allowed_address_hook) {
1723 ok = allowed_address_hook(addr);
1724 if (ok >= 0) return ok;
1727 if (addresses[unit] != NULL) {
1728 ok = ip_addr_check(addr, addresses[unit]);
1734 return 0; /* no addresses authorized */
1735 return allow_any_ip || privileged || !have_route_to(addr);
1739 ip_addr_check(addr, addrs)
1741 struct permitted_ip *addrs;
1744 if ((addr & addrs->mask) == addrs->base)
1745 return addrs->permit;
1749 * bad_ip_adrs - return 1 if the IP address is one we don't want
1750 * to use, such as an address in the loopback net or a multicast address.
1751 * addr is in network byte order.
1758 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1759 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1763 * some_ip_ok - check a wordlist to see if it authorizes any
1768 struct wordlist *addrs;
1770 for (; addrs != 0; addrs = addrs->next) {
1771 if (addrs->word[0] == '-')
1773 if (addrs->word[0] != '!')
1774 return 1; /* some IP address is allowed */
1780 * check_access - complain if a secret file has too-liberal permissions.
1783 check_access(f, filename)
1789 if (fstat(fileno(f), &sbuf) < 0) {
1790 warn("cannot stat secret file %s: %m", filename);
1791 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1792 warn("Warning - secret file %s has world and/or group access",
1799 * scan_authfile - Scan an authorization file for a secret suitable
1800 * for authenticating `client' on `server'. The return value is -1
1801 * if no secret is found, otherwise >= 0. The return value has
1802 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1803 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1804 * Any following words on the line up to a "--" (i.e. address authorization
1805 * info) are placed in a wordlist and returned in *addrs. Any
1806 * following words (extra options) are placed in a wordlist and
1807 * returned in *opts.
1808 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1811 scan_authfile(f, client, server, secret, addrs, opts, filename)
1816 struct wordlist **addrs;
1817 struct wordlist **opts;
1821 int got_flag, best_flag;
1823 struct wordlist *ap, *addr_list, *alist, **app;
1824 char word[MAXWORDLEN];
1825 char atfile[MAXWORDLEN];
1826 char lsecret[MAXWORDLEN];
1833 if (!getword(f, word, &newline, filename))
1834 return -1; /* file is empty??? */
1839 * Skip until we find a word at the start of a line.
1841 while (!newline && getword(f, word, &newline, filename))
1844 break; /* got to end of file */
1847 * Got a client - check if it's a match or a wildcard.
1850 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1855 got_flag = NONWILD_CLIENT;
1858 * Now get a server and check if it matches.
1860 if (!getword(f, word, &newline, filename))
1864 if (!ISWILD(word)) {
1865 if (server != NULL && strcmp(word, server) != 0)
1867 got_flag |= NONWILD_SERVER;
1871 * Got some sort of a match - see if it's better than what
1874 if (got_flag <= best_flag)
1880 if (!getword(f, word, &newline, filename))
1885 if (secret != NULL) {
1887 * Special syntax: @/pathname means read secret from file.
1889 if (word[0] == '@' && word[1] == '/') {
1890 strlcpy(atfile, word+1, sizeof(atfile));
1891 if ((sf = fopen(atfile, "r")) == NULL) {
1892 warn("can't open indirect secret file %s", atfile);
1895 check_access(sf, atfile);
1896 if (!getword(sf, word, &xxx, atfile)) {
1897 warn("no secret in indirect secret file %s", atfile);
1903 strlcpy(lsecret, word, sizeof(lsecret));
1907 * Now read address authorization info and make a wordlist.
1911 if (!getword(f, word, &newline, filename) || newline)
1913 ap = (struct wordlist *)
1914 malloc(sizeof(struct wordlist) + strlen(word) + 1);
1916 novm("authorized addresses");
1917 ap->word = (char *) (ap + 1);
1918 strcpy(ap->word, word);
1925 * This is the best so far; remember it.
1927 best_flag = got_flag;
1929 free_wordlist(addr_list);
1932 strlcpy(secret, lsecret, MAXWORDLEN);
1938 /* scan for a -- word indicating the start of options */
1939 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
1940 if (strcmp(ap->word, "--") == 0)
1942 /* ap = start of options */
1944 ap = ap->next; /* first option */
1945 free(*app); /* free the "--" word */
1946 *app = NULL; /* terminate addr list */
1950 else if (ap != NULL)
1954 else if (addr_list != NULL)
1955 free_wordlist(addr_list);
1961 * wordlist_count - return the number of items in a wordlist
1965 struct wordlist *wp;
1969 for (n = 0; wp != NULL; wp = wp->next)
1975 * free_wordlist - release memory allocated for a wordlist.
1979 struct wordlist *wp;
1981 struct wordlist *next;
1983 while (wp != NULL) {
1991 * auth_script_done - called when the auth-up or auth-down script
1995 auth_script_done(arg)
1998 auth_script_pid = 0;
1999 switch (auth_script_state) {
2001 if (auth_state == s_down) {
2002 auth_script_state = s_down;
2003 auth_script(_PATH_AUTHDOWN);
2007 if (auth_state == s_up) {
2008 auth_script_state = s_up;
2009 auth_script(_PATH_AUTHUP);
2016 * auth_script - execute a script with arguments
2017 * interface-name peer-name real-user tty speed
2029 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2030 user_name = pw->pw_name;
2032 slprintf(struid, sizeof(struid), "%d", getuid());
2035 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2039 argv[2] = peer_authname;
2040 argv[3] = user_name;
2045 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);