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.
54 static bool hide_password = 1;
57 * Command-line options.
59 static option_t pap_option_list[] = {
60 { "hide-password", o_bool, &hide_password,
61 "Don't output passwords to log", OPT_PRIO | 1 },
62 { "show-password", o_bool, &hide_password,
63 "Show password string in debug log messages", OPT_PRIOSUB | 0 },
65 { "pap-restart", o_int, &upap[0].us_timeouttime,
66 "Set retransmit timeout for PAP", OPT_PRIO },
67 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
68 "Set max number of transmissions for auth-reqs", OPT_PRIO },
69 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
70 "Set time limit for peer PAP authentication", OPT_PRIO },
76 * Protocol entry points.
78 static void upap_init(int);
79 static void upap_lowerup(int);
80 static void upap_lowerdown(int);
81 static void upap_input(int, u_char *, int);
82 static void upap_protrej(int);
83 static int upap_printpkt(u_char *, int,
84 void (*)(void *, char *, ...), void *);
86 struct protent pap_protent = {
106 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
108 static void upap_timeout(void *);
109 static void upap_reqtimeout(void *);
110 static void upap_rauthreq(upap_state *, u_char *, int, int);
111 static void upap_rauthack(upap_state *, u_char *, int, int);
112 static void upap_rauthnak(upap_state *, u_char *, int, int);
113 static void upap_sauthreq(upap_state *);
114 static void upap_sresp(upap_state *, int, int, char *, int);
118 * upap_init - Initialize a UPAP unit.
123 upap_state *u = &upap[unit];
130 u->us_clientstate = UPAPCS_INITIAL;
131 u->us_serverstate = UPAPSS_INITIAL;
133 u->us_timeouttime = UPAP_DEFTIMEOUT;
134 u->us_maxtransmits = 10;
135 u->us_reqtimeout = UPAP_DEFREQTIME;
140 * upap_authwithpeer - Authenticate us with our peer (start client).
142 * Set new state and send authenticate's.
145 upap_authwithpeer(int unit, char *user, char *password)
147 upap_state *u = &upap[unit];
149 /* Save the username and password we're given */
151 u->us_userlen = strlen(user);
152 u->us_passwd = password;
153 u->us_passwdlen = strlen(password);
156 /* Lower layer up yet? */
157 if (u->us_clientstate == UPAPCS_INITIAL ||
158 u->us_clientstate == UPAPCS_PENDING) {
159 u->us_clientstate = UPAPCS_PENDING;
163 upap_sauthreq(u); /* Start protocol */
168 * upap_authpeer - Authenticate our peer (start server).
173 upap_authpeer(int unit)
175 upap_state *u = &upap[unit];
177 /* Lower layer up yet? */
178 if (u->us_serverstate == UPAPSS_INITIAL ||
179 u->us_serverstate == UPAPSS_PENDING) {
180 u->us_serverstate = UPAPSS_PENDING;
184 u->us_serverstate = UPAPSS_LISTEN;
185 if (u->us_reqtimeout > 0)
186 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
191 * upap_timeout - Retransmission timer for sending auth-reqs expired.
194 upap_timeout(void *arg)
196 upap_state *u = (upap_state *) arg;
198 if (u->us_clientstate != UPAPCS_AUTHREQ)
201 if (u->us_transmits >= u->us_maxtransmits) {
202 /* give up in disgust */
203 error("No response to PAP authenticate-requests");
204 u->us_clientstate = UPAPCS_BADAUTH;
205 auth_withpeer_fail(u->us_unit, PPP_PAP);
209 upap_sauthreq(u); /* Send Authenticate-Request */
214 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
217 upap_reqtimeout(void *arg)
219 upap_state *u = (upap_state *) arg;
221 if (u->us_serverstate != UPAPSS_LISTEN)
224 auth_peer_fail(u->us_unit, PPP_PAP);
225 u->us_serverstate = UPAPSS_BADAUTH;
230 * upap_lowerup - The lower layer is up.
232 * Start authenticating if pending.
235 upap_lowerup(int unit)
237 upap_state *u = &upap[unit];
239 if (u->us_clientstate == UPAPCS_INITIAL)
240 u->us_clientstate = UPAPCS_CLOSED;
241 else if (u->us_clientstate == UPAPCS_PENDING) {
242 upap_sauthreq(u); /* send an auth-request */
245 if (u->us_serverstate == UPAPSS_INITIAL)
246 u->us_serverstate = UPAPSS_CLOSED;
247 else if (u->us_serverstate == UPAPSS_PENDING) {
248 u->us_serverstate = UPAPSS_LISTEN;
249 if (u->us_reqtimeout > 0)
250 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
256 * upap_lowerdown - The lower layer is down.
258 * Cancel all timeouts.
261 upap_lowerdown(int unit)
263 upap_state *u = &upap[unit];
265 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
266 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
267 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
268 UNTIMEOUT(upap_reqtimeout, u);
270 u->us_clientstate = UPAPCS_INITIAL;
271 u->us_serverstate = UPAPSS_INITIAL;
276 * upap_protrej - Peer doesn't speak this protocol.
278 * This shouldn't happen. In any case, pretend lower layer went down.
281 upap_protrej(int unit)
283 upap_state *u = &upap[unit];
285 if (u->us_clientstate == UPAPCS_AUTHREQ) {
286 error("PAP authentication failed due to protocol-reject");
287 auth_withpeer_fail(unit, PPP_PAP);
289 if (u->us_serverstate == UPAPSS_LISTEN) {
290 error("PAP authentication of peer failed (protocol-reject)");
291 auth_peer_fail(unit, PPP_PAP);
293 upap_lowerdown(unit);
298 * upap_input - Input UPAP packet.
301 upap_input(int unit, u_char *inpacket, int l)
303 upap_state *u = &upap[unit];
309 * Parse header (code, id and length).
310 * If packet too short, drop it.
313 if (l < UPAP_HEADERLEN) {
314 UPAPDEBUG(("pap_input: rcvd short header."));
320 if (len < UPAP_HEADERLEN) {
321 UPAPDEBUG(("pap_input: rcvd illegal length."));
325 UPAPDEBUG(("pap_input: rcvd short packet."));
328 len -= UPAP_HEADERLEN;
331 * Action depends on code.
335 upap_rauthreq(u, inp, id, len);
339 upap_rauthack(u, inp, id, len);
343 upap_rauthnak(u, inp, id, len);
346 default: /* XXX Need code reject */
353 * upap_rauth - Receive Authenticate.
356 upap_rauthreq(upap_state *u, u_char *inp, int id, int len)
358 u_char ruserlen, rpasswdlen;
359 char *ruser, *rpasswd;
365 if (u->us_serverstate < UPAPSS_LISTEN)
369 * If we receive a duplicate authenticate-request, we are
370 * supposed to return the same status as for the first request.
372 if (u->us_serverstate == UPAPSS_OPEN) {
373 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
376 if (u->us_serverstate == UPAPSS_BADAUTH) {
377 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
385 UPAPDEBUG(("pap_rauth: rcvd short packet."));
388 GETCHAR(ruserlen, inp);
389 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
391 UPAPDEBUG(("pap_rauth: rcvd short packet."));
394 ruser = (char *) inp;
395 INCPTR(ruserlen, inp);
396 GETCHAR(rpasswdlen, inp);
397 if (len < rpasswdlen) {
398 UPAPDEBUG(("pap_rauth: rcvd short packet."));
401 rpasswd = (char *) inp;
404 * Check the username and password given.
406 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
408 BZERO(rpasswd, rpasswdlen);
411 * Check remote number authorization. A plugin may have filled in
412 * the remote number or added an allowed number, and rather than
413 * return an authenticate failure, is leaving it for us to verify.
415 if (retcode == UPAP_AUTHACK) {
416 if (!auth_number()) {
417 /* We do not want to leak info about the pap result. */
418 retcode = UPAP_AUTHNAK; /* XXX exit value will be "wrong" */
419 warn("calling number %q is not authorized", remote_number);
423 msglen = strlen(msg);
426 upap_sresp(u, retcode, id, msg, msglen);
428 /* Null terminate and clean remote name. */
429 slprintf(rhostname, sizeof(rhostname), "%.*v", ruserlen, ruser);
431 if (retcode == UPAP_AUTHACK) {
432 u->us_serverstate = UPAPSS_OPEN;
433 notice("PAP peer authentication succeeded for %q", rhostname);
434 auth_peer_success(u->us_unit, PPP_PAP, 0, ruser, ruserlen);
436 u->us_serverstate = UPAPSS_BADAUTH;
437 warn("PAP peer authentication failed for %q", rhostname);
438 auth_peer_fail(u->us_unit, PPP_PAP);
441 if (u->us_reqtimeout > 0)
442 UNTIMEOUT(upap_reqtimeout, u);
447 * upap_rauthack - Receive Authenticate-Ack.
450 upap_rauthack(upap_state *u, u_char *inp, int id, int len)
455 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
462 UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
464 GETCHAR(msglen, inp);
466 len -= sizeof (u_char);
468 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
472 PRINTMSG(msg, msglen);
476 u->us_clientstate = UPAPCS_OPEN;
478 auth_withpeer_success(u->us_unit, PPP_PAP, 0);
483 * upap_rauthnak - Receive Authenticate-Nak.
486 upap_rauthnak(upap_state *u, u_char *inp, int id, int len)
491 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
498 UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
500 GETCHAR(msglen, inp);
502 len -= sizeof (u_char);
504 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
508 PRINTMSG(msg, msglen);
512 u->us_clientstate = UPAPCS_BADAUTH;
514 error("PAP authentication failed");
515 auth_withpeer_fail(u->us_unit, PPP_PAP);
520 * upap_sauthreq - Send an Authenticate-Request.
523 upap_sauthreq(upap_state *u)
528 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
529 u->us_userlen + u->us_passwdlen;
530 outp = outpacket_buf;
532 MAKEHEADER(outp, PPP_PAP);
534 PUTCHAR(UPAP_AUTHREQ, outp);
535 PUTCHAR(++u->us_id, outp);
536 PUTSHORT(outlen, outp);
537 PUTCHAR(u->us_userlen, outp);
538 BCOPY(u->us_user, outp, u->us_userlen);
539 INCPTR(u->us_userlen, outp);
540 PUTCHAR(u->us_passwdlen, outp);
541 BCOPY(u->us_passwd, outp, u->us_passwdlen);
543 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
545 TIMEOUT(upap_timeout, u, u->us_timeouttime);
547 u->us_clientstate = UPAPCS_AUTHREQ;
552 * upap_sresp - Send a response (ack or nak).
555 upap_sresp(upap_state *u, int code, int id, char *msg, int msglen)
560 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
561 outp = outpacket_buf;
562 MAKEHEADER(outp, PPP_PAP);
566 PUTSHORT(outlen, outp);
567 PUTCHAR(msglen, outp);
568 BCOPY(msg, outp, msglen);
569 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
573 * upap_printpkt - print the contents of a PAP packet.
575 static char *upap_codenames[] = {
576 "AuthReq", "AuthAck", "AuthNak"
580 upap_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...), void *arg)
583 int mlen, ulen, wlen;
584 char *user, *pwd, *msg;
587 if (plen < UPAP_HEADERLEN)
593 if (len < UPAP_HEADERLEN || len > plen)
596 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
597 printer(arg, " %s", upap_codenames[code-1]);
599 printer(arg, " code=0x%x", code);
600 printer(arg, " id=0x%x", id);
601 len -= UPAP_HEADERLEN;
610 if (len < ulen + wlen + 2)
612 user = (char *) (p + 1);
613 pwd = (char *) (p + ulen + 2);
614 p += ulen + wlen + 2;
615 len -= ulen + wlen + 2;
616 printer(arg, " user=");
617 print_string(user, ulen, printer, arg);
618 printer(arg, " password=");
620 print_string(pwd, wlen, printer, arg);
622 printer(arg, "<hidden>");
631 msg = (char *) (p + 1);
635 print_string(msg, mlen, printer, arg);
639 /* print the rest of the bytes in the packet */
640 for (; len > 0; --len) {
642 printer(arg, " %.2x", code);