SAMV71 Xplained Ultra Software Package 1.3

auth.c

00001 /*****************************************************************************
00002 * auth.c - Network Authentication and Phase Control program file.
00003 *
00004 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
00005 * Copyright (c) 1997 by Global Election Systems Inc.  All rights reserved.
00006 *
00007 * The authors hereby grant permission to use, copy, modify, distribute,
00008 * and license this software and its documentation for any purpose, provided
00009 * that existing copyright notices are retained in all copies and that this
00010 * notice and the following disclaimer are included verbatim in any 
00011 * distributions. No written agreement, license, or royalty fee is required
00012 * for any of the authorized uses.
00013 *
00014 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
00015 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
00017 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00018 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00019 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00020 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00021 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024 *
00025 ******************************************************************************
00026 * REVISION HISTORY
00027 *
00028 * 03-01-01 Marc Boucher <marc@mbsi.ca>
00029 *   Ported to lwIP.
00030 * 97-12-08 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
00031 *   Ported from public pppd code.
00032 *****************************************************************************/
00033 /*
00034  * auth.c - PPP authentication and phase control.
00035  *
00036  * Copyright (c) 1993 The Australian National University.
00037  * All rights reserved.
00038  *
00039  * Redistribution and use in source and binary forms are permitted
00040  * provided that the above copyright notice and this paragraph are
00041  * duplicated in all such forms and that any documentation,
00042  * advertising materials, and other materials related to such
00043  * distribution and use acknowledge that the software was developed
00044  * by the Australian National University.  The name of the University
00045  * may not be used to endorse or promote products derived from this
00046  * software without specific prior written permission.
00047  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00048  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00049  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00050  *
00051  * Copyright (c) 1989 Carnegie Mellon University.
00052  * All rights reserved.
00053  *
00054  * Redistribution and use in source and binary forms are permitted
00055  * provided that the above copyright notice and this paragraph are
00056  * duplicated in all such forms and that any documentation,
00057  * advertising materials, and other materials related to such
00058  * distribution and use acknowledge that the software was developed
00059  * by Carnegie Mellon University.  The name of the
00060  * University may not be used to endorse or promote products derived
00061  * from this software without specific prior written permission.
00062  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00063  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00064  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00065  */
00066 
00067 #include "lwip/opt.h"
00068 
00069 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
00070 
00071 #include "ppp.h"
00072 #include "pppdebug.h"
00073 
00074 #include "fsm.h"
00075 #include "lcp.h"
00076 #include "pap.h"
00077 #include "chap.h"
00078 #include "auth.h"
00079 #include "ipcp.h"
00080 
00081 #if CBCP_SUPPORT
00082 #include "cbcp.h"
00083 #endif /* CBCP_SUPPORT */
00084 
00085 #include <string.h>
00086 
00087 /*************************/
00088 /*** LOCAL DEFINITIONS ***/
00089 /*************************/
00090 
00091 /* Bits in auth_pending[] */
00092 #define PAP_WITHPEER    1
00093 #define PAP_PEER        2
00094 #define CHAP_WITHPEER   4
00095 #define CHAP_PEER       8
00096 
00097 
00098 /************************/
00099 /*** LOCAL DATA TYPES ***/
00100 /************************/
00101 /* Used for storing a sequence of words.  Usually malloced. */
00102 struct wordlist {
00103   struct wordlist *next;
00104   char        word[1];
00105 };
00106 
00107 
00108 /***********************************/
00109 /*** LOCAL FUNCTION DECLARATIONS ***/
00110 /***********************************/
00111 extern char *crypt (const char *, const char *);
00112 
00113 /* Prototypes for procedures local to this file. */
00114 
00115 static void network_phase (int);
00116 static void check_idle (void *);
00117 static void connect_time_expired (void *);
00118 #if 0
00119 static int  login (char *, char *, char **, int *);
00120 #endif
00121 static void logout (void);
00122 static int  null_login (int);
00123 static int  get_pap_passwd (int, char *, char *);
00124 static int  have_pap_secret (void);
00125 static int  have_chap_secret (char *, char *, u32_t);
00126 static int  ip_addr_check (u32_t, struct wordlist *);
00127 #if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
00128 static void set_allowed_addrs(int unit, struct wordlist *addrs);
00129 static void free_wordlist (struct wordlist *);
00130 #endif /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
00131 #if CBCP_SUPPORT
00132 static void callback_phase (int);
00133 #endif /* CBCP_SUPPORT */
00134 
00135 
00136 /******************************/
00137 /*** PUBLIC DATA STRUCTURES ***/
00138 /******************************/
00139 
00140 
00141 /*****************************/
00142 /*** LOCAL DATA STRUCTURES ***/
00143 /*****************************/
00144 #if PAP_SUPPORT || CHAP_SUPPORT
00145 /* The name by which the peer authenticated itself to us. */
00146 static char peer_authname[MAXNAMELEN];
00147 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
00148 
00149 /* Records which authentication operations haven't completed yet. */
00150 static int auth_pending[NUM_PPP];
00151 
00152 /* Set if we have successfully called login() */
00153 static int logged_in;
00154 
00155 /* Set if we have run the /etc/ppp/auth-up script. */
00156 static int did_authup;
00157 
00158 /* List of addresses which the peer may use. */
00159 static struct wordlist *addresses[NUM_PPP];
00160 
00161 /* Number of network protocols which we have opened. */
00162 static int num_np_open;
00163 
00164 /* Number of network protocols which have come up. */
00165 static int num_np_up;
00166 
00167 #if PAP_SUPPORT || CHAP_SUPPORT
00168 /* Set if we got the contents of passwd[] from the pap-secrets file. */
00169 static int passwd_from_file;
00170 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
00171 
00172 
00173 /***********************************/
00174 /*** PUBLIC FUNCTION DEFINITIONS ***/
00175 /***********************************/
00176 /*
00177  * An Open on LCP has requested a change from Dead to Establish phase.
00178  * Do what's necessary to bring the physical layer up.
00179  */
00180 void
00181 link_required(int unit)
00182 {
00183   LWIP_UNUSED_ARG(unit);
00184 
00185   AUTHDEBUG((LOG_INFO, "link_required: %d\n", unit));
00186 }
00187 
00188 /*
00189  * LCP has terminated the link; go to the Dead phase and take the
00190  * physical layer down.
00191  */
00192 void
00193 link_terminated(int unit)
00194 {
00195   AUTHDEBUG((LOG_INFO, "link_terminated: %d\n", unit));
00196   if (lcp_phase[unit] == PHASE_DEAD) {
00197     return;
00198   }
00199   if (logged_in) {
00200     logout();
00201   }
00202   lcp_phase[unit] = PHASE_DEAD;
00203   AUTHDEBUG((LOG_NOTICE, "Connection terminated.\n"));
00204   pppLinkTerminated(unit);
00205 }
00206 
00207 /*
00208  * LCP has gone down; it will either die or try to re-establish.
00209  */
00210 void
00211 link_down(int unit)
00212 {
00213   int i;
00214   struct protent *protp;
00215   
00216   AUTHDEBUG((LOG_INFO, "link_down: %d\n", unit));
00217   if (did_authup) {
00218     /* XXX Do link down processing. */
00219     did_authup = 0;
00220   }
00221   for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
00222     if (!protp->enabled_flag) {
00223       continue;
00224     }
00225     if (protp->protocol != PPP_LCP && protp->lowerdown != NULL) {
00226       (*protp->lowerdown)(unit);
00227     }
00228     if (protp->protocol < 0xC000 && protp->close != NULL) {
00229       (*protp->close)(unit, "LCP down");
00230     }
00231   }
00232   num_np_open = 0;
00233   num_np_up = 0;
00234   if (lcp_phase[unit] != PHASE_DEAD) {
00235     lcp_phase[unit] = PHASE_TERMINATE;
00236   }
00237   pppLinkDown(unit);
00238 }
00239 
00240 /*
00241  * The link is established.
00242  * Proceed to the Dead, Authenticate or Network phase as appropriate.
00243  */
00244 void
00245 link_established(int unit)
00246 {
00247   int auth;
00248   int i;
00249   struct protent *protp;
00250   lcp_options *wo = &lcp_wantoptions[unit];
00251   lcp_options *go = &lcp_gotoptions[unit];
00252 #if PAP_SUPPORT || CHAP_SUPPORT
00253   lcp_options *ho = &lcp_hisoptions[unit];
00254 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
00255 
00256   AUTHDEBUG((LOG_INFO, "link_established: %d\n", unit));
00257   /*
00258    * Tell higher-level protocols that LCP is up.
00259    */
00260   for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
00261     if (protp->protocol != PPP_LCP && protp->enabled_flag && protp->lowerup != NULL) {
00262       (*protp->lowerup)(unit);
00263     }
00264   }
00265   if (ppp_settings.auth_required && !(go->neg_chap || go->neg_upap)) {
00266     /*
00267      * We wanted the peer to authenticate itself, and it refused:
00268      * treat it as though it authenticated with PAP using a username
00269      * of "" and a password of "".  If that's not OK, boot it out.
00270      */
00271     if (!wo->neg_upap || !null_login(unit)) {
00272       AUTHDEBUG((LOG_WARNING, "peer refused to authenticate\n"));
00273       lcp_close(unit, "peer refused to authenticate");
00274       return;
00275     }
00276   }
00277     
00278   lcp_phase[unit] = PHASE_AUTHENTICATE;
00279   auth = 0;
00280 #if CHAP_SUPPORT
00281   if (go->neg_chap) {
00282     ChapAuthPeer(unit, ppp_settings.our_name, go->chap_mdtype);
00283     auth |= CHAP_PEER;
00284   } 
00285 #endif /* CHAP_SUPPORT */
00286 #if PAP_SUPPORT && CHAP_SUPPORT
00287   else
00288 #endif /* PAP_SUPPORT && CHAP_SUPPORT */
00289 #if PAP_SUPPORT
00290   if (go->neg_upap) {
00291     upap_authpeer(unit);
00292     auth |= PAP_PEER;
00293   }
00294 #endif /* PAP_SUPPORT */
00295 #if CHAP_SUPPORT
00296   if (ho->neg_chap) {
00297     ChapAuthWithPeer(unit, ppp_settings.user, ho->chap_mdtype);
00298     auth |= CHAP_WITHPEER;
00299   }
00300 #endif /* CHAP_SUPPORT */
00301 #if PAP_SUPPORT && CHAP_SUPPORT
00302   else
00303 #endif /* PAP_SUPPORT && CHAP_SUPPORT */
00304 #if PAP_SUPPORT
00305   if (ho->neg_upap) {
00306     if (ppp_settings.passwd[0] == 0) {
00307       passwd_from_file = 1;
00308       if (!get_pap_passwd(unit, ppp_settings.user, ppp_settings.passwd)) {
00309         AUTHDEBUG((LOG_ERR, "No secret found for PAP login\n"));
00310       }
00311     }
00312     upap_authwithpeer(unit, ppp_settings.user, ppp_settings.passwd);
00313     auth |= PAP_WITHPEER;
00314   }
00315 #endif /* PAP_SUPPORT */
00316   auth_pending[unit] = auth;
00317 
00318   if (!auth) {
00319     network_phase(unit);
00320   }
00321 }
00322 
00323 /*
00324  * The peer has failed to authenticate himself using `protocol'.
00325  */
00326 void
00327 auth_peer_fail(int unit, u16_t protocol)
00328 {
00329   LWIP_UNUSED_ARG(protocol);
00330 
00331   AUTHDEBUG((LOG_INFO, "auth_peer_fail: %d proto=%X\n", unit, protocol));
00332   /*
00333    * Authentication failure: take the link down
00334    */
00335   lcp_close(unit, "Authentication failed");
00336 }
00337 
00338 
00339 #if PAP_SUPPORT || CHAP_SUPPORT
00340 /*
00341  * The peer has been successfully authenticated using `protocol'.
00342  */
00343 void
00344 auth_peer_success(int unit, u16_t protocol, char *name, int namelen)
00345 {
00346   int pbit;
00347   
00348   AUTHDEBUG((LOG_INFO, "auth_peer_success: %d proto=%X\n", unit, protocol));
00349   switch (protocol) {
00350     case PPP_CHAP:
00351       pbit = CHAP_PEER;
00352       break;
00353     case PPP_PAP:
00354       pbit = PAP_PEER;
00355       break;
00356     default:
00357       AUTHDEBUG((LOG_WARNING, "auth_peer_success: unknown protocol %x\n", protocol));
00358       return;
00359   }
00360   
00361   /*
00362    * Save the authenticated name of the peer for later.
00363    */
00364   if (namelen > sizeof(peer_authname) - 1) {
00365     namelen = sizeof(peer_authname) - 1;
00366   }
00367   BCOPY(name, peer_authname, namelen);
00368   peer_authname[namelen] = 0;
00369   
00370   /*
00371    * If there is no more authentication still to be done,
00372    * proceed to the network (or callback) phase.
00373    */
00374   if ((auth_pending[unit] &= ~pbit) == 0) {
00375     network_phase(unit);
00376   }
00377 }
00378 
00379 /*
00380  * We have failed to authenticate ourselves to the peer using `protocol'.
00381  */
00382 void
00383 auth_withpeer_fail(int unit, u16_t protocol)
00384 {
00385   int errCode = PPPERR_AUTHFAIL;
00386   
00387   LWIP_UNUSED_ARG(protocol);
00388 
00389   AUTHDEBUG((LOG_INFO, "auth_withpeer_fail: %d proto=%X\n", unit, protocol));
00390   if (passwd_from_file) {
00391     BZERO(ppp_settings.passwd, MAXSECRETLEN);
00392   }
00393   /* 
00394    * XXX Warning: the unit number indicates the interface which is
00395    * not necessarily the PPP connection.  It works here as long
00396    * as we are only supporting PPP interfaces.
00397    */
00398   pppIOCtl(unit, PPPCTLS_ERRCODE, &errCode);
00399 
00400   /*
00401    * We've failed to authenticate ourselves to our peer.
00402    * He'll probably take the link down, and there's not much
00403    * we can do except wait for that.
00404    */
00405 }
00406 
00407 /*
00408  * We have successfully authenticated ourselves with the peer using `protocol'.
00409  */
00410 void
00411 auth_withpeer_success(int unit, u16_t protocol)
00412 {
00413   int pbit;
00414   
00415   AUTHDEBUG((LOG_INFO, "auth_withpeer_success: %d proto=%X\n", unit, protocol));
00416   switch (protocol) {
00417     case PPP_CHAP:
00418       pbit = CHAP_WITHPEER;
00419       break;
00420     case PPP_PAP:
00421       if (passwd_from_file) {
00422         BZERO(ppp_settings.passwd, MAXSECRETLEN);
00423       }
00424       pbit = PAP_WITHPEER;
00425       break;
00426     default:
00427       AUTHDEBUG((LOG_WARNING, "auth_peer_success: unknown protocol %x\n", protocol));
00428       pbit = 0;
00429   }
00430   
00431   /*
00432    * If there is no more authentication still being done,
00433    * proceed to the network (or callback) phase.
00434    */
00435   if ((auth_pending[unit] &= ~pbit) == 0) {
00436     network_phase(unit);
00437   }
00438 }
00439 #endif /* PAP_SUPPORT || CHAP_SUPPORT */
00440 
00441 
00442 /*
00443  * np_up - a network protocol has come up.
00444  */
00445 void
00446 np_up(int unit, u16_t proto)
00447 {
00448   LWIP_UNUSED_ARG(unit);
00449   LWIP_UNUSED_ARG(proto);
00450 
00451   AUTHDEBUG((LOG_INFO, "np_up: %d proto=%X\n", unit, proto));
00452   if (num_np_up == 0) {
00453     AUTHDEBUG((LOG_INFO, "np_up: maxconnect=%d idle_time_limit=%d\n",ppp_settings.maxconnect,ppp_settings.idle_time_limit));
00454     /*
00455      * At this point we consider that the link has come up successfully.
00456      */
00457     if (ppp_settings.idle_time_limit > 0) {
00458       TIMEOUT(check_idle, NULL, ppp_settings.idle_time_limit);
00459     }
00460     
00461     /*
00462      * Set a timeout to close the connection once the maximum
00463      * connect time has expired.
00464      */
00465     if (ppp_settings.maxconnect > 0) {
00466       TIMEOUT(connect_time_expired, 0, ppp_settings.maxconnect);
00467     }
00468   }
00469   ++num_np_up;
00470 }
00471 
00472 /*
00473  * np_down - a network protocol has gone down.
00474  */
00475 void
00476 np_down(int unit, u16_t proto)
00477 {
00478   LWIP_UNUSED_ARG(unit);
00479   LWIP_UNUSED_ARG(proto);
00480 
00481   AUTHDEBUG((LOG_INFO, "np_down: %d proto=%X\n", unit, proto));
00482   if (--num_np_up == 0 && ppp_settings.idle_time_limit > 0) {
00483     UNTIMEOUT(check_idle, NULL);
00484   }
00485 }
00486 
00487 /*
00488  * np_finished - a network protocol has finished using the link.
00489  */
00490 void
00491 np_finished(int unit, u16_t proto)
00492 {
00493   LWIP_UNUSED_ARG(unit);
00494   LWIP_UNUSED_ARG(proto);
00495 
00496   AUTHDEBUG((LOG_INFO, "np_finished: %d proto=%X\n", unit, proto));
00497   if (--num_np_open <= 0) {
00498     /* no further use for the link: shut up shop. */
00499     lcp_close(0, "No network protocols running");
00500   }
00501 }
00502 
00503 /*
00504  * auth_reset - called when LCP is starting negotiations to recheck
00505  * authentication options, i.e. whether we have appropriate secrets
00506  * to use for authenticating ourselves and/or the peer.
00507  */
00508 void
00509 auth_reset(int unit)
00510 {
00511   lcp_options *go = &lcp_gotoptions[unit];
00512   lcp_options *ao = &lcp_allowoptions[0];
00513   ipcp_options *ipwo = &ipcp_wantoptions[0];
00514   u32_t remote;
00515 
00516   AUTHDEBUG((LOG_INFO, "auth_reset: %d\n", unit));
00517   ao->neg_upap = !ppp_settings.refuse_pap && (ppp_settings.passwd[0] != 0 || get_pap_passwd(unit, NULL, NULL));
00518   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)*/;
00519 
00520   if (go->neg_upap && !have_pap_secret()) {
00521     go->neg_upap = 0;
00522   }
00523   if (go->neg_chap) {
00524     remote = ipwo->accept_remote? 0: ipwo->hisaddr;
00525     if (!have_chap_secret(ppp_settings.remote_name, ppp_settings.our_name, remote)) {
00526       go->neg_chap = 0;
00527     }
00528   }
00529 }
00530 
00531 #if PAP_SUPPORT
00532 /*
00533  * check_passwd - Check the user name and passwd against the PAP secrets
00534  * file.  If requested, also check against the system password database,
00535  * and login the user if OK.
00536  *
00537  * returns:
00538  *  UPAP_AUTHNAK: Authentication failed.
00539  *  UPAP_AUTHACK: Authentication succeeded.
00540  * In either case, msg points to an appropriate message.
00541  */
00542 int
00543 check_passwd( int unit, char *auser, int userlen, char *apasswd, int passwdlen, char **msg, int *msglen)
00544 {
00545 #if 1
00546   LWIP_UNUSED_ARG(unit);
00547   LWIP_UNUSED_ARG(auser);
00548   LWIP_UNUSED_ARG(userlen);
00549   LWIP_UNUSED_ARG(apasswd);
00550   LWIP_UNUSED_ARG(passwdlen);
00551   LWIP_UNUSED_ARG(msglen);
00552   *msg = (char *) 0;
00553   return UPAP_AUTHACK;     /* XXX Assume all entries OK. */
00554 #else
00555   int ret = 0;
00556   struct wordlist *addrs = NULL;
00557   char passwd[256], user[256];
00558   char secret[MAXWORDLEN];
00559   static u_short attempts = 0;
00560   
00561   /*
00562    * Make copies of apasswd and auser, then null-terminate them.
00563    */
00564   BCOPY(apasswd, passwd, passwdlen);
00565   passwd[passwdlen] = '\0';
00566   BCOPY(auser, user, userlen);
00567   user[userlen] = '\0';
00568   *msg = (char *) 0;
00569 
00570   /* XXX Validate user name and password. */
00571   ret = UPAP_AUTHACK;     /* XXX Assume all entries OK. */
00572       
00573   if (ret == UPAP_AUTHNAK) {
00574     if (*msg == (char *) 0) {
00575       *msg = "Login incorrect";
00576     }
00577     *msglen = strlen(*msg);
00578     /*
00579      * Frustrate passwd stealer programs.
00580      * Allow 10 tries, but start backing off after 3 (stolen from login).
00581      * On 10'th, drop the connection.
00582      */
00583     if (attempts++ >= 10) {
00584       AUTHDEBUG((LOG_WARNING, "%d LOGIN FAILURES BY %s\n", attempts, user));
00585       /*ppp_panic("Excess Bad Logins");*/
00586     }
00587     if (attempts > 3) {
00588       sys_msleep((attempts - 3) * 5);
00589     }
00590     if (addrs != NULL) {
00591       free_wordlist(addrs);
00592     }
00593   } else {
00594     attempts = 0; /* Reset count */
00595     if (*msg == (char *) 0) {
00596       *msg = "Login ok";
00597     }
00598     *msglen = strlen(*msg);
00599     set_allowed_addrs(unit, addrs);
00600   }
00601 
00602   BZERO(passwd, sizeof(passwd));
00603   BZERO(secret, sizeof(secret));
00604 
00605   return ret;
00606 #endif
00607 }
00608 #endif /* PAP_SUPPORT */
00609 
00610 
00611 /*
00612  * auth_ip_addr - check whether the peer is authorized to use
00613  * a given IP address.  Returns 1 if authorized, 0 otherwise.
00614  */
00615 int
00616 auth_ip_addr(int unit, u32_t addr)
00617 {
00618   return ip_addr_check(addr, addresses[unit]);
00619 }
00620 
00621 /*
00622  * bad_ip_adrs - return 1 if the IP address is one we don't want
00623  * to use, such as an address in the loopback net or a multicast address.
00624  * addr is in network byte order.
00625  */
00626 int
00627 bad_ip_adrs(u32_t addr)
00628 {
00629   addr = ntohl(addr);
00630   return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
00631       || IN_MULTICAST(addr) || IN_BADCLASS(addr);
00632 }
00633 
00634 
00635 #if CHAP_SUPPORT
00636 /*
00637  * get_secret - open the CHAP secret file and return the secret
00638  * for authenticating the given client on the given server.
00639  * (We could be either client or server).
00640  */
00641 int get_secret( int unit, char *client, char *server, char *secret, int *secret_len, int save_addrs)
00642 {
00643 #if 1
00644   int len;
00645   struct wordlist *addrs;
00646 
00647   LWIP_UNUSED_ARG(unit);
00648   LWIP_UNUSED_ARG(server);
00649   LWIP_UNUSED_ARG(save_addrs);
00650 
00651   addrs = NULL;
00652 
00653   if(!client || !client[0] || strcmp(client, ppp_settings.user)) {
00654     return 0;
00655   }
00656 
00657   len = strlen(ppp_settings.passwd);
00658   if (len > MAXSECRETLEN) {
00659     AUTHDEBUG((LOG_ERR, "Secret for %s on %s is too long\n", client, server));
00660     len = MAXSECRETLEN;
00661   }
00662 
00663   BCOPY(ppp_settings.passwd, secret, len);
00664   *secret_len = len;
00665 
00666   return 1;
00667 #else
00668   int ret = 0, len;
00669   struct wordlist *addrs;
00670   char secbuf[MAXWORDLEN];
00671   
00672   addrs = NULL;
00673   secbuf[0] = 0;
00674 
00675   /* XXX Find secret. */
00676   if (ret < 0) {
00677     return 0;
00678   }
00679 
00680   if (save_addrs) {
00681     set_allowed_addrs(unit, addrs);
00682   }
00683 
00684   len = strlen(secbuf);
00685   if (len > MAXSECRETLEN) {
00686     AUTHDEBUG((LOG_ERR, "Secret for %s on %s is too long\n", client, server));
00687     len = MAXSECRETLEN;
00688   }
00689 
00690   BCOPY(secbuf, secret, len);
00691   BZERO(secbuf, sizeof(secbuf));
00692   *secret_len = len;
00693 
00694   return 1;
00695 #endif
00696 }
00697 #endif /* CHAP_SUPPORT */
00698 
00699 
00700 #if 0 /* UNUSED */
00701 /*
00702  * auth_check_options - called to check authentication options.
00703  */
00704 void
00705 auth_check_options(void)
00706 {
00707   lcp_options *wo = &lcp_wantoptions[0];
00708   int can_auth;
00709   ipcp_options *ipwo = &ipcp_wantoptions[0];
00710   u32_t remote;
00711 
00712   /* Default our_name to hostname, and user to our_name */
00713   if (ppp_settings.our_name[0] == 0 || ppp_settings.usehostname) {
00714       strcpy(ppp_settings.our_name, ppp_settings.hostname);
00715   }
00716 
00717   if (ppp_settings.user[0] == 0) {
00718     strcpy(ppp_settings.user, ppp_settings.our_name);
00719   }
00720 
00721   /* If authentication is required, ask peer for CHAP or PAP. */
00722   if (ppp_settings.auth_required && !wo->neg_chap && !wo->neg_upap) {
00723     wo->neg_chap = 1;
00724     wo->neg_upap = 1;
00725   }
00726   
00727   /*
00728    * Check whether we have appropriate secrets to use
00729    * to authenticate the peer.
00730    */
00731   can_auth = wo->neg_upap && have_pap_secret();
00732   if (!can_auth && wo->neg_chap) {
00733     remote = ipwo->accept_remote? 0: ipwo->hisaddr;
00734     can_auth = have_chap_secret(ppp_settings.remote_name, ppp_settings.our_name, remote);
00735   }
00736 
00737   if (ppp_settings.auth_required && !can_auth) {
00738     ppp_panic("No auth secret");
00739   }
00740 }
00741 #endif
00742 
00743 
00744 /**********************************/
00745 /*** LOCAL FUNCTION DEFINITIONS ***/
00746 /**********************************/
00747 /*
00748  * Proceed to the network phase.
00749  */
00750 static void
00751 network_phase(int unit)
00752 {
00753   int i;
00754   struct protent *protp;
00755   lcp_options *go = &lcp_gotoptions[unit];
00756   
00757   /*
00758    * If the peer had to authenticate, run the auth-up script now.
00759    */
00760   if ((go->neg_chap || go->neg_upap) && !did_authup) {
00761     /* XXX Do setup for peer authentication. */
00762     did_authup = 1;
00763   }
00764 
00765 #if CBCP_SUPPORT
00766   /*
00767    * If we negotiated callback, do it now.
00768    */
00769   if (go->neg_cbcp) {
00770     lcp_phase[unit] = PHASE_CALLBACK;
00771     (*cbcp_protent.open)(unit);
00772     return;
00773   }
00774 #endif /* CBCP_SUPPORT */
00775 
00776   lcp_phase[unit] = PHASE_NETWORK;
00777   for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
00778     if (protp->protocol < 0xC000 && protp->enabled_flag && protp->open != NULL) {
00779       (*protp->open)(unit);
00780       if (protp->protocol != PPP_CCP) {
00781         ++num_np_open;
00782       }
00783     }
00784   }
00785 
00786   if (num_np_open == 0) {
00787     /* nothing to do */
00788     lcp_close(0, "No network protocols running");
00789   }
00790 }
00791 
00792 /*
00793  * check_idle - check whether the link has been idle for long
00794  * enough that we can shut it down.
00795  */
00796 static void
00797 check_idle(void *arg)
00798 {
00799   struct ppp_idle idle;
00800   u_short itime;
00801   
00802   LWIP_UNUSED_ARG(arg);
00803   if (!get_idle_time(0, &idle)) {
00804     return;
00805   }
00806   itime = LWIP_MIN(idle.xmit_idle, idle.recv_idle);
00807   if (itime >= ppp_settings.idle_time_limit) {
00808     /* link is idle: shut it down. */
00809     AUTHDEBUG((LOG_INFO, "Terminating connection due to lack of activity.\n"));
00810     lcp_close(0, "Link inactive");
00811   } else {
00812     TIMEOUT(check_idle, NULL, ppp_settings.idle_time_limit - itime);
00813   }
00814 }
00815 
00816 /*
00817  * connect_time_expired - log a message and close the connection.
00818  */
00819 static void
00820 connect_time_expired(void *arg)
00821 {
00822   LWIP_UNUSED_ARG(arg);
00823 
00824   AUTHDEBUG((LOG_INFO, "Connect time expired\n"));
00825   lcp_close(0, "Connect time expired");   /* Close connection */
00826 }
00827 
00828 #if 0
00829 /*
00830  * login - Check the user name and password against the system
00831  * password database, and login the user if OK.
00832  *
00833  * returns:
00834  *  UPAP_AUTHNAK: Login failed.
00835  *  UPAP_AUTHACK: Login succeeded.
00836  * In either case, msg points to an appropriate message.
00837  */
00838 static int
00839 login(char *user, char *passwd, char **msg, int *msglen)
00840 {
00841   /* XXX Fail until we decide that we want to support logins. */
00842   return (UPAP_AUTHNAK);
00843 }
00844 #endif
00845 
00846 /*
00847  * logout - Logout the user.
00848  */
00849 static void
00850 logout(void)
00851 {
00852   logged_in = 0;
00853 }
00854 
00855 /*
00856  * null_login - Check if a username of "" and a password of "" are
00857  * acceptable, and iff so, set the list of acceptable IP addresses
00858  * and return 1.
00859  */
00860 static int
00861 null_login(int unit)
00862 {
00863   LWIP_UNUSED_ARG(unit);
00864   /* XXX Fail until we decide that we want to support logins. */
00865   return 0;
00866 }
00867 
00868 /*
00869  * get_pap_passwd - get a password for authenticating ourselves with
00870  * our peer using PAP.  Returns 1 on success, 0 if no suitable password
00871  * could be found.
00872  */
00873 static int
00874 get_pap_passwd(int unit, char *user, char *passwd)
00875 {
00876   LWIP_UNUSED_ARG(unit);
00877 /* normally we would reject PAP if no password is provided,
00878    but this causes problems with some providers (like CHT in Taiwan)
00879    who incorrectly request PAP and expect a bogus/empty password, so
00880    always provide a default user/passwd of "none"/"none"
00881 */
00882   if(user) {
00883     strcpy(user, "none");
00884   }
00885   if(passwd) {
00886     strcpy(passwd, "none");
00887   }
00888   return 1;
00889 }
00890 
00891 /*
00892  * have_pap_secret - check whether we have a PAP file with any
00893  * secrets that we could possibly use for authenticating the peer.
00894  */
00895 static int
00896 have_pap_secret(void)
00897 {
00898   /* XXX Fail until we set up our passwords. */
00899   return 0;
00900 }
00901 
00902 /*
00903  * have_chap_secret - check whether we have a CHAP file with a
00904  * secret that we could possibly use for authenticating `client'
00905  * on `server'.  Either can be the null string, meaning we don't
00906  * know the identity yet.
00907  */
00908 static int
00909 have_chap_secret(char *client, char *server, u32_t remote)
00910 {
00911   LWIP_UNUSED_ARG(client);
00912   LWIP_UNUSED_ARG(server);
00913   LWIP_UNUSED_ARG(remote);
00914   /* XXX Fail until we set up our passwords. */
00915   return 0;
00916 }
00917 
00918 #if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
00919 /*
00920  * set_allowed_addrs() - set the list of allowed addresses.
00921  */
00922 static void
00923 set_allowed_addrs(int unit, struct wordlist *addrs)
00924 {
00925   if (addresses[unit] != NULL) {
00926     free_wordlist(addresses[unit]);
00927   }
00928   addresses[unit] = addrs;
00929 
00930 #if 0
00931   /*
00932    * If there's only one authorized address we might as well
00933    * ask our peer for that one right away
00934    */
00935   if (addrs != NULL && addrs->next == NULL) {
00936     char *p = addrs->word;
00937     struct ipcp_options *wo = &ipcp_wantoptions[unit];
00938     u32_t a;
00939     struct hostent *hp;
00940     
00941     if (wo->hisaddr == 0 && *p != '!' && *p != '-' && strchr(p, '/') == NULL) {
00942       hp = gethostbyname(p);
00943       if (hp != NULL && hp->h_addrtype == AF_INET) {
00944         a = *(u32_t *)hp->h_addr;
00945       } else {
00946         a = inet_addr(p);
00947       }
00948       if (a != (u32_t) -1) {
00949         wo->hisaddr = a;
00950       }
00951     }
00952   }
00953 #endif
00954 }
00955 #endif /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
00956 
00957 static int
00958 ip_addr_check(u32_t addr, struct wordlist *addrs)
00959 {
00960   /* don't allow loopback or multicast address */
00961   if (bad_ip_adrs(addr)) {
00962     return 0;
00963   }
00964 
00965   if (addrs == NULL) {
00966     return !ppp_settings.auth_required; /* no addresses authorized */
00967   }
00968 
00969   /* XXX All other addresses allowed. */
00970   return 1;
00971 }
00972 
00973 #if 0 /* PAP_SUPPORT || CHAP_SUPPORT */
00974 /*
00975  * free_wordlist - release memory allocated for a wordlist.
00976  */
00977 static void
00978 free_wordlist(struct wordlist *wp)
00979 {
00980   struct wordlist *next;
00981   
00982   while (wp != NULL) {
00983     next = wp->next;
00984     free(wp);
00985     wp = next;
00986   }
00987 }
00988 #endif  /* 0 */ /* PAP_SUPPORT || CHAP_SUPPORT */
00989 
00990 #endif /* PPP_SUPPORT */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines