import from HEAD:
[freeradius.git] / src / main / radclient.c
index e5aa22a..cd1d937 100644 (file)
@@ -24,7 +24,6 @@
 static const char rcsid[] = "$Id$";
 
 #include "autoconf.h"
-#include "libradius.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -55,6 +54,7 @@ static const char rcsid[] = "$Id$";
 #include "conf.h"
 #include "radpaths.h"
 #include "missing.h"
+#include "libradius.h"
 
 static int retries = 10;
 static float timeout = 3;
@@ -66,7 +66,7 @@ static int totallost = 0;
 
 static int server_port = 0;
 static int packet_code = 0;
-static lrad_ipaddr_t server_ipaddr;
+static uint32_t server_ipaddr = 0;
 static int resend_count = 1;
 static int done = 1;
 
@@ -98,7 +98,7 @@ static radclient_t *radclient_head = NULL;
 static radclient_t *radclient_tail = NULL;
 
 
-static void usage(void)
+static void NEVER_RETURNS usage(void)
 {
        fprintf(stderr, "Usage: radclient [options] server[:port] <command> [<secret>]\n");
 
@@ -219,12 +219,12 @@ static radclient_t *radclient_init(const char *filename)
                 *      Keep a copy of the the User-Password attribute.
                 */
                if ((vp = pairfind(radclient->request->vps, PW_PASSWORD)) != NULL) {
-                       strNcpy(radclient->password, (char *)vp->strvalue, sizeof(vp->strvalue));
+                       strNcpy(radclient->password, (char *)vp->strvalue, sizeof(radclient->password));
                        /*
                         *      Otherwise keep a copy of the CHAP-Password attribute.
                         */
                } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD)) != NULL) {
-                       strNcpy(radclient->password, (char *)vp->strvalue, sizeof(vp->strvalue));
+                       strNcpy(radclient->password, (char *)vp->strvalue, sizeof(radclient->password));
                } else {
                        radclient->password[0] = '\0';
                }
@@ -249,11 +249,6 @@ static radclient_t *radclient_init(const char *filename)
                                radclient->request->dst_port = (vp->lvalue & 0xffff);
                                break;
 
-                       case PW_PACKET_DST_IP_ADDRESS:
-                               radclient->request->dst_ipaddr.af = AF_INET;
-                               radclient->request->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->lvalue;
-                               break;
-
                        case PW_DIGEST_REALM:
                        case PW_DIGEST_NONCE:
                        case PW_DIGEST_METHOD:
@@ -301,14 +296,7 @@ static int radclient_sane(radclient_t *radclient)
        if (radclient->request->dst_port == 0) {
                radclient->request->dst_port = server_port;
        }
