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
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
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
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
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.29 2002/12/04 23:03:33 paulus Exp $"
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
},
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
= {
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.
127 upap_state
*u
= &upap
[unit
];
134 u
->us_clientstate
= UPAPCS_INITIAL
;
135 u
->us_serverstate
= UPAPSS_INITIAL
;
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.
149 upap_authwithpeer(unit
, user
, password
)
151 char *user
, *password
;
153 upap_state
*u
= &upap
[unit
];
155 /* Save the username and password we're given */
157 u
->us_userlen
= strlen(user
);
158 u
->us_passwd
= password
;
159 u
->us_passwdlen
= strlen(password
);
162 /* Lower layer up yet? */
163 if (u
->us_clientstate
== UPAPCS_INITIAL
||
164 u
->us_clientstate
== UPAPCS_PENDING
) {
165 u
->us_clientstate
= UPAPCS_PENDING
;
169 upap_sauthreq(u
); /* Start protocol */
174 * upap_authpeer - Authenticate our peer (start server).
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
;
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.
204 upap_state
*u
= (upap_state
*) arg
;
206 if (u
->us_clientstate
!= UPAPCS_AUTHREQ
)
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
);
217 upap_sauthreq(u
); /* Send Authenticate-Request */
222 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
228 upap_state
*u
= (upap_state
*) arg
;
230 if (u
->us_serverstate
!= UPAPSS_LISTEN
)
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.
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.
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.
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.
313 upap_input(unit
, inpacket
, l
)
318 upap_state
*u
= &upap
[unit
];
324 * Parse header (code, id and length).
325 * If packet too short, drop it.
328 if (l
< UPAP_HEADERLEN
) {
329 UPAPDEBUG(("pap_input: rcvd short header."));
335 if (len
< UPAP_HEADERLEN
) {
336 UPAPDEBUG(("pap_input: rcvd illegal length."));
340 UPAPDEBUG(("pap_input: rcvd short packet."));
343 len
-= UPAP_HEADERLEN
;
346 * Action depends on code.
350 upap_rauthreq(u
, inp
, id
, len
);
354 upap_rauthack(u
, inp
, id
, len
);
358 upap_rauthnak(u
, inp
, id
, len
);
361 default: /* XXX Need code reject */
368 * upap_rauth - Receive Authenticate.
371 upap_rauthreq(u
, inp
, id
, len
)
377 u_char ruserlen
, rpasswdlen
;
378 char *ruser
, *rpasswd
;
384 if (u
->us_serverstate
< UPAPSS_LISTEN
)
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 */
395 if (u
->us_serverstate
== UPAPSS_BADAUTH
) {
396 upap_sresp(u
, UPAP_AUTHNAK
, id
, "", 0); /* return auth-nak */
404 UPAPDEBUG(("pap_rauth: rcvd short packet."));
407 GETCHAR(ruserlen
, inp
);
408 len
-= sizeof (u_char
) + ruserlen
+ sizeof (u_char
);
410 UPAPDEBUG(("pap_rauth: rcvd short packet."));
413 ruser
= (char *) inp
;
414 INCPTR(ruserlen
, inp
);
415 GETCHAR(rpasswdlen
, inp
);
416 if (len
< rpasswdlen
) {
417 UPAPDEBUG(("pap_rauth: rcvd short packet."));
420 rpasswd
= (char *) inp
;
423 * Check the username and password given.
425 retcode
= check_passwd(u
->us_unit
, ruser
, ruserlen
, rpasswd
,
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
);
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
);
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.
469 upap_rauthack(u
, inp
, id
, len
)
478 if (u
->us_clientstate
!= UPAPCS_AUTHREQ
) /* XXX */
485 UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
487 GETCHAR(msglen
, inp
);
489 len
-= sizeof (u_char
);
491 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
495 PRINTMSG(msg
, msglen
);
499 u
->us_clientstate
= UPAPCS_OPEN
;
501 notice("PAP authentication succeeded");
502 auth_withpeer_success(u
->us_unit
, PPP_PAP
, 0);
507 * upap_rauthnak - Receive Authenticate-Nak.
510 upap_rauthnak(u
, inp
, id
, len
)
519 if (u
->us_clientstate
!= UPAPCS_AUTHREQ
) /* XXX */
526 UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
528 GETCHAR(msglen
, inp
);
530 len
-= sizeof (u_char
);
532 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
536 PRINTMSG(msg
, msglen
);
540 u
->us_clientstate
= UPAPCS_BADAUTH
;
542 error("PAP authentication failed");
543 auth_withpeer_fail(u
->us_unit
, PPP_PAP
);
548 * upap_sauthreq - Send an Authenticate-Request.
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
);
576 u
->us_clientstate
= UPAPCS_AUTHREQ
;
581 * upap_sresp - Send a response (ack or nak).
584 upap_sresp(u
, code
, id
, msg
, msglen
)
593 outlen
= UPAP_HEADERLEN
+ sizeof (u_char
) + msglen
;
594 outp
= outpacket_buf
;
595 MAKEHEADER(outp
, PPP_PAP
);
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"
613 upap_printpkt(p
, plen
, printer
, arg
)
616 void (*printer
) __P((void *, char *, ...));
620 int mlen
, ulen
, wlen
;
621 char *user
, *pwd
, *msg
;
624 if (plen
< UPAP_HEADERLEN
)
630 if (len
< UPAP_HEADERLEN
|| len
> plen
)
633 if (code
>= 1 && code
<= sizeof(upap_codenames
) / sizeof(char *))
634 printer(arg
, " %s", upap_codenames
[code
-1]);
636 printer(arg
, " code=0x%x", code
);
637 printer(arg
, " id=0x%x", id
);
638 len
-= UPAP_HEADERLEN
;
647 if (len
< ulen
+ wlen
+ 2)
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=");
657 print_string(pwd
, wlen
, printer
, arg
);
659 printer(arg
, "<hidden>");
668 msg
= (char *) (p
+ 1);
672 print_string(msg
, mlen
, printer
, arg
);
676 /* print the rest of the bytes in the packet */
677 for (; len
> 0; --len
) {
679 printer(arg
, " %.2x", code
);