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 = "secret";
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;
67 * Read valuepairs from the fp up to End-Of-File.
69 static VALUE_PAIR *readvp(FILE *fp)
80 while (!error && fgets(buf, sizeof(buf), fp) != NULL) {
84 /* If we get a '\n' by itself, we assume that's the end of that VP */
85 if((buf[0] == '\n') && (list)) {
86 return error ? NULL: list;
88 if((buf[0] == '\n') && (!list)) {
92 if ((vp = pairread(&p, &last_token)) == NULL) {
93 librad_perror("radclient:");
98 } while (last_token == T_COMMA);
102 return error ? NULL: list;
105 static void usage(void)
107 fprintf(stderr, "Usage: radclient [-c count] [-d raddb] [-f file] [-r retries] [-t timeout]\n"
108 "[-i id] [-qvx] server acct|auth <secret>\n");
110 fprintf(stderr, " -c count Send each packet 'count' times.\n");
111 fprintf(stderr, " -d raddb Set dictionary directory.\n");
112 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
113 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retires' times.\n");
114 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying.\n");
115 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
116 fprintf(stderr, " -q Do not print anything out.\n");
117 fprintf(stderr, " -s Print out summary information of auth results.\n");
118 fprintf(stderr, " -v Show program version information.\n");
119 fprintf(stderr, " -x Debugging mode.\n");
124 static int getport(const char *name)
128 svp = getservbyname (name, "udp");
133 return ntohs(svp->s_port);
136 static int send_packet(RADIUS_PACKET *req, RADIUS_PACKET **rep)
141 for (i = 0; i < retries; i++) {
144 rad_send(req, secret);
146 /* And wait for reply, timing out as necessary */
148 FD_SET(req->sockfd, &rdfdesc);
150 tv.tv_sec = (int)timeout;
151 tv.tv_usec = 1000000 * (timeout - (int)timeout);
153 /* Something's wrong if we don't get exactly one fd. */
154 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
158 *rep = rad_recv(req->sockfd);
161 } else { /* NULL: couldn't receive the packet */
162 librad_perror("radclient:");
167 /* No response or no data read (?) */
169 fprintf(stderr, "radclient: no response from server\n");
173 if (rad_decode(*rep, req, secret) != 0) {
174 librad_perror("rad_decode");
178 /* libradius debug already prints out the value pairs for us */
179 if (!librad_debug && do_output) {
180 printf("Received response ID %d, code %d, length = %d\n",
181 (*rep)->id, (*rep)->code, (*rep)->data_len);
182 vp_printlist(stdout, (*rep)->vps);
184 if((*rep)->code == PW_AUTHENTICATION_ACK) {
193 int main(int argc, char **argv)
196 RADIUS_PACKET *rep = NULL;
200 const char *radius_dir = RADDBDIR;
201 char *filename = NULL;
209 id = ((int)getpid() & 0xff);
211 while ((c = getopt(argc, argv, "c:d:f:hi:qst:r:xv")) != EOF) switch(c) {
213 if (!isdigit(*optarg))
215 count = atoi(optarg);
230 if (!isdigit(*optarg))
232 retries = atoi(optarg);
235 if (!isdigit(*optarg))
238 if ((id < 0) || (id > 255)) {
246 if (!isdigit(*optarg))
248 timeout = atof(optarg);
251 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
259 argc -= (optind - 1);
260 argv += (optind - 1);
266 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
267 librad_perror("radclient");
271 if ((req = rad_alloc(1)) == NULL) {
272 librad_perror("radclient");
279 * Strip port from hostname if needed.
281 if ((p = strchr(argv[1], ':')) != NULL) {
287 * See what kind of request we want to send.
289 if (strcmp(argv[2], "auth") == 0) {
290 if (port == 0) port = getport("radius");
291 if (port == 0) port = PW_AUTH_UDP_PORT;
292 req->code = PW_AUTHENTICATION_REQUEST;
293 } else if (strcmp(argv[2], "acct") == 0) {
294 if (port == 0) port = getport("radacct");
295 if (port == 0) port = PW_ACCT_UDP_PORT;
296 req->code = PW_ACCOUNTING_REQUEST;
298 } else if (isdigit(argv[2][0])) {
299 if (port == 0) port = getport("radius");
300 if (port == 0) port = PW_AUTH_UDP_PORT;
301 req->code = atoi(argv[2]);
309 req->dst_port = port;
310 req->dst_ipaddr = ip_getaddr(argv[1]);
311 if (req->dst_ipaddr == INADDR_NONE) {
312 librad_perror("radclient: %s: ", argv[1]);
319 if (argv[3]) secret = argv[3];
323 * Maybe read them, from stdin, if there's no
324 * filename, or if the filename is '-'.
326 if (filename && (strcmp(filename, "-") != 0)) {
327 fp = fopen(filename, "r");
329 fprintf(stderr, "radclient: Error opening %s: %s\n",
330 filename, strerror(errno));
340 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
341 perror("radclient: socket: ");
346 if(req->vps) pairfree(&req->vps);
348 if ((req->vps = readvp(fp)) == NULL) {
354 * Encrypt the Password attribute.
356 if ((vp = pairfind(req->vps, PW_PASSWORD)) != NULL) {
357 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
359 rad_pwencode((char *)vp->strvalue,
361 secret, (char *)req->vector);
364 * Not there, encrypt the CHAP-Password attribute.
366 } else if ((vp = pairfind(req->vps, PW_CHAP_PASSWORD)) != NULL) {
367 strNcpy(password, (char *)vp->strvalue, sizeof(vp->strvalue));
368 rad_chap_encode(req, (char *) vp->strvalue, req->id, vp);
376 * Loop, sending the packet N times.
378 for (loop = 0; loop < count; loop++) {
382 * If we've already sent a packet, free up the old
383 * one, and ensure that the next packet has a unique
384 * ID and authentication vector.
390 if (*password != '\0') {
391 vp = pairfind(req->vps, PW_PASSWORD);
393 strNcpy((char *)vp->strvalue, password,
395 vp->length = strlen(password);
399 librad_md5_calc(req->vector, req->vector,
400 sizeof(req->vector));
402 send_packet(req, &rep);
407 printf("\n\t Total approved auths: %d\n", totalapp);
408 printf("\t Total denied auths: %d\n", totaldeny);