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.af != req->dst_ipaddr.af) ||
161 (memcmp(&(*rep)->src_ipaddr.ipaddr,
162 &req->dst_ipaddr.ipaddr,
163 ((req->dst_ipaddr.af == AF_INET ? /* AF_INET6? */
164 sizeof(req->dst_ipaddr.ipaddr.ip4addr) : /* FIXME: AF_INET6 */
165 sizeof(req->dst_ipaddr.ipaddr.ip6addr)))) != 0) ||
166 ((*rep)->src_port != req->dst_port)) {
167 char src[128], dst[128];
169 ip_ntoh(&(*rep)->src_ipaddr, src, sizeof(src));
170 ip_ntoh(&req->dst_ipaddr, dst, sizeof(dst));
171 fprintf(stderr, "radclient: ERROR: Sent request to host %s port %d, got response from host %s port %d\n!",
173 src, (*rep)->src_port);
177 } else { /* NULL: couldn't receive the packet */
178 librad_perror("radclient:");
183 /* No response or no data read (?) */
185 fprintf(stderr, "radclient: no response from server\n");
189 if (rad_decode(*rep, req, secret) != 0) {
190 librad_perror("rad_decode");
194 /* libradius debug already prints out the value pairs for us */
195 if (!librad_debug && do_output) {
196 printf("Received response ID %d, code %d, length = %d\n",
197 (*rep)->id, (*rep)->code, (*rep)->data_len);
198 vp_printlist(stdout, (*rep)->vps);
200 if((*rep)->code == PW_AUTHENTICATION_ACK) {
209 static void cleanresp(RADIUS_PACKET *resp)
211 VALUE_PAIR *vpnext, *vp, **last;
215 * maybe should just copy things we care about, or keep
216 * a copy of the original input and start from there again?
218 pairdelete(&resp->vps, PW_EAP_MESSAGE);
219 pairdelete(&resp->vps, ATTRIBUTE_EAP_BASE+PW_EAP_IDENTITY);
222 for(vp = *last; vp != NULL; vp = vpnext)
226 if((vp->attribute > ATTRIBUTE_EAP_BASE &&
227 vp->attribute <= ATTRIBUTE_EAP_BASE+256) ||
228 (vp->attribute > ATTRIBUTE_EAP_SIM_BASE &&
229 vp->attribute <= ATTRIBUTE_EAP_SIM_BASE+256))
240 * we got an EAP-Request/Sim/Start message in a legal state.
242 * pick a supported version, put it into the reply, and insert a nonce.
244 static int process_eap_start(RADIUS_PACKET *req,
247 VALUE_PAIR *vp, *newvp;
248 VALUE_PAIR *anyidreq_vp, *fullauthidreq_vp, *permanentidreq_vp;
249 uint16_t *versions, selectedversion;
250 unsigned int i,versioncount;
252 /* form new response clear of any EAP stuff */
255 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_VERSION_LIST)) == NULL) {
256 fprintf(stderr, "illegal start message has no VERSION_LIST\n");
260 versions = (uint16_t *)vp->vp_strvalue;
262 /* verify that the attribute length is big enough for a length field */
265 fprintf(stderr, "start message has illegal VERSION_LIST. Too short: %d\n", vp->length);
269 versioncount = ntohs(versions[0])/2;
270 /* verify that the attribute length is big enough for the given number
271 * of versions present.
273 if((unsigned)vp->length <= (versioncount*2 + 2))
275 fprintf(stderr, "start message is too short. Claimed %d versions does not fit in %d bytes\n", versioncount, vp->length);
280 * record the versionlist for the MK calculation.
282 eapsim_mk.versionlistlen = versioncount*2;
283 memcpy(eapsim_mk.versionlist, (unsigned char *)(versions+1),
284 eapsim_mk.versionlistlen);
286 /* walk the version list, and pick the one we support, which
287 * at present, is 1, EAP_SIM_VERSION.
290 for(i=0; i < versioncount; i++)
292 if(ntohs(versions[i+1]) == EAP_SIM_VERSION)
294 selectedversion=EAP_SIM_VERSION;
298 if(selectedversion == 0)
300 fprintf(stderr, "eap-sim start message. No compatible version found. We need %d\n", EAP_SIM_VERSION);
301 for(i=0; i < versioncount; i++)
303 fprintf(stderr, "\tfound version %d\n",
304 ntohs(versions[i+1]));
309 * now make sure that we have only FULLAUTH_ID_REQ.
310 * I think that it actually might not matter - we can answer in
311 * anyway we like, but it is illegal to have more than one
314 anyidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_ANY_ID_REQ);
315 fullauthidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_FULLAUTH_ID_REQ);
316 permanentidreq_vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_PERMANENT_ID_REQ);
318 if(fullauthidreq_vp == NULL ||
319 anyidreq_vp != NULL ||
320 permanentidreq_vp != NULL) {
321 fprintf(stderr, "start message has %sanyidreq, %sfullauthid and %spermanentid. Illegal combination.\n",
322 (anyidreq_vp != NULL ? "a " : "no "),
323 (fullauthidreq_vp != NULL ? "a " : "no "),
324 (permanentidreq_vp != NULL ? "a " : "no "));
328 /* okay, we have just any_id_req there, so fill in response */
330 /* mark the subtype as being EAP-SIM/Response/Start */
331 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
332 newvp->lvalue = eapsim_start;
333 pairreplace(&(rep->vps), newvp);
335 /* insert selected version into response. */
336 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_SELECTED_VERSION,
338 versions = (uint16_t *)newvp->vp_strvalue;
339 versions[0] = htons(selectedversion);
341 pairreplace(&(rep->vps), newvp);
343 /* record the selected version */
344 memcpy(eapsim_mk.versionselect, (unsigned char *)versions, 2);
351 * insert a nonce_mt that we make up.
353 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_NONCE_MT,
355 newvp->vp_octets[0]=0;
356 newvp->vp_octets[1]=0;
357 newvp->length = 18; /* 16 bytes of nonce + padding */
359 nonce[0]=lrad_rand();
360 nonce[1]=lrad_rand();
361 nonce[2]=lrad_rand();
362 nonce[3]=lrad_rand();
363 memcpy(&newvp->vp_octets[2], nonce, 16);
364 pairreplace(&(rep->vps), newvp);
366 /* also keep a copy of the nonce! */
367 memcpy(eapsim_mk.nonce_mt, nonce, 16);
371 uint16_t *pidlen, idlen;
374 * insert the identity here.
376 vp = pairfind(rep->vps, PW_USER_NAME);
379 fprintf(stderr, "eap-sim: We need to have a User-Name attribute!\n");
382 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_IDENTITY,
384 idlen = strlen(vp->vp_strvalue);
385 pidlen = (uint16_t *)newvp->vp_strvalue;
386 *pidlen = htons(idlen);
387 newvp->length = idlen + 2;
389 memcpy(&newvp->vp_strvalue[2], vp->vp_strvalue, idlen);
390 pairreplace(&(rep->vps), newvp);
393 memcpy(eapsim_mk.identity, vp->vp_strvalue, idlen);
394 eapsim_mk.identitylen = idlen;
401 * we got an EAP-Request/Sim/Challenge message in a legal state.
403 * use the RAND challenge to produce the SRES result, and then
404 * use that to generate a new MAC.
406 * for the moment, we ignore the RANDs, then just plug in the SRES
410 static int process_eap_challenge(RADIUS_PACKET *req,
414 VALUE_PAIR *mac, *randvp;
415 VALUE_PAIR *sres1,*sres2,*sres3;
416 VALUE_PAIR *Kc1, *Kc2, *Kc3;
419 /* look for the AT_MAC and the challenge data */
420 mac = pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC);
421 randvp= pairfind(req->vps, ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_RAND);
422 if(mac == NULL || rand == NULL) {
423 fprintf(stderr, "radeapclient: challenge message needs to contain RAND and MAC\n");
428 * compare RAND with randX, to verify this is the right response
432 VALUE_PAIR *randcfgvp[3];
433 unsigned char *randcfg[3];
435 randcfg[0] = &randvp->vp_strvalue[2];
436 randcfg[1] = &randvp->vp_strvalue[2+EAPSIM_RAND_SIZE];
437 randcfg[2] = &randvp->vp_strvalue[2+EAPSIM_RAND_SIZE*2];
439 randcfgvp[0] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND1);
440 randcfgvp[1] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND2);
441 randcfgvp[2] = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_RAND3);
443 if(randcfgvp[0] == NULL ||
444 randcfgvp[1] == NULL ||
445 randcfgvp[2] == NULL) {
446 fprintf(stderr, "radeapclient: needs to have rand1, 2 and 3 set.\n");
450 if(memcmp(randcfg[0], randcfgvp[0]->vp_strvalue, EAPSIM_RAND_SIZE)!=0 ||
451 memcmp(randcfg[1], randcfgvp[1]->vp_strvalue, EAPSIM_RAND_SIZE)!=0 ||
452 memcmp(randcfg[2], randcfgvp[2]->vp_strvalue, EAPSIM_RAND_SIZE)!=0) {
455 fprintf(stderr, "radeapclient: one of rand 1,2,3 didn't match\n");
456 for(rnum = 0; rnum < 3; rnum++) {
457 fprintf(stderr, "received rand %d: ", rnum);
459 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
466 fprintf(stderr, "%02x", randcfg[rnum][i]);
468 fprintf(stderr, "\nconfigured rand %d: ", rnum);
470 for (i = 0; i < EAPSIM_RAND_SIZE; i++) {
477 fprintf(stderr, "%02x", randcfgvp[rnum]->vp_strvalue[i]);
479 fprintf(stderr, "\n");
486 * now dig up the sres values from the response packet,
487 * which were put there when we read things in.
489 * Really, they should be calculated from the RAND!
492 sres1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES1);
493 sres2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES2);
494 sres3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_SRES3);
499 fprintf(stderr, "radeapclient: needs to have sres1, 2 and 3 set.\n");
502 memcpy(eapsim_mk.sres[0], sres1->vp_strvalue, sizeof(eapsim_mk.sres[0]));
503 memcpy(eapsim_mk.sres[1], sres2->vp_strvalue, sizeof(eapsim_mk.sres[1]));
504 memcpy(eapsim_mk.sres[2], sres3->vp_strvalue, sizeof(eapsim_mk.sres[2]));
506 Kc1 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC1);
507 Kc2 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC2);
508 Kc3 = pairfind(rep->vps, ATTRIBUTE_EAP_SIM_KC3);
513 fprintf(stderr, "radeapclient: needs to have Kc1, 2 and 3 set.\n");
516 memcpy(eapsim_mk.Kc[0], Kc1->vp_strvalue, sizeof(eapsim_mk.Kc[0]));
517 memcpy(eapsim_mk.Kc[1], Kc2->vp_strvalue, sizeof(eapsim_mk.Kc[1]));
518 memcpy(eapsim_mk.Kc[2], Kc3->vp_strvalue, sizeof(eapsim_mk.Kc[2]));
520 /* all set, calculate keys */
521 eapsim_calculate_keys(&eapsim_mk);
524 eapsim_dump_mk(&eapsim_mk);
527 /* verify the MAC, now that we have all the keys. */
528 if(eapsim_checkmac(req->vps, eapsim_mk.K_aut,
529 eapsim_mk.nonce_mt, sizeof(eapsim_mk.nonce_mt),
531 printf("MAC check succeed\n");
535 printf("calculated MAC (");
536 for (i = 0; i < 20; i++) {
543 printf("%02x", calcmac[i]);
545 printf(" did not match\n");
549 /* form new response clear of any EAP stuff */
552 /* mark the subtype as being EAP-SIM/Response/Start */
553 newvp = paircreate(ATTRIBUTE_EAP_SIM_SUBTYPE, PW_TYPE_INTEGER);
554 newvp->lvalue = eapsim_challenge;
555 pairreplace(&(rep->vps), newvp);
558 * fill the SIM_MAC with a field that will in fact get appended
559 * to the packet before the MAC is calculated
561 newvp = paircreate(ATTRIBUTE_EAP_SIM_BASE+PW_EAP_SIM_MAC,
563 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*0, sres1->vp_strvalue, EAPSIM_SRES_SIZE);
564 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*1, sres2->vp_strvalue, EAPSIM_SRES_SIZE);
565 memcpy(newvp->vp_strvalue+EAPSIM_SRES_SIZE*2, sres3->vp_strvalue, EAPSIM_SRES_SIZE);
566 newvp->length = EAPSIM_SRES_SIZE*3;
567 pairreplace(&(rep->vps), newvp);
569 newvp = paircreate(ATTRIBUTE_EAP_SIM_KEY, PW_TYPE_OCTETS);
570 memcpy(newvp->vp_strvalue, eapsim_mk.K_aut, EAPSIM_AUTH_SIZE);
571 newvp->length = EAPSIM_AUTH_SIZE;
572 pairreplace(&(rep->vps), newvp);
578 * this code runs the EAP-SIM client state machine.
579 * the *request* is from the server.
580 * the *reponse* is to the server.
583 static int respond_eap_sim(RADIUS_PACKET *req,
586 enum eapsim_clientstates state, newstate;
587 enum eapsim_subtype subtype;
588 VALUE_PAIR *vp, *statevp, *radstate, *eapid;
589 char statenamebuf[32], subtypenamebuf[32];
591 if ((radstate = paircopy2(req->vps, PW_STATE)) == NULL)
596 if ((eapid = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
601 /* first, dig up the state from the request packet, setting
602 * outselves to be in EAP-SIM-Start state if there is none.
605 if((statevp = pairfind(resp->vps, ATTRIBUTE_EAP_SIM_STATE)) == NULL)
607 /* must be initial request */
608 statevp = paircreate(ATTRIBUTE_EAP_SIM_STATE, PW_TYPE_INTEGER);
609 statevp->lvalue = eapsim_client_init;
610 pairreplace(&(resp->vps), statevp);
612 state = statevp->lvalue;
615 * map the attributes, and authenticate them.
617 unmap_eapsim_types(req);
619 printf("<+++ EAP-sim decoded packet:\n");
620 vp_printlist(stdout, req->vps);
622 if((vp = pairfind(req->vps, ATTRIBUTE_EAP_SIM_SUBTYPE)) == NULL)
626 subtype = vp->lvalue;
629 * look for the appropriate state, and process incoming message
632 case eapsim_client_init:
635 newstate = process_eap_start(req, resp);
638 case eapsim_challenge:
639 case eapsim_notification:
642 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
643 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
644 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
645 /* invalid state, drop message */
650 case eapsim_client_start:
653 /* NOT SURE ABOUT THIS ONE, retransmit, I guess */
654 newstate = process_eap_start(req, resp);
657 case eapsim_challenge:
658 newstate = process_eap_challenge(req, resp);
662 fprintf(stderr, "radeapclient: sim in state %s message %s is illegal. Reply dropped.\n",
663 sim_state2name(state, statenamebuf, sizeof(statenamebuf)),
664 sim_subtype2name(subtype, subtypenamebuf, sizeof(subtypenamebuf)));
665 /* invalid state, drop message */
672 fprintf(stderr, "radeapclient: sim in illegal state %s\n",
673 sim_state2name(state, statenamebuf, sizeof(statenamebuf)));
677 /* copy the eap state object in */
678 pairreplace(&(resp->vps), eapid);
680 /* update stete info, and send new packet */
681 map_eapsim_types(resp);
683 /* copy the radius state object in */
684 pairreplace(&(resp->vps), radstate);
686 statevp->lvalue = newstate;
690 static int respond_eap_md5(RADIUS_PACKET *req,
693 VALUE_PAIR *vp, *id, *state;
694 int valuesize, namesize;
695 unsigned char identifier;
696 unsigned char *value;
703 if ((state = paircopy2(req->vps, PW_STATE)) == NULL)
705 fprintf(stderr, "radeapclient: no state attribute found\n");
709 if ((id = paircopy2(req->vps, ATTRIBUTE_EAP_ID)) == NULL)
711 fprintf(stderr, "radeapclient: no EAP-ID attribute found\n");
714 identifier = id->lvalue;
716 if ((vp = pairfind(req->vps, ATTRIBUTE_EAP_BASE+PW_EAP_MD5)) == NULL)
718 fprintf(stderr, "radeapclient: no EAP-MD5 attribute found\n");
722 /* got the details of the MD5 challenge */
723 valuesize = vp->vp_octets[0];
724 value = &vp->vp_octets[1];
725 name = &vp->vp_octets[valuesize+1];
726 namesize = vp->length - (valuesize + 1);
729 if(valuesize > vp->length)
731 fprintf(stderr, "radeapclient: md5 valuesize if too big (%d > %d)\n",
732 valuesize, vp->length);
736 /* now do the CHAP operation ourself, rather than build the
737 * buffer. We could also call rad_chap_encode, but it wants
738 * a CHAP-Challenge, which we don't want to bother with.
740 lrad_MD5Init(&context);
741 lrad_MD5Update(&context, &identifier, 1);
742 lrad_MD5Update(&context, password, strlen(password));
743 lrad_MD5Update(&context, value, valuesize);
744 lrad_MD5Final(response, &context);
746 vp = paircreate(ATTRIBUTE_EAP_BASE+PW_EAP_MD5, PW_TYPE_OCTETS);
748 memcpy(&vp->vp_strvalue[1], response, 16);
751 pairreplace(&(rep->vps), vp);
753 pairreplace(&(rep->vps), id);
755 /* copy the state object in */
756 pairreplace(&(rep->vps), state);
763 static int sendrecv_eap(RADIUS_PACKET *rep)
765 RADIUS_PACKET *req = NULL;
766 VALUE_PAIR *vp, *vpnext;
767 int tried_eap_md5 = 0;
770 * Keep a copy of the the User-Password attribute.
772 if ((vp = pairfind(rep->vps, ATTRIBUTE_EAP_MD5_PASSWORD)) != NULL) {
773 strNcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
775 } else if ((vp = pairfind(rep->vps, PW_PASSWORD)) != NULL) {
776 strNcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
778 * Otherwise keep a copy of the CHAP-Password attribute.
780 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
781 strNcpy(password, (char *)vp->vp_strvalue, sizeof(vp->vp_strvalue));
789 printf("\n+++> About to send encoded packet:\n");
790 vp_printlist(stdout, rep->vps);
793 * if there are EAP types, encode them into an EAP-Message
799 * Fix up Digest-Attributes issues
801 for (vp = rep->vps; vp != NULL; vp = vp->next) {
802 switch (vp->attribute) {
806 case PW_DIGEST_REALM:
807 case PW_DIGEST_NONCE:
808 case PW_DIGEST_METHOD:
811 case PW_DIGEST_ALGORITHM:
812 case PW_DIGEST_BODY_DIGEST:
813 case PW_DIGEST_CNONCE:
814 case PW_DIGEST_NONCE_COUNT:
815 case PW_DIGEST_USER_NAME:
817 memmove(&vp->vp_strvalue[2], &vp->vp_octets[0], vp->length);
818 vp->vp_octets[0] = vp->attribute - PW_DIGEST_REALM + 1;
820 vp->vp_octets[1] = vp->length;
821 vp->attribute = PW_DIGEST_ATTRIBUTES;
827 * If we've already sent a packet, free up the old
828 * one, and ensure that the next packet has a unique
829 * ID and authentication vector.
836 librad_md5_calc(rep->vector, rep->vector,
837 sizeof(rep->vector));
839 if (*password != '\0') {
840 if ((vp = pairfind(rep->vps, PW_PASSWORD)) != NULL) {
841 strNcpy((char *)vp->vp_strvalue, password, strlen(password) + 1);
842 vp->length = strlen(password);
844 } else if ((vp = pairfind(rep->vps, PW_CHAP_PASSWORD)) != NULL) {
845 strNcpy((char *)vp->vp_strvalue, password, strlen(password) + 1);
846 vp->length = strlen(password);
848 rad_chap_encode(rep, (char *) vp->vp_strvalue, rep->id, vp);
851 } /* there WAS a password */
853 /* send the response, wait for the next request */
854 send_packet(rep, &req);
856 /* okay got back the packet, go and decode the EAP-Message. */
857 unmap_eap_types(req);
859 printf("<+++ EAP decoded packet:\n");
860 vp_printlist(stdout, req->vps);
862 /* now look for the code type. */
863 for (vp = req->vps; vp != NULL; vp = vpnext) {
866 switch (vp->attribute) {
870 case ATTRIBUTE_EAP_BASE+PW_EAP_MD5:
871 if(respond_eap_md5(req, rep) && tried_eap_md5 < 3)
878 case ATTRIBUTE_EAP_BASE+PW_EAP_SIM:
879 if(respond_eap_sim(req, rep))
891 int main(int argc, char **argv)
897 char *filename = NULL;
902 id = ((int)getpid() & 0xff);
905 radlog_dest = RADLOG_STDERR;
907 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xXv")) != EOF)
911 if (!isdigit((int) *optarg))
913 count = atoi(optarg);
930 sha1_data_problems = 1;
936 if (!isdigit((int) *optarg))
938 retries = atoi(optarg);
941 if (!isdigit((int) *optarg))
944 if ((id < 0) || (id > 255)) {
952 if (!isdigit((int) *optarg))
954 timeout = atof(optarg);
957 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
961 fp = fopen(optarg, "r");
963 fprintf(stderr, "radclient: Error opening %s: %s\n",
964 optarg, strerror(errno));
967 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
968 fprintf(stderr, "radclient: Error reading %s: %s\n",
969 optarg, strerror(errno));
974 /* truncate newline */
975 p = filesecret + strlen(filesecret) - 1;
976 while ((p >= filesecret) &&
982 if (strlen(filesecret) < 2) {
983 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
994 argc -= (optind - 1);
995 argv += (optind - 1);
998 ((secret == NULL) && (argc < 4))) {
1002 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
1003 librad_perror("radclient");
1007 if ((req = rad_alloc(1)) == NULL) {
1008 librad_perror("radclient");
1016 if((randinit = fopen("/dev/urandom", "r")) == NULL)
1018 perror("/dev/urandom");
1020 fread(randctx.randrsl, 256, 1, randinit);
1024 lrad_randinit(&randctx, 1);
1030 * Strip port from hostname if needed.
1032 if ((p = strchr(argv[1], ':')) != NULL) {
1038 * See what kind of request we want to send.
1040 if (strcmp(argv[2], "auth") == 0) {
1041 if (port == 0) port = getport("radius");
1042 if (port == 0) port = PW_AUTH_UDP_PORT;
1043 req->code = PW_AUTHENTICATION_REQUEST;
1045 } else if (strcmp(argv[2], "acct") == 0) {
1046 if (port == 0) port = getport("radacct");
1047 if (port == 0) port = PW_ACCT_UDP_PORT;
1048 req->code = PW_ACCOUNTING_REQUEST;
1051 } else if (strcmp(argv[2], "status") == 0) {
1052 if (port == 0) port = getport("radius");
1053 if (port == 0) port = PW_AUTH_UDP_PORT;
1054 req->code = PW_STATUS_SERVER;
1056 } else if (strcmp(argv[2], "disconnect") == 0) {
1057 if (port == 0) port = PW_POD_UDP_PORT;
1058 req->code = PW_DISCONNECT_REQUEST;
1060 } else if (isdigit((int) argv[2][0])) {
1061 if (port == 0) port = getport("radius");
1062 if (port == 0) port = PW_AUTH_UDP_PORT;
1063 req->code = atoi(argv[2]);
1069 * Ensure that the configuration is initialized.
1071 memset(&mainconfig, 0, sizeof(mainconfig));
1076 req->dst_port = port;
1077 if (ip_hton(argv[1], AF_INET, &req->dst_ipaddr) < 0) {
1078 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
1085 if (argv[3]) secret = argv[3];
1089 * Maybe read them, from stdin, if there's no
1090 * filename, or if the filename is '-'.
1092 if (filename && (strcmp(filename, "-") != 0)) {
1093 fp = fopen(filename, "r");
1095 fprintf(stderr, "radclient: Error opening %s: %s\n",
1096 filename, strerror(errno));
1106 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1107 perror("radclient: socket: ");
1112 if(req->vps) pairfree(&req->vps);
1114 if ((req->vps = readvp2(fp, &filedone, "radeapclient:"))
1123 printf("\n\t Total approved auths: %d\n", totalapp);
1124 printf("\t Total denied auths: %d\n", totaldeny);