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