]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/auth.c
o Redesign the layering mechanism and make the aliasing code part of
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / auth.c
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.43 1999/03/31 14:21:44 brian Exp $
21  *
22  *      TODO:
23  *              o Implement check against with registered IP addresses.
24  */
25 #include <sys/param.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <sys/un.h>
30
31 #include <pwd.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <termios.h>
35 #include <unistd.h>
36
37 #include "layer.h"
38 #include "mbuf.h"
39 #include "defs.h"
40 #include "log.h"
41 #include "timer.h"
42 #include "fsm.h"
43 #include "iplist.h"
44 #include "throughput.h"
45 #include "slcompress.h"
46 #include "lqr.h"
47 #include "hdlc.h"
48 #include "ipcp.h"
49 #include "auth.h"
50 #include "systems.h"
51 #include "lcp.h"
52 #include "ccp.h"
53 #include "link.h"
54 #include "descriptor.h"
55 #include "chat.h"
56 #include "proto.h"
57 #include "filter.h"
58 #include "mp.h"
59 #ifndef NORADIUS
60 #include "radius.h"
61 #endif
62 #include "cbcp.h"
63 #include "chap.h"
64 #include "async.h"
65 #include "physical.h"
66 #include "datalink.h"
67 #include "bundle.h"
68
69 const char *
70 Auth2Nam(u_short auth, u_char type)
71 {
72   static char chap[10];
73
74   switch (auth) {
75   case PROTO_PAP:
76     return "PAP";
77   case PROTO_CHAP:
78     snprintf(chap, sizeof chap, "CHAP 0x%02x", type);
79     return chap;
80   case 0:
81     return "none";
82   }
83   return "unknown";
84 }
85
86 static int
87 auth_CheckPasswd(const char *name, const char *data, const char *key)
88 {
89   if (!strcmp(data, "*")) {
90     /* Then look up the real password database */
91     struct passwd *pw;
92     int result;
93
94     result = (pw = getpwnam(name)) &&
95              !strcmp(crypt(key, pw->pw_passwd), pw->pw_passwd);
96     endpwent();
97     return result;
98   }
99
100   return !strcmp(data, key);
101 }
102
103 int
104 auth_SetPhoneList(const char *name, char *phone, int phonelen)
105 {
106   FILE *fp;
107   int n;
108   char *vector[6];
109   char buff[LINE_LEN];
110
111   fp = OpenSecret(SECRETFILE);
112   if (fp != NULL) {
113     while (fgets(buff, sizeof buff, fp)) {
114       if (buff[0] == '#')
115         continue;
116       buff[strlen(buff) - 1] = '\0';
117       memset(vector, '\0', sizeof vector);
118       n = MakeArgs(buff, vector, VECSIZE(vector));
119       if (n < 5)
120         continue;
121       if (strcmp(vector[0], name) == 0) {
122         CloseSecret(fp);
123         if (*vector[4] == '\0')
124           return 0;
125         strncpy(phone, vector[4], phonelen - 1);
126         phone[phonelen - 1] = '\0';
127         return 1;               /* Valid */
128       }
129     }
130     CloseSecret(fp);
131   }
132   *phone = '\0';
133   return 0;
134 }
135
136 int
137 auth_Select(struct bundle *bundle, const char *name)
138 {
139   FILE *fp;
140   int n;
141   char *vector[5];
142   char buff[LINE_LEN];
143
144   if (*name == '\0') {
145     ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
146     return 1;
147   }
148
149 #ifndef NORADIUS
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))
153       return 0;
154     ipcp_Setup(&bundle->ncp.ipcp, bundle->radius.mask.s_addr);
155     /* Continue with ppp.secret in case we've got a new label */
156   }
157 #endif
158
159   fp = OpenSecret(SECRETFILE);
160   if (fp != NULL) {
161     while (fgets(buff, sizeof buff, fp)) {
162       if (buff[0] == '#')
163         continue;
164       buff[strlen(buff) - 1] = '\0';
165       memset(vector, '\0', sizeof vector);
166       n = MakeArgs(buff, vector, VECSIZE(vector));
167       if (n < 2)
168         continue;
169       if (strcmp(vector[0], name) == 0) {
170         CloseSecret(fp);
171 #ifndef NORADIUS
172         if (!bundle->radius.valid || bundle->radius.ip.s_addr == INADDR_NONE) {
173 #endif
174           if (n > 2 && *vector[2] && strcmp(vector[2], "*") &&
175               !ipcp_UseHisaddr(bundle, vector[2], 1))
176             return 0;
177           ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
178 #ifndef NORADIUS
179         }
180 #endif
181         if (n > 3 && *vector[3] && strcmp(vector[3], "*"))
182           bundle_SetLabel(bundle, vector[3]);
183         return 1;               /* Valid */
184       }
185     }
186     CloseSecret(fp);
187   }
188
189 #ifndef NOPASSWDAUTH
190   /* Let 'em in anyway - they must have been in the passwd file */
191   ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
192   return 1;
193 #else
194 #ifndef NORADIUS
195   if (bundle->radius.valid)
196     return 1;
197 #endif
198
199   /* Disappeared from ppp.secret ??? */
200   return 0;
201 #endif
202 }
203
204 int
205 auth_Validate(struct bundle *bundle, const char *name,
206              const char *key, struct physical *physical)
207 {
208   /* Used by PAP routines */
209
210   FILE *fp;
211   int n;
212   char *vector[5];
213   char buff[LINE_LEN];
214
215   fp = OpenSecret(SECRETFILE);
216   if (fp != NULL) {
217     while (fgets(buff, sizeof buff, fp)) {
218       if (buff[0] == '#')
219         continue;
220       buff[strlen(buff) - 1] = 0;
221       memset(vector, '\0', sizeof vector);
222       n = MakeArgs(buff, vector, VECSIZE(vector));
223       if (n < 2)
224         continue;
225       if (strcmp(vector[0], name) == 0) {
226         CloseSecret(fp);
227         return auth_CheckPasswd(name, vector[1], key);
228       }
229     }
230     CloseSecret(fp);
231   }
232
233 #ifndef NOPASSWDAUTH
234   if (Enabled(bundle, OPT_PASSWDAUTH))
235     return auth_CheckPasswd(name, "*", key);
236 #endif
237
238   return 0;                     /* Invalid */
239 }
240
241 char *
242 auth_GetSecret(struct bundle *bundle, const char *name, int len,
243               struct physical *physical)
244 {
245   /* Used by CHAP routines */
246
247   FILE *fp;
248   int n;
249   char *vector[5];
250   static char buff[LINE_LEN];
251
252   fp = OpenSecret(SECRETFILE);
253   if (fp == NULL)
254     return (NULL);
255
256   while (fgets(buff, sizeof buff, fp)) {
257     if (buff[0] == '#')
258       continue;
259     buff[strlen(buff) - 1] = 0;
260     memset(vector, '\0', sizeof vector);
261     n = MakeArgs(buff, vector, VECSIZE(vector));
262     if (n < 2)
263       continue;
264     if (strlen(vector[0]) == len && strncmp(vector[0], name, len) == 0) {
265       CloseSecret(fp);
266       return vector[1];
267     }
268   }
269   CloseSecret(fp);
270   return (NULL);                /* Invalid */
271 }
272
273 static void
274 AuthTimeout(void *vauthp)
275 {
276   struct authinfo *authp = (struct authinfo *)vauthp;
277
278   timer_Stop(&authp->authtimer);
279   if (--authp->retry > 0) {
280     authp->id++;
281     (*authp->fn.req)(authp);
282     timer_Start(&authp->authtimer);
283   } else {
284     log_Printf(LogPHASE, "Auth: No response from server\n");
285     datalink_AuthNotOk(authp->physical->dl);
286   }
287 }
288
289 void
290 auth_Init(struct authinfo *authp, struct physical *p, auth_func req,
291           auth_func success, auth_func failure)
292 {
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 */
297   authp->fn.req = req;
298   authp->fn.success = success;
299   authp->fn.failure = failure;
300   authp->physical = p;
301 }
302
303 void
304 auth_StartReq(struct authinfo *authp)
305 {
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;
312   authp->id = 1;
313   (*authp->fn.req)(authp);
314   timer_Start(&authp->authtimer);
315 }
316
317 void
318 auth_StopTimer(struct authinfo *authp)
319 {
320   timer_Stop(&authp->authtimer);
321 }
322
323 struct mbuf *
324 auth_ReadHeader(struct authinfo *authp, struct mbuf *bp)
325 {
326   int len;
327
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))
332       return bp;
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);
336   } else {
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);
340   }
341
342   mbuf_Free(bp);
343   return NULL;
344 }
345
346 struct mbuf *
347 auth_ReadName(struct authinfo *authp, struct mbuf *bp, int len)
348 {
349   if (len > sizeof authp->in.name - 1)
350     log_Printf(LogWARN, "auth_ReadName: Name too long (%d) !\n", len);
351   else {
352     int mlen = mbuf_Length(bp);
353
354     if (len > mlen)
355       log_Printf(LogWARN, "auth_ReadName: Short packet (%d > %d) !\n",
356                  len, mlen);
357     else {
358       bp = mbuf_Read(bp, (u_char *)authp->in.name, len);
359       authp->in.name[len] = '\0';
360       return bp;
361     }
362   }
363
364   *authp->in.name = '\0';
365   mbuf_Free(bp);
366   return NULL;
367 }