OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [lwip_tcpip/] [current/] [src/] [netif/] [ppp/] [auth.c] - Blame information for rev 865

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
/*****************************************************************************
2
* auth.c - Network Authentication and Phase Control program file.
3
*
4
* Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
5
* Copyright (c) 1997 by Global Election Systems Inc.  All rights reserved.
6
*
7
* The authors hereby grant permission to use, copy, modify, distribute,
8
* and license this software and its documentation for any purpose, provided
9
* that existing copyright notices are retained in all copies and that this
10
* notice and the following disclaimer are included verbatim in any
11
* distributions. No written agreement, license, or royalty fee is required
12
* for any of the authorized uses.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
15
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
*
25
******************************************************************************
26
* REVISION HISTORY
27
*
28
* 03-01-01 Marc Boucher <marc@mbsi.ca>
29
*   Ported to lwIP.
30
* 97-12-08 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
31
*   Ported from public pppd code.
32
*****************************************************************************/
33
/*
34
 * auth.c - PPP authentication and phase control.
35
 *
36
 * Copyright (c) 1993 The Australian National University.
37
 * All rights reserved.
38
 *
39
 * Redistribution and use in source and binary forms are permitted
40
 * provided that the above copyright notice and this paragraph are
41
 * duplicated in all such forms and that any documentation,
42
 * advertising materials, and other materials related to such
43
 * distribution and use acknowledge that the software was developed
44
 * by the Australian National University.  The name of the University
45
 * may not be used to endorse or promote products derived from this
46
 * software without specific prior written permission.
47
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50
 *
51
 * Copyright (c) 1989 Carnegie Mellon University.
52
 * All rights reserved.
53
 *
54
 * Redistribution and use in source and binary forms are permitted
55
 * provided that the above copyright notice and this paragraph are
56
 * duplicated in all such forms and that any documentation,
57
 * advertising materials, and other materials related to such
58
 * distribution and use acknowledge that the software was developed
59
 * by Carnegie Mellon University.  The name of the
60
 * University may not be used to endorse or promote products derived
61
 * from this software without specific prior written permission.
62
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
63
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
64
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
65
 */
66
 
67
#include "lwip/opt.h"
68
 
69
#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
70
 
71
#include "netif/ppp/ppp.h"
72
#include "pppdebug.h"
73
#include "timesys.h"
74
 
75
#include "fsm.h"
76
#include "lcp.h"
77
#include "pap.h"
78
#include "chap.h"
79
#include "auth.h"
80
#include "ipcp.h"
81
 
82
#if CBCP_SUPPORT
83
#include "cbcp.h"
84
#endif /* CBCP_SUPPORT */
85
 
86
/*************************/
87
/*** LOCAL DEFINITIONS ***/
88
/*************************/
89
 
90
/* Bits in auth_pending[] */
91
#define PAP_WITHPEER    1
92
#define PAP_PEER        2
93
#define CHAP_WITHPEER   4
94
#define CHAP_PEER       8
95
 
96
 
97
/************************/
98
/*** LOCAL DATA TYPES ***/
99
/************************/
100
/* Used for storing a sequence of words.  Usually malloced. */
101
struct wordlist {
102
  struct wordlist *next;
103
  char        word[1];
104
};
105
 
106
 
107
/***********************************/
108
/*** LOCAL FUNCTION DECLARATIONS ***/
109
/***********************************/
110
extern char *crypt (const char *, const char *);
111
 
112
/* Prototypes for procedures local to this file. */
113
 
114
static void network_phase (int);
115
static void check_idle (void *);
116
static void connect_time_expired (void *);
117
#if 0
118
static int  login (char *, char *, char **, int *);
119
#endif
120
static void logout (void);
121
static int  null_login (int);
122
static int  get_pap_passwd (int, char *, char *);
123
static int  have_pap_secret (void);
124
static int  have_chap_secret (char *, char *, u32_t);
125
static int  ip_addr_check (u32_t, struct wordlist *);
126
#if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
127
static void set_allowed_addrs(int unit, struct wordlist *addrs);
128
static void free_wordlist (struct wordlist *);
129
#endif /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
130
#if CBCP_SUPPORT
131
static void callback_phase (int);
132
#endif /* CBCP_SUPPORT */
133
 
134
 
135
/******************************/
136
/*** PUBLIC DATA STRUCTURES ***/
137
/******************************/
138
 
