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 $"
82 #include <sys/types.h>
84 #include <sys/socket.h>
87 #if defined(_PATH_LASTLOG) && defined(__linux__)
92 #include <netinet/in.h>
93 #include <arpa/inet.h>
99 #define PW_PPP PW_LOGIN
105 #include <systemd/sd-daemon.h>
115 #include "chap-new.h"
123 #include "pathnames.h"
127 /* Bits in scan_authfile return value */
128 #define NONWILD_SERVER 1
129 #define NONWILD_CLIENT 2
131 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
133 /* The name by which the peer authenticated itself to us. */
134 char peer_authname[MAXNAMELEN];
136 /* Records which authentication operations haven't completed yet. */
137 static int auth_pending[NUM_PPP];
139 /* Records which authentication operations have been completed. */
140 int auth_done[NUM_PPP];
142 /* List of addresses which the peer may use. */
143 static struct permitted_ip *addresses[NUM_PPP];
145 /* Wordlist giving addresses which the peer may use
146 without authenticating itself. */
147 static struct wordlist *noauth_addrs;
149 /* Remote telephone number, if available */
150 char remote_number[MAXNAMELEN];
152 /* Wordlist giving remote telephone numbers which may connect. */
153 static struct wordlist *permitted_numbers;
155 /* Extra options to apply, from the secrets file entry for the peer. */
156 static struct wordlist *extra_options;
158 /* Number of network protocols which we have opened. */
159 static int num_np_open;
161 /* Number of network protocols which have come up. */
162 static int num_np_up;
164 /* Set if we got the contents of passwd[] from the pap-secrets file. */
165 static int passwd_from_file;
167 /* Set if we require authentication only because we have a default route. */
168 static bool default_auth;
170 /* Hook to enable a plugin to control the idle time limit */
171 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
173 /* Hook for a plugin to say whether we can possibly authenticate any peer */
174 int (*pap_check_hook) __P((void)) = NULL;
176 /* Hook for a plugin to check the PAP user and password */
177 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
178 struct wordlist **paddrs,
179 struct wordlist **popts)) = NULL;
181 /* Hook for a plugin to know about the PAP user logout */
182 void (*pap_logout_hook) __P((void)) = NULL;
184 /* Hook for a plugin to get the PAP password for authenticating us */
185 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
187 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
188 int (*chap_check_hook) __P((void)) = NULL;
190 /* Hook for a plugin to get the CHAP password for authenticating us */
191 int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
194 /* Hook for a plugin to get the EAP-TLS password for authenticating us */
195 int (*eaptls_passwd_hook) __P((char *user, char *passwd)) = NULL;
198 /* Hook for a plugin to say whether it is OK if the peer
199 refuses to authenticate. */
200 int (*null_auth_hook) __P((struct wordlist **paddrs,
201 struct wordlist **popts)) = NULL;
203 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
205 #ifdef HAVE_MULTILINK
206 /* Hook for plugin to hear when an interface joins a multilink bundle */
207 void (*multilink_join_hook) __P((void)) = NULL;
210 /* A notifier for when the peer has authenticated itself,
211 and we are proceeding to the network phase. */
212 struct notifier *auth_up_notifier = NULL;
214 /* A notifier for when the link goes down. */
215 struct notifier *link_down_notifier = NULL;
218 * This is used to ensure that we don't start an auth-up/down
219 * script while one is already running.
226 static enum script_state auth_state = s_down;
227 static enum script_state auth_script_state = s_down;
228 static pid_t auth_script_pid = 0;
233 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
234 bool session_mgmt = 0; /* Do session management (login records) */
235 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
236 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
237 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
238 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
240 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
241 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
243 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
244 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
246 bool usehostname = 0; /* Use hostname for our_name */
247 bool auth_required = 0; /* Always require authentication from peer */
248 bool allow_any_ip = 0; /* Allow peer to use any IP address */
249 bool explicit_remote = 0; /* User specified explicit remote name */
250 bool explicit_user = 0; /* Set if "user" option supplied */
251 bool explicit_passwd = 0; /* Set if "password" option supplied */
252 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
254 char *cacert_file = NULL; /* CA certificate file (pem format) */
255 char *ca_path = NULL; /* directory with CA certificates */
256 char *cert_file = NULL; /* client certificate file (pem format) */
257 char *privkey_file = NULL; /* client private key file (pem format) */
258 char *crl_dir = NULL; /* directory containing CRL files */
259 char *crl_file = NULL; /* Certificate Revocation List (CRL) file (pem format) */
260 char *max_tls_version = NULL; /* Maximum TLS protocol version (default=1.2) */
261 bool need_peer_eap = 0; /* Require peer to authenticate us */
264 static char *uafname; /* name of most recent +ua file */
266 extern char *crypt __P((const char *, const char *));
268 /* Prototypes for procedures local to this file. */
270 static void network_phase __P((int));
271 static void check_idle __P((void *));
272 static void connect_time_expired __P((void *));
273 static int null_login __P((int));
274 static int get_pap_passwd __P((char *));
275 static int have_pap_secret __P((int *));
276 static int have_chap_secret __P((char *, char *, int, int *));
277 static int have_srp_secret __P((char *client, char *server, int need_ip,
281 static int have_eaptls_secret_server
282 __P((char *client, char *server, int need_ip, int *lacks_ipp));
283 static int have_eaptls_secret_client __P((char *client, char *server));
284 static int scan_authfile_eaptls __P((FILE * f, char *client, char *server,
285 char *cli_cert, char *serv_cert,
286 char *ca_cert, char *pk,
287 struct wordlist ** addrs,
288 struct wordlist ** opts,
289 char *filename, int flags));
292 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
293 static int scan_authfile __P((FILE *, char *, char *, char *,
294 struct wordlist **, struct wordlist **,
296 static void free_wordlist __P((struct wordlist *));
297 static void auth_script __P((char *));
298 static void auth_script_done __P((void *));
299 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
300 static int some_ip_ok __P((struct wordlist *));
301 static int setupapfile __P((char **));
302 static int privgroup __P((char **));
303 static int set_noauth_addr __P((char **));
304 static int set_permitted_number __P((char **));
305 static void check_access __P((FILE *, char *));
306 static int wordlist_count __P((struct wordlist *));
309 static void check_maxoctets __P((void *));
313 * Authentication-related options.
315 option_t auth_options[] = {
316 { "auth", o_bool, &auth_required,
317 "Require authentication from peer", OPT_PRIO | 1 },
318 { "noauth", o_bool, &auth_required,
319 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
321 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
322 "Require PAP authentication from peer",
323 OPT_PRIOSUB | 1, &auth_required },
324 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
325 "Require PAP authentication from peer",
326 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
327 { "require-chap", o_bool, &auth_required,
328 "Require CHAP authentication from peer",
329 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
330 &lcp_wantoptions[0].chap_mdtype },
331 { "+chap", o_bool, &auth_required,
332 "Require CHAP authentication from peer",
333 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
334 &lcp_wantoptions[0].chap_mdtype },
336 { "require-mschap", o_bool, &auth_required,
337 "Require MS-CHAP authentication from peer",
338 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
339 &lcp_wantoptions[0].chap_mdtype },
340 { "+mschap", o_bool, &auth_required,
341 "Require MS-CHAP authentication from peer",
342 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
343 &lcp_wantoptions[0].chap_mdtype },
344 { "require-mschap-v2", o_bool, &auth_required,
345 "Require MS-CHAPv2 authentication from peer",
346 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
347 &lcp_wantoptions[0].chap_mdtype },
348 { "+mschap-v2", o_bool, &auth_required,
349 "Require MS-CHAPv2 authentication from peer",
350 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
351 &lcp_wantoptions[0].chap_mdtype },
354 { "refuse-pap", o_bool, &refuse_pap,
355 "Don't agree to auth to peer with PAP", 1 },
356 { "-pap", o_bool, &refuse_pap,
357 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
358 { "refuse-chap", o_bool, &refuse_chap,
359 "Don't agree to auth to peer with CHAP",
360 OPT_A2CLRB | MDTYPE_MD5,
361 &lcp_allowoptions[0].chap_mdtype },
362 { "-chap", o_bool, &refuse_chap,
363 "Don't allow CHAP authentication with peer",
364 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
365 &lcp_allowoptions[0].chap_mdtype },
367 { "refuse-mschap", o_bool, &refuse_mschap,
368 "Don't agree to auth to peer with MS-CHAP",
369 OPT_A2CLRB | MDTYPE_MICROSOFT,
370 &lcp_allowoptions[0].chap_mdtype },
371 { "-mschap", o_bool, &refuse_mschap,
372 "Don't allow MS-CHAP authentication with peer",
373 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
374 &lcp_allowoptions[0].chap_mdtype },
375 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
376 "Don't agree to auth to peer with MS-CHAPv2",
377 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
378 &lcp_allowoptions[0].chap_mdtype },
379 { "-mschap-v2", o_bool, &refuse_mschap_v2,
380 "Don't allow MS-CHAPv2 authentication with peer",
381 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
382 &lcp_allowoptions[0].chap_mdtype },
385 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
386 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
388 { "refuse-eap", o_bool, &refuse_eap,
389 "Don't agree to authenticate to peer with EAP", 1 },
391 { "name", o_string, our_name,
392 "Set local name for authentication",
393 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
395 { "+ua", o_special, (void *)setupapfile,
396 "Get PAP user and password from file",
397 OPT_PRIO | OPT_A2STRVAL, &uafname },
399 { "user", o_string, user,
400 "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
401 &explicit_user, MAXNAMELEN },
403 { "password", o_string, passwd,
404 "Password for authenticating us to the peer",
405 OPT_PRIO | OPT_STATIC | OPT_HIDE,
406 &explicit_passwd, MAXSECRETLEN },
408 { "usehostname", o_bool, &usehostname,
409 "Must use hostname for authentication", 1 },
411 { "remotename", o_string, remote_name,
412 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
413 &explicit_remote, MAXNAMELEN },
415 { "login", o_bool, &uselogin,
416 "Use system password database for PAP", OPT_A2COPY | 1 ,
418 { "enable-session", o_bool, &session_mgmt,
419 "Enable session accounting for remote peers", OPT_PRIV | 1 },
421 { "papcrypt", o_bool, &cryptpap,
422 "PAP passwords are encrypted", 1 },
424 { "privgroup", o_special, (void *)privgroup,
425 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
427 { "allow-ip", o_special, (void *)set_noauth_addr,
428 "Set IP address(es) which can be used without authentication",
429 OPT_PRIV | OPT_A2LIST },
431 { "remotenumber", o_string, remote_number,
432 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
435 { "allow-number", o_special, (void *)set_permitted_number,
436 "Set telephone number(s) which are allowed to connect",
437 OPT_PRIV | OPT_A2LIST },
440 { "ca", o_string, &cacert_file, "EAP-TLS CA certificate in PEM format" },
441 { "capath", o_string, &ca_path, "EAP-TLS CA certificate directory" },
442 { "cert", o_string, &cert_file, "EAP-TLS client certificate in PEM format" },
443 { "key", o_string, &privkey_file, "EAP-TLS client private key in PEM format" },
444 { "crl-dir", o_string, &crl_dir, "Use CRLs in directory" },
445 { "crl", o_string, &crl_file, "Use specific CRL file" },
446 { "max-tls-version", o_string, &max_tls_version,
447 "Maximum TLS version (1.0/1.1/1.2 (default)/1.3)" },
448 { "need-peer-eap", o_bool, &need_peer_eap,
449 "Require the peer to authenticate us", 1 },
450 #endif /* USE_EAPTLS */
455 * setupapfile - specifies UPAP info for authenticating with peer.
464 char u[MAXNAMELEN], p[MAXSECRETLEN];
467 lcp_allowoptions[0].neg_upap = 1;
469 /* open user info file */
470 fname = strdup(*argv);
472 novm("+ua file name");
474 if (seteuid(getuid()) == -1) {
475 option_error("unable to reset uid before opening %s: %m", fname);
479 ufile = fopen(fname, "r");
480 if (seteuid(euid) == -1)
481 fatal("unable to regain privileges: %m");
483 option_error("unable to open user login data file %s", fname);
487 check_access(ufile, fname);
491 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
492 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
494 option_error("unable to read user login data file %s", fname);
500 /* get rid of newlines */
502 if (l > 0 && u[l-1] == '\n')
505 if (l > 0 && p[l-1] == '\n')
508 if (override_value("user", option_priority, fname)) {
509 strlcpy(user, u, sizeof(user));
512 if (override_value("passwd", option_priority, fname)) {
513 strlcpy(passwd, p, sizeof(passwd));
523 * privgroup - allow members of the group to have privileged access.
534 option_error("group %s is unknown", *argv);
537 for (i = 0; i < ngroups; ++i) {
538 if (groups[i] == g->gr_gid) {
548 * set_noauth_addr - set address(es) that can be used without authentication.
549 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
552 set_noauth_addr(argv)
556 int l = strlen(addr) + 1;
559 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
561 novm("allow-ip argument");
562 wp->word = (char *) (wp + 1);
563 wp->next = noauth_addrs;
564 BCOPY(addr, wp->word, l);
571 * set_permitted_number - set remote telephone number(s) that may connect.
574 set_permitted_number(argv)
577 char *number = *argv;
578 int l = strlen(number) + 1;
581 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
583 novm("allow-number argument");
584 wp->word = (char *) (wp + 1);
585 wp->next = permitted_numbers;
586 BCOPY(number, wp->word, l);
587 permitted_numbers = wp;
593 * An Open on LCP has requested a change from Dead to Establish phase.
602 * Bring the link up to the point of being able to do ppp.
604 void start_link(unit)
607 status = EXIT_CONNECT_FAILED;
608 new_phase(PHASE_SERIALCONN);
611 devfd = the_channel->connect();
615 /* set up the serial device as a ppp interface */
617 * N.B. we used to do tdb_writelock/tdb_writeunlock around this
618 * (from establish_ppp to set_ifunit). However, we won't be
619 * doing the set_ifunit in multilink mode, which is the only time
620 * we need the atomicity that the tdb_writelock/tdb_writeunlock
621 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
623 fd_ppp = the_channel->establish_ppp(devfd);
625 status = EXIT_FATAL_ERROR;
629 if (!demand && ifunit >= 0)
633 * Start opening the connection and wait for
634 * incoming events (reply, timeout, etc.).
637 notice("Connect: %s <--> %s", ifname, ppp_devnam);
639 notice("Starting negotiation on %s", ppp_devnam);
642 status = EXIT_NEGOTIATION_FAILED;
643 new_phase(PHASE_ESTABLISH);
649 new_phase(PHASE_DISCONNECT);
650 if (the_channel->disconnect)
651 the_channel->disconnect();
654 new_phase(PHASE_DEAD);
655 if (the_channel->cleanup)
656 (*the_channel->cleanup)();
660 * LCP has terminated the link; go to the Dead phase and take the
661 * physical layer down.
664 link_terminated(unit)
667 if (phase == PHASE_DEAD || phase == PHASE_MASTER)
669 new_phase(PHASE_DISCONNECT);
671 if (pap_logout_hook) {
676 if (!doing_multilink) {
677 notice("Connection terminated.");
680 notice("Link terminated.");
683 * Delete pid files before disestablishing ppp. Otherwise it
684 * can happen that another pppd gets the same unit and then
685 * we delete its pid file.
687 if (!doing_multilink && !demand)
691 * If we may want to bring the link up again, transfer
692 * the ppp unit back to the loopback. Set the
693 * real serial device back to its normal mode of operation.
698 the_channel->disestablish_ppp(devfd);
705 if (!doing_multilink && !demand)
706 script_unsetenv("IFNAME");
709 * Run disconnector script, if requested.
710 * XXX we may not be able to do this if the line has hung up!
712 if (devfd >= 0 && the_channel->disconnect) {
713 the_channel->disconnect();
716 if (the_channel->cleanup)
717 (*the_channel->cleanup)();
719 if (doing_multilink && multilink_master) {
720 if (!bundle_terminating) {
721 new_phase(PHASE_MASTER);
722 if (master_detach && !detached)
725 mp_bundle_terminated();
727 new_phase(PHASE_DEAD);
731 * LCP has gone down; it will either die or try to re-establish.
737 if (auth_state != s_down) {
738 notify(link_down_notifier, 0);
740 if (auth_script_state == s_up && auth_script_pid == 0) {
741 update_link_stats(unit);
742 auth_script_state = s_down;
743 auth_script(_PATH_AUTHDOWN);
746 if (!doing_multilink) {
747 upper_layers_down(unit);
748 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
749 new_phase(PHASE_ESTABLISH);
751 /* XXX if doing_multilink, should do something to stop
752 network-layer traffic on the link */
755 void upper_layers_down(int unit)
758 struct protent *protp;
760 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
761 if (!protp->enabled_flag)
763 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
764 (*protp->lowerdown)(unit);
765 if (protp->protocol < 0xC000 && protp->close != NULL)
766 (*protp->close)(unit, "LCP down");
773 * The link is established.
774 * Proceed to the Dead, Authenticate or Network phase as appropriate.
777 link_established(unit)
781 lcp_options *wo = &lcp_wantoptions[unit];
782 lcp_options *go = &lcp_gotoptions[unit];
783 lcp_options *ho = &lcp_hisoptions[unit];
785 lcp_options *ao = &lcp_allowoptions[unit];
788 struct protent *protp;
791 * Tell higher-level protocols that LCP is up.
793 if (!doing_multilink) {
794 for (i = 0; (protp = protocols[i]) != NULL; ++i)
795 if (protp->protocol != PPP_LCP && protp->enabled_flag
796 && protp->lowerup != NULL)
797 (*protp->lowerup)(unit);
800 if (!auth_required && noauth_addrs != NULL)
801 set_allowed_addrs(unit, NULL, NULL);
803 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
805 * We wanted the peer to authenticate itself, and it refused:
806 * if we have some address(es) it can use without auth, fine,
807 * otherwise treat it as though it authenticated with PAP using
808 * a username of "" and a password of "". If that's not OK,
811 if (noauth_addrs != NULL) {
812 set_allowed_addrs(unit, NULL, NULL);
813 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
814 warn("peer refused to authenticate: terminating link");
815 status = EXIT_PEER_AUTH_FAILED;
816 lcp_close(unit, "peer refused to authenticate");
822 if (need_peer_eap && !ao->neg_eap) {
823 warn("eap required to authenticate us but no suitable secrets");
824 lcp_close(unit, "couldn't negotiate eap");
825 status = EXIT_AUTH_TOPEER_FAILED;
829 if (need_peer_eap && !ho->neg_eap) {
830 warn("peer doesn't want to authenticate us with eap");
831 lcp_close(unit, "couldn't negotiate eap");
832 status = EXIT_PEER_AUTH_FAILED;
837 new_phase(PHASE_AUTHENTICATE);
840 eap_authpeer(unit, our_name);
842 } else if (go->neg_chap) {
843 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
845 } else if (go->neg_upap) {
850 eap_authwithpeer(unit, user);
851 auth |= EAP_WITHPEER;
852 } else if (ho->neg_chap) {
853 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
854 auth |= CHAP_WITHPEER;
855 } else if (ho->neg_upap) {
856 /* If a blank password was explicitly given as an option, trust
857 the user and don't try to look up one. */
858 if (passwd[0] == 0 && !explicit_passwd) {
859 passwd_from_file = 1;
860 if (!get_pap_passwd(passwd))
861 error("No secret found for PAP login");
863 upap_authwithpeer(unit, user, passwd);
864 auth |= PAP_WITHPEER;
866 auth_pending[unit] = auth;
874 * Proceed to the network phase.
880 lcp_options *go = &lcp_gotoptions[unit];
882 /* Log calling number. */
884 notice("peer from calling number %q authorized", remote_number);
887 * If the peer had to authenticate, run the auth-up script now.
889 if (go->neg_chap || go->neg_upap || go->neg_eap) {
890 notify(auth_up_notifier, 0);
892 if (auth_script_state == s_down && auth_script_pid == 0) {
893 auth_script_state = s_up;
894 auth_script(_PATH_AUTHUP);
900 * If we negotiated callback, do it now.
903 new_phase(PHASE_CALLBACK);
904 (*cbcp_protent.open)(unit);
910 * Process extra options from the secrets file
913 options_from_list(extra_options, 1);
914 free_wordlist(extra_options);
917 start_networks(unit);
925 struct protent *protp;
926 int ecp_required, mppe_required;
928 new_phase(PHASE_NETWORK);
930 #ifdef HAVE_MULTILINK
932 if (mp_join_bundle()) {
933 if (multilink_join_hook)
934 (*multilink_join_hook)();
935 if (updetach && !nodetach)
940 #endif /* HAVE_MULTILINK */
944 set_filters(&pass_filter, &active_filter);
946 /* Start CCP and ECP */
947 for (i = 0; (protp = protocols[i]) != NULL; ++i)
948 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
949 && protp->enabled_flag && protp->open != NULL)
953 * Bring up other network protocols iff encryption is not required.
955 ecp_required = ecp_gotoptions[unit].required;
956 mppe_required = ccp_gotoptions[unit].mppe;
957 if (!ecp_required && !mppe_required)
958 continue_networks(unit);
962 continue_networks(unit)
966 struct protent *protp;
969 * Start the "real" network protocols.
971 for (i = 0; (protp = protocols[i]) != NULL; ++i)
972 if (protp->protocol < 0xC000
973 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
974 && protp->enabled_flag && protp->open != NULL) {
979 if (num_np_open == 0)
981 lcp_close(0, "No network protocols running");
985 * The peer has failed to authenticate himself using `protocol'.
988 auth_peer_fail(unit, protocol)
992 * Authentication failure: take the link down
994 status = EXIT_PEER_AUTH_FAILED;
995 lcp_close(unit, "Authentication failed");
999 * The peer has been successfully authenticated using `protocol'.
1002 auth_peer_success(unit, protocol, prot_flavor, name, namelen)
1003 int unit, protocol, prot_flavor;
1012 switch (prot_flavor) {
1014 bit |= CHAP_MD5_PEER;
1017 case CHAP_MICROSOFT:
1018 bit |= CHAP_MS_PEER;
1020 case CHAP_MICROSOFT_V2:
1021 bit |= CHAP_MS2_PEER;
1033 warn("auth_peer_success: unknown protocol %x", protocol);
1038 * Save the authenticated name of the peer for later.
1040 if (namelen > sizeof(peer_authname) - 1)
1041 namelen = sizeof(peer_authname) - 1;
1042 BCOPY(name, peer_authname, namelen);
1043 peer_authname[namelen] = 0;
1044 script_setenv("PEERNAME", peer_authname, 0);
1046 /* Save the authentication method for later. */
1047 auth_done[unit] |= bit;
1050 * If there is no more authentication still to be done,
1051 * proceed to the network (or callback) phase.
1053 if ((auth_pending[unit] &= ~bit) == 0)
1054 network_phase(unit);
1058 * We have failed to authenticate ourselves to the peer using `protocol'.
1061 auth_withpeer_fail(unit, protocol)
1064 if (passwd_from_file)
1065 BZERO(passwd, MAXSECRETLEN);
1067 * We've failed to authenticate ourselves to our peer.
1068 * Some servers keep sending CHAP challenges, but there
1069 * is no point in persisting without any way to get updated
1070 * authentication secrets.
1072 status = EXIT_AUTH_TOPEER_FAILED;
1073 lcp_close(unit, "Failed to authenticate ourselves to peer");
1077 * We have successfully authenticated ourselves with the peer using `protocol'.
1080 auth_withpeer_success(unit, protocol, prot_flavor)
1081 int unit, protocol, prot_flavor;
1084 const char *prot = "";
1088 bit = CHAP_WITHPEER;
1090 switch (prot_flavor) {
1092 bit |= CHAP_MD5_WITHPEER;
1095 case CHAP_MICROSOFT:
1096 bit |= CHAP_MS_WITHPEER;
1098 case CHAP_MICROSOFT_V2:
1099 bit |= CHAP_MS2_WITHPEER;
1105 if (passwd_from_file)
1106 BZERO(passwd, MAXSECRETLEN);
1115 warn("auth_withpeer_success: unknown protocol %x", protocol);
1119 notice("%s authentication succeeded", prot);
1121 /* Save the authentication method for later. */
1122 auth_done[unit] |= bit;
1125 * If there is no more authentication still being done,
1126 * proceed to the network (or callback) phase.
1128 if ((auth_pending[unit] &= ~bit) == 0)
1129 network_phase(unit);
1134 * np_up - a network protocol has come up.
1142 if (num_np_up == 0) {
1144 * At this point we consider that the link has come up successfully.
1148 new_phase(PHASE_RUNNING);
1150 if (idle_time_hook != 0)
1151 tlim = (*idle_time_hook)(NULL);
1153 tlim = idle_time_limit;
1155 TIMEOUT(check_idle, NULL, tlim);
1158 * Set a timeout to close the connection once the maximum
1159 * connect time has expired.
1162 TIMEOUT(connect_time_expired, 0, maxconnect);
1166 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1170 * Detach now, if the updetach option was given.
1172 if (updetach && !nodetach) {
1173 dbglog("updetach is set. Now detaching.");
1176 } else if (nodetach && up_sdnotify) {
1177 dbglog("up_sdnotify is set. Now notifying systemd: READY=1");
1178 sd_notify(0, "READY=1");
1186 * np_down - a network protocol has gone down.
1189 np_down(unit, proto)
1192 if (--num_np_up == 0) {
1193 UNTIMEOUT(check_idle, NULL);
1194 UNTIMEOUT(connect_time_expired, NULL);
1196 UNTIMEOUT(check_maxoctets, NULL);
1198 new_phase(PHASE_NETWORK);
1203 * np_finished - a network protocol has finished using the link.
1206 np_finished(unit, proto)
1209 if (--num_np_open <= 0) {
1210 /* no further use for the link: shut up shop. */
1211 lcp_close(0, "No network protocols running");
1217 check_maxoctets(arg)
1222 update_link_stats(ifunit);
1225 switch(maxoctets_dir) {
1226 case PPP_OCTETS_DIRECTION_IN:
1227 used = link_stats.bytes_in;
1229 case PPP_OCTETS_DIRECTION_OUT:
1230 used = link_stats.bytes_out;
1232 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1233 case PPP_OCTETS_DIRECTION_MAXSESSION:
1234 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
1237 used = link_stats.bytes_in+link_stats.bytes_out;
1240 if (used > maxoctets) {
1241 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1242 status = EXIT_TRAFFIC_LIMIT;
1243 lcp_close(0, "Traffic limit");
1246 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1252 * check_idle - check whether the link has been idle for long
1253 * enough that we can shut it down.
1259 struct ppp_idle idle;
1263 if (!get_idle_time(0, &idle))
1265 if (idle_time_hook != 0) {
1266 tlim = idle_time_hook(&idle);
1268 itime = MIN(idle.xmit_idle, idle.recv_idle);
1269 tlim = idle_time_limit - itime;
1272 /* link is idle: shut it down. */
1273 notice("Terminating connection due to lack of activity.");
1274 status = EXIT_IDLE_TIMEOUT;
1275 lcp_close(0, "Link inactive");
1278 TIMEOUT(check_idle, NULL, tlim);
1283 * connect_time_expired - log a message and close the connection.
1286 connect_time_expired(arg)
1289 info("Connect time expired");
1290 status = EXIT_CONNECT_TIME;
1291 lcp_close(0, "Connect time expired"); /* Close connection */
1295 * auth_check_options - called to check authentication options.
1298 auth_check_options()
1300 lcp_options *wo = &lcp_wantoptions[0];
1304 /* Default our_name to hostname, and user to our_name */
1305 if (our_name[0] == 0 || usehostname)
1306 strlcpy(our_name, hostname, sizeof(our_name));
1307 /* If a blank username was explicitly given as an option, trust
1308 the user and don't use our_name */
1309 if (user[0] == 0 && !explicit_user)
1310 strlcpy(user, our_name, sizeof(user));
1313 * If we have a default route, require the peer to authenticate
1314 * unless the noauth option was given or the real user is root.
1316 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1321 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1322 if (wo->chap_mdtype)
1325 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1326 if (auth_required) {
1328 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1329 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
1330 wo->chap_mdtype = chap_mdtype_all;
1336 wo->chap_mdtype = MDTYPE_NONE;
1342 * Check whether we have appropriate secrets to use
1343 * to authenticate the peer. Note that EAP can authenticate by way
1344 * of a CHAP-like exchanges as well as SRP.
1347 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1348 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1349 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1350 our_name, 1, &lacks_ip);
1352 if (!can_auth && wo->neg_eap) {
1353 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1354 our_name, 1, &lacks_ip);
1358 if (!can_auth && wo->neg_eap) {
1360 have_eaptls_secret_server((explicit_remote ? remote_name :
1361 NULL), our_name, 1, &lacks_ip);
1366 if (auth_required && !can_auth && noauth_addrs == NULL) {
1369 "By default the remote system is required to authenticate itself");
1371 "(because this system has a default route to the internet)");
1372 } else if (explicit_remote)
1374 "The remote system (%s) is required to authenticate itself",
1378 "The remote system is required to authenticate itself");
1380 "but I couldn't find any suitable secret (password) for it to use to do so.");
1383 "(None of the available passwords would let it use an IP address.)");
1389 * Early check for remote number authorization.
1391 if (!auth_number()) {
1392 warn("calling number %q is not authorized", remote_number);
1393 exit(EXIT_CNID_AUTH_FAILED);
1398 * auth_reset - called when LCP is starting negotiations to recheck
1399 * authentication options, i.e. whether we have appropriate secrets
1400 * to use for authenticating ourselves and/or the peer.
1406 lcp_options *go = &lcp_gotoptions[unit];
1407 lcp_options *ao = &lcp_allowoptions[unit];
1411 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1412 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1413 && ((passwd[0] != 0 || explicit_passwd) ||
1414 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1416 ao->neg_eap = !refuse_eap && (
1418 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1419 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1420 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL)
1422 || have_eaptls_secret_client(user, (explicit_remote? remote_name: NULL))
1427 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1430 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1431 our_name, 1, NULL)))
1435 (hadchap == 0 || (hadchap == -1 &&
1436 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1438 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1441 && !have_eaptls_secret_server((explicit_remote? remote_name: NULL),
1450 * check_passwd - Check the user name and passwd against the PAP secrets
1451 * file. If requested, also check against the system password database,
1452 * and login the user if OK.
1455 * UPAP_AUTHNAK: Authentication failed.
1456 * UPAP_AUTHACK: Authentication succeeded.
1457 * In either case, msg points to an appropriate message.
1460 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1471 struct wordlist *addrs = NULL, *opts = NULL;
1472 char passwd[256], user[256];
1473 char secret[MAXWORDLEN];
1474 static int attempts = 0;
1477 * Make copies of apasswd and auser, then null-terminate them.
1478 * If there are unprintable characters in the password, make
1481 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1482 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1486 * Check if a plugin wants to handle this.
1488 if (pap_auth_hook) {
1489 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1491 /* note: set_allowed_addrs() saves opts (but not addrs):
1494 set_allowed_addrs(unit, addrs, opts);
1496 free_wordlist(opts);
1498 free_wordlist(addrs);
1499 BZERO(passwd, sizeof(passwd));
1500 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1505 * Open the file of pap secrets and scan for a suitable secret
1506 * for authenticating this user.
1508 filename = _PATH_UPAPFILE;
1509 addrs = opts = NULL;
1511 f = fopen(filename, "r");
1513 error("Can't open PAP password file %s: %m", filename);
1516 check_access(f, filename);
1517 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1518 warn("no PAP secret found for %s", user);
1521 * If the secret is "@login", it means to check
1522 * the password against the login database.
1524 int login_secret = strcmp(secret, "@login") == 0;
1526 if (uselogin || login_secret) {
1527 /* login option or secret is @login */
1528 if (session_full(user, passwd, devnam, msg) == 0) {
1531 } else if (session_mgmt) {
1532 if (session_check(user, NULL, devnam, NULL) == 0) {
1533 warn("Peer %q failed PAP Session verification", user);
1537 if (secret[0] != 0 && !login_secret) {
1538 /* password given in pap-secrets - must match */
1539 if (cryptpap || strcmp(passwd, secret) != 0) {
1540 char *cbuf = crypt(passwd, secret);
1541 if (!cbuf || strcmp(cbuf, secret) != 0)
1549 if (ret == UPAP_AUTHNAK) {
1551 *msg = "Login incorrect";
1553 * XXX can we ever get here more than once??
1554 * Frustrate passwd stealer programs.
1555 * Allow 10 tries, but start backing off after 3 (stolen from login).
1556 * On 10'th, drop the connection.
1558 if (attempts++ >= 10) {
1559 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1560 lcp_close(unit, "login failed");
1563 sleep((u_int) (attempts - 3) * 5);
1565 free_wordlist(opts);
1568 attempts = 0; /* Reset count */
1571 set_allowed_addrs(unit, addrs, opts);
1575 free_wordlist(addrs);
1576 BZERO(passwd, sizeof(passwd));
1577 BZERO(secret, sizeof(secret));
1583 * null_login - Check if a username of "" and a password of "" are
1584 * acceptable, and iff so, set the list of acceptable IP addresses
1594 struct wordlist *addrs, *opts;
1595 char secret[MAXWORDLEN];
1598 * Check if a plugin wants to handle this.
1602 ret = (*null_auth_hook)(&addrs, &opts);
1605 * Open the file of pap secrets and scan for a suitable secret.
1608 filename = _PATH_UPAPFILE;
1610 f = fopen(filename, "r");
1613 check_access(f, filename);
1615 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1616 ret = i >= 0 && secret[0] == 0;
1617 BZERO(secret, sizeof(secret));
1622 set_allowed_addrs(unit, addrs, opts);
1624 free_wordlist(opts);
1626 free_wordlist(addrs);
1633 * get_pap_passwd - get a password for authenticating ourselves with
1634 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1636 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1639 get_pap_passwd(passwd)
1645 char secret[MAXWORDLEN];
1648 * Check whether a plugin wants to supply this.
1650 if (pap_passwd_hook) {
1651 ret = (*pap_passwd_hook)(user, passwd);
1656 filename = _PATH_UPAPFILE;
1657 f = fopen(filename, "r");
1660 check_access(f, filename);
1661 ret = scan_authfile(f, user,
1662 (remote_name[0]? remote_name: NULL),
1663 secret, NULL, NULL, filename, 0);
1668 strlcpy(passwd, secret, MAXSECRETLEN);
1669 BZERO(secret, sizeof(secret));
1675 * have_pap_secret - check whether we have a PAP file with any
1676 * secrets that we could possibly use for authenticating the peer.
1679 have_pap_secret(lacks_ipp)
1685 struct wordlist *addrs;
1687 /* let the plugin decide, if there is one */
1688 if (pap_check_hook) {
1689 ret = (*pap_check_hook)();
1694 filename = _PATH_UPAPFILE;
1695 f = fopen(filename, "r");
1699 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1700 NULL, &addrs, NULL, filename, 0);
1702 if (ret >= 0 && !some_ip_ok(addrs)) {
1708 free_wordlist(addrs);
1715 * have_chap_secret - check whether we have a CHAP file with a
1716 * secret that we could possibly use for authenticating `client'
1717 * on `server'. Either can be the null string, meaning we don't
1718 * know the identity yet.
1721 have_chap_secret(client, server, need_ip, lacks_ipp)
1730 struct wordlist *addrs;
1732 if (chap_check_hook) {
1733 ret = (*chap_check_hook)();
1739 filename = _PATH_CHAPFILE;
1740 f = fopen(filename, "r");
1744 if (client != NULL && client[0] == 0)
1746 else if (server != NULL && server[0] == 0)
1749 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1751 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1757 free_wordlist(addrs);
1764 * have_srp_secret - check whether we have a SRP file with a
1765 * secret that we could possibly use for authenticating `client'
1766 * on `server'. Either can be the null string, meaning we don't
1767 * know the identity yet.
1770 have_srp_secret(client, server, need_ip, lacks_ipp)
1779 struct wordlist *addrs;
1781 filename = _PATH_SRPFILE;
1782 f = fopen(filename, "r");
1786 if (client != NULL && client[0] == 0)
1788 else if (server != NULL && server[0] == 0)
1791 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1793 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1799 free_wordlist(addrs);
1806 * get_secret - open the CHAP secret file and return the secret
1807 * for authenticating the given client on the given server.
1808 * (We could be either client or server).
1811 get_secret(unit, client, server, secret, secret_len, am_server)
1822 struct wordlist *addrs, *opts;
1823 char secbuf[MAXWORDLEN];
1825 if (!am_server && passwd[0] != 0) {
1826 strlcpy(secbuf, passwd, sizeof(secbuf));
1827 } else if (!am_server && chap_passwd_hook) {
1828 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1829 error("Unable to obtain CHAP password for %s on %s from plugin",
1834 filename = _PATH_CHAPFILE;
1838 f = fopen(filename, "r");
1840 error("Can't open chap secret file %s: %m", filename);
1843 check_access(f, filename);
1845 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1851 set_allowed_addrs(unit, addrs, opts);
1853 free_wordlist(opts);
1855 free_wordlist(addrs);
1858 len = strlen(secbuf);
1859 if (len > MAXSECRETLEN) {
1860 error("Secret for %s on %s is too long", client, server);
1863 BCOPY(secbuf, secret, len);
1864 BZERO(secbuf, sizeof(secbuf));
1872 * get_srp_secret - open the SRP secret file and return the secret
1873 * for authenticating the given client on the given server.
1874 * (We could be either client or server).
1877 get_srp_secret(unit, client, server, secret, am_server)
1887 struct wordlist *addrs, *opts;
1889 if (!am_server && passwd[0] != '\0') {
1890 strlcpy(secret, passwd, MAXWORDLEN);
1892 filename = _PATH_SRPFILE;
1895 fp = fopen(filename, "r");
1897 error("Can't open srp secret file %s: %m", filename);
1900 check_access(fp, filename);
1903 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1904 filename, am_server);
1910 set_allowed_addrs(unit, addrs, opts);
1911 else if (opts != NULL)
1912 free_wordlist(opts);
1914 free_wordlist(addrs);
1921 * set_allowed_addrs() - set the list of allowed addresses.
1922 * Also looks for `--' indicating options to apply for this peer
1923 * and leaves the following words in extra_options.
1926 set_allowed_addrs(unit, addrs, opts)
1928 struct wordlist *addrs;
1929 struct wordlist *opts;
1932 struct wordlist *ap, **plink;
1933 struct permitted_ip *ip;
1934 char *ptr_word, *ptr_mask;
1937 u_int32_t a, mask, ah, offset;
1938 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1939 u_int32_t suggested_ip = 0;
1941 if (addresses[unit] != NULL)
1942 free(addresses[unit]);
1943 addresses[unit] = NULL;
1944 if (extra_options != NULL)
1945 free_wordlist(extra_options);
1946 extra_options = opts;
1949 * Count the number of IP addresses given.
1951 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1954 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1958 /* temporarily append the noauth_addrs list to addrs */
1959 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1961 *plink = noauth_addrs;
1964 for (ap = addrs; ap != NULL; ap = ap->next) {
1965 /* "-" means no addresses authorized, "*" means any address allowed */
1966 ptr_word = ap->word;
1967 if (strcmp(ptr_word, "-") == 0)
1969 if (strcmp(ptr_word, "*") == 0) {
1971 ip[n].base = ip[n].mask = 0;
1977 if (*ptr_word == '!') {
1982 mask = ~ (u_int32_t) 0;
1984 ptr_mask = strchr (ptr_word, '/');
1985 if (ptr_mask != NULL) {
1989 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1990 if (bit_count <= 0 || bit_count > 32) {
1991 warn("invalid address length %v in auth. address list",
1995 bit_count = 32 - bit_count; /* # bits in host part */
1997 offset = ifunit + 1;
2001 warn("invalid address length syntax: %v", ptr_mask+1);
2008 hp = gethostbyname(ptr_word);
2009 if (hp != NULL && hp->h_addrtype == AF_INET) {
2010 a = *(u_int32_t *)hp->h_addr;
2012 np = getnetbyname (ptr_word);
2013 if (np != NULL && np->n_addrtype == AF_INET) {
2014 a = htonl ((u_int32_t)np->n_net);
2015 if (ptr_mask == NULL) {
2016 /* calculate appropriate mask for net */
2019 mask = IN_CLASSA_NET;
2020 else if (IN_CLASSB(ah))
2021 mask = IN_CLASSB_NET;
2022 else if (IN_CLASSC(ah))
2023 mask = IN_CLASSC_NET;
2026 a = inet_addr (ptr_word);
2030 if (ptr_mask != NULL)
2033 if (a == (u_int32_t)-1L) {
2034 warn("unknown host %s in auth. address list", ap->word);
2038 if (offset >= ~mask) {
2039 warn("interface unit %d too large for subnet %v",
2043 a = htonl((ntohl(a) & mask) + offset);
2044 mask = ~(u_int32_t)0;
2046 ip[n].mask = htonl(mask);
2047 ip[n].base = a & ip[n].mask;
2049 if (~mask == 0 && suggested_ip == 0)
2054 ip[n].permit = 0; /* make the last entry forbid all addresses */
2055 ip[n].base = 0; /* to terminate the list */
2058 addresses[unit] = ip;
2061 * If the address given for the peer isn't authorized, or if
2062 * the user hasn't given one, AND there is an authorized address
2063 * which is a single host, then use that if we find one.
2065 if (suggested_ip != 0
2066 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
2067 wo->hisaddr = suggested_ip;
2069 * Do we insist on this address? No, if there are other
2070 * addresses authorized than the suggested one.
2073 wo->accept_remote = 1;
2078 * auth_ip_addr - check whether the peer is authorized to use
2079 * a given IP address. Returns 1 if authorized, 0 otherwise.
2082 auth_ip_addr(unit, addr)
2088 /* don't allow loopback or multicast address */
2089 if (bad_ip_adrs(addr))
2092 if (allowed_address_hook) {
2093 ok = allowed_address_hook(addr);
2094 if (ok >= 0) return ok;
2097 if (addresses[unit] != NULL) {
2098 ok = ip_addr_check(addr, addresses[unit]);
2104 return 0; /* no addresses authorized */
2105 return allow_any_ip || privileged || !have_route_to(addr);
2109 ip_addr_check(addr, addrs)
2111 struct permitted_ip *addrs;
2114 if ((addr & addrs->mask) == addrs->base)
2115 return addrs->permit;
2119 * bad_ip_adrs - return 1 if the IP address is one we don't want
2120 * to use, such as an address in the loopback net or a multicast address.
2121 * addr is in network byte order.
2128 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2129 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2133 * some_ip_ok - check a wordlist to see if it authorizes any
2138 struct wordlist *addrs;
2140 for (; addrs != 0; addrs = addrs->next) {
2141 if (addrs->word[0] == '-')
2143 if (addrs->word[0] != '!')
2144 return 1; /* some IP address is allowed */
2150 * auth_number - check whether the remote number is allowed to connect.
2151 * Returns 1 if authorized, 0 otherwise.
2156 struct wordlist *wp = permitted_numbers;
2159 /* Allow all if no authorization list. */
2163 /* Allow if we have a match in the authorization list. */
2165 /* trailing '*' wildcard */
2166 l = strlen(wp->word);
2167 if ((wp->word)[l - 1] == '*')
2169 if (!strncasecmp(wp->word, remote_number, l))
2178 * check_access - complain if a secret file has too-liberal permissions.
2181 check_access(f, filename)
2187 if (fstat(fileno(f), &sbuf) < 0) {
2188 warn("cannot stat secret file %s: %m", filename);
2189 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2190 warn("Warning - secret file %s has world and/or group access",
2197 * scan_authfile - Scan an authorization file for a secret suitable
2198 * for authenticating `client' on `server'. The return value is -1
2199 * if no secret is found, otherwise >= 0. The return value has
2200 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2201 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2202 * Any following words on the line up to a "--" (i.e. address authorization
2203 * info) are placed in a wordlist and returned in *addrs. Any
2204 * following words (extra options) are placed in a wordlist and
2205 * returned in *opts.
2206 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2207 * Flags are non-zero if we need two colons in the secret in order to
2211 scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
2216 struct wordlist **addrs;
2217 struct wordlist **opts;
2222 int got_flag, best_flag;
2224 struct wordlist *ap, *addr_list, *alist, **app;
2225 char word[MAXWORDLEN];
2226 char atfile[MAXWORDLEN];
2227 char lsecret[MAXWORDLEN];
2235 if (!getword(f, word, &newline, filename))
2236 return -1; /* file is empty??? */
2241 * Skip until we find a word at the start of a line.
2243 while (!newline && getword(f, word, &newline, filename))
2246 break; /* got to end of file */
2249 * Got a client - check if it's a match or a wildcard.
2252 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2257 got_flag = NONWILD_CLIENT;
2260 * Now get a server and check if it matches.
2262 if (!getword(f, word, &newline, filename))
2266 if (!ISWILD(word)) {
2267 if (server != NULL && strcmp(word, server) != 0)
2269 got_flag |= NONWILD_SERVER;
2273 * Got some sort of a match - see if it's better than what
2276 if (got_flag <= best_flag)
2282 if (!getword(f, word, &newline, filename))
2288 * SRP-SHA1 authenticator should never be reading secrets from
2289 * a file. (Authenticatee may, though.)
2291 if (flags && ((cp = strchr(word, ':')) == NULL ||
2292 strchr(cp + 1, ':') == NULL))
2295 if (secret != NULL) {
2297 * Special syntax: @/pathname means read secret from file.
2299 if (word[0] == '@' && word[1] == '/') {
2300 strlcpy(atfile, word+1, sizeof(atfile));
2301 if ((sf = fopen(atfile, "r")) == NULL) {
2302 warn("can't open indirect secret file %s", atfile);
2305 check_access(sf, atfile);
2306 if (!getword(sf, word, &xxx, atfile)) {
2307 warn("no secret in indirect secret file %s", atfile);
2313 strlcpy(lsecret, word, sizeof(lsecret));
2317 * Now read address authorization info and make a wordlist.
2321 if (!getword(f, word, &newline, filename) || newline)
2323 ap = (struct wordlist *)
2324 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2326 novm("authorized addresses");
2327 ap->word = (char *) (ap + 1);
2328 strcpy(ap->word, word);
2335 * This is the best so far; remember it.
2337 best_flag = got_flag;
2339 free_wordlist(addr_list);
2342 strlcpy(secret, lsecret, MAXWORDLEN);
2348 /* scan for a -- word indicating the start of options */
2349 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2350 if (strcmp(ap->word, "--") == 0)
2352 /* ap = start of options */
2354 ap = ap->next; /* first option */
2355 free(*app); /* free the "--" word */
2356 *app = NULL; /* terminate addr list */
2360 else if (ap != NULL)
2364 else if (addr_list != NULL)
2365 free_wordlist(addr_list);
2371 * wordlist_count - return the number of items in a wordlist
2375 struct wordlist *wp;
2379 for (n = 0; wp != NULL; wp = wp->next)
2385 * free_wordlist - release memory allocated for a wordlist.
2389 struct wordlist *wp;
2391 struct wordlist *next;
2393 while (wp != NULL) {
2401 * auth_script_done - called when the auth-up or auth-down script
2405 auth_script_done(arg)
2408 auth_script_pid = 0;
2409 switch (auth_script_state) {
2411 if (auth_state == s_down) {
2412 auth_script_state = s_down;
2413 auth_script(_PATH_AUTHDOWN);
2417 if (auth_state == s_up) {
2418 auth_script_state = s_up;
2419 auth_script(_PATH_AUTHUP);
2426 * auth_script - execute a script with arguments
2427 * interface-name peer-name real-user tty speed
2439 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2440 user_name = pw->pw_name;
2442 slprintf(struid, sizeof(struid), "%d", getuid());
2445 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2449 argv[2] = peer_authname;
2450 argv[3] = user_name;
2455 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);
2461 have_eaptls_secret_server(client, server, need_ip, lacks_ipp)
2470 struct wordlist *addrs;
2471 char servcertfile[MAXWORDLEN];
2472 char clicertfile[MAXWORDLEN];
2473 char cacertfile[MAXWORDLEN];
2474 char pkfile[MAXWORDLEN];
2476 filename = _PATH_EAPTLSSERVFILE;
2477 f = fopen(filename, "r");
2481 if (client != NULL && client[0] == 0)
2483 else if (server != NULL && server[0] == 0)
2487 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2488 cacertfile, pkfile, &addrs, NULL, filename,
2494 if (ret >= 0 && !eaptls_init_ssl(1, cacertfile, servcertfile,
2495 clicertfile, pkfile))
2499 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
2505 free_wordlist(addrs);
2512 have_eaptls_secret_client(client, server)
2519 struct wordlist *addrs = NULL;
2520 char servcertfile[MAXWORDLEN];
2521 char clicertfile[MAXWORDLEN];
2522 char cacertfile[MAXWORDLEN];
2523 char pkfile[MAXWORDLEN];
2525 if (client != NULL && client[0] == 0)
2527 else if (server != NULL && server[0] == 0)
2530 if ((cacert_file || ca_path) && cert_file && privkey_file)
2533 filename = _PATH_EAPTLSCLIFILE;
2534 f = fopen(filename, "r");
2539 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2540 cacertfile, pkfile, &addrs, NULL, filename,
2545 if (ret >= 0 && !eaptls_init_ssl(0, cacertfile, clicertfile,
2546 servcertfile, pkfile))
2551 free_wordlist(addrs);
2558 scan_authfile_eaptls(f, client, server, cli_cert, serv_cert, ca_cert, pk,
2559 addrs, opts, filename, flags)
2567 struct wordlist **addrs;
2568 struct wordlist **opts;
2573 int got_flag, best_flag;
2574 struct wordlist *ap, *addr_list, *alist, **app;
2575 char word[MAXWORDLEN];
2582 if (!getword(f, word, &newline, filename))
2583 return -1; /* file is empty??? */
2588 * Skip until we find a word at the start of a line.
2590 while (!newline && getword(f, word, &newline, filename));
2592 break; /* got to end of file */
2595 * Got a client - check if it's a match or a wildcard.
2598 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2603 got_flag = NONWILD_CLIENT;
2606 * Now get a server and check if it matches.
2608 if (!getword(f, word, &newline, filename))
2612 if (!ISWILD(word)) {
2613 if (server != NULL && strcmp(word, server) != 0)
2615 got_flag |= NONWILD_SERVER;
2619 * Got some sort of a match - see if it's better than what
2622 if (got_flag <= best_flag)
2628 if (!getword(f, word, &newline, filename))
2632 if (strcmp(word, "-") != 0) {
2633 strlcpy(cli_cert, word, MAXWORDLEN);
2640 if (!getword(f, word, &newline, filename))
2644 if (strcmp(word, "-") != 0) {
2645 strlcpy(serv_cert, word, MAXWORDLEN);
2652 if (!getword(f, word, &newline, filename))
2656 strlcpy(ca_cert, word, MAXWORDLEN);
2661 if (!getword(f, word, &newline, filename))
2665 strlcpy(pk, word, MAXWORDLEN);
2669 * Now read address authorization info and make a wordlist.
2673 if (!getword(f, word, &newline, filename) || newline)
2675 ap = (struct wordlist *)
2676 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2678 novm("authorized addresses");
2679 ap->word = (char *) (ap + 1);
2680 strcpy(ap->word, word);
2686 * This is the best so far; remember it.
2688 best_flag = got_flag;
2690 free_wordlist(addr_list);
2697 /* scan for a -- word indicating the start of options */
2698 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2699 if (strcmp(ap->word, "--") == 0)
2701 /* ap = start of options */
2703 ap = ap->next; /* first option */
2704 free(*app); /* free the "--" word */
2705 *app = NULL; /* terminate addr list */
2709 else if (ap != NULL)
2713 else if (addr_list != NULL)
2714 free_wordlist(addr_list);
2721 get_eaptls_secret(unit, client, server, clicertfile, servcertfile,
2722 cacertfile, capath, pkfile, am_server)
2735 char *filename = NULL;
2736 struct wordlist *addrs = NULL;
2737 struct wordlist *opts = NULL;
2739 /* maybe overkill, but it eases debugging */
2740 bzero(clicertfile, MAXWORDLEN);
2741 bzero(servcertfile, MAXWORDLEN);
2742 bzero(cacertfile, MAXWORDLEN);
2743 bzero(capath, MAXWORDLEN);
2744 bzero(pkfile, MAXWORDLEN);
2746 /* the ca+cert+privkey can also be specified as options */
2747 if (!am_server && (cacert_file || ca_path) && cert_file && privkey_file )
2749 strlcpy( clicertfile, cert_file, MAXWORDLEN );
2751 strlcpy( cacertfile, cacert_file, MAXWORDLEN );
2753 strlcpy( capath, ca_path, MAXWORDLEN );
2754 strlcpy( pkfile, privkey_file, MAXWORDLEN );
2758 filename = (am_server ? _PATH_EAPTLSSERVFILE : _PATH_EAPTLSCLIFILE);
2761 fp = fopen(filename, "r");
2764 error("Can't open eap-tls secret file %s: %m", filename);
2768 check_access(fp, filename);
2770 ret = scan_authfile_eaptls(fp, client, server, clicertfile, servcertfile,
2771 cacertfile, pkfile, &addrs, &opts, filename, 0);
2775 if (ret < 0) return 0;
2778 if (eaptls_passwd_hook)
2780 dbglog( "Calling eaptls password hook" );
2781 if ( (*eaptls_passwd_hook)(pkfile, passwd) < 0)
2783 error("Unable to obtain EAP-TLS password for %s (%s) from plugin",
2789 set_allowed_addrs(unit, addrs, opts);
2790 else if (opts != NULL)
2791 free_wordlist(opts);
2793 free_wordlist(addrs);