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>
29 #ifdef HAVE_READLINE_READLINE_H
30 #include <readline/readline.h>
31 #include <readline/history.h>
35 * FIXME: configure checks.
37 #include <sys/types.h>
38 #include <sys/socket.h>
42 static int fr_domain_socket(const char *path)
47 struct sockaddr_un saremote;
50 if (len >= sizeof(saremote.sun_path)) {
51 fprintf(stderr, "Path too long in filename\n");
55 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
56 fprintf(stderr, "Failed creating socket: %s\n",
61 saremote.sun_family = AF_UNIX;
62 memcpy(saremote.sun_path, path, len); /* not zero terminated */
64 socklen = sizeof(saremote.sun_family) + len;
66 if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
67 fprintf(stderr, "Failed connecting to %s: %s\n",
68 path, strerror(errno));
77 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
78 fprintf(stderr, "Failure getting socket flags: %s",
85 if( fcntl(sockfd, F_SETFL, flags) < 0) {
86 fprintf(stderr, "Failure setting socket flags: %s",
97 int main(int argc, char **argv)
102 char *p, buffer[2048];
105 fprintf(stderr, "Figure it out, einstein.\n");
109 #ifdef HAVE_READLINE_READLINE_H
111 rl_bind_key('\t', rl_insert);
115 * FIXME: Get destination from command line, if possible?
117 sockfd = fr_domain_socket(argv[1]);
127 #ifndef HAVE_READLINE_READLINE_H
128 fprintf(stdout, "radmin> ");
131 line = fgets(buffer, sizeof(buffer), stdin);
134 p = strchr(buffer, '\n');
136 fprintf(stderr, "line too long\n");
142 line = readline("radmin> ");
155 * Write the text to the socket.
157 if (write(sockfd, line, strlen(line)) < 0) break;
158 if (write(sockfd, "\r\n", 2) < 0) break;
163 if ((strcmp(line, "exit") == 0) ||
164 (strcmp(line, "quit") == 0)) {
175 memset(buffer, 0, sizeof(buffer));
178 len = recv(sockfd, buffer + size,
179 sizeof(buffer) - size - 1, MSG_DONTWAIT);
182 * No data: keep looping
184 if ((errno == EAGAIN) || (errno == EINTR)) {
188 fprintf(stderr, "Error reading socket: %s\n",
192 if (len == 0) break; /* clean close of socket */
198 * There really has to be a better way of
201 p = strstr(buffer, "radmin> ");
208 if (p[-1] == '\n') p[-1] = '\0';
216 * Blank prompt. Go get another line.
218 if (!buffer[0]) continue;
220 buffer[size] = '\0'; /* this is at least right */