139
 
140
/*****************************/
141
/*** LOCAL DATA STRUCTURES ***/
142
/*****************************/
143
#if PAP_SUPPORT || CHAP_SUPPORT
144
/* The name by which the peer authenticated itself to us. */
145
static char peer_authname[MAXNAMELEN];
146
#endif /* PAP_SUPPORT || CHAP_SUPPORT */
147
 
148
/* Records which authentication operations haven't completed yet. */
149
static int auth_pending[NUM_PPP];
150
 
151
/* Set if we have successfully called login() */
152
static int logged_in;
153
 
154
/* Set if we have run the /etc/ppp/auth-up script. */
155
static int did_authup;
156
 
157
/* List of addresses which the peer may use. */
158
static struct wordlist *addresses[NUM_PPP];
159
 
160
/* Number of network protocols which we have opened. */
161
static int num_np_open;
162
 
163
/* Number of network protocols which have come up. */
164
static int num_np_up;
165
 
166
#if PAP_SUPPORT || CHAP_SUPPORT
167
/* Set if we got the contents of passwd[] from the pap-secrets file. */
168
static int passwd_from_file;
169
#endif /* PAP_SUPPORT || CHAP_SUPPORT */
170
 
171
 
172
/***********************************/
173
/*** PUBLIC FUNCTION DEFINITIONS ***/
174
/***********************************/
175
/*
176
 * An Open on LCP has requested a change from Dead to Establish phase.
177
 * Do what's necessary to bring the physical layer up.
178
 */
179
void
180
link_required(int unit)
181
{
182
  LWIP_UNUSED_ARG(unit);
183
 
184
  AUTHDEBUG((LOG_INFO, "link_required: %d\n", unit));
185
}
186
 
187
/*
188
 * LCP has terminated the link; go to the Dead phase and take the
189
 * physical layer down.
190
 */
191
void
192
link_terminated(int unit)
193
{
194
  AUTHDEBUG((LOG_INFO, "link_terminated: %d\n", unit));
195
  if (lcp_phase[unit] == PHASE_DEAD) {
196
    return;
197
  }
198
  if (logged_in) {
199
    logout();
200
  }
201
  lcp_phase[unit] = PHASE_DEAD;
202
  AUTHDEBUG((LOG_NOTICE, "Connection terminated.\n"));
203
  ppp_link_terminated(unit);
204
}
205
 
206
/*
207
 * LCP has gone down; it will either die or try to re-establish.
208
 */
209
void
210
link_down(int unit)
211
{
212
  int i;
213
  struct protent *protp;
214
 
215
  AUTHDEBUG((LOG_INFO, "link_down: %d\n", unit));
216
  if (did_authup) {
217
    /* XXX Do link down processing. */
218
    did_authup = 0;
219
  }
220
  for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
221
    if (!protp->enabled_flag) {
222
      continue;
223
    }
224
    if (protp->protocol != PPP_LCP && protp->lowerdown != NULL) {
225
      (*protp->lowerdown)(unit);
226
    }
227
    if (protp->protocol < 0xC000 && protp->close != NULL) {
228
      (*protp->close)(unit, "LCP down");
229
    }
230
  }
231
  num_np_open = 0;
232
  num_np_up = 0;
233
  if (lcp_phase[unit] != PHASE_DEAD) {
234
    lcp_phase[unit] = PHASE_TERMINATE;
235
  }
236
  ppp_link_down(unit);
237
}
238
 
239
/*
240
 * The link is established.
241
 * Proceed to the Dead, Authenticate or Network phase as appropriate.
242
 */
243
void
244
link_established(int unit)
245
{
246
  int auth;
247
  int i;
248
  struct protent *protp;
249
  lcp_options *wo = &lcp_wantoptions[unit];
250
  lcp_options *go = &lcp_gotoptions[unit];
251
#if PAP_SUPPORT || CHAP_SUPPORT
252
  lcp_options *ho = &lcp_hisoptions[unit];
253
#endif /* PAP_SUPPORT || CHAP_SUPPORT */
254
 
255
  AUTHDEBUG((LOG_INFO, "link_established: %d\n", unit));
256
  /*
257
   * Tell higher-level protocols that LCP is up.
258
   */
259
  for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
260
    if (protp->protocol != PPP_LCP && protp->enabled_flag && protp->lowerup != NULL) {
261
      (*protp->lowerup)(unit);
262
    }
263
  }
