2 * upap.c - User/Password Authentication Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
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.
19 * $FreeBSD: src/usr.sbin/pppd/upap.c,v 1.8 1999/08/28 01:19:08 peter Exp $
20 * $DragonFly: src/usr.sbin/pppd/upap.c,v 1.3 2003/11/03 19:31:40 eirikn Exp $
29 #include <sys/types.h>
37 * Protocol entry points.
39 static void upap_init(int);
40 static void upap_lowerup(int);
41 static void upap_lowerdown(int);
42 static void upap_input(int, u_char *, int);
43 static void upap_protrej(int);
44 static int upap_printpkt(u_char *, int,
45 void (*)(void *, char *, ...), void *);
47 struct protent pap_protent = {
65 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
67 static void upap_timeout(void *);
68 static void upap_reqtimeout(void *);
69 static void upap_rauthreq(upap_state *, u_char *, int, int);
70 static void upap_rauthack(upap_state *, u_char *, int, int);
71 static void upap_rauthnak(upap_state *, u_char *, int, int);
72 static void upap_sauthreq(upap_state *);
73 static void upap_sresp(upap_state *, int, int, char *, int);
77 * upap_init - Initialize a UPAP unit.
83 upap_state *u = &upap[unit];
90 u->us_clientstate = UPAPCS_INITIAL;
91 u->us_serverstate = UPAPSS_INITIAL;
93 u->us_timeouttime = UPAP_DEFTIMEOUT;
94 u->us_maxtransmits = 10;
95 u->us_reqtimeout = UPAP_DEFREQTIME;
100 * upap_authwithpeer - Authenticate us with our peer (start client).
102 * Set new state and send authenticate's.
105 upap_authwithpeer(unit, user, password)
107 char *user, *password;
109 upap_state *u = &upap[unit];
111 /* Save the username and password we're given */
113 u->us_userlen = strlen(user);
114 u->us_passwd = password;
115 u->us_passwdlen = strlen(password);
118 /* Lower layer up yet? */
119 if (u->us_clientstate == UPAPCS_INITIAL ||
120 u->us_clientstate == UPAPCS_PENDING) {
121 u->us_clientstate = UPAPCS_PENDING;
125 upap_sauthreq(u); /* Start protocol */
130 * upap_authpeer - Authenticate our peer (start server).
138 upap_state *u = &upap[unit];
140 /* Lower layer up yet? */
141 if (u->us_serverstate == UPAPSS_INITIAL ||
142 u->us_serverstate == UPAPSS_PENDING) {
143 u->us_serverstate = UPAPSS_PENDING;
147 u->us_serverstate = UPAPSS_LISTEN;
148 if (u->us_reqtimeout > 0)
149 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
154 * upap_timeout - Retransmission timer for sending auth-reqs expired.
160 upap_state *u = (upap_state *) arg;
162 if (u->us_clientstate != UPAPCS_AUTHREQ)
165 if (u->us_transmits >= u->us_maxtransmits) {
166 /* give up in disgust */
167 syslog(LOG_ERR, "No response to PAP authenticate-requests");
168 u->us_clientstate = UPAPCS_BADAUTH;
169 auth_withpeer_fail(u->us_unit, PPP_PAP);
173 upap_sauthreq(u); /* Send Authenticate-Request */
178 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
184 upap_state *u = (upap_state *) arg;
186 if (u->us_serverstate != UPAPSS_LISTEN)
189 auth_peer_fail(u->us_unit, PPP_PAP);
190 u->us_serverstate = UPAPSS_BADAUTH;
195 * upap_lowerup - The lower layer is up.
197 * Start authenticating if pending.
203 upap_state *u = &upap[unit];
205 if (u->us_clientstate == UPAPCS_INITIAL)
206 u->us_clientstate = UPAPCS_CLOSED;
207 else if (u->us_clientstate == UPAPCS_PENDING) {
208 upap_sauthreq(u); /* send an auth-request */
211 if (u->us_serverstate == UPAPSS_INITIAL)
212 u->us_serverstate = UPAPSS_CLOSED;
213 else if (u->us_serverstate == UPAPSS_PENDING) {
214 u->us_serverstate = UPAPSS_LISTEN;
215 if (u->us_reqtimeout > 0)
216 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
222 * upap_lowerdown - The lower layer is down.
224 * Cancel all timeouts.
230 upap_state *u = &upap[unit];
232 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
233 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
234 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
235 UNTIMEOUT(upap_reqtimeout, u);
237 u->us_clientstate = UPAPCS_INITIAL;
238 u->us_serverstate = UPAPSS_INITIAL;
243 * upap_protrej - Peer doesn't speak this protocol.
245 * This shouldn't happen. In any case, pretend lower layer went down.
251 upap_state *u = &upap[unit];
253 if (u->us_clientstate == UPAPCS_AUTHREQ) {
254 syslog(LOG_ERR, "PAP authentication failed due to protocol-reject");
255 auth_withpeer_fail(unit, PPP_PAP);
257 if (u->us_serverstate == UPAPSS_LISTEN) {
258 syslog(LOG_ERR, "PAP authentication of peer failed (protocol-reject)");
259 auth_peer_fail(unit, PPP_PAP);
261 upap_lowerdown(unit);
266 * upap_input - Input UPAP packet.
269 upap_input(unit, inpacket, l)
274 upap_state *u = &upap[unit];
280 * Parse header (code, id and length).
281 * If packet too short, drop it.
284 if (l < UPAP_HEADERLEN) {
285 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short header."));
291 if (len < UPAP_HEADERLEN) {
292 UPAPDEBUG((LOG_INFO, "pap_input: rcvd illegal length."));
296 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short packet."));
299 len -= UPAP_HEADERLEN;
302 * Action depends on code.
306 upap_rauthreq(u, inp, id, len);
310 upap_rauthack(u, inp, id, len);
314 upap_rauthnak(u, inp, id, len);
317 default: /* XXX Need code reject */
324 * upap_rauth - Receive Authenticate.
327 upap_rauthreq(u, inp, id, len)
333 u_char ruserlen, rpasswdlen;
334 char *ruser, *rpasswd;
339 UPAPDEBUG((LOG_INFO, "pap_rauth: Rcvd id %d.", id));
341 if (u->us_serverstate < UPAPSS_LISTEN)
345 * If we receive a duplicate authenticate-request, we are
346 * supposed to return the same status as for the first request.
348 if (u->us_serverstate == UPAPSS_OPEN) {
349 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
352 if (u->us_serverstate == UPAPSS_BADAUTH) {
353 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
360 if (len < sizeof (u_char)) {
361 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
364 GETCHAR(ruserlen, inp);
365 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
367 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
370 ruser = (char *) inp;
371 INCPTR(ruserlen, inp);
372 GETCHAR(rpasswdlen, inp);
373 if (len < rpasswdlen) {
374 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
377 rpasswd = (char *) inp;
380 * Check the username and password given.
382 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
383 rpasswdlen, &msg, &msglen);
384 BZERO(rpasswd, rpasswdlen);
386 upap_sresp(u, retcode, id, msg, msglen);
388 if (retcode == UPAP_AUTHACK) {
389 u->us_serverstate = UPAPSS_OPEN;
390 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
392 u->us_serverstate = UPAPSS_BADAUTH;
393 auth_peer_fail(u->us_unit, PPP_PAP);
396 if (u->us_reqtimeout > 0)
397 UNTIMEOUT(upap_reqtimeout, u);
402 * upap_rauthack - Receive Authenticate-Ack.
405 upap_rauthack(u, inp, id, len)
414 UPAPDEBUG((LOG_INFO, "pap_rauthack: Rcvd id %d.", id));
415 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
421 if (len < sizeof (u_char)) {
422 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
425 GETCHAR(msglen, inp);
426 len -= sizeof (u_char);
428 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
432 PRINTMSG(msg, msglen);
434 u->us_clientstate = UPAPCS_OPEN;
436 auth_withpeer_success(u->us_unit, PPP_PAP);
441 * upap_rauthnak - Receive Authenticate-Nakk.
444 upap_rauthnak(u, inp, id, len)
453 UPAPDEBUG((LOG_INFO, "pap_rauthnak: Rcvd id %d.", id));
454 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
460 if (len < sizeof (u_char)) {
461 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
464 GETCHAR(msglen, inp);
465 len -= sizeof (u_char);
467 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
471 PRINTMSG(msg, msglen);
473 u->us_clientstate = UPAPCS_BADAUTH;
475 syslog(LOG_ERR, "PAP authentication failed");
476 auth_withpeer_fail(u->us_unit, PPP_PAP);
481 * upap_sauthreq - Send an Authenticate-Request.
490 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
491 u->us_userlen + u->us_passwdlen;
492 outp = outpacket_buf;
494 MAKEHEADER(outp, PPP_PAP);
496 PUTCHAR(UPAP_AUTHREQ, outp);
497 PUTCHAR(++u->us_id, outp);
498 PUTSHORT(outlen, outp);
499 PUTCHAR(u->us_userlen, outp);
500 BCOPY(u->us_user, outp, u->us_userlen);
501 INCPTR(u->us_userlen, outp);
502 PUTCHAR(u->us_passwdlen, outp);
503 BCOPY(u->us_passwd, outp, u->us_passwdlen);
505 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
507 UPAPDEBUG((LOG_INFO, "pap_sauth: Sent id %d.", u->us_id));
509 TIMEOUT(upap_timeout, u, u->us_timeouttime);
511 u->us_clientstate = UPAPCS_AUTHREQ;
516 * upap_sresp - Send a response (ack or nak).
519 upap_sresp(u, code, id, msg, msglen)
528 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
529 outp = outpacket_buf;
530 MAKEHEADER(outp, PPP_PAP);
534 PUTSHORT(outlen, outp);
535 PUTCHAR(msglen, outp);
536 BCOPY(msg, outp, msglen);
537 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
539 UPAPDEBUG((LOG_INFO, "pap_sresp: Sent code %d, id %d.", code, id));
543 * upap_printpkt - print the contents of a PAP packet.
545 static char *upap_codenames[] = {
546 "AuthReq", "AuthAck", "AuthNak"
550 upap_printpkt(p, plen, printer, arg)
553 void (*printer)(void *, char *, ...);
557 int mlen, ulen, wlen;
558 char *user, *pwd, *msg;
561 if (plen < UPAP_HEADERLEN)
567 if (len < UPAP_HEADERLEN || len > plen)
570 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
571 printer(arg, " %s", upap_codenames[code-1]);
573 printer(arg, " code=0x%x", code);
574 printer(arg, " id=0x%x", id);
575 len -= UPAP_HEADERLEN;
584 if (len < ulen + wlen + 2)
586 user = (char *) (p + 1);
587 pwd = (char *) (p + ulen + 2);
588 p += ulen + wlen + 2;
589 len -= ulen + wlen + 2;
590 printer(arg, " user=");
591 print_string(user, ulen, printer, arg);
592 printer(arg, " password=");
593 print_string(pwd, wlen, printer, arg);
602 msg = (char *) (p + 1);
606 print_string(msg, mlen, printer, arg);
610 /* print the rest of the bytes in the packet */
611 for (; len > 0; --len) {
613 printer(arg, " %.2x", code);