1 /*
2 * auth.c - PPP authentication and phase control.
3 *
4 * Copyright (c) 1993-2002 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. The name(s) of the authors of this software must not be used to
14 * endorse or promote products derived from this software without
15 * prior written permission.
16 *
17 * 3. Redistributions of any form whatsoever must retain the following
18 * acknowledgment:
19 * "This product includes software developed by Paul Mackerras
20 * <paulus@samba.org>".
21 *
22 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 *
30 * Derived from main.c, which is:
31 *
32 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 *
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 *
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
44 * distribution.
45 *
46 * 3. The name "Carnegie Mellon University" must not be used to
47 * endorse or promote products derived from this software without
48 * prior written permission. For permission or any legal
49 * details, please contact
50 * Office of Technology Transfer
51 * Carnegie Mellon University
52 * 5000 Forbes Avenue
53 * Pittsburgh, PA 15213-3890
54 * (412) 268-4387, fax: (412) 268-7395
55 * tech-transfer@andrew.cmu.edu
56 *
57 * 4. Redistributions of any form whatsoever must retain the following
58 * acknowledgment:
59 * "This product includes software developed by Computing Services
60 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
61 *
62 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
63 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
65 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
67 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
68 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
69 */
70
71 #define RCSID "$Id: auth.c,v 1.117 2008/07/01 12:27:56 paulus Exp $"
72
73 #include <stdio.h>
74 #include <stddef.h>
75 #include <stdlib.h>
76 #include <unistd.h>
77 #include <errno.h>
78 #include <pwd.h>
79 #include <grp.h>
80 #include <string.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 HAS_SHADOW
96 #include <shadow.h>
97 #ifndef PW_PPP
98 #define PW_PPP PW_LOGIN
99 #endif
100 #endif
101 #include <time.h>
102
103 #include "pppd.h"
104 #include "fsm.h"
105 #include "lcp.h"
106 #include "ccp.h"
107 #include "ecp.h"
108 #include "ipcp.h"
109 #include "upap.h"
110 #include "chap-new.h"
111 #include "eap.h"
112 #ifdef CBCP_SUPPORT
113 #include "cbcp.h"
114 #endif
115 #include "pathnames.h"
116 #include "session.h"
117
118 static const char rcsid[] = RCSID;
119
120 /* Bits in scan_authfile return value */
121 #define NONWILD_SERVER 1
122 #define NONWILD_CLIENT 2
123
124 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
125
126 /* The name by which the peer authenticated itself to us. */
127 char peer_authname[MAXNAMELEN];
128
129 /* Records which authentication operations haven't completed yet. */
130 static int auth_pending[NUM_PPP];
131
132 /* Records which authentication operations have been completed. */
133 int auth_done[NUM_PPP];
134
135 /* List of addresses which the peer may use. */
136 static struct permitted_ip *addresses[NUM_PPP];
137
138 /* Wordlist giving addresses which the peer may use
139 without authenticating itself. */
140 static struct wordlist *noauth_addrs;
141
142 /* Remote telephone number, if available */
143 char remote_number[MAXNAMELEN];
144
145 /* Wordlist giving remote telephone numbers which may connect. */
146 static struct wordlist *permitted_numbers;
147
148 /* Extra options to apply, from the secrets file entry for the peer. */
149 static struct wordlist *extra_options;
150
151 /* Number of network protocols which we have opened. */
152 static int num_np_open;
153
154 /* Number of network protocols which have come up. */
155 static int num_np_up;
156
157 /* Set if we got the contents of passwd[] from the pap-secrets file. */
158 static int passwd_from_file;
159
160 /* Set if we require authentication only because we have a default route. */
161 static bool default_auth;
162
163 /* Hook to enable a plugin to control the idle time limit */
164 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
165
166 /* Hook for a plugin to say whether we can possibly authenticate any peer */
167 int (*pap_check_hook) __P((void)) = NULL;
168
169 /* Hook for a plugin to check the PAP user and password */
170 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
171 struct wordlist **paddrs,
172 struct wordlist **popts)) = NULL;
173
174 /* Hook for a plugin to know about the PAP user logout */
175 void (*pap_logout_hook) __P((void)) = NULL;
176
177 /* Hook for a plugin to get the PAP password for authenticating us */
178 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
179
180 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
181 int (*chap_check_hook) __P((void)) = NULL;
182
183 /* Hook for a plugin to get the CHAP password for authenticating us */
184 int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
185
186 /* Hook for a plugin to say whether it is OK if the peer
187 refuses to authenticate. */
188 int (*null_auth_hook) __P((struct wordlist **paddrs,
189 struct wordlist **popts)) = NULL;
190
191 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
192
193 #ifdef HAVE_MULTILINK
194 /* Hook for plugin to hear when an interface joins a multilink bundle */
195 void (*multilink_join_hook) __P((void)) = NULL;
196 #endif
197
198 /* A notifier for when the peer has authenticated itself,
199 and we are proceeding to the network phase. */
200 struct notifier *auth_up_notifier = NULL;
201
202 /* A notifier for when the link goes down. */
203 struct notifier *link_down_notifier = NULL;
204
205 /*
206 * This is used to ensure that we don't start an auth-up/down
207 * script while one is already running.
208 */
209 enum script_state {
210 s_down,
211 s_up
212 };
213
214 static enum script_state auth_state = s_down;
215 static enum script_state auth_script_state = s_down;
216 static pid_t auth_script_pid = 0;
217
218 /*
219 * Option variables.
220 */
221 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
222 bool session_mgmt = 0; /* Do session management (login records) */
223 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
224 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
225 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
226 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
227 #ifdef CHAPMS
228 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
229 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
230 #else
231 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
232 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
233 #endif
234 bool usehostname = 0; /* Use hostname for our_name */
235 bool auth_required = 0; /* Always require authentication from peer */
236 bool allow_any_ip = 0; /* Allow peer to use any IP address */
237 bool explicit_remote = 0; /* User specified explicit remote name */
238 bool explicit_user = 0; /* Set if "user" option supplied */
239 bool explicit_passwd = 0; /* Set if "password" option supplied */
240 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
241
242 static char *uafname; /* name of most recent +ua file */
243
244 extern char *crypt __P((const char *, const char *));
245
246 /* Prototypes for procedures local to this file. */
247
248 static void network_phase __P((int));
249 static void check_idle __P((void *));
250 static void connect_time_expired __P((void *));
251 static int null_login __P((int));
252 static int get_pap_passwd __P((char *));
253 static int have_pap_secret __P((int *));
254 static int have_chap_secret __P((char *, char *, int, int *));
255 static int have_srp_secret __P((char *client, char *server, int need_ip,
256 int *lacks_ipp));
257 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
258 static int scan_authfile __P((FILE *, char *, char *, char *,
259 struct wordlist **, struct wordlist **,
260 char *, int));
261 static void free_wordlist __P((struct wordlist *));
262 static void auth_script __P((char *));
263 static void auth_script_done __P((void *));
264 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
265 static int some_ip_ok __P((struct wordlist *));
266 static int setupapfile __P((char **));
267 static int privgroup __P((char **));
268 static int set_noauth_addr __P((char **));
269 static int set_permitted_number __P((char **));
270 static void check_access __P((FILE *, char *));
271 static int wordlist_count __P((struct wordlist *));
272
273 #ifdef MAXOCTETS
274 static void check_maxoctets __P((void *));
275 #endif
276
277 /*
278 * Authentication-related options.
279 */
280 option_t auth_options[] = {
281 { "auth", o_bool, &auth_required,
282 "Require authentication from peer", OPT_PRIO | 1 },
283 { "noauth", o_bool, &auth_required,
284 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
285 &allow_any_ip },
286 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
287 "Require PAP authentication from peer",
288 OPT_PRIOSUB | 1, &auth_required },
289 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
290 "Require PAP authentication from peer",
291 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
292 { "require-chap", o_bool, &auth_required,
293 "Require CHAP authentication from peer",
294 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
295 &lcp_wantoptions[0].chap_mdtype },
296 { "+chap", o_bool, &auth_required,
297 "Require CHAP authentication from peer",
298 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
299 &lcp_wantoptions[0].chap_mdtype },
300 #ifdef CHAPMS
301 { "require-mschap", o_bool, &auth_required,
302 "Require MS-CHAP authentication from peer",
303 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
304 &lcp_wantoptions[0].chap_mdtype },
305 { "+mschap", o_bool, &auth_required,
306 "Require MS-CHAP authentication from peer",
307 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
308 &lcp_wantoptions[0].chap_mdtype },
309 { "require-mschap-v2", o_bool, &auth_required,
310 "Require MS-CHAPv2 authentication from peer",
311 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
312 &lcp_wantoptions[0].chap_mdtype },
313 { "+mschap-v2", o_bool, &auth_required,
314 "Require MS-CHAPv2 authentication from peer",
315 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
316 &lcp_wantoptions[0].chap_mdtype },
317 #endif
318
319 { "refuse-pap", o_bool, &refuse_pap,
320 "Don't agree to auth to peer with PAP", 1 },
321 { "-pap", o_bool, &refuse_pap,
322 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
323 { "refuse-chap", o_bool, &refuse_chap,
324 "Don't agree to auth to peer with CHAP",
325 OPT_A2CLRB | MDTYPE_MD5,
326 &lcp_allowoptions[0].chap_mdtype },
327 { "-chap", o_bool, &refuse_chap,
328 "Don't allow CHAP authentication with peer",
329 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
330 &lcp_allowoptions[0].chap_mdtype },
331 #ifdef CHAPMS
332 { "refuse-mschap", o_bool, &refuse_mschap,
333 "Don't agree to auth to peer with MS-CHAP",
334 OPT_A2CLRB | MDTYPE_MICROSOFT,
335 &lcp_allowoptions[0].chap_mdtype },
336 { "-mschap", o_bool, &refuse_mschap,
337 "Don't allow MS-CHAP authentication with peer",
338 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
339 &lcp_allowoptions[0].chap_mdtype },
340 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
341 "Don't agree to auth to peer with MS-CHAPv2",
342 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
343 &lcp_allowoptions[0].chap_mdtype },
344 { "-mschap-v2", o_bool, &refuse_mschap_v2,
345 "Don't allow MS-CHAPv2 authentication with peer",
346 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
347 &lcp_allowoptions[0].chap_mdtype },
348 #endif
349
350 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
351 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
352 &auth_required },
353 { "refuse-eap", o_bool, &refuse_eap,
354 "Don't agree to authenticate to peer with EAP", 1 },
355
356 { "name", o_string, our_name,
357 "Set local name for authentication",
358 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
359
360 { "+ua", o_special, (void *)setupapfile,
361 "Get PAP user and password from file",
362 OPT_PRIO | OPT_A2STRVAL, &uafname },
363
364 { "user", o_string, user,
365 "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
366 &explicit_user, MAXNAMELEN },
367
368 { "password", o_string, passwd,
369 "Password for authenticating us to the peer",
370 OPT_PRIO | OPT_STATIC | OPT_HIDE,
371 &explicit_passwd, MAXSECRETLEN },
372
373 { "usehostname", o_bool, &usehostname,
374 "Must use hostname for authentication", 1 },
375
376 { "remotename", o_string, remote_name,
377 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
378 &explicit_remote, MAXNAMELEN },
379
380 { "login", o_bool, &uselogin,
381 "Use system password database for PAP", OPT_A2COPY | 1 ,
382 &session_mgmt },
383 { "enable-session", o_bool, &session_mgmt,
384 "Enable session accounting for remote peers", OPT_PRIV | 1 },
385
386 { "papcrypt", o_bool, &cryptpap,
387 "PAP passwords are encrypted", 1 },
388
389 { "privgroup", o_special, (void *)privgroup,
390 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
391
392 { "allow-ip", o_special, (void *)set_noauth_addr,
393 "Set IP address(es) which can be used without authentication",
394 OPT_PRIV | OPT_A2LIST },
395
396 { "remotenumber", o_string, remote_number,
397 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
398 NULL, MAXNAMELEN },
399
400 { "allow-number", o_special, (void *)set_permitted_number,
401 "Set telephone number(s) which are allowed to connect",
402 OPT_PRIV | OPT_A2LIST },
403
404 { NULL }
405 };
406
407 /*
408 * setupapfile - specifies UPAP info for authenticating with peer.
409 */
410 static int
setupapfile(argv)411 setupapfile(argv)
412 char **argv;
413 {
414 FILE *ufile;
415 int l;
416 uid_t euid;
417 char u[MAXNAMELEN], p[MAXSECRETLEN];
418 char *fname;
419
420 lcp_allowoptions[0].neg_upap = 1;
421
422 /* open user info file */
423 fname = strdup(*argv);
424 if (fname == NULL)
425 novm("+ua file name");
426 euid = geteuid();
427 if (seteuid(getuid()) == -1) {
428 option_error("unable to reset uid before opening %s: %m", fname);
429 return 0;
430 }
431 ufile = fopen(fname, "r");
432 if (seteuid(euid) == -1)
433 fatal("unable to regain privileges: %m");
434 if (ufile == NULL) {
435 option_error("unable to open user login data file %s", fname);
436 return 0;
437 }
438 check_access(ufile, fname);
439 uafname = fname;
440
441 /* get username */
442 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
443 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
444 fclose(ufile);
445 option_error("unable to read user login data file %s", fname);
446 return 0;
447 }
448 fclose(ufile);
449
450 /* get rid of newlines */
451 l = strlen(u);
452 if (l > 0 && u[l-1] == '\n')
453 u[l-1] = 0;
454 l = strlen(p);
455 if (l > 0 && p[l-1] == '\n')
456 p[l-1] = 0;
457
458 if (override_value("user", option_priority, fname)) {
459 strlcpy(user, u, sizeof(user));
460 explicit_user = 1;
461 }
462 if (override_value("passwd", option_priority, fname)) {
463 strlcpy(passwd, p, sizeof(passwd));
464 explicit_passwd = 1;
465 }
466
467 return (1);
468 }
469
470
471 /*
472 * privgroup - allow members of the group to have privileged access.
473 */
474 static int
privgroup(argv)475 privgroup(argv)
476 char **argv;
477 {
478 struct group *g;
479 int i;
480
481 g = getgrnam(*argv);
482 if (g == 0) {
483 option_error("group %s is unknown", *argv);
484 return 0;
485 }
486 for (i = 0; i < ngroups; ++i) {
487 if (groups[i] == g->gr_gid) {
488 privileged = 1;
489 break;
490 }
491 }
492 return 1;
493 }
494
495
496 /*
497 * set_noauth_addr - set address(es) that can be used without authentication.
498 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
499 */
500 static int
set_noauth_addr(argv)501 set_noauth_addr(argv)
502 char **argv;
503 {
504 char *addr = *argv;
505 int l = strlen(addr) + 1;
506 struct wordlist *wp;
507
508 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
509 if (wp == NULL)
510 novm("allow-ip argument");
511 wp->word = (char *) (wp + 1);
512 wp->next = noauth_addrs;
513 BCOPY(addr, wp->word, l);
514 noauth_addrs = wp;
515 return 1;
516 }
517
518
519 /*
520 * set_permitted_number - set remote telephone number(s) that may connect.
521 */
522 static int
set_permitted_number(argv)523 set_permitted_number(argv)
524 char **argv;
525 {
526 char *number = *argv;
527 int l = strlen(number) + 1;
528 struct wordlist *wp;
529
530 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
531 if (wp == NULL)
532 novm("allow-number argument");
533 wp->word = (char *) (wp + 1);
534 wp->next = permitted_numbers;
535 BCOPY(number, wp->word, l);
536 permitted_numbers = wp;
537 return 1;
538 }
539
540
541 /*
542 * An Open on LCP has requested a change from Dead to Establish phase.
543 */
544 void
link_required(unit)545 link_required(unit)
546 int unit;
547 {
548 }
549
550 /*
551 * Bring the link up to the point of being able to do ppp.
552 */
start_link(unit)553 void start_link(unit)
554 int unit;
555 {
556 status = EXIT_CONNECT_FAILED;
557 new_phase(PHASE_SERIALCONN);
558
559 hungup = 0;
560 devfd = the_channel->connect();
561 if (devfd < 0)
562 goto fail;
563
564 /* set up the serial device as a ppp interface */
565 /*
566 * N.B. we used to do tdb_writelock/tdb_writeunlock around this
567 * (from establish_ppp to set_ifunit). However, we won't be
568 * doing the set_ifunit in multilink mode, which is the only time
569 * we need the atomicity that the tdb_writelock/tdb_writeunlock
570 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
571 */
572 fd_ppp = the_channel->establish_ppp(devfd);
573 if (fd_ppp < 0) {
574 status = EXIT_FATAL_ERROR;
575 goto disconnect;
576 }
577
578 if (!demand && ifunit >= 0)
579 set_ifunit(1);
580
581 /*
582 * Start opening the connection and wait for
583 * incoming events (reply, timeout, etc.).
584 */
585 if (ifunit >= 0)
586 notice("Connect: %s <--> %s", ifname, ppp_devnam);
587 else
588 notice("Starting negotiation on %s", ppp_devnam);
589 add_fd(fd_ppp);
590
591 status = EXIT_NEGOTIATION_FAILED;
592 new_phase(PHASE_ESTABLISH);
593
594 lcp_lowerup(0);
595 return;
596
597 disconnect:
598 new_phase(PHASE_DISCONNECT);
599 if (the_channel->disconnect)
600 the_channel->disconnect();
601
602 fail:
603 new_phase(PHASE_DEAD);
604 if (the_channel->cleanup)
605 (*the_channel->cleanup)();
606 }
607
608 /*
609 * LCP has terminated the link; go to the Dead phase and take the
610 * physical layer down.
611 */
612 void
link_terminated(unit)613 link_terminated(unit)
614 int unit;
615 {
616 if (phase == PHASE_DEAD || phase == PHASE_MASTER)
617 return;
618 new_phase(PHASE_DISCONNECT);
619
620 if (pap_logout_hook) {
621 pap_logout_hook();
622 }
623 session_end(devnam);
624
625 if (!doing_multilink) {
626 notice("Connection terminated.");
627 print_link_stats();
628 } else
629 notice("Link terminated.");
630
631 /*
632 * Delete pid files before disestablishing ppp. Otherwise it
633 * can happen that another pppd gets the same unit and then
634 * we delete its pid file.
635 */
636 if (!doing_multilink && !demand)
637 remove_pidfiles();
638
639 /*
640 * If we may want to bring the link up again, transfer
641 * the ppp unit back to the loopback. Set the
642 * real serial device back to its normal mode of operation.
643 */
644 if (fd_ppp >= 0) {
645 remove_fd(fd_ppp);
646 clean_check();
647 the_channel->disestablish_ppp(devfd);
648 if (doing_multilink)
649 mp_exit_bundle();
650 fd_ppp = -1;
651 }
652 if (!hungup)
653 lcp_lowerdown(0);
654 if (!doing_multilink && !demand)
655 script_unsetenv("IFNAME");
656
657 /*
658 * Run disconnector script, if requested.
659 * XXX we may not be able to do this if the line has hung up!
660 */
661 if (devfd >= 0 && the_channel->disconnect) {
662 the_channel->disconnect();
663 devfd = -1;
664 }
665 if (the_channel->cleanup)
666 (*the_channel->cleanup)();
667
668 if (doing_multilink && multilink_master) {
669 if (!bundle_terminating) {
670 new_phase(PHASE_MASTER);
671 if (master_detach && !detached)
672 detach();
673 } else
674 mp_bundle_terminated();
675 } else
676 new_phase(PHASE_DEAD);
677 }
678
679 /*
680 * LCP has gone down; it will either die or try to re-establish.
681 */
682 void
link_down(unit)683 link_down(unit)
684 int unit;
685 {
686 if (auth_state != s_down) {
687 notify(link_down_notifier, 0);
688 auth_state = s_down;
689 if (auth_script_state == s_up && auth_script_pid == 0) {
690 update_link_stats(unit);
691 auth_script_state = s_down;
692 auth_script(_PATH_AUTHDOWN);
693 }
694 }
695 if (!doing_multilink) {
696 upper_layers_down(unit);
697 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
698 new_phase(PHASE_ESTABLISH);
699 }
700 /* XXX if doing_multilink, should do something to stop
701 network-layer traffic on the link */
702 }
703
upper_layers_down(int unit)704 void upper_layers_down(int unit)
705 {
706 int i;
707 struct protent *protp;
708
709 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
710 if (!protp->enabled_flag)
711 continue;
712 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
713 (*protp->lowerdown)(unit);
714 if (protp->protocol < 0xC000 && protp->close != NULL)
715 (*protp->close)(unit, "LCP down");
716 }
717 num_np_open = 0;
718 num_np_up = 0;
719 }
720
721 /*
722 * The link is established.
723 * Proceed to the Dead, Authenticate or Network phase as appropriate.
724 */
725 void
link_established(unit)726 link_established(unit)
727 int unit;
728 {
729 int auth;
730 lcp_options *wo = &lcp_wantoptions[unit];
731 lcp_options *go = &lcp_gotoptions[unit];
732 lcp_options *ho = &lcp_hisoptions[unit];
733 int i;
734 struct protent *protp;
735
736 /*
737 * Tell higher-level protocols that LCP is up.
738 */
739 if (!doing_multilink) {
740 for (i = 0; (protp = protocols[i]) != NULL; ++i)
741 if (protp->protocol != PPP_LCP && protp->enabled_flag
742 && protp->lowerup != NULL)
743 (*protp->lowerup)(unit);
744 }
745
746 if (!auth_required && noauth_addrs != NULL)
747 set_allowed_addrs(unit, NULL, NULL);
748
749 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
750 /*
751 * We wanted the peer to authenticate itself, and it refused:
752 * if we have some address(es) it can use without auth, fine,
753 * otherwise treat it as though it authenticated with PAP using
754 * a username of "" and a password of "". If that's not OK,
755 * boot it out.
756 */
757 if (noauth_addrs != NULL) {
758 set_allowed_addrs(unit, NULL, NULL);
759 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
760 warn("peer refused to authenticate: terminating link");
761 status = EXIT_PEER_AUTH_FAILED;
762 lcp_close(unit, "peer refused to authenticate");
763 return;
764 }
765 }
766
767 new_phase(PHASE_AUTHENTICATE);
768 auth = 0;
769 if (go->neg_eap) {
770 eap_authpeer(unit, our_name);
771 auth |= EAP_PEER;
772 } else if (go->neg_chap) {
773 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
774 auth |= CHAP_PEER;
775 } else if (go->neg_upap) {
776 upap_authpeer(unit);
777 auth |= PAP_PEER;
778 }
779 if (ho->neg_eap) {
780 eap_authwithpeer(unit, user);
781 auth |= EAP_WITHPEER;
782 } else if (ho->neg_chap) {
783 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
784 auth |= CHAP_WITHPEER;
785 } else if (ho->neg_upap) {
786 /* If a blank password was explicitly given as an option, trust
787 the user and don't try to look up one. */
788 if (passwd[0] == 0 && !explicit_passwd) {
789 passwd_from_file = 1;
790 if (!get_pap_passwd(passwd))
791 error("No secret found for PAP login");
792 }
793 upap_authwithpeer(unit, user, passwd);
794 auth |= PAP_WITHPEER;
795 }
796 auth_pending[unit] = auth;
797 auth_done[unit] = 0;
798
799 if (!auth)
800 network_phase(unit);
801 }
802
803 /*
804 * Proceed to the network phase.
805 */
806 static void
network_phase(unit)807 network_phase(unit)
808 int unit;
809 {
810 lcp_options *go = &lcp_gotoptions[unit];
811
812 /* Log calling number. */
813 if (*remote_number)
814 notice("peer from calling number %q authorized", remote_number);
815
816 /*
817 * If the peer had to authenticate, run the auth-up script now.
818 */
819 if (go->neg_chap || go->neg_upap || go->neg_eap) {
820 notify(auth_up_notifier, 0);
821 auth_state = s_up;
822 if (auth_script_state == s_down && auth_script_pid == 0) {
823 auth_script_state = s_up;
824 auth_script(_PATH_AUTHUP);
825 }
826 }
827
828 #ifdef CBCP_SUPPORT
829 /*
830 * If we negotiated callback, do it now.
831 */
832 if (go->neg_cbcp) {
833 new_phase(PHASE_CALLBACK);
834 (*cbcp_protent.open)(unit);
835 return;
836 }
837 #endif
838
839 /*
840 * Process extra options from the secrets file
841 */
842 if (extra_options) {
843 options_from_list(extra_options, 1);
844 free_wordlist(extra_options);
845 extra_options = 0;
846 }
847 start_networks(unit);
848 }
849
850 void
start_networks(unit)851 start_networks(unit)
852 int unit;
853 {
854 int i;
855 struct protent *protp;
856 int ecp_required, mppe_required;
857
858 new_phase(PHASE_NETWORK);
859
860 #ifdef HAVE_MULTILINK
861 if (multilink) {
862 if (mp_join_bundle()) {
863 if (multilink_join_hook)
864 (*multilink_join_hook)();
865 if (updetach && !nodetach)
866 detach();
867 return;
868 }
869 }
870 #endif /* HAVE_MULTILINK */
871
872 #ifdef PPP_FILTER
873 if (!demand)
874 set_filters(&pass_filter, &active_filter);
875 #endif
876 /* Start CCP and ECP */
877 for (i = 0; (protp = protocols[i]) != NULL; ++i)
878 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
879 && protp->enabled_flag && protp->open != NULL)
880 (*protp->open)(0);
881
882 /*
883 * Bring up other network protocols iff encryption is not required.
884 */
885 ecp_required = ecp_gotoptions[unit].required;
886 mppe_required = ccp_gotoptions[unit].mppe;
887 if (!ecp_required && !mppe_required)
888 continue_networks(unit);
889 }
890
891 void
continue_networks(unit)892 continue_networks(unit)
893 int unit;
894 {
895 int i;
896 struct protent *protp;
897
898 /*
899 * Start the "real" network protocols.
900 */
901 for (i = 0; (protp = protocols[i]) != NULL; ++i)
902 if (protp->protocol < 0xC000
903 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
904 && protp->enabled_flag && protp->open != NULL) {
905 (*protp->open)(0);
906 ++num_np_open;
907 }
908
909 if (num_np_open == 0)
910 /* nothing to do */
911 lcp_close(0, "No network protocols running");
912 }
913
914 /*
915 * The peer has failed to authenticate himself using `protocol'.
916 */
917 void
auth_peer_fail(unit,protocol)918 auth_peer_fail(unit, protocol)
919 int unit, protocol;
920 {
921 /*
922 * Authentication failure: take the link down
923 */
924 status = EXIT_PEER_AUTH_FAILED;
925 lcp_close(unit, "Authentication failed");
926 }
927
928 /*
929 * The peer has been successfully authenticated using `protocol'.
930 */
931 void
auth_peer_success(unit,protocol,prot_flavor,name,namelen)932 auth_peer_success(unit, protocol, prot_flavor, name, namelen)
933 int unit, protocol, prot_flavor;
934 char *name;
935 int namelen;
936 {
937 int bit;
938
939 switch (protocol) {
940 case PPP_CHAP:
941 bit = CHAP_PEER;
942 switch (prot_flavor) {
943 case CHAP_MD5:
944 bit |= CHAP_MD5_PEER;
945 break;
946 #ifdef CHAPMS
947 case CHAP_MICROSOFT:
948 bit |= CHAP_MS_PEER;
949 break;
950 case CHAP_MICROSOFT_V2:
951 bit |= CHAP_MS2_PEER;
952 break;
953 #endif
954 }
955 break;
956 case PPP_PAP:
957 bit = PAP_PEER;
958 break;
959 case PPP_EAP:
960 bit = EAP_PEER;
961 break;
962 default:
963 warn("auth_peer_success: unknown protocol %x", protocol);
964 return;
965 }
966
967 /*
968 * Save the authenticated name of the peer for later.
969 */
970 if (namelen > sizeof(peer_authname) - 1)
971 namelen = sizeof(peer_authname) - 1;
972 BCOPY(name, peer_authname, namelen);
973 peer_authname[namelen] = 0;
974 script_setenv("PEERNAME", peer_authname, 0);
975
976 /* Save the authentication method for later. */
977 auth_done[unit] |= bit;
978
979 /*
980 * If there is no more authentication still to be done,
981 * proceed to the network (or callback) phase.
982 */
983 if ((auth_pending[unit] &= ~bit) == 0)
984 network_phase(unit);
985 }
986
987 /*
988 * We have failed to authenticate ourselves to the peer using `protocol'.
989 */
990 void
auth_withpeer_fail(unit,protocol)991 auth_withpeer_fail(unit, protocol)
992 int unit, protocol;
993 {
994 if (passwd_from_file)
995 BZERO(passwd, MAXSECRETLEN);
996 /*
997 * We've failed to authenticate ourselves to our peer.
998 * Some servers keep sending CHAP challenges, but there
999 * is no point in persisting without any way to get updated
1000 * authentication secrets.
1001 */
1002 status = EXIT_AUTH_TOPEER_FAILED;
1003 lcp_close(unit, "Failed to authenticate ourselves to peer");
1004 }
1005
1006 /*
1007 * We have successfully authenticated ourselves with the peer using `protocol'.
1008 */
1009 void
auth_withpeer_success(unit,protocol,prot_flavor)1010 auth_withpeer_success(unit, protocol, prot_flavor)
1011 int unit, protocol, prot_flavor;
1012 {
1013 int bit;
1014 const char *prot = "";
1015
1016 switch (protocol) {
1017 case PPP_CHAP:
1018 bit = CHAP_WITHPEER;
1019 prot = "CHAP";
1020 switch (prot_flavor) {
1021 case CHAP_MD5:
1022 bit |= CHAP_MD5_WITHPEER;
1023 break;
1024 #ifdef CHAPMS
1025 case CHAP_MICROSOFT:
1026 bit |= CHAP_MS_WITHPEER;
1027 break;
1028 case CHAP_MICROSOFT_V2:
1029 bit |= CHAP_MS2_WITHPEER;
1030 break;
1031 #endif
1032 }
1033 break;
1034 case PPP_PAP:
1035 if (passwd_from_file)
1036 BZERO(passwd, MAXSECRETLEN);
1037 bit = PAP_WITHPEER;
1038 prot = "PAP";
1039 break;
1040 case PPP_EAP:
1041 bit = EAP_WITHPEER;
1042 prot = "EAP";
1043 break;
1044 default:
1045 warn("auth_withpeer_success: unknown protocol %x", protocol);
1046 bit = 0;
1047 }
1048
1049 notice("%s authentication succeeded", prot);
1050
1051 /* Save the authentication method for later. */
1052 auth_done[unit] |= bit;
1053
1054 /*
1055 * If there is no more authentication still being done,
1056 * proceed to the network (or callback) phase.
1057 */
1058 if ((auth_pending[unit] &= ~bit) == 0)
1059 network_phase(unit);
1060 }
1061
1062
1063 /*
1064 * np_up - a network protocol has come up.
1065 */
1066 void
np_up(unit,proto)1067 np_up(unit, proto)
1068 int unit, proto;
1069 {
1070 int tlim;
1071
1072 if (num_np_up == 0) {
1073 /*
1074 * At this point we consider that the link has come up successfully.
1075 */
1076 status = EXIT_OK;
1077 unsuccess = 0;
1078 new_phase(PHASE_RUNNING);
1079
1080 if (idle_time_hook != 0)
1081 tlim = (*idle_time_hook)(NULL);
1082 else
1083 tlim = idle_time_limit;
1084 if (tlim > 0)
1085 TIMEOUT(check_idle, NULL, tlim);
1086
1087 /*
1088 * Set a timeout to close the connection once the maximum
1089 * connect time has expired.
1090 */
1091 if (maxconnect > 0)
1092 TIMEOUT(connect_time_expired, 0, maxconnect);
1093
1094 #ifdef MAXOCTETS
1095 if (maxoctets > 0)
1096 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1097 #endif
1098
1099 /*
1100 * Detach now, if the updetach option was given.
1101 */
1102 if (updetach && !nodetach)
1103 detach();
1104 }
1105 ++num_np_up;
1106 }
1107
1108 /*
1109 * np_down - a network protocol has gone down.
1110 */
1111 void
np_down(unit,proto)1112 np_down(unit, proto)
1113 int unit, proto;
1114 {
1115 if (--num_np_up == 0) {
1116 UNTIMEOUT(check_idle, NULL);
1117 UNTIMEOUT(connect_time_expired, NULL);
1118 #ifdef MAXOCTETS
1119 UNTIMEOUT(check_maxoctets, NULL);
1120 #endif
1121 new_phase(PHASE_NETWORK);
1122 }
1123 }
1124
1125 /*
1126 * np_finished - a network protocol has finished using the link.
1127 */
1128 void
np_finished(unit,proto)1129 np_finished(unit, proto)
1130 int unit, proto;
1131 {
1132 if (--num_np_open <= 0) {
1133 /* no further use for the link: shut up shop. */
1134 lcp_close(0, "No network protocols running");
1135 }
1136 }
1137
1138 #ifdef MAXOCTETS
1139 static void
check_maxoctets(arg)1140 check_maxoctets(arg)
1141 void *arg;
1142 {
1143 unsigned int used;
1144
1145 update_link_stats(ifunit);
1146 link_stats_valid=0;
1147
1148 switch(maxoctets_dir) {
1149 case PPP_OCTETS_DIRECTION_IN:
1150 used = link_stats.bytes_in;
1151 break;
1152 case PPP_OCTETS_DIRECTION_OUT:
1153 used = link_stats.bytes_out;
1154 break;
1155 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1156 case PPP_OCTETS_DIRECTION_MAXSESSION:
1157 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
1158 break;
1159 default:
1160 used = link_stats.bytes_in+link_stats.bytes_out;
1161 break;
1162 }
1163 if (used > maxoctets) {
1164 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1165 status = EXIT_TRAFFIC_LIMIT;
1166 lcp_close(0, "Traffic limit");
1167 need_holdoff = 0;
1168 } else {
1169 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1170 }
1171 }
1172 #endif
1173
1174 /*
1175 * check_idle - check whether the link has been idle for long
1176 * enough that we can shut it down.
1177 */
1178 static void
check_idle(arg)1179 check_idle(arg)
1180 void *arg;
1181 {
1182 struct ppp_idle idle;
1183 time_t itime;
1184 int tlim;
1185
1186 if (!get_idle_time(0, &idle))
1187 return;
1188 if (idle_time_hook != 0) {
1189 tlim = idle_time_hook(&idle);
1190 } else {
1191 itime = MIN(idle.xmit_idle, idle.recv_idle);
1192 tlim = idle_time_limit - itime;
1193 }
1194 if (tlim <= 0) {
1195 /* link is idle: shut it down. */
1196 notice("Terminating connection due to lack of activity.");
1197 status = EXIT_IDLE_TIMEOUT;
1198 lcp_close(0, "Link inactive");
1199 need_holdoff = 0;
1200 } else {
1201 TIMEOUT(check_idle, NULL, tlim);
1202 }
1203 }
1204
1205 /*
1206 * connect_time_expired - log a message and close the connection.
1207 */
1208 static void
connect_time_expired(arg)1209 connect_time_expired(arg)
1210 void *arg;
1211 {
1212 info("Connect time expired");
1213 status = EXIT_CONNECT_TIME;
1214 lcp_close(0, "Connect time expired"); /* Close connection */
1215 }
1216
1217 /*
1218 * auth_check_options - called to check authentication options.
1219 */
1220 void
auth_check_options()1221 auth_check_options()
1222 {
1223 lcp_options *wo = &lcp_wantoptions[0];
1224 int can_auth;
1225 int lacks_ip;
1226
1227 /* Default our_name to hostname, and user to our_name */
1228 if (our_name[0] == 0 || usehostname)
1229 strlcpy(our_name, hostname, sizeof(our_name));
1230 /* If a blank username was explicitly given as an option, trust
1231 the user and don't use our_name */
1232 if (user[0] == 0 && !explicit_user)
1233 strlcpy(user, our_name, sizeof(user));
1234
1235 /*
1236 * If we have a default route, require the peer to authenticate
1237 * unless the noauth option was given or the real user is root.
1238 */
1239 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1240 auth_required = 1;
1241 default_auth = 1;
1242 }
1243
1244 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1245 if (wo->chap_mdtype)
1246 wo->neg_chap = 1;
1247
1248 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1249 if (auth_required) {
1250 allow_any_ip = 0;
1251 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1252 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
1253 wo->chap_mdtype = chap_mdtype_all;
1254 wo->neg_upap = 1;
1255 wo->neg_eap = 1;
1256 }
1257 } else {
1258 wo->neg_chap = 0;
1259 wo->chap_mdtype = MDTYPE_NONE;
1260 wo->neg_upap = 0;
1261 wo->neg_eap = 0;
1262 }
1263
1264 /*
1265 * Check whether we have appropriate secrets to use
1266 * to authenticate the peer. Note that EAP can authenticate by way
1267 * of a CHAP-like exchanges as well as SRP.
1268 */
1269 lacks_ip = 0;
1270 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1271 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1272 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1273 our_name, 1, &lacks_ip);
1274 }
1275 if (!can_auth && wo->neg_eap) {
1276 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1277 our_name, 1, &lacks_ip);
1278 }
1279
1280 if (auth_required && !can_auth && noauth_addrs == NULL) {
1281 if (default_auth) {
1282 option_error(
1283 "By default the remote system is required to authenticate itself");
1284 option_error(
1285 "(because this system has a default route to the internet)");
1286 } else if (explicit_remote)
1287 option_error(
1288 "The remote system (%s) is required to authenticate itself",
1289 remote_name);
1290 else
1291 option_error(
1292 "The remote system is required to authenticate itself");
1293 option_error(
1294 "but I couldn't find any suitable secret (password) for it to use to do so.");
1295 if (lacks_ip)
1296 option_error(
1297 "(None of the available passwords would let it use an IP address.)");
1298
1299 exit(1);
1300 }
1301
1302 /*
1303 * Early check for remote number authorization.
1304 */
1305 if (!auth_number()) {
1306 warn("calling number %q is not authorized", remote_number);
1307 exit(EXIT_CNID_AUTH_FAILED);
1308 }
1309 }
1310
1311 /*
1312 * auth_reset - called when LCP is starting negotiations to recheck
1313 * authentication options, i.e. whether we have appropriate secrets
1314 * to use for authenticating ourselves and/or the peer.
1315 */
1316 void
auth_reset(unit)1317 auth_reset(unit)
1318 int unit;
1319 {
1320 lcp_options *go = &lcp_gotoptions[unit];
1321 lcp_options *ao = &lcp_allowoptions[unit];
1322 int hadchap;
1323
1324 hadchap = -1;
1325 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1326 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1327 && (passwd[0] != 0 ||
1328 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1329 NULL), 0, NULL)));
1330 ao->neg_eap = !refuse_eap && (
1331 passwd[0] != 0 ||
1332 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1333 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1334 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL));
1335
1336 hadchap = -1;
1337 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1338 go->neg_upap = 0;
1339 if (go->neg_chap) {
1340 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1341 our_name, 1, NULL)))
1342 go->neg_chap = 0;
1343 }
1344 if (go->neg_eap &&
1345 (hadchap == 0 || (hadchap == -1 &&
1346 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1347 1, NULL))) &&
1348 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1349 NULL))
1350 go->neg_eap = 0;
1351 }
1352
1353
1354 /*
1355 * check_passwd - Check the user name and passwd against the PAP secrets
1356 * file. If requested, also check against the system password database,
1357 * and login the user if OK.
1358 *
1359 * returns:
1360 * UPAP_AUTHNAK: Authentication failed.
1361 * UPAP_AUTHACK: Authentication succeeded.
1362 * In either case, msg points to an appropriate message.
1363 */
1364 int
check_passwd(unit,auser,userlen,apasswd,passwdlen,msg)1365 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1366 int unit;
1367 char *auser;
1368 int userlen;
1369 char *apasswd;
1370 int passwdlen;
1371 char **msg;
1372 {
1373 #if defined(__ANDROID__)
1374 return UPAP_AUTHNAK;
1375 #else
1376 int ret;
1377 char *filename;
1378 FILE *f;
1379 struct wordlist *addrs = NULL, *opts = NULL;
1380 char passwd[256], user[256];
1381 char secret[MAXWORDLEN];
1382 static int attempts = 0;
1383
1384 /*
1385 * Make copies of apasswd and auser, then null-terminate them.
1386 * If there are unprintable characters in the password, make
1387 * them visible.
1388 */
1389 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1390 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1391 *msg = "";
1392
1393 /*
1394 * Check if a plugin wants to handle this.
1395 */
1396 if (pap_auth_hook) {
1397 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1398 if (ret >= 0) {
1399 /* note: set_allowed_addrs() saves opts (but not addrs):
1400 don't free it! */
1401 if (ret)
1402 set_allowed_addrs(unit, addrs, opts);
1403 else if (opts != 0)
1404 free_wordlist(opts);
1405 if (addrs != 0)
1406 free_wordlist(addrs);
1407 BZERO(passwd, sizeof(passwd));
1408 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1409 }
1410 }
1411
1412 /*
1413 * Open the file of pap secrets and scan for a suitable secret
1414 * for authenticating this user.
1415 */
1416 filename = _PATH_UPAPFILE;
1417 addrs = opts = NULL;
1418 ret = UPAP_AUTHNAK;
1419 f = fopen(filename, "r");
1420 if (f == NULL) {
1421 error("Can't open PAP password file %s: %m", filename);
1422
1423 } else {
1424 check_access(f, filename);
1425 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1426 warn("no PAP secret found for %s", user);
1427 } else {
1428 /*
1429 * If the secret is "@login", it means to check
1430 * the password against the login database.
1431 */
1432 int login_secret = strcmp(secret, "@login") == 0;
1433 ret = UPAP_AUTHACK;
1434 if (uselogin || login_secret) {
1435 /* login option or secret is @login */
1436 if (session_full(user, passwd, devnam, msg) == 0) {
1437 ret = UPAP_AUTHNAK;
1438 }
1439 } else if (session_mgmt) {
1440 if (session_check(user, NULL, devnam, NULL) == 0) {
1441 warn("Peer %q failed PAP Session verification", user);
1442 ret = UPAP_AUTHNAK;
1443 }
1444 }
1445 if (secret[0] != 0 && !login_secret) {
1446 /* password given in pap-secrets - must match */
1447 if (cryptpap || strcmp(passwd, secret) != 0) {
1448 char *cbuf = crypt(passwd, secret);
1449 if (!cbuf || strcmp(cbuf, secret) != 0)
1450 ret = UPAP_AUTHNAK;
1451 }
1452 }
1453 }
1454 fclose(f);
1455 }
1456
1457 if (ret == UPAP_AUTHNAK) {
1458 if (**msg == 0)
1459 *msg = "Login incorrect";
1460 /*
1461 * XXX can we ever get here more than once??
1462 * Frustrate passwd stealer programs.
1463 * Allow 10 tries, but start backing off after 3 (stolen from login).
1464 * On 10'th, drop the connection.
1465 */
1466 if (attempts++ >= 10) {
1467 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1468 lcp_close(unit, "login failed");
1469 }
1470 if (attempts > 3)
1471 sleep((u_int) (attempts - 3) * 5);
1472 if (opts != NULL)
1473 free_wordlist(opts);
1474
1475 } else {
1476 attempts = 0; /* Reset count */
1477 if (**msg == 0)
1478 *msg = "Login ok";
1479 set_allowed_addrs(unit, addrs, opts);
1480 }
1481
1482 if (addrs != NULL)
1483 free_wordlist(addrs);
1484 BZERO(passwd, sizeof(passwd));
1485 BZERO(secret, sizeof(secret));
1486
1487 return ret;
1488 #endif
1489 }
1490
1491 /*
1492 * null_login - Check if a username of "" and a password of "" are
1493 * acceptable, and iff so, set the list of acceptable IP addresses
1494 * and return 1.
1495 */
1496 static int
null_login(unit)1497 null_login(unit)
1498 int unit;
1499 {
1500 char *filename;
1501 FILE *f;
1502 int i, ret;
1503 struct wordlist *addrs, *opts;
1504 char secret[MAXWORDLEN];
1505
1506 /*
1507 * Check if a plugin wants to handle this.
1508 */
1509 ret = -1;
1510 if (null_auth_hook)
1511 ret = (*null_auth_hook)(&addrs, &opts);
1512
1513 /*
1514 * Open the file of pap secrets and scan for a suitable secret.
1515 */
1516 if (ret <= 0) {
1517 filename = _PATH_UPAPFILE;
1518 addrs = NULL;
1519 f = fopen(filename, "r");
1520 if (f == NULL)
1521 return 0;
1522 check_access(f, filename);
1523
1524 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1525 ret = i >= 0 && secret[0] == 0;
1526 BZERO(secret, sizeof(secret));
1527 fclose(f);
1528 }
1529
1530 if (ret)
1531 set_allowed_addrs(unit, addrs, opts);
1532 else if (opts != 0)
1533 free_wordlist(opts);
1534 if (addrs != 0)
1535 free_wordlist(addrs);
1536
1537 return ret;
1538 }
1539
1540
1541 /*
1542 * get_pap_passwd - get a password for authenticating ourselves with
1543 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1544 * could be found.
1545 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1546 */
1547 static int
get_pap_passwd(passwd)1548 get_pap_passwd(passwd)
1549 char *passwd;
1550 {
1551 char *filename;
1552 FILE *f;
1553 int ret;
1554 char secret[MAXWORDLEN];
1555
1556 /*
1557 * Check whether a plugin wants to supply this.
1558 */
1559 if (pap_passwd_hook) {
1560 ret = (*pap_passwd_hook)(user, passwd);
1561 if (ret >= 0)
1562 return ret;
1563 }
1564
1565 filename = _PATH_UPAPFILE;
1566 f = fopen(filename, "r");
1567 if (f == NULL)
1568 return 0;
1569 check_access(f, filename);
1570 ret = scan_authfile(f, user,
1571 (remote_name[0]? remote_name: NULL),
1572 secret, NULL, NULL, filename, 0);
1573 fclose(f);
1574 if (ret < 0)
1575 return 0;
1576 if (passwd != NULL)
1577 strlcpy(passwd, secret, MAXSECRETLEN);
1578 BZERO(secret, sizeof(secret));
1579 return 1;
1580 }
1581
1582
1583 /*
1584 * have_pap_secret - check whether we have a PAP file with any
1585 * secrets that we could possibly use for authenticating the peer.
1586 */
1587 static int
have_pap_secret(lacks_ipp)1588 have_pap_secret(lacks_ipp)
1589 int *lacks_ipp;
1590 {
1591 FILE *f;
1592 int ret;
1593 char *filename;
1594 struct wordlist *addrs;
1595
1596 /* let the plugin decide, if there is one */
1597 if (pap_check_hook) {
1598 ret = (*pap_check_hook)();
1599 if (ret >= 0)
1600 return ret;
1601 }
1602
1603 filename = _PATH_UPAPFILE;
1604 f = fopen(filename, "r");
1605 if (f == NULL)
1606 return 0;
1607
1608 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1609 NULL, &addrs, NULL, filename, 0);
1610 fclose(f);
1611 if (ret >= 0 && !some_ip_ok(addrs)) {
1612 if (lacks_ipp != 0)
1613 *lacks_ipp = 1;
1614 ret = -1;
1615 }
1616 if (addrs != 0)
1617 free_wordlist(addrs);
1618
1619 return ret >= 0;
1620 }
1621
1622
1623 /*
1624 * have_chap_secret - check whether we have a CHAP file with a
1625 * secret that we could possibly use for authenticating `client'
1626 * on `server'. Either can be the null string, meaning we don't
1627 * know the identity yet.
1628 */
1629 static int
have_chap_secret(client,server,need_ip,lacks_ipp)1630 have_chap_secret(client, server, need_ip, lacks_ipp)
1631 char *client;
1632 char *server;
1633 int need_ip;
1634 int *lacks_ipp;
1635 {
1636 FILE *f;
1637 int ret;
1638 char *filename;
1639 struct wordlist *addrs;
1640
1641 if (chap_check_hook) {
1642 ret = (*chap_check_hook)();
1643 if (ret >= 0) {
1644 return ret;
1645 }
1646 }
1647
1648 filename = _PATH_CHAPFILE;
1649 f = fopen(filename, "r");
1650 if (f == NULL)
1651 return 0;
1652
1653 if (client != NULL && client[0] == 0)
1654 client = NULL;
1655 else if (server != NULL && server[0] == 0)
1656 server = NULL;
1657
1658 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1659 fclose(f);
1660 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1661 if (lacks_ipp != 0)
1662 *lacks_ipp = 1;
1663 ret = -1;
1664 }
1665 if (addrs != 0)
1666 free_wordlist(addrs);
1667
1668 return ret >= 0;
1669 }
1670
1671
1672 /*
1673 * have_srp_secret - check whether we have a SRP file with a
1674 * secret that we could possibly use for authenticating `client'
1675 * on `server'. Either can be the null string, meaning we don't
1676 * know the identity yet.
1677 */
1678 static int
have_srp_secret(client,server,need_ip,lacks_ipp)1679 have_srp_secret(client, server, need_ip, lacks_ipp)
1680 char *client;
1681 char *server;
1682 int need_ip;
1683 int *lacks_ipp;
1684 {
1685 FILE *f;
1686 int ret;
1687 char *filename;
1688 struct wordlist *addrs;
1689
1690 filename = _PATH_SRPFILE;
1691 f = fopen(filename, "r");
1692 if (f == NULL)
1693 return 0;
1694
1695 if (client != NULL && client[0] == 0)
1696 client = NULL;
1697 else if (server != NULL && server[0] == 0)
1698 server = NULL;
1699
1700 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1701 fclose(f);
1702 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1703 if (lacks_ipp != 0)
1704 *lacks_ipp = 1;
1705 ret = -1;
1706 }
1707 if (addrs != 0)
1708 free_wordlist(addrs);
1709
1710 return ret >= 0;
1711 }
1712
1713
1714 /*
1715 * get_secret - open the CHAP secret file and return the secret
1716 * for authenticating the given client on the given server.
1717 * (We could be either client or server).
1718 */
1719 int
get_secret(unit,client,server,secret,secret_len,am_server)1720 get_secret(unit, client, server, secret, secret_len, am_server)
1721 int unit;
1722 char *client;
1723 char *server;
1724 char *secret;
1725 int *secret_len;
1726 int am_server;
1727 {
1728 FILE *f;
1729 int ret, len;
1730 char *filename;
1731 struct wordlist *addrs, *opts;
1732 char secbuf[MAXWORDLEN];
1733
1734 if (!am_server && passwd[0] != 0) {
1735 strlcpy(secbuf, passwd, sizeof(secbuf));
1736 } else if (!am_server && chap_passwd_hook) {
1737 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1738 error("Unable to obtain CHAP password for %s on %s from plugin",
1739 client, server);
1740 return 0;
1741 }
1742 } else {
1743 filename = _PATH_CHAPFILE;
1744 addrs = NULL;
1745 secbuf[0] = 0;
1746
1747 f = fopen(filename, "r");
1748 if (f == NULL) {
1749 error("Can't open chap secret file %s: %m", filename);
1750 return 0;
1751 }
1752 check_access(f, filename);
1753
1754 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1755 fclose(f);
1756 if (ret < 0)
1757 return 0;
1758
1759 if (am_server)
1760 set_allowed_addrs(unit, addrs, opts);
1761 else if (opts != 0)
1762 free_wordlist(opts);
1763 if (addrs != 0)
1764 free_wordlist(addrs);
1765 }
1766
1767 len = strlen(secbuf);
1768 if (len > MAXSECRETLEN) {
1769 error("Secret for %s on %s is too long", client, server);
1770 len = MAXSECRETLEN;
1771 }
1772 BCOPY(secbuf, secret, len);
1773 BZERO(secbuf, sizeof(secbuf));
1774 *secret_len = len;
1775
1776 return 1;
1777 }
1778
1779
1780 /*
1781 * get_srp_secret - open the SRP secret file and return the secret
1782 * for authenticating the given client on the given server.
1783 * (We could be either client or server).
1784 */
1785 int
get_srp_secret(unit,client,server,secret,am_server)1786 get_srp_secret(unit, client, server, secret, am_server)
1787 int unit;
1788 char *client;
1789 char *server;
1790 char *secret;
1791 int am_server;
1792 {
1793 FILE *fp;
1794 int ret;
1795 char *filename;
1796 struct wordlist *addrs, *opts;
1797
1798 if (!am_server && passwd[0] != '\0') {
1799 strlcpy(secret, passwd, MAXWORDLEN);
1800 } else {
1801 filename = _PATH_SRPFILE;
1802 addrs = NULL;
1803
1804 fp = fopen(filename, "r");
1805 if (fp == NULL) {
1806 error("Can't open srp secret file %s: %m", filename);
1807 return 0;
1808 }
1809 check_access(fp, filename);
1810
1811 secret[0] = '\0';
1812 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1813 filename, am_server);
1814 fclose(fp);
1815 if (ret < 0)
1816 return 0;
1817
1818 if (am_server)
1819 set_allowed_addrs(unit, addrs, opts);
1820 else if (opts != NULL)
1821 free_wordlist(opts);
1822 if (addrs != NULL)
1823 free_wordlist(addrs);
1824 }
1825
1826 return 1;
1827 }
1828
1829 /*
1830 * set_allowed_addrs() - set the list of allowed addresses.
1831 * Also looks for `--' indicating options to apply for this peer
1832 * and leaves the following words in extra_options.
1833 */
1834 static void
set_allowed_addrs(unit,addrs,opts)1835 set_allowed_addrs(unit, addrs, opts)
1836 int unit;
1837 struct wordlist *addrs;
1838 struct wordlist *opts;
1839 {
1840 int n;
1841 struct wordlist *ap, **plink;
1842 struct permitted_ip *ip;
1843 char *ptr_word, *ptr_mask;
1844 struct hostent *hp;
1845 struct netent *np;
1846 u_int32_t a, mask, ah, offset;
1847 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1848 u_int32_t suggested_ip = 0;
1849
1850 if (addresses[unit] != NULL)
1851 free(addresses[unit]);
1852 addresses[unit] = NULL;
1853 if (extra_options != NULL)
1854 free_wordlist(extra_options);
1855 extra_options = opts;
1856
1857 /*
1858 * Count the number of IP addresses given.
1859 */
1860 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1861 if (n == 0)
1862 return;
1863 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1864 if (ip == 0)
1865 return;
1866
1867 /* temporarily append the noauth_addrs list to addrs */
1868 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1869 ;
1870 *plink = noauth_addrs;
1871
1872 n = 0;
1873 for (ap = addrs; ap != NULL; ap = ap->next) {
1874 /* "-" means no addresses authorized, "*" means any address allowed */
1875 ptr_word = ap->word;
1876 if (strcmp(ptr_word, "-") == 0)
1877 break;
1878 if (strcmp(ptr_word, "*") == 0) {
1879 ip[n].permit = 1;
1880 ip[n].base = ip[n].mask = 0;
1881 ++n;
1882 break;
1883 }
1884
1885 ip[n].permit = 1;
1886 if (*ptr_word == '!') {
1887 ip[n].permit = 0;
1888 ++ptr_word;
1889 }
1890
1891 mask = ~ (u_int32_t) 0;
1892 offset = 0;
1893 ptr_mask = strchr (ptr_word, '/');
1894 if (ptr_mask != NULL) {
1895 int bit_count;
1896 char *endp;
1897
1898 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1899 if (bit_count <= 0 || bit_count > 32) {
1900 warn("invalid address length %v in auth. address list",
1901 ptr_mask+1);
1902 continue;
1903 }
1904 bit_count = 32 - bit_count; /* # bits in host part */
1905 if (*endp == '+') {
1906 offset = ifunit + 1;
1907 ++endp;
1908 }
1909 if (*endp != 0) {
1910 warn("invalid address length syntax: %v", ptr_mask+1);
1911 continue;
1912 }
1913 *ptr_mask = '\0';
1914 mask <<= bit_count;
1915 }
1916
1917 hp = gethostbyname(ptr_word);
1918 if (hp != NULL && hp->h_addrtype == AF_INET) {
1919 a = *(u_int32_t *)hp->h_addr;
1920 } else {
1921 np = getnetbyname (ptr_word);
1922 if (np != NULL && np->n_addrtype == AF_INET) {
1923 a = htonl ((u_int32_t)np->n_net);
1924 if (ptr_mask == NULL) {
1925 /* calculate appropriate mask for net */
1926 ah = ntohl(a);
1927 if (IN_CLASSA(ah))
1928 mask = IN_CLASSA_NET;
1929 else if (IN_CLASSB(ah))
1930 mask = IN_CLASSB_NET;
1931 else if (IN_CLASSC(ah))
1932 mask = IN_CLASSC_NET;
1933 }
1934 } else {
1935 a = inet_addr (ptr_word);
1936 }
1937 }
1938
1939 if (ptr_mask != NULL)
1940 *ptr_mask = '/';
1941
1942 if (a == (u_int32_t)-1L) {
1943 warn("unknown host %s in auth. address list", ap->word);
1944 continue;
1945 }
1946 if (offset != 0) {
1947 if (offset >= ~mask) {
1948 warn("interface unit %d too large for subnet %v",
1949 ifunit, ptr_word);
1950 continue;
1951 }
1952 a = htonl((ntohl(a) & mask) + offset);
1953 mask = ~(u_int32_t)0;
1954 }
1955 ip[n].mask = htonl(mask);
1956 ip[n].base = a & ip[n].mask;
1957 ++n;
1958 if (~mask == 0 && suggested_ip == 0)
1959 suggested_ip = a;
1960 }
1961 *plink = NULL;
1962
1963 ip[n].permit = 0; /* make the last entry forbid all addresses */
1964 ip[n].base = 0; /* to terminate the list */
1965 ip[n].mask = 0;
1966
1967 addresses[unit] = ip;
1968
1969 /*
1970 * If the address given for the peer isn't authorized, or if
1971 * the user hasn't given one, AND there is an authorized address
1972 * which is a single host, then use that if we find one.
1973 */
1974 if (suggested_ip != 0
1975 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
1976 wo->hisaddr = suggested_ip;
1977 /*
1978 * Do we insist on this address? No, if there are other
1979 * addresses authorized than the suggested one.
1980 */
1981 if (n > 1)
1982 wo->accept_remote = 1;
1983 }
1984 }
1985
1986 /*
1987 * auth_ip_addr - check whether the peer is authorized to use
1988 * a given IP address. Returns 1 if authorized, 0 otherwise.
1989 */
1990 int
auth_ip_addr(unit,addr)1991 auth_ip_addr(unit, addr)
1992 int unit;
1993 u_int32_t addr;
1994 {
1995 int ok;
1996
1997 /* don't allow loopback or multicast address */
1998 if (bad_ip_adrs(addr))
1999 return 0;
2000
2001 if (allowed_address_hook) {
2002 ok = allowed_address_hook(addr);
2003 if (ok >= 0) return ok;
2004 }
2005
2006 if (addresses[unit] != NULL) {
2007 ok = ip_addr_check(addr, addresses[unit]);
2008 if (ok >= 0)
2009 return ok;
2010 }
2011
2012 if (auth_required)
2013 return 0; /* no addresses authorized */
2014 return allow_any_ip || privileged || !have_route_to(addr);
2015 }
2016
2017 static int
ip_addr_check(addr,addrs)2018 ip_addr_check(addr, addrs)
2019 u_int32_t addr;
2020 struct permitted_ip *addrs;
2021 {
2022 for (; ; ++addrs)
2023 if ((addr & addrs->mask) == addrs->base)
2024 return addrs->permit;
2025 }
2026
2027 /*
2028 * bad_ip_adrs - return 1 if the IP address is one we don't want
2029 * to use, such as an address in the loopback net or a multicast address.
2030 * addr is in network byte order.
2031 */
2032 int
bad_ip_adrs(addr)2033 bad_ip_adrs(addr)
2034 u_int32_t addr;
2035 {
2036 addr = ntohl(addr);
2037 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2038 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2039 }
2040
2041 /*
2042 * some_ip_ok - check a wordlist to see if it authorizes any
2043 * IP address(es).
2044 */
2045 static int
some_ip_ok(addrs)2046 some_ip_ok(addrs)
2047 struct wordlist *addrs;
2048 {
2049 for (; addrs != 0; addrs = addrs->next) {
2050 if (addrs->word[0] == '-')
2051 break;
2052 if (addrs->word[0] != '!')
2053 return 1; /* some IP address is allowed */
2054 }
2055 return 0;
2056 }
2057
2058 /*
2059 * auth_number - check whether the remote number is allowed to connect.
2060 * Returns 1 if authorized, 0 otherwise.
2061 */
2062 int
auth_number()2063 auth_number()
2064 {
2065 struct wordlist *wp = permitted_numbers;
2066 int l;
2067
2068 /* Allow all if no authorization list. */
2069 if (!wp)
2070 return 1;
2071
2072 /* Allow if we have a match in the authorization list. */
2073 while (wp) {
2074 /* trailing '*' wildcard */
2075 l = strlen(wp->word);
2076 if ((wp->word)[l - 1] == '*')
2077 l--;
2078 if (!strncasecmp(wp->word, remote_number, l))
2079 return 1;
2080 wp = wp->next;
2081 }
2082
2083 return 0;
2084 }
2085
2086 /*
2087 * check_access - complain if a secret file has too-liberal permissions.
2088 */
2089 static void
check_access(f,filename)2090 check_access(f, filename)
2091 FILE *f;
2092 char *filename;
2093 {
2094 struct stat sbuf;
2095
2096 if (fstat(fileno(f), &sbuf) < 0) {
2097 warn("cannot stat secret file %s: %m", filename);
2098 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2099 warn("Warning - secret file %s has world and/or group access",
2100 filename);
2101 }
2102 }
2103
2104
2105 /*
2106 * scan_authfile - Scan an authorization file for a secret suitable
2107 * for authenticating `client' on `server'. The return value is -1
2108 * if no secret is found, otherwise >= 0. The return value has
2109 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2110 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2111 * Any following words on the line up to a "--" (i.e. address authorization
2112 * info) are placed in a wordlist and returned in *addrs. Any
2113 * following words (extra options) are placed in a wordlist and
2114 * returned in *opts.
2115 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2116 * Flags are non-zero if we need two colons in the secret in order to
2117 * match.
2118 */
2119 static int
scan_authfile(f,client,server,secret,addrs,opts,filename,flags)2120 scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
2121 FILE *f;
2122 char *client;
2123 char *server;
2124 char *secret;
2125 struct wordlist **addrs;
2126 struct wordlist **opts;
2127 char *filename;
2128 int flags;
2129 {
2130 int newline, xxx;
2131 int got_flag, best_flag;
2132 FILE *sf;
2133 struct wordlist *ap, *addr_list, *alist, **app;
2134 char word[MAXWORDLEN];
2135 char atfile[MAXWORDLEN];
2136 char lsecret[MAXWORDLEN];
2137 char *cp;
2138
2139 if (addrs != NULL)
2140 *addrs = NULL;
2141 if (opts != NULL)
2142 *opts = NULL;
2143 addr_list = NULL;
2144 if (!getword(f, word, &newline, filename))
2145 return -1; /* file is empty??? */
2146 newline = 1;
2147 best_flag = -1;
2148 for (;;) {
2149 /*
2150 * Skip until we find a word at the start of a line.
2151 */
2152 while (!newline && getword(f, word, &newline, filename))
2153 ;
2154 if (!newline)
2155 break; /* got to end of file */
2156
2157 /*
2158 * Got a client - check if it's a match or a wildcard.
2159 */
2160 got_flag = 0;
2161 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2162 newline = 0;
2163 continue;
2164 }
2165 if (!ISWILD(word))
2166 got_flag = NONWILD_CLIENT;
2167
2168 /*
2169 * Now get a server and check if it matches.
2170 */
2171 if (!getword(f, word, &newline, filename))
2172 break;
2173 if (newline)
2174 continue;
2175 if (!ISWILD(word)) {
2176 if (server != NULL && strcmp(word, server) != 0)
2177 continue;
2178 got_flag |= NONWILD_SERVER;
2179 }
2180
2181 /*
2182 * Got some sort of a match - see if it's better than what
2183 * we have already.
2184 */
2185 if (got_flag <= best_flag)
2186 continue;
2187
2188 /*
2189 * Get the secret.
2190 */
2191 if (!getword(f, word, &newline, filename))
2192 break;
2193 if (newline)
2194 continue;
2195
2196 /*
2197 * SRP-SHA1 authenticator should never be reading secrets from
2198 * a file. (Authenticatee may, though.)
2199 */
2200 if (flags && ((cp = strchr(word, ':')) == NULL ||
2201 strchr(cp + 1, ':') == NULL))
2202 continue;
2203
2204 if (secret != NULL) {
2205 /*
2206 * Special syntax: @/pathname means read secret from file.
2207 */
2208 if (word[0] == '@' && word[1] == '/') {
2209 strlcpy(atfile, word+1, sizeof(atfile));
2210 if ((sf = fopen(atfile, "r")) == NULL) {
2211 warn("can't open indirect secret file %s", atfile);
2212 continue;
2213 }
2214 check_access(sf, atfile);
2215 if (!getword(sf, word, &xxx, atfile)) {
2216 warn("no secret in indirect secret file %s", atfile);
2217 fclose(sf);
2218 continue;
2219 }
2220 fclose(sf);
2221 }
2222 strlcpy(lsecret, word, sizeof(lsecret));
2223 }
2224
2225 /*
2226 * Now read address authorization info and make a wordlist.
2227 */
2228 app = &alist;
2229 for (;;) {
2230 if (!getword(f, word, &newline, filename) || newline)
2231 break;
2232 ap = (struct wordlist *)
2233 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2234 if (ap == NULL)
2235 novm("authorized addresses");
2236 ap->word = (char *) (ap + 1);
2237 strcpy(ap->word, word);
2238 *app = ap;
2239 app = &ap->next;
2240 }
2241 *app = NULL;
2242
2243 /*
2244 * This is the best so far; remember it.
2245 */
2246 best_flag = got_flag;
2247 if (addr_list)
2248 free_wordlist(addr_list);
2249 addr_list = alist;
2250 if (secret != NULL)
2251 strlcpy(secret, lsecret, MAXWORDLEN);
2252
2253 if (!newline)
2254 break;
2255 }
2256
2257 /* scan for a -- word indicating the start of options */
2258 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2259 if (strcmp(ap->word, "--") == 0)
2260 break;
2261 /* ap = start of options */
2262 if (ap != NULL) {
2263 ap = ap->next; /* first option */
2264 free(*app); /* free the "--" word */
2265 *app = NULL; /* terminate addr list */
2266 }
2267 if (opts != NULL)
2268 *opts = ap;
2269 else if (ap != NULL)
2270 free_wordlist(ap);
2271 if (addrs != NULL)
2272 *addrs = addr_list;
2273 else if (addr_list != NULL)
2274 free_wordlist(addr_list);
2275
2276 return best_flag;
2277 }
2278
2279 /*
2280 * wordlist_count - return the number of items in a wordlist
2281 */
2282 static int
wordlist_count(wp)2283 wordlist_count(wp)
2284 struct wordlist *wp;
2285 {
2286 int n;
2287
2288 for (n = 0; wp != NULL; wp = wp->next)
2289 ++n;
2290 return n;
2291 }
2292
2293 /*
2294 * free_wordlist - release memory allocated for a wordlist.
2295 */
2296 static void
free_wordlist(wp)2297 free_wordlist(wp)
2298 struct wordlist *wp;
2299 {
2300 struct wordlist *next;
2301
2302 while (wp != NULL) {
2303 next = wp->next;
2304 free(wp);
2305 wp = next;
2306 }
2307 }
2308
2309 /*
2310 * auth_script_done - called when the auth-up or auth-down script
2311 * has finished.
2312 */
2313 static void
auth_script_done(arg)2314 auth_script_done(arg)
2315 void *arg;
2316 {
2317 auth_script_pid = 0;
2318 switch (auth_script_state) {
2319 case s_up:
2320 if (auth_state == s_down) {
2321 auth_script_state = s_down;
2322 auth_script(_PATH_AUTHDOWN);
2323 }
2324 break;
2325 case s_down:
2326 if (auth_state == s_up) {
2327 auth_script_state = s_up;
2328 auth_script(_PATH_AUTHUP);
2329 }
2330 break;
2331 }
2332 }
2333
2334 /*
2335 * auth_script - execute a script with arguments
2336 * interface-name peer-name real-user tty speed
2337 */
2338 static void
auth_script(script)2339 auth_script(script)
2340 char *script;
2341 {
2342 char strspeed[32];
2343 struct passwd *pw;
2344 char struid[32];
2345 char *user_name;
2346 char *argv[8];
2347
2348 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2349 user_name = pw->pw_name;
2350 else {
2351 slprintf(struid, sizeof(struid), "%d", getuid());
2352 user_name = struid;
2353 }
2354 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2355
2356 argv[0] = script;
2357 argv[1] = ifname;
2358 argv[2] = peer_authname;
2359 argv[3] = user_name;
2360 argv[4] = devnam;
2361 argv[5] = strspeed;
2362 argv[6] = NULL;
2363
2364 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);
2365 }
2366