xref: /freebsd/usr.sbin/ppp/auth.c (revision 455aabc3f8ec34f01e0c38a52775d8fad9b7b55c)
11ae349f5Scvs2svn /*
21ae349f5Scvs2svn  *			PPP Secret Key Module
31ae349f5Scvs2svn  *
41ae349f5Scvs2svn  *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
51ae349f5Scvs2svn  *
61ae349f5Scvs2svn  *   Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd.
71ae349f5Scvs2svn  *
81ae349f5Scvs2svn  * Redistribution and use in source and binary forms are permitted
91ae349f5Scvs2svn  * provided that the above copyright notice and this paragraph are
101ae349f5Scvs2svn  * duplicated in all such forms and that any documentation,
111ae349f5Scvs2svn  * advertising materials, and other materials related to such
121ae349f5Scvs2svn  * distribution and use acknowledge that the software was developed
131ae349f5Scvs2svn  * by the Internet Initiative Japan, Inc.  The name of the
141ae349f5Scvs2svn  * IIJ may not be used to endorse or promote products derived
151ae349f5Scvs2svn  * from this software without specific prior written permission.
161ae349f5Scvs2svn  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
171ae349f5Scvs2svn  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
181ae349f5Scvs2svn  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
191ae349f5Scvs2svn  *
20455aabc3SBrian Somers  * $Id: auth.c,v 1.27.2.5 1998/02/02 19:33:33 brian Exp $
211ae349f5Scvs2svn  *
221ae349f5Scvs2svn  *	TODO:
231ae349f5Scvs2svn  *		o Implement check against with registered IP addresses.
241ae349f5Scvs2svn  */
251ae349f5Scvs2svn #include <sys/param.h>
261ae349f5Scvs2svn #include <netinet/in.h>
271ae349f5Scvs2svn 
2863b73463SBrian Somers #include <assert.h>
291ae349f5Scvs2svn #include <stdio.h>
301ae349f5Scvs2svn #include <string.h>
316140ba11SBrian Somers #include <termios.h>
321ae349f5Scvs2svn #include <unistd.h>
331ae349f5Scvs2svn 
341ae349f5Scvs2svn #include "command.h"
351ae349f5Scvs2svn #include "mbuf.h"
361ae349f5Scvs2svn #include "defs.h"
371ae349f5Scvs2svn #include "timer.h"
381ae349f5Scvs2svn #include "fsm.h"
3929e275ceSBrian Somers #include "iplist.h"
4029e275ceSBrian Somers #include "throughput.h"
411ae349f5Scvs2svn #include "ipcp.h"
421ae349f5Scvs2svn #include "loadalias.h"
431ae349f5Scvs2svn #include "vars.h"
441ae349f5Scvs2svn #include "auth.h"
451ae349f5Scvs2svn #include "chat.h"
461ae349f5Scvs2svn #include "systems.h"
476140ba11SBrian Somers #include "lcp.h"
486140ba11SBrian Somers #include "hdlc.h"
496140ba11SBrian Somers #include "async.h"
506140ba11SBrian Somers #include "link.h"
518c07a7b2SBrian Somers #include "physical.h"
52455aabc3SBrian Somers #include "lcpproto.h"
53455aabc3SBrian Somers 
54455aabc3SBrian Somers const char *
55455aabc3SBrian Somers Auth2Nam(u_short auth)
56455aabc3SBrian Somers {
57455aabc3SBrian Somers   switch (auth) {
58455aabc3SBrian Somers   case PROTO_PAP:
59455aabc3SBrian Somers     return "PAP";
60455aabc3SBrian Somers   case PROTO_CHAP:
61455aabc3SBrian Somers     return "CHAP";
62455aabc3SBrian Somers   case 0:
63455aabc3SBrian Somers     return "none";
64455aabc3SBrian Somers   }
65455aabc3SBrian Somers   return "unknown";
66455aabc3SBrian Somers }
671ae349f5Scvs2svn 
681ae349f5Scvs2svn void
691ae349f5Scvs2svn LocalAuthInit()
701ae349f5Scvs2svn {
711ae349f5Scvs2svn   if (!(mode&MODE_DAEMON))
721ae349f5Scvs2svn     /* We're allowed in interactive mode */
731ae349f5Scvs2svn     VarLocalAuth = LOCAL_AUTH;
741ae349f5Scvs2svn   else if (VarHaveLocalAuthKey)
751ae349f5Scvs2svn     VarLocalAuth = *VarLocalAuthKey == '\0' ? LOCAL_AUTH : LOCAL_NO_AUTH;
761ae349f5Scvs2svn   else
771ae349f5Scvs2svn     switch (LocalAuthValidate(SECRETFILE, VarShortHost, "")) {
781ae349f5Scvs2svn     case NOT_FOUND:
791ae349f5Scvs2svn       VarLocalAuth = LOCAL_DENY;
801ae349f5Scvs2svn       break;
811ae349f5Scvs2svn     case VALID:
821ae349f5Scvs2svn       VarLocalAuth = LOCAL_AUTH;
831ae349f5Scvs2svn       break;
841ae349f5Scvs2svn     case INVALID:
851ae349f5Scvs2svn       VarLocalAuth = LOCAL_NO_AUTH;
861ae349f5Scvs2svn       break;
871ae349f5Scvs2svn     }
881ae349f5Scvs2svn }
891ae349f5Scvs2svn 
901ae349f5Scvs2svn LOCAL_AUTH_VALID
911ae349f5Scvs2svn LocalAuthValidate(const char *fname, const char *system, const char *key)
921ae349f5Scvs2svn {
931ae349f5Scvs2svn   FILE *fp;
941ae349f5Scvs2svn   int n;
951ae349f5Scvs2svn   char *vector[3];
961ae349f5Scvs2svn   char buff[LINE_LEN];
971ae349f5Scvs2svn   LOCAL_AUTH_VALID rc;
981ae349f5Scvs2svn 
991ae349f5Scvs2svn   rc = NOT_FOUND;		/* No system entry */
1001ae349f5Scvs2svn   fp = OpenSecret(fname);
1011ae349f5Scvs2svn   if (fp == NULL)
1021ae349f5Scvs2svn     return (rc);
1031ae349f5Scvs2svn   while (fgets(buff, sizeof buff, fp)) {
1041ae349f5Scvs2svn     if (buff[0] == '#')
1051ae349f5Scvs2svn       continue;
1061ae349f5Scvs2svn     buff[strlen(buff) - 1] = 0;
1071ae349f5Scvs2svn     memset(vector, '\0', sizeof vector);
1081ae349f5Scvs2svn     n = MakeArgs(buff, vector, VECSIZE(vector));
1091ae349f5Scvs2svn     if (n < 1)
1101ae349f5Scvs2svn       continue;
1111ae349f5Scvs2svn     if (strcmp(vector[0], system) == 0) {
1121ae349f5Scvs2svn       if ((vector[1] == (char *) NULL && (key == NULL || *key == '\0')) ||
1131ae349f5Scvs2svn           (vector[1] != (char *) NULL && strcmp(vector[1], key) == 0)) {
1141ae349f5Scvs2svn 	rc = VALID;		/* Valid   */
1151ae349f5Scvs2svn       } else {
1161ae349f5Scvs2svn 	rc = INVALID;		/* Invalid */
1171ae349f5Scvs2svn       }
1181ae349f5Scvs2svn       break;
1191ae349f5Scvs2svn     }
1201ae349f5Scvs2svn   }
1211ae349f5Scvs2svn   CloseSecret(fp);
1221ae349f5Scvs2svn   return (rc);
1231ae349f5Scvs2svn }
1241ae349f5Scvs2svn 
1251ae349f5Scvs2svn int
1267a6f8720SBrian Somers AuthValidate(struct bundle *bundle, const char *fname, const char *system,
1277a6f8720SBrian Somers              const char *key, struct physical *physical)
1281ae349f5Scvs2svn {
1291ae349f5Scvs2svn   FILE *fp;
1301ae349f5Scvs2svn   int n;
1311ae349f5Scvs2svn   char *vector[5];
1321ae349f5Scvs2svn   char buff[LINE_LEN];
1331ae349f5Scvs2svn   char passwd[100];
1341ae349f5Scvs2svn 
1351ae349f5Scvs2svn   fp = OpenSecret(fname);
1361ae349f5Scvs2svn   if (fp == NULL)
1371ae349f5Scvs2svn     return (0);
1381ae349f5Scvs2svn   while (fgets(buff, sizeof buff, fp)) {
1391ae349f5Scvs2svn     if (buff[0] == '#')
1401ae349f5Scvs2svn       continue;
1411ae349f5Scvs2svn     buff[strlen(buff) - 1] = 0;
1421ae349f5Scvs2svn     memset(vector, '\0', sizeof vector);
1431ae349f5Scvs2svn     n = MakeArgs(buff, vector, VECSIZE(vector));
1441ae349f5Scvs2svn     if (n < 2)
1451ae349f5Scvs2svn       continue;
1461ae349f5Scvs2svn     if (strcmp(vector[0], system) == 0) {
1471ae349f5Scvs2svn       ExpandString(vector[1], passwd, sizeof passwd, 0);
1481ae349f5Scvs2svn       if (strcmp(passwd, key) == 0) {
1491ae349f5Scvs2svn 	CloseSecret(fp);
1507a6f8720SBrian Somers 	if (n > 2 && !UseHisaddr(bundle, vector[2], 1))
1511ae349f5Scvs2svn 	    return (0);
1527a6f8720SBrian Somers         /* XXX This should be deferred - we may join an existing bundle ! */
1537a6f8720SBrian Somers 	IpcpInit(bundle, physical2link(physical));
1541ae349f5Scvs2svn 	if (n > 3)
1551ae349f5Scvs2svn 	  SetLabel(vector[3]);
1561ae349f5Scvs2svn 	return (1);		/* Valid */
1571ae349f5Scvs2svn       }
1581ae349f5Scvs2svn     }
1591ae349f5Scvs2svn   }
1601ae349f5Scvs2svn   CloseSecret(fp);
1611ae349f5Scvs2svn   return (0);			/* Invalid */
1621ae349f5Scvs2svn }
1631ae349f5Scvs2svn 
1641ae349f5Scvs2svn char *
1657a6f8720SBrian Somers AuthGetSecret(struct bundle *bundle, const char *fname, const char *system,
1667a6f8720SBrian Somers               int len, int setaddr, struct physical *physical)
1671ae349f5Scvs2svn {
1681ae349f5Scvs2svn   FILE *fp;
1691ae349f5Scvs2svn   int n;
1701ae349f5Scvs2svn   char *vector[5];
1711ae349f5Scvs2svn   char buff[LINE_LEN];
1721ae349f5Scvs2svn   static char passwd[100];
1731ae349f5Scvs2svn 
1741ae349f5Scvs2svn   fp = OpenSecret(fname);
1751ae349f5Scvs2svn   if (fp == NULL)
1761ae349f5Scvs2svn     return (NULL);
1771ae349f5Scvs2svn   while (fgets(buff, sizeof buff, fp)) {
1781ae349f5Scvs2svn     if (buff[0] == '#')
1791ae349f5Scvs2svn       continue;
1801ae349f5Scvs2svn     buff[strlen(buff) - 1] = 0;
1811ae349f5Scvs2svn     memset(vector, '\0', sizeof vector);
1821ae349f5Scvs2svn     n = MakeArgs(buff, vector, VECSIZE(vector));
1831ae349f5Scvs2svn     if (n < 2)
1841ae349f5Scvs2svn       continue;
1851ae349f5Scvs2svn     if (strlen(vector[0]) == len && strncmp(vector[0], system, len) == 0) {
1861ae349f5Scvs2svn       ExpandString(vector[1], passwd, sizeof passwd, 0);
18729e275ceSBrian Somers       if (setaddr)
18829e275ceSBrian Somers 	memset(&IpcpInfo.DefHisAddress, '\0', sizeof IpcpInfo.DefHisAddress);
1891ae349f5Scvs2svn       if (n > 2 && setaddr)
1907a6f8720SBrian Somers 	if (UseHisaddr(bundle, vector[2], 1))
1917a6f8720SBrian Somers           /* XXX This should be deferred - we may join an existing bundle ! */
1927a6f8720SBrian Somers 	  IpcpInit(bundle, physical2link(physical));
1931ae349f5Scvs2svn         else
1941ae349f5Scvs2svn           return NULL;
1951ae349f5Scvs2svn       if (n > 3)
1961ae349f5Scvs2svn         SetLabel(vector[3]);
1971ae349f5Scvs2svn       return (passwd);
1981ae349f5Scvs2svn     }
1991ae349f5Scvs2svn   }
2001ae349f5Scvs2svn   CloseSecret(fp);
2011ae349f5Scvs2svn   return (NULL);		/* Invalid */
2021ae349f5Scvs2svn }
2031ae349f5Scvs2svn 
2041ae349f5Scvs2svn static void
2051ae349f5Scvs2svn AuthTimeout(void *vauthp)
2061ae349f5Scvs2svn {
2071ae349f5Scvs2svn   struct pppTimer *tp;
2081ae349f5Scvs2svn   struct authinfo *authp = (struct authinfo *)vauthp;
2091ae349f5Scvs2svn 
2101ae349f5Scvs2svn   tp = &authp->authtimer;
2111ae349f5Scvs2svn   StopTimer(tp);
2121ae349f5Scvs2svn   if (--authp->retry > 0) {
2131ae349f5Scvs2svn     StartTimer(tp);
21463b73463SBrian Somers     (authp->ChallengeFunc) (++authp->id, authp->physical);
2151ae349f5Scvs2svn   }
2161ae349f5Scvs2svn }
2171ae349f5Scvs2svn 
2181ae349f5Scvs2svn void
21963b73463SBrian Somers StartAuthChallenge(struct authinfo *authp, struct physical *physical)
2201ae349f5Scvs2svn {
2211ae349f5Scvs2svn   struct pppTimer *tp;
2221ae349f5Scvs2svn 
22363b73463SBrian Somers   assert(authp->physical == NULL);
22463b73463SBrian Somers 
22563b73463SBrian Somers   authp->physical = physical;
22663b73463SBrian Somers 
2271ae349f5Scvs2svn   tp = &authp->authtimer;
2281ae349f5Scvs2svn   StopTimer(tp);
2291ae349f5Scvs2svn   tp->func = AuthTimeout;
2301ae349f5Scvs2svn   tp->load = VarRetryTimeout * SECTICKS;
2311ae349f5Scvs2svn   tp->state = TIMER_STOPPED;
2321ae349f5Scvs2svn   tp->arg = (void *) authp;
2331ae349f5Scvs2svn   StartTimer(tp);
2341ae349f5Scvs2svn   authp->retry = 3;
2351ae349f5Scvs2svn   authp->id = 1;
23663b73463SBrian Somers   (authp->ChallengeFunc) (authp->id, physical);
2371ae349f5Scvs2svn }
2381ae349f5Scvs2svn 
2391ae349f5Scvs2svn void
2401ae349f5Scvs2svn StopAuthTimer(struct authinfo *authp)
2411ae349f5Scvs2svn {
2421ae349f5Scvs2svn   StopTimer(&authp->authtimer);
24363b73463SBrian Somers   authp->physical = NULL;
2441ae349f5Scvs2svn }
245