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 $"
86 #include <sys/types.h>
88 #include <sys/socket.h>
91 #if defined(_PATH_LASTLOG) && defined(__linux__)
96 #include <netinet/in.h>
97 #include <arpa/inet.h>
103 #define PW_PPP PW_LOGIN
113 #include <systemd/sd-daemon.h>
123 #include "chap-new.h"
131 #include "pathnames.h"
135 /* Bits in scan_authfile return value */
136 #define NONWILD_SERVER 1
137 #define NONWILD_CLIENT 2
139 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
141 /* The name by which the peer authenticated itself to us. */
142 char peer_authname[MAXNAMELEN];
144 /* Records which authentication operations haven't completed yet. */
145 static int auth_pending[NUM_PPP];
147 /* Records which authentication operations have been completed. */
148 int auth_done[NUM_PPP];
150 /* List of addresses which the peer may use. */
151 static struct permitted_ip *addresses[NUM_PPP];
153 /* Wordlist giving addresses which the peer may use
154 without authenticating itself. */
155 static struct wordlist *noauth_addrs;
157 /* Remote telephone number, if available */
158 char remote_number[MAXNAMELEN];
160 /* Wordlist giving remote telephone numbers which may connect. */
161 static struct wordlist *permitted_numbers;
163 /* Extra options to apply, from the secrets file entry for the peer. */
164 static struct wordlist *extra_options;
166 /* Number of network protocols which we have opened. */
167 static int num_np_open;
169 /* Number of network protocols which have come up. */
170 static int num_np_up;
172 /* Set if we got the contents of passwd[] from the pap-secrets file. */
173 static int passwd_from_file;
175 /* Set if we require authentication only because we have a default route. */
176 static bool default_auth;
178 /* Hook to enable a plugin to control the idle time limit */
179 int (*idle_time_hook)(struct ppp_idle *) = NULL;
181 /* Hook for a plugin to say whether we can possibly authenticate any peer */
182 int (*pap_check_hook)(void) = NULL;
184 /* Hook for a plugin to check the PAP user and password */
185 int (*pap_auth_hook)(char *user, char *passwd, char **msgp,
186 struct wordlist **paddrs,
187 struct wordlist **popts) = NULL;
189 /* Hook for a plugin to know about the PAP user logout */
190 void (*pap_logout_hook)(void) = NULL;
192 /* Hook for a plugin to get the PAP password for authenticating us */
193 int (*pap_passwd_hook)(char *user, char *passwd) = NULL;
195 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
196 int (*chap_check_hook)(void) = NULL;
198 /* Hook for a plugin to get the CHAP password for authenticating us */
199 int (*chap_passwd_hook)(char *user, char *passwd) = NULL;
202 /* Hook for a plugin to get the EAP-TLS password for authenticating us */
203 int (*eaptls_passwd_hook)(char *user, char *passwd) = NULL;
206 /* Hook for a plugin to say whether it is OK if the peer
207 refuses to authenticate. */
208 int (*null_auth_hook)(struct wordlist **paddrs,
209 struct wordlist **popts) = NULL;
211 int (*allowed_address_hook)(u_int32_t addr) = NULL;
213 #ifdef HAVE_MULTILINK
214 /* Hook for plugin to hear when an interface joins a multilink bundle */
215 void (*multilink_join_hook)(void) = NULL;
218 /* A notifier for when the peer has authenticated itself,
219 and we are proceeding to the network phase. */
220 struct notifier *auth_up_notifier = NULL;
222 /* A notifier for when the link goes down. */
223 struct notifier *link_down_notifier = NULL;
226 * This is used to ensure that we don't start an auth-up/down
227 * script while one is already running.
234 static enum script_state auth_state = s_down;
235 static enum script_state auth_script_state = s_down;
236 static pid_t auth_script_pid = 0;
241 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
242 bool session_mgmt = 0; /* Do session management (login records) */
243 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
244 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
245 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
246 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
248 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
249 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
251 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
252 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
254 bool usehostname = 0; /* Use hostname for our_name */
255 bool auth_required = 0; /* Always require authentication from peer */
256 bool allow_any_ip = 0; /* Allow peer to use any IP address */
257 bool explicit_remote = 0; /* User specified explicit remote name */
258 bool explicit_user = 0; /* Set if "user" option supplied */
259 bool explicit_passwd = 0; /* Set if "password" option supplied */
260 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
262 char *cacert_file = NULL; /* CA certificate file (pem format) */
263 char *ca_path = NULL; /* directory with CA certificates */
264 char *cert_file = NULL; /* client certificate file (pem format) */
265 char *privkey_file = NULL; /* client private key file (pem format) */
266 char *pkcs12_file = NULL; /* client private key envelope file (pkcs12 format) */
267 char *crl_dir = NULL; /* directory containing CRL files */
268 char *crl_file = NULL; /* Certificate Revocation List (CRL) file (pem format) */
269 char *max_tls_version = NULL; /* Maximum TLS protocol version (default=1.2) */
270 char *tls_verify_method = NULL;
271 bool tls_verify_key_usage = 0;
272 bool need_peer_eap = 0; /* Require peer to authenticate us */
274 bool tls_verify_cert = 0; /* Do not verify server's SSL certificate */
276 static char *uafname; /* name of most recent +ua file */
278 /* Prototypes for procedures local to this file. */
280 static void network_phase (int);
281 static void check_idle (void *);
282 static void connect_time_expired (void *);
283 static int null_login (int);
284 static int get_pap_passwd (char *);
285 static int have_pap_secret (int *);
286 static int have_chap_secret (char *, char *, int, int *);
287 static int have_srp_secret(char *client, char *server, int need_ip,
291 static int have_eaptls_secret_server
292 (char *client, char *server, int need_ip, int *lacks_ipp);
293 static int have_eaptls_secret_client (char *client, char *server);
294 static int scan_authfile_eaptls(FILE * f, char *client, char *server,
295 char *cli_cert, char *serv_cert,
296 char *ca_cert, char *pk,
297 struct wordlist ** addrs,
298 struct wordlist ** opts,
299 char *filename, int flags);
302 static int ip_addr_check (u_int32_t, struct permitted_ip *);
303 static int scan_authfile(FILE *, char *, char *, char *,
304 struct wordlist **, struct wordlist **,
306 static void free_wordlist (struct wordlist *);
307 static void auth_script (char *);
308 static void auth_script_done (void *);
309 static void set_allowed_addrs (int, struct wordlist *, struct wordlist *);
310 static int some_ip_ok (struct wordlist *);
311 static int setupapfile (char **);
312 static int privgroup (char **);
313 static int set_noauth_addr (char **);
314 static int set_permitted_number (char **);
315 static void check_access (FILE *, char *);
316 static int wordlist_count (struct wordlist *);
319 static void check_maxoctets (void *);
323 * Authentication-related options.
325 option_t auth_options[] = {
326 { "auth", o_bool, &auth_required,
327 "Require authentication from peer", OPT_PRIO | 1 },
328 { "noauth", o_bool, &auth_required,
329 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
331 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
332 "Require PAP authentication from peer",
333 OPT_PRIOSUB | 1, &auth_required },
334 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
335 "Require PAP authentication from peer",
336 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
337 { "require-chap", o_bool, &auth_required,
338 "Require CHAP authentication from peer",
339 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
340 &lcp_wantoptions[0].chap_mdtype },
341 { "+chap", o_bool, &auth_required,
342 "Require CHAP authentication from peer",
343 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
344 &lcp_wantoptions[0].chap_mdtype },
346 { "require-mschap", o_bool, &auth_required,
347 "Require MS-CHAP authentication from peer",
348 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
349 &lcp_wantoptions[0].chap_mdtype },
350 { "+mschap", o_bool, &auth_required,
351 "Require MS-CHAP authentication from peer",
352 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
353 &lcp_wantoptions[0].chap_mdtype },
354 { "require-mschap-v2", o_bool, &auth_required,
355 "Require MS-CHAPv2 authentication from peer",
356 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
357 &lcp_wantoptions[0].chap_mdtype },
358 { "+mschap-v2", o_bool, &auth_required,
359 "Require MS-CHAPv2 authentication from peer",
360 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
361 &lcp_wantoptions[0].chap_mdtype },
364 { "refuse-pap", o_bool, &refuse_pap,
365 "Don't agree to auth to peer with PAP", 1 },
366 { "-pap", o_bool, &refuse_pap,
367 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
368 { "refuse-chap", o_bool, &refuse_chap,
369 "Don't agree to auth to peer with CHAP",
370 OPT_A2CLRB | MDTYPE_MD5,
371 &lcp_allowoptions[0].chap_mdtype },
372 { "-chap", o_bool, &refuse_chap,
373 "Don't allow CHAP authentication with peer",
374 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
375 &lcp_allowoptions[0].chap_mdtype },
377 { "refuse-mschap", o_bool, &refuse_mschap,
378 "Don't agree to auth to peer with MS-CHAP",
379 OPT_A2CLRB | MDTYPE_MICROSOFT,
380 &lcp_allowoptions[0].chap_mdtype },
381 { "-mschap", o_bool, &refuse_mschap,
382 "Don't allow MS-CHAP authentication with peer",
383 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
384 &lcp_allowoptions[0].chap_mdtype },
385 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
386 "Don't agree to auth to peer with MS-CHAPv2",
387 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
388 &lcp_allowoptions[0].chap_mdtype },
389 { "-mschap-v2", o_bool, &refuse_mschap_v2,
390 "Don't allow MS-CHAPv2 authentication with peer",
391 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
392 &lcp_allowoptions[0].chap_mdtype },
395 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
396 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
398 { "refuse-eap", o_bool, &refuse_eap,
399 "Don't agree to authenticate to peer with EAP", 1 },
401 { "name", o_string, our_name,
402 "Set local name for authentication",
403 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
405 { "+ua", o_special, (void *)setupapfile,
406 "Get PAP user and password from file",
407 OPT_PRIO | OPT_A2STRVAL, &uafname },
409 { "user", o_string, user,
410 "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
411 &explicit_user, MAXNAMELEN },
413 { "password", o_string, passwd,
414 "Password for authenticating us to the peer",
415 OPT_PRIO | OPT_STATIC | OPT_HIDE,
416 &explicit_passwd, MAXSECRETLEN },
418 { "usehostname", o_bool, &usehostname,
419 "Must use hostname for authentication", 1 },
421 { "remotename", o_string, remote_name,
422 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
423 &explicit_remote, MAXNAMELEN },
425 { "login", o_bool, &uselogin,
426 "Use system password database for PAP", OPT_A2COPY | 1 ,
428 { "enable-session", o_bool, &session_mgmt,
429 "Enable session accounting for remote peers", OPT_PRIV | 1 },
431 { "papcrypt", o_bool, &cryptpap,
432 "PAP passwords are encrypted", 1 },
434 { "privgroup", o_special, (void *)privgroup,
435 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
437 { "allow-ip", o_special, (void *)set_noauth_addr,
438 "Set IP address(es) which can be used without authentication",
439 OPT_PRIV | OPT_A2LIST },
441 { "remotenumber", o_string, remote_number,
442 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
445 { "allow-number", o_special, (void *)set_permitted_number,
446 "Set telephone number(s) which are allowed to connect",
447 OPT_PRIV | OPT_A2LIST },
449 { "tls-verify-certificate", o_bool, &tls_verify_cert,
450 "Enable server's SSL certificate validation", 1 },
453 { "ca", o_string, &cacert_file, "EAP-TLS CA certificate in PEM format" },
454 { "capath", o_string, &ca_path, "EAP-TLS CA certificate directory" },
455 { "cert", o_string, &cert_file, "EAP-TLS client certificate in PEM format" },
456 { "key", o_string, &privkey_file, "EAP-TLS client private key in PEM format" },
457 { "crl-dir", o_string, &crl_dir, "Use CRLs in directory" },
458 { "crl", o_string, &crl_file, "Use specific CRL file" },
459 { "pkcs12", o_string, &pkcs12_file, "EAP-TLS client credentials in PKCS12 format" },
460 { "max-tls-version", o_string, &max_tls_version,
461 "Maximum TLS version (1.0/1.1/1.2 (default)/1.3)" },
462 { "tls-verify-key-usage", o_bool, &tls_verify_key_usage,
463 "Verify certificate type and extended key usage" },
464 { "tls-verify-method", o_string, &tls_verify_method,
465 "Verify peer by method (none|subject|name|suffix)" },
466 { "need-peer-eap", o_bool, &need_peer_eap,
467 "Require the peer to authenticate us", 1 },
468 #endif /* USE_EAPTLS */
473 * setupapfile - specifies UPAP info for authenticating with peer.
476 setupapfile(char **argv)
481 char u[MAXNAMELEN], p[MAXSECRETLEN];
484 lcp_allowoptions[0].neg_upap = 1;
486 /* open user info file */
487 fname = strdup(*argv);
489 novm("+ua file name");
491 if (seteuid(getuid()) == -1) {
492 option_error("unable to reset uid before opening %s: %m", fname);
496 ufile = fopen(fname, "r");
497 if (seteuid(euid) == -1)
498 fatal("unable to regain privileges: %m");
500 option_error("unable to open user login data file %s", fname);
504 check_access(ufile, fname);
508 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
509 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
511 option_error("unable to read user login data file %s", fname);
517 /* get rid of newlines */
519 if (l > 0 && u[l-1] == '\n')
522 if (l > 0 && p[l-1] == '\n')
525 if (override_value("user", option_priority, fname)) {
526 strlcpy(user, u, sizeof(user));
529 if (override_value("passwd", option_priority, fname)) {
530 strlcpy(passwd, p, sizeof(passwd));
540 * privgroup - allow members of the group to have privileged access.
543 privgroup(char **argv)
550 option_error("group %s is unknown", *argv);
553 for (i = 0; i < ngroups; ++i) {
554 if (groups[i] == g->gr_gid) {
564 * set_noauth_addr - set address(es) that can be used without authentication.
565 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
568 set_noauth_addr(char **argv)
571 int l = strlen(addr) + 1;
574 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
576 novm("allow-ip argument");
577 wp->word = (char *) (wp + 1);
578 wp->next = noauth_addrs;
579 BCOPY(addr, wp->word, l);
586 * set_permitted_number - set remote telephone number(s) that may connect.
589 set_permitted_number(char **argv)
591 char *number = *argv;
592 int l = strlen(number) + 1;
595 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
597 novm("allow-number argument");
598 wp->word = (char *) (wp + 1);
599 wp->next = permitted_numbers;
600 BCOPY(number, wp->word, l);
601 permitted_numbers = wp;
607 * An Open on LCP has requested a change from Dead to Establish phase.
610 link_required(int unit)
615 * Bring the link up to the point of being able to do ppp.
617 void start_link(int unit)
619 status = EXIT_CONNECT_FAILED;
620 new_phase(PHASE_SERIALCONN);
623 devfd = the_channel->connect();
627 /* set up the serial device as a ppp interface */
629 * N.B. we used to do tdb_writelock/tdb_writeunlock around this
630 * (from establish_ppp to set_ifunit). However, we won't be
631 * doing the set_ifunit in multilink mode, which is the only time
632 * we need the atomicity that the tdb_writelock/tdb_writeunlock
633 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
635 fd_ppp = the_channel->establish_ppp(devfd);
637 status = EXIT_FATAL_ERROR;
641 if (!demand && ifunit >= 0)
645 * Start opening the connection and wait for
646 * incoming events (reply, timeout, etc.).
649 notice("Connect: %s <--> %s", ifname, ppp_devnam);
651 notice("Starting negotiation on %s", ppp_devnam);
654 status = EXIT_NEGOTIATION_FAILED;
655 new_phase(PHASE_ESTABLISH);
661 new_phase(PHASE_DISCONNECT);
662 if (the_channel->disconnect)
663 the_channel->disconnect();
666 new_phase(PHASE_DEAD);
667 if (the_channel->cleanup)
668 (*the_channel->cleanup)();
672 * LCP has terminated the link; go to the Dead phase and take the
673 * physical layer down.
676 link_terminated(int unit)
678 if (phase == PHASE_DEAD || phase == PHASE_MASTER)
680 new_phase(PHASE_DISCONNECT);
682 if (pap_logout_hook) {
687 if (!doing_multilink) {
688 notice("Connection terminated.");
691 notice("Link terminated.");
694 * Delete pid files before disestablishing ppp. Otherwise it
695 * can happen that another pppd gets the same unit and then
696 * we delete its pid file.
698 if (!doing_multilink && !demand)
702 * If we may want to bring the link up again, transfer
703 * the ppp unit back to the loopback. Set the
704 * real serial device back to its normal mode of operation.
709 the_channel->disestablish_ppp(devfd);
716 if (!doing_multilink && !demand)
717 script_unsetenv("IFNAME");
720 * Run disconnector script, if requested.
721 * XXX we may not be able to do this if the line has hung up!
723 if (devfd >= 0 && the_channel->disconnect) {
724 the_channel->disconnect();
727 if (the_channel->cleanup)
728 (*the_channel->cleanup)();
730 if (doing_multilink && multilink_master) {
731 if (!bundle_terminating) {
732 new_phase(PHASE_MASTER);
733 if (master_detach && !detached)
736 mp_bundle_terminated();
738 new_phase(PHASE_DEAD);
742 * LCP has gone down; it will either die or try to re-establish.
747 if (auth_state != s_down) {
748 notify(link_down_notifier, 0);
750 if (auth_script_state == s_up && auth_script_pid == 0) {
751 update_link_stats(unit);
752 auth_script_state = s_down;
753 auth_script(_PATH_AUTHDOWN);
756 if (!doing_multilink) {
757 upper_layers_down(unit);
758 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
759 new_phase(PHASE_ESTABLISH);
761 /* XXX if doing_multilink, should do something to stop
762 network-layer traffic on the link */
765 void upper_layers_down(int unit)
768 struct protent *protp;
770 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
771 if (!protp->enabled_flag)
773 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
774 (*protp->lowerdown)(unit);
775 if (protp->protocol < 0xC000 && protp->close != NULL)
776 (*protp->close)(unit, "LCP down");
783 * The link is established.
784 * Proceed to the Dead, Authenticate or Network phase as appropriate.
787 link_established(int unit)
790 lcp_options *wo = &lcp_wantoptions[unit];
791 lcp_options *go = &lcp_gotoptions[unit];
792 lcp_options *ho = &lcp_hisoptions[unit];
794 lcp_options *ao = &lcp_allowoptions[unit];
797 struct protent *protp;
800 * Tell higher-level protocols that LCP is up.
802 if (!doing_multilink) {
803 for (i = 0; (protp = protocols[i]) != NULL; ++i)
804 if (protp->protocol != PPP_LCP && protp->enabled_flag
805 && protp->lowerup != NULL)
806 (*protp->lowerup)(unit);
809 if (!auth_required && noauth_addrs != NULL)
810 set_allowed_addrs(unit, NULL, NULL);
812 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
814 * We wanted the peer to authenticate itself, and it refused:
815 * if we have some address(es) it can use without auth, fine,
816 * otherwise treat it as though it authenticated with PAP using
817 * a username of "" and a password of "". If that's not OK,
820 if (noauth_addrs != NULL) {
821 set_allowed_addrs(unit, NULL, NULL);
822 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
823 warn("peer refused to authenticate: terminating link");
824 status = EXIT_PEER_AUTH_FAILED;
825 lcp_close(unit, "peer refused to authenticate");
831 if (need_peer_eap && !ao->neg_eap) {
832 warn("eap required to authenticate us but no suitable secrets");
833 lcp_close(unit, "couldn't negotiate eap");
834 status = EXIT_AUTH_TOPEER_FAILED;
838 if (need_peer_eap && !ho->neg_eap) {
839 warn("peer doesn't want to authenticate us with eap");
840 lcp_close(unit, "couldn't negotiate eap");
841 status = EXIT_PEER_AUTH_FAILED;
846 new_phase(PHASE_AUTHENTICATE);
849 eap_authpeer(unit, our_name);
851 } else if (go->neg_chap) {
852 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
854 } else if (go->neg_upap) {
859 eap_authwithpeer(unit, user);
860 auth |= EAP_WITHPEER;
861 } else if (ho->neg_chap) {
862 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
863 auth |= CHAP_WITHPEER;
864 } else if (ho->neg_upap) {
865 /* If a blank password was explicitly given as an option, trust
866 the user and don't try to look up one. */
867 if (passwd[0] == 0 && !explicit_passwd) {
868 passwd_from_file = 1;
869 if (!get_pap_passwd(passwd))
870 error("No secret found for PAP login");
872 upap_authwithpeer(unit, user, passwd);
873 auth |= PAP_WITHPEER;
875 auth_pending[unit] = auth;
883 * Proceed to the network phase.
886 network_phase(int unit)
888 lcp_options *go = &lcp_gotoptions[unit];
890 /* Log calling number. */
892 notice("peer from calling number %q authorized", remote_number);
895 * If the peer had to authenticate, run the auth-up script now.
897 notify(auth_up_notifier, 0);
898 if (go->neg_chap || go->neg_upap || go->neg_eap) {
900 if (auth_script_state == s_down && auth_script_pid == 0) {
901 auth_script_state = s_up;
902 auth_script(_PATH_AUTHUP);
908 * If we negotiated callback, do it now.
911 new_phase(PHASE_CALLBACK);
912 (*cbcp_protent.open)(unit);
918 * Process extra options from the secrets file
921 options_from_list(extra_options, 1);
922 free_wordlist(extra_options);
925 start_networks(unit);
929 start_networks(int unit)
932 struct protent *protp;
933 int ecp_required, mppe_required;
935 new_phase(PHASE_NETWORK);
937 #ifdef HAVE_MULTILINK
939 if (mp_join_bundle()) {
940 if (multilink_join_hook)
941 (*multilink_join_hook)();
942 if (updetach && !nodetach)
947 #endif /* HAVE_MULTILINK */
951 set_filters(&pass_filter, &active_filter);
953 /* Start CCP and ECP */
954 for (i = 0; (protp = protocols[i]) != NULL; ++i)
955 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
956 && protp->enabled_flag && protp->open != NULL)
960 * Bring up other network protocols iff encryption is not required.
962 ecp_required = ecp_gotoptions[unit].required;
963 mppe_required = ccp_gotoptions[unit].mppe;
964 if (!ecp_required && !mppe_required)
965 continue_networks(unit);
969 continue_networks(int unit)
972 struct protent *protp;
975 * Start the "real" network protocols.
977 for (i = 0; (protp = protocols[i]) != NULL; ++i)
978 if (protp->protocol < 0xC000
979 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
980 && protp->enabled_flag && protp->open != NULL) {
985 if (num_np_open == 0)
987 lcp_close(0, "No network protocols running");
991 * The peer has failed to authenticate himself using `protocol'.
994 auth_peer_fail(int unit, int protocol)
997 * Authentication failure: take the link down
999 status = EXIT_PEER_AUTH_FAILED;
1000 lcp_close(unit, "Authentication failed");
1004 * The peer has been successfully authenticated using `protocol'.
1007 auth_peer_success(int unit, int protocol, int prot_flavor,
1008 char *name, int namelen)
1015 switch (prot_flavor) {
1017 bit |= CHAP_MD5_PEER;
1020 case CHAP_MICROSOFT:
1021 bit |= CHAP_MS_PEER;
1023 case CHAP_MICROSOFT_V2:
1024 bit |= CHAP_MS2_PEER;
1036 warn("auth_peer_success: unknown protocol %x", protocol);
1041 * Save the authenticated name of the peer for later.
1043 if (namelen > sizeof(peer_authname) - 1)
1044 namelen = sizeof(peer_authname) - 1;
1045 BCOPY(name, peer_authname, namelen);
1046 peer_authname[namelen] = 0;
1047 script_setenv("PEERNAME", peer_authname, 0);
1049 /* Save the authentication method for later. */
1050 auth_done[unit] |= bit;
1053 * If there is no more authentication still to be done,
1054 * proceed to the network (or callback) phase.
1056 if ((auth_pending[unit] &= ~bit) == 0)
1057 network_phase(unit);
1061 * We have failed to authenticate ourselves to the peer using `protocol'.
1064 auth_withpeer_fail(int unit, int protocol)
1066 if (passwd_from_file)
1067 BZERO(passwd, MAXSECRETLEN);
1069 * We've failed to authenticate ourselves to our peer.
1070 * Some servers keep sending CHAP challenges, but there
1071 * is no point in persisting without any way to get updated
1072 * authentication secrets.
1074 status = EXIT_AUTH_TOPEER_FAILED;
1075 lcp_close(unit, "Failed to authenticate ourselves to peer");
1079 * We have successfully authenticated ourselves with the peer using `protocol'.
1082 auth_withpeer_success(int unit, int protocol, int prot_flavor)
1085 const char *prot = "";
1089 bit = CHAP_WITHPEER;
1091 switch (prot_flavor) {
1093 bit |= CHAP_MD5_WITHPEER;
1096 case CHAP_MICROSOFT:
1097 bit |= CHAP_MS_WITHPEER;
1099 case CHAP_MICROSOFT_V2:
1100 bit |= CHAP_MS2_WITHPEER;
1106 if (passwd_from_file)
1107 BZERO(passwd, MAXSECRETLEN);
1116 warn("auth_withpeer_success: unknown protocol %x", protocol);
1120 notice("%s authentication succeeded", prot);
1122 /* Save the authentication method for later. */
1123 auth_done[unit] |= bit;
1126 * If there is no more authentication still being done,
1127 * proceed to the network (or callback) phase.
1129 if ((auth_pending[unit] &= ~bit) == 0)
1130 network_phase(unit);
1135 * np_up - a network protocol has come up.
1138 np_up(int unit, int proto)
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(int unit, int proto)
1191 if (--num_np_up == 0) {
1192 UNTIMEOUT(check_idle, NULL);
1193 UNTIMEOUT(connect_time_expired, NULL);
1195 UNTIMEOUT(check_maxoctets, NULL);
1197 new_phase(PHASE_NETWORK);
1202 * np_finished - a network protocol has finished using the link.
1205 np_finished(int unit, int proto)
1207 if (--num_np_open <= 0) {
1208 /* no further use for the link: shut up shop. */
1209 lcp_close(0, "No network protocols running");
1215 check_maxoctets(void *arg)
1219 update_link_stats(ifunit);
1222 switch(maxoctets_dir) {
1223 case PPP_OCTETS_DIRECTION_IN:
1224 used = link_stats.bytes_in;
1226 case PPP_OCTETS_DIRECTION_OUT:
1227 used = link_stats.bytes_out;
1229 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1230 case PPP_OCTETS_DIRECTION_MAXSESSION:
1231 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
1234 used = link_stats.bytes_in+link_stats.bytes_out;
1237 if (used > maxoctets) {
1238 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1239 status = EXIT_TRAFFIC_LIMIT;
1240 lcp_close(0, "Traffic limit");
1243 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1249 * check_idle - check whether the link has been idle for long
1250 * enough that we can shut it down.
1253 check_idle(void *arg)
1255 struct ppp_idle idle;
1259 if (!get_idle_time(0, &idle))
1261 if (idle_time_hook != 0) {
1262 tlim = idle_time_hook(&idle);
1264 itime = MIN(idle.xmit_idle, idle.recv_idle);
1265 tlim = idle_time_limit - itime;
1268 /* link is idle: shut it down. */
1269 notice("Terminating connection due to lack of activity.");
1270 status = EXIT_IDLE_TIMEOUT;
1271 lcp_close(0, "Link inactive");
1274 TIMEOUT(check_idle, NULL, tlim);
1279 * connect_time_expired - log a message and close the connection.
1282 connect_time_expired(void *arg)
1284 info("Connect time expired");
1285 status = EXIT_CONNECT_TIME;
1286 lcp_close(0, "Connect time expired"); /* Close connection */
1290 * auth_check_options - called to check authentication options.
1293 auth_check_options(void)
1295 lcp_options *wo = &lcp_wantoptions[0];
1299 /* Default our_name to hostname, and user to our_name */
1300 if (our_name[0] == 0 || usehostname)
1301 strlcpy(our_name, hostname, sizeof(our_name));
1302 /* If a blank username was explicitly given as an option, trust
1303 the user and don't use our_name */
1304 if (user[0] == 0 && !explicit_user)
1305 strlcpy(user, our_name, sizeof(user));
1308 * If we have a default route, require the peer to authenticate
1309 * unless the noauth option was given or the real user is root.
1311 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1316 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1317 if (wo->chap_mdtype)
1320 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1321 if (auth_required) {
1323 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1324 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
1325 wo->chap_mdtype = chap_mdtype_all;
1331 wo->chap_mdtype = MDTYPE_NONE;
1337 * Check whether we have appropriate secrets to use
1338 * to authenticate the peer. Note that EAP can authenticate by way
1339 * of a CHAP-like exchanges as well as SRP.
1342 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1343 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1344 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1345 our_name, 1, &lacks_ip);
1347 if (!can_auth && wo->neg_eap) {
1348 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1349 our_name, 1, &lacks_ip);
1353 if (!can_auth && wo->neg_eap) {
1355 have_eaptls_secret_server((explicit_remote ? remote_name :
1356 NULL), our_name, 1, &lacks_ip);
1361 if (auth_required && !can_auth && noauth_addrs == NULL) {
1364 "By default the remote system is required to authenticate itself");
1366 "(because this system has a default route to the internet)");
1367 } else if (explicit_remote)
1369 "The remote system (%s) is required to authenticate itself",
1373 "The remote system is required to authenticate itself");
1375 "but I couldn't find any suitable secret (password) for it to use to do so.");
1378 "(None of the available passwords would let it use an IP address.)");
1384 * Early check for remote number authorization.
1386 if (!auth_number()) {
1387 warn("calling number %q is not authorized", remote_number);
1388 exit(EXIT_CNID_AUTH_FAILED);
1393 * auth_reset - called when LCP is starting negotiations to recheck
1394 * authentication options, i.e. whether we have appropriate secrets
1395 * to use for authenticating ourselves and/or the peer.
1398 auth_reset(int unit)
1400 lcp_options *go = &lcp_gotoptions[unit];
1401 lcp_options *ao = &lcp_allowoptions[unit];
1405 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1406 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1407 && ((passwd[0] != 0 || explicit_passwd) ||
1408 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1410 ao->neg_eap = !refuse_eap && (
1412 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1413 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1414 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL)
1416 || have_eaptls_secret_client(user, (explicit_remote? remote_name: NULL))
1421 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1424 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1425 our_name, 1, NULL)))
1429 (hadchap == 0 || (hadchap == -1 &&
1430 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1432 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1435 && !have_eaptls_secret_server((explicit_remote? remote_name: NULL),
1444 * check_passwd - Check the user name and passwd against the PAP secrets
1445 * file. If requested, also check against the system password database,
1446 * and login the user if OK.
1449 * UPAP_AUTHNAK: Authentication failed.
1450 * UPAP_AUTHACK: Authentication succeeded.
1451 * In either case, msg points to an appropriate message.
1454 check_passwd(int unit,
1455 char *auser, int userlen,
1456 char *apasswd, int passwdlen, char **msg)
1461 struct wordlist *addrs = NULL, *opts = NULL;
1462 char passwd[256], user[256];
1463 char secret[MAXWORDLEN];
1464 static int attempts = 0;
1467 * Make copies of apasswd and auser, then null-terminate them.
1468 * If there are unprintable characters in the password, make
1471 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1472 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1476 * Check if a plugin wants to handle this.
1478 if (pap_auth_hook) {
1479 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1481 /* note: set_allowed_addrs() saves opts (but not addrs):
1484 set_allowed_addrs(unit, addrs, opts);
1486 free_wordlist(opts);
1488 free_wordlist(addrs);
1489 BZERO(passwd, sizeof(passwd));
1490 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1495 * Open the file of pap secrets and scan for a suitable secret
1496 * for authenticating this user.
1498 filename = _PATH_UPAPFILE;
1499 addrs = opts = NULL;
1501 f = fopen(filename, "r");
1503 error("Can't open PAP password file %s: %m", filename);
1506 check_access(f, filename);
1507 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1508 warn("no PAP secret found for %s", user);
1511 * If the secret is "@login", it means to check
1512 * the password against the login database.
1514 int login_secret = strcmp(secret, "@login") == 0;
1516 if (uselogin || login_secret) {
1517 /* login option or secret is @login */
1518 if (session_full(user, passwd, devnam, msg) == 0) {
1521 } else if (session_mgmt) {
1522 if (session_check(user, NULL, devnam, NULL) == 0) {
1523 warn("Peer %q failed PAP Session verification", user);
1527 if (secret[0] != 0 && !login_secret) {
1528 /* password given in pap-secrets - must match */
1529 if (cryptpap || strcmp(passwd, secret) != 0) {
1531 char *cbuf = crypt(passwd, secret);
1532 if (!cbuf || strcmp(cbuf, secret) != 0)
1541 if (ret == UPAP_AUTHNAK) {
1543 *msg = "Login incorrect";
1545 * XXX can we ever get here more than once??
1546 * Frustrate passwd stealer programs.
1547 * Allow 10 tries, but start backing off after 3 (stolen from login).
1548 * On 10'th, drop the connection.
1550 if (attempts++ >= 10) {
1551 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1552 lcp_close(unit, "login failed");
1555 sleep((u_int) (attempts - 3) * 5);
1557 free_wordlist(opts);
1560 attempts = 0; /* Reset count */
1563 set_allowed_addrs(unit, addrs, opts);
1567 free_wordlist(addrs);
1568 BZERO(passwd, sizeof(passwd));
1569 BZERO(secret, sizeof(secret));
1575 * null_login - Check if a username of "" and a password of "" are
1576 * acceptable, and iff so, set the list of acceptable IP addresses
1580 null_login(int unit)
1585 struct wordlist *addrs, *opts;
1586 char secret[MAXWORDLEN];
1589 * Check if a plugin wants to handle this.
1593 ret = (*null_auth_hook)(&addrs, &opts);
1596 * Open the file of pap secrets and scan for a suitable secret.
1599 filename = _PATH_UPAPFILE;
1601 f = fopen(filename, "r");
1604 check_access(f, filename);
1606 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1607 ret = i >= 0 && secret[0] == 0;
1608 BZERO(secret, sizeof(secret));
1613 set_allowed_addrs(unit, addrs, opts);
1615 free_wordlist(opts);
1617 free_wordlist(addrs);
1624 * get_pap_passwd - get a password for authenticating ourselves with
1625 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1627 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1630 get_pap_passwd(char *passwd)
1635 char secret[MAXWORDLEN];
1638 * Check whether a plugin wants to supply this.
1640 if (pap_passwd_hook) {
1641 ret = (*pap_passwd_hook)(user, passwd);
1646 filename = _PATH_UPAPFILE;
1647 f = fopen(filename, "r");
1650 check_access(f, filename);
1651 ret = scan_authfile(f, user,
1652 (remote_name[0]? remote_name: NULL),
1653 secret, NULL, NULL, filename, 0);
1658 strlcpy(passwd, secret, MAXSECRETLEN);
1659 BZERO(secret, sizeof(secret));
1665 * have_pap_secret - check whether we have a PAP file with any
1666 * secrets that we could possibly use for authenticating the peer.
1669 have_pap_secret(int *lacks_ipp)
1674 struct wordlist *addrs;
1676 /* let the plugin decide, if there is one */
1677 if (pap_check_hook) {
1678 ret = (*pap_check_hook)();
1683 filename = _PATH_UPAPFILE;
1684 f = fopen(filename, "r");
1688 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1689 NULL, &addrs, NULL, filename, 0);
1691 if (ret >= 0 && !some_ip_ok(addrs)) {
1697 free_wordlist(addrs);
1704 * have_chap_secret - check whether we have a CHAP file with a
1705 * secret that we could possibly use for authenticating `client'
1706 * on `server'. Either can be the null string, meaning we don't
1707 * know the identity yet.
1710 have_chap_secret(char *client, char *server,
1711 int need_ip, int *lacks_ipp)
1716 struct wordlist *addrs;
1718 if (chap_check_hook) {
1719 ret = (*chap_check_hook)();
1725 filename = _PATH_CHAPFILE;
1726 f = fopen(filename, "r");
1730 if (client != NULL && client[0] == 0)
1732 else if (server != NULL && server[0] == 0)
1735 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1737 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1743 free_wordlist(addrs);
1750 * have_srp_secret - check whether we have a SRP file with a
1751 * secret that we could possibly use for authenticating `client'
1752 * on `server'. Either can be the null string, meaning we don't
1753 * know the identity yet.
1756 have_srp_secret(char *client, char *server, int need_ip, int *lacks_ipp)
1761 struct wordlist *addrs;
1763 filename = _PATH_SRPFILE;
1764 f = fopen(filename, "r");
1768 if (client != NULL && client[0] == 0)
1770 else if (server != NULL && server[0] == 0)
1773 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1775 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1781 free_wordlist(addrs);
1788 * get_secret - open the CHAP secret file and return the secret
1789 * for authenticating the given client on the given server.
1790 * (We could be either client or server).
1793 get_secret(int unit, char *client, char *server,
1794 char *secret, int *secret_len, int am_server)
1799 struct wordlist *addrs, *opts;
1800 char secbuf[MAXWORDLEN];
1802 if (!am_server && passwd[0] != 0) {
1803 strlcpy(secbuf, passwd, sizeof(secbuf));
1804 } else if (!am_server && chap_passwd_hook) {
1805 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1806 error("Unable to obtain CHAP password for %s on %s from plugin",
1811 filename = _PATH_CHAPFILE;
1815 f = fopen(filename, "r");
1817 error("Can't open chap secret file %s: %m", filename);
1820 check_access(f, filename);
1822 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1828 set_allowed_addrs(unit, addrs, opts);
1830 free_wordlist(opts);
1832 free_wordlist(addrs);
1835 len = strlen(secbuf);
1836 if (len > MAXSECRETLEN) {
1837 error("Secret for %s on %s is too long", client, server);
1840 BCOPY(secbuf, secret, len);
1841 BZERO(secbuf, sizeof(secbuf));
1849 * get_srp_secret - open the SRP secret file and return the secret
1850 * for authenticating the given client on the given server.
1851 * (We could be either client or server).
1854 get_srp_secret(int unit, char *client, char *server,
1855 char *secret, int am_server)
1860 struct wordlist *addrs, *opts;
1862 if (!am_server && passwd[0] != '\0') {
1863 strlcpy(secret, passwd, MAXWORDLEN);
1865 filename = _PATH_SRPFILE;
1868 fp = fopen(filename, "r");
1870 error("Can't open srp secret file %s: %m", filename);
1873 check_access(fp, filename);
1876 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1877 filename, am_server);
1883 set_allowed_addrs(unit, addrs, opts);
1884 else if (opts != NULL)
1885 free_wordlist(opts);
1887 free_wordlist(addrs);
1894 * set_allowed_addrs() - set the list of allowed addresses.
1895 * Also looks for `--' indicating options to apply for this peer
1896 * and leaves the following words in extra_options.
1899 set_allowed_addrs(int unit, struct wordlist *addrs,
1900 struct wordlist *opts)
1903 struct wordlist *ap, **plink;
1904 struct permitted_ip *ip;
1905 char *ptr_word, *ptr_mask;
1908 u_int32_t a, mask, ah, offset;
1909 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1910 u_int32_t suggested_ip = 0;
1912 if (addresses[unit] != NULL)
1913 free(addresses[unit]);
1914 addresses[unit] = NULL;
1915 if (extra_options != NULL)
1916 free_wordlist(extra_options);
1917 extra_options = opts;
1920 * Count the number of IP addresses given.
1922 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1925 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1929 /* temporarily append the noauth_addrs list to addrs */
1930 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1932 *plink = noauth_addrs;
1935 for (ap = addrs; ap != NULL; ap = ap->next) {
1936 /* "-" means no addresses authorized, "*" means any address allowed */
1937 ptr_word = ap->word;
1938 if (strcmp(ptr_word, "-") == 0)
1940 if (strcmp(ptr_word, "*") == 0) {
1942 ip[n].base = ip[n].mask = 0;
1948 if (*ptr_word == '!') {
1953 mask = ~ (u_int32_t) 0;
1955 ptr_mask = strchr (ptr_word, '/');
1956 if (ptr_mask != NULL) {
1960 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1961 if (bit_count <= 0 || bit_count > 32) {
1962 warn("invalid address length %v in auth. address list",
1966 bit_count = 32 - bit_count; /* # bits in host part */
1968 offset = ifunit + 1;
1972 warn("invalid address length syntax: %v", ptr_mask+1);
1979 hp = gethostbyname(ptr_word);
1980 if (hp != NULL && hp->h_addrtype == AF_INET) {
1981 a = *(u_int32_t *)hp->h_addr;
1983 np = getnetbyname (ptr_word);
1984 if (np != NULL && np->n_addrtype == AF_INET) {
1985 a = htonl ((u_int32_t)np->n_net);
1986 if (ptr_mask == NULL) {
1987 /* calculate appropriate mask for net */
1990 mask = IN_CLASSA_NET;
1991 else if (IN_CLASSB(ah))
1992 mask = IN_CLASSB_NET;
1993 else if (IN_CLASSC(ah))
1994 mask = IN_CLASSC_NET;
1997 a = inet_addr (ptr_word);
2001 if (ptr_mask != NULL)
2004 if (a == (u_int32_t)-1L) {
2005 warn("unknown host %s in auth. address list", ap->word);
2009 if (offset >= ~mask) {
2010 warn("interface unit %d too large for subnet %v",
2014 a = htonl((ntohl(a) & mask) + offset);
2015 mask = ~(u_int32_t)0;
2017 ip[n].mask = htonl(mask);
2018 ip[n].base = a & ip[n].mask;
2020 if (~mask == 0 && suggested_ip == 0)
2025 ip[n].permit = 0; /* make the last entry forbid all addresses */
2026 ip[n].base = 0; /* to terminate the list */
2029 addresses[unit] = ip;
2032 * If the address given for the peer isn't authorized, or if
2033 * the user hasn't given one, AND there is an authorized address
2034 * which is a single host, then use that if we find one.
2036 if (suggested_ip != 0
2037 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
2038 wo->hisaddr = suggested_ip;
2040 * Do we insist on this address? No, if there are other
2041 * addresses authorized than the suggested one.
2044 wo->accept_remote = 1;
2049 * auth_ip_addr - check whether the peer is authorized to use
2050 * a given IP address. Returns 1 if authorized, 0 otherwise.
2053 auth_ip_addr(int unit, u_int32_t addr)
2057 /* don't allow loopback or multicast address */
2058 if (bad_ip_adrs(addr))
2061 if (allowed_address_hook) {
2062 ok = allowed_address_hook(addr);
2063 if (ok >= 0) return ok;
2066 if (addresses[unit] != NULL) {
2067 ok = ip_addr_check(addr, addresses[unit]);
2073 return 0; /* no addresses authorized */
2074 return allow_any_ip || privileged || !have_route_to(addr);
2078 ip_addr_check(u_int32_t addr, struct permitted_ip *addrs)
2081 if ((addr & addrs->mask) == addrs->base)
2082 return addrs->permit;
2086 * bad_ip_adrs - return 1 if the IP address is one we don't want
2087 * to use, such as an address in the loopback net or a multicast address.
2088 * addr is in network byte order.
2091 bad_ip_adrs(u_int32_t addr)
2094 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2095 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2099 * some_ip_ok - check a wordlist to see if it authorizes any
2103 some_ip_ok(struct wordlist *addrs)
2105 for (; addrs != 0; addrs = addrs->next) {
2106 if (addrs->word[0] == '-')
2108 if (addrs->word[0] != '!')
2109 return 1; /* some IP address is allowed */
2115 * auth_number - check whether the remote number is allowed to connect.
2116 * Returns 1 if authorized, 0 otherwise.
2121 struct wordlist *wp = permitted_numbers;
2124 /* Allow all if no authorization list. */
2128 /* Allow if we have a match in the authorization list. */
2130 /* trailing '*' wildcard */
2131 l = strlen(wp->word);
2132 if ((wp->word)[l - 1] == '*')
2134 if (!strncasecmp(wp->word, remote_number, l))
2143 * check_access - complain if a secret file has too-liberal permissions.
2146 check_access(FILE *f, char *filename)
2150 if (fstat(fileno(f), &sbuf) < 0) {
2151 warn("cannot stat secret file %s: %m", filename);
2152 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2153 warn("Warning - secret file %s has world and/or group access",
2160 * scan_authfile - Scan an authorization file for a secret suitable
2161 * for authenticating `client' on `server'. The return value is -1
2162 * if no secret is found, otherwise >= 0. The return value has
2163 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2164 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2165 * Any following words on the line up to a "--" (i.e. address authorization
2166 * info) are placed in a wordlist and returned in *addrs. Any
2167 * following words (extra options) are placed in a wordlist and
2168 * returned in *opts.
2169 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2170 * Flags are non-zero if we need two colons in the secret in order to
2174 scan_authfile(FILE *f, char *client, char *server,
2175 char *secret, struct wordlist **addrs,
2176 struct wordlist **opts, char *filename,
2180 int got_flag, best_flag;
2182 struct wordlist *ap, *addr_list, *alist, **app;
2183 char word[MAXWORDLEN];
2184 char atfile[MAXWORDLEN];
2185 char lsecret[MAXWORDLEN];
2193 if (!getword(f, word, &newline, filename))
2194 return -1; /* file is empty??? */
2199 * Skip until we find a word at the start of a line.
2201 while (!newline && getword(f, word, &newline, filename))
2204 break; /* got to end of file */
2207 * Got a client - check if it's a match or a wildcard.
2210 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2215 got_flag = NONWILD_CLIENT;
2218 * Now get a server and check if it matches.
2220 if (!getword(f, word, &newline, filename))
2224 if (!ISWILD(word)) {
2225 if (server != NULL && strcmp(word, server) != 0)
2227 got_flag |= NONWILD_SERVER;
2231 * Got some sort of a match - see if it's better than what
2234 if (got_flag <= best_flag)
2240 if (!getword(f, word, &newline, filename))
2246 * SRP-SHA1 authenticator should never be reading secrets from
2247 * a file. (Authenticatee may, though.)
2249 if (flags && ((cp = strchr(word, ':')) == NULL ||
2250 strchr(cp + 1, ':') == NULL))
2253 if (secret != NULL) {
2255 * Special syntax: @/pathname means read secret from file.
2257 if (word[0] == '@' && word[1] == '/') {
2258 strlcpy(atfile, word+1, sizeof(atfile));
2259 if ((sf = fopen(atfile, "r")) == NULL) {
2260 warn("can't open indirect secret file %s", atfile);
2263 check_access(sf, atfile);
2264 if (!getword(sf, word, &xxx, atfile)) {
2265 warn("no secret in indirect secret file %s", atfile);
2271 strlcpy(lsecret, word, sizeof(lsecret));
2275 * Now read address authorization info and make a wordlist.
2279 if (!getword(f, word, &newline, filename) || newline)
2281 ap = (struct wordlist *)
2282 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2284 novm("authorized addresses");
2285 ap->word = (char *) (ap + 1);
2286 strcpy(ap->word, word);
2293 * This is the best so far; remember it.
2295 best_flag = got_flag;
2297 free_wordlist(addr_list);
2300 strlcpy(secret, lsecret, MAXWORDLEN);
2306 /* scan for a -- word indicating the start of options */
2307 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2308 if (strcmp(ap->word, "--") == 0)
2310 /* ap = start of options */
2312 ap = ap->next; /* first option */
2313 free(*app); /* free the "--" word */
2314 *app = NULL; /* terminate addr list */
2318 else if (ap != NULL)
2322 else if (addr_list != NULL)
2323 free_wordlist(addr_list);
2329 * wordlist_count - return the number of items in a wordlist
2332 wordlist_count(struct wordlist *wp)
2336 for (n = 0; wp != NULL; wp = wp->next)
2342 * free_wordlist - release memory allocated for a wordlist.
2345 free_wordlist(struct wordlist *wp)
2347 struct wordlist *next;
2349 while (wp != NULL) {
2357 * auth_script_done - called when the auth-up or auth-down script
2361 auth_script_done(void *arg)
2363 auth_script_pid = 0;
2364 switch (auth_script_state) {
2366 if (auth_state == s_down) {
2367 auth_script_state = s_down;
2368 auth_script(_PATH_AUTHDOWN);
2372 if (auth_state == s_up) {
2373 auth_script_state = s_up;
2374 auth_script(_PATH_AUTHUP);
2381 * auth_script - execute a script with arguments
2382 * interface-name peer-name real-user tty speed
2385 auth_script(char *script)
2393 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2394 user_name = pw->pw_name;
2396 slprintf(struid, sizeof(struid), "%d", getuid());
2399 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2403 argv[2] = peer_authname;
2404 argv[3] = user_name;
2409 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);
2415 have_eaptls_secret_server(char *client, char *server,
2416 int need_ip, int *lacks_ipp)
2421 struct wordlist *addrs;
2422 char servcertfile[MAXWORDLEN];
2423 char clicertfile[MAXWORDLEN];
2424 char cacertfile[MAXWORDLEN];
2425 char pkfile[MAXWORDLEN];
2427 filename = _PATH_EAPTLSSERVFILE;
2428 f = fopen(filename, "r");
2432 if (client != NULL && client[0] == 0)
2434 else if (server != NULL && server[0] == 0)
2438 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2439 cacertfile, pkfile, &addrs, NULL, filename,
2445 if (ret >= 0 && !eaptls_init_ssl(1, cacertfile, servcertfile,
2446 clicertfile, pkfile))
2450 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
2456 free_wordlist(addrs);
2463 have_eaptls_secret_client(char *client, char *server)
2468 struct wordlist *addrs = NULL;
2469 char servcertfile[MAXWORDLEN];
2470 char clicertfile[MAXWORDLEN];
2471 char cacertfile[MAXWORDLEN];
2472 char pkfile[MAXWORDLEN];
2474 if (client != NULL && client[0] == 0)
2476 else if (server != NULL && server[0] == 0)
2479 if ((cacert_file || ca_path) && cert_file && privkey_file)
2484 filename = _PATH_EAPTLSCLIFILE;
2485 f = fopen(filename, "r");
2490 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2491 cacertfile, pkfile, &addrs, NULL, filename,
2496 if (ret >= 0 && !eaptls_init_ssl(0, cacertfile, clicertfile,
2497 servcertfile, pkfile))
2502 free_wordlist(addrs);
2509 scan_authfile_eaptls(FILE *f, char *client, char *server,
2510 char *cli_cert, char *serv_cert, char *ca_cert,
2511 char *pk, struct wordlist **addrs,
2512 struct wordlist **opts,
2513 char *filename, int flags)
2516 int got_flag, best_flag;
2517 struct wordlist *ap, *addr_list, *alist, **app;
2518 char word[MAXWORDLEN];
2525 if (!getword(f, word, &newline, filename))
2526 return -1; /* file is empty??? */
2531 * Skip until we find a word at the start of a line.
2533 while (!newline && getword(f, word, &newline, filename));
2535 break; /* got to end of file */
2538 * Got a client - check if it's a match or a wildcard.
2541 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2546 got_flag = NONWILD_CLIENT;
2549 * Now get a server and check if it matches.
2551 if (!getword(f, word, &newline, filename))
2555 if (!ISWILD(word)) {
2556 if (server != NULL && strcmp(word, server) != 0)
2558 got_flag |= NONWILD_SERVER;
2562 * Got some sort of a match - see if it's better than what
2565 if (got_flag <= best_flag)
2571 if (!getword(f, word, &newline, filename))
2575 if (strcmp(word, "-") != 0) {
2576 strlcpy(cli_cert, word, MAXWORDLEN);
2583 if (!getword(f, word, &newline, filename))
2587 if (strcmp(word, "-") != 0) {
2588 strlcpy(serv_cert, word, MAXWORDLEN);
2595 if (!getword(f, word, &newline, filename))
2599 strlcpy(ca_cert, word, MAXWORDLEN);
2604 if (!getword(f, word, &newline, filename))
2608 strlcpy(pk, word, MAXWORDLEN);
2612 * Now read address authorization info and make a wordlist.
2616 if (!getword(f, word, &newline, filename) || newline)
2618 ap = (struct wordlist *)
2619 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2621 novm("authorized addresses");
2622 ap->word = (char *) (ap + 1);
2623 strcpy(ap->word, word);
2629 * This is the best so far; remember it.
2631 best_flag = got_flag;
2633 free_wordlist(addr_list);
2640 /* scan for a -- word indicating the start of options */
2641 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2642 if (strcmp(ap->word, "--") == 0)
2644 /* ap = start of options */
2646 ap = ap->next; /* first option */
2647 free(*app); /* free the "--" word */
2648 *app = NULL; /* terminate addr list */
2652 else if (ap != NULL)
2656 else if (addr_list != NULL)
2657 free_wordlist(addr_list);
2664 get_eaptls_secret(int unit, char *client, char *server,
2665 char *clicertfile, char *servcertfile, char *cacertfile,
2666 char *capath, char *pkfile, char *pkcs12, int am_server)
2670 char *filename = NULL;
2671 struct wordlist *addrs = NULL;
2672 struct wordlist *opts = NULL;
2674 /* maybe overkill, but it eases debugging */
2675 bzero(clicertfile, MAXWORDLEN);
2676 bzero(servcertfile, MAXWORDLEN);
2677 bzero(cacertfile, MAXWORDLEN);
2678 bzero(capath, MAXWORDLEN);
2679 bzero(pkfile, MAXWORDLEN);
2680 bzero(pkcs12, MAXWORDLEN);
2682 /* the ca+cert+privkey can also be specified as options */
2683 if (!am_server && (cacert_file || ca_path) && cert_file && privkey_file )
2685 strlcpy( clicertfile, cert_file, MAXWORDLEN );
2687 strlcpy( cacertfile, cacert_file, MAXWORDLEN );
2689 strlcpy( capath, ca_path, MAXWORDLEN );
2690 strlcpy( pkfile, privkey_file, MAXWORDLEN );
2692 else if (!am_server && pkcs12_file)
2694 strlcpy( pkcs12, pkcs12_file, MAXWORDLEN );
2696 strlcpy( cacertfile, cacert_file, MAXWORDLEN );
2698 strlcpy( capath, ca_path, MAXWORDLEN );
2702 filename = (am_server ? _PATH_EAPTLSSERVFILE : _PATH_EAPTLSCLIFILE);
2705 fp = fopen(filename, "r");
2708 error("Can't open eap-tls secret file %s: %m", filename);
2712 check_access(fp, filename);
2714 ret = scan_authfile_eaptls(fp, client, server, clicertfile, servcertfile,
2715 cacertfile, pkfile, &addrs, &opts, filename, 0);
2719 if (ret < 0) return 0;
2722 if (eaptls_passwd_hook)
2724 dbglog( "Calling eaptls password hook" );
2725 if ( (*eaptls_passwd_hook)(pkfile, passwd) < 0)
2727 error("Unable to obtain EAP-TLS password for %s (%s) from plugin",
2733 set_allowed_addrs(unit, addrs, opts);
2734 else if (opts != NULL)
2735 free_wordlist(opts);
2737 free_wordlist(addrs);