import from HEAD:
[freeradius.git] / src / main / radclient.c
index 9e64e46..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 uint32_t server_ipaddr = INADDR_NONE;
+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,10 +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 = vp->lvalue;
-                               break;
-
                        case PW_DIGEST_REALM:
                        case PW_DIGEST_NONCE:
                        case PW_DIGEST_METHOD:
@@ -300,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 == 0) {
-               if (server_ipaddr == INADDR_NONE) {
-                       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) {
@@ -492,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;
@@ -580,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;
 }
@@ -632,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 */
        }
 
@@ -650,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 */
@@ -665,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,
@@ -877,12 +871,10 @@ int main(int argc, char **argv)
        /*
         *      Resolve hostname.
         */
-       if (strcmp(argv[1], "-") != 0) {
-               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);
-               }
+       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);
        }
 
        /*