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.13 1999/03/02 05:29:39 paulus Exp $";
30 #include <sys/types.h>
37 static bool hide_password;
40 * Command-line options.
42 static option_t pap_option_list[] = {
43 { "hide-password", o_bool, &hide_password,
44 "Don't output passwords to log" },
45 { "pap-restart", o_int, &upap[0].us_timeouttime,
46 "Set retransmit timeout for PAP" },
47 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
48 "Set max number of transmissions for auth-reqs" },
49 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
50 "Set time limit for peer PAP authentication" },
55 * Protocol entry points.
57 static void upap_init __P((int));
58 static void upap_lowerup __P((int));
59 static void upap_lowerdown __P((int));
60 static void upap_input __P((int, u_char *, int));
61 static void upap_protrej __P((int));
62 static int upap_printpkt __P((u_char *, int,
63 void (*) __P((void *, char *, ...)), void *));
65 struct protent pap_protent = {
84 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
86 static void upap_timeout __P((void *));
87 static void upap_reqtimeout __P((void *));
88 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
89 static void upap_rauthack __P((upap_state *, u_char *, int, int));
90 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
91 static void upap_sauthreq __P((upap_state *));
92 static void upap_sresp __P((upap_state *, int, int, char *, int));
96 * upap_init - Initialize a UPAP unit.
102 upap_state *u = &upap[unit];
109 u->us_clientstate = UPAPCS_INITIAL;
110 u->us_serverstate = UPAPSS_INITIAL;
112 u->us_timeouttime = UPAP_DEFTIMEOUT;
113 u->us_maxtransmits = 10;
114 u->us_reqtimeout = UPAP_DEFREQTIME;
119 * upap_authwithpeer - Authenticate us with our peer (start client).
121 * Set new state and send authenticate's.
124 upap_authwithpeer(unit, user, password)
126 char *user, *password;
128 upap_state *u = &upap[unit];
130 /* Save the username and password we're given */
132 u->us_userlen = strlen(user);
133 u->us_passwd = password;
134 u->us_passwdlen = strlen(password);
137 /* Lower layer up yet? */
138 if (u->us_clientstate == UPAPCS_INITIAL ||
139 u->us_clientstate == UPAPCS_PENDING) {
140 u->us_clientstate = UPAPCS_PENDING;
144 upap_sauthreq(u); /* Start protocol */
149 * upap_authpeer - Authenticate our peer (start server).
157 upap_state *u = &upap[unit];
159 /* Lower layer up yet? */
160 if (u->us_serverstate == UPAPSS_INITIAL ||
161 u->us_serverstate == UPAPSS_PENDING) {
162 u->us_serverstate = UPAPSS_PENDING;
166 u->us_serverstate = UPAPSS_LISTEN;
167 if (u->us_reqtimeout > 0)
168 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
173 * upap_timeout - Retransmission timer for sending auth-reqs expired.
179 upap_state *u = (upap_state *) arg;
181 if (u->us_clientstate != UPAPCS_AUTHREQ)
184 if (u->us_transmits >= u->us_maxtransmits) {
185 /* give up in disgust */
186 syslog(LOG_ERR, "No response to PAP authenticate-requests");
187 u->us_clientstate = UPAPCS_BADAUTH;
188 auth_withpeer_fail(u->us_unit, PPP_PAP);
192 upap_sauthreq(u); /* Send Authenticate-Request */
197 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
203 upap_state *u = (upap_state *) arg;
205 if (u->us_serverstate != UPAPSS_LISTEN)
208 auth_peer_fail(u->us_unit, PPP_PAP);
209 u->us_serverstate = UPAPSS_BADAUTH;
214 * upap_lowerup - The lower layer is up.
216 * Start authenticating if pending.
222 upap_state *u = &upap[unit];
224 if (u->us_clientstate == UPAPCS_INITIAL)
225 u->us_clientstate = UPAPCS_CLOSED;
226 else if (u->us_clientstate == UPAPCS_PENDING) {
227 upap_sauthreq(u); /* send an auth-request */
230 if (u->us_serverstate == UPAPSS_INITIAL)
231 u->us_serverstate = UPAPSS_CLOSED;
232 else if (u->us_serverstate == UPAPSS_PENDING) {
233 u->us_serverstate = UPAPSS_LISTEN;
234 if (u->us_reqtimeout > 0)
235 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
241 * upap_lowerdown - The lower layer is down.
243 * Cancel all timeouts.
249 upap_state *u = &upap[unit];
251 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
252 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
253 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
254 UNTIMEOUT(upap_reqtimeout, u);
256 u->us_clientstate = UPAPCS_INITIAL;
257 u->us_serverstate = UPAPSS_INITIAL;
262 * upap_protrej - Peer doesn't speak this protocol.
264 * This shouldn't happen. In any case, pretend lower layer went down.
270 upap_state *u = &upap[unit];
272 if (u->us_clientstate == UPAPCS_AUTHREQ) {
273 syslog(LOG_ERR, "PAP authentication failed due to protocol-reject");
274 auth_withpeer_fail(unit, PPP_PAP);
276 if (u->us_serverstate == UPAPSS_LISTEN) {
277 syslog(LOG_ERR, "PAP authentication of peer failed (protocol-reject)");
278 auth_peer_fail(unit, PPP_PAP);
280 upap_lowerdown(unit);
285 * upap_input - Input UPAP packet.
288 upap_input(unit, inpacket, l)
293 upap_state *u = &upap[unit];
299 * Parse header (code, id and length).
300 * If packet too short, drop it.
303 if (l < UPAP_HEADERLEN) {
304 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short header."));
310 if (len < UPAP_HEADERLEN) {
311 UPAPDEBUG((LOG_INFO, "pap_input: rcvd illegal length."));
315 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short packet."));
318 len -= UPAP_HEADERLEN;
321 * Action depends on code.
325 upap_rauthreq(u, inp, id, len);
329 upap_rauthack(u, inp, id, len);
333 upap_rauthnak(u, inp, id, len);
336 default: /* XXX Need code reject */
343 * upap_rauth - Receive Authenticate.
346 upap_rauthreq(u, inp, id, len)
352 u_char ruserlen, rpasswdlen;
353 char *ruser, *rpasswd;
358 UPAPDEBUG((LOG_INFO, "pap_rauth: Rcvd id %d.", id));
360 if (u->us_serverstate < UPAPSS_LISTEN)
364 * If we receive a duplicate authenticate-request, we are
365 * supposed to return the same status as for the first request.
367 if (u->us_serverstate == UPAPSS_OPEN) {
368 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
371 if (u->us_serverstate == UPAPSS_BADAUTH) {
372 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
379 if (len < sizeof (u_char)) {
380 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
383 GETCHAR(ruserlen, inp);
384 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
386 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
389 ruser = (char *) inp;
390 INCPTR(ruserlen, inp);
391 GETCHAR(rpasswdlen, inp);
392 if (len < rpasswdlen) {
393 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
396 rpasswd = (char *) inp;
399 * Check the username and password given.
401 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
402 rpasswdlen, &msg, &msglen);
403 BZERO(rpasswd, rpasswdlen);
405 upap_sresp(u, retcode, id, msg, msglen);
407 if (retcode == UPAP_AUTHACK) {
408 u->us_serverstate = UPAPSS_OPEN;
409 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
411 u->us_serverstate = UPAPSS_BADAUTH;
412 auth_peer_fail(u->us_unit, PPP_PAP);
415 if (u->us_reqtimeout > 0)
416 UNTIMEOUT(upap_reqtimeout, u);
421 * upap_rauthack - Receive Authenticate-Ack.
424 upap_rauthack(u, inp, id, len)
433 UPAPDEBUG((LOG_INFO, "pap_rauthack: Rcvd id %d.", id));
434 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
440 if (len < sizeof (u_char)) {
441 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
444 GETCHAR(msglen, inp);
445 len -= sizeof (u_char);
447 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
451 PRINTMSG(msg, msglen);
453 u->us_clientstate = UPAPCS_OPEN;
455 auth_withpeer_success(u->us_unit, PPP_PAP);
460 * upap_rauthnak - Receive Authenticate-Nakk.
463 upap_rauthnak(u, inp, id, len)
472 UPAPDEBUG((LOG_INFO, "pap_rauthnak: Rcvd id %d.", id));
473 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
479 if (len < sizeof (u_char)) {
480 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
483 GETCHAR(msglen, inp);
484 len -= sizeof (u_char);
486 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
490 PRINTMSG(msg, msglen);
492 u->us_clientstate = UPAPCS_BADAUTH;
494 syslog(LOG_ERR, "PAP authentication failed");
495 auth_withpeer_fail(u->us_unit, PPP_PAP);
500 * upap_sauthreq - Send an Authenticate-Request.
509 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
510 u->us_userlen + u->us_passwdlen;
511 outp = outpacket_buf;
513 MAKEHEADER(outp, PPP_PAP);
515 PUTCHAR(UPAP_AUTHREQ, outp);
516 PUTCHAR(++u->us_id, outp);
517 PUTSHORT(outlen, outp);
518 PUTCHAR(u->us_userlen, outp);
519 BCOPY(u->us_user, outp, u->us_userlen);
520 INCPTR(u->us_userlen, outp);
521 PUTCHAR(u->us_passwdlen, outp);
522 BCOPY(u->us_passwd, outp, u->us_passwdlen);
524 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
526 UPAPDEBUG((LOG_INFO, "pap_sauth: Sent id %d.", u->us_id));
528 TIMEOUT(upap_timeout, u, u->us_timeouttime);
530 u->us_clientstate = UPAPCS_AUTHREQ;
535 * upap_sresp - Send a response (ack or nak).
538 upap_sresp(u, code, id, msg, msglen)
547 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
548 outp = outpacket_buf;
549 MAKEHEADER(outp, PPP_PAP);
553 PUTSHORT(outlen, outp);
554 PUTCHAR(msglen, outp);
555 BCOPY(msg, outp, msglen);
556 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
558 UPAPDEBUG((LOG_INFO, "pap_sresp: Sent code %d, id %d.", code, id));
562 * upap_printpkt - print the contents of a PAP packet.
564 static char *upap_codenames[] = {
565 "AuthReq", "AuthAck", "AuthNak"
569 upap_printpkt(p, plen, printer, arg)
572 void (*printer) __P((void *, char *, ...));
576 int mlen, ulen, wlen;
577 char *user, *pwd, *msg;
580 if (plen < UPAP_HEADERLEN)
586 if (len < UPAP_HEADERLEN || len > plen)
589 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
590 printer(arg, " %s", upap_codenames[code-1]);
592 printer(arg, " code=0x%x", code);
593 printer(arg, " id=0x%x", id);
594 len -= UPAP_HEADERLEN;
603 if (len < ulen + wlen + 2)
605 user = (char *) (p + 1);
606 pwd = (char *) (p + ulen + 2);
607 p += ulen + wlen + 2;
608 len -= ulen + wlen + 2;
609 printer(arg, " user=");
610 print_string(user, ulen, printer, arg);
611 printer(arg, " password=");
613 print_string(pwd, wlen, printer, arg);
615 printer(arg, "<hidden>");
624 msg = (char *) (p + 1);
628 print_string(msg, mlen, printer, arg);
632 /* print the rest of the bytes in the packet */
633 for (; len > 0; --len) {
635 printer(arg, " %.2x", code);