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.7 1995/12/18 03:46:48 paulus Exp $";
30 #include <sys/types.h>
37 struct protent pap_protent = {
38 PPP_PAP, upap_init, upap_input, upap_protrej,
39 upap_lowerup, upap_lowerdown, NULL, NULL,
40 upap_printpkt, NULL, 1, "PAP"
43 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
45 static void upap_timeout __P((caddr_t));
46 static void upap_reqtimeout __P((caddr_t));
47 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
48 static void upap_rauthack __P((upap_state *, u_char *, int, int));
49 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
50 static void upap_sauthreq __P((upap_state *));
51 static void upap_sresp __P((upap_state *, int, int, char *, int));
55 * upap_init - Initialize a UPAP unit.
61 upap_state *u = &upap[unit];
68 u->us_clientstate = UPAPCS_INITIAL;
69 u->us_serverstate = UPAPSS_INITIAL;
71 u->us_timeouttime = UPAP_DEFTIMEOUT;
72 u->us_maxtransmits = 10;
73 u->us_reqtimeout = UPAP_DEFREQTIME;
78 * upap_authwithpeer - Authenticate us with our peer (start client).
80 * Set new state and send authenticate's.
83 upap_authwithpeer(unit, user, password)
85 char *user, *password;
87 upap_state *u = &upap[unit];
89 /* Save the username and password we're given */
91 u->us_userlen = strlen(user);
92 u->us_passwd = password;
93 u->us_passwdlen = strlen(password);
96 /* Lower layer up yet? */
97 if (u->us_clientstate == UPAPCS_INITIAL ||
98 u->us_clientstate == UPAPCS_PENDING) {
99 u->us_clientstate = UPAPCS_PENDING;
103 upap_sauthreq(u); /* Start protocol */
108 * upap_authpeer - Authenticate our peer (start server).
116 upap_state *u = &upap[unit];
118 /* Lower layer up yet? */
119 if (u->us_serverstate == UPAPSS_INITIAL ||
120 u->us_serverstate == UPAPSS_PENDING) {
121 u->us_serverstate = UPAPSS_PENDING;
125 u->us_serverstate = UPAPSS_LISTEN;
126 if (u->us_reqtimeout > 0)
127 TIMEOUT(upap_reqtimeout, (caddr_t) u, u->us_reqtimeout);
132 * upap_timeout - Retransmission timer for sending auth-reqs expired.
138 upap_state *u = (upap_state *) arg;
140 if (u->us_clientstate != UPAPCS_AUTHREQ)
143 if (u->us_transmits >= u->us_maxtransmits) {
144 /* give up in disgust */
145 syslog(LOG_ERR, "No response to PAP authenticate-requests");
146 u->us_clientstate = UPAPCS_BADAUTH;
147 auth_withpeer_fail(u->us_unit, PPP_PAP);
151 upap_sauthreq(u); /* Send Authenticate-Request */
156 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
162 upap_state *u = (upap_state *) arg;
164 if (u->us_serverstate != UPAPSS_LISTEN)
167 auth_peer_fail(u->us_unit, PPP_PAP);
168 u->us_serverstate = UPAPSS_BADAUTH;
173 * upap_lowerup - The lower layer is up.
175 * Start authenticating if pending.
181 upap_state *u = &upap[unit];
183 if (u->us_clientstate == UPAPCS_INITIAL)
184 u->us_clientstate = UPAPCS_CLOSED;
185 else if (u->us_clientstate == UPAPCS_PENDING) {
186 upap_sauthreq(u); /* send an auth-request */
189 if (u->us_serverstate == UPAPSS_INITIAL)
190 u->us_serverstate = UPAPSS_CLOSED;
191 else if (u->us_serverstate == UPAPSS_PENDING) {
192 u->us_serverstate = UPAPSS_LISTEN;
193 if (u->us_reqtimeout > 0)
194 TIMEOUT(upap_reqtimeout, (caddr_t) u, u->us_reqtimeout);
200 * upap_lowerdown - The lower layer is down.
202 * Cancel all timeouts.
208 upap_state *u = &upap[unit];
210 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
211 UNTIMEOUT(upap_timeout, (caddr_t) u); /* Cancel timeout */
212 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
213 UNTIMEOUT(upap_reqtimeout, (caddr_t) u);
215 u->us_clientstate = UPAPCS_INITIAL;
216 u->us_serverstate = UPAPSS_INITIAL;
221 * upap_protrej - Peer doesn't speak this protocol.
223 * This shouldn't happen. In any case, pretend lower layer went down.
229 upap_state *u = &upap[unit];
231 if (u->us_clientstate == UPAPCS_AUTHREQ) {
232 syslog(LOG_ERR, "PAP authentication failed due to protocol-reject");
233 auth_withpeer_fail(unit, PPP_PAP);
235 if (u->us_serverstate == UPAPSS_LISTEN) {
236 syslog(LOG_ERR, "PAP authentication of peer failed (protocol-reject)");
237 auth_peer_fail(unit, PPP_PAP);
239 upap_lowerdown(unit);
244 * upap_input - Input UPAP packet.
247 upap_input(unit, inpacket, l)
252 upap_state *u = &upap[unit];
258 * Parse header (code, id and length).
259 * If packet too short, drop it.
262 if (l < UPAP_HEADERLEN) {
263 UPAPDEBUG((LOG_INFO, "upap_input: rcvd short header."));
269 if (len < UPAP_HEADERLEN) {
270 UPAPDEBUG((LOG_INFO, "upap_input: rcvd illegal length."));
274 UPAPDEBUG((LOG_INFO, "upap_input: rcvd short packet."));
277 len -= UPAP_HEADERLEN;
280 * Action depends on code.
284 upap_rauthreq(u, inp, id, len);
288 upap_rauthack(u, inp, id, len);
292 upap_rauthnak(u, inp, id, len);
295 default: /* XXX Need code reject */
302 * upap_rauth - Receive Authenticate.
305 upap_rauthreq(u, inp, id, len)
311 u_char ruserlen, rpasswdlen;
312 char *ruser, *rpasswd;
317 UPAPDEBUG((LOG_INFO, "upap_rauth: Rcvd id %d.", id));
319 if (u->us_serverstate < UPAPSS_LISTEN)
323 * If we receive a duplicate authenticate-request, we are
324 * supposed to return the same status as for the first request.
326 if (u->us_serverstate == UPAPSS_OPEN) {
327 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
330 if (u->us_serverstate == UPAPSS_BADAUTH) {
331 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
338 if (len < sizeof (u_char)) {
339 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
342 GETCHAR(ruserlen, inp);
343 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
345 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
348 ruser = (char *) inp;
349 INCPTR(ruserlen, inp);
350 GETCHAR(rpasswdlen, inp);
351 if (len < rpasswdlen) {
352 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
355 rpasswd = (char *) inp;
358 * Check the username and password given.
360 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
361 rpasswdlen, &msg, &msglen);
363 upap_sresp(u, retcode, id, msg, msglen);
365 if (retcode == UPAP_AUTHACK) {
366 u->us_serverstate = UPAPSS_OPEN;
367 auth_peer_success(u->us_unit, PPP_PAP);
369 u->us_serverstate = UPAPSS_BADAUTH;
370 auth_peer_fail(u->us_unit, PPP_PAP);
373 if (u->us_reqtimeout > 0)
374 UNTIMEOUT(upap_reqtimeout, (caddr_t) u);
379 * upap_rauthack - Receive Authenticate-Ack.
382 upap_rauthack(u, inp, id, len)
391 UPAPDEBUG((LOG_INFO, "upap_rauthack: Rcvd id %d.", id));
392 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
398 if (len < sizeof (u_char)) {
399 UPAPDEBUG((LOG_INFO, "upap_rauthack: rcvd short packet."));
402 GETCHAR(msglen, inp);
403 len -= sizeof (u_char);
405 UPAPDEBUG((LOG_INFO, "upap_rauthack: rcvd short packet."));
409 PRINTMSG(msg, msglen);
411 u->us_clientstate = UPAPCS_OPEN;
413 auth_withpeer_success(u->us_unit, PPP_PAP);
418 * upap_rauthnak - Receive Authenticate-Nakk.
421 upap_rauthnak(u, inp, id, len)
430 UPAPDEBUG((LOG_INFO, "upap_rauthnak: Rcvd id %d.", id));
431 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
437 if (len < sizeof (u_char)) {
438 UPAPDEBUG((LOG_INFO, "upap_rauthnak: rcvd short packet."));
441 GETCHAR(msglen, inp);
442 len -= sizeof (u_char);
444 UPAPDEBUG((LOG_INFO, "upap_rauthnak: rcvd short packet."));
448 PRINTMSG(msg, msglen);
450 u->us_clientstate = UPAPCS_BADAUTH;
452 syslog(LOG_ERR, "PAP authentication failed");
453 auth_withpeer_fail(u->us_unit, PPP_PAP);
458 * upap_sauthreq - Send an Authenticate-Request.
467 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
468 u->us_userlen + u->us_passwdlen;
469 outp = outpacket_buf;
471 MAKEHEADER(outp, PPP_PAP);
473 PUTCHAR(UPAP_AUTHREQ, outp);
474 PUTCHAR(++u->us_id, outp);
475 PUTSHORT(outlen, outp);
476 PUTCHAR(u->us_userlen, outp);
477 BCOPY(u->us_user, outp, u->us_userlen);
478 INCPTR(u->us_userlen, outp);
479 PUTCHAR(u->us_passwdlen, outp);
480 BCOPY(u->us_passwd, outp, u->us_passwdlen);
482 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
484 UPAPDEBUG((LOG_INFO, "upap_sauth: Sent id %d.", u->us_id));
486 TIMEOUT(upap_timeout, (caddr_t) u, u->us_timeouttime);
488 u->us_clientstate = UPAPCS_AUTHREQ;
493 * upap_sresp - Send a response (ack or nak).
496 upap_sresp(u, code, id, msg, msglen)
505 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
506 outp = outpacket_buf;
507 MAKEHEADER(outp, PPP_PAP);
511 PUTSHORT(outlen, outp);
512 PUTCHAR(msglen, outp);
513 BCOPY(msg, outp, msglen);
514 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
516 UPAPDEBUG((LOG_INFO, "upap_sresp: Sent code %d, id %d.", code, id));
520 * upap_printpkt - print the contents of a PAP packet.
522 char *upap_codenames[] = {
523 "AuthReq", "AuthAck", "AuthNak"
527 upap_printpkt(p, plen, printer, arg)
530 void (*printer) __P((void *, char *, ...));
534 int mlen, ulen, wlen;
535 char *user, *pwd, *msg;
538 if (plen < UPAP_HEADERLEN)
544 if (len < UPAP_HEADERLEN || len > plen)
547 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
548 printer(arg, " %s", upap_codenames[code-1]);
550 printer(arg, " code=0x%x", code);
551 printer(arg, " id=0x%x", id);
552 len -= UPAP_HEADERLEN;
561 if (len < ulen + wlen + 2)
563 user = (char *) (p + 1);
564 pwd = (char *) (p + ulen + 2);
565 p += ulen + wlen + 2;
566 len -= ulen + wlen + 2;
567 printer(arg, " user=");
568 print_string(user, ulen, printer, arg);
569 printer(arg, " password=");
570 print_string(pwd, wlen, printer, arg);
579 msg = (char *) (p + 1);
582 printer(arg, "msg=");
583 print_string(msg, mlen, printer, arg);
587 /* print the rest of the bytes in the packet */
588 for (; len > 0; --len) {
590 printer(arg, " %.2x", code);