]> code.citadel.org Git - citadel.git/blobdiff - webcit/setup.c
* setup now deletes old inittab entries if they were present
[citadel.git] / webcit / setup.c
index 5d08633b66b21cd9ecd23db0c8fa7f50b7b43258..4984e34e8d2c31db14cabafc2f6977ae77670ee2 100644 (file)
@@ -7,23 +7,10 @@
  *
  */
 
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <fcntl.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 <limits.h>
-#include <pwd.h>
-
+#include "config.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
  */
-void set_init_entry(char *which_entry, char *new_state) {
+void delete_init_entry(char *which_entry)
+{
        char *inittab = NULL;
        FILE *fp;
        char buf[SIZ];
@@ -61,15 +49,13 @@ 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);
-                               sprintf(buf, "%s:%s:%s:%s",
-                                       entry, levels, state, prog);
+                               buf[0] = 0;     /* delete it */
                        }
                }
 
@@ -95,20 +81,23 @@ void set_init_entry(char *which_entry, char *new_state) {
 
 
 /* 
- * Shut down the Citadel service if necessary, during setup.
+ * Remove any /etc/inittab entries for webcit, because we don't
+ * start it that way anymore.
  */
-void shutdown_service(void) {
+void delete_the_old_way(void) {
        FILE *infp;
-       char buf[SIZ];
-       char looking_for[SIZ];
+       char buf[1024];
+       char looking_for[1024];
        int have_entry = 0;
-       char entry[SIZ];
-       char prog[SIZ];
+       char entry[1024];
+       char prog[1024];
+       char init_entry[1024];
+
 
        strcpy(init_entry, "");
 
        /* Determine the fully qualified path name of webserver */
-       snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
+       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 webserver.
@@ -119,8 +108,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;
@@ -133,17 +122,7 @@ void shutdown_service(void) {
        /* Bail out if there's nothing to do. */
        if (!have_entry) return;
 
-       set_init_entry(init_entry, "off");
-}
-
-
-/*
- * Start the Citadel service.
- */
-void start_the_service(void) {
-       if (strlen(init_entry) > 0) {
-               set_init_entry(init_entry, "respawn");
-       }
+       delete_init_entry(init_entry);
 }
 
 
@@ -175,8 +154,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];
 
@@ -194,12 +173,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");
@@ -230,7 +222,9 @@ void set_value(char *prompt, char str[])
        int i;
 #endif
        char buf[SIZ];
+       char dialog_result[PATH_MAX];
        char setupmsg[SIZ];
+       FILE *fp;
 
        strcpy(setupmsg, "");
 
@@ -245,16 +239,36 @@ void set_value(char *prompt, char str[])
                if (strlen(buf) != 0)
                        strcpy(str, buf);
                break;
+
+       case UI_DIALOG:
+               CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
+               sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
+                       getenv("CTDL_DIALOG"),
+                       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,
+               newtFormAddComponent(form, newtEntry(1, 8, str, 74, (const char **) &result,
                                        NEWT_FLAG_RETURNEXIT));
                newtRunForm(form);
                strcpy(str, result);
@@ -284,12 +298,19 @@ void important_message(char *title, char *msgtext)
                fgets(buf, sizeof buf, stdin);
                break;
 
+       case UI_DIALOG:
+               sprintf(buf, "exec %s --msgbox '%s' 19 72",
+                       getenv("CTDL_DIALOG"),
+                       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"));
@@ -322,6 +343,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) {
 
@@ -347,6 +370,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) {
@@ -376,33 +426,26 @@ void progress(char *text, long int curr, long int cmax)
 
 
 /*
- * check_inittab_entry()  -- Make sure "webserver" is in /etc/inittab
+ * install_init_scripts()  -- Make sure "webserver" is in /etc/inittab
  *
  */
-void check_inittab_entry(void)
+void install_init_scripts(void)
 {
-       FILE *infp;
-       char buf[SIZ];
-       char looking_for[SIZ];
        char question[SIZ];
-       char entryname[5];
-       char listenport[128];
+       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 webserver */
-       snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
-
-       /* If there's already an entry, then we have nothing left to do. */
-       if (strlen(init_entry) > 0) {
-               return;
-       }
+       FILE *fp;
 
        /* Otherwise, prompt the user to create an entry. */
        snprintf(question, sizeof question,
-               "There is no '%s' entry in /etc/inittab.\n"
-               "Would you like to add one?",
-               looking_for);
+               "Would you like to automatically start WebCit at boot?"
+       );
        if (yesno(question) == 0)
                return;
 
@@ -411,8 +454,20 @@ void check_inittab_entry(void)
                "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(listenport, "2000");
-       set_value(question, listenport);
+       sprintf(http_port, "80");
+       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) ) {
@@ -437,35 +492,45 @@ void check_inittab_entry(void)
                }
        }
 
