xref: /freebsd/usr.sbin/ppp/auth.c (revision 6140ba1177f35afccbaf1c04700fea5809d22ee7)
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  *
206140ba11SBrian Somers  * $Id: auth.c,v 1.27.2.4 1998/02/02 19:32:00 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"
521ae349f5Scvs2svn 
531ae349f5Scvs2svn void
541ae349f5Scvs2svn LocalAuthInit()
551ae349f5Scvs2svn {
561ae349f5Scvs2svn   if (!(mode&MODE_DAEMON))
571ae349f5Scvs2svn     /* We're allowed in interactive mode */
581ae349f5Scvs2svn     VarLocalAuth = LOCAL_AUTH;
591ae349f5Scvs2svn   else if (VarHaveLocalAuthKey)
601ae349f5Scvs2svn     VarLocalAuth = *VarLocalAuthKey == '\0' ? LOCAL_AUTH : LOCAL_NO_AUTH;
611ae349f5Scvs2svn   else
621ae349f5Scvs2svn     switch (LocalAuthValidate(SECRETFILE, VarShortHost, "")) {
631ae349f5Scvs2svn     case NOT_FOUND:
641ae349f5Scvs2svn       VarLocalAuth = LOCAL_DENY;
651ae349f5Scvs2svn       break;
661ae349f5Scvs2svn     case VALID:
671ae349f5Scvs2svn       VarLocalAuth = LOCAL_AUTH;
681ae349f5Scvs2svn       break;
691ae349f5Scvs2svn     case INVALID:
701ae349f5Scvs2svn       VarLocalAuth = LOCAL_NO_AUTH;
711ae349f5Scvs2svn       break;
721ae349f5Scvs2svn     }
731ae349f5Scvs2svn }
741ae349f5Scvs2svn 
751ae349f5Scvs2svn LOCAL_AUTH_VALID
761ae349f5Scvs2svn LocalAuthValidate(const char *fname, const char *system, const char *key)
771ae349f5Scvs2svn {
781ae349f5Scvs2svn   FILE *fp;
791ae349f5Scvs2svn   int n;
801ae349f5Scvs2svn   char *vector[3];
811ae349f5Scvs2svn   char buff[LINE_LEN];
821ae349f5Scvs2svn   LOCAL_AUTH_VALID rc;
831ae349f5Scvs2svn 
841ae349f5Scvs2svn   rc = NOT_FOUND;		/* No system entry */
851ae349f5Scvs2svn   fp = OpenSecret(fname);
861ae349f5Scvs2svn   if (fp == NULL)
871ae349f5Scvs2svn     return (rc);
881ae349f5Scvs2svn   while (fgets(buff, sizeof buff, fp)) {
891ae349f5Scvs2svn     if (buff[0] == '#')
901ae349f5Scvs2svn       continue;
911ae349f5Scvs2svn     buff[strlen(buff) - 1] = 0;
921ae349f5Scvs2svn     memset(vector, '\0', sizeof vector);
931ae349f5Scvs2svn     n = MakeArgs(buff, vector, VECSIZE(vector));
941ae349f5Scvs2svn     if (n < 1)
951ae349f5Scvs2svn       continue;
961ae349f5Scvs2svn     if (strcmp(vector[0], system) == 0) {
971ae349f5Scvs2svn       if ((vector[1] == (char *) NULL && (key == NULL || *key == '\0')) ||
981ae349f5Scvs2svn           (vector[1] != (char *) NULL && strcmp(vector[1], key) == 0)) {
991ae349f5Scvs2svn 	rc = VALID;		/* Valid   */
1001ae349f5Scvs2svn       } else {
1011ae349f5Scvs2svn 	rc = INVALID;		/* Invalid */
1021ae349f5Scvs2svn       }
1031ae349f5Scvs2svn       break;
1041ae349f5Scvs2svn     }
1051ae349f5Scvs2svn   }
1061ae349f5Scvs2svn   CloseSecret(fp);
1071ae349f5Scvs2svn   return (rc);
1081ae349f5Scvs2svn }
1091ae349f5Scvs2svn 
1101ae349f5Scvs2svn int
1117a6f8720SBrian Somers AuthValidate(struct bundle *bundle, const char *fname, const char *system,
1127a6f8720SBrian Somers              const char *key, struct physical *physical)
1131ae349f5Scvs2svn {
1141ae349f5Scvs2svn   FILE *fp;
1151ae349f5Scvs2svn   int n;
1161ae349f5Scvs2svn   char *vector[5];
1171ae349f5Scvs2svn   char buff[LINE_LEN];
1181ae349f5Scvs2svn   char passwd[100];
1191ae349f5Scvs2svn 
1201ae349f5Scvs2svn   fp = OpenSecret(fname);
1211ae349f5Scvs2svn   if (fp == NULL)
1221ae349f5Scvs2svn     return (0);
1231ae349f5Scvs2svn   while (fgets(buff, sizeof buff, fp)) {
1241ae349f5Scvs2svn     if (buff[0] == '#')
1251ae349f5Scvs2svn       continue;
1261ae349f5Scvs2svn     buff[strlen(buff) - 1] = 0;
1271ae349f5Scvs2svn     memset(vector, '\0', sizeof vector);
1281ae349f5Scvs2svn     n = MakeArgs(buff, vector, VECSIZE(vector));
1291ae349f5Scvs2svn     if (n < 2)
1301ae349f5Scvs2svn       continue;
1311ae349f5Scvs2svn     if (strcmp(vector[0], system) == 0) {
1321ae349f5Scvs2svn       ExpandString(vector[1], passwd, sizeof passwd, 0);
1331ae349f5Scvs2svn       if (strcmp(passwd, key) == 0) {
1341ae349f5Scvs2svn 	CloseSecret(fp);
1357a6f8720SBrian Somers 	if (n > 2 && !UseHisaddr(bundle, vector[2], 1))
1361ae349f5Scvs2svn 	    return (0);
1377a6f8720SBrian Somers         /* XXX This should be deferred - we may join an existing bundle ! */
1387a6f8720SBrian Somers 	IpcpInit(bundle, physical2link(physical));
1391ae349f5Scvs2svn 	if (n > 3)
1401ae349f5Scvs2svn 	  SetLabel(vector[3]);
1411ae349f5Scvs2svn 	return (1);		/* Valid */
1421ae349f5Scvs2svn       }
1431ae349f5Scvs2svn     }
1441ae349f5Scvs2svn   }
1451ae349f5Scvs2svn   CloseSecret(fp);
1461ae349f5Scvs2svn   return (0);			/* Invalid */
1471ae349f5Scvs2svn }
1481ae349f5Scvs2svn 
1491ae349f5Scvs2svn char *
1507a6f8720SBrian Somers AuthGetSecret(struct bundle *bundle, const char *fname, const char *system,
1517a6f8720SBrian Somers               int len, int setaddr, struct physical *physical)
1521ae349f5Scvs2svn {
1531ae349f5Scvs2svn   FILE *fp;
1541ae349f5Scvs2svn   int n;
1551ae349f5Scvs2svn   char *vector[5];
1561ae349f5Scvs2svn   char buff[LINE_LEN];
1571ae349f5Scvs2svn   static char passwd[100];
1581ae349f5Scvs2svn 
1591ae349f5Scvs2svn   fp = OpenSecret(fname);
1601ae349f5Scvs2svn   if (fp == NULL)
1611ae349f5Scvs2svn     return (NULL);
1621ae349f5Scvs2svn   while (fgets(buff, sizeof buff, fp)) {
1631ae349f5Scvs2svn     if (buff[0] == '#')
1641ae349f5Scvs2svn       continue;
1651ae349f5Scvs2svn     buff[strlen(buff) - 1] = 0;
1661ae349f5Scvs2svn     memset(vector, '\0', sizeof vector);
1671ae349f5Scvs2svn     n = MakeArgs(buff, vector, VECSIZE(vector));
1681ae349f5Scvs2svn     if (n < 2)
1691ae349f5Scvs2svn       continue;
1701ae349f5Scvs2svn     if (strlen(vector[0]) == len && strncmp(vector[0], system, len) == 0) {
1711ae349f5Scvs2svn       ExpandString(vector[1], passwd, sizeof passwd, 0);
17229e275ceSBrian Somers       if (setaddr)
17329e275ceSBrian Somers 	memset(&IpcpInfo.DefHisAddress, '\0', sizeof IpcpInfo.DefHisAddress);
1741ae349f5Scvs2svn       if (n > 2 && setaddr)
1757a6f8720SBrian Somers 	if (UseHisaddr(bundle, vector[2], 1))
1767a6f8720SBrian Somers           /* XXX This should be deferred - we may join an existing bundle ! */
1777a6f8720SBrian Somers 	  IpcpInit(bundle, physical2link(physical));
1781ae349f5Scvs2svn         else
1791ae349f5Scvs2svn           return NULL;
1801ae349f5Scvs2svn       if (n > 3)
1811ae349f5Scvs2svn         SetLabel(vector[3]);
1821ae349f5Scvs2svn       return (passwd);
1831ae349f5Scvs2svn     }
1841ae349f5Scvs2svn   }
1851ae349f5Scvs2svn   CloseSecret(fp);
1861ae349f5Scvs2svn   return (NULL);		/* Invalid */
1871ae349f5Scvs2svn }
1881ae349f5Scvs2svn 
1891ae349f5Scvs2svn static void
1901ae349f5Scvs2svn AuthTimeout(void *vauthp)
1911ae349f5Scvs2svn {
1921ae349f5Scvs2svn   struct pppTimer *tp;
1931ae349f5Scvs2svn   struct authinfo *authp = (struct authinfo *)vauthp;
1941ae349f5Scvs2svn 
1951ae349f5Scvs2svn   tp = &authp->authtimer;
1961ae349f5Scvs2svn   StopTimer(tp);
1971ae349f5Scvs2svn   if (--authp->retry > 0) {
1981ae349f5Scvs2svn     StartTimer(tp);
19963b73463SBrian Somers     (authp->ChallengeFunc) (++authp->id, authp->physical);
2001ae349f5Scvs2svn   }
2011ae349f5Scvs2svn }
2021ae349f5Scvs2svn 
2031ae349f5Scvs2svn void
20463b73463SBrian Somers StartAuthChallenge(struct authinfo *authp, struct physical *physical)
2051ae349f5Scvs2svn {
2061ae349f5Scvs2svn   struct pppTimer *tp;
2071ae349f5Scvs2svn 
20863b73463SBrian Somers   assert(authp->physical == NULL);
20963b73463SBrian Somers 
21063b73463SBrian Somers   authp->physical = physical;
21163b73463SBrian Somers 
2121ae349f5Scvs2svn   tp = &authp->authtimer;
2131ae349f5Scvs2svn   StopTimer(tp);
2141ae349f5Scvs2svn   tp->func = AuthTimeout;
2151ae349f5Scvs2svn   tp->load = VarRetryTimeout * SECTICKS;
2161ae349f5Scvs2svn   tp->state = TIMER_STOPPED;
2171ae349f5Scvs2svn   tp->arg = (void *) authp;
2181ae349f5Scvs2svn   StartTimer(tp);
2191ae349f5Scvs2svn   authp->retry = 3;
2201ae349f5Scvs2svn   authp->id = 1;
22163b73463SBrian Somers   (authp->ChallengeFunc) (authp->id, physical);
2221ae349f5Scvs2svn }
2231ae349f5Scvs2svn 
2241ae349f5Scvs2svn void
2251ae349f5Scvs2svn StopAuthTimer(struct authinfo *authp)
2261ae349f5Scvs2svn {
2271ae349f5Scvs2svn   StopTimer(&authp->authtimer);
22863b73463SBrian Somers   authp->physical = NULL;
2291ae349f5Scvs2svn }
230