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