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 static int getport(const char *name)
123 svp = getservbyname (name, "udp");
128 return ntohs(svp->s_port);
131 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
136 for (i = 0; i < retries; i++) {
139 rad_send(req, NULL, secret);
141 /* And wait for reply, timing out as necessary */
143 FD_SET(req->sockfd, &rdfdesc);
145 tv.tv_sec = (int)timeout;
146 tv.tv_usec = 1000000 * (timeout - (int) timeout);
148 /* Something's wrong if we don't get exactly one fd. */
149 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
153 *rep = rad_recv(req->sockfd);
156 * If we get a response from a machine
157 * which we did NOT send a request to,
160 if (((*rep)->src_ipaddr != req->dst_ipaddr) ||
161 ((*rep)->src_port != req->dst_port)) {
162 char src[64], dst[64];
164 ip_ntoa(src, (*rep)->src_ipaddr);
165 ip_ntoa(dst, req->dst_ipaddr);
166 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
168 src, (*rep)->src_port);
172 } else { /* NULL: couldn't receive the packet */
173 librad_perror("radclient:");
178 /* No response or no data read (?) */
180 fprintf(stderr, "radclient: no response from server\n");
185 * FIXME: Discard the packet & listen for another.
187 * Hmm... we should really be using eapol_test, which does
188 * a lot more than radeapclient.
190 if (rad_verify(*rep, req, secret) != 0) {
191 librad_perror("rad_verify");
195 if (rad_decode(*rep, req, secret) != 0) {
196 librad_perror("rad_decode");
200 /* libradius debug already prints out the value pairs for us */
201 if (!librad_debug && do_output) {
202 printf("Received response ID %d, code %d, length = %d\n",
203 (*rep)->id, (*rep)->code, (*rep)->data_len);
204 vp_printlist(stdout, (*rep)->vps);
206 if((*rep)->code == PW_AUTHENTICATION_ACK) {
215 static void cleanresp(RADIUS_PACKET *resp)
217 VALUE_PAIR *vpnext, *vp, **last;
221 * maybe should just copy things we care about, or keep
222 * a copy of the original input and start from there again?
224 pairdelete(&resp->vps, PW_EAP_MESSAGE);
225 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY);
228 for(vp = *last; vp != NULL; vp = vpnext)
232 if((vp->attribute > ATTRIBUTE_EAP_BASE &&
233 vp->attribute <= ATTRIBUTE_EAP_BASE+256) ||
234 (vp->attribute > ATTRIBUTE_EAP_SIM_BASE &&
235 vp->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
246 * we got an EAP-Request/Sim/Start message in a legal state.
248 * pick a supported version, put it into the reply, and insert a nonce.
250 static int process_eap_start(RADIUS_PACKET *req,
253 VALUE_PAIR *vp, *newvp;
254 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
255 uint16_t *versions, selectedversion;
256 unsigned int i,versioncount;
258 /* form new response clear of any EAP stuff */
261 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST)) == NULL) {
262 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
266 versions = (uint16_t *)vp->strvalue;
268 /* verify that the attribute length is big enough for a length field */
271 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %d\n", vp->length);
275 versioncount = ntohs(versions[0])/2;
276 /* verify that the attribute length is big enough for the given number
277 * of versions present.
279 if((unsigned)vp->length <= (versioncount*2 + 2))
281 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %d bytes\n", versioncount, vp->length);
286 * record the versionlist for the MK calculation.
288 eapsim_mk.versionlistlen = versioncount*2;
289 memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
290 eapsim_mk.versionlistlen);
292 /* walk the version list, and pick the one we support, which
293 * at present, is 1, EAP_SIM_VERSION.
296 for(i=0; i < versioncount; i++)
298 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
300 selectedversion=EAP_SIM_VERSION;
304 if(selectedversion == 0)
306 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
307 for(i=0; i < versioncount; i++)
309 fprintf(stderr, "\tfound version %d\n",
310 ntohs(versions[i+1]));
315 * now make sure that we have only FULLAUTH_ID_REQ.
316 * I think that it actually might not matter - we can answer in
317 * anyway we like, but it is illegal to have more than one
320 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ);
321 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ);
322 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ);
324 if(fullauthidreq_vp == NULL ||
325 anyidreq_vp != NULL ||
326 permanentidreq_vp != NULL) {
327 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
328 (anyidreq_vp != NULL ? "a " : "no "),
329 (fullauthidreq_vp != NULL ? "a " : "no "),
330 (permanentidreq_vp != NULL ? "a " : "no "));
334 /* okay, we have just any_id_req there, so fill in response */
336 /* mark the subtype as being EAP-SIM/Response/Start */
337 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
338 newvp->lvalue = eapsim_start;
339 pairreplace(&(rep->vps), newvp);
341 /* insert selected version into response. */
342 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
344 versions = (uint16_t *)newvp->strvalue;
345 versions[0] = htons(selectedversion);
347 pairreplace(&(rep->vps), newvp);
349 /* record the selected version */
350 memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
357 * insert a nonce_mt that we make up.
359 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
361 newvp->strvalue[0]=0;
362 newvp->strvalue[1]=0;
363 newvp->length = 18; /* 16 bytes of nonce + padding */
365 nonce[0]=lrad_rand();
366 nonce[1]=lrad_rand();
367 nonce[2]=lrad_rand();
368 nonce[3]=lrad_rand();
369 memcpy(&newvp->strvalue[2], nonce, 16);
370 pairreplace(&(rep->vps), newvp);
372 /* also keep a copy of the nonce! */
373 memcpy(eapsim_mk.nonce_mt, nonce, 16);
377 uint16_t *pidlen, idlen;
380 * insert the identity here.
382 vp = pairfind(rep->vps, PW_USER_NAME);
385 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
388 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
390 idlen = strlen(vp->strvalue);
391 pidlen = (uint16_t *)newvp->strvalue;
392 *pidlen = htons(idlen);
393 newvp->length = idlen + 2;
395 memcpy(&newvp->strvalue[2], vp->strvalue, idlen);
396 pairreplace(&(rep->vps), newvp);
399 memcpy(eapsim_mk.identity, vp->strvalue, idlen);
400 eapsim_mk.identitylen = idlen;
407 * we got an EAP-Request/Sim/Challenge message in a legal state.
409 * use the RAND challenge to produce the SRES result, and then
410 * use that to generate a new MAC.
412 * for the moment, we ignore the RANDs, then just plug in the SRES
416 static int process_eap_challenge(RADIUS_PACKET *req,
420 VALUE_PAIR *mac, *randvp;
421 VALUE_PAIR *sres1,*sres2,*sres3;
422 VALUE_PAIR *Kc1, *Kc2, *Kc3;
425 /* look for the AT_MAC and the challenge data */
426 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
427 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND);
428 if(mac == NULL || rand == NULL) {
429 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
434 * compare RAND with randX, to verify this is the right response
438 VALUE_PAIR *randcfgvp[3];
439 unsigned char *randcfg[3];
441 randcfg[0] = &randvp->strvalue[2];
442 randcfg[1] = &randvp->strvalue[2+EAPSIM_RAND_SIZE];
443 randcfg[2] = &randvp->strvalue[2+EAPSIM_RAND_SIZE*2];
445 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1);
446 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2);
447 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3);
449 if(randcfgvp[0] == NULL ||
450 randcfgvp[1] == NULL ||
451 randcfgvp[2] == NULL) {
452 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
456 if(memcmp(randcfg[0], randcfgvp[0]->strvalue, EAPSIM_RAND_SIZE)!=0 ||
457 memcmp(randcfg[1], randcfgvp[1]->strvalue, EAPSIM_RAND_SIZE)!=0 ||
458 memcmp(randcfg[2], randcfgvp[2]->strvalue, EAPSIM_RAND_SIZE)!=0) {
461 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
462 for(rnum = 0; rnum < 3; rnum++) {
463 fprintf(stderr, "received rand %d: ", rnum);
465 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
472 fprintf(stderr, "%02x", randcfg[rnum][i]);
474 fprintf(stderr, "\nconfigured rand %d: ", rnum);
476 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
483 fprintf(stderr, "%02x", randcfgvp[rnum]->strvalue[i]);
485 fprintf(stderr, "\n");
492 * now dig up the sres values from the response packet,
493 * which were put there when we read things in.
495 * Really, they should be calculated from the RAND!
498 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1);
499 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2);
500 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3);
505 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
508 memcpy(eapsim_mk.sres[0], sres1->strvalue, sizeof(eapsim_mk.sres[0]));
509 memcpy(eapsim_mk.sres[1], sres2->strvalue, sizeof(eapsim_mk.sres[1]));
510 memcpy(eapsim_mk.sres[2], sres3->strvalue, sizeof(eapsim_mk.sres[2]));
512 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1);
513 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2);
514 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3);
519 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
522 memcpy(eapsim_mk.Kc[0], Kc1->strvalue, sizeof(eapsim_mk.Kc[0]));
523 memcpy(eapsim_mk.Kc[1], Kc2->strvalue, sizeof(eapsim_mk.Kc[1]));
524 memcpy(eapsim_mk.Kc[2], Kc3->strvalue, sizeof(eapsim_mk.Kc[2]));
526 /* all set, calculate keys */
527 eapsim_calculate_keys(&eapsim_mk);
530 eapsim_dump_mk(&eapsim_mk);
533 /* verify the MAC, now that we have all the keys. */
534 if(eapsim_checkmac(req->vps, eapsim_mk.K_aut,
535 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
537 printf("MAC check succeed\n");
541 printf("calculated MAC (");
542 for (i = 0; i < 20; i++) {
549 printf("%02x", calcmac[i]);
551 printf(" did not match\n");
555 /* form new response clear of any EAP stuff */
558 /* mark the subtype as being EAP-SIM/Response/Start */
559 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
560 newvp->lvalue = eapsim_challenge;
561 pairreplace(&(rep->vps), newvp);
564 * fill the SIM_MAC with a field that will in fact get appended
565 * to the packet before the MAC is calculated
567 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC,
569 memcpy(newvp->strvalue+EAPSIM_SRES_SIZE*0, sres1->strvalue, EAPSIM_SRES_SIZE);
570 memcpy(newvp->strvalue+EAPSIM_SRES_SIZE*1, sres2->strvalue, EAPSIM_SRES_SIZE);
571 memcpy(newvp->strvalue+EAPSIM_SRES_SIZE*2, sres3->strvalue, EAPSIM_SRES_SIZE);
572 newvp->length = EAPSIM_SRES_SIZE*3;
573 pairreplace(&(rep->vps), newvp);
575 newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, PW_TYPE_OCTETS);
576 memcpy(newvp->strvalue, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
577 newvp->length = EAPSIM_AUTH_SIZE;
578 pairreplace(&(rep->vps), newvp);
584 * this code runs the EAP-SIM client state machine.
585 * the *request* is from the server.
586 * the *reponse* is to the server.
589 static int respond_eap_sim(RADIUS_PACKET *req,
592 enum eapsim_clientstates state, newstate;
593 enum eapsim_subtype subtype;
594 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
595 char statenamebuf[32], subtypenamebuf[32];
597 if ((radstate = paircopy2(req->vps, PW_STATE)) == NULL)
602 if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
607 /* first, dig up the state from the request packet, setting
608 * outselves to be in EAP-SIM-Start state if there is none.
611 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE)) == NULL)
613 /* must be initial request */
614 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, PW_TYPE_INTEGER);
615 statevp->lvalue = eapsim_client_init;
616 pairreplace(&(resp->vps), statevp);
618 state = statevp->lvalue;
621 * map the attributes, and authenticate them.
623 unmap_eapsim_types(req);
625 printf("<+++ EAP-sim decoded packet:\n");
626 vp_printlist(stdout, req->vps);
628 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE)) == NULL)
632 subtype = vp->lvalue;
635 * look for the appropriate state, and process incoming message
638 case eapsim_client_init:
641 newstate = process_eap_start(req, resp);
644 case eapsim_challenge:
645 case eapsim_notification:
648 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
649 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
650 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
651 /* invalid state, drop message */
656 case eapsim_client_start:
659 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
660 newstate = process_eap_start(req, resp);
663 case eapsim_challenge:
664 newstate = process_eap_challenge(req, resp);
668 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
669 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
670 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
671 /* invalid state, drop message */
678 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
679 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
683 /* copy the eap state object in */
684 pairreplace(&(resp->vps), eapid);
686 /* update stete info, and send new packet */
687 map_eapsim_types(resp);
689 /* copy the radius state object in */
690 pairreplace(&(resp->vps), radstate);
692 statevp->lvalue = newstate;
696 static int respond_eap_md5(RADIUS_PACKET *req,
699 VALUE_PAIR *vp, *id, *state;
700 int valuesize, namesize;
701 unsigned char identifier;
702 unsigned char *value;
709 if ((state = paircopy2(req->vps, PW_STATE)) == NULL)
711 fprintf(stderr, "radeapclient: no state attribute found\n");
715 if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
717 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
720 identifier = id->lvalue;
722 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5)) == NULL)
724 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
728 /* got the details of the MD5 challenge */
729 valuesize = vp->strvalue[0];
730 value = &vp->strvalue[1];
731 name = &vp->strvalue[valuesize+1];
732 namesize = vp->length - (valuesize + 1);
735 if(valuesize > vp->length)
737 fprintf(stderr, "radeapclient: md5 valuesize if too big (%d > %d)\n",
738 valuesize, vp->length);
742 /* now do the CHAP operation ourself, rather than build the
743 * buffer. We could also call rad_chap_encode, but it wants
744 * a CHAP-Challenge, which we don't want to bother with.
746 librad_MD5Init(&context);
747 librad_MD5Update(&context, &identifier, 1);
748 librad_MD5Update(&context, password, strlen(password));
749 librad_MD5Update(&context, value, valuesize);
750 librad_MD5Final(response, &context);
752 vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, PW_TYPE_OCTETS);
754 memcpy(&vp->strvalue[1], response, 16);
757 pairreplace(&(rep->vps), vp);
759 pairreplace(&(rep->vps), id);
761 /* copy the state object in */
762 pairreplace(&(rep->vps), state);
769 static int sendrecv_eap(RADIUS_PACKET *rep)
771 RADIUS_PACKET *req = NULL;
772 VALUE_PAIR *vp, *vpnext;
773 int tried_eap_md5 = 0;
776 * Keep a copy of the the User-Password attribute.
778 if ((vp = pairfind(rep->vps, ATTRIBUTE_EAP_MD5_PASSWORD)) != NULL) {
779 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
781 } else if ((vp = pairfind(rep->vps, PW_PASSWORD)) != NULL) {
782 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
784 * Otherwise keep a copy of the CHAP-Password attribute.
786 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
787 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
795 printf("\n+++> About to send encoded packet:\n");
796 vp_printlist(stdout, rep->vps);
799 * if there are EAP types, encode them into an EAP-Message
805 * Fix up Digest-Attributes issues
807 for (vp = rep->vps; vp != NULL; vp = vp->next) {
808 switch (vp->attribute) {
812 case PW_DIGEST_REALM:
813 case PW_DIGEST_NONCE:
814 case PW_DIGEST_METHOD:
817 case PW_DIGEST_ALGORITHM:
818 case PW_DIGEST_BODY_DIGEST:
819 case PW_DIGEST_CNONCE:
820 case PW_DIGEST_NONCE_COUNT:
821 case PW_DIGEST_USER_NAME:
823 memmove(&vp->strvalue[2], &vp->strvalue[0], vp->length);
824 vp->strvalue[0] = vp->attribute - PW_DIGEST_REALM + 1;
826 vp->strvalue[1] = vp->length;
827 vp->attribute = PW_DIGEST_ATTRIBUTES;
833 * If we've already sent a packet, free up the old
834 * one, and ensure that the next packet has a unique
835 * ID and authentication vector.
842 librad_md5_calc(rep->vector, rep->vector,
843 sizeof(rep->vector));
845 if (*password != '\0') {
846 if ((vp = pairfind(rep->vps, PW_PASSWORD)) != NULL) {
847 strNcpy((char *)vp->strvalue, password, strlen(password) + 1);
848 vp->length = strlen(password);
850 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
851 strNcpy((char *)vp->strvalue, password, strlen(password) + 1);
852 vp->length = strlen(password);
854 rad_chap_encode(rep, (char *) vp->strvalue, rep->id, vp);
857 } /* there WAS a password */
859 /* send the response, wait for the next request */
860 send_packet(rep, &req);
862 /* okay got back the packet, go and decode the EAP-Message. */
863 unmap_eap_types(req);
865 printf("<+++ EAP decoded packet:\n");
866 vp_printlist(stdout, req->vps);
868 /* now look for the code type. */
869 for (vp = req->vps; vp != NULL; vp = vpnext) {
872 switch (vp->attribute) {
876 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
877 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
884 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
885 if(respond_eap_sim(req, rep))
897 int main(int argc, char **argv)
903 char *filename = NULL;
908 id = ((int)getpid() & 0xff);
911 radlog_dest = RADLOG_STDERR;
913 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
917 if (!isdigit((int) *optarg))
919 count = atoi(optarg);
937 sha1_data_problems = 1; /* for debugging only */
944 if (!isdigit((int) *optarg))
946 retries = atoi(optarg);
949 if (!isdigit((int) *optarg))
952 if ((id < 0) || (id > 255)) {
960 if (!isdigit((int) *optarg))
962 timeout = atof(optarg);
965 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
969 fp = fopen(optarg, "r");
971 fprintf(stderr, "radclient: Error opening %s: %s\n",
972 optarg, strerror(errno));
975 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
976 fprintf(stderr, "radclient: Error reading %s: %s\n",
977 optarg, strerror(errno));
982 /* truncate newline */
983 p = filesecret + strlen(filesecret) - 1;
984 while ((p >= filesecret) &&
990 if (strlen(filesecret) < 2) {
991 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
1002 argc -= (optind - 1);
1003 argv += (optind - 1);
1006 ((secret == NULL) && (argc < 4))) {
1010 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1011 librad_perror("radclient");
1015 if ((req = rad_alloc(1)) == NULL) {
1016 librad_perror("radclient");
1024 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1026 perror("/dev/urandom");
1028 fread(randctx.randrsl, 256, 1, randinit);
1032 lrad_randinit(&randctx, 1);
1038 * Strip port from hostname if needed.
1040 if ((p = strchr(argv[1], ':')) != NULL) {
1046 * See what kind of request we want to send.
1048 if (strcmp(argv[2], "auth") == 0) {
1049 if (port == 0) port = getport("radius");
1050 if (port == 0) port = PW_AUTH_UDP_PORT;
1051 req->code = PW_AUTHENTICATION_REQUEST;
1053 } else if (strcmp(argv[2], "acct") == 0) {
1054 if (port == 0) port = getport("radacct");
1055 if (port == 0) port = PW_ACCT_UDP_PORT;
1056 req->code = PW_ACCOUNTING_REQUEST;
1059 } else if (strcmp(argv[2], "status") == 0) {
1060 if (port == 0) port = getport("radius");
1061 if (port == 0) port = PW_AUTH_UDP_PORT;
1062 req->code = PW_STATUS_SERVER;
1064 } else if (strcmp(argv[2], "disconnect") == 0) {
1065 if (port == 0) port = PW_POD_UDP_PORT;
1066 req->code = PW_DISCONNECT_REQUEST;
1068 } else if (isdigit((int) argv[2][0])) {
1069 if (port == 0) port = getport("radius");
1070 if (port == 0) port = PW_AUTH_UDP_PORT;
1071 req->code = atoi(argv[2]);
1077 * Ensure that the configuration is initialized.
1079 memset(&mainconfig, 0, sizeof(mainconfig));
1084 req->dst_port = port;
1085 req->dst_ipaddr = ip_getaddr(argv[1]);
1086 if (req->dst_ipaddr == INADDR_NONE) {
1087 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
1094 if (argv[3]) secret = argv[3];
1098 * Maybe read them, from stdin, if there's no
1099 * filename, or if the filename is '-'.
1101 if (filename && (strcmp(filename, "-") != 0)) {
1102 fp = fopen(filename, "r");
1104 fprintf(stderr, "radclient: Error opening %s: %s\n",
1105 filename, strerror(errno));
1115 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1116 perror("radclient: socket: ");
1121 if(req->vps) pairfree(&req->vps);
1123 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1132 printf("\n\t Total approved auths: %d\n", totalapp);
1133 printf("\t Total denied auths: %d\n", totaldeny);