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