2 * radmin.c RADIUS Administration tool.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2008 The FreeRADIUS server project
21 * Copyright 2008 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/libradius.h>
28 #include <freeradius-devel/radpaths.h>
30 #ifdef HAVE_READLINE_READLINE_H
31 #include <readline/readline.h>
32 #include <readline/history.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
44 static int fr_domain_socket(const char *path)
49 struct sockaddr_un saremote;
52 if (len >= sizeof(saremote.sun_path)) {
53 fprintf(stderr, "Path too long in filename\n");
57 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
58 fprintf(stderr, "Failed creating socket: %s\n",
63 saremote.sun_family = AF_UNIX;
64 memcpy(saremote.sun_path, path, len); /* not zero terminated */
66 socklen = sizeof(saremote.sun_family) + len;
68 if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
69 fprintf(stderr, "Failed connecting to %s: %s\n",
70 path, strerror(errno));
79 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
80 fprintf(stderr, "Failure getting socket flags: %s",
87 if( fcntl(sockfd, F_SETFL, flags) < 0) {
88 fprintf(stderr, "Failure setting socket flags: %s",
99 int main(int argc, char **argv)
105 const char *file = RUNDIR "/radiusd/radiusd.sock";
106 char *p, buffer[2048];
109 ((argc == 2) && (strcmp(argv[1], "-h") == 0))) {
110 printf("Usage: radmin [socket]\n");
114 if (argc == 2) file = argv[1];
116 #ifdef HAVE_READLINE_READLINE_H
118 rl_bind_key('\t', rl_insert);
122 * FIXME: Get destination from command line, if possible?
124 sockfd = fr_domain_socket(file);
130 * Read initial magic && version information.
132 for (size = 0; size < 8; size += len) {
133 len = read(sockfd, buffer + size, 8 - size);
135 fprintf(stderr, "Failed reading initial data from socket: %s\n",
141 memcpy(&magic, buffer, 4);
142 magic = ntohl(magic);
143 if (magic != 0xf7eead15) {
144 fprintf(stderr, "Socket is not FreeRADIUS administration socket\n");
148 memcpy(&magic, buffer + 4, 4);
149 magic = ntohl(magic);
151 fprintf(stderr, "Socket version mismatch: Need 1, got %d\n",
161 #ifndef HAVE_READLINE_READLINE_H
162 fprintf(stdout, "radmin> ");
165 line = fgets(buffer, sizeof(buffer), stdin);
168 p = strchr(buffer, '\n');
170 fprintf(stderr, "line too long\n");
176 line = readline("radmin> ");
189 * Write the text to the socket.
191 if (write(sockfd, line, strlen(line)) < 0) break;
192 if (write(sockfd, "\r\n", 2) < 0) break;
197 if ((strcmp(line, "exit") == 0) ||
198 (strcmp(line, "quit") == 0)) {
209 memset(buffer, 0, sizeof(buffer));
216 FD_SET(sockfd, &readfds);
218 rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL);
220 if (errno == EINTR) continue;
222 fprintf(stderr, "Failed selecting: %s\n",
227 len = recv(sockfd, buffer + size,
228 sizeof(buffer) - size - 1, MSG_DONTWAIT);
231 * No data: keep looping
233 if ((errno == EAGAIN) || (errno == EINTR)) {
237 fprintf(stderr, "Error reading socket: %s\n",
241 if (len == 0) break; /* clean close of socket */
247 * There really has to be a better way of
250 p = strstr(buffer, "radmin> ");
257 if (p[-1] == '\n') p[-1] = '\0';
265 * Blank prompt. Go get another line.
267 if (!buffer[0]) continue;
269 buffer[size] = '\0'; /* this is at least right */