2 * radeapclient.c EAP specific radius packet debug tool.
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.
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.
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
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>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/libradius.h>
36 #include <freeradius-devel/conf.h>
37 #include <freeradius-devel/radpaths.h>
38 #include <freeradius-devel/md5.h>
40 #include "eap_types.h"
43 extern int sha1_data_problems;
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; */
59 radlog_dest_t radlog_dest = RADLOG_STDERR;
60 const char *radlog_dir = NULL;
64 struct eapsim_keys eapsim_mk;
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);
71 static void NEVER_RETURNS usage(void)
73 fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
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");
91 int radlog(int lvl, const char *msg, ...)
96 r = lvl; /* shut up compiler */
99 r = vfprintf(stderr, msg, ap);
106 int log_debug(const char *msg, ...)
112 r = vfprintf(stderr, msg, ap);
119 static int getport(const char *name)
123 svp = getservbyname (name, "udp");
128 return ntohs(svp->s_port);
131 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
136 for (i = 0; i < retries; i++) {
139 rad_send(req, NULL, secret);
141 /* And wait for reply, timing out as necessary */
143 FD_SET(req->sockfd, &rdfdesc);
145 tv.tv_sec = (int)timeout;
146 tv.tv_usec = 1000000 * (timeout - (int) timeout);
148 /* Something's wrong if we don't get exactly one fd. */
149 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
153 *rep = rad_recv(req->sockfd);
156 * If we get a response from a machine
157 * which we did NOT send a request to,
160 if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
161 (memcmp(&(*rep)->src_ipaddr.ipaddr,
162 &req->dst_ipaddr.ipaddr,
163 ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
164 sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
165 sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
166 ((*rep)->src_port != req->dst_port)) {
167 char src[128], dst[128];
169 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
170 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
171 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
173 src, (*rep)->src_port);
177 } else { /* NULL: couldn't receive the packet */
178 librad_perror("radclient:");
183 /* No response or no data read (?) */
185 fprintf(stderr, "radclient: no response from server\n");
190 * FIXME: Discard the packet & listen for another.
192 * Hmm... we should really be using eapol_test, which does
193 * a lot more than radeapclient.
195 if (rad_verify(*rep, req, secret) != 0) {
196 librad_perror("rad_verify");
200 if (rad_decode(*rep, req, secret) != 0) {
201 librad_perror("rad_decode");
205 /* libradius debug already prints out the value pairs for us */
206 if (!librad_debug && do_output) {
207 printf("Received response ID %d, code %d, length = %d\n",
208 (*rep)->id, (*rep)->code, (*rep)->data_len);
209 vp_printlist(stdout, (*rep)->vps);
211 if((*rep)->code == PW_AUTHENTICATION_ACK) {
220 static void cleanresp(RADIUS_PACKET *resp)
222 VALUE_PAIR *vpnext, *vp, **last;
226 * maybe should just copy things we care about, or keep
227 * a copy of the original input and start from there again?
229 pairdelete(&resp->vps, PW_EAP_MESSAGE);
230 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY);
233 for(vp = *last; vp != NULL; vp = vpnext)
237 if((vp->attribute > ATTRIBUTE_EAP_BASE &&
238 vp->attribute <= ATTRIBUTE_EAP_BASE+256) ||
239 (vp->attribute > ATTRIBUTE_EAP_SIM_BASE &&
240 vp->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
251 * we got an EAP-Request/Sim/Start message in a legal state.
253 * pick a supported version, put it into the reply, and insert a nonce.
255 static int process_eap_start(RADIUS_PACKET *req,
258 VALUE_PAIR *vp, *newvp;
259 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
260 uint16_t *versions, selectedversion;
261 unsigned int i,versioncount;
263 /* form new response clear of any EAP stuff */
266 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST)) == NULL) {
267 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
271 versions = (uint16_t *)vp->vp_strvalue;
273 /* verify that the attribute length is big enough for a length field */
276 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %d\n", vp->length);
280 versioncount = ntohs(versions[0])/2;
281 /* verify that the attribute length is big enough for the given number
282 * of versions present.
284 if((unsigned)vp->length <= (versioncount*2 + 2))
286 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %d bytes\n", versioncount, vp->length);
291 * record the versionlist for the MK calculation.
293 eapsim_mk.versionlistlen = versioncount*2;
294 memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
295 eapsim_mk.versionlistlen);
297 /* walk the version list, and pick the one we support, which
298 * at present, is 1, EAP_SIM_VERSION.
301 for(i=0; i < versioncount; i++)
303 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
305 selectedversion=EAP_SIM_VERSION;
309 if(selectedversion == 0)
311 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
312 for(i=0; i < versioncount; i++)
314 fprintf(stderr, "\tfound version %d\n",
315 ntohs(versions[i+1]));
320 * now make sure that we have only FULLAUTH_ID_REQ.
321 * I think that it actually might not matter - we can answer in
322 * anyway we like, but it is illegal to have more than one
325 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ);
326 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ);
327 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ);
329 if(fullauthidreq_vp == NULL ||
330 anyidreq_vp != NULL ||
331 permanentidreq_vp != NULL) {
332 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
333 (anyidreq_vp != NULL ? "a " : "no "),
334 (fullauthidreq_vp != NULL ? "a " : "no "),
335 (permanentidreq_vp != NULL ? "a " : "no "));
339 /* okay, we have just any_id_req there, so fill in response */
341 /* mark the subtype as being EAP-SIM/Response/Start */
342 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
343 newvp->vp_integer = eapsim_start;
344 pairreplace(&(rep->vps), newvp);
346 /* insert selected version into response. */
347 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
349 versions = (uint16_t *)newvp->vp_strvalue;
350 versions[0] = htons(selectedversion);
352 pairreplace(&(rep->vps), newvp);
354 /* record the selected version */
355 memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
362 * insert a nonce_mt that we make up.
364 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
366 newvp->vp_octets[0]=0;
367 newvp->vp_octets[1]=0;
368 newvp->length = 18; /* 16 bytes of nonce + padding */
374 memcpy(&newvp->vp_octets[2], nonce, 16);
375 pairreplace(&(rep->vps), newvp);
377 /* also keep a copy of the nonce! */
378 memcpy(eapsim_mk.nonce_mt, nonce, 16);
382 uint16_t *pidlen, idlen;
385 * insert the identity here.
387 vp = pairfind(rep->vps, PW_USER_NAME);
390 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
393 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
395 idlen = strlen(vp->vp_strvalue);
396 pidlen = (uint16_t *)newvp->vp_strvalue;
397 *pidlen = htons(idlen);
398 newvp->length = idlen + 2;
400 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
401 pairreplace(&(rep->vps), newvp);
404 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
405 eapsim_mk.identitylen = idlen;
412 * we got an EAP-Request/Sim/Challenge message in a legal state.
414 * use the RAND challenge to produce the SRES result, and then
415 * use that to generate a new MAC.
417 * for the moment, we ignore the RANDs, then just plug in the SRES
421 static int process_eap_challenge(RADIUS_PACKET *req,
425 VALUE_PAIR *mac, *randvp;
426 VALUE_PAIR *sres1,*sres2,*sres3;
427 VALUE_PAIR *Kc1, *Kc2, *Kc3;
430 /* look for the AT_MAC and the challenge data */
431 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
432 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND);
433 if(mac == NULL || randvp == NULL) {
434 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
439 * compare RAND with randX, to verify this is the right response
443 VALUE_PAIR *randcfgvp[3];
446 randcfg[0] = &randvp->vp_octets[2];
447 randcfg[1] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE];
448 randcfg[2] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE*2];
450 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1);
451 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2);
452 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3);
454 if(randcfgvp[0] == NULL ||
455 randcfgvp[1] == NULL ||
456 randcfgvp[2] == NULL) {
457 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
461 if(memcmp(randcfg[0], randcfgvp[0]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
462 memcmp(randcfg[1], randcfgvp[1]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
463 memcmp(randcfg[2], randcfgvp[2]->vp_octets, EAPSIM_RAND_SIZE)!=0) {
466 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
467 for(rnum = 0; rnum < 3; rnum++) {
468 fprintf(stderr, "received rand %d: ", rnum);
470 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
477 fprintf(stderr, "%02x", randcfg[rnum][i]);
479 fprintf(stderr, "\nconfigured rand %d: ", rnum);
481 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
488 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
490 fprintf(stderr, "\n");
497 * now dig up the sres values from the response packet,
498 * which were put there when we read things in.
500 * Really, they should be calculated from the RAND!
503 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1);
504 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2);
505 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3);
510 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
513 memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
514 memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
515 memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
517 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1);
518 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2);
519 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3);
524 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
527 memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
528 memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
529 memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
531 /* all set, calculate keys */
532 eapsim_calculate_keys(&eapsim_mk);
535 eapsim_dump_mk(&eapsim_mk);
538 /* verify the MAC, now that we have all the keys. */
539 if(eapsim_checkmac(req->vps, eapsim_mk.K_aut,
540 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
542 printf("MAC check succeed\n");
546 printf("calculated MAC (");
547 for (i = 0; i < 20; i++) {
554 printf("%02x", calcmac[i]);
556 printf(" did not match\n");
560 /* form new response clear of any EAP stuff */
563 /* mark the subtype as being EAP-SIM/Response/Start */
564 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
565 newvp->vp_integer = eapsim_challenge;
566 pairreplace(&(rep->vps), newvp);
569 * fill the SIM_MAC with a field that will in fact get appended
570 * to the packet before the MAC is calculated
572 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC,
574 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
575 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
576 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
577 newvp->length = EAPSIM_SRES_SIZE*3;
578 pairreplace(&(rep->vps), newvp);
580 newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, PW_TYPE_OCTETS);
581 memcpy(newvp->vp_strvalue, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
582 newvp->length = EAPSIM_AUTH_SIZE;
583 pairreplace(&(rep->vps), newvp);
589 * this code runs the EAP-SIM client state machine.
590 * the *request* is from the server.
591 * the *reponse* is to the server.
594 static int respond_eap_sim(RADIUS_PACKET *req,
597 enum eapsim_clientstates state, newstate;
598 enum eapsim_subtype subtype;
599 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
600 char statenamebuf[32], subtypenamebuf[32];
602 if ((radstate = paircopy2(req->vps, PW_STATE)) == NULL)
607 if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
612 /* first, dig up the state from the request packet, setting
613 * outselves to be in EAP-SIM-Start state if there is none.
616 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE)) == NULL)
618 /* must be initial request */
619 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, PW_TYPE_INTEGER);
620 statevp->vp_integer = eapsim_client_init;
621 pairreplace(&(resp->vps), statevp);
623 state = statevp->vp_integer;
626 * map the attributes, and authenticate them.
628 unmap_eapsim_types(req);
630 printf("<+++ EAP-sim decoded packet:\n");
631 vp_printlist(stdout, req->vps);
633 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE)) == NULL)
637 subtype = vp->vp_integer;
640 * look for the appropriate state, and process incoming message
643 case eapsim_client_init:
646 newstate = process_eap_start(req, resp);
649 case eapsim_challenge:
650 case eapsim_notification:
653 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
654 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
655 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
656 /* invalid state, drop message */
661 case eapsim_client_start:
664 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
665 newstate = process_eap_start(req, resp);
668 case eapsim_challenge:
669 newstate = process_eap_challenge(req, resp);
673 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
674 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
675 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
676 /* invalid state, drop message */
683 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
684 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
688 /* copy the eap state object in */
689 pairreplace(&(resp->vps), eapid);
691 /* update stete info, and send new packet */
692 map_eapsim_types(resp);
694 /* copy the radius state object in */
695 pairreplace(&(resp->vps), radstate);
697 statevp->vp_integer = newstate;
701 static int respond_eap_md5(RADIUS_PACKET *req,
704 VALUE_PAIR *vp, *id, *state;
705 size_t valuesize, namesize;
710 uint8_t response[16];
714 if ((state = paircopy2(req->vps, PW_STATE)) == NULL)
716 fprintf(stderr, "radeapclient: no state attribute found\n");
720 if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
722 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
725 identifier = id->vp_integer;
727 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5)) == NULL)
729 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
733 /* got the details of the MD5 challenge */
734 valuesize = vp->vp_octets[0];
735 value = &vp->vp_octets[1];
736 name = &vp->vp_octets[valuesize+1];
737 namesize = vp->length - (valuesize + 1);
740 if(valuesize > vp->length)
742 fprintf(stderr, "radeapclient: md5 valuesize if too big (%d > %d)\n",
743 valuesize, vp->length);
747 /* now do the CHAP operation ourself, rather than build the
748 * buffer. We could also call rad_chap_encode, but it wants
749 * a CHAP-Challenge, which we don't want to bother with.
751 fr_MD5Init(&context);
752 fr_MD5Update(&context, &identifier, 1);
753 fr_MD5Update(&context, (uint8_t *) password, strlen(password));
754 fr_MD5Update(&context, value, valuesize);
755 fr_MD5Final(response, &context);
757 vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, PW_TYPE_OCTETS);
759 memcpy(&vp->vp_strvalue[1], response, 16);
762 pairreplace(&(rep->vps), vp);
764 pairreplace(&(rep->vps), id);
766 /* copy the state object in */
767 pairreplace(&(rep->vps), state);
774 static int sendrecv_eap(RADIUS_PACKET *rep)
776 RADIUS_PACKET *req = NULL;
777 VALUE_PAIR *vp, *vpnext;
778 int tried_eap_md5 = 0;
781 * Keep a copy of the the User-Password attribute.
783 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD)) != NULL) {
784 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
786 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD)) != NULL) {
787 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
789 * Otherwise keep a copy of the CHAP-Password attribute.
791 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
792 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
800 printf("\n+++> About to send encoded packet:\n");
801 vp_printlist(stdout, rep->vps);
804 * if there are EAP types, encode them into an EAP-Message
810 * Fix up Digest-Attributes issues
812 for (vp = rep->vps; vp != NULL; vp = vp->next) {
813 switch (vp->attribute) {
817 case PW_DIGEST_REALM:
818 case PW_DIGEST_NONCE:
819 case PW_DIGEST_METHOD:
822 case PW_DIGEST_ALGORITHM:
823 case PW_DIGEST_BODY_DIGEST:
824 case PW_DIGEST_CNONCE:
825 case PW_DIGEST_NONCE_COUNT:
826 case PW_DIGEST_USER_NAME:
828 memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
829 vp->vp_octets[0] = vp->attribute - PW_DIGEST_REALM + 1;
831 vp->vp_octets[1] = vp->length;
832 vp->attribute = PW_DIGEST_ATTRIBUTES;
838 * If we've already sent a packet, free up the old
839 * one, and ensure that the next packet has a unique
840 * ID and authentication vector.
847 fr_md5_calc(rep->vector, rep->vector,
848 sizeof(rep->vector));
850 if (*password != '\0') {
851 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD)) != NULL) {
852 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
853 vp->length = strlen(password);
855 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD)) != NULL) {
856 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
857 vp->length = strlen(password);
859 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
860 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
861 vp->length = strlen(password);
863 rad_chap_encode(rep, vp->vp_octets, rep->id, vp);
866 } /* there WAS a password */
868 /* send the response, wait for the next request */
869 send_packet(rep, &req);
871 /* okay got back the packet, go and decode the EAP-Message. */
872 unmap_eap_types(req);
874 printf("<+++ EAP decoded packet:\n");
875 vp_printlist(stdout, req->vps);
877 /* now look for the code type. */
878 for (vp = req->vps; vp != NULL; vp = vpnext) {
881 switch (vp->attribute) {
885 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
886 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
893 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
894 if(respond_eap_sim(req, rep))
906 int main(int argc, char **argv)
912 char *filename = NULL;
917 id = ((int)getpid() & 0xff);
920 radlog_dest = RADLOG_STDERR;
922 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
926 if (!isdigit((int) *optarg))
928 count = atoi(optarg);
946 sha1_data_problems = 1; /* for debugging only */
953 if (!isdigit((int) *optarg))
955 retries = atoi(optarg);
958 if (!isdigit((int) *optarg))
961 if ((id < 0) || (id > 255)) {
969 if (!isdigit((int) *optarg))
971 timeout = atof(optarg);
974 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
978 fp = fopen(optarg, "r");
980 fprintf(stderr, "radclient: Error opening %s: %s\n",
981 optarg, strerror(errno));
984 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
985 fprintf(stderr, "radclient: Error reading %s: %s\n",
986 optarg, strerror(errno));
991 /* truncate newline */
992 p = filesecret + strlen(filesecret) - 1;
993 while ((p >= filesecret) &&
999 if (strlen(filesecret) < 2) {
1000 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1003 secret = filesecret;
1011 argc -= (optind - 1);
1012 argv += (optind - 1);
1015 ((secret == NULL) && (argc < 4))) {
1019 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1020 librad_perror("radclient");
1024 if ((req = rad_alloc(1)) == NULL) {
1025 librad_perror("radclient");
1033 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1035 perror("/dev/urandom");
1037 fread(randctx.randrsl, 256, 1, randinit);
1041 fr_randinit(&randctx, 1);
1047 * Strip port from hostname if needed.
1049 if ((p = strchr(argv[1], ':')) != NULL) {
1055 * See what kind of request we want to send.
1057 if (strcmp(argv[2], "auth") == 0) {
1058 if (port == 0) port = getport("radius");
1059 if (port == 0) port = PW_AUTH_UDP_PORT;
1060 req->code = PW_AUTHENTICATION_REQUEST;
1062 } else if (strcmp(argv[2], "acct") == 0) {
1063 if (port == 0) port = getport("radacct");
1064 if (port == 0) port = PW_ACCT_UDP_PORT;
1065 req->code = PW_ACCOUNTING_REQUEST;
1068 } else if (strcmp(argv[2], "status") == 0) {
1069 if (port == 0) port = getport("radius");
1070 if (port == 0) port = PW_AUTH_UDP_PORT;
1071 req->code = PW_STATUS_SERVER;
1073 } else if (strcmp(argv[2], "disconnect") == 0) {
1074 if (port == 0) port = PW_POD_UDP_PORT;
1075 req->code = PW_DISCONNECT_REQUEST;
1077 } else if (isdigit((int) argv[2][0])) {
1078 if (port == 0) port = getport("radius");
1079 if (port == 0) port = PW_AUTH_UDP_PORT;
1080 req->code = atoi(argv[2]);
1088 req->dst_port = port;
1089 if (ip_hton(argv[1], AF_INET, &req->dst_ipaddr) < 0) {
1090 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
1097 if (argv[3]) secret = argv[3];
1101 * Maybe read them, from stdin, if there's no
1102 * filename, or if the filename is '-'.
1104 if (filename && (strcmp(filename, "-") != 0)) {
1105 fp = fopen(filename, "r");
1107 fprintf(stderr, "radclient: Error opening %s: %s\n",
1108 filename, strerror(errno));
1118 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1119 perror("radclient: socket: ");
1124 if(req->vps) pairfree(&req->vps);
1126 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1135 printf("\n\t Total approved auths: %d\n", totalapp);
1136 printf("\t Total denied auths: %d\n", totaldeny);
1142 * given a radius request with some attributes in the EAP range, build
1143 * them all into a single EAP-Message body.
1145 * Note that this function will build multiple EAP-Message bodies
1146 * if there are multiple eligible EAP-types. This is incorrect, as the
1147 * recipient will in fact concatenate them.
1149 * XXX - we could break the loop once we process one type. Maybe this
1150 * just deserves an assert?
1153 static void map_eap_types(RADIUS_PACKET *req)
1155 VALUE_PAIR *vp, *vpnext;
1160 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID);
1162 id = ((int)getpid() & 0xff);
1164 id = vp->vp_integer;
1167 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE);
1169 eapcode = PW_EAP_REQUEST;
1171 eapcode = vp->vp_integer;
1175 for(vp = req->vps; vp != NULL; vp = vpnext) {
1176 /* save it in case it changes! */
1179 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
1180 vp->attribute < ATTRIBUTE_EAP_BASE+256) {
1189 eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
1192 case PW_EAP_IDENTITY:
1193 case PW_EAP_NOTIFICATION:
1204 * no known special handling, it is just encoded as an
1205 * EAP-message with the given type.
1208 /* nuke any existing EAP-Messages */
1209 pairdelete(&req->vps, PW_EAP_MESSAGE);
1211 memset(&ep, 0, sizeof(ep));
1214 ep.type.type = eap_type;
1215 ep.type.length = vp->length;
1216 ep.type.data = malloc(vp->length);
1217 memcpy(ep.type.data,vp->vp_octets, vp->length);
1218 eap_basic_compose(req, &ep);
1223 * given a radius request with an EAP-Message body, decode it specific
1226 static void unmap_eap_types(RADIUS_PACKET *rep)
1233 /* find eap message */
1234 e = eap_vp2packet(rep->vps);
1236 /* nothing to do! */
1237 if(e == NULL) return;
1239 /* create EAP-ID and EAP-CODE attributes to start */
1240 eap1 = paircreate(ATTRIBUTE_EAP_ID, PW_TYPE_INTEGER);
1241 eap1->vp_integer = e->id;
1242 pairadd(&(rep->vps), eap1);
1244 eap1 = paircreate(ATTRIBUTE_EAP_CODE, PW_TYPE_INTEGER);
1245 eap1->vp_integer = e->code;
1246 pairadd(&(rep->vps), eap1);
1251 case PW_EAP_SUCCESS:
1252 case PW_EAP_FAILURE:
1256 case PW_EAP_REQUEST:
1257 case PW_EAP_RESPONSE:
1258 /* there is a type field, which we use to create
1259 * a new attribute */
1261 /* the length was decode already into the attribute
1262 * length, and was checked already. Network byte
1263 * order, just pull it out using math.
1265 len = e->length[0]*256 + e->length[1];
1267 /* verify the length is big enough to hold type */
1275 type += ATTRIBUTE_EAP_BASE;
1278 if(len > MAX_STRING_LEN) {
1279 len = MAX_STRING_LEN;
1282 eap1 = paircreate(type, PW_TYPE_OCTETS);
1283 memcpy(eap1->vp_strvalue, &e->data[1], len);
1285 pairadd(&(rep->vps), eap1);
1292 static int map_eapsim_types(RADIUS_PACKET *r)
1297 memset(&ep, 0, sizeof(ep));
1298 ret = map_eapsim_basictypes(r, &ep);
1302 eap_basic_compose(r, &ep);
1307 static int unmap_eapsim_types(RADIUS_PACKET *r)
1311 esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM);
1313 radlog(L_ERR, "eap: EAP-Sim attribute not found");
1317 return unmap_eapsim_basictypes(r, esvp->vp_octets, esvp->length);
1324 const char *radius_dir = RADDBDIR;
1326 int radlog(int lvl, const char *msg, ...)
1332 r = vfprintf(stderr, msg, ap);
1334 fputc('\n', stderr);
1339 main(int argc, char *argv[])
1342 RADIUS_PACKET *req,*req2;
1343 VALUE_PAIR *vp, *vpkey, *vpextra;
1344 extern unsigned int sha1_data_problems;
1351 sha1_data_problems = 1;
1354 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1355 librad_perror("radclient");
1359 if ((req = rad_alloc(1)) == NULL) {
1360 librad_perror("radclient");
1364 if ((req2 = rad_alloc(1)) == NULL) {
1365 librad_perror("radclient");
1370 if(req->vps) pairfree(&req->vps);
1371 if(req2->vps) pairfree(&req2->vps);
1373 if ((req->vps = readvp2(stdin, &filedone, "eapsimlib:")) == NULL) {
1377 printf("\nRead:\n");
1378 vp_printlist(stdout, req->vps);
1380 map_eapsim_types(req);
1382 printf("Mapped to:\n");
1383 vp_printlist(stdout, req->vps);
1385 /* find the EAP-Message, copy it to req2 */
1386 vp = paircopy2(req->vps, PW_EAP_MESSAGE);
1388 if(vp == NULL) continue;
1390 pairadd(&req2->vps, vp);
1392 /* only call unmap for sim types here */
1393 unmap_eap_types(req2);
1394 unmap_eapsim_types(req2);
1396 printf("Unmapped to:\n");
1397 vp_printlist(stdout, req2->vps);
1399 vp = pairfind(req2->vps,
1400 ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
1401 vpkey = pairfind(req->vps, ATTRIBUTE_EAP_SIM_KEY);
1402 vpextra = pairfind(req->vps, ATTRIBUTE_EAP_SIM_EXTRA);
1404 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1405 uint8_t calcmac[16];
1407 /* find the EAP-Message, copy it to req2 */
1409 memset(calcmac, 0, sizeof(calcmac));
1410 printf("Confirming MAC...");
1411 if(eapsim_checkmac(req2->vps, vpkey->vp_strvalue,
1412 vpextra->vp_strvalue, vpextra->length,
1414 printf("succeed\n");
1418 printf("calculated MAC (");
1419 for (i = 0; i < 20; i++) {
1426 printf("%02x", calcmac[i]);
1428 printf(" did not match\n");