static const char rcsid[] = "$Id$";
#include "autoconf.h"
-#include "libradius.h"
#include <stdio.h>
#include <stdlib.h>
#include "conf.h"
#include "radpaths.h"
#include "missing.h"
+#include "libradius.h"
static int retries = 10;
static float timeout = 3;
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;
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");
* 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';
}
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:
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) {
*/
static int request_cmp(const void *one, const void *two)
{
- int rcode;
const radclient_t *a = one;
const radclient_t *b = 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;
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;
/*
* 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;
}
*/
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 */
}
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 */
}
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 */
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,
return 0;
}
-
static int getport(const char *name)
{
struct servent *svp;
/*
* 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);
}
/*