]> git.ozlabs.org Git - ppp.git/blob - pppd/upap.c
maxfail option, exit code for when when we fail to auth
[ppp.git] / pppd / upap.c
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.
18  */
19
20 #ifndef lint
21 static char rcsid[] = "$Id: upap.c,v 1.17 1999/06/24 00:17:48 paulus Exp $";
22 #endif
23
24 /*
25  * TODO:
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "pppd.h"
32 #include "upap.h"
33
34 static bool hide_password;
35
36 /*
37  * Command-line options.
38  */
39 static option_t pap_option_list[] = {
40     { "hide-password", o_bool, &hide_password,
41       "Don't output passwords to log", 1 },
42     { "pap-restart", o_int, &upap[0].us_timeouttime,
43       "Set retransmit timeout for PAP" },
44     { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
45       "Set max number of transmissions for auth-reqs" },
46     { "pap-timeout", o_int, &upap[0].us_reqtimeout,
47       "Set time limit for peer PAP authentication" },
48     { NULL }
49 };
50
51 /*
52  * Protocol entry points.
53  */
54 static void upap_init __P((int));
55 static void upap_lowerup __P((int));
56 static void upap_lowerdown __P((int));
57 static void upap_input __P((int, u_char *, int));
58 static void upap_protrej __P((int));
59 static int  upap_printpkt __P((u_char *, int,
60                                void (*) __P((void *, char *, ...)), void *));
61
62 struct protent pap_protent = {
63     PPP_PAP,
64     upap_init,
65     upap_input,
66     upap_protrej,
67     upap_lowerup,
68     upap_lowerdown,
69     NULL,
70     NULL,
71     upap_printpkt,
72     NULL,
73     1,
74     "PAP",
75     NULL,
76     pap_option_list,
77     NULL,
78     NULL,
79     NULL
80 };
81
82 upap_state upap[NUM_PPP];               /* UPAP state; one for each unit */
83
84 static void upap_timeout __P((void *));
85 static void upap_reqtimeout __P((void *));
86 static void upap_rauthreq __P((upap_state *, u_char *, int, int));
87 static void upap_rauthack __P((upap_state *, u_char *, int, int));
88 static void upap_rauthnak __P((upap_state *, u_char *, int, int));
89 static void upap_sauthreq __P((upap_state *));
90 static void upap_sresp __P((upap_state *, int, int, char *, int));
91
92
93 /*
94  * upap_init - Initialize a UPAP unit.
95  */
96 static void
97 upap_init(unit)
98     int unit;
99 {
100     upap_state *u = &upap[unit];
101
102     u->us_unit = unit;
103     u->us_user = NULL;
104     u->us_userlen = 0;
105     u->us_passwd = NULL;
106     u->us_passwdlen = 0;
107     u->us_clientstate = UPAPCS_INITIAL;
108     u->us_serverstate = UPAPSS_INITIAL;
109     u->us_id = 0;
110     u->us_timeouttime = UPAP_DEFTIMEOUT;
111     u->us_maxtransmits = 10;
112     u->us_reqtimeout = UPAP_DEFREQTIME;
113 }
114
115
116 /*
117  * upap_authwithpeer - Authenticate us with our peer (start client).
118  *
119  * Set new state and send authenticate's.
120  */
121 void
122 upap_authwithpeer(unit, user, password)
123     int unit;
124     char *user, *password;
125 {
126     upap_state *u = &upap[unit];
127
128     /* Save the username and password we're given */
129     u->us_user = user;
130     u->us_userlen = strlen(user);
131     u->us_passwd = password;
132     u->us_passwdlen = strlen(password);
133     u->us_transmits = 0;
134
135     /* Lower layer up yet? */
136     if (u->us_clientstate == UPAPCS_INITIAL ||
137         u->us_clientstate == UPAPCS_PENDING) {
138         u->us_clientstate = UPAPCS_PENDING;
139         return;
140     }
141
142     upap_sauthreq(u);                   /* Start protocol */
143 }
144
145
146 /*
147  * upap_authpeer - Authenticate our peer (start server).
148  *
149  * Set new state.
150  */
151 void
152 upap_authpeer(unit)
153     int unit;
154 {
155     upap_state *u = &upap[unit];
156
157     /* Lower layer up yet? */
158     if (u->us_serverstate == UPAPSS_INITIAL ||
159         u->us_serverstate == UPAPSS_PENDING) {
160         u->us_serverstate = UPAPSS_PENDING;
161         return;
162     }
163
164     u->us_serverstate = UPAPSS_LISTEN;
165     if (u->us_reqtimeout > 0)
166         TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
167 }
168
169
170 /*
171  * upap_timeout - Retransmission timer for sending auth-reqs expired.
172  */
173 static void
174 upap_timeout(arg)
175     void *arg;
176 {
177     upap_state *u = (upap_state *) arg;
178
179     if (u->us_clientstate != UPAPCS_AUTHREQ)
180         return;
181
182     if (u->us_transmits >= u->us_maxtransmits) {
183         /* give up in disgust */
184         error("No response to PAP authenticate-requests");
185         u->us_clientstate = UPAPCS_BADAUTH;
186         auth_withpeer_fail(u->us_unit, PPP_PAP);
187         return;
188     }
189
190     upap_sauthreq(u);           /* Send Authenticate-Request */
191 }
192
193
194 /*
195  * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
196  */
197 static void
198 upap_reqtimeout(arg)
199     void *arg;
200 {
201     upap_state *u = (upap_state *) arg;
202
203     if (u->us_serverstate != UPAPSS_LISTEN)
204         return;                 /* huh?? */
205
206     auth_peer_fail(u->us_unit, PPP_PAP);
207     u->us_serverstate = UPAPSS_BADAUTH;
208 }
209
210
211 /*
212  * upap_lowerup - The lower layer is up.
213  *
214  * Start authenticating if pending.
215  */
216 static void
217 upap_lowerup(unit)
218     int unit;
219 {
220     upap_state *u = &upap[unit];
221
222     if (u->us_clientstate == UPAPCS_INITIAL)
223         u->us_clientstate = UPAPCS_CLOSED;
224     else if (u->us_clientstate == UPAPCS_PENDING) {
225         upap_sauthreq(u);       /* send an auth-request */
226     }
227
228     if (u->us_serverstate == UPAPSS_INITIAL)
229         u->us_serverstate = UPAPSS_CLOSED;
230     else if (u->us_serverstate == UPAPSS_PENDING) {
231         u->us_serverstate = UPAPSS_LISTEN;
232         if (u->us_reqtimeout > 0)
233             TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
234     }
235 }
236
237
238 /*
239  * upap_lowerdown - The lower layer is down.
240  *
241  * Cancel all timeouts.
242  */
243 static void
244 upap_lowerdown(unit)
245     int unit;
246 {
247     upap_state *u = &upap[unit];
248
249     if (u->us_clientstate == UPAPCS_AUTHREQ)    /* Timeout pending? */
250         UNTIMEOUT(upap_timeout, u);             /* Cancel timeout */
251     if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0)
252         UNTIMEOUT(upap_reqtimeout, u);
253
254     u->us_clientstate = UPAPCS_INITIAL;
255     u->us_serverstate = UPAPSS_INITIAL;
256 }
257
258
259 /*
260  * upap_protrej - Peer doesn't speak this protocol.
261  *
262  * This shouldn't happen.  In any case, pretend lower layer went down.
263  */
264 static void
265 upap_protrej(unit)
266     int unit;
267 {
268     upap_state *u = &upap[unit];
269
270     if (u->us_clientstate == UPAPCS_AUTHREQ) {
271         error("PAP authentication failed due to protocol-reject");
272         auth_withpeer_fail(unit, PPP_PAP);
273     }
274     if (u->us_serverstate == UPAPSS_LISTEN) {
275         error("PAP authentication of peer failed (protocol-reject)");
276         auth_peer_fail(unit, PPP_PAP);
277     }
278     upap_lowerdown(unit);
279 }
280
281
282 /*
283  * upap_input - Input UPAP packet.
284  */
285 static void
286 upap_input(unit, inpacket, l)
287     int unit;
288     u_char *inpacket;
289     int l;
290 {
291     upap_state *u = &upap[unit];
292     u_char *inp;
293     u_char code, id;
294     int len;
295
296     /*
297      * Parse header (code, id and length).
298      * If packet too short, drop it.
299      */
300     inp = inpacket;
301     if (l < UPAP_HEADERLEN) {
302         UPAPDEBUG(("pap_input: rcvd short header."));
303         return;
304     }
305     GETCHAR(code, inp);
306     GETCHAR(id, inp);
307     GETSHORT(len, inp);
308     if (len < UPAP_HEADERLEN) {
309         UPAPDEBUG(("pap_input: rcvd illegal length."));
310         return;
311     }
312     if (len > l) {
313         UPAPDEBUG(("pap_input: rcvd short packet."));
314         return;
315     }
316     len -= UPAP_HEADERLEN;
317
318     /*
319      * Action depends on code.
320      */
321     switch (code) {
322     case UPAP_AUTHREQ:
323         upap_rauthreq(u, inp, id, len);
324         break;
325
326     case UPAP_AUTHACK:
327         upap_rauthack(u, inp, id, len);
328         break;
329
330     case UPAP_AUTHNAK:
331         upap_rauthnak(u, inp, id, len);
332         break;
333
334     default:                            /* XXX Need code reject */
335         break;
336     }
337 }
338
339
340 /*
341  * upap_rauth - Receive Authenticate.
342  */
343 static void
344 upap_rauthreq(u, inp, id, len)
345     upap_state *u;
346     u_char *inp;
347     int id;
348     int len;
349 {
350     u_char ruserlen, rpasswdlen;
351     char *ruser, *rpasswd;
352     int retcode;
353     char *msg;
354     int msglen;
355
356     if (u->us_serverstate < UPAPSS_LISTEN)
357         return;
358
359     /*
360      * If we receive a duplicate authenticate-request, we are
361      * supposed to return the same status as for the first request.
362      */
363     if (u->us_serverstate == UPAPSS_OPEN) {
364         upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
365         return;
366     }
367     if (u->us_serverstate == UPAPSS_BADAUTH) {
368         upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
369         return;
370     }
371
372     /*
373      * Parse user/passwd.
374      */
375     if (len < sizeof (u_char)) {
376         UPAPDEBUG(("pap_rauth: rcvd short packet."));
377         return;
378     }
379     GETCHAR(ruserlen, inp);
380     len -= sizeof (u_char) + ruserlen + sizeof (u_char);
381     if (len < 0) {
382         UPAPDEBUG(("pap_rauth: rcvd short packet."));
383         return;
384     }
385     ruser = (char *) inp;
386     INCPTR(ruserlen, inp);
387     GETCHAR(rpasswdlen, inp);
388     if (len < rpasswdlen) {
389         UPAPDEBUG(("pap_rauth: rcvd short packet."));
390         return;
391     }
392     rpasswd = (char *) inp;
393
394     /*
395      * Check the username and password given.
396      */
397     retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd,
398                            rpasswdlen, &msg, &msglen);
399     BZERO(rpasswd, rpasswdlen);
400
401     upap_sresp(u, retcode, id, msg, msglen);
402
403     if (retcode == UPAP_AUTHACK) {
404         u->us_serverstate = UPAPSS_OPEN;
405         auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
406     } else {
407         u->us_serverstate = UPAPSS_BADAUTH;
408         auth_peer_fail(u->us_unit, PPP_PAP);
409     }
410
411     if (u->us_reqtimeout > 0)
412         UNTIMEOUT(upap_reqtimeout, u);
413 }
414
415
416 /*
417  * upap_rauthack - Receive Authenticate-Ack.
418  */
419 static void
420 upap_rauthack(u, inp, id, len)
421     upap_state *u;
422     u_char *inp;
423     int id;
424     int len;
425 {
426     u_char msglen;
427     char *msg;
428
429     if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
430         return;
431
432     /*
433      * Parse message.
434      */
435     if (len < sizeof (u_char)) {
436         UPAPDEBUG(("pap_rauthack: rcvd short packet."));
437         return;
438     }
439     GETCHAR(msglen, inp);
440     if (msglen > 0) {
441         len -= sizeof (u_char);
442         if (len < msglen) {
443             UPAPDEBUG(("pap_rauthack: rcvd short packet."));
444             return;
445         }
446         msg = (char *) inp;
447         PRINTMSG(msg, msglen);
448     }
449
450     u->us_clientstate = UPAPCS_OPEN;
451
452     auth_withpeer_success(u->us_unit, PPP_PAP);
453 }
454
455
456 /*
457  * upap_rauthnak - Receive Authenticate-Nakk.
458  */
459 static void
460 upap_rauthnak(u, inp, id, len)
461     upap_state *u;
462     u_char *inp;
463     int id;
464     int len;
465 {
466     u_char msglen;
467     char *msg;
468
469     if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
470         return;
471
472     /*
473      * Parse message.
474      */
475     if (len < sizeof (u_char)) {
476         UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
477         return;
478     }
479     GETCHAR(msglen, inp);
480     if (msglen > 0) {
481         len -= sizeof (u_char);
482         if (len < msglen) {
483             UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
484             return;
485         }
486         msg = (char *) inp;
487         PRINTMSG(msg, msglen);
488     }
489
490     u->us_clientstate = UPAPCS_BADAUTH;
491
492     error("PAP authentication failed");
493     auth_withpeer_fail(u->us_unit, PPP_PAP);
494 }
495
496
497 /*
498  * upap_sauthreq - Send an Authenticate-Request.
499  */
500 static void
501 upap_sauthreq(u)
502     upap_state *u;
503 {
504     u_char *outp;
505     int outlen;
506
507     outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
508         u->us_userlen + u->us_passwdlen;
509     outp = outpacket_buf;
510     
511     MAKEHEADER(outp, PPP_PAP);
512
513     PUTCHAR(UPAP_AUTHREQ, outp);
514     PUTCHAR(++u->us_id, outp);
515     PUTSHORT(outlen, outp);
516     PUTCHAR(u->us_userlen, outp);
517     BCOPY(u->us_user, outp, u->us_userlen);
518     INCPTR(u->us_userlen, outp);
519     PUTCHAR(u->us_passwdlen, outp);
520     BCOPY(u->us_passwd, outp, u->us_passwdlen);
521
522     output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
523
524     TIMEOUT(upap_timeout, u, u->us_timeouttime);
525     ++u->us_transmits;
526     u->us_clientstate = UPAPCS_AUTHREQ;
527 }
528
529
530 /*
531  * upap_sresp - Send a response (ack or nak).
532  */
533 static void
534 upap_sresp(u, code, id, msg, msglen)
535     upap_state *u;
536     u_char code, id;
537     char *msg;
538     int msglen;
539 {
540     u_char *outp;
541     int outlen;
542
543     outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
544     outp = outpacket_buf;
545     MAKEHEADER(outp, PPP_PAP);
546
547     PUTCHAR(code, outp);
548     PUTCHAR(id, outp);
549     PUTSHORT(outlen, outp);
550     PUTCHAR(msglen, outp);
551     BCOPY(msg, outp, msglen);
552     output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
553 }
554
555 /*
556  * upap_printpkt - print the contents of a PAP packet.
557  */
558 static char *upap_codenames[] = {
559     "AuthReq", "AuthAck", "AuthNak"
560 };
561
562 static int
563 upap_printpkt(p, plen, printer, arg)
564     u_char *p;
565     int plen;
566     void (*printer) __P((void *, char *, ...));
567     void *arg;
568 {
569     int code, id, len;
570     int mlen, ulen, wlen;
571     char *user, *pwd, *msg;
572     u_char *pstart;
573
574     if (plen < UPAP_HEADERLEN)
575         return 0;
576     pstart = p;
577     GETCHAR(code, p);
578     GETCHAR(id, p);
579     GETSHORT(len, p);
580     if (len < UPAP_HEADERLEN || len > plen)
581         return 0;
582
583     if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
584         printer(arg, " %s", upap_codenames[code-1]);
585     else
586         printer(arg, " code=0x%x", code);
587     printer(arg, " id=0x%x", id);
588     len -= UPAP_HEADERLEN;
589     switch (code) {
590     case UPAP_AUTHREQ:
591         if (len < 1)
592             break;
593         ulen = p[0];
594         if (len < ulen + 2)
595             break;
596         wlen = p[ulen + 1];
597         if (len < ulen + wlen + 2)
598             break;
599         user = (char *) (p + 1);
600         pwd = (char *) (p + ulen + 2);
601         p += ulen + wlen + 2;
602         len -= ulen + wlen + 2;
603         printer(arg, " user=");
604         print_string(user, ulen, printer, arg);
605         printer(arg, " password=");
606         if (!hide_password)
607             print_string(pwd, wlen, printer, arg);
608         else
609             printer(arg, "<hidden>");
610         break;
611     case UPAP_AUTHACK:
612     case UPAP_AUTHNAK:
613         if (len < 1)
614             break;
615         mlen = p[0];
616         if (len < mlen + 1)
617             break;
618         msg = (char *) (p + 1);
619         p += mlen + 1;
620         len -= mlen + 1;
621         printer(arg, " ");
622         print_string(msg, mlen, printer, arg);
623         break;
624     }
625
626     /* print the rest of the bytes in the packet */
627     for (; len > 0; --len) {
628         GETCHAR(code, p);
629         printer(arg, " %.2x", code);
630     }
631
632     return p - pstart;
633 }