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 bool filedone = false;
50 static int totalapp = 0;
51 static int totaldeny = 0;
52 static char filesecret[256];
53 char const *radius_dir = NULL;
54 char const *dict_dir = NULL;
55 char const *progname = "radeapclient";
56 /* fr_randctx randctx; */
58 struct main_config_t main_config;
59 char const *radiusd_version = "";
62 #include <freeradius-devel/tls.h>
65 log_dst_t radlog_dest = L_DST_STDERR;
66 char const *radlog_dir = NULL;
67 log_debug_t debug_flag = 0;
70 struct eapsim_keys eapsim_mk;
72 static void map_eap_methods(RADIUS_PACKET *req);
73 static void unmap_eap_methods(RADIUS_PACKET *rep);
74 static int map_eapsim_types(RADIUS_PACKET *r);
75 static int unmap_eapsim_types(RADIUS_PACKET *r);
77 void debug_pair_list(UNUSED VALUE_PAIR *vp)
82 static void NEVER_RETURNS usage(void)
84 fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
86 fprintf(stderr, " <command> One of auth, acct, status, or disconnect.\n");
87 fprintf(stderr, " -d raddb Set dictionary directory.\n");
88 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
89 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retries' times.\n");
90 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying (may be a floating point number).\n");
91 fprintf(stderr, " -h Print usage help information.\n");
92 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
93 fprintf(stderr, " -S file read secret from file, not command line.\n");
94 fprintf(stderr, " -q Do not print anything out.\n");
95 fprintf(stderr, " -s Print out summary information of auth results.\n");
96 fprintf(stderr, " -v Show program version information.\n");
97 fprintf(stderr, " -x Debugging mode.\n");
98 fprintf(stderr, " -4 Use IPv4 address of server\n");
99 fprintf(stderr, " -6 Use IPv6 address of server.\n");
104 int radlog(UNUSED log_type_t lvl, char const *fmt, ...)
110 r = vfprintf(stderr, fmt, ap);
117 void vradlog_request(UNUSED log_type_t lvl, UNUSED log_debug_t priority,
118 UNUSED REQUEST *request, char const *msg, va_list ap)
120 vfprintf(stderr, msg, ap);
124 static uint16_t getport(char const *name)
128 svp = getservbyname (name, "udp");
133 return ntohs(svp->s_port);
138 static void debug_packet(RADIUS_PACKET *packet, int direction)
142 char const *received, *from;
143 fr_ipaddr_t const *ip;
148 if (direction == 0) {
149 received = "Received";
150 from = "from"; /* what else? */
151 ip = &packet->src_ipaddr;
152 port = packet->src_port;
155 received = "Sending";
156 from = "to"; /* hah! */
157 ip = &packet->dst_ipaddr;
158 port = packet->dst_port;
162 * Client-specific debugging re-prints the input
163 * packet into the client log.
165 * This really belongs in a utility library
167 if (is_radius_code(packet->code)) {
168 printf("%s %s packet %s host %s port %d, id=%d, length=%d\n",
169 received, fr_packet_codes[packet->code], from,
170 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
171 port, packet->id, (int) packet->data_len);
173 printf("%s packet %s host %s port %d code=%d, id=%d, length=%d\n",
175 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
177 packet->code, packet->id, (int) packet->data_len);
180 for (vp = packet->vps; vp != NULL; vp = vp->next) {
181 vp_prints(buffer, sizeof(buffer), vp);
182 printf("\t%s\n", buffer);
188 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
193 if (!req || !rep || !*rep) return -1;
195 for (i = 0; i < retries; i++) {
198 debug_packet(req, R_SENT);
200 if (rad_send(req, NULL, secret) < 0) {
201 fr_perror("radeapclient");
205 /* And wait for reply, timing out as necessary */
207 FD_SET(req->sockfd, &rdfdesc);
209 tv.tv_sec = (int)timeout;
210 tv.tv_usec = 1000000 * (timeout - (int) timeout);
212 /* Something's wrong if we don't get exactly one fd. */
213 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
217 *rep = rad_recv(req->sockfd, 0);
220 * If we get a response from a machine
221 * which we did NOT send a request to,
224 if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
225 (memcmp(&(*rep)->src_ipaddr.ipaddr,
226 &req->dst_ipaddr.ipaddr,
227 ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
228 sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
229 sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
230 ((*rep)->src_port != req->dst_port)) {
231 char src[128], dst[128];
233 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
234 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
235 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
237 src, (*rep)->src_port);
241 } else { /* NULL: couldn't receive the packet */
242 fr_perror("radclient");
247 /* No response or no data read (?) */
249 fprintf(stderr, "radclient: no response from server\n");
254 * FIXME: Discard the packet & listen for another.
256 * Hmm... we should really be using eapol_test, which does
257 * a lot more than radeapclient.
259 if (rad_verify(*rep, req, secret) != 0) {
260 fr_perror("rad_verify");
264 if (rad_decode(*rep, req, secret) != 0) {
265 fr_perror("rad_decode");
269 /* libradius debug already prints out the value pairs for us */
270 if (!fr_debug_flag && do_output) {
271 debug_packet(*rep, R_RECV);
273 if((*rep)->code == PW_CODE_AUTHENTICATION_ACK) {
275 } else if ((*rep)->code == PW_CODE_AUTHENTICATION_REJECT) {
282 static void cleanresp(RADIUS_PACKET *resp)
284 VALUE_PAIR *vpnext, *vp, **last;
288 * maybe should just copy things we care about, or keep
289 * a copy of the original input and start from there again?
291 pairdelete(&resp->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
292 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY, 0, TAG_ANY);
295 for(vp = *last; vp != NULL; vp = vpnext)
299 if((vp->da->attr > ATTRIBUTE_EAP_BASE &&
300 vp->da->attr <= ATTRIBUTE_EAP_BASE+256) ||
301 (vp->da->attr > ATTRIBUTE_EAP_SIM_BASE &&
302 vp->da->attr <= ATTRIBUTE_EAP_SIM_BASE+256))
313 * we got an EAP-Request/Sim/Start message in a legal state.
315 * pick a supported version, put it into the reply, and insert a nonce.
317 static int process_eap_start(RADIUS_PACKET *req,
320 VALUE_PAIR *vp, *newvp;
321 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
322 uint16_t const *versions;
323 uint16_t selectedversion;
324 unsigned int i,versioncount;
326 /* form new response clear of any EAP stuff */
329 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST, 0, TAG_ANY)) == NULL) {
330 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
334 versions = (uint16_t const *) vp->vp_strvalue;
336 /* verify that the attribute length is big enough for a length field */
339 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %u\n", (unsigned int) vp->length);
343 versioncount = ntohs(versions[0])/2;
344 /* verify that the attribute length is big enough for the given number
345 * of versions present.
347 if((unsigned)vp->length <= (versioncount*2 + 2))
349 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %u bytes\n", versioncount, (unsigned int) vp->length);
354 * record the versionlist for the MK calculation.
356 eapsim_mk.versionlistlen = versioncount*2;
357 memcpy(eapsim_mk.versionlist, (unsigned char const *)(versions+1),
358 eapsim_mk.versionlistlen);
360 /* walk the version list, and pick the one we support, which
361 * at present, is 1, EAP_SIM_VERSION.
364 for(i=0; i < versioncount; i++)
366 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
368 selectedversion=EAP_SIM_VERSION;
372 if(selectedversion == 0)
374 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
375 for(i=0; i < versioncount; i++)
377 fprintf(stderr, "\tfound version %d\n",
378 ntohs(versions[i+1]));
383 * now make sure that we have only FULLAUTH_ID_REQ.
384 * I think that it actually might not matter - we can answer in
385 * anyway we like, but it is illegal to have more than one
388 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ, 0, TAG_ANY);
389 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ, 0, TAG_ANY);
390 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ, 0, TAG_ANY);
392 if(!fullauthidreq_vp ||
393 anyidreq_vp != NULL ||
394 permanentidreq_vp != NULL) {
395 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
396 (anyidreq_vp != NULL ? "a " : "no "),
397 (fullauthidreq_vp != NULL ? "a " : "no "),
398 (permanentidreq_vp != NULL ? "a " : "no "));
402 /* okay, we have just any_id_req there, so fill in response */
404 /* mark the subtype as being EAP-SIM/Response/Start */
405 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_SUBTYPE, 0);
406 newvp->vp_integer = eapsim_start;
407 pairreplace(&(rep->vps), newvp);
409 /* insert selected version into response. */
411 uint16_t no_versions;
413 no_versions = htons(selectedversion);
415 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE + PW_EAP_SIM_SELECTED_VERSION, 0);
416 pairmemcpy(newvp, (uint8_t *) &no_versions, 2);
417 pairreplace(&(rep->vps), newvp);
419 /* record the selected version */
420 memcpy(eapsim_mk.versionselect, &no_versions, 2);
429 * insert a nonce_mt that we make up.
436 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT, 0);
438 p = talloc_zero_array(newvp, uint8_t, 18); /* 18 = 16 bytes of nonce + padding */
439 memcpy(&p[2], nonce, 16);
440 pairmemsteal(newvp, p);
442 pairreplace(&(rep->vps), newvp);
444 /* also keep a copy of the nonce! */
445 memcpy(eapsim_mk.nonce_mt, nonce, 16);
454 * insert the identity here.
456 vp = pairfind(rep->vps, PW_USER_NAME, 0, TAG_ANY);
459 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
462 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY, 0);
464 idlen = strlen(vp->vp_strvalue);
465 p = talloc_zero_array(newvp, uint8_t, idlen + 2);
466 no_idlen = htons(idlen);
467 memcpy(p, &no_idlen, 2);
468 memcpy(p + 2, vp->vp_strvalue, idlen);
469 pairmemsteal(newvp, p);
471 pairreplace(&(rep->vps), newvp);
474 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
475 eapsim_mk.identitylen = idlen;
482 * we got an EAP-Request/Sim/Challenge message in a legal state.
484 * use the RAND challenge to produce the SRES result, and then
485 * use that to generate a new MAC.
487 * for the moment, we ignore the RANDs, then just plug in the SRES
491 static int process_eap_challenge(RADIUS_PACKET *req,
495 VALUE_PAIR *mac, *randvp;
496 VALUE_PAIR *sres1,*sres2,*sres3;
497 VALUE_PAIR *Kc1, *Kc2, *Kc3;
500 /* look for the AT_MAC and the challenge data */
501 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
502 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND, 0, TAG_ANY);
503 if(!mac || !randvp) {
504 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
509 * compare RAND with randX, to verify this is the right response
513 VALUE_PAIR *randcfgvp[3];
514 uint8_t const *randcfg[3];
516 randcfg[0] = &randvp->vp_octets[2];
517 randcfg[1] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE];
518 randcfg[2] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE*2];
520 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1, 0, TAG_ANY);
521 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2, 0, TAG_ANY);
522 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3, 0, TAG_ANY);
527 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
531 if(memcmp(randcfg[0], randcfgvp[0]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
532 memcmp(randcfg[1], randcfgvp[1]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
533 memcmp(randcfg[2], randcfgvp[2]->vp_octets, EAPSIM_RAND_SIZE)!=0) {
536 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
537 for(rnum = 0; rnum < 3; rnum++) {
538 fprintf(stderr, "received rand %d: ", rnum);
540 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
547 fprintf(stderr, "%02x", randcfg[rnum][i]);
549 fprintf(stderr, "\nconfigured rand %d: ", rnum);
551 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
558 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
560 fprintf(stderr, "\n");
567 * now dig up the sres values from the response packet,
568 * which were put there when we read things in.
570 * Really, they should be calculated from the RAND!
573 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1, 0, TAG_ANY);
574 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2, 0, TAG_ANY);
575 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3, 0, TAG_ANY);
580 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
583 memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
584 memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
585 memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
587 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1, 0, TAG_ANY);
588 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2, 0, TAG_ANY);
589 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3, 0, TAG_ANY);
594 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
597 memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
598 memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
599 memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
601 /* all set, calculate keys */
602 eapsim_calculate_keys(&eapsim_mk);
605 eapsim_dump_mk(&eapsim_mk);
608 /* verify the MAC, now that we have all the keys. */
609 if(eapsim_checkmac(NULL, req->vps, eapsim_mk.K_aut,
610 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
612 printf("MAC check succeed\n");
616 printf("calculated MAC (");
617 for (i = 0; i < 20; i++) {
624 printf("%02x", calcmac[i]);
626 printf(" did not match\n");
630 /* form new response clear of any EAP stuff */
633 /* mark the subtype as being EAP-SIM/Response/Start */
634 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_SUBTYPE, 0);
635 newvp->vp_integer = eapsim_challenge;
636 pairreplace(&(rep->vps), newvp);
641 * fill the SIM_MAC with a field that will in fact get appended
642 * to the packet before the MAC is calculated
644 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0);
646 p = talloc_zero_array(newvp, uint8_t, EAPSIM_SRES_SIZE*3);
647 memcpy(p+EAPSIM_SRES_SIZE * 0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
648 memcpy(p+EAPSIM_SRES_SIZE * 1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
649 memcpy(p+EAPSIM_SRES_SIZE * 2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
650 pairmemsteal(newvp, p);
652 pairreplace(&(rep->vps), newvp);
655 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_KEY, 0);
656 pairmemcpy(newvp, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
658 pairreplace(&(rep->vps), newvp);
664 * this code runs the EAP-SIM client state machine.
665 * the *request* is from the server.
666 * the *reponse* is to the server.
669 static int respond_eap_sim(RADIUS_PACKET *req,
672 enum eapsim_clientstates state, newstate;
673 enum eapsim_subtype subtype;
674 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
675 char statenamebuf[32], subtypenamebuf[32];
677 if ((radstate = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
682 if ((eapid = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
687 /* first, dig up the state from the request packet, setting
688 * outselves to be in EAP-SIM-Start state if there is none.
691 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE, 0, TAG_ANY)) == NULL)
693 /* must be initial request */
694 statevp = paircreate(resp, ATTRIBUTE_EAP_SIM_STATE, 0);
695 statevp->vp_integer = eapsim_client_init;
696 pairreplace(&(resp->vps), statevp);
698 state = statevp->vp_integer;
701 * map the attributes, and authenticate them.
703 unmap_eapsim_types(req);
705 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE, 0, TAG_ANY)) == NULL)
709 subtype = vp->vp_integer;
712 * look for the appropriate state, and process incoming message
715 case eapsim_client_init:
718 newstate = process_eap_start(req, resp);
721 case eapsim_challenge:
722 case eapsim_notification:
725 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
726 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
727 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
728 /* invalid state, drop message */
733 case eapsim_client_start:
736 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
737 newstate = process_eap_start(req, resp);
740 case eapsim_challenge:
741 newstate = process_eap_challenge(req, resp);
745 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
746 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
747 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
748 /* invalid state, drop message */
755 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
756 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
760 /* copy the eap state object in */
761 pairreplace(&(resp->vps), eapid);
763 /* update stete info, and send new packet */
764 map_eapsim_types(resp);
766 /* copy the radius state object in */
767 pairreplace(&(resp->vps), radstate);
769 statevp->vp_integer = newstate;
773 static int respond_eap_md5(RADIUS_PACKET *req,
776 VALUE_PAIR *vp, *id, *state;
779 uint8_t const *value;
781 uint8_t response[16];
785 if ((state = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
787 fprintf(stderr, "radeapclient: no state attribute found\n");
791 if ((id = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
793 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
796 identifier = id->vp_integer;
798 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0, TAG_ANY)) == NULL)
800 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
804 /* got the details of the MD5 challenge */
805 valuesize = vp->vp_octets[0];
806 value = &vp->vp_octets[1];
809 if(valuesize > vp->length)
811 fprintf(stderr, "radeapclient: md5 valuesize if too big (%u > %u)\n",
812 (unsigned int) valuesize, (unsigned int) vp->length);
816 /* now do the CHAP operation ourself, rather than build the
817 * buffer. We could also call rad_chap_encode, but it wants
818 * a CHAP-Challenge, which we don't want to bother with.
820 fr_MD5Init(&context);
821 fr_MD5Update(&context, &identifier, 1);
822 fr_MD5Update(&context, (uint8_t *) password, strlen(password));
823 fr_MD5Update(&context, value, valuesize);
824 fr_MD5Final(response, &context);
830 vp = paircreate(rep, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0);
833 p = talloc_zero_array(vp, uint8_t, 17);
835 memcpy(p, &lg_response, 1);
836 memcpy(p + 1, response, 16);
839 pairreplace(&(rep->vps), vp);
841 pairreplace(&(rep->vps), id);
843 /* copy the state object in */
844 pairreplace(&(rep->vps), state);
851 static int sendrecv_eap(RADIUS_PACKET *rep)
853 RADIUS_PACKET *req = NULL;
854 VALUE_PAIR *vp, *vpnext;
855 int tried_eap_md5 = 0;
860 * Keep a copy of the the User-Password attribute.
862 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
863 strlcpy(password, vp->vp_strvalue, sizeof(password));
865 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
866 strlcpy(password, vp->vp_strvalue, sizeof(password));
868 * Otherwise keep a copy of the CHAP-Password attribute.
870 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
871 strlcpy(password, vp->vp_strvalue, sizeof(password));
880 * if there are EAP types, encode them into an EAP-Message
883 map_eap_methods(rep);
886 * Fix up Digest-Attributes issues
888 for (vp = rep->vps; vp != NULL; vp = vp->next) {
889 switch (vp->da->attr) {
893 case PW_DIGEST_REALM:
894 case PW_DIGEST_NONCE:
895 case PW_DIGEST_METHOD:
898 case PW_DIGEST_ALGORITHM:
899 case PW_DIGEST_BODY_DIGEST:
900 case PW_DIGEST_CNONCE:
901 case PW_DIGEST_NONCE_COUNT:
902 case PW_DIGEST_USER_NAME:
908 p = talloc_array(vp, uint8_t, vp->length + 2);
910 memcpy(p + 2, vp->vp_octets, vp->length);
911 p[0] = vp->da->attr - PW_DIGEST_REALM + 1;
915 da = dict_attrbyvalue(PW_DIGEST_ATTRIBUTES, 0);
919 * Re-do pairmemsteal ourselves,
920 * because we play games with
921 * vp->da, and pairmemsteal goes
922 * to GREAT lengths to sanitize
923 * and fix and change and
924 * double-check the various
927 memcpy(&q, &vp->vp_octets, sizeof(q));
930 vp->vp_octets = talloc_steal(vp, p);
940 * If we've already sent a packet, free up the old
941 * one, and ensure that the next packet has a unique
942 * ID and authentication vector.
945 talloc_free(rep->data);
949 fr_md5_calc(rep->vector, rep->vector,
950 sizeof(rep->vector));
952 if (*password != '\0') {
953 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
954 pairstrcpy(vp, password);
956 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
957 pairstrcpy(vp, password);
959 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
960 pairstrcpy(vp, password);
963 p = talloc_zero_array(vp, uint8_t, 17);
964 rad_chap_encode(rep, p, rep->id, vp);
967 } /* there WAS a password */
969 /* send the response, wait for the next request */
970 send_packet(rep, &req);
974 /* okay got back the packet, go and decode the EAP-Message. */
975 unmap_eap_methods(req);
977 debug_packet(req, R_RECV);
979 /* now look for the code type. */
980 for (vp = req->vps; vp != NULL; vp = vpnext) {
983 switch (vp->da->attr) {
987 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
988 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
995 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
996 if(respond_eap_sim(req, rep))
1008 void set_radius_dir(TALLOC_CTX *ctx, char const *path)
1013 memcpy(&p, &radius_dir, sizeof(p));
1017 if (path) radius_dir = talloc_strdup(ctx, path);
1021 int main(int argc, char **argv)
1027 char *filename = NULL;
1030 int force_af = AF_UNSPEC;
1033 * We probably don't want to free the talloc autofree context
1034 * directly, so we'll allocate a new context beneath it, and
1035 * free that before any leak reports.
1037 TALLOC_CTX *autofree = talloc_init("main");
1039 id = ((int)getpid() & 0xff);
1042 radlog_dest = L_DST_STDERR;
1044 set_radius_dir(autofree, RADIUS_DIR);
1046 while ((c = getopt(argc, argv, "46c:d:D:f:hi:qst:r:S:xXv")) != EOF)
1053 force_af = AF_INET6;
1056 set_radius_dir(autofree, optarg);
1059 main_config.dictionary_dir = talloc_typed_strdup(NULL, optarg);
1074 sha1_data_problems = 1; /* for debugging only */
1081 if (!isdigit((int) *optarg))
1083 retries = atoi(optarg);
1086 if (!isdigit((int) *optarg))
1089 if ((id < 0) || (id > 255)) {
1097 if (!isdigit((int) *optarg))
1099 timeout = atof(optarg);
1102 printf("radeapclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
1106 fp = fopen(optarg, "r");
1108 fprintf(stderr, "radclient: Error opening %s: %s\n",
1109 optarg, fr_syserror(errno));
1112 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1113 fprintf(stderr, "radclient: Error reading %s: %s\n",
1114 optarg, fr_syserror(errno));
1119 /* truncate newline */
1120 p = filesecret + strlen(filesecret) - 1;
1121 while ((p >= filesecret) &&
1127 if (strlen(filesecret) < 2) {
1128 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1131 secret = filesecret;
1139 argc -= (optind - 1);
1140 argv += (optind - 1);
1143 ((!secret) && (argc < 4))) {
1147 if (!main_config.dictionary_dir) {
1148 main_config.dictionary_dir = DICTDIR;
1152 * Read the distribution dictionaries first, then
1153 * the ones in raddb.
1155 DEBUG2("including dictionary file %s/%s", main_config.dictionary_dir, RADIUS_DICTIONARY);
1156 if (dict_init(main_config.dictionary_dir, RADIUS_DICTIONARY) != 0) {
1157 ERROR("Errors reading dictionary: %s",
1163 * It's OK if this one doesn't exist.
1165 int rcode = dict_read(radius_dir, RADIUS_DICTIONARY);
1167 ERROR("Errors reading %s/%s: %s", radius_dir, RADIUS_DICTIONARY,
1173 * We print this after reading it. That way if
1174 * it doesn't exist, it's OK, and we don't print
1178 DEBUG2("including dictionary file %s/%s", radius_dir, RADIUS_DICTIONARY);
1181 req = rad_alloc(NULL, 1);
1183 fr_perror("radclient");
1191 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1193 perror("/dev/urandom");
1195 fread(randctx.randrsl, 256, 1, randinit);
1199 fr_randinit(&randctx, 1);
1207 if (force_af == AF_UNSPEC) force_af = AF_INET;
1208 req->dst_ipaddr.af = force_af;
1209 if (strcmp(argv[1], "-") != 0) {
1210 char const *hostname = argv[1];
1211 char const *portname = argv[1];
1214 if (*argv[1] == '[') { /* IPv6 URL encoded */
1215 p = strchr(argv[1], ']');
1216 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1220 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1221 buffer[p - argv[1] - 1] = '\0';
1227 p = strchr(portname, ':');
1228 if (p && (strchr(p + 1, ':') == NULL)) {
1235 if (ip_hton(hostname, force_af, &req->dst_ipaddr) < 0) {
1236 fprintf(stderr, "radclient: Failed to find IP address for host %s: %s\n", hostname, fr_syserror(errno));
1241 * Strip port from hostname if needed.
1243 if (portname) port = atoi(portname);
1247 * See what kind of request we want to send.
1249 if (strcmp(argv[2], "auth") == 0) {
1250 if (port == 0) port = getport("radius");
1251 if (port == 0) port = PW_AUTH_UDP_PORT;
1252 req->code = PW_CODE_AUTHENTICATION_REQUEST;
1254 } else if (strcmp(argv[2], "acct") == 0) {
1255 if (port == 0) port = getport("radacct");
1256 if (port == 0) port = PW_ACCT_UDP_PORT;
1257 req->code = PW_CODE_ACCOUNTING_REQUEST;
1260 } else if (strcmp(argv[2], "status") == 0) {
1261 if (port == 0) port = getport("radius");
1262 if (port == 0) port = PW_AUTH_UDP_PORT;
1263 req->code = PW_CODE_STATUS_SERVER;
1265 } else if (strcmp(argv[2], "disconnect") == 0) {
1266 if (port == 0) port = PW_POD_UDP_PORT;
1267 req->code = PW_CODE_DISCONNECT_REQUEST;
1269 } else if (isdigit((int) argv[2][0])) {
1270 if (port == 0) port = getport("radius");
1271 if (port == 0) port = PW_AUTH_UDP_PORT;
1272 req->code = atoi(argv[2]);
1276 req->dst_port = port;
1281 if (argv[3]) secret = argv[3];
1285 * Maybe read them, from stdin, if there's no
1286 * filename, or if the filename is '-'.
1288 if (filename && (strcmp(filename, "-") != 0)) {
1289 fp = fopen(filename, "r");
1291 fprintf(stderr, "radclient: Error opening %s: %s\n",
1292 filename, fr_syserror(errno));
1302 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1303 perror("radclient: socket: ");
1308 if (req->vps) pairfree(&req->vps);
1309 if (readvp2(&req->vps, NULL, fp, &filedone) < 0) {
1310 fr_perror("radeapclient");
1318 printf("\n\t Total approved auths: %d\n", totalapp);
1319 printf("\t Total denied auths: %d\n", totaldeny);
1322 talloc_free(autofree);
1328 * given a radius request with some attributes in the EAP range, build
1329 * them all into a single EAP-Message body.
1331 * Note that this function will build multiple EAP-Message bodies
1332 * if there are multiple eligible EAP-types. This is incorrect, as the
1333 * recipient will in fact concatenate them.
1335 * XXX - we could break the loop once we process one type. Maybe this
1336 * just deserves an assert?
1339 static void map_eap_methods(RADIUS_PACKET *req)
1341 VALUE_PAIR *vp, *vpnext;
1345 eap_packet_t *pt_ep = talloc_zero(req, eap_packet_t);
1347 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY);
1349 id = ((int)getpid() & 0xff);
1351 id = vp->vp_integer;
1354 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE, 0, TAG_ANY);
1356 eapcode = PW_EAP_REQUEST;
1358 eapcode = vp->vp_integer;
1361 for(vp = req->vps; vp != NULL; vp = vpnext) {
1362 /* save it in case it changes! */
1365 if(vp->da->attr >= ATTRIBUTE_EAP_BASE &&
1366 vp->da->attr < ATTRIBUTE_EAP_BASE+256) {
1375 eap_method = vp->da->attr - ATTRIBUTE_EAP_BASE;
1377 switch(eap_method) {
1378 case PW_EAP_IDENTITY:
1379 case PW_EAP_NOTIFICATION:
1390 * no known special handling, it is just encoded as an
1391 * EAP-message with the given type.
1394 /* nuke any existing EAP-Messages */
1395 pairdelete(&req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1397 pt_ep->code = eapcode;
1399 pt_ep->type.num = eap_method;
1400 pt_ep->type.length = vp->length;
1402 pt_ep->type.data = talloc_memdup(vp, vp->vp_octets, vp->length);
1403 talloc_set_type(pt_ep->type.data, uint8_t);
1405 eap_basic_compose(req, pt_ep);
1410 * given a radius request with an EAP-Message body, decode it specific
1413 static void unmap_eap_methods(RADIUS_PACKET *rep)
1416 eap_packet_raw_t *e;
1422 /* find eap message */
1423 e = eap_vp2packet(NULL, rep->vps);
1425 /* nothing to do! */
1428 /* create EAP-ID and EAP-CODE attributes to start */
1429 eap1 = paircreate(rep, ATTRIBUTE_EAP_ID, 0);
1430 eap1->vp_integer = e->id;
1431 pairadd(&(rep->vps), eap1);
1433 eap1 = paircreate(rep, ATTRIBUTE_EAP_CODE, 0);
1434 eap1->vp_integer = e->code;
1435 pairadd(&(rep->vps), eap1);
1439 case PW_EAP_SUCCESS:
1440 case PW_EAP_FAILURE:
1444 case PW_EAP_REQUEST:
1445 case PW_EAP_RESPONSE:
1446 /* there is a type field, which we use to create
1447 * a new attribute */
1449 /* the length was decode already into the attribute
1450 * length, and was checked already. Network byte
1451 * order, just pull it out using math.
1453 len = e->length[0]*256 + e->length[1];
1455 /* verify the length is big enough to hold type */
1464 type += ATTRIBUTE_EAP_BASE;
1467 if (len > MAX_STRING_LEN) {
1468 len = MAX_STRING_LEN;
1471 eap1 = paircreate(rep, type, 0);
1472 pairmemcpy(eap1, e->data + 1, len);
1474 pairadd(&(rep->vps), eap1);
1482 static int map_eapsim_types(RADIUS_PACKET *r)
1486 eap_packet_t *pt_ep = talloc_zero(r, eap_packet_t);
1488 ret = map_eapsim_basictypes(r, pt_ep);
1494 eap_basic_compose(r, pt_ep);
1499 static int unmap_eapsim_types(RADIUS_PACKET *r)
1505 esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM, 0, TAG_ANY);
1507 ERROR("eap: EAP-Sim attribute not found");
1511 eap_data = talloc_memdup(esvp, esvp->vp_octets, esvp->length);
1512 talloc_set_type(eap_data, uint8_t);
1514 rcode_unmap = unmap_eapsim_basictypes(r, eap_data, esvp->length);
1516 talloc_free(eap_data);
1524 char const *radius_dir = RADDBDIR;
1526 int radlog(int lvl, char const *msg, ...)
1532 r = vfprintf(stderr, msg, ap);
1534 fputc('\n', stderr);
1539 main(int argc, char *argv[])
1542 RADIUS_PACKET *req,*req2;
1543 VALUE_PAIR *vp, *vpkey, *vpextra;
1544 extern unsigned int sha1_data_problems;
1551 sha1_data_problems = 1;
1554 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1555 fr_perror("radclient");
1559 req = rad_alloc(NULL, 1)
1561 fr_perror("radclient");
1565 req2 = rad_alloc(NULL, 1);
1567 fr_perror("radclient");
1572 if (req->vps) pairfree(&req->vps);
1573 if (req2->vps) pairfree(&req2->vps);
1575 if (readvp2(&req->vps, NULL, stdin, &filedone) < 0) {
1576 fr_perror("radeapclient");
1580 if (fr_debug_flag > 1) {
1581 printf("\nRead:\n");
1582 vp_printlist(stdout, req->vps);
1585 map_eapsim_types(req);
1586 map_eap_methods(req);
1588 if (fr_debug_flag > 1) {
1589 printf("Mapped to:\n");
1590 vp_printlist(stdout, req->vps);
1593 /* find the EAP-Message, copy it to req2 */
1594 vp = paircopy2(NULL, req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1598 pairadd(&req2->vps, vp);
1600 /* only call unmap for sim types here */
1601 unmap_eap_methods(req2);
1602 unmap_eapsim_types(req2);
1604 if (fr_debug_flag > 1) {
1605 printf("Unmapped to:\n");
1606 vp_printlist(stdout, req2->vps);
1609 vp = pairfind(req2->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
1610 vpkey = pairfind(req->vps, ATTRIBUTE_EAP_SIM_KEY, 0, TAG_ANY);
1611 vpextra = pairfind(req->vps, ATTRIBUTE_EAP_SIM_EXTRA, 0, TAG_ANY);
1613 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1614 uint8_t calcmac[16];
1616 /* find the EAP-Message, copy it to req2 */
1618 memset(calcmac, 0, sizeof(calcmac));
1619 printf("Confirming MAC...");
1620 if(eapsim_checkmac(req2->vps, vpkey->vp_strvalue,
1621 vpextra->vp_strvalue, vpextra->length,
1623 printf("succeed\n");
1627 printf("calculated MAC (");
1628 for (i = 0; i < 20; i++) {
1635 printf("%02x", calcmac[i]);
1637 printf(" did not match\n");