264
  if (ppp_settings.auth_required && !(go->neg_chap || go->neg_upap)) {
265
    /*
266
     * We wanted the peer to authenticate itself, and it refused:
267
     * treat it as though it authenticated with PAP using a username
268
     * of "" and a password of "".  If that's not OK, boot it out.
269
     */
270
    if (!wo->neg_upap || !null_login(unit)) {
271
      AUTHDEBUG((LOG_WARNING, "peer refused to authenticate\n"));
272
      lcp_close(unit, "peer refused to authenticate");
273
      return;
274
    }
275
  }
276
 
277
  lcp_phase[unit] = PHASE_AUTHENTICATE;
278
  auth = 0;
279
#if CHAP_SUPPORT
280
  if (go->neg_chap) {
281
    chap_auth_peer(unit, ppp_settings.our_name, go->chap_mdtype);
282
    auth |= CHAP_PEER;
283
  }
284
#endif /* CHAP_SUPPORT */
285
#if PAP_SUPPORT && CHAP_SUPPORT
286
  else
287
#endif /* PAP_SUPPORT && CHAP_SUPPORT */
288
#if PAP_SUPPORT
289
  if (go->neg_upap) {
290
    upap_authpeer(unit);
291
    auth |= PAP_PEER;
292
  }
293
#endif /* PAP_SUPPORT */
294
#if CHAP_SUPPORT
295
  if (ho->neg_chap) {
296
    chap_auth_with_peer(unit, ppp_settings.user, ho->chap_mdtype);
297
    auth |= CHAP_WITHPEER;
298
  }
299
#endif /* CHAP_SUPPORT */
300
#if PAP_SUPPORT && CHAP_SUPPORT
301
  else
302
#endif /* PAP_SUPPORT && CHAP_SUPPORT */
303
#if PAP_SUPPORT
304
  if (ho->neg_upap) {
305
    if (ppp_settings.passwd[0] == 0) {
306
      passwd_from_file = 1;
307
      if (!get_pap_passwd(unit, ppp_settings.user, ppp_settings.passwd)) {
308
        AUTHDEBUG((LOG_ERR, "No secret found for PAP login\n"));
309
      }
310
    }
311
    upap_authwithpeer(unit, ppp_settings.user, ppp_settings.passwd);
312
    auth |= PAP_WITHPEER;
313
  }
314
#endif /* PAP_SUPPORT */
315
  auth_pending[unit] = auth;
316
 
317
  if (!auth) {
318
    network_phase(unit);
319
  }
320
}
321
 
322
/*
323
 * The peer has failed to authenticate himself using `protocol'.
324
 */
325
void
326
auth_peer_fail(int unit, u16_t protocol)
327
{
328
  LWIP_UNUSED_ARG(protocol);
329
 
330
  AUTHDEBUG((LOG_INFO, "auth_peer_fail: %d proto=%X\n", unit, protocol));
331
  /*
332
   * Authentication failure: take the link down
333
   */
334
  lcp_close(unit, "Authentication failed");
335
}
336
 
337
 
338
#if PAP_SUPPORT || CHAP_SUPPORT
339
/*
340
 * The peer has been successfully authenticated using `protocol'.
341
 */
342
void
343
auth_peer_success(int unit, u16_t protocol, char *name, int namelen)
344
{
345
  int pbit;
346
 
347
  AUTHDEBUG((LOG_INFO, "auth_peer_success: %d proto=%X\n", unit, protocol));
348
  switch (protocol) {
349
    case PPP_CHAP:
350
      pbit = CHAP_PEER;
351
      break;
352
    case PPP_PAP:
353
      pbit = PAP_PEER;
354
      break;
355
    default:
356
      AUTHDEBUG((LOG_WARNING, "auth_peer_success: unknown protocol %x\n", protocol));
357
      return;
358
  }
359
 
360
  /*
361
   * Save the authenticated name of the peer for later.
362
   */
363
  if (namelen > sizeof(peer_authname) - 1) {
364
    namelen = sizeof(peer_authname) - 1;
365
  }
366
  BCOPY(name, peer_authname, namelen);
367
  peer_authname[namelen] = 0;
368
 
369
  /*
370
   * If there is no more authentication still to be done,
371
   * proceed to the network (or callback) phase.
372
   */
373
  if ((auth_pending[unit] &= ~pbit) == 0) {
374
    network_phase(unit);
375
  }
376
}
377
 
