Many more changes to get it to build.
[freeradius.git] / src / modules / rlm_eap / radeapclient.c
1 /*
2  * radeapclient.c       EAP specific radius packet debug tool.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  */
24
25 #include <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
28 #include <freeradius-devel/libradius.h>
29
30 #include <ctype.h>
31
32 #if HAVE_GETOPT_H
33 #       include <getopt.h>
34 #endif
35
36 #include <freeradius-devel/conf.h>
37 #include <freeradius-devel/radpaths.h>
38 #include <freeradius-devel/md5.h>
39
40 #include "eap_types.h"
41 #include "eap_sim.h"
42
43 extern int sha1_data_problems;
44
45 static int retries = 10;
46 static float timeout = 3;
47 static const char *secret = NULL;
48 static int do_output = 1;
49 static int do_summary = 0;
50 static int filedone = 0;
51 static int totalapp = 0;
52 static int totaldeny = 0;
53 static char filesecret[256];
54 const char *radius_dir = RADDBDIR;
55 const char *progname = "radeapclient";
56 /* fr_randctx randctx; */
57
58
59 radlog_dest_t radlog_dest = RADLOG_STDERR;
60 const char *radlog_dir = NULL;
61 int debug_flag = 0;
62 char password[256];
63
64 struct eapsim_keys eapsim_mk;
65
66 static void map_eap_types(RADIUS_PACKET *req);
67 static void unmap_eap_types(RADIUS_PACKET *rep);
68 static int map_eapsim_types(RADIUS_PACKET *r);
69 static int unmap_eapsim_types(RADIUS_PACKET *r);
70
71 void debug_pair_list(UNUSED VALUE_PAIR *vp)
72 {
73         return;
74 }
75
76 static void NEVER_RETURNS usage(void)
77 {
78         fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
79
80         fprintf(stderr, "  <command>    One of auth, acct, status, or disconnect.\n");
81         fprintf(stderr, "  -c count    Send each packet 'count' times.\n");
82         fprintf(stderr, "  -d raddb    Set dictionary directory.\n");
83         fprintf(stderr, "  -f file     Read packets from file, not stdin.\n");
84         fprintf(stderr, "  -r retries  If timeout, retry sending the packet 'retries' times.\n");
85         fprintf(stderr, "  -t timeout  Wait 'timeout' seconds before retrying (may be a floating point number).\n");
86         fprintf(stderr, "  -i id       Set request id to 'id'.  Values may be 0..255\n");
87         fprintf(stderr, "  -S file     read secret from file, not command line.\n");
88         fprintf(stderr, "  -q          Do not print anything out.\n");
89         fprintf(stderr, "  -s          Print out summary information of auth results.\n");
90         fprintf(stderr, "  -v          Show program version information.\n");
91         fprintf(stderr, "  -x          Debugging mode.\n");
92
93         exit(1);
94 }
95
96 int radlog(int lvl, const char *msg, ...)
97 {
98         va_list ap;
99         int r;
100
101         r = lvl; /* shut up compiler */
102
103         va_start(ap, msg);
104         r = vfprintf(stderr, msg, ap);
105         va_end(ap);
106         fputc('\n', stderr);
107
108         return r;
109 }
110
111 int log_debug(const char *msg, ...)
112 {
113         va_list ap;
114         int r;
115
116         va_start(ap, msg);
117         r = vfprintf(stderr, msg, ap);
118         va_end(ap);
119         fputc('\n', stderr);
120
121         return r;
122 }
123
124 void radlog_request(UNUSED int lvl, UNUSED int priority,
125                     UNUSED REQUEST *request, const char *msg, ...)
126 {
127         va_list ap;
128
129         va_start(ap, msg);
130         vfprintf(stderr, msg, ap);
131         va_end(ap);
132         fputc('\n', stderr);
133 }
134
135 static int getport(const char *name)
136 {
137         struct  servent         *svp;
138
139         svp = getservbyname (name, "udp");
140         if (!svp) {
141                 return 0;
142         }
143
144         return ntohs(svp->s_port);
145 }
146
147 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
148 {
149         int i;
150         struct timeval  tv;
151
152         for (i = 0; i < retries; i++) {
153                 fd_set          rdfdesc;
154
155                 rad_send(req, NULL, secret);
156
157                 /* And wait for reply, timing out as necessary */
158                 FD_ZERO(&rdfdesc);
159                 FD_SET(req->sockfd, &rdfdesc);
160
161                 tv.tv_sec = (int)timeout;
162                 tv.tv_usec = 1000000 * (timeout - (int) timeout);
163
164                 /* Something's wrong if we don't get exactly one fd. */
165                 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
166                         continue;
167                 }
168
169                 *rep = rad_recv(req->sockfd, 0);
170                 if (*rep != NULL) {
171                         /*
172                          *      If we get a response from a machine
173                          *      which we did NOT send a request to,
174                          *      then complain.
175                          */
176                         if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
177                             (memcmp(&(*rep)->src_ipaddr.ipaddr,
178                                     &req->dst_ipaddr.ipaddr,
179                                     ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
180                                       sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
181                                       sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
182                             ((*rep)->src_port != req->dst_port)) {
183                                 char src[128], dst[128];
184
185                                 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
186                                 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
187                                 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
188                                         dst, req->dst_port,
189                                         src, (*rep)->src_port);
190                                 exit(1);
191                         }
192                         break;
193                 } else {        /* NULL: couldn't receive the packet */
194                         fr_perror("radclient:");
195                         exit(1);
196                 }
197         }
198
199         /* No response or no data read (?) */
200         if (i == retries) {
201                 fprintf(stderr, "radclient: no response from server\n");
202                 exit(1);
203         }
204
205         /*
206          *      FIXME: Discard the packet & listen for another.
207          *
208          *      Hmm... we should really be using eapol_test, which does
209          *      a lot more than radeapclient.
210          */
211         if (rad_verify(*rep, req, secret) != 0) {
212                 fr_perror("rad_verify");
213                 exit(1);
214         }
215
216         if (rad_decode(*rep, req, secret) != 0) {
217                 fr_perror("rad_decode");
218                 exit(1);
219         }
220
221         /* libradius debug already prints out the value pairs for us */
222         if (!fr_debug_flag && do_output) {
223                 printf("Received response ID %d, code %d, length = %d\n",
224                        (*rep)->id, (*rep)->code, (int) (*rep)->data_len);
225                 vp_printlist(stdout, (*rep)->vps);
226         }
227         if((*rep)->code == PW_AUTHENTICATION_ACK) {
228                 totalapp++;
229         } else {
230                 totaldeny++;
231         }
232
233         return 0;
234 }
235
236 static void cleanresp(RADIUS_PACKET *resp)
237 {
238         VALUE_PAIR *vpnext, *vp, **last;
239
240
241         /*
242          * maybe should just copy things we care about, or keep
243          * a copy of the original input and start from there again?
244          */
245         pairdelete(&resp->vps, PW_EAP_MESSAGE, 0);
246         pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY, 0);
247
248         last = &resp->vps;
249         for(vp = *last; vp != NULL; vp = vpnext)
250         {
251                 vpnext = vp->next;
252
253                 if((vp->attribute > ATTRIBUTE_EAP_BASE &&
254                     vp->attribute <= ATTRIBUTE_EAP_BASE+256) ||
255                    (vp->attribute > ATTRIBUTE_EAP_SIM_BASE &&
256                     vp->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
257                 {
258                         *last = vpnext;
259                         pairbasicfree(vp);
260                 } else {
261                         last = &vp->next;
262                 }
263         }
264 }
265
266 /*
267  * we got an EAP-Request/Sim/Start message in a legal state.
268  *
269  * pick a supported version, put it into the reply, and insert a nonce.
270  */
271 static int process_eap_start(RADIUS_PACKET *req,
272                              RADIUS_PACKET *rep)
273 {
274         VALUE_PAIR *vp, *newvp;
275         VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
276         uint16_t *versions, selectedversion;
277         unsigned int i,versioncount;
278
279         /* form new response clear of any EAP stuff */
280         cleanresp(rep);
281
282         if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST, 0)) == NULL) {
283                 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
284                 return 0;
285         }
286
287         versions = (uint16_t *)vp->vp_strvalue;
288
289         /* verify that the attribute length is big enough for a length field */
290         if(vp->length < 4)
291         {
292                 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %u\n", (unsigned int) vp->length);
293                 return 0;
294         }
295
296         versioncount = ntohs(versions[0])/2;
297         /* verify that the attribute length is big enough for the given number
298          * of versions present.
299          */
300         if((unsigned)vp->length <= (versioncount*2 + 2))
301         {
302                 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %u bytes\n", versioncount, (unsigned int) vp->length);
303                 return 0;
304         }
305
306         /*
307          * record the versionlist for the MK calculation.
308          */
309         eapsim_mk.versionlistlen = versioncount*2;
310         memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
311                eapsim_mk.versionlistlen);
312
313         /* walk the version list, and pick the one we support, which
314          * at present, is 1, EAP_SIM_VERSION.
315          */
316         selectedversion=0;
317         for(i=0; i < versioncount; i++)
318         {
319                 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
320                 {
321                         selectedversion=EAP_SIM_VERSION;
322                         break;
323                 }
324         }
325         if(selectedversion == 0)
326         {
327                 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
328                 for(i=0; i < versioncount; i++)
329                 {
330                         fprintf(stderr, "\tfound version %d\n",
331                                 ntohs(versions[i+1]));
332                 }
333         }
334
335         /*
336          * now make sure that we have only FULLAUTH_ID_REQ.
337          * I think that it actually might not matter - we can answer in
338          * anyway we like, but it is illegal to have more than one
339          * present.
340          */
341         anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ, 0);
342         fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ, 0);
343         permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ, 0);
344
345         if(fullauthidreq_vp == NULL ||
346            anyidreq_vp != NULL ||
347            permanentidreq_vp != NULL) {
348                 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
349                         (anyidreq_vp != NULL ? "a " : "no "),
350                         (fullauthidreq_vp != NULL ? "a " : "no "),
351                         (permanentidreq_vp != NULL ? "a " : "no "));
352                 return 0;
353         }
354
355         /* okay, we have just any_id_req there, so fill in response */
356
357         /* mark the subtype as being EAP-SIM/Response/Start */
358         newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, 0, PW_TYPE_INTEGER);
359         newvp->vp_integer = eapsim_start;
360         pairreplace(&(rep->vps), newvp);
361
362         /* insert selected version into response. */
363         newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
364                            0, PW_TYPE_OCTETS);
365         versions = (uint16_t *)newvp->vp_strvalue;
366         versions[0] = htons(selectedversion);
367         newvp->length = 2;
368         pairreplace(&(rep->vps), newvp);
369
370         /* record the selected version */
371         memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
372
373         vp = newvp = NULL;
374
375         {
376                 uint32_t nonce[4];
377                 /*
378                  * insert a nonce_mt that we make up.
379                  */
380                 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
381                                    0, PW_TYPE_OCTETS);
382                 newvp->vp_octets[0]=0;
383                 newvp->vp_octets[1]=0;
384                 newvp->length = 18;  /* 16 bytes of nonce + padding */
385
386                 nonce[0]=fr_rand();
387                 nonce[1]=fr_rand();
388                 nonce[2]=fr_rand();
389                 nonce[3]=fr_rand();
390                 memcpy(&newvp->vp_octets[2], nonce, 16);
391                 pairreplace(&(rep->vps), newvp);
392
393                 /* also keep a copy of the nonce! */
394                 memcpy(eapsim_mk.nonce_mt, nonce, 16);
395         }
396
397         {
398                 uint16_t *pidlen, idlen;
399
400                 /*
401                  * insert the identity here.
402                  */
403                 vp = pairfind(rep->vps, PW_USER_NAME, 0);
404                 if(vp == NULL)
405                 {
406                         fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
407                         return 0;
408                 }
409                 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
410                                    0, PW_TYPE_OCTETS);
411                 idlen = strlen(vp->vp_strvalue);
412                 pidlen = (uint16_t *)newvp->vp_strvalue;
413                 *pidlen = htons(idlen);
414                 newvp->length = idlen + 2;
415
416                 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
417                 pairreplace(&(rep->vps), newvp);
418
419                 /* record it */
420                 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
421                 eapsim_mk.identitylen = idlen;
422         }
423
424         return 1;
425 }
426
427 /*
428  * we got an EAP-Request/Sim/Challenge message in a legal state.
429  *
430  * use the RAND challenge to produce the SRES result, and then
431  * use that to generate a new MAC.
432  *
433  * for the moment, we ignore the RANDs, then just plug in the SRES
434  * values.
435  *
436  */
437 static int process_eap_challenge(RADIUS_PACKET *req,
438                                  RADIUS_PACKET *rep)
439 {
440         VALUE_PAIR *newvp;
441         VALUE_PAIR *mac, *randvp;
442         VALUE_PAIR *sres1,*sres2,*sres3;
443         VALUE_PAIR *Kc1, *Kc2, *Kc3;
444         uint8_t calcmac[20];
445
446         /* look for the AT_MAC and the challenge data */
447         mac   = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0);
448         randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND, 0);
449         if(mac == NULL || randvp == NULL) {
450                 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
451                 return 0;
452         }
453
454         /*
455          * compare RAND with randX, to verify this is the right response
456          * to this challenge.
457          */
458         {
459           VALUE_PAIR *randcfgvp[3];
460           uint8_t *randcfg[3];
461
462           randcfg[0] = &randvp->vp_octets[2];
463           randcfg[1] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE];
464           randcfg[2] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE*2];
465
466           randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1, 0);
467           randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2, 0);
468           randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3, 0);
469
470           if(randcfgvp[0] == NULL ||
471              randcfgvp[1] == NULL ||
472              randcfgvp[2] == NULL) {
473             fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
474             return 0;
475           }
476
477           if(memcmp(randcfg[0], randcfgvp[0]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
478              memcmp(randcfg[1], randcfgvp[1]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
479              memcmp(randcfg[2], randcfgvp[2]->vp_octets, EAPSIM_RAND_SIZE)!=0) {
480             int rnum,i,j;
481
482             fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
483             for(rnum = 0; rnum < 3; rnum++) {
484               fprintf(stderr, "received   rand %d: ", rnum);
485               j=0;
486               for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
487                 if(j==4) {
488                   printf("_");
489                   j=0;
490                 }
491                 j++;
492
493                 fprintf(stderr, "%02x", randcfg[rnum][i]);
494               }
495               fprintf(stderr, "\nconfigured rand %d: ", rnum);
496               j=0;
497               for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
498                 if(j==4) {
499                   printf("_");
500                   j=0;
501                 }
502                 j++;
503
504                 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
505               }
506               fprintf(stderr, "\n");
507             }
508             return 0;
509           }
510         }
511
512         /*
513          * now dig up the sres values from the response packet,
514          * which were put there when we read things in.
515          *
516          * Really, they should be calculated from the RAND!
517          *
518          */
519         sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1, 0);
520         sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2, 0);
521         sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3, 0);
522
523         if(sres1 == NULL ||
524            sres2 == NULL ||
525            sres3 == NULL) {
526                 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
527                 return 0;
528         }
529         memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
530         memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
531         memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
532
533         Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1, 0);
534         Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2, 0);
535         Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3, 0);
536
537         if(Kc1 == NULL ||
538            Kc2 == NULL ||
539            Kc3 == NULL) {
540                 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
541                 return 0;
542         }
543         memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
544         memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
545         memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
546
547         /* all set, calculate keys */
548         eapsim_calculate_keys(&eapsim_mk);
549
550         if(debug_flag) {
551           eapsim_dump_mk(&eapsim_mk);
552         }
553
554         /* verify the MAC, now that we have all the keys. */
555         if(eapsim_checkmac(req->vps, eapsim_mk.K_aut,
556                            eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
557                            calcmac)) {
558                 printf("MAC check succeed\n");
559         } else {
560                 int i, j;
561                 j=0;
562                 printf("calculated MAC (");
563                 for (i = 0; i < 20; i++) {
564                         if(j==4) {
565                                 printf("_");
566                                 j=0;
567                         }
568                         j++;
569
570                         printf("%02x", calcmac[i]);
571                 }
572                 printf(" did not match\n");
573                 return 0;
574         }
575
576         /* form new response clear of any EAP stuff */
577         cleanresp(rep);
578
579         /* mark the subtype as being EAP-SIM/Response/Start */
580         newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, 0, PW_TYPE_INTEGER);
581         newvp->vp_integer = eapsim_challenge;
582         pairreplace(&(rep->vps), newvp);
583
584         /*
585          * fill the SIM_MAC with a field that will in fact get appended
586          * to the packet before the MAC is calculated
587          */
588         newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0,
589                            PW_TYPE_OCTETS);
590         memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
591         memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
592         memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
593         newvp->length = EAPSIM_SRES_SIZE*3;
594         pairreplace(&(rep->vps), newvp);
595
596         newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, 0, PW_TYPE_OCTETS);
597         memcpy(newvp->vp_strvalue,    eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
598         newvp->length = EAPSIM_AUTH_SIZE;
599         pairreplace(&(rep->vps), newvp);
600
601         return 1;
602 }
603
604 /*
605  * this code runs the EAP-SIM client state machine.
606  * the *request* is from the server.
607  * the *reponse* is to the server.
608  *
609  */
610 static int respond_eap_sim(RADIUS_PACKET *req,
611                            RADIUS_PACKET *resp)
612 {
613         enum eapsim_clientstates state, newstate;
614         enum eapsim_subtype subtype;
615         VALUE_PAIR *vp, *statevp, *radstate, *eapid;
616         char statenamebuf[32], subtypenamebuf[32];
617
618         if ((radstate = paircopy2(req->vps, PW_STATE, 0)) == NULL)
619         {
620                 return 0;
621         }
622
623         if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID, 0)) == NULL)
624         {
625                 return 0;
626         }
627
628         /* first, dig up the state from the request packet, setting
629          * outselves to be in EAP-SIM-Start state if there is none.
630          */
631
632         if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE, 0)) == NULL)
633         {
634                 /* must be initial request */
635                 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, 0, PW_TYPE_INTEGER);
636                 statevp->vp_integer = eapsim_client_init;
637                 pairreplace(&(resp->vps), statevp);
638         }
639         state = statevp->vp_integer;
640
641         /*
642          * map the attributes, and authenticate them.
643          */
644         unmap_eapsim_types(req);
645
646         printf("<+++ EAP-sim decoded packet:\n");
647         vp_printlist(stdout, req->vps);
648
649         if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE, 0)) == NULL)
650         {
651                 return 0;
652         }
653         subtype = vp->vp_integer;
654
655         /*
656          * look for the appropriate state, and process incoming message
657          */
658         switch(state) {
659         case eapsim_client_init:
660                 switch(subtype) {
661                 case eapsim_start:
662                         newstate = process_eap_start(req, resp);
663                         break;
664
665                 case eapsim_challenge:
666                 case eapsim_notification:
667                 case eapsim_reauth:
668                 default:
669                         fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
670                                 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
671                                 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
672                         /* invalid state, drop message */
673                         return 0;
674                 }
675                 break;
676
677         case eapsim_client_start:
678                 switch(subtype) {
679                 case eapsim_start:
680                         /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
681                         newstate = process_eap_start(req, resp);
682                         break;
683
684                 case eapsim_challenge:
685                         newstate = process_eap_challenge(req, resp);
686                         break;
687
688                 default:
689                         fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
690                                 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
691                                 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
692                         /* invalid state, drop message */
693                         return 0;
694                 }
695                 break;
696
697
698         default:
699                 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
700                         sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
701                 return 0;
702         }
703
704         /* copy the eap state object in */
705         pairreplace(&(resp->vps), eapid);
706
707         /* update stete info, and send new packet */
708         map_eapsim_types(resp);
709
710         /* copy the radius state object in */
711         pairreplace(&(resp->vps), radstate);
712
713         statevp->vp_integer = newstate;
714         return 1;
715 }
716
717 static int respond_eap_md5(RADIUS_PACKET *req,
718                            RADIUS_PACKET *rep)
719 {
720         VALUE_PAIR *vp, *id, *state;
721         size_t valuesize, namesize;
722         uint8_t identifier;
723         uint8_t *value;
724         uint8_t *name;
725         FR_MD5_CTX      context;
726         uint8_t    response[16];
727
728         cleanresp(rep);
729
730         if ((state = paircopy2(req->vps, PW_STATE, 0)) == NULL)
731         {
732                 fprintf(stderr, "radeapclient: no state attribute found\n");
733                 return 0;
734         }
735
736         if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID, 0)) == NULL)
737         {
738                 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
739                 return 0;
740         }
741         identifier = id->vp_integer;
742
743         if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0)) == NULL)
744         {
745                 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
746                 return 0;
747         }
748
749         /* got the details of the MD5 challenge */
750         valuesize = vp->vp_octets[0];
751         value = &vp->vp_octets[1];
752         name  = &vp->vp_octets[valuesize+1];
753         namesize = vp->length - (valuesize + 1);
754
755         /* sanitize items */
756         if(valuesize > vp->length)
757         {
758                 fprintf(stderr, "radeapclient: md5 valuesize if too big (%u > %u)\n",
759                         (unsigned int) valuesize, (unsigned int) vp->length);
760                 return 0;
761         }
762
763         /* now do the CHAP operation ourself, rather than build the
764          * buffer. We could also call rad_chap_encode, but it wants
765          * a CHAP-Challenge, which we don't want to bother with.
766          */
767         fr_MD5Init(&context);
768         fr_MD5Update(&context, &identifier, 1);
769         fr_MD5Update(&context, (uint8_t *) password, strlen(password));
770         fr_MD5Update(&context, value, valuesize);
771         fr_MD5Final(response, &context);
772
773         vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0, PW_TYPE_OCTETS);
774         vp->vp_octets[0]=16;
775         memcpy(&vp->vp_strvalue[1], response, 16);
776         vp->length = 17;
777
778         pairreplace(&(rep->vps), vp);
779
780         pairreplace(&(rep->vps), id);
781
782         /* copy the state object in */
783         pairreplace(&(rep->vps), state);
784
785         return 1;
786 }
787
788
789
790 static int sendrecv_eap(RADIUS_PACKET *rep)
791 {
792         RADIUS_PACKET *req = NULL;
793         VALUE_PAIR *vp, *vpnext;
794         int tried_eap_md5 = 0;
795
796         /*
797          *      Keep a copy of the the User-Password attribute.
798          */
799         if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0)) != NULL) {
800                 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
801
802         } else  if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0)) != NULL) {
803                 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
804                 /*
805                  *      Otherwise keep a copy of the CHAP-Password attribute.
806                  */
807         } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0)) != NULL) {
808                 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
809         } else {
810                 *password = '\0';
811         }
812
813  again:
814         rep->id++;
815
816         printf("\n+++> About to send encoded packet:\n");
817         vp_printlist(stdout, rep->vps);
818
819         /*
820          * if there are EAP types, encode them into an EAP-Message
821          *
822          */
823         map_eap_types(rep);
824
825         /*
826          *  Fix up Digest-Attributes issues
827          */
828         for (vp = rep->vps; vp != NULL; vp = vp->next) {
829                 switch (vp->attribute) {
830                 default:
831                         break;
832
833                 case PW_DIGEST_REALM:
834                 case PW_DIGEST_NONCE:
835                 case PW_DIGEST_METHOD:
836                 case PW_DIGEST_URI:
837                 case PW_DIGEST_QOP:
838                 case PW_DIGEST_ALGORITHM:
839                 case PW_DIGEST_BODY_DIGEST:
840                 case PW_DIGEST_CNONCE:
841                 case PW_DIGEST_NONCE_COUNT:
842                 case PW_DIGEST_USER_NAME:
843                         /* overlapping! */
844                         memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
845                         vp->vp_octets[0] = vp->attribute - PW_DIGEST_REALM + 1;
846                         vp->length += 2;
847                         vp->vp_octets[1] = vp->length;
848                         vp->attribute = PW_DIGEST_ATTRIBUTES;
849                         break;
850                 }
851         }
852
853         /*
854          *      If we've already sent a packet, free up the old
855          *      one, and ensure that the next packet has a unique
856          *      ID and authentication vector.
857          */
858         if (rep->data) {
859                 free(rep->data);
860                 rep->data = NULL;
861         }
862
863         fr_md5_calc(rep->vector, rep->vector,
864                         sizeof(rep->vector));
865
866         if (*password != '\0') {
867                 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0)) != NULL) {
868                         strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
869                         vp->length = strlen(password);
870
871                 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0)) != NULL) {
872                         strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
873                         vp->length = strlen(password);
874
875                 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0)) != NULL) {
876                         strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
877                         vp->length = strlen(password);
878
879                         rad_chap_encode(rep, vp->vp_octets, rep->id, vp);
880                         vp->length = 17;
881                 }
882         } /* there WAS a password */
883
884         /* send the response, wait for the next request */
885         send_packet(rep, &req);
886
887         /* okay got back the packet, go and decode the EAP-Message. */
888         unmap_eap_types(req);
889
890         printf("<+++ EAP decoded packet:\n");
891         vp_printlist(stdout, req->vps);
892
893         /* now look for the code type. */
894         for (vp = req->vps; vp != NULL; vp = vpnext) {
895                 vpnext = vp->next;
896
897                 switch (vp->attribute) {
898                 default:
899                         break;
900
901                 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
902                         if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
903                         {
904                                 tried_eap_md5++;
905                                 goto again;
906                         }
907                         break;
908
909                 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
910                         if(respond_eap_sim(req, rep))
911                         {
912                                 goto again;
913                         }
914                         break;
915                 }
916         }
917
918         return 1;
919 }
920
921
922 int main(int argc, char **argv)
923 {
924         RADIUS_PACKET *req;
925         char *p;
926         int c;
927         int port = 0;
928         char *filename = NULL;
929         FILE *fp;
930         int count = 1;
931         int id;
932
933         id = ((int)getpid() & 0xff);
934         fr_debug_flag = 0;
935
936         radlog_dest = RADLOG_STDERR;
937
938         while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
939         {
940                 switch(c) {
941                 case 'c':
942                         if (!isdigit((int) *optarg))
943                                 usage();
944                         count = atoi(optarg);
945                         break;
946                 case 'd':
947                         radius_dir = optarg;
948                         break;
949                 case 'f':
950                         filename = optarg;
951                         break;
952                 case 'q':
953                         do_output = 0;
954                         break;
955                 case 'x':
956                         debug_flag++;
957                         fr_debug_flag++;
958                         break;
959
960                 case 'X':
961 #if 0
962                   sha1_data_problems = 1; /* for debugging only */
963 #endif
964                   break;
965
966
967
968                 case 'r':
969                         if (!isdigit((int) *optarg))
970                                 usage();
971                         retries = atoi(optarg);
972                         break;
973                 case 'i':
974                         if (!isdigit((int) *optarg))
975                                 usage();
976                         id = atoi(optarg);
977                         if ((id < 0) || (id > 255)) {
978                                 usage();
979                         }
980                         break;
981                 case 's':
982                         do_summary = 1;
983                         break;
984                 case 't':
985                         if (!isdigit((int) *optarg))
986                                 usage();
987                         timeout = atof(optarg);
988                         break;
989                 case 'v':
990                         printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
991                         exit(0);
992                         break;
993                case 'S':
994                        fp = fopen(optarg, "r");
995                        if (!fp) {
996                                fprintf(stderr, "radclient: Error opening %s: %s\n",
997                                        optarg, strerror(errno));
998                                exit(1);
999                        }
1000                        if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1001                                fprintf(stderr, "radclient: Error reading %s: %s\n",
1002                                        optarg, strerror(errno));
1003                                exit(1);
1004                        }
1005                        fclose(fp);
1006
1007                        /* truncate newline */
1008                        p = filesecret + strlen(filesecret) - 1;
1009                        while ((p >= filesecret) &&
1010                               (*p < ' ')) {
1011                                *p = '\0';
1012                                --p;
1013                        }
1014
1015                        if (strlen(filesecret) < 2) {
1016                                fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1017                                exit(1);
1018                        }
1019                        secret = filesecret;
1020                        break;
1021                 case 'h':
1022                 default:
1023                         usage();
1024                         break;
1025                 }
1026         }
1027         argc -= (optind - 1);
1028         argv += (optind - 1);
1029
1030         if ((argc < 3)  ||
1031             ((secret == NULL) && (argc < 4))) {
1032                 usage();
1033         }
1034
1035         if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1036                 fr_perror("radclient");
1037                 return 1;
1038         }
1039
1040         if ((req = rad_alloc(1)) == NULL) {
1041                 fr_perror("radclient");
1042                 exit(1);
1043         }
1044
1045 #if 0
1046         {
1047                 FILE *randinit;
1048
1049                 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1050                 {
1051                         perror("/dev/urandom");
1052                 } else {
1053                         fread(randctx.randrsl, 256, 1, randinit);
1054                         fclose(randinit);
1055                 }
1056         }
1057         fr_randinit(&randctx, 1);
1058 #endif
1059
1060         req->id = id;
1061
1062         /*
1063          *      Strip port from hostname if needed.
1064          */
1065         if ((p = strchr(argv[1], ':')) != NULL) {
1066                 *p++ = 0;
1067                 port = atoi(p);
1068         }
1069
1070         /*
1071          *      See what kind of request we want to send.
1072          */
1073         if (strcmp(argv[2], "auth") == 0) {
1074                 if (port == 0) port = getport("radius");
1075                 if (port == 0) port = PW_AUTH_UDP_PORT;
1076                 req->code = PW_AUTHENTICATION_REQUEST;
1077
1078         } else if (strcmp(argv[2], "acct") == 0) {
1079                 if (port == 0) port = getport("radacct");
1080                 if (port == 0) port = PW_ACCT_UDP_PORT;
1081                 req->code = PW_ACCOUNTING_REQUEST;
1082                 do_summary = 0;
1083
1084         } else if (strcmp(argv[2], "status") == 0) {
1085                 if (port == 0) port = getport("radius");
1086                 if (port == 0) port = PW_AUTH_UDP_PORT;
1087                 req->code = PW_STATUS_SERVER;
1088
1089         } else if (strcmp(argv[2], "disconnect") == 0) {
1090                 if (port == 0) port = PW_POD_UDP_PORT;
1091                 req->code = PW_DISCONNECT_REQUEST;
1092
1093         } else if (isdigit((int) argv[2][0])) {
1094                 if (port == 0) port = getport("radius");
1095                 if (port == 0) port = PW_AUTH_UDP_PORT;
1096                 req->code = atoi(argv[2]);
1097         } else {
1098                 usage();
1099         }
1100
1101         /*
1102          *      Resolve hostname.
1103          */
1104         req->dst_port = port;
1105         if (ip_hton(argv[1], AF_INET, &req->dst_ipaddr) < 0) {
1106                 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
1107                 exit(1);
1108         }
1109
1110         /*
1111          *      Add the secret.
1112          */
1113         if (argv[3]) secret = argv[3];
1114
1115         /*
1116          *      Read valuepairs.
1117          *      Maybe read them, from stdin, if there's no
1118          *      filename, or if the filename is '-'.
1119          */
1120         if (filename && (strcmp(filename, "-") != 0)) {
1121                 fp = fopen(filename, "r");
1122                 if (!fp) {
1123                         fprintf(stderr, "radclient: Error opening %s: %s\n",
1124                                 filename, strerror(errno));
1125                         exit(1);
1126                 }
1127         } else {
1128                 fp = stdin;
1129         }
1130
1131         /*
1132          *      Send request.
1133          */
1134         if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1135                 perror("radclient: socket: ");
1136                 exit(1);
1137         }
1138
1139         while(!filedone) {
1140                 if(req->vps) pairfree(&req->vps);
1141
1142                 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1143                     == NULL) {
1144                         break;
1145                 }
1146
1147                 sendrecv_eap(req);
1148         }
1149
1150         if(do_summary) {
1151                 printf("\n\t   Total approved auths:  %d\n", totalapp);
1152                 printf("\t     Total denied auths:  %d\n", totaldeny);
1153         }
1154         return 0;
1155 }
1156
1157 /*
1158  * given a radius request with some attributes in the EAP range, build
1159  * them all into a single EAP-Message body.
1160  *
1161  * Note that this function will build multiple EAP-Message bodies
1162  * if there are multiple eligible EAP-types. This is incorrect, as the
1163  * recipient will in fact concatenate them.
1164  *
1165  * XXX - we could break the loop once we process one type. Maybe this
1166  *       just deserves an assert?
1167  *
1168  */
1169 static void map_eap_types(RADIUS_PACKET *req)
1170 {
1171         VALUE_PAIR *vp, *vpnext;
1172         int id, eapcode;
1173         EAP_PACKET ep;
1174         int eap_type;
1175
1176         vp = pairfind(req->vps, ATTRIBUTE_EAP_ID, 0);
1177         if(vp == NULL) {
1178                 id = ((int)getpid() & 0xff);
1179         } else {
1180                 id = vp->vp_integer;
1181         }
1182
1183         vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE, 0);
1184         if(vp == NULL) {
1185                 eapcode = PW_EAP_REQUEST;
1186         } else {
1187                 eapcode = vp->vp_integer;
1188         }
1189
1190
1191         for(vp = req->vps; vp != NULL; vp = vpnext) {
1192                 /* save it in case it changes! */
1193                 vpnext = vp->next;
1194
1195                 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
1196                    vp->attribute < ATTRIBUTE_EAP_BASE+256) {
1197                         break;
1198                 }
1199         }
1200
1201         if(vp == NULL) {
1202                 return;
1203         }
1204
1205         eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
1206
1207         switch(eap_type) {
1208         case PW_EAP_IDENTITY:
1209         case PW_EAP_NOTIFICATION:
1210         case PW_EAP_NAK:
1211         case PW_EAP_MD5:
1212         case PW_EAP_OTP:
1213         case PW_EAP_GTC:
1214         case PW_EAP_TLS:
1215         case PW_EAP_LEAP:
1216         case PW_EAP_TTLS:
1217         case PW_EAP_PEAP:
1218         default:
1219                 /*
1220                  * no known special handling, it is just encoded as an
1221                  * EAP-message with the given type.
1222                  */
1223
1224                 /* nuke any existing EAP-Messages */
1225                 pairdelete(&req->vps, PW_EAP_MESSAGE, 0);
1226
1227                 memset(&ep, 0, sizeof(ep));
1228                 ep.code = eapcode;
1229                 ep.id   = id;
1230                 ep.type.type = eap_type;
1231                 ep.type.length = vp->length;
1232                 ep.type.data = malloc(vp->length);
1233                 memcpy(ep.type.data,vp->vp_octets, vp->length);
1234                 eap_basic_compose(req, &ep);
1235         }
1236 }
1237
1238 /*
1239  * given a radius request with an EAP-Message body, decode it specific
1240  * attributes.
1241  */
1242 static void unmap_eap_types(RADIUS_PACKET *rep)
1243 {
1244         VALUE_PAIR *eap1;
1245         eap_packet_t *e;
1246         int len;
1247         int type;
1248
1249         /* find eap message */
1250         e = eap_vp2packet(rep->vps);
1251
1252         /* nothing to do! */
1253         if(e == NULL) return;
1254
1255         /* create EAP-ID and EAP-CODE attributes to start */
1256         eap1 = paircreate(ATTRIBUTE_EAP_ID, 0, PW_TYPE_INTEGER);
1257         eap1->vp_integer = e->id;
1258         pairadd(&(rep->vps), eap1);
1259
1260         eap1 = paircreate(ATTRIBUTE_EAP_CODE, 0, PW_TYPE_INTEGER);
1261         eap1->vp_integer = e->code;
1262         pairadd(&(rep->vps), eap1);
1263
1264         switch(e->code)
1265         {
1266         default:
1267         case PW_EAP_SUCCESS:
1268         case PW_EAP_FAILURE:
1269                 /* no data */
1270                 break;
1271
1272         case PW_EAP_REQUEST:
1273         case PW_EAP_RESPONSE:
1274                 /* there is a type field, which we use to create
1275                  * a new attribute */
1276
1277                 /* the length was decode already into the attribute
1278                  * length, and was checked already. Network byte
1279                  * order, just pull it out using math.
1280                  */
1281                 len = e->length[0]*256 + e->length[1];
1282
1283                 /* verify the length is big enough to hold type */
1284                 if(len < 5)
1285                 {
1286                         free(e);
1287                         return;
1288                 }
1289
1290                 type = e->data[0];
1291
1292                 type += ATTRIBUTE_EAP_BASE;
1293                 len -= 5;
1294
1295                 if(len > MAX_STRING_LEN) {
1296                         len = MAX_STRING_LEN;
1297                 }
1298
1299                 eap1 = paircreate(type, 0, PW_TYPE_OCTETS);
1300                 memcpy(eap1->vp_strvalue, &e->data[1], len);
1301                 eap1->length = len;
1302                 pairadd(&(rep->vps), eap1);
1303                 break;
1304         }
1305
1306         free(e);
1307         return;
1308 }
1309
1310 static int map_eapsim_types(RADIUS_PACKET *r)
1311 {
1312         EAP_PACKET ep;
1313         int ret;
1314
1315         memset(&ep, 0, sizeof(ep));
1316         ret = map_eapsim_basictypes(r, &ep);
1317         if(ret != 1) {
1318                 return ret;
1319         }
1320         eap_basic_compose(r, &ep);
1321
1322         return 1;
1323 }
1324
1325 static int unmap_eapsim_types(RADIUS_PACKET *r)
1326 {
1327         VALUE_PAIR             *esvp;
1328
1329         esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM, 0);
1330         if (esvp == NULL) {
1331                 radlog(L_ERR, "eap: EAP-Sim attribute not found");
1332                 return 0;
1333         }
1334
1335         return unmap_eapsim_basictypes(r, esvp->vp_octets, esvp->length);
1336 }
1337
1338 #ifdef TEST_CASE
1339
1340 #include <assert.h>
1341
1342 const char *radius_dir = RADDBDIR;
1343
1344 int radlog(int lvl, const char *msg, ...)
1345 {
1346         va_list ap;
1347         int r;
1348
1349         va_start(ap, msg);
1350         r = vfprintf(stderr, msg, ap);
1351         va_end(ap);
1352         fputc('\n', stderr);
1353
1354         return r;
1355 }
1356
1357 main(int argc, char *argv[])
1358 {
1359         int filedone;
1360         RADIUS_PACKET *req,*req2;
1361         VALUE_PAIR *vp, *vpkey, *vpextra;
1362         extern unsigned int sha1_data_problems;
1363
1364         req = NULL;
1365         req2 = NULL;
1366         filedone = 0;
1367
1368         if(argc>1) {
1369           sha1_data_problems = 1;
1370         }
1371
1372         if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1373                 fr_perror("radclient");
1374                 return 1;
1375         }
1376
1377         if ((req = rad_alloc(1)) == NULL) {
1378                 fr_perror("radclient");
1379                 exit(1);
1380         }
1381
1382         if ((req2 = rad_alloc(1)) == NULL) {
1383                 fr_perror("radclient");
1384                 exit(1);
1385         }
1386
1387         while(!filedone) {
1388                 if(req->vps) pairfree(&req->vps);
1389                 if(req2->vps) pairfree(&req2->vps);
1390
1391                 if ((req->vps = readvp2(stdin, &filedone, "eapsimlib:")) == NULL) {
1392                         break;
1393                 }
1394
1395                 printf("\nRead:\n");
1396                 vp_printlist(stdout, req->vps);
1397
1398                 map_eapsim_types(req);
1399                 map_eap_types(req);
1400                 printf("Mapped to:\n");
1401                 vp_printlist(stdout, req->vps);
1402
1403                 /* find the EAP-Message, copy it to req2 */
1404                 vp = paircopy2(req->vps, PW_EAP_MESSAGE);
1405
1406                 if(vp == NULL) continue;
1407
1408                 pairadd(&req2->vps, vp);
1409
1410                 /* only call unmap for sim types here */
1411                 unmap_eap_types(req2);
1412                 unmap_eapsim_types(req2);
1413
1414                 printf("Unmapped to:\n");
1415                 vp_printlist(stdout, req2->vps);
1416
1417                 vp = pairfind(req2->vps,
1418                               ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
1419                 vpkey   = pairfind(req->vps, ATTRIBUTE_EAP_SIM_KEY);
1420                 vpextra = pairfind(req->vps, ATTRIBUTE_EAP_SIM_EXTRA);
1421
1422                 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1423                         uint8_t calcmac[16];
1424
1425                         /* find the EAP-Message, copy it to req2 */
1426
1427                         memset(calcmac, 0, sizeof(calcmac));
1428                         printf("Confirming MAC...");
1429                         if(eapsim_checkmac(req2->vps, vpkey->vp_strvalue,
1430                                            vpextra->vp_strvalue, vpextra->length,
1431                                            calcmac)) {
1432                                 printf("succeed\n");
1433                         } else {
1434                                 int i, j;
1435
1436                                 printf("calculated MAC (");
1437                                 for (i = 0; i < 20; i++) {
1438                                         if(j==4) {
1439                                                 printf("_");
1440                                                 j=0;
1441                                         }
1442                                         j++;
1443
1444                                         printf("%02x", calcmac[i]);
1445                                 }
1446                                 printf(" did not match\n");
1447                         }
1448                 }
1449
1450                 fflush(stdout);
1451         }
1452 }
1453 #endif
1454