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