2 * radclient General radius packet debug tool.
4 * Version: @(#)radclient 1.10 25-Jul-1999 miquels@cistron.nl
17 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <netinet/in.h>
23 # include <sys/select.h>
31 #include "libradius.h"
35 * Read valuepairs from the fp up to End-Of-File.
37 VALUE_PAIR *readvp(FILE *fp)
48 while (!error && fgets(buf, 128, fp) != NULL) {
52 if ((vp = pairread(&p, &eol)) == NULL) {
53 librad_perror("radclient:");
60 return error ? NULL: list;
65 fprintf(stderr, "Usage: radclient [-d raddb ] [-f file] [-t timeout] [-nx] server acct|auth <secret>\n");
69 int getport(char *name)
73 svp = getservbyname (name, "udp");
78 return ntohs(svp->s_port);
81 int main(int argc, char **argv)
84 RADIUS_PACKET *rep = NULL;
87 char *secret = "secret";
94 char *radius_dir = RADDBDIR;
95 char *filename = NULL;
98 while ((c = getopt(argc, argv, "d:f:nxt:")) != EOF) switch(c) {
112 timeout = atoi(optarg);
118 argc -= (optind - 1);
119 argv += (optind - 1);
125 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
126 librad_perror("radclient");
130 if ((req = rad_alloc(1)) == NULL) {
131 librad_perror("radclient");
134 req->id = getpid() & 0xFF;
137 * Strip port from hostname if needed.
139 if ((p = strchr(argv[1], ':')) != NULL) {
145 * See what kind of request we want to send.
147 if (strcmp(argv[2], "auth") == 0) {
148 if (port == 0) port = getport("radius");
149 if (port == 0) port = PW_AUTH_UDP_PORT;
150 req->code = PW_AUTHENTICATION_REQUEST;
151 } else if (strcmp(argv[2], "acct") == 0) {
152 if (port == 0) port = getport("radacct");
153 if (port == 0) port = PW_ACCT_UDP_PORT;
154 req->code = PW_ACCOUNTING_REQUEST;
155 } else if (isdigit(argv[2][0])) {
156 if (port == 0) port = PW_AUTH_UDP_PORT;
157 port = atoi(argv[2]);
165 req->dst_port = port;
166 req->dst_ipaddr = ip_getaddr(argv[1]);
167 if (req->dst_ipaddr == 0) {
168 librad_perror("radclient: %s: ", argv[1]);
175 if (argv[3]) secret = argv[3];
179 * Maybe read them, from stdin, if there's no
180 * filename, or if the filename is '-'.
182 if (filename && (strcmp(filename, "-") != 0)) {
183 fp = fopen(filename, "r");
185 fprintf(stderr, "radclient: Error opening %s: %s\n",
186 filename, strerror(errno));
193 if ((req->vps = readvp(fp)) == NULL) {
200 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
201 perror("radclient: socket: ");
205 for (i = 0; i < 10; i++) {
208 rad_send(req, s, secret);
210 /* And wait for reply, timing out as necessary */
217 /* Something's wrong if we don't get exactly one fd. */
218 if (select(s+1, &rdfdesc, NULL, NULL, &tv) != 1) {
225 } else { /* NULL: couldn't receive the packet */
226 librad_perror("radclient:");
231 /* No response or no data read (?) */
233 fprintf(stderr, "radclient: no response from server\n");
237 if (rad_decode(rep, secret) != 0) {
238 librad_perror("rad_decode");
242 /* libradius debug already prints out the value pairs for us */
243 if (!librad_debug && do_output)
244 vp_printlist(stdout, rep->vps);