]> code.citadel.org Git - citadel.git/blobdiff - webcit/setup.c
* buildpackages can create deb source tarballs now.
[citadel.git] / webcit / setup.c
index f17e7ac01f0993571195be9097104ae55fd566a9..8022ca0d271839995f307110f200d5d1b497d0f2 100644 (file)
@@ -7,31 +7,25 @@
  *
  */
 
-
+#include "config.h"
 #include "webcit.h"
 #include "webserver.h"
 
 
-#ifdef HAVE_NEWT
-#include <newt.h>
-#endif
-
-
 #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
+ * Delete an entry from /etc/inittab
  */
-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];
@@ -55,9 +49,7 @@ void set_init_entry(char *which_entry, char *new_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);
+                               strcpy(state, "off");   /* disable it */
                        }
                }
 
@@ -83,15 +75,18 @@ 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, "");
 
@@ -121,28 +116,13 @@ 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);
 }
 
 
 
 void cleanup(int exitcode)
 {
-#ifdef HAVE_NEWT
-       newtCls();
-       newtRefresh();
-       newtFinished();
-#endif
        exit(exitcode);
 }
 
@@ -159,11 +139,6 @@ void title(char *text)
 
 int yesno(char *question)
 {
-#ifdef HAVE_NEWT
-       newtComponent form = NULL;
-       newtComponent yesbutton = NULL;
-       newtComponent nobutton = NULL;
-#endif
        int i = 0;
        int answer = 0;
        char buf[SIZ];
@@ -195,41 +170,12 @@ int yesno(char *question)
                }
                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', sizeof buf);
-                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
-               }
-               yesbutton = newtButton(10, 5, "Yes");
-               nobutton = newtButton(60, 5, "No");
-               newtFormAddComponent(form, yesbutton);
-               newtFormAddComponent(form, nobutton);
-               if (newtRunForm(form) == yesbutton) {
-                       answer = 1;
-               }
-               else {
-                       answer = 0;
-               }
-               newtPopWindow();
-               newtFormDestroy(form);  
-
-               break;
-#endif
-
        }
        return (answer);
 }
 
 void set_value(char *prompt, char str[])
 {
-#ifdef HAVE_NEWT
-       newtComponent form;
-       char *result;
-       int i;
-#endif
        char buf[SIZ];
        char dialog_result[PATH_MAX];
        char setupmsg[SIZ];
@@ -268,34 +214,12 @@ void set_value(char *prompt, char str[])
                }
                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', sizeof buf);
-                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
-               }
-               newtFormAddComponent(form, newtEntry(1, 8, str, 74, (const char **) &result,
-                                       NEWT_FLAG_RETURNEXIT));
-               newtRunForm(form);
-               strcpy(str, result);
-
-               newtPopWindow();
-               newtFormDestroy(form);  
-
-#endif
        }
 }
 
 
 void important_message(char *title, char *msgtext)
 {
-#ifdef HAVE_NEWT
-       newtComponent form = NULL;
-       int i = 0;
-#endif
        char buf[SIZ];
 
        switch (setup_type) {
@@ -313,22 +237,6 @@ void important_message(char *title, char *msgtext)
                        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', sizeof buf);
-                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
-               }
-               newtFormAddComponent(form, newtButton(35, 5, "OK"));
-               newtRunForm(form);
-               newtPopWindow();
-               newtFormDestroy(form);  
-               break;
-#endif
-
        }
 }
 
@@ -340,16 +248,6 @@ void display_error(char *error_message)
 
 void progress(char *text, long int curr, long int cmax)
 {
-#ifdef HAVE_NEWT
-
-       /* These variables are static because progress() gets called
-        * multiple times during the course of whatever operation is
-        * being performed.  This makes setup non-threadsafe, but who
-        * cares?
-        */
-       static newtComponent form = NULL;
-       static newtComponent scale = NULL;
-#endif
        static long dots_printed = 0L;
        long a = 0;
        char buf[SIZ];
@@ -405,29 +303,6 @@ void progress(char *text, long int curr, long int cmax)
                        }
                }
                break;
-
-#ifdef HAVE_NEWT
-       case UI_NEWT:
-               if (curr == 0) {
-                       newtCenteredWindow(76, 8, text);
-                       form = newtForm(NULL, NULL, 0);
-                       scale = newtScale(1, 3, 74, cmax);
-                       newtFormAddComponent(form, scale);
-                       newtDrawForm(form);
-                       newtRefresh();
-               }
-               if ((curr > 0) && (curr <= cmax)) {
-                       newtScaleSet(scale, curr);
-                       newtRefresh();
-               }
-               if (curr == cmax) {
-                       newtFormDestroy(form);  
-                       newtPopWindow();
-                       newtRefresh();
-               }
-               break;
-#endif
-
        }
 }
 
@@ -435,16 +310,12 @@ 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 http_port[128];
 #ifdef HAVE_OPENSSL
        char https_port[128];
