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 void radlog_request(UNUSED int lvl, UNUSED int priority,
120 UNUSED REQUEST *request, const char *msg, ...)
125 vfprintf(stderr, msg, ap);
130 static int getport(const char *name)
134 svp = getservbyname (name, "udp");
139 return ntohs(svp->s_port);
142 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
147 for (i = 0; i < retries; i++) {
150 rad_send(req, NULL, secret);
152 /* And wait for reply, timing out as necessary */
154 FD_SET(req->sockfd, &rdfdesc);
156 tv.tv_sec = (int)timeout;
157 tv.tv_usec = 1000000 * (timeout - (int) timeout);
159 /* Something's wrong if we don't get exactly one fd. */
160 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
164 *rep = rad_recv(req->sockfd, 0);
167 * If we get a response from a machine
168 * which we did NOT send a request to,
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];
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!",
184 src, (*rep)->src_port);
188 } else { /* NULL: couldn't receive the packet */
189 librad_perror("radclient:");
194 /* No response or no data read (?) */
196 fprintf(stderr, "radclient: no response from server\n");
201 * FIXME: Discard the packet & listen for another.
203 * Hmm... we should really be using eapol_test, which does
204 * a lot more than radeapclient.
206 if (rad_verify(*rep, req, secret) != 0) {
207 librad_perror("rad_verify");
211 if (rad_decode(*rep, req, secret) != 0) {
212 librad_perror("rad_decode");
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);
222 if((*rep)->code == PW_AUTHENTICATION_ACK) {
231 static void cleanresp(RADIUS_PACKET *resp)
233 VALUE_PAIR *vpnext, *vp, **last;
237 * maybe should just copy things we care about, or keep
238 * a copy of the original input and start from there again?
240 pairdelete(&resp->vps, PW_EAP_MESSAGE);
241 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY);
244 for(vp = *last; vp != NULL; vp = vpnext)
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))
262 * we got an EAP-Request/Sim/Start message in a legal state.
264 * pick a supported version, put it into the reply, and insert a nonce.
266 static int process_eap_start(RADIUS_PACKET *req,
269 VALUE_PAIR *vp, *newvp;
270 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
271 uint16_t *versions, selectedversion;
272 unsigned int i,versioncount;
274 /* form new response clear of any EAP stuff */
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");
282 versions = (uint16_t *)vp->vp_strvalue;
284 /* verify that the attribute length is big enough for a length field */
287 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %d\n", vp->length);
291 versioncount = ntohs(versions[0])/2;
292 /* verify that the attribute length is big enough for the given number
293 * of versions present.
295 if((unsigned)vp->length <= (versioncount*2 + 2))
297 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %d bytes\n", versioncount, vp->length);
302 * record the versionlist for the MK calculation.
304 eapsim_mk.versionlistlen = versioncount*2;
305 memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
306 eapsim_mk.versionlistlen);
308 /* walk the version list, and pick the one we support, which
309 * at present, is 1, EAP_SIM_VERSION.
312 for(i=0; i < versioncount; i++)
314 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
316 selectedversion=EAP_SIM_VERSION;
320 if(selectedversion == 0)
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++)
325 fprintf(stderr, "\tfound version %d\n",
326 ntohs(versions[i+1]));
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
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);
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 "));
350 /* okay, we have just any_id_req there, so fill in response */
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);
357 /* insert selected version into response. */
358 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
360 versions = (uint16_t *)newvp->vp_strvalue;
361 versions[0] = htons(selectedversion);
363 pairreplace(&(rep->vps), newvp);
365 /* record the selected version */
366 memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
373 * insert a nonce_mt that we make up.
375 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
377 newvp->vp_octets[0]=0;
378 newvp->vp_octets[1]=0;
379 newvp->length = 18; /* 16 bytes of nonce + padding */
385 memcpy(&newvp->vp_octets[2], nonce, 16);
386 pairreplace(&(rep->vps), newvp);
388 /* also keep a copy of the nonce! */
389 memcpy(eapsim_mk.nonce_mt, nonce, 16);
393 uint16_t *pidlen, idlen;
396 * insert the identity here.
398 vp = pairfind(rep->vps, PW_USER_NAME);
401 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
404 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
406 idlen = strlen(vp->vp_strvalue);
407 pidlen = (uint16_t *)newvp->vp_strvalue;
408 *pidlen = htons(idlen);
409 newvp->length = idlen + 2;
411 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
412 pairreplace(&(rep->vps), newvp);
415 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
416 eapsim_mk.identitylen = idlen;
423 * we got an EAP-Request/Sim/Challenge message in a legal state.
425 * use the RAND challenge to produce the SRES result, and then
426 * use that to generate a new MAC.
428 * for the moment, we ignore the RANDs, then just plug in the SRES
432 static int process_eap_challenge(RADIUS_PACKET *req,
436 VALUE_PAIR *mac, *randvp;
437 VALUE_PAIR *sres1,*sres2,*sres3;
438 VALUE_PAIR *Kc1, *Kc2, *Kc3;
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");
450 * compare RAND with randX, to verify this is the right response
454 VALUE_PAIR *randcfgvp[3];
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];
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);
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");
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) {
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);
481 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
488 fprintf(stderr, "%02x", randcfg[rnum][i]);
490 fprintf(stderr, "\nconfigured rand %d: ", rnum);
492 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
499 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
501 fprintf(stderr, "\n");
508 * now dig up the sres values from the response packet,
509 * which were put there when we read things in.
511 * Really, they should be calculated from the RAND!
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);
521 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
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]));
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);
535 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
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]));
542 /* all set, calculate keys */
543 eapsim_calculate_keys(&eapsim_mk);
546 eapsim_dump_mk(&eapsim_mk);
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),
553 printf("MAC check succeed\n");
557 printf("calculated MAC (");
558 for (i = 0; i < 20; i++) {
565 printf("%02x", calcmac[i]);
567 printf(" did not match\n");
571 /* form new response clear of any EAP stuff */
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);
580 * fill the SIM_MAC with a field that will in fact get appended
581 * to the packet before the MAC is calculated
583 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC,
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);
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);
600 * this code runs the EAP-SIM client state machine.
601 * the *request* is from the server.
602 * the *reponse* is to the server.
605 static int respond_eap_sim(RADIUS_PACKET *req,
608 enum eapsim_clientstates state, newstate;
609 enum eapsim_subtype subtype;
610 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
611 char statenamebuf[32], subtypenamebuf[32];
613 if ((radstate = paircopy2(req->vps, PW_STATE)) == NULL)
618 if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
623 /* first, dig up the state from the request packet, setting
624 * outselves to be in EAP-SIM-Start state if there is none.
627 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE)) == NULL)
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);
634 state = statevp->vp_integer;
637 * map the attributes, and authenticate them.
639 unmap_eapsim_types(req);
641 printf("<+++ EAP-sim decoded packet:\n");
642 vp_printlist(stdout, req->vps);
644 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE)) == NULL)
648 subtype = vp->vp_integer;
651 * look for the appropriate state, and process incoming message
654 case eapsim_client_init:
657 newstate = process_eap_start(req, resp);
660 case eapsim_challenge:
661 case eapsim_notification:
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 */
672 case eapsim_client_start:
675 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
676 newstate = process_eap_start(req, resp);
679 case eapsim_challenge:
680 newstate = process_eap_challenge(req, resp);
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 */
694 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
695 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
699 /* copy the eap state object in */
700 pairreplace(&(resp->vps), eapid);
702 /* update stete info, and send new packet */
703 map_eapsim_types(resp);
705 /* copy the radius state object in */
706 pairreplace(&(resp->vps), radstate);
708 statevp->vp_integer = newstate;
712 static int respond_eap_md5(RADIUS_PACKET *req,
715 VALUE_PAIR *vp, *id, *state;
716 size_t valuesize, namesize;
721 uint8_t response[16];
725 if ((state = paircopy2(req->vps, PW_STATE)) == NULL)
727 fprintf(stderr, "radeapclient: no state attribute found\n");
731 if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
733 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
736 identifier = id->vp_integer;
738 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5)) == NULL)
740 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
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);
751 if(valuesize > vp->length)
753 fprintf(stderr, "radeapclient: md5 valuesize if too big (%d > %d)\n",
754 valuesize, vp->length);
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.
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);
768 vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, PW_TYPE_OCTETS);
770 memcpy(&vp->vp_strvalue[1], response, 16);
773 pairreplace(&(rep->vps), vp);
775 pairreplace(&(rep->vps), id);
777 /* copy the state object in */
778 pairreplace(&(rep->vps), state);
785 static int sendrecv_eap(RADIUS_PACKET *rep)
787 RADIUS_PACKET *req = NULL;
788 VALUE_PAIR *vp, *vpnext;
789 int tried_eap_md5 = 0;
792 * Keep a copy of the the User-Password attribute.
794 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD)) != NULL) {
795 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
797 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD)) != NULL) {
798 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
800 * Otherwise keep a copy of the CHAP-Password attribute.
802 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
803 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
811 printf("\n+++> About to send encoded packet:\n");
812 vp_printlist(stdout, rep->vps);
815 * if there are EAP types, encode them into an EAP-Message
821 * Fix up Digest-Attributes issues
823 for (vp = rep->vps; vp != NULL; vp = vp->next) {
824 switch (vp->attribute) {
828 case PW_DIGEST_REALM:
829 case PW_DIGEST_NONCE:
830 case PW_DIGEST_METHOD:
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:
839 memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
840 vp->vp_octets[0] = vp->attribute - PW_DIGEST_REALM + 1;
842 vp->vp_octets[1] = vp->length;
843 vp->attribute = PW_DIGEST_ATTRIBUTES;
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.
858 fr_md5_calc(rep->vector, rep->vector,
859 sizeof(rep->vector));
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);
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);
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);
874 rad_chap_encode(rep, vp->vp_octets, rep->id, vp);
877 } /* there WAS a password */
879 /* send the response, wait for the next request */
880 send_packet(rep, &req);
882 /* okay got back the packet, go and decode the EAP-Message. */
883 unmap_eap_types(req);
885 printf("<+++ EAP decoded packet:\n");
886 vp_printlist(stdout, req->vps);
888 /* now look for the code type. */
889 for (vp = req->vps; vp != NULL; vp = vpnext) {
892 switch (vp->attribute) {
896 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
897 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
904 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
905 if(respond_eap_sim(req, rep))
917 int main(int argc, char **argv)
923 char *filename = NULL;
928 id = ((int)getpid() & 0xff);
931 radlog_dest = RADLOG_STDERR;
933 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
937 if (!isdigit((int) *optarg))
939 count = atoi(optarg);
957 sha1_data_problems = 1; /* for debugging only */
964 if (!isdigit((int) *optarg))
966 retries = atoi(optarg);
969 if (!isdigit((int) *optarg))
972 if ((id < 0) || (id > 255)) {
980 if (!isdigit((int) *optarg))
982 timeout = atof(optarg);
985 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
989 fp = fopen(optarg, "r");
991 fprintf(stderr, "radclient: Error opening %s: %s\n",
992 optarg, strerror(errno));
995 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
996 fprintf(stderr, "radclient: Error reading %s: %s\n",
997 optarg, strerror(errno));
1002 /* truncate newline */
1003 p = filesecret + strlen(filesecret) - 1;
1004 while ((p >= filesecret) &&
1010 if (strlen(filesecret) < 2) {
1011 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1014 secret = filesecret;
1022 argc -= (optind - 1);
1023 argv += (optind - 1);
1026 ((secret == NULL) && (argc < 4))) {
1030 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1031 librad_perror("radclient");
1035 if ((req = rad_alloc(1)) == NULL) {
1036 librad_perror("radclient");
1044 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1046 perror("/dev/urandom");
1048 fread(randctx.randrsl, 256, 1, randinit);
1052 fr_randinit(&randctx, 1);
1058 * Strip port from hostname if needed.
1060 if ((p = strchr(argv[1], ':')) != NULL) {
1066 * See what kind of request we want to send.
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;
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;
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;
1084 } else if (strcmp(argv[2], "disconnect") == 0) {
1085 if (port == 0) port = PW_POD_UDP_PORT;
1086 req->code = PW_DISCONNECT_REQUEST;
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]);
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]);
1108 if (argv[3]) secret = argv[3];
1112 * Maybe read them, from stdin, if there's no
1113 * filename, or if the filename is '-'.
1115 if (filename && (strcmp(filename, "-") != 0)) {
1116 fp = fopen(filename, "r");
1118 fprintf(stderr, "radclient: Error opening %s: %s\n",
1119 filename, strerror(errno));
1129 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1130 perror("radclient: socket: ");
1135 if(req->vps) pairfree(&req->vps);
1137 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1146 printf("\n\t Total approved auths: %d\n", totalapp);
1147 printf("\t Total denied auths: %d\n", totaldeny);
1153 * given a radius request with some attributes in the EAP range, build
1154 * them all into a single EAP-Message body.
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.
1160 * XXX - we could break the loop once we process one type. Maybe this
1161 * just deserves an assert?
1164 static void map_eap_types(RADIUS_PACKET *req)
1166 VALUE_PAIR *vp, *vpnext;
1171 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID);
1173 id = ((int)getpid() & 0xff);
1175 id = vp->vp_integer;
1178 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE);
1180 eapcode = PW_EAP_REQUEST;
1182 eapcode = vp->vp_integer;
1186 for(vp = req->vps; vp != NULL; vp = vpnext) {
1187 /* save it in case it changes! */
1190 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
1191 vp->attribute < ATTRIBUTE_EAP_BASE+256) {
1200 eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
1203 case PW_EAP_IDENTITY:
1204 case PW_EAP_NOTIFICATION:
1215 * no known special handling, it is just encoded as an
1216 * EAP-message with the given type.
1219 /* nuke any existing EAP-Messages */
1220 pairdelete(&req->vps, PW_EAP_MESSAGE);
1222 memset(&ep, 0, sizeof(ep));
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);
1234 * given a radius request with an EAP-Message body, decode it specific
1237 static void unmap_eap_types(RADIUS_PACKET *rep)
1244 /* find eap message */
1245 e = eap_vp2packet(rep->vps);
1247 /* nothing to do! */
1248 if(e == NULL) return;
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);
1255 eap1 = paircreate(ATTRIBUTE_EAP_CODE, PW_TYPE_INTEGER);
1256 eap1->vp_integer = e->code;
1257 pairadd(&(rep->vps), eap1);
1262 case PW_EAP_SUCCESS:
1263 case PW_EAP_FAILURE:
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 */
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.
1276 len = e->length[0]*256 + e->length[1];
1278 /* verify the length is big enough to hold type */
1286 type += ATTRIBUTE_EAP_BASE;
1289 if(len > MAX_STRING_LEN) {
1290 len = MAX_STRING_LEN;
1293 eap1 = paircreate(type, PW_TYPE_OCTETS);
1294 memcpy(eap1->vp_strvalue, &e->data[1], len);
1296 pairadd(&(rep->vps), eap1);
1303 static int map_eapsim_types(RADIUS_PACKET *r)
1308 memset(&ep, 0, sizeof(ep));
1309 ret = map_eapsim_basictypes(r, &ep);
1313 eap_basic_compose(r, &ep);
1318 static int unmap_eapsim_types(RADIUS_PACKET *r)
1322 esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM);
1324 radlog(L_ERR, "eap: EAP-Sim attribute not found");
1328 return unmap_eapsim_basictypes(r, esvp->vp_octets, esvp->length);
1335 const char *radius_dir = RADDBDIR;
1337 int radlog(int lvl, const char *msg, ...)
1343 r = vfprintf(stderr, msg, ap);
1345 fputc('\n', stderr);
1350 main(int argc, char *argv[])
1353 RADIUS_PACKET *req,*req2;
1354 VALUE_PAIR *vp, *vpkey, *vpextra;
1355 extern unsigned int sha1_data_problems;
1362 sha1_data_problems = 1;
1365 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1366 librad_perror("radclient");
1370 if ((req = rad_alloc(1)) == NULL) {
1371 librad_perror("radclient");
1375 if ((req2 = rad_alloc(1)) == NULL) {
1376 librad_perror("radclient");
1381 if(req->vps) pairfree(&req->vps);
1382 if(req2->vps) pairfree(&req2->vps);
1384 if ((req->vps = readvp2(stdin, &filedone, "eapsimlib:")) == NULL) {
1388 printf("\nRead:\n");
1389 vp_printlist(stdout, req->vps);
1391 map_eapsim_types(req);
1393 printf("Mapped to:\n");
1394 vp_printlist(stdout, req->vps);
1396 /* find the EAP-Message, copy it to req2 */
1397 vp = paircopy2(req->vps, PW_EAP_MESSAGE);
1399 if(vp == NULL) continue;
1401 pairadd(&req2->vps, vp);
1403 /* only call unmap for sim types here */
1404 unmap_eap_types(req2);
1405 unmap_eapsim_types(req2);
1407 printf("Unmapped to:\n");
1408 vp_printlist(stdout, req2->vps);
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);
1415 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1416 uint8_t calcmac[16];
1418 /* find the EAP-Message, copy it to req2 */
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,
1425 printf("succeed\n");
1429 printf("calculated MAC (");
1430 for (i = 0; i < 20; i++) {
1437 printf("%02x", calcmac[i]);
1439 printf(" did not match\n");