2 * radeapclient.c EAP specific radius packet debug tool.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/libradius.h>
36 #include <freeradius-devel/conf.h>
37 #include <freeradius-devel/radpaths.h>
38 #include <freeradius-devel/md5.h>
40 #include "eap_types.h"
43 extern int sha1_data_problems;
45 static int retries = 10;
46 static float timeout = 3;
47 static const char *secret = NULL;
48 static int do_output = 1;
49 static int do_summary = 0;
50 static int filedone = 0;
51 static int totalapp = 0;
52 static int totaldeny = 0;
53 static char filesecret[256];
54 const char *radius_dir = RADDBDIR;
55 const char *progname = "radeapclient";
56 /* lrad_randctx randctx; */
59 radlog_dest_t radlog_dest = RADLOG_STDERR;
60 const char *radlog_dir = NULL;
62 struct main_config_t mainconfig;
65 struct eapsim_keys eapsim_mk;
67 static void NEVER_RETURNS usage(void)
69 fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
71 fprintf(stderr, " <command> One of auth, acct, status, or disconnect.\n");
72 fprintf(stderr, " -c count Send each packet 'count' times.\n");
73 fprintf(stderr, " -d raddb Set dictionary directory.\n");
74 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
75 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retries' times.\n");
76 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying (may be a floating point number).\n");
77 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
78 fprintf(stderr, " -S file read secret from file, not command line.\n");
79 fprintf(stderr, " -q Do not print anything out.\n");
80 fprintf(stderr, " -s Print out summary information of auth results.\n");
81 fprintf(stderr, " -v Show program version information.\n");
82 fprintf(stderr, " -x Debugging mode.\n");
87 int radlog(int lvl, const char *msg, ...)
92 r = lvl; /* shut up compiler */
95 r = vfprintf(stderr, msg, ap);
102 int log_debug(const char *msg, ...)
108 r = vfprintf(stderr, msg, ap);
115 static int getport(const char *name)
119 svp = getservbyname (name, "udp");
124 return ntohs(svp->s_port);
127 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
132 for (i = 0; i < retries; i++) {
135 rad_send(req, NULL, secret);
137 /* And wait for reply, timing out as necessary */
139 FD_SET(req->sockfd, &rdfdesc);
141 tv.tv_sec = (int)timeout;
142 tv.tv_usec = 1000000 * (timeout - (int) timeout);
144 /* Something's wrong if we don't get exactly one fd. */
145 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
149 *rep = rad_recv(req->sockfd);
152 * If we get a response from a machine
153 * which we did NOT send a request to,
156 if (((*rep)->src_ipaddr.af != req->dst_ipaddr.af) ||
157 (memcmp(&(*rep)->src_ipaddr.ipaddr,
158 &req->dst_ipaddr.ipaddr,
159 ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
160 sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
161 sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
162 ((*rep)->src_port != req->dst_port)) {
163 char src[128], dst[128];
165 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
166 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
167 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
169 src, (*rep)->src_port);
173 } else { /* NULL: couldn't receive the packet */
174 librad_perror("radclient:");
179 /* No response or no data read (?) */
181 fprintf(stderr, "radclient: no response from server\n");
186 * FIXME: Discard the packet & listen for another.
188 * Hmm... we should really be using eapol_test, which does
189 * a lot more than radeapclient.
191 if (rad_verify(*rep, req, secret) != 0) {
192 librad_perror("rad_verify");
196 if (rad_decode(*rep, req, secret) != 0) {
197 librad_perror("rad_decode");
201 /* libradius debug already prints out the value pairs for us */
202 if (!librad_debug && do_output) {
203 printf("Received response ID %d, code %d, length = %d\n",
204 (*rep)->id, (*rep)->code, (*rep)->data_len);
205 vp_printlist(stdout, (*rep)->vps);
207 if((*rep)->code == PW_AUTHENTICATION_ACK) {
216 static void cleanresp(RADIUS_PACKET *resp)
218 VALUE_PAIR *vpnext, *vp, **last;
222 * maybe should just copy things we care about, or keep
223 * a copy of the original input and start from there again?
225 pairdelete(&resp->vps, PW_EAP_MESSAGE);
226 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY);
229 for(vp = *last; vp != NULL; vp = vpnext)
233 if((vp->attribute > ATTRIBUTE_EAP_BASE &&
234 vp->attribute <= ATTRIBUTE_EAP_BASE+256) ||
235 (vp->attribute > ATTRIBUTE_EAP_SIM_BASE &&
236 vp->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
247 * we got an EAP-Request/Sim/Start message in a legal state.
249 * pick a supported version, put it into the reply, and insert a nonce.
251 static int process_eap_start(RADIUS_PACKET *req,
254 VALUE_PAIR *vp, *newvp;
255 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
256 uint16_t *versions, selectedversion;
257 unsigned int i,versioncount;
259 /* form new response clear of any EAP stuff */
262 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST)) == NULL) {
263 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
267 versions = (uint16_t *)vp->vp_strvalue;
269 /* verify that the attribute length is big enough for a length field */
272 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %d\n", vp->length);
276 versioncount = ntohs(versions[0])/2;
277 /* verify that the attribute length is big enough for the given number
278 * of versions present.
280 if((unsigned)vp->length <= (versioncount*2 + 2))
282 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %d bytes\n", versioncount, vp->length);
287 * record the versionlist for the MK calculation.
289 eapsim_mk.versionlistlen = versioncount*2;
290 memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
291 eapsim_mk.versionlistlen);
293 /* walk the version list, and pick the one we support, which
294 * at present, is 1, EAP_SIM_VERSION.
297 for(i=0; i < versioncount; i++)
299 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
301 selectedversion=EAP_SIM_VERSION;
305 if(selectedversion == 0)
307 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
308 for(i=0; i < versioncount; i++)
310 fprintf(stderr, "\tfound version %d\n",
311 ntohs(versions[i+1]));
316 * now make sure that we have only FULLAUTH_ID_REQ.
317 * I think that it actually might not matter - we can answer in
318 * anyway we like, but it is illegal to have more than one
321 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ);
322 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ);
323 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ);
325 if(fullauthidreq_vp == NULL ||
326 anyidreq_vp != NULL ||
327 permanentidreq_vp != NULL) {
328 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
329 (anyidreq_vp != NULL ? "a " : "no "),
330 (fullauthidreq_vp != NULL ? "a " : "no "),
331 (permanentidreq_vp != NULL ? "a " : "no "));
335 /* okay, we have just any_id_req there, so fill in response */
337 /* mark the subtype as being EAP-SIM/Response/Start */
338 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
339 newvp->lvalue = eapsim_start;
340 pairreplace(&(rep->vps), newvp);
342 /* insert selected version into response. */
343 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
345 versions = (uint16_t *)newvp->vp_strvalue;
346 versions[0] = htons(selectedversion);
348 pairreplace(&(rep->vps), newvp);
350 /* record the selected version */
351 memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
358 * insert a nonce_mt that we make up.
360 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
362 newvp->vp_octets[0]=0;
363 newvp->vp_octets[1]=0;
364 newvp->length = 18; /* 16 bytes of nonce + padding */
366 nonce[0]=lrad_rand();
367 nonce[1]=lrad_rand();
368 nonce[2]=lrad_rand();
369 nonce[3]=lrad_rand();
370 memcpy(&newvp->vp_octets[2], nonce, 16);
371 pairreplace(&(rep->vps), newvp);
373 /* also keep a copy of the nonce! */
374 memcpy(eapsim_mk.nonce_mt, nonce, 16);
378 uint16_t *pidlen, idlen;
381 * insert the identity here.
383 vp = pairfind(rep->vps, PW_USER_NAME);
386 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
389 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
391 idlen = strlen(vp->vp_strvalue);
392 pidlen = (uint16_t *)newvp->vp_strvalue;
393 *pidlen = htons(idlen);
394 newvp->length = idlen + 2;
396 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
397 pairreplace(&(rep->vps), newvp);
400 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
401 eapsim_mk.identitylen = idlen;
408 * we got an EAP-Request/Sim/Challenge message in a legal state.
410 * use the RAND challenge to produce the SRES result, and then
411 * use that to generate a new MAC.
413 * for the moment, we ignore the RANDs, then just plug in the SRES
417 static int process_eap_challenge(RADIUS_PACKET *req,
421 VALUE_PAIR *mac, *randvp;
422 VALUE_PAIR *sres1,*sres2,*sres3;
423 VALUE_PAIR *Kc1, *Kc2, *Kc3;
426 /* look for the AT_MAC and the challenge data */
427 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
428 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND);
429 if(mac == NULL || rand == NULL) {
430 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
435 * compare RAND with randX, to verify this is the right response
439 VALUE_PAIR *randcfgvp[3];
440 unsigned char *randcfg[3];
442 randcfg[0] = &randvp->vp_strvalue[2];
443 randcfg[1] = &randvp->vp_strvalue[2+EAPSIM_RAND_SIZE];
444 randcfg[2] = &randvp->vp_strvalue[2+EAPSIM_RAND_SIZE*2];
446 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1);
447 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2);
448 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3);
450 if(randcfgvp[0] == NULL ||
451 randcfgvp[1] == NULL ||
452 randcfgvp[2] == NULL) {
453 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
457 if(memcmp(randcfg[0], randcfgvp[0]->vp_strvalue, EAPSIM_RAND_SIZE)!=0 ||
458 memcmp(randcfg[1], randcfgvp[1]->vp_strvalue, EAPSIM_RAND_SIZE)!=0 ||
459 memcmp(randcfg[2], randcfgvp[2]->vp_strvalue, EAPSIM_RAND_SIZE)!=0) {
462 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
463 for(rnum = 0; rnum < 3; rnum++) {
464 fprintf(stderr, "received rand %d: ", rnum);
466 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
473 fprintf(stderr, "%02x", randcfg[rnum][i]);
475 fprintf(stderr, "\nconfigured rand %d: ", rnum);
477 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
484 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_strvalue[i]);
486 fprintf(stderr, "\n");
493 * now dig up the sres values from the response packet,
494 * which were put there when we read things in.
496 * Really, they should be calculated from the RAND!
499 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1);
500 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2);
501 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3);
506 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
509 memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
510 memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
511 memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
513 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1);
514 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2);
515 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3);
520 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
523 memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
524 memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
525 memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
527 /* all set, calculate keys */
528 eapsim_calculate_keys(&eapsim_mk);
531 eapsim_dump_mk(&eapsim_mk);
534 /* verify the MAC, now that we have all the keys. */
535 if(eapsim_checkmac(req->vps, eapsim_mk.K_aut,
536 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
538 printf("MAC check succeed\n");
542 printf("calculated MAC (");
543 for (i = 0; i < 20; i++) {
550 printf("%02x", calcmac[i]);
552 printf(" did not match\n");
556 /* form new response clear of any EAP stuff */
559 /* mark the subtype as being EAP-SIM/Response/Start */
560 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
561 newvp->lvalue = eapsim_challenge;
562 pairreplace(&(rep->vps), newvp);
565 * fill the SIM_MAC with a field that will in fact get appended
566 * to the packet before the MAC is calculated
568 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC,
570 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
571 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
572 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
573 newvp->length = EAPSIM_SRES_SIZE*3;
574 pairreplace(&(rep->vps), newvp);
576 newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, PW_TYPE_OCTETS);
577 memcpy(newvp->vp_strvalue, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
578 newvp->length = EAPSIM_AUTH_SIZE;
579 pairreplace(&(rep->vps), newvp);
585 * this code runs the EAP-SIM client state machine.
586 * the *request* is from the server.
587 * the *reponse* is to the server.
590 static int respond_eap_sim(RADIUS_PACKET *req,
593 enum eapsim_clientstates state, newstate;
594 enum eapsim_subtype subtype;
595 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
596 char statenamebuf[32], subtypenamebuf[32];
598 if ((radstate = paircopy2(req->vps, PW_STATE)) == NULL)
603 if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
608 /* first, dig up the state from the request packet, setting
609 * outselves to be in EAP-SIM-Start state if there is none.
612 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE)) == NULL)
614 /* must be initial request */
615 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, PW_TYPE_INTEGER);
616 statevp->lvalue = eapsim_client_init;
617 pairreplace(&(resp->vps), statevp);
619 state = statevp->lvalue;
622 * map the attributes, and authenticate them.
624 unmap_eapsim_types(req);
626 printf("<+++ EAP-sim decoded packet:\n");
627 vp_printlist(stdout, req->vps);
629 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE)) == NULL)
633 subtype = vp->lvalue;
636 * look for the appropriate state, and process incoming message
639 case eapsim_client_init:
642 newstate = process_eap_start(req, resp);
645 case eapsim_challenge:
646 case eapsim_notification:
649 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
650 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
651 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
652 /* invalid state, drop message */
657 case eapsim_client_start:
660 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
661 newstate = process_eap_start(req, resp);
664 case eapsim_challenge:
665 newstate = process_eap_challenge(req, resp);
669 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
670 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
671 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
672 /* invalid state, drop message */
679 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
680 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
684 /* copy the eap state object in */
685 pairreplace(&(resp->vps), eapid);
687 /* update stete info, and send new packet */
688 map_eapsim_types(resp);
690 /* copy the radius state object in */
691 pairreplace(&(resp->vps), radstate);
693 statevp->lvalue = newstate;
697 static int respond_eap_md5(RADIUS_PACKET *req,
700 VALUE_PAIR *vp, *id, *state;
701 int valuesize, namesize;
702 unsigned char identifier;
703 unsigned char *value;
710 if ((state = paircopy2(req->vps, PW_STATE)) == NULL)
712 fprintf(stderr, "radeapclient: no state attribute found\n");
716 if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
718 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
721 identifier = id->lvalue;
723 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5)) == NULL)
725 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
729 /* got the details of the MD5 challenge */
730 valuesize = vp->vp_octets[0];
731 value = &vp->vp_octets[1];
732 name = &vp->vp_octets[valuesize+1];
733 namesize = vp->length - (valuesize + 1);
736 if(valuesize > vp->length)
738 fprintf(stderr, "radeapclient: md5 valuesize if too big (%d > %d)\n",
739 valuesize, vp->length);
743 /* now do the CHAP operation ourself, rather than build the
744 * buffer. We could also call rad_chap_encode, but it wants
745 * a CHAP-Challenge, which we don't want to bother with.
747 lrad_MD5Init(&context);
748 lrad_MD5Update(&context, &identifier, 1);
749 lrad_MD5Update(&context, password, strlen(password));
750 lrad_MD5Update(&context, value, valuesize);
751 lrad_MD5Final(response, &context);
753 vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, PW_TYPE_OCTETS);
755 memcpy(&vp->vp_strvalue[1], response, 16);
758 pairreplace(&(rep->vps), vp);
760 pairreplace(&(rep->vps), id);
762 /* copy the state object in */
763 pairreplace(&(rep->vps), state);
770 static int sendrecv_eap(RADIUS_PACKET *rep)
772 RADIUS_PACKET *req = NULL;
773 VALUE_PAIR *vp, *vpnext;
774 int tried_eap_md5 = 0;
777 * Keep a copy of the the User-Password attribute.
779 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD)) != NULL) {
780 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
782 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD)) != NULL) {
783 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
785 * Otherwise keep a copy of the CHAP-Password attribute.
787 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
788 strlcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
796 printf("\n+++> About to send encoded packet:\n");
797 vp_printlist(stdout, rep->vps);
800 * if there are EAP types, encode them into an EAP-Message
806 * Fix up Digest-Attributes issues
808 for (vp = rep->vps; vp != NULL; vp = vp->next) {
809 switch (vp->attribute) {
813 case PW_DIGEST_REALM:
814 case PW_DIGEST_NONCE:
815 case PW_DIGEST_METHOD:
818 case PW_DIGEST_ALGORITHM:
819 case PW_DIGEST_BODY_DIGEST:
820 case PW_DIGEST_CNONCE:
821 case PW_DIGEST_NONCE_COUNT:
822 case PW_DIGEST_USER_NAME:
824 memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
825 vp->vp_octets[0] = vp->attribute - PW_DIGEST_REALM + 1;
827 vp->vp_octets[1] = vp->length;
828 vp->attribute = PW_DIGEST_ATTRIBUTES;
834 * If we've already sent a packet, free up the old
835 * one, and ensure that the next packet has a unique
836 * ID and authentication vector.
843 librad_md5_calc(rep->vector, rep->vector,
844 sizeof(rep->vector));
846 if (*password != '\0') {
847 if ((vp = pairfind(rep->vps, PW_CLEARTEXT_PASSWORD)) != NULL) {
848 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
849 vp->length = strlen(password);
851 } else if ((vp = pairfind(rep->vps, PW_USER_PASSWORD)) != NULL) {
852 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
853 vp->length = strlen(password);
855 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
856 strlcpy((char *)vp->vp_strvalue, password, sizeof(vp->vp_strvalue));
857 vp->length = strlen(password);
859 rad_chap_encode(rep, (char *) vp->vp_strvalue, rep->id, vp);
862 } /* there WAS a password */
864 /* send the response, wait for the next request */
865 send_packet(rep, &req);
867 /* okay got back the packet, go and decode the EAP-Message. */
868 unmap_eap_types(req);
870 printf("<+++ EAP decoded packet:\n");
871 vp_printlist(stdout, req->vps);
873 /* now look for the code type. */
874 for (vp = req->vps; vp != NULL; vp = vpnext) {
877 switch (vp->attribute) {
881 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
882 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
889 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
890 if(respond_eap_sim(req, rep))
902 int main(int argc, char **argv)
908 char *filename = NULL;
913 id = ((int)getpid() & 0xff);
916 radlog_dest = RADLOG_STDERR;
918 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
922 if (!isdigit((int) *optarg))
924 count = atoi(optarg);
942 sha1_data_problems = 1; /* for debugging only */
949 if (!isdigit((int) *optarg))
951 retries = atoi(optarg);
954 if (!isdigit((int) *optarg))
957 if ((id < 0) || (id > 255)) {
965 if (!isdigit((int) *optarg))
967 timeout = atof(optarg);
970 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
974 fp = fopen(optarg, "r");
976 fprintf(stderr, "radclient: Error opening %s: %s\n",
977 optarg, strerror(errno));
980 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
981 fprintf(stderr, "radclient: Error reading %s: %s\n",
982 optarg, strerror(errno));
987 /* truncate newline */
988 p = filesecret + strlen(filesecret) - 1;
989 while ((p >= filesecret) &&
995 if (strlen(filesecret) < 2) {
996 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1007 argc -= (optind - 1);
1008 argv += (optind - 1);
1011 ((secret == NULL) && (argc < 4))) {
1015 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1016 librad_perror("radclient");
1020 if ((req = rad_alloc(1)) == NULL) {
1021 librad_perror("radclient");
1029 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1031 perror("/dev/urandom");
1033 fread(randctx.randrsl, 256, 1, randinit);
1037 lrad_randinit(&randctx, 1);
1043 * Strip port from hostname if needed.
1045 if ((p = strchr(argv[1], ':')) != NULL) {
1051 * See what kind of request we want to send.
1053 if (strcmp(argv[2], "auth") == 0) {
1054 if (port == 0) port = getport("radius");
1055 if (port == 0) port = PW_AUTH_UDP_PORT;
1056 req->code = PW_AUTHENTICATION_REQUEST;
1058 } else if (strcmp(argv[2], "acct") == 0) {
1059 if (port == 0) port = getport("radacct");
1060 if (port == 0) port = PW_ACCT_UDP_PORT;
1061 req->code = PW_ACCOUNTING_REQUEST;
1064 } else if (strcmp(argv[2], "status") == 0) {
1065 if (port == 0) port = getport("radius");
1066 if (port == 0) port = PW_AUTH_UDP_PORT;
1067 req->code = PW_STATUS_SERVER;
1069 } else if (strcmp(argv[2], "disconnect") == 0) {
1070 if (port == 0) port = PW_POD_UDP_PORT;
1071 req->code = PW_DISCONNECT_REQUEST;
1073 } else if (isdigit((int) argv[2][0])) {
1074 if (port == 0) port = getport("radius");
1075 if (port == 0) port = PW_AUTH_UDP_PORT;
1076 req->code = atoi(argv[2]);
1082 * Ensure that the configuration is initialized.
1084 memset(&mainconfig, 0, sizeof(mainconfig));
1089 req->dst_port = port;
1090 if (ip_hton(argv[1], AF_INET, &req->dst_ipaddr) < 0) {
1091 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
1098 if (argv[3]) secret = argv[3];
1102 * Maybe read them, from stdin, if there's no
1103 * filename, or if the filename is '-'.
1105 if (filename && (strcmp(filename, "-") != 0)) {
1106 fp = fopen(filename, "r");
1108 fprintf(stderr, "radclient: Error opening %s: %s\n",
1109 filename, strerror(errno));
1119 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1120 perror("radclient: socket: ");
1125 if(req->vps) pairfree(&req->vps);
1127 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1136 printf("\n\t Total approved auths: %d\n", totalapp);
1137 printf("\t Total denied auths: %d\n", totaldeny);