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)
104 const char *file = RUNDIR "/radiusd/radiusd.sock";
105 char *p, buffer[2048];
108 ((argc == 2) && (strcmp(argv[1], "-h") == 0))) {
109 printf("Usage: radmin [socket]\n");
113 if (argc == 2) file = argv[1];
115 #ifdef HAVE_READLINE_READLINE_H
117 rl_bind_key('\t', rl_insert);
121 * FIXME: Get destination from command line, if possible?
123 sockfd = fr_domain_socket(file);
133 #ifndef HAVE_READLINE_READLINE_H
134 fprintf(stdout, "radmin> ");
137 line = fgets(buffer, sizeof(buffer), stdin);
140 p = strchr(buffer, '\n');
142 fprintf(stderr, "line too long\n");
148 line = readline("radmin> ");
161 * Write the text to the socket.
163 if (write(sockfd, line, strlen(line)) < 0) break;
164 if (write(sockfd, "\r\n", 2) < 0) break;
169 if ((strcmp(line, "exit") == 0) ||
170 (strcmp(line, "quit") == 0)) {
181 memset(buffer, 0, sizeof(buffer));
188 FD_SET(sockfd, &readfds);
190 rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL);
192 if (errno == EINTR) continue;
194 fprintf(stderr, "Failed selecting: %s\n",
199 len = recv(sockfd, buffer + size,
200 sizeof(buffer) - size - 1, MSG_DONTWAIT);
203 * No data: keep looping
205 if ((errno == EAGAIN) || (errno == EINTR)) {
209 fprintf(stderr, "Error reading socket: %s\n",
213 if (len == 0) break; /* clean close of socket */
219 * There really has to be a better way of
222 p = strstr(buffer, "radmin> ");
229 if (p[-1] == '\n') p[-1] = '\0';
237 * Blank prompt. Go get another line.
239 if (!buffer[0]) continue;
241 buffer[size] = '\0'; /* this is at least right */