#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
+#ifndef SUN_LEN
+#define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
+#endif
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
#ifdef HAVE_LIBREADLINE
+
#if defined(HAVE_READLINE_READLINE_H)
#include <readline/readline.h>
+#define USE_READLINE (1)
#elif defined(HAVE_READLINE_H)
#include <readline.h>
-#else /* !defined(HAVE_READLINE_H) */
-extern char *readline ();
+#define USE_READLINE (1)
#endif /* !defined(HAVE_READLINE_H) */
-char *cmdline = NULL;
-#else /* !defined(HAVE_READLINE_READLINE_H) */
- /* no readline */
+
#endif /* HAVE_LIBREADLINE */
#ifdef HAVE_READLINE_HISTORY
#if defined(HAVE_READLINE_HISTORY_H)
#include <readline/history.h>
+#define USE_READLINE_HISTORY (1)
#elif defined(HAVE_HISTORY_H)
#include <history.h>
-#else /* !defined(HAVE_HISTORY_H) */
-extern void add_history ();
-extern int write_history ();
-extern int read_history ();
+#define USE_READLINE_HISTORY (1)
#endif /* defined(HAVE_READLINE_HISTORY_H) */
- /* no history */
+
#endif /* HAVE_READLINE_HISTORY */
/*
* For configuration file stuff.
*/
-const char *radius_dir = RADDBDIR;
+char *radius_dir = RADDBDIR;
const char *progname = "radmin";
/*
return -1;
}
+static FILE *outputfp = NULL;
+static int echo = FALSE;
+
static int fr_domain_socket(const char *path)
{
int sockfd = -1;
}
saremote.sun_family = AF_UNIX;
- memcpy(saremote.sun_path, path, len); /* not zero terminated */
+ memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
- socklen = sizeof(saremote.sun_family) + len;
+ socklen = SUN_LEN(&saremote);
if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
+ struct stat buf;
+
+ close(sockfd);
fprintf(stderr, "%s: Failed connecting to %s: %s\n",
progname, path, strerror(errno));
- close(sockfd);
+
+ /*
+ * The file doesn't exist. Tell the user how to
+ * fix it.
+ */
+ if ((stat(path, &buf) < 0) &&
+ (errno == ENOENT)) {
+ fprintf(stderr, " Perhaps you need to run the commands:\n\tcd /etc/raddb\n\tln -s sites-available/control-socket sites-enabled/control-socket\n and then re-start the server?\n");
+ }
+
return -1;
}
printf("Usage: %s [ args ]\n", progname);
printf(" -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
printf(" -e command Execute 'command' and then exit.\n");
+ printf(" -E Echo commands as they are being executed.\n");
printf(" -f socket_file Open socket_file directly, without reading radius.conf\n");
printf(" -i input_file Read commands from 'input_file'.\n");
printf(" -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
{
char *p;
ssize_t size, len;
- int flag = 1;
+
+ if (echo) {
+ fprintf(outputfp, "%s\n", command);
+ }
/*
* Write the text to the socket.
memset(buffer, 0, bufsize);
- while (flag == 1) {
+ while (1) {
int rcode;
fd_set readfds;
*p = '\0';
if (p[-1] == '\n') p[-1] = '\0';
-
- flag = 0;
break;
}
}
return 2;
}
+#define MAX_COMMANDS (4)
int main(int argc, char **argv)
{
const char *input_file = NULL;
FILE *inputfp = stdin;
const char *output_file = NULL;
- FILE *outputfp = stdout;
+
+ char *commands[MAX_COMMANDS];
+ int num_commands = -1;
+
+ outputfp = stdout; /* stdout is not a constant value... */
if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
progname = argv[0];
else
progname++;
- while ((argval = getopt(argc, argv, "d:hi:e:f:n:o:q")) != EOF) {
+ while ((argval = getopt(argc, argv, "d:hi:e:Ef:n:o:q")) != EOF) {
switch(argval) {
case 'd':
if (file) {
break;
case 'e':
- line = optarg;
+ num_commands++; /* starts at -1 */
+ if (num_commands >= MAX_COMMANDS) {
+ fprintf(stderr, "%s: Too many '-e'\n",
+ progname);
+ exit(1);
+ }
+ commands[num_commands] = optarg;
+ break;
+
+ case 'E':
+ echo = TRUE;
break;
case 'f':
*/
if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = 1;
-#ifdef HAVE_LIBREADLINE
+#ifdef USE_READLINE
if (!quiet) {
+#ifdef USE_READLINE_HISTORY
using_history();
+#endif
rl_bind_key('\t', rl_insert);
}
#endif
/*
* Run one command.
*/
- if (line) {
- size = run_command(sockfd, line, buffer, sizeof(buffer));
- if (size < 0) exit(1);
- if ((size == 0) || (size == 1)) exit(0);
+ if (num_commands >= 0) {
+ int i;
- fputs(buffer, outputfp);
- fflush(outputfp);
+ for (i = 0; i <= num_commands; i++) {
+ size = run_command(sockfd, commands[i],
+ buffer, sizeof(buffer));
+ if (size < 0) exit(1);
+
+ if (buffer[0]) {
+ fputs(buffer, outputfp);
+ fprintf(outputfp, "\n");
+ fflush(outputfp);
+ }
+ }
exit(0);
}
*/
while (1) {
-#ifndef HAVE_LIBREADLINE
+#ifndef USE_READLINE
if (!quiet) {
printf("radmin> ");
fflush(stdout);
continue;
}
+#ifdef USE_READLINE_HISTORY
add_history(line);
+#endif
} else /* quiet, or no readline */
#endif
{
line = fgets(buffer, sizeof(buffer), inputfp);
if (!line) break;
-
+
p = strchr(buffer, '\n');
if (!p) {
fprintf(stderr, "%s: Input line too long\n",
*p = '\0';
+ /*
+ * Strip off leading spaces.
+ */
for (p = line; *p != '\0'; p++) {
if ((p[0] == ' ') ||
(p[0] == '\t')) {
line = NULL;
break;
}
+
+ break;
}
/*
* Comments: keep going.
*/
if (!line) continue;
+
+ /*
+ * Strip off CR / LF
+ */
+ for (p = line; *p != '\0'; p++) {
+ if ((p[0] == '\r') ||
+ (p[0] == '\n')) {
+ p[0] = '\0';
+ break;
+ }
+ }
}
if (strcmp(line, "reconnect") == 0) {
fputs(buffer, outputfp);
fflush(outputfp);
+ fprintf(outputfp, "\n");
}
fprintf(outputfp, "\n");