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.11 1997/04/30 05:59:56 paulus Exp $";
30 #include <sys/types.h>
38 * Protocol entry points.
40 static void upap_init __P((int));
41 static void upap_lowerup __P((int));
42 static void upap_lowerdown __P((int));
43 static void upap_input __P((int, u_char *, int));
44 static void upap_protrej __P((int));
45 static int upap_printpkt __P((u_char *, int,
46 void (*) __P((void *, char *, ...)), void *));
48 struct protent pap_protent = {
66 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
68 static void upap_timeout __P((void *));
69 static void upap_reqtimeout __P((void *));
70 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
71 static void upap_rauthack __P((upap_state *, u_char *, int, int));
72 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
73 static void upap_sauthreq __P((upap_state *));
74 static void upap_sresp __P((upap_state *, int, int, char *, int));
78 * upap_init - Initialize a UPAP unit.
84 upap_state *u = &upap[unit];
91 u->us_clientstate = UPAPCS_INITIAL;
92 u->us_serverstate = UPAPSS_INITIAL;
94 u->us_timeouttime = UPAP_DEFTIMEOUT;
95 u->us_maxtransmits = 10;
96 u->us_reqtimeout = UPAP_DEFREQTIME;
101 * upap_authwithpeer - Authenticate us with our peer (start client).
103 * Set new state and send authenticate's.
106 upap_authwithpeer(unit, user, password)
108 char *user, *password;
110 upap_state *u = &upap[unit];
112 /* Save the username and password we're given */
114 u->us_userlen = strlen(user);
115 u->us_passwd = password;
116 u->us_passwdlen = strlen(password);
119 /* Lower layer up yet? */
120 if (u->us_clientstate == UPAPCS_INITIAL ||
121 u->us_clientstate == UPAPCS_PENDING) {
122 u->us_clientstate = UPAPCS_PENDING;
126 upap_sauthreq(u); /* Start protocol */
131 * upap_authpeer - Authenticate our peer (start server).
139 upap_state *u = &upap[unit];
141 /* Lower layer up yet? */
142 if (u->us_serverstate == UPAPSS_INITIAL ||
143 u->us_serverstate == UPAPSS_PENDING) {
144 u->us_serverstate = UPAPSS_PENDING;
148 u->us_serverstate = UPAPSS_LISTEN;
149 if (u->us_reqtimeout > 0)
150 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
155 * upap_timeout - Retransmission timer for sending auth-reqs expired.
161 upap_state *u = (upap_state *) arg;
163 if (u->us_clientstate != UPAPCS_AUTHREQ)
166 if (u->us_transmits >= u->us_maxtransmits) {
167 /* give up in disgust */
168 syslog(LOG_ERR, "No response to PAP authenticate-requests");
169 u->us_clientstate = UPAPCS_BADAUTH;
170 auth_withpeer_fail(u->us_unit, PPP_PAP);
174 upap_sauthreq(u); /* Send Authenticate-Request */
179 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
185 upap_state *u = (upap_state *) arg;
187 if (u->us_serverstate != UPAPSS_LISTEN)
190 auth_peer_fail(u->us_unit, PPP_PAP);
191 u->us_serverstate = UPAPSS_BADAUTH;
196 * upap_lowerup - The lower layer is up.
198 * Start authenticating if pending.
204 upap_state *u = &upap[unit];
206 if (u->us_clientstate == UPAPCS_INITIAL)
207 u->us_clientstate = UPAPCS_CLOSED;
208 else if (u->us_clientstate == UPAPCS_PENDING) {
209 upap_sauthreq(u); /* send an auth-request */
212 if (u->us_serverstate == UPAPSS_INITIAL)
213 u->us_serverstate = UPAPSS_CLOSED;
214 else if (u->us_serverstate == UPAPSS_PENDING) {
215 u->us_serverstate = UPAPSS_LISTEN;
216 if (u->us_reqtimeout > 0)
217 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
223 * upap_lowerdown - The lower layer is down.
225 * Cancel all timeouts.
231 upap_state *u = &upap[unit];
233 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
234 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
235 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
236 UNTIMEOUT(upap_reqtimeout, u);
238 u->us_clientstate = UPAPCS_INITIAL;
239 u->us_serverstate = UPAPSS_INITIAL;
244 * upap_protrej - Peer doesn't speak this protocol.
246 * This shouldn't happen. In any case, pretend lower layer went down.
252 upap_state *u = &upap[unit];
254 if (u->us_clientstate == UPAPCS_AUTHREQ) {
255 syslog(LOG_ERR, "PAP authentication failed due to protocol-reject");
256 auth_withpeer_fail(unit, PPP_PAP);
258 if (u->us_serverstate == UPAPSS_LISTEN) {
259 syslog(LOG_ERR, "PAP authentication of peer failed (protocol-reject)");
260 auth_peer_fail(unit, PPP_PAP);
262 upap_lowerdown(unit);
267 * upap_input - Input UPAP packet.
270 upap_input(unit, inpacket, l)
275 upap_state *u = &upap[unit];
281 * Parse header (code, id and length).
282 * If packet too short, drop it.
285 if (l < UPAP_HEADERLEN) {
286 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short header."));
292 if (len < UPAP_HEADERLEN) {
293 UPAPDEBUG((LOG_INFO, "pap_input: rcvd illegal length."));
297 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short packet."));
300 len -= UPAP_HEADERLEN;
303 * Action depends on code.
307 upap_rauthreq(u, inp, id, len);
311 upap_rauthack(u, inp, id, len);
315 upap_rauthnak(u, inp, id, len);
318 default: /* XXX Need code reject */
325 * upap_rauth - Receive Authenticate.
328 upap_rauthreq(u, inp, id, len)
334 u_char ruserlen, rpasswdlen;
335 char *ruser, *rpasswd;
340 UPAPDEBUG((LOG_INFO, "pap_rauth: Rcvd id %d.", id));
342 if (u->us_serverstate < UPAPSS_LISTEN)
346 * If we receive a duplicate authenticate-request, we are
347 * supposed to return the same status as for the first request.
349 if (u->us_serverstate == UPAPSS_OPEN) {
350 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
353 if (u->us_serverstate == UPAPSS_BADAUTH) {
354 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
361 if (len < sizeof (u_char)) {
362 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
365 GETCHAR(ruserlen, inp);
366 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
368 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
371 ruser = (char *) inp;
372 INCPTR(ruserlen, inp);
373 GETCHAR(rpasswdlen, inp);
374 if (len < rpasswdlen) {
375 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
378 rpasswd = (char *) inp;
381 * Check the username and password given.
383 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
384 rpasswdlen, &msg, &msglen);
385 BZERO(rpasswd, rpasswdlen);
387 upap_sresp(u, retcode, id, msg, msglen);
389 if (retcode == UPAP_AUTHACK) {
390 u->us_serverstate = UPAPSS_OPEN;
391 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
393 u->us_serverstate = UPAPSS_BADAUTH;
394 auth_peer_fail(u->us_unit, PPP_PAP);
397 if (u->us_reqtimeout > 0)
398 UNTIMEOUT(upap_reqtimeout, u);
403 * upap_rauthack - Receive Authenticate-Ack.
406 upap_rauthack(u, inp, id, len)
415 UPAPDEBUG((LOG_INFO, "pap_rauthack: Rcvd id %d.", id));
416 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
422 if (len < sizeof (u_char)) {
423 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
426 GETCHAR(msglen, inp);
427 len -= sizeof (u_char);
429 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
433 PRINTMSG(msg, msglen);
435 u->us_clientstate = UPAPCS_OPEN;
437 auth_withpeer_success(u->us_unit, PPP_PAP);
442 * upap_rauthnak - Receive Authenticate-Nakk.
445 upap_rauthnak(u, inp, id, len)
454 UPAPDEBUG((LOG_INFO, "pap_rauthnak: Rcvd id %d.", id));
455 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
461 if (len < sizeof (u_char)) {
462 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
465 GETCHAR(msglen, inp);
466 len -= sizeof (u_char);
468 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
472 PRINTMSG(msg, msglen);
474 u->us_clientstate = UPAPCS_BADAUTH;
476 syslog(LOG_ERR, "PAP authentication failed");
477 auth_withpeer_fail(u->us_unit, PPP_PAP);
482 * upap_sauthreq - Send an Authenticate-Request.
491 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
492 u->us_userlen + u->us_passwdlen;
493 outp = outpacket_buf;
495 MAKEHEADER(outp, PPP_PAP);
497 PUTCHAR(UPAP_AUTHREQ, outp);
498 PUTCHAR(++u->us_id, outp);
499 PUTSHORT(outlen, outp);
500 PUTCHAR(u->us_userlen, outp);
501 BCOPY(u->us_user, outp, u->us_userlen);
502 INCPTR(u->us_userlen, outp);
503 PUTCHAR(u->us_passwdlen, outp);
504 BCOPY(u->us_passwd, outp, u->us_passwdlen);
506 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
508 UPAPDEBUG((LOG_INFO, "pap_sauth: Sent id %d.", u->us_id));
510 TIMEOUT(upap_timeout, u, u->us_timeouttime);
512 u->us_clientstate = UPAPCS_AUTHREQ;
517 * upap_sresp - Send a response (ack or nak).
520 upap_sresp(u, code, id, msg, msglen)
529 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
530 outp = outpacket_buf;
531 MAKEHEADER(outp, PPP_PAP);
535 PUTSHORT(outlen, outp);
536 PUTCHAR(msglen, outp);
537 BCOPY(msg, outp, msglen);
538 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
540 UPAPDEBUG((LOG_INFO, "pap_sresp: Sent code %d, id %d.", code, id));
544 * upap_printpkt - print the contents of a PAP packet.
546 static char *upap_codenames[] = {
547 "AuthReq", "AuthAck", "AuthNak"
551 upap_printpkt(p, plen, printer, arg)
554 void (*printer) __P((void *, char *, ...));
558 int mlen, ulen, wlen;
559 char *user, *pwd, *msg;
562 if (plen < UPAP_HEADERLEN)
568 if (len < UPAP_HEADERLEN || len > plen)
571 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
572 printer(arg, " %s", upap_codenames[code-1]);
574 printer(arg, " code=0x%x", code);
575 printer(arg, " id=0x%x", id);
576 len -= UPAP_HEADERLEN;
585 if (len < ulen + wlen + 2)
587 user = (char *) (p + 1);
588 pwd = (char *) (p + ulen + 2);
589 p += ulen + wlen + 2;
590 len -= ulen + wlen + 2;
591 printer(arg, " user=");
592 print_string(user, ulen, printer, arg);
593 printer(arg, " password=");
594 print_string(pwd, wlen, printer, arg);
603 msg = (char *) (p + 1);
607 print_string(msg, mlen, printer, arg);
611 /* print the rest of the bytes in the packet */
612 for (; len > 0; --len) {
614 printer(arg, " %.2x", code);