Skip to content

Commit

Permalink
Set port in radclient 'auto' mode too
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed May 20, 2014
1 parent 7af114b commit 77accf3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/include/radius.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef enum {
} PW_TYPE;

typedef enum {
PW_CODE_INVALID = 0, //!< Packet code is invalid
PW_CODE_UNDEFINED = 0, //!< Packet code has not been set
PW_CODE_AUTHENTICATION_REQUEST = 1, //!< RFC2865 - Access-Request
PW_CODE_AUTHENTICATION_ACK = 2, //!< RFC2865 - Access-Accept
PW_CODE_AUTHENTICATION_REJECT = 3, //!< RFC2865 - Access-Reject
Expand Down
130 changes: 74 additions & 56 deletions src/main/radclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static bool do_output = true;

static rc_stats_t stats;

static int server_port = 0;
static uint16_t server_port = 0;
static int packet_code = 0;
static fr_ipaddr_t server_ipaddr;
static int resend_count = 1;
Expand Down Expand Up @@ -111,6 +111,17 @@ static void NEVER_RETURNS usage(void)
exit(1);
}

static const FR_NAME_NUMBER request_types[] = {
{ "auth", PW_CODE_AUTHENTICATION_REQUEST },
{ "challenge", PW_CODE_ACCESS_CHALLENGE },
{ "status", PW_CODE_STATUS_SERVER },
{ "disconnect", PW_CODE_DISCONNECT_REQUEST },
{ "coa", PW_CODE_COA_REQUEST },
{ "auto", PW_CODE_UNDEFINED },

{ NULL, 0}
};

/*
* Free a radclient struct, which may (or may not)
* already be in the list.
Expand Down Expand Up @@ -181,6 +192,45 @@ static int mschapv1_encode(RADIUS_PACKET *packet, VALUE_PAIR **request,
return 1;
}


static int getport(char const *name)
{
struct servent *svp;

svp = getservbyname(name, "udp");
if (!svp) {
return 0;
}

return ntohs(svp->s_port);
}

static void radclient_get_port(PW_CODE type, uint16_t *port)
{
switch (type) {
default:
case PW_CODE_AUTHENTICATION_REQUEST:
case PW_CODE_ACCESS_CHALLENGE:
case PW_CODE_STATUS_SERVER:
if (*port == 0) *port = getport("radius");
if (*port == 0) *port = PW_AUTH_UDP_PORT;
return;

case PW_CODE_ACCOUNTING_REQUEST:
if (*port == 0) *port = getport("radacct");
if (*port == 0) *port = PW_ACCT_UDP_PORT;
return;

case PW_CODE_DISCONNECT_REQUEST:
case PW_CODE_COA_REQUEST:
if (*port == 0) *port = PW_COA_UDP_PORT;
return;

case PW_CODE_UNDEFINED:
if (*port == 0) *port = 0;
}
}

/*
* Initialize a radclient data structure and add it to
* the global linked list.
Expand Down Expand Up @@ -479,6 +529,14 @@ static int radclient_init(TALLOC_CTX *ctx, rc_file_pair_t *files)
}
} /* loop over the VP's we read in */

/*
* Automatically set the port if we don't have a global
* or packet specific one.
*/
if ((server_port == 0) && (request->packet->dst_port == 0)) {
radclient_get_port(request->packet->code, &request->packet->dst_port);
}

/*
* Add it to the tail of the list.
*/
Expand Down Expand Up @@ -965,19 +1023,6 @@ static int recv_one_packet(int wait_time)
return 0;
}


static int getport(char const *name)
{
struct servent *svp;

svp = getservbyname (name, "udp");
if (!svp) {
return 0;
}

return ntohs(svp->s_port);
}

int main(int argc, char **argv)
{
int c;
Expand Down Expand Up @@ -1182,6 +1227,20 @@ int main(int argc, char **argv)
}
fr_strerror(); /* Clear the error buffer */


/*
* Get the request type
*/
if (!isdigit((int) argv[2][0])) {
packet_code = fr_str2int(request_types, argv[2], -2);
if (packet_code == -2) {
ERROR("Unrecognised request type \"%s\"", argv[2]);
usage();
}
} else {
packet_code = atoi(argv[2]);
}

/*
* Resolve hostname.
*/
Expand Down Expand Up @@ -1225,48 +1284,7 @@ int main(int argc, char **argv)
if (portname) server_port = atoi(portname);
}

/*
* See what kind of request we want to send.
*/
if (strcmp(argv[2], "auth") == 0) {
if (server_port == 0) server_port = getport("radius");
if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
packet_code = PW_CODE_AUTHENTICATION_REQUEST;

} else if (strcmp(argv[2], "challenge") == 0) {
if (server_port == 0) server_port = getport("radius");
if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
packet_code = PW_CODE_ACCESS_CHALLENGE;

} else if (strcmp(argv[2], "acct") == 0) {
if (server_port == 0) server_port = getport("radacct");
if (server_port == 0) server_port = PW_ACCT_UDP_PORT;
packet_code = PW_CODE_ACCOUNTING_REQUEST;
do_summary = false;

} else if (strcmp(argv[2], "status") == 0) {
if (server_port == 0) server_port = getport("radius");
if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
packet_code = PW_CODE_STATUS_SERVER;

} else if (strcmp(argv[2], "disconnect") == 0) {
if (server_port == 0) server_port = PW_COA_UDP_PORT;
packet_code = PW_CODE_DISCONNECT_REQUEST;

} else if (strcmp(argv[2], "coa") == 0) {
if (server_port == 0) server_port = PW_COA_UDP_PORT;
packet_code = PW_CODE_COA_REQUEST;

} else if (strcmp(argv[2], "auto") == 0) {
packet_code = -1;

} else if (isdigit((int) argv[2][0])) {
if (server_port == 0) server_port = getport("radius");
if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
packet_code = atoi(argv[2]);
} else {
usage();
}
radclient_get_port(packet_code, &server_port);

/*
* Add the secret.
Expand Down

0 comments on commit 77accf3

Please sign in to comment.