__aeabi_ldivmod: fix sign logic
[minix.git] / lib / liblwip / netif / ppp / auth.c
blobc3c49d22a2547b3ac626304f7da5fd2992e2af0d
1 /*****************************************************************************
2 * auth.c - Network Authentication and Phase Control program file.
4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
5 * Copyright (c) 1997 by Global Election Systems Inc. All rights reserved.
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.
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.
25 ******************************************************************************
26 * REVISION HISTORY
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 *****************************************************************************/
34 * auth.c - PPP authentication and phase control.
36 * Copyright (c) 1993 The Australian National University.
37 * All rights reserved.
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.
51 * Copyright (c) 1989 Carnegie Mellon University.
52 * All rights reserved.
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.
67 #include "lwip/opt.h"
69 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
71 #include "ppp.h"
72 #include "pppdebug.h"
74 #include "fsm.h"
75 #include "lcp.h"
76 #include "pap.h"
77 #include "chap.h"
78 #include "auth.h"
79 #include "ipcp.h"
81 #if CBCP_SUPPORT
82 #include "cbcp.h"
83 #endif /* CBCP_SUPPORT */
85 #include "lwip/inet.h"
87 #include <string.h>
89 #if 0 /* UNUSED */
90 /* Bits in scan_authfile return value */
91 #define NONWILD_SERVER 1
92 #define NONWILD_CLIENT 2
94 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
95 #endif /* UNUSED */
97 #if PAP_SUPPORT || CHAP_SUPPORT
98 /* The name by which the peer authenticated itself to us. */
99 static char peer_authname[MAXNAMELEN];
100 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
102 /* Records which authentication operations haven't completed yet. */
103 static int auth_pending[NUM_PPP];
105 /* Set if we have successfully called plogin() */
106 static int logged_in;
108 /* Set if we have run the /etc/ppp/auth-up script. */
109 static int did_authup; /* @todo, we don't need this in lwip*/
111 /* List of addresses which the peer may use. */
112 static struct wordlist *addresses[NUM_PPP];
114 #if 0 /* UNUSED */
115 /* Wordlist giving addresses which the peer may use
116 without authenticating itself. */
117 static struct wordlist *noauth_addrs;
119 /* Extra options to apply, from the secrets file entry for the peer. */
120 static struct wordlist *extra_options;
121 #endif /* UNUSED */
123 /* Number of network protocols which we have opened. */
124 static int num_np_open;
126 /* Number of network protocols which have come up. */
127 static int num_np_up;
129 #if PAP_SUPPORT || CHAP_SUPPORT
130 /* Set if we got the contents of passwd[] from the pap-secrets file. */
131 static int passwd_from_file;
132 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
134 #if 0 /* UNUSED */
135 /* Set if we require authentication only because we have a default route. */
136 static bool default_auth;
138 /* Hook to enable a plugin to control the idle time limit */
139 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
141 /* Hook for a plugin to say whether we can possibly authenticate any peer */
142 int (*pap_check_hook) __P((void)) = NULL;
144 /* Hook for a plugin to check the PAP user and password */
145 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
146 struct wordlist **paddrs,
147 struct wordlist **popts)) = NULL;
149 /* Hook for a plugin to know about the PAP user logout */
150 void (*pap_logout_hook) __P((void)) = NULL;
152 /* Hook for a plugin to get the PAP password for authenticating us */
153 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
156 * This is used to ensure that we don't start an auth-up/down
157 * script while one is already running.
159 enum script_state {
160 s_down,
161 s_up
164 static enum script_state auth_state = s_down;
165 static enum script_state auth_script_state = s_down;
166 static pid_t auth_script_pid = 0;
169 * Option variables.
170 * lwip: some of these are present in the ppp_settings structure
172 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
173 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
174 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
175 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
176 bool usehostname = 0; /* Use hostname for our_name */
177 bool auth_required = 0; /* Always require authentication from peer */
178 bool allow_any_ip = 0; /* Allow peer to use any IP address */
179 bool explicit_remote = 0; /* User specified explicit remote name */
180 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
182 #endif /* UNUSED */
184 /* Bits in auth_pending[] */
185 #define PAP_WITHPEER 1
186 #define PAP_PEER 2
187 #define CHAP_WITHPEER 4
188 #define CHAP_PEER 8
190 /* @todo, move this somewhere */
191 /* Used for storing a sequence of words. Usually malloced. */
192 struct wordlist {
193 struct wordlist *next;
194 char word[1];
198 extern char *crypt (const char *, const char *);
200 /* Prototypes for procedures local to this file. */
202 static void network_phase (int);
203 static void check_idle (void *);
204 static void connect_time_expired (void *);
205 #if 0
206 static int plogin (char *, char *, char **, int *);
207 #endif
208 static void plogout (void);
209 static int null_login (int);
210 static int get_pap_passwd (int, char *, char *);
211 static int have_pap_secret (void);
212 static int have_chap_secret (char *, char *, u32_t);
213 static int ip_addr_check (u32_t, struct wordlist *);
215 #if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
216 static int scan_authfile (FILE *, char *, char *, char *,
217 struct wordlist **, struct wordlist **,
218 char *);
219 static void free_wordlist (struct wordlist *);
220 static void auth_script (char *);
221 static void auth_script_done (void *);
222 static void set_allowed_addrs (int unit, struct wordlist *addrs);
223 static int some_ip_ok (struct wordlist *);
224 static int setupapfile (char **);
225 static int privgroup (char **);
226 static int set_noauth_addr (char **);
227 static void check_access (FILE *, char *);
228 #endif /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
230 #if 0 /* UNUSED */
232 * Authentication-related options.
234 option_t auth_options[] = {
235 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
236 "Require PAP authentication from peer", 1, &auth_required },
237 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
238 "Require PAP authentication from peer", 1, &auth_required },
239 { "refuse-pap", o_bool, &refuse_pap,
240 "Don't agree to auth to peer with PAP", 1 },
241 { "-pap", o_bool, &refuse_pap,
242 "Don't allow PAP authentication with peer", 1 },
243 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
244 "Require CHAP authentication from peer", 1, &auth_required },
245 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
246 "Require CHAP authentication from peer", 1, &auth_required },
247 { "refuse-chap", o_bool, &refuse_chap,
248 "Don't agree to auth to peer with CHAP", 1 },
249 { "-chap", o_bool, &refuse_chap,
250 "Don't allow CHAP authentication with peer", 1 },
251 { "name", o_string, our_name,
252 "Set local name for authentication",
253 OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
254 { "user", o_string, user,
255 "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
256 { "usehostname", o_bool, &usehostname,
257 "Must use hostname for authentication", 1 },
258 { "remotename", o_string, remote_name,
259 "Set remote name for authentication", OPT_STATIC,
260 &explicit_remote, MAXNAMELEN },
261 { "auth", o_bool, &auth_required,
262 "Require authentication from peer", 1 },
263 { "noauth", o_bool, &auth_required,
264 "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
265 { "login", o_bool, &uselogin,
266 "Use system password database for PAP", 1 },
267 { "papcrypt", o_bool, &cryptpap,
268 "PAP passwords are encrypted", 1 },
269 { "+ua", o_special, (void *)setupapfile,
270 "Get PAP user and password from file" },
271 { "password", o_string, passwd,
272 "Password for authenticating us to the peer", OPT_STATIC,
273 NULL, MAXSECRETLEN },
274 { "privgroup", o_special, (void *)privgroup,
275 "Allow group members to use privileged options", OPT_PRIV },
276 { "allow-ip", o_special, (void *)set_noauth_addr,
277 "Set IP address(es) which can be used without authentication",
278 OPT_PRIV },
279 { NULL }
281 #endif /* UNUSED */
282 #if 0 /* UNUSED */
284 * setupapfile - specifies UPAP info for authenticating with peer.
286 static int
287 setupapfile(char **argv)
289 FILE * ufile;
290 int l;
292 lcp_allowoptions[0].neg_upap = 1;
294 /* open user info file */
295 seteuid(getuid());
296 ufile = fopen(*argv, "r");
297 seteuid(0);
298 if (ufile == NULL) {
299 option_error("unable to open user login data file %s", *argv);
300 return 0;
302 check_access(ufile, *argv);
304 /* get username */
305 if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
306 || fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
307 option_error("unable to read user login data file %s", *argv);
308 return 0;
310 fclose(ufile);
312 /* get rid of newlines */
313 l = strlen(user);
314 if (l > 0 && user[l-1] == '\n')
315 user[l-1] = 0;
316 l = strlen(passwd);
317 if (l > 0 && passwd[l-1] == '\n')
318 passwd[l-1] = 0;
320 return (1);
322 #endif /* UNUSED */
324 #if 0 /* UNUSED */
326 * privgroup - allow members of the group to have privileged access.
328 static int
329 privgroup(char **argv)
331 struct group *g;
332 int i;
334 g = getgrnam(*argv);
335 if (g == 0) {
336 option_error("group %s is unknown", *argv);
337 return 0;
339 for (i = 0; i < ngroups; ++i) {
340 if (groups[i] == g->gr_gid) {
341 privileged = 1;
342 break;
345 return 1;
347 #endif
349 #if 0 /* UNUSED */
351 * set_noauth_addr - set address(es) that can be used without authentication.
352 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
354 static int
355 set_noauth_addr(char **argv)
357 char *addr = *argv;
358 int l = strlen(addr);
359 struct wordlist *wp;
361 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l + 1);
362 if (wp == NULL)
363 novm("allow-ip argument");
364 wp->word = (char *) (wp + 1);
365 wp->next = noauth_addrs;
366 BCOPY(addr, wp->word, l);
367 noauth_addrs = wp;
368 return 1;
370 #endif /* UNUSED */
373 * An Open on LCP has requested a change from Dead to Establish phase.
374 * Do what's necessary to bring the physical layer up.
376 void
377 link_required(int unit)
379 LWIP_UNUSED_ARG(unit);
381 AUTHDEBUG(LOG_INFO, ("link_required: %d\n", unit));
385 * LCP has terminated the link; go to the Dead phase and take the
386 * physical layer down.
388 void
389 link_terminated(int unit)
391 AUTHDEBUG(LOG_INFO, ("link_terminated: %d\n", unit));
392 if (lcp_phase[unit] == PHASE_DEAD) {
393 return;
395 if (logged_in) {
396 plogout();
398 lcp_phase[unit] = PHASE_DEAD;
399 AUTHDEBUG(LOG_NOTICE, ("Connection terminated.\n"));
400 pppLinkTerminated(unit);
404 * LCP has gone down; it will either die or try to re-establish.
406 void
407 link_down(int unit)
409 int i;
410 struct protent *protp;
412 AUTHDEBUG(LOG_INFO, ("link_down: %d\n", unit));
414 if (did_authup) {
415 /* XXX Do link down processing. */
416 did_authup = 0;
418 for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
419 if (!protp->enabled_flag) {
420 continue;
422 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL) {
423 (*protp->lowerdown)(unit);
425 if (protp->protocol < 0xC000 && protp->close != NULL) {
426 (*protp->close)(unit, "LCP down");
429 num_np_open = 0; /* number of network protocols we have opened */
430 num_np_up = 0; /* Number of network protocols which have come up */
432 if (lcp_phase[unit] != PHASE_DEAD) {
433 lcp_phase[unit] = PHASE_TERMINATE;
435 pppLinkDown(unit);
439 * The link is established.
440 * Proceed to the Dead, Authenticate or Network phase as appropriate.
442 void
443 link_established(int unit)
445 int auth;
446 int i;
447 struct protent *protp;
448 lcp_options *wo = &lcp_wantoptions[unit];
449 lcp_options *go = &lcp_gotoptions[unit];
450 #if PAP_SUPPORT || CHAP_SUPPORT
451 lcp_options *ho = &lcp_hisoptions[unit];
452 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
454 AUTHDEBUG(LOG_INFO, ("link_established: unit %d; Lowering up all protocols...\n", unit));
456 * Tell higher-level protocols that LCP is up.
458 for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
459 if (protp->protocol != PPP_LCP && protp->enabled_flag && protp->lowerup != NULL) {
460 (*protp->lowerup)(unit);
463 if (ppp_settings.auth_required && !(go->neg_chap || go->neg_upap)) {
465 * We wanted the peer to authenticate itself, and it refused:
466 * treat it as though it authenticated with PAP using a username
467 * of "" and a password of "". If that's not OK, boot it out.
469 if (!wo->neg_upap || !null_login(unit)) {
470 AUTHDEBUG(LOG_WARNING, ("peer refused to authenticate\n"));
471 lcp_close(unit, "peer refused to authenticate");
472 return;
476 lcp_phase[unit] = PHASE_AUTHENTICATE;
477 auth = 0;
478 #if CHAP_SUPPORT
479 if (go->neg_chap) {
480 ChapAuthPeer(unit, ppp_settings.our_name, go->chap_mdtype);
481 auth |= CHAP_PEER;
483 #endif /* CHAP_SUPPORT */
484 #if PAP_SUPPORT && CHAP_SUPPORT
485 else
486 #endif /* PAP_SUPPORT && CHAP_SUPPORT */
487 #if PAP_SUPPORT
488 if (go->neg_upap) {
489 upap_authpeer(unit);
490 auth |= PAP_PEER;
492 #endif /* PAP_SUPPORT */
493 #if CHAP_SUPPORT
494 if (ho->neg_chap) {
495 ChapAuthWithPeer(unit, ppp_settings.user, ho->chap_mdtype);
496 auth |= CHAP_WITHPEER;
498 #endif /* CHAP_SUPPORT */
499 #if PAP_SUPPORT && CHAP_SUPPORT
500 else
501 #endif /* PAP_SUPPORT && CHAP_SUPPORT */
502 #if PAP_SUPPORT
503 if (ho->neg_upap) {
504 if (ppp_settings.passwd[0] == 0) {
505 passwd_from_file = 1;
506 if (!get_pap_passwd(unit, ppp_settings.user, ppp_settings.passwd)) {
507 AUTHDEBUG(LOG_ERR, ("No secret found for PAP login\n"));
510 upap_authwithpeer(unit, ppp_settings.user, ppp_settings.passwd);
511 auth |= PAP_WITHPEER;
513 #endif /* PAP_SUPPORT */
514 auth_pending[unit] = auth;
516 if (!auth) {
517 network_phase(unit);
522 * Proceed to the network phase.
524 static void
525 network_phase(int unit)
527 int i;
528 struct protent *protp;
529 lcp_options *go = &lcp_gotoptions[unit];
532 * If the peer had to authenticate, run the auth-up script now.
534 if ((go->neg_chap || go->neg_upap) && !did_authup) {
535 /* XXX Do setup for peer authentication. */
536 did_authup = 1;
539 #if CBCP_SUPPORT
541 * If we negotiated callback, do it now.
543 if (go->neg_cbcp) {
544 lcp_phase[unit] = PHASE_CALLBACK;
545 (*cbcp_protent.open)(unit);
546 return;
548 #endif /* CBCP_SUPPORT */
550 lcp_phase[unit] = PHASE_NETWORK;
551 for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
552 if (protp->protocol < 0xC000 && protp->enabled_flag && protp->open != NULL) {
553 (*protp->open)(unit);
554 if (protp->protocol != PPP_CCP) {
555 ++num_np_open;
560 if (num_np_open == 0) {
561 /* nothing to do */
562 lcp_close(0, "No network protocols running");
565 /* @todo: add void start_networks(void) here (pppd 2.3.11) */
568 * The peer has failed to authenticate himself using `protocol'.
570 void
571 auth_peer_fail(int unit, u16_t protocol)
573 LWIP_UNUSED_ARG(protocol);
575 AUTHDEBUG(LOG_INFO, ("auth_peer_fail: %d proto=%X\n", unit, protocol));
577 * Authentication failure: take the link down
579 lcp_close(unit, "Authentication failed");
583 #if PAP_SUPPORT || CHAP_SUPPORT
585 * The peer has been successfully authenticated using `protocol'.
587 void
588 auth_peer_success(int unit, u16_t protocol, char *name, int namelen)
590 int pbit;
592 AUTHDEBUG(LOG_INFO, ("auth_peer_success: %d proto=%X\n", unit, protocol));
593 switch (protocol) {
594 case PPP_CHAP:
595 pbit = CHAP_PEER;
596 break;
597 case PPP_PAP:
598 pbit = PAP_PEER;
599 break;
600 default:
601 AUTHDEBUG(LOG_WARNING, ("auth_peer_success: unknown protocol %x\n", protocol));
602 return;
606 * Save the authenticated name of the peer for later.
608 if (namelen > (int)sizeof(peer_authname) - 1) {
609 namelen = sizeof(peer_authname) - 1;
611 BCOPY(name, peer_authname, namelen);
612 peer_authname[namelen] = 0;
615 * If there is no more authentication still to be done,
616 * proceed to the network (or callback) phase.
618 if ((auth_pending[unit] &= ~pbit) == 0) {
619 network_phase(unit);
624 * We have failed to authenticate ourselves to the peer using `protocol'.
626 void
627 auth_withpeer_fail(int unit, u16_t protocol)
629 int errCode = PPPERR_AUTHFAIL;
631 LWIP_UNUSED_ARG(protocol);
633 AUTHDEBUG(LOG_INFO, ("auth_withpeer_fail: %d proto=%X\n", unit, protocol));
634 if (passwd_from_file) {
635 BZERO(ppp_settings.passwd, MAXSECRETLEN);
639 * We've failed to authenticate ourselves to our peer.
640 * He'll probably take the link down, and there's not much
641 * we can do except wait for that.
643 pppIOCtl(unit, PPPCTLS_ERRCODE, &errCode);
644 lcp_close(unit, "Failed to authenticate ourselves to peer");
648 * We have successfully authenticated ourselves with the peer using `protocol'.
650 void
651 auth_withpeer_success(int unit, u16_t protocol)
653 int pbit;
655 AUTHDEBUG(LOG_INFO, ("auth_withpeer_success: %d proto=%X\n", unit, protocol));
656 switch (protocol) {
657 case PPP_CHAP:
658 pbit = CHAP_WITHPEER;
659 break;
660 case PPP_PAP:
661 if (passwd_from_file) {
662 BZERO(ppp_settings.passwd, MAXSECRETLEN);
664 pbit = PAP_WITHPEER;
665 break;
666 default:
667 AUTHDEBUG(LOG_WARNING, ("auth_peer_success: unknown protocol %x\n", protocol));
668 pbit = 0;
672 * If there is no more authentication still being done,
673 * proceed to the network (or callback) phase.
675 if ((auth_pending[unit] &= ~pbit) == 0) {
676 network_phase(unit);
679 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
683 * np_up - a network protocol has come up.
685 void
686 np_up(int unit, u16_t proto)
688 LWIP_UNUSED_ARG(unit);
689 LWIP_UNUSED_ARG(proto);
691 AUTHDEBUG(LOG_INFO, ("np_up: %d proto=%X\n", unit, proto));
692 if (num_np_up == 0) {
693 AUTHDEBUG(LOG_INFO, ("np_up: maxconnect=%d idle_time_limit=%d\n",ppp_settings.maxconnect,ppp_settings.idle_time_limit));
695 * At this point we consider that the link has come up successfully.
697 if (ppp_settings.idle_time_limit > 0) {
698 TIMEOUT(check_idle, NULL, ppp_settings.idle_time_limit);
702 * Set a timeout to close the connection once the maximum
703 * connect time has expired.
705 if (ppp_settings.maxconnect > 0) {
706 TIMEOUT(connect_time_expired, 0, ppp_settings.maxconnect);
709 ++num_np_up;
713 * np_down - a network protocol has gone down.
715 void
716 np_down(int unit, u16_t proto)
718 LWIP_UNUSED_ARG(unit);
719 LWIP_UNUSED_ARG(proto);
721 AUTHDEBUG(LOG_INFO, ("np_down: %d proto=%X\n", unit, proto));
722 if (--num_np_up == 0 && ppp_settings.idle_time_limit > 0) {
723 UNTIMEOUT(check_idle, NULL);
728 * np_finished - a network protocol has finished using the link.
730 void
731 np_finished(int unit, u16_t proto)
733 LWIP_UNUSED_ARG(unit);
734 LWIP_UNUSED_ARG(proto);
736 AUTHDEBUG(LOG_INFO, ("np_finished: %d proto=%X\n", unit, proto));
737 if (--num_np_open <= 0) {
738 /* no further use for the link: shut up shop. */
739 lcp_close(0, "No network protocols running");
744 * check_idle - check whether the link has been idle for long
745 * enough that we can shut it down.
747 static void
748 check_idle(void *arg)
750 struct ppp_idle idle;
751 u_short itime;
753 LWIP_UNUSED_ARG(arg);
754 if (!get_idle_time(0, &idle)) {
755 return;
757 itime = LWIP_MIN(idle.xmit_idle, idle.recv_idle);
758 if (itime >= ppp_settings.idle_time_limit) {
759 /* link is idle: shut it down. */
760 AUTHDEBUG(LOG_INFO, ("Terminating connection due to lack of activity.\n"));
761 lcp_close(0, "Link inactive");
762 } else {
763 TIMEOUT(check_idle, NULL, ppp_settings.idle_time_limit - itime);
768 * connect_time_expired - log a message and close the connection.
770 static void
771 connect_time_expired(void *arg)
773 LWIP_UNUSED_ARG(arg);
775 AUTHDEBUG(LOG_INFO, ("Connect time expired\n"));
776 lcp_close(0, "Connect time expired"); /* Close connection */
779 #if 0 /* UNUSED */
781 * auth_check_options - called to check authentication options.
783 void
784 auth_check_options(void)
786 lcp_options *wo = &lcp_wantoptions[0];
787 int can_auth;
788 ipcp_options *ipwo = &ipcp_wantoptions[0];
789 u32_t remote;
791 /* Default our_name to hostname, and user to our_name */
792 if (ppp_settings.our_name[0] == 0 || ppp_settings.usehostname) {
793 strcpy(ppp_settings.our_name, ppp_settings.hostname);
796 if (ppp_settings.user[0] == 0) {
797 strcpy(ppp_settings.user, ppp_settings.our_name);
800 /* If authentication is required, ask peer for CHAP or PAP. */
801 if (ppp_settings.auth_required && !wo->neg_chap && !wo->neg_upap) {
802 wo->neg_chap = 1;
803 wo->neg_upap = 1;
807 * Check whether we have appropriate secrets to use
808 * to authenticate the peer.
810 can_auth = wo->neg_upap && have_pap_secret();
811 if (!can_auth && wo->neg_chap) {
812 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
813 can_auth = have_chap_secret(ppp_settings.remote_name, ppp_settings.our_name, remote);
816 if (ppp_settings.auth_required && !can_auth) {
817 ppp_panic("No auth secret");
820 #endif /* UNUSED */
823 * auth_reset - called when LCP is starting negotiations to recheck
824 * authentication options, i.e. whether we have appropriate secrets
825 * to use for authenticating ourselves and/or the peer.
827 void
828 auth_reset(int unit)
830 lcp_options *go = &lcp_gotoptions[unit];
831 lcp_options *ao = &lcp_allowoptions[0];
832 ipcp_options *ipwo = &ipcp_wantoptions[0];
833 u32_t remote;
835 AUTHDEBUG(LOG_INFO, ("auth_reset: %d\n", unit));
836 ao->neg_upap = !ppp_settings.refuse_pap && (ppp_settings.passwd[0] != 0 || get_pap_passwd(unit, NULL, NULL));
837 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)*/;
839 if (go->neg_upap && !have_pap_secret()) {
840 go->neg_upap = 0;
842 if (go->neg_chap) {
843 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
844 if (!have_chap_secret(ppp_settings.remote_name, ppp_settings.our_name, remote)) {
845 go->neg_chap = 0;
850 #if PAP_SUPPORT
852 * check_passwd - Check the user name and passwd against the PAP secrets
853 * file. If requested, also check against the system password database,
854 * and login the user if OK.
856 * returns:
857 * UPAP_AUTHNAK: Authentication failed.
858 * UPAP_AUTHACK: Authentication succeeded.
859 * In either case, msg points to an appropriate message.
861 u_char
862 check_passwd( int unit, char *auser, int userlen, char *apasswd, int passwdlen, char **msg, int *msglen)
864 #if 1 /* XXX Assume all entries OK. */
865 LWIP_UNUSED_ARG(unit);
866 LWIP_UNUSED_ARG(auser);
867 LWIP_UNUSED_ARG(userlen);
868 LWIP_UNUSED_ARG(apasswd);
869 LWIP_UNUSED_ARG(passwdlen);
870 LWIP_UNUSED_ARG(msglen);
871 *msg = (char *) 0;
872 return UPAP_AUTHACK; /* XXX Assume all entries OK. */
873 #else
874 u_char ret = 0;
875 struct wordlist *addrs = NULL;
876 char passwd[256], user[256];
877 char secret[MAXWORDLEN];
878 static u_short attempts = 0;
881 * Make copies of apasswd and auser, then null-terminate them.
883 BCOPY(apasswd, passwd, passwdlen);
884 passwd[passwdlen] = '\0';
885 BCOPY(auser, user, userlen);
886 user[userlen] = '\0';
887 *msg = (char *) 0;
889 /* XXX Validate user name and password. */
890 ret = UPAP_AUTHACK; /* XXX Assume all entries OK. */
892 if (ret == UPAP_AUTHNAK) {
893 if (*msg == (char *) 0) {
894 *msg = "Login incorrect";
896 *msglen = strlen(*msg);
898 * Frustrate passwd stealer programs.
899 * Allow 10 tries, but start backing off after 3 (stolen from login).
900 * On 10'th, drop the connection.
902 if (attempts++ >= 10) {
903 AUTHDEBUG(LOG_WARNING, ("%d LOGIN FAILURES BY %s\n", attempts, user));
904 /*ppp_panic("Excess Bad Logins");*/
906 if (attempts > 3) {
907 /* @todo: this was sleep(), i.e. seconds, not milliseconds
908 * I don't think we really need this in lwIP - we would block tcpip_thread!
910 /*sys_msleep((attempts - 3) * 5);*/
912 if (addrs != NULL) {
913 free_wordlist(addrs);
915 } else {
916 attempts = 0; /* Reset count */
917 if (*msg == (char *) 0) {
918 *msg = "Login ok";
920 *msglen = strlen(*msg);
921 set_allowed_addrs(unit, addrs);
924 BZERO(passwd, sizeof(passwd));
925 BZERO(secret, sizeof(secret));
927 return ret;
928 #endif
930 #endif /* PAP_SUPPORT */
932 #if 0 /* UNUSED */
934 * This function is needed for PAM.
937 #ifdef USE_PAM
939 /* lwip does not support PAM*/
941 #endif /* USE_PAM */
943 #endif /* UNUSED */
946 #if 0 /* UNUSED */
948 * plogin - Check the user name and password against the system
949 * password database, and login the user if OK.
951 * returns:
952 * UPAP_AUTHNAK: Login failed.
953 * UPAP_AUTHACK: Login succeeded.
954 * In either case, msg points to an appropriate message.
956 static int
957 plogin(char *user, char *passwd, char **msg, int *msglen)
960 LWIP_UNUSED_ARG(user);
961 LWIP_UNUSED_ARG(passwd);
962 LWIP_UNUSED_ARG(msg);
963 LWIP_UNUSED_ARG(msglen);
966 /* The new lines are here align the file when
967 * compared against the pppd 2.3.11 code */
984 /* XXX Fail until we decide that we want to support logins. */
985 return (UPAP_AUTHNAK);
987 #endif
992 * plogout - Logout the user.
994 static void
995 plogout(void)
997 logged_in = 0;
1001 * null_login - Check if a username of "" and a password of "" are
1002 * acceptable, and iff so, set the list of acceptable IP addresses
1003 * and return 1.
1005 static int
1006 null_login(int unit)
1008 LWIP_UNUSED_ARG(unit);
1009 /* XXX Fail until we decide that we want to support logins. */
1010 return 0;
1015 * get_pap_passwd - get a password for authenticating ourselves with
1016 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1017 * could be found.
1019 static int
1020 get_pap_passwd(int unit, char *user, char *passwd)
1022 LWIP_UNUSED_ARG(unit);
1023 /* normally we would reject PAP if no password is provided,
1024 but this causes problems with some providers (like CHT in Taiwan)
1025 who incorrectly request PAP and expect a bogus/empty password, so
1026 always provide a default user/passwd of "none"/"none"
1028 @todo: This should be configured by the user, instead of being hardcoded here!
1030 if(user) {
1031 strcpy(user, "none");
1033 if(passwd) {
1034 strcpy(passwd, "none");
1036 return 1;
1040 * have_pap_secret - check whether we have a PAP file with any
1041 * secrets that we could possibly use for authenticating the peer.
1043 static int
1044 have_pap_secret(void)
1046 /* XXX Fail until we set up our passwords. */
1047 return 0;
1051 * have_chap_secret - check whether we have a CHAP file with a
1052 * secret that we could possibly use for authenticating `client'
1053 * on `server'. Either can be the null string, meaning we don't
1054 * know the identity yet.
1056 static int
1057 have_chap_secret(char *client, char *server, u32_t remote)
1059 LWIP_UNUSED_ARG(client);
1060 LWIP_UNUSED_ARG(server);
1061 LWIP_UNUSED_ARG(remote);
1063 /* XXX Fail until we set up our passwords. */
1064 return 0;
1066 #if CHAP_SUPPORT
1069 * get_secret - open the CHAP secret file and return the secret
1070 * for authenticating the given client on the given server.
1071 * (We could be either client or server).
1074 get_secret(int unit, char *client, char *server, char *secret, int *secret_len, int save_addrs)
1076 #if 1
1077 int len;
1078 struct wordlist *addrs;
1080 LWIP_UNUSED_ARG(unit);
1081 LWIP_UNUSED_ARG(server);
1082 LWIP_UNUSED_ARG(save_addrs);
1084 addrs = NULL;
1086 if(!client || !client[0] || strcmp(client, ppp_settings.user)) {
1087 return 0;
1090 len = (int)strlen(ppp_settings.passwd);
1091 if (len > MAXSECRETLEN) {
1092 AUTHDEBUG(LOG_ERR, ("Secret for %s on %s is too long\n", client, server));
1093 len = MAXSECRETLEN;
1096 BCOPY(ppp_settings.passwd, secret, len);
1097 *secret_len = len;
1099 return 1;
1100 #else
1101 int ret = 0, len;
1102 struct wordlist *addrs;
1103 char secbuf[MAXWORDLEN];
1105 addrs = NULL;
1106 secbuf[0] = 0;
1108 /* XXX Find secret. */
1109 if (ret < 0) {
1110 return 0;
1113 if (save_addrs) {
1114 set_allowed_addrs(unit, addrs);
1117 len = strlen(secbuf);
1118 if (len > MAXSECRETLEN) {
1119 AUTHDEBUG(LOG_ERR, ("Secret for %s on %s is too long\n", client, server));
1120 len = MAXSECRETLEN;
1123 BCOPY(secbuf, secret, len);
1124 BZERO(secbuf, sizeof(secbuf));
1125 *secret_len = len;
1127 return 1;
1128 #endif
1130 #endif /* CHAP_SUPPORT */
1133 #if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
1135 * set_allowed_addrs() - set the list of allowed addresses.
1137 static void
1138 set_allowed_addrs(int unit, struct wordlist *addrs)
1140 if (addresses[unit] != NULL) {
1141 free_wordlist(addresses[unit]);
1143 addresses[unit] = addrs;
1145 #if 0
1147 * If there's only one authorized address we might as well
1148 * ask our peer for that one right away
1150 if (addrs != NULL && addrs->next == NULL) {
1151 char *p = addrs->word;
1152 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1153 u32_t a;
1154 struct hostent *hp;
1156 if (wo->hisaddr == 0 && *p != '!' && *p != '-' && strchr(p, '/') == NULL) {
1157 hp = gethostbyname(p);
1158 if (hp != NULL && hp->h_addrtype == AF_INET) {
1159 a = *(u32_t *)hp->h_addr;
1160 } else {
1161 a = inet_addr(p);
1163 if (a != (u32_t) -1) {
1164 wo->hisaddr = a;
1168 #endif
1170 #endif /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
1173 * auth_ip_addr - check whether the peer is authorized to use
1174 * a given IP address. Returns 1 if authorized, 0 otherwise.
1177 auth_ip_addr(int unit, u32_t addr)
1179 return ip_addr_check(addr, addresses[unit]);
1182 static int /* @todo: integrate this funtion into auth_ip_addr()*/
1183 ip_addr_check(u32_t addr, struct wordlist *addrs)
1185 /* don't allow loopback or multicast address */
1186 if (bad_ip_adrs(addr)) {
1187 return 0;
1190 if (addrs == NULL) {
1191 return !ppp_settings.auth_required; /* no addresses authorized */
1194 /* XXX All other addresses allowed. */
1195 return 1;
1199 * bad_ip_adrs - return 1 if the IP address is one we don't want
1200 * to use, such as an address in the loopback net or a multicast address.
1201 * addr is in network byte order.
1204 bad_ip_adrs(u32_t addr)
1206 addr = ntohl(addr);
1207 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1208 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1211 #if 0 /* UNUSED */ /* PAP_SUPPORT || CHAP_SUPPORT */
1213 * some_ip_ok - check a wordlist to see if it authorizes any
1214 * IP address(es).
1216 static int
1217 some_ip_ok(struct wordlist *addrs)
1219 for (; addrs != 0; addrs = addrs->next) {
1220 if (addrs->word[0] == '-')
1221 break;
1222 if (addrs->word[0] != '!')
1223 return 1; /* some IP address is allowed */
1225 return 0;
1229 * check_access - complain if a secret file has too-liberal permissions.
1231 static void
1232 check_access(FILE *f, char *filename)
1234 struct stat sbuf;
1236 if (fstat(fileno(f), &sbuf) < 0) {
1237 warn("cannot stat secret file %s: %m", filename);
1238 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1239 warn("Warning - secret file %s has world and/or group access",
1240 filename);
1246 * scan_authfile - Scan an authorization file for a secret suitable
1247 * for authenticating `client' on `server'. The return value is -1
1248 * if no secret is found, otherwise >= 0. The return value has
1249 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1250 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1251 * Any following words on the line up to a "--" (i.e. address authorization
1252 * info) are placed in a wordlist and returned in *addrs. Any
1253 * following words (extra options) are placed in a wordlist and
1254 * returned in *opts.
1255 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1257 static int
1258 scan_authfile(FILE *f, char *client, char *server, char *secret, struct wordlist **addrs, struct wordlist **opts, char *filename)
1260 /* We do not (currently) need this in lwip */
1261 return 0; /* dummy */
1264 * free_wordlist - release memory allocated for a wordlist.
1266 static void
1267 free_wordlist(struct wordlist *wp)
1269 struct wordlist *next;
1271 while (wp != NULL) {
1272 next = wp->next;
1273 free(wp);
1274 wp = next;
1279 * auth_script_done - called when the auth-up or auth-down script
1280 * has finished.
1282 static void
1283 auth_script_done(void *arg)
1285 auth_script_pid = 0;
1286 switch (auth_script_state) {
1287 case s_up:
1288 if (auth_state == s_down) {
1289 auth_script_state = s_down;
1290 auth_script(_PATH_AUTHDOWN);
1292 break;
1293 case s_down:
1294 if (auth_state == s_up) {
1295 auth_script_state = s_up;
1296 auth_script(_PATH_AUTHUP);
1298 break;
1303 * auth_script - execute a script with arguments
1304 * interface-name peer-name real-user tty speed
1306 static void
1307 auth_script(char *script)
1309 char strspeed[32];
1310 struct passwd *pw;
1311 char struid[32];
1312 char *user_name;
1313 char *argv[8];
1315 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1316 user_name = pw->pw_name;
1317 else {
1318 slprintf(struid, sizeof(struid), "%d", getuid());
1319 user_name = struid;
1321 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1323 argv[0] = script;
1324 argv[1] = ifname;
1325 argv[2] = peer_authname;
1326 argv[3] = user_name;
1327 argv[4] = devnam;
1328 argv[5] = strspeed;
1329 argv[6] = NULL;
1331 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);
1333 #endif /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
1334 #endif /* PPP_SUPPORT */