import from HEAD:
[freeradius.git] / src / main / radclient.c
1 /*
2  * radclient.c  General radius packet debug tool.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2000  The FreeRADIUS server project
21  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  */
24 static const char rcsid[] = "$Id$";
25
26 #include "autoconf.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #ifdef HAVE_UNISTD_H
32 #       include <unistd.h>
33 #endif
34
35 #include <string.h>
36 #include <ctype.h>
37 #include <netdb.h>
38 #include <sys/socket.h>
39
40 #ifdef HAVE_NETINET_IN_H
41 #       include <netinet/in.h>
42 #endif
43
44 #ifdef HAVE_SYS_SELECT_H
45 #       include <sys/select.h>
46 #endif
47
48 #ifdef HAVE_GETOPT_H
49 #       include <getopt.h>
50 #endif
51
52 #include <assert.h>
53
54 #include "conf.h"
55 #include "radpaths.h"
56 #include "missing.h"
57 #include "libradius.h"
58
59 static int retries = 10;
60 static float timeout = 3;
61 static const char *secret = NULL;
62 static int do_output = 1;
63 static int totalapp = 0;
64 static int totaldeny = 0;
65 static int totallost = 0;
66
67 static int server_port = 0;
68 static int packet_code = 0;
69 static uint32_t server_ipaddr = 0;
70 static int resend_count = 1;
71 static int done = 1;
72
73 static int sockfd;
74 static int radius_id[256];
75 static int last_used_id = -1;
76
77 static rbtree_t *filename_tree = NULL;
78 static rbtree_t *request_tree = NULL;
79
80 static int sleep_time = -1;
81
82 typedef struct radclient_t {
83         struct          radclient_t *prev;
84         struct          radclient_t *next;
85
86         const char      *filename;
87         int             packet_number; /* in the file */
88         char            password[256];
89         time_t          timestamp;
90         RADIUS_PACKET   *request;
91         RADIUS_PACKET   *reply;
92         int             resend;
93         int             tries;
94         int             done;
95 } radclient_t;
96
97 static radclient_t *radclient_head = NULL;
98 static radclient_t *radclient_tail = NULL;
99
100
101 static void NEVER_RETURNS usage(void)
102 {
103         fprintf(stderr, "Usage: radclient [options] server[:port] <command> [<secret>]\n");
104
105         fprintf(stderr, "  <command>    One of auth, acct, status, coa, or disconnect.\n");
106         fprintf(stderr, "  -c count    Send each packet 'count' times.\n");
107         fprintf(stderr, "  -d raddb    Set dictionary directory.\n");
108         fprintf(stderr, "  -f file     Read packets from file, not stdin.\n");
109         fprintf(stderr, "  -i id       Set request id to 'id'.  Values may be 0..255\n");
110         fprintf(stderr, "  -n num      Send N requests/s\n");
111         fprintf(stderr, "  -p num      Send 'num' packets from a file in parallel.\n");
112         fprintf(stderr, "  -q          Do not print anything out.\n");
113         fprintf(stderr, "  -r retries  If timeout, retry sending the packet 'retries' times.\n");
114         fprintf(stderr, "  -s          Print out summary information of auth results.\n");
115         fprintf(stderr, "  -S file     read secret from file, not command line.\n");
116         fprintf(stderr, "  -t timeout  Wait 'timeout' seconds before retrying (may be a floating point number).\n");
117         fprintf(stderr, "  -v          Show program version information.\n");
118         fprintf(stderr, "  -x          Debugging mode.\n");
119
120         exit(1);
121 }
122
123 /*
124  *      Free a radclient struct, which may (or may not)
125  *      already be in the list.
126  */
127 static void radclient_free(radclient_t *radclient)
128 {
129         radclient_t *prev, *next;
130
131         if (radclient->request) rad_free(&radclient->request);
132         if (radclient->reply) rad_free(&radclient->reply);
133
134         prev = radclient->prev;
135         next = radclient->next;
136
137         if (prev) {
138                 assert(radclient_head != radclient);
139                 prev->next = next;
140         } else if (radclient_head) {
141                 assert(radclient_head == radclient);
142                 radclient_head = next;
143         }
144
145         if (next) {
146                 assert(radclient_tail != radclient);
147                 next->prev = prev;
148         } else if (radclient_tail) {
149                 assert(radclient_tail == radclient);
150                 radclient_tail = prev;
151         }
152
153         free(radclient);
154 }
155
156 /*
157  *      Initialize a radclient data structure
158  */
159 static radclient_t *radclient_init(const char *filename)
160 {
161         FILE *fp;
162         VALUE_PAIR *vp;
163         radclient_t *start, *radclient, *prev = NULL;
164         int filedone = 0;
165         int packet_number = 1;
166
167         start = NULL;
168         assert(filename != NULL);
169
170         /*
171          *      Determine where to read the VP's from.
172          */
173         if (strcmp(filename, "-") != 0) {
174                 fp = fopen(filename, "r");
175                 if (!fp) {
176                         fprintf(stderr, "radclient: Error opening %s: %s\n",
177                                 filename, strerror(errno));
178                         return NULL;
179                 }
180         } else {
181                 fp = stdin;
182         }
183
184         /*
185          *      Loop until the file is done.
186          */
187         do {
188                 /*
189                  *      Allocate it.
190                  */
191                 radclient = malloc(sizeof(*radclient));
192                 if (!radclient) {
193                         perror("radclient: X");
194                         if (fp != stdin) fclose(fp);
195                         return NULL; /* memory leak "start" */
196                 }
197                 memset(radclient, 0, sizeof(*radclient));
198
199                 radclient->request = rad_alloc(1);
200                 if (!radclient->request) {
201                         librad_perror("radclient: X");
202                         radclient_free(radclient);
203                         if (fp != stdin) fclose(fp);
204                         return NULL; /* memory leak "start" */
205                 }
206
207                 radclient->filename = filename;
208                 radclient->request->id = -1; /* allocate when sending */
209                 radclient->packet_number = packet_number++;
210
211                 /*
212                  *      Read the VP's.
213                  */
214                 radclient->request->vps = readvp2(fp, &filedone, "radclient: X");
215                 if (!radclient->request->vps) {
216                         radclient_free(radclient);
217                         if (fp != stdin) fclose(fp);
218                         return start; /* done: return the list */
219                 }
220
221                 /*
222                  *      Keep a copy of the the User-Password attribute.
223                  */
224                 if ((vp = pairfind(radclient->request->vps, PW_PASSWORD)) != NULL) {
225                         strNcpy(radclient->password, (char *)vp->strvalue, sizeof(radclient->password));
226                         /*
227                          *      Otherwise keep a copy of the CHAP-Password attribute.
228                          */
229                 } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD)) != NULL) {
230                         strNcpy(radclient->password, (char *)vp->strvalue, sizeof(radclient->password));
231                 } else {
232                         radclient->password[0] = '\0';
233                 }
234
235                 /*
236                  *  Fix up Digest-Attributes issues
237                  */
238                 for (vp = radclient->request->vps; vp != NULL; vp = vp->next) {
239                         switch (vp->attribute) {
240                         default:
241                                 break;
242
243                                 /*
244                                  *      Allow it to set the packet type in
245                                  *      the attributes read from the file.
246                                  */
247                         case PW_PACKET_TYPE:
248                                 radclient->request->code = vp->lvalue;
249                                 break;
250
251                         case PW_PACKET_DST_PORT:
252                                 radclient->request->dst_port = (vp->lvalue & 0xffff);
253                                 break;
254
255                         case PW_DIGEST_REALM:
256                         case PW_DIGEST_NONCE:
257                         case PW_DIGEST_METHOD:
258                         case PW_DIGEST_URI:
259                         case PW_DIGEST_QOP:
260                         case PW_DIGEST_ALGORITHM:
261                         case PW_DIGEST_BODY_DIGEST:
262                         case PW_DIGEST_CNONCE:
263                         case PW_DIGEST_NONCE_COUNT:
264                         case PW_DIGEST_USER_NAME:
265                                 /* overlapping! */
266                                 memmove(&vp->strvalue[2], &vp->strvalue[0], vp->length);
267                                 vp->strvalue[0] = vp->attribute - PW_DIGEST_REALM + 1;
268                                 vp->length += 2;
269                                 vp->strvalue[1] = vp->length;
270                                 vp->attribute = PW_DIGEST_ATTRIBUTES;
271                                 break;
272                         }
273                 } /* loop over the VP's we read in */
274
275                 if (!start) {
276                         start = radclient;
277                         prev = start;
278                 } else {
279                         prev->next = radclient;
280                         radclient->prev = prev;
281                         prev = radclient;
282                 }
283         } while (!filedone); /* loop until the file is done. */
284
285         if (fp != stdin) fclose(fp);
286
287         /*
288          *      And we're done.
289          */
290         return start;
291 }
292
293
294 /*
295  *      Sanity check each argument.
296  */
297 static int radclient_sane(radclient_t *radclient)
298 {
299         if (radclient->request->dst_port == 0) {
300                 radclient->request->dst_port = server_port;
301         }
302         radclient->request->dst_ipaddr = server_ipaddr;
303
304         if (radclient->request->code == 0) {
305                 if (packet_code == -1) {
306                         fprintf(stderr, "radclient: Request was \"auto\", but request %d in file %s did not contain Packet-Type\n",
307                                 radclient->packet_number, radclient->filename);
308                         return -1;
309                 }
310
311                 radclient->request->code = packet_code;
312         }
313         radclient->request->sockfd = sockfd;
314
315         return 0;
316 }
317
318
319 /*
320  *      For request handline.
321  */
322 static int filename_cmp(const void *one, const void *two)
323 {
324         return strcmp((const char *) one, (const char *) two);
325 }
326
327 static int filename_walk(void *context, void *data)
328 {
329         const char      *filename = data;
330         radclient_t     *radclient;
331
332         context = context;      /* -Wunused */
333
334         /*
335          *      Initialize the request we're about
336          *      to send.
337          */
338         radclient = radclient_init(filename);
339         if (!radclient) {
340                 exit(1);
341         }
342
343         if (!radclient_head) {
344                 assert(radclient_tail == NULL);
345                 radclient_head = radclient;
346         } else {
347                 assert(radclient_tail->next == NULL);
348                 radclient_tail->next = radclient;
349                 radclient->prev = radclient_tail;
350         }
351
352         /*
353          *      We may have had a list of "radclient" structures
354          *      returned to us.
355          */
356         while (radclient->next) radclient = radclient->next;
357         radclient_tail = radclient;
358
359         return 0;
360 }
361
362
363 /*
364  *      Compare two RADIUS_PACKET data structures, based on a number
365  *      of criteria.
366  */
367 static int request_cmp(const void *one, const void *two)
368 {
369         const radclient_t *a = one;
370         const radclient_t *b = two;
371
372         /*
373          *      The following code looks unreasonable, but it's
374          *      the only way to make the comparisons work.
375          */
376         if (a->request->id < b->request->id) return -1;
377         if (a->request->id > b->request->id) return +1;
378
379         if (a->request->dst_ipaddr < b->request->dst_ipaddr) return -1;
380         if (a->request->dst_ipaddr > b->request->dst_ipaddr) return +1;
381
382         if (a->request->dst_port < b->request->dst_port) return -1;
383         if (a->request->dst_port > b->request->dst_port) return +1;
384
385         /*
386          *      Everything's equal.  Say so.
387          */
388         return 0;
389 }
390
391 /*
392  *      "Free" a request.
393  */
394 static void request_free(void *data)
395 {
396         radclient_t *radclient = (radclient_t *) data;
397
398         if (!radclient || !radclient->request ||
399             (radclient->request->id < 0)) {
400                 return;
401         }
402
403         /*
404          *      One more unused RADIUS ID.
405          */
406         radius_id[radclient->request->id] = 0;
407         radclient->request->id = -1;
408
409         /*
410          *      If we've already sent a packet, free up the old one,
411          *      and ensure that the next packet has a unique
412          *      authentication vector.
413          */
414         if (radclient->request->data) {
415                 free(radclient->request->data);
416                 radclient->request->data = NULL;
417         }
418
419         if (radclient->reply) rad_free(&radclient->reply);
420 }
421
422
423 /*
424  *      Send one packet.
425  */
426 static int send_one_packet(radclient_t *radclient)
427 {
428         int i;
429
430         assert(radclient->done == 0);
431
432         /*
433          *      Remember when we have to wake up, to re-send the
434          *      request, of we didn't receive a response.
435          */
436         if ((sleep_time == -1) ||
437             (sleep_time > (int) timeout)) {
438                 sleep_time = (int) timeout;
439         }
440
441         /*
442          *      Haven't sent the packet yet.  Initialize it.
443          */
444         if (radclient->request->id == -1) {
445                 int found = 0;
446
447                 assert(radclient->reply == NULL);
448
449                 /*
450                  *      Find a free packet Id
451                  */
452                 for (i = 0; i < 256; i++) {
453                         if (radius_id[(last_used_id + i) & 0xff] == 0) {
454                                 last_used_id = (last_used_id + i) & 0xff;
455                                 radius_id[last_used_id] = 1;
456                                 radclient->request->id = last_used_id++;
457                                 found = 1;
458                                 break;
459                         }
460                 }
461
462                 /*
463                  *      Didn't find a free packet ID, we're not done,
464                  *      we don't sleep, and we stop trying to process
465                  *      this packet.
466                  */
467                 if (!found) {
468                         done = 0;
469                         sleep_time = 0;
470                         return 0;
471                 }
472
473                 assert(radclient->request->id != -1);
474                 assert(radclient->request->data == NULL);
475
476                 librad_md5_calc(radclient->request->vector, radclient->request->vector,
477                                 sizeof(radclient->request->vector));
478
479                 /*
480                  *      Update the password, so it can be encrypted with the
481                  *      new authentication vector.
482                  */
483                 if (radclient->password[0] != '\0') {
484                         VALUE_PAIR *vp;
485
486                         if ((vp = pairfind(radclient->request->vps, PW_PASSWORD)) != NULL) {
487                                 strNcpy((char *)vp->strvalue, radclient->password, sizeof(vp->strvalue));
488                                 vp->length = strlen(vp->strvalue);
489
490                         } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD)) != NULL) {
491                                 strNcpy((char *)vp->strvalue, radclient->password, sizeof(vp->strvalue));
492                                 vp->length = strlen(vp->strvalue);
493
494                                 rad_chap_encode(radclient->request, (char *) vp->strvalue, radclient->request->id, vp);
495                                 vp->length = 17;
496                         }
497                 }
498
499                 radclient->timestamp = time(NULL);
500                 radclient->tries = 1;
501                 radclient->resend++;
502
503                 /*
504                  *      Duplicate found.  Serious error!
505                  */
506                 if (rbtree_insert(request_tree, radclient) == 0) {
507                         assert(0 == 1);
508                 }
509
510         } else {                /* radclient->request->id >= 0 */
511                 time_t now = time(NULL);
512
513                 /*
514                  *      FIXME: Accounting packets are never retried!
515                  *      The Acct-Delay-Time attribute is updated to
516                  *      reflect the delay, and the packet is re-sent
517                  *      from scratch!
518                  */
519
520                 /*
521                  *      Not time for a retry, do so.
522                  */
523                 if ((now - radclient->timestamp) < timeout) {
524                         /*
525                          *      When we walk over the tree sending
526                          *      packets, we update the minimum time
527                          *      required to sleep.
528                          */
529                         if ((sleep_time == -1) ||
530                             (sleep_time > (now - radclient->timestamp))) {
531                                 sleep_time = now - radclient->timestamp;
532                         }
533                         return 0;
534                 }
535
536                 /*
537                  *      We're not trying later, maybe the packet is done.
538                  */
539                 if (radclient->tries == retries) {
540                         rbnode_t *node;
541                         assert(radclient->request->id >= 0);
542                         
543                         /*
544                          *      Delete the request from the tree of
545                          *      outstanding requests.
546                          */
547                         node = rbtree_find(request_tree, radclient);
548                         assert(node != NULL);
549                         
550                         fprintf(stderr, "radclient: no response from server for ID %d\n", radclient->request->id);
551                         rbtree_delete(request_tree, node);
552                         
553                         /*
554                          *      Normally we mark it "done" when we've received
555                          *      the response, but this is a special case.
556                          */
557                         if (radclient->resend == resend_count) {
558                                 radclient->done = 1;
559                         }
560                         totallost++;
561                         return -1;
562                 }
563
564                 /*
565                  *      We are trying later.
566                  */
567                 radclient->timestamp = now;
568                 radclient->tries++;
569         }
570
571
572         /*
573          *      Send the packet.
574          */
575         if (rad_send(radclient->request, NULL, secret) < 0) {
576                 fprintf(stderr, "radclient: Failed to send packet for ID %d: %s\n",
577                         radclient->request->id, librad_errstr);
578         }
579
580         return 0;
581 }
582
583 /*
584  *      Receive one packet, maybe.
585  */
586 static int recv_one_packet(int wait_time)
587 {
588         fd_set          set;
589         struct timeval  tv;
590         radclient_t     myclient, *radclient;
591         RADIUS_PACKET   myrequest, *reply;
592         rbnode_t        *node;
593
594
595         /* And wait for reply, timing out as necessary */
596         FD_ZERO(&set);
597         FD_SET(sockfd, &set);
598
599         if (wait_time <= 0) {
600                 tv.tv_sec = 0;
601         } else {
602                 tv.tv_sec = wait_time;
603         }
604         tv.tv_usec = 0;
605
606         /*
607          *      No packet was received.
608          */
609         if (select(sockfd + 1, &set, NULL, NULL, &tv) != 1) {
610                 return 0;
611         }
612
613         /*
614          *      Look for the packet.
615          */
616         reply = rad_recv(sockfd);
617         if (!reply) {
618                 fprintf(stderr, "radclient: received bad packet: %s\n",
619                         librad_errstr);
620                 return -1;      /* bad packet */
621         }
622
623         myclient.request = &myrequest;
624         myrequest.id = reply->id;
625         myrequest.dst_ipaddr = reply->src_ipaddr;
626         myrequest.dst_port = reply->src_port;
627
628         node = rbtree_find(request_tree, &myclient);
629         if (!node) {
630                 fprintf(stderr, "radclient: received response to request we did not send.\n");
631                 rad_free(&reply);
632                 return -1;      /* got reply to packet we didn't send */
633         }
634
635         radclient = rbtree_node2data(request_tree, node);
636         assert(radclient != NULL);
637         rbtree_delete(request_tree, node);
638         assert(radclient->request->id == -1);
639         assert(radclient->request->data == NULL);
640
641         assert(radclient->reply == NULL);
642         radclient->reply = reply;
643
644         /*
645          *      FIXME: Do stuff to process the reply.
646          */
647         if (rad_verify(reply, radclient->request, secret) != 0) {
648                 librad_perror("rad_verify");
649                 totallost++;
650                 goto packet_done; /* shared secret is incorrect */
651         }
652
653         if (rad_decode(reply, radclient->request, secret) != 0) {
654                 librad_perror("rad_decode");
655                 totallost++;
656                 goto packet_done; /* shared secret is incorrect */
657         }
658
659         /* libradius debug already prints out the value pairs for us */
660         if (!librad_debug && do_output) {
661                 printf("Received response ID %d, code %d, length = %d\n",
662                        reply->id, reply->code, reply->data_len);
663                 vp_printlist(stdout, reply->vps);
664         }
665         if (reply->code != PW_AUTHENTICATION_REJECT) {
666                 totalapp++;
667         } else {
668                 totaldeny++;
669         }
670
671 packet_done:
672         rad_free(&radclient->reply);
673
674         /*
675          *      Once we've sent the packet as many times as requested,
676          *      mark it done.
677          */
678         if (radclient->resend == resend_count) {
679                 assert((node = rbtree_find(request_tree, radclient)) == NULL);
680                 radclient->done = 1;
681         }
682
683         return 0;
684 }
685
686 static int getport(const char *name)
687 {
688         struct  servent         *svp;
689
690         svp = getservbyname (name, "udp");
691         if (!svp) {
692                 return 0;
693         }
694
695         return ntohs(svp->s_port);
696 }
697
698 int main(int argc, char **argv)
699 {
700         char *p;
701         int c;
702         const char *radius_dir = RADDBDIR;
703         char filesecret[256];
704         FILE *fp;
705         int do_summary = 0;
706         int persec = 0;
707         int parallel = 1;
708         radclient_t     *this;
709
710         librad_debug = 0;
711
712         filename_tree = rbtree_create(filename_cmp, NULL, 0);
713         if (!filename_tree) {
714                 fprintf(stderr, "radclient: Out of memory\n");
715                 exit(1);
716         }
717
718         request_tree = rbtree_create(request_cmp, request_free, 0);
719         if (!request_tree) {
720                 fprintf(stderr, "radclient: Out of memory\n");
721                 exit(1);
722         }
723
724         while ((c = getopt(argc, argv, "c:d:f:hi:n:p:qr:sS:t:vx")) != EOF) switch(c) {
725                 case 'c':
726                         if (!isdigit((int) *optarg))
727                                 usage();
728                         resend_count = atoi(optarg);
729                         break;
730                 case 'd':
731                         radius_dir = optarg;
732                         break;
733                 case 'f':
734                         rbtree_insert(filename_tree, optarg);
735                         break;
736                 case 'i':
737                         if (!isdigit((int) *optarg))
738                                 usage();
739                         last_used_id = atoi(optarg);
740                         if ((last_used_id < 0) || (last_used_id > 255)) {
741                                 usage();
742                         }
743                         break;
744
745                 case 'n':
746                         persec = atoi(optarg);
747                         if (persec <= 0) usage();
748                         break;
749
750                 case 'p':
751                         parallel = atoi(optarg);
752                         if (parallel <= 0) usage();
753                         break;
754
755                 case 'q':
756                         do_output = 0;
757                         break;
758                 case 'r':
759                         if (!isdigit((int) *optarg))
760                                 usage();
761                         retries = atoi(optarg);
762                         if ((retries == 0) || (retries > 1000)) usage();
763                         break;
764                 case 's':
765                         do_summary = 1;
766                         break;
767                case 'S':
768                        fp = fopen(optarg, "r");
769                        if (!fp) {
770                                fprintf(stderr, "radclient: Error opening %s: %s\n",
771                                        optarg, strerror(errno));
772                                exit(1);
773                        }
774                        if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
775                                fprintf(stderr, "radclient: Error reading %s: %s\n",
776                                        optarg, strerror(errno));
777                                exit(1);
778                        }
779                        fclose(fp);
780
781                        /* truncate newline */
782                        p = filesecret + strlen(filesecret) - 1;
783                        while ((p >= filesecret) &&
784                               (*p < ' ')) {
785                                *p = '\0';
786                                --p;
787                        }
788
789                        if (strlen(filesecret) < 2) {
790                                fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
791                                exit(1);
792                        }
793                        secret = filesecret;
794                        break;
795                 case 't':
796                         if (!isdigit((int) *optarg))
797                                 usage();
798                         timeout = atof(optarg);
799                         break;
800                 case 'v':
801                         printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
802                         exit(0);
803                         break;
804                 case 'x':
805                         librad_debug++;
806                         break;
807                 case 'h':
808                 default:
809                         usage();
810                         break;
811         }
812         argc -= (optind - 1);
813         argv += (optind - 1);
814
815         if ((argc < 3)  ||
816             ((secret == NULL) && (argc < 4))) {
817                 usage();
818         }
819
820         if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
821                 librad_perror("radclient");
822                 return 1;
823         }
824
825         /*
826          *      Strip port from hostname if needed.
827          */
828         if ((p = strchr(argv[1], ':')) != NULL) {
829                 *p++ = 0;
830                 server_port = atoi(p);
831         }
832
833         /*
834          *      Grab the socket.
835          */
836         if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
837                 perror("radclient: socket: ");
838                 exit(1);
839         }
840         memset(radius_id, 0, sizeof(radius_id));
841
842         /*
843          *      See what kind of request we want to send.
844          */
845         if (strcmp(argv[2], "auth") == 0) {
846                 if (server_port == 0) server_port = getport("radius");
847                 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
848                 packet_code = PW_AUTHENTICATION_REQUEST;
849
850         } else if (strcmp(argv[2], "challenge") == 0) {
851                 if (server_port == 0) server_port = getport("radius");
852                 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
853                 packet_code = PW_ACCESS_CHALLENGE;
854
855         } else if (strcmp(argv[2], "acct") == 0) {
856                 if (server_port == 0) server_port = getport("radacct");
857                 if (server_port == 0) server_port = PW_ACCT_UDP_PORT;
858                 packet_code = PW_ACCOUNTING_REQUEST;
859                 do_summary = 0;
860
861         } else if (strcmp(argv[2], "status") == 0) {
862                 if (server_port == 0) server_port = getport("radius");
863                 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
864                 packet_code = PW_STATUS_SERVER;
865
866         } else if (strcmp(argv[2], "disconnect") == 0) {
867                 if (server_port == 0) server_port = PW_POD_UDP_PORT;
868                 packet_code = PW_DISCONNECT_REQUEST;
869
870         } else if (strcmp(argv[2], "coa") == 0) {
871                 if (server_port == 0) server_port = PW_POD_UDP_PORT;
872                 packet_code = PW_COA_REQUEST;
873
874         } else if (strcmp(argv[2], "auto") == 0) {
875                 packet_code = -1;
876
877         } else if (isdigit((int) argv[2][0])) {
878                 if (server_port == 0) server_port = getport("radius");
879                 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
880                 packet_code = atoi(argv[2]);
881         } else {
882                 usage();
883         }
884
885         /*
886          *      Resolve hostname.
887          */
888         server_ipaddr = ip_getaddr(argv[1]);
889         if (server_ipaddr == INADDR_NONE) {
890                 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
891                 exit(1);
892         }
893
894         /*
895          *      Add the secret.
896          */
897         if (argv[3]) secret = argv[3];
898
899         /*
900          *      If no '-f' is specified, we're reading from stdin.
901          */
902         if (rbtree_num_elements(filename_tree) == 0) {
903                 rbtree_insert(filename_tree, "-");
904         }
905
906         /*
907          *      Walk over the list of filenames, creating the requests.
908          */
909         if (rbtree_walk(filename_tree, InOrder, filename_walk, NULL) != 0) {
910                 exit(1);
911         }
912
913         /*
914          *      No packets read.  Die.
915          */
916         if (!radclient_head) {
917                 fprintf(stderr, "radclient: Nothing to send.\n");
918                 exit(1);
919         }
920
921         /*
922          *      Walk over the list of packets, sanity checking
923          *      everything.
924          */
925         for (this = radclient_head; this != NULL; this = this->next) {
926                 if (radclient_sane(this) != 0) {
927                         exit(1);
928                 }
929         }
930
931         if (last_used_id < 0) last_used_id = getpid() & 0xff;
932
933         /*
934          *      Walk over the packets to send, until
935          *      we're all done.
936          *
937          *      FIXME: This currently busy-loops until it receives
938          *      all of the packets.  It should really have some sort of
939          *      send packet, get time to wait, select for time, etc.
940          *      loop.
941          */
942         do {
943                 int n = parallel;
944                 radclient_t *next;
945                 const char *filename = NULL;
946
947                 done = 1;
948                 sleep_time = -1;
949
950                 /*
951                  *      Walk over the packets, sending them.
952                  */
953
954                 for (this = radclient_head; this != NULL; this = next) {
955                         next = this->next;
956
957                         /*
958                          *      If there's a packet to receive,
959                          *      receive it, but don't wait for a
960                          *      packet.
961                          */
962                         recv_one_packet(0);
963
964                         /*
965                          *      This packet is done.  Delete it.
966                          */
967                         if (this->done) {
968                                 radclient_free(this);
969                                 continue;
970                         }
971
972                         /*
973                          *      Packets from multiple '-f' are sent
974                          *      in parallel.
975                          *
976                          *      Packets from one file are sent in
977                          *      series, unless '-p' is specified, in
978                          *      which case N packets from each file
979                          *      are sent in parallel.
980                          */
981                         if (this->filename != filename) {
982                                 filename = this->filename;
983                                 n = parallel;
984                         }
985
986                         if (n > 0) {
987                                 n--;
988
989                                 /*
990                                  *      Send the current packet.
991                                  */
992                                 send_one_packet(this);
993
994                                 /*
995                                  *      Wait a little before sending
996                                  *      the next packet, if told to.
997                                  */
998                                 if (persec) {
999                                         struct timeval tv;
1000
1001                                         /*
1002                                          *      Don't sleep elsewhere.
1003                                          */
1004                                         sleep_time = 0;
1005
1006                                         if (persec == 1) {
1007                                                 tv.tv_sec = 1;
1008                                                 tv.tv_usec = 0;
1009                                         } else {
1010                                                 tv.tv_sec = 0;
1011                                                 tv.tv_usec = 1000000/persec;
1012                                         }
1013                                         
1014                                         /*
1015                                          *      Sleep for milliseconds,
1016                                          *      portably.
1017                                          *
1018                                          *      If we get an error or
1019                                          *      a signal, treat it like
1020                                          *      a normal timeout.
1021                                          */
1022                                         select(0, NULL, NULL, NULL, &tv);
1023                                 }
1024
1025                                 /*
1026                                  *      If we haven't sent this packet
1027                                  *      often enough, we're not done,
1028                                  *      and we shouldn't sleep.
1029                                  */
1030                                 if (this->resend < resend_count) {
1031                                         done = 0;
1032                                         sleep_time = 0;
1033                                 }
1034                         } else { /* haven't sent this packet, we're not done */
1035                                 assert(this->done == 0);
1036                                 assert(this->reply == NULL);
1037                                 done = 0;
1038                         }
1039                 }
1040
1041                 /*
1042                  *      Still have outstanding requests.
1043                  */
1044                 if (rbtree_num_elements(request_tree) > 0) {
1045                         done = 0;
1046                 } else {
1047                         sleep_time = 0;
1048                 }
1049
1050                 /*
1051                  *      Nothing to do until we receive a request, so
1052                  *      sleep until then.  Once we receive one packet,
1053                  *      we go back, and walk through the whole list again,
1054                  *      sending more packets (if necessary), and updating
1055                  *      the sleep time.
1056                  */
1057                 if (!done && (sleep_time > 0)) {
1058                         recv_one_packet(sleep_time);
1059                 }
1060         } while (!done);
1061
1062         rbtree_free(filename_tree);
1063         rbtree_free(request_tree);
1064
1065         if (do_summary) {
1066                 printf("\n\t   Total approved auths:  %d\n", totalapp);
1067                 printf("\t     Total denied auths:  %d\n", totaldeny);
1068                 printf("\t       Total lost auths:  %d\n", totallost);
1069         }
1070
1071         return 0;
1072 }