Generally use NULL instead of explicitly casting 0 to some pointer type (part2).
[dragonfly.git] / usr.sbin / pppd / upap.c
CommitLineData
984263bc
MD
1/*
2 * upap.c - User/Password Authentication Protocol.
3 *
4 * Copyright (c) 1989 Carnegie Mellon University.
5 * All rights reserved.
6 *
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.
1de703da
MD
18 *
19 * $FreeBSD: src/usr.sbin/pppd/upap.c,v 1.8 1999/08/28 01:19:08 peter Exp $
21da1569 20 * $DragonFly: src/usr.sbin/pppd/upap.c,v 1.4 2005/11/24 23:42:54 swildner Exp $
984263bc
MD
21 */
22
984263bc
MD
23/*
24 * TODO:
25 */
26
27#include <stdio.h>
28#include <string.h>
29#include <sys/types.h>
30#include <sys/time.h>
31#include <syslog.h>
32
33#include "pppd.h"
34#include "upap.h"
35
36/*
37 * Protocol entry points.
38 */
2d8a3be7
EN
39static void upap_init(int);
40static void upap_lowerup(int);
41static void upap_lowerdown(int);
42static void upap_input(int, u_char *, int);
43static void upap_protrej(int);
44static int upap_printpkt(u_char *, int,
45 void (*)(void *, char *, ...), void *);
984263bc
MD
46
47struct 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,
58 1,
59 "PAP",
60 NULL,
61 NULL,
62 NULL
63};
64
65upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
66
2d8a3be7
EN
67static void upap_timeout(void *);
68static void upap_reqtimeout(void *);
69static void upap_rauthreq(upap_state *, u_char *, int, int);
70static void upap_rauthack(upap_state *, u_char *, int, int);
71static void upap_rauthnak(upap_state *, u_char *, int, int);
72static void upap_sauthreq(upap_state *);
73static void upap_sresp(upap_state *, int, int, char *, int);
984263bc
MD
74
75
76/*
77 * upap_init - Initialize a UPAP unit.
78 */
79static void
21da1569 80upap_init(int unit)
984263bc
MD
81{
82 upap_state *u = &upap[unit];
83
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;
95}
96
97
98/*
99 * upap_authwithpeer - Authenticate us with our peer (start client).
100 *
101 * Set new state and send authenticate's.
102 */
103void
21da1569 104upap_authwithpeer(int unit, char *user, char *password)
984263bc
MD
105{
106 upap_state *u = &upap[unit];
107
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;
114
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;
120 }
121
122 upap_sauthreq(u); /* Start protocol */
123}
124
125
126/*
127 * upap_authpeer - Authenticate our peer (start server).
128 *
129 * Set new state.
130 */
131void
21da1569 132upap_authpeer(int unit)
984263bc
MD
133{
134 upap_state *u = &upap[unit];
135
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;
141 }
142
143 u->us_serverstate = UPAPSS_LISTEN;
144 if (u->us_reqtimeout > 0)
145 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
146}
147
148
149/*
150 * upap_timeout - Retransmission timer for sending auth-reqs expired.
151 */
152static void
21da1569 153upap_timeout(void *arg)
984263bc
MD
154{
155 upap_state *u = (upap_state *) arg;
156
157 if (u->us_clientstate != UPAPCS_AUTHREQ)
158 return;
159
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;
166 }
167
168 upap_sauthreq(u); /* Send Authenticate-Request */
169}
170
171
172/*
173 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
174 */
175static void
21da1569 176upap_reqtimeout(void *arg)
984263bc
MD
177{
178 upap_state *u = (upap_state *) arg;
179
180 if (u->us_serverstate != UPAPSS_LISTEN)
181 return; /* huh?? */
182
183 auth_peer_fail(u->us_unit, PPP_PAP);
184 u->us_serverstate = UPAPSS_BADAUTH;
185}
186
187
188/*
189 * upap_lowerup - The lower layer is up.
190 *
191 * Start authenticating if pending.
192 */
193static void
21da1569 194upap_lowerup(int unit)
984263bc
MD
195{
196 upap_state *u = &upap[unit];
197
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 */
202 }
203
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);
210 }
211}
212
213
214/*
215 * upap_lowerdown - The lower layer is down.
216 *
217 * Cancel all timeouts.
218 */
219static void
21da1569 220upap_lowerdown(int unit)
984263bc
MD
221{
222 upap_state *u = &upap[unit];
223
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);
228
229 u->us_clientstate = UPAPCS_INITIAL;
230 u->us_serverstate = UPAPSS_INITIAL;
231}
232
233
234/*
235 * upap_protrej - Peer doesn't speak this protocol.
236 *
237 * This shouldn't happen. In any case, pretend lower layer went down.
238 */
239static void
21da1569 240upap_protrej(int unit)
984263bc
MD
241{
242 upap_state *u = &upap[unit];
243
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);
247 }
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);
251 }
252 upap_lowerdown(unit);
253}
254
255
256/*
257 * upap_input - Input UPAP packet.
258 */
259static void
21da1569 260upap_input(int unit, u_char *inpacket, int l)
984263bc
MD
261{
262 upap_state *u = &upap[unit];
263 u_char *inp;
264 u_char code, id;
265 int len;
266
267 /*
268 * Parse header (code, id and length).
269 * If packet too short, drop it.
270 */
271 inp = inpacket;
272 if (l < UPAP_HEADERLEN) {
273 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short header."));
274 return;
275 }
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;
282 }
283 if (len > l) {
284 UPAPDEBUG((LOG_INFO, "pap_input: rcvd short packet."));
285 return;
286 }
287 len -= UPAP_HEADERLEN;
288
289 /*
290 * Action depends on code.
291 */
292 switch (code) {
293 case UPAP_AUTHREQ:
294 upap_rauthreq(u, inp, id, len);
295 break;
296
297 case UPAP_AUTHACK:
298 upap_rauthack(u, inp, id, len);
299 break;
300
301 case UPAP_AUTHNAK:
302 upap_rauthnak(u, inp, id, len);
303 break;
304
305 default: /* XXX Need code reject */
306 break;
307 }
308}
309
310
311/*
312 * upap_rauth - Receive Authenticate.
313 */
314static void
21da1569 315upap_rauthreq(upap_state *u, u_char *inp, int id, int len)
984263bc
MD
316{
317 u_char ruserlen, rpasswdlen;
318 char *ruser, *rpasswd;
319 int retcode;
320 char *msg;
321 int msglen;
322
323 UPAPDEBUG((LOG_INFO, "pap_rauth: Rcvd id %d.", id));
324
325 if (u->us_serverstate < UPAPSS_LISTEN)
326 return;
327
328 /*
329 * If we receive a duplicate authenticate-request, we are
330 * supposed to return the same status as for the first request.
331 */
332 if (u->us_serverstate == UPAPSS_OPEN) {
333 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
334 return;
335 }
336 if (u->us_serverstate == UPAPSS_BADAUTH) {
337 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
338 return;
339 }
340
341 /*
342 * Parse user/passwd.
343 */
344 if (len < sizeof (u_char)) {
345 UPAPDEBUG((LOG_INFO, "pap_rauth: rcvd short packet."));
346 return;
347 }
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;
353 }
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;
360 }
361 rpasswd = (char *) inp;
362
363 /*
364 * Check the username and password given.
365 */
366 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
367 rpasswdlen, &msg, &msglen);
368 BZERO(rpasswd, rpasswdlen);
369
370 upap_sresp(u, retcode, id, msg, msglen);
371
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);
378 }
379
380 if (u->us_reqtimeout > 0)
381 UNTIMEOUT(upap_reqtimeout, u);
382}
383
384
385/*
386 * upap_rauthack - Receive Authenticate-Ack.
387 */
388static void
21da1569 389upap_rauthack(upap_state *u, u_char *inp, int id, int len)
984263bc
MD
390{
391 u_char msglen;
392 char *msg;
393
394 UPAPDEBUG((LOG_INFO, "pap_rauthack: Rcvd id %d.", id));
395 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
396 return;
397
398 /*
399 * Parse message.
400 */
401 if (len < sizeof (u_char)) {
402 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
403 return;
404 }
405 GETCHAR(msglen, inp);
406 len -= sizeof (u_char);
407 if (len < msglen) {
408 UPAPDEBUG((LOG_INFO, "pap_rauthack: rcvd short packet."));
409 return;
410 }
411 msg = (char *) inp;
412 PRINTMSG(msg, msglen);
413
414 u->us_clientstate = UPAPCS_OPEN;
415
416 auth_withpeer_success(u->us_unit, PPP_PAP);
417}
418
419
420/*
421 * upap_rauthnak - Receive Authenticate-Nakk.
422 */
423static void
21da1569 424upap_rauthnak(upap_state *u, u_char *inp, int id, int len)
984263bc
MD
425{
426 u_char msglen;
427 char *msg;
428
429 UPAPDEBUG((LOG_INFO, "pap_rauthnak: Rcvd id %d.", id));
430 if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
431 return;
432
433 /*
434 * Parse message.
435 */
436 if (len < sizeof (u_char)) {
437 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
438 return;
439 }
440 GETCHAR(msglen, inp);
441 len -= sizeof (u_char);
442 if (len < msglen) {
443 UPAPDEBUG((LOG_INFO, "pap_rauthnak: rcvd short packet."));
444 return;
445 }
446 msg = (char *) inp;
447 PRINTMSG(msg, msglen);
448
449 u->us_clientstate = UPAPCS_BADAUTH;
450
451 syslog(LOG_ERR, "PAP authentication failed");
452 auth_withpeer_fail(u->us_unit, PPP_PAP);
453}
454
455
456/*
457 * upap_sauthreq - Send an Authenticate-Request.
458 */
459static void
21da1569 460upap_sauthreq(upap_state *u)
984263bc
MD
461{
462 u_char *outp;
463 int outlen;
464
465 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
466 u->us_userlen + u->us_passwdlen;
467 outp = outpacket_buf;
468
469 MAKEHEADER(outp, PPP_PAP);
470
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);
479
480 output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
481
482 UPAPDEBUG((LOG_INFO, "pap_sauth: Sent id %d.", u->us_id));
483
484 TIMEOUT(upap_timeout, u, u->us_timeouttime);
485 ++u->us_transmits;
486 u->us_clientstate = UPAPCS_AUTHREQ;
487}
488
489
490/*
491 * upap_sresp - Send a response (ack or nak).
492 */
493static void
21da1569 494upap_sresp(upap_state *u, int code, int id, char *msg, int msglen)
984263bc
MD
495{
496 u_char *outp;
497 int outlen;
498
499 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
500 outp = outpacket_buf;
501 MAKEHEADER(outp, PPP_PAP);
502
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);
509
510 UPAPDEBUG((LOG_INFO, "pap_sresp: Sent code %d, id %d.", code, id));
511}
512
513/*
514 * upap_printpkt - print the contents of a PAP packet.
515 */
516static char *upap_codenames[] = {
517 "AuthReq", "AuthAck", "AuthNak"
518};
519
520static int
21da1569
SW
521upap_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...),
522 void *arg)
984263bc
MD
523{
524 int code, id, len;
525 int mlen, ulen, wlen;
526 char *user, *pwd, *msg;
527 u_char *pstart;
528
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;
537
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;
576 }
577
578 /* print the rest of the bytes in the packet */
579 for (; len > 0; --len) {
580 GETCHAR(code, p);
581 printer(arg, " %.2x", code);
582 }
583
584 return p - pstart;
585}