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