378
/*
379
 * We have failed to authenticate ourselves to the peer using `protocol'.
380
 */
381
void
382
auth_withpeer_fail(int unit, u16_t protocol)
383
{
384
  int errCode = PPPERR_AUTHFAIL;
385
 
386
  LWIP_UNUSED_ARG(protocol);
387
 
388
  AUTHDEBUG((LOG_INFO, "auth_withpeer_fail: %d proto=%X\n", unit, protocol));
389
  if (passwd_from_file) {
390
    BZERO(ppp_settings.passwd, MAXSECRETLEN);
391
  }
392
  /*
393
   * XXX Warning: the unit number indicates the interface which is
394
   * not necessarily the PPP connection.  It works here as long
395
   * as we are only supporting PPP interfaces.
396
   */
397
  ppp_ioctl(unit, PPPCTLS_ERRCODE, &errCode);
398
 
399
  /*
400
   * We've failed to authenticate ourselves to our peer.
401
   * He'll probably take the link down, and there's not much
402
   * we can do except wait for that.
403
   */
404
}
405
 
406
/*
407
 * We have successfully authenticated ourselves with the peer using `protocol'.
408
 */
409
void
410
auth_withpeer_success(int unit, u16_t protocol)
411
{
412
  int pbit;
413
 
414
  AUTHDEBUG((LOG_INFO, "auth_withpeer_success: %d proto=%X\n", unit, protocol));
415
  switch (protocol) {
416
    case PPP_CHAP:
417
      pbit = CHAP_WITHPEER;
418
      break;
419
    case PPP_PAP:
420
      if (passwd_from_file) {
421
        BZERO(ppp_settings.passwd, MAXSECRETLEN);
422
      }
423
      pbit = PAP_WITHPEER;
424
      break;
425
    default:
426
      AUTHDEBUG((LOG_WARNING, "auth_peer_success: unknown protocol %x\n", protocol));
427
      pbit = 0;
428
  }
429
 
430
  /*
431
   * If there is no more authentication still being done,
432
   * proceed to the network (or callback) phase.
433
   */
434
  if ((auth_pending[unit] &= ~pbit) == 0) {
435
    network_phase(unit);
436
  }
437
}
438
#endif /* PAP_SUPPORT || CHAP_SUPPORT */
439
 
440
 
441
/*
442
 * np_up - a network protocol has come up.
443
 */
444
void
445
np_up(int unit, u16_t proto)
446
{
447
  LWIP_UNUSED_ARG(unit);
448
  LWIP_UNUSED_ARG(proto);
449
 
450
  AUTHDEBUG((LOG_INFO, "np_up: %d proto=%X\n", unit, proto));
451
  if (num_np_up == 0) {
452
    AUTHDEBUG((LOG_INFO, "np_up: maxconnect=%d idle_time_limit=%d\n",ppp_settings.maxconnect,ppp_settings.idle_time_limit));
453
    /*
454
     * At this point we consider that the link has come up successfully.
455
     */
456
    if (ppp_settings.idle_time_limit > 0) {
457
      TIMEOUT(check_idle, NULL, ppp_settings.idle_time_limit);
458
    }
459
 
460
    /*
461
     * Set a timeout to close the connection once the maximum
462
     * connect time has expired.
463
     */
464
    if (ppp_settings.maxconnect > 0) {
465
      TIMEOUT(connect_time_expired, 0, ppp_settings.maxconnect);
466
    }
467
  }
468
  ++num_np_up;
469
}
470
 
471
/*
472
 * np_down - a network protocol has gone down.
473
 */
474
void
475
np_down(int unit, u16_t proto)
476
{
477
  LWIP_UNUSED_ARG(unit);
478
  LWIP_UNUSED_ARG(proto);
479
 
480
  AUTHDEBUG((LOG_INFO, "np_down: %d proto=%X\n", unit, proto));
481
  if (--num_np_up == 0 && ppp_settings.idle_time_limit > 0) {
482
    UNTIMEOUT(check_idle, NULL);
483
  }
484
}
485
 
486
/*
487
 * np_finished - a network protocol has finished using the link.
488
 */
