801e745ab7c31adca05aa0f90a804ec65ce7692e
[dragonfly.git] / usr.sbin / ppp / pap.c
1 /*-
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)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/usr.sbin/ppp/pap.c,v 1.42.2.4 2002/09/01 02:12:29 brian Exp $
29  * $DragonFly: src/usr.sbin/ppp/pap.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38
39 #include <stdlib.h>
40 #include <string.h>             /* strlen/memcpy */
41 #include <termios.h>
42
43 #include "layer.h"
44 #include "mbuf.h"
45 #include "log.h"
46 #include "defs.h"
47 #include "timer.h"
48 #include "fsm.h"
49 #include "auth.h"
50 #include "pap.h"
51 #include "lqr.h"
52 #include "hdlc.h"
53 #include "lcp.h"
54 #include "proto.h"
55 #include "async.h"
56 #include "throughput.h"
57 #include "ccp.h"
58 #include "link.h"
59 #include "descriptor.h"
60 #include "physical.h"
61 #include "iplist.h"
62 #include "slcompress.h"
63 #include "ncpaddr.h"
64 #include "ipcp.h"
65 #include "filter.h"
66 #include "mp.h"
67 #ifndef NORADIUS
68 #include "radius.h"
69 #endif
70 #include "ipv6cp.h"
71 #include "ncp.h"
72 #include "bundle.h"
73 #include "chat.h"
74 #include "chap.h"
75 #include "cbcp.h"
76 #include "datalink.h"
77
78 static const char * const papcodes[] = {
79   "???", "REQUEST", "SUCCESS", "FAILURE"
80 };
81 #define MAXPAPCODE (sizeof papcodes / sizeof papcodes[0] - 1)
82
83 static void
84 pap_Req(struct authinfo *authp)
85 {
86   struct bundle *bundle = authp->physical->dl->bundle;
87   struct fsmheader lh;
88   struct mbuf *bp;
89   u_char *cp;
90   int namelen, keylen, plen;
91
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;
100   lh.id = authp->id;
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);
105   *cp++ = namelen;
106   memcpy(cp, bundle->cfg.auth.name, namelen);
107   cp += namelen;
108   *cp++ = keylen;
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);
112 }
113
114 static void
115 SendPapCode(struct authinfo *authp, int code, const char *message)
116 {
117   struct fsmheader lh;
118   struct mbuf *bp;
119   u_char *cp;
120   int plen, mlen;
121
122   lh.code = code;
123   lh.id = authp->id;
124   mlen = strlen(message);
125   plen = mlen + 1;
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);
130   /*
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() !)
134    */
135   *cp++ = mlen > 255 ? 255 : mlen;
136   memcpy(cp, message, mlen);
137   log_Printf(LogPHASE, "Pap Output: %s\n", papcodes[code]);
138
139   link_PushPacket(&authp->physical->link, bp, authp->physical->dl->bundle,
140                   LINK_QUEUES(&authp->physical->link) - 1, PROTO_PAP);
141 }
142
143 static void
144 pap_Success(struct authinfo *authp)
145 {
146   struct bundle *bundle = authp->physical->dl->bundle;
147
148   datalink_GotAuthname(authp->physical->dl, authp->in.name);
149 #ifndef NORADIUS
150   if (*bundle->radius.cfg.file && bundle->radius.repstr)
151     SendPapCode(authp, PAP_ACK, bundle->radius.repstr);
152   else
153 #endif
154     SendPapCode(authp, PAP_ACK, "Greetings!!");
155   authp->physical->link.lcp.auth_ineed = 0;
156   if (Enabled(bundle, OPT_UTMP))
157     physical_Login(authp->physical, authp->in.name);
158
159   if (authp->physical->link.lcp.auth_iwait == 0)
160     /*
161      * Either I didn't need to authenticate, or I've already been
162      * told that I got the answer right.
163      */
164     datalink_AuthOk(authp->physical->dl);
165 }
166
167 static void
168 pap_Failure(struct authinfo *authp)
169 {
170   SendPapCode(authp, PAP_NAK, "Login incorrect");
171   datalink_AuthNotOk(authp->physical->dl);
172 }
173
174 void
175 pap_Init(struct authinfo *pap, struct physical *p)
176 {
177   auth_Init(pap, p, pap_Req, pap_Success, pap_Failure);
178 }
179
180 struct mbuf *
181 pap_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
182 {
183   struct physical *p = link2physical(l);
184   struct authinfo *authp = &p->dl->pap;
185   u_char nlen, klen, *key;
186   const char *txt;
187   int txtlen;
188
189   if (p == NULL) {
190     log_Printf(LogERROR, "pap_Input: Not a physical link - dropped\n");
191     m_freem(bp);
192     return NULL;
193   }
194
195   if (bundle_Phase(bundle) != PHASE_NETWORK &&
196       bundle_Phase(bundle) != PHASE_AUTHENTICATE) {
197     log_Printf(LogPHASE, "Unexpected pap input - dropped !\n");
198     m_freem(bp);
199     return NULL;
200   }
201
202   if ((bp = auth_ReadHeader(authp, bp)) == NULL &&
203       ntohs(authp->in.hdr.length) == 0) {
204     log_Printf(LogWARN, "Pap Input: Truncated header !\n");
205     return NULL;
206   }
207
208   if (authp->in.hdr.code == 0 || authp->in.hdr.code > MAXPAPCODE) {
209     log_Printf(LogPHASE, "Pap Input: %d: Bad PAP code !\n", authp->in.hdr.code);
210     m_freem(bp);
211     return NULL;
212   }
213
214   if (authp->in.hdr.code != PAP_REQUEST && authp->id != authp->in.hdr.id &&
215       Enabled(bundle, OPT_IDCHECK)) {
216     /* Wrong conversation dude ! */
217     log_Printf(LogPHASE, "Pap Input: %s dropped (got id %d, not %d)\n",
218                papcodes[authp->in.hdr.code], authp->in.hdr.id, authp->id);
219     m_freem(bp);
220     return NULL;
221   }
222   m_settype(bp, MB_PAPIN);
223   authp->id = authp->in.hdr.id;         /* We respond with this id */
224
225   if (bp) {
226     bp = mbuf_Read(bp, &nlen, 1);
227     if (authp->in.hdr.code == PAP_ACK) {
228       /*
229        * Don't restrict the length of our acknowledgement freetext to
230        * nlen (a one-byte length).  Show the rest of the ack packet
231        * instead.  This isn't really part of the protocol.....
232        */
233       bp = m_pullup(bp);
234       txt = MBUF_CTOP(bp);
235       txtlen = m_length(bp);
236     } else {
237       bp = auth_ReadName(authp, bp, nlen);
238       txt = authp->in.name;
239       txtlen = strlen(authp->in.name);
240     }
241   } else {
242     txt = "";
243     txtlen = 0;
244   }
245
246   log_Printf(LogPHASE, "Pap Input: %s (%.*s)\n",
247              papcodes[authp->in.hdr.code], txtlen, txt);
248
249   switch (authp->in.hdr.code) {
250     case PAP_REQUEST:
251       if (bp == NULL) {
252         log_Printf(LogPHASE, "Pap Input: No key given !\n");
253         break;
254       }
255       bp = mbuf_Read(bp, &klen, 1);
256       if (m_length(bp) < klen) {
257         log_Printf(LogERROR, "Pap Input: Truncated key !\n");
258         break;
259       }
260       if ((key = malloc(klen+1)) == NULL) {
261         log_Printf(LogERROR, "Pap Input: Out of memory !\n");
262         break;
263       }
264       bp = mbuf_Read(bp, key, klen);
265       key[klen] = '\0';
266
267 #ifndef NORADIUS
268       if (*bundle->radius.cfg.file) {
269         if (!radius_Authenticate(&bundle->radius, authp, authp->in.name,
270                                  key, strlen(key), NULL, 0))
271           pap_Failure(authp);
272       } else
273 #endif
274       if (auth_Validate(bundle, authp->in.name, key))
275         pap_Success(authp);
276       else
277         pap_Failure(authp);
278
279       free(key);
280       break;
281
282     case PAP_ACK:
283       auth_StopTimer(authp);
284       if (p->link.lcp.auth_iwait == PROTO_PAP) {
285         p->link.lcp.auth_iwait = 0;
286         if (p->link.lcp.auth_ineed == 0)
287           /*
288            * We've succeeded in our ``login''
289            * If we're not expecting  the peer to authenticate (or he already
290            * has), proceed to network phase.
291            */
292           datalink_AuthOk(p->dl);
293       }
294       break;
295
296     case PAP_NAK:
297       auth_StopTimer(authp);
298       datalink_AuthNotOk(p->dl);
299       break;
300   }
301
302   m_freem(bp);
303   return NULL;
304 }