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.
58 static bool hide_password = 1;
61 * Command-line options.
63 static option_t pap_option_list[] = {
64 { "hide-password", o_bool, &hide_password,
65 "Don't output passwords to log", OPT_PRIO | 1 },
66 { "show-password", o_bool, &hide_password,
67 "Show password string in debug log messages", OPT_PRIOSUB | 0 },
69 { "pap-restart", o_int, &upap[0].us_timeouttime,
70 "Set retransmit timeout for PAP", OPT_PRIO },
71 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
72 "Set max number of transmissions for auth-reqs", OPT_PRIO },
73 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
74 "Set time limit for peer PAP authentication", OPT_PRIO },
80 * Protocol entry points.
82 static void upap_init(int);
83 static void upap_lowerup(int);
84 static void upap_lowerdown(int);
85 static void upap_input(int, u_char *, int);
86 static void upap_protrej(int);
87 static int upap_printpkt(u_char *, int,
88 void (*)(void *, char *, ...), void *);
90 struct protent pap_protent = {
110 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
112 static void upap_timeout(void *);
113 static void upap_reqtimeout(void *);
114 static void upap_rauthreq(upap_state *, u_char *, int, int);
115 static void upap_rauthack(upap_state *, u_char *, int, int);
116 static void upap_rauthnak(upap_state *, u_char *, int, int);
117 static void upap_sauthreq(upap_state *);
118 static void upap_sresp(upap_state *, int, int, char *, int);
122 * 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(int unit, char *user, char *password)
151 upap_state *u = &upap[unit];
153 /* Save the username and password we're given */
155 u->us_userlen = strlen(user);
156 u->us_passwd = password;
157 u->us_passwdlen = strlen(password);
160 /* Lower layer up yet? */
161 if (u->us_clientstate == UPAPCS_INITIAL ||
162 u->us_clientstate == UPAPCS_PENDING) {
163 u->us_clientstate = UPAPCS_PENDING;
167 upap_sauthreq(u); /* Start protocol */
172 * upap_authpeer - Authenticate our peer (start server).
177 upap_authpeer(int unit)
179 upap_state *u = &upap[unit];
181 /* Lower layer up yet? */
182 if (u->us_serverstate == UPAPSS_INITIAL ||
183 u->us_serverstate == UPAPSS_PENDING) {
184 u->us_serverstate = UPAPSS_PENDING;
188 u->us_serverstate = UPAPSS_LISTEN;
189 if (u->us_reqtimeout > 0)
190 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
195 * upap_timeout - Retransmission timer for sending auth-reqs expired.
198 upap_timeout(void *arg)
200 upap_state *u = (upap_state *) arg;
202 if (u->us_clientstate != UPAPCS_AUTHREQ)
205 if (u->us_transmits >= u->us_maxtransmits) {
206 /* give up in disgust */
207 error("No response to PAP authenticate-requests");
208 u->us_clientstate = UPAPCS_BADAUTH;
209 auth_withpeer_fail(u->us_unit, PPP_PAP);
213 upap_sauthreq(u); /* Send Authenticate-Request */
218 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
221 upap_reqtimeout(void *arg)
223 upap_state *u = (upap_state *) arg;
225 if (u->us_serverstate != UPAPSS_LISTEN)
228 auth_peer_fail(u->us_unit, PPP_PAP);
229 u->us_serverstate = UPAPSS_BADAUTH;
234 * upap_lowerup - The lower layer is up.
236 * Start authenticating if pending.
239 upap_lowerup(int unit)
241 upap_state *u = &upap[unit];
243 if (u->us_clientstate == UPAPCS_INITIAL)
244 u->us_clientstate = UPAPCS_CLOSED;
245 else if (u->us_clientstate == UPAPCS_PENDING) {
246 upap_sauthreq(u); /* send an auth-request */
249 if (u->us_serverstate == UPAPSS_INITIAL)
250 u->us_serverstate = UPAPSS_CLOSED;
251 else if (u->us_serverstate == UPAPSS_PENDING) {
252 u->us_serverstate = UPAPSS_LISTEN;
253 if (u->us_reqtimeout > 0)
254 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
260 * upap_lowerdown - The lower layer is down.
262 * Cancel all timeouts.
265 upap_lowerdown(int unit)
267 upap_state *u = &upap[unit];
269 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
270 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
271 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
272 UNTIMEOUT(upap_reqtimeout, u);
274 u->us_clientstate = UPAPCS_INITIAL;
275 u->us_serverstate = UPAPSS_INITIAL;
280 * upap_protrej - Peer doesn't speak this protocol.
282 * This shouldn't happen. In any case, pretend lower layer went down.
285 upap_protrej(int unit)
287 upap_state *u = &upap[unit];
289 if (u->us_clientstate == UPAPCS_AUTHREQ) {
290 error("PAP authentication failed due to protocol-reject");
291 auth_withpeer_fail(unit, PPP_PAP);
293 if (u->us_serverstate == UPAPSS_LISTEN) {
294 error("PAP authentication of peer failed (protocol-reject)");
295 auth_peer_fail(unit, PPP_PAP);
297 upap_lowerdown(unit);
302 * upap_input - Input UPAP packet.
305 upap_input(int unit, u_char *inpacket, int l)
307 upap_state *u = &upap[unit];
313 * Parse header (code, id and length).
314 * If packet too short, drop it.
317 if (l < UPAP_HEADERLEN) {
318 UPAPDEBUG(("pap_input: rcvd short header."));
324 if (len < UPAP_HEADERLEN) {
325 UPAPDEBUG(("pap_input: rcvd illegal length."));
329 UPAPDEBUG(("pap_input: rcvd short packet."));
332 len -= UPAP_HEADERLEN;
335 * Action depends on code.
339 upap_rauthreq(u, inp, id, len);
343 upap_rauthack(u, inp, id, len);
347 upap_rauthnak(u, inp, id, len);
350 default: /* XXX Need code reject */
357 * upap_rauth - Receive Authenticate.
360 upap_rauthreq(upap_state *u, u_char *inp, int id, int len)
362 u_char ruserlen, rpasswdlen;
363 char *ruser, *rpasswd;
369 if (u->us_serverstate < UPAPSS_LISTEN)
373 * If we receive a duplicate authenticate-request, we are
374 * supposed to return the same status as for the first request.
376 if (u->us_serverstate == UPAPSS_OPEN) {
377 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
380 if (u->us_serverstate == UPAPSS_BADAUTH) {
381 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
389 UPAPDEBUG(("pap_rauth: rcvd short packet."));
392 GETCHAR(ruserlen, inp);
393 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
395 UPAPDEBUG(("pap_rauth: rcvd short packet."));
398 ruser = (char *) inp;
399 INCPTR(ruserlen, inp);
400 GETCHAR(rpasswdlen, inp);
401 if (len < rpasswdlen) {
402 UPAPDEBUG(("pap_rauth: rcvd short packet."));
405 rpasswd = (char *) inp;
408 * Check the username and password given.
410 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
412 BZERO(rpasswd, rpasswdlen);
415 * Check remote number authorization. A plugin may have filled in
416 * the remote number or added an allowed number, and rather than
417 * return an authenticate failure, is leaving it for us to verify.
419 if (retcode == UPAP_AUTHACK) {
420 if (!auth_number()) {
421 /* We do not want to leak info about the pap result. */
422 retcode = UPAP_AUTHNAK; /* XXX exit value will be "wrong" */
423 warn("calling number %q is not authorized", remote_number);
427 msglen = strlen(msg);
430 upap_sresp(u, retcode, id, msg, msglen);
432 /* Null terminate and clean remote name. */
433 slprintf(rhostname, sizeof(rhostname), "%.*v", ruserlen, ruser);
435 if (retcode == UPAP_AUTHACK) {
436 u->us_serverstate = UPAPSS_OPEN;
437 notice("PAP peer authentication succeeded for %q", rhostname);
438 auth_peer_success(u->us_unit, PPP_PAP, 0, ruser, ruserlen);
440 u->us_serverstate = UPAPSS_BADAUTH;
441 warn("PAP peer authentication failed for %q", rhostname);
442 auth_peer_fail(u->us_unit, PPP_PAP);
445 if (u->us_reqtimeout > 0)
446 UNTIMEOUT(upap_reqtimeout, u);
451 * upap_rauthack - Receive Authenticate-Ack.
454 upap_rauthack(upap_state *u, u_char *inp, int id, int len)
459 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
466 UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
468 GETCHAR(msglen, inp);
470 len -= sizeof (u_char);
472 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
476 PRINTMSG(msg, msglen);
480 u->us_clientstate = UPAPCS_OPEN;
482 auth_withpeer_success(u->us_unit, PPP_PAP, 0);
487 * upap_rauthnak - Receive Authenticate-Nak.
490 upap_rauthnak(upap_state *u, u_char *inp, int id, int len)
495 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
502 UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
504 GETCHAR(msglen, inp);
506 len -= sizeof (u_char);
508 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
512 PRINTMSG(msg, msglen);
516 u->us_clientstate = UPAPCS_BADAUTH;
518 error("PAP authentication failed");
519 auth_withpeer_fail(u->us_unit, PPP_PAP);
524 * upap_sauthreq - Send an Authenticate-Request.
527 upap_sauthreq(upap_state *u)
532 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
533 u->us_userlen + u->us_passwdlen;
534 outp = outpacket_buf;
536 MAKEHEADER(outp, PPP_PAP);
538 PUTCHAR(UPAP_AUTHREQ, outp);
539 PUTCHAR(++u->us_id, outp);
540 PUTSHORT(outlen, outp);
541 PUTCHAR(u->us_userlen, outp);
542 BCOPY(u->us_user, outp, u->us_userlen);
543 INCPTR(u->us_userlen, outp);
544 PUTCHAR(u->us_passwdlen, outp);
545 BCOPY(u->us_passwd, outp, u->us_passwdlen);
547 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
549 TIMEOUT(upap_timeout, u, u->us_timeouttime);
551 u->us_clientstate = UPAPCS_AUTHREQ;
556 * upap_sresp - Send a response (ack or nak).
559 upap_sresp(upap_state *u, int code, int id, char *msg, int msglen)
564 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
565 outp = outpacket_buf;
566 MAKEHEADER(outp, PPP_PAP);
570 PUTSHORT(outlen, outp);
571 PUTCHAR(msglen, outp);
572 BCOPY(msg, outp, msglen);
573 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
577 * upap_printpkt - print the contents of a PAP packet.
579 static char *upap_codenames[] = {
580 "AuthReq", "AuthAck", "AuthNak"
584 upap_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...), void *arg)
587 int mlen, ulen, wlen;
588 char *user, *pwd, *msg;
591 if (plen < UPAP_HEADERLEN)
597 if (len < UPAP_HEADERLEN || len > plen)
600 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
601 printer(arg, " %s", upap_codenames[code-1]);
603 printer(arg, " code=0x%x", code);
604 printer(arg, " id=0x%x", id);
605 len -= UPAP_HEADERLEN;
614 if (len < ulen + wlen + 2)
616 user = (char *) (p + 1);
617 pwd = (char *) (p + ulen + 2);
618 p += ulen + wlen + 2;
619 len -= ulen + wlen + 2;
620 printer(arg, " user=");
621 print_string(user, ulen, printer, arg);
622 printer(arg, " password=");
624 print_string(pwd, wlen, printer, arg);
626 printer(arg, "<hidden>");
635 msg = (char *) (p + 1);
639 print_string(msg, mlen, printer, arg);
643 /* print the rest of the bytes in the packet */
644 for (; len > 0; --len) {
646 printer(arg, " %.2x", code);