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