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>
39 #include <string.h> /* strlen/memcpy */
55 #include "throughput.h"
58 #include "descriptor.h"
61 #include "slcompress.h"
78 static const char * const papcodes[] = {
79 "???", "REQUEST", "SUCCESS", "FAILURE"
81 #define MAXPAPCODE (sizeof papcodes / sizeof papcodes[0] - 1)
84 pap_Req(struct authinfo *authp)
86 struct bundle *bundle = authp->physical->dl->bundle;
90 int namelen, keylen, plen;
92 namelen = strlen(bundle->cfg.auth.name);
93 keylen = strlen(bundle->cfg.auth.key);
94 plen = namelen + keylen + 2;
95 log_Printf(LogDEBUG, "pap_Req: namelen = %d, keylen = %d\n", namelen, keylen);
96 log_Printf(LogPHASE, "Pap Output: %s ********\n", bundle->cfg.auth.name);
97 if (*bundle->cfg.auth.name == '\0')
98 log_Printf(LogWARN, "Sending empty PAP authname!\n");
99 lh.code = PAP_REQUEST;
101 lh.length = htons(plen + sizeof(struct fsmheader));
102 bp = m_get(plen + sizeof(struct fsmheader), MB_PAPOUT);
103 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
104 cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
106 memcpy(cp, bundle->cfg.auth.name, namelen);
109 memcpy(cp, bundle->cfg.auth.key, keylen);
110 link_PushPacket(&authp->physical->link, bp, bundle,
111 LINK_QUEUES(&authp->physical->link) - 1, PROTO_PAP);
115 SendPapCode(struct authinfo *authp, int code, const char *message)
124 mlen = strlen(message);
126 lh.length = htons(plen + sizeof(struct fsmheader));
127 bp = m_get(plen + sizeof(struct fsmheader), MB_PAPOUT);
128 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
129 cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
131 * If our message is longer than 255 bytes, truncate the length to
132 * 255 and send the entire message anyway. Maybe the other end will
133 * display it... (see pap_Input() !)
135 *cp++ = mlen > 255 ? 255 : mlen;
136 memcpy(cp, message, mlen);
137 log_Printf(LogPHASE, "Pap Output: %s\n", papcodes[code]);
139 link_PushPacket(&authp->physical->link, bp, authp->physical->dl->bundle,
140 LINK_QUEUES(&authp->physical->link) - 1, PROTO_PAP);
144 pap_Success(struct authinfo *authp)
146 datalink_GotAuthname(authp->physical->dl, authp->in.name);
147 SendPapCode(authp, PAP_ACK, "Greetings!!");
148 authp->physical->link.lcp.auth_ineed = 0;
149 if (Enabled(authp->physical->dl->bundle, OPT_UTMP))
150 physical_Login(authp->physical, authp->in.name);
152 if (authp->physical->link.lcp.auth_iwait == 0)
154 * Either I didn't need to authenticate, or I've already been
155 * told that I got the answer right.
157 datalink_AuthOk(authp->physical->dl);
161 pap_Failure(struct authinfo *authp)
163 SendPapCode(authp, PAP_NAK, "Login incorrect");
164 datalink_AuthNotOk(authp->physical->dl);
168 pap_Init(struct authinfo *pap, struct physical *p)
170 auth_Init(pap, p, pap_Req, pap_Success, pap_Failure);
174 pap_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
176 struct physical *p = link2physical(l);
177 struct authinfo *authp = &p->dl->pap;
178 u_char nlen, klen, *key;
183 log_Printf(LogERROR, "pap_Input: Not a physical link - dropped\n");
188 if (bundle_Phase(bundle) != PHASE_NETWORK &&
189 bundle_Phase(bundle) != PHASE_AUTHENTICATE) {
190 log_Printf(LogPHASE, "Unexpected pap input - dropped !\n");
195 if ((bp = auth_ReadHeader(authp, bp)) == NULL &&
196 ntohs(authp->in.hdr.length) == 0) {
197 log_Printf(LogWARN, "Pap Input: Truncated header !\n");
201 if (authp->in.hdr.code == 0 || authp->in.hdr.code > MAXPAPCODE) {
202 log_Printf(LogPHASE, "Pap Input: %d: Bad PAP code !\n", authp->in.hdr.code);
207 if (authp->in.hdr.code != PAP_REQUEST && authp->id != authp->in.hdr.id &&
208 Enabled(bundle, OPT_IDCHECK)) {
209 /* Wrong conversation dude ! */
210 log_Printf(LogPHASE, "Pap Input: %s dropped (got id %d, not %d)\n",
211 papcodes[authp->in.hdr.code], authp->in.hdr.id, authp->id);
215 m_settype(bp, MB_PAPIN);
216 authp->id = authp->in.hdr.id; /* We respond with this id */
219 bp = mbuf_Read(bp, &nlen, 1);
220 if (authp->in.hdr.code == PAP_ACK) {
222 * Don't restrict the length of our acknowledgement freetext to
223 * nlen (a one-byte length). Show the rest of the ack packet
224 * instead. This isn't really part of the protocol.....
228 txtlen = m_length(bp);
230 bp = auth_ReadName(authp, bp, nlen);
231 txt = authp->in.name;
232 txtlen = strlen(authp->in.name);
239 log_Printf(LogPHASE, "Pap Input: %s (%.*s)\n",
240 papcodes[authp->in.hdr.code], txtlen, txt);
242 switch (authp->in.hdr.code) {
245 log_Printf(LogPHASE, "Pap Input: No key given !\n");
248 bp = mbuf_Read(bp, &klen, 1);
249 if (m_length(bp) < klen) {
250 log_Printf(LogERROR, "Pap Input: Truncated key !\n");
253 if ((key = malloc(klen+1)) == NULL) {
254 log_Printf(LogERROR, "Pap Input: Out of memory !\n");
257 bp = mbuf_Read(bp, key, klen);
261 if (*bundle->radius.cfg.file)
262 radius_Authenticate(&bundle->radius, authp, authp->in.name,
263 key, strlen(key), NULL, 0);
266 if (auth_Validate(bundle, authp->in.name, key, p))
275 auth_StopTimer(authp);
276 if (p->link.lcp.auth_iwait == PROTO_PAP) {
277 p->link.lcp.auth_iwait = 0;
278 if (p->link.lcp.auth_ineed == 0)
280 * We've succeeded in our ``login''
281 * If we're not expecting the peer to authenticate (or he already
282 * has), proceed to network phase.
284 datalink_AuthOk(p->dl);
289 auth_StopTimer(authp);
290 datalink_AuthNotOk(p->dl);