@@ -453,19 +324,12 @@ void check_inittab_entry(void)
        char portname[128];
        struct utsname my_utsname;
 
-       /* 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) {
-               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;
 
@@ -512,41 +376,86 @@ void check_inittab_entry(void)
                }
        }
 
-       /* Generate unique entry names 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, "h%s:2345:respawn:%s -p%s %s %s\n",
-                       entryname, looking_for,
-                       http_port, hostname, portname);
+       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"
+                       "WEBCIT_DIR=%s\n", setup_directory);
+       fprintf(fp,     "HTTP_PORT=%s\n", http_port);
 #ifdef HAVE_OPENSSL
-               fprintf(infp, "s%s:2345:respawn:%s -p%s -s %s %s\n",
-                       entryname, looking_for,
-                       https_port, hostname, portname);
+       fprintf(fp,     "HTTPS_PORT=%s\n", https_port);
 #endif
-               fclose(infp);
-               strcpy(init_entry, entryname);
-       }
+       fprintf(fp,     "CTDL_HOSTNAME=%s\n", hostname);
+       fprintf(fp,     "CTDL_PORTNAME=%s\n", portname);
+       fprintf(fp,     "\n"
+                       "test -d /var/run || exit 0\n"
+                       "\n"
+                       "case \"$1\" in\n"
+                       "\n"
+                       "start)         echo -n \"Starting WebCit... \"\n"
+                       "               if   $WEBCIT_DIR/webserver "
+                                                       "-d/var/run/webcit.pid "
+                                                       "-t/dev/null "
+                                                       "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
+                       "               then\n"
+                       "                       echo \"ok\"\n"
+                       "               else\n"
+                       "                       echo \"failed\"\n"
+                       "               fi\n");
+#ifdef HAVE_OPENSSL
+       fprintf(fp,     "               echo -n \"Starting WebCit SSL... \"\n"
+                       "               if  $WEBCIT_DIR/webserver "
+                                                       "-d/var/run/webcit-ssl.pid "
+                                                       "-t/dev/null "
+                                                       "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
+                       "               then\n"
+                       "                       echo \"ok\"\n"
+                       "               else\n"
+                       "                       echo \"failed\"\n"
+                       "               fi\n");
+#endif
+       fprintf(fp,     "               ;;\n"
+                       "stop)          echo -n \"Stopping WebCit... \"\n"
+                       "               if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
+                       "               then\n"
+                       "                       echo \"ok\"\n"
+                       "               else\n"
+                       "                       echo \"failed\"\n"
+                       "               fi\n"
+                       "               rm -f /var/run/webcit.pid 2>/dev/null\n");
+#ifdef HAVE_OPENSSL
+       fprintf(fp,     "               echo -n \"Stopping WebCit SSL... \"\n"
+                       "               if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
+                       "               then\n"
+                       "                       echo \"ok\"\n"
+                       "               else\n"
+                       "                       echo \"failed\"\n"
+                       "               fi\n"
+                       "               rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
+#endif
+       fprintf(fp,     "               ;;\n"
+                       "restart)       $0 stop\n"
+                       "               $0 start\n"
+                       "               ;;\n"
+                       "*)             echo \"Usage: $0 {start|stop|restart}\"\n"
+                       "               exit 1\n"
+                       "               ;;\n"
+                       "esac\n"
+       );
+
+       fclose(fp);
+       chmod("/etc/init.d/webcit", 0755);
+
+       /* Set up the run levels. */
+       system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
+       system("for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/S84webcit ; done 2>/dev/null");
+       system("for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/K15webcit ; done 2>/dev/null");
+
 }
 
 
@@ -563,12 +472,6 @@ int discover_ui(void)
                return UI_DIALOG;
        }
                
-#ifdef HAVE_NEWT
-       newtInit();
-       newtCls();
-       newtDrawRootText(0, 0, "WebCit Setup");
-       return UI_NEWT;
-#endif
        return UI_TEXT;
 }
 
@@ -618,17 +521,8 @@ int main(int argc, char *argv[])
                cleanup(0);
        }
 
-       /* If we're on something BSDish then we don't have inittab */
-       if (access("/etc/inittab", F_OK)) {
-               important_message("Not running SysV style init",
-                               "WebCit Setup can only run on systems that use /etc/inittab.\n"
-                               "Please manually configure your startup scripts to run WebCit\n"
-                               "when the system is booted.\n");
-               cleanup(0);
-       }
-
        /* 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"));
        }
@@ -642,12 +536,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) {
@@ -659,20 +554,24 @@ 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);
+               if (!access("/etc/init.d/webcit", X_OK)) {
+                       system("/etc/init.d/webcit stop");
+                       system("/etc/init.d/webcit start");
                }
+
                sprintf(aaa,
                        "Setup is finished.  You may now log in.\n"
-                       "Point your web browser at %s\n", suggested_url);
+                       "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.");