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
104 #include <systemd/sd-daemon.h>
114 #include "chap-new.h"
119 #include "pathnames.h"
122 static const char rcsid[] = RCSID;
124 /* Bits in scan_authfile return value */
125 #define NONWILD_SERVER 1
126 #define NONWILD_CLIENT 2
128 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
130 /* The name by which the peer authenticated itself to us. */
131 char peer_authname[MAXNAMELEN];
133 /* Records which authentication operations haven't completed yet. */
134 static int auth_pending[NUM_PPP];
136 /* Records which authentication operations have been completed. */
137 int auth_done[NUM_PPP];
139 /* List of addresses which the peer may use. */
140 static struct permitted_ip *addresses[NUM_PPP];
142 /* Wordlist giving addresses which the peer may use
143 without authenticating itself. */
144 static struct wordlist *noauth_addrs;
146 /* Remote telephone number, if available */
147 char remote_number[MAXNAMELEN];
149 /* Wordlist giving remote telephone numbers which may connect. */
150 static struct wordlist *permitted_numbers;
152 /* Extra options to apply, from the secrets file entry for the peer. */
153 static struct wordlist *extra_options;
155 /* Number of network protocols which we have opened. */
156 static int num_np_open;
158 /* Number of network protocols which have come up. */
159 static int num_np_up;
161 /* Set if we got the contents of passwd[] from the pap-secrets file. */
162 static int passwd_from_file;
164 /* Set if we require authentication only because we have a default route. */
165 static bool default_auth;
167 /* Hook to enable a plugin to control the idle time limit */
168 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
170 /* Hook for a plugin to say whether we can possibly authenticate any peer */
171 int (*pap_check_hook) __P((void)) = NULL;
173 /* Hook for a plugin to check the PAP user and password */
174 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
175 struct wordlist **paddrs,
176 struct wordlist **popts)) = NULL;
178 /* Hook for a plugin to know about the PAP user logout */
179 void (*pap_logout_hook) __P((void)) = NULL;
181 /* Hook for a plugin to get the PAP password for authenticating us */
182 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
184 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
185 int (*chap_check_hook) __P((void)) = NULL;
187 /* Hook for a plugin to get the CHAP password for authenticating us */
188 int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
190 /* Hook for a plugin to say whether it is OK if the peer
191 refuses to authenticate. */
192 int (*null_auth_hook) __P((struct wordlist **paddrs,
193 struct wordlist **popts)) = NULL;
195 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
197 #ifdef HAVE_MULTILINK
198 /* Hook for plugin to hear when an interface joins a multilink bundle */
199 void (*multilink_join_hook) __P((void)) = 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;
225 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
226 bool session_mgmt = 0; /* Do session management (login records) */
227 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
228 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
229 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
230 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
232 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
233 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
235 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
236 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
238 bool usehostname = 0; /* Use hostname for our_name */
239 bool auth_required = 0; /* Always require authentication from peer */
240 bool allow_any_ip = 0; /* Allow peer to use any IP address */
241 bool explicit_remote = 0; /* User specified explicit remote name */
242 bool explicit_user = 0; /* Set if "user" option supplied */
243 bool explicit_passwd = 0; /* Set if "password" option supplied */
244 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
246 static char *uafname; /* name of most recent +ua file */
248 extern char *crypt __P((const char *, const char *));
250 /* Prototypes for procedures local to this file. */
252 static void network_phase __P((int));
253 static void check_idle __P((void *));
254 static void connect_time_expired __P((void *));
255 static int null_login __P((int));
256 static int get_pap_passwd __P((char *));
257 static int have_pap_secret __P((int *));
258 static int have_chap_secret __P((char *, char *, int, int *));
259 static int have_srp_secret __P((char *client, char *server, int need_ip,
261 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
262 static int scan_authfile __P((FILE *, char *, char *, char *,
263 struct wordlist **, struct wordlist **,
265 static void free_wordlist __P((struct wordlist *));
266 static void auth_script __P((char *));
267 static void auth_script_done __P((void *));
268 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
269 static int some_ip_ok __P((struct wordlist *));
270 static int setupapfile __P((char **));
271 static int privgroup __P((char **));
272 static int set_noauth_addr __P((char **));
273 static int set_permitted_number __P((char **));
274 static void check_access __P((FILE *, char *));
275 static int wordlist_count __P((struct wordlist *));
278 static void check_maxoctets __P((void *));
282 * Authentication-related options.
284 option_t auth_options[] = {
285 { "auth", o_bool, &auth_required,
286 "Require authentication from peer", OPT_PRIO | 1 },
287 { "noauth", o_bool, &auth_required,
288 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
290 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
291 "Require PAP authentication from peer",
292 OPT_PRIOSUB | 1, &auth_required },
293 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
294 "Require PAP authentication from peer",
295 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
296 { "require-chap", o_bool, &auth_required,
297 "Require CHAP authentication from peer",
298 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
299 &lcp_wantoptions[0].chap_mdtype },
300 { "+chap", o_bool, &auth_required,
301 "Require CHAP authentication from peer",
302 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
303 &lcp_wantoptions[0].chap_mdtype },
305 { "require-mschap", o_bool, &auth_required,
306 "Require MS-CHAP authentication from peer",
307 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
308 &lcp_wantoptions[0].chap_mdtype },
309 { "+mschap", o_bool, &auth_required,
310 "Require MS-CHAP authentication from peer",
311 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
312 &lcp_wantoptions[0].chap_mdtype },
313 { "require-mschap-v2", o_bool, &auth_required,
314 "Require MS-CHAPv2 authentication from peer",
315 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
316 &lcp_wantoptions[0].chap_mdtype },
317 { "+mschap-v2", o_bool, &auth_required,
318 "Require MS-CHAPv2 authentication from peer",
319 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
320 &lcp_wantoptions[0].chap_mdtype },
323 { "refuse-pap", o_bool, &refuse_pap,
324 "Don't agree to auth to peer with PAP", 1 },
325 { "-pap", o_bool, &refuse_pap,
326 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
327 { "refuse-chap", o_bool, &refuse_chap,
328 "Don't agree to auth to peer with CHAP",
329 OPT_A2CLRB | MDTYPE_MD5,
330 &lcp_allowoptions[0].chap_mdtype },
331 { "-chap", o_bool, &refuse_chap,
332 "Don't allow CHAP authentication with peer",
333 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
334 &lcp_allowoptions[0].chap_mdtype },
336 { "refuse-mschap", o_bool, &refuse_mschap,
337 "Don't agree to auth to peer with MS-CHAP",
338 OPT_A2CLRB | MDTYPE_MICROSOFT,
339 &lcp_allowoptions[0].chap_mdtype },
340 { "-mschap", o_bool, &refuse_mschap,
341 "Don't allow MS-CHAP authentication with peer",
342 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
343 &lcp_allowoptions[0].chap_mdtype },
344 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
345 "Don't agree to auth to peer with MS-CHAPv2",
346 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
347 &lcp_allowoptions[0].chap_mdtype },
348 { "-mschap-v2", o_bool, &refuse_mschap_v2,
349 "Don't allow MS-CHAPv2 authentication with peer",
350 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
351 &lcp_allowoptions[0].chap_mdtype },
354 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
355 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
357 { "refuse-eap", o_bool, &refuse_eap,
358 "Don't agree to authenticate to peer with EAP", 1 },
360 { "name", o_string, our_name,
361 "Set local name for authentication",
362 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
364 { "+ua", o_special, (void *)setupapfile,
365 "Get PAP user and password from file",
366 OPT_PRIO | OPT_A2STRVAL, &uafname },
368 { "user", o_string, user,
369 "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
370 &explicit_user, MAXNAMELEN },
372 { "password", o_string, passwd,
373 "Password for authenticating us to the peer",
374 OPT_PRIO | OPT_STATIC | OPT_HIDE,
375 &explicit_passwd, MAXSECRETLEN },
377 { "usehostname", o_bool, &usehostname,
378 "Must use hostname for authentication", 1 },
380 { "remotename", o_string, remote_name,
381 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
382 &explicit_remote, MAXNAMELEN },
384 { "login", o_bool, &uselogin,
385 "Use system password database for PAP", OPT_A2COPY | 1 ,
387 { "enable-session", o_bool, &session_mgmt,
388 "Enable session accounting for remote peers", OPT_PRIV | 1 },
390 { "papcrypt", o_bool, &cryptpap,
391 "PAP passwords are encrypted", 1 },
393 { "privgroup", o_special, (void *)privgroup,
394 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
396 { "allow-ip", o_special, (void *)set_noauth_addr,
397 "Set IP address(es) which can be used without authentication",
398 OPT_PRIV | OPT_A2LIST },
400 { "remotenumber", o_string, remote_number,
401 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
404 { "allow-number", o_special, (void *)set_permitted_number,
405 "Set telephone number(s) which are allowed to connect",
406 OPT_PRIV | OPT_A2LIST },
412 * setupapfile - specifies UPAP info for authenticating with peer.
421 char u[MAXNAMELEN], p[MAXSECRETLEN];
424 lcp_allowoptions[0].neg_upap = 1;
426 /* open user info file */
427 fname = strdup(*argv);
429 novm("+ua file name");
431 if (seteuid(getuid()) == -1) {
432 option_error("unable to reset uid before opening %s: %m", fname);
436 ufile = fopen(fname, "r");
437 if (seteuid(euid) == -1)
438 fatal("unable to regain privileges: %m");
440 option_error("unable to open user login data file %s", fname);
444 check_access(ufile, fname);
448 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
449 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
451 option_error("unable to read user login data file %s", fname);
457 /* get rid of newlines */
459 if (l > 0 && u[l-1] == '\n')
462 if (l > 0 && p[l-1] == '\n')
465 if (override_value("user", option_priority, fname)) {
466 strlcpy(user, u, sizeof(user));
469 if (override_value("passwd", option_priority, fname)) {
470 strlcpy(passwd, p, sizeof(passwd));
480 * privgroup - allow members of the group to have privileged access.
491 option_error("group %s is unknown", *argv);
494 for (i = 0; i < ngroups; ++i) {
495 if (groups[i] == g->gr_gid) {
505 * set_noauth_addr - set address(es) that can be used without authentication.
506 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
509 set_noauth_addr(argv)
513 int l = strlen(addr) + 1;
516 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
518 novm("allow-ip argument");
519 wp->word = (char *) (wp + 1);
520 wp->next = noauth_addrs;
521 BCOPY(addr, wp->word, l);
528 * set_permitted_number - set remote telephone number(s) that may connect.
531 set_permitted_number(argv)
534 char *number = *argv;
535 int l = strlen(number) + 1;
538 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
540 novm("allow-number argument");
541 wp->word = (char *) (wp + 1);
542 wp->next = permitted_numbers;
543 BCOPY(number, wp->word, l);
544 permitted_numbers = wp;
550 * An Open on LCP has requested a change from Dead to Establish phase.
559 * Bring the link up to the point of being able to do ppp.
561 void start_link(unit)
564 status = EXIT_CONNECT_FAILED;
565 new_phase(PHASE_SERIALCONN);
568 devfd = the_channel->connect();
572 /* set up the serial device as a ppp interface */
574 * N.B. we used to do tdb_writelock/tdb_writeunlock around this
575 * (from establish_ppp to set_ifunit). However, we won't be
576 * doing the set_ifunit in multilink mode, which is the only time
577 * we need the atomicity that the tdb_writelock/tdb_writeunlock
578 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
580 fd_ppp = the_channel->establish_ppp(devfd);
582 status = EXIT_FATAL_ERROR;
586 if (!demand && ifunit >= 0)
590 * Start opening the connection and wait for
591 * incoming events (reply, timeout, etc.).
594 notice("Connect: %s <--> %s", ifname, ppp_devnam);
596 notice("Starting negotiation on %s", ppp_devnam);
599 status = EXIT_NEGOTIATION_FAILED;
600 new_phase(PHASE_ESTABLISH);
606 new_phase(PHASE_DISCONNECT);
607 if (the_channel->disconnect)
608 the_channel->disconnect();
611 new_phase(PHASE_DEAD);
612 if (the_channel->cleanup)
613 (*the_channel->cleanup)();
617 * LCP has terminated the link; go to the Dead phase and take the
618 * physical layer down.
621 link_terminated(unit)
624 if (phase == PHASE_DEAD || phase == PHASE_MASTER)
626 new_phase(PHASE_DISCONNECT);
628 if (pap_logout_hook) {
633 if (!doing_multilink) {
634 notice("Connection terminated.");
637 notice("Link terminated.");
640 * Delete pid files before disestablishing ppp. Otherwise it
641 * can happen that another pppd gets the same unit and then
642 * we delete its pid file.
644 if (!doing_multilink && !demand)
648 * If we may want to bring the link up again, transfer
649 * the ppp unit back to the loopback. Set the
650 * real serial device back to its normal mode of operation.
655 the_channel->disestablish_ppp(devfd);
662 if (!doing_multilink && !demand)
663 script_unsetenv("IFNAME");
666 * Run disconnector script, if requested.
667 * XXX we may not be able to do this if the line has hung up!
669 if (devfd >= 0 && the_channel->disconnect) {
670 the_channel->disconnect();
673 if (the_channel->cleanup)
674 (*the_channel->cleanup)();
676 if (doing_multilink && multilink_master) {
677 if (!bundle_terminating) {
678 new_phase(PHASE_MASTER);
679 if (master_detach && !detached)
682 mp_bundle_terminated();
684 new_phase(PHASE_DEAD);
688 * LCP has gone down; it will either die or try to re-establish.
694 if (auth_state != s_down) {
695 notify(link_down_notifier, 0);
697 if (auth_script_state == s_up && auth_script_pid == 0) {
698 update_link_stats(unit);
699 auth_script_state = s_down;
700 auth_script(_PATH_AUTHDOWN);
703 if (!doing_multilink) {
704 upper_layers_down(unit);
705 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
706 new_phase(PHASE_ESTABLISH);
708 /* XXX if doing_multilink, should do something to stop
709 network-layer traffic on the link */
712 void upper_layers_down(int unit)
715 struct protent *protp;
717 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
718 if (!protp->enabled_flag)
720 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
721 (*protp->lowerdown)(unit);
722 if (protp->protocol < 0xC000 && protp->close != NULL)
723 (*protp->close)(unit, "LCP down");
730 * The link is established.
731 * Proceed to the Dead, Authenticate or Network phase as appropriate.
734 link_established(unit)
738 lcp_options *wo = &lcp_wantoptions[unit];
739 lcp_options *go = &lcp_gotoptions[unit];
740 lcp_options *ho = &lcp_hisoptions[unit];
742 struct protent *protp;
745 * Tell higher-level protocols that LCP is up.
747 if (!doing_multilink) {
748 for (i = 0; (protp = protocols[i]) != NULL; ++i)
749 if (protp->protocol != PPP_LCP && protp->enabled_flag
750 && protp->lowerup != NULL)
751 (*protp->lowerup)(unit);
754 if (!auth_required && noauth_addrs != NULL)
755 set_allowed_addrs(unit, NULL, NULL);
757 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
759 * We wanted the peer to authenticate itself, and it refused:
760 * if we have some address(es) it can use without auth, fine,
761 * otherwise treat it as though it authenticated with PAP using
762 * a username of "" and a password of "". If that's not OK,
765 if (noauth_addrs != NULL) {
766 set_allowed_addrs(unit, NULL, NULL);
767 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
768 warn("peer refused to authenticate: terminating link");
769 status = EXIT_PEER_AUTH_FAILED;
770 lcp_close(unit, "peer refused to authenticate");
775 new_phase(PHASE_AUTHENTICATE);
778 eap_authpeer(unit, our_name);
780 } else if (go->neg_chap) {
781 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
783 } else if (go->neg_upap) {
788 eap_authwithpeer(unit, user);
789 auth |= EAP_WITHPEER;
790 } else if (ho->neg_chap) {
791 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
792 auth |= CHAP_WITHPEER;
793 } else if (ho->neg_upap) {
794 /* If a blank password was explicitly given as an option, trust
795 the user and don't try to look up one. */
796 if (passwd[0] == 0 && !explicit_passwd) {
797 passwd_from_file = 1;
798 if (!get_pap_passwd(passwd))
799 error("No secret found for PAP login");
801 upap_authwithpeer(unit, user, passwd);
802 auth |= PAP_WITHPEER;
804 auth_pending[unit] = auth;
812 * Proceed to the network phase.
818 lcp_options *go = &lcp_gotoptions[unit];
820 /* Log calling number. */
822 notice("peer from calling number %q authorized", remote_number);
825 * If the peer had to authenticate, run the auth-up script now.
827 if (go->neg_chap || go->neg_upap || go->neg_eap) {
828 notify(auth_up_notifier, 0);
830 if (auth_script_state == s_down && auth_script_pid == 0) {
831 auth_script_state = s_up;
832 auth_script(_PATH_AUTHUP);
838 * If we negotiated callback, do it now.
841 new_phase(PHASE_CALLBACK);
842 (*cbcp_protent.open)(unit);
848 * Process extra options from the secrets file
851 options_from_list(extra_options, 1);
852 free_wordlist(extra_options);
855 start_networks(unit);
863 struct protent *protp;
864 int ecp_required, mppe_required;
866 new_phase(PHASE_NETWORK);
868 #ifdef HAVE_MULTILINK
870 if (mp_join_bundle()) {
871 if (multilink_join_hook)
872 (*multilink_join_hook)();
873 if (updetach && !nodetach)
878 #endif /* HAVE_MULTILINK */
882 set_filters(&pass_filter, &active_filter);
884 /* Start CCP and ECP */
885 for (i = 0; (protp = protocols[i]) != NULL; ++i)
886 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
887 && protp->enabled_flag && protp->open != NULL)
891 * Bring up other network protocols iff encryption is not required.
893 ecp_required = ecp_gotoptions[unit].required;
894 mppe_required = ccp_gotoptions[unit].mppe;
895 if (!ecp_required && !mppe_required)
896 continue_networks(unit);
900 continue_networks(unit)
904 struct protent *protp;
907 * Start the "real" network protocols.
909 for (i = 0; (protp = protocols[i]) != NULL; ++i)
910 if (protp->protocol < 0xC000
911 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
912 && protp->enabled_flag && protp->open != NULL) {
917 if (num_np_open == 0)
919 lcp_close(0, "No network protocols running");
923 * The peer has failed to authenticate himself using `protocol'.
926 auth_peer_fail(unit, protocol)
930 * Authentication failure: take the link down
932 status = EXIT_PEER_AUTH_FAILED;
933 lcp_close(unit, "Authentication failed");
937 * The peer has been successfully authenticated using `protocol'.
940 auth_peer_success(unit, protocol, prot_flavor, name, namelen)
941 int unit, protocol, prot_flavor;
950 switch (prot_flavor) {
952 bit |= CHAP_MD5_PEER;
958 case CHAP_MICROSOFT_V2:
959 bit |= CHAP_MS2_PEER;
971 warn("auth_peer_success: unknown protocol %x", protocol);
976 * Save the authenticated name of the peer for later.
978 if (namelen > sizeof(peer_authname) - 1)
979 namelen = sizeof(peer_authname) - 1;
980 BCOPY(name, peer_authname, namelen);
981 peer_authname[namelen] = 0;
982 script_setenv("PEERNAME", peer_authname, 0);
984 /* Save the authentication method for later. */
985 auth_done[unit] |= bit;
988 * If there is no more authentication still to be done,
989 * proceed to the network (or callback) phase.
991 if ((auth_pending[unit] &= ~bit) == 0)
996 * We have failed to authenticate ourselves to the peer using `protocol'.
999 auth_withpeer_fail(unit, protocol)
1002 if (passwd_from_file)
1003 BZERO(passwd, MAXSECRETLEN);
1005 * We've failed to authenticate ourselves to our peer.
1006 * Some servers keep sending CHAP challenges, but there
1007 * is no point in persisting without any way to get updated
1008 * authentication secrets.
1010 status = EXIT_AUTH_TOPEER_FAILED;
1011 lcp_close(unit, "Failed to authenticate ourselves to peer");
1015 * We have successfully authenticated ourselves with the peer using `protocol'.
1018 auth_withpeer_success(unit, protocol, prot_flavor)
1019 int unit, protocol, prot_flavor;
1022 const char *prot = "";
1026 bit = CHAP_WITHPEER;
1028 switch (prot_flavor) {
1030 bit |= CHAP_MD5_WITHPEER;
1033 case CHAP_MICROSOFT:
1034 bit |= CHAP_MS_WITHPEER;
1036 case CHAP_MICROSOFT_V2:
1037 bit |= CHAP_MS2_WITHPEER;
1043 if (passwd_from_file)
1044 BZERO(passwd, MAXSECRETLEN);
1053 warn("auth_withpeer_success: unknown protocol %x", protocol);
1057 notice("%s authentication succeeded", prot);
1059 /* Save the authentication method for later. */
1060 auth_done[unit] |= bit;
1063 * If there is no more authentication still being done,
1064 * proceed to the network (or callback) phase.
1066 if ((auth_pending[unit] &= ~bit) == 0)
1067 network_phase(unit);
1072 * np_up - a network protocol has come up.
1080 if (num_np_up == 0) {
1082 * At this point we consider that the link has come up successfully.
1086 new_phase(PHASE_RUNNING);
1088 if (idle_time_hook != 0)
1089 tlim = (*idle_time_hook)(NULL);
1091 tlim = idle_time_limit;
1093 TIMEOUT(check_idle, NULL, tlim);
1096 * Set a timeout to close the connection once the maximum
1097 * connect time has expired.
1100 TIMEOUT(connect_time_expired, 0, maxconnect);
1104 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1108 * Detach now, if the updetach option was given.
1110 if (updetach && !nodetach) {
1111 dbglog("updetach is set. Now detaching.");
1114 } else if (nodetach && up_sdnotify) {
1115 dbglog("up_sdnotify is set. Now notifying systemd: READY=1");
1116 sd_notify(0, "READY=1");
1124 * np_down - a network protocol has gone down.
1127 np_down(unit, proto)
1130 if (--num_np_up == 0) {
1131 UNTIMEOUT(check_idle, NULL);
1132 UNTIMEOUT(connect_time_expired, NULL);
1134 UNTIMEOUT(check_maxoctets, NULL);
1136 new_phase(PHASE_NETWORK);
1141 * np_finished - a network protocol has finished using the link.
1144 np_finished(unit, proto)
1147 if (--num_np_open <= 0) {
1148 /* no further use for the link: shut up shop. */
1149 lcp_close(0, "No network protocols running");
1155 check_maxoctets(arg)
1160 update_link_stats(ifunit);
1163 switch(maxoctets_dir) {
1164 case PPP_OCTETS_DIRECTION_IN:
1165 used = link_stats.bytes_in;
1167 case PPP_OCTETS_DIRECTION_OUT:
1168 used = link_stats.bytes_out;
1170 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1171 case PPP_OCTETS_DIRECTION_MAXSESSION:
1172 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
1175 used = link_stats.bytes_in+link_stats.bytes_out;
1178 if (used > maxoctets) {
1179 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1180 status = EXIT_TRAFFIC_LIMIT;
1181 lcp_close(0, "Traffic limit");
1184 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1190 * check_idle - check whether the link has been idle for long
1191 * enough that we can shut it down.
1197 struct ppp_idle idle;
1201 if (!get_idle_time(0, &idle))
1203 if (idle_time_hook != 0) {
1204 tlim = idle_time_hook(&idle);
1206 itime = MIN(idle.xmit_idle, idle.recv_idle);
1207 tlim = idle_time_limit - itime;
1210 /* link is idle: shut it down. */
1211 notice("Terminating connection due to lack of activity.");
1212 status = EXIT_IDLE_TIMEOUT;
1213 lcp_close(0, "Link inactive");
1216 TIMEOUT(check_idle, NULL, tlim);
1221 * connect_time_expired - log a message and close the connection.
1224 connect_time_expired(arg)
1227 info("Connect time expired");
1228 status = EXIT_CONNECT_TIME;
1229 lcp_close(0, "Connect time expired"); /* Close connection */
1233 * auth_check_options - called to check authentication options.
1236 auth_check_options()
1238 lcp_options *wo = &lcp_wantoptions[0];
1242 /* Default our_name to hostname, and user to our_name */
1243 if (our_name[0] == 0 || usehostname)
1244 strlcpy(our_name, hostname, sizeof(our_name));
1245 /* If a blank username was explicitly given as an option, trust
1246 the user and don't use our_name */
1247 if (user[0] == 0 && !explicit_user)
1248 strlcpy(user, our_name, sizeof(user));
1251 * If we have a default route, require the peer to authenticate
1252 * unless the noauth option was given or the real user is root.
1254 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1259 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1260 if (wo->chap_mdtype)
1263 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1264 if (auth_required) {
1266 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1267 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
1268 wo->chap_mdtype = chap_mdtype_all;
1274 wo->chap_mdtype = MDTYPE_NONE;
1280 * Check whether we have appropriate secrets to use
1281 * to authenticate the peer. Note that EAP can authenticate by way
1282 * of a CHAP-like exchanges as well as SRP.
1285 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1286 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1287 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1288 our_name, 1, &lacks_ip);
1290 if (!can_auth && wo->neg_eap) {
1291 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1292 our_name, 1, &lacks_ip);
1295 if (auth_required && !can_auth && noauth_addrs == NULL) {
1298 "By default the remote system is required to authenticate itself");
1300 "(because this system has a default route to the internet)");
1301 } else if (explicit_remote)
1303 "The remote system (%s) is required to authenticate itself",
1307 "The remote system is required to authenticate itself");
1309 "but I couldn't find any suitable secret (password) for it to use to do so.");
1312 "(None of the available passwords would let it use an IP address.)");
1318 * Early check for remote number authorization.
1320 if (!auth_number()) {
1321 warn("calling number %q is not authorized", remote_number);
1322 exit(EXIT_CNID_AUTH_FAILED);
1327 * auth_reset - called when LCP is starting negotiations to recheck
1328 * authentication options, i.e. whether we have appropriate secrets
1329 * to use for authenticating ourselves and/or the peer.
1335 lcp_options *go = &lcp_gotoptions[unit];
1336 lcp_options *ao = &lcp_allowoptions[unit];
1340 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1341 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1342 && (passwd[0] != 0 ||
1343 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1345 ao->neg_eap = !refuse_eap && (
1347 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1348 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1349 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL));
1352 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1355 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1356 our_name, 1, NULL)))
1360 (hadchap == 0 || (hadchap == -1 &&
1361 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1363 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1370 * check_passwd - Check the user name and passwd against the PAP secrets
1371 * file. If requested, also check against the system password database,
1372 * and login the user if OK.
1375 * UPAP_AUTHNAK: Authentication failed.
1376 * UPAP_AUTHACK: Authentication succeeded.
1377 * In either case, msg points to an appropriate message.
1380 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1391 struct wordlist *addrs = NULL, *opts = NULL;
1392 char passwd[256], user[256];
1393 char secret[MAXWORDLEN];
1394 static int attempts = 0;
1397 * Make copies of apasswd and auser, then null-terminate them.
1398 * If there are unprintable characters in the password, make
1401 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1402 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1406 * Check if a plugin wants to handle this.
1408 if (pap_auth_hook) {
1409 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1411 /* note: set_allowed_addrs() saves opts (but not addrs):
1414 set_allowed_addrs(unit, addrs, opts);
1416 free_wordlist(opts);
1418 free_wordlist(addrs);
1419 BZERO(passwd, sizeof(passwd));
1420 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1425 * Open the file of pap secrets and scan for a suitable secret
1426 * for authenticating this user.
1428 filename = _PATH_UPAPFILE;
1429 addrs = opts = NULL;
1431 f = fopen(filename, "r");
1433 error("Can't open PAP password file %s: %m", filename);
1436 check_access(f, filename);
1437 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1438 warn("no PAP secret found for %s", user);
1441 * If the secret is "@login", it means to check
1442 * the password against the login database.
1444 int login_secret = strcmp(secret, "@login") == 0;
1446 if (uselogin || login_secret) {
1447 /* login option or secret is @login */
1448 if (session_full(user, passwd, devnam, msg) == 0) {
1451 } else if (session_mgmt) {
1452 if (session_check(user, NULL, devnam, NULL) == 0) {
1453 warn("Peer %q failed PAP Session verification", user);
1457 if (secret[0] != 0 && !login_secret) {
1458 /* password given in pap-secrets - must match */
1459 if (cryptpap || strcmp(passwd, secret) != 0) {
1460 char *cbuf = crypt(passwd, secret);
1461 if (!cbuf || strcmp(cbuf, secret) != 0)
1469 if (ret == UPAP_AUTHNAK) {
1471 *msg = "Login incorrect";
1473 * XXX can we ever get here more than once??
1474 * Frustrate passwd stealer programs.
1475 * Allow 10 tries, but start backing off after 3 (stolen from login).
1476 * On 10'th, drop the connection.
1478 if (attempts++ >= 10) {
1479 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1480 lcp_close(unit, "login failed");
1483 sleep((u_int) (attempts - 3) * 5);
1485 free_wordlist(opts);
1488 attempts = 0; /* Reset count */
1491 set_allowed_addrs(unit, addrs, opts);
1495 free_wordlist(addrs);
1496 BZERO(passwd, sizeof(passwd));
1497 BZERO(secret, sizeof(secret));
1503 * null_login - Check if a username of "" and a password of "" are
1504 * acceptable, and iff so, set the list of acceptable IP addresses
1514 struct wordlist *addrs, *opts;
1515 char secret[MAXWORDLEN];
1518 * Check if a plugin wants to handle this.
1522 ret = (*null_auth_hook)(&addrs, &opts);
1525 * Open the file of pap secrets and scan for a suitable secret.
1528 filename = _PATH_UPAPFILE;
1530 f = fopen(filename, "r");
1533 check_access(f, filename);
1535 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1536 ret = i >= 0 && secret[0] == 0;
1537 BZERO(secret, sizeof(secret));
1542 set_allowed_addrs(unit, addrs, opts);
1544 free_wordlist(opts);
1546 free_wordlist(addrs);
1553 * get_pap_passwd - get a password for authenticating ourselves with
1554 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1556 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1559 get_pap_passwd(passwd)
1565 char secret[MAXWORDLEN];
1568 * Check whether a plugin wants to supply this.
1570 if (pap_passwd_hook) {
1571 ret = (*pap_passwd_hook)(user, passwd);
1576 filename = _PATH_UPAPFILE;
1577 f = fopen(filename, "r");
1580 check_access(f, filename);
1581 ret = scan_authfile(f, user,
1582 (remote_name[0]? remote_name: NULL),
1583 secret, NULL, NULL, filename, 0);
1588 strlcpy(passwd, secret, MAXSECRETLEN);
1589 BZERO(secret, sizeof(secret));
1595 * have_pap_secret - check whether we have a PAP file with any
1596 * secrets that we could possibly use for authenticating the peer.
1599 have_pap_secret(lacks_ipp)
1605 struct wordlist *addrs;
1607 /* let the plugin decide, if there is one */
1608 if (pap_check_hook) {
1609 ret = (*pap_check_hook)();
1614 filename = _PATH_UPAPFILE;
1615 f = fopen(filename, "r");
1619 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1620 NULL, &addrs, NULL, filename, 0);
1622 if (ret >= 0 && !some_ip_ok(addrs)) {
1628 free_wordlist(addrs);
1635 * have_chap_secret - check whether we have a CHAP file with a
1636 * secret that we could possibly use for authenticating `client'
1637 * on `server'. Either can be the null string, meaning we don't
1638 * know the identity yet.
1641 have_chap_secret(client, server, need_ip, lacks_ipp)
1650 struct wordlist *addrs;
1652 if (chap_check_hook) {
1653 ret = (*chap_check_hook)();
1659 filename = _PATH_CHAPFILE;
1660 f = fopen(filename, "r");
1664 if (client != NULL && client[0] == 0)
1666 else if (server != NULL && server[0] == 0)
1669 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1671 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1677 free_wordlist(addrs);
1684 * have_srp_secret - check whether we have a SRP file with a
1685 * secret that we could possibly use for authenticating `client'
1686 * on `server'. Either can be the null string, meaning we don't
1687 * know the identity yet.
1690 have_srp_secret(client, server, need_ip, lacks_ipp)
1699 struct wordlist *addrs;
1701 filename = _PATH_SRPFILE;
1702 f = fopen(filename, "r");
1706 if (client != NULL && client[0] == 0)
1708 else if (server != NULL && server[0] == 0)
1711 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1713 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1719 free_wordlist(addrs);
1726 * get_secret - open the CHAP secret file and return the secret
1727 * for authenticating the given client on the given server.
1728 * (We could be either client or server).
1731 get_secret(unit, client, server, secret, secret_len, am_server)
1742 struct wordlist *addrs, *opts;
1743 char secbuf[MAXWORDLEN];
1745 if (!am_server && passwd[0] != 0) {
1746 strlcpy(secbuf, passwd, sizeof(secbuf));
1747 } else if (!am_server && chap_passwd_hook) {
1748 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1749 error("Unable to obtain CHAP password for %s on %s from plugin",
1754 filename = _PATH_CHAPFILE;
1758 f = fopen(filename, "r");
1760 error("Can't open chap secret file %s: %m", filename);
1763 check_access(f, filename);
1765 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1771 set_allowed_addrs(unit, addrs, opts);
1773 free_wordlist(opts);
1775 free_wordlist(addrs);
1778 len = strlen(secbuf);
1779 if (len > MAXSECRETLEN) {
1780 error("Secret for %s on %s is too long", client, server);
1783 BCOPY(secbuf, secret, len);
1784 BZERO(secbuf, sizeof(secbuf));
1792 * get_srp_secret - open the SRP secret file and return the secret
1793 * for authenticating the given client on the given server.
1794 * (We could be either client or server).
1797 get_srp_secret(unit, client, server, secret, am_server)
1807 struct wordlist *addrs, *opts;
1809 if (!am_server && passwd[0] != '\0') {
1810 strlcpy(secret, passwd, MAXWORDLEN);
1812 filename = _PATH_SRPFILE;
1815 fp = fopen(filename, "r");
1817 error("Can't open srp secret file %s: %m", filename);
1820 check_access(fp, filename);
1823 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1824 filename, am_server);
1830 set_allowed_addrs(unit, addrs, opts);
1831 else if (opts != NULL)
1832 free_wordlist(opts);
1834 free_wordlist(addrs);
1841 * set_allowed_addrs() - set the list of allowed addresses.
1842 * Also looks for `--' indicating options to apply for this peer
1843 * and leaves the following words in extra_options.
1846 set_allowed_addrs(unit, addrs, opts)
1848 struct wordlist *addrs;
1849 struct wordlist *opts;
1852 struct wordlist *ap, **plink;
1853 struct permitted_ip *ip;
1854 char *ptr_word, *ptr_mask;
1857 u_int32_t a, mask, ah, offset;
1858 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1859 u_int32_t suggested_ip = 0;
1861 if (addresses[unit] != NULL)
1862 free(addresses[unit]);
1863 addresses[unit] = NULL;
1864 if (extra_options != NULL)
1865 free_wordlist(extra_options);
1866 extra_options = opts;
1869 * Count the number of IP addresses given.
1871 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1874 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1878 /* temporarily append the noauth_addrs list to addrs */
1879 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1881 *plink = noauth_addrs;
1884 for (ap = addrs; ap != NULL; ap = ap->next) {
1885 /* "-" means no addresses authorized, "*" means any address allowed */
1886 ptr_word = ap->word;
1887 if (strcmp(ptr_word, "-") == 0)
1889 if (strcmp(ptr_word, "*") == 0) {
1891 ip[n].base = ip[n].mask = 0;
1897 if (*ptr_word == '!') {
1902 mask = ~ (u_int32_t) 0;
1904 ptr_mask = strchr (ptr_word, '/');
1905 if (ptr_mask != NULL) {
1909 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1910 if (bit_count <= 0 || bit_count > 32) {
1911 warn("invalid address length %v in auth. address list",
1915 bit_count = 32 - bit_count; /* # bits in host part */
1917 offset = ifunit + 1;
1921 warn("invalid address length syntax: %v", ptr_mask+1);
1928 hp = gethostbyname(ptr_word);
1929 if (hp != NULL && hp->h_addrtype == AF_INET) {
1930 a = *(u_int32_t *)hp->h_addr;
1932 np = getnetbyname (ptr_word);
1933 if (np != NULL && np->n_addrtype == AF_INET) {
1934 a = htonl ((u_int32_t)np->n_net);
1935 if (ptr_mask == NULL) {
1936 /* calculate appropriate mask for net */
1939 mask = IN_CLASSA_NET;
1940 else if (IN_CLASSB(ah))
1941 mask = IN_CLASSB_NET;
1942 else if (IN_CLASSC(ah))
1943 mask = IN_CLASSC_NET;
1946 a = inet_addr (ptr_word);
1950 if (ptr_mask != NULL)
1953 if (a == (u_int32_t)-1L) {
1954 warn("unknown host %s in auth. address list", ap->word);
1958 if (offset >= ~mask) {
1959 warn("interface unit %d too large for subnet %v",
1963 a = htonl((ntohl(a) & mask) + offset);
1964 mask = ~(u_int32_t)0;
1966 ip[n].mask = htonl(mask);
1967 ip[n].base = a & ip[n].mask;
1969 if (~mask == 0 && suggested_ip == 0)
1974 ip[n].permit = 0; /* make the last entry forbid all addresses */
1975 ip[n].base = 0; /* to terminate the list */
1978 addresses[unit] = ip;
1981 * If the address given for the peer isn't authorized, or if
1982 * the user hasn't given one, AND there is an authorized address
1983 * which is a single host, then use that if we find one.
1985 if (suggested_ip != 0
1986 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
1987 wo->hisaddr = suggested_ip;
1989 * Do we insist on this address? No, if there are other
1990 * addresses authorized than the suggested one.
1993 wo->accept_remote = 1;
1998 * auth_ip_addr - check whether the peer is authorized to use
1999 * a given IP address. Returns 1 if authorized, 0 otherwise.
2002 auth_ip_addr(unit, addr)
2008 /* don't allow loopback or multicast address */
2009 if (bad_ip_adrs(addr))
2012 if (allowed_address_hook) {
2013 ok = allowed_address_hook(addr);
2014 if (ok >= 0) return ok;
2017 if (addresses[unit] != NULL) {
2018 ok = ip_addr_check(addr, addresses[unit]);
2024 return 0; /* no addresses authorized */
2025 return allow_any_ip || privileged || !have_route_to(addr);
2029 ip_addr_check(addr, addrs)
2031 struct permitted_ip *addrs;
2034 if ((addr & addrs->mask) == addrs->base)
2035 return addrs->permit;
2039 * bad_ip_adrs - return 1 if the IP address is one we don't want
2040 * to use, such as an address in the loopback net or a multicast address.
2041 * addr is in network byte order.
2048 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2049 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2053 * some_ip_ok - check a wordlist to see if it authorizes any
2058 struct wordlist *addrs;
2060 for (; addrs != 0; addrs = addrs->next) {
2061 if (addrs->word[0] == '-')
2063 if (addrs->word[0] != '!')
2064 return 1; /* some IP address is allowed */
2070 * auth_number - check whether the remote number is allowed to connect.
2071 * Returns 1 if authorized, 0 otherwise.
2076 struct wordlist *wp = permitted_numbers;
2079 /* Allow all if no authorization list. */
2083 /* Allow if we have a match in the authorization list. */
2085 /* trailing '*' wildcard */
2086 l = strlen(wp->word);
2087 if ((wp->word)[l - 1] == '*')
2089 if (!strncasecmp(wp->word, remote_number, l))
2098 * check_access - complain if a secret file has too-liberal permissions.
2101 check_access(f, filename)
2107 if (fstat(fileno(f), &sbuf) < 0) {
2108 warn("cannot stat secret file %s: %m", filename);
2109 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2110 warn("Warning - secret file %s has world and/or group access",
2117 * scan_authfile - Scan an authorization file for a secret suitable
2118 * for authenticating `client' on `server'. The return value is -1
2119 * if no secret is found, otherwise >= 0. The return value has
2120 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2121 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2122 * Any following words on the line up to a "--" (i.e. address authorization
2123 * info) are placed in a wordlist and returned in *addrs. Any
2124 * following words (extra options) are placed in a wordlist and
2125 * returned in *opts.
2126 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2127 * Flags are non-zero if we need two colons in the secret in order to
2131 scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
2136 struct wordlist **addrs;
2137 struct wordlist **opts;
2142 int got_flag, best_flag;
2144 struct wordlist *ap, *addr_list, *alist, **app;
2145 char word[MAXWORDLEN];
2146 char atfile[MAXWORDLEN];
2147 char lsecret[MAXWORDLEN];
2155 if (!getword(f, word, &newline, filename))
2156 return -1; /* file is empty??? */
2161 * Skip until we find a word at the start of a line.
2163 while (!newline && getword(f, word, &newline, filename))
2166 break; /* got to end of file */
2169 * Got a client - check if it's a match or a wildcard.
2172 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2177 got_flag = NONWILD_CLIENT;
2180 * Now get a server and check if it matches.
2182 if (!getword(f, word, &newline, filename))
2186 if (!ISWILD(word)) {
2187 if (server != NULL && strcmp(word, server) != 0)
2189 got_flag |= NONWILD_SERVER;
2193 * Got some sort of a match - see if it's better than what
2196 if (got_flag <= best_flag)
2202 if (!getword(f, word, &newline, filename))
2208 * SRP-SHA1 authenticator should never be reading secrets from
2209 * a file. (Authenticatee may, though.)
2211 if (flags && ((cp = strchr(word, ':')) == NULL ||
2212 strchr(cp + 1, ':') == NULL))
2215 if (secret != NULL) {
2217 * Special syntax: @/pathname means read secret from file.
2219 if (word[0] == '@' && word[1] == '/') {
2220 strlcpy(atfile, word+1, sizeof(atfile));
2221 if ((sf = fopen(atfile, "r")) == NULL) {
2222 warn("can't open indirect secret file %s", atfile);
2225 check_access(sf, atfile);
2226 if (!getword(sf, word, &xxx, atfile)) {
2227 warn("no secret in indirect secret file %s", atfile);
2233 strlcpy(lsecret, word, sizeof(lsecret));
2237 * Now read address authorization info and make a wordlist.
2241 if (!getword(f, word, &newline, filename) || newline)
2243 ap = (struct wordlist *)
2244 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2246 novm("authorized addresses");
2247 ap->word = (char *) (ap + 1);
2248 strcpy(ap->word, word);
2255 * This is the best so far; remember it.
2257 best_flag = got_flag;
2259 free_wordlist(addr_list);
2262 strlcpy(secret, lsecret, MAXWORDLEN);
2268 /* scan for a -- word indicating the start of options */
2269 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2270 if (strcmp(ap->word, "--") == 0)
2272 /* ap = start of options */
2274 ap = ap->next; /* first option */
2275 free(*app); /* free the "--" word */
2276 *app = NULL; /* terminate addr list */
2280 else if (ap != NULL)
2284 else if (addr_list != NULL)
2285 free_wordlist(addr_list);
2291 * wordlist_count - return the number of items in a wordlist
2295 struct wordlist *wp;
2299 for (n = 0; wp != NULL; wp = wp->next)
2305 * free_wordlist - release memory allocated for a wordlist.
2309 struct wordlist *wp;
2311 struct wordlist *next;
2313 while (wp != NULL) {
2321 * auth_script_done - called when the auth-up or auth-down script
2325 auth_script_done(arg)
2328 auth_script_pid = 0;
2329 switch (auth_script_state) {
2331 if (auth_state == s_down) {
2332 auth_script_state = s_down;
2333 auth_script(_PATH_AUTHDOWN);
2337 if (auth_state == s_up) {
2338 auth_script_state = s_up;
2339 auth_script(_PATH_AUTHUP);
2346 * auth_script - execute a script with arguments
2347 * interface-name peer-name real-user tty speed
2359 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2360 user_name = pw->pw_name;
2362 slprintf(struid, sizeof(struid), "%d", getuid());
2365 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2369 argv[2] = peer_authname;
2370 argv[3] = user_name;
2375 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);