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 = mbuf_Alloc(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 = mbuf_Alloc(plen + sizeof(struct fsmheader), MB_PAPOUT); 115 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader)); 116 cp = MBUF_CTOP(bp) + sizeof(struct fsmheader); 117 *cp++ = mlen; 118 memcpy(cp, message, mlen); 119 log_Printf(LogPHASE, "Pap Output: %s\n", papcodes[code]); 120 121 link_PushPacket(&authp->physical->link, bp, authp->physical->dl->bundle, 122 LINK_QUEUES(&authp->physical->link) - 1, PROTO_PAP); 123 } 124 125 static void 126 pap_Success(struct authinfo *authp) 127 { 128 datalink_GotAuthname(authp->physical->dl, authp->in.name); 129 SendPapCode(authp, PAP_ACK, "Greetings!!"); 130 authp->physical->link.lcp.auth_ineed = 0; 131 if (Enabled(authp->physical->dl->bundle, OPT_UTMP)) 132 physical_Login(authp->physical, authp->in.name); 133 134 if (authp->physical->link.lcp.auth_iwait == 0) 135 /* 136 * Either I didn't need to authenticate, or I've already been 137 * told that I got the answer right. 138 */ 139 datalink_AuthOk(authp->physical->dl); 140 } 141 142 static void 143 pap_Failure(struct authinfo *authp) 144 { 145 SendPapCode(authp, PAP_NAK, "Login incorrect"); 146 datalink_AuthNotOk(authp->physical->dl); 147 } 148 149 void 150 pap_Init(struct authinfo *pap, struct physical *p) 151 { 152 auth_Init(pap, p, pap_Req, pap_Success, pap_Failure); 153 } 154 155 struct mbuf * 156 pap_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 157 { 158 struct physical *p = link2physical(l); 159 struct authinfo *authp = &p->dl->pap; 160 u_char nlen, klen, *key; 161 162 if (p == NULL) { 163 log_Printf(LogERROR, "pap_Input: Not a physical link - dropped\n"); 164 mbuf_Free(bp); 165 return NULL; 166 } 167 168 if (bundle_Phase(bundle) != PHASE_NETWORK && 169 bundle_Phase(bundle) != PHASE_AUTHENTICATE) { 170 log_Printf(LogPHASE, "Unexpected pap input - dropped !\n"); 171 mbuf_Free(bp); 172 return NULL; 173 } 174 175 if ((bp = auth_ReadHeader(authp, bp)) == NULL && 176 ntohs(authp->in.hdr.length) == 0) { 177 log_Printf(LogWARN, "Pap Input: Truncated header !\n"); 178 return NULL; 179 } 180 181 if (authp->in.hdr.code == 0 || authp->in.hdr.code > MAXPAPCODE) { 182 log_Printf(LogPHASE, "Pap Input: %d: Bad PAP code !\n", authp->in.hdr.code); 183 mbuf_Free(bp); 184 return NULL; 185 } 186 187 if (authp->in.hdr.code != PAP_REQUEST && authp->id != authp->in.hdr.id && 188 Enabled(bundle, OPT_IDCHECK)) { 189 /* Wrong conversation dude ! */ 190 log_Printf(LogPHASE, "Pap Input: %s dropped (got id %d, not %d)\n", 191 papcodes[authp->in.hdr.code], authp->in.hdr.id, authp->id); 192 mbuf_Free(bp); 193 return NULL; 194 } 195 mbuf_SetType(bp, MB_PAPIN); 196 authp->id = authp->in.hdr.id; /* We respond with this id */ 197 198 if (bp) { 199 bp = mbuf_Read(bp, &nlen, 1); 200 bp = auth_ReadName(authp, bp, nlen); 201 } 202 203 log_Printf(LogPHASE, "Pap Input: %s (%s)\n", 204 papcodes[authp->in.hdr.code], authp->in.name); 205 206 switch (authp->in.hdr.code) { 207 case PAP_REQUEST: 208 if (bp == NULL) { 209 log_Printf(LogPHASE, "Pap Input: No key given !\n"); 210 break; 211 } 212 bp = mbuf_Read(bp, &klen, 1); 213 if (mbuf_Length(bp) < klen) { 214 log_Printf(LogERROR, "Pap Input: Truncated key !\n"); 215 break; 216 } 217 if ((key = malloc(klen+1)) == NULL) { 218 log_Printf(LogERROR, "Pap Input: Out of memory !\n"); 219 break; 220 } 221 bp = mbuf_Read(bp, key, klen); 222 key[klen] = '\0'; 223 224 #ifndef NORADIUS 225 if (*bundle->radius.cfg.file) 226 radius_Authenticate(&bundle->radius, authp, authp->in.name, 227 key, NULL); 228 else 229 #endif 230 if (auth_Validate(bundle, authp->in.name, key, p)) 231 pap_Success(authp); 232 else 233 pap_Failure(authp); 234 235 free(key); 236 break; 237 238 case PAP_ACK: 239 auth_StopTimer(authp); 240 if (p->link.lcp.auth_iwait == PROTO_PAP) { 241 p->link.lcp.auth_iwait = 0; 242 if (p->link.lcp.auth_ineed == 0) 243 /* 244 * We've succeeded in our ``login'' 245 * If we're not expecting the peer to authenticate (or he already 246 * has), proceed to network phase. 247 */ 248 datalink_AuthOk(p->dl); 249 } 250 break; 251 252 case PAP_NAK: 253 auth_StopTimer(authp); 254 datalink_AuthNotOk(p->dl); 255 break; 256 } 257 258 mbuf_Free(bp); 259 return NULL; 260 } 261