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. The name(s) of the authors of this software must not be used to
14 * endorse or promote products derived from this software without
15 * prior written permission.
17 * 3. Redistributions of any form whatsoever must retain the following
19 * "This product includes software developed by Paul Mackerras
20 * <paulus@samba.org>".
22 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 * Derived from main.c, which is:
32 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
46 * 3. The name "Carnegie Mellon University" must not be used to
47 * endorse or promote products derived from this software without
48 * prior written permission. For permission or any legal
49 * details, please contact
50 * Office of Technology Transfer
51 * Carnegie Mellon University
53 * Pittsburgh, PA 15213-3890
54 * (412) 268-4387, fax: (412) 268-7395
55 * tech-transfer@andrew.cmu.edu
57 * 4. Redistributions of any form whatsoever must retain the following
59 * "This product includes software developed by Computing Services
60 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
62 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
63 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
65 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
67 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
68 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 #define RCSID "$Id: auth.c,v 1.117 2008/07/01 12:27:56 paulus Exp $"
81 #include <sys/types.h>
83 #include <sys/socket.h>
86 #if defined(_PATH_LASTLOG) && defined(__linux__)
91 #include <netinet/in.h>
92 #include <arpa/inet.h>
98 #define PW_PPP PW_LOGIN
110 #include "chap-new.h"
115 #include "pathnames.h"
118 static const char rcsid[] = RCSID;
120 /* Bits in scan_authfile return value */
121 #define NONWILD_SERVER 1
122 #define NONWILD_CLIENT 2
124 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
126 /* The name by which the peer authenticated itself to us. */
127 char peer_authname[MAXNAMELEN];
129 /* Records which authentication operations haven't completed yet. */
130 static int auth_pending[NUM_PPP];
132 /* Records which authentication operations have been completed. */
133 int auth_done[NUM_PPP];
135 /* List of addresses which the peer may use. */
136 static struct permitted_ip *addresses[NUM_PPP];
138 /* Wordlist giving addresses which the peer may use
139 without authenticating itself. */
140 static struct wordlist *noauth_addrs;
142 /* Remote telephone number, if available */
143 char remote_number[MAXNAMELEN];
145 /* Wordlist giving remote telephone numbers which may connect. */
146 static struct wordlist *permitted_numbers;
148 /* Extra options to apply, from the secrets file entry for the peer. */
149 static struct wordlist *extra_options;
151 /* Number of network protocols which we have opened. */
152 static int num_np_open;
154 /* Number of network protocols which have come up. */
155 static int num_np_up;
157 /* Set if we got the contents of passwd[] from the pap-secrets file. */
158 static int passwd_from_file;
160 /* Set if we require authentication only because we have a default route. */
161 static bool default_auth;
163 /* Hook to enable a plugin to control the idle time limit */
164 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
166 /* Hook for a plugin to say whether we can possibly authenticate any peer */
167 int (*pap_check_hook) __P((void)) = NULL;
169 /* Hook for a plugin to check the PAP user and password */
170 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
171 struct wordlist **paddrs,
172 struct wordlist **popts)) = NULL;
174 /* Hook for a plugin to know about the PAP user logout */
175 void (*pap_logout_hook) __P((void)) = NULL;
177 /* Hook for a plugin to get the PAP password for authenticating us */
178 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
180 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
181 int (*chap_check_hook) __P((void)) = NULL;
183 /* Hook for a plugin to get the CHAP password for authenticating us */
184 int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
186 /* Hook for a plugin to say whether it is OK if the peer
187 refuses to authenticate. */
188 int (*null_auth_hook) __P((struct wordlist **paddrs,
189 struct wordlist **popts)) = NULL;
191 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
193 #ifdef HAVE_MULTILINK
194 /* Hook for plugin to hear when an interface joins a multilink bundle */
195 void (*multilink_join_hook) __P((void)) = NULL;
198 /* A notifier for when the peer has authenticated itself,
199 and we are proceeding to the network phase. */
200 struct notifier *auth_up_notifier = NULL;
202 /* A notifier for when the link goes down. */
203 struct notifier *link_down_notifier = NULL;
206 * This is used to ensure that we don't start an auth-up/down
207 * script while one is already running.
214 static enum script_state auth_state = s_down;
215 static enum script_state auth_script_state = s_down;
216 static pid_t auth_script_pid = 0;
221 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
222 bool session_mgmt = 0; /* Do session management (login records) */
223 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
224 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
225 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
226 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
228 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
229 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
231 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
232 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
234 bool usehostname = 0; /* Use hostname for our_name */
235 bool auth_required = 0; /* Always require authentication from peer */
236 bool allow_any_ip = 0; /* Allow peer to use any IP address */
237 bool explicit_remote = 0; /* User specified explicit remote name */
238 bool explicit_user = 0; /* Set if "user" option supplied */
239 bool explicit_passwd = 0; /* Set if "password" option supplied */
240 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
242 static char *uafname; /* name of most recent +ua file */
244 extern char *crypt __P((const char *, const char *));
246 /* Prototypes for procedures local to this file. */
248 static void network_phase __P((int));
249 static void check_idle __P((void *));
250 static void connect_time_expired __P((void *));
251 static int null_login __P((int));
252 static int get_pap_passwd __P((char *));
253 static int have_pap_secret __P((int *));
254 static int have_chap_secret __P((char *, char *, int, int *));
255 static int have_srp_secret __P((char *client, char *server, int need_ip,
257 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
258 static int scan_authfile __P((FILE *, char *, char *, char *,
259 struct wordlist **, struct wordlist **,
261 static void free_wordlist __P((struct wordlist *));
262 static void auth_script __P((char *));
263 static void auth_script_done __P((void *));
264 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
265 static int some_ip_ok __P((struct wordlist *));
266 static int setupapfile __P((char **));
267 static int privgroup __P((char **));
268 static int set_noauth_addr __P((char **));
269 static int set_permitted_number __P((char **));
270 static void check_access __P((FILE *, char *));
271 static int wordlist_count __P((struct wordlist *));
274 static void check_maxoctets __P((void *));
278 * Authentication-related options.
280 option_t auth_options[] = {
281 { "auth", o_bool, &auth_required,
282 "Require authentication from peer", OPT_PRIO | 1 },
283 { "noauth", o_bool, &auth_required,
284 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
286 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
287 "Require PAP authentication from peer",
288 OPT_PRIOSUB | 1, &auth_required },
289 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
290 "Require PAP authentication from peer",
291 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
292 { "require-chap", o_bool, &auth_required,
293 "Require CHAP authentication from peer",
294 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
295 &lcp_wantoptions[0].chap_mdtype },
296 { "+chap", o_bool, &auth_required,
297 "Require CHAP authentication from peer",
298 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
299 &lcp_wantoptions[0].chap_mdtype },
301 { "require-mschap", o_bool, &auth_required,
302 "Require MS-CHAP authentication from peer",
303 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
304 &lcp_wantoptions[0].chap_mdtype },
305 { "+mschap", o_bool, &auth_required,
306 "Require MS-CHAP authentication from peer",
307 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
308 &lcp_wantoptions[0].chap_mdtype },
309 { "require-mschap-v2", o_bool, &auth_required,
310 "Require MS-CHAPv2 authentication from peer",
311 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
312 &lcp_wantoptions[0].chap_mdtype },
313 { "+mschap-v2", o_bool, &auth_required,
314 "Require MS-CHAPv2 authentication from peer",
315 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
316 &lcp_wantoptions[0].chap_mdtype },
319 { "refuse-pap", o_bool, &refuse_pap,
320 "Don't agree to auth to peer with PAP", 1 },
321 { "-pap", o_bool, &refuse_pap,
322 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
323 { "refuse-chap", o_bool, &refuse_chap,
324 "Don't agree to auth to peer with CHAP",
325 OPT_A2CLRB | MDTYPE_MD5,
326 &lcp_allowoptions[0].chap_mdtype },
327 { "-chap", o_bool, &refuse_chap,
328 "Don't allow CHAP authentication with peer",
329 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
330 &lcp_allowoptions[0].chap_mdtype },
332 { "refuse-mschap", o_bool, &refuse_mschap,
333 "Don't agree to auth to peer with MS-CHAP",
334 OPT_A2CLRB | MDTYPE_MICROSOFT,
335 &lcp_allowoptions[0].chap_mdtype },
336 { "-mschap", o_bool, &refuse_mschap,
337 "Don't allow MS-CHAP authentication with peer",
338 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
339 &lcp_allowoptions[0].chap_mdtype },
340 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
341 "Don't agree to auth to peer with MS-CHAPv2",
342 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
343 &lcp_allowoptions[0].chap_mdtype },
344 { "-mschap-v2", o_bool, &refuse_mschap_v2,
345 "Don't allow MS-CHAPv2 authentication with peer",
346 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
347 &lcp_allowoptions[0].chap_mdtype },
350 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
351 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
353 { "refuse-eap", o_bool, &refuse_eap,
354 "Don't agree to authenticate to peer with EAP", 1 },
356 { "name", o_string, our_name,
357 "Set local name for authentication",
358 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
360 { "+ua", o_special, (void *)setupapfile,
361 "Get PAP user and password from file",
362 OPT_PRIO | OPT_A2STRVAL, &uafname },
364 { "user", o_string, user,
365 "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
366 &explicit_user, MAXNAMELEN },
368 { "password", o_string, passwd,
369 "Password for authenticating us to the peer",
370 OPT_PRIO | OPT_STATIC | OPT_HIDE,
371 &explicit_passwd, MAXSECRETLEN },
373 { "usehostname", o_bool, &usehostname,
374 "Must use hostname for authentication", 1 },
376 { "remotename", o_string, remote_name,
377 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
378 &explicit_remote, MAXNAMELEN },
380 { "login", o_bool, &uselogin,
381 "Use system password database for PAP", OPT_A2COPY | 1 ,
383 { "enable-session", o_bool, &session_mgmt,
384 "Enable session accounting for remote peers", OPT_PRIV | 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.
417 char u[MAXNAMELEN], p[MAXSECRETLEN];
420 lcp_allowoptions[0].neg_upap = 1;
422 /* open user info file */
423 fname = strdup(*argv);
425 novm("+ua file name");
427 if (seteuid(getuid()) == -1) {
428 option_error("unable to reset uid before opening %s: %m", fname);
431 ufile = fopen(fname, "r");
432 if (seteuid(euid) == -1)
433 fatal("unable to regain privileges: %m");
435 option_error("unable to open user login data file %s", fname);
438 check_access(ufile, fname);
442 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
443 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
445 option_error("unable to read user login data file %s", fname);
450 /* get rid of newlines */
452 if (l > 0 && u[l-1] == '\n')
455 if (l > 0 && p[l-1] == '\n')
458 if (override_value("user", option_priority, fname)) {
459 strlcpy(user, u, sizeof(user));
462 if (override_value("passwd", option_priority, fname)) {
463 strlcpy(passwd, p, sizeof(passwd));
472 * privgroup - allow members of the group to have privileged access.
483 option_error("group %s is unknown", *argv);
486 for (i = 0; i < ngroups; ++i) {
487 if (groups[i] == g->gr_gid) {
497 * set_noauth_addr - set address(es) that can be used without authentication.
498 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
501 set_noauth_addr(argv)
505 int l = strlen(addr) + 1;
508 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
510 novm("allow-ip argument");
511 wp->word = (char *) (wp + 1);
512 wp->next = noauth_addrs;
513 BCOPY(addr, wp->word, l);
520 * set_permitted_number - set remote telephone number(s) that may connect.
523 set_permitted_number(argv)
526 char *number = *argv;
527 int l = strlen(number) + 1;
530 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
532 novm("allow-number argument");
533 wp->word = (char *) (wp + 1);
534 wp->next = permitted_numbers;
535 BCOPY(number, wp->word, l);
536 permitted_numbers = wp;
542 * An Open on LCP has requested a change from Dead to Establish phase.
551 * Bring the link up to the point of being able to do ppp.
553 void start_link(unit)
556 new_phase(PHASE_SERIALCONN);
559 devfd = the_channel->connect();
563 /* set up the serial device as a ppp interface */
565 * N.B. we used to do tdb_writelock/tdb_writeunlock around this
566 * (from establish_ppp to set_ifunit). However, we won't be
567 * doing the set_ifunit in multilink mode, which is the only time
568 * we need the atomicity that the tdb_writelock/tdb_writeunlock
569 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
571 fd_ppp = the_channel->establish_ppp(devfd);
573 status = EXIT_FATAL_ERROR;
577 if (!demand && ifunit >= 0)
581 * Start opening the connection and wait for
582 * incoming events (reply, timeout, etc.).
585 notice("Connect: %s <--> %s", ifname, ppp_devnam);
587 notice("Starting negotiation on %s", ppp_devnam);
590 status = EXIT_NEGOTIATION_FAILED;
591 new_phase(PHASE_ESTABLISH);
597 new_phase(PHASE_DISCONNECT);
598 if (the_channel->disconnect)
599 the_channel->disconnect();
602 new_phase(PHASE_DEAD);
603 if (the_channel->cleanup)
604 (*the_channel->cleanup)();
608 * LCP has terminated the link; go to the Dead phase and take the
609 * physical layer down.
612 link_terminated(unit)
615 if (phase == PHASE_DEAD || phase == PHASE_MASTER)
617 new_phase(PHASE_DISCONNECT);
619 if (pap_logout_hook) {
624 if (!doing_multilink) {
625 notice("Connection terminated.");
628 notice("Link terminated.");
631 * Delete pid files before disestablishing ppp. Otherwise it
632 * can happen that another pppd gets the same unit and then
633 * we delete its pid file.
635 if (!doing_multilink && !demand)
639 * If we may want to bring the link up again, transfer
640 * the ppp unit back to the loopback. Set the
641 * real serial device back to its normal mode of operation.
646 the_channel->disestablish_ppp(devfd);
653 if (!doing_multilink && !demand)
654 script_unsetenv("IFNAME");
657 * Run disconnector script, if requested.
658 * XXX we may not be able to do this if the line has hung up!
660 if (devfd >= 0 && the_channel->disconnect) {
661 the_channel->disconnect();
664 if (the_channel->cleanup)
665 (*the_channel->cleanup)();
667 if (doing_multilink && multilink_master) {
668 if (!bundle_terminating) {
669 new_phase(PHASE_MASTER);
670 if (master_detach && !detached)
673 mp_bundle_terminated();
675 new_phase(PHASE_DEAD);
679 * LCP has gone down; it will either die or try to re-establish.
685 if (auth_state != s_down) {
686 notify(link_down_notifier, 0);
688 if (auth_script_state == s_up && auth_script_pid == 0) {
689 update_link_stats(unit);
690 auth_script_state = s_down;
691 auth_script(_PATH_AUTHDOWN);
694 if (!doing_multilink) {
695 upper_layers_down(unit);
696 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
697 new_phase(PHASE_ESTABLISH);
699 /* XXX if doing_multilink, should do something to stop
700 network-layer traffic on the link */
703 void upper_layers_down(int unit)
706 struct protent *protp;
708 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
709 if (!protp->enabled_flag)
711 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
712 (*protp->lowerdown)(unit);
713 if (protp->protocol < 0xC000 && protp->close != NULL)
714 (*protp->close)(unit, "LCP down");
721 * The link is established.
722 * Proceed to the Dead, Authenticate or Network phase as appropriate.
725 link_established(unit)
729 lcp_options *wo = &lcp_wantoptions[unit];
730 lcp_options *go = &lcp_gotoptions[unit];
731 lcp_options *ho = &lcp_hisoptions[unit];
733 struct protent *protp;
736 * Tell higher-level protocols that LCP is up.
738 if (!doing_multilink) {
739 for (i = 0; (protp = protocols[i]) != NULL; ++i)
740 if (protp->protocol != PPP_LCP && protp->enabled_flag
741 && protp->lowerup != NULL)
742 (*protp->lowerup)(unit);
745 if (!auth_required && noauth_addrs != NULL)
746 set_allowed_addrs(unit, NULL, NULL);
748 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
750 * We wanted the peer to authenticate itself, and it refused:
751 * if we have some address(es) it can use without auth, fine,
752 * otherwise treat it as though it authenticated with PAP using
753 * a username of "" and a password of "". If that's not OK,
756 if (noauth_addrs != NULL) {
757 set_allowed_addrs(unit, NULL, NULL);
758 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
759 warn("peer refused to authenticate: terminating link");
760 status = EXIT_PEER_AUTH_FAILED;
761 lcp_close(unit, "peer refused to authenticate");
766 new_phase(PHASE_AUTHENTICATE);
769 eap_authpeer(unit, our_name);
771 } else if (go->neg_chap) {
772 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
774 } else if (go->neg_upap) {
779 eap_authwithpeer(unit, user);
780 auth |= EAP_WITHPEER;
781 } else if (ho->neg_chap) {
782 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
783 auth |= CHAP_WITHPEER;
784 } else if (ho->neg_upap) {
785 /* If a blank password was explicitly given as an option, trust
786 the user and don't try to look up one. */
787 if (passwd[0] == 0 && !explicit_passwd) {
788 passwd_from_file = 1;
789 if (!get_pap_passwd(passwd))
790 error("No secret found for PAP login");
792 upap_authwithpeer(unit, user, passwd);
793 auth |= PAP_WITHPEER;
795 auth_pending[unit] = auth;
803 * Proceed to the network phase.
809 lcp_options *go = &lcp_gotoptions[unit];
811 /* Log calling number. */
813 notice("peer from calling number %q authorized", remote_number);
816 * If the peer had to authenticate, run the auth-up script now.
818 if (go->neg_chap || go->neg_upap || go->neg_eap) {
819 notify(auth_up_notifier, 0);
821 if (auth_script_state == s_down && auth_script_pid == 0) {
822 auth_script_state = s_up;
823 auth_script(_PATH_AUTHUP);
829 * If we negotiated callback, do it now.
832 new_phase(PHASE_CALLBACK);
833 (*cbcp_protent.open)(unit);
839 * Process extra options from the secrets file
842 options_from_list(extra_options, 1);
843 free_wordlist(extra_options);
846 start_networks(unit);
854 struct protent *protp;
855 int ecp_required, mppe_required;
857 new_phase(PHASE_NETWORK);
859 #ifdef HAVE_MULTILINK
861 if (mp_join_bundle()) {
862 if (multilink_join_hook)
863 (*multilink_join_hook)();
864 if (updetach && !nodetach)
869 #endif /* HAVE_MULTILINK */
873 set_filters(&pass_filter, &active_filter);
875 /* Start CCP and ECP */
876 for (i = 0; (protp = protocols[i]) != NULL; ++i)
877 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
878 && protp->enabled_flag && protp->open != NULL)
882 * Bring up other network protocols iff encryption is not required.
884 ecp_required = ecp_gotoptions[unit].required;
885 mppe_required = ccp_gotoptions[unit].mppe;
886 if (!ecp_required && !mppe_required)
887 continue_networks(unit);
891 continue_networks(unit)
895 struct protent *protp;
898 * Start the "real" network protocols.
900 for (i = 0; (protp = protocols[i]) != NULL; ++i)
901 if (protp->protocol < 0xC000
902 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
903 && protp->enabled_flag && protp->open != NULL) {
908 if (num_np_open == 0)
910 lcp_close(0, "No network protocols running");
914 * The peer has failed to authenticate himself using `protocol'.
917 auth_peer_fail(unit, protocol)
921 * Authentication failure: take the link down
923 status = EXIT_PEER_AUTH_FAILED;
924 lcp_close(unit, "Authentication failed");
928 * The peer has been successfully authenticated using `protocol'.
931 auth_peer_success(unit, protocol, prot_flavor, name, namelen)
932 int unit, protocol, prot_flavor;
941 switch (prot_flavor) {
943 bit |= CHAP_MD5_PEER;
949 case CHAP_MICROSOFT_V2:
950 bit |= CHAP_MS2_PEER;
962 warn("auth_peer_success: unknown protocol %x", protocol);
967 * Save the authenticated name of the peer for later.
969 if (namelen > sizeof(peer_authname) - 1)
970 namelen = sizeof(peer_authname) - 1;
971 BCOPY(name, peer_authname, namelen);
972 peer_authname[namelen] = 0;
973 script_setenv("PEERNAME", peer_authname, 0);
975 /* Save the authentication method for later. */
976 auth_done[unit] |= bit;
979 * If there is no more authentication still to be done,
980 * proceed to the network (or callback) phase.
982 if ((auth_pending[unit] &= ~bit) == 0)
987 * We have failed to authenticate ourselves to the peer using `protocol'.
990 auth_withpeer_fail(unit, protocol)
993 if (passwd_from_file)
994 BZERO(passwd, MAXSECRETLEN);
996 * We've failed to authenticate ourselves to our peer.
997 * Some servers keep sending CHAP challenges, but there
998 * is no point in persisting without any way to get updated
999 * authentication secrets.
1001 status = EXIT_AUTH_TOPEER_FAILED;
1002 lcp_close(unit, "Failed to authenticate ourselves to peer");
1006 * We have successfully authenticated ourselves with the peer using `protocol'.
1009 auth_withpeer_success(unit, protocol, prot_flavor)
1010 int unit, protocol, prot_flavor;
1013 const char *prot = "";
1017 bit = CHAP_WITHPEER;
1019 switch (prot_flavor) {
1021 bit |= CHAP_MD5_WITHPEER;
1024 case CHAP_MICROSOFT:
1025 bit |= CHAP_MS_WITHPEER;
1027 case CHAP_MICROSOFT_V2:
1028 bit |= CHAP_MS2_WITHPEER;
1034 if (passwd_from_file)
1035 BZERO(passwd, MAXSECRETLEN);
1044 warn("auth_withpeer_success: unknown protocol %x", protocol);
1048 notice("%s authentication succeeded", prot);
1050 /* Save the authentication method for later. */
1051 auth_done[unit] |= bit;
1054 * If there is no more authentication still being done,
1055 * proceed to the network (or callback) phase.
1057 if ((auth_pending[unit] &= ~bit) == 0)
1058 network_phase(unit);
1063 * np_up - a network protocol has come up.
1071 if (num_np_up == 0) {
1073 * At this point we consider that the link has come up successfully.
1077 new_phase(PHASE_RUNNING);
1079 if (idle_time_hook != 0)
1080 tlim = (*idle_time_hook)(NULL);
1082 tlim = idle_time_limit;
1084 TIMEOUT(check_idle, NULL, tlim);
1087 * Set a timeout to close the connection once the maximum
1088 * connect time has expired.
1091 TIMEOUT(connect_time_expired, 0, maxconnect);
1095 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1099 * Detach now, if the updetach option was given.
1101 if (updetach && !nodetach)
1108 * np_down - a network protocol has gone down.
1111 np_down(unit, proto)
1114 if (--num_np_up == 0) {
1115 UNTIMEOUT(check_idle, NULL);
1116 UNTIMEOUT(connect_time_expired, NULL);
1118 UNTIMEOUT(check_maxoctets, NULL);
1120 new_phase(PHASE_NETWORK);
1125 * np_finished - a network protocol has finished using the link.
1128 np_finished(unit, proto)
1131 if (--num_np_open <= 0) {
1132 /* no further use for the link: shut up shop. */
1133 lcp_close(0, "No network protocols running");
1139 check_maxoctets(arg)
1144 update_link_stats(ifunit);
1147 switch(maxoctets_dir) {
1148 case PPP_OCTETS_DIRECTION_IN:
1149 used = link_stats.bytes_in;
1151 case PPP_OCTETS_DIRECTION_OUT:
1152 used = link_stats.bytes_out;
1154 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1155 case PPP_OCTETS_DIRECTION_MAXSESSION:
1156 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
1159 used = link_stats.bytes_in+link_stats.bytes_out;
1162 if (used > maxoctets) {
1163 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1164 status = EXIT_TRAFFIC_LIMIT;
1165 lcp_close(0, "Traffic limit");
1168 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1174 * check_idle - check whether the link has been idle for long
1175 * enough that we can shut it down.
1181 struct ppp_idle idle;
1185 if (!get_idle_time(0, &idle))
1187 if (idle_time_hook != 0) {
1188 tlim = idle_time_hook(&idle);
1190 itime = MIN(idle.xmit_idle, idle.recv_idle);
1191 tlim = idle_time_limit - itime;
1194 /* link is idle: shut it down. */
1195 notice("Terminating connection due to lack of activity.");
1196 status = EXIT_IDLE_TIMEOUT;
1197 lcp_close(0, "Link inactive");
1200 TIMEOUT(check_idle, NULL, tlim);
1205 * connect_time_expired - log a message and close the connection.
1208 connect_time_expired(arg)
1211 info("Connect time expired");
1212 status = EXIT_CONNECT_TIME;
1213 lcp_close(0, "Connect time expired"); /* Close connection */
1217 * auth_check_options - called to check authentication options.
1220 auth_check_options()
1222 lcp_options *wo = &lcp_wantoptions[0];
1226 /* Default our_name to hostname, and user to our_name */
1227 if (our_name[0] == 0 || usehostname)
1228 strlcpy(our_name, hostname, sizeof(our_name));
1229 /* If a blank username was explicitly given as an option, trust
1230 the user and don't use our_name */
1231 if (user[0] == 0 && !explicit_user)
1232 strlcpy(user, our_name, sizeof(user));
1235 * If we have a default route, require the peer to authenticate
1236 * unless the noauth option was given or the real user is root.
1238 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1243 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1244 if (wo->chap_mdtype)
1247 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1248 if (auth_required) {
1250 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1251 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
1252 wo->chap_mdtype = chap_mdtype_all;
1258 wo->chap_mdtype = MDTYPE_NONE;
1264 * Check whether we have appropriate secrets to use
1265 * to authenticate the peer. Note that EAP can authenticate by way
1266 * of a CHAP-like exchanges as well as SRP.
1269 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1270 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1271 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1272 our_name, 1, &lacks_ip);
1274 if (!can_auth && wo->neg_eap) {
1275 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1276 our_name, 1, &lacks_ip);
1279 if (auth_required && !can_auth && noauth_addrs == NULL) {
1282 "By default the remote system is required to authenticate itself");
1284 "(because this system has a default route to the internet)");
1285 } else if (explicit_remote)
1287 "The remote system (%s) is required to authenticate itself",
1291 "The remote system is required to authenticate itself");
1293 "but I couldn't find any suitable secret (password) for it to use to do so.");
1296 "(None of the available passwords would let it use an IP address.)");
1302 * Early check for remote number authorization.
1304 if (!auth_number()) {
1305 warn("calling number %q is not authorized", remote_number);
1306 exit(EXIT_CNID_AUTH_FAILED);
1311 * auth_reset - called when LCP is starting negotiations to recheck
1312 * authentication options, i.e. whether we have appropriate secrets
1313 * to use for authenticating ourselves and/or the peer.
1319 lcp_options *go = &lcp_gotoptions[unit];
1320 lcp_options *ao = &lcp_allowoptions[unit];
1324 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1325 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1326 && (passwd[0] != 0 ||
1327 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1329 ao->neg_eap = !refuse_eap && (
1331 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1332 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1333 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL));
1336 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1339 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1340 our_name, 1, NULL)))
1344 (hadchap == 0 || (hadchap == -1 &&
1345 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1347 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1354 * check_passwd - Check the user name and passwd against the PAP secrets
1355 * file. If requested, also check against the system password database,
1356 * and login the user if OK.
1359 * UPAP_AUTHNAK: Authentication failed.
1360 * UPAP_AUTHACK: Authentication succeeded.
1361 * In either case, msg points to an appropriate message.
1364 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1375 struct wordlist *addrs = NULL, *opts = NULL;
1376 char passwd[256], user[256];
1377 char secret[MAXWORDLEN];
1378 static int attempts = 0;
1381 * Make copies of apasswd and auser, then null-terminate them.
1382 * If there are unprintable characters in the password, make
1385 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1386 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1390 * Check if a plugin wants to handle this.
1392 if (pap_auth_hook) {
1393 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1395 /* note: set_allowed_addrs() saves opts (but not addrs):
1398 set_allowed_addrs(unit, addrs, opts);
1400 free_wordlist(opts);
1402 free_wordlist(addrs);
1403 BZERO(passwd, sizeof(passwd));
1404 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1409 * Open the file of pap secrets and scan for a suitable secret
1410 * for authenticating this user.
1412 filename = _PATH_UPAPFILE;
1413 addrs = opts = NULL;
1415 f = fopen(filename, "r");
1417 error("Can't open PAP password file %s: %m", filename);
1420 check_access(f, filename);
1421 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1422 warn("no PAP secret found for %s", user);
1425 * If the secret is "@login", it means to check
1426 * the password against the login database.
1428 int login_secret = strcmp(secret, "@login") == 0;
1430 if (uselogin || login_secret) {
1431 /* login option or secret is @login */
1432 if (session_full(user, passwd, devnam, msg) == 0) {
1435 } else if (session_mgmt) {
1436 if (session_check(user, NULL, devnam, NULL) == 0) {
1437 warn("Peer %q failed PAP Session verification", user);
1441 if (secret[0] != 0 && !login_secret) {
1442 /* password given in pap-secrets - must match */
1443 if (cryptpap || strcmp(passwd, secret) != 0) {
1444 char *cbuf = crypt(passwd, secret);
1445 if (!cbuf || strcmp(cbuf, secret) != 0)
1453 if (ret == UPAP_AUTHNAK) {
1455 *msg = "Login incorrect";
1457 * XXX can we ever get here more than once??
1458 * Frustrate passwd stealer programs.
1459 * Allow 10 tries, but start backing off after 3 (stolen from login).
1460 * On 10'th, drop the connection.
1462 if (attempts++ >= 10) {
1463 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1464 lcp_close(unit, "login failed");
1467 sleep((u_int) (attempts - 3) * 5);
1469 free_wordlist(opts);
1472 attempts = 0; /* Reset count */
1475 set_allowed_addrs(unit, addrs, opts);
1479 free_wordlist(addrs);
1480 BZERO(passwd, sizeof(passwd));
1481 BZERO(secret, sizeof(secret));
1487 * null_login - Check if a username of "" and a password of "" are
1488 * acceptable, and iff so, set the list of acceptable IP addresses
1498 struct wordlist *addrs, *opts;
1499 char secret[MAXWORDLEN];
1502 * Check if a plugin wants to handle this.
1506 ret = (*null_auth_hook)(&addrs, &opts);
1509 * Open the file of pap secrets and scan for a suitable secret.
1512 filename = _PATH_UPAPFILE;
1514 f = fopen(filename, "r");
1517 check_access(f, filename);
1519 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1520 ret = i >= 0 && secret[0] == 0;
1521 BZERO(secret, sizeof(secret));
1526 set_allowed_addrs(unit, addrs, opts);
1528 free_wordlist(opts);
1530 free_wordlist(addrs);
1537 * get_pap_passwd - get a password for authenticating ourselves with
1538 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1540 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1543 get_pap_passwd(passwd)
1549 char secret[MAXWORDLEN];
1552 * Check whether a plugin wants to supply this.
1554 if (pap_passwd_hook) {
1555 ret = (*pap_passwd_hook)(user, passwd);
1560 filename = _PATH_UPAPFILE;
1561 f = fopen(filename, "r");
1564 check_access(f, filename);
1565 ret = scan_authfile(f, user,
1566 (remote_name[0]? remote_name: NULL),
1567 secret, NULL, NULL, filename, 0);
1572 strlcpy(passwd, secret, MAXSECRETLEN);
1573 BZERO(secret, sizeof(secret));
1579 * have_pap_secret - check whether we have a PAP file with any
1580 * secrets that we could possibly use for authenticating the peer.
1583 have_pap_secret(lacks_ipp)
1589 struct wordlist *addrs;
1591 /* let the plugin decide, if there is one */
1592 if (pap_check_hook) {
1593 ret = (*pap_check_hook)();
1598 filename = _PATH_UPAPFILE;
1599 f = fopen(filename, "r");
1603 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1604 NULL, &addrs, NULL, filename, 0);
1606 if (ret >= 0 && !some_ip_ok(addrs)) {
1612 free_wordlist(addrs);
1619 * have_chap_secret - check whether we have a CHAP file with a
1620 * secret that we could possibly use for authenticating `client'
1621 * on `server'. Either can be the null string, meaning we don't
1622 * know the identity yet.
1625 have_chap_secret(client, server, need_ip, lacks_ipp)
1634 struct wordlist *addrs;
1636 if (chap_check_hook) {
1637 ret = (*chap_check_hook)();
1643 filename = _PATH_CHAPFILE;
1644 f = fopen(filename, "r");
1648 if (client != NULL && client[0] == 0)
1650 else if (server != NULL && server[0] == 0)
1653 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1655 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1661 free_wordlist(addrs);
1668 * have_srp_secret - check whether we have a SRP file with a
1669 * secret that we could possibly use for authenticating `client'
1670 * on `server'. Either can be the null string, meaning we don't
1671 * know the identity yet.
1674 have_srp_secret(client, server, need_ip, lacks_ipp)
1683 struct wordlist *addrs;
1685 filename = _PATH_SRPFILE;
1686 f = fopen(filename, "r");
1690 if (client != NULL && client[0] == 0)
1692 else if (server != NULL && server[0] == 0)
1695 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1697 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1703 free_wordlist(addrs);
1710 * get_secret - open the CHAP secret file and return the secret
1711 * for authenticating the given client on the given server.
1712 * (We could be either client or server).
1715 get_secret(unit, client, server, secret, secret_len, am_server)
1726 struct wordlist *addrs, *opts;
1727 char secbuf[MAXWORDLEN];
1729 if (!am_server && passwd[0] != 0) {
1730 strlcpy(secbuf, passwd, sizeof(secbuf));
1731 } else if (!am_server && chap_passwd_hook) {
1732 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1733 error("Unable to obtain CHAP password for %s on %s from plugin",
1738 filename = _PATH_CHAPFILE;
1742 f = fopen(filename, "r");
1744 error("Can't open chap secret file %s: %m", filename);
1747 check_access(f, filename);
1749 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1755 set_allowed_addrs(unit, addrs, opts);
1757 free_wordlist(opts);
1759 free_wordlist(addrs);
1762 len = strlen(secbuf);
1763 if (len > MAXSECRETLEN) {
1764 error("Secret for %s on %s is too long", client, server);
1767 BCOPY(secbuf, secret, len);
1768 BZERO(secbuf, sizeof(secbuf));
1776 * get_srp_secret - open the SRP secret file and return the secret
1777 * for authenticating the given client on the given server.
1778 * (We could be either client or server).
1781 get_srp_secret(unit, client, server, secret, am_server)
1791 struct wordlist *addrs, *opts;
1793 if (!am_server && passwd[0] != '\0') {
1794 strlcpy(secret, passwd, MAXWORDLEN);
1796 filename = _PATH_SRPFILE;
1799 fp = fopen(filename, "r");
1801 error("Can't open srp secret file %s: %m", filename);
1804 check_access(fp, filename);
1807 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1808 filename, am_server);
1814 set_allowed_addrs(unit, addrs, opts);
1815 else if (opts != NULL)
1816 free_wordlist(opts);
1818 free_wordlist(addrs);
1825 * set_allowed_addrs() - set the list of allowed addresses.
1826 * Also looks for `--' indicating options to apply for this peer
1827 * and leaves the following words in extra_options.
1830 set_allowed_addrs(unit, addrs, opts)
1832 struct wordlist *addrs;
1833 struct wordlist *opts;
1836 struct wordlist *ap, **plink;
1837 struct permitted_ip *ip;
1838 char *ptr_word, *ptr_mask;
1841 u_int32_t a, mask, ah, offset;
1842 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1843 u_int32_t suggested_ip = 0;
1845 if (addresses[unit] != NULL)
1846 free(addresses[unit]);
1847 addresses[unit] = NULL;
1848 if (extra_options != NULL)
1849 free_wordlist(extra_options);
1850 extra_options = opts;
1853 * Count the number of IP addresses given.
1855 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1858 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1862 /* temporarily append the noauth_addrs list to addrs */
1863 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1865 *plink = noauth_addrs;
1868 for (ap = addrs; ap != NULL; ap = ap->next) {
1869 /* "-" means no addresses authorized, "*" means any address allowed */
1870 ptr_word = ap->word;
1871 if (strcmp(ptr_word, "-") == 0)
1873 if (strcmp(ptr_word, "*") == 0) {
1875 ip[n].base = ip[n].mask = 0;
1881 if (*ptr_word == '!') {
1886 mask = ~ (u_int32_t) 0;
1888 ptr_mask = strchr (ptr_word, '/');
1889 if (ptr_mask != NULL) {
1893 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1894 if (bit_count <= 0 || bit_count > 32) {
1895 warn("invalid address length %v in auth. address list",
1899 bit_count = 32 - bit_count; /* # bits in host part */
1901 offset = ifunit + 1;
1905 warn("invalid address length syntax: %v", ptr_mask+1);
1912 hp = gethostbyname(ptr_word);
1913 if (hp != NULL && hp->h_addrtype == AF_INET) {
1914 a = *(u_int32_t *)hp->h_addr;
1916 np = getnetbyname (ptr_word);
1917 if (np != NULL && np->n_addrtype == AF_INET) {
1918 a = htonl ((u_int32_t)np->n_net);
1919 if (ptr_mask == NULL) {
1920 /* calculate appropriate mask for net */
1923 mask = IN_CLASSA_NET;
1924 else if (IN_CLASSB(ah))
1925 mask = IN_CLASSB_NET;
1926 else if (IN_CLASSC(ah))
1927 mask = IN_CLASSC_NET;
1930 a = inet_addr (ptr_word);
1934 if (ptr_mask != NULL)
1937 if (a == (u_int32_t)-1L) {
1938 warn("unknown host %s in auth. address list", ap->word);
1942 if (offset >= ~mask) {
1943 warn("interface unit %d too large for subnet %v",
1947 a = htonl((ntohl(a) & mask) + offset);
1948 mask = ~(u_int32_t)0;
1950 ip[n].mask = htonl(mask);
1951 ip[n].base = a & ip[n].mask;
1953 if (~mask == 0 && suggested_ip == 0)
1958 ip[n].permit = 0; /* make the last entry forbid all addresses */
1959 ip[n].base = 0; /* to terminate the list */
1962 addresses[unit] = ip;
1965 * If the address given for the peer isn't authorized, or if
1966 * the user hasn't given one, AND there is an authorized address
1967 * which is a single host, then use that if we find one.
1969 if (suggested_ip != 0
1970 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
1971 wo->hisaddr = suggested_ip;
1973 * Do we insist on this address? No, if there are other
1974 * addresses authorized than the suggested one.
1977 wo->accept_remote = 1;
1982 * auth_ip_addr - check whether the peer is authorized to use
1983 * a given IP address. Returns 1 if authorized, 0 otherwise.
1986 auth_ip_addr(unit, addr)
1992 /* don't allow loopback or multicast address */
1993 if (bad_ip_adrs(addr))
1996 if (allowed_address_hook) {
1997 ok = allowed_address_hook(addr);
1998 if (ok >= 0) return ok;
2001 if (addresses[unit] != NULL) {
2002 ok = ip_addr_check(addr, addresses[unit]);
2008 return 0; /* no addresses authorized */
2009 return allow_any_ip || privileged || !have_route_to(addr);
2013 ip_addr_check(addr, addrs)
2015 struct permitted_ip *addrs;
2018 if ((addr & addrs->mask) == addrs->base)
2019 return addrs->permit;
2023 * bad_ip_adrs - return 1 if the IP address is one we don't want
2024 * to use, such as an address in the loopback net or a multicast address.
2025 * addr is in network byte order.
2032 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2033 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2037 * some_ip_ok - check a wordlist to see if it authorizes any
2042 struct wordlist *addrs;
2044 for (; addrs != 0; addrs = addrs->next) {
2045 if (addrs->word[0] == '-')
2047 if (addrs->word[0] != '!')
2048 return 1; /* some IP address is allowed */
2054 * auth_number - check whether the remote number is allowed to connect.
2055 * Returns 1 if authorized, 0 otherwise.
2060 struct wordlist *wp = permitted_numbers;
2063 /* Allow all if no authorization list. */
2067 /* Allow if we have a match in the authorization list. */
2069 /* trailing '*' wildcard */
2070 l = strlen(wp->word);
2071 if ((wp->word)[l - 1] == '*')
2073 if (!strncasecmp(wp->word, remote_number, l))
2082 * check_access - complain if a secret file has too-liberal permissions.
2085 check_access(f, filename)
2091 if (fstat(fileno(f), &sbuf) < 0) {
2092 warn("cannot stat secret file %s: %m", filename);
2093 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2094 warn("Warning - secret file %s has world and/or group access",
2101 * scan_authfile - Scan an authorization file for a secret suitable
2102 * for authenticating `client' on `server'. The return value is -1
2103 * if no secret is found, otherwise >= 0. The return value has
2104 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2105 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2106 * Any following words on the line up to a "--" (i.e. address authorization
2107 * info) are placed in a wordlist and returned in *addrs. Any
2108 * following words (extra options) are placed in a wordlist and
2109 * returned in *opts.
2110 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2111 * Flags are non-zero if we need two colons in the secret in order to
2115 scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
2120 struct wordlist **addrs;
2121 struct wordlist **opts;
2126 int got_flag, best_flag;
2128 struct wordlist *ap, *addr_list, *alist, **app;
2129 char word[MAXWORDLEN];
2130 char atfile[MAXWORDLEN];
2131 char lsecret[MAXWORDLEN];
2139 if (!getword(f, word, &newline, filename))
2140 return -1; /* file is empty??? */
2145 * Skip until we find a word at the start of a line.
2147 while (!newline && getword(f, word, &newline, filename))
2150 break; /* got to end of file */
2153 * Got a client - check if it's a match or a wildcard.
2156 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2161 got_flag = NONWILD_CLIENT;
2164 * Now get a server and check if it matches.
2166 if (!getword(f, word, &newline, filename))
2170 if (!ISWILD(word)) {
2171 if (server != NULL && strcmp(word, server) != 0)
2173 got_flag |= NONWILD_SERVER;
2177 * Got some sort of a match - see if it's better than what
2180 if (got_flag <= best_flag)
2186 if (!getword(f, word, &newline, filename))
2192 * SRP-SHA1 authenticator should never be reading secrets from
2193 * a file. (Authenticatee may, though.)
2195 if (flags && ((cp = strchr(word, ':')) == NULL ||
2196 strchr(cp + 1, ':') == NULL))
2199 if (secret != NULL) {
2201 * Special syntax: @/pathname means read secret from file.
2203 if (word[0] == '@' && word[1] == '/') {
2204 strlcpy(atfile, word+1, sizeof(atfile));
2205 if ((sf = fopen(atfile, "r")) == NULL) {
2206 warn("can't open indirect secret file %s", atfile);
2209 check_access(sf, atfile);
2210 if (!getword(sf, word, &xxx, atfile)) {
2211 warn("no secret in indirect secret file %s", atfile);
2217 strlcpy(lsecret, word, sizeof(lsecret));
2221 * Now read address authorization info and make a wordlist.
2225 if (!getword(f, word, &newline, filename) || newline)
2227 ap = (struct wordlist *)
2228 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2230 novm("authorized addresses");
2231 ap->word = (char *) (ap + 1);
2232 strcpy(ap->word, word);
2239 * This is the best so far; remember it.
2241 best_flag = got_flag;
2243 free_wordlist(addr_list);
2246 strlcpy(secret, lsecret, MAXWORDLEN);
2252 /* scan for a -- word indicating the start of options */
2253 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2254 if (strcmp(ap->word, "--") == 0)
2256 /* ap = start of options */
2258 ap = ap->next; /* first option */
2259 free(*app); /* free the "--" word */
2260 *app = NULL; /* terminate addr list */
2264 else if (ap != NULL)
2268 else if (addr_list != NULL)
2269 free_wordlist(addr_list);
2275 * wordlist_count - return the number of items in a wordlist
2279 struct wordlist *wp;
2283 for (n = 0; wp != NULL; wp = wp->next)
2289 * free_wordlist - release memory allocated for a wordlist.
2293 struct wordlist *wp;
2295 struct wordlist *next;
2297 while (wp != NULL) {
2305 * auth_script_done - called when the auth-up or auth-down script
2309 auth_script_done(arg)
2312 auth_script_pid = 0;
2313 switch (auth_script_state) {
2315 if (auth_state == s_down) {
2316 auth_script_state = s_down;
2317 auth_script(_PATH_AUTHDOWN);
2321 if (auth_state == s_up) {
2322 auth_script_state = s_up;
2323 auth_script(_PATH_AUTHUP);
2330 * auth_script - execute a script with arguments
2331 * interface-name peer-name real-user tty speed
2343 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2344 user_name = pw->pw_name;
2346 slprintf(struid, sizeof(struid), "%d", getuid());
2349 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2353 argv[2] = peer_authname;
2354 argv[3] = user_name;
2359 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);