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.14 1999/03/16 03:15:15 paulus Exp $";
34 static bool hide_password;
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" },
42 { "pap-restart", o_int, &upap[0].us_timeouttime,
43 "Set retransmit timeout for PAP" },
44 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
45 "Set max number of transmissions for auth-reqs" },
46 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
47 "Set time limit for peer PAP authentication" },
52 * Protocol entry points.
54 static void upap_init __P((int));
55 static void upap_lowerup __P((int));
56 static void upap_lowerdown __P((int));
57 static void upap_input __P((int, u_char *, int));
58 static void upap_protrej __P((int));
59 static int upap_printpkt __P((u_char *, int,
60 void (*) __P((void *, char *, ...)), void *));
62 struct protent pap_protent = {
81 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
83 static void upap_timeout __P((void *));
84 static void upap_reqtimeout __P((void *));
85 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
86 static void upap_rauthack __P((upap_state *, u_char *, int, int));
87 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
88 static void upap_sauthreq __P((upap_state *));
89 static void upap_sresp __P((upap_state *, int, int, char *, int));
93 * upap_init - Initialize a UPAP unit.
99 upap_state *u = &upap[unit];
106 u->us_clientstate = UPAPCS_INITIAL;
107 u->us_serverstate = UPAPSS_INITIAL;
109 u->us_timeouttime = UPAP_DEFTIMEOUT;
110 u->us_maxtransmits = 10;
111 u->us_reqtimeout = UPAP_DEFREQTIME;
116 * upap_authwithpeer - Authenticate us with our peer (start client).
118 * Set new state and send authenticate's.
121 upap_authwithpeer(unit, user, password)
123 char *user, *password;
125 upap_state *u = &upap[unit];
127 /* Save the username and password we're given */
129 u->us_userlen = strlen(user);
130 u->us_passwd = password;
131 u->us_passwdlen = strlen(password);
134 /* Lower layer up yet? */
135 if (u->us_clientstate == UPAPCS_INITIAL ||
136 u->us_clientstate == UPAPCS_PENDING) {
137 u->us_clientstate = UPAPCS_PENDING;
141 upap_sauthreq(u); /* Start protocol */
146 * upap_authpeer - Authenticate our peer (start server).
154 upap_state *u = &upap[unit];
156 /* Lower layer up yet? */
157 if (u->us_serverstate == UPAPSS_INITIAL ||
158 u->us_serverstate == UPAPSS_PENDING) {
159 u->us_serverstate = UPAPSS_PENDING;
163 u->us_serverstate = UPAPSS_LISTEN;
164 if (u->us_reqtimeout > 0)
165 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
170 * upap_timeout - Retransmission timer for sending auth-reqs expired.
176 upap_state *u = (upap_state *) arg;
178 if (u->us_clientstate != UPAPCS_AUTHREQ)
181 if (u->us_transmits >= u->us_maxtransmits) {
182 /* give up in disgust */
183 error("No response to PAP authenticate-requests");
184 u->us_clientstate = UPAPCS_BADAUTH;
185 auth_withpeer_fail(u->us_unit, PPP_PAP);
189 upap_sauthreq(u); /* Send Authenticate-Request */
194 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
200 upap_state *u = (upap_state *) arg;
202 if (u->us_serverstate != UPAPSS_LISTEN)
205 auth_peer_fail(u->us_unit, PPP_PAP);
206 u->us_serverstate = UPAPSS_BADAUTH;
211 * upap_lowerup - The lower layer is up.
213 * Start authenticating if pending.
219 upap_state *u = &upap[unit];
221 if (u->us_clientstate == UPAPCS_INITIAL)
222 u->us_clientstate = UPAPCS_CLOSED;
223 else if (u->us_clientstate == UPAPCS_PENDING) {
224 upap_sauthreq(u); /* send an auth-request */
227 if (u->us_serverstate == UPAPSS_INITIAL)
228 u->us_serverstate = UPAPSS_CLOSED;
229 else if (u->us_serverstate == UPAPSS_PENDING) {
230 u->us_serverstate = UPAPSS_LISTEN;
231 if (u->us_reqtimeout > 0)
232 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
238 * upap_lowerdown - The lower layer is down.
240 * Cancel all timeouts.
246 upap_state *u = &upap[unit];
248 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
249 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
250 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
251 UNTIMEOUT(upap_reqtimeout, u);
253 u->us_clientstate = UPAPCS_INITIAL;
254 u->us_serverstate = UPAPSS_INITIAL;
259 * upap_protrej - Peer doesn't speak this protocol.
261 * This shouldn't happen. In any case, pretend lower layer went down.
267 upap_state *u = &upap[unit];
269 if (u->us_clientstate == UPAPCS_AUTHREQ) {
270 error("PAP authentication failed due to protocol-reject");
271 auth_withpeer_fail(unit, PPP_PAP);
273 if (u->us_serverstate == UPAPSS_LISTEN) {
274 error("PAP authentication of peer failed (protocol-reject)");
275 auth_peer_fail(unit, PPP_PAP);
277 upap_lowerdown(unit);
282 * upap_input - Input UPAP packet.
285 upap_input(unit, inpacket, l)
290 upap_state *u = &upap[unit];
296 * Parse header (code, id and length).
297 * If packet too short, drop it.
300 if (l < UPAP_HEADERLEN) {
301 UPAPDEBUG(("pap_input: rcvd short header."));
307 if (len < UPAP_HEADERLEN) {
308 UPAPDEBUG(("pap_input: rcvd illegal length."));
312 UPAPDEBUG(("pap_input: rcvd short packet."));
315 len -= UPAP_HEADERLEN;
318 * Action depends on code.
322 upap_rauthreq(u, inp, id, len);
326 upap_rauthack(u, inp, id, len);
330 upap_rauthnak(u, inp, id, len);
333 default: /* XXX Need code reject */
340 * upap_rauth - Receive Authenticate.
343 upap_rauthreq(u, inp, id, len)
349 u_char ruserlen, rpasswdlen;
350 char *ruser, *rpasswd;
355 if (u->us_serverstate < UPAPSS_LISTEN)
359 * If we receive a duplicate authenticate-request, we are
360 * supposed to return the same status as for the first request.
362 if (u->us_serverstate == UPAPSS_OPEN) {
363 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
366 if (u->us_serverstate == UPAPSS_BADAUTH) {
367 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
374 if (len < sizeof (u_char)) {
375 UPAPDEBUG(("pap_rauth: rcvd short packet."));
378 GETCHAR(ruserlen, inp);
379 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
381 UPAPDEBUG(("pap_rauth: rcvd short packet."));
384 ruser = (char *) inp;
385 INCPTR(ruserlen, inp);
386 GETCHAR(rpasswdlen, inp);
387 if (len < rpasswdlen) {
388 UPAPDEBUG(("pap_rauth: rcvd short packet."));
391 rpasswd = (char *) inp;
394 * Check the username and password given.
396 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
397 rpasswdlen, &msg, &msglen);
398 BZERO(rpasswd, rpasswdlen);
400 upap_sresp(u, retcode, id, msg, msglen);
402 if (retcode == UPAP_AUTHACK) {
403 u->us_serverstate = UPAPSS_OPEN;
404 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
406 u->us_serverstate = UPAPSS_BADAUTH;
407 auth_peer_fail(u->us_unit, PPP_PAP);
410 if (u->us_reqtimeout > 0)
411 UNTIMEOUT(upap_reqtimeout, u);
416 * upap_rauthack - Receive Authenticate-Ack.
419 upap_rauthack(u, inp, id, len)
428 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
434 if (len < sizeof (u_char)) {
435 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
438 GETCHAR(msglen, inp);
439 len -= sizeof (u_char);
441 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
445 PRINTMSG(msg, msglen);
447 u->us_clientstate = UPAPCS_OPEN;
449 auth_withpeer_success(u->us_unit, PPP_PAP);
454 * upap_rauthnak - Receive Authenticate-Nakk.
457 upap_rauthnak(u, inp, id, len)
466 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
472 if (len < sizeof (u_char)) {
473 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
476 GETCHAR(msglen, inp);
477 len -= sizeof (u_char);
479 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
483 PRINTMSG(msg, msglen);
485 u->us_clientstate = UPAPCS_BADAUTH;
487 error("PAP authentication failed");
488 auth_withpeer_fail(u->us_unit, PPP_PAP);
493 * upap_sauthreq - Send an Authenticate-Request.
502 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
503 u->us_userlen + u->us_passwdlen;
504 outp = outpacket_buf;
506 MAKEHEADER(outp, PPP_PAP);
508 PUTCHAR(UPAP_AUTHREQ, outp);
509 PUTCHAR(++u->us_id, outp);
510 PUTSHORT(outlen, outp);
511 PUTCHAR(u->us_userlen, outp);
512 BCOPY(u->us_user, outp, u->us_userlen);
513 INCPTR(u->us_userlen, outp);
514 PUTCHAR(u->us_passwdlen, outp);
515 BCOPY(u->us_passwd, outp, u->us_passwdlen);
517 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
519 TIMEOUT(upap_timeout, u, u->us_timeouttime);
521 u->us_clientstate = UPAPCS_AUTHREQ;
526 * upap_sresp - Send a response (ack or nak).
529 upap_sresp(u, code, id, msg, msglen)
538 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
539 outp = outpacket_buf;
540 MAKEHEADER(outp, PPP_PAP);
544 PUTSHORT(outlen, outp);
545 PUTCHAR(msglen, outp);
546 BCOPY(msg, outp, msglen);
547 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
551 * upap_printpkt - print the contents of a PAP packet.
553 static char *upap_codenames[] = {
554 "AuthReq", "AuthAck", "AuthNak"
558 upap_printpkt(p, plen, printer, arg)
561 void (*printer) __P((void *, char *, ...));
565 int mlen, ulen, wlen;
566 char *user, *pwd, *msg;
569 if (plen < UPAP_HEADERLEN)
575 if (len < UPAP_HEADERLEN || len > plen)
578 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
579 printer(arg, " %s", upap_codenames[code-1]);
581 printer(arg, " code=0x%x", code);
582 printer(arg, " id=0x%x", id);
583 len -= UPAP_HEADERLEN;
592 if (len < ulen + wlen + 2)
594 user = (char *) (p + 1);
595 pwd = (char *) (p + ulen + 2);
596 p += ulen + wlen + 2;
597 len -= ulen + wlen + 2;
598 printer(arg, " user=");
599 print_string(user, ulen, printer, arg);
600 printer(arg, " password=");
602 print_string(pwd, wlen, printer, arg);
604 printer(arg, "<hidden>");
613 msg = (char *) (p + 1);
617 print_string(msg, mlen, printer, arg);
621 /* print the rest of the bytes in the packet */
622 for (; len > 0; --len) {
624 printer(arg, " %.2x", code);