2 * auth.c - PPP authentication and phase control.
4 * Copyright (c) 1993-2002 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 * Derived from main.c, which is:
37 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
51 * 3. The name "Carnegie Mellon University" must not be used to
52 * endorse or promote products derived from this software without
53 * prior written permission. For permission or any legal
54 * details, please contact
55 * Office of Technology Transfer
56 * Carnegie Mellon University
58 * Pittsburgh, PA 15213-3890
59 * (412) 268-4387, fax: (412) 268-7395
60 * tech-transfer@andrew.cmu.edu
62 * 4. Redistributions of any form whatsoever must retain the following
64 * "This product includes software developed by Computing Services
65 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
67 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
68 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
69 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
70 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
71 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
72 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
73 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
76 #define RCSID "$Id: auth.c,v 1.98 2004/10/31 22:23:18 paulus Exp $"
85 #include <sys/types.h>
87 #include <sys/socket.h>
90 #if defined(_PATH_LASTLOG) && defined(__linux__)
95 #include <netinet/in.h>
96 #include <arpa/inet.h>
99 #include <security/pam_appl.h>
105 #define PW_PPP PW_LOGIN
117 #include "chap-new.h"
122 #include "pathnames.h"
124 static const char rcsid[] = RCSID;
126 /* Bits in scan_authfile return value */
127 #define NONWILD_SERVER 1
128 #define NONWILD_CLIENT 2
130 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
132 /* The name by which the peer authenticated itself to us. */
133 char peer_authname[MAXNAMELEN];
135 /* Records which authentication operations haven't completed yet. */
136 static int auth_pending[NUM_PPP];
138 /* Records which authentication operations have been completed. */
139 int auth_done[NUM_PPP];
141 /* Set if we have successfully called plogin() */
142 static int logged_in;
144 /* List of addresses which the peer may use. */
145 static struct permitted_ip *addresses[NUM_PPP];
147 /* Wordlist giving addresses which the peer may use
148 without authenticating itself. */
149 static struct wordlist *noauth_addrs;
151 /* Remote telephone number, if available */
152 char remote_number[MAXNAMELEN];
154 /* Wordlist giving remote telephone numbers which may connect. */
155 static struct wordlist *permitted_numbers;
157 /* Extra options to apply, from the secrets file entry for the peer. */
158 static struct wordlist *extra_options;
160 /* Number of network protocols which we have opened. */
161 static int num_np_open;
163 /* Number of network protocols which have come up. */
164 static int num_np_up;
166 /* Set if we got the contents of passwd[] from the pap-secrets file. */
167 static int passwd_from_file;
169 /* Set if we require authentication only because we have a default route. */
170 static bool default_auth;
172 /* Hook to enable a plugin to control the idle time limit */
173 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
175 /* Hook for a plugin to say whether we can possibly authenticate any peer */
176 int (*pap_check_hook) __P((void)) = NULL;
178 /* Hook for a plugin to check the PAP user and password */
179 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
180 struct wordlist **paddrs,
181 struct wordlist **popts)) = NULL;
183 /* Hook for a plugin to know about the PAP user logout */
184 void (*pap_logout_hook) __P((void)) = NULL;
186 /* Hook for a plugin to get the PAP password for authenticating us */
187 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
189 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
190 int (*chap_check_hook) __P((void)) = NULL;
192 /* Hook for a plugin to get the CHAP password for authenticating us */
193 int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
195 /* Hook for a plugin to say whether it is OK if the peer
196 refuses to authenticate. */
197 int (*null_auth_hook) __P((struct wordlist **paddrs,
198 struct wordlist **popts)) = NULL;
200 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
202 /* A notifier for when the peer has authenticated itself,
203 and we are proceeding to the network phase. */
204 struct notifier *auth_up_notifier = NULL;
206 /* A notifier for when the link goes down. */
207 struct notifier *link_down_notifier = NULL;
210 * This is used to ensure that we don't start an auth-up/down
211 * script while one is already running.
218 static enum script_state auth_state = s_down;
219 static enum script_state auth_script_state = s_down;
220 static pid_t auth_script_pid = 0;
222 static int used_login; /* peer authenticated against login database */
227 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
228 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
229 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
230 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
231 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
233 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
234 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
236 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
237 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
239 bool usehostname = 0; /* Use hostname for our_name */
240 bool auth_required = 0; /* Always require authentication from peer */
241 bool allow_any_ip = 0; /* Allow peer to use any IP address */
242 bool explicit_remote = 0; /* User specified explicit remote name */
243 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
245 static char *uafname; /* name of most recent +ua file */
247 extern char *crypt __P((const char *, const char *));
249 /* Prototypes for procedures local to this file. */
251 static void network_phase __P((int));
252 static void check_idle __P((void *));
253 static void connect_time_expired __P((void *));
254 static int plogin __P((char *, char *, char **));
255 static void plogout __P((void));
256 static int null_login __P((int));
257 static int get_pap_passwd __P((char *));
258 static int have_pap_secret __P((int *));
259 static int have_chap_secret __P((char *, char *, int, int *));
260 static int have_srp_secret __P((char *client, char *server, int need_ip,
262 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
263 static int scan_authfile __P((FILE *, char *, char *, char *,
264 struct wordlist **, struct wordlist **,
266 static void free_wordlist __P((struct wordlist *));
267 static void auth_script __P((char *));
268 static void auth_script_done __P((void *));
269 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
270 static int some_ip_ok __P((struct wordlist *));
271 static int setupapfile __P((char **));
272 static int privgroup __P((char **));
273 static int set_noauth_addr __P((char **));
274 static int set_permitted_number __P((char **));
275 static void check_access __P((FILE *, char *));
276 static int wordlist_count __P((struct wordlist *));
279 static void check_maxoctets __P((void *));
283 * Authentication-related options.
285 option_t auth_options[] = {
286 { "auth", o_bool, &auth_required,
287 "Require authentication from peer", OPT_PRIO | 1 },
288 { "noauth", o_bool, &auth_required,
289 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
291 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
292 "Require PAP authentication from peer",
293 OPT_PRIOSUB | 1, &auth_required },
294 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
295 "Require PAP authentication from peer",
296 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
297 { "require-chap", o_bool, &auth_required,
298 "Require CHAP authentication from peer",
299 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
300 &lcp_wantoptions[0].chap_mdtype },
301 { "+chap", o_bool, &auth_required,
302 "Require CHAP authentication from peer",
303 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
304 &lcp_wantoptions[0].chap_mdtype },
306 { "require-mschap", o_bool, &auth_required,
307 "Require MS-CHAP authentication from peer",
308 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
309 &lcp_wantoptions[0].chap_mdtype },
310 { "+mschap", o_bool, &auth_required,
311 "Require MS-CHAP authentication from peer",
312 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
313 &lcp_wantoptions[0].chap_mdtype },
314 { "require-mschap-v2", o_bool, &auth_required,
315 "Require MS-CHAPv2 authentication from peer",
316 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
317 &lcp_wantoptions[0].chap_mdtype },
318 { "+mschap-v2", o_bool, &auth_required,
319 "Require MS-CHAPv2 authentication from peer",
320 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
321 &lcp_wantoptions[0].chap_mdtype },
324 { "refuse-pap", o_bool, &refuse_pap,
325 "Don't agree to auth to peer with PAP", 1 },
326 { "-pap", o_bool, &refuse_pap,
327 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
328 { "refuse-chap", o_bool, &refuse_chap,
329 "Don't agree to auth to peer with CHAP",
330 OPT_A2CLRB | MDTYPE_MD5,
331 &lcp_allowoptions[0].chap_mdtype },
332 { "-chap", o_bool, &refuse_chap,
333 "Don't allow CHAP authentication with peer",
334 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
335 &lcp_allowoptions[0].chap_mdtype },
337 { "refuse-mschap", o_bool, &refuse_mschap,
338 "Don't agree to auth to peer with MS-CHAP",
339 OPT_A2CLRB | MDTYPE_MICROSOFT,
340 &lcp_allowoptions[0].chap_mdtype },
341 { "-mschap", o_bool, &refuse_mschap,
342 "Don't allow MS-CHAP authentication with peer",
343 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
344 &lcp_allowoptions[0].chap_mdtype },
345 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
346 "Don't agree to auth to peer with MS-CHAPv2",
347 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
348 &lcp_allowoptions[0].chap_mdtype },
349 { "-mschap-v2", o_bool, &refuse_mschap_v2,
350 "Don't allow MS-CHAPv2 authentication with peer",
351 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
352 &lcp_allowoptions[0].chap_mdtype },
355 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
356 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
358 { "refuse-eap", o_bool, &refuse_eap,
359 "Don't agree to authenticate to peer with EAP", 1 },
361 { "name", o_string, our_name,
362 "Set local name for authentication",
363 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
365 { "+ua", o_special, (void *)setupapfile,
366 "Get PAP user and password from file",
367 OPT_PRIO | OPT_A2STRVAL, &uafname },
369 { "user", o_string, user,
370 "Set name for auth with peer", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN },
372 { "password", o_string, passwd,
373 "Password for authenticating us to the peer",
374 OPT_PRIO | OPT_STATIC | OPT_HIDE, NULL, MAXSECRETLEN },
376 { "usehostname", o_bool, &usehostname,
377 "Must use hostname for authentication", 1 },
379 { "remotename", o_string, remote_name,
380 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
381 &explicit_remote, MAXNAMELEN },
383 { "login", o_bool, &uselogin,
384 "Use system password database for PAP", 1 },
386 { "papcrypt", o_bool, &cryptpap,
387 "PAP passwords are encrypted", 1 },
389 { "privgroup", o_special, (void *)privgroup,
390 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
392 { "allow-ip", o_special, (void *)set_noauth_addr,
393 "Set IP address(es) which can be used without authentication",
394 OPT_PRIV | OPT_A2LIST },
396 { "remotenumber", o_string, remote_number,
397 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
400 { "allow-number", o_special, (void *)set_permitted_number,
401 "Set telephone number(s) which are allowed to connect",
402 OPT_PRIV | OPT_A2LIST },
408 * setupapfile - specifies UPAP info for authenticating with peer.
416 char u[MAXNAMELEN], p[MAXSECRETLEN];
419 lcp_allowoptions[0].neg_upap = 1;
421 /* open user info file */
422 fname = strdup(*argv);
424 novm("+ua file name");
426 ufile = fopen(fname, "r");
429 option_error("unable to open user login data file %s", fname);
432 check_access(ufile, fname);
436 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
437 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
439 option_error("unable to read user login data file %s", fname);
444 /* get rid of newlines */
446 if (l > 0 && u[l-1] == '\n')
449 if (l > 0 && p[l-1] == '\n')
452 if (override_value("user", option_priority, fname))
453 strlcpy(user, u, sizeof(user));
454 if (override_value("passwd", option_priority, fname))
455 strlcpy(passwd, p, sizeof(passwd));
462 * privgroup - allow members of the group to have privileged access.
473 option_error("group %s is unknown", *argv);
476 for (i = 0; i < ngroups; ++i) {
477 if (groups[i] == g->gr_gid) {
487 * set_noauth_addr - set address(es) that can be used without authentication.
488 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
491 set_noauth_addr(argv)
495 int l = strlen(addr) + 1;
498 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
500 novm("allow-ip argument");
501 wp->word = (char *) (wp + 1);
502 wp->next = noauth_addrs;
503 BCOPY(addr, wp->word, l);
510 * set_permitted_number - set remote telephone number(s) that may connect.
513 set_permitted_number(argv)
516 char *number = *argv;
517 int l = strlen(number) + 1;
520 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
522 novm("allow-number argument");
523 wp->word = (char *) (wp + 1);
524 wp->next = permitted_numbers;
525 BCOPY(number, wp->word, l);
526 permitted_numbers = wp;
532 * An Open on LCP has requested a change from Dead to Establish phase.
533 * Do what's necessary to bring the physical layer up.
542 * LCP has terminated the link; go to the Dead phase and take the
543 * physical layer down.
546 link_terminated(unit)
549 if (phase == PHASE_DEAD)
551 if (pap_logout_hook) {
557 new_phase(PHASE_DEAD);
558 notice("Connection terminated.");
562 * LCP has gone down; it will either die or try to re-establish.
569 struct protent *protp;
571 notify(link_down_notifier, 0);
573 if (auth_script_state == s_up && auth_script_pid == 0) {
574 update_link_stats(unit);
575 auth_script_state = s_down;
576 auth_script(_PATH_AUTHDOWN);
578 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
579 if (!protp->enabled_flag)
581 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
582 (*protp->lowerdown)(unit);
583 if (protp->protocol < 0xC000 && protp->close != NULL)
584 (*protp->close)(unit, "LCP down");
588 if (phase != PHASE_DEAD)
589 new_phase(PHASE_ESTABLISH);
593 * The link is established.
594 * Proceed to the Dead, Authenticate or Network phase as appropriate.
597 link_established(unit)
601 lcp_options *wo = &lcp_wantoptions[unit];
602 lcp_options *go = &lcp_gotoptions[unit];
603 lcp_options *ho = &lcp_hisoptions[unit];
605 struct protent *protp;
608 * Tell higher-level protocols that LCP is up.
610 for (i = 0; (protp = protocols[i]) != NULL; ++i)
611 if (protp->protocol != PPP_LCP && protp->enabled_flag
612 && protp->lowerup != NULL)
613 (*protp->lowerup)(unit);
615 if (!auth_required && noauth_addrs != NULL)
616 set_allowed_addrs(unit, NULL, NULL);
618 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
620 * We wanted the peer to authenticate itself, and it refused:
621 * if we have some address(es) it can use without auth, fine,
622 * otherwise treat it as though it authenticated with PAP using
623 * a username of "" and a password of "". If that's not OK,
626 if (noauth_addrs != NULL) {
627 set_allowed_addrs(unit, NULL, NULL);
628 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
629 warn("peer refused to authenticate: terminating link");
630 lcp_close(unit, "peer refused to authenticate");
631 status = EXIT_PEER_AUTH_FAILED;
636 new_phase(PHASE_AUTHENTICATE);
640 eap_authpeer(unit, our_name);
642 } else if (go->neg_chap) {
643 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
645 } else if (go->neg_upap) {
650 eap_authwithpeer(unit, user);
651 auth |= EAP_WITHPEER;
652 } else if (ho->neg_chap) {
653 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
654 auth |= CHAP_WITHPEER;
655 } else if (ho->neg_upap) {
656 if (passwd[0] == 0) {
657 passwd_from_file = 1;
658 if (!get_pap_passwd(passwd))
659 error("No secret found for PAP login");
661 upap_authwithpeer(unit, user, passwd);
662 auth |= PAP_WITHPEER;
664 auth_pending[unit] = auth;
672 * Proceed to the network phase.
678 lcp_options *go = &lcp_gotoptions[unit];
680 /* Log calling number. */
682 notice("peer from calling number %q authorized", remote_number);
685 * If the peer had to authenticate, run the auth-up script now.
687 if (go->neg_chap || go->neg_upap || go->neg_eap) {
688 notify(auth_up_notifier, 0);
690 if (auth_script_state == s_down && auth_script_pid == 0) {
691 auth_script_state = s_up;
692 auth_script(_PATH_AUTHUP);
698 * If we negotiated callback, do it now.
701 new_phase(PHASE_CALLBACK);
702 (*cbcp_protent.open)(unit);
708 * Process extra options from the secrets file
711 options_from_list(extra_options, 1);
712 free_wordlist(extra_options);
715 start_networks(unit);
723 struct protent *protp;
724 int ecp_required, mppe_required;
726 new_phase(PHASE_NETWORK);
728 #ifdef HAVE_MULTILINK
730 if (mp_join_bundle()) {
731 if (updetach && !nodetach)
736 #endif /* HAVE_MULTILINK */
740 set_filters(&pass_filter, &active_filter);
742 /* Start CCP and ECP */
743 for (i = 0; (protp = protocols[i]) != NULL; ++i)
744 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
745 && protp->enabled_flag && protp->open != NULL)
749 * Bring up other network protocols iff encryption is not required.
751 ecp_required = ecp_gotoptions[unit].required;
752 mppe_required = ccp_gotoptions[unit].mppe;
753 if (!ecp_required && !mppe_required)
754 continue_networks(unit);
758 continue_networks(unit)
762 struct protent *protp;
765 * Start the "real" network protocols.
767 for (i = 0; (protp = protocols[i]) != NULL; ++i)
768 if (protp->protocol < 0xC000
769 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
770 && protp->enabled_flag && protp->open != NULL) {
775 if (num_np_open == 0)
777 lcp_close(0, "No network protocols running");
781 * The peer has failed to authenticate himself using `protocol'.
784 auth_peer_fail(unit, protocol)
788 * Authentication failure: take the link down
790 lcp_close(unit, "Authentication failed");
791 status = EXIT_PEER_AUTH_FAILED;
795 * The peer has been successfully authenticated using `protocol'.
798 auth_peer_success(unit, protocol, prot_flavor, name, namelen)
799 int unit, protocol, prot_flavor;
808 switch (prot_flavor) {
810 bit |= CHAP_MD5_PEER;
816 case CHAP_MICROSOFT_V2:
817 bit |= CHAP_MS2_PEER;
829 warn("auth_peer_success: unknown protocol %x", protocol);
834 * Save the authenticated name of the peer for later.
836 if (namelen > sizeof(peer_authname) - 1)
837 namelen = sizeof(peer_authname) - 1;
838 BCOPY(name, peer_authname, namelen);
839 peer_authname[namelen] = 0;
840 script_setenv("PEERNAME", peer_authname, 0);
842 /* Save the authentication method for later. */
843 auth_done[unit] |= bit;
846 * If there is no more authentication still to be done,
847 * proceed to the network (or callback) phase.
849 if ((auth_pending[unit] &= ~bit) == 0)
854 * We have failed to authenticate ourselves to the peer using `protocol'.
857 auth_withpeer_fail(unit, protocol)
860 if (passwd_from_file)
861 BZERO(passwd, MAXSECRETLEN);
863 * We've failed to authenticate ourselves to our peer.
864 * Some servers keep sending CHAP challenges, but there
865 * is no point in persisting without any way to get updated
866 * authentication secrets.
868 lcp_close(unit, "Failed to authenticate ourselves to peer");
869 status = EXIT_AUTH_TOPEER_FAILED;
873 * We have successfully authenticated ourselves with the peer using `protocol'.
876 auth_withpeer_success(unit, protocol, prot_flavor)
877 int unit, protocol, prot_flavor;
884 switch (prot_flavor) {
886 bit |= CHAP_MD5_WITHPEER;
890 bit |= CHAP_MS_WITHPEER;
892 case CHAP_MICROSOFT_V2:
893 bit |= CHAP_MS2_WITHPEER;
899 if (passwd_from_file)
900 BZERO(passwd, MAXSECRETLEN);
907 warn("auth_withpeer_success: unknown protocol %x", protocol);
911 /* Save the authentication method for later. */
912 auth_done[unit] |= bit;
915 * If there is no more authentication still being done,
916 * proceed to the network (or callback) phase.
918 if ((auth_pending[unit] &= ~bit) == 0)
924 * np_up - a network protocol has come up.
932 if (num_np_up == 0) {
934 * At this point we consider that the link has come up successfully.
938 new_phase(PHASE_RUNNING);
940 if (idle_time_hook != 0)
941 tlim = (*idle_time_hook)(NULL);
943 tlim = idle_time_limit;
945 TIMEOUT(check_idle, NULL, tlim);
948 * Set a timeout to close the connection once the maximum
949 * connect time has expired.
952 TIMEOUT(connect_time_expired, 0, maxconnect);
956 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
960 * Detach now, if the updetach option was given.
962 if (updetach && !nodetach)
969 * np_down - a network protocol has gone down.
975 if (--num_np_up == 0) {
976 UNTIMEOUT(check_idle, NULL);
977 UNTIMEOUT(connect_time_expired, NULL);
979 UNTIMEOUT(check_maxoctets, NULL);
981 new_phase(PHASE_NETWORK);
986 * np_finished - a network protocol has finished using the link.
989 np_finished(unit, proto)
992 if (--num_np_open <= 0) {
993 /* no further use for the link: shut up shop. */
994 lcp_close(0, "No network protocols running");
1000 check_maxoctets(arg)
1006 update_link_stats(ifunit);
1009 switch(maxoctets_dir) {
1010 case PPP_OCTETS_DIRECTION_IN:
1011 used = link_stats.bytes_in;
1013 case PPP_OCTETS_DIRECTION_OUT:
1014 used = link_stats.bytes_out;
1016 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1017 case PPP_OCTETS_DIRECTION_MAXSESSION:
1018 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
1021 used = link_stats.bytes_in+link_stats.bytes_out;
1024 diff = maxoctets - used;
1026 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1027 lcp_close(0, "Traffic limit");
1029 status = EXIT_TRAFFIC_LIMIT;
1031 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1037 * check_idle - check whether the link has been idle for long
1038 * enough that we can shut it down.
1044 struct ppp_idle idle;
1048 if (!get_idle_time(0, &idle))
1050 if (idle_time_hook != 0) {
1051 tlim = idle_time_hook(&idle);
1053 itime = MIN(idle.xmit_idle, idle.recv_idle);
1054 tlim = idle_time_limit - itime;
1057 /* link is idle: shut it down. */
1058 notice("Terminating connection due to lack of activity.");
1059 lcp_close(0, "Link inactive");
1061 status = EXIT_IDLE_TIMEOUT;
1063 TIMEOUT(check_idle, NULL, tlim);
1068 * connect_time_expired - log a message and close the connection.
1071 connect_time_expired(arg)
1074 info("Connect time expired");
1075 status = EXIT_CONNECT_TIME;
1076 lcp_close(0, "Connect time expired"); /* Close connection */
1080 * auth_check_options - called to check authentication options.
1083 auth_check_options()
1085 lcp_options *wo = &lcp_wantoptions[0];
1089 /* Default our_name to hostname, and user to our_name */
1090 if (our_name[0] == 0 || usehostname)
1091 strlcpy(our_name, hostname, sizeof(our_name));
1093 strlcpy(user, our_name, sizeof(user));
1096 * If we have a default route, require the peer to authenticate
1097 * unless the noauth option was given or the real user is root.
1099 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1104 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1105 if (wo->chap_mdtype)
1108 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1109 if (auth_required) {
1111 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1113 wo->chap_mdtype = chap_mdtype_all;
1119 wo->chap_mdtype = MDTYPE_NONE;
1125 * Check whether we have appropriate secrets to use
1126 * to authenticate the peer. Note that EAP can authenticate by way
1127 * of a CHAP-like exchanges as well as SRP.
1130 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1131 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1132 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1133 our_name, 1, &lacks_ip);
1135 if (!can_auth && wo->neg_eap) {
1136 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1137 our_name, 1, &lacks_ip);
1140 if (auth_required && !can_auth && noauth_addrs == NULL) {
1143 "By default the remote system is required to authenticate itself");
1145 "(because this system has a default route to the internet)");
1146 } else if (explicit_remote)
1148 "The remote system (%s) is required to authenticate itself",
1152 "The remote system is required to authenticate itself");
1154 "but I couldn't find any suitable secret (password) for it to use to do so.");
1157 "(None of the available passwords would let it use an IP address.)");
1163 * Early check for remote number authorization.
1165 if (!auth_number()) {
1166 warn("calling number %q is not authorized", remote_number);
1167 exit(EXIT_CNID_AUTH_FAILED);
1172 * auth_reset - called when LCP is starting negotiations to recheck
1173 * authentication options, i.e. whether we have appropriate secrets
1174 * to use for authenticating ourselves and/or the peer.
1180 lcp_options *go = &lcp_gotoptions[unit];
1181 lcp_options *ao = &lcp_allowoptions[unit];
1185 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1186 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1187 && (passwd[0] != 0 ||
1188 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1190 ao->neg_eap = !refuse_eap && (
1192 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1193 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1194 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL));
1197 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1200 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1201 our_name, 1, NULL)))
1205 (hadchap == 0 || (hadchap == -1 &&
1206 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1208 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1215 * check_passwd - Check the user name and passwd against the PAP secrets
1216 * file. If requested, also check against the system password database,
1217 * and login the user if OK.
1220 * UPAP_AUTHNAK: Authentication failed.
1221 * UPAP_AUTHACK: Authentication succeeded.
1222 * In either case, msg points to an appropriate message.
1225 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1236 struct wordlist *addrs = NULL, *opts = NULL;
1237 char passwd[256], user[256];
1238 char secret[MAXWORDLEN];
1239 static int attempts = 0;
1242 * Make copies of apasswd and auser, then null-terminate them.
1243 * If there are unprintable characters in the password, make
1246 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1247 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1251 * Check if a plugin wants to handle this.
1253 if (pap_auth_hook) {
1254 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1256 /* note: set_allowed_addrs() saves opts (but not addrs):
1259 set_allowed_addrs(unit, addrs, opts);
1261 free_wordlist(opts);
1263 free_wordlist(addrs);
1264 BZERO(passwd, sizeof(passwd));
1265 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1270 * Open the file of pap secrets and scan for a suitable secret
1271 * for authenticating this user.
1273 filename = _PATH_UPAPFILE;
1274 addrs = opts = NULL;
1276 f = fopen(filename, "r");
1278 error("Can't open PAP password file %s: %m", filename);
1281 check_access(f, filename);
1282 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1283 warn("no PAP secret found for %s", user);
1286 * If the secret is "@login", it means to check
1287 * the password against the login database.
1289 int login_secret = strcmp(secret, "@login") == 0;
1291 if (uselogin || login_secret) {
1292 /* login option or secret is @login */
1293 if ((ret = plogin(user, passwd, msg)) == UPAP_AUTHACK)
1296 if (secret[0] != 0 && !login_secret) {
1297 /* password given in pap-secrets - must match */
1298 if ((cryptpap || strcmp(passwd, secret) != 0)
1299 && strcmp(crypt(passwd, secret), secret) != 0)
1306 if (ret == UPAP_AUTHNAK) {
1308 *msg = "Login incorrect";
1310 * XXX can we ever get here more than once??
1311 * Frustrate passwd stealer programs.
1312 * Allow 10 tries, but start backing off after 3 (stolen from login).
1313 * On 10'th, drop the connection.
1315 if (attempts++ >= 10) {
1316 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1317 lcp_close(unit, "login failed");
1320 sleep((u_int) (attempts - 3) * 5);
1322 free_wordlist(opts);
1325 attempts = 0; /* Reset count */
1328 set_allowed_addrs(unit, addrs, opts);
1332 free_wordlist(addrs);
1333 BZERO(passwd, sizeof(passwd));
1334 BZERO(secret, sizeof(secret));
1340 * This function is needed for PAM.
1344 /* Static variables used to communicate between the conversation function
1345 * and the server_login function
1347 static char *PAM_username;
1348 static char *PAM_password;
1349 static int PAM_error = 0;
1350 static pam_handle_t *pamh = NULL;
1352 /* PAM conversation function
1353 * Here we assume (for now, at least) that echo on means login name, and
1354 * echo off means password.
1357 static int PAM_conv (int num_msg,
1361 struct pam_message **msg,
1362 struct pam_response **resp, void *appdata_ptr)
1365 struct pam_response *reply = NULL;
1367 #define COPY_STRING(s) (s) ? strdup(s) : NULL
1369 reply = malloc(sizeof(struct pam_response) * num_msg);
1370 if (!reply) return PAM_CONV_ERR;
1372 for (replies = 0; replies < num_msg; replies++) {
1373 switch (msg[replies]->msg_style) {
1374 case PAM_PROMPT_ECHO_ON:
1375 reply[replies].resp_retcode = PAM_SUCCESS;
1376 reply[replies].resp = COPY_STRING(PAM_username);
1377 /* PAM frees resp */
1379 case PAM_PROMPT_ECHO_OFF:
1380 reply[replies].resp_retcode = PAM_SUCCESS;
1381 reply[replies].resp = COPY_STRING(PAM_password);
1382 /* PAM frees resp */
1387 /* ignore it, but pam still wants a NULL response... */
1388 reply[replies].resp_retcode = PAM_SUCCESS;
1389 reply[replies].resp = NULL;
1392 /* Must be an error of some sort... */
1395 return PAM_CONV_ERR;
1402 static struct pam_conv PAM_conversation = {
1406 #endif /* USE_PAM */
1409 * plogin - Check the user name and password against the system
1410 * password database, and login the user if OK.
1413 * UPAP_AUTHNAK: Login failed.
1414 * UPAP_AUTHACK: Login succeeded.
1415 * In either case, msg points to an appropriate message.
1419 plogin(user, passwd, msg)
1429 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1430 if (pam_error != PAM_SUCCESS) {
1431 *msg = (char *) pam_strerror (pamh, pam_error);
1433 return UPAP_AUTHNAK;
1436 * Define the fields for the credential validation
1439 PAM_username = user;
1440 PAM_password = passwd;
1442 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1447 pam_error = pam_authenticate (pamh, PAM_SILENT);
1448 if (pam_error == PAM_SUCCESS && !PAM_error) {
1449 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1450 if (pam_error == PAM_SUCCESS)
1451 pam_error = pam_open_session (pamh, PAM_SILENT);
1454 *msg = (char *) pam_strerror (pamh, pam_error);
1459 reopen_log(); /* apparently the PAM stuff does closelog() */
1460 PAM_username = NULL;
1461 PAM_password = NULL;
1462 if (pam_error != PAM_SUCCESS)
1463 return UPAP_AUTHNAK;
1464 #else /* #ifdef USE_PAM */
1467 * Use the non-PAM methods directly
1472 struct spwd *getspnam();
1474 struct passwd *pw = getpwnam(user);
1478 return (UPAP_AUTHNAK);
1481 spwd = getspnam(user);
1484 /* check the age of the password entry */
1485 long now = time(NULL) / 86400L;
1487 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1488 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1489 && spwd->sp_lstchg >= 0
1490 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1491 warn("Password for %s has expired", user);
1492 return (UPAP_AUTHNAK);
1494 pw->pw_passwd = spwd->sp_pwdp;
1499 * If no passwd, don't let them login.
1501 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1502 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1503 return (UPAP_AUTHNAK);
1505 #endif /* #ifdef USE_PAM */
1508 * Write a wtmp entry for this user.
1512 if (strncmp(tty, "/dev/", 5) == 0)
1514 logwtmp(tty, user, ifname); /* Add wtmp login entry */
1516 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1517 if (pw != (struct passwd *)NULL) {
1521 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1522 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1523 memset((void *)&ll, 0, sizeof(ll));
1524 (void)time(&ll.ll_time);
1525 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1526 (void)write(fd, (char *)&ll, sizeof(ll));
1530 #endif /* _PATH_LASTLOG and not USE_PAM */
1532 info("user %s logged in", user);
1535 return (UPAP_AUTHACK);
1539 * plogout - Logout the user.
1548 pam_error = pam_close_session (pamh, PAM_SILENT);
1549 pam_end (pamh, pam_error);
1552 /* Apparently the pam stuff does closelog(). */
1554 #else /* ! USE_PAM */
1558 if (strncmp(tty, "/dev/", 5) == 0)
1560 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1561 #endif /* ! USE_PAM */
1567 * null_login - Check if a username of "" and a password of "" are
1568 * acceptable, and iff so, set the list of acceptable IP addresses
1578 struct wordlist *addrs, *opts;
1579 char secret[MAXWORDLEN];
1582 * Check if a plugin wants to handle this.
1586 ret = (*null_auth_hook)(&addrs, &opts);
1589 * Open the file of pap secrets and scan for a suitable secret.
1592 filename = _PATH_UPAPFILE;
1594 f = fopen(filename, "r");
1597 check_access(f, filename);
1599 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1600 ret = i >= 0 && secret[0] == 0;
1601 BZERO(secret, sizeof(secret));
1606 set_allowed_addrs(unit, addrs, opts);
1608 free_wordlist(opts);
1610 free_wordlist(addrs);
1617 * get_pap_passwd - get a password for authenticating ourselves with
1618 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1620 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1623 get_pap_passwd(passwd)
1629 char secret[MAXWORDLEN];
1632 * Check whether a plugin wants to supply this.
1634 if (pap_passwd_hook) {
1635 ret = (*pap_passwd_hook)(user, passwd);
1640 filename = _PATH_UPAPFILE;
1641 f = fopen(filename, "r");
1644 check_access(f, filename);
1645 ret = scan_authfile(f, user,
1646 (remote_name[0]? remote_name: NULL),
1647 secret, NULL, NULL, filename, 0);
1652 strlcpy(passwd, secret, MAXSECRETLEN);
1653 BZERO(secret, sizeof(secret));
1659 * have_pap_secret - check whether we have a PAP file with any
1660 * secrets that we could possibly use for authenticating the peer.
1663 have_pap_secret(lacks_ipp)
1669 struct wordlist *addrs;
1671 /* let the plugin decide, if there is one */
1672 if (pap_check_hook) {
1673 ret = (*pap_check_hook)();
1678 filename = _PATH_UPAPFILE;
1679 f = fopen(filename, "r");
1683 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1684 NULL, &addrs, NULL, filename, 0);
1686 if (ret >= 0 && !some_ip_ok(addrs)) {
1692 free_wordlist(addrs);
1699 * have_chap_secret - check whether we have a CHAP file with a
1700 * secret that we could possibly use for authenticating `client'
1701 * on `server'. Either can be the null string, meaning we don't
1702 * know the identity yet.
1705 have_chap_secret(client, server, need_ip, lacks_ipp)
1714 struct wordlist *addrs;
1716 if (chap_check_hook) {
1717 ret = (*chap_check_hook)();
1723 filename = _PATH_CHAPFILE;
1724 f = fopen(filename, "r");
1728 if (client != NULL && client[0] == 0)
1730 else if (server != NULL && server[0] == 0)
1733 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1735 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1741 free_wordlist(addrs);
1748 * have_srp_secret - check whether we have a SRP file with a
1749 * secret that we could possibly use for authenticating `client'
1750 * on `server'. Either can be the null string, meaning we don't
1751 * know the identity yet.
1754 have_srp_secret(client, server, need_ip, lacks_ipp)
1763 struct wordlist *addrs;
1765 filename = _PATH_SRPFILE;
1766 f = fopen(filename, "r");
1770 if (client != NULL && client[0] == 0)
1772 else if (server != NULL && server[0] == 0)
1775 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1777 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1783 free_wordlist(addrs);
1790 * get_secret - open the CHAP secret file and return the secret
1791 * for authenticating the given client on the given server.
1792 * (We could be either client or server).
1795 get_secret(unit, client, server, secret, secret_len, am_server)
1806 struct wordlist *addrs, *opts;
1807 char secbuf[MAXWORDLEN];
1809 if (!am_server && passwd[0] != 0) {
1810 strlcpy(secbuf, passwd, sizeof(secbuf));
1811 } else if (!am_server && chap_passwd_hook) {
1812 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1813 error("Unable to obtain CHAP password for %s on %s from plugin",
1818 filename = _PATH_CHAPFILE;
1822 f = fopen(filename, "r");
1824 error("Can't open chap secret file %s: %m", filename);
1827 check_access(f, filename);
1829 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1835 set_allowed_addrs(unit, addrs, opts);
1837 free_wordlist(opts);
1839 free_wordlist(addrs);
1842 len = strlen(secbuf);
1843 if (len > MAXSECRETLEN) {
1844 error("Secret for %s on %s is too long", client, server);
1847 BCOPY(secbuf, secret, len);
1848 BZERO(secbuf, sizeof(secbuf));
1856 * get_srp_secret - open the SRP secret file and return the secret
1857 * for authenticating the given client on the given server.
1858 * (We could be either client or server).
1861 get_srp_secret(unit, client, server, secret, am_server)
1871 struct wordlist *addrs, *opts;
1873 if (!am_server && passwd[0] != '\0') {
1874 strlcpy(secret, passwd, MAXWORDLEN);
1876 filename = _PATH_SRPFILE;
1879 fp = fopen(filename, "r");
1881 error("Can't open srp secret file %s: %m", filename);
1884 check_access(fp, filename);
1887 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1888 filename, am_server);
1894 set_allowed_addrs(unit, addrs, opts);
1895 else if (opts != NULL)
1896 free_wordlist(opts);
1898 free_wordlist(addrs);
1905 * set_allowed_addrs() - set the list of allowed addresses.
1906 * Also looks for `--' indicating options to apply for this peer
1907 * and leaves the following words in extra_options.
1910 set_allowed_addrs(unit, addrs, opts)
1912 struct wordlist *addrs;
1913 struct wordlist *opts;
1916 struct wordlist *ap, **plink;
1917 struct permitted_ip *ip;
1918 char *ptr_word, *ptr_mask;
1921 u_int32_t a, mask, ah, offset;
1922 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1923 u_int32_t suggested_ip = 0;
1925 if (addresses[unit] != NULL)
1926 free(addresses[unit]);
1927 addresses[unit] = NULL;
1928 if (extra_options != NULL)
1929 free_wordlist(extra_options);
1930 extra_options = opts;
1933 * Count the number of IP addresses given.
1935 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1938 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1942 /* temporarily append the noauth_addrs list to addrs */
1943 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1945 *plink = noauth_addrs;
1948 for (ap = addrs; ap != NULL; ap = ap->next) {
1949 /* "-" means no addresses authorized, "*" means any address allowed */
1950 ptr_word = ap->word;
1951 if (strcmp(ptr_word, "-") == 0)
1953 if (strcmp(ptr_word, "*") == 0) {
1955 ip[n].base = ip[n].mask = 0;
1961 if (*ptr_word == '!') {
1966 mask = ~ (u_int32_t) 0;
1968 ptr_mask = strchr (ptr_word, '/');
1969 if (ptr_mask != NULL) {
1973 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1974 if (bit_count <= 0 || bit_count > 32) {
1975 warn("invalid address length %v in auth. address list",
1979 bit_count = 32 - bit_count; /* # bits in host part */
1981 offset = ifunit + 1;
1985 warn("invalid address length syntax: %v", ptr_mask+1);
1992 hp = gethostbyname(ptr_word);
1993 if (hp != NULL && hp->h_addrtype == AF_INET) {
1994 a = *(u_int32_t *)hp->h_addr;
1996 np = getnetbyname (ptr_word);
1997 if (np != NULL && np->n_addrtype == AF_INET) {
1998 a = htonl ((u_int32_t)np->n_net);
1999 if (ptr_mask == NULL) {
2000 /* calculate appropriate mask for net */
2003 mask = IN_CLASSA_NET;
2004 else if (IN_CLASSB(ah))
2005 mask = IN_CLASSB_NET;
2006 else if (IN_CLASSC(ah))
2007 mask = IN_CLASSC_NET;
2010 a = inet_addr (ptr_word);
2014 if (ptr_mask != NULL)
2017 if (a == (u_int32_t)-1L) {
2018 warn("unknown host %s in auth. address list", ap->word);
2022 if (offset >= ~mask) {
2023 warn("interface unit %d too large for subnet %v",
2027 a = htonl((ntohl(a) & mask) + offset);
2028 mask = ~(u_int32_t)0;
2030 ip[n].mask = htonl(mask);
2031 ip[n].base = a & ip[n].mask;
2033 if (~mask == 0 && suggested_ip == 0)
2038 ip[n].permit = 0; /* make the last entry forbid all addresses */
2039 ip[n].base = 0; /* to terminate the list */
2042 addresses[unit] = ip;
2045 * If the address given for the peer isn't authorized, or if
2046 * the user hasn't given one, AND there is an authorized address
2047 * which is a single host, then use that if we find one.
2049 if (suggested_ip != 0
2050 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
2051 wo->hisaddr = suggested_ip;
2053 * Do we insist on this address? No, if there are other
2054 * addresses authorized than the suggested one.
2057 wo->accept_remote = 1;
2062 * auth_ip_addr - check whether the peer is authorized to use
2063 * a given IP address. Returns 1 if authorized, 0 otherwise.
2066 auth_ip_addr(unit, addr)
2072 /* don't allow loopback or multicast address */
2073 if (bad_ip_adrs(addr))
2076 if (allowed_address_hook) {
2077 ok = allowed_address_hook(addr);
2078 if (ok >= 0) return ok;
2081 if (addresses[unit] != NULL) {
2082 ok = ip_addr_check(addr, addresses[unit]);
2088 return 0; /* no addresses authorized */
2089 return allow_any_ip || privileged || !have_route_to(addr);
2093 ip_addr_check(addr, addrs)
2095 struct permitted_ip *addrs;
2098 if ((addr & addrs->mask) == addrs->base)
2099 return addrs->permit;
2103 * bad_ip_adrs - return 1 if the IP address is one we don't want
2104 * to use, such as an address in the loopback net or a multicast address.
2105 * addr is in network byte order.
2112 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2113 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2117 * some_ip_ok - check a wordlist to see if it authorizes any
2122 struct wordlist *addrs;
2124 for (; addrs != 0; addrs = addrs->next) {
2125 if (addrs->word[0] == '-')
2127 if (addrs->word[0] != '!')
2128 return 1; /* some IP address is allowed */
2134 * auth_number - check whether the remote number is allowed to connect.
2135 * Returns 1 if authorized, 0 otherwise.
2140 struct wordlist *wp = permitted_numbers;
2143 /* Allow all if no authorization list. */
2147 /* Allow if we have a match in the authorization list. */
2149 /* trailing '*' wildcard */
2150 l = strlen(wp->word);
2151 if ((wp->word)[l - 1] == '*')
2153 if (!strncasecmp(wp->word, remote_number, l))
2162 * check_access - complain if a secret file has too-liberal permissions.
2165 check_access(f, filename)
2171 if (fstat(fileno(f), &sbuf) < 0) {
2172 warn("cannot stat secret file %s: %m", filename);
2173 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2174 warn("Warning - secret file %s has world and/or group access",
2181 * scan_authfile - Scan an authorization file for a secret suitable
2182 * for authenticating `client' on `server'. The return value is -1
2183 * if no secret is found, otherwise >= 0. The return value has
2184 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2185 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2186 * Any following words on the line up to a "--" (i.e. address authorization
2187 * info) are placed in a wordlist and returned in *addrs. Any
2188 * following words (extra options) are placed in a wordlist and
2189 * returned in *opts.
2190 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2191 * Flags are non-zero if we need two colons in the secret in order to
2195 scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
2200 struct wordlist **addrs;
2201 struct wordlist **opts;
2206 int got_flag, best_flag;
2208 struct wordlist *ap, *addr_list, *alist, **app;
2209 char word[MAXWORDLEN];
2210 char atfile[MAXWORDLEN];
2211 char lsecret[MAXWORDLEN];
2219 if (!getword(f, word, &newline, filename))
2220 return -1; /* file is empty??? */
2225 * Skip until we find a word at the start of a line.
2227 while (!newline && getword(f, word, &newline, filename))
2230 break; /* got to end of file */
2233 * Got a client - check if it's a match or a wildcard.
2236 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2241 got_flag = NONWILD_CLIENT;
2244 * Now get a server and check if it matches.
2246 if (!getword(f, word, &newline, filename))
2250 if (!ISWILD(word)) {
2251 if (server != NULL && strcmp(word, server) != 0)
2253 got_flag |= NONWILD_SERVER;
2257 * Got some sort of a match - see if it's better than what
2260 if (got_flag <= best_flag)
2266 if (!getword(f, word, &newline, filename))
2272 * SRP-SHA1 authenticator should never be reading secrets from
2273 * a file. (Authenticatee may, though.)
2275 if (flags && ((cp = strchr(word, ':')) == NULL ||
2276 strchr(cp + 1, ':') == NULL))
2279 if (secret != NULL) {
2281 * Special syntax: @/pathname means read secret from file.
2283 if (word[0] == '@' && word[1] == '/') {
2284 strlcpy(atfile, word+1, sizeof(atfile));
2285 if ((sf = fopen(atfile, "r")) == NULL) {
2286 warn("can't open indirect secret file %s", atfile);
2289 check_access(sf, atfile);
2290 if (!getword(sf, word, &xxx, atfile)) {
2291 warn("no secret in indirect secret file %s", atfile);
2297 strlcpy(lsecret, word, sizeof(lsecret));
2301 * Now read address authorization info and make a wordlist.
2305 if (!getword(f, word, &newline, filename) || newline)
2307 ap = (struct wordlist *)
2308 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2310 novm("authorized addresses");
2311 ap->word = (char *) (ap + 1);
2312 strcpy(ap->word, word);
2319 * This is the best so far; remember it.
2321 best_flag = got_flag;
2323 free_wordlist(addr_list);
2326 strlcpy(secret, lsecret, MAXWORDLEN);
2332 /* scan for a -- word indicating the start of options */
2333 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2334 if (strcmp(ap->word, "--") == 0)
2336 /* ap = start of options */
2338 ap = ap->next; /* first option */
2339 free(*app); /* free the "--" word */
2340 *app = NULL; /* terminate addr list */
2344 else if (ap != NULL)
2348 else if (addr_list != NULL)
2349 free_wordlist(addr_list);
2355 * wordlist_count - return the number of items in a wordlist
2359 struct wordlist *wp;
2363 for (n = 0; wp != NULL; wp = wp->next)
2369 * free_wordlist - release memory allocated for a wordlist.
2373 struct wordlist *wp;
2375 struct wordlist *next;
2377 while (wp != NULL) {
2385 * auth_script_done - called when the auth-up or auth-down script
2389 auth_script_done(arg)
2392 auth_script_pid = 0;
2393 switch (auth_script_state) {
2395 if (auth_state == s_down) {
2396 auth_script_state = s_down;
2397 auth_script(_PATH_AUTHDOWN);
2401 if (auth_state == s_up) {
2402 auth_script_state = s_up;
2403 auth_script(_PATH_AUTHUP);
2410 * auth_script - execute a script with arguments
2411 * interface-name peer-name real-user tty speed
2423 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2424 user_name = pw->pw_name;
2426 slprintf(struid, sizeof(struid), "%d", getuid());
2429 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2433 argv[2] = peer_authname;
2434 argv[3] = user_name;
2439 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);