2 * auth.c - PPP authentication and phase control.
4 * Copyright (c) 1993-2024 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
19 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
21 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
23 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
24 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Derived from main.c, which is:
28 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in
39 * the documentation and/or other materials provided with the
42 * 3. The name "Carnegie Mellon University" must not be used to
43 * endorse or promote products derived from this software without
44 * prior written permission. For permission or any legal
45 * details, please contact
46 * Office of Technology Transfer
47 * Carnegie Mellon University
49 * Pittsburgh, PA 15213-3890
50 * (412) 268-4387, fax: (412) 268-7395
51 * tech-transfer@andrew.cmu.edu
53 * 4. Redistributions of any form whatsoever must retain the following
55 * "This product includes software developed by Computing Services
56 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
58 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
59 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
60 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
61 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
63 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
64 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
80 #include <sys/param.h>
81 #include <sys/types.h>
83 #include <sys/socket.h>
86 #if defined(_PATH_LASTLOG) && defined(__linux__)
91 #include <netinet/in.h>
92 #include <arpa/inet.h>
98 #define PW_PPP PW_LOGIN
108 #include <systemd/sd-daemon.h>
111 #include "pppd-private.h"
121 #ifdef PPP_WITH_EAPTLS
127 #include "multilink.h"
128 #include "pathnames.h"
132 /* Bits in scan_authfile return value */
133 #define NONWILD_SERVER 1
134 #define NONWILD_CLIENT 2
136 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
138 /* The name by which the peer authenticated itself to us. */
139 char peer_authname[MAXNAMELEN];
141 /* Records which authentication operations haven't completed yet. */
142 static int auth_pending[NUM_PPP];
144 /* Records which authentication operations have been completed. */
145 int auth_done[NUM_PPP];
147 /* List of addresses which the peer may use. */
148 static struct permitted_ip *addresses[NUM_PPP];
150 /* Wordlist giving addresses which the peer may use
151 without authenticating itself. */
152 static struct wordlist *noauth_addrs;
154 /* Remote telephone number, if available */
155 char remote_number[MAXNAMELEN];
157 /* Wordlist giving remote telephone numbers which may connect. */
158 static struct wordlist *permitted_numbers;
160 /* Extra options to apply, from the secrets file entry for the peer. */
161 static struct wordlist *extra_options;
163 /* Number of network protocols which we have opened. */
164 static int num_np_open;
166 /* Number of network protocols which have come up. */
167 static int num_np_up;
169 /* Set if we got the contents of passwd[] from the pap-secrets file. */
170 static int passwd_from_file;
172 /* Set if we require authentication only because we have a default route. */
173 static bool default_auth;
175 /* Hook to enable a plugin to control the idle time limit */
176 int (*idle_time_hook)(struct ppp_idle *) = NULL;
178 /* Hook for a plugin to say whether we can possibly authenticate any peer */
179 pap_check_hook_fn *pap_check_hook = NULL;
181 /* Hook for a plugin to check the PAP user and password */
182 pap_auth_hook_fn *pap_auth_hook = NULL;
184 /* Hook for a plugin to know about the PAP user logout */
185 pap_logout_hook_fn *pap_logout_hook = NULL;
187 /* Hook for a plugin to get the PAP password for authenticating us */
188 pap_passwd_hook_fn *pap_passwd_hook = NULL;
190 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
191 chap_check_hook_fn *chap_check_hook = NULL;
193 /* Hook for a plugin to get the CHAP password for authenticating us */
194 chap_passwd_hook_fn *chap_passwd_hook = NULL;
196 #ifdef PPP_WITH_EAPTLS
197 /* Hook for a plugin to get the EAP-TLS password for authenticating us */
198 eaptls_passwd_hook_fn *eaptls_passwd_hook = NULL;
201 /* Hook for a plugin to say whether it is OK if the peer
202 refuses to authenticate. */
203 int (*null_auth_hook)(struct wordlist **paddrs,
204 struct wordlist **popts) = NULL;
206 int (*allowed_address_hook)(u_int32_t addr) = NULL;
208 /* A notifier for when the peer has authenticated itself,
209 and we are proceeding to the network phase. */
210 struct notifier *auth_up_notifier = NULL;
212 /* A notifier for when the link goes down. */
213 struct notifier *link_down_notifier = NULL;
216 * This is used to ensure that we don't start an auth-up/down
217 * script while one is already running.
224 static enum script_state auth_state = s_down;
225 static enum script_state auth_script_state = s_down;
226 static pid_t auth_script_pid = 0;
231 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
232 bool session_mgmt = 0; /* Do session management (login records) */
233 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
234 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
235 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
236 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
237 #ifdef PPP_WITH_CHAPMS
238 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
239 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
241 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
242 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
244 bool usehostname = 0; /* Use hostname for our_name */
245 bool auth_required = 0; /* Always require authentication from peer */
246 bool allow_any_ip = 0; /* Allow peer to use any IP address */
247 bool explicit_remote = 0; /* User specified explicit remote name */
248 bool explicit_user = 0; /* Set if "user" option supplied */
249 bool explicit_passwd = 0; /* Set if "password" option supplied */
250 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
251 char path_upapfile[MAXPATHLEN]; /* Pathname of pap-secrets file */
252 char path_chapfile[MAXPATHLEN]; /* Pathname of chap-secrets file */
254 #if defined(PPP_WITH_EAPTLS) || defined(PPP_WITH_PEAP)
255 char *cacert_file = NULL; /* CA certificate file (pem format) */
256 char *ca_path = NULL; /* Directory with CA certificates */
257 char *crl_dir = NULL; /* Directory containing CRL files */
258 char *crl_file = NULL; /* Certificate Revocation List (CRL) file (pem format) */
259 char *max_tls_version = NULL; /* Maximum TLS protocol version (default=1.2) */
260 char *tls_verify_method = NULL; /* Verify certificate method */
261 bool tls_verify_key_usage = 0; /* Verify peer certificate key usage */
264 #if defined(PPP_WITH_EAPTLS)
265 char *cert_file = NULL; /* Client certificate file (pem format) */
266 char *privkey_file = NULL; /* Client private key file (pem format) */
267 char *pkcs12_file = NULL; /* Client private key envelope file (pkcs12 format) */
268 bool need_peer_eap = 0; /* Require peer to authenticate us */
271 static char *uafname; /* name of most recent +ua file */
273 /* Prototypes for procedures local to this file. */
275 static void network_phase (int);
276 static void check_idle (void *);
277 static void connect_time_expired (void *);
278 static int null_login (int);
279 static int get_pap_passwd (char *);
280 static int have_pap_secret (int *);
281 static int have_chap_secret (char *, char *, int, int *);
282 static int have_srp_secret(char *client, char *server, int need_ip,
285 #ifdef PPP_WITH_EAPTLS
286 static int have_eaptls_secret_server
287 (char *client, char *server, int need_ip, int *lacks_ipp);
288 static int have_eaptls_secret_client (char *client, char *server);
289 static int scan_authfile_eaptls(FILE * f, char *client, char *server,
290 char *cli_cert, char *serv_cert,
291 char *ca_cert, char *pk,
292 struct wordlist ** addrs,
293 struct wordlist ** opts,
294 char *filename, int flags);
297 static int ip_addr_check (u_int32_t, struct permitted_ip *);
298 static int scan_authfile(FILE *, char *, char *, char *,
299 struct wordlist **, struct wordlist **,
301 static void free_wordlist (struct wordlist *);
302 static void auth_script (char *);
303 static void auth_script_done (void *);
304 static void set_allowed_addrs (int, struct wordlist *, struct wordlist *);
305 static int some_ip_ok (struct wordlist *);
306 static int setupapfile (char **);
307 static int privgroup (char **);
308 static int set_noauth_addr (char **);
309 static int set_permitted_number (char **);
310 static void check_access (FILE *, char *);
311 static int wordlist_count (struct wordlist *);
312 static void check_maxoctets (void *);
315 * Authentication-related options.
317 struct option auth_options[] = {
318 { "auth", o_bool, &auth_required,
319 "Require authentication from peer", OPT_PRIO | 1 },
320 { "noauth", o_bool, &auth_required,
321 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
323 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
324 "Require PAP authentication from peer",
325 OPT_PRIOSUB | 1, &auth_required },
326 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
327 "Require PAP authentication from peer",
328 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
329 { "require-chap", o_bool, &auth_required,
330 "Require CHAP authentication from peer",
331 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
332 &lcp_wantoptions[0].chap_mdtype },
333 { "+chap", o_bool, &auth_required,
334 "Require CHAP authentication from peer",
335 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
336 &lcp_wantoptions[0].chap_mdtype },
337 #ifdef PPP_WITH_CHAPMS
338 { "require-mschap", o_bool, &auth_required,
339 "Require MS-CHAP authentication from peer",
340 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
341 &lcp_wantoptions[0].chap_mdtype },
342 { "+mschap", o_bool, &auth_required,
343 "Require MS-CHAP authentication from peer",
344 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
345 &lcp_wantoptions[0].chap_mdtype },
346 { "require-mschap-v2", o_bool, &auth_required,
347 "Require MS-CHAPv2 authentication from peer",
348 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
349 &lcp_wantoptions[0].chap_mdtype },
350 { "+mschap-v2", o_bool, &auth_required,
351 "Require MS-CHAPv2 authentication from peer",
352 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
353 &lcp_wantoptions[0].chap_mdtype },
356 { "refuse-pap", o_bool, &refuse_pap,
357 "Don't agree to auth to peer with PAP", 1 },
358 { "-pap", o_bool, &refuse_pap,
359 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
360 { "refuse-chap", o_bool, &refuse_chap,
361 "Don't agree to auth to peer with CHAP",
362 OPT_A2CLRB | MDTYPE_MD5,
363 &lcp_allowoptions[0].chap_mdtype },
364 { "-chap", o_bool, &refuse_chap,
365 "Don't allow CHAP authentication with peer",
366 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
367 &lcp_allowoptions[0].chap_mdtype },
368 #ifdef PPP_WITH_CHAPMS
369 { "refuse-mschap", o_bool, &refuse_mschap,
370 "Don't agree to auth to peer with MS-CHAP",
371 OPT_A2CLRB | MDTYPE_MICROSOFT,
372 &lcp_allowoptions[0].chap_mdtype },
373 { "-mschap", o_bool, &refuse_mschap,
374 "Don't allow MS-CHAP authentication with peer",
375 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
376 &lcp_allowoptions[0].chap_mdtype },
377 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
378 "Don't agree to auth to peer with MS-CHAPv2",
379 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
380 &lcp_allowoptions[0].chap_mdtype },
381 { "-mschap-v2", o_bool, &refuse_mschap_v2,
382 "Don't allow MS-CHAPv2 authentication with peer",
383 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
384 &lcp_allowoptions[0].chap_mdtype },
387 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
388 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
390 { "refuse-eap", o_bool, &refuse_eap,
391 "Don't agree to authenticate to peer with EAP", 1 },
393 { "name", o_string, our_name,
394 "Set local name for authentication",
395 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
397 { "+ua", o_special, (void *)setupapfile,
398 "Get PAP user and password from file",
399 OPT_PRIO | OPT_A2STRVAL, &uafname },
401 { "user", o_string, user,
402 "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
403 &explicit_user, MAXNAMELEN },
405 { "password", o_string, passwd,
406 "Password for authenticating us to the peer",
407 OPT_PRIO | OPT_STATIC | OPT_HIDE,
408 &explicit_passwd, MAXSECRETLEN },
410 { "usehostname", o_bool, &usehostname,
411 "Must use hostname for authentication", 1 },
413 { "remotename", o_string, remote_name,
414 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
415 &explicit_remote, MAXNAMELEN },
417 { "pap-secrets", o_string, path_upapfile,
418 "Set pathname of pap-secrets", OPT_PRIO | OPT_PRIV | OPT_STATIC,
421 { "chap-secrets", o_string, path_chapfile,
422 "Set pathname of chap-secrets", OPT_PRIO | OPT_PRIV | OPT_STATIC,
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 #if defined(PPP_WITH_EAPTLS) || defined(PPP_WITH_PEAP)
450 { "ca", o_string, &cacert_file, "CA certificate in PEM format" },
451 { "capath", o_string, &ca_path, "TLS CA certificate directory" },
452 { "crl-dir", o_string, &crl_dir, "Use CRLs in directory" },
453 { "crl", o_string, &crl_file, "Use specific CRL file" },
454 { "max-tls-version", o_string, &max_tls_version,
455 "Maximum TLS version (1.0/1.1/1.2 (default)/1.3)" },
456 { "tls-verify-key-usage", o_bool, &tls_verify_key_usage,
457 "Verify certificate type and extended key usage" },
458 { "tls-verify-method", o_string, &tls_verify_method,
459 "Verify peer by method (none|subject|name|suffix)" },
462 #if defined(PPP_WITH_EAPTLS)
463 { "cert", o_string, &cert_file, "client certificate in PEM format" },
464 { "key", o_string, &privkey_file, "client private key in PEM format" },
465 { "pkcs12", o_string, &pkcs12_file, "EAP-TLS client credentials in PKCS12 format" },
466 { "need-peer-eap", o_bool, &need_peer_eap,
467 "Require the peer to authenticate us", 1 },
468 #endif /* PPP_WITH_EAPTLS */
479 ppp_get_remote_number(void)
481 return remote_number;
485 ppp_set_remote_number(const char *buf)
488 strlcpy(remote_number, buf, sizeof(remote_number));
489 ppp_script_setenv("REMOTENUMBER", remote_number, 0);
494 ppp_peer_authname(char *buf, size_t bufsz)
496 if (buf && bufsz > 0) {
497 strlcpy(buf, peer_authname, bufsz);
500 return peer_authname;
504 * setupapfile - specifies UPAP info for authenticating with peer.
507 setupapfile(char **argv)
512 char u[MAXNAMELEN], p[MAXSECRETLEN];
515 lcp_allowoptions[0].neg_upap = 1;
517 /* open user info file */
518 fname = strdup(*argv);
520 novm("+ua file name");
522 if (seteuid(getuid()) == -1) {
523 ppp_option_error("unable to reset uid before opening %s: %m", fname);
527 ufile = fopen(fname, "r");
528 if (seteuid(euid) == -1)
529 fatal("unable to regain privileges: %m");
531 ppp_option_error("unable to open user login data file %s", fname);
535 check_access(ufile, fname);
539 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
540 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
542 ppp_option_error("unable to read user login data file %s", fname);
548 /* get rid of newlines */
550 if (l > 0 && u[l-1] == '\n')
553 if (l > 0 && p[l-1] == '\n')
556 if (override_value("user", option_priority, fname)) {
557 strlcpy(user, u, sizeof(user));
560 if (override_value("passwd", option_priority, fname)) {
561 strlcpy(passwd, p, sizeof(passwd));
571 * privgroup - allow members of the group to have privileged access.
574 privgroup(char **argv)
581 ppp_option_error("group %s is unknown", *argv);
584 for (i = 0; i < ngroups; ++i) {
585 if (groups[i] == g->gr_gid) {
595 * set_noauth_addr - set address(es) that can be used without authentication.
596 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
599 set_noauth_addr(char **argv)
602 int l = strlen(addr) + 1;
605 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
607 novm("allow-ip argument");
608 wp->word = (char *) (wp + 1);
609 wp->next = noauth_addrs;
610 BCOPY(addr, wp->word, l);
617 * set_permitted_number - set remote telephone number(s) that may connect.
620 set_permitted_number(char **argv)
622 char *number = *argv;
623 int l = strlen(number) + 1;
626 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
628 novm("allow-number argument");
629 wp->word = (char *) (wp + 1);
630 wp->next = permitted_numbers;
631 BCOPY(number, wp->word, l);
632 permitted_numbers = wp;
638 * An Open on LCP has requested a change from Dead to Establish phase.
641 link_required(int unit)
646 * Bring the link up to the point of being able to do ppp.
648 void start_link(int unit)
650 ppp_set_status(EXIT_CONNECT_FAILED);
651 new_phase(PHASE_SERIALCONN);
654 devfd = the_channel->connect();
658 /* set up the serial device as a ppp interface */
660 * N.B. we used to do tdb_writelock/tdb_writeunlock around this
661 * (from establish_ppp to set_ifunit). However, we won't be
662 * doing the set_ifunit in multilink mode, which is the only time
663 * we need the atomicity that the tdb_writelock/tdb_writeunlock
664 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
666 fd_ppp = the_channel->establish_ppp(devfd);
668 ppp_set_status(EXIT_FATAL_ERROR);
672 if (!demand && ifunit >= 0)
676 * Start opening the connection and wait for
677 * incoming events (reply, timeout, etc.).
680 notice("Connect: %s <--> %s", ifname, ppp_devname);
682 notice("Starting negotiation on %s", ppp_devname);
685 ppp_set_status(EXIT_NEGOTIATION_FAILED);
686 new_phase(PHASE_ESTABLISH);
692 new_phase(PHASE_DISCONNECT);
693 if (the_channel->disconnect)
694 the_channel->disconnect();
697 new_phase(PHASE_DEAD);
698 if (the_channel->cleanup)
699 (*the_channel->cleanup)();
703 * LCP has terminated the link; go to the Dead phase and take the
704 * physical layer down.
707 link_terminated(int unit)
709 if (in_phase(PHASE_DEAD) || in_phase(PHASE_MASTER))
711 new_phase(PHASE_DISCONNECT);
713 if (pap_logout_hook) {
719 notice("Connection terminated.");
722 notice("Link terminated.");
725 * Delete pid files before disestablishing ppp. Otherwise it
726 * can happen that another pppd gets the same unit and then
727 * we delete its pid file.
729 if (!demand && !mp_on())
732 * If we may want to bring the link up again, transfer
733 * the ppp unit back to the loopback. Set the
734 * real serial device back to its normal mode of operation.
739 the_channel->disestablish_ppp(devfd);
746 if (!mp_on() && !demand)
747 ppp_script_unsetenv("IFNAME");
750 * Run disconnector script, if requested.
751 * XXX we may not be able to do this if the line has hung up!
753 if (devfd >= 0 && the_channel->disconnect) {
754 the_channel->disconnect();
757 if (the_channel->cleanup)
758 (*the_channel->cleanup)();
760 if (mp_on() && mp_master()) {
761 if (!bundle_terminating) {
762 new_phase(PHASE_MASTER);
763 if (master_detach && !detached)
766 mp_bundle_terminated();
768 new_phase(PHASE_DEAD);
772 * LCP has gone down; it will either die or try to re-establish.
777 if (auth_state != s_down) {
778 notify(link_down_notifier, 0);
780 if (auth_script_state == s_up && auth_script_pid == 0) {
781 ppp_get_link_stats(NULL);
782 auth_script_state = s_down;
783 auth_script(PPP_PATH_AUTHDOWN);
788 upper_layers_down(unit);
789 if (!in_phase(PHASE_DEAD) && !in_phase(PHASE_MASTER))
790 new_phase(PHASE_ESTABLISH);
792 /* XXX if doing_multilink, should do something to stop
793 network-layer traffic on the link */
796 void upper_layers_down(int unit)
799 struct protent *protp;
801 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
802 if (!protp->enabled_flag)
804 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
805 (*protp->lowerdown)(unit);
806 if (protp->protocol < 0xC000 && protp->close != NULL)
807 (*protp->close)(unit, "LCP down");
814 * The link is established.
815 * Proceed to the Dead, Authenticate or Network phase as appropriate.
818 link_established(int unit)
821 lcp_options *wo = &lcp_wantoptions[unit];
822 lcp_options *go = &lcp_gotoptions[unit];
823 lcp_options *ho = &lcp_hisoptions[unit];
824 #ifdef PPP_WITH_EAPTLS
825 lcp_options *ao = &lcp_allowoptions[unit];
828 struct protent *protp;
831 * Tell higher-level protocols that LCP is up.
834 for (i = 0; (protp = protocols[i]) != NULL; ++i)
835 if (protp->protocol != PPP_LCP && protp->enabled_flag
836 && protp->lowerup != NULL)
837 (*protp->lowerup)(unit);
838 if (!auth_required && noauth_addrs != NULL)
839 set_allowed_addrs(unit, NULL, NULL);
841 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
843 * We wanted the peer to authenticate itself, and it refused:
844 * if we have some address(es) it can use without auth, fine,
845 * otherwise treat it as though it authenticated with PAP using
846 * a username of "" and a password of "". If that's not OK,
849 if (noauth_addrs != NULL) {
850 set_allowed_addrs(unit, NULL, NULL);
851 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
852 warn("peer refused to authenticate: terminating link");
853 ppp_set_status(EXIT_PEER_AUTH_FAILED);
854 lcp_close(unit, "peer refused to authenticate");
859 #ifdef PPP_WITH_EAPTLS
860 if (need_peer_eap && !ao->neg_eap) {
861 warn("eap required to authenticate us but no suitable secrets");
862 lcp_close(unit, "couldn't negotiate eap");
863 ppp_set_status(EXIT_AUTH_TOPEER_FAILED);
867 if (need_peer_eap && !ho->neg_eap) {
868 warn("peer doesn't want to authenticate us with eap");
869 lcp_close(unit, "couldn't negotiate eap");
870 ppp_set_status(EXIT_PEER_AUTH_FAILED);
875 new_phase(PHASE_AUTHENTICATE);
878 eap_authpeer(unit, our_name);
880 } else if (go->neg_chap) {
881 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
883 } else if (go->neg_upap) {
888 eap_authwithpeer(unit, user);
889 auth |= EAP_WITHPEER;
890 } else if (ho->neg_chap) {
891 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
892 auth |= CHAP_WITHPEER;
893 } else if (ho->neg_upap) {
894 /* If a blank password was explicitly given as an option, trust
895 the user and don't try to look up one. */
896 if (passwd[0] == 0 && !explicit_passwd) {
897 passwd_from_file = 1;
898 if (!get_pap_passwd(passwd))
899 error("No secret found for PAP login");
901 upap_authwithpeer(unit, user, passwd);
902 auth |= PAP_WITHPEER;
904 auth_pending[unit] = auth;
912 * Proceed to the network phase.
915 network_phase(int unit)
917 lcp_options *go = &lcp_gotoptions[unit];
919 /* Log calling number. */
921 notice("peer from calling number %q authorized", remote_number);
924 * If the peer had to authenticate, run the auth-up script now.
926 notify(auth_up_notifier, 0);
927 if (go->neg_chap || go->neg_upap || go->neg_eap) {
929 if (auth_script_state == s_down && auth_script_pid == 0) {
930 auth_script_state = s_up;
931 auth_script(PPP_PATH_AUTHUP);
937 * If we negotiated callback, do it now.
940 new_phase(PHASE_CALLBACK);
941 (*cbcp_protent.open)(unit);
947 * Process extra options from the secrets file
950 options_from_list(extra_options, 1);
951 free_wordlist(extra_options);
954 start_networks(unit);
958 start_networks(int unit)
961 struct protent *protp;
962 int ecp_required, mppe_required;
964 new_phase(PHASE_NETWORK);
966 #ifdef PPP_WITH_MULTILINK
968 if (mp_join_bundle()) {
969 if (multilink_join_hook)
970 (*multilink_join_hook)();
971 if (updetach && !nodetach)
976 #endif /* PPP_WITH_MULTILINK */
978 #ifdef PPP_WITH_FILTER
980 set_filters(&pass_filter, &active_filter);
982 /* Start CCP and ECP */
983 for (i = 0; (protp = protocols[i]) != NULL; ++i)
984 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
985 && protp->enabled_flag && protp->open != NULL)
989 * Bring up other network protocols iff encryption is not required.
991 ecp_required = ecp_gotoptions[unit].required;
992 mppe_required = ccp_gotoptions[unit].mppe;
993 if (!ecp_required && !mppe_required)
994 continue_networks(unit);
998 continue_networks(int unit)
1001 struct protent *protp;
1004 * Start the "real" network protocols.
1006 for (i = 0; (protp = protocols[i]) != NULL; ++i)
1007 if (protp->protocol < 0xC000
1008 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
1009 && protp->enabled_flag && protp->open != NULL) {
1014 if (num_np_open == 0)
1016 lcp_close(0, "No network protocols running");
1020 * The peer has failed to authenticate himself using `protocol'.
1023 auth_peer_fail(int unit, int protocol)
1026 * Authentication failure: take the link down
1028 ppp_set_status(EXIT_PEER_AUTH_FAILED);
1029 lcp_close(unit, "Authentication failed");
1033 * The peer has been successfully authenticated using `protocol'.
1036 auth_peer_success(int unit, int protocol, int prot_flavor,
1037 char *name, int namelen)
1046 switch (prot_flavor) {
1048 bit |= CHAP_MD5_PEER;
1050 #ifdef PPP_WITH_CHAPMS
1051 case CHAP_MICROSOFT:
1052 bit |= CHAP_MS_PEER;
1054 case CHAP_MICROSOFT_V2:
1055 bit |= CHAP_MS2_PEER;
1069 warn("auth_peer_success: unknown protocol %x", protocol);
1070 prot = "unknown protocol";
1075 * Save the authenticated name of the peer for later.
1077 if (namelen > sizeof(peer_authname) - 1)
1078 namelen = sizeof(peer_authname) - 1;
1079 BCOPY(name, peer_authname, namelen);
1080 peer_authname[namelen] = 0;
1081 ppp_script_setenv("PEERNAME", peer_authname, 0);
1082 notice("Peer %q authenticated with %s", peer_authname, prot);
1084 /* Save the authentication method for later. */
1085 auth_done[unit] |= bit;
1088 * If there is no more authentication still to be done,
1089 * proceed to the network (or callback) phase.
1091 if ((auth_pending[unit] &= ~bit) == 0)
1092 network_phase(unit);
1096 * We have failed to authenticate ourselves to the peer using `protocol'.
1099 auth_withpeer_fail(int unit, int protocol)
1101 if (passwd_from_file)
1102 BZERO(passwd, MAXSECRETLEN);
1104 * We've failed to authenticate ourselves to our peer.
1105 * Some servers keep sending CHAP challenges, but there
1106 * is no point in persisting without any way to get updated
1107 * authentication secrets.
1109 ppp_set_status(EXIT_AUTH_TOPEER_FAILED);
1110 lcp_close(unit, "Failed to authenticate ourselves to peer");
1114 * We have successfully authenticated ourselves with the peer using `protocol'.
1117 auth_withpeer_success(int unit, int protocol, int prot_flavor)
1120 const char *prot = "";
1124 bit = CHAP_WITHPEER;
1126 switch (prot_flavor) {
1128 bit |= CHAP_MD5_WITHPEER;
1130 #ifdef PPP_WITH_CHAPMS
1131 case CHAP_MICROSOFT:
1132 bit |= CHAP_MS_WITHPEER;
1134 case CHAP_MICROSOFT_V2:
1135 bit |= CHAP_MS2_WITHPEER;
1141 if (passwd_from_file)
1142 BZERO(passwd, MAXSECRETLEN);
1151 warn("auth_withpeer_success: unknown protocol %x", protocol);
1155 notice("%s authentication succeeded", prot);
1157 /* Save the authentication method for later. */
1158 auth_done[unit] |= bit;
1161 * If there is no more authentication still being done,
1162 * proceed to the network (or callback) phase.
1164 if ((auth_pending[unit] &= ~bit) == 0)
1165 network_phase(unit);
1170 * np_up - a network protocol has come up.
1173 np_up(int unit, int proto)
1177 if (num_np_up == 0) {
1179 * At this point we consider that the link has come up successfully.
1181 ppp_set_status(EXIT_OK);
1183 new_phase(PHASE_RUNNING);
1185 if (idle_time_hook != 0)
1186 tlim = (*idle_time_hook)(NULL);
1188 tlim = ppp_get_max_idle_time();
1190 TIMEOUT(check_idle, NULL, tlim);
1193 * Set a timeout to close the connection once the maximum
1194 * connect time has expired.
1196 if (ppp_get_max_connect_time() > 0)
1197 TIMEOUT(connect_time_expired, 0, ppp_get_max_connect_time());
1200 * Configure a check to see if session has outlived it's limit
1201 * in terms of octets
1204 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1207 * Detach now, if the updetach option was given.
1209 if (updetach && !nodetach) {
1210 dbglog("updetach is set. Now detaching.");
1213 } else if (nodetach && up_sdnotify) {
1214 dbglog("up_sdnotify is set. Now notifying systemd: READY=1");
1215 sd_notify(0, "READY=1");
1223 * np_down - a network protocol has gone down.
1226 np_down(int unit, int proto)
1228 if (--num_np_up == 0) {
1229 UNTIMEOUT(check_idle, NULL);
1230 UNTIMEOUT(connect_time_expired, NULL);
1231 UNTIMEOUT(check_maxoctets, NULL);
1232 new_phase(PHASE_NETWORK);
1237 * np_finished - a network protocol has finished using the link.
1240 np_finished(int unit, int proto)
1242 if (--num_np_open <= 0) {
1243 /* no further use for the link: shut up shop. */
1244 lcp_close(0, "No network protocols running");
1249 * Periodic callback to check if session has reached its limit. The period defaults
1250 * to 1 second and is configurable by setting "mo-timeout" in configuration
1253 check_maxoctets(void *arg)
1255 unsigned int used = 0;
1256 ppp_link_stats_st stats;
1258 if (ppp_get_link_stats(&stats)) {
1259 switch(maxoctets_dir) {
1260 case PPP_OCTETS_DIRECTION_IN:
1261 used = stats.bytes_in;
1263 case PPP_OCTETS_DIRECTION_OUT:
1264 used = stats.bytes_out;
1266 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1267 case PPP_OCTETS_DIRECTION_MAXSESSION:
1268 used = (stats.bytes_in > stats.bytes_out)
1273 used = stats.bytes_in+stats.bytes_out;
1278 if (used > maxoctets) {
1279 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1280 ppp_set_status(EXIT_TRAFFIC_LIMIT);
1281 lcp_close(0, "Traffic limit");
1282 link_stats_print = 0;
1285 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1290 * check_idle - check whether the link has been idle for long
1291 * enough that we can shut it down.
1294 check_idle(void *arg)
1296 struct ppp_idle idle;
1300 if (!get_idle_time(0, &idle))
1302 if (idle_time_hook != 0) {
1303 tlim = idle_time_hook(&idle);
1305 itime = MIN(idle.xmit_idle, idle.recv_idle);
1306 tlim = ppp_get_max_idle_time() - itime;
1309 /* link is idle: shut it down. */
1310 notice("Terminating connection due to lack of activity.");
1311 ppp_set_status(EXIT_IDLE_TIMEOUT);
1312 lcp_close(0, "Link inactive");
1315 TIMEOUT(check_idle, NULL, tlim);
1320 * connect_time_expired - log a message and close the connection.
1323 connect_time_expired(void *arg)
1325 info("Connect time expired");
1326 ppp_set_status(EXIT_CONNECT_TIME);
1327 lcp_close(0, "Connect time expired"); /* Close connection */
1332 * auth_check_options - called to check authentication options.
1335 auth_check_options(void)
1337 lcp_options *wo = &lcp_wantoptions[0];
1341 /* Default our_name to hostname, and user to our_name */
1342 if (our_name[0] == 0 || usehostname)
1343 strlcpy(our_name, hostname, sizeof(our_name));
1345 /* If a blank username was explicitly given as an option, trust
1346 the user and don't use our_name */
1347 if (user[0] == 0 && !explicit_user)
1348 strlcpy(user, our_name, sizeof(user));
1350 #if defined(SYSTEM_CA_PATH) && (defined(PPP_WITH_EAPTLS) || defined(PPP_WITH_PEAP))
1351 /* Use system default for CA Path if not specified */
1353 ca_path = SYSTEM_CA_PATH;
1358 * If we have a default route, require the peer to authenticate
1359 * unless the noauth option was given or the real user is root.
1361 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1366 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1367 if (wo->chap_mdtype)
1370 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1371 if (auth_required) {
1373 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1374 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
1375 wo->chap_mdtype = chap_mdtype_all;
1381 wo->chap_mdtype = MDTYPE_NONE;
1387 * Check whether we have appropriate secrets to use
1388 * to authenticate the peer. Note that EAP can authenticate by way
1389 * of a CHAP-like exchanges as well as SRP.
1392 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1393 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1394 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1395 our_name, 1, &lacks_ip);
1397 if (!can_auth && wo->neg_eap) {
1398 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1399 our_name, 1, &lacks_ip);
1402 #ifdef PPP_WITH_EAPTLS
1403 if (!can_auth && wo->neg_eap) {
1405 have_eaptls_secret_server((explicit_remote ? remote_name :
1406 NULL), our_name, 1, &lacks_ip);
1411 if (auth_required && !can_auth && noauth_addrs == NULL) {
1414 "By default the remote system is required to authenticate itself");
1416 "(because this system has a default route to the internet)");
1417 } else if (explicit_remote)
1419 "The remote system (%s) is required to authenticate itself",
1423 "The remote system is required to authenticate itself");
1425 "but I couldn't find any suitable secret (password) for it to use to do so.");
1428 "(None of the available passwords would let it use an IP address.)");
1434 * Early check for remote number authorization.
1436 if (!auth_number()) {
1437 warn("calling number %q is not authorized", remote_number);
1438 exit(EXIT_CNID_AUTH_FAILED);
1443 * auth_reset - called when LCP is starting negotiations to recheck
1444 * authentication options, i.e. whether we have appropriate secrets
1445 * to use for authenticating ourselves and/or the peer.
1448 auth_reset(int unit)
1450 lcp_options *go = &lcp_gotoptions[unit];
1451 lcp_options *ao = &lcp_allowoptions[unit];
1455 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1456 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1457 && ((passwd[0] != 0 || explicit_passwd) ||
1458 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1460 ao->neg_eap = !refuse_eap && (
1462 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1463 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1464 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL)
1465 #ifdef PPP_WITH_EAPTLS
1466 || have_eaptls_secret_client(user, (explicit_remote? remote_name: NULL))
1471 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1474 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1475 our_name, 1, NULL)))
1479 (hadchap == 0 || (hadchap == -1 &&
1480 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1482 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1484 #ifdef PPP_WITH_EAPTLS
1485 && !have_eaptls_secret_server((explicit_remote? remote_name: NULL),
1494 * check_passwd - Check the user name and passwd against the PAP secrets
1495 * file. If requested, also check against the system password database,
1496 * and login the user if OK.
1499 * UPAP_AUTHNAK: Authentication failed.
1500 * UPAP_AUTHACK: Authentication succeeded.
1501 * In either case, msg points to an appropriate message.
1504 check_passwd(int unit,
1505 char *auser, int userlen,
1506 char *apasswd, int passwdlen, char **msg)
1511 struct wordlist *addrs = NULL, *opts = NULL;
1512 char passwd[256], user[256];
1513 char secret[MAXWORDLEN];
1514 static int attempts = 0;
1517 * Make copies of apasswd and auser, then null-terminate them.
1518 * If there are unprintable characters in the password, make
1521 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1522 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1526 * Check if a plugin wants to handle this.
1528 if (pap_auth_hook) {
1529 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1531 /* note: set_allowed_addrs() saves opts (but not addrs):
1534 set_allowed_addrs(unit, addrs, opts);
1536 free_wordlist(opts);
1538 free_wordlist(addrs);
1539 BZERO(passwd, sizeof(passwd));
1540 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1545 * Open the file of pap secrets and scan for a suitable secret
1546 * for authenticating this user.
1548 filename = path_upapfile;
1549 addrs = opts = NULL;
1551 f = fopen(filename, "r");
1553 error("Can't open PAP password file %s: %m", filename);
1556 check_access(f, filename);
1557 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1558 warn("no PAP secret found for %s", user);
1561 * If the secret is "@login", it means to check
1562 * the password against the login database.
1564 int login_secret = strcmp(secret, "@login") == 0;
1566 if (uselogin || login_secret) {
1567 /* login option or secret is @login */
1568 if (session_full(user, passwd, devnam, msg) == 0) {
1571 } else if (session_mgmt) {
1572 if (session_check(user, NULL, devnam, NULL) == 0) {
1573 warn("Peer %q failed PAP Session verification", user);
1577 if (secret[0] != 0 && !login_secret) {
1578 /* password given in pap-secrets - must match */
1579 if (cryptpap || strcmp(passwd, secret) != 0) {
1581 char *cbuf = crypt(passwd, secret);
1582 if (!cbuf || strcmp(cbuf, secret) != 0)
1591 if (ret == UPAP_AUTHNAK) {
1593 *msg = "Login incorrect";
1595 * XXX can we ever get here more than once??
1596 * Frustrate passwd stealer programs.
1597 * Allow 10 tries, but start backing off after 3 (stolen from login).
1598 * On 10'th, drop the connection.
1600 if (attempts++ >= 10) {
1601 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1602 lcp_close(unit, "login failed");
1605 sleep((u_int) (attempts - 3) * 5);
1607 free_wordlist(opts);
1610 attempts = 0; /* Reset count */
1613 set_allowed_addrs(unit, addrs, opts);
1617 free_wordlist(addrs);
1618 BZERO(passwd, sizeof(passwd));
1619 BZERO(secret, sizeof(secret));
1625 * null_login - Check if a username of "" and a password of "" are
1626 * acceptable, and iff so, set the list of acceptable IP addresses
1630 null_login(int unit)
1635 struct wordlist *addrs, *opts;
1636 char secret[MAXWORDLEN];
1639 * Check if a plugin wants to handle this.
1643 ret = (*null_auth_hook)(&addrs, &opts);
1646 * Open the file of pap secrets and scan for a suitable secret.
1649 filename = path_upapfile;
1651 f = fopen(filename, "r");
1654 check_access(f, filename);
1656 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1657 ret = i >= 0 && secret[0] == 0;
1658 BZERO(secret, sizeof(secret));
1663 set_allowed_addrs(unit, addrs, opts);
1665 free_wordlist(opts);
1667 free_wordlist(addrs);
1674 * get_pap_passwd - get a password for authenticating ourselves with
1675 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1677 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1680 get_pap_passwd(char *passwd)
1685 char secret[MAXWORDLEN];
1688 * Check whether a plugin wants to supply this.
1690 if (pap_passwd_hook) {
1691 ret = (*pap_passwd_hook)(user, passwd);
1696 filename = path_upapfile;
1697 f = fopen(filename, "r");
1700 check_access(f, filename);
1701 ret = scan_authfile(f, user,
1702 (remote_name[0]? remote_name: NULL),
1703 secret, NULL, NULL, filename, 0);
1708 strlcpy(passwd, secret, MAXSECRETLEN);
1709 BZERO(secret, sizeof(secret));
1715 * have_pap_secret - check whether we have a PAP file with any
1716 * secrets that we could possibly use for authenticating the peer.
1719 have_pap_secret(int *lacks_ipp)
1724 struct wordlist *addrs;
1726 /* let the plugin decide, if there is one */
1727 if (pap_check_hook) {
1728 ret = (*pap_check_hook)();
1733 filename = path_upapfile;
1734 f = fopen(filename, "r");
1738 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1739 NULL, &addrs, NULL, filename, 0);
1741 if (ret >= 0 && !some_ip_ok(addrs)) {
1747 free_wordlist(addrs);
1754 * have_chap_secret - check whether we have a CHAP file with a
1755 * secret that we could possibly use for authenticating `client'
1756 * on `server'. Either can be the null string, meaning we don't
1757 * know the identity yet.
1760 have_chap_secret(char *client, char *server,
1761 int need_ip, int *lacks_ipp)
1766 struct wordlist *addrs;
1768 if (chap_check_hook) {
1769 ret = (*chap_check_hook)();
1775 filename = path_chapfile;
1776 f = fopen(filename, "r");
1780 if (client != NULL && client[0] == 0)
1782 else if (server != NULL && server[0] == 0)
1785 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1787 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1793 free_wordlist(addrs);
1800 * have_srp_secret - check whether we have a SRP file with a
1801 * secret that we could possibly use for authenticating `client'
1802 * on `server'. Either can be the null string, meaning we don't
1803 * know the identity yet.
1806 have_srp_secret(char *client, char *server, int need_ip, int *lacks_ipp)
1811 struct wordlist *addrs;
1813 filename = PPP_PATH_SRPFILE;
1814 f = fopen(filename, "r");
1818 if (client != NULL && client[0] == 0)
1820 else if (server != NULL && server[0] == 0)
1823 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1825 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1831 free_wordlist(addrs);
1838 * get_secret - open the CHAP secret file and return the secret
1839 * for authenticating the given client on the given server.
1840 * (We could be either client or server).
1843 get_secret(int unit, char *client, char *server,
1844 char *secret, int *secret_len, int am_server)
1849 struct wordlist *addrs, *opts;
1850 char secbuf[MAXWORDLEN];
1852 if (!am_server && passwd[0] != 0) {
1853 strlcpy(secbuf, passwd, sizeof(secbuf));
1854 } else if (!am_server && chap_passwd_hook) {
1855 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1856 error("Unable to obtain CHAP password for %s on %s from plugin",
1861 filename = path_chapfile;
1865 f = fopen(filename, "r");
1867 error("Can't open chap secret file %s: %m", filename);
1870 check_access(f, filename);
1872 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1878 set_allowed_addrs(unit, addrs, opts);
1880 free_wordlist(opts);
1882 free_wordlist(addrs);
1885 len = strlen(secbuf);
1886 if (len > MAXSECRETLEN) {
1887 error("Secret for %s on %s is too long", client, server);
1890 BCOPY(secbuf, secret, len);
1891 BZERO(secbuf, sizeof(secbuf));
1899 * get_srp_secret - open the SRP secret file and return the secret
1900 * for authenticating the given client on the given server.
1901 * (We could be either client or server).
1904 get_srp_secret(int unit, char *client, char *server,
1905 char *secret, int am_server)
1910 struct wordlist *addrs, *opts;
1912 if (!am_server && passwd[0] != '\0') {
1913 strlcpy(secret, passwd, MAXWORDLEN);
1915 filename = PPP_PATH_SRPFILE;
1918 fp = fopen(filename, "r");
1920 error("Can't open srp secret file %s: %m", filename);
1923 check_access(fp, filename);
1926 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1927 filename, am_server);
1933 set_allowed_addrs(unit, addrs, opts);
1934 else if (opts != NULL)
1935 free_wordlist(opts);
1937 free_wordlist(addrs);
1944 * set_allowed_addrs() - set the list of allowed addresses.
1945 * Also looks for `--' indicating options to apply for this peer
1946 * and leaves the following words in extra_options.
1949 set_allowed_addrs(int unit, struct wordlist *addrs,
1950 struct wordlist *opts)
1953 struct wordlist *ap, **plink;
1954 struct permitted_ip *ip;
1955 char *ptr_word, *ptr_mask;
1958 u_int32_t a, mask, ah, offset;
1959 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1960 u_int32_t suggested_ip = 0;
1962 if (addresses[unit] != NULL)
1963 free(addresses[unit]);
1964 addresses[unit] = NULL;
1965 if (extra_options != NULL)
1966 free_wordlist(extra_options);
1967 extra_options = opts;
1970 * Count the number of IP addresses given.
1972 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1975 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1979 /* temporarily append the noauth_addrs list to addrs */
1980 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1982 *plink = noauth_addrs;
1985 for (ap = addrs; ap != NULL; ap = ap->next) {
1986 /* "-" means no addresses authorized, "*" means any address allowed */
1987 ptr_word = ap->word;
1988 if (strcmp(ptr_word, "-") == 0)
1990 if (strcmp(ptr_word, "*") == 0) {
1992 ip[n].base = ip[n].mask = 0;
1998 if (*ptr_word == '!') {
2003 mask = ~ (u_int32_t) 0;
2005 ptr_mask = strchr (ptr_word, '/');
2006 if (ptr_mask != NULL) {
2010 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
2011 if (bit_count <= 0 || bit_count > 32) {
2012 warn("invalid address length %v in auth. address list",
2016 bit_count = 32 - bit_count; /* # bits in host part */
2018 offset = ifunit + 1;
2022 warn("invalid address length syntax: %v", ptr_mask+1);
2029 hp = gethostbyname(ptr_word);
2030 if (hp != NULL && hp->h_addrtype == AF_INET) {
2031 a = *(u_int32_t *)hp->h_addr;
2033 np = getnetbyname (ptr_word);
2034 if (np != NULL && np->n_addrtype == AF_INET) {
2035 a = htonl ((u_int32_t)np->n_net);
2036 if (ptr_mask == NULL) {
2037 /* calculate appropriate mask for net */
2040 mask = IN_CLASSA_NET;
2041 else if (IN_CLASSB(ah))
2042 mask = IN_CLASSB_NET;
2043 else if (IN_CLASSC(ah))
2044 mask = IN_CLASSC_NET;
2047 a = inet_addr (ptr_word);
2051 if (ptr_mask != NULL)
2054 if (a == (u_int32_t)-1L) {
2055 warn("unknown host %s in auth. address list", ap->word);
2059 if (offset >= ~mask) {
2060 warn("interface unit %d too large for subnet %v",
2064 a = htonl((ntohl(a) & mask) + offset);
2065 mask = ~(u_int32_t)0;
2067 ip[n].mask = htonl(mask);
2068 ip[n].base = a & ip[n].mask;
2070 if (~mask == 0 && suggested_ip == 0)
2075 ip[n].permit = 0; /* make the last entry forbid all addresses */
2076 ip[n].base = 0; /* to terminate the list */
2079 addresses[unit] = ip;
2082 * If the address given for the peer isn't authorized, or if
2083 * the user hasn't given one, AND there is an authorized address
2084 * which is a single host, then use that if we find one.
2086 if (suggested_ip != 0
2087 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
2088 wo->hisaddr = suggested_ip;
2090 * Do we insist on this address? No, if there are other
2091 * addresses authorized than the suggested one.
2094 wo->accept_remote = 1;
2099 * auth_ip_addr - check whether the peer is authorized to use
2100 * a given IP address. Returns 1 if authorized, 0 otherwise.
2103 auth_ip_addr(int unit, u_int32_t addr)
2107 /* don't allow loopback or multicast address */
2108 if (ppp_bad_ip_addr(addr))
2111 if (allowed_address_hook) {
2112 ok = allowed_address_hook(addr);
2113 if (ok >= 0) return ok;
2116 if (addresses[unit] != NULL) {
2117 ok = ip_addr_check(addr, addresses[unit]);
2123 return 0; /* no addresses authorized */
2124 return allow_any_ip || privileged || !have_route_to(addr);
2128 ip_addr_check(u_int32_t addr, struct permitted_ip *addrs)
2131 if ((addr & addrs->mask) == addrs->base)
2132 return addrs->permit;
2136 * Check if given addr in network byte order is in the looback network, or a multicast address.
2139 ppp_bad_ip_addr(u_int32_t addr)
2142 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2143 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2147 * some_ip_ok - check a wordlist to see if it authorizes any
2151 some_ip_ok(struct wordlist *addrs)
2153 for (; addrs != 0; addrs = addrs->next) {
2154 if (addrs->word[0] == '-')
2156 if (addrs->word[0] != '!')
2157 return 1; /* some IP address is allowed */
2163 * auth_number - check whether the remote number is allowed to connect.
2164 * Returns 1 if authorized, 0 otherwise.
2169 struct wordlist *wp = permitted_numbers;
2172 /* Allow all if no authorization list. */
2176 /* Allow if we have a match in the authorization list. */
2178 /* trailing '*' wildcard */
2179 l = strlen(wp->word);
2180 if (l > 0 && (wp->word)[l - 1] == '*') {
2181 if (!strncasecmp(wp->word, remote_number, l - 1))
2183 } else if (strcasecmp(wp->word, remote_number) == 0)
2192 * check_access - complain if a secret file has too-liberal permissions.
2195 check_access(FILE *f, char *filename)
2199 if (fstat(fileno(f), &sbuf) < 0) {
2200 warn("cannot stat secret file %s: %m", filename);
2201 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2202 warn("Warning - secret file %s has world and/or group access",
2209 * scan_authfile - Scan an authorization file for a secret suitable
2210 * for authenticating `client' on `server'. The return value is -1
2211 * if no secret is found, otherwise >= 0. The return value has
2212 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2213 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2214 * Any following words on the line up to a "--" (i.e. address authorization
2215 * info) are placed in a wordlist and returned in *addrs. Any
2216 * following words (extra options) are placed in a wordlist and
2217 * returned in *opts.
2218 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2219 * Flags are non-zero if we need two colons in the secret in order to
2223 scan_authfile(FILE *f, char *client, char *server,
2224 char *secret, struct wordlist **addrs,
2225 struct wordlist **opts, char *filename,
2229 int got_flag, best_flag;
2231 struct wordlist *ap, *addr_list, *alist, **app;
2232 char word[MAXWORDLEN];
2233 char atfile[MAXWORDLEN];
2234 char lsecret[MAXWORDLEN];
2242 if (!getword(f, word, &newline, filename))
2243 return -1; /* file is empty??? */
2248 * Skip until we find a word at the start of a line.
2250 while (!newline && getword(f, word, &newline, filename))
2253 break; /* got to end of file */
2256 * Got a client - check if it's a match or a wildcard.
2259 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2264 got_flag = NONWILD_CLIENT;
2267 * Now get a server and check if it matches.
2269 if (!getword(f, word, &newline, filename))
2273 if (!ISWILD(word)) {
2274 if (server != NULL && strcmp(word, server) != 0)
2276 got_flag |= NONWILD_SERVER;
2280 * Got some sort of a match - see if it's better than what
2283 if (got_flag <= best_flag)
2289 if (!getword(f, word, &newline, filename))
2295 * SRP-SHA1 authenticator should never be reading secrets from
2296 * a file. (Authenticatee may, though.)
2298 if (flags && ((cp = strchr(word, ':')) == NULL ||
2299 strchr(cp + 1, ':') == NULL))
2302 if (secret != NULL) {
2304 * Special syntax: @/pathname means read secret from file.
2306 if (word[0] == '@' && word[1] == '/') {
2307 strlcpy(atfile, word+1, sizeof(atfile));
2308 if ((sf = fopen(atfile, "r")) == NULL) {
2309 warn("can't open indirect secret file %s", atfile);
2312 check_access(sf, atfile);
2313 if (!getword(sf, word, &xxx, atfile)) {
2314 warn("no secret in indirect secret file %s", atfile);
2320 strlcpy(lsecret, word, sizeof(lsecret));
2324 * Now read address authorization info and make a wordlist.
2328 if (!getword(f, word, &newline, filename) || newline)
2330 ap = (struct wordlist *)
2331 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2333 novm("authorized addresses");
2334 ap->word = (char *) (ap + 1);
2335 strcpy(ap->word, word);
2342 * This is the best so far; remember it.
2344 best_flag = got_flag;
2346 free_wordlist(addr_list);
2349 strlcpy(secret, lsecret, MAXWORDLEN);
2355 /* scan for a -- word indicating the start of options */
2356 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2357 if (strcmp(ap->word, "--") == 0)
2359 /* ap = start of options */
2361 ap = ap->next; /* first option */
2362 free(*app); /* free the "--" word */
2363 *app = NULL; /* terminate addr list */
2367 else if (ap != NULL)
2371 else if (addr_list != NULL)
2372 free_wordlist(addr_list);
2378 * wordlist_count - return the number of items in a wordlist
2381 wordlist_count(struct wordlist *wp)
2385 for (n = 0; wp != NULL; wp = wp->next)
2391 * free_wordlist - release memory allocated for a wordlist.
2394 free_wordlist(struct wordlist *wp)
2396 struct wordlist *next;
2398 while (wp != NULL) {
2406 * auth_script_done - called when the auth-up or auth-down script
2410 auth_script_done(void *arg)
2412 auth_script_pid = 0;
2413 switch (auth_script_state) {
2415 if (auth_state == s_down) {
2416 auth_script_state = s_down;
2417 auth_script(PPP_PATH_AUTHDOWN);
2421 if (auth_state == s_up) {
2422 auth_script_state = s_up;
2423 auth_script(PPP_PATH_AUTHUP);
2430 * auth_script - execute a script with arguments
2431 * interface-name peer-name real-user tty speed
2434 auth_script(char *script)
2442 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2443 user_name = pw->pw_name;
2445 slprintf(struid, sizeof(struid), "%d", getuid());
2448 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2452 argv[2] = peer_authname;
2453 argv[3] = user_name;
2459 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);
2463 #ifdef PPP_WITH_EAPTLS
2465 have_eaptls_secret_server(char *client, char *server,
2466 int need_ip, int *lacks_ipp)
2471 struct wordlist *addrs;
2472 char servcertfile[MAXWORDLEN];
2473 char clicertfile[MAXWORDLEN];
2474 char cacertfile[MAXWORDLEN];
2475 char pkfile[MAXWORDLEN];
2477 filename = PPP_PATH_EAPTLSSERVFILE;
2478 f = fopen(filename, "r");
2482 if (client != NULL && client[0] == 0)
2484 else if (server != NULL && server[0] == 0)
2488 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2489 cacertfile, pkfile, &addrs, NULL, filename,
2495 if (ret >= 0 && !eaptls_init_ssl(1, cacertfile, servcertfile,
2496 clicertfile, pkfile))
2500 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
2506 free_wordlist(addrs);
2513 have_eaptls_secret_client(char *client, char *server)
2518 struct wordlist *addrs = NULL;
2519 char servcertfile[MAXWORDLEN];
2520 char clicertfile[MAXWORDLEN];
2521 char cacertfile[MAXWORDLEN];
2522 char pkfile[MAXWORDLEN];
2524 if (client != NULL && client[0] == 0)
2526 else if (server != NULL && server[0] == 0)
2529 if ((cacert_file || ca_path) && cert_file && privkey_file)
2534 filename = PPP_PATH_EAPTLSCLIFILE;
2535 f = fopen(filename, "r");
2540 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2541 cacertfile, pkfile, &addrs, NULL, filename,
2546 if (ret >= 0 && !eaptls_init_ssl(0, cacertfile, clicertfile,
2547 servcertfile, pkfile))
2552 free_wordlist(addrs);
2559 scan_authfile_eaptls(FILE *f, char *client, char *server,
2560 char *cli_cert, char *serv_cert, char *ca_cert,
2561 char *pk, struct wordlist **addrs,
2562 struct wordlist **opts,
2563 char *filename, int flags)
2566 int got_flag, best_flag;
2567 struct wordlist *ap, *addr_list, *alist, **app;
2568 char word[MAXWORDLEN];
2575 if (!getword(f, word, &newline, filename))
2576 return -1; /* file is empty??? */
2581 * Skip until we find a word at the start of a line.
2583 while (!newline && getword(f, word, &newline, filename));
2585 break; /* got to end of file */
2588 * Got a client - check if it's a match or a wildcard.
2591 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2596 got_flag = NONWILD_CLIENT;
2599 * Now get a server and check if it matches.
2601 if (!getword(f, word, &newline, filename))
2605 if (!ISWILD(word)) {
2606 if (server != NULL && strcmp(word, server) != 0)
2608 got_flag |= NONWILD_SERVER;
2612 * Got some sort of a match - see if it's better than what
2615 if (got_flag <= best_flag)
2621 if (!getword(f, word, &newline, filename))
2625 if (strcmp(word, "-") != 0) {
2626 strlcpy(cli_cert, word, MAXWORDLEN);
2633 if (!getword(f, word, &newline, filename))
2637 if (strcmp(word, "-") != 0) {
2638 strlcpy(serv_cert, word, MAXWORDLEN);
2645 if (!getword(f, word, &newline, filename))
2649 strlcpy(ca_cert, word, MAXWORDLEN);
2654 if (!getword(f, word, &newline, filename))
2658 strlcpy(pk, word, MAXWORDLEN);
2662 * Now read address authorization info and make a wordlist.
2666 if (!getword(f, word, &newline, filename) || newline)
2668 ap = (struct wordlist *)
2669 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2671 novm("authorized addresses");
2672 ap->word = (char *) (ap + 1);
2673 strcpy(ap->word, word);
2679 * This is the best so far; remember it.
2681 best_flag = got_flag;
2683 free_wordlist(addr_list);
2690 /* scan for a -- word indicating the start of options */
2691 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2692 if (strcmp(ap->word, "--") == 0)
2694 /* ap = start of options */
2696 ap = ap->next; /* first option */
2697 free(*app); /* free the "--" word */
2698 *app = NULL; /* terminate addr list */
2702 else if (ap != NULL)
2706 else if (addr_list != NULL)
2707 free_wordlist(addr_list);
2714 get_eaptls_secret(int unit, char *client, char *server,
2715 char *clicertfile, char *servcertfile, char *cacertfile,
2716 char *capath, char *pkfile, char *pkcs12, int am_server)
2720 char *filename = NULL;
2721 struct wordlist *addrs = NULL;
2722 struct wordlist *opts = NULL;
2724 /* maybe overkill, but it eases debugging */
2725 bzero(clicertfile, MAXWORDLEN);
2726 bzero(servcertfile, MAXWORDLEN);
2727 bzero(cacertfile, MAXWORDLEN);
2728 bzero(capath, MAXWORDLEN);
2729 bzero(pkfile, MAXWORDLEN);
2730 bzero(pkcs12, MAXWORDLEN);
2732 /* the ca+cert+privkey can also be specified as options */
2733 if (!am_server && (cacert_file || ca_path) && cert_file && privkey_file )
2735 strlcpy( clicertfile, cert_file, MAXWORDLEN );
2737 strlcpy( cacertfile, cacert_file, MAXWORDLEN );
2739 strlcpy( capath, ca_path, MAXWORDLEN );
2740 strlcpy( pkfile, privkey_file, MAXWORDLEN );
2742 else if (!am_server && pkcs12_file)
2744 strlcpy( pkcs12, pkcs12_file, MAXWORDLEN );
2746 strlcpy( cacertfile, cacert_file, MAXWORDLEN );
2748 strlcpy( capath, ca_path, MAXWORDLEN );
2752 filename = (am_server ? PPP_PATH_EAPTLSSERVFILE : PPP_PATH_EAPTLSCLIFILE);
2755 fp = fopen(filename, "r");
2758 error("Can't open eap-tls secret file %s: %m", filename);
2762 check_access(fp, filename);
2764 ret = scan_authfile_eaptls(fp, client, server, clicertfile, servcertfile,
2765 cacertfile, pkfile, &addrs, &opts, filename, 0);
2769 if (ret < 0) return 0;
2772 if (eaptls_passwd_hook)
2774 dbglog( "Calling eaptls password hook" );
2775 if ( (*eaptls_passwd_hook)(pkfile, passwd) < 0)
2777 error("Unable to obtain EAP-TLS password for %s (%s) from plugin",
2783 set_allowed_addrs(unit, addrs, opts);
2784 else if (opts != NULL)
2785 free_wordlist(opts);
2787 free_wordlist(addrs);