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 const *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(log_type_t lvl, char const *fmt, ...)
106 r = lvl; /* shut up compiler */
109 r = vfprintf(stderr, fmt, ap);
116 void vradlog_request(UNUSED log_type_t lvl, UNUSED log_debug_t priority,
117 UNUSED REQUEST *request, char const *msg, va_list ap)
119 vfprintf(stderr, msg, ap);
123 static int getport(char const *name)
127 svp = getservbyname (name, "udp");
132 return ntohs(svp->s_port);
137 static void debug_packet(RADIUS_PACKET *packet, int direction)
141 char const *received, *from;
142 fr_ipaddr_t const *ip;
147 if (direction == 0) {
148 received = "Received";
149 from = "from"; /* what else? */
150 ip = &packet->src_ipaddr;
151 port = packet->src_port;
154 received = "Sending";
155 from = "to"; /* hah! */
156 ip = &packet->dst_ipaddr;
157 port = packet->dst_port;
161 * Client-specific debugging re-prints the input
162 * packet into the client log.
164 * This really belongs in a utility library
166 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
167 printf("%s %s packet %s host %s port %d, id=%d, length=%d\n",
168 received, fr_packet_codes[packet->code], from,
169 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
170 port, packet->id, (int) packet->data_len);
172 printf("%s packet %s host %s port %d code=%d, id=%d, length=%d\n",
174 inet_ntop(ip->af, &ip->ipaddr, buffer, sizeof(buffer)),
176 packet->code, packet->id, (int) packet->data_len);
179 for (vp = packet->vps; vp != NULL; vp = vp->next) {
180 vp_prints(buffer, sizeof(buffer), vp);
181 printf("\t%s\n", buffer);
187 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
192 for (i = 0; i < retries; i++) {
195 debug_packet(req, R_SENT);
197 rad_send(req, NULL, secret);
199 /* And wait for reply, timing out as necessary */
201 FD_SET(req->sockfd, &rdfdesc);
203 tv.tv_sec = (int)timeout;
204 tv.tv_usec = 1000000 * (timeout - (int) timeout);
206 /* Something's wrong if we don't get exactly one fd. */
207 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
211 *rep = rad_recv(req->sockfd, 0);
214 * If we get a response from a machine
215 * which we did NOT send a request to,
218 if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
219 (memcmp(&(*rep)->src_ipaddr.ipaddr,
220 &req->dst_ipaddr.ipaddr,
221 ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
222 sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
223 sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
224 ((*rep)->src_port != req->dst_port)) {
225 char src[128], dst[128];
227 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
228 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
229 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
231 src, (*rep)->src_port);
235 } else { /* NULL: couldn't receive the packet */
236 fr_perror("radclient:");
241 /* No response or no data read (?) */
243 fprintf(stderr, "radclient: no response from server\n");
248 * FIXME: Discard the packet & listen for another.
250 * Hmm... we should really be using eapol_test, which does
251 * a lot more than radeapclient.
253 if (rad_verify(*rep, req, secret) != 0) {
254 fr_perror("rad_verify");
258 if (rad_decode(*rep, req, secret) != 0) {
259 fr_perror("rad_decode");
263 /* libradius debug already prints out the value pairs for us */
264 if (!fr_debug_flag && do_output) {
265 debug_packet(*rep, R_RECV);
267 if((*rep)->code == PW_AUTHENTICATION_ACK) {
269 } else if ((*rep)->code == PW_AUTHENTICATION_REJECT) {
276 static void cleanresp(RADIUS_PACKET *resp)
278 VALUE_PAIR *vpnext, *vp, **last;
282 * maybe should just copy things we care about, or keep
283 * a copy of the original input and start from there again?
285 pairdelete(&resp->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
286 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY, 0, TAG_ANY);
289 for(vp = *last; vp != NULL; vp = vpnext)
293 if((vp->da->attr > ATTRIBUTE_EAP_BASE &&
294 vp->da->attr <= ATTRIBUTE_EAP_BASE+256) ||
295 (vp->da->attr > ATTRIBUTE_EAP_SIM_BASE &&
296 vp->da->attr <= ATTRIBUTE_EAP_SIM_BASE+256))
307 * we got an EAP-Request/Sim/Start message in a legal state.
309 * pick a supported version, put it into the reply, and insert a nonce.
311 static int process_eap_start(RADIUS_PACKET *req,
314 VALUE_PAIR *vp, *newvp;
315 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
316 uint16_t const *versions;
317 uint16_t selectedversion;
318 unsigned int i,versioncount;
320 /* form new response clear of any EAP stuff */
323 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST, 0, TAG_ANY)) == NULL) {
324 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
328 versions = (uint16_t const *) vp->vp_strvalue;
330 /* verify that the attribute length is big enough for a length field */
333 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %u\n", (unsigned int) vp->length);
337 versioncount = ntohs(versions[0])/2;
338 /* verify that the attribute length is big enough for the given number
339 * of versions present.
341 if((unsigned)vp->length <= (versioncount*2 + 2))
343 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %u bytes\n", versioncount, (unsigned int) vp->length);
348 * record the versionlist for the MK calculation.
350 eapsim_mk.versionlistlen = versioncount*2;
351 memcpy(eapsim_mk.versionlist, (unsigned char const *)(versions+1),
352 eapsim_mk.versionlistlen);
354 /* walk the version list, and pick the one we support, which
355 * at present, is 1, EAP_SIM_VERSION.
358 for(i=0; i < versioncount; i++)
360 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
362 selectedversion=EAP_SIM_VERSION;
366 if(selectedversion == 0)
368 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
369 for(i=0; i < versioncount; i++)
371 fprintf(stderr, "\tfound version %d\n",
372 ntohs(versions[i+1]));
377 * now make sure that we have only FULLAUTH_ID_REQ.
378 * I think that it actually might not matter - we can answer in
379 * anyway we like, but it is illegal to have more than one
382 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ, 0, TAG_ANY);
383 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ, 0, TAG_ANY);
384 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ, 0, TAG_ANY);
386 if(!fullauthidreq_vp ||
387 anyidreq_vp != NULL ||
388 permanentidreq_vp != NULL) {
389 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
390 (anyidreq_vp != NULL ? "a " : "no "),
391 (fullauthidreq_vp != NULL ? "a " : "no "),
392 (permanentidreq_vp != NULL ? "a " : "no "));
396 /* okay, we have just any_id_req there, so fill in response */
398 /* mark the subtype as being EAP-SIM/Response/Start */
399 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_SUBTYPE, 0);
400 newvp->vp_integer = eapsim_start;
401 pairreplace(&(rep->vps), newvp);
403 /* insert selected version into response. */
404 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION, 0);
405 versions = (uint16_t const *)newvp->vp_strvalue;
406 versions[0] = htons(selectedversion);
408 pairreplace(&(rep->vps), newvp);
410 /* record the selected version */
411 memcpy(eapsim_mk.versionselect, (unsigned char const *)versions, 2);
418 * insert a nonce_mt that we make up.
420 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT, 0);
421 newvp->vp_octets[0]=0;
422 newvp->vp_octets[1]=0;
423 newvp->length = 18; /* 16 bytes of nonce + padding */
429 memcpy(&newvp->vp_octets[2], nonce, 16);
430 pairreplace(&(rep->vps), newvp);
432 /* also keep a copy of the nonce! */
433 memcpy(eapsim_mk.nonce_mt, nonce, 16);
437 uint16_t *pidlen, idlen;
440 * insert the identity here.
442 vp = pairfind(rep->vps, PW_USER_NAME, 0, TAG_ANY);
445 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
448 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY, 0);
449 idlen = strlen(vp->vp_strvalue);
450 pidlen = (uint16_t *)newvp->vp_strvalue;
451 *pidlen = htons(idlen);
452 newvp->length = idlen + 2;
454 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
455 pairreplace(&(rep->vps), newvp);
458 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
459 eapsim_mk.identitylen = idlen;
466 * we got an EAP-Request/Sim/Challenge message in a legal state.
468 * use the RAND challenge to produce the SRES result, and then
469 * use that to generate a new MAC.
471 * for the moment, we ignore the RANDs, then just plug in the SRES
475 static int process_eap_challenge(RADIUS_PACKET *req,
479 VALUE_PAIR *mac, *randvp;
480 VALUE_PAIR *sres1,*sres2,*sres3;
481 VALUE_PAIR *Kc1, *Kc2, *Kc3;
484 /* look for the AT_MAC and the challenge data */
485 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
486 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND, 0, TAG_ANY);
487 if(!mac || !randvp) {
488 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
493 * compare RAND with randX, to verify this is the right response
497 VALUE_PAIR *randcfgvp[3];
500 randcfg[0] = &randvp->vp_octets[2];
501 randcfg[1] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE];
502 randcfg[2] = &randvp->vp_octets[2+EAPSIM_RAND_SIZE*2];
504 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1, 0, TAG_ANY);
505 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2, 0, TAG_ANY);
506 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3, 0, TAG_ANY);
511 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
515 if(memcmp(randcfg[0], randcfgvp[0]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
516 memcmp(randcfg[1], randcfgvp[1]->vp_octets, EAPSIM_RAND_SIZE)!=0 ||
517 memcmp(randcfg[2], randcfgvp[2]->vp_octets, EAPSIM_RAND_SIZE)!=0) {
520 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
521 for(rnum = 0; rnum < 3; rnum++) {
522 fprintf(stderr, "received rand %d: ", rnum);
524 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
531 fprintf(stderr, "%02x", randcfg[rnum][i]);
533 fprintf(stderr, "\nconfigured rand %d: ", rnum);
535 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
542 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_octets[i]);
544 fprintf(stderr, "\n");
551 * now dig up the sres values from the response packet,
552 * which were put there when we read things in.
554 * Really, they should be calculated from the RAND!
557 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1, 0, TAG_ANY);
558 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2, 0, TAG_ANY);
559 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3, 0, TAG_ANY);
564 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
567 memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
568 memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
569 memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
571 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1, 0, TAG_ANY);
572 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2, 0, TAG_ANY);
573 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3, 0, TAG_ANY);
578 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
581 memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
582 memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
583 memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
585 /* all set, calculate keys */
586 eapsim_calculate_keys(&eapsim_mk);
589 eapsim_dump_mk(&eapsim_mk);
592 /* verify the MAC, now that we have all the keys. */
593 if(eapsim_checkmac(NULL, req->vps, eapsim_mk.K_aut,
594 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
596 printf("MAC check succeed\n");
600 printf("calculated MAC (");
601 for (i = 0; i < 20; i++) {
608 printf("%02x", calcmac[i]);
610 printf(" did not match\n");
614 /* form new response clear of any EAP stuff */
617 /* mark the subtype as being EAP-SIM/Response/Start */
618 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_SUBTYPE, 0);
619 newvp->vp_integer = eapsim_challenge;
620 pairreplace(&(rep->vps), newvp);
623 * fill the SIM_MAC with a field that will in fact get appended
624 * to the packet before the MAC is calculated
626 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0);
627 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
628 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
629 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
630 newvp->length = EAPSIM_SRES_SIZE*3;
631 pairreplace(&(rep->vps), newvp);
633 newvp = paircreate(rep, ATTRIBUTE_EAP_SIM_KEY, 0);
634 memcpy(newvp->vp_strvalue, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
635 newvp->length = EAPSIM_AUTH_SIZE;
636 pairreplace(&(rep->vps), newvp);
642 * this code runs the EAP-SIM client state machine.
643 * the *request* is from the server.
644 * the *reponse* is to the server.
647 static int respond_eap_sim(RADIUS_PACKET *req,
650 enum eapsim_clientstates state, newstate;
651 enum eapsim_subtype subtype;
652 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
653 char statenamebuf[32], subtypenamebuf[32];
655 if ((radstate = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
660 if ((eapid = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
665 /* first, dig up the state from the request packet, setting
666 * outselves to be in EAP-SIM-Start state if there is none.
669 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE, 0, TAG_ANY)) == NULL)
671 /* must be initial request */
672 statevp = paircreate(resp, ATTRIBUTE_EAP_SIM_STATE, 0);
673 statevp->vp_integer = eapsim_client_init;
674 pairreplace(&(resp->vps), statevp);
676 state = statevp->vp_integer;
679 * map the attributes, and authenticate them.
681 unmap_eapsim_types(req);
683 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE, 0, TAG_ANY)) == NULL)
687 subtype = vp->vp_integer;
690 * look for the appropriate state, and process incoming message
693 case eapsim_client_init:
696 newstate = process_eap_start(req, resp);
699 case eapsim_challenge:
700 case eapsim_notification:
703 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
704 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
705 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
706 /* invalid state, drop message */
711 case eapsim_client_start:
714 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
715 newstate = process_eap_start(req, resp);
718 case eapsim_challenge:
719 newstate = process_eap_challenge(req, resp);
723 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
724 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
725 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
726 /* invalid state, drop message */
733 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
734 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
738 /* copy the eap state object in */
739 pairreplace(&(resp->vps), eapid);
741 /* update stete info, and send new packet */
742 map_eapsim_types(resp);
744 /* copy the radius state object in */
745 pairreplace(&(resp->vps), radstate);
747 statevp->vp_integer = newstate;
751 static int respond_eap_md5(RADIUS_PACKET *req,
754 VALUE_PAIR *vp, *id, *state;
755 size_t valuesize, namesize;
757 uint8_t const *value;
760 uint8_t response[16];
764 if ((state = paircopy2(NULL, req->vps, PW_STATE, 0, TAG_ANY)) == NULL)
766 fprintf(stderr, "radeapclient: no state attribute found\n");
770 if ((id = paircopy2(NULL, req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY)) == NULL)
772 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
775 identifier = id->vp_integer;
777 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0, TAG_ANY)) == NULL)
779 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
783 /* got the details of the MD5 challenge */
784 valuesize = vp->vp_octets[0];
785 value = &vp->vp_octets[1];
786 name = &vp->vp_octets[valuesize+1];
787 namesize = vp->length - (valuesize + 1);
790 if(valuesize > vp->length)
792 fprintf(stderr, "radeapclient: md5 valuesize if too big (%u > %u)\n",
793 (unsigned int) valuesize, (unsigned int) vp->length);
797 /* now do the CHAP operation ourself, rather than build the
798 * buffer. We could also call rad_chap_encode, but it wants
799 * a CHAP-Challenge, which we don't want to bother with.
801 fr_MD5Init(&context);
802 fr_MD5Update(&context, &identifier, 1);
803 fr_MD5Update(&context, (uint8_t *) password, strlen(password));
804 fr_MD5Update(&context, value, valuesize);
805 fr_MD5Final(response, &context);
807 vp = paircreate(rep, ATTRIBUTE_EAP_BASE+PW_EAP_MD5, 0);
809 memcpy(&vp->vp_strvalue[1], response, 16);
812 pairreplace(&(rep->vps), vp);
814 pairreplace(&(rep->vps), id);
816 /* copy the state object in */
817 pairreplace(&(rep->vps), state);
824 static int sendrecv_eap(RADIUS_PACKET *rep)
826 RADIUS_PACKET *req = NULL;
827 VALUE_PAIR *vp, *vpnext;
828 int tried_eap_md5 = 0;
830 if (vp->length > sizeof(password)) {
831 fprintf(stderr, "radeapclient: Password buffer too small have %zu bytes need %zu bytes\n",
832 sizeof(password), vp->length);
836 * Keep a copy of the the User-Password attribute.
838 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
839 strlcpy(password, vp->vp_strvalue, sizeof(password));
841 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
842 strlcpy(password, vp->vp_strvalue, sizeof(password));
844 * Otherwise keep a copy of the CHAP-Password attribute.
846 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
847 strlcpy(password, vp->vp_strvalue, sizeof(password));
856 * if there are EAP types, encode them into an EAP-Message
859 map_eap_methods(rep);
862 * Fix up Digest-Attributes issues
864 for (vp = rep->vps; vp != NULL; vp = vp->next) {
865 switch (vp->da->attr) {
869 case PW_DIGEST_REALM:
870 case PW_DIGEST_NONCE:
871 case PW_DIGEST_METHOD:
874 case PW_DIGEST_ALGORITHM:
875 case PW_DIGEST_BODY_DIGEST:
876 case PW_DIGEST_CNONCE:
877 case PW_DIGEST_NONCE_COUNT:
878 case PW_DIGEST_USER_NAME:
880 memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
881 vp->vp_octets[0] = vp->da->attr - PW_DIGEST_REALM + 1;
883 vp->vp_octets[1] = vp->length;
884 vp->da->attr = PW_DIGEST_ATTRIBUTES;
890 * If we've already sent a packet, free up the old
891 * one, and ensure that the next packet has a unique
892 * ID and authentication vector.
895 talloc_free(rep->data);
899 fr_md5_calc(rep->vector, rep->vector,
900 sizeof(rep->vector));
902 if (*password != '\0') {
903 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD, 0, TAG_ANY)) != NULL) {
904 pairstrcpy(vp, password);
906 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
907 pairstrcpy(vp, password);
909 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
910 pairstrcpy(vp, password);
912 rad_chap_encode(rep, vp->vp_octets, rep->id, vp);
915 } /* there WAS a password */
917 /* send the response, wait for the next request */
918 send_packet(rep, &req);
920 /* okay got back the packet, go and decode the EAP-Message. */
921 unmap_eap_methods(req);
923 debug_packet(req, R_RECV);
925 /* now look for the code type. */
926 for (vp = req->vps; vp != NULL; vp = vpnext) {
929 switch (vp->da->attr) {
933 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
934 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
941 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
942 if(respond_eap_sim(req, rep))
954 int main(int argc, char **argv)
960 char *filename = NULL;
964 int force_af = AF_UNSPEC;
966 id = ((int)getpid() & 0xff);
969 radlog_dest = L_DST_STDERR;
971 while ((c = getopt(argc, argv, "46c:d:f:hi:qst:r:S:xXv")) != EOF)
981 if (!isdigit((int) *optarg))
983 count = atoi(optarg);
986 radius_dir = strdup(optarg);
1001 sha1_data_problems = 1; /* for debugging only */
1008 if (!isdigit((int) *optarg))
1010 retries = atoi(optarg);
1013 if (!isdigit((int) *optarg))
1016 if ((id < 0) || (id > 255)) {
1024 if (!isdigit((int) *optarg))
1026 timeout = atof(optarg);
1029 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
1033 fp = fopen(optarg, "r");
1035 fprintf(stderr, "radclient: Error opening %s: %s\n",
1036 optarg, strerror(errno));
1039 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1040 fprintf(stderr, "radclient: Error reading %s: %s\n",
1041 optarg, strerror(errno));
1046 /* truncate newline */
1047 p = filesecret + strlen(filesecret) - 1;
1048 while ((p >= filesecret) &&
1054 if (strlen(filesecret) < 2) {
1055 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1058 secret = filesecret;
1066 argc -= (optind - 1);
1067 argv += (optind - 1);
1070 ((!secret) && (argc < 4))) {
1074 if (!radius_dir) radius_dir = strdup(RADDBDIR);
1076 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1077 fr_perror("radclient");
1081 req = rad_alloc(NULL, 1);
1083 fr_perror("radclient");
1091 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1093 perror("/dev/urandom");
1095 fread(randctx.randrsl, 256, 1, randinit);
1099 fr_randinit(&randctx, 1);
1107 if (force_af == AF_UNSPEC) force_af = AF_INET;
1108 req->dst_ipaddr.af = force_af;
1109 if (strcmp(argv[1], "-") != 0) {
1110 char const *hostname = argv[1];
1111 char const *portname = argv[1];
1114 if (*argv[1] == '[') { /* IPv6 URL encoded */
1115 p = strchr(argv[1], ']');
1116 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1120 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1121 buffer[p - argv[1] - 1] = '\0';
1127 p = strchr(portname, ':');
1128 if (p && (strchr(p + 1, ':') == NULL)) {
1135 if (ip_hton(hostname, force_af, &req->dst_ipaddr) < 0) {
1136 fprintf(stderr, "radclient: Failed to find IP address for host %s: %s\n", hostname, strerror(errno));
1141 * Strip port from hostname if needed.
1143 if (portname) port = atoi(portname);
1147 * See what kind of request we want to send.
1149 if (strcmp(argv[2], "auth") == 0) {
1150 if (port == 0) port = getport("radius");
1151 if (port == 0) port = PW_AUTH_UDP_PORT;
1152 req->code = PW_AUTHENTICATION_REQUEST;
1154 } else if (strcmp(argv[2], "acct") == 0) {
1155 if (port == 0) port = getport("radacct");
1156 if (port == 0) port = PW_ACCT_UDP_PORT;
1157 req->code = PW_ACCOUNTING_REQUEST;
1160 } else if (strcmp(argv[2], "status") == 0) {
1161 if (port == 0) port = getport("radius");
1162 if (port == 0) port = PW_AUTH_UDP_PORT;
1163 req->code = PW_STATUS_SERVER;
1165 } else if (strcmp(argv[2], "disconnect") == 0) {
1166 if (port == 0) port = PW_POD_UDP_PORT;
1167 req->code = PW_DISCONNECT_REQUEST;
1169 } else if (isdigit((int) argv[2][0])) {
1170 if (port == 0) port = getport("radius");
1171 if (port == 0) port = PW_AUTH_UDP_PORT;
1172 req->code = atoi(argv[2]);
1176 req->dst_port = port;
1181 if (argv[3]) secret = argv[3];
1185 * Maybe read them, from stdin, if there's no
1186 * filename, or if the filename is '-'.
1188 if (filename && (strcmp(filename, "-") != 0)) {
1189 fp = fopen(filename, "r");
1191 fprintf(stderr, "radclient: Error opening %s: %s\n",
1192 filename, strerror(errno));
1202 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1203 perror("radclient: socket: ");
1208 if(req->vps) pairfree(&req->vps);
1210 if ((req->vps = readvp2(NULL, fp, &filedone, "radeapclient:"))
1220 printf("\n\t Total approved auths: %d\n", totalapp);
1221 printf("\t Total denied auths: %d\n", totaldeny);
1227 * given a radius request with some attributes in the EAP range, build
1228 * them all into a single EAP-Message body.
1230 * Note that this function will build multiple EAP-Message bodies
1231 * if there are multiple eligible EAP-types. This is incorrect, as the
1232 * recipient will in fact concatenate them.
1234 * XXX - we could break the loop once we process one type. Maybe this
1235 * just deserves an assert?
1238 static void map_eap_methods(RADIUS_PACKET *req)
1240 VALUE_PAIR *vp, *vpnext;
1245 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID, 0, TAG_ANY);
1247 id = ((int)getpid() & 0xff);
1249 id = vp->vp_integer;
1252 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE, 0, TAG_ANY);
1254 eapcode = PW_EAP_REQUEST;
1256 eapcode = vp->vp_integer;
1260 for(vp = req->vps; vp != NULL; vp = vpnext) {
1261 /* save it in case it changes! */
1264 if(vp->da->attr >= ATTRIBUTE_EAP_BASE &&
1265 vp->da->attr < ATTRIBUTE_EAP_BASE+256) {
1274 eap_method = vp->da->attr - ATTRIBUTE_EAP_BASE;
1276 switch(eap_method) {
1277 case PW_EAP_IDENTITY:
1278 case PW_EAP_NOTIFICATION:
1289 * no known special handling, it is just encoded as an
1290 * EAP-message with the given type.
1293 /* nuke any existing EAP-Messages */
1294 pairdelete(&req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1296 memset(&ep, 0, sizeof(ep));
1299 ep.type.num = eap_method;
1300 ep.type.length = vp->length;
1301 ep.type.data = vp->vp_octets; /* no need for copy */
1302 eap_basic_compose(req, &ep);
1307 * given a radius request with an EAP-Message body, decode it specific
1310 static void unmap_eap_methods(RADIUS_PACKET *rep)
1313 eap_packet_raw_t *e;
1317 /* find eap message */
1318 e = eap_vp2packet(NULL, rep->vps);
1320 /* nothing to do! */
1323 /* create EAP-ID and EAP-CODE attributes to start */
1324 eap1 = paircreate(rep, ATTRIBUTE_EAP_ID, 0);
1325 eap1->vp_integer = e->id;
1326 pairadd(&(rep->vps), eap1);
1328 eap1 = paircreate(rep, ATTRIBUTE_EAP_CODE, 0);
1329 eap1->vp_integer = e->code;
1330 pairadd(&(rep->vps), eap1);
1334 case PW_EAP_SUCCESS:
1335 case PW_EAP_FAILURE:
1339 case PW_EAP_REQUEST:
1340 case PW_EAP_RESPONSE:
1341 /* there is a type field, which we use to create
1342 * a new attribute */
1344 /* the length was decode already into the attribute
1345 * length, and was checked already. Network byte
1346 * order, just pull it out using math.
1348 len = e->length[0]*256 + e->length[1];
1350 /* verify the length is big enough to hold type */
1359 type += ATTRIBUTE_EAP_BASE;
1362 if(len > MAX_STRING_LEN) {
1363 len = MAX_STRING_LEN;
1366 eap1 = paircreate(rep, type, 0);
1367 memcpy(eap1->vp_strvalue, &e->data[1], len);
1369 pairadd(&(rep->vps), eap1);
1377 static int map_eapsim_types(RADIUS_PACKET *r)
1382 memset(&ep, 0, sizeof(ep));
1383 ret = map_eapsim_basictypes(r, &ep);
1387 eap_basic_compose(r, &ep);
1392 static int unmap_eapsim_types(RADIUS_PACKET *r)
1396 esvp = pairfind(r->vps, ATTRIBUTE_EAP_BASE+PW_EAP_SIM, 0, TAG_ANY);
1398 ERROR("eap: EAP-Sim attribute not found");
1402 return unmap_eapsim_basictypes(r, esvp->vp_octets, esvp->length);
1409 char const *radius_dir = RADDBDIR;
1411 int radlog(int lvl, char const *msg, ...)
1417 r = vfprintf(stderr, msg, ap);
1419 fputc('\n', stderr);
1424 main(int argc, char *argv[])
1427 RADIUS_PACKET *req,*req2;
1428 VALUE_PAIR *vp, *vpkey, *vpextra;
1429 extern unsigned int sha1_data_problems;
1436 sha1_data_problems = 1;
1439 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1440 fr_perror("radclient");
1444 req = rad_alloc(NULL, 1)
1446 fr_perror("radclient");
1450 req2 = rad_alloc(NULL, 1);
1452 fr_perror("radclient");
1457 if(req->vps) pairfree(&req->vps);
1458 if(req2->vps) pairfree(&req2->vps);
1460 if ((req->vps = readvp2(NULL, stdin, &filedone, "eapsimlib:")) == NULL) {
1464 if (fr_debug_flag > 1) {
1465 printf("\nRead:\n");
1466 vp_printlist(stdout, req->vps);
1469 map_eapsim_types(req);
1470 map_eap_methods(req);
1472 if (fr_debug_flag > 1) {
1473 printf("Mapped to:\n");
1474 vp_printlist(stdout, req->vps);
1477 /* find the EAP-Message, copy it to req2 */
1478 vp = paircopy2(NULL, req->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1482 pairadd(&req2->vps, vp);
1484 /* only call unmap for sim types here */
1485 unmap_eap_methods(req2);
1486 unmap_eapsim_types(req2);
1488 if (fr_debug_flag > 1) {
1489 printf("Unmapped to:\n");
1490 vp_printlist(stdout, req2->vps);
1493 vp = pairfind(req2->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC, 0, TAG_ANY);
1494 vpkey = pairfind(req->vps, ATTRIBUTE_EAP_SIM_KEY, 0, TAG_ANY);
1495 vpextra = pairfind(req->vps, ATTRIBUTE_EAP_SIM_EXTRA, 0, TAG_ANY);
1497 if(vp != NULL && vpkey != NULL && vpextra!=NULL) {
1498 uint8_t calcmac[16];
1500 /* find the EAP-Message, copy it to req2 */
1502 memset(calcmac, 0, sizeof(calcmac));
1503 printf("Confirming MAC...");
1504 if(eapsim_checkmac(req2->vps, vpkey->vp_strvalue,
1505 vpextra->vp_strvalue, vpextra->length,
1507 printf("succeed\n");
1511 printf("calculated MAC (");
1512 for (i = 0; i < 20; i++) {
1519 printf("%02x", calcmac[i]);
1521 printf(" did not match\n");