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>
27 #include <freeradius-devel/libradius.h>
35 #include <freeradius-devel/conf.h>
36 #include <freeradius-devel/radpaths.h>
37 #include <freeradius-devel/md5.h>
39 #include "eap_types.h"
42 extern int sha1_data_problems;
44 static int retries = 10;
45 static float timeout = 3;
46 static char const *secret = NULL;
47 static int do_output = 1;
48 static int do_summary = 0;
49 static int filedone = 0;
50 static int totalapp = 0;
51 static int totaldeny = 0;
52 static char filesecret[256];
53 char *radius_dir = NULL;
54 char const *progname = "radeapclient";
55 /* fr_randctx randctx; */
58 #include <freeradius-devel/tls.h>
61 log_dst_t radlog_dest = L_DST_STDERR;
62 char const *radlog_dir = NULL;
63 log_debug_t debug_flag = 0;
66 struct eapsim_keys eapsim_mk;
68 static void map_eap_methods(RADIUS_PACKET *req);
69 static void unmap_eap_methods(RADIUS_PACKET *rep);
70 static int map_eapsim_types(RADIUS_PACKET *r);
71 static int unmap_eapsim_types(RADIUS_PACKET *r);
73 void debug_pair_list(UNUSED VALUE_PAIR *vp)
78 static void NEVER_RETURNS usage(void)
80 fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
82 fprintf(stderr, " <command> One of auth, acct, status, or disconnect.\n");
83 fprintf(stderr, " -c count Send each packet 'count' times.\n");
84 fprintf(stderr, " -d raddb Set dictionary directory.\n");
85 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
86 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retries' times.\n");
87 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying (may be a floating point number).\n");
88 fprintf(stderr, " -h Print usage help information.\n");
89 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
90 fprintf(stderr, " -S file read secret from file, not command line.\n");
91 fprintf(stderr, " -q Do not print anything out.\n");
92 fprintf(stderr, " -s Print out summary information of auth results.\n");
93 fprintf(stderr, " -v Show program version information.\n");
94 fprintf(stderr, " -x Debugging mode.\n");
95 fprintf(stderr, " -4 Use IPv4 address of server\n");
96 fprintf(stderr, " -6 Use IPv6 address of server.\n");
101 int radlog(int lvl, char const *msg, ...)
106 r = lvl; /* shut up compiler */
109 r = vfprintf(stderr, msg, ap);
116 void radlog_request(UNUSED int lvl, UNUSED int priority,
117 UNUSED REQUEST *request, char const *msg, ...)
122 vfprintf(stderr, msg, ap);
127 static int getport(char const *name)
131 svp = getservbyname (name, "udp");
136 return ntohs(svp->s_port);
141 static void debug_packet(RADIUS_PACKET *packet, int direction)
145 char const *received, *from;
146 const fr_ipaddr_t *ip;
151 if (direction == 0) {
152 received = "Received";
153 from = "from"; /* what else? */
154 ip = &packet->src_ipaddr;
155 port = packet->src_port;
158 received = "Sending";
159 from = "to"; /* hah! */
160 ip = &packet->dst_ipaddr;
161 port = packet->dst_port;
165 * Client-specific debugging re-prints the input
166 * packet into the client log.
168 * This really belongs in a utility library
170 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
171 printf("%s %s packet %s host %s port %d, id=%d, length=%d\n",
172 received, fr_packet_codes[packet->code], from,
173 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
174 port, packet->id, (int) packet->data_len);
176 printf("%s packet %s host %s port %d code=%d, id=%d, length=%d\n",
178 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
180 packet->code, packet->id, (int) packet->data_len);
183 for (vp = packet->vps; vp != NULL; vp = vp->next) {
184 vp_prints(buffer, sizeof(buffer), vp);
185 printf("\t%s\n", buffer);
191 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
196 for (i = 0; i < retries; i++) {
199 debug_packet(req, R_SENT);
201 rad_send(req, NULL, secret);
203 /* And wait for reply, timing out as necessary */
205 FD_SET(req->sockfd, &rdfdesc);
207 tv.tv_sec = (int)timeout;
208 tv.tv_usec = 1000000 * (timeout - (int) timeout);
210 /* Something's wrong if we don't get exactly one fd. */
211 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
215 *rep = rad_recv(req->sockfd, 0);
218 * If we get a response from a machine
219 * which we did NOT send a request to,
222 if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
223 (memcmp(&(*rep)->src_ipaddr.ipaddr,
224 &req->dst_ipaddr.ipaddr,
225 ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
226 sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
227 sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
228 ((*rep)->src_port != req->dst_port)) {
229 char src[128], dst[128];
231 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
232 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
233 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
235 src, (*rep)->src_port);
239 } else { /* NULL: couldn't receive the packet */
240 fr_perror("radclient:");
245 /* No response or no data read (?) */
247 fprintf(stderr, "radclient: no response from server\n");
252 * FIXME: Discard the packet & listen for another.
254 * Hmm... we should really be using eapol_test, which does
255 * a lot more than radeapclient.
257 if (rad_verify(*rep, req, secret) != 0) {
258 fr_perror("rad_verify");
262 if (rad_decode(*rep, req, secret) != 0) {
263 fr_perror("rad_decode");
267 /* libradius debug already prints out the value pairs for us */
268 if (!fr_debug_flag && do_output) {
269 debug_packet(*rep, R_RECV);
271 if((*rep)->code == PW_AUTHENTICATION_ACK) {
273 } else if ((*rep)->code == PW_AUTHENTICATION_REJECT) {
280 static void cleanresp(RADIUS_PACKET *resp)
282 VALUE_PAIR *vpnext, *vp, **last;
286 * maybe should just copy things we care about, or keep
287 * a copy of the original input and start from there again?
289 pairdelete(&resp->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
290 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY, 0, TAG_ANY);
293 for(vp = *last; vp != NULL; vp = vpnext)
297 if((vp->da->attribute > ATTRIBUTE_EAP_BASE &&
298 vp->da->attribute <= ATTRIBUTE_EAP_BASE+256) ||
299 (vp->da->attribute > ATTRIBUTE_EAP_SIM_BASE &&
300 vp->da->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
311 * we got an EAP-Request/Sim/Start message in a legal state.
313 * pick a supported version, put it into the reply, and insert a nonce.
315 static int process_eap_start(RADIUS_PACKET *req,
318 VALUE_PAIR *vp, *newvp;
319 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
320 uint16_t *versions, selectedversion;
321 unsigned int i,versioncount;
323 /* form new response clear of any EAP stuff */
326 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST, 0, TAG_ANY)) == NULL) {
327 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
331 versions = (uint16_t *)vp->vp_strvalue;
333 /* verify that the attribute length is big enough for a length field */
336 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %u\n", (unsigned int) vp->length);
340 versioncount = ntohs(versions[0])/2;
341 /* verify that the attribute length is big enough for the given number
342 * of versions present.
344 if((unsigned)vp->length <= (versioncount*2 + 2))
346 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %u bytes\n", versioncount, (unsigned int) vp->length);
351 * record the versionlist for the MK calculation.
353 eapsim_mk.versionlistlen = versioncount*2;
354 memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
355 eapsim_mk.versionlistlen);
357 /* walk the version list, and pick the one we support, which
358 * at present, is 1, EAP_SIM_VERSION.
361 for(i=0; i < versioncount; i++)
363 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
365 selectedversion=EAP_SIM_VERSION;
369 if(selectedversion == 0)
371 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
372 for(i=0; i < versioncount; i++)
374 fprintf(stderr, "\tfound version %d\n",
375 ntohs(versions[i+1]));
380 * now make sure that we have only FULLAUTH_ID_REQ.
381 * I think that it actually might not matter - we can answer in
382 * anyway we like, but it is illegal to have more than one
385 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ, 0, TAG_ANY);
386 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ, 0, TAG_ANY);
387 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ, 0, TAG_ANY);
389 if(!fullauthidreq_vp ||
390 anyidreq_vp != NULL ||
391 permanentidreq_vp != NULL) {
392 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
393 (anyidreq_vp != NULL ? "a " : "no "),
394 (fullauthidreq_vp != NULL ? "a " : "no "),
395 (permanentidreq_vp != NULL ? "a " : "no "));
399 /* okay, we have just any_id_req there, so fill in response */
401 /* mark the subtype as being EAP-SIM/Response/Start */
402 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, 0, PW_TYPE_INTEGER);
403 newvp->vp_integer = eapsim_start;
404 pairreplace(&(rep->vps), newvp);
406 /* insert selected version into response. */
407 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
409 versions = (uint16_t *)newvp->vp_strvalue;
410 versions[0] = htons(selectedversion);
412 pairreplace(&(rep->vps), newvp);
414 /* record the selected version */
415 memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
422 * insert a nonce_mt that we make up.
424 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
426 newvp->vp_octets[0]=0;
427 newvp->vp_octets[1]=0;
428 newvp->length = 18; /* 16 bytes of nonce + padding */
434 memcpy(&newvp->vp_octets[2], nonce, 16);
435 pairreplace(&(rep->vps), newvp);
437 /* also keep a copy of the nonce! */
438 memcpy(eapsim_mk.nonce_mt, nonce, 16);
442 uint16_t *pidlen, idlen;
445 * insert the identity here.
447 vp = pairfind(rep->vps, PW_USER_NAME, 0, TAG_ANY);
450 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
453 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
455 idlen = strlen(vp->vp_strvalue);
456 pidlen = (uint16_t *)newvp->vp_strvalue;
457 *pidlen = htons(idlen);
458 newvp->length = idlen + 2;
460 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
461 pairreplace(&(rep->vps), newvp);
464 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
465 eapsim_mk.identitylen = idlen;
472 * we got an EAP-Request/Sim/Challenge message in a legal state.
474 * use the RAND challenge to produce the SRES result, and then
475 * use that to generate a new MAC.
477 * for the moment, we ignore the RANDs, then just plug in the SRES
481 static int process_eap_challenge(RADIUS_PACKET *req,
485 VALUE_PAIR *mac, *randvp;
486 VALUE_PAIR *sres1,*sres2,*sres3;
487 VALUE_PAIR *Kc1, *Kc2, *Kc3;
490 /* look for the AT_MAC and the challenge data */
491 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
492 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND, 0, TAG_ANY);
493 if(!mac || !randvp) {
494 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
499 * compare RAND with randX, to verify this is the right response
503 VALUE_PAIR *randcfgvp[3];
506 randcfg[0] = &randvp->vp_octets[2];
507 randcfg[1] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE];
508 randcfg[2] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE*2];
510 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1, 0, TAG_ANY);
511 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2, 0, TAG_ANY);
512 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3, 0, TAG_ANY);
517 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
521 if(memcmp(randcfg[0], randcfgvp[0]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
522 memcmp(randcfg[1], randcfgvp[1]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
523 memcmp(randcfg[2], randcfgvp[2]->vp_octets, EAPSIM_RAND_SIZE)!=0) {
526 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
527 for(rnum = 0; rnum < 3; rnum++) {
528 fprintf(stderr, "received rand %d: ", rnum);
530 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
537 fprintf(stderr, "%02x", randcfg[rnum][i]);
539 fprintf(stderr, "\nconfigured rand %d: ", rnum);
541 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
548 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
550 fprintf(stderr, "\n");
557 * now dig up the sres values from the response packet,
558 * which were put there when we read things in.
560 * Really, they should be calculated from the RAND!
563 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1, 0, TAG_ANY);
564 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2, 0, TAG_ANY);
565 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3, 0, TAG_ANY);
570 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
573 memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
574 memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
575 memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
577 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1, 0, TAG_ANY);
578 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2, 0, TAG_ANY);
579 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3, 0, TAG_ANY);
584 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
587 memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
588 memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
589 memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
591 /* all set, calculate keys */
592 eapsim_calculate_keys(&eapsim_mk);
595 eapsim_dump_mk(&eapsim_mk);
598 /* verify the MAC, now that we have all the keys. */
599 if(eapsim_checkmac(NULL, req->vps, eapsim_mk.K_aut,
600 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
602 printf("MAC check succeed\n");
606 printf("calculated MAC (");
607 for (i = 0; i < 20; i++) {
614 printf("%02x", calcmac[i]);
616 printf(" did not match\n");
620 /* form new response clear of any EAP stuff */
623 /* mark the subtype as being EAP-SIM/Response/Start */
624 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, 0, PW_TYPE_INTEGER);
625 newvp->vp_integer = eapsim_challenge;
626 pairreplace(&(rep->vps), newvp);
629 * fill the SIM_MAC with a field that will in fact get appended
630 * to the packet before the MAC is calculated
632 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0,
634 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
635 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
636 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
637 newvp->length = EAPSIM_SRES_SIZE*3;
638 pairreplace(&(rep->vps), newvp);
640 newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, 0, PW_TYPE_OCTETS);
641 memcpy(newvp->vp_strvalue, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
642 newvp->length = EAPSIM_AUTH_SIZE;
643 pairreplace(&(rep->vps), newvp);
649 * this code runs the EAP-SIM client state machine.
650 * the *request* is from the server.
651 * the *reponse* is to the server.
654 static int respond_eap_sim(RADIUS_PACKET *req,
657 enum eapsim_clientstates state, newstate;
658 enum eapsim_subtype subtype;
659 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
660 char statenamebuf[32], subtypenamebuf[32];
662 if ((radstate = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
667 if ((eapid = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
672 /* first, dig up the state from the request packet, setting
673 * outselves to be in EAP-SIM-Start state if there is none.
676 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE, 0, TAG_ANY)) == NULL)
678 /* must be initial request */
679 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, 0, PW_TYPE_INTEGER);
680 statevp->vp_integer = eapsim_client_init;
681 pairreplace(&(resp->vps), statevp);
683 state = statevp->vp_integer;
686 * map the attributes, and authenticate them.
688 unmap_eapsim_types(req);
690 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE, 0, TAG_ANY)) == NULL)
694 subtype = vp->vp_integer;
697 * look for the appropriate state, and process incoming message
700 case eapsim_client_init:
703 newstate = process_eap_start(req, resp);
706 case eapsim_challenge:
707 case eapsim_notification:
710 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
711 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
712 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
713 /* invalid state, drop message */
718 case eapsim_client_start:
721 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
722 newstate = process_eap_start(req, resp);
725 case eapsim_challenge:
726 newstate = process_eap_challenge(req, resp);
730 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
731 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
732 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
733 /* invalid state, drop message */
740 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
741 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
745 /* copy the eap state object in */
746 pairreplace(&(resp->vps), eapid);
748 /* update stete info, and send new packet */
749 map_eapsim_types(resp);
751 /* copy the radius state object in */
752 pairreplace(&(resp->vps), radstate);
754 statevp->vp_integer = newstate;
758 static int respond_eap_md5(RADIUS_PACKET *req,
761 VALUE_PAIR *vp, *id, *state;
762 size_t valuesize, namesize;
767 uint8_t response[16];
771 if ((state = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
773 fprintf(stderr, "radeapclient: no state attribute found\n");
777 if ((id = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
779 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
782 identifier = id->vp_integer;
784 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0, TAG_ANY)) == NULL)
786 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
790 /* got the details of the MD5 challenge */
791 valuesize = vp->vp_octets[0];
792 value = &vp->vp_octets[1];
793 name = &vp->vp_octets[valuesize+1];
794 namesize = vp->length - (valuesize + 1);
797 if(valuesize > vp->length)
799 fprintf(stderr, "radeapclient: md5 valuesize if too big (%u > %u)\n",
800 (unsigned int) valuesize, (unsigned int) vp->length);
804 /* now do the CHAP operation ourself, rather than build the
805 * buffer. We could also call rad_chap_encode, but it wants
806 * a CHAP-Challenge, which we don't want to bother with.
808 fr_MD5Init(&context);
809 fr_MD5Update(&context, &identifier, 1);
810 fr_MD5Update(&context, (uint8_t *) password, strlen(password));
811 fr_MD5Update(&context, value, valuesize);
812 fr_MD5Final(response, &context);
814 vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0, PW_TYPE_OCTETS);
816 memcpy(&vp->vp_strvalue[1], response, 16);
819 pairreplace(&(rep->vps), vp);
821 pairreplace(&(rep->vps), id);
823 /* copy the state object in */
824 pairreplace(&(rep->vps), state);
831 static int sendrecv_eap(RADIUS_PACKET *rep)
833 RADIUS_PACKET *req = NULL;
834 VALUE_PAIR *vp, *vpnext;
835 int tried_eap_md5 = 0;
838 * Keep a copy of the the User-Password attribute.
840 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
841 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
843 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
844 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
846 * Otherwise keep a copy of the CHAP-Password attribute.
848 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
849 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
858 * if there are EAP types, encode them into an EAP-Message
861 map_eap_methods(rep);
864 * Fix up Digest-Attributes issues
866 for (vp = rep->vps; vp != NULL; vp = vp->next) {
867 switch (vp->da->attribute) {
871 case PW_DIGEST_REALM:
872 case PW_DIGEST_NONCE:
873 case PW_DIGEST_METHOD:
876 case PW_DIGEST_ALGORITHM:
877 case PW_DIGEST_BODY_DIGEST:
878 case PW_DIGEST_CNONCE:
879 case PW_DIGEST_NONCE_COUNT:
880 case PW_DIGEST_USER_NAME:
882 memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
883 vp->vp_octets[0] = vp->da->attribute - PW_DIGEST_REALM + 1;
885 vp->vp_octets[1] = vp->length;
886 vp->da->attribute = PW_DIGEST_ATTRIBUTES;
892 * If we've already sent a packet, free up the old
893 * one, and ensure that the next packet has a unique
894 * ID and authentication vector.
897 talloc_free(rep->data);
901 fr_md5_calc(rep->vector, rep->vector,
902 sizeof(rep->vector));
904 if (*password != '\0') {
905 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
906 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
907 vp->length = strlen(password);
909 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
910 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
911 vp->length = strlen(password);
913 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
914 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
915 vp->length = strlen(password);
917 rad_chap_encode(rep, vp->vp_octets, rep->id, vp);
920 } /* there WAS a password */
922 /* send the response, wait for the next request */
923 send_packet(rep, &req);
925 /* okay got back the packet, go and decode the EAP-Message. */
926 unmap_eap_methods(req);
928 debug_packet(req, R_RECV);
930 /* now look for the code type. */
931 for (vp = req->vps; vp != NULL; vp = vpnext) {
934 switch (vp->da->attribute) {
938 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
939 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
946 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
947 if(respond_eap_sim(req, rep))
959 int main(int argc, char **argv)
965 char *filename = NULL;
969 int force_af = AF_UNSPEC;
971 id = ((int)getpid() & 0xff);
974 radlog_dest = L_DST_STDERR;
976 while ((c = getopt(argc, argv, "46c:d:f:hi:qst:r:S:xXv")) != EOF)
986 if (!isdigit((int) *optarg))
988 count = atoi(optarg);
991 radius_dir = strdup(optarg);
1006 sha1_data_problems = 1; /* for debugging only */
1013 if (!isdigit((int) *optarg))
1015 retries = atoi(optarg);
1018 if (!isdigit((int) *optarg))
1021 if ((id < 0) || (id > 255)) {
1029 if (!isdigit((int) *optarg))
1031 timeout = atof(optarg);
1034 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
1038 fp = fopen(optarg, "r");
1040 fprintf(stderr, "radclient: Error opening %s: %s\n",
1041 optarg, strerror(errno));
1044 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1045 fprintf(stderr, "radclient: Error reading %s: %s\n",
1046 optarg, strerror(errno));
1051 /* truncate newline */
1052 p = filesecret + strlen(filesecret) - 1;
1053 while ((p >= filesecret) &&
1059 if (strlen(filesecret) < 2) {
1060 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1063 secret = filesecret;
1071 argc -= (optind - 1);
1072 argv += (optind - 1);
1075 ((!secret) && (argc < 4))) {
1079 if (!radius_dir) radius_dir = strdup(RADDBDIR);
1081 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1082 fr_perror("radclient");
1086 req = rad_alloc(NULL, 1);
1088 fr_perror("radclient");
1096 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1098 perror("/dev/urandom");
1100 fread(randctx.randrsl, 256, 1, randinit);
1104 fr_randinit(&randctx, 1);
1112 if (force_af == AF_UNSPEC) force_af = AF_INET;
1113 req->dst_ipaddr.af = force_af;
1114 if (strcmp(argv[1], "-") != 0) {
1115 char const *hostname = argv[1];
1116 char const *portname = argv[1];
1119 if (*argv[1] == '[') { /* IPv6 URL encoded */
1120 p = strchr(argv[1], ']');
1121 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1125 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1126 buffer[p - argv[1] - 1] = '\0';
1132 p = strchr(portname, ':');
1133 if (p && (strchr(p + 1, ':') == NULL)) {
1140 if (ip_hton(hostname, force_af, &req->dst_ipaddr) < 0) {
1141 fprintf(stderr, "radclient: Failed to find IP address for host %s: %s\n", hostname, strerror(errno));
1146 * Strip port from hostname if needed.
1148 if (portname) port = atoi(portname);
1152 * See what kind of request we want to send.
1154 if (strcmp(argv[2], "auth") == 0) {
1155 if (port == 0) port = getport("radius");
1156 if (port == 0) port = PW_AUTH_UDP_PORT;
1157 req->code = PW_AUTHENTICATION_REQUEST;
1159 } else if (strcmp(argv[2], "acct") == 0) {
1160 if (port == 0) port = getport("radacct");
1161 if (port == 0) port = PW_ACCT_UDP_PORT;
1162 req->code = PW_ACCOUNTING_REQUEST;
1165 } else if (strcmp(argv[2], "status") == 0) {
1166 if (port == 0) port = getport("radius");
1167 if (port == 0) port = PW_AUTH_UDP_PORT;
1168 req->code = PW_STATUS_SERVER;
1170 } else if (strcmp(argv[2], "disconnect") == 0) {
1171 if (port == 0) port = PW_POD_UDP_PORT;
1172 req->code = PW_DISCONNECT_REQUEST;
1174 } else if (isdigit((int) argv[2][0])) {
1175 if (port == 0) port = getport("radius");
1176 if (port == 0) port = PW_AUTH_UDP_PORT;
1177 req->code = atoi(argv[2]);
1181 req->dst_port = port;
1186 if (argv[3]) secret = argv[3];
1190 * Maybe read them, from stdin, if there's no
1191 * filename, or if the filename is '-'.
1193 if (filename && (strcmp(filename, "-") != 0)) {
1194 fp = fopen(filename, "r");
1196 fprintf(stderr, "radclient: Error opening %s: %s\n",
1197 filename, strerror(errno));
1207 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1208 perror("radclient: socket: ");
1213 if(req->vps) pairfree(&req->vps);
1215 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1225 printf("\n\t Total approved auths: %d\n", totalapp);
1226 printf("\t Total denied auths: %d\n", totaldeny);
1232 * given a radius request with some attributes in the EAP range, build
1233 * them all into a single EAP-Message body.
1235 * Note that this function will build multiple EAP-Message bodies
1236 * if there are multiple eligible EAP-types. This is incorrect, as the
1237 * recipient will in fact concatenate them.
1239 * XXX - we could break the loop once we process one type. Maybe this
1240 * just deserves an assert?
1243 static void map_eap_methods(RADIUS_PACKET *req)
1245 VALUE_PAIR *vp, *vpnext;
1250 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY);
1252 id = ((int)getpid() & 0xff);
1254 id = vp->vp_integer;
1257 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE, 0, TAG_ANY);
1259 eapcode = PW_EAP_REQUEST;
1261 eapcode = vp->vp_integer;
1265 for(vp = req->vps; vp != NULL; vp = vpnext) {
1266 /* save it in case it changes! */
1269 if(vp->da->attribute >= ATTRIBUTE_EAP_BASE &&
1270 vp->da->attribute < ATTRIBUTE_EAP_BASE+256) {
1279 eap_method = vp->da->attribute - ATTRIBUTE_EAP_BASE;
1281 switch(eap_method) {
1282 case PW_EAP_IDENTITY:
1283 case PW_EAP_NOTIFICATION:
1294 * no known special handling, it is just encoded as an
1295 * EAP-message with the given type.
1298 /* nuke any existing EAP-Messages */
1299 pairdelete(&req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1301 memset(&ep, 0, sizeof(ep));
1304 ep.type.num = eap_method;
1305 ep.type.length = vp->length;
1306 ep.type.data = vp->vp_octets; /* no need for copy */
1307 eap_basic_compose(req, &ep);
1312 * given a radius request with an EAP-Message body, decode it specific
1315 static void unmap_eap_methods(RADIUS_PACKET *rep)
1318 eap_packet_raw_t *e;
1322 /* find eap message */
1323 e = eap_vp2packet(NULL, rep->vps);
1325 /* nothing to do! */
1328 /* create EAP-ID and EAP-CODE attributes to start */
1329 eap1 = paircreate(rep, ATTRIBUTE_EAP_ID, 0, PW_TYPE_INTEGER);
1330 eap1->vp_integer = e->id;
1331 pairadd(&(rep->vps), eap1);
1333 eap1 = paircreate(rep, ATTRIBUTE_EAP_CODE, 0, PW_TYPE_INTEGER);
1334 eap1->vp_integer = e->code;
1335 pairadd(&(rep->vps), eap1);
1340 case PW_EAP_SUCCESS:
1341 case PW_EAP_FAILURE:
1345 case PW_EAP_REQUEST:
1346 case PW_EAP_RESPONSE:
1347 /* there is a type field, which we use to create
1348 * a new attribute */
1350 /* the length was decode already into the attribute
1351 * length, and was checked already. Network byte
1352 * order, just pull it out using math.
1354 len = e->length[0]*256 + e->length[1];
1356 /* verify the length is big enough to hold type */
1365 type += ATTRIBUTE_EAP_BASE;
1368 if(len > MAX_STRING_LEN) {
1369 len = MAX_STRING_LEN;
1372 eap1 = paircreate(rep, type, 0);
1373 memcpy(eap1->vp_strvalue, &e->data[1], len);
1375 pairadd(&(rep->vps), eap1);
1383 static int map_eapsim_types(RADIUS_PACKET *r)
1388 memset(&ep, 0, sizeof(ep));
1389 ret = map_eapsim_basictypes(r, &ep);
1393 eap_basic_compose(r, &ep);
1398 static int unmap_eapsim_types(RADIUS_PACKET *r)
1402 esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM, 0, TAG_ANY);
1404 ERROR("eap: EAP-Sim attribute not found");
1408 return unmap_eapsim_basictypes(r, esvp->vp_octets, esvp->length);
1415 char const *radius_dir = RADDBDIR;
1417 int radlog(int lvl, char const *msg, ...)
1423 r = vfprintf(stderr, msg, ap);
1425 fputc('\n', stderr);
1430 main(int argc, char *argv[])
1433 RADIUS_PACKET *req,*req2;
1434 VALUE_PAIR *vp, *vpkey, *vpextra;
1435 extern unsigned int sha1_data_problems;
1442 sha1_data_problems = 1;
1445 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1446 fr_perror("radclient");
1450 req = rad_alloc(NULL, 1)
1452 fr_perror("radclient");
1456 req2 = rad_alloc(NULL, 1);
1458 fr_perror("radclient");
1463 if(req->vps) pairfree(&req->vps);
1464 if(req2->vps) pairfree(&req2->vps);
1466 if ((req->vps = readvp2(stdin, &filedone, "eapsimlib:")) == NULL) {
1470 if (fr_debug_flag > 1) {
1471 printf("\nRead:\n");
1472 vp_printlist(stdout, req->vps);
1475 map_eapsim_types(req);
1476 map_eap_methods(req);
1478 if (fr_debug_flag > 1) {
1479 printf("Mapped to:\n");
1480 vp_printlist(stdout, req->vps);
1483 /* find the EAP-Message, copy it to req2 */
1484 vp = paircopy2(NULL, req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1488 pairadd(&req2->vps, vp);
1490 /* only call unmap for sim types here */
1491 unmap_eap_methods(req2);
1492 unmap_eapsim_types(req2);
1494 if (fr_debug_flag > 1) {
1495 printf("Unmapped to:\n");
1496 vp_printlist(stdout, req2->vps);
1499 vp = pairfind(req2->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
1500 vpkey = pairfind(req->vps, ATTRIBUTE_EAP_SIM_KEY, 0, TAG_ANY);
1501 vpextra = pairfind(req->vps, ATTRIBUTE_EAP_SIM_EXTRA, 0, TAG_ANY);
1503 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1504 uint8_t calcmac[16];
1506 /* find the EAP-Message, copy it to req2 */
1508 memset(calcmac, 0, sizeof(calcmac));
1509 printf("Confirming MAC...");
1510 if(eapsim_checkmac(req2->vps, vpkey->vp_strvalue,
1511 vpextra->vp_strvalue, vpextra->length,
1513 printf("succeed\n");
1517 printf("calculated MAC (");
1518 for (i = 0; i < 20; i++) {
1525 printf("%02x", calcmac[i]);
1527 printf(" did not match\n");