2 * upap.c - User/Password Authentication Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 static char rcsid[] = "$Id: upap.c,v 1.6 1995/06/12 12:02:24 paulus Exp $";
30 #include <sys/types.h>
38 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
41 static void upap_timeout __P((caddr_t));
42 static void upap_reqtimeout __P((caddr_t));
43 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
44 static void upap_rauthack __P((upap_state *, u_char *, int, int));
45 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
46 static void upap_sauthreq __P((upap_state *));
47 static void upap_sresp __P((upap_state *, int, int, char *, int));
51 * upap_init - Initialize a UPAP unit.
57 upap_state *u = &upap[unit];
64 u->us_clientstate = UPAPCS_INITIAL;
65 u->us_serverstate = UPAPSS_INITIAL;
67 u->us_timeouttime = UPAP_DEFTIMEOUT;
68 u->us_maxtransmits = 10;
69 u->us_reqtimeout = UPAP_DEFREQTIME;
74 * upap_authwithpeer - Authenticate us with our peer (start client).
76 * Set new state and send authenticate's.
79 upap_authwithpeer(unit, user, password)
81 char *user, *password;
83 upap_state *u = &upap[unit];
85 /* Save the username and password we're given */
87 u->us_userlen = strlen(user);
88 u->us_passwd = password;
89 u->us_passwdlen = strlen(password);
92 /* Lower layer up yet? */
93 if (u->us_clientstate == UPAPCS_INITIAL ||
94 u->us_clientstate == UPAPCS_PENDING) {
95 u->us_clientstate = UPAPCS_PENDING;
99 upap_sauthreq(u); /* Start protocol */
104 * upap_authpeer - Authenticate our peer (start server).
112 upap_state *u = &upap[unit];
114 /* Lower layer up yet? */
115 if (u->us_serverstate == UPAPSS_INITIAL ||
116 u->us_serverstate == UPAPSS_PENDING) {
117 u->us_serverstate = UPAPSS_PENDING;
121 u->us_serverstate = UPAPSS_LISTEN;
122 if (u->us_reqtimeout > 0)
123 TIMEOUT(upap_reqtimeout, (caddr_t) u, u->us_reqtimeout);
128 * upap_timeout - Retransmission timer for sending auth-reqs expired.
134 upap_state *u = (upap_state *) arg;
136 if (u->us_clientstate != UPAPCS_AUTHREQ)
139 if (u->us_transmits >= u->us_maxtransmits) {
140 /* give up in disgust */
141 syslog(LOG_ERR, "No response to PAP authenticate-requests");
142 u->us_clientstate = UPAPCS_BADAUTH;
143 auth_withpeer_fail(u->us_unit, PPP_PAP);
147 upap_sauthreq(u); /* Send Authenticate-Request */
152 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
158 upap_state *u = (upap_state *) arg;
160 if (u->us_serverstate != UPAPSS_LISTEN)
163 auth_peer_fail(u->us_unit, PPP_PAP);
164 u->us_serverstate = UPAPSS_BADAUTH;
169 * upap_lowerup - The lower layer is up.
171 * Start authenticating if pending.
177 upap_state *u = &upap[unit];
179 if (u->us_clientstate == UPAPCS_INITIAL)
180 u->us_clientstate = UPAPCS_CLOSED;
181 else if (u->us_clientstate == UPAPCS_PENDING) {
182 upap_sauthreq(u); /* send an auth-request */
185 if (u->us_serverstate == UPAPSS_INITIAL)
186 u->us_serverstate = UPAPSS_CLOSED;
187 else if (u->us_serverstate == UPAPSS_PENDING) {
188 u->us_serverstate = UPAPSS_LISTEN;
189 if (u->us_reqtimeout > 0)
190 TIMEOUT(upap_reqtimeout, (caddr_t) u, u->us_reqtimeout);
196 * upap_lowerdown - The lower layer is down.
198 * Cancel all timeouts.
204 upap_state *u = &upap[unit];
206 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
207 UNTIMEOUT(upap_timeout, (caddr_t) u); /* Cancel timeout */
208 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
209 UNTIMEOUT(upap_reqtimeout, (caddr_t) u);
211 u->us_clientstate = UPAPCS_INITIAL;
212 u->us_serverstate = UPAPSS_INITIAL;
217 * upap_protrej - Peer doesn't speak this protocol.
219 * This shouldn't happen. In any case, pretend lower layer went down.
225 upap_state *u = &upap[unit];
227 if (u->us_clientstate == UPAPCS_AUTHREQ) {
228 syslog(LOG_ERR, "PAP authentication failed due to protocol-reject");
229 auth_withpeer_fail(unit, PPP_PAP);
231 if (u->us_serverstate == UPAPSS_LISTEN) {
232 syslog(LOG_ERR, "PAP authentication of peer failed (protocol-reject)");
233 auth_peer_fail(unit, PPP_PAP);
235 upap_lowerdown(unit);
240 * upap_input - Input UPAP packet.
243 upap_input(unit, inpacket, l)
248 upap_state *u = &upap[unit];
254 * Parse header (code, id and length).
255 * If packet too short, drop it.
258 if (l < UPAP_HEADERLEN) {
259 UPAPDEBUG((LOG_INFO, "upap_input: rcvd short header."));
265 if (len < UPAP_HEADERLEN) {
266 UPAPDEBUG((LOG_INFO, "upap_input: rcvd illegal length."));
270 UPAPDEBUG((LOG_INFO, "upap_input: rcvd short packet."));
273 len -= UPAP_HEADERLEN;
276 * Action depends on code.
280 upap_rauthreq(u, inp, id, len);
284 upap_rauthack(u, inp, id, len);
288 upap_rauthnak(u, inp, id, len);
291 default: /* XXX Need code reject */
298 * upap_rauth - Receive Authenticate.
301 upap_rauthreq(u, inp, id, len)
307 u_char ruserlen, rpasswdlen;
308 char *ruser, *rpasswd;
313 UPAPDEBUG((LOG_INFO, "upap_rauth: Rcvd id %d.", id));
315 if (u->us_serverstate < UPAPSS_LISTEN)
319 * If we receive a duplicate authenticate-request, we are
320 * supposed to return the same status as for the first request.
322 if (u->us_serverstate == UPAPSS_OPEN) {
323 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
326 if (u->us_serverstate == UPAPSS_BADAUTH) {
327 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
334 if (len < sizeof (u_char)) {
335 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
338 GETCHAR(ruserlen, inp);
339 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
341 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
344 ruser = (char *) inp;
345 INCPTR(ruserlen, inp);
346 GETCHAR(rpasswdlen, inp);
347 if (len < rpasswdlen) {
348 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
351 rpasswd = (char *) inp;
354 * Check the username and password given.
356 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
357 rpasswdlen, &msg, &msglen);
359 upap_sresp(u, retcode, id, msg, msglen);
361 if (retcode == UPAP_AUTHACK) {
362 u->us_serverstate = UPAPSS_OPEN;
363 auth_peer_success(u->us_unit, PPP_PAP);
365 u->us_serverstate = UPAPSS_BADAUTH;
366 auth_peer_fail(u->us_unit, PPP_PAP);
369 if (u->us_reqtimeout > 0)
370 UNTIMEOUT(upap_reqtimeout, (caddr_t) u);
375 * upap_rauthack - Receive Authenticate-Ack.
378 upap_rauthack(u, inp, id, len)
387 UPAPDEBUG((LOG_INFO, "upap_rauthack: Rcvd id %d.", id));
388 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
394 if (len < sizeof (u_char)) {
395 UPAPDEBUG((LOG_INFO, "upap_rauthack: rcvd short packet."));
398 GETCHAR(msglen, inp);
399 len -= sizeof (u_char);
401 UPAPDEBUG((LOG_INFO, "upap_rauthack: rcvd short packet."));
405 PRINTMSG(msg, msglen);
407 u->us_clientstate = UPAPCS_OPEN;
409 auth_withpeer_success(u->us_unit, PPP_PAP);
414 * upap_rauthnak - Receive Authenticate-Nakk.
417 upap_rauthnak(u, inp, id, len)
426 UPAPDEBUG((LOG_INFO, "upap_rauthnak: Rcvd id %d.", id));
427 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
433 if (len < sizeof (u_char)) {
434 UPAPDEBUG((LOG_INFO, "upap_rauthnak: rcvd short packet."));
437 GETCHAR(msglen, inp);
438 len -= sizeof (u_char);
440 UPAPDEBUG((LOG_INFO, "upap_rauthnak: rcvd short packet."));
444 PRINTMSG(msg, msglen);
446 u->us_clientstate = UPAPCS_BADAUTH;
448 syslog(LOG_ERR, "PAP authentication failed");
449 auth_withpeer_fail(u->us_unit, PPP_PAP);
454 * upap_sauthreq - Send an Authenticate-Request.
463 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
464 u->us_userlen + u->us_passwdlen;
465 outp = outpacket_buf;
467 MAKEHEADER(outp, PPP_PAP);
469 PUTCHAR(UPAP_AUTHREQ, outp);
470 PUTCHAR(++u->us_id, outp);
471 PUTSHORT(outlen, outp);
472 PUTCHAR(u->us_userlen, outp);
473 BCOPY(u->us_user, outp, u->us_userlen);
474 INCPTR(u->us_userlen, outp);
475 PUTCHAR(u->us_passwdlen, outp);
476 BCOPY(u->us_passwd, outp, u->us_passwdlen);
478 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
480 UPAPDEBUG((LOG_INFO, "upap_sauth: Sent id %d.", u->us_id));
482 TIMEOUT(upap_timeout, (caddr_t) u, u->us_timeouttime);
484 u->us_clientstate = UPAPCS_AUTHREQ;
489 * upap_sresp - Send a response (ack or nak).
492 upap_sresp(u, code, id, msg, msglen)
501 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
502 outp = outpacket_buf;
503 MAKEHEADER(outp, PPP_PAP);
507 PUTSHORT(outlen, outp);
508 PUTCHAR(msglen, outp);
509 BCOPY(msg, outp, msglen);
510 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
512 UPAPDEBUG((LOG_INFO, "upap_sresp: Sent code %d, id %d.", code, id));
516 * upap_printpkt - print the contents of a PAP packet.
518 char *upap_codenames[] = {
519 "AuthReq", "AuthAck", "AuthNak"
523 upap_printpkt(p, plen, printer, arg)
526 void (*printer) __P((void *, char *, ...));
530 int mlen, ulen, wlen;
531 char *user, *pwd, *msg;
534 if (plen < UPAP_HEADERLEN)
540 if (len < UPAP_HEADERLEN || len > plen)
543 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
544 printer(arg, " %s", upap_codenames[code-1]);
546 printer(arg, " code=0x%x", code);
547 printer(arg, " id=0x%x", id);
548 len -= UPAP_HEADERLEN;
557 if (len < ulen + wlen + 2)
559 user = (char *) (p + 1);
560 pwd = (char *) (p + ulen + 2);
561 p += ulen + wlen + 2;
562 len -= ulen + wlen + 2;
563 printer(arg, " user=");
564 print_string(user, ulen, printer, arg);
565 printer(arg, " password=");
566 print_string(pwd, wlen, printer, arg);
575 msg = (char *) (p + 1);
578 printer(arg, "msg=");
579 print_string(msg, mlen, printer, arg);
583 /* print the rest of the bytes in the packet */
584 for (; len > 0; --len) {
586 printer(arg, " %.2x", code);