xref: /freebsd/usr.sbin/ppp/auth.c (revision d82e286489da73321a47e329d98a98817b0438b6)
1 /*
2  *			PPP Secret Key Module
3  *
4  *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5  *
6  *   Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the Internet Initiative Japan, Inc.  The name of the
14  * IIJ may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * $Id: auth.c,v 1.4 1995/05/30 03:50:25 rgrimes Exp $
21  *
22  *	TODO:
23  *		o Implement check against with registered IP addresses.
24  */
25 #include "fsm.h"
26 #include "lcpproto.h"
27 #include "ipcp.h"
28 #include "vars.h"
29 #include "auth.h"
30 
31 extern FILE *OpenSecret();
32 extern void CloseSecret();
33 
34 LOCAL_AUTH_VALID
35 LocalAuthInit(void){
36 
37   char *p;
38 
39   if ( gethostname( VarShortHost, sizeof(VarShortHost))) {
40   	return(NOT_FOUND);
41   }
42   if ( p = strchr( VarShortHost, '.' ) )
43 	*p = '\0';
44 
45   VarLocalAuth = LOCAL_NO_AUTH;
46   return LocalAuthValidate( SECRETFILE, VarShortHost, "" );
47 
48 }
49 
50 LOCAL_AUTH_VALID
51 LocalAuthValidate( char *fname, char *system, char *key) {
52   FILE *fp;
53   int n;
54   char *vector[20];	/* XXX */
55   char buff[200];	/* XXX */
56   LOCAL_AUTH_VALID rc;
57 
58   rc = NOT_FOUND;		/* No system entry */
59   fp = OpenSecret(fname);
60   if (fp == NULL)
61     return( rc );
62   while (fgets(buff, sizeof(buff), fp)) {
63     if (buff[0] == '#')
64       continue;
65     buff[strlen(buff)-1] = 0;
66     bzero(vector, sizeof(vector));
67     n = MakeArgs(buff, &vector);
68     if (n < 1)
69       continue;
70     if (strcmp(vector[0], system) == 0) {
71       if ( vector[1] != (char *) NULL && strcmp(vector[1], key) == 0) {
72 	rc = VALID;		/* Valid   */
73       } else {
74 	rc = INVALID;		/* Invalid */
75       }
76       break;
77     }
78   }
79   CloseSecret(fp);
80   return( rc );
81 }
82 
83 int
84 AuthValidate(fname, system, key)
85 char *fname, *system, *key;
86 {
87   FILE *fp;
88   int n;
89   char *vector[20];
90   char buff[200];
91   char passwd[100];
92 
93   fp = OpenSecret(fname);
94   if (fp == NULL)
95     return(0);
96   while (fgets(buff, sizeof(buff), fp)) {
97     if (buff[0] == '#')
98       continue;
99     buff[strlen(buff)-1] = 0;
100     bzero(vector, sizeof(vector));
101     n = MakeArgs(buff, &vector);
102     if (n < 2)
103       continue;
104     if (strcmp(vector[0], system) == 0) {
105       ExpandString(vector[1], passwd, 0);
106       if (strcmp(passwd, key) == 0) {
107 	CloseSecret(fp);
108         bzero(&DefHisAddress, sizeof(DefHisAddress));
109         n -= 2;
110         if (n > 0) {
111 	  ParseAddr(n--, &vector[2],
112 	    &DefHisAddress.ipaddr, &DefHisAddress.mask, &DefHisAddress.width);
113 	}
114 	IpcpInit();
115 	return(1);	/* Valid */
116       }
117     }
118   }
119   CloseSecret(fp);
120   return(0);		/* Invalid */
121 }
122 
123 char *
124 AuthGetSecret(fname, system, len, setaddr)
125 char *fname, *system;
126 int len, setaddr;
127 {
128   FILE *fp;
129   int n;
130   char *vector[20];
131   char buff[200];
132   static char passwd[100];
133 
134   fp = OpenSecret(fname);
135   if (fp == NULL)
136     return(NULL);
137   while (fgets(buff, sizeof(buff), fp)) {
138     if (buff[0] == '#')
139       continue;
140     buff[strlen(buff)-1] = 0;
141     bzero(vector, sizeof(vector));
142     n = MakeArgs(buff, &vector);
143     if (n < 2)
144       continue;
145     if (strlen(vector[0]) == len && strncmp(vector[0], system, len) == 0) {
146       ExpandString(vector[1], passwd, 0);
147       if (setaddr) {
148         bzero(&DefHisAddress, sizeof(DefHisAddress));
149       }
150       n -= 2;
151       if (n > 0 && setaddr) {
152 #ifdef DEBUG
153 	LogPrintf(LOG_LCP, "*** n = %d, %s\n", n, vector[2]);
154 #endif
155 	ParseAddr(n--, &vector[2],
156 	  &DefHisAddress.ipaddr, &DefHisAddress.mask, &DefHisAddress.width);
157 	IpcpInit();
158       }
159       return(passwd);
160     }
161   }
162   CloseSecret(fp);
163   return(NULL);		/* Invalid */
164 }
165 
166 static void
167 AuthTimeout(authp)
168 struct authinfo *authp;
169 {
170   struct pppTimer *tp;
171 
172   tp = &authp->authtimer;
173   StopTimer(tp);
174   if (--authp->retry > 0) {
175     StartTimer(tp);
176     (authp->ChallengeFunc)(++authp->id);
177   }
178 }
179 
180 void
181 StartAuthChallenge(authp)
182 struct authinfo *authp;
183 {
184   struct pppTimer *tp;
185 
186   tp = &authp->authtimer;
187   StopTimer(tp);
188   tp->func = AuthTimeout;
189   tp->load = VarRetryTimeout * SECTICKS;
190   tp->state = TIMER_STOPPED;
191   tp->arg = (void *)authp;
192   StartTimer(tp);
193   authp->retry = 3;
194   authp->id = 1;
195   (authp->ChallengeFunc)(authp->id);
196 }
197 
198 void
199 StopAuthTimer(authp)
200 struct authinfo *authp;
201 {
202   StopTimer(&authp->authtimer);
203 }
204