Tomato 1.28
[tomato.git] / release / src / router / pppd / pppd / upap.c
blob717cbd18d16212d2f7743d3ab8f8ef61cfe6118c
1 /*
2 * upap.c - User/Password Authentication Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
5 * All rights reserved.
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.3 2004/10/06 10:23:18 honor Exp $"
23 * TODO:
26 #include <stdio.h>
27 #include <string.h>
29 #include "pppd.h"
30 #include "upap.h"
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", OPT_PRIO | 1 },
42 { "show-password", o_bool, &hide_password,
43 "Show password string in debug log messages", OPT_PRIOSUB | 0 },
45 { "pap-restart", o_int, &upap[0].us_timeouttime,
46 "Set retransmit timeout for PAP", OPT_PRIO },
47 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
48 "Set max number of transmissions for auth-reqs", OPT_PRIO },
49 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
50 "Set time limit for peer PAP authentication", OPT_PRIO },
52 { NULL }
56 * Protocol entry points.
58 static void upap_init __P((int));
59 static void upap_lowerup __P((int));
60 static void upap_lowerdown __P((int));
61 static void upap_input __P((int, u_char *, int));
62 static void upap_protrej __P((int));
63 static int upap_printpkt __P((u_char *, int,
64 void (*) __P((void *, char *, ...)), void *));
66 struct protent pap_protent = {
67 PPP_PAP,
68 upap_init,
69 upap_input,
70 upap_protrej,
71 upap_lowerup,
72 upap_lowerdown,
73 NULL,
74 NULL,
75 upap_printpkt,
76 NULL,
78 "PAP",
79 NULL,
80 pap_option_list,
81 NULL,
82 NULL,
83 NULL
86 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
88 static void upap_timeout __P((void *));
89 static void upap_reqtimeout __P((void *));
90 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
91 static void upap_rauthack __P((upap_state *, u_char *, int, int));
92 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
93 static void upap_sauthreq __P((upap_state *));
94 static void upap_sresp __P((upap_state *, int, int, char *, int));
98 * upap_init - Initialize a UPAP unit.
100 static void
101 upap_init(unit)
102 int unit;
104 upap_state *u = &upap[unit];
106 u->us_unit = unit;
107 u->us_user = NULL;
108 u->us_userlen = 0;
109 u->us_passwd = NULL;
110 u->us_passwdlen = 0;
111 u->us_clientstate = UPAPCS_INITIAL;
112 u->us_serverstate = UPAPSS_INITIAL;
113 u->us_id = 0;
114 u->us_timeouttime = UPAP_DEFTIMEOUT;
115 u->us_maxtransmits = 10;
116 u->us_reqtimeout = UPAP_DEFREQTIME;
121 * upap_authwithpeer - Authenticate us with our peer (start client).
123 * Set new state and send authenticate's.
125 void
126 upap_authwithpeer(unit, user, password)
127 int unit;
128 char *user, *password;
130 upap_state *u = &upap[unit];
132 /* Save the username and password we're given */
133 u->us_user = user;
134 u->us_userlen = strlen(user);
135 u->us_passwd = password;
136 u->us_passwdlen = strlen(password);
137 u->us_transmits = 0;
139 /* Lower layer up yet? */
140 if (u->us_clientstate == UPAPCS_INITIAL ||
141 u->us_clientstate == UPAPCS_PENDING) {
142 u->us_clientstate = UPAPCS_PENDING;
143 return;
146 upap_sauthreq(u); /* Start protocol */
151 * upap_authpeer - Authenticate our peer (start server).
153 * Set new state.
155 void
156 upap_authpeer(unit)
157 int unit;
159 upap_state *u = &upap[unit];
161 /* Lower layer up yet? */
162 if (u->us_serverstate == UPAPSS_INITIAL ||
163 u->us_serverstate == UPAPSS_PENDING) {
164 u->us_serverstate = UPAPSS_PENDING;
165 return;
168 u->us_serverstate = UPAPSS_LISTEN;
169 if (u->us_reqtimeout > 0)
170 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
175 * upap_timeout - Retransmission timer for sending auth-reqs expired.
177 static void
178 upap_timeout(arg)
179 void *arg;
181 upap_state *u = (upap_state *) arg;
183 if (u->us_clientstate != UPAPCS_AUTHREQ)
184 return;
186 if (u->us_transmits >= u->us_maxtransmits) {
187 /* give up in disgust */
188 error("No response to PAP authenticate-requests");
189 u->us_clientstate = UPAPCS_BADAUTH;
190 auth_withpeer_fail(u->us_unit, PPP_PAP);
191 return;
194 upap_sauthreq(u); /* Send Authenticate-Request */
199 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
201 static void
202 upap_reqtimeout(arg)
203 void *arg;
205 upap_state *u = (upap_state *) arg;
207 if (u->us_serverstate != UPAPSS_LISTEN)
208 return; /* huh?? */
210 auth_peer_fail(u->us_unit, PPP_PAP);
211 u->us_serverstate = UPAPSS_BADAUTH;
216 * upap_lowerup - The lower layer is up.
218 * Start authenticating if pending.
220 static void
221 upap_lowerup(unit)
222 int unit;
224 upap_state *u = &upap[unit];
226 if (u->us_clientstate == UPAPCS_INITIAL)
227 u->us_clientstate = UPAPCS_CLOSED;
228 else if (u->us_clientstate == UPAPCS_PENDING) {
229 upap_sauthreq(u); /* send an auth-request */
232 if (u->us_serverstate == UPAPSS_INITIAL)
233 u->us_serverstate = UPAPSS_CLOSED;
234 else if (u->us_serverstate == UPAPSS_PENDING) {
235 u->us_serverstate = UPAPSS_LISTEN;
236 if (u->us_reqtimeout > 0)
237 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
243 * upap_lowerdown - The lower layer is down.
245 * Cancel all timeouts.
247 static void
248 upap_lowerdown(unit)
249 int unit;
251 upap_state *u = &upap[unit];
253 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
254 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
255 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
256 UNTIMEOUT(upap_reqtimeout, u);
258 u->us_clientstate = UPAPCS_INITIAL;
259 u->us_serverstate = UPAPSS_INITIAL;
264 * upap_protrej - Peer doesn't speak this protocol.
266 * This shouldn't happen. In any case, pretend lower layer went down.
268 static void
269 upap_protrej(unit)
270 int unit;
272 upap_state *u = &upap[unit];
274 if (u->us_clientstate == UPAPCS_AUTHREQ) {
275 error("PAP authentication failed due to protocol-reject");
276 auth_withpeer_fail(unit, PPP_PAP);
278 if (u->us_serverstate == UPAPSS_LISTEN) {
279 error("PAP authentication of peer failed (protocol-reject)");
280 auth_peer_fail(unit, PPP_PAP);
282 upap_lowerdown(unit);
287 * upap_input - Input UPAP packet.
289 static void
290 upap_input(unit, inpacket, l)
291 int unit;
292 u_char *inpacket;
293 int l;
295 upap_state *u = &upap[unit];
296 u_char *inp;
297 u_char code, id;
298 int len;
301 * Parse header (code, id and length).
302 * If packet too short, drop it.
304 inp = inpacket;
305 if (l < UPAP_HEADERLEN) {
306 UPAPDEBUG(("pap_input: rcvd short header."));
307 return;
309 GETCHAR(code, inp);
310 GETCHAR(id, inp);
311 GETSHORT(len, inp);
312 if (len < UPAP_HEADERLEN) {
313 UPAPDEBUG(("pap_input: rcvd illegal length."));
314 return;
316 if (len > l) {
317 UPAPDEBUG(("pap_input: rcvd short packet."));
318 return;
320 len -= UPAP_HEADERLEN;
323 * Action depends on code.
325 switch (code) {
326 case UPAP_AUTHREQ:
327 upap_rauthreq(u, inp, id, len);
328 break;
330 case UPAP_AUTHACK:
331 upap_rauthack(u, inp, id, len);
332 break;
334 case UPAP_AUTHNAK:
335 upap_rauthnak(u, inp, id, len);
336 break;
338 default: /* XXX Need code reject */
339 break;
345 * upap_rauth - Receive Authenticate.
347 static void
348 upap_rauthreq(u, inp, id, len)
349 upap_state *u;
350 u_char *inp;
351 int id;
352 int len;
354 u_char ruserlen, rpasswdlen;
355 char *ruser, *rpasswd;
356 int retcode;
357 char *msg;
358 int msglen;
360 if (u->us_serverstate < UPAPSS_LISTEN)
361 return;
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 */
369 return;
371 if (u->us_serverstate == UPAPSS_BADAUTH) {
372 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
373 return;
377 * Parse user/passwd.
379 if (len < 1) {
380 UPAPDEBUG(("pap_rauth: rcvd short packet."));
381 return;
383 GETCHAR(ruserlen, inp);
384 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
385 if (len < 0) {
386 UPAPDEBUG(("pap_rauth: rcvd short packet."));
387 return;
389 ruser = (char *) inp;
390 INCPTR(ruserlen, inp);
391 GETCHAR(rpasswdlen, inp);
392 if (len < rpasswdlen) {
393 UPAPDEBUG(("pap_rauth: rcvd short packet."));
394 return;
396 rpasswd = (char *) inp;
399 * Check the username and password given.
401 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
402 rpasswdlen, &msg);
403 BZERO(rpasswd, rpasswdlen);
404 msglen = strlen(msg);
405 if (msglen > 255)
406 msglen = 255;
408 upap_sresp(u, retcode, id, msg, msglen);
410 if (retcode == UPAP_AUTHACK) {
411 u->us_serverstate = UPAPSS_OPEN;
412 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
413 } else {
414 u->us_serverstate = UPAPSS_BADAUTH;
415 auth_peer_fail(u->us_unit, PPP_PAP);
418 if (u->us_reqtimeout > 0)
419 UNTIMEOUT(upap_reqtimeout, u);
424 * upap_rauthack - Receive Authenticate-Ack.
426 static void
427 upap_rauthack(u, inp, id, len)
428 upap_state *u;
429 u_char *inp;
430 int id;
431 int len;
433 u_char msglen;
434 char *msg;
436 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
437 return;
440 * Parse message.
442 if (len < 1) {
443 UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
444 } else {
445 GETCHAR(msglen, inp);
446 if (msglen > 0) {
447 len -= sizeof (u_char);
448 if (len < msglen) {
449 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
450 return;
452 msg = (char *) inp;
453 PRINTMSG(msg, msglen);
457 u->us_clientstate = UPAPCS_OPEN;
459 auth_withpeer_success(u->us_unit, PPP_PAP);
464 * upap_rauthnak - Receive Authenticate-Nakk.
466 static void
467 upap_rauthnak(u, inp, id, len)
468 upap_state *u;
469 u_char *inp;
470 int id;
471 int len;
473 u_char msglen;
474 char *msg;
476 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
477 return;
480 * Parse message.
482 if (len < 1) {
483 UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
484 } else {
485 GETCHAR(msglen, inp);
486 if (msglen > 0) {
487 len -= sizeof (u_char);
488 if (len < msglen) {
489 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
490 return;
492 msg = (char *) inp;
493 PRINTMSG(msg, msglen);
497 u->us_clientstate = UPAPCS_BADAUTH;
499 error("PAP authentication failed");
501 //log_to_file("PAP_AUTH_FAIL");
502 system("ppp_event -t PAP_AUTH_FAIL &");
504 auth_withpeer_fail(u->us_unit, PPP_PAP);
509 * upap_sauthreq - Send an Authenticate-Request.
511 static void
512 upap_sauthreq(u)
513 upap_state *u;
515 u_char *outp;
516 int outlen;
518 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
519 u->us_userlen + u->us_passwdlen;
520 outp = outpacket_buf;
522 MAKEHEADER(outp, PPP_PAP);
524 PUTCHAR(UPAP_AUTHREQ, outp);
525 PUTCHAR(++u->us_id, outp);
526 PUTSHORT(outlen, outp);
527 PUTCHAR(u->us_userlen, outp);
528 BCOPY(u->us_user, outp, u->us_userlen);
529 INCPTR(u->us_userlen, outp);
530 PUTCHAR(u->us_passwdlen, outp);
531 BCOPY(u->us_passwd, outp, u->us_passwdlen);
533 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
535 TIMEOUT(upap_timeout, u, u->us_timeouttime);
536 ++u->us_transmits;
537 u->us_clientstate = UPAPCS_AUTHREQ;
542 * upap_sresp - Send a response (ack or nak).
544 static void
545 upap_sresp(u, code, id, msg, msglen)
546 upap_state *u;
547 u_char code, id;
548 char *msg;
549 int msglen;
551 u_char *outp;
552 int outlen;
554 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
555 outp = outpacket_buf;
556 MAKEHEADER(outp, PPP_PAP);
558 PUTCHAR(code, outp);
559 PUTCHAR(id, outp);
560 PUTSHORT(outlen, outp);
561 PUTCHAR(msglen, outp);
562 BCOPY(msg, outp, msglen);
563 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
567 * upap_printpkt - print the contents of a PAP packet.
569 static char *upap_codenames[] = {
570 "AuthReq", "AuthAck", "AuthNak"
573 static int
574 upap_printpkt(p, plen, printer, arg)
575 u_char *p;
576 int plen;
577 void (*printer) __P((void *, char *, ...));
578 void *arg;
580 int code, id, len;
581 int mlen, ulen, wlen;
582 char *user, *pwd, *msg;
583 u_char *pstart;
585 if (plen < UPAP_HEADERLEN)
586 return 0;
587 pstart = p;
588 GETCHAR(code, p);
589 GETCHAR(id, p);
590 GETSHORT(len, p);
591 if (len < UPAP_HEADERLEN || len > plen)
592 return 0;
594 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
595 printer(arg, " %s", upap_codenames[code-1]);
596 else
597 printer(arg, " code=0x%x", code);
598 printer(arg, " id=0x%x", id);
599 len -= UPAP_HEADERLEN;
600 switch (code) {
601 case UPAP_AUTHREQ:
602 if (len < 1)
603 break;
604 ulen = p[0];
605 if (len < ulen + 2)
606 break;
607 wlen = p[ulen + 1];
608 if (len < ulen + wlen + 2)
609 break;
610 user = (char *) (p + 1);
611 pwd = (char *) (p + ulen + 2);
612 p += ulen + wlen + 2;
613 len -= ulen + wlen + 2;
614 printer(arg, " user=");
615 print_string(user, ulen, printer, arg);
616 printer(arg, " password=");
617 if (!hide_password)
618 print_string(pwd, wlen, printer, arg);
619 else
620 printer(arg, "<hidden>");
621 break;
622 case UPAP_AUTHACK:
623 case UPAP_AUTHNAK:
624 if (len < 1)
625 break;
626 mlen = p[0];
627 if (len < mlen + 1)
628 break;
629 msg = (char *) (p + 1);
630 p += mlen + 1;
631 len -= mlen + 1;
632 printer(arg, " ");
633 print_string(msg, mlen, printer, arg);
634 break;
637 /* print the rest of the bytes in the packet */
638 for (; len > 0; --len) {
639 GETCHAR(code, p);
640 printer(arg, " %.2x", code);
643 return p - pstart;