489
void
490
np_finished(int unit, u16_t proto)
491
{
492
  LWIP_UNUSED_ARG(unit);
493
  LWIP_UNUSED_ARG(proto);
494
 
495
  AUTHDEBUG((LOG_INFO, "np_finished: %d proto=%X\n", unit, proto));
496
  if (--num_np_open <= 0) {
497
    /* no further use for the link: shut up shop. */
498
    lcp_close(0, "No network protocols running");
499
  }
500
}
501
 
502
/*
503
 * auth_reset - called when LCP is starting negotiations to recheck
504
 * authentication options, i.e. whether we have appropriate secrets
505
 * to use for authenticating ourselves and/or the peer.
506
 */
507
void
508
auth_reset(int unit)
509
{
510
  lcp_options *go = &lcp_gotoptions[unit];
511
  lcp_options *ao = &lcp_allowoptions[0];
512
  ipcp_options *ipwo = &ipcp_wantoptions[0];
513
  u32_t remote;
514
 
515
  AUTHDEBUG((LOG_INFO, "auth_reset: %d\n", unit));
516
  ao->neg_upap = !ppp_settings.refuse_pap && (ppp_settings.passwd[0] != 0 || get_pap_passwd(unit, NULL, NULL));
517
  ao->neg_chap = !ppp_settings.refuse_chap && ppp_settings.passwd[0] != 0 /*have_chap_secret(ppp_settings.user, ppp_settings.remote_name, (u32_t)0)*/;
518
 
519
  if (go->neg_upap && !have_pap_secret()) {
520
    go->neg_upap = 0;
521
  }
522
  if (go->neg_chap) {
523
    remote = ipwo->accept_remote? 0: ipwo->hisaddr;
524
    if (!have_chap_secret(ppp_settings.remote_name, ppp_settings.our_name, remote)) {
525
      go->neg_chap = 0;
526
    }
527
  }
528
}
529
 
530
#if PAP_SUPPORT
531
/*
532
 * check_passwd - Check the user name and passwd against the PAP secrets
533
 * file.  If requested, also check against the system password database,
534
 * and login the user if OK.
535
 *
536
 * returns:
537
 *  UPAP_AUTHNAK: Authentication failed.
538
 *  UPAP_AUTHACK: Authentication succeeded.
539
 * In either case, msg points to an appropriate message.
540
 */
541
int
542
check_passwd( int unit, char *auser, int userlen, char *apasswd, int passwdlen, char **msg, int *msglen)
543
{
544
#if 1
545
  LWIP_UNUSED_ARG(unit);
546
  LWIP_UNUSED_ARG(auser);
547
  LWIP_UNUSED_ARG(userlen);
548
  LWIP_UNUSED_ARG(apasswd);
549
  LWIP_UNUSED_ARG(passwdlen);
550
  LWIP_UNUSED_ARG(msglen);
551
  *msg = (char *) 0;
552
  return UPAP_AUTHACK;     /* XXX Assume all entries OK. */
553
#else
554
  int ret = 0;
555
  struct wordlist *addrs = NULL;
556
  char passwd[256], user[256];
557
  char secret[MAXWORDLEN];
558
  static u16_t attempts = 0;
559
 
560
  /*
561
   * Make copies of apasswd and auser, then null-terminate them.
562
   */
563
  BCOPY(apasswd, passwd, passwdlen);
564
  passwd[passwdlen] = '\0';
565
  BCOPY(auser, user, userlen);
566
  user[userlen] = '\0';
567
  *msg = (char *) 0;
568
 
569
  /* XXX Validate user name and password. */
570
  ret = UPAP_AUTHACK;     /* XXX Assume all entries OK. */
571
 
572
  if (ret == UPAP_AUTHNAK) {
573
    if (*msg == (char *) 0) {
574
      *msg = "Login incorrect";
575
    }
576
    *msglen = strlen(*msg);
577
    /*
578
     * Frustrate passwd stealer programs.
579
     * Allow 10 tries, but start backing off after 3 (stolen from login).
580
     * On 10'th, drop the connection.
581
     */
582
    if (attempts++ >= 10) {
583
      AUTHDEBUG((LOG_WARNING, "%d LOGIN FAILURES BY %s\n", attempts, user));
584
      /*ppp_panic("Excess Bad Logins");*/
585
    }
586
    if (attempts > 3) {
587
      sys_msleep((attempts - 3) * 5);
588
    }
589
    if (addrs != NULL) {
590
      free_wordlist(addrs);
591
    }
592
  } else {
593
    attempts = 0; /* Reset count */
594
    if (*msg == (char *) 0) {
595
      *msg = "Login ok";
596
    }
597
    *msglen = strlen(*msg);
598
    set_allowed_addrs(unit, addrs);
599
  }
600
 
601
  BZERO(passwd, sizeof(passwd));
602
  BZERO(secret, sizeof(secret));
603
 
604
  return ret;
605
#endif
606
}
607
#endif /* PAP_SUPPORT */
608
 
