2 * radclient.c General 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$";
27 #include "libradius.h"
39 #include <sys/socket.h>
42 # include <netinet/in.h>
46 # include <sys/select.h>
57 static int retries = 10;
58 static float timeout = 3;
59 static const char *secret = NULL;
60 static int do_output = 1;
61 static int do_summary = 0;
62 static int filedone = 0;
63 static int totalapp = 0;
64 static int totaldeny = 0;
65 static char filesecret[256];
68 * Read valuepairs from the fp up to End-Of-File.
70 static VALUE_PAIR *readvp(FILE *fp)
73 LRAD_TOKEN last_token;
81 while (!error && fgets(buf, sizeof(buf), fp) != NULL) {
85 /* If we get a '\n' by itself, we assume that's the end of that VP */
86 if((buf[0] == '\n') && (list)) {
87 return error ? NULL: list;
89 if((buf[0] == '\n') && (!list)) {
93 if ((vp = pairread(&p, &last_token)) == NULL) {
94 librad_perror("radclient:");
99 } while (last_token == T_COMMA);
103 return error ? NULL: list;
106 static void usage(void)
108 fprintf(stderr, "Usage: radclient [-c count] [-d raddb] [-f file] [-r retries] [-t timeout]\n"
109 "[-i id] [-qvxS] server[:port] acct|auth [<secret>]\n");
111 fprintf(stderr, " -c count Send each packet 'count' times.\n");
112 fprintf(stderr, " -d raddb Set dictionary directory.\n");
113 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
114 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retires' times.\n");
115 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying.\n");
116 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
117 fprintf(stderr, " -S file read secret from file, not command line.\n");
118 fprintf(stderr, " -q Do not print anything out.\n");
119 fprintf(stderr, " -s Print out summary information of auth results.\n");
120 fprintf(stderr, " -v Show program version information.\n");
121 fprintf(stderr, " -x Debugging mode.\n");
126 static int getport(const char *name)
130 svp = getservbyname (name, "udp");
135 return ntohs(svp->s_port);
138 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
143 for (i = 0; i < retries; i++) {
146 rad_send(req, NULL, secret);
148 /* And wait for reply, timing out as necessary */
150 FD_SET(req->sockfd, &rdfdesc);
152 tv.tv_sec = (int)timeout;
153 tv.tv_usec = 1000000 * (timeout - (int)timeout);
155 /* Something's wrong if we don't get exactly one fd. */
156 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
160 *rep = rad_recv(req->sockfd);
163 } else { /* NULL: couldn't receive the packet */
164 librad_perror("radclient:");
169 /* No response or no data read (?) */
171 fprintf(stderr, "radclient: no response from server\n");
175 if (rad_decode(*rep, req, secret) != 0) {
176 librad_perror("rad_decode");
180 /* libradius debug already prints out the value pairs for us */
181 if (!librad_debug && do_output) {
182 printf("Received response ID %d, code %d, length = %d\n",
183 (*rep)->id, (*rep)->code, (*rep)->data_len);
184 vp_printlist(stdout, (*rep)->vps);
186 if((*rep)->code == PW_AUTHENTICATION_ACK) {
195 int main(int argc, char **argv)
198 RADIUS_PACKET *rep = NULL;
202 const char *radius_dir = RADDBDIR;
203 char *filename = NULL;
211 id = ((int)getpid() & 0xff);
214 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:S:xv")) != EOF) switch(c) {
216 if (!isdigit(*optarg))
218 count = atoi(optarg);
233 if (!isdigit(*optarg))
235 retries = atoi(optarg);
238 if (!isdigit(*optarg))
241 if ((id < 0) || (id > 255)) {
249 if (!isdigit(*optarg))
251 timeout = atof(optarg);
254 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
258 fp = fopen(optarg, "r");
260 fprintf(stderr, "radclient: Error opening %s: %s\n",
261 optarg, strerror(errno));
264 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
265 fprintf(stderr, "radclient: Error reading %s: %s\n",
266 optarg, strerror(errno));
271 /* truncate newline */
272 p = filesecret + strlen(filesecret) - 1;
273 while ((p >= filesecret) &&
279 if (strlen(filesecret) < 2) {
280 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
290 argc -= (optind - 1);
291 argv += (optind - 1);
294 ((secret == NULL) && (argc < 4))) {
298 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
299 librad_perror("radclient");
303 if ((req = rad_alloc(1)) == NULL) {
304 librad_perror("radclient");
311 * Strip port from hostname if needed.
313 if ((p = strchr(argv[1], ':')) != NULL) {
319 * See what kind of request we want to send.
321 if (strcmp(argv[2], "auth") == 0) {
322 if (port == 0) port = getport("radius");
323 if (port == 0) port = PW_AUTH_UDP_PORT;
324 req->code = PW_AUTHENTICATION_REQUEST;
325 } else if (strcmp(argv[2], "acct") == 0) {
326 if (port == 0) port = getport("radacct");
327 if (port == 0) port = PW_ACCT_UDP_PORT;
328 req->code = PW_ACCOUNTING_REQUEST;
330 } else if (isdigit(argv[2][0])) {
331 if (port == 0) port = getport("radius");
332 if (port == 0) port = PW_AUTH_UDP_PORT;
333 req->code = atoi(argv[2]);
341 req->dst_port = port;
342 req->dst_ipaddr = ip_getaddr(argv[1]);
343 if (req->dst_ipaddr == INADDR_NONE) {
344 librad_perror("radclient: %s: ", argv[1]);
351 if (argv[3]) secret = argv[3];
355 * Maybe read them, from stdin, if there's no
356 * filename, or if the filename is '-'.
358 if (filename && (strcmp(filename, "-") != 0)) {
359 fp = fopen(filename, "r");
361 fprintf(stderr, "radclient: Error opening %s: %s\n",
362 filename, strerror(errno));
372 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
373 perror("radclient: socket: ");
378 if(req->vps) pairfree(&req->vps);
380 if ((req->vps = readvp(fp)) == NULL) {
386 * Keep a copy of the the User-Password attribute.
388 if ((vp = pairfind(req->vps, PW_PASSWORD)) != NULL) {
389 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
391 * Otherwise keep a copy of the CHAP-Password attribute.
393 } else if ((vp = pairfind(req->vps, PW_CHAP_PASSWORD)) != NULL) {
394 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
400 * Fix up Digest-Attributes issues
402 for (vp = req->vps; vp != NULL; vp = vp->next) {
403 switch (vp->attribute) {
407 case PW_DIGEST_REALM:
408 case PW_DIGEST_NONCE:
409 case PW_DIGEST_METHOD:
412 case PW_DIGEST_ALGORITHM:
413 case PW_DIGEST_BODY_DIGEST:
414 case PW_DIGEST_CNONCE:
415 case PW_DIGEST_NONCE_COUNT:
416 case PW_DIGEST_USER_NAME:
418 memmove(&vp->strvalue[2], &vp->strvalue[0], vp->length);
419 vp->strvalue[0] = vp->attribute - PW_DIGEST_REALM + 1;
421 vp->strvalue[1] = vp->length;
422 vp->attribute = PW_DIGEST_ATTRIBUTES;
428 * Loop, sending the packet N times.
430 for (loop = 0; loop < count; loop++) {
434 * If we've already sent a packet, free up the old
435 * one, and ensure that the next packet has a unique
436 * ID and authentication vector.
443 librad_md5_calc(req->vector, req->vector,
444 sizeof(req->vector));
446 if (*password != '\0') {
447 vp = pairfind(req->vps, PW_PASSWORD);
449 strNcpy((char *)vp->strvalue, password, vp->length + 1);
450 vp->length = strlen(password);
452 rad_pwencode((char *)vp->strvalue,
454 secret, (char *)req->vector);
455 } else if ((vp = pairfind(req->vps, PW_CHAP_PASSWORD)) != NULL) {
456 strNcpy((char *)vp->strvalue, password, vp->length + 1);
457 vp->length = strlen(password);
459 rad_chap_encode(req, (char *) vp->strvalue, req->id, vp);
462 } /* there WAS a password */
463 send_packet(req, &rep);
468 printf("\n\t Total approved auths: %d\n", totalapp);
469 printf("\t Total denied auths: %d\n", totaldeny);