Debian Codegraph @

pppd/auth.c

raw
1 /*
2 * auth.c - PPP authentication and phase control.
3 *
4 * Copyright (c) 1993-2024 Paul Mackerras. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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
16 * distribution.
17 *
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.
25 *
26 * Derived from main.c, which is:
27 *
28 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 *
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 *
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
40 * distribution.
41 *
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
48 * 5000 Forbes Avenue
49 * Pittsburgh, PA 15213-3890
50 * (412) 268-4387, fax: (412) 268-7395
51 * [email protected]
52 *
53 * 4. Redistributions of any form whatsoever must retain the following
54 * acknowledgment:
55 * "This product includes software developed by Computing Services
56 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
57 *
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.
65 */
66
67 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70
71 #include <stdio.h>
72 #include <stddef.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75 #include <errno.h>
76 #include <pwd.h>
77 #include <grp.h>
78 #include <string.h>
79 #include <strings.h>
80 #include <sys/param.h>
81 #include <sys/types.h>
82 #include <sys/stat.h>
83 #include <sys/socket.h>
84 #include <utmp.h>
85 #include <fcntl.h>
86 #if defined(_PATH_LASTLOG) && defined(__linux__)
87 #include <lastlog.h>
88 #endif
89
90 #include <netdb.h>
91 #include <netinet/in.h>
92 #include <arpa/inet.h>
93
94
95 #ifdef HAVE_SHADOW_H
96 #include <shadow.h>
97 #ifndef PW_PPP
98 #define PW_PPP PW_LOGIN
99 #endif
100 #endif
101 #include <time.h>
102
103 #ifdef HAVE_CRYPT_H
104 #include <crypt.h>
105 #endif
106
107 #ifdef SYSTEMD
108 #include <systemd/sd-daemon.h>
109 #endif
110
111 #include "pppd-private.h"
112 #include "options.h"
113 #include "fsm.h"
114 #include "lcp.h"
115 #include "ccp.h"
116 #include "ecp.h"
117 #include "ipcp.h"
118 #include "upap.h"
119 #include "chap.h"
120 #include "eap.h"
121 #ifdef PPP_WITH_EAPTLS
122 #include "eap-tls.h"
123 #endif
124 #ifdef PPP_WITH_CBCP
125 #include "cbcp.h"
126 #endif
127 #include "multilink.h"
128 #include "pathnames.h"
129 #include "session.h"
130
131
132 /* Bits in scan_authfile return value */
133 #define NONWILD_SERVER 1
134 #define NONWILD_CLIENT 2
135
136 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
137
138 /* The name by which the peer authenticated itself to us. */
139 char peer_authname[MAXNAMELEN];
140
141 /* Records which authentication operations haven't completed yet. */
142 static int auth_pending[NUM_PPP];
143
144 /* Records which authentication operations have been completed. */
145 int auth_done[NUM_PPP];
146
147 /* List of addresses which the peer may use. */
148 static struct permitted_ip *addresses[NUM_PPP];
149
150 /* Wordlist giving addresses which the peer may use
151 without authenticating itself. */
152 static struct wordlist *noauth_addrs;
153
154 /* Remote telephone number, if available */
155 char remote_number[MAXNAMELEN];
156
157 /* Wordlist giving remote telephone numbers which may connect. */
158 static struct wordlist *permitted_numbers;
159
160 /* Extra options to apply, from the secrets file entry for the peer. */
161 static struct wordlist *extra_options;
162
163 /* Number of network protocols which we have opened. */
164 static int num_np_open;
165
166 /* Number of network protocols which have come up. */
167 static int num_np_up;
168
169 /* Set if we got the contents of passwd[] from the pap-secrets file. */
170 static int passwd_from_file;
171
172 /* Set if we require authentication only because we have a default route. */
173 static bool default_auth;
174
175 /* Hook to enable a plugin to control the idle time limit */
176 int (*idle_time_hook)(struct ppp_idle *) = NULL;
177
178 /* Hook for a plugin to say whether we can possibly authenticate any peer */
179 pap_check_hook_fn *pap_check_hook = NULL;
180
181 /* Hook for a plugin to check the PAP user and password */
182 pap_auth_hook_fn *pap_auth_hook = NULL;
183
184 /* Hook for a plugin to know about the PAP user logout */
185 pap_logout_hook_fn *pap_logout_hook = NULL;
186
187 /* Hook for a plugin to get the PAP password for authenticating us */
188 pap_passwd_hook_fn *pap_passwd_hook = NULL;
189
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;
192
193 /* Hook for a plugin to get the CHAP password for authenticating us */
194 chap_passwd_hook_fn *chap_passwd_hook = NULL;
195
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;
199 #endif
200
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;
205
206 int (*allowed_address_hook)(u_int32_t addr) = NULL;
207
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;
211
212 /* A notifier for when the link goes down. */
213 struct notifier *link_down_notifier = NULL;
214
215 /*
216 * This is used to ensure that we don't start an auth-up/down
217 * script while one is already running.
218 */
219 enum script_state {
220 s_down,
221 s_up
222 };
223
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;
227
228 /*
229 * Option variables.
230 */
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 */
240 #else
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 */
243 #endif
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 */
253
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 */
262 #endif
263
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 */
269 #endif
270
271 static char *uafname; /* name of most recent +ua file */
272
273 /* Prototypes for procedures local to this file. */
274
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,
283 int *lacks_ipp);
284
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);
295 #endif
296
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 **,
300 char *, int);
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 *);
313
314 /*
315 * Authentication-related options.
316 */
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,
322 &allow_any_ip },
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 },
354 #endif
355
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 },
385 #endif
386
387 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
388 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
389 &auth_required },
390 { "refuse-eap", o_bool, &refuse_eap,
391 "Don't agree to authenticate to peer with EAP", 1 },
392
393 { "name", o_string, our_name,
394 "Set local name for authentication",
395 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
396
397 { "+ua", o_special, (void *)setupapfile,
398 "Get PAP user and password from file",
399 OPT_PRIO | OPT_A2STRVAL, &uafname },
400
401 { "user", o_string, user,
402 "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
403 &explicit_user, MAXNAMELEN },
404
405 { "password", o_string, passwd,
406 "Password for authenticating us to the peer",
407 OPT_PRIO | OPT_STATIC | OPT_HIDE,
408 &explicit_passwd, MAXSECRETLEN },
409
410 { "usehostname", o_bool, &usehostname,
411 "Must use hostname for authentication", 1 },
412
413 { "remotename", o_string, remote_name,
414 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
415 &explicit_remote, MAXNAMELEN },
416
417 { "pap-secrets", o_string, path_upapfile,
418 "Set pathname of pap-secrets", OPT_PRIO | OPT_PRIV | OPT_STATIC,
419 NULL, MAXPATHLEN },
420
421 { "chap-secrets", o_string, path_chapfile,
422 "Set pathname of chap-secrets", OPT_PRIO | OPT_PRIV | OPT_STATIC,
423 NULL, MAXPATHLEN },
424
425 { "login", o_bool, &uselogin,
426 "Use system password database for PAP", OPT_A2COPY | 1 ,
427 &session_mgmt },
428 { "enable-session", o_bool, &session_mgmt,
429 "Enable session accounting for remote peers", OPT_PRIV | 1 },
430
431 { "papcrypt", o_bool, &cryptpap,
432 "PAP passwords are encrypted", 1 },
433
434 { "privgroup", o_special, (void *)privgroup,
435 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
436
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 },
440
441 { "remotenumber", o_string, remote_number,
442 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
443 NULL, MAXNAMELEN },
444
445 { "allow-number", o_special, (void *)set_permitted_number,
446 "Set telephone number(s) which are allowed to connect",
447 OPT_PRIV | OPT_A2LIST },
448
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)" },
460 #endif
461
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 */
469 { NULL }
470 };
471
472 const char *
473 ppp_remote_name()
474 {
475 return remote_name;
476 }
477
478 const char *
479 ppp_get_remote_number(void)
480 {
481 return remote_number;
482 }
483
484 void
485 ppp_set_remote_number(const char *buf)
486 {
487 if (buf) {
488 strlcpy(remote_number, buf, sizeof(remote_number));
489 ppp_script_setenv("REMOTENUMBER", remote_number, 0);
490 }
491 }
492
493 const char *
494 ppp_peer_authname(char *buf, size_t bufsz)
495 {
496 if (buf && bufsz > 0) {
497 strlcpy(buf, peer_authname, bufsz);
498 return buf;
499 }
500 return peer_authname;
501 }
502
503 /*
504 * setupapfile - specifies UPAP info for authenticating with peer.
505 */
506 static int
507 setupapfile(char **argv)
508 {
509 FILE *ufile;
510 int l;
511 uid_t euid;
512 char u[MAXNAMELEN], p[MAXSECRETLEN];
513 char *fname;
514
515 lcp_allowoptions[0].neg_upap = 1;
516
517 /* open user info file */
518 fname = strdup(*argv);
519 if (fname == NULL)
520 novm("+ua file name");
521 euid = geteuid();
522 if (seteuid(getuid()) == -1) {
523 ppp_option_error("unable to reset uid before opening %s: %m", fname);
524 free(fname);
525 return 0;
526 }
527 ufile = fopen(fname, "r");
528 if (seteuid(euid) == -1)
529 fatal("unable to regain privileges: %m");
530 if (ufile == NULL) {
531 ppp_option_error("unable to open user login data file %s", fname);
532 free(fname);
533 return 0;
534 }
535 check_access(ufile, fname);
536 uafname = fname;
537
538 /* get username */
539 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
540 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
541 fclose(ufile);
542 ppp_option_error("unable to read user login data file %s", fname);
543 free(fname);
544 return 0;
545 }
546 fclose(ufile);
547
548 /* get rid of newlines */
549 l = strlen(u);
550 if (l > 0 && u[l-1] == '\n')
551 u[l-1] = 0;
552 l = strlen(p);
553 if (l > 0 && p[l-1] == '\n')
554 p[l-1] = 0;
555
556 if (override_value("user", option_priority, fname)) {
557 strlcpy(user, u, sizeof(user));
558 explicit_user = 1;
559 }
560 if (override_value("passwd", option_priority, fname)) {
561 strlcpy(passwd, p, sizeof(passwd));
562 explicit_passwd = 1;
563 }
564
565 free(fname);
566 return (1);
567 }
568
569
570 /*
571 * privgroup - allow members of the group to have privileged access.
572 */
573 static int
574 privgroup(char **argv)
575 {
576 struct group *g;
577 int i;
578
579 g = getgrnam(*argv);
580 if (g == 0) {
581 ppp_option_error("group %s is unknown", *argv);
582 return 0;
583 }
584 for (i = 0; i < ngroups; ++i) {
585 if (groups[i] == g->gr_gid) {
586 privileged = 1;
587 break;
588 }
589 }
590 return 1;
591 }
592
593
594 /*
595 * set_noauth_addr - set address(es) that can be used without authentication.
596 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
597 */
598 static int
599 set_noauth_addr(char **argv)
600 {
601 char *addr = *argv;
602 int l = strlen(addr) + 1;
603 struct wordlist *wp;
604
605 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
606 if (wp == NULL)
607 novm("allow-ip argument");
608 wp->word = (char *) (wp + 1);
609 wp->next = noauth_addrs;
610 BCOPY(addr, wp->word, l);
611 noauth_addrs = wp;
612 return 1;
613 }
614
615
616 /*
617 * set_permitted_number - set remote telephone number(s) that may connect.
618 */
619 static int
620 set_permitted_number(char **argv)
621 {
622 char *number = *argv;
623 int l = strlen(number) + 1;
624 struct wordlist *wp;
625
626 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
627 if (wp == NULL)
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;
633 return 1;
634 }
635
636
637 /*
638 * An Open on LCP has requested a change from Dead to Establish phase.
639 */
640 void
641 link_required(int unit)
642 {
643 }
644
645 /*
646 * Bring the link up to the point of being able to do ppp.
647 */
648 void start_link(int unit)
649 {
650 ppp_set_status(EXIT_CONNECT_FAILED);
651 new_phase(PHASE_SERIALCONN);
652
653 hungup = 0;
654 devfd = the_channel->connect();
655 if (devfd < 0)
656 goto fail;
657
658 /* set up the serial device as a ppp interface */
659 /*
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.
665 */
666 fd_ppp = the_channel->establish_ppp(devfd);
667 if (fd_ppp < 0) {
668 ppp_set_status(EXIT_FATAL_ERROR);
669 goto disconnect;
670 }
671
672 if (!demand && ifunit >= 0)
673 set_ifunit(1);
674
675 /*
676 * Start opening the connection and wait for
677 * incoming events (reply, timeout, etc.).
678 */
679 if (ifunit >= 0)
680 notice("Connect: %s <--> %s", ifname, ppp_devname);
681 else
682 notice("Starting negotiation on %s", ppp_devname);
683 add_fd(fd_ppp);
684
685 ppp_set_status(EXIT_NEGOTIATION_FAILED);
686 new_phase(PHASE_ESTABLISH);
687
688 lcp_lowerup(0);
689 return;
690
691 disconnect:
692 new_phase(PHASE_DISCONNECT);
693 if (the_channel->disconnect)
694 the_channel->disconnect();
695
696 fail:
697 new_phase(PHASE_DEAD);
698 if (the_channel->cleanup)
699 (*the_channel->cleanup)();
700 }
701
702 /*
703 * LCP has terminated the link; go to the Dead phase and take the
704 * physical layer down.
705 */
706 void
707 link_terminated(int unit)
708 {
709 if (in_phase(PHASE_DEAD) || in_phase(PHASE_MASTER))
710 return;
711 new_phase(PHASE_DISCONNECT);
712
713 if (pap_logout_hook) {
714 pap_logout_hook();
715 }
716 session_end(devnam);
717
718 if (!mp_on()) {
719 notice("Connection terminated.");
720 print_link_stats();
721 } else
722 notice("Link terminated.");
723
724 /*
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.
728 */
729 if (!demand && !mp_on())
730 remove_pidfiles();
731 /*
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.
735 */
736 if (fd_ppp >= 0) {
737 remove_fd(fd_ppp);
738 clean_check();
739 the_channel->disestablish_ppp(devfd);
740 if (mp_on())
741 mp_exit_bundle();
742 fd_ppp = -1;
743 }
744 if (!hungup)
745 lcp_lowerdown(0);
746 if (!mp_on() && !demand)
747 ppp_script_unsetenv("IFNAME");
748
749 /*
750 * Run disconnector script, if requested.
751 * XXX we may not be able to do this if the line has hung up!
752 */
753 if (devfd >= 0 && the_channel->disconnect) {
754 the_channel->disconnect();
755 devfd = -1;
756 }
757 if (the_channel->cleanup)
758 (*the_channel->cleanup)();
759
760 if (mp_on() && mp_master()) {
761 if (!bundle_terminating) {
762 new_phase(PHASE_MASTER);
763 if (master_detach && !detached)
764 detach();
765 } else
766 mp_bundle_terminated();
767 } else
768 new_phase(PHASE_DEAD);
769 }
770
771 /*
772 * LCP has gone down; it will either die or try to re-establish.
773 */
774 void
775 link_down(int unit)
776 {
777 if (auth_state != s_down) {
778 notify(link_down_notifier, 0);
779 auth_state = s_down;
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);
784 }
785 }
786 if (!mp_on())
787 {
788 upper_layers_down(unit);
789 if (!in_phase(PHASE_DEAD) && !in_phase(PHASE_MASTER))
790 new_phase(PHASE_ESTABLISH);
791 }
792 /* XXX if doing_multilink, should do something to stop
793 network-layer traffic on the link */
794 }
795
796 void upper_layers_down(int unit)
797 {
798 int i;
799 struct protent *protp;
800
801 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
802 if (!protp->enabled_flag)
803 continue;
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");
808 }
809 num_np_open = 0;
810 num_np_up = 0;
811 }
812
813 /*
814 * The link is established.
815 * Proceed to the Dead, Authenticate or Network phase as appropriate.
816 */
817 void
818 link_established(int unit)
819 {
820 int auth;
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];
826 #endif
827 int i;
828 struct protent *protp;
829
830 /*
831 * Tell higher-level protocols that LCP is up.
832 */
833 if (!mp_on())
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);
840
841 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
842 /*
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,
847 * boot it out.
848 */
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");
855 return;
856 }
857 }
858
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);
864 return;
865 }
866
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);
871 return;
872 }
873 #endif
874
875 new_phase(PHASE_AUTHENTICATE);
876 auth = 0;
877 if (go->neg_eap) {
878 eap_authpeer(unit, our_name);
879 auth |= EAP_PEER;
880 } else if (go->neg_chap) {
881 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
882 auth |= CHAP_PEER;
883 } else if (go->neg_upap) {
884 upap_authpeer(unit);
885 auth |= PAP_PEER;
886 }
887 if (ho->neg_eap) {
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");
900 }
901 upap_authwithpeer(unit, user, passwd);
902 auth |= PAP_WITHPEER;
903 }
904 auth_pending[unit] = auth;
905 auth_done[unit] = 0;
906
907 if (!auth)
908 network_phase(unit);
909 }
910
911 /*
912 * Proceed to the network phase.
913 */
914 static void
915 network_phase(int unit)
916 {
917 lcp_options *go = &lcp_gotoptions[unit];
918
919 /* Log calling number. */
920 if (*remote_number)
921 notice("peer from calling number %q authorized", remote_number);
922
923 /*
924 * If the peer had to authenticate, run the auth-up script now.
925 */
926 notify(auth_up_notifier, 0);
927 if (go->neg_chap || go->neg_upap || go->neg_eap) {
928 auth_state = s_up;
929 if (auth_script_state == s_down && auth_script_pid == 0) {
930 auth_script_state = s_up;
931 auth_script(PPP_PATH_AUTHUP);
932 }
933 }
934
935 #ifdef PPP_WITH_CBCP
936 /*
937 * If we negotiated callback, do it now.
938 */
939 if (go->neg_cbcp) {
940 new_phase(PHASE_CALLBACK);
941 (*cbcp_protent.open)(unit);
942 return;
943 }
944 #endif
945
946 /*
947 * Process extra options from the secrets file
948 */
949 if (extra_options) {
950 options_from_list(extra_options, 1);
951 free_wordlist(extra_options);
952 extra_options = 0;
953 }
954 start_networks(unit);
955 }
956
957 void
958 start_networks(int unit)
959 {
960 int i;
961 struct protent *protp;
962 int ecp_required, mppe_required;
963
964 new_phase(PHASE_NETWORK);
965
966 #ifdef PPP_WITH_MULTILINK
967 if (multilink) {
968 if (mp_join_bundle()) {
969 if (multilink_join_hook)
970 (*multilink_join_hook)();
971 if (updetach && !nodetach)
972 detach();
973 return;
974 }
975 }
976 #endif /* PPP_WITH_MULTILINK */
977
978 #ifdef PPP_WITH_FILTER
979 if (!demand)
980 set_filters(&pass_filter, &active_filter);
981 #endif
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)
986 (*protp->open)(0);
987
988 /*
989 * Bring up other network protocols iff encryption is not required.
990 */
991 ecp_required = ecp_gotoptions[unit].required;
992 mppe_required = ccp_gotoptions[unit].mppe;
993 if (!ecp_required && !mppe_required)
994 continue_networks(unit);
995 }
996
997 void
998 continue_networks(int unit)
999 {
1000 int i;
1001 struct protent *protp;
1002
1003 /*
1004 * Start the "real" network protocols.
1005 */
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) {
1010 (*protp->open)(0);
1011 ++num_np_open;
1012 }
1013
1014 if (num_np_open == 0)
1015 /* nothing to do */
1016 lcp_close(0, "No network protocols running");
1017 }
1018
1019 /*
1020 * The peer has failed to authenticate himself using `protocol'.
1021 */
1022 void
1023 auth_peer_fail(int unit, int protocol)
1024 {
1025 /*
1026 * Authentication failure: take the link down
1027 */
1028 ppp_set_status(EXIT_PEER_AUTH_FAILED);
1029 lcp_close(unit, "Authentication failed");
1030 }
1031
1032 /*
1033 * The peer has been successfully authenticated using `protocol'.
1034 */
1035 void
1036 auth_peer_success(int unit, int protocol, int prot_flavor,
1037 char *name, int namelen)
1038 {
1039 int bit;
1040 const char *prot;
1041
1042 switch (protocol) {
1043 case PPP_CHAP:
1044 bit = CHAP_PEER;
1045 prot = "CHAP";
1046 switch (prot_flavor) {
1047 case CHAP_MD5:
1048 bit |= CHAP_MD5_PEER;
1049 break;
1050 #ifdef PPP_WITH_CHAPMS
1051 case CHAP_MICROSOFT:
1052 bit |= CHAP_MS_PEER;
1053 break;
1054 case CHAP_MICROSOFT_V2:
1055 bit |= CHAP_MS2_PEER;
1056 break;
1057 #endif
1058 }
1059 break;
1060 case PPP_PAP:
1061 bit = PAP_PEER;
1062 prot = "PAP";
1063 break;
1064 case PPP_EAP:
1065 bit = EAP_PEER;
1066 prot = "EAP";
1067 break;
1068 default:
1069 warn("auth_peer_success: unknown protocol %x", protocol);
1070 prot = "unknown protocol";
1071 return;
1072 }
1073
1074 /*
1075 * Save the authenticated name of the peer for later.
1076 */
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);
1083
1084 /* Save the authentication method for later. */
1085 auth_done[unit] |= bit;
1086
1087 /*
1088 * If there is no more authentication still to be done,
1089 * proceed to the network (or callback) phase.
1090 */
1091 if ((auth_pending[unit] &= ~bit) == 0)
1092 network_phase(unit);
1093 }
1094
1095 /*
1096 * We have failed to authenticate ourselves to the peer using `protocol'.
1097 */
1098 void
1099 auth_withpeer_fail(int unit, int protocol)
1100 {
1101 if (passwd_from_file)
1102 BZERO(passwd, MAXSECRETLEN);
1103 /*
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.
1108 */
1109 ppp_set_status(EXIT_AUTH_TOPEER_FAILED);
1110 lcp_close(unit, "Failed to authenticate ourselves to peer");
1111 }
1112
1113 /*
1114 * We have successfully authenticated ourselves with the peer using `protocol'.
1115 */
1116 void
1117 auth_withpeer_success(int unit, int protocol, int prot_flavor)
1118 {
1119 int bit;
1120 const char *prot = "";
1121
1122 switch (protocol) {
1123 case PPP_CHAP:
1124 bit = CHAP_WITHPEER;
1125 prot = "CHAP";
1126 switch (prot_flavor) {
1127 case CHAP_MD5:
1128 bit |= CHAP_MD5_WITHPEER;
1129 break;
1130 #ifdef PPP_WITH_CHAPMS
1131 case CHAP_MICROSOFT:
1132 bit |= CHAP_MS_WITHPEER;
1133 break;
1134 case CHAP_MICROSOFT_V2:
1135 bit |= CHAP_MS2_WITHPEER;
1136 break;
1137 #endif
1138 }
1139 break;
1140 case PPP_PAP:
1141 if (passwd_from_file)
1142 BZERO(passwd, MAXSECRETLEN);
1143 bit = PAP_WITHPEER;
1144 prot = "PAP";
1145 break;
1146 case PPP_EAP:
1147 bit = EAP_WITHPEER;
1148 prot = "EAP";
1149 break;
1150 default:
1151 warn("auth_withpeer_success: unknown protocol %x", protocol);
1152 bit = 0;
1153 }
1154
1155 notice("%s authentication succeeded", prot);
1156
1157 /* Save the authentication method for later. */
1158 auth_done[unit] |= bit;
1159
1160 /*
1161 * If there is no more authentication still being done,
1162 * proceed to the network (or callback) phase.
1163 */
1164 if ((auth_pending[unit] &= ~bit) == 0)
1165 network_phase(unit);
1166 }
1167
1168
1169 /*
1170 * np_up - a network protocol has come up.
1171 */
1172 void
1173 np_up(int unit, int proto)
1174 {
1175 int tlim;
1176
1177 if (num_np_up == 0) {
1178 /*
1179 * At this point we consider that the link has come up successfully.
1180 */
1181 ppp_set_status(EXIT_OK);
1182 unsuccess = 0;
1183 new_phase(PHASE_RUNNING);
1184
1185 if (idle_time_hook != 0)
1186 tlim = (*idle_time_hook)(NULL);
1187 else
1188 tlim = ppp_get_max_idle_time();
1189 if (tlim > 0)
1190 TIMEOUT(check_idle, NULL, tlim);
1191
1192 /*
1193 * Set a timeout to close the connection once the maximum
1194 * connect time has expired.
1195 */
1196 if (ppp_get_max_connect_time() > 0)
1197 TIMEOUT(connect_time_expired, 0, ppp_get_max_connect_time());
1198
1199 /*
1200 * Configure a check to see if session has outlived it's limit
1201 * in terms of octets
1202 */
1203 if (maxoctets > 0)
1204 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1205
1206 /*
1207 * Detach now, if the updetach option was given.
1208 */
1209 if (updetach && !nodetach) {
1210 dbglog("updetach is set. Now detaching.");
1211 detach();
1212 #ifdef SYSTEMD
1213 } else if (nodetach && up_sdnotify) {
1214 dbglog("up_sdnotify is set. Now notifying systemd: READY=1");
1215 sd_notify(0, "READY=1");
1216 #endif
1217 }
1218 }
1219 ++num_np_up;
1220 }
1221
1222 /*
1223 * np_down - a network protocol has gone down.
1224 */
1225 void
1226 np_down(int unit, int proto)
1227 {
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);
1233 }
1234 }
1235
1236 /*
1237 * np_finished - a network protocol has finished using the link.
1238 */
1239 void
1240 np_finished(int unit, int proto)
1241 {
1242 if (--num_np_open <= 0) {
1243 /* no further use for the link: shut up shop. */
1244 lcp_close(0, "No network protocols running");
1245 }
1246 }
1247
1248 /*
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
1251 */
1252 static void
1253 check_maxoctets(void *arg)
1254 {
1255 unsigned int used = 0;
1256 ppp_link_stats_st stats;
1257
1258 if (ppp_get_link_stats(&stats)) {
1259 switch(maxoctets_dir) {
1260 case PPP_OCTETS_DIRECTION_IN:
1261 used = stats.bytes_in;
1262 break;
1263 case PPP_OCTETS_DIRECTION_OUT:
1264 used = stats.bytes_out;
1265 break;
1266 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1267 case PPP_OCTETS_DIRECTION_MAXSESSION:
1268 used = (stats.bytes_in > stats.bytes_out)
1269 ? stats.bytes_in
1270 : stats.bytes_out;
1271 break;
1272 default:
1273 used = stats.bytes_in+stats.bytes_out;
1274 break;
1275 }
1276 }
1277
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;
1283 need_holdoff = 0;
1284 } else {
1285 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1286 }
1287 }
1288
1289 /*
1290 * check_idle - check whether the link has been idle for long
1291 * enough that we can shut it down.
1292 */
1293 static void
1294 check_idle(void *arg)
1295 {
1296 struct ppp_idle idle;
1297 time_t itime;
1298 int tlim;
1299
1300 if (!get_idle_time(0, &idle))
1301 return;
1302 if (idle_time_hook != 0) {
1303 tlim = idle_time_hook(&idle);
1304 } else {
1305 itime = MIN(idle.xmit_idle, idle.recv_idle);
1306 tlim = ppp_get_max_idle_time() - itime;
1307 }
1308 if (tlim <= 0) {
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");
1313 need_holdoff = 0;
1314 } else {
1315 TIMEOUT(check_idle, NULL, tlim);
1316 }
1317 }
1318
1319 /*
1320 * connect_time_expired - log a message and close the connection.
1321 */
1322 static void
1323 connect_time_expired(void *arg)
1324 {
1325 info("Connect time expired");
1326 ppp_set_status(EXIT_CONNECT_TIME);
1327 lcp_close(0, "Connect time expired"); /* Close connection */
1328 need_holdoff = 0;
1329 }
1330
1331 /*
1332 * auth_check_options - called to check authentication options.
1333 */
1334 void
1335 auth_check_options(void)
1336 {
1337 lcp_options *wo = &lcp_wantoptions[0];
1338 int can_auth;
1339 int lacks_ip;
1340
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));
1344
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));
1349
1350 #if defined(SYSTEM_CA_PATH) && (defined(PPP_WITH_EAPTLS) || defined(PPP_WITH_PEAP))
1351 /* Use system default for CA Path if not specified */
1352 if (!ca_path) {
1353 ca_path = SYSTEM_CA_PATH;
1354 }
1355 #endif
1356
1357 /*
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.
1360 */
1361 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1362 auth_required = 1;
1363 default_auth = 1;
1364 }
1365
1366 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1367 if (wo->chap_mdtype)
1368 wo->neg_chap = 1;
1369
1370 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1371 if (auth_required) {
1372 allow_any_ip = 0;
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;
1376 wo->neg_upap = 1;
1377 wo->neg_eap = 1;
1378 }
1379 } else {
1380 wo->neg_chap = 0;
1381 wo->chap_mdtype = MDTYPE_NONE;
1382 wo->neg_upap = 0;
1383 wo->neg_eap = 0;
1384 }
1385
1386 /*
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.
1390 */
1391 lacks_ip = 0;
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);
1396 }
1397 if (!can_auth && wo->neg_eap) {
1398 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1399 our_name, 1, &lacks_ip);
1400 }
1401
1402 #ifdef PPP_WITH_EAPTLS
1403 if (!can_auth && wo->neg_eap) {
1404 can_auth =
1405 have_eaptls_secret_server((explicit_remote ? remote_name :
1406 NULL), our_name, 1, &lacks_ip);
1407
1408 }
1409 #endif
1410
1411 if (auth_required && !can_auth && noauth_addrs == NULL) {
1412 if (default_auth) {
1413 ppp_option_error(
1414 "By default the remote system is required to authenticate itself");
1415 ppp_option_error(
1416 "(because this system has a default route to the internet)");
1417 } else if (explicit_remote)
1418 ppp_option_error(
1419 "The remote system (%s) is required to authenticate itself",
1420 remote_name);
1421 else
1422 ppp_option_error(
1423 "The remote system is required to authenticate itself");
1424 ppp_option_error(
1425 "but I couldn't find any suitable secret (password) for it to use to do so.");
1426 if (lacks_ip)
1427 ppp_option_error(
1428 "(None of the available passwords would let it use an IP address.)");
1429
1430 exit(1);
1431 }
1432
1433 /*
1434 * Early check for remote number authorization.
1435 */
1436 if (!auth_number()) {
1437 warn("calling number %q is not authorized", remote_number);
1438 exit(EXIT_CNID_AUTH_FAILED);
1439 }
1440 }
1441
1442 /*
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.
1446 */
1447 void
1448 auth_reset(int unit)
1449 {
1450 lcp_options *go = &lcp_gotoptions[unit];
1451 lcp_options *ao = &lcp_allowoptions[unit];
1452 int hadchap;
1453
1454 hadchap = -1;
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:
1459 NULL), 0, NULL)));
1460 ao->neg_eap = !refuse_eap && (
1461 passwd[0] != 0 ||
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))
1467 #endif
1468 );
1469
1470 hadchap = -1;
1471 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1472 go->neg_upap = 0;
1473 if (go->neg_chap) {
1474 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1475 our_name, 1, NULL)))
1476 go->neg_chap = 0;
1477 }
1478 if (go->neg_eap &&
1479 (hadchap == 0 || (hadchap == -1 &&
1480 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1481 1, NULL))) &&
1482 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1483 NULL)
1484 #ifdef PPP_WITH_EAPTLS
1485 && !have_eaptls_secret_server((explicit_remote? remote_name: NULL),
1486 our_name, 1, NULL)
1487 #endif
1488 )
1489 go->neg_eap = 0;
1490 }
1491
1492
1493 /*
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.
1497 *
1498 * returns:
1499 * UPAP_AUTHNAK: Authentication failed.
1500 * UPAP_AUTHACK: Authentication succeeded.
1501 * In either case, msg points to an appropriate message.
1502 */
1503 int
1504 check_passwd(int unit,
1505 char *auser, int userlen,
1506 char *apasswd, int passwdlen, char **msg)
1507 {
1508 int ret;
1509 char *filename;
1510 FILE *f;
1511 struct wordlist *addrs = NULL, *opts = NULL;
1512 char passwd[256], user[256];
1513 char secret[MAXWORDLEN];
1514 static int attempts = 0;
1515
1516 /*
1517 * Make copies of apasswd and auser, then null-terminate them.
1518 * If there are unprintable characters in the password, make
1519 * them visible.
1520 */
1521 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1522 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1523 *msg = "";
1524
1525 /*
1526 * Check if a plugin wants to handle this.
1527 */
1528 if (pap_auth_hook) {
1529 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1530 if (ret >= 0) {
1531 /* note: set_allowed_addrs() saves opts (but not addrs):
1532 don't free it! */
1533 if (ret)
1534 set_allowed_addrs(unit, addrs, opts);
1535 else if (opts != 0)
1536 free_wordlist(opts);
1537 if (addrs != 0)
1538 free_wordlist(addrs);
1539 BZERO(passwd, sizeof(passwd));
1540 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1541 }
1542 }
1543
1544 /*
1545 * Open the file of pap secrets and scan for a suitable secret
1546 * for authenticating this user.
1547 */
1548 filename = path_upapfile;
1549 addrs = opts = NULL;
1550 ret = UPAP_AUTHNAK;
1551 f = fopen(filename, "r");
1552 if (f == NULL) {
1553 error("Can't open PAP password file %s: %m", filename);
1554
1555 } else {
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);
1559 } else {
1560 /*
1561 * If the secret is "@login", it means to check
1562 * the password against the login database.
1563 */
1564 int login_secret = strcmp(secret, "@login") == 0;
1565 ret = UPAP_AUTHACK;
1566 if (uselogin || login_secret) {
1567 /* login option or secret is @login */
1568 if (session_full(user, passwd, devnam, msg) == 0) {
1569 ret = UPAP_AUTHNAK;
1570 }
1571 } else if (session_mgmt) {
1572 if (session_check(user, NULL, devnam, NULL) == 0) {
1573 warn("Peer %q failed PAP Session verification", user);
1574 ret = UPAP_AUTHNAK;
1575 }
1576 }
1577 if (secret[0] != 0 && !login_secret) {
1578 /* password given in pap-secrets - must match */
1579 if (cryptpap || strcmp(passwd, secret) != 0) {
1580 #ifdef HAVE_CRYPT_H
1581 char *cbuf = crypt(passwd, secret);
1582 if (!cbuf || strcmp(cbuf, secret) != 0)
1583 #endif
1584 ret = UPAP_AUTHNAK;
1585 }
1586 }
1587 }
1588 fclose(f);
1589 }
1590
1591 if (ret == UPAP_AUTHNAK) {
1592 if (**msg == 0)
1593 *msg = "Login incorrect";
1594 /*
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.
1599 */
1600 if (attempts++ >= 10) {
1601 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1602 lcp_close(unit, "login failed");
1603 }
1604 if (attempts > 3)
1605 sleep((u_int) (attempts - 3) * 5);
1606 if (opts != NULL)
1607 free_wordlist(opts);
1608
1609 } else {
1610 attempts = 0; /* Reset count */
1611 if (**msg == 0)
1612 *msg = "Login ok";
1613 set_allowed_addrs(unit, addrs, opts);
1614 }
1615
1616 if (addrs != NULL)
1617 free_wordlist(addrs);
1618 BZERO(passwd, sizeof(passwd));
1619 BZERO(secret, sizeof(secret));
1620
1621 return ret;
1622 }
1623
1624 /*
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
1627 * and return 1.
1628 */
1629 static int
1630 null_login(int unit)
1631 {
1632 char *filename;
1633 FILE *f;
1634 int i, ret;
1635 struct wordlist *addrs, *opts;
1636 char secret[MAXWORDLEN];
1637
1638 /*
1639 * Check if a plugin wants to handle this.
1640 */
1641 ret = -1;
1642 if (null_auth_hook)
1643 ret = (*null_auth_hook)(&addrs, &opts);
1644
1645 /*
1646 * Open the file of pap secrets and scan for a suitable secret.
1647 */
1648 if (ret <= 0) {
1649 filename = path_upapfile;
1650 addrs = NULL;
1651 f = fopen(filename, "r");
1652 if (f == NULL)
1653 return 0;
1654 check_access(f, filename);
1655
1656 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1657 ret = i >= 0 && secret[0] == 0;
1658 BZERO(secret, sizeof(secret));
1659 fclose(f);
1660 }
1661
1662 if (ret)
1663 set_allowed_addrs(unit, addrs, opts);
1664 else if (opts != 0)
1665 free_wordlist(opts);
1666 if (addrs != 0)
1667 free_wordlist(addrs);
1668
1669 return ret;
1670 }
1671
1672
1673 /*
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
1676 * could be found.
1677 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1678 */
1679 static int
1680 get_pap_passwd(char *passwd)
1681 {
1682 char *filename;
1683 FILE *f;
1684 int ret;
1685 char secret[MAXWORDLEN];
1686
1687 /*
1688 * Check whether a plugin wants to supply this.
1689 */
1690 if (pap_passwd_hook) {
1691 ret = (*pap_passwd_hook)(user, passwd);
1692 if (ret >= 0)
1693 return ret;
1694 }
1695
1696 filename = path_upapfile;
1697 f = fopen(filename, "r");
1698 if (f == NULL)
1699 return 0;
1700 check_access(f, filename);
1701 ret = scan_authfile(f, user,
1702 (remote_name[0]? remote_name: NULL),
1703 secret, NULL, NULL, filename, 0);
1704 fclose(f);
1705 if (ret < 0)
1706 return 0;
1707 if (passwd != NULL)
1708 strlcpy(passwd, secret, MAXSECRETLEN);
1709 BZERO(secret, sizeof(secret));
1710 return 1;
1711 }
1712
1713
1714 /*
1715 * have_pap_secret - check whether we have a PAP file with any
1716 * secrets that we could possibly use for authenticating the peer.
1717 */
1718 static int
1719 have_pap_secret(int *lacks_ipp)
1720 {
1721 FILE *f;
1722 int ret;
1723 char *filename;
1724 struct wordlist *addrs;
1725
1726 /* let the plugin decide, if there is one */
1727 if (pap_check_hook) {
1728 ret = (*pap_check_hook)();
1729 if (ret >= 0)
1730 return ret;
1731 }
1732
1733 filename = path_upapfile;
1734 f = fopen(filename, "r");
1735 if (f == NULL)
1736 return 0;
1737
1738 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1739 NULL, &addrs, NULL, filename, 0);
1740 fclose(f);
1741 if (ret >= 0 && !some_ip_ok(addrs)) {
1742 if (lacks_ipp != 0)
1743 *lacks_ipp = 1;
1744 ret = -1;
1745 }
1746 if (addrs != 0)
1747 free_wordlist(addrs);
1748
1749 return ret >= 0;
1750 }
1751
1752
1753 /*
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.
1758 */
1759 static int
1760 have_chap_secret(char *client, char *server,
1761 int need_ip, int *lacks_ipp)
1762 {
1763 FILE *f;
1764 int ret;
1765 char *filename;
1766 struct wordlist *addrs;
1767
1768 if (chap_check_hook) {
1769 ret = (*chap_check_hook)();
1770 if (ret >= 0) {
1771 return ret;
1772 }
1773 }
1774
1775 filename = path_chapfile;
1776 f = fopen(filename, "r");
1777 if (f == NULL)
1778 return 0;
1779
1780 if (client != NULL && client[0] == 0)
1781 client = NULL;
1782 else if (server != NULL && server[0] == 0)
1783 server = NULL;
1784
1785 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1786 fclose(f);
1787 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1788 if (lacks_ipp != 0)
1789 *lacks_ipp = 1;
1790 ret = -1;
1791 }
1792 if (addrs != 0)
1793 free_wordlist(addrs);
1794
1795 return ret >= 0;
1796 }
1797
1798
1799 /*
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.
1804 */
1805 static int
1806 have_srp_secret(char *client, char *server, int need_ip, int *lacks_ipp)
1807 {
1808 FILE *f;
1809 int ret;
1810 char *filename;
1811 struct wordlist *addrs;
1812
1813 filename = PPP_PATH_SRPFILE;
1814 f = fopen(filename, "r");
1815 if (f == NULL)
1816 return 0;
1817
1818 if (client != NULL && client[0] == 0)
1819 client = NULL;
1820 else if (server != NULL && server[0] == 0)
1821 server = NULL;
1822
1823 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1824 fclose(f);
1825 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1826 if (lacks_ipp != 0)
1827 *lacks_ipp = 1;
1828 ret = -1;
1829 }
1830 if (addrs != 0)
1831 free_wordlist(addrs);
1832
1833 return ret >= 0;
1834 }
1835
1836
1837 /*
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).
1841 */
1842 int
1843 get_secret(int unit, char *client, char *server,
1844 char *secret, int *secret_len, int am_server)
1845 {
1846 FILE *f;
1847 int ret, len;
1848 char *filename;
1849 struct wordlist *addrs, *opts;
1850 char secbuf[MAXWORDLEN];
1851
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",
1857 client, server);
1858 return 0;
1859 }
1860 } else {
1861 filename = path_chapfile;
1862 addrs = NULL;
1863 secbuf[0] = 0;
1864
1865 f = fopen(filename, "r");
1866 if (f == NULL) {
1867 error("Can't open chap secret file %s: %m", filename);
1868 return 0;
1869 }
1870 check_access(f, filename);
1871
1872 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1873 fclose(f);
1874 if (ret < 0)
1875 return 0;
1876
1877 if (am_server)
1878 set_allowed_addrs(unit, addrs, opts);
1879 else if (opts != 0)
1880 free_wordlist(opts);
1881 if (addrs != 0)
1882 free_wordlist(addrs);
1883 }
1884
1885 len = strlen(secbuf);
1886 if (len > MAXSECRETLEN) {
1887 error("Secret for %s on %s is too long", client, server);
1888 len = MAXSECRETLEN;
1889 }
1890 BCOPY(secbuf, secret, len);
1891 BZERO(secbuf, sizeof(secbuf));
1892 *secret_len = len;
1893
1894 return 1;
1895 }
1896
1897
1898 /*
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).
1902 */
1903 int
1904 get_srp_secret(int unit, char *client, char *server,
1905 char *secret, int am_server)
1906 {
1907 FILE *fp;
1908 int ret;
1909 char *filename;
1910 struct wordlist *addrs, *opts;
1911
1912 if (!am_server && passwd[0] != '\0') {
1913 strlcpy(secret, passwd, MAXWORDLEN);
1914 } else {
1915 filename = PPP_PATH_SRPFILE;
1916 addrs = NULL;
1917
1918 fp = fopen(filename, "r");
1919 if (fp == NULL) {
1920 error("Can't open srp secret file %s: %m", filename);
1921 return 0;
1922 }
1923 check_access(fp, filename);
1924
1925 secret[0] = '\0';
1926 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1927 filename, am_server);
1928 fclose(fp);
1929 if (ret < 0)
1930 return 0;
1931
1932 if (am_server)
1933 set_allowed_addrs(unit, addrs, opts);
1934 else if (opts != NULL)
1935 free_wordlist(opts);
1936 if (addrs != NULL)
1937 free_wordlist(addrs);
1938 }
1939
1940 return 1;
1941 }
1942
1943 /*
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.
1947 */
1948 static void
1949 set_allowed_addrs(int unit, struct wordlist *addrs,
1950 struct wordlist *opts)
1951 {
1952 int n;
1953 struct wordlist *ap, **plink;
1954 struct permitted_ip *ip;
1955 char *ptr_word, *ptr_mask;
1956 struct hostent *hp;
1957 struct netent *np;
1958 u_int32_t a, mask, ah, offset;
1959 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1960 u_int32_t suggested_ip = 0;
1961
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;
1968
1969 /*
1970 * Count the number of IP addresses given.
1971 */
1972 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1973 if (n == 0)
1974 return;
1975 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1976 if (ip == 0)
1977 return;
1978
1979 /* temporarily append the noauth_addrs list to addrs */
1980 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1981 ;
1982 *plink = noauth_addrs;
1983
1984 n = 0;
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)
1989 break;
1990 if (strcmp(ptr_word, "*") == 0) {
1991 ip[n].permit = 1;
1992 ip[n].base = ip[n].mask = 0;
1993 ++n;
1994 break;
1995 }
1996
1997 ip[n].permit = 1;
1998 if (*ptr_word == '!') {
1999 ip[n].permit = 0;
2000 ++ptr_word;
2001 }
2002
2003 mask = ~ (u_int32_t) 0;
2004 offset = 0;
2005 ptr_mask = strchr (ptr_word, '/');
2006 if (ptr_mask != NULL) {
2007 int bit_count;
2008 char *endp;
2009
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",
2013 ptr_mask+1);
2014 continue;
2015 }
2016 bit_count = 32 - bit_count; /* # bits in host part */
2017 if (*endp == '+') {
2018 offset = ifunit + 1;
2019 ++endp;
2020 }
2021 if (*endp != 0) {
2022 warn("invalid address length syntax: %v", ptr_mask+1);
2023 continue;
2024 }
2025 *ptr_mask = '\0';
2026 mask <<= bit_count;
2027 }
2028
2029 hp = gethostbyname(ptr_word);
2030 if (hp != NULL && hp->h_addrtype == AF_INET) {
2031 a = *(u_int32_t *)hp->h_addr;
2032 } else {
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 */
2038 ah = ntohl(a);
2039 if (IN_CLASSA(ah))
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;
2045 }
2046 } else {
2047 a = inet_addr (ptr_word);
2048 }
2049 }
2050
2051 if (ptr_mask != NULL)
2052 *ptr_mask = '/';
2053
2054 if (a == (u_int32_t)-1L) {
2055 warn("unknown host %s in auth. address list", ap->word);
2056 continue;
2057 }
2058 if (offset != 0) {
2059 if (offset >= ~mask) {
2060 warn("interface unit %d too large for subnet %v",
2061 ifunit, ptr_word);
2062 continue;
2063 }
2064 a = htonl((ntohl(a) & mask) + offset);
2065 mask = ~(u_int32_t)0;
2066 }
2067 ip[n].mask = htonl(mask);
2068 ip[n].base = a & ip[n].mask;
2069 ++n;
2070 if (~mask == 0 && suggested_ip == 0)
2071 suggested_ip = a;
2072 }
2073 *plink = NULL;
2074
2075 ip[n].permit = 0; /* make the last entry forbid all addresses */
2076 ip[n].base = 0; /* to terminate the list */
2077 ip[n].mask = 0;
2078
2079 addresses[unit] = ip;
2080
2081 /*
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.
2085 */
2086 if (suggested_ip != 0
2087 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
2088 wo->hisaddr = suggested_ip;
2089 /*
2090 * Do we insist on this address? No, if there are other
2091 * addresses authorized than the suggested one.
2092 */
2093 if (n > 1)
2094 wo->accept_remote = 1;
2095 }
2096 }
2097
2098 /*
2099 * auth_ip_addr - check whether the peer is authorized to use
2100 * a given IP address. Returns 1 if authorized, 0 otherwise.
2101 */
2102 int
2103 auth_ip_addr(int unit, u_int32_t addr)
2104 {
2105 int ok;
2106
2107 /* don't allow loopback or multicast address */
2108 if (ppp_bad_ip_addr(addr))
2109 return 0;
2110
2111 if (allowed_address_hook) {
2112 ok = allowed_address_hook(addr);
2113 if (ok >= 0) return ok;
2114 }
2115
2116 if (addresses[unit] != NULL) {
2117 ok = ip_addr_check(addr, addresses[unit]);
2118 if (ok >= 0)
2119 return ok;
2120 }
2121
2122 if (auth_required)
2123 return 0; /* no addresses authorized */
2124 return allow_any_ip || privileged || !have_route_to(addr);
2125 }
2126
2127 static int
2128 ip_addr_check(u_int32_t addr, struct permitted_ip *addrs)
2129 {
2130 for (; ; ++addrs)
2131 if ((addr & addrs->mask) == addrs->base)
2132 return addrs->permit;
2133 }
2134
2135 /*
2136 * Check if given addr in network byte order is in the looback network, or a multicast address.
2137 */
2138 bool
2139 ppp_bad_ip_addr(u_int32_t addr)
2140 {
2141 addr = ntohl(addr);
2142 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2143 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2144 }
2145
2146 /*
2147 * some_ip_ok - check a wordlist to see if it authorizes any
2148 * IP address(es).
2149 */
2150 static int
2151 some_ip_ok(struct wordlist *addrs)
2152 {
2153 for (; addrs != 0; addrs = addrs->next) {
2154 if (addrs->word[0] == '-')
2155 break;
2156 if (addrs->word[0] != '!')
2157 return 1; /* some IP address is allowed */
2158 }
2159 return 0;
2160 }
2161
2162 /*
2163 * auth_number - check whether the remote number is allowed to connect.
2164 * Returns 1 if authorized, 0 otherwise.
2165 */
2166 int
2167 auth_number(void)
2168 {
2169 struct wordlist *wp = permitted_numbers;
2170 size_t l;
2171
2172 /* Allow all if no authorization list. */
2173 if (!wp)
2174 return 1;
2175
2176 /* Allow if we have a match in the authorization list. */
2177 while (wp) {
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))
2182 return 1;
2183 } else if (strcasecmp(wp->word, remote_number) == 0)
2184 return 1;
2185 wp = wp->next;
2186 }
2187
2188 return 0;
2189 }
2190
2191 /*
2192 * check_access - complain if a secret file has too-liberal permissions.
2193 */
2194 static void
2195 check_access(FILE *f, char *filename)
2196 {
2197 struct stat sbuf;
2198
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",
2203 filename);
2204 }
2205 }
2206
2207
2208 /*
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
2220 * match.
2221 */
2222 static int
2223 scan_authfile(FILE *f, char *client, char *server,
2224 char *secret, struct wordlist **addrs,
2225 struct wordlist **opts, char *filename,
2226 int flags)
2227 {
2228 int newline, xxx;
2229 int got_flag, best_flag;
2230 FILE *sf;
2231 struct wordlist *ap, *addr_list, *alist, **app;
2232 char word[MAXWORDLEN];
2233 char atfile[MAXWORDLEN];
2234 char lsecret[MAXWORDLEN];
2235 char *cp;
2236
2237 if (addrs != NULL)
2238 *addrs = NULL;
2239 if (opts != NULL)
2240 *opts = NULL;
2241 addr_list = NULL;
2242 if (!getword(f, word, &newline, filename))
2243 return -1; /* file is empty??? */
2244 newline = 1;
2245 best_flag = -1;
2246 for (;;) {
2247 /*
2248 * Skip until we find a word at the start of a line.
2249 */
2250 while (!newline && getword(f, word, &newline, filename))
2251 ;
2252 if (!newline)
2253 break; /* got to end of file */
2254
2255 /*
2256 * Got a client - check if it's a match or a wildcard.
2257 */
2258 got_flag = 0;
2259 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2260 newline = 0;
2261 continue;
2262 }
2263 if (!ISWILD(word))
2264 got_flag = NONWILD_CLIENT;
2265
2266 /*
2267 * Now get a server and check if it matches.
2268 */
2269 if (!getword(f, word, &newline, filename))
2270 break;
2271 if (newline)
2272 continue;
2273 if (!ISWILD(word)) {
2274 if (server != NULL && strcmp(word, server) != 0)
2275 continue;
2276 got_flag |= NONWILD_SERVER;
2277 }
2278
2279 /*
2280 * Got some sort of a match - see if it's better than what
2281 * we have already.
2282 */
2283 if (got_flag <= best_flag)
2284 continue;
2285
2286 /*
2287 * Get the secret.
2288 */
2289 if (!getword(f, word, &newline, filename))
2290 break;
2291 if (newline)
2292 continue;
2293
2294 /*
2295 * SRP-SHA1 authenticator should never be reading secrets from
2296 * a file. (Authenticatee may, though.)
2297 */
2298 if (flags && ((cp = strchr(word, ':')) == NULL ||
2299 strchr(cp + 1, ':') == NULL))
2300 continue;
2301
2302 if (secret != NULL) {
2303 /*
2304 * Special syntax: @/pathname means read secret from file.
2305 */
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);
2310 continue;
2311 }
2312 check_access(sf, atfile);
2313 if (!getword(sf, word, &xxx, atfile)) {
2314 warn("no secret in indirect secret file %s", atfile);
2315 fclose(sf);
2316 continue;
2317 }
2318 fclose(sf);
2319 }
2320 strlcpy(lsecret, word, sizeof(lsecret));
2321 }
2322
2323 /*
2324 * Now read address authorization info and make a wordlist.
2325 */
2326 app = &alist;
2327 for (;;) {
2328 if (!getword(f, word, &newline, filename) || newline)
2329 break;
2330 ap = (struct wordlist *)
2331 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2332 if (ap == NULL)
2333 novm("authorized addresses");
2334 ap->word = (char *) (ap + 1);
2335 strcpy(ap->word, word);
2336 *app = ap;
2337 app = &ap->next;
2338 }
2339 *app = NULL;
2340
2341 /*
2342 * This is the best so far; remember it.
2343 */
2344 best_flag = got_flag;
2345 if (addr_list)
2346 free_wordlist(addr_list);
2347 addr_list = alist;
2348 if (secret != NULL)
2349 strlcpy(secret, lsecret, MAXWORDLEN);
2350
2351 if (!newline)
2352 break;
2353 }
2354
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)
2358 break;
2359 /* ap = start of options */
2360 if (ap != NULL) {
2361 ap = ap->next; /* first option */
2362 free(*app); /* free the "--" word */
2363 *app = NULL; /* terminate addr list */
2364 }
2365 if (opts != NULL)
2366 *opts = ap;
2367 else if (ap != NULL)
2368 free_wordlist(ap);
2369 if (addrs != NULL)
2370 *addrs = addr_list;
2371 else if (addr_list != NULL)
2372 free_wordlist(addr_list);
2373
2374 return best_flag;
2375 }
2376
2377 /*
2378 * wordlist_count - return the number of items in a wordlist
2379 */
2380 static int
2381 wordlist_count(struct wordlist *wp)
2382 {
2383 int n;
2384
2385 for (n = 0; wp != NULL; wp = wp->next)
2386 ++n;
2387 return n;
2388 }
2389
2390 /*
2391 * free_wordlist - release memory allocated for a wordlist.
2392 */
2393 static void
2394 free_wordlist(struct wordlist *wp)
2395 {
2396 struct wordlist *next;
2397
2398 while (wp != NULL) {
2399 next = wp->next;
2400 free(wp);
2401 wp = next;
2402 }
2403 }
2404
2405 /*
2406 * auth_script_done - called when the auth-up or auth-down script
2407 * has finished.
2408 */
2409 static void
2410 auth_script_done(void *arg)
2411 {
2412 auth_script_pid = 0;
2413 switch (auth_script_state) {
2414 case s_up:
2415 if (auth_state == s_down) {
2416 auth_script_state = s_down;
2417 auth_script(PPP_PATH_AUTHDOWN);
2418 }
2419 break;
2420 case s_down:
2421 if (auth_state == s_up) {
2422 auth_script_state = s_up;
2423 auth_script(PPP_PATH_AUTHUP);
2424 }
2425 break;
2426 }
2427 }
2428
2429 /*
2430 * auth_script - execute a script with arguments
2431 * interface-name peer-name real-user tty speed
2432 */
2433 static void
2434 auth_script(char *script)
2435 {
2436 char strspeed[32];
2437 struct passwd *pw;
2438 char struid[32];
2439 char *user_name;
2440 char *argv[8];
2441
2442 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2443 user_name = pw->pw_name;
2444 else {
2445 slprintf(struid, sizeof(struid), "%d", getuid());
2446 user_name = struid;
2447 }
2448 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2449
2450 argv[0] = script;
2451 argv[1] = ifname;
2452 argv[2] = peer_authname;
2453 argv[3] = user_name;
2454 argv[4] = devnam;
2455 argv[5] = strspeed;
2456 argv[6] = ipparam;
2457 argv[7] = NULL;
2458
2459 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);
2460 }
2461
2462
2463 #ifdef PPP_WITH_EAPTLS
2464 static int
2465 have_eaptls_secret_server(char *client, char *server,
2466 int need_ip, int *lacks_ipp)
2467 {
2468 FILE *f;
2469 int ret;
2470 char *filename;
2471 struct wordlist *addrs;
2472 char servcertfile[MAXWORDLEN];
2473 char clicertfile[MAXWORDLEN];
2474 char cacertfile[MAXWORDLEN];
2475 char pkfile[MAXWORDLEN];
2476
2477 filename = PPP_PATH_EAPTLSSERVFILE;
2478 f = fopen(filename, "r");
2479 if (f == NULL)
2480 return 0;
2481
2482 if (client != NULL && client[0] == 0)
2483 client = NULL;
2484 else if (server != NULL && server[0] == 0)
2485 server = NULL;
2486
2487 ret =
2488 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2489 cacertfile, pkfile, &addrs, NULL, filename,
2490 0);
2491
2492 fclose(f);
2493
2494 /*
2495 if (ret >= 0 && !eaptls_init_ssl(1, cacertfile, servcertfile,
2496 clicertfile, pkfile))
2497 ret = -1;
2498 */
2499
2500 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
2501 if (lacks_ipp != 0)
2502 *lacks_ipp = 1;
2503 ret = -1;
2504 }
2505 if (addrs != 0)
2506 free_wordlist(addrs);
2507
2508 return ret >= 0;
2509 }
2510
2511
2512 static int
2513 have_eaptls_secret_client(char *client, char *server)
2514 {
2515 FILE *f;
2516 int ret;
2517 char *filename;
2518 struct wordlist *addrs = NULL;
2519 char servcertfile[MAXWORDLEN];
2520 char clicertfile[MAXWORDLEN];
2521 char cacertfile[MAXWORDLEN];
2522 char pkfile[MAXWORDLEN];
2523
2524 if (client != NULL && client[0] == 0)
2525 client = NULL;
2526 else if (server != NULL && server[0] == 0)
2527 server = NULL;
2528
2529 if ((cacert_file || ca_path) && cert_file && privkey_file)
2530 return 1;
2531 if (pkcs12_file)
2532 return 1;
2533
2534 filename = PPP_PATH_EAPTLSCLIFILE;
2535 f = fopen(filename, "r");
2536 if (f == NULL)
2537 return 0;
2538
2539 ret =
2540 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile,
2541 cacertfile, pkfile, &addrs, NULL, filename,
2542 0);
2543 fclose(f);
2544
2545 /*
2546 if (ret >= 0 && !eaptls_init_ssl(0, cacertfile, clicertfile,
2547 servcertfile, pkfile))
2548 ret = -1;
2549 */
2550
2551 if (addrs != 0)
2552 free_wordlist(addrs);
2553
2554 return ret >= 0;
2555 }
2556
2557
2558 static int
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)
2564 {
2565 int newline;
2566 int got_flag, best_flag;
2567 struct wordlist *ap, *addr_list, *alist, **app;
2568 char word[MAXWORDLEN];
2569
2570 if (addrs != NULL)
2571 *addrs = NULL;
2572 if (opts != NULL)
2573 *opts = NULL;
2574 addr_list = NULL;
2575 if (!getword(f, word, &newline, filename))
2576 return -1; /* file is empty??? */
2577 newline = 1;
2578 best_flag = -1;
2579 for (;;) {
2580 /*
2581 * Skip until we find a word at the start of a line.
2582 */
2583 while (!newline && getword(f, word, &newline, filename));
2584 if (!newline)
2585 break; /* got to end of file */
2586
2587 /*
2588 * Got a client - check if it's a match or a wildcard.
2589 */
2590 got_flag = 0;
2591 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2592 newline = 0;
2593 continue;
2594 }
2595 if (!ISWILD(word))
2596 got_flag = NONWILD_CLIENT;
2597
2598 /*
2599 * Now get a server and check if it matches.
2600 */
2601 if (!getword(f, word, &newline, filename))
2602 break;
2603 if (newline)
2604 continue;
2605 if (!ISWILD(word)) {
2606 if (server != NULL && strcmp(word, server) != 0)
2607 continue;
2608 got_flag |= NONWILD_SERVER;
2609 }
2610
2611 /*
2612 * Got some sort of a match - see if it's better than what
2613 * we have already.
2614 */
2615 if (got_flag <= best_flag)
2616 continue;
2617
2618 /*
2619 * Get the cli_cert
2620 */
2621 if (!getword(f, word, &newline, filename))
2622 break;
2623 if (newline)
2624 continue;
2625 if (strcmp(word, "-") != 0) {
2626 strlcpy(cli_cert, word, MAXWORDLEN);
2627 } else
2628 cli_cert[0] = 0;
2629
2630 /*
2631 * Get serv_cert
2632 */
2633 if (!getword(f, word, &newline, filename))
2634 break;
2635 if (newline)
2636 continue;
2637 if (strcmp(word, "-") != 0) {
2638 strlcpy(serv_cert, word, MAXWORDLEN);
2639 } else
2640 serv_cert[0] = 0;
2641
2642 /*
2643 * Get ca_cert
2644 */
2645 if (!getword(f, word, &newline, filename))
2646 break;
2647 if (newline)
2648 continue;
2649 strlcpy(ca_cert, word, MAXWORDLEN);
2650
2651 /*
2652 * Get pk
2653 */
2654 if (!getword(f, word, &newline, filename))
2655 break;
2656 if (newline)
2657 continue;
2658 strlcpy(pk, word, MAXWORDLEN);
2659
2660
2661 /*
2662 * Now read address authorization info and make a wordlist.
2663 */
2664 app = &alist;
2665 for (;;) {
2666 if (!getword(f, word, &newline, filename) || newline)
2667 break;
2668 ap = (struct wordlist *)
2669 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2670 if (ap == NULL)
2671 novm("authorized addresses");
2672 ap->word = (char *) (ap + 1);
2673 strcpy(ap->word, word);
2674 *app = ap;
2675 app = &ap->next;
2676 }
2677 *app = NULL;
2678 /*
2679 * This is the best so far; remember it.
2680 */
2681 best_flag = got_flag;
2682 if (addr_list)
2683 free_wordlist(addr_list);
2684 addr_list = alist;
2685
2686 if (!newline)
2687 break;
2688 }
2689
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)
2693 break;
2694 /* ap = start of options */
2695 if (ap != NULL) {
2696 ap = ap->next; /* first option */
2697 free(*app); /* free the "--" word */
2698 *app = NULL; /* terminate addr list */
2699 }
2700 if (opts != NULL)
2701 *opts = ap;
2702 else if (ap != NULL)
2703 free_wordlist(ap);
2704 if (addrs != NULL)
2705 *addrs = addr_list;
2706 else if (addr_list != NULL)
2707 free_wordlist(addr_list);
2708
2709 return best_flag;
2710 }
2711
2712
2713 int
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)
2717 {
2718 FILE *fp;
2719 int ret;
2720 char *filename = NULL;
2721 struct wordlist *addrs = NULL;
2722 struct wordlist *opts = NULL;
2723
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);
2731
2732 /* the ca+cert+privkey can also be specified as options */
2733 if (!am_server && (cacert_file || ca_path) && cert_file && privkey_file )
2734 {
2735 strlcpy( clicertfile, cert_file, MAXWORDLEN );
2736 if (cacert_file)
2737 strlcpy( cacertfile, cacert_file, MAXWORDLEN );
2738 if (ca_path)
2739 strlcpy( capath, ca_path, MAXWORDLEN );
2740 strlcpy( pkfile, privkey_file, MAXWORDLEN );
2741 }
2742 else if (!am_server && pkcs12_file)
2743 {
2744 strlcpy( pkcs12, pkcs12_file, MAXWORDLEN );
2745 if (cacert_file)
2746 strlcpy( cacertfile, cacert_file, MAXWORDLEN );
2747 if (ca_path)
2748 strlcpy( capath, ca_path, MAXWORDLEN );
2749 }
2750 else
2751 {
2752 filename = (am_server ? PPP_PATH_EAPTLSSERVFILE : PPP_PATH_EAPTLSCLIFILE);
2753 addrs = NULL;
2754
2755 fp = fopen(filename, "r");
2756 if (fp == NULL)
2757 {
2758 error("Can't open eap-tls secret file %s: %m", filename);
2759 return 0;
2760 }
2761
2762 check_access(fp, filename);
2763
2764 ret = scan_authfile_eaptls(fp, client, server, clicertfile, servcertfile,
2765 cacertfile, pkfile, &addrs, &opts, filename, 0);
2766
2767 fclose(fp);
2768
2769 if (ret < 0) return 0;
2770 }
2771
2772 if (eaptls_passwd_hook)
2773 {
2774 dbglog( "Calling eaptls password hook" );
2775 if ( (*eaptls_passwd_hook)(pkfile, passwd) < 0)
2776 {
2777 error("Unable to obtain EAP-TLS password for %s (%s) from plugin",
2778 client, pkfile);
2779 return 0;
2780 }
2781 }
2782 if (am_server)
2783 set_allowed_addrs(unit, addrs, opts);
2784 else if (opts != NULL)
2785 free_wordlist(opts);
2786 if (addrs != NULL)
2787 free_wordlist(addrs);
2788
2789 return 1;
2790 }
2791 #endif