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)
81 int rad_virtual_server(REQUEST UNUSED *request)
83 /*We're not the server so we cannot do this*/
87 static void NEVER_RETURNS usage(void)
89 fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
91 fprintf(stderr, " <command> One of auth, acct, status, or disconnect.\n");
92 fprintf(stderr, " -d raddb Set dictionary directory.\n");
93 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
94 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retries' times.\n");
95 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying (may be a floating point number).\n");
96 fprintf(stderr, " -h Print usage help information.\n");
97 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
98 fprintf(stderr, " -S file read secret from file, not command line.\n");
99 fprintf(stderr, " -q Do not print anything out.\n");
100 fprintf(stderr, " -s Print out summary information of auth results.\n");
101 fprintf(stderr, " -v Show program version information.\n");
102 fprintf(stderr, " -x Debugging mode.\n");
103 fprintf(stderr, " -4 Use IPv4 address of server\n");
104 fprintf(stderr, " -6 Use IPv6 address of server.\n");
109 int radlog(UNUSED log_type_t lvl, char const *fmt, ...)
115 r = vfprintf(stderr, fmt, ap);
122 void vradlog_request(UNUSED log_type_t lvl, UNUSED log_debug_t priority,
123 UNUSED REQUEST *request, char const *msg, va_list ap)
125 vfprintf(stderr, msg, ap);
129 static uint16_t getport(char const *name)
133 svp = getservbyname (name, "udp");
138 return ntohs(svp->s_port);
143 static void debug_packet(RADIUS_PACKET *packet, int direction)
147 char const *received, *from;
148 fr_ipaddr_t const *ip;
153 if (direction == 0) {
154 received = "Received";
155 from = "from"; /* what else? */
156 ip = &packet->src_ipaddr;
157 port = packet->src_port;
160 received = "Sending";
161 from = "to"; /* hah! */
162 ip = &packet->dst_ipaddr;
163 port = packet->dst_port;
167 * Client-specific debugging re-prints the input
168 * packet into the client log.
170 * This really belongs in a utility library
172 if (is_radius_code(packet->code)) {
173 printf("%s %s packet %s host %s port %d, id=%d, length=%d\n",
174 received, fr_packet_codes[packet->code], from,
175 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
176 port, packet->id, (int) packet->data_len);
178 printf("%s packet %s host %s port %d code=%d, id=%d, length=%d\n",
180 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
182 packet->code, packet->id, (int) packet->data_len);
185 for (vp = packet->vps; vp != NULL; vp = vp->next) {
186 vp_prints(buffer, sizeof(buffer), vp);
187 printf("\t%s\n", buffer);
193 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
198 if (!req || !rep || !*rep) return -1;
200 for (i = 0; i < retries; i++) {
203 debug_packet(req, R_SENT);
205 if (rad_send(req, NULL, secret) < 0) {
206 fr_perror("radeapclient");
210 /* And wait for reply, timing out as necessary */
212 FD_SET(req->sockfd, &rdfdesc);
214 tv.tv_sec = (int)timeout;
215 tv.tv_usec = 1000000 * (timeout - (int) timeout);
217 /* Something's wrong if we don't get exactly one fd. */
218 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
222 *rep = rad_recv(req->sockfd, 0);
225 * If we get a response from a machine
226 * which we did NOT send a request to,
229 if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
230 (memcmp(&(*rep)->src_ipaddr.ipaddr,
231 &req->dst_ipaddr.ipaddr,
232 ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
233 sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
234 sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
235 ((*rep)->src_port != req->dst_port)) {
236 char src[128], dst[128];
238 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
239 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
240 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
242 src, (*rep)->src_port);
246 } else { /* NULL: couldn't receive the packet */
247 fr_perror("radclient");
252 /* No response or no data read (?) */
254 fprintf(stderr, "radclient: no response from server\n");
259 * FIXME: Discard the packet & listen for another.
261 * Hmm... we should really be using eapol_test, which does
262 * a lot more than radeapclient.
264 if (rad_verify(*rep, req, secret) != 0) {
265 fr_perror("rad_verify");
269 if (rad_decode(*rep, req, secret) != 0) {
270 fr_perror("rad_decode");
274 /* libradius debug already prints out the value pairs for us */
275 if (!fr_debug_flag && do_output) {
276 debug_packet(*rep, R_RECV);
278 if((*rep)->code == PW_CODE_AUTHENTICATION_ACK) {
280 } else if ((*rep)->code == PW_CODE_AUTHENTICATION_REJECT) {
287 static void cleanresp(RADIUS_PACKET *resp)
289 VALUE_PAIR *vpnext, *vp, **last;
293 * maybe should just copy things we care about, or keep
294 * a copy of the original input and start from there again?
296 pairdelete(&resp->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
297 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY, 0, TAG_ANY);
300 for(vp = *last; vp != NULL; vp = vpnext)
304 if((vp->da->attr > ATTRIBUTE_EAP_BASE &&
305 vp->da->attr <= ATTRIBUTE_EAP_BASE+256) ||
306 (vp->da->attr > ATTRIBUTE_EAP_SIM_BASE &&
307 vp->da->attr <= ATTRIBUTE_EAP_SIM_BASE+256))
318 * we got an EAP-Request/Sim/Start message in a legal state.
320 * pick a supported version, put it into the reply, and insert a nonce.
322 static int process_eap_start(RADIUS_PACKET *req,
325 VALUE_PAIR *vp, *newvp;
326 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
327 uint16_t const *versions;
328 uint16_t selectedversion;
329 unsigned int i,versioncount;
331 /* form new response clear of any EAP stuff */
334 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST, 0, TAG_ANY)) == NULL) {
335 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
339 versions = (uint16_t const *) vp->vp_strvalue;
341 /* verify that the attribute length is big enough for a length field */
344 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %u\n", (unsigned int) vp->length);
348 versioncount = ntohs(versions[0])/2;
349 /* verify that the attribute length is big enough for the given number
350 * of versions present.
352 if((unsigned)vp->length <= (versioncount*2 + 2))
354 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %u bytes\n", versioncount, (unsigned int) vp->length);
359 * record the versionlist for the MK calculation.
361 eapsim_mk.versionlistlen = versioncount*2;
362 memcpy(eapsim_mk.versionlist, (unsigned char const *)(versions+1),
363 eapsim_mk.versionlistlen);
365 /* walk the version list, and pick the one we support, which
366 * at present, is 1, EAP_SIM_VERSION.
369 for(i=0; i < versioncount; i++)
371 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
373 selectedversion=EAP_SIM_VERSION;
377 if(selectedversion == 0)
379 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
380 for(i=0; i < versioncount; i++)
382 fprintf(stderr, "\tfound version %d\n",
383 ntohs(versions[i+1]));
388 * now make sure that we have only FULLAUTH_ID_REQ.
389 * I think that it actually might not matter - we can answer in
390 * anyway we like, but it is illegal to have more than one
393 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ, 0, TAG_ANY);
394 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ, 0, TAG_ANY);
395 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ, 0, TAG_ANY);
397 if(!fullauthidreq_vp ||
398 anyidreq_vp != NULL ||
399 permanentidreq_vp != NULL) {
400 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
401 (anyidreq_vp != NULL ? "a " : "no "),
402 (fullauthidreq_vp != NULL ? "a " : "no "),
403 (permanentidreq_vp != NULL ? "a " : "no "));
407 /* okay, we have just any_id_req there, so fill in response */
409 /* mark the subtype as being EAP-SIM/Response/Start */
410 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_SUBTYPE, 0);
411 newvp->vp_integer = eapsim_start;
412 pairreplace(&(rep->vps), newvp);
414 /* insert selected version into response. */
416 uint16_t no_versions;
418 no_versions = htons(selectedversion);
420 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE + PW_EAP_SIM_SELECTED_VERSION, 0);
421 pairmemcpy(newvp, (uint8_t *) &no_versions, 2);
422 pairreplace(&(rep->vps), newvp);
424 /* record the selected version */
425 memcpy(eapsim_mk.versionselect, &no_versions, 2);
434 * insert a nonce_mt that we make up.
441 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT, 0);
443 p = talloc_zero_array(newvp, uint8_t, 18); /* 18 = 16 bytes of nonce + padding */
444 memcpy(&p[2], nonce, 16);
445 pairmemsteal(newvp, p);
447 pairreplace(&(rep->vps), newvp);
449 /* also keep a copy of the nonce! */
450 memcpy(eapsim_mk.nonce_mt, nonce, 16);
459 * insert the identity here.
461 vp = pairfind(rep->vps, PW_USER_NAME, 0, TAG_ANY);
464 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
467 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY, 0);
469 idlen = strlen(vp->vp_strvalue);
470 p = talloc_zero_array(newvp, uint8_t, idlen + 2);
471 no_idlen = htons(idlen);
472 memcpy(p, &no_idlen, 2);
473 memcpy(p + 2, vp->vp_strvalue, idlen);
474 pairmemsteal(newvp, p);
476 pairreplace(&(rep->vps), newvp);
479 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
480 eapsim_mk.identitylen = idlen;
487 * we got an EAP-Request/Sim/Challenge message in a legal state.
489 * use the RAND challenge to produce the SRES result, and then
490 * use that to generate a new MAC.
492 * for the moment, we ignore the RANDs, then just plug in the SRES
496 static int process_eap_challenge(RADIUS_PACKET *req,
500 VALUE_PAIR *mac, *randvp;
501 VALUE_PAIR *sres1,*sres2,*sres3;
502 VALUE_PAIR *Kc1, *Kc2, *Kc3;
505 /* look for the AT_MAC and the challenge data */
506 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
507 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND, 0, TAG_ANY);
508 if(!mac || !randvp) {
509 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
514 * compare RAND with randX, to verify this is the right response
518 VALUE_PAIR *randcfgvp[3];
519 uint8_t const *randcfg[3];
521 randcfg[0] = &randvp->vp_octets[2];
522 randcfg[1] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE];
523 randcfg[2] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE*2];
525 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1, 0, TAG_ANY);
526 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2, 0, TAG_ANY);
527 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3, 0, TAG_ANY);
532 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
536 if(memcmp(randcfg[0], randcfgvp[0]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
537 memcmp(randcfg[1], randcfgvp[1]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
538 memcmp(randcfg[2], randcfgvp[2]->vp_octets, EAPSIM_RAND_SIZE)!=0) {
541 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
542 for(rnum = 0; rnum < 3; rnum++) {
543 fprintf(stderr, "received rand %d: ", rnum);
545 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
552 fprintf(stderr, "%02x", randcfg[rnum][i]);
554 fprintf(stderr, "\nconfigured rand %d: ", rnum);
556 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
563 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
565 fprintf(stderr, "\n");
572 * now dig up the sres values from the response packet,
573 * which were put there when we read things in.
575 * Really, they should be calculated from the RAND!
578 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1, 0, TAG_ANY);
579 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2, 0, TAG_ANY);
580 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3, 0, TAG_ANY);
585 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
588 memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
589 memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
590 memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
592 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1, 0, TAG_ANY);
593 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2, 0, TAG_ANY);
594 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3, 0, TAG_ANY);
599 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
602 memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
603 memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
604 memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
606 /* all set, calculate keys */
607 eapsim_calculate_keys(&eapsim_mk);
610 eapsim_dump_mk(&eapsim_mk);
613 /* verify the MAC, now that we have all the keys. */
614 if(eapsim_checkmac(NULL, req->vps, eapsim_mk.K_aut,
615 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
617 printf("MAC check succeed\n");
621 printf("calculated MAC (");
622 for (i = 0; i < 20; i++) {
629 printf("%02x", calcmac[i]);
631 printf(" did not match\n");
635 /* form new response clear of any EAP stuff */
638 /* mark the subtype as being EAP-SIM/Response/Start */
639 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_SUBTYPE, 0);
640 newvp->vp_integer = eapsim_challenge;
641 pairreplace(&(rep->vps), newvp);
646 * fill the SIM_MAC with a field that will in fact get appended
647 * to the packet before the MAC is calculated
649 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0);
651 p = talloc_zero_array(newvp, uint8_t, EAPSIM_SRES_SIZE*3);
652 memcpy(p+EAPSIM_SRES_SIZE * 0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
653 memcpy(p+EAPSIM_SRES_SIZE * 1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
654 memcpy(p+EAPSIM_SRES_SIZE * 2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
655 pairmemsteal(newvp, p);
657 pairreplace(&(rep->vps), newvp);
660 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_KEY, 0);
661 pairmemcpy(newvp, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
663 pairreplace(&(rep->vps), newvp);
669 * this code runs the EAP-SIM client state machine.
670 * the *request* is from the server.
671 * the *reponse* is to the server.
674 static int respond_eap_sim(RADIUS_PACKET *req,
677 enum eapsim_clientstates state, newstate;
678 enum eapsim_subtype subtype;
679 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
680 char statenamebuf[32], subtypenamebuf[32];
682 if ((radstate = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
687 if ((eapid = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
692 /* first, dig up the state from the request packet, setting
693 * outselves to be in EAP-SIM-Start state if there is none.
696 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE, 0, TAG_ANY)) == NULL)
698 /* must be initial request */
699 statevp = paircreate(resp, ATTRIBUTE_EAP_SIM_STATE, 0);
700 statevp->vp_integer = eapsim_client_init;
701 pairreplace(&(resp->vps), statevp);
703 state = statevp->vp_integer;
706 * map the attributes, and authenticate them.
708 unmap_eapsim_types(req);
710 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE, 0, TAG_ANY)) == NULL)
714 subtype = vp->vp_integer;
717 * look for the appropriate state, and process incoming message
720 case eapsim_client_init:
723 newstate = process_eap_start(req, resp);
726 case eapsim_challenge:
727 case eapsim_notification:
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 */
738 case eapsim_client_start:
741 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
742 newstate = process_eap_start(req, resp);
745 case eapsim_challenge:
746 newstate = process_eap_challenge(req, resp);
750 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
751 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
752 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
753 /* invalid state, drop message */
760 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
761 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
765 /* copy the eap state object in */
766 pairreplace(&(resp->vps), eapid);
768 /* update stete info, and send new packet */
769 map_eapsim_types(resp);
771 /* copy the radius state object in */
772 pairreplace(&(resp->vps), radstate);
774 statevp->vp_integer = newstate;
778 static int respond_eap_md5(RADIUS_PACKET *req,
781 VALUE_PAIR *vp, *id, *state;
784 uint8_t const *value;
786 uint8_t response[16];
790 if ((state = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
792 fprintf(stderr, "radeapclient: no state attribute found\n");
796 if ((id = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
798 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
801 identifier = id->vp_integer;
803 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0, TAG_ANY)) == NULL)
805 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
809 /* got the details of the MD5 challenge */
810 valuesize = vp->vp_octets[0];
811 value = &vp->vp_octets[1];
814 if(valuesize > vp->length)
816 fprintf(stderr, "radeapclient: md5 valuesize if too big (%u > %u)\n",
817 (unsigned int) valuesize, (unsigned int) vp->length);
821 /* now do the CHAP operation ourself, rather than build the
822 * buffer. We could also call rad_chap_encode, but it wants
823 * a CHAP-Challenge, which we don't want to bother with.
825 fr_MD5Init(&context);
826 fr_MD5Update(&context, &identifier, 1);
827 fr_MD5Update(&context, (uint8_t *) password, strlen(password));
828 fr_MD5Update(&context, value, valuesize);
829 fr_MD5Final(response, &context);
835 vp = paircreate(rep, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0);
838 p = talloc_zero_array(vp, uint8_t, 17);
840 memcpy(p, &lg_response, 1);
841 memcpy(p + 1, response, 16);
844 pairreplace(&(rep->vps), vp);
846 pairreplace(&(rep->vps), id);
848 /* copy the state object in */
849 pairreplace(&(rep->vps), state);
856 static int sendrecv_eap(RADIUS_PACKET *rep)
858 RADIUS_PACKET *req = NULL;
859 VALUE_PAIR *vp, *vpnext;
860 int tried_eap_md5 = 0;
865 * Keep a copy of the the User-Password attribute.
867 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
868 strlcpy(password, vp->vp_strvalue, sizeof(password));
870 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
871 strlcpy(password, vp->vp_strvalue, sizeof(password));
873 * Otherwise keep a copy of the CHAP-Password attribute.
875 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
876 strlcpy(password, vp->vp_strvalue, sizeof(password));
885 * if there are EAP types, encode them into an EAP-Message
888 map_eap_methods(rep);
891 * Fix up Digest-Attributes issues
893 for (vp = rep->vps; vp != NULL; vp = vp->next) {
894 switch (vp->da->attr) {
898 case PW_DIGEST_REALM:
899 case PW_DIGEST_NONCE:
900 case PW_DIGEST_METHOD:
903 case PW_DIGEST_ALGORITHM:
904 case PW_DIGEST_BODY_DIGEST:
905 case PW_DIGEST_CNONCE:
906 case PW_DIGEST_NONCE_COUNT:
907 case PW_DIGEST_USER_NAME:
913 p = talloc_array(vp, uint8_t, vp->length + 2);
915 memcpy(p + 2, vp->vp_octets, vp->length);
916 p[0] = vp->da->attr - PW_DIGEST_REALM + 1;
920 da = dict_attrbyvalue(PW_DIGEST_ATTRIBUTES, 0);
924 * Re-do pairmemsteal ourselves,
925 * because we play games with
926 * vp->da, and pairmemsteal goes
927 * to GREAT lengths to sanitize
928 * and fix and change and
929 * double-check the various
932 memcpy(&q, &vp->vp_octets, sizeof(q));
935 vp->vp_octets = talloc_steal(vp, p);
945 * If we've already sent a packet, free up the old
946 * one, and ensure that the next packet has a unique
947 * ID and authentication vector.
950 talloc_free(rep->data);
954 fr_md5_calc(rep->vector, rep->vector,
955 sizeof(rep->vector));
957 if (*password != '\0') {
958 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
959 pairstrcpy(vp, password);
961 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
962 pairstrcpy(vp, password);
964 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
965 pairstrcpy(vp, password);
968 p = talloc_zero_array(vp, uint8_t, 17);
969 rad_chap_encode(rep, p, rep->id, vp);
972 } /* there WAS a password */
974 /* send the response, wait for the next request */
975 send_packet(rep, &req);
979 /* okay got back the packet, go and decode the EAP-Message. */
980 unmap_eap_methods(req);
982 debug_packet(req, R_RECV);
984 /* now look for the code type. */
985 for (vp = req->vps; vp != NULL; vp = vpnext) {
988 switch (vp->da->attr) {
992 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
993 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
1000 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
1001 if(respond_eap_sim(req, rep))
1013 void set_radius_dir(TALLOC_CTX *ctx, char const *path)
1018 memcpy(&p, &radius_dir, sizeof(p));
1022 if (path) radius_dir = talloc_strdup(ctx, path);
1026 int main(int argc, char **argv)
1032 char *filename = NULL;
1035 int force_af = AF_UNSPEC;
1038 * We probably don't want to free the talloc autofree context
1039 * directly, so we'll allocate a new context beneath it, and
1040 * free that before any leak reports.
1042 TALLOC_CTX *autofree = talloc_init("main");
1044 id = ((int)getpid() & 0xff);
1047 radlog_dest = L_DST_STDERR;
1049 set_radius_dir(autofree, RADIUS_DIR);
1051 while ((c = getopt(argc, argv, "46c:d:D:f:hi:qst:r:S:xXv")) != EOF)
1058 force_af = AF_INET6;
1061 set_radius_dir(autofree, optarg);
1064 main_config.dictionary_dir = talloc_typed_strdup(NULL, optarg);
1079 sha1_data_problems = 1; /* for debugging only */
1086 if (!isdigit((int) *optarg))
1088 retries = atoi(optarg);
1091 if (!isdigit((int) *optarg))
1094 if ((id < 0) || (id > 255)) {
1102 if (!isdigit((int) *optarg))
1104 timeout = atof(optarg);
1107 printf("radeapclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
1111 fp = fopen(optarg, "r");
1113 fprintf(stderr, "radclient: Error opening %s: %s\n",
1114 optarg, fr_syserror(errno));
1117 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1118 fprintf(stderr, "radclient: Error reading %s: %s\n",
1119 optarg, fr_syserror(errno));
1124 /* truncate newline */
1125 p = filesecret + strlen(filesecret) - 1;
1126 while ((p >= filesecret) &&
1132 if (strlen(filesecret) < 2) {
1133 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1136 secret = filesecret;
1144 argc -= (optind - 1);
1145 argv += (optind - 1);
1148 ((!secret) && (argc < 4))) {
1152 if (!main_config.dictionary_dir) {
1153 main_config.dictionary_dir = DICTDIR;
1157 * Read the distribution dictionaries first, then
1158 * the ones in raddb.
1160 DEBUG2("including dictionary file %s/%s", main_config.dictionary_dir, RADIUS_DICTIONARY);
1161 if (dict_init(main_config.dictionary_dir, RADIUS_DICTIONARY) != 0) {
1162 ERROR("Errors reading dictionary: %s",
1168 * It's OK if this one doesn't exist.
1170 int rcode = dict_read(radius_dir, RADIUS_DICTIONARY);
1172 ERROR("Errors reading %s/%s: %s", radius_dir, RADIUS_DICTIONARY,
1178 * We print this after reading it. That way if
1179 * it doesn't exist, it's OK, and we don't print
1183 DEBUG2("including dictionary file %s/%s", radius_dir, RADIUS_DICTIONARY);
1186 req = rad_alloc(NULL, 1);
1188 fr_perror("radclient");
1196 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1198 perror("/dev/urandom");
1200 fread(randctx.randrsl, 256, 1, randinit);
1204 fr_randinit(&randctx, 1);
1212 if (force_af == AF_UNSPEC) force_af = AF_INET;
1213 req->dst_ipaddr.af = force_af;
1214 if (strcmp(argv[1], "-") != 0) {
1215 char const *hostname = argv[1];
1216 char const *portname = argv[1];
1219 if (*argv[1] == '[') { /* IPv6 URL encoded */
1220 p = strchr(argv[1], ']');
1221 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1225 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1226 buffer[p - argv[1] - 1] = '\0';
1232 p = strchr(portname, ':');
1233 if (p && (strchr(p + 1, ':') == NULL)) {
1240 if (ip_hton(&req->dst_ipaddr, force_af, hostname, false) < 0) {
1241 fprintf(stderr, "radclient: Failed to find IP address for host %s: %s\n", hostname, fr_syserror(errno));
1246 * Strip port from hostname if needed.
1248 if (portname) port = atoi(portname);
1252 * See what kind of request we want to send.
1254 if (strcmp(argv[2], "auth") == 0) {
1255 if (port == 0) port = getport("radius");
1256 if (port == 0) port = PW_AUTH_UDP_PORT;
1257 req->code = PW_CODE_AUTHENTICATION_REQUEST;
1259 } else if (strcmp(argv[2], "acct") == 0) {
1260 if (port == 0) port = getport("radacct");
1261 if (port == 0) port = PW_ACCT_UDP_PORT;
1262 req->code = PW_CODE_ACCOUNTING_REQUEST;
1265 } else if (strcmp(argv[2], "status") == 0) {
1266 if (port == 0) port = getport("radius");
1267 if (port == 0) port = PW_AUTH_UDP_PORT;
1268 req->code = PW_CODE_STATUS_SERVER;
1270 } else if (strcmp(argv[2], "disconnect") == 0) {
1271 if (port == 0) port = PW_POD_UDP_PORT;
1272 req->code = PW_CODE_DISCONNECT_REQUEST;
1274 } else if (isdigit((int) argv[2][0])) {
1275 if (port == 0) port = getport("radius");
1276 if (port == 0) port = PW_AUTH_UDP_PORT;
1277 req->code = atoi(argv[2]);
1281 req->dst_port = port;
1286 if (argv[3]) secret = argv[3];
1290 * Maybe read them, from stdin, if there's no
1291 * filename, or if the filename is '-'.
1293 if (filename && (strcmp(filename, "-") != 0)) {
1294 fp = fopen(filename, "r");
1296 fprintf(stderr, "radclient: Error opening %s: %s\n",
1297 filename, fr_syserror(errno));
1307 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1308 perror("radclient: socket: ");
1313 if (req->vps) pairfree(&req->vps);
1314 if (readvp2(&req->vps, NULL, fp, &filedone) < 0) {
1315 fr_perror("radeapclient");
1323 printf("\n\t Total approved auths: %d\n", totalapp);
1324 printf("\t Total denied auths: %d\n", totaldeny);
1327 talloc_free(autofree);
1333 * given a radius request with some attributes in the EAP range, build
1334 * them all into a single EAP-Message body.
1336 * Note that this function will build multiple EAP-Message bodies
1337 * if there are multiple eligible EAP-types. This is incorrect, as the
1338 * recipient will in fact concatenate them.
1340 * XXX - we could break the loop once we process one type. Maybe this
1341 * just deserves an assert?
1344 static void map_eap_methods(RADIUS_PACKET *req)
1346 VALUE_PAIR *vp, *vpnext;
1350 eap_packet_t *pt_ep = talloc_zero(req, eap_packet_t);
1352 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY);
1354 id = ((int)getpid() & 0xff);
1356 id = vp->vp_integer;
1359 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE, 0, TAG_ANY);
1361 eapcode = PW_EAP_REQUEST;
1363 eapcode = vp->vp_integer;
1366 for(vp = req->vps; vp != NULL; vp = vpnext) {
1367 /* save it in case it changes! */
1370 if(vp->da->attr >= ATTRIBUTE_EAP_BASE &&
1371 vp->da->attr < ATTRIBUTE_EAP_BASE+256) {
1380 eap_method = vp->da->attr - ATTRIBUTE_EAP_BASE;
1382 switch(eap_method) {
1383 case PW_EAP_IDENTITY:
1384 case PW_EAP_NOTIFICATION:
1395 * no known special handling, it is just encoded as an
1396 * EAP-message with the given type.
1399 /* nuke any existing EAP-Messages */
1400 pairdelete(&req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1402 pt_ep->code = eapcode;
1404 pt_ep->type.num = eap_method;
1405 pt_ep->type.length = vp->length;
1407 pt_ep->type.data = talloc_memdup(vp, vp->vp_octets, vp->length);
1408 talloc_set_type(pt_ep->type.data, uint8_t);
1410 eap_basic_compose(req, pt_ep);
1415 * given a radius request with an EAP-Message body, decode it specific
1418 static void unmap_eap_methods(RADIUS_PACKET *rep)
1421 eap_packet_raw_t *e;
1427 /* find eap message */
1428 e = eap_vp2packet(NULL, rep->vps);
1430 /* nothing to do! */
1433 /* create EAP-ID and EAP-CODE attributes to start */
1434 eap1 = paircreate(rep, ATTRIBUTE_EAP_ID, 0);
1435 eap1->vp_integer = e->id;
1436 pairadd(&(rep->vps), eap1);
1438 eap1 = paircreate(rep, ATTRIBUTE_EAP_CODE, 0);
1439 eap1->vp_integer = e->code;
1440 pairadd(&(rep->vps), eap1);
1444 case PW_EAP_SUCCESS:
1445 case PW_EAP_FAILURE:
1449 case PW_EAP_REQUEST:
1450 case PW_EAP_RESPONSE:
1451 /* there is a type field, which we use to create
1452 * a new attribute */
1454 /* the length was decode already into the attribute
1455 * length, and was checked already. Network byte
1456 * order, just pull it out using math.
1458 len = e->length[0]*256 + e->length[1];
1460 /* verify the length is big enough to hold type */
1469 type += ATTRIBUTE_EAP_BASE;
1472 if (len > MAX_STRING_LEN) {
1473 len = MAX_STRING_LEN;
1476 eap1 = paircreate(rep, type, 0);
1477 pairmemcpy(eap1, e->data + 1, len);
1479 pairadd(&(rep->vps), eap1);
1487 static int map_eapsim_types(RADIUS_PACKET *r)
1491 eap_packet_t *pt_ep = talloc_zero(r, eap_packet_t);
1493 ret = map_eapsim_basictypes(r, pt_ep);
1499 eap_basic_compose(r, pt_ep);
1504 static int unmap_eapsim_types(RADIUS_PACKET *r)
1510 esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM, 0, TAG_ANY);
1512 ERROR("eap: EAP-Sim attribute not found");
1516 eap_data = talloc_memdup(esvp, esvp->vp_octets, esvp->length);
1517 talloc_set_type(eap_data, uint8_t);
1519 rcode_unmap = unmap_eapsim_basictypes(r, eap_data, esvp->length);
1521 talloc_free(eap_data);
1529 char const *radius_dir = RADDBDIR;
1531 int radlog(int lvl, char const *msg, ...)
1537 r = vfprintf(stderr, msg, ap);
1539 fputc('\n', stderr);
1544 main(int argc, char *argv[])
1547 RADIUS_PACKET *req,*req2;
1548 VALUE_PAIR *vp, *vpkey, *vpextra;
1549 extern unsigned int sha1_data_problems;
1556 sha1_data_problems = 1;
1559 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1560 fr_perror("radclient");
1564 req = rad_alloc(NULL, 1)
1566 fr_perror("radclient");
1570 req2 = rad_alloc(NULL, 1);
1572 fr_perror("radclient");
1577 if (req->vps) pairfree(&req->vps);
1578 if (req2->vps) pairfree(&req2->vps);
1580 if (readvp2(&req->vps, NULL, stdin, &filedone) < 0) {
1581 fr_perror("radeapclient");
1585 if (fr_debug_flag > 1) {
1586 printf("\nRead:\n");
1587 vp_printlist(stdout, req->vps);
1590 map_eapsim_types(req);
1591 map_eap_methods(req);
1593 if (fr_debug_flag > 1) {
1594 printf("Mapped to:\n");
1595 vp_printlist(stdout, req->vps);
1598 /* find the EAP-Message, copy it to req2 */
1599 vp = paircopy2(NULL, req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1603 pairadd(&req2->vps, vp);
1605 /* only call unmap for sim types here */
1606 unmap_eap_methods(req2);
1607 unmap_eapsim_types(req2);
1609 if (fr_debug_flag > 1) {
1610 printf("Unmapped to:\n");
1611 vp_printlist(stdout, req2->vps);
1614 vp = pairfind(req2->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
1615 vpkey = pairfind(req->vps, ATTRIBUTE_EAP_SIM_KEY, 0, TAG_ANY);
1616 vpextra = pairfind(req->vps, ATTRIBUTE_EAP_SIM_EXTRA, 0, TAG_ANY);
1618 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1619 uint8_t calcmac[16];
1621 /* find the EAP-Message, copy it to req2 */
1623 memset(calcmac, 0, sizeof(calcmac));
1624 printf("Confirming MAC...");
1625 if(eapsim_checkmac(req2->vps, vpkey->vp_strvalue,
1626 vpextra->vp_strvalue, vpextra->length,
1628 printf("succeed\n");
1632 printf("calculated MAC (");
1633 for (i = 0; i < 20; i++) {
1640 printf("%02x", calcmac[i]);
1642 printf(" did not match\n");