-       /* Generate a unique entry name for /etc/inittab */
-       snprintf(entryname, sizeof entryname, "c0");
-       do {
-               ++entryname[1];
-               if (entryname[1] > '9') {
-                       entryname[1] = 0;
-                       ++entryname[0];
-                       if (entryname[0] > 'z') {
-                               display_error(
-                                  "Can't generate a unique entry name");
-                               return;
-                       }
-               }
-               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");
-       if (infp == NULL) {
-               display_error(strerror(errno));
-       } else {
-               fprintf(infp, "# Start the WebCit server...\n");
-               fprintf(infp, "%s:2345:respawn:%s -p%s %s %s\n",
-                       entryname, looking_for,
-                       listenport, hostname, portname);
-               fclose(infp);
-               strcpy(init_entry, entryname);
+       fp = fopen("/etc/init.d/webcit", "w");
+       if (fp == NULL) {
+               display_error("Cannot create /etc/init.d/webcit");
+               return;
        }
+
+       fprintf(fp,     "#!/bin/sh\n"
+                       "\n"
+                       "DAEMON=%s/ctdlsvc\n", setup_directory);
+       fprintf(fp,     "\n"
+                       "test -x $DAEMON || exit 0\n"
+                       "test -d /var/run || exit 0\n"
+                       "\n"
+                       "case \"$1\" in\n"
+                       "\n"
+                       "start)         echo -n \"Starting WebCit... \"\n"
+                       "               if $DAEMON /var/run/webcit.pid %s/webserver "
+                                                       "-p%s %s %s\n",
+                                                       setup_directory, http_port, hostname, portname);
+       fprintf(fp,     "               then\n"
+                       "                       echo \"ok\"\n"
+                       "               else\n"
+                       "                       echo \"failed\"\n"
+                       "               fi\n");
+       fprintf(fp,     "               ;;\n"
+                       "stop)          echo -n \"Stopping WebCit... \"\n"
+                       "               kill `cat /var/run/webcit.pid`\n"
+                       "               rm -f /var/run/webcit.pid\n"
+                       "               echo \"ok\"\n"
+                       "               ;;\n"
+                       "*)             echo \"Usage: $0 {start|stop}\"\n"
+                       "               exit 1\n"
+                       "               ;;\n"
+                       "esac\n"
+       );
+
+       fclose(fp);
+       chmod("/etc/init.d/webcit", 0755);
 }
 
 
@@ -477,6 +542,11 @@ void check_inittab_entry(void)
 int discover_ui(void)
 {
 
+       /* Use "dialog" if we have it */
+       if (getenv("CTDL_DIALOG") != NULL) {
+               return UI_DIALOG;
+       }
+               
 #ifdef HAVE_NEWT
        newtInit();
        newtCls();
@@ -493,8 +563,9 @@ int discover_ui(void)
 int main(int argc, char *argv[])
 {
        int a;
-       char aaa[128];
+       char aaa[256];
        int info_only = 0;
+       strcpy(suggested_url, "http://<your_host_name>:<port>/");
 
        /* set an invalid setup type */
        setup_type = (-1);
@@ -532,7 +603,7 @@ int main(int argc, char *argv[])
        }
 
        /* Get started in a valid setup directory. */
-       strcpy(setup_directory, WEBCITDIR);
+       strcpy(setup_directory, PREFIX);
        if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
                strcpy(setup_directory, getenv("WEBCIT"));
        }
@@ -546,12 +617,13 @@ int main(int argc, char *argv[])
                cleanup(errno);
        }
 
-       /* 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);
-       }
+       /*
+        * We used to start WebCit by putting it directly into /etc/inittab.
+        * Since some systems are moving away from init, we can't do this anymore.
+        */
+       progress("Removing obsolete /etc/inittab entries...", 0, 1);
+       delete_the_old_way();
+       progress("Removing obsolete /etc/inittab entries...", 1, 1);
 
        /* Now begin. */
        switch (setup_type) {
@@ -563,22 +635,29 @@ int main(int argc, char *argv[])
 
        }
 
-       check_inittab_entry();  /* Check /etc/inittab */
+       /* 
+        * If we're running on SysV, install init scripts.
+        */
+       if (!access("/var/run", W_OK)) {
+               install_init_scripts();
+       }
 
-       /* See if we can start the WebCit service. */
-       if (strlen(init_entry) > 0) {
-               for (a=0; a<=3; ++a) {
-                       progress("Starting the WebCit service...", a, 3);
-                       if (a == 0) start_the_service();
-                       sleep(1);
-               }
-               important_message("Setup finished",
-                       "Setup is finished.  You may now log in.");
+       for (a=0; a<=3; ++a) {
+               progress("FIXME Starting the WebCit service...", a, 3);
+               /* if (a == 0) start_the_service(); */
+               sleep(1);
        }
-       else {
+
+       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",
                        "Setup is finished.  You may now start the server.");
-       }
+       } */
 
        cleanup(0);
        return 0;