609
 
610
/*
611
 * auth_ip_addr - check whether the peer is authorized to use
612
 * a given IP address.  Returns 1 if authorized, 0 otherwise.
613
 */
614
int
615
auth_ip_addr(int unit, u32_t addr)
616
{
617
  return ip_addr_check(addr, addresses[unit]);
618
}
619
 
620
/*
621
 * bad_ip_adrs - return 1 if the IP address is one we don't want
622
 * to use, such as an address in the loopback net or a multicast address.
623
 * addr is in network byte order.
624
 */
625
int
626
bad_ip_adrs(u32_t addr)
627
{
628
  addr = ntohl(addr);
629
  return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
630
      || IN_MULTICAST(addr) || IN_BADCLASS(addr);
631
}
632
 
633
 
634
#if CHAP_SUPPORT
635
/*
636
 * get_secret - open the CHAP secret file and return the secret
637
 * for authenticating the given client on the given server.
638
 * (We could be either client or server).
639
 */
640
int get_secret( int unit, char *client, char *server, char *secret, int *secret_len, int save_addrs)
641
{
642
#if 1
643
  int len;
644
  struct wordlist *addrs;
645
 
646
  LWIP_UNUSED_ARG(unit);
647
  LWIP_UNUSED_ARG(server);
648
  LWIP_UNUSED_ARG(save_addrs);
649
 
650
  addrs = NULL;
651
 
652
  if(!client || !client[0] || strcmp(client, ppp_settings.user)) {
653
    return 0;
654
  }
655
 
656
  len = strlen(ppp_settings.passwd);
657
  if (len > MAXSECRETLEN) {
658
    AUTHDEBUG((LOG_ERR, "Secret for %s on %s is too long\n", client, server));
659
    len = MAXSECRETLEN;
660
  }
661
 
662
  BCOPY(ppp_settings.passwd, secret, len);
663
  *secret_len = len;
664
 
665
  return 1;
666
#else
667
  int ret = 0, len;
668
  struct wordlist *addrs;
669
  char secbuf[MAXWORDLEN];
670
 
671
  addrs = NULL;
672
  secbuf[0] = 0;
673
 
674
  /* XXX Find secret. */
675
  if (ret < 0) {
676
    return 0;
677
  }
678
 
679
  if (save_addrs) {
680
    set_allowed_addrs(unit, addrs);
681
  }
682
 
683
  len = strlen(secbuf);
684
  if (len > MAXSECRETLEN) {
685
    AUTHDEBUG((LOG_ERR, "Secret for %s on %s is too long\n", client, server));
686
    len = MAXSECRETLEN;
687
  }
688
 
689
  BCOPY(secbuf, secret, len);
690
  BZERO(secbuf, sizeof(secbuf));
691
  *secret_len = len;
692
 
693
  return 1;
694
#endif
695
}
696
#endif /* CHAP_SUPPORT */
697
 
698
 
699
#if 0 /* UNUSED */
700
/*
701
 * auth_check_options - called to check authentication options.
702
 */
