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"
34 * Read valuepairs from the fp up to End-Of-File.
36 VALUE_PAIR *readvp(FILE *fp)
47 while (!error && fgets(buf, 128, fp) != NULL) {
51 if ((vp = pairread(&p, &eol)) == NULL) {
52 librad_perror("radclient:");
59 return error ? NULL: list;
64 fprintf(stderr, "Usage: radclient [-d raddb ] [-f file] [-t timeout] [-nx] server acct|auth <secret>\n");
68 int getport(char *name)
72 svp = getservbyname (name, "udp");
77 return ntohs(svp->s_port);
80 int main(int argc, char **argv)
83 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) {
198 * Find the password pair and encode it.
200 if ((vp = pairfind(req->vps, PW_PASSWORD)) != NULL)
201 rad_pwencode(vp->strvalue, &(vp->length), secret, req->vector);
206 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
207 perror("radclient: socket: ");
211 for (i = 0; i < 10; i++) {
214 rad_send(req, s, secret);
216 /* And wait for reply, timing out as necessary */
223 /* Something's wrong if we don't get exactly one fd. */
224 if (select(s+1, &rdfdesc, NULL, NULL, &tv) != 1) {
231 } else { /* NULL: couldn't receive the packet */
232 librad_perror("radclient:");
237 /* No response or no data read (?) */
239 fprintf(stderr, "radclient: no response from server\n");
243 if (rad_decode(rep, secret) != 0) {
244 librad_perror("rad_decode");
248 /* libradius debug already prints out the value pairs for us */
249 if (!librad_debug && do_output)
250 vp_printlist(stdout, rep->vps);