]> git.ozlabs.org Git - ppp.git/blob - pppd/upap.c
Forgot to remote *.ln files (made by lint) when doing make clean.
[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 #define RCSID   "$Id: upap.c,v 1.21 1999/09/11 12:09:00 paulus Exp $"
21
22 /*
23  * TODO:
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "pppd.h"
30 #include "upap.h"
31
32 static const char rcsid[] = RCSID;
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);
399     BZERO(rpasswd, rpasswdlen);
400     msglen = strlen(msg);
401     if (msglen > 255)
402         msglen = 255;
403
404     upap_sresp(u, retcode, id, msg, msglen);
405
406     if (retcode == UPAP_AUTHACK) {
407         u->us_serverstate = UPAPSS_OPEN;
408         auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
409     } else {
410         u->us_serverstate = UPAPSS_BADAUTH;
411         auth_peer_fail(u->us_unit, PPP_PAP);
412     }
413
414     if (u->us_reqtimeout > 0)
415         UNTIMEOUT(upap_reqtimeout, u);
416 }
417
418
419 /*
420  * upap_rauthack - Receive Authenticate-Ack.
421  */
422 static void
423 upap_rauthack(u, inp, id, len)
424     upap_state *u;
425     u_char *inp;
426     int id;
427     int len;
428 {
429     u_char msglen;
430     char *msg;
431
432     if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
433         return;
434
435     /*
436      * Parse message.
437      */
438     if (len < sizeof (u_char)) {
439         UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
440     } else {
441         GETCHAR(msglen, inp);
442         if (msglen > 0) {
443             len -= sizeof (u_char);
444             if (len < msglen) {
445                 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
446                 return;
447             }
448             msg = (char *) inp;
449             PRINTMSG(msg, msglen);
450         }
451     }
452
453     u->us_clientstate = UPAPCS_OPEN;
454
455     auth_withpeer_success(u->us_unit, PPP_PAP);
456 }
457
458
459 /*
460  * upap_rauthnak - Receive Authenticate-Nakk.
461  */
462 static void
463 upap_rauthnak(u, inp, id, len)
464     upap_state *u;
465     u_char *inp;
466     int id;
467     int len;
468 {
469     u_char msglen;
470     char *msg;
471
472     if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
473         return;
474
475     /*
476      * Parse message.
477      */
478     if (len < sizeof (u_char)) {
479         UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
480     } else {
481         GETCHAR(msglen, inp);
482         if (msglen > 0) {
483             len -= sizeof (u_char);
484             if (len < msglen) {
485                 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
486                 return;
487             }
488             msg = (char *) inp;
489             PRINTMSG(msg, msglen);
490         }
491     }
492
493     u->us_clientstate = UPAPCS_BADAUTH;
494
495     error("PAP authentication failed");
496     auth_withpeer_fail(u->us_unit, PPP_PAP);
497 }
498
499
500 /*
501  * upap_sauthreq - Send an Authenticate-Request.
502  */
503 static void
504 upap_sauthreq(u)
505     upap_state *u;
506 {
507     u_char *outp;
508     int outlen;
509
510     outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
511         u->us_userlen + u->us_passwdlen;
512     outp = outpacket_buf;
513     
514     MAKEHEADER(outp, PPP_PAP);
515
516     PUTCHAR(UPAP_AUTHREQ, outp);
517     PUTCHAR(++u->us_id, outp);
518     PUTSHORT(outlen, outp);
519     PUTCHAR(u->us_userlen, outp);
520     BCOPY(u->us_user, outp, u->us_userlen);
521     INCPTR(u->us_userlen, outp);
522     PUTCHAR(u->us_passwdlen, outp);
523     BCOPY(u->us_passwd, outp, u->us_passwdlen);
524
525     output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
526
527     TIMEOUT(upap_timeout, u, u->us_timeouttime);
528     ++u->us_transmits;
529     u->us_clientstate = UPAPCS_AUTHREQ;
530 }
531
532
533 /*
534  * upap_sresp - Send a response (ack or nak).
535  */
536 static void
537 upap_sresp(u, code, id, msg, msglen)
538     upap_state *u;
539     u_char code, id;
540     char *msg;
541     int msglen;
542 {
543     u_char *outp;
544     int outlen;
545
546     outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
547     outp = outpacket_buf;
548     MAKEHEADER(outp, PPP_PAP);
549
550     PUTCHAR(code, outp);
551     PUTCHAR(id, outp);
552     PUTSHORT(outlen, outp);
553     PUTCHAR(msglen, outp);
554     BCOPY(msg, outp, msglen);
555     output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
556 }
557
558 /*
559  * upap_printpkt - print the contents of a PAP packet.
560  */
561 static char *upap_codenames[] = {
562     "AuthReq", "AuthAck", "AuthNak"
563 };
564
565 static int
566 upap_printpkt(p, plen, printer, arg)
567     u_char *p;
568     int plen;
569     void (*printer) __P((void *, char *, ...));
570     void *arg;
571 {
572     int code, id, len;
573     int mlen, ulen, wlen;
574     char *user, *pwd, *msg;
575     u_char *pstart;
576
577     if (plen < UPAP_HEADERLEN)
578         return 0;
579     pstart = p;
580     GETCHAR(code, p);
581     GETCHAR(id, p);
582     GETSHORT(len, p);
583     if (len < UPAP_HEADERLEN || len > plen)
584         return 0;
585
586     if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
587         printer(arg, " %s", upap_codenames[code-1]);
588     else
589         printer(arg, " code=0x%x", code);
590     printer(arg, " id=0x%x", id);
591     len -= UPAP_HEADERLEN;
592     switch (code) {
593     case UPAP_AUTHREQ:
594         if (len < 1)
595             break;
596         ulen = p[0];
597         if (len < ulen + 2)
598             break;
599         wlen = p[ulen + 1];
600         if (len < ulen + wlen + 2)
601             break;
602         user = (char *) (p + 1);
603         pwd = (char *) (p + ulen + 2);
604         p += ulen + wlen + 2;
605         len -= ulen + wlen + 2;
606         printer(arg, " user=");
607         print_string(user, ulen, printer, arg);
608         printer(arg, " password=");
609         if (!hide_password)
610             print_string(pwd, wlen, printer, arg);
611         else
612             printer(arg, "<hidden>");
613         break;
614     case UPAP_AUTHACK:
615     case UPAP_AUTHNAK:
616         if (len < 1)
617             break;
618         mlen = p[0];
619         if (len < mlen + 1)
620             break;
621         msg = (char *) (p + 1);
622         p += mlen + 1;
623         len -= mlen + 1;
624         printer(arg, " ");
625         print_string(msg, mlen, printer, arg);
626         break;
627     }
628
629     /* print the rest of the bytes in the packet */
630     for (; len > 0; --len) {
631         GETCHAR(code, p);
632         printer(arg, " %.2x", code);
633     }
634
635     return p - pstart;
636 }