2 * PPP Secret Key Module
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
6 * Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd.
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.
20 * $Id: auth.c,v 1.43 1999/03/31 14:21:44 brian Exp $
23 * o Implement check against with registered IP addresses.
25 #include <sys/param.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
44 #include "throughput.h"
45 #include "slcompress.h"
54 #include "descriptor.h"
70 Auth2Nam(u_short auth, u_char type)
78 snprintf(chap, sizeof chap, "CHAP 0x%02x", type);
87 auth_CheckPasswd(const char *name, const char *data, const char *key)
89 if (!strcmp(data, "*")) {
90 /* Then look up the real password database */
94 result = (pw = getpwnam(name)) &&
95 !strcmp(crypt(key, pw->pw_passwd), pw->pw_passwd);
100 return !strcmp(data, key);
104 auth_SetPhoneList(const char *name, char *phone, int phonelen)
111 fp = OpenSecret(SECRETFILE);
113 while (fgets(buff, sizeof buff, fp)) {
116 buff[strlen(buff) - 1] = '\0';
117 memset(vector, '\0', sizeof vector);
118 n = MakeArgs(buff, vector, VECSIZE(vector));
121 if (strcmp(vector[0], name) == 0) {
123 if (*vector[4] == '\0')
125 strncpy(phone, vector[4], phonelen - 1);
126 phone[phonelen - 1] = '\0';
127 return 1; /* Valid */
137 auth_Select(struct bundle *bundle, const char *name)
145 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
150 if (bundle->radius.valid && bundle->radius.ip.s_addr != INADDR_NONE) {
151 /* We've got a radius IP - it overrides everything */
152 if (!ipcp_UseHisIPaddr(bundle, bundle->radius.ip))
154 ipcp_Setup(&bundle->ncp.ipcp, bundle->radius.mask.s_addr);
155 /* Continue with ppp.secret in case we've got a new label */
159 fp = OpenSecret(SECRETFILE);
161 while (fgets(buff, sizeof buff, fp)) {
164 buff[strlen(buff) - 1] = '\0';
165 memset(vector, '\0', sizeof vector);
166 n = MakeArgs(buff, vector, VECSIZE(vector));
169 if (strcmp(vector[0], name) == 0) {
172 if (!bundle->radius.valid || bundle->radius.ip.s_addr == INADDR_NONE) {
174 if (n > 2 && *vector[2] && strcmp(vector[2], "*") &&
175 !ipcp_UseHisaddr(bundle, vector[2], 1))
177 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
181 if (n > 3 && *vector[3] && strcmp(vector[3], "*"))
182 bundle_SetLabel(bundle, vector[3]);
183 return 1; /* Valid */
190 /* Let 'em in anyway - they must have been in the passwd file */
191 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
195 if (bundle->radius.valid)
199 /* Disappeared from ppp.secret ??? */
205 auth_Validate(struct bundle *bundle, const char *name,
206 const char *key, struct physical *physical)
208 /* Used by PAP routines */
215 fp = OpenSecret(SECRETFILE);
217 while (fgets(buff, sizeof buff, fp)) {
220 buff[strlen(buff) - 1] = 0;
221 memset(vector, '\0', sizeof vector);
222 n = MakeArgs(buff, vector, VECSIZE(vector));
225 if (strcmp(vector[0], name) == 0) {
227 return auth_CheckPasswd(name, vector[1], key);
234 if (Enabled(bundle, OPT_PASSWDAUTH))
235 return auth_CheckPasswd(name, "*", key);
238 return 0; /* Invalid */
242 auth_GetSecret(struct bundle *bundle, const char *name, int len,
243 struct physical *physical)
245 /* Used by CHAP routines */
250 static char buff[LINE_LEN];
252 fp = OpenSecret(SECRETFILE);
256 while (fgets(buff, sizeof buff, fp)) {
259 buff[strlen(buff) - 1] = 0;
260 memset(vector, '\0', sizeof vector);
261 n = MakeArgs(buff, vector, VECSIZE(vector));
264 if (strlen(vector[0]) == len && strncmp(vector[0], name, len) == 0) {
270 return (NULL); /* Invalid */
274 AuthTimeout(void *vauthp)
276 struct authinfo *authp = (struct authinfo *)vauthp;
278 timer_Stop(&authp->authtimer);
279 if (--authp->retry > 0) {
281 (*authp->fn.req)(authp);
282 timer_Start(&authp->authtimer);
284 log_Printf(LogPHASE, "Auth: No response from server\n");
285 datalink_AuthNotOk(authp->physical->dl);
290 auth_Init(struct authinfo *authp, struct physical *p, auth_func req,
291 auth_func success, auth_func failure)
293 memset(authp, '\0', sizeof(struct authinfo));
294 authp->cfg.fsm.timeout = DEF_FSMRETRY;
295 authp->cfg.fsm.maxreq = DEF_FSMAUTHTRIES;
296 authp->cfg.fsm.maxtrm = 0; /* not used */
298 authp->fn.success = success;
299 authp->fn.failure = failure;
304 auth_StartReq(struct authinfo *authp)
306 timer_Stop(&authp->authtimer);
307 authp->authtimer.func = AuthTimeout;
308 authp->authtimer.name = "auth";
309 authp->authtimer.load = authp->cfg.fsm.timeout * SECTICKS;
310 authp->authtimer.arg = (void *)authp;
311 authp->retry = authp->cfg.fsm.maxreq;
313 (*authp->fn.req)(authp);
314 timer_Start(&authp->authtimer);
318 auth_StopTimer(struct authinfo *authp)
320 timer_Stop(&authp->authtimer);
324 auth_ReadHeader(struct authinfo *authp, struct mbuf *bp)
328 len = mbuf_Length(bp);
329 if (len >= sizeof authp->in.hdr) {
330 bp = mbuf_Read(bp, (u_char *)&authp->in.hdr, sizeof authp->in.hdr);
331 if (len >= ntohs(authp->in.hdr.length))
333 authp->in.hdr.length = htons(0);
334 log_Printf(LogWARN, "auth_ReadHeader: Short packet (%d > %d) !\n",
335 ntohs(authp->in.hdr.length), len);
337 authp->in.hdr.length = htons(0);
338 log_Printf(LogWARN, "auth_ReadHeader: Short packet header (%d > %d) !\n",
339 (int)(sizeof authp->in.hdr), len);
347 auth_ReadName(struct authinfo *authp, struct mbuf *bp, int len)
349 if (len > sizeof authp->in.name - 1)
350 log_Printf(LogWARN, "auth_ReadName: Name too long (%d) !\n", len);
352 int mlen = mbuf_Length(bp);
355 log_Printf(LogWARN, "auth_ReadName: Short packet (%d > %d) !\n",
358 bp = mbuf_Read(bp, (u_char *)authp->in.name, len);
359 authp->in.name[len] = '\0';
364 *authp->in.name = '\0';