703
void
704
auth_check_options(void)
705
{
706
  lcp_options *wo = &lcp_wantoptions[0];
707
  int can_auth;
708
  ipcp_options *ipwo = &ipcp_wantoptions[0];
709
  u32_t remote;
710
 
711
  /* Default our_name to hostname, and user to our_name */
712
  if (ppp_settings.our_name[0] == 0 || ppp_settings.usehostname) {
713
      strcpy(ppp_settings.our_name, ppp_settings.hostname);
714
  }
715
 
716
  if (ppp_settings.user[0] == 0) {
717
    strcpy(ppp_settings.user, ppp_settings.our_name);
718
  }
719
 
720
  /* If authentication is required, ask peer for CHAP or PAP. */
721
  if (ppp_settings.auth_required && !wo->neg_chap && !wo->neg_upap) {
722
    wo->neg_chap = 1;
723
    wo->neg_upap = 1;
724
  }
725
 
726
  /*
727
   * Check whether we have appropriate secrets to use
728
   * to authenticate the peer.
729
   */
730
  can_auth = wo->neg_upap && have_pap_secret();
731
  if (!can_auth && wo->neg_chap) {
732
    remote = ipwo->accept_remote? 0: ipwo->hisaddr;
733
    can_auth = have_chap_secret(ppp_settings.remote_name, ppp_settings.our_name, remote);
734
  }
735
 
736
  if (ppp_settings.auth_required && !can_auth) {
737
    ppp_panic("No auth secret");
738
  }
739
}
740
#endif
741
 
742
 
743
/**********************************/
744
/*** LOCAL FUNCTION DEFINITIONS ***/
745
/**********************************/
746
/*
747
 * Proceed to the network phase.
748
 */
749
static void
750
network_phase(int unit)
751
{
752
  int i;
753
  struct protent *protp;
754
  lcp_options *go = &lcp_gotoptions[unit];
755
 
756
  /*
757
   * If the peer had to authenticate, run the auth-up script now.
758
   */
759
  if ((go->neg_chap || go->neg_upap) && !did_authup) {
760
    /* XXX Do setup for peer authentication. */
761
    did_authup = 1;
762
  }
763
 
764
#if CBCP_SUPPORT
765
  /*
766
   * If we negotiated callback, do it now.
767
   */
768
  if (go->neg_cbcp) {
769
    lcp_phase[unit] = PHASE_CALLBACK;
770
    (*cbcp_protent.open)(unit);
771
    return;
772
  }
773
#endif /* CBCP_SUPPORT */
774
 
775
  lcp_phase[unit] = PHASE_NETWORK;
776
  for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
777
    if (protp->protocol < 0xC000 && protp->enabled_flag && protp->open != NULL) {
778
      (*protp->open)(unit);
779
      if (protp->protocol != PPP_CCP) {
780
        ++num_np_open;
781
      }
782
    }
783
  }
784
 
785
  if (num_np_open == 0) {
786
    /* nothing to do */
787
    lcp_close(0, "No network protocols running");
788
  }
789
}
790
 
791
/*
792
 * check_idle - check whether the link has been idle for long
793
 * enough that we can shut it down.
794
 */
795
static void
796
check_idle(void *arg)
797
{
798
  struct ppp_idle idle;
799
  u16_t itime;
800
 
801
  LWIP_UNUSED_ARG(arg);
802
  if (!get_idle_time(0, &idle)) {
803
    return;
804
  }
805
  itime = LWIP_MIN(idle.xmit_idle, idle.recv_idle);
806
  if (itime >= ppp_settings.idle_time_limit) {
807
    /* link is idle: shut it down. */
808
    AUTHDEBUG((LOG_INFO, "Terminating connection due to lack of activity.\n"));
809
    lcp_close(0, "Link inactive");
810
  } else {
811
    TIMEOUT(check_idle, NULL, ppp_settings.idle_time_limit - itime);
812
  }
813
}
814
 
815
/*
816
 * connect_time_expired - log a message and close the connection.
817
 */
818
static void
819
connect_time_expired(void *arg)
820
{
821
  LWIP_UNUSED_ARG(arg);
822
 
823
  AUTHDEBUG((LOG_INFO, "Connect time expired\n"));
824
  lcp_close(0, "Connect time expired");   /* Close connection */
825
}
826
 
827
#if 0
828
/*
829
 * login - Check the user name and password against the system
830
 * password database, and login the user if OK.
831
 *
832
 * returns:
833
 *  UPAP_AUTHNAK: Login failed.
834
 *  UPAP_AUTHACK: Login succeeded.
835
 * In either case, msg points to an appropriate message.
836
 */
837
static int
838
login(char *user, char *passwd, char **msg, int *msglen)
839
{
840
  /* XXX Fail until we decide that we want to support logins. */
841
  return (UPAP_AUTHNAK);
842
}
843
#endif
844
 
845
/*
846
 * logout - Logout the user.
847
 */
