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.3 1994/09/21 06:47:37 paulus Exp $";
29 #include <sys/types.h>
37 upap_state upap[N_PPP]; /* UPAP state; one for each unit */
40 static void upap_timeout __P((caddr_t));
41 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
42 static void upap_rauthack __P((upap_state *, u_char *, int, int));
43 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
44 static void upap_sauthreq __P((upap_state *));
45 static void upap_sresp __P((upap_state *, int, int, char *, int));
49 * upap_init - Initialize a UPAP unit.
55 upap_state *u = &upap[unit];
62 u->us_clientstate = UPAPCS_INITIAL;
63 u->us_serverstate = UPAPSS_INITIAL;
65 u->us_timeouttime = UPAP_DEFTIMEOUT;
66 u->us_maxtransmits = 10;
71 * upap_authwithpeer - Authenticate us with our peer (start client).
73 * Set new state and send authenticate's.
76 upap_authwithpeer(unit, user, password)
78 char *user, *password;
80 upap_state *u = &upap[unit];
82 /* Save the username and password we're given */
84 u->us_userlen = strlen(user);
85 u->us_passwd = password;
86 u->us_passwdlen = strlen(password);
89 /* Lower layer up yet? */
90 if (u->us_clientstate == UPAPCS_INITIAL ||
91 u->us_clientstate == UPAPCS_PENDING) {
92 u->us_clientstate = UPAPCS_PENDING;
96 upap_sauthreq(u); /* Start protocol */
101 * upap_authpeer - Authenticate our peer (start server).
109 upap_state *u = &upap[unit];
111 /* Lower layer up yet? */
112 if (u->us_serverstate == UPAPSS_INITIAL ||
113 u->us_serverstate == UPAPSS_PENDING) {
114 u->us_serverstate = UPAPSS_PENDING;
118 u->us_serverstate = UPAPSS_LISTEN;
123 * upap_timeout - Timeout expired.
129 upap_state *u = (upap_state *) arg;
131 if (u->us_clientstate != UPAPCS_AUTHREQ)
134 if (u->us_transmits >= u->us_maxtransmits) {
135 /* give up in disgust */
136 syslog(LOG_ERR, "No response to PAP authenticate-requests");
137 u->us_clientstate = UPAPCS_BADAUTH;
138 auth_withpeer_fail(u->us_unit, PPP_PAP);
142 upap_sauthreq(u); /* Send Authenticate-Request */
147 * upap_lowerup - The lower layer is up.
149 * Start authenticating if pending.
155 upap_state *u = &upap[unit];
157 if (u->us_clientstate == UPAPCS_INITIAL)
158 u->us_clientstate = UPAPCS_CLOSED;
159 else if (u->us_clientstate == UPAPCS_PENDING) {
160 upap_sauthreq(u); /* send an auth-request */
163 if (u->us_serverstate == UPAPSS_INITIAL)
164 u->us_serverstate = UPAPSS_CLOSED;
165 else if (u->us_serverstate == UPAPSS_PENDING)
166 u->us_serverstate = UPAPSS_LISTEN;
171 * upap_lowerdown - The lower layer is down.
173 * Cancel all timeouts.
179 upap_state *u = &upap[unit];
181 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
182 UNTIMEOUT(upap_timeout, (caddr_t) u); /* Cancel timeout */
184 u->us_clientstate = UPAPCS_INITIAL;
185 u->us_serverstate = UPAPSS_INITIAL;
190 * upap_protrej - Peer doesn't speak this protocol.
192 * This shouldn't happen. In any case, pretend lower layer went down.
198 upap_state *u = &upap[unit];
200 if (u->us_clientstate == UPAPCS_AUTHREQ) {
201 syslog(LOG_ERR, "PAP authentication failed due to protocol-reject");
202 auth_withpeer_fail(unit, PPP_PAP);
204 if (u->us_serverstate == UPAPSS_LISTEN) {
205 syslog(LOG_ERR, "PAP authentication of peer failed (protocol-reject)");
206 auth_peer_fail(unit, PPP_PAP);
208 upap_lowerdown(unit);
213 * upap_input - Input UPAP packet.
216 upap_input(unit, inpacket, l)
221 upap_state *u = &upap[unit];
227 * Parse header (code, id and length).
228 * If packet too short, drop it.
231 if (l < UPAP_HEADERLEN) {
232 UPAPDEBUG((LOG_INFO, "upap_input: rcvd short header."));
238 if (len < UPAP_HEADERLEN) {
239 UPAPDEBUG((LOG_INFO, "upap_input: rcvd illegal length."));
243 UPAPDEBUG((LOG_INFO, "upap_input: rcvd short packet."));
246 len -= UPAP_HEADERLEN;
249 * Action depends on code.
253 upap_rauthreq(u, inp, id, len);
257 upap_rauthack(u, inp, id, len);
261 upap_rauthnak(u, inp, id, len);
264 default: /* XXX Need code reject */
271 * upap_rauth - Receive Authenticate.
274 upap_rauthreq(u, inp, id, len)
280 u_char ruserlen, rpasswdlen;
281 char *ruser, *rpasswd;
286 UPAPDEBUG((LOG_INFO, "upap_rauth: Rcvd id %d.", id));
288 if (u->us_serverstate < UPAPSS_LISTEN)
292 * If we receive a duplicate authenticate-request, we are
293 * supposed to return the same status as for the first request.
295 if (u->us_serverstate == UPAPSS_OPEN) {
296 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
299 if (u->us_serverstate == UPAPSS_BADAUTH) {
300 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
307 if (len < sizeof (u_char)) {
308 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
311 GETCHAR(ruserlen, inp);
312 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
314 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
317 ruser = (char *) inp;
318 INCPTR(ruserlen, inp);
319 GETCHAR(rpasswdlen, inp);
320 if (len < rpasswdlen) {
321 UPAPDEBUG((LOG_INFO, "upap_rauth: rcvd short packet."));
324 rpasswd = (char *) inp;
327 * Check the username and password given.
329 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
330 rpasswdlen, &msg, &msglen);
332 upap_sresp(u, retcode, id, msg, msglen);
334 if (retcode == UPAP_AUTHACK) {
335 u->us_serverstate = UPAPSS_OPEN;
336 auth_peer_success(u->us_unit, PPP_PAP);
338 u->us_serverstate = UPAPSS_BADAUTH;
339 auth_peer_fail(u->us_unit, PPP_PAP);
345 * upap_rauthack - Receive Authenticate-Ack.
348 upap_rauthack(u, inp, id, len)
357 UPAPDEBUG((LOG_INFO, "upap_rauthack: Rcvd id %d.", id));
358 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
364 if (len < sizeof (u_char)) {
365 UPAPDEBUG((LOG_INFO, "upap_rauthack: rcvd short packet."));
368 GETCHAR(msglen, inp);
369 len -= sizeof (u_char);
371 UPAPDEBUG((LOG_INFO, "upap_rauthack: rcvd short packet."));
375 PRINTMSG(msg, msglen);
377 u->us_clientstate = UPAPCS_OPEN;
379 auth_withpeer_success(u->us_unit, PPP_PAP);
384 * upap_rauthnak - Receive Authenticate-Nakk.
387 upap_rauthnak(u, inp, id, len)
396 UPAPDEBUG((LOG_INFO, "upap_rauthnak: Rcvd id %d.", id));
397 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
403 if (len < sizeof (u_char)) {
404 UPAPDEBUG((LOG_INFO, "upap_rauthnak: rcvd short packet."));
407 GETCHAR(msglen, inp);
408 len -= sizeof (u_char);
410 UPAPDEBUG((LOG_INFO, "upap_rauthnak: rcvd short packet."));
414 PRINTMSG(msg, msglen);
416 u->us_clientstate = UPAPCS_BADAUTH;
418 syslog(LOG_ERR, "PAP authentication failed");
419 auth_withpeer_fail(u->us_unit, PPP_PAP);
424 * upap_sauthreq - Send an Authenticate-Request.
433 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
434 u->us_userlen + u->us_passwdlen;
435 outp = outpacket_buf;
437 MAKEHEADER(outp, PPP_PAP);
439 PUTCHAR(UPAP_AUTHREQ, outp);
440 PUTCHAR(++u->us_id, outp);
441 PUTSHORT(outlen, outp);
442 PUTCHAR(u->us_userlen, outp);
443 BCOPY(u->us_user, outp, u->us_userlen);
444 INCPTR(u->us_userlen, outp);
445 PUTCHAR(u->us_passwdlen, outp);
446 BCOPY(u->us_passwd, outp, u->us_passwdlen);
448 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
450 UPAPDEBUG((LOG_INFO, "upap_sauth: Sent id %d.", u->us_id));
452 TIMEOUT(upap_timeout, (caddr_t) u, u->us_timeouttime);
454 u->us_clientstate = UPAPCS_AUTHREQ;
459 * upap_sresp - Send a response (ack or nak).
462 upap_sresp(u, code, id, msg, msglen)
471 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
472 outp = outpacket_buf;
473 MAKEHEADER(outp, PPP_PAP);
477 PUTSHORT(outlen, outp);
478 PUTCHAR(msglen, outp);
479 BCOPY(msg, outp, msglen);
480 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
482 UPAPDEBUG((LOG_INFO, "upap_sresp: Sent code %d, id %d.", code, id));
486 * upap_printpkt - print the contents of a PAP packet.
488 char *upap_codenames[] = {
489 "AuthReq", "AuthAck", "AuthNak"
493 upap_printpkt(p, plen, printer, arg)
496 void (*printer) __P((void *, char *, ...));
500 int mlen, ulen, wlen;
501 char *user, *pwd, *msg;
504 if (plen < UPAP_HEADERLEN)
510 if (len < UPAP_HEADERLEN || len > plen)
513 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
514 printer(arg, " %s", upap_codenames[code-1]);
516 printer(arg, " code=0x%x", code);
517 printer(arg, " id=0x%x", id);
518 len -= UPAP_HEADERLEN;
527 if (len < ulen + wlen + 2)
529 user = (char *) (p + 1);
530 pwd = (char *) (p + ulen + 2);
531 p += ulen + wlen + 2;
532 len -= ulen + wlen + 2;
533 printer(arg, " user=");
534 print_string(user, ulen, printer, arg);
535 printer(arg, " password=");
536 print_string(pwd, wlen, printer, arg);
545 msg = (char *) (p + 1);
548 printer(arg, "msg=");
549 print_string(msg, mlen, printer, arg);
553 /* print the rest of the bytes in the packet */
554 for (; len > 0; --len) {
556 printer(arg, " %.2x", code);