2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <sys/socket.h>
46 #include <security/pam_appl.h>
48 #include <security/openpam.h>
59 #include "throughput.h"
60 #include "slcompress.h"
70 #include "descriptor.h"
88 Auth2Nam(u_short auth, u_char type)
96 snprintf(chap, sizeof chap, "CHAP 0x%02x", type);
104 #if !defined(NOPAM) && !defined(OPENPAM)
106 pam_conv(int n, const struct pam_message **msg, struct pam_response **resp,
110 if (n != 1 || msg[0]->msg_style != PAM_PROMPT_ECHO_OFF)
111 return (PAM_CONV_ERR);
112 if ((*resp = malloc(sizeof(struct pam_response))) == NULL)
113 return (PAM_CONV_ERR);
114 (*resp)[0].resp = strdup((const char *)data);
115 (*resp)[0].resp_retcode = 0;
117 return ((*resp)[0].resp != NULL ? PAM_SUCCESS : PAM_CONV_ERR);
119 #endif /* !defined(NOPAM) && !defined(OPENPAM) */
122 auth_CheckPasswd(const char *name, const char *data, const char *key)
124 if (!strcmp(data, "*")) {
126 /* Then look up the real password database */
134 cryptpw = crypt(key, pw->pw_passwd);
136 result = (cryptpw != NULL) && !strcmp(cryptpw, pw->pw_passwd);
143 /* Then consult with PAM. */
147 struct pam_conv pamc = {
149 &openpam_nullconv, NULL
155 if (pam_start("ppp", name, &pamc, &pamh) != PAM_SUCCESS)
158 if ((status = pam_set_item(pamh, PAM_AUTHTOK, key)) == PAM_SUCCESS)
160 status = pam_authenticate(pamh, 0);
161 pam_end(pamh, status);
162 return (status == PAM_SUCCESS);
166 return !strcmp(data, key);
170 auth_SetPhoneList(const char *name, char *phone, int phonelen)
174 char *vector[6], buff[LINE_LEN];
177 fp = OpenSecret(SECRETFILE);
181 while (fgets(buff, sizeof buff, fp)) {
185 buff[strlen(buff) - 1] = '\0';
186 memset(vector, '\0', sizeof vector);
187 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
188 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
191 if (strcmp(vector[0], name) == 0) {
193 if (*vector[4] == '\0')
195 strncpy(phone, vector[4], phonelen - 1);
196 phone[phonelen - 1] = '\0';
197 return 1; /* Valid */
201 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
202 /* Look for the name without the leading domain */
215 auth_Select(struct bundle *bundle, const char *name)
219 char *vector[5], buff[LINE_LEN];
223 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
228 if (bundle->radius.valid && bundle->radius.ip.s_addr != INADDR_NONE &&
229 bundle->radius.ip.s_addr != RADIUS_INADDR_POOL) {
230 /* We've got a radius IP - it overrides everything */
231 if (!ipcp_UseHisIPaddr(bundle, bundle->radius.ip))
233 ipcp_Setup(&bundle->ncp.ipcp, bundle->radius.mask.s_addr);
234 /* Continue with ppp.secret in case we've got a new label */
238 fp = OpenSecret(SECRETFILE);
242 while (fgets(buff, sizeof buff, fp)) {
246 buff[strlen(buff) - 1] = '\0';
247 memset(vector, '\0', sizeof vector);
248 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
249 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
252 if (strcmp(vector[0], name) == 0) {
255 if (!bundle->radius.valid || bundle->radius.ip.s_addr == INADDR_NONE) {
257 if (n > 2 && *vector[2] && strcmp(vector[2], "*") &&
258 !ipcp_UseHisaddr(bundle, vector[2], 1))
260 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
264 if (n > 3 && *vector[3] && strcmp(vector[3], "*"))
265 bundle_SetLabel(bundle, vector[3]);
266 return 1; /* Valid */
270 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
271 /* Look for the name without the leading domain */
281 /* Let 'em in anyway - they must have been in the passwd file */
282 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
286 if (bundle->radius.valid)
290 /* Disappeared from ppp.secret ??? */
296 auth_Validate(struct bundle *bundle, const char *name, const char *key)
298 /* Used by PAP routines */
302 char *vector[5], buff[LINE_LEN];
305 fp = OpenSecret(SECRETFILE);
309 while (fgets(buff, sizeof buff, fp)) {
313 buff[strlen(buff) - 1] = 0;
314 memset(vector, '\0', sizeof vector);
315 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
316 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
319 if (strcmp(vector[0], name) == 0) {
321 return auth_CheckPasswd(name, vector[1], key);
326 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
327 /* Look for the name without the leading domain */
339 if (Enabled(bundle, OPT_PASSWDAUTH))
340 return auth_CheckPasswd(name, "*", key);
343 return 0; /* Invalid */
347 auth_GetSecret(const char *name, size_t len)
349 /* Used by CHAP routines */
355 static char buff[LINE_LEN]; /* vector[] will point here when returned */
357 fp = OpenSecret(SECRETFILE);
363 while (fgets(buff, sizeof buff, fp)) {
367 n = strlen(buff) - 1;
369 buff[n] = '\0'; /* Trim the '\n' */
370 memset(vector, '\0', sizeof vector);
371 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
372 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
375 if (strlen(vector[0]) == len && strncmp(vector[0], name, len) == 0) {
381 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
382 /* Go back and look for the name without the leading domain */
383 len -= slash - name + 1;
390 return (NULL); /* Invalid */
394 AuthTimeout(void *vauthp)
396 struct authinfo *authp = (struct authinfo *)vauthp;
398 timer_Stop(&authp->authtimer);
399 if (--authp->retry > 0) {
401 (*authp->fn.req)(authp);
402 timer_Start(&authp->authtimer);
404 log_Printf(LogPHASE, "Auth: No response from server\n");
405 datalink_AuthNotOk(authp->physical->dl);
410 auth_Init(struct authinfo *authp, struct physical *p, auth_func req,
411 auth_func success, auth_func failure)
413 memset(authp, '\0', sizeof(struct authinfo));
414 authp->cfg.fsm.timeout = DEF_FSMRETRY;
415 authp->cfg.fsm.maxreq = DEF_FSMAUTHTRIES;
416 authp->cfg.fsm.maxtrm = 0; /* not used */
418 authp->fn.success = success;
419 authp->fn.failure = failure;
424 auth_StartReq(struct authinfo *authp)
426 timer_Stop(&authp->authtimer);
427 authp->authtimer.func = AuthTimeout;
428 authp->authtimer.name = "auth";
429 authp->authtimer.load = authp->cfg.fsm.timeout * SECTICKS;
430 authp->authtimer.arg = (void *)authp;
431 authp->retry = authp->cfg.fsm.maxreq;
433 (*authp->fn.req)(authp);
434 timer_Start(&authp->authtimer);
438 auth_StopTimer(struct authinfo *authp)
440 timer_Stop(&authp->authtimer);
444 auth_ReadHeader(struct authinfo *authp, struct mbuf *bp)
449 if (len >= sizeof authp->in.hdr) {
450 bp = mbuf_Read(bp, (u_char *)&authp->in.hdr, sizeof authp->in.hdr);
451 if (len >= ntohs(authp->in.hdr.length))
453 authp->in.hdr.length = htons(0);
454 log_Printf(LogWARN, "auth_ReadHeader: Short packet (%u > %zu) !\n",
455 ntohs(authp->in.hdr.length), len);
457 authp->in.hdr.length = htons(0);
458 log_Printf(LogWARN, "auth_ReadHeader: Short packet header (%u > %zu) !\n",
459 (int)(sizeof authp->in.hdr), len);
467 auth_ReadName(struct authinfo *authp, struct mbuf *bp, size_t len)
469 if (len > sizeof authp->in.name - 1)
470 log_Printf(LogWARN, "auth_ReadName: Name too long (%zu) !\n", len);
472 size_t mlen = m_length(bp);
475 log_Printf(LogWARN, "auth_ReadName: Short packet (%zu > %zu) !\n",
478 bp = mbuf_Read(bp, (u_char *)authp->in.name, len);
479 authp->in.name[len] = '\0';
484 *authp->in.name = '\0';