Changes to update Tomato RAF.
[tomato.git] / release / src / router / pppd / pppd / upap.c
blobc2b50f576c3375d1f9c670fbf95f213596888ad8
1 /*
2 * upap.c - User/Password Authentication Protocol.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
24 * 5000 Forbes Avenue
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
30 * acknowledgment:
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 #define RCSID "$Id: upap.c,v 1.30 2005/07/13 10:41:58 paulus Exp $"
46 * TODO:
49 #include <stdio.h>
50 #include <string.h>
52 #include "pppd.h"
53 #include "upap.h"
55 static const char rcsid[] = RCSID;
57 static bool hide_password = 1;
60 * Command-line options.
62 static option_t pap_option_list[] = {
63 { "hide-password", o_bool, &hide_password,
64 "Don't output passwords to log", OPT_PRIO | 1 },
65 { "show-password", o_bool, &hide_password,
66 "Show password string in debug log messages", OPT_PRIOSUB | 0 },
68 { "pap-restart", o_int, &upap[0].us_timeouttime,
69 "Set retransmit timeout for PAP", OPT_PRIO },
70 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
71 "Set max number of transmissions for auth-reqs", OPT_PRIO },
72 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
73 "Set time limit for peer PAP authentication", OPT_PRIO },
75 { NULL }
79 * Protocol entry points.
81 static void upap_init __P((int));
82 static void upap_lowerup __P((int));
83 static void upap_lowerdown __P((int));
84 static void upap_input __P((int, u_char *, int));
85 static void upap_protrej __P((int));
86 static int upap_printpkt __P((u_char *, int,
87 void (*) __P((void *, char *, ...)), void *));
89 struct protent pap_protent = {
90 PPP_PAP,
91 upap_init,
92 upap_input,
93 upap_protrej,
94 upap_lowerup,
95 upap_lowerdown,
96 NULL,
97 NULL,
98 upap_printpkt,
99 NULL,
101 "PAP",
102 NULL,
103 pap_option_list,
104 NULL,
105 NULL,
106 NULL
109 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
111 static void upap_timeout __P((void *));
112 static void upap_reqtimeout __P((void *));
113 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
114 static void upap_rauthack __P((upap_state *, u_char *, int, int));
115 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
116 static void upap_sauthreq __P((upap_state *));
117 static void upap_sresp __P((upap_state *, int, int, char *, int));
121 * upap_init - Initialize a UPAP unit.
123 static void
124 upap_init(unit)
125 int unit;
127 upap_state *u = &upap[unit];
129 u->us_unit = unit;
130 u->us_user = NULL;
131 u->us_userlen = 0;
132 u->us_passwd = NULL;
133 u->us_passwdlen = 0;
134 u->us_clientstate = UPAPCS_INITIAL;
135 u->us_serverstate = UPAPSS_INITIAL;
136 u->us_id = 0;
137 u->us_timeouttime = UPAP_DEFTIMEOUT;
138 u->us_maxtransmits = 10;
139 u->us_reqtimeout = UPAP_DEFREQTIME;
144 * upap_authwithpeer - Authenticate us with our peer (start client).
146 * Set new state and send authenticate's.
148 void
149 upap_authwithpeer(unit, user, password)
150 int unit;
151 char *user, *password;
153 upap_state *u = &upap[unit];
155 /* Save the username and password we're given */
156 u->us_user = user;
157 u->us_userlen = strlen(user);
158 u->us_passwd = password;
159 u->us_passwdlen = strlen(password);
160 u->us_transmits = 0;
162 /* Lower layer up yet? */
163 if (u->us_clientstate == UPAPCS_INITIAL ||
164 u->us_clientstate == UPAPCS_PENDING) {
165 u->us_clientstate = UPAPCS_PENDING;
166 return;
169 upap_sauthreq(u); /* Start protocol */
174 * upap_authpeer - Authenticate our peer (start server).
176 * Set new state.
178 void
179 upap_authpeer(unit)
180 int unit;
182 upap_state *u = &upap[unit];
184 /* Lower layer up yet? */
185 if (u->us_serverstate == UPAPSS_INITIAL ||
186 u->us_serverstate == UPAPSS_PENDING) {
187 u->us_serverstate = UPAPSS_PENDING;
188 return;
191 u->us_serverstate = UPAPSS_LISTEN;
192 if (u->us_reqtimeout > 0)
193 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
198 * upap_timeout - Retransmission timer for sending auth-reqs expired.
200 static void
201 upap_timeout(arg)
202 void *arg;
204 upap_state *u = (upap_state *) arg;
206 if (u->us_clientstate != UPAPCS_AUTHREQ)
207 return;
209 if (u->us_transmits >= u->us_maxtransmits) {
210 /* give up in disgust */
211 error("No response to PAP authenticate-requests");
212 u->us_clientstate = UPAPCS_BADAUTH;
213 auth_withpeer_fail(u->us_unit, PPP_PAP);
214 return;
217 upap_sauthreq(u); /* Send Authenticate-Request */
222 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
224 static void
225 upap_reqtimeout(arg)
226 void *arg;
228 upap_state *u = (upap_state *) arg;
230 if (u->us_serverstate != UPAPSS_LISTEN)
231 return; /* huh?? */
233 auth_peer_fail(u->us_unit, PPP_PAP);
234 u->us_serverstate = UPAPSS_BADAUTH;
239 * upap_lowerup - The lower layer is up.
241 * Start authenticating if pending.
243 static void
244 upap_lowerup(unit)
245 int unit;
247 upap_state *u = &upap[unit];
249 if (u->us_clientstate == UPAPCS_INITIAL)
250 u->us_clientstate = UPAPCS_CLOSED;
251 else if (u->us_clientstate == UPAPCS_PENDING) {
252 upap_sauthreq(u); /* send an auth-request */
255 if (u->us_serverstate == UPAPSS_INITIAL)
256 u->us_serverstate = UPAPSS_CLOSED;
257 else if (u->us_serverstate == UPAPSS_PENDING) {
258 u->us_serverstate = UPAPSS_LISTEN;
259 if (u->us_reqtimeout > 0)
260 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
266 * upap_lowerdown - The lower layer is down.
268 * Cancel all timeouts.
270 static void
271 upap_lowerdown(unit)
272 int unit;
274 upap_state *u = &upap[unit];
276 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
277 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
278 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
279 UNTIMEOUT(upap_reqtimeout, u);
281 u->us_clientstate = UPAPCS_INITIAL;
282 u->us_serverstate = UPAPSS_INITIAL;
287 * upap_protrej - Peer doesn't speak this protocol.
289 * This shouldn't happen. In any case, pretend lower layer went down.
291 static void
292 upap_protrej(unit)
293 int unit;
295 upap_state *u = &upap[unit];
297 if (u->us_clientstate == UPAPCS_AUTHREQ) {
298 error("PAP authentication failed due to protocol-reject");
299 auth_withpeer_fail(unit, PPP_PAP);
301 if (u->us_serverstate == UPAPSS_LISTEN) {
302 error("PAP authentication of peer failed (protocol-reject)");
303 auth_peer_fail(unit, PPP_PAP);
305 upap_lowerdown(unit);
310 * upap_input - Input UPAP packet.
312 static void
313 upap_input(unit, inpacket, l)
314 int unit;
315 u_char *inpacket;
316 int l;
318 upap_state *u = &upap[unit];
319 u_char *inp;
320 u_char code, id;
321 int len;
324 * Parse header (code, id and length).
325 * If packet too short, drop it.
327 inp = inpacket;
328 if (l < UPAP_HEADERLEN) {
329 UPAPDEBUG(("pap_input: rcvd short header."));
330 return;
332 GETCHAR(code, inp);
333 GETCHAR(id, inp);
334 GETSHORT(len, inp);
335 if (len < UPAP_HEADERLEN) {
336 UPAPDEBUG(("pap_input: rcvd illegal length."));
337 return;
339 if (len > l) {
340 UPAPDEBUG(("pap_input: rcvd short packet."));
341 return;
343 len -= UPAP_HEADERLEN;
346 * Action depends on code.
348 switch (code) {
349 case UPAP_AUTHREQ:
350 upap_rauthreq(u, inp, id, len);
351 break;
353 case UPAP_AUTHACK:
354 upap_rauthack(u, inp, id, len);
355 break;
357 case UPAP_AUTHNAK:
358 upap_rauthnak(u, inp, id, len);
359 break;
361 default: /* XXX Need code reject */
362 break;
368 * upap_rauth - Receive Authenticate.
370 static void
371 upap_rauthreq(u, inp, id, len)
372 upap_state *u;
373 u_char *inp;
374 int id;
375 int len;
377 u_char ruserlen, rpasswdlen;
378 char *ruser, *rpasswd;
379 char rhostname[256];
380 int retcode;
381 char *msg;
382 int msglen;
384 if (u->us_serverstate < UPAPSS_LISTEN)
385 return;
388 * If we receive a duplicate authenticate-request, we are
389 * supposed to return the same status as for the first request.
391 if (u->us_serverstate == UPAPSS_OPEN) {
392 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
393 return;
395 if (u->us_serverstate == UPAPSS_BADAUTH) {
396 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
397 return;
401 * Parse user/passwd.
403 if (len < 1) {
404 UPAPDEBUG(("pap_rauth: rcvd short packet."));
405 return;
407 GETCHAR(ruserlen, inp);
408 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
409 if (len < 0) {
410 UPAPDEBUG(("pap_rauth: rcvd short packet."));
411 return;
413 ruser = (char *) inp;
414 INCPTR(ruserlen, inp);
415 GETCHAR(rpasswdlen, inp);
416 if (len < rpasswdlen) {
417 UPAPDEBUG(("pap_rauth: rcvd short packet."));
418 return;
420 rpasswd = (char *) inp;
423 * Check the username and password given.
425 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
426 rpasswdlen, &msg);
427 BZERO(rpasswd, rpasswdlen);
430 * Check remote number authorization. A plugin may have filled in
431 * the remote number or added an allowed number, and rather than
432 * return an authenticate failure, is leaving it for us to verify.
434 if (retcode == UPAP_AUTHACK) {
435 if (!auth_number()) {
436 /* We do not want to leak info about the pap result. */
437 retcode = UPAP_AUTHNAK; /* XXX exit value will be "wrong" */
438 warn("calling number %q is not authorized", remote_number);
442 msglen = strlen(msg);
443 if (msglen > 255)
444 msglen = 255;
445 upap_sresp(u, retcode, id, msg, msglen);
447 /* Null terminate and clean remote name. */
448 slprintf(rhostname, sizeof(rhostname), "%.*v", ruserlen, ruser);
450 if (retcode == UPAP_AUTHACK) {
451 u->us_serverstate = UPAPSS_OPEN;
452 notice("PAP peer authentication succeeded for %q", rhostname);
453 auth_peer_success(u->us_unit, PPP_PAP, 0, ruser, ruserlen);
454 } else {
455 u->us_serverstate = UPAPSS_BADAUTH;
456 warn("PAP peer authentication failed for %q", rhostname);
457 auth_peer_fail(u->us_unit, PPP_PAP);
460 if (u->us_reqtimeout > 0)
461 UNTIMEOUT(upap_reqtimeout, u);
466 * upap_rauthack - Receive Authenticate-Ack.
468 static void
469 upap_rauthack(u, inp, id, len)
470 upap_state *u;
471 u_char *inp;
472 int id;
473 int len;
475 u_char msglen;
476 char *msg;
478 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
479 return;
482 * Parse message.
484 if (len < 1) {
485 UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
486 } else {
487 GETCHAR(msglen, inp);
488 if (msglen > 0) {
489 len -= sizeof (u_char);
490 if (len < msglen) {
491 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
492 return;
494 msg = (char *) inp;
495 PRINTMSG(msg, msglen);
499 u->us_clientstate = UPAPCS_OPEN;
501 auth_withpeer_success(u->us_unit, PPP_PAP, 0);
506 * upap_rauthnak - Receive Authenticate-Nak.
508 static void
509 upap_rauthnak(u, inp, id, len)
510 upap_state *u;
511 u_char *inp;
512 int id;
513 int len;
515 u_char msglen;
516 char *msg;
518 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
519 return;
522 * Parse message.
524 if (len < 1) {
525 UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
526 } else {
527 GETCHAR(msglen, inp);
528 if (msglen > 0) {
529 len -= sizeof (u_char);
530 if (len < msglen) {
531 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
532 return;
534 msg = (char *) inp;
535 PRINTMSG(msg, msglen);
539 u->us_clientstate = UPAPCS_BADAUTH;
541 error("PAP authentication failed");
542 system("ppp_event -t PAP_AUTH_FAIL &");
543 auth_withpeer_fail(u->us_unit, PPP_PAP);
548 * upap_sauthreq - Send an Authenticate-Request.
550 static void
551 upap_sauthreq(u)
552 upap_state *u;
554 u_char *outp;
555 int outlen;
557 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
558 u->us_userlen + u->us_passwdlen;
559 outp = outpacket_buf;
561 MAKEHEADER(outp, PPP_PAP);
563 PUTCHAR(UPAP_AUTHREQ, outp);
564 PUTCHAR(++u->us_id, outp);
565 PUTSHORT(outlen, outp);
566 PUTCHAR(u->us_userlen, outp);
567 BCOPY(u->us_user, outp, u->us_userlen);
568 INCPTR(u->us_userlen, outp);
569 PUTCHAR(u->us_passwdlen, outp);
570 BCOPY(u->us_passwd, outp, u->us_passwdlen);
572 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
574 TIMEOUT(upap_timeout, u, u->us_timeouttime);
575 ++u->us_transmits;
576 u->us_clientstate = UPAPCS_AUTHREQ;
581 * upap_sresp - Send a response (ack or nak).
583 static void
584 upap_sresp(u, code, id, msg, msglen)
585 upap_state *u;
586 u_char code, id;
587 char *msg;
588 int msglen;
590 u_char *outp;
591 int outlen;
593 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
594 outp = outpacket_buf;
595 MAKEHEADER(outp, PPP_PAP);
597 PUTCHAR(code, outp);
598 PUTCHAR(id, outp);
599 PUTSHORT(outlen, outp);
600 PUTCHAR(msglen, outp);
601 BCOPY(msg, outp, msglen);
602 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
606 * upap_printpkt - print the contents of a PAP packet.
608 static char *upap_codenames[] = {
609 "AuthReq", "AuthAck", "AuthNak"
612 static int
613 upap_printpkt(p, plen, printer, arg)
614 u_char *p;
615 int plen;
616 void (*printer) __P((void *, char *, ...));
617 void *arg;
619 int code, id, len;
620 int mlen, ulen, wlen;
621 char *user, *pwd, *msg;
622 u_char *pstart;
624 if (plen < UPAP_HEADERLEN)
625 return 0;
626 pstart = p;
627 GETCHAR(code, p);
628 GETCHAR(id, p);
629 GETSHORT(len, p);
630 if (len < UPAP_HEADERLEN || len > plen)
631 return 0;
633 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
634 printer(arg, " %s", upap_codenames[code-1]);
635 else
636 printer(arg, " code=0x%x", code);
637 printer(arg, " id=0x%x", id);
638 len -= UPAP_HEADERLEN;
639 switch (code) {
640 case UPAP_AUTHREQ:
641 if (len < 1)
642 break;
643 ulen = p[0];
644 if (len < ulen + 2)
645 break;
646 wlen = p[ulen + 1];
647 if (len < ulen + wlen + 2)
648 break;
649 user = (char *) (p + 1);
650 pwd = (char *) (p + ulen + 2);
651 p += ulen + wlen + 2;
652 len -= ulen + wlen + 2;
653 printer(arg, " user=");
654 print_string(user, ulen, printer, arg);
655 printer(arg, " password=");
656 if (!hide_password)
657 print_string(pwd, wlen, printer, arg);
658 else
659 printer(arg, "<hidden>");
660 break;
661 case UPAP_AUTHACK:
662 case UPAP_AUTHNAK:
663 if (len < 1)
664 break;
665 mlen = p[0];
666 if (len < mlen + 1)
667 break;
668 msg = (char *) (p + 1);
669 p += mlen + 1;
670 len -= mlen + 1;
671 printer(arg, " ");
672 print_string(msg, mlen, printer, arg);
673 break;
676 /* print the rest of the bytes in the packet */
677 for (; len > 0; --len) {
678 GETCHAR(code, p);
679 printer(arg, " %.2x", code);
682 return p - pstart;