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