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.
20 #define RCSID "$Id: upap.c,v 1.23 1999/11/20 05:11:47 paulus Exp $"
32 static const char rcsid[] = RCSID;
34 static bool hide_password = 1;
37 * Command-line options.
39 static option_t pap_option_list[] = {
40 { "hide-password", o_bool, &hide_password,
41 "Don't output passwords to log", 1 },
42 { "show-password", o_bool, &hide_password,
43 "Show password string in debug log messages", 0 },
44 { "pap-restart", o_int, &upap[0].us_timeouttime,
45 "Set retransmit timeout for PAP" },
46 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
47 "Set max number of transmissions for auth-reqs" },
48 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
49 "Set time limit for peer PAP authentication" },
54 * Protocol entry points.
56 static void upap_init __P((int));
57 static void upap_lowerup __P((int));
58 static void upap_lowerdown __P((int));
59 static void upap_input __P((int, u_char *, int));
60 static void upap_protrej __P((int));
61 static int upap_printpkt __P((u_char *, int,
62 void (*) __P((void *, char *, ...)), void *));
64 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 error("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 error("PAP authentication failed due to protocol-reject");
274 auth_withpeer_fail(unit, PPP_PAP);
276 if (u->us_serverstate == UPAPSS_LISTEN) {
277 error("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(("pap_input: rcvd short header."));
310 if (len < UPAP_HEADERLEN) {
311 UPAPDEBUG(("pap_input: rcvd illegal length."));
315 UPAPDEBUG(("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 if (u->us_serverstate < UPAPSS_LISTEN)
362 * If we receive a duplicate authenticate-request, we are
363 * supposed to return the same status as for the first request.
365 if (u->us_serverstate == UPAPSS_OPEN) {
366 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
369 if (u->us_serverstate == UPAPSS_BADAUTH) {
370 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
378 UPAPDEBUG(("pap_rauth: rcvd short packet."));
381 GETCHAR(ruserlen, inp);
382 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
384 UPAPDEBUG(("pap_rauth: rcvd short packet."));
387 ruser = (char *) inp;
388 INCPTR(ruserlen, inp);
389 GETCHAR(rpasswdlen, inp);
390 if (len < rpasswdlen) {
391 UPAPDEBUG(("pap_rauth: rcvd short packet."));
394 rpasswd = (char *) inp;
397 * Check the username and password given.
399 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
401 BZERO(rpasswd, rpasswdlen);
402 msglen = strlen(msg);
406 upap_sresp(u, retcode, id, msg, msglen);
408 if (retcode == UPAP_AUTHACK) {
409 u->us_serverstate = UPAPSS_OPEN;
410 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
412 u->us_serverstate = UPAPSS_BADAUTH;
413 auth_peer_fail(u->us_unit, PPP_PAP);
416 if (u->us_reqtimeout > 0)
417 UNTIMEOUT(upap_reqtimeout, u);
422 * upap_rauthack - Receive Authenticate-Ack.
425 upap_rauthack(u, inp, id, len)
434 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
441 UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
443 GETCHAR(msglen, inp);
445 len -= sizeof (u_char);
447 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
451 PRINTMSG(msg, msglen);
455 u->us_clientstate = UPAPCS_OPEN;
457 auth_withpeer_success(u->us_unit, PPP_PAP);
462 * upap_rauthnak - Receive Authenticate-Nakk.
465 upap_rauthnak(u, inp, id, len)
474 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
481 UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
483 GETCHAR(msglen, inp);
485 len -= sizeof (u_char);
487 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
491 PRINTMSG(msg, msglen);
495 u->us_clientstate = UPAPCS_BADAUTH;
497 error("PAP authentication failed");
498 auth_withpeer_fail(u->us_unit, PPP_PAP);
503 * upap_sauthreq - Send an Authenticate-Request.
512 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
513 u->us_userlen + u->us_passwdlen;
514 outp = outpacket_buf;
516 MAKEHEADER(outp, PPP_PAP);
518 PUTCHAR(UPAP_AUTHREQ, outp);
519 PUTCHAR(++u->us_id, outp);
520 PUTSHORT(outlen, outp);
521 PUTCHAR(u->us_userlen, outp);
522 BCOPY(u->us_user, outp, u->us_userlen);
523 INCPTR(u->us_userlen, outp);
524 PUTCHAR(u->us_passwdlen, outp);
525 BCOPY(u->us_passwd, outp, u->us_passwdlen);
527 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
529 TIMEOUT(upap_timeout, u, u->us_timeouttime);
531 u->us_clientstate = UPAPCS_AUTHREQ;
536 * upap_sresp - Send a response (ack or nak).
539 upap_sresp(u, code, id, msg, msglen)
548 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
549 outp = outpacket_buf;
550 MAKEHEADER(outp, PPP_PAP);
554 PUTSHORT(outlen, outp);
555 PUTCHAR(msglen, outp);
556 BCOPY(msg, outp, msglen);
557 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
561 * upap_printpkt - print the contents of a PAP packet.
563 static char *upap_codenames[] = {
564 "AuthReq", "AuthAck", "AuthNak"
568 upap_printpkt(p, plen, printer, arg)
571 void (*printer) __P((void *, char *, ...));
575 int mlen, ulen, wlen;
576 char *user, *pwd, *msg;
579 if (plen < UPAP_HEADERLEN)
585 if (len < UPAP_HEADERLEN || len > plen)
588 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
589 printer(arg, " %s", upap_codenames[code-1]);
591 printer(arg, " code=0x%x", code);
592 printer(arg, " id=0x%x", id);
593 len -= UPAP_HEADERLEN;
602 if (len < ulen + wlen + 2)
604 user = (char *) (p + 1);
605 pwd = (char *) (p + ulen + 2);
606 p += ulen + wlen + 2;
607 len -= ulen + wlen + 2;
608 printer(arg, " user=");
609 print_string(user, ulen, printer, arg);
610 printer(arg, " password=");
612 print_string(pwd, wlen, printer, arg);
614 printer(arg, "<hidden>");
623 msg = (char *) (p + 1);
627 print_string(msg, mlen, printer, arg);
631 /* print the rest of the bytes in the packet */
632 for (; len > 0; --len) {
634 printer(arg, " %.2x", code);