sdhci - Implement ADMA2 transfer support. Keep SDMA support for now.
[dragonfly.git] / usr.sbin / pppd / upap.c
blob32074d4d674962e336a3d9d97a10a3a522c2a6eb
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.
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.4 2005/11/24 23:42:54 swildner Exp $
24 * TODO:
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <syslog.h>
33 #include "pppd.h"
34 #include "upap.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 = {
48 PPP_PAP,
49 upap_init,
50 upap_input,
51 upap_protrej,
52 upap_lowerup,
53 upap_lowerdown,
54 NULL,
55 NULL,
56 upap_printpkt,
57 NULL,
59 "PAP",
60 NULL,
61 NULL,
62 NULL
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.
79 static void
80 upap_init(int unit)
82 upap_state *u = &upap[unit];
84 u->us_unit = unit;
85 u->us_user = NULL;
86 u->us_userlen = 0;
87 u->us_passwd = NULL;
88 u->us_passwdlen = 0;
89 u->us_clientstate = UPAPCS_INITIAL;
90 u->us_serverstate = UPAPSS_INITIAL;
91 u->us_id = 0;
92 u->us_timeouttime = UPAP_DEFTIMEOUT;
93 u->us_maxtransmits = 10;
94 u->us_reqtimeout = UPAP_DEFREQTIME;
99 * upap_authwithpeer - Authenticate us with our peer (start client).
101 * Set new state and send authenticate's.
103 void
104 upap_authwithpeer(int unit, char *user, char *password)
106 upap_state *u = &upap[unit];
108 /* Save the username and password we're given */
109 u->us_user = user;
110 u->us_userlen = strlen(user);
111 u->us_passwd = password;
112 u->us_passwdlen = strlen(password);
113 u->us_transmits = 0;
115 /* Lower layer up yet? */
116 if (u->us_clientstate == UPAPCS_INITIAL ||
117 u->us_clientstate == UPAPCS_PENDING) {
118 u->us_clientstate = UPAPCS_PENDING;
119 return;
122 upap_sauthreq(u); /* Start protocol */
127 * upap_authpeer - Authenticate our peer (start server).
129 * Set new state.
131 void
132 upap_authpeer(int unit)
134 upap_state *u = &upap[unit];
136 /* Lower layer up yet? */
137 if (u->us_serverstate == UPAPSS_INITIAL ||
138 u->us_serverstate == UPAPSS_PENDING) {
139 u->us_serverstate = UPAPSS_PENDING;
140 return;
143 u->us_serverstate = UPAPSS_LISTEN;
144 if (u->us_reqtimeout > 0)
145 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
150 * upap_timeout - Retransmission timer for sending auth-reqs expired.
152 static void
153 upap_timeout(void *arg)
155 upap_state *u = (upap_state *) arg;
157 if (u->us_clientstate != UPAPCS_AUTHREQ)
158 return;
160 if (u->us_transmits >= u->us_maxtransmits) {
161 /* give up in disgust */
162 syslog(LOG_ERR, "No response to PAP authenticate-requests");
163 u->us_clientstate = UPAPCS_BADAUTH;
164 auth_withpeer_fail(u->us_unit, PPP_PAP);
165 return;
168 upap_sauthreq(u); /* Send Authenticate-Request */
173 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
175 static void
176 upap_reqtimeout(void *arg)
178 upap_state *u = (upap_state *) arg;
180 if (u->us_serverstate != UPAPSS_LISTEN)
181 return; /* huh?? */
183 auth_peer_fail(u->us_unit, PPP_PAP);
184 u->us_serverstate = UPAPSS_BADAUTH;
189 * upap_lowerup - The lower layer is up.
191 * Start authenticating if pending.
193 static void
194 upap_lowerup(int unit)
196 upap_state *u = &upap[unit];
198 if (u->us_clientstate == UPAPCS_INITIAL)
199 u->us_clientstate = UPAPCS_CLOSED;
200 else if (u->us_clientstate == UPAPCS_PENDING) {
201 upap_sauthreq(u); /* send an auth-request */
204 if (u->us_serverstate == UPAPSS_INITIAL)
205 u->us_serverstate = UPAPSS_CLOSED;
206 else if (u->us_serverstate == UPAPSS_PENDING) {
207 u->us_serverstate = UPAPSS_LISTEN;
208 if (u->us_reqtimeout > 0)
209 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
215 * upap_lowerdown - The lower layer is down.
217 * Cancel all timeouts.
219 static void
220 upap_lowerdown(int unit)
222 upap_state *u = &upap[unit];
224 if (u->us_clientstate == UPAPCS_AUTHREQ) /* Timeout pending? */
225 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
226 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
227 UNTIMEOUT(upap_reqtimeout, u);
229 u->us_clientstate = UPAPCS_INITIAL;
230 u->us_serverstate = UPAPSS_INITIAL;
235 * upap_protrej - Peer doesn't speak this protocol.
237 * This shouldn't happen. In any case, pretend lower layer went down.
239 static void
240 upap_protrej(int unit)
242 upap_state *u = &upap[unit];
244 if (u->us_clientstate == UPAPCS_AUTHREQ) {
245 syslog(LOG_ERR, "PAP authentication failed due to protocol-reject");
246 auth_withpeer_fail(unit, PPP_PAP);
248 if (u->us_serverstate == UPAPSS_LISTEN) {
249 syslog(LOG_ERR, "PAP authentication of peer failed (protocol-reject)");
250 auth_peer_fail(unit, PPP_PAP);
252 upap_lowerdown(unit);
257 * upap_input - Input UPAP packet.
259 static void
260 upap_input(int unit, u_char *inpacket, int l)
262 upap_state *u = &upap[unit];
263 u_char *inp;
264 u_char code, id;
265 int len;
268 * Parse header (code, id and length).
269 * If packet too short, drop it.
271 inp = inpacket;
272 if (l < UPAP_HEADERLEN) {
273 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short header."));
274 return;
276 GETCHAR(code, inp);
277 GETCHAR(id, inp);
278 GETSHORT(len, inp);
279 if (len < UPAP_HEADERLEN) {
280 UPAPDEBUG((LOG_INFO, "pap_input: rcvd illegal length."));
281 return;
283 if (len > l) {
284 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short packet."));
285 return;
287 len -= UPAP_HEADERLEN;
290 * Action depends on code.
292 switch (code) {
293 case UPAP_AUTHREQ:
294 upap_rauthreq(u, inp, id, len);
295 break;
297 case UPAP_AUTHACK:
298 upap_rauthack(u, inp, id, len);
299 break;
301 case UPAP_AUTHNAK:
302 upap_rauthnak(u, inp, id, len);
303 break;
305 default: /* XXX Need code reject */
306 break;
312 * upap_rauth - Receive Authenticate.
314 static void
315 upap_rauthreq(upap_state *u, u_char *inp, int id, int len)
317 u_char ruserlen, rpasswdlen;
318 char *ruser, *rpasswd;
319 int retcode;
320 char *msg;
321 int msglen;
323 UPAPDEBUG((LOG_INFO, "pap_rauth: Rcvd id %d.", id));
325 if (u->us_serverstate < UPAPSS_LISTEN)
326 return;
329 * If we receive a duplicate authenticate-request, we are
330 * supposed to return the same status as for the first request.
332 if (u->us_serverstate == UPAPSS_OPEN) {
333 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
334 return;
336 if (u->us_serverstate == UPAPSS_BADAUTH) {
337 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
338 return;
342 * Parse user/passwd.
344 if (len < sizeof (u_char)) {
345 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
346 return;
348 GETCHAR(ruserlen, inp);
349 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
350 if (len < 0) {
351 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
352 return;
354 ruser = (char *) inp;
355 INCPTR(ruserlen, inp);
356 GETCHAR(rpasswdlen, inp);
357 if (len < rpasswdlen) {
358 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
359 return;
361 rpasswd = (char *) inp;
364 * Check the username and password given.
366 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
367 rpasswdlen, &msg, &msglen);
368 BZERO(rpasswd, rpasswdlen);
370 upap_sresp(u, retcode, id, msg, msglen);
372 if (retcode == UPAP_AUTHACK) {
373 u->us_serverstate = UPAPSS_OPEN;
374 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
375 } else {
376 u->us_serverstate = UPAPSS_BADAUTH;
377 auth_peer_fail(u->us_unit, PPP_PAP);
380 if (u->us_reqtimeout > 0)
381 UNTIMEOUT(upap_reqtimeout, u);
386 * upap_rauthack - Receive Authenticate-Ack.
388 static void
389 upap_rauthack(upap_state *u, u_char *inp, int id, int len)
391 u_char msglen;
392 char *msg;
394 UPAPDEBUG((LOG_INFO, "pap_rauthack: Rcvd id %d.", id));
395 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
396 return;
399 * Parse message.
401 if (len < sizeof (u_char)) {
402 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
403 return;
405 GETCHAR(msglen, inp);
406 len -= sizeof (u_char);
407 if (len < msglen) {
408 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
409 return;
411 msg = (char *) inp;
412 PRINTMSG(msg, msglen);
414 u->us_clientstate = UPAPCS_OPEN;
416 auth_withpeer_success(u->us_unit, PPP_PAP);
421 * upap_rauthnak - Receive Authenticate-Nakk.
423 static void
424 upap_rauthnak(upap_state *u, u_char *inp, int id, int len)
426 u_char msglen;
427 char *msg;
429 UPAPDEBUG((LOG_INFO, "pap_rauthnak: Rcvd id %d.", id));
430 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
431 return;
434 * Parse message.
436 if (len < sizeof (u_char)) {
437 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
438 return;
440 GETCHAR(msglen, inp);
441 len -= sizeof (u_char);
442 if (len < msglen) {
443 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
444 return;
446 msg = (char *) inp;
447 PRINTMSG(msg, msglen);
449 u->us_clientstate = UPAPCS_BADAUTH;
451 syslog(LOG_ERR, "PAP authentication failed");
452 auth_withpeer_fail(u->us_unit, PPP_PAP);
457 * upap_sauthreq - Send an Authenticate-Request.
459 static void
460 upap_sauthreq(upap_state *u)
462 u_char *outp;
463 int outlen;
465 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
466 u->us_userlen + u->us_passwdlen;
467 outp = outpacket_buf;
469 MAKEHEADER(outp, PPP_PAP);
471 PUTCHAR(UPAP_AUTHREQ, outp);
472 PUTCHAR(++u->us_id, outp);
473 PUTSHORT(outlen, outp);
474 PUTCHAR(u->us_userlen, outp);
475 BCOPY(u->us_user, outp, u->us_userlen);
476 INCPTR(u->us_userlen, outp);
477 PUTCHAR(u->us_passwdlen, outp);
478 BCOPY(u->us_passwd, outp, u->us_passwdlen);
480 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
482 UPAPDEBUG((LOG_INFO, "pap_sauth: Sent id %d.", u->us_id));
484 TIMEOUT(upap_timeout, u, u->us_timeouttime);
485 ++u->us_transmits;
486 u->us_clientstate = UPAPCS_AUTHREQ;
491 * upap_sresp - Send a response (ack or nak).
493 static void
494 upap_sresp(upap_state *u, int code, int id, char *msg, int msglen)
496 u_char *outp;
497 int outlen;
499 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
500 outp = outpacket_buf;
501 MAKEHEADER(outp, PPP_PAP);
503 PUTCHAR(code, outp);
504 PUTCHAR(id, outp);
505 PUTSHORT(outlen, outp);
506 PUTCHAR(msglen, outp);
507 BCOPY(msg, outp, msglen);
508 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
510 UPAPDEBUG((LOG_INFO, "pap_sresp: Sent code %d, id %d.", code, id));
514 * upap_printpkt - print the contents of a PAP packet.
516 static char *upap_codenames[] = {
517 "AuthReq", "AuthAck", "AuthNak"
520 static int
521 upap_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...),
522 void *arg)
524 int code, id, len;
525 int mlen, ulen, wlen;
526 char *user, *pwd, *msg;
527 u_char *pstart;
529 if (plen < UPAP_HEADERLEN)
530 return 0;
531 pstart = p;
532 GETCHAR(code, p);
533 GETCHAR(id, p);
534 GETSHORT(len, p);
535 if (len < UPAP_HEADERLEN || len > plen)
536 return 0;
538 if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
539 printer(arg, " %s", upap_codenames[code-1]);
540 else
541 printer(arg, " code=0x%x", code);
542 printer(arg, " id=0x%x", id);
543 len -= UPAP_HEADERLEN;
544 switch (code) {
545 case UPAP_AUTHREQ:
546 if (len < 1)
547 break;
548 ulen = p[0];
549 if (len < ulen + 2)
550 break;
551 wlen = p[ulen + 1];
552 if (len < ulen + wlen + 2)
553 break;
554 user = (char *) (p + 1);
555 pwd = (char *) (p + ulen + 2);
556 p += ulen + wlen + 2;
557 len -= ulen + wlen + 2;
558 printer(arg, " user=");
559 print_string(user, ulen, printer, arg);
560 printer(arg, " password=");
561 print_string(pwd, wlen, printer, arg);
562 break;
563 case UPAP_AUTHACK:
564 case UPAP_AUTHNAK:
565 if (len < 1)
566 break;
567 mlen = p[0];
568 if (len < mlen + 1)
569 break;
570 msg = (char *) (p + 1);
571 p += mlen + 1;
572 len -= mlen + 1;
573 printer(arg, " ");
574 print_string(msg, mlen, printer, arg);
575 break;
578 /* print the rest of the bytes in the packet */
579 for (; len > 0; --len) {
580 GETCHAR(code, p);
581 printer(arg, " %.2x", code);
584 return p - pstart;