]> git.ozlabs.org Git - ppp.git/blob - pppd/upap.c
Log calling number failed authorization at warn instead of error, to be
[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.28 2002/10/12 02:30:21 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
406     /*
407      * Check remote number authorization.  A plugin may have filled in
408      * the remote number or added an allowed number, and rather than
409      * return an authenticate failure, is leaving it for us to verify.
410      */
411     if (retcode == UPAP_AUTHACK) {
412         if (!auth_number()) {
413             /* We do not want to leak info about the pap result. */
414             retcode = UPAP_AUTHNAK; /* XXX exit value will be "wrong" */
415             warn("calling number %q is not authorized", remote_number);
416         }
417     }
418
419     msglen = strlen(msg);
420     if (msglen > 255)
421         msglen = 255;
422     upap_sresp(u, retcode, id, msg, msglen);
423
424     /* Null terminate and clean remote name. */
425     slprintf(rhostname, sizeof(rhostname), "%.*v", ruserlen, ruser);
426
427     if (retcode == UPAP_AUTHACK) {
428         u->us_serverstate = UPAPSS_OPEN;
429         notice("PAP peer authentication succeeded for %q", rhostname);
430         auth_peer_success(u->us_unit, PPP_PAP, 0, ruser, ruserlen);
431     } else {
432         u->us_serverstate = UPAPSS_BADAUTH;
433         warn("PAP peer authentication failed for %q", rhostname);
434         auth_peer_fail(u->us_unit, PPP_PAP);
435     }
436
437     if (u->us_reqtimeout > 0)
438         UNTIMEOUT(upap_reqtimeout, u);
439 }
440
441
442 /*
443  * upap_rauthack - Receive Authenticate-Ack.
444  */
445 static void
446 upap_rauthack(u, inp, id, len)
447     upap_state *u;
448     u_char *inp;
449     int id;
450     int len;
451 {
452     u_char msglen;
453     char *msg;
454
455     if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
456         return;
457
458     /*
459      * Parse message.
460      */
461     if (len < 1) {
462         UPAPDEBUG(("pap_rauthack: ignoring missing msg-length."));
463     } else {
464         GETCHAR(msglen, inp);
465         if (msglen > 0) {
466             len -= sizeof (u_char);
467             if (len < msglen) {
468                 UPAPDEBUG(("pap_rauthack: rcvd short packet."));
469                 return;
470             }
471             msg = (char *) inp;
472             PRINTMSG(msg, msglen);
473         }
474     }
475
476     u->us_clientstate = UPAPCS_OPEN;
477
478     notice("PAP authentication succeeded");
479     auth_withpeer_success(u->us_unit, PPP_PAP, 0);
480 }
481
482
483 /*
484  * upap_rauthnak - Receive Authenticate-Nak.
485  */
486 static void
487 upap_rauthnak(u, inp, id, len)
488     upap_state *u;
489     u_char *inp;
490     int id;
491     int len;
492 {
493     u_char msglen;
494     char *msg;
495
496     if (u->us_clientstate != UPAPCS_AUTHREQ) /* XXX */
497         return;
498
499     /*
500      * Parse message.
501      */
502     if (len < 1) {
503         UPAPDEBUG(("pap_rauthnak: ignoring missing msg-length."));
504     } else {
505         GETCHAR(msglen, inp);
506         if (msglen > 0) {
507             len -= sizeof (u_char);
508             if (len < msglen) {
509                 UPAPDEBUG(("pap_rauthnak: rcvd short packet."));
510                 return;
511             }
512             msg = (char *) inp;
513             PRINTMSG(msg, msglen);
514         }
515     }
516
517     u->us_clientstate = UPAPCS_BADAUTH;
518
519     error("PAP authentication failed");
520     auth_withpeer_fail(u->us_unit, PPP_PAP);
521 }
522
523
524 /*
525  * upap_sauthreq - Send an Authenticate-Request.
526  */
527 static void
528 upap_sauthreq(u)
529     upap_state *u;
530 {
531     u_char *outp;
532     int outlen;
533
534     outlen = UPAP_HEADERLEN + 2 * sizeof (u_char) +
535         u->us_userlen + u->us_passwdlen;
536     outp = outpacket_buf;
537     
538     MAKEHEADER(outp, PPP_PAP);
539
540     PUTCHAR(UPAP_AUTHREQ, outp);
541     PUTCHAR(++u->us_id, outp);
542     PUTSHORT(outlen, outp);
543     PUTCHAR(u->us_userlen, outp);
544     BCOPY(u->us_user, outp, u->us_userlen);
545     INCPTR(u->us_userlen, outp);
546     PUTCHAR(u->us_passwdlen, outp);
547     BCOPY(u->us_passwd, outp, u->us_passwdlen);
548
549     output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
550
551     TIMEOUT(upap_timeout, u, u->us_timeouttime);
552     ++u->us_transmits;
553     u->us_clientstate = UPAPCS_AUTHREQ;
554 }
555
556
557 /*
558  * upap_sresp - Send a response (ack or nak).
559  */
560 static void
561 upap_sresp(u, code, id, msg, msglen)
562     upap_state *u;
563     u_char code, id;
564     char *msg;
565     int msglen;
566 {
567     u_char *outp;
568     int outlen;
569
570     outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
571     outp = outpacket_buf;
572     MAKEHEADER(outp, PPP_PAP);
573
574     PUTCHAR(code, outp);
575     PUTCHAR(id, outp);
576     PUTSHORT(outlen, outp);
577     PUTCHAR(msglen, outp);
578     BCOPY(msg, outp, msglen);
579     output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN);
580 }
581
582 /*
583  * upap_printpkt - print the contents of a PAP packet.
584  */
585 static char *upap_codenames[] = {
586     "AuthReq", "AuthAck", "AuthNak"
587 };
588
589 static int
590 upap_printpkt(p, plen, printer, arg)
591     u_char *p;
592     int plen;
593     void (*printer) __P((void *, char *, ...));
594     void *arg;
595 {
596     int code, id, len;
597     int mlen, ulen, wlen;
598     char *user, *pwd, *msg;
599     u_char *pstart;
600
601     if (plen < UPAP_HEADERLEN)
602         return 0;
603     pstart = p;
604     GETCHAR(code, p);
605     GETCHAR(id, p);
606     GETSHORT(len, p);
607     if (len < UPAP_HEADERLEN || len > plen)
608         return 0;
609
610     if (code >= 1 && code <= sizeof(upap_codenames) / sizeof(char *))
611         printer(arg, " %s", upap_codenames[code-1]);
612     else
613         printer(arg, " code=0x%x", code);
614     printer(arg, " id=0x%x", id);
615     len -= UPAP_HEADERLEN;
616     switch (code) {
617     case UPAP_AUTHREQ:
618         if (len < 1)
619             break;
620         ulen = p[0];
621         if (len < ulen + 2)
622             break;
623         wlen = p[ulen + 1];
624         if (len < ulen + wlen + 2)
625             break;
626         user = (char *) (p + 1);
627         pwd = (char *) (p + ulen + 2);
628         p += ulen + wlen + 2;
629         len -= ulen + wlen + 2;
630         printer(arg, " user=");
631         print_string(user, ulen, printer, arg);
632         printer(arg, " password=");
633         if (!hide_password)
634             print_string(pwd, wlen, printer, arg);
635         else
636             printer(arg, "<hidden>");
637         break;
638     case UPAP_AUTHACK:
639     case UPAP_AUTHNAK:
640         if (len < 1)
641             break;
642         mlen = p[0];
643         if (len < mlen + 1)
644             break;
645         msg = (char *) (p + 1);
646         p += mlen + 1;
647         len -= mlen + 1;
648         printer(arg, " ");
649         print_string(msg, mlen, printer, arg);
650         break;
651     }
652
653     /* print the rest of the bytes in the packet */
654     for (; len > 0; --len) {
655         GETCHAR(code, p);
656         printer(arg, " %.2x", code);
657     }
658
659     return p - pstart;
660 }