Signed / unsigned fixes and function prototypes
[freeradius.git] / src / main / radmin.c
index 4354de6..d67f50d 100644 (file)
@@ -33,42 +33,46 @@ RCSID("$Id$")
 
 #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";
 
 /*
@@ -86,6 +90,9 @@ int radius_xlat(UNUSED char *out, UNUSED int outlen, UNUSED const char *fmt,
        return -1;
 }
 
+static FILE *outputfp = NULL;
+static int echo = FALSE;
+
 static int fr_domain_socket(const char *path)
 {
        int sockfd = -1;
@@ -107,14 +114,26 @@ static int fr_domain_socket(const char *path)
         }
 
         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;
         }
 
@@ -147,6 +166,7 @@ static int usage(void)
        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");
@@ -161,7 +181,10 @@ static ssize_t run_command(int sockfd, const char *command,
 {
        char *p;
        ssize_t size, len;
-       int flag = 1;
+
+       if (echo) {
+               fprintf(outputfp, "%s\n", command);
+       }
 
        /*
         *      Write the text to the socket.
@@ -177,7 +200,7 @@ static ssize_t run_command(int sockfd, const char *command,
 
        memset(buffer, 0, bufsize);
 
-       while (flag == 1) {
+       while (1) {
                int rcode;
                fd_set readfds;
 
@@ -230,8 +253,6 @@ static ssize_t run_command(int sockfd, const char *command,
                        *p = '\0';
 
                        if (p[-1] == '\n') p[-1] = '\0';
-
-                       flag = 0;
                        break;
                }
        }
@@ -246,6 +267,7 @@ static ssize_t run_command(int sockfd, const char *command,
        return 2;
 }
 
+#define MAX_COMMANDS (4)
 
 int main(int argc, char **argv)
 {
@@ -261,14 +283,18 @@ 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) {
@@ -279,7 +305,17 @@ int main(int argc, char **argv)
                        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':
@@ -393,9 +429,11 @@ int main(int argc, char **argv)
         */
        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
@@ -439,13 +477,20 @@ int main(int argc, char **argv)
        /*
         *      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);
        }
 
@@ -465,7 +510,7 @@ int main(int argc, char **argv)
         */
 
        while (1) {
-#ifndef HAVE_LIBREADLINE
+#ifndef USE_READLINE
                if (!quiet) {
                        printf("radmin> ");
                        fflush(stdout);
@@ -481,13 +526,15 @@ int main(int argc, char **argv)
                                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",
@@ -497,6 +544,9 @@ int main(int argc, char **argv)
                        
                        *p = '\0';
 
+                       /*
+                        *      Strip off leading spaces.
+                        */
                        for (p = line; *p != '\0'; p++) {
                                if ((p[0] == ' ') ||
                                    (p[0] == '\t')) {
@@ -508,12 +558,25 @@ int main(int argc, char **argv)
                                        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) {
@@ -537,6 +600,7 @@ int main(int argc, char **argv)
 
                fputs(buffer, outputfp);
                fflush(outputfp);
+               fprintf(outputfp, "\n");
        }
 
        fprintf(outputfp, "\n");