2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6 * Internet Initiative Japan, Inc (IIJ)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <sys/socket.h>
48 #include <security/pam_appl.h>
50 #include <security/openpam.h>
61 #include "throughput.h"
62 #include "slcompress.h"
72 #include "descriptor.h"
90 Auth2Nam(u_short auth, u_char type)
98 snprintf(chap, sizeof chap, "CHAP 0x%02x", type);
106 #if !defined(NOPAM) && !defined(OPENPAM)
108 pam_conv(int n, const struct pam_message **msg, struct pam_response **resp,
112 if (n != 1 || msg[0]->msg_style != PAM_PROMPT_ECHO_OFF)
113 return (PAM_CONV_ERR);
114 if ((*resp = malloc(sizeof(struct pam_response))) == NULL)
115 return (PAM_CONV_ERR);
116 (*resp)[0].resp = strdup((const char *)data);
117 (*resp)[0].resp_retcode = 0;
119 return ((*resp)[0].resp != NULL ? PAM_SUCCESS : PAM_CONV_ERR);
121 #endif /* !defined(NOPAM) && !defined(OPENPAM) */
124 auth_CheckPasswd(const char *name, const char *data, const char *key)
126 if (!strcmp(data, "*")) {
128 /* Then look up the real password database */
136 cryptpw = crypt(key, pw->pw_passwd);
138 result = (cryptpw != NULL) && !strcmp(cryptpw, pw->pw_passwd);
145 /* Then consult with PAM. */
149 struct pam_conv pamc = {
151 &openpam_nullconv, NULL
157 if (pam_start("ppp", name, &pamc, &pamh) != PAM_SUCCESS)
160 if ((status = pam_set_item(pamh, PAM_AUTHTOK, key)) == PAM_SUCCESS)
162 status = pam_authenticate(pamh, 0);
163 pam_end(pamh, status);
164 return (status == PAM_SUCCESS);
168 return !strcmp(data, key);
172 auth_SetPhoneList(const char *name, char *phone, int phonelen)
176 char *vector[6], buff[LINE_LEN];
179 fp = OpenSecret(SECRETFILE);
183 while (fgets(buff, sizeof buff, fp)) {
187 buff[strlen(buff) - 1] = '\0';
188 memset(vector, '\0', sizeof vector);
189 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
190 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
193 if (strcmp(vector[0], name) == 0) {
195 if (*vector[4] == '\0')
197 strncpy(phone, vector[4], phonelen - 1);
198 phone[phonelen - 1] = '\0';
199 return 1; /* Valid */
203 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
204 /* Look for the name without the leading domain */
217 auth_Select(struct bundle *bundle, const char *name)
221 char *vector[5], buff[LINE_LEN];
225 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
230 if (bundle->radius.valid && bundle->radius.ip.s_addr != INADDR_NONE &&
231 bundle->radius.ip.s_addr != RADIUS_INADDR_POOL) {
232 /* We've got a radius IP - it overrides everything */
233 if (!ipcp_UseHisIPaddr(bundle, bundle->radius.ip))
235 ipcp_Setup(&bundle->ncp.ipcp, bundle->radius.mask.s_addr);
236 /* Continue with ppp.secret in case we've got a new label */
240 fp = OpenSecret(SECRETFILE);
244 while (fgets(buff, sizeof buff, fp)) {
248 buff[strlen(buff) - 1] = '\0';
249 memset(vector, '\0', sizeof vector);
250 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
251 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
254 if (strcmp(vector[0], name) == 0) {
257 if (!bundle->radius.valid || bundle->radius.ip.s_addr == INADDR_NONE) {
259 if (n > 2 && *vector[2] && strcmp(vector[2], "*") &&
260 !ipcp_UseHisaddr(bundle, vector[2], 1))
262 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
266 if (n > 3 && *vector[3] && strcmp(vector[3], "*"))
267 bundle_SetLabel(bundle, vector[3]);
268 return 1; /* Valid */
272 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
273 /* Look for the name without the leading domain */
283 /* Let 'em in anyway - they must have been in the passwd file */
284 ipcp_Setup(&bundle->ncp.ipcp, INADDR_NONE);
288 if (bundle->radius.valid)
292 /* Disappeared from ppp.secret ??? */
298 auth_Validate(struct bundle *bundle, const char *name, const char *key)
300 /* Used by PAP routines */
304 char *vector[5], buff[LINE_LEN];
307 fp = OpenSecret(SECRETFILE);
311 while (fgets(buff, sizeof buff, fp)) {
315 buff[strlen(buff) - 1] = 0;
316 memset(vector, '\0', sizeof vector);
317 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
318 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
321 if (strcmp(vector[0], name) == 0) {
323 return auth_CheckPasswd(name, vector[1], key);
328 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
329 /* Look for the name without the leading domain */
341 if (Enabled(bundle, OPT_PASSWDAUTH))
342 return auth_CheckPasswd(name, "*", key);
345 return 0; /* Invalid */
349 auth_GetSecret(const char *name, size_t len)
351 /* Used by CHAP routines */
357 static char buff[LINE_LEN]; /* vector[] will point here when returned */
359 fp = OpenSecret(SECRETFILE);
365 while (fgets(buff, sizeof buff, fp)) {
369 n = strlen(buff) - 1;
371 buff[n] = '\0'; /* Trim the '\n' */
372 memset(vector, '\0', sizeof vector);
373 if ((n = MakeArgs(buff, vector, VECSIZE(vector), PARSE_REDUCE)) < 0)
374 log_Printf(LogWARN, "%s: %d: Invalid line\n", SECRETFILE, lineno);
377 if (strlen(vector[0]) == len && strncmp(vector[0], name, len) == 0) {
383 if ((slash = strrchr(name, '\\')) != NULL && slash[1]) {
384 /* Go back and look for the name without the leading domain */
385 len -= slash - name + 1;
392 return (NULL); /* Invalid */
396 AuthTimeout(void *vauthp)
398 struct authinfo *authp = (struct authinfo *)vauthp;
400 timer_Stop(&authp->authtimer);
401 if (--authp->retry > 0) {
403 (*authp->fn.req)(authp);
404 timer_Start(&authp->authtimer);
406 log_Printf(LogPHASE, "Auth: No response from server\n");
407 datalink_AuthNotOk(authp->physical->dl);
412 auth_Init(struct authinfo *authp, struct physical *p, auth_func req,
413 auth_func success, auth_func failure)
415 memset(authp, '\0', sizeof(struct authinfo));
416 authp->cfg.fsm.timeout = DEF_FSMRETRY;
417 authp->cfg.fsm.maxreq = DEF_FSMAUTHTRIES;
418 authp->cfg.fsm.maxtrm = 0; /* not used */
420 authp->fn.success = success;
421 authp->fn.failure = failure;
426 auth_StartReq(struct authinfo *authp)
428 timer_Stop(&authp->authtimer);
429 authp->authtimer.func = AuthTimeout;
430 authp->authtimer.name = "auth";
431 authp->authtimer.load = authp->cfg.fsm.timeout * SECTICKS;
432 authp->authtimer.arg = (void *)authp;
433 authp->retry = authp->cfg.fsm.maxreq;
435 (*authp->fn.req)(authp);
436 timer_Start(&authp->authtimer);
440 auth_StopTimer(struct authinfo *authp)
442 timer_Stop(&authp->authtimer);
446 auth_ReadHeader(struct authinfo *authp, struct mbuf *bp)
451 if (len >= sizeof authp->in.hdr) {
452 bp = mbuf_Read(bp, (u_char *)&authp->in.hdr, sizeof authp->in.hdr);
453 if (len >= ntohs(authp->in.hdr.length))
455 authp->in.hdr.length = htons(0);
456 log_Printf(LogWARN, "auth_ReadHeader: Short packet (%u > %zu) !\n",
457 ntohs(authp->in.hdr.length), len);
459 authp->in.hdr.length = htons(0);
460 log_Printf(LogWARN, "auth_ReadHeader: Short packet header (%u > %zu) !\n",
461 (int)(sizeof authp->in.hdr), len);
469 auth_ReadName(struct authinfo *authp, struct mbuf *bp, size_t len)
471 if (len > sizeof authp->in.name - 1)
472 log_Printf(LogWARN, "auth_ReadName: Name too long (%zu) !\n", len);
474 size_t mlen = m_length(bp);
477 log_Printf(LogWARN, "auth_ReadName: Short packet (%zu > %zu) !\n",
480 bp = mbuf_Read(bp, (u_char *)authp->in.name, len);
481 authp->in.name[len] = '\0';
486 *authp->in.name = '\0';