848
static void
849
logout(void)
850
{
851
  logged_in = 0;
852
}
853
 
854
/*
855
 * null_login - Check if a username of "" and a password of "" are
856
 * acceptable, and iff so, set the list of acceptable IP addresses
857
 * and return 1.
858
 */
859
static int
860
null_login(int unit)
861
{
862
  LWIP_UNUSED_ARG(unit);
863
  /* XXX Fail until we decide that we want to support logins. */
864
  return 0;
865
}
866
 
867
/*
868
 * get_pap_passwd - get a password for authenticating ourselves with
869
 * our peer using PAP.  Returns 1 on success, 0 if no suitable password
870
 * could be found.
871
 */
872
static int
873
get_pap_passwd(int unit, char *user, char *passwd)
874
{
875
  LWIP_UNUSED_ARG(unit);
876
/* normally we would reject PAP if no password is provided,
877
   but this causes problems with some providers (like CHT in Taiwan)
878
   who incorrectly request PAP and expect a bogus/empty password, so
879
   always provide a default user/passwd of "none"/"none"
880
*/
881
  if(user) {
882
    strcpy(user, "none");
883
  }
884
  if(passwd) {
885
    strcpy(passwd, "none");
886
  }
887
  return 1;
888
}
889
 
890
/*
891
 * have_pap_secret - check whether we have a PAP file with any
892
 * secrets that we could possibly use for authenticating the peer.
893
 */
894
static int
895
have_pap_secret(void)
896
{
897
  /* XXX Fail until we set up our passwords. */
898
  return 0;
899
}
900
 
901
/*
902
 * have_chap_secret - check whether we have a CHAP file with a
903
 * secret that we could possibly use for authenticating `client'
904
 * on `server'.  Either can be the null string, meaning we don't
905
 * know the identity yet.
906
 */
907
static int
908
have_chap_secret(char *client, char *server, u32_t remote)
909
{
910
  LWIP_UNUSED_ARG(client);
911
  LWIP_UNUSED_ARG(server);
912
  LWIP_UNUSED_ARG(remote);
913
  /* XXX Fail until we set up our passwords. */
914
  return 0;
915
}
916
 
917
#if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
918
/*
919
 * set_allowed_addrs() - set the list of allowed addresses.
920
 */
921
static void
922
set_allowed_addrs(int unit, struct wordlist *addrs)
923
{
924
  if (addresses[unit] != NULL) {
925
    free_wordlist(addresses[unit]);
926
  }
927
  addresses[unit] = addrs;
928
 
929
#if 0
930
  /*
931
   * If there's only one authorized address we might as well
932
   * ask our peer for that one right away
933
   */
934
  if (addrs != NULL && addrs->next == NULL) {
935
    char *p = addrs->word;
936
    struct ipcp_options *wo = &ipcp_wantoptions[unit];
937
    u32_t a;
938
    struct hostent *hp;
939
 
940
    if (wo->hisaddr == 0 && *p != '!' && *p != '-' && strchr(p, '/') == NULL) {
941
      hp = gethostbyname(p);
942
      if (hp != NULL && hp->h_addrtype == AF_INET) {
943
        a = *(u32_t *)hp->h_addr;
944
      } else {
945
        a = inet_addr(p);
946
      }
947
      if (a != (u32_t) -1) {
948
        wo->hisaddr = a;
949
      }
950
    }
951
  }
952
#endif
953
}
954
#endif /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
955
 
956
static int
957
ip_addr_check(u32_t addr, struct wordlist *addrs)
958
{
959
  /* don't allow loopback or multicast address */
960
  if (bad_ip_adrs(addr)) {
961
    return 0;
962
  }
963
 
964
  if (addrs == NULL) {
965
    return !ppp_settings.auth_required; /* no addresses authorized */
966
  }
967
 
968
  /* XXX All other addresses allowed. */
969
  return 1;
970
}
971
 
972
#if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
973
/*
974
 * free_wordlist - release memory allocated for a wordlist.
975
 */
976
static void
977
free_wordlist(struct wordlist *wp)
978
{
979
  struct wordlist *next;
980
 
981
  while (wp != NULL) {
982
    next = wp->next;
983
    free(wp);
984
    wp = next;
985
  }
986
}
987
#endif  /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
988
 
989
#endif /* PPP_SUPPORT */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.