4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
6 * Copyright (C) 1993-94, Internet Initiative Japan, Inc.
7 * All rights reserverd.
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.
21 * $Id: pap.c,v 1.19 1997/11/22 03:37:43 brian Exp $
25 #include <sys/param.h>
26 #include <netinet/in.h>
48 #include "loadalias.h"
55 static const char *papcodes[] = { "???", "REQUEST", "ACK", "NAK" };
58 SendPapChallenge(int papid)
63 int namelen, keylen, plen;
65 namelen = strlen(VarAuthName);
66 keylen = strlen(VarAuthKey);
67 plen = namelen + keylen + 2;
68 LogPrintf(LogDEBUG, "SendPapChallenge: namelen = %d, keylen = %d\n",
70 if (LogIsKept(LogDEBUG))
71 LogPrintf(LogPHASE, "PAP: %s (%s)\n", VarAuthName, VarAuthKey);
73 LogPrintf(LogPHASE, "PAP: %s\n", VarAuthName);
74 lh.code = PAP_REQUEST;
76 lh.length = htons(plen + sizeof(struct fsmheader));
77 bp = mballoc(plen + sizeof(struct fsmheader), MB_FSM);
78 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
79 cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
81 memcpy(cp, VarAuthName, namelen);
84 memcpy(cp, VarAuthKey, keylen);
86 HdlcOutput(PRI_LINK, PROTO_PAP, bp);
89 struct authinfo AuthPapInfo = {
94 SendPapCode(int id, int code, const char *message)
103 mlen = strlen(message);
105 lh.length = htons(plen + sizeof(struct fsmheader));
106 bp = mballoc(plen + sizeof(struct fsmheader), MB_FSM);
107 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
108 cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
110 memcpy(cp, message, mlen);
111 LogPrintf(LogPHASE, "PapOutput: %s\n", papcodes[code]);
112 HdlcOutput(PRI_LINK, PROTO_PAP, bp);
116 * Validate given username and passwrd against with secret table
119 PapValidate(u_char * name, u_char * key)
127 LogPrintf(LogDEBUG, "PapValidate: name %s (%d), key %s (%d)\n",
128 name, nlen, key, klen);
131 if (Enabled(ConfPasswdAuth)) {
135 LogPrintf(LogLCP, "Using PasswdAuth\n");
136 result = (pwd = getpwnam(name)) &&
137 !strcmp(crypt(key, pwd->pw_passwd), pwd->pw_passwd);
143 return (AuthValidate(SECRETFILE, name, key));
147 PapInput(struct mbuf * bp)
149 int len = plength(bp);
150 struct fsmheader *php;
151 struct lcpstate *lcp = &LcpInfo;
154 if (len >= sizeof(struct fsmheader)) {
155 php = (struct fsmheader *) MBUF_CTOP(bp);
156 if (len >= ntohs(php->length)) {
157 if (php->code < PAP_REQUEST || php->code > PAP_NAK)
159 LogPrintf(LogPHASE, "PapInput: %s\n", papcodes[php->code]);
163 cp = (u_char *) (php + 1);
164 if (PapValidate(cp, cp + *cp + 1)) {
165 SendPapCode(php->id, PAP_ACK, "Greetings!!");
167 if (lcp->auth_iwait == 0) {
168 if ((mode & MODE_DIRECT) && isatty(modem) && Enabled(ConfUtmp))
170 LogPrintf(LogERROR, "Oops, already logged in on %s\n",
174 memset(&ut, 0, sizeof ut);
176 strncpy(ut.ut_name, cp+1, sizeof ut.ut_name - 1);
177 strncpy(ut.ut_line, VarBaseDevice, sizeof ut.ut_line - 1);
178 if (logout(ut.ut_line))
179 logwtmp(ut.ut_line, "", "");
183 NewPhase(PHASE_NETWORK);
186 SendPapCode(php->id, PAP_NAK, "Login incorrect");
187 reconnect(RECON_FALSE);
192 StopAuthTimer(&AuthPapInfo);
193 cp = (u_char *) (php + 1);
196 LogPrintf(LogPHASE, "Received PAP_ACK (%s)\n", cp);
197 if (lcp->auth_iwait == PROTO_PAP) {
199 if (lcp->auth_ineed == 0)
200 NewPhase(PHASE_NETWORK);
204 StopAuthTimer(&AuthPapInfo);
205 cp = (u_char *) (php + 1);
208 LogPrintf(LogPHASE, "Received PAP_NAK (%s)\n", cp);
209 reconnect(RECON_FALSE);