-       if (radclient->request->dst_ipaddr.af == 0) {
-               if (server_ipaddr.af == AF_UNSPEC) {
-                       fprintf(stderr, "radclient: No server was given, but request %d in file %s did not contain Packet-Dst-IP-Address\n",
-                               radclient->packet_number, radclient->filename);
-                       return -1;
-               }
-               radclient->request->dst_ipaddr = server_ipaddr;
-       }
+       radclient->request->dst_ipaddr = server_ipaddr;
 
        if (radclient->request->code == 0) {
                if (packet_code == -1) {
@@ -375,7 +363,6 @@ static int filename_walk(void *context, void *data)
  */
 static int request_cmp(const void *one, const void *two)
 {
-       int rcode;
        const radclient_t *a = one;
        const radclient_t *b = two;
 
@@ -386,26 +373,8 @@ static int request_cmp(const void *one, const void *two)
        if (a->request->id < b->request->id) return -1;
        if (a->request->id > b->request->id) return +1;
 
-       if (a->request->dst_ipaddr.af < b->request->dst_ipaddr.af) return -1;
-       if (a->request->dst_ipaddr.af > b->request->dst_ipaddr.af) return +1;
-
-       if (a->request->dst_ipaddr.af != AF_INET) return -1; /* FIXME */
-
-       switch (a->request->dst_ipaddr.af) {
-       case AF_INET:
-               rcode = memcmp(&a->request->dst_ipaddr.ipaddr.ip4addr,
-                              &b->request->dst_ipaddr.ipaddr.ip4addr,
-                              sizeof(a->request->dst_ipaddr.ipaddr.ip4addr));
-               break;
-       case AF_INET6:
-               rcode = memcmp(&a->request->dst_ipaddr.ipaddr.ip6addr,
-                              &b->request->dst_ipaddr.ipaddr.ip6addr,
-                              sizeof(a->request->dst_ipaddr.ipaddr.ip6addr));
-               break;
-       default:                /* FIXME: die! */
-               break;
-       }
-       if (rcode != 0) return rcode;
+       if (a->request->dst_ipaddr < b->request->dst_ipaddr) return -1;
+       if (a->request->dst_ipaddr > b->request->dst_ipaddr) return +1;
 
        if (a->request->dst_port < b->request->dst_port) return -1;
        if (a->request->dst_port > b->request->dst_port) return +1;
@@ -512,12 +481,12 @@ static int send_one_packet(radclient_t *radclient)
                        VALUE_PAIR *vp;
 
                        if ((vp = pairfind(radclient->request->vps, PW_PASSWORD)) != NULL) {
-                               strNcpy((char *)vp->strvalue, radclient->password, strlen(radclient->password) + 1);
-                               vp->length = strlen(radclient->password);
+                               strNcpy((char *)vp->strvalue, radclient->password, sizeof(vp->strvalue));
+                               vp->length = strlen(vp->strvalue);
 
                        } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD)) != NULL) {
-                               strNcpy((char *)vp->strvalue, radclient->password, strlen(radclient->password) + 1);
-                               vp->length = strlen(radclient->password);
+                               strNcpy((char *)vp->strvalue, radclient->password, sizeof(vp->strvalue));
+                               vp->length = strlen(vp->strvalue);
 
                                rad_chap_encode(radclient->request, (char *) vp->strvalue, radclient->request->id, vp);
                                vp->length = 17;
@@ -600,7 +569,10 @@ static int send_one_packet(radclient_t *radclient)
        /*
         *      Send the packet.
         */
-       rad_send(radclient->request, NULL, secret);
+       if (rad_send(radclient->request, NULL, secret) < 0) {
+               fprintf(stderr, "radclient: Failed to send packet for ID %d: %s\n",
+                       radclient->request->id, librad_errstr);
+       }
 
        return 0;
 }
@@ -640,8 +612,7 @@ static int recv_one_packet(int wait_time)
         */
        reply = rad_recv(sockfd);
        if (!reply) {
-               fprintf(stderr, "radclient: received bad packet: %s\n",
-                       librad_errstr);
+               fprintf(stderr, "radclient: received bad packet\n");
                return -1;      /* bad packet */
        }
 
@@ -653,6 +624,7 @@ static int recv_one_packet(int wait_time)
        node = rbtree_find(request_tree, &myclient);
        if (!node) {
                fprintf(stderr, "radclient: received response to request we did not send.\n");
+               rad_free(&reply);
                return -1;      /* got reply to packet we didn't send */
        }
 
@@ -671,7 +643,7 @@ static int recv_one_packet(int wait_time)
        if (rad_decode(reply, radclient->request, secret) != 0) {
                librad_perror("rad_decode");
                totallost++;
-               return -1;
+               goto packet_done; /* shared secret is incorrect */
        }
 
        /* libradius debug already prints out the value pairs for us */
@@ -686,7 +658,8 @@ static int recv_one_packet(int wait_time)
                totaldeny++;
        }
 
-       if (radclient->reply) rad_free(&radclient->reply);
+packet_done:
+       rad_free(&radclient->reply);
 
        /*
         *      Once we've sent the packet as many times as requested,
@@ -700,7 +673,6 @@ static int recv_one_packet(int wait_time)
        return 0;
 }
 
-
 static int getport(const char *name)
 {
        struct  servent         *svp;
@@ -899,12 +871,10 @@ int main(int argc, char **argv)
        /*
         *      Resolve hostname.
         */
-       server_ipaddr.af = AF_UNSPEC;
-       if (strcmp(argv[1], "-") != 0) {
-               if (ip_hton(argv[1], AF_INET, &server_ipaddr) < 0) {
-                       fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
-                       exit(1);
-               }
+       server_ipaddr = ip_getaddr(argv[1]);
+       if (server_ipaddr == INADDR_NONE) {
+               fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
+               exit(1);
        }
 
        /*