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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
24 static const char rcsid[] = "$Id$";
38 #include <sys/socket.h>
41 # include <netinet/in.h>
45 # include <sys/select.h>
55 #include "../../include/md5.h"
57 #include "eap_types.h"
60 extern int sha1_data_problems;
62 static int retries = 10;
63 static float timeout = 3;
64 static const char *secret = NULL;
65 static int do_output = 1;
66 static int do_summary = 0;
67 static int filedone = 0;
68 static int totalapp = 0;
69 static int totaldeny = 0;
70 static char filesecret[256];
71 const char *radius_dir = RADDBDIR;
72 const char *progname = "radeapclient";
73 /* lrad_randctx randctx; */
76 radlog_dest_t radlog_dest = RADLOG_STDERR;
77 const char *radlog_dir = NULL;
79 struct main_config_t mainconfig;
82 struct eapsim_keys eapsim_mk;
84 static void NEVER_RETURNS usage(void)
86 fprintf(stderr, "Usage: radeapclient [options] server[:port] <command> [<secret>]\n");
88 fprintf(stderr, " <command> One of auth, acct, status, or disconnect.\n");
89 fprintf(stderr, " -c count Send each packet 'count' times.\n");
90 fprintf(stderr, " -d raddb Set dictionary directory.\n");
91 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
92 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retries' times.\n");
93 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying (may be a floating point number).\n");
94 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
95 fprintf(stderr, " -S file read secret from file, not command line.\n");
96 fprintf(stderr, " -q Do not print anything out.\n");
97 fprintf(stderr, " -s Print out summary information of auth results.\n");
98 fprintf(stderr, " -v Show program version information.\n");
99 fprintf(stderr, " -x Debugging mode.\n");
104 int radlog(int lvl, const char *msg, ...)
109 r = lvl; /* shut up compiler */
112 r = vfprintf(stderr, msg, ap);
119 int log_debug(const char *msg, ...)
125 r = vfprintf(stderr, msg, ap);
132 static int getport(const char *name)
136 svp = getservbyname (name, "udp");
141 return ntohs(svp->s_port);
144 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
149 for (i = 0; i < retries; i++) {
152 rad_send(req, NULL, secret);
154 /* And wait for reply, timing out as necessary */
156 FD_SET(req->sockfd, &rdfdesc);
158 tv.tv_sec = (int)timeout;
159 tv.tv_usec = 1000000 * (timeout - (int) timeout);
161 /* Something's wrong if we don't get exactly one fd. */
162 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
166 *rep = rad_recv(req->sockfd);
169 * If we get a response from a machine
170 * which we did NOT send a request to,
173 if (((*rep)->src_ipaddr != req->dst_ipaddr) ||
174 ((*rep)->src_port != req->dst_port)) {
175 char src[64], dst[64];
177 ip_ntoa(src, (*rep)->src_ipaddr);
178 ip_ntoa(dst, req->dst_ipaddr);
179 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
181 src, (*rep)->src_port);
185 } else { /* NULL: couldn't receive the packet */
186 librad_perror("radclient:");
191 /* No response or no data read (?) */
193 fprintf(stderr, "radclient: no response from server\n");
198 * FIXME: Discard the packet & listen for another.
200 * Hmm... we should really be using eapol_test, which does
201 * a lot more than radeapclient.
203 if (rad_verify(*rep, req, secret) != 0) {
204 librad_perror("rad_verify");
208 if (rad_decode(*rep, req, secret) != 0) {
209 librad_perror("rad_decode");
213 /* libradius debug already prints out the value pairs for us */
214 if (!librad_debug && do_output) {
215 printf("Received response ID %d, code %d, length = %d\n",
216 (*rep)->id, (*rep)->code, (*rep)->data_len);
217 vp_printlist(stdout, (*rep)->vps);
219 if((*rep)->code == PW_AUTHENTICATION_ACK) {
228 static void cleanresp(RADIUS_PACKET *resp)
230 VALUE_PAIR *vpnext, *vp, **last;
234 * maybe should just copy things we care about, or keep
235 * a copy of the original input and start from there again?
237 pairdelete(&resp->vps, PW_EAP_MESSAGE);
238 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY);
241 for(vp = *last; vp != NULL; vp = vpnext)
245 if((vp->attribute > ATTRIBUTE_EAP_BASE &&
246 vp->attribute <= ATTRIBUTE_EAP_BASE+256) ||
247 (vp->attribute > ATTRIBUTE_EAP_SIM_BASE &&
248 vp->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
259 * we got an EAP-Request/Sim/Start message in a legal state.
261 * pick a supported version, put it into the reply, and insert a nonce.
263 static int process_eap_start(RADIUS_PACKET *req,
266 VALUE_PAIR *vp, *newvp;
267 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
268 uint16_t *versions, selectedversion;
269 unsigned int i,versioncount;
271 /* form new response clear of any EAP stuff */
274 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST)) == NULL) {
275 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
279 versions = (uint16_t *)vp->strvalue;
281 /* verify that the attribute length is big enough for a length field */
284 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %d\n", vp->length);
288 versioncount = ntohs(versions[0])/2;
289 /* verify that the attribute length is big enough for the given number
290 * of versions present.
292 if((unsigned)vp->length <= (versioncount*2 + 2))
294 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %d bytes\n", versioncount, vp->length);
299 * record the versionlist for the MK calculation.
301 eapsim_mk.versionlistlen = versioncount*2;
302 memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
303 eapsim_mk.versionlistlen);
305 /* walk the version list, and pick the one we support, which
306 * at present, is 1, EAP_SIM_VERSION.
309 for(i=0; i < versioncount; i++)
311 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
313 selectedversion=EAP_SIM_VERSION;
317 if(selectedversion == 0)
319 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
320 for(i=0; i < versioncount; i++)
322 fprintf(stderr, "\tfound version %d\n",
323 ntohs(versions[i+1]));
328 * now make sure that we have only FULLAUTH_ID_REQ.
329 * I think that it actually might not matter - we can answer in
330 * anyway we like, but it is illegal to have more than one
333 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ);
334 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ);
335 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ);
337 if(fullauthidreq_vp == NULL ||
338 anyidreq_vp != NULL ||
339 permanentidreq_vp != NULL) {
340 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
341 (anyidreq_vp != NULL ? "a " : "no "),
342 (fullauthidreq_vp != NULL ? "a " : "no "),
343 (permanentidreq_vp != NULL ? "a " : "no "));
347 /* okay, we have just any_id_req there, so fill in response */
349 /* mark the subtype as being EAP-SIM/Response/Start */
350 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
351 newvp->lvalue = eapsim_start;
352 pairreplace(&(rep->vps), newvp);
354 /* insert selected version into response. */
355 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
357 versions = (uint16_t *)newvp->strvalue;
358 versions[0] = htons(selectedversion);
360 pairreplace(&(rep->vps), newvp);
362 /* record the selected version */
363 memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
370 * insert a nonce_mt that we make up.
372 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
374 newvp->strvalue[0]=0;
375 newvp->strvalue[1]=0;
376 newvp->length = 18; /* 16 bytes of nonce + padding */
378 nonce[0]=lrad_rand();
379 nonce[1]=lrad_rand();
380 nonce[2]=lrad_rand();
381 nonce[3]=lrad_rand();
382 memcpy(&newvp->strvalue[2], nonce, 16);
383 pairreplace(&(rep->vps), newvp);
385 /* also keep a copy of the nonce! */
386 memcpy(eapsim_mk.nonce_mt, nonce, 16);
390 uint16_t *pidlen, idlen;
393 * insert the identity here.
395 vp = pairfind(rep->vps, PW_USER_NAME);
398 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
401 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
403 idlen = strlen(vp->strvalue);
404 pidlen = (uint16_t *)newvp->strvalue;
405 *pidlen = htons(idlen);
406 newvp->length = idlen + 2;
408 memcpy(&newvp->strvalue[2], vp->strvalue, idlen);
409 pairreplace(&(rep->vps), newvp);
412 memcpy(eapsim_mk.identity, vp->strvalue, idlen);
413 eapsim_mk.identitylen = idlen;
420 * we got an EAP-Request/Sim/Challenge message in a legal state.
422 * use the RAND challenge to produce the SRES result, and then
423 * use that to generate a new MAC.
425 * for the moment, we ignore the RANDs, then just plug in the SRES
429 static int process_eap_challenge(RADIUS_PACKET *req,
433 VALUE_PAIR *mac, *randvp;
434 VALUE_PAIR *sres1,*sres2,*sres3;
435 VALUE_PAIR *Kc1, *Kc2, *Kc3;
438 /* look for the AT_MAC and the challenge data */
439 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
440 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND);
441 if(mac == NULL || randvp == NULL) {
442 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
447 * compare RAND with randX, to verify this is the right response
451 VALUE_PAIR *randcfgvp[3];
452 unsigned char *randcfg[3];
454 randcfg[0] = &randvp->strvalue[2];
455 randcfg[1] = &randvp->strvalue[2+EAPSIM_RAND_SIZE];
456 randcfg[2] = &randvp->strvalue[2+EAPSIM_RAND_SIZE*2];
458 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1);
459 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2);
460 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3);
462 if(randcfgvp[0] == NULL ||
463 randcfgvp[1] == NULL ||
464 randcfgvp[2] == NULL) {
465 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
469 if(memcmp(randcfg[0], randcfgvp[0]->strvalue, EAPSIM_RAND_SIZE)!=0 ||
470 memcmp(randcfg[1], randcfgvp[1]->strvalue, EAPSIM_RAND_SIZE)!=0 ||
471 memcmp(randcfg[2], randcfgvp[2]->strvalue, EAPSIM_RAND_SIZE)!=0) {
474 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
475 for(rnum = 0; rnum < 3; rnum++) {
476 fprintf(stderr, "received rand %d: ", rnum);
478 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
485 fprintf(stderr, "%02x", randcfg[rnum][i]);
487 fprintf(stderr, "\nconfigured rand %d: ", rnum);
489 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
496 fprintf(stderr, "%02x", randcfgvp[rnum]->strvalue[i]);
498 fprintf(stderr, "\n");
505 * now dig up the sres values from the response packet,
506 * which were put there when we read things in.
508 * Really, they should be calculated from the RAND!
511 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1);
512 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2);
513 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3);
518 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
521 memcpy(eapsim_mk.sres[0], sres1->strvalue, sizeof(eapsim_mk.sres[0]));
522 memcpy(eapsim_mk.sres[1], sres2->strvalue, sizeof(eapsim_mk.sres[1]));
523 memcpy(eapsim_mk.sres[2], sres3->strvalue, sizeof(eapsim_mk.sres[2]));
525 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1);
526 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2);
527 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3);
532 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
535 memcpy(eapsim_mk.Kc[0], Kc1->strvalue, sizeof(eapsim_mk.Kc[0]));
536 memcpy(eapsim_mk.Kc[1], Kc2->strvalue, sizeof(eapsim_mk.Kc[1]));
537 memcpy(eapsim_mk.Kc[2], Kc3->strvalue, sizeof(eapsim_mk.Kc[2]));
539 /* all set, calculate keys */
540 eapsim_calculate_keys(&eapsim_mk);
543 eapsim_dump_mk(&eapsim_mk);
546 /* verify the MAC, now that we have all the keys. */
547 if(eapsim_checkmac(req->vps, eapsim_mk.K_aut,
548 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
550 printf("MAC check succeed\n");
554 printf("calculated MAC (");
555 for (i = 0; i < 20; i++) {
562 printf("%02x", calcmac[i]);
564 printf(" did not match\n");
568 /* form new response clear of any EAP stuff */
571 /* mark the subtype as being EAP-SIM/Response/Start */
572 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
573 newvp->lvalue = eapsim_challenge;
574 pairreplace(&(rep->vps), newvp);
577 * fill the SIM_MAC with a field that will in fact get appended
578 * to the packet before the MAC is calculated
580 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC,
582 memcpy(newvp->strvalue+EAPSIM_SRES_SIZE*0, sres1->strvalue, EAPSIM_SRES_SIZE);
583 memcpy(newvp->strvalue+EAPSIM_SRES_SIZE*1, sres2->strvalue, EAPSIM_SRES_SIZE);
584 memcpy(newvp->strvalue+EAPSIM_SRES_SIZE*2, sres3->strvalue, EAPSIM_SRES_SIZE);
585 newvp->length = EAPSIM_SRES_SIZE*3;
586 pairreplace(&(rep->vps), newvp);
588 newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, PW_TYPE_OCTETS);
589 memcpy(newvp->strvalue, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
590 newvp->length = EAPSIM_AUTH_SIZE;
591 pairreplace(&(rep->vps), newvp);
597 * this code runs the EAP-SIM client state machine.
598 * the *request* is from the server.
599 * the *reponse* is to the server.
602 static int respond_eap_sim(RADIUS_PACKET *req,
605 enum eapsim_clientstates state, newstate;
606 enum eapsim_subtype subtype;
607 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
608 char statenamebuf[32], subtypenamebuf[32];
610 if ((radstate = paircopy2(req->vps, PW_STATE)) == NULL)
615 if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
620 /* first, dig up the state from the request packet, setting
621 * outselves to be in EAP-SIM-Start state if there is none.
624 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE)) == NULL)
626 /* must be initial request */
627 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, PW_TYPE_INTEGER);
628 statevp->lvalue = eapsim_client_init;
629 pairreplace(&(resp->vps), statevp);
631 state = statevp->lvalue;
634 * map the attributes, and authenticate them.
636 unmap_eapsim_types(req);
638 printf("<+++ EAP-sim decoded packet:\n");
639 vp_printlist(stdout, req->vps);
641 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE)) == NULL)
645 subtype = vp->lvalue;
648 * look for the appropriate state, and process incoming message
651 case eapsim_client_init:
654 newstate = process_eap_start(req, resp);
657 case eapsim_challenge:
658 case eapsim_notification:
661 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
662 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
663 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
664 /* invalid state, drop message */
669 case eapsim_client_start:
672 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
673 newstate = process_eap_start(req, resp);
676 case eapsim_challenge:
677 newstate = process_eap_challenge(req, resp);
681 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
682 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
683 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
684 /* invalid state, drop message */
691 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
692 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
696 /* copy the eap state object in */
697 pairreplace(&(resp->vps), eapid);
699 /* update stete info, and send new packet */
700 map_eapsim_types(resp);
702 /* copy the radius state object in */
703 pairreplace(&(resp->vps), radstate);
705 statevp->lvalue = newstate;
709 static int respond_eap_md5(RADIUS_PACKET *req,
712 VALUE_PAIR *vp, *id, *state;
713 int valuesize, namesize;
714 unsigned char identifier;
715 unsigned char *value;
722 if ((state = paircopy2(req->vps, PW_STATE)) == NULL)
724 fprintf(stderr, "radeapclient: no state attribute found\n");
728 if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
730 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
733 identifier = id->lvalue;
735 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5)) == NULL)
737 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
741 /* got the details of the MD5 challenge */
742 valuesize = vp->strvalue[0];
743 value = &vp->strvalue[1];
744 name = &vp->strvalue[valuesize+1];
745 namesize = vp->length - (valuesize + 1);
748 if(valuesize > vp->length)
750 fprintf(stderr, "radeapclient: md5 valuesize if too big (%d > %d)\n",
751 valuesize, vp->length);
755 /* now do the CHAP operation ourself, rather than build the
756 * buffer. We could also call rad_chap_encode, but it wants
757 * a CHAP-Challenge, which we don't want to bother with.
759 librad_MD5Init(&context);
760 librad_MD5Update(&context, &identifier, 1);
761 librad_MD5Update(&context, password, strlen(password));
762 librad_MD5Update(&context, value, valuesize);
763 librad_MD5Final(response, &context);
765 vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, PW_TYPE_OCTETS);
767 memcpy(&vp->strvalue[1], response, 16);
770 pairreplace(&(rep->vps), vp);
772 pairreplace(&(rep->vps), id);
774 /* copy the state object in */
775 pairreplace(&(rep->vps), state);
782 static int sendrecv_eap(RADIUS_PACKET *rep)
784 RADIUS_PACKET *req = NULL;
785 VALUE_PAIR *vp, *vpnext;
786 int tried_eap_md5 = 0;
789 * Keep a copy of the the User-Password attribute.
791 if ((vp = pairfind(rep->vps, ATTRIBUTE_EAP_MD5_PASSWORD)) != NULL) {
792 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
794 } else if ((vp = pairfind(rep->vps, PW_PASSWORD)) != NULL) {
795 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
797 * Otherwise keep a copy of the CHAP-Password attribute.
799 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
800 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
808 printf("\n+++> About to send encoded packet:\n");
809 vp_printlist(stdout, rep->vps);
812 * if there are EAP types, encode them into an EAP-Message
818 * Fix up Digest-Attributes issues
820 for (vp = rep->vps; vp != NULL; vp = vp->next) {
821 switch (vp->attribute) {
825 case PW_DIGEST_REALM:
826 case PW_DIGEST_NONCE:
827 case PW_DIGEST_METHOD:
830 case PW_DIGEST_ALGORITHM:
831 case PW_DIGEST_BODY_DIGEST:
832 case PW_DIGEST_CNONCE:
833 case PW_DIGEST_NONCE_COUNT:
834 case PW_DIGEST_USER_NAME:
836 memmove(&vp->strvalue[2], &vp->strvalue[0], vp->length);
837 vp->strvalue[0] = vp->attribute - PW_DIGEST_REALM + 1;
839 vp->strvalue[1] = vp->length;
840 vp->attribute = PW_DIGEST_ATTRIBUTES;
846 * If we've already sent a packet, free up the old
847 * one, and ensure that the next packet has a unique
848 * ID and authentication vector.
855 librad_md5_calc(rep->vector, rep->vector,
856 sizeof(rep->vector));
858 if (*password != '\0') {
859 if ((vp = pairfind(rep->vps, PW_PASSWORD)) != NULL) {
860 strNcpy((char *)vp->strvalue, password, strlen(password) + 1);
861 vp->length = strlen(password);
863 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
864 strNcpy((char *)vp->strvalue, password, strlen(password) + 1);
865 vp->length = strlen(password);
867 rad_chap_encode(rep, (char *) vp->strvalue, rep->id, vp);
870 } /* there WAS a password */
872 /* send the response, wait for the next request */
873 send_packet(rep, &req);
875 /* okay got back the packet, go and decode the EAP-Message. */
876 unmap_eap_types(req);
878 printf("<+++ EAP decoded packet:\n");
879 vp_printlist(stdout, req->vps);
881 /* now look for the code type. */
882 for (vp = req->vps; vp != NULL; vp = vpnext) {
885 switch (vp->attribute) {
889 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
890 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
897 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
898 if(respond_eap_sim(req, rep))
910 int main(int argc, char **argv)
916 char *filename = NULL;
921 id = ((int)getpid() & 0xff);
924 radlog_dest = RADLOG_STDERR;
926 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
930 if (!isdigit((int) *optarg))
932 count = atoi(optarg);
950 sha1_data_problems = 1; /* for debugging only */
957 if (!isdigit((int) *optarg))
959 retries = atoi(optarg);
962 if (!isdigit((int) *optarg))
965 if ((id < 0) || (id > 255)) {
973 if (!isdigit((int) *optarg))
975 timeout = atof(optarg);
978 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
982 fp = fopen(optarg, "r");
984 fprintf(stderr, "radclient: Error opening %s: %s\n",
985 optarg, strerror(errno));
988 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
989 fprintf(stderr, "radclient: Error reading %s: %s\n",
990 optarg, strerror(errno));
995 /* truncate newline */
996 p = filesecret + strlen(filesecret) - 1;
997 while ((p >= filesecret) &&
1003 if (strlen(filesecret) < 2) {
1004 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1007 secret = filesecret;
1015 argc -= (optind - 1);
1016 argv += (optind - 1);
1019 ((secret == NULL) && (argc < 4))) {
1023 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1024 librad_perror("radclient");
1028 if ((req = rad_alloc(1)) == NULL) {
1029 librad_perror("radclient");
1037 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1039 perror("/dev/urandom");
1041 fread(randctx.randrsl, 256, 1, randinit);
1045 lrad_randinit(&randctx, 1);
1051 * Strip port from hostname if needed.
1053 if ((p = strchr(argv[1], ':')) != NULL) {
1059 * See what kind of request we want to send.
1061 if (strcmp(argv[2], "auth") == 0) {
1062 if (port == 0) port = getport("radius");
1063 if (port == 0) port = PW_AUTH_UDP_PORT;
1064 req->code = PW_AUTHENTICATION_REQUEST;
1066 } else if (strcmp(argv[2], "acct") == 0) {
1067 if (port == 0) port = getport("radacct");
1068 if (port == 0) port = PW_ACCT_UDP_PORT;
1069 req->code = PW_ACCOUNTING_REQUEST;
1072 } else if (strcmp(argv[2], "status") == 0) {
1073 if (port == 0) port = getport("radius");
1074 if (port == 0) port = PW_AUTH_UDP_PORT;
1075 req->code = PW_STATUS_SERVER;
1077 } else if (strcmp(argv[2], "disconnect") == 0) {
1078 if (port == 0) port = PW_POD_UDP_PORT;
1079 req->code = PW_DISCONNECT_REQUEST;
1081 } else if (isdigit((int) argv[2][0])) {
1082 if (port == 0) port = getport("radius");
1083 if (port == 0) port = PW_AUTH_UDP_PORT;
1084 req->code = atoi(argv[2]);
1090 * Ensure that the configuration is initialized.
1092 memset(&mainconfig, 0, sizeof(mainconfig));
1097 req->dst_port = port;
1098 req->dst_ipaddr = ip_getaddr(argv[1]);
1099 if (req->dst_ipaddr == INADDR_NONE) {
1100 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
1107 if (argv[3]) secret = argv[3];
1111 * Maybe read them, from stdin, if there's no
1112 * filename, or if the filename is '-'.
1114 if (filename && (strcmp(filename, "-") != 0)) {
1115 fp = fopen(filename, "r");
1117 fprintf(stderr, "radclient: Error opening %s: %s\n",
1118 filename, strerror(errno));
1128 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1129 perror("radclient: socket: ");
1134 if(req->vps) pairfree(&req->vps);
1136 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1145 printf("\n\t Total approved auths: %d\n", totalapp);
1146 printf("\t Total denied auths: %d\n", totaldeny);