]> code.citadel.org Git - citadel.git/blobdiff - webcit/setup.c
* Header file adjustments to make it work on FreeBSD
[citadel.git] / webcit / setup.c
index ebf91c88c422d23bf1088d8d9e5efddbe0b81be1..f392011122142b9e2e29a90ad90bcf19f1848a82 100644 (file)
@@ -2,26 +2,44 @@
  * $Id$
  *
  * WebCit setup utility
+ * 
+ * (This is basically just an install wizard.  It's not required.)
  *
  */
 
+
+#include <ctype.h>
 #include <stdlib.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
+#endif
+#include <signal.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/utsname.h>
 #include <sys/wait.h>
-#include <signal.h>
-#include <netdb.h>
-#include <errno.h>
+#include <sys/socket.h>
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#ifdef HAVE_LIMITS_H
 #include <limits.h>
+#endif
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <string.h>
 #include <pwd.h>
-
+#include <errno.h>
+#include <stdarg.h>
+#include <pthread.h>
+#include <signal.h>
+#include <sys/utsname.h>
 #include "webcit.h"
+#include "webserver.h"
+
 
 #ifdef HAVE_NEWT
 #include <newt.h>
 
 
 #define UI_TEXT                0       /* Default setup type -- text only */
+#define UI_DIALOG      2       /* Use the 'dialog' program */
 #define UI_SILENT      3       /* Silent running, for use in scripts */
 #define UI_NEWT                4       /* Use the "newt" window library */
 
 int setup_type;
 char setup_directory[SIZ];
 char init_entry[SIZ];
-
+int using_web_installer = 0;
+char suggested_url[SIZ];
 
 /*
  * Set an entry in inittab to the desired state
@@ -58,10 +78,10 @@ void set_init_entry(char *which_entry, char *new_state) {
        while(fgets(buf, sizeof buf, fp) != NULL) {
 
                if (num_tokens(buf, ':') == 4) {
-                       extract_token(entry, buf, 0, ':');
-                       extract_token(levels, buf, 1, ':');
-                       extract_token(state, buf, 2, ':');
-                       extract_token(prog, buf, 3, ':'); /* includes 0x0a LF */
+                       extract_token(entry, buf, 0, ':', sizeof entry);
+                       extract_token(levels, buf, 1, ':', sizeof levels);
+                       extract_token(state, buf, 2, ':', sizeof state);
+                       extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
 
                        if (!strcmp(entry, which_entry)) {
                                strcpy(state, new_state);
@@ -104,11 +124,11 @@ void shutdown_service(void) {
 
        strcpy(init_entry, "");
 
-       /* Determine the fully qualified path name of citserver */
-       snprintf(looking_for, sizeof looking_for, "%s/citserver ", WEBCITDIR);
+       /* Determine the fully qualified path name of webserver */
+       snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
 
        /* Pound through /etc/inittab line by line.  Set have_entry to 1 if
-        * an entry is found which we believe starts citserver.
+        * an entry is found which we believe starts webserver.
         */
        infp = fopen("/etc/inittab", "r");
        if (infp == NULL) {
@@ -116,8 +136,8 @@ void shutdown_service(void) {
        } else {
                while (fgets(buf, sizeof buf, infp) != NULL) {
                        buf[strlen(buf) - 1] = 0;
-                       extract_token(entry, buf, 0, ':');      
-                       extract_token(prog, buf, 3, ':');
+                       extract_token(entry, buf, 0, ':', sizeof entry);
+                       extract_token(prog, buf, 3, ':', sizeof prog);
                        if (!strncasecmp(prog, looking_for,
                           strlen(looking_for))) {
                                ++have_entry;
@@ -172,8 +192,8 @@ int yesno(char *question)
        newtComponent form = NULL;
        newtComponent yesbutton = NULL;
        newtComponent nobutton = NULL;
-       int i = 0;
 #endif
+       int i = 0;
        int answer = 0;
        char buf[SIZ];
 
@@ -191,12 +211,25 @@ int yesno(char *question)
                } while ((answer < 0) || (answer > 1));
                break;
 
+       case UI_DIALOG:
+               sprintf(buf, "exec %s --yesno '%s' 10 72",
+                       getenv("CTDL_DIALOG"),
+                       question);
+               i = system(buf);
+               if (i == 0) {
+                       answer = 1;
+               }
+               else {
+                       answer = 0;
+               }
+               break;
+
 #ifdef HAVE_NEWT
        case UI_NEWT:
                newtCenteredWindow(76, 10, "Question");
                form = newtForm(NULL, NULL, 0);
                for (i=0; i<num_tokens(question, '\n'); ++i) {
-                       extract_token(buf, question, i, '\n');
+                       extract_token(buf, question, i, '\n', sizeof buf);
                        newtFormAddComponent(form, newtLabel(1, 1+i, buf));
                }
                yesbutton = newtButton(10, 5, "Yes");
@@ -227,10 +260,10 @@ void set_value(char *prompt, char str[])
        int i;
 #endif
        char buf[SIZ];
-       char tempfile[PATH_MAX];
+       char *dialog_result;
        char setupmsg[SIZ];
+       FILE *fp;
 
-       strcpy(tempfile, tmpnam(NULL));
        strcpy(setupmsg, "");
 
        switch (setup_type) {
@@ -244,13 +277,34 @@ void set_value(char *prompt, char str[])
                if (strlen(buf) != 0)
                        strcpy(str, buf);
                break;
+
+       case UI_DIALOG:
+               dialog_result = tmpnam(NULL);
+               sprintf(buf, "exec %s --backtitle '%s' --inputbox '%s' 19 72 '%s' 2>%s",
+                       getenv("CTDL_DIALOG"),
+                       "WebCit setup",
+                       prompt,
+                       str,
+                       dialog_result);
+               system(buf);
+               fp = fopen(dialog_result, "r");
+               if (fp != NULL) {
+                       fgets(str, sizeof buf, fp);
+                       if (str[strlen(str)-1] == 10) {
+                               str[strlen(str)-1] = 0;
+                       }
+                       fclose(fp);
+                       unlink(dialog_result);
+               }
+               break;
+
 #ifdef HAVE_NEWT
        case UI_NEWT:
 
                newtCenteredWindow(76, 10, "WebCit setup");
                form = newtForm(NULL, NULL, 0);
                for (i=0; i<num_tokens(prompt, '\n'); ++i) {
-                       extract_token(buf, prompt, i, '\n');
+                       extract_token(buf, prompt, i, '\n', sizeof buf);
                        newtFormAddComponent(form, newtLabel(1, 1+i, buf));
                }
                newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
@@ -283,12 +337,20 @@ void important_message(char *title, char *msgtext)
                fgets(buf, sizeof buf, stdin);
                break;
 
+       case UI_DIALOG:
+               sprintf(buf, "exec %s --backtitle '%s' --msgbox '%s' 19 72",
+                       getenv("CTDL_DIALOG"),
+                       title,
+                       msgtext);
+               system(buf);
+               break;
+
 #ifdef HAVE_NEWT
        case UI_NEWT:
                newtCenteredWindow(76, 10, title);
                form = newtForm(NULL, NULL, 0);
                for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
-                       extract_token(buf, msgtext, i, '\n');
+                       extract_token(buf, msgtext, i, '\n', sizeof buf);
                        newtFormAddComponent(form, newtLabel(1, 1+i, buf));
                }
                newtFormAddComponent(form, newtButton(35, 5, "OK"));
@@ -321,6 +383,8 @@ void progress(char *text, long int curr, long int cmax)
 #endif
        static long dots_printed = 0L;
        long a = 0;
+       char buf[SIZ];
+       static FILE *fp = NULL;
 
        switch (setup_type) {
 
@@ -346,6 +410,33 @@ void progress(char *text, long int curr, long int cmax)
                }
                break;
 
+       case UI_DIALOG:
+               if (curr == 0) {
+                       sprintf(buf, "exec %s --gauge '%s' 7 72 0",
+                               getenv("CTDL_DIALOG"),
+                               text);
+                       fp = popen(buf, "w");
+                       if (fp != NULL) {
+                               fprintf(fp, "0\n");
+                               fflush(fp);
+                       }
+               } 
+               else if (curr == cmax) {
+                       if (fp != NULL) {
+                               fprintf(fp, "100\n");
+                               pclose(fp);
+                               fp = NULL;
+                       }
+               }
+               else {
+                       a = (curr * 100) / cmax;
+                       if (fp != NULL) {
+                               fprintf(fp, "%ld\n", a);
+                               fflush(fp);
+                       }
+               }
+               break;
+
 #ifdef HAVE_NEWT
        case UI_NEWT:
                if (curr == 0) {
@@ -383,11 +474,18 @@ void check_inittab_entry(void)
        FILE *infp;
        char buf[SIZ];
        char looking_for[SIZ];
-       char question[128];
+       char question[SIZ];
        char entryname[5];
+       char http_port[128];
+#ifdef HAVE_OPENSSL
+       char https_port[128];
+#endif
+       char hostname[128];
+       char portname[128];
+       struct utsname my_utsname;
 
-       /* Determine the fully qualified path name of citserver */
-       snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
+       /* Determine the fully qualified path name of webserver */
+       snprintf(looking_for, sizeof looking_for, "%s/webserver", setup_directory);
 
        /* If there's already an entry, then we have nothing left to do. */
        if (strlen(init_entry) > 0) {
@@ -402,7 +500,50 @@ void check_inittab_entry(void)
        if (yesno(question) == 0)
                return;
 
-       /* Generate a unique entry name for /etc/inittab */
+       snprintf(question, sizeof question,
+               "On which port do you want WebCit to listen for HTTP "
+               "requests?\n\nYou can use the standard port (80) if you are "
+               "not running another\nweb server (such as Apache), otherwise "
+               "select another port.");
+       sprintf(http_port, "2000");
+       set_value(question, http_port);
+       uname(&my_utsname);
+       sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
+
+#ifdef HAVE_OPENSSL
+       snprintf(question, sizeof question,
+               "On which port do you want WebCit to listen for HTTPS "
+               "requests?\n\nYou can use the standard port (443) if you are "
+               "not running another\nweb server (such as Apache), otherwise "
+               "select another port.");
+       sprintf(https_port, "443");
+       set_value(question, https_port);
+#endif
+
+       /* Find out where Citadel is. */
+       if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
+               strcpy(hostname, "uds");
+               strcpy(portname, getenv("CITADEL"));
+       }
+       else {
+               snprintf(question, sizeof question,
+                       "Is the Citadel service running on the same host as WebCit?");
+               if (yesno(question)) {
+                       sprintf(hostname, "uds");
+                       sprintf(portname, "/usr/local/citadel");
+                       set_value("In what directory is Citadel installed?", portname);
+               }
+               else {
+                       sprintf(hostname, "127.0.0.1");
+                       sprintf(portname, "504");
+                       set_value("Enter the host name or IP address of your "
+                               "Citadel server.", hostname);
+                       set_value("Enter the port number on which Citadel is "
+                               "running (usually 504)", portname);
+               }
+       }
+
+       /* Generate unique entry names for /etc/inittab */
        snprintf(entryname, sizeof entryname, "c0");
        do {
                ++entryname[1];
@@ -418,6 +559,7 @@ void check_inittab_entry(void)
                snprintf(buf, sizeof buf,
                     "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
        } while (system(buf) == 0);
+       
 
        /* Now write it out to /etc/inittab */
        infp = fopen("/etc/inittab", "a");
@@ -425,8 +567,14 @@ void check_inittab_entry(void)
                display_error(strerror(errno));
        } else {
                fprintf(infp, "# Start the WebCit server...\n");
-               fprintf(infp, "%s:2345:respawn:%s -h%s\n",
-                       entryname, looking_for, setup_directory);
+               fprintf(infp, "h%s:2345:respawn:%s -p%s %s %s\n",
+                       entryname, looking_for,
+                       http_port, hostname, portname);
+#ifdef HAVE_OPENSSL
+               fprintf(infp, "s%s:2345:respawn:%s -p%s -s %s %s\n",
+                       entryname, looking_for,
+                       https_port, hostname, portname);
+#endif
                fclose(infp);
                strcpy(init_entry, entryname);
        }
@@ -435,24 +583,17 @@ void check_inittab_entry(void)
 
 
 
-/* 
- * Check to see if our server really works.  Returns 0 on success.
- */
-int test_server(void) {
-
-       return 0;               /* FIXME ... stubbed out */
-
-}
-
-
-
-
 /*
  * Figure out what type of user interface we're going to use
  */
 int discover_ui(void)
 {
 
+       /* Use "dialog" if we have it */
+       if (getenv("CTDL_DIALOG") != NULL) {
+               return UI_DIALOG;
+       }
+               
 #ifdef HAVE_NEWT
        newtInit();
        newtCls();
@@ -469,15 +610,18 @@ int discover_ui(void)
 int main(int argc, char *argv[])
 {
        int a;
-       int curr;
-       char aaa[128];
-       FILE *fp;
+       char aaa[256];
        int info_only = 0;
-       gid_t gid;
+       strcpy(suggested_url, "http://<your_host_name>:<port>/");
 
        /* set an invalid setup type */
        setup_type = (-1);
 
+       /* Check to see if we're running the web installer */
+       if (getenv("CITADEL_INSTALLER") != NULL) {
+               using_web_installer = 1;
+       }
+
        /* parse command line args */
        for (a = 0; a < argc; ++a) {
                if (!strncmp(argv[a], "-u", 2)) {
@@ -507,30 +651,26 @@ int main(int argc, char *argv[])
 
        /* Get started in a valid setup directory. */
        strcpy(setup_directory, WEBCITDIR);
-       set_value("FIXME enter your setup directory, dumbass.",
-               setup_directory);
+       if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
+               strcpy(setup_directory, getenv("WEBCIT"));
+       }
+       else {
+               set_value("In what directory is WebCit installed?",
+                       setup_directory);
+       }
        if (chdir(setup_directory) != 0) {
                important_message("WebCit Setup",
                          "The directory you specified does not exist.");
                cleanup(errno);
        }
 
-       /* See if we need to shut down the Citadel service. */
+       /* See if we need to shut down the WebCit service. */
        for (a=0; a<=3; ++a) {
                progress("Shutting down the WebCit service...", a, 3);
                if (a == 0) shutdown_service();
                sleep(1);
        }
 
-       /* Make sure it's stopped. */
-       if (test_server() == 0) {
-               important_message("WebCit Setup",
-                       "The Citadel service is still running.\n"
-                       "Please stop the service manually and run "
-                       "setup again.");
-               cleanup(1);
-       }
-
        /* Now begin. */
        switch (setup_type) {
 
@@ -550,16 +690,10 @@ int main(int argc, char *argv[])
                        if (a == 0) start_the_service();
                        sleep(1);
                }
-               if (test_server() == 0) {
-                       important_message("Setup finished",
-                               "Setup is finished.  You may now log in.");
-               }
-               else {
-                       important_message("Setup finished",
-                               "Setup is finished, but the WebCit service "
-                               "failed to start.\n"
-                               "Go back and check your configuration.");
-               }
+               sprintf(aaa,
+                       "Setup is finished.  You may now log in.\n"
+                       "Point your web browser at %s\n", suggested_url);
+               important_message("Setup finished", aaa);
        }
        else {
                important_message("Setup finished",