xref: /freebsd/usr.sbin/ppp/pap.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
1 /*
2  *			PPP PAP Module
3  *
4  *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5  *
6  *   Copyright (C) 1993-94, Internet Initiative Japan, Inc.
7  *		     All rights reserverd.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that the above copyright notice and this paragraph are
11  * duplicated in all such forms and that any documentation,
12  * advertising materials, and other materials related to such
13  * distribution and use acknowledge that the software was developed
14  * by the Internet Initiative Japan, Inc.  The name of the
15  * IIJ may not be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * $FreeBSD$
22  *
23  *	TODO:
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 <stdlib.h>
32 #include <string.h>		/* strlen/memcpy */
33 #include <termios.h>
34 
35 #include "layer.h"
36 #include "mbuf.h"
37 #include "log.h"
38 #include "defs.h"
39 #include "timer.h"
40 #include "fsm.h"
41 #include "lcp.h"
42 #include "auth.h"
43 #include "pap.h"
44 #include "lqr.h"
45 #include "hdlc.h"
46 #include "proto.h"
47 #include "async.h"
48 #include "throughput.h"
49 #include "ccp.h"
50 #include "link.h"
51 #include "descriptor.h"
52 #include "physical.h"
53 #include "iplist.h"
54 #include "slcompress.h"
55 #include "ipcp.h"
56 #include "filter.h"
57 #include "mp.h"
58 #ifndef NORADIUS
59 #include "radius.h"
60 #endif
61 #include "bundle.h"
62 #include "chat.h"
63 #include "chap.h"
64 #include "cbcp.h"
65 #include "datalink.h"
66 
67 static const char *papcodes[] = { "???", "REQUEST", "SUCCESS", "FAILURE" };
68 #define MAXPAPCODE (sizeof papcodes / sizeof papcodes[0] - 1)
69 
70 static void
71 pap_Req(struct authinfo *authp)
72 {
73   struct bundle *bundle = authp->physical->dl->bundle;
74   struct fsmheader lh;
75   struct mbuf *bp;
76   u_char *cp;
77   int namelen, keylen, plen;
78 
79   namelen = strlen(bundle->cfg.auth.name);
80   keylen = strlen(bundle->cfg.auth.key);
81   plen = namelen + keylen + 2;
82   log_Printf(LogDEBUG, "pap_Req: namelen = %d, keylen = %d\n", namelen, keylen);
83   log_Printf(LogPHASE, "Pap Output: %s ********\n", bundle->cfg.auth.name);
84   if (*bundle->cfg.auth.name == '\0')
85     log_Printf(LogWARN, "Sending empty PAP authname!\n");
86   lh.code = PAP_REQUEST;
87   lh.id = authp->id;
88   lh.length = htons(plen + sizeof(struct fsmheader));
89   bp = m_get(plen + sizeof(struct fsmheader), MB_PAPOUT);
90   memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
91   cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
92   *cp++ = namelen;
93   memcpy(cp, bundle->cfg.auth.name, namelen);
94   cp += namelen;
95   *cp++ = keylen;
96   memcpy(cp, bundle->cfg.auth.key, keylen);
97   link_PushPacket(&authp->physical->link, bp, bundle,
98                   LINK_QUEUES(&authp->physical->link) - 1, PROTO_PAP);
99 }
100 
101 static void
102 SendPapCode(struct authinfo *authp, int code, const char *message)
103 {
104   struct fsmheader lh;
105   struct mbuf *bp;
106   u_char *cp;
107   int plen, mlen;
108 
109   lh.code = code;
110   lh.id = authp->id;
111   mlen = strlen(message);
112   plen = mlen + 1;
113   lh.length = htons(plen + sizeof(struct fsmheader));
114   bp = m_get(plen + sizeof(struct fsmheader), MB_PAPOUT);
115   memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
116   cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
117   /*
118    * If our message is longer than 255 bytes, truncate the length to
119    * 255 and send the entire message anyway.  Maybe the other end will
120    * display it... (see pap_Input() !)
121    */
122   *cp++ = mlen > 255 ? 255 : mlen;
123   memcpy(cp, message, mlen);
124   log_Printf(LogPHASE, "Pap Output: %s\n", papcodes[code]);
125 
126   link_PushPacket(&authp->physical->link, bp, authp->physical->dl->bundle,
127                   LINK_QUEUES(&authp->physical->link) - 1, PROTO_PAP);
128 }
129 
130 static void
131 pap_Success(struct authinfo *authp)
132 {
133   datalink_GotAuthname(authp->physical->dl, authp->in.name);
134   SendPapCode(authp, PAP_ACK, "Greetings!!");
135   authp->physical->link.lcp.auth_ineed = 0;
136   if (Enabled(authp->physical->dl->bundle, OPT_UTMP))
137     physical_Login(authp->physical, authp->in.name);
138 
139   if (authp->physical->link.lcp.auth_iwait == 0)
140     /*
141      * Either I didn't need to authenticate, or I've already been
142      * told that I got the answer right.
143      */
144     datalink_AuthOk(authp->physical->dl);
145 }
146 
147 static void
148 pap_Failure(struct authinfo *authp)
149 {
150   SendPapCode(authp, PAP_NAK, "Login incorrect");
151   datalink_AuthNotOk(authp->physical->dl);
152 }
153 
154 void
155 pap_Init(struct authinfo *pap, struct physical *p)
156 {
157   auth_Init(pap, p, pap_Req, pap_Success, pap_Failure);
158 }
159 
160 struct mbuf *
161 pap_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
162 {
163   struct physical *p = link2physical(l);
164   struct authinfo *authp = &p->dl->pap;
165   u_char nlen, klen, *key;
166   const char *txt;
167   int txtlen;
168 
169   if (p == NULL) {
170     log_Printf(LogERROR, "pap_Input: Not a physical link - dropped\n");
171     m_freem(bp);
172     return NULL;
173   }
174 
175   if (bundle_Phase(bundle) != PHASE_NETWORK &&
176       bundle_Phase(bundle) != PHASE_AUTHENTICATE) {
177     log_Printf(LogPHASE, "Unexpected pap input - dropped !\n");
178     m_freem(bp);
179     return NULL;
180   }
181 
182   if ((bp = auth_ReadHeader(authp, bp)) == NULL &&
183       ntohs(authp->in.hdr.length) == 0) {
184     log_Printf(LogWARN, "Pap Input: Truncated header !\n");
185     return NULL;
186   }
187 
188   if (authp->in.hdr.code == 0 || authp->in.hdr.code > MAXPAPCODE) {
189     log_Printf(LogPHASE, "Pap Input: %d: Bad PAP code !\n", authp->in.hdr.code);
190     m_freem(bp);
191     return NULL;
192   }
193 
194   if (authp->in.hdr.code != PAP_REQUEST && authp->id != authp->in.hdr.id &&
195       Enabled(bundle, OPT_IDCHECK)) {
196     /* Wrong conversation dude ! */
197     log_Printf(LogPHASE, "Pap Input: %s dropped (got id %d, not %d)\n",
198                papcodes[authp->in.hdr.code], authp->in.hdr.id, authp->id);
199     m_freem(bp);
200     return NULL;
201   }
202   m_settype(bp, MB_PAPIN);
203   authp->id = authp->in.hdr.id;		/* We respond with this id */
204 
205   if (bp) {
206     bp = mbuf_Read(bp, &nlen, 1);
207     if (authp->in.hdr.code == PAP_ACK) {
208       /*
209        * Don't restrict the length of our acknowledgement freetext to
210        * nlen (a one-byte length).  Show the rest of the ack packet
211        * instead.  This isn't really part of the protocol.....
212        */
213       bp = m_pullup(bp);
214       txt = MBUF_CTOP(bp);
215       txtlen = m_length(bp);
216     } else {
217       bp = auth_ReadName(authp, bp, nlen);
218       txt = authp->in.name;
219       txtlen = strlen(authp->in.name);
220     }
221   } else {
222     txt = "";
223     txtlen = 0;
224   }
225 
226   log_Printf(LogPHASE, "Pap Input: %s (%.*s)\n",
227              papcodes[authp->in.hdr.code], txtlen, txt);
228 
229   switch (authp->in.hdr.code) {
230     case PAP_REQUEST:
231       if (bp == NULL) {
232         log_Printf(LogPHASE, "Pap Input: No key given !\n");
233         break;
234       }
235       bp = mbuf_Read(bp, &klen, 1);
236       if (m_length(bp) < klen) {
237         log_Printf(LogERROR, "Pap Input: Truncated key !\n");
238         break;
239       }
240       if ((key = malloc(klen+1)) == NULL) {
241         log_Printf(LogERROR, "Pap Input: Out of memory !\n");
242         break;
243       }
244       bp = mbuf_Read(bp, key, klen);
245       key[klen] = '\0';
246 
247 #ifndef NORADIUS
248       if (*bundle->radius.cfg.file)
249         radius_Authenticate(&bundle->radius, authp, authp->in.name,
250                             key, NULL);
251       else
252 #endif
253       if (auth_Validate(bundle, authp->in.name, key, p))
254         pap_Success(authp);
255       else
256         pap_Failure(authp);
257 
258       free(key);
259       break;
260 
261     case PAP_ACK:
262       auth_StopTimer(authp);
263       if (p->link.lcp.auth_iwait == PROTO_PAP) {
264         p->link.lcp.auth_iwait = 0;
265         if (p->link.lcp.auth_ineed == 0)
266           /*
267            * We've succeeded in our ``login''
268            * If we're not expecting  the peer to authenticate (or he already
269            * has), proceed to network phase.
270            */
271           datalink_AuthOk(p->dl);
272       }
273       break;
274 
275     case PAP_NAK:
276       auth_StopTimer(authp);
277       datalink_AuthNotOk(p->dl);
278       break;
279   }
280 
281   m_freem(bp);
282   return NULL;
283 }
284