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.17 1997/10/16 23:55:19 brian Exp $
25 #include <sys/param.h>
26 #include <netinet/in.h>
47 #include "loadalias.h"
55 static char *papcodes[] = {
56 "???", "REQUEST", "ACK", "NAK"
60 SendPapChallenge(int papid)
65 int namelen, keylen, plen;
67 namelen = strlen(VarAuthName);
68 keylen = strlen(VarAuthKey);
69 plen = namelen + keylen + 2;
70 LogPrintf(LogDEBUG, "SendPapChallenge: namelen = %d, keylen = %d\n",
72 if (LogIsKept(LogDEBUG))
73 LogPrintf(LogPHASE, "PAP: %s (%s)\n", VarAuthName, VarAuthKey);
75 LogPrintf(LogPHASE, "PAP: %s\n", VarAuthName);
76 lh.code = PAP_REQUEST;
78 lh.length = htons(plen + sizeof(struct fsmheader));
79 bp = mballoc(plen + sizeof(struct fsmheader), MB_FSM);
80 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
81 cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
83 memcpy(cp, VarAuthName, namelen);
86 memcpy(cp, VarAuthKey, keylen);
88 HdlcOutput(PRI_LINK, PROTO_PAP, bp);
91 struct authinfo AuthPapInfo = {
96 SendPapCode(int id, int code, char *message)
105 mlen = strlen(message);
107 lh.length = htons(plen + sizeof(struct fsmheader));
108 bp = mballoc(plen + sizeof(struct fsmheader), MB_FSM);
109 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
110 cp = MBUF_CTOP(bp) + sizeof(struct fsmheader);
112 memcpy(cp, message, mlen);
113 LogPrintf(LogPHASE, "PapOutput: %s\n", papcodes[code]);
114 HdlcOutput(PRI_LINK, PROTO_PAP, bp);
118 * Validate given username and passwrd against with secret table
121 PapValidate(u_char * name, u_char * key)
129 LogPrintf(LogDEBUG, "PapValidate: name %s (%d), key %s (%d)\n",
130 name, nlen, key, klen);
133 if (Enabled(ConfPasswdAuth)) {
137 LogPrintf(LogLCP, "Using PasswdAuth\n");
138 result = (pwd = getpwnam(name)) &&
139 !strcmp(crypt(key, pwd->pw_passwd), pwd->pw_passwd);
145 return (AuthValidate(SECRETFILE, name, key));
149 PapInput(struct mbuf * bp)
151 int len = plength(bp);
152 struct fsmheader *php;
153 struct lcpstate *lcp = &LcpInfo;
156 if (len >= sizeof(struct fsmheader)) {
157 php = (struct fsmheader *) MBUF_CTOP(bp);
158 if (len >= ntohs(php->length)) {
159 if (php->code < PAP_REQUEST || php->code > PAP_NAK)
161 LogPrintf(LogPHASE, "PapInput: %s\n", papcodes[php->code]);
165 cp = (u_char *) (php + 1);
166 if (PapValidate(cp, cp + *cp + 1)) {
167 SendPapCode(php->id, PAP_ACK, "Greetings!!");
169 if (lcp->auth_iwait == 0) {
170 if ((mode & MODE_DIRECT) && isatty(modem) && Enabled(ConfUtmp))
172 LogPrintf(LogERROR, "Oops, already logged in on %s\n",
176 memset(&ut, 0, sizeof(ut));
178 strncpy(ut.ut_name, cp+1, sizeof(ut.ut_name)-1);
179 strncpy(ut.ut_line, VarBaseDevice, sizeof(ut.ut_line)-1);
180 if (logout(ut.ut_line))
181 logwtmp(ut.ut_line, "", "");
185 NewPhase(PHASE_NETWORK);
188 SendPapCode(php->id, PAP_NAK, "Login incorrect");
189 reconnect(RECON_FALSE);
194 StopAuthTimer(&AuthPapInfo);
195 cp = (u_char *) (php + 1);
198 LogPrintf(LogPHASE, "Received PAP_ACK (%s)\n", cp);
199 if (lcp->auth_iwait == PROTO_PAP) {
201 if (lcp->auth_ineed == 0)
202 NewPhase(PHASE_NETWORK);
206 StopAuthTimer(&AuthPapInfo);
207 cp = (u_char *) (php + 1);
210 LogPrintf(LogPHASE, "Received PAP_NAK (%s)\n", cp);
211 reconnect(RECON_FALSE);