xref: /freebsd/usr.sbin/ppp/auth.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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$
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 "filter.h"
30 #include "auth.h"
31 #include "chat.h"
32 
33 extern FILE *OpenSecret();
34 extern void CloseSecret();
35 
36 LOCAL_AUTH_VALID
37 LocalAuthInit(void)
38 {
39 
40   char *p;
41 
42   if ( gethostname( VarShortHost, sizeof(VarShortHost))) {
43   	return(NOT_FOUND);
44   }
45   p = strchr( VarShortHost, '.' );
46   if (p)
47 	*p = '\0';
48 
49   VarLocalAuth = LOCAL_NO_AUTH;
50   return LocalAuthValidate( SECRETFILE, VarShortHost, "" );
51 
52 }
53 
54 LOCAL_AUTH_VALID
55 LocalAuthValidate( char *fname, char *system, char *key) {
56   FILE *fp;
57   int n;
58   char *vector[20];	/* XXX */
59   char buff[200];	/* XXX */
60   LOCAL_AUTH_VALID rc;
61 
62   rc = NOT_FOUND;		/* No system entry */
63   fp = OpenSecret(fname);
64   if (fp == NULL)
65     return( rc );
66   while (fgets(buff, sizeof(buff), fp)) {
67     if (buff[0] == '#')
68       continue;
69     buff[strlen(buff)-1] = 0;
70     bzero(vector, sizeof(vector));
71     n = MakeArgs(buff, vector);
72     if (n < 1)
73       continue;
74     if (strcmp(vector[0], system) == 0) {
75       if ( vector[1] != (char *) NULL && strcmp(vector[1], key) == 0) {
76 	rc = VALID;		/* Valid   */
77       } else {
78 	rc = INVALID;		/* Invalid */
79       }
80       break;
81     }
82   }
83   CloseSecret(fp);
84   return( rc );
85 }
86 
87 int
88 AuthValidate(fname, system, key)
89 char *fname, *system, *key;
90 {
91   FILE *fp;
92   int n;
93   char *vector[20];
94   char buff[200];
95   char passwd[100];
96 
97   fp = OpenSecret(fname);
98   if (fp == NULL)
99     return(0);
100   while (fgets(buff, sizeof(buff), fp)) {
101     if (buff[0] == '#')
102       continue;
103     buff[strlen(buff)-1] = 0;
104     bzero(vector, sizeof(vector));
105     n = MakeArgs(buff, vector);
106     if (n < 2)
107       continue;
108     if (strcmp(vector[0], system) == 0) {
109       ExpandString(vector[1], passwd, sizeof(passwd), 0);
110       if (strcmp(passwd, key) == 0) {
111 	CloseSecret(fp);
112         bzero(&DefHisAddress, sizeof(DefHisAddress));
113         n -= 2;
114         if (n > 0) {
115 	  ParseAddr(n--, &vector[2],
116 	    &DefHisAddress.ipaddr, &DefHisAddress.mask, &DefHisAddress.width);
117 	}
118 	IpcpInit();
119 	return(1);	/* Valid */
120       }
121     }
122   }
123   CloseSecret(fp);
124   return(0);		/* Invalid */
125 }
126 
127 char *
128 AuthGetSecret(fname, system, len, setaddr)
129 char *fname, *system;
130 int len, setaddr;
131 {
132   FILE *fp;
133   int n;
134   char *vector[20];
135   char buff[200];
136   static char passwd[100];
137 
138   fp = OpenSecret(fname);
139   if (fp == NULL)
140     return(NULL);
141   while (fgets(buff, sizeof(buff), fp)) {
142     if (buff[0] == '#')
143       continue;
144     buff[strlen(buff)-1] = 0;
145     bzero(vector, sizeof(vector));
146     n = MakeArgs(buff, vector);
147     if (n < 2)
148       continue;
149     if (strlen(vector[0]) == len && strncmp(vector[0], system, len) == 0) {
150       ExpandString(vector[1], passwd, sizeof(passwd), 0);
151       if (setaddr) {
152         bzero(&DefHisAddress, sizeof(DefHisAddress));
153       }
154       n -= 2;
155       if (n > 0 && setaddr) {
156 #ifdef DEBUG
157 	LogPrintf(LOG_LCP_BIT, "*** n = %d, %s\n", n, vector[2]);
158 #endif
159 	ParseAddr(n--, &vector[2],
160 	  &DefHisAddress.ipaddr, &DefHisAddress.mask, &DefHisAddress.width);
161 	IpcpInit();
162       }
163       return(passwd);
164     }
165   }
166   CloseSecret(fp);
167   return(NULL);		/* Invalid */
168 }
169 
170 static void
171 AuthTimeout(authp)
172 struct authinfo *authp;
173 {
174   struct pppTimer *tp;
175 
176   tp = &authp->authtimer;
177   StopTimer(tp);
178   if (--authp->retry > 0) {
179     StartTimer(tp);
180     (authp->ChallengeFunc)(++authp->id);
181   }
182 }
183 
184 void
185 StartAuthChallenge(authp)
186 struct authinfo *authp;
187 {
188   struct pppTimer *tp;
189 
190   tp = &authp->authtimer;
191   StopTimer(tp);
192   tp->func = AuthTimeout;
193   tp->load = VarRetryTimeout * SECTICKS;
194   tp->state = TIMER_STOPPED;
195   tp->arg = (void *)authp;
196   StartTimer(tp);
197   authp->retry = 3;
198   authp->id = 1;
199   (authp->ChallengeFunc)(authp->id);
200 }
201 
202 void
203 StopAuthTimer(authp)
204 struct authinfo *authp;
205 {
206   StopTimer(&authp->authtimer);
207 }
208