* Added a skeleton "setup" program to the tree. It does not work yet.
authorArt Cancro <ajc@citadel.org>
Fri, 22 Aug 2003 00:25:40 +0000 (00:25 +0000)
committerArt Cancro <ajc@citadel.org>
Fri, 22 Aug 2003 00:25:40 +0000 (00:25 +0000)
webcit/ChangeLog
webcit/Makefile.in
webcit/configure.in
webcit/setup.c [new file with mode: 0644]
webcit/tools.c

index d89df0d28b951053b75dc7d7261485e6ae3e91c5..f76deb51e6e31002be56689e684cafdff1199074 100644 (file)
@@ -1,4 +1,7 @@
 $Log$
+Revision 500.21  2003/08/22 00:25:40  ajc
+* Added a skeleton "setup" program to the tree.  It does not work yet.
+
 Revision 500.20  2003/08/20 03:48:55  ajc
 * Added iconbar shortcut to Contacts room
 * Minimum required Citadel is now 6.11
@@ -1568,3 +1571,4 @@ Sun Dec  6 19:50:55 EST 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
 
 1998-12-03 Nathan Bryant <bryant@cs.usm.maine.edu>
        * webserver.c: warning fix
+
index 90f84e421dd06ae30ef2ddbbdeed61f8d40dc4d8..efb52db2b27f4c67e37e1a5d9231ce49c2249618 100644 (file)
@@ -6,12 +6,13 @@ CFLAGS=@CFLAGS@
 DEFS=@DEFS@
 LIBOBJS=@LIBOBJS@
 LIBS=@LIBS@
+SETUP_LIBS=@SETUP_LIBS@
 PTHREAD_DEFS=@PTHREAD_DEFS@
 srcdir=@srcdir@
 
 # End of configuration section
 
-all: webserver
+all: webserver setup
 
 .SUFFIXES: .c .o
 
@@ -21,6 +22,8 @@ clean:
 distclean: clean
        rm -f Makefile config.cache config.log config.status
 
+setup: setup.o tools.o
+       $(CC) setup.o tools.o  $(LIBOBJS) $(LIBS) $(SETUP_LIBS) -o setup
 
 webserver: webserver.o context_loop.o tools.o ical_dezonify.o \
        cookie_conversion.o locate_host.o floors.o summary.o \
index 73aa21989d3c602826cdd00b4c7440c11033ec42..f7720f4e46b2d81648900443b49ade3fd2188521 100644 (file)
@@ -5,6 +5,7 @@ AC_INIT(webserver.c)
 AC_CANONICAL_HOST
 
 AC_ARG_WITH(with_libical, [  --with-libical          use libical calendaring library])
+AC_ARG_WITH(with_newt, [  --with-newt          use newt window library])
 
 dnl Set some system-specific variables which are OK to set before compiler
 dnl checks:
@@ -108,5 +109,22 @@ main() {
        )
 fi
 
+dnl Checks for the newt window library.
+if test "x$with_newt" != xno ; then
+       AC_CHECK_HEADERS(newt.h,
+               [AC_CHECK_LIB(newt, newtInit,
+                       [ok_newt=yes],,
+       )])
+fi
+
+if test "x$ok_newt" = xyes ; then
+       SETUP_LIBS="-lnewt $SETUP_LIBS"
+       AC_DEFINE(HAVE_NEWT)
+fi
+
+
+
+AC_SUBST(SETUP_LIBS)
+
 AC_OUTPUT(Makefile)
 
diff --git a/webcit/setup.c b/webcit/setup.c
new file mode 100644 (file)
index 0000000..ebf91c8
--- /dev/null
@@ -0,0 +1,571 @@
+/*
+ * $Id$
+ *
+ * WebCit setup utility
+ *
+ */
+
+#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 "webcit.h"
+
+#ifdef HAVE_NEWT
+#include <newt.h>
+#endif
+
+
+#define UI_TEXT                0       /* Default setup type -- text only */
+#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];
+
+
+/*
+ * Set an entry in inittab to the desired state
+ */
+void set_init_entry(char *which_entry, char *new_state) {
+       char *inittab = NULL;
+       FILE *fp;
+       char buf[SIZ];
+       char entry[SIZ];
+       char levels[SIZ];
+       char state[SIZ];
+       char prog[SIZ];
+
+       inittab = strdup("");
+       if (inittab == NULL) return;
+
+       fp = fopen("/etc/inittab", "r");
+       if (fp == NULL) return;
+
+       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 */
+
+                       if (!strcmp(entry, which_entry)) {
+                               strcpy(state, new_state);
+                               sprintf(buf, "%s:%s:%s:%s",
+                                       entry, levels, state, prog);
+                       }
+               }
+
+               inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
+               if (inittab == NULL) {
+                       fclose(fp);
+                       return;
+               }
+               
+               strcat(inittab, buf);
+       }
+       fclose(fp);
+       fp = fopen("/etc/inittab", "w");
+       if (fp != NULL) {
+               fwrite(inittab, strlen(inittab), 1, fp);
+               fclose(fp);
+               kill(1, SIGHUP);        /* Tell init to re-read /etc/inittab */
+       }
+       free(inittab);
+}
+
+
+
+
+/* 
+ * Shut down the Citadel service if necessary, during setup.
+ */
+void shutdown_service(void) {
+       FILE *infp;
+       char buf[SIZ];
+       char looking_for[SIZ];
+       int have_entry = 0;
+       char entry[SIZ];
+       char prog[SIZ];
+
+       strcpy(init_entry, "");
+
+       /* Determine the fully qualified path name of citserver */
+       snprintf(looking_for, sizeof looking_for, "%s/citserver ", WEBCITDIR);
+
+       /* Pound through /etc/inittab line by line.  Set have_entry to 1 if
+        * an entry is found which we believe starts citserver.
+        */
+       infp = fopen("/etc/inittab", "r");
+       if (infp == NULL) {
+               return;
+       } else {
+               while (fgets(buf, sizeof buf, infp) != NULL) {
+                       buf[strlen(buf) - 1] = 0;
+                       extract_token(entry, buf, 0, ':');      
+                       extract_token(prog, buf, 3, ':');
+                       if (!strncasecmp(prog, looking_for,
+                          strlen(looking_for))) {
+                               ++have_entry;
+                               strcpy(init_entry, entry);
+                       }
+               }
+               fclose(infp);
+       }
+
+       /* 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");
+       }
+}
+
+
+
+void cleanup(int exitcode)
+{
+#ifdef HAVE_NEWT
+       newtCls();
+       newtRefresh();
+       newtFinished();
+#endif
+       exit(exitcode);
+}
+
+
+
+void title(char *text)
+{
+       if (setup_type == UI_TEXT) {
+               printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
+       }
+}
+
+
+
+int yesno(char *question)
+{
+#ifdef HAVE_NEWT
+       newtComponent form = NULL;
+       newtComponent yesbutton = NULL;
+       newtComponent nobutton = NULL;
+       int i = 0;
+#endif
+       int answer = 0;
+       char buf[SIZ];
+
+       switch (setup_type) {
+
+       case UI_TEXT:
+               do {
+                       printf("%s\nYes/No --> ", question);
+                       fgets(buf, sizeof buf, stdin);
+                       answer = tolower(buf[0]);
+                       if (answer == 'y')
+                               answer = 1;
+                       else if (answer == 'n')
+                               answer = 0;
+               } while ((answer < 0) || (answer > 1));
+               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');
+                       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 tempfile[PATH_MAX];
+       char setupmsg[SIZ];
+
+       strcpy(tempfile, tmpnam(NULL));
+       strcpy(setupmsg, "");
+
+       switch (setup_type) {
+       case UI_TEXT:
+               title("WebCit setup");
+               printf("\n%s\n", prompt);
+               printf("This is currently set to:\n%s\n", str);
+               printf("Enter new value or press return to leave unchanged:\n");
+               fgets(buf, sizeof buf, stdin);
+               buf[strlen(buf) - 1] = 0;
+               if (strlen(buf) != 0)
+                       strcpy(str, buf);
+               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');
+                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
+               }
+               newtFormAddComponent(form, newtEntry(1, 8, str, 74, &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) {
+
+       case UI_TEXT:
+               printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
+               printf("       %s \n\n%s\n\n", title, msgtext);
+               printf("Press return to continue...");
+               fgets(buf, sizeof buf, stdin);
+               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');
+                       newtFormAddComponent(form, newtLabel(1, 1+i, buf));
+               }
+               newtFormAddComponent(form, newtButton(35, 5, "OK"));
+               newtRunForm(form);
+               newtPopWindow();
+               newtFormDestroy(form);  
+               break;
+#endif
+
+       }
+}
+
+
+void display_error(char *error_message)
+{
+       important_message("Error", 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;
+
+       switch (setup_type) {
+
+       case UI_TEXT:
+               if (curr == 0) {
+                       printf("%s\n", text);
+                       printf("..........................");
+                       printf("..........................");
+                       printf("..........................\r");
+                       fflush(stdout);
+                       dots_printed = 0;
+               } else if (curr == cmax) {
+                       printf("\r%79s\n", "");
+               } else {
+                       a = (curr * 100) / cmax;
+                       a = a * 78;
+                       a = a / 100;
+                       while (dots_printed < a) {
+                               printf("*");
+                               ++dots_printed;
+                               fflush(stdout);
+                       }
+               }
+               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
+
+       }
+}
+
+
+
+
+/*
+ * check_inittab_entry()  -- Make sure "webserver" is in /etc/inittab
+ *
+ */
+void check_inittab_entry(void)
+{
+       FILE *infp;
+       char buf[SIZ];
+       char looking_for[SIZ];
+       char question[128];
+       char entryname[5];
+
+       /* Determine the fully qualified path name of citserver */
+       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;
+       }
+
+       /* 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);
+       if (yesno(question) == 0)
+               return;
+
+       /* 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 -h%s\n",
+                       entryname, looking_for, setup_directory);
+               fclose(infp);
+               strcpy(init_entry, entryname);
+       }
+}
+
+
+
+
+/* 
+ * 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)
+{
+
+#ifdef HAVE_NEWT
+       newtInit();
+       newtCls();
+       newtDrawRootText(0, 0, "WebCit Setup");
+       return UI_NEWT;
+#endif
+       return UI_TEXT;
+}
+
+
+
+
+
+int main(int argc, char *argv[])
+{
+       int a;
+       int curr;
+       char aaa[128];
+       FILE *fp;
+       int info_only = 0;
+       gid_t gid;
+
+       /* set an invalid setup type */
+       setup_type = (-1);
+
+       /* parse command line args */
+       for (a = 0; a < argc; ++a) {
+               if (!strncmp(argv[a], "-u", 2)) {
+                       strcpy(aaa, argv[a]);
+                       strcpy(aaa, &aaa[2]);
+                       setup_type = atoi(aaa);
+               }
+               if (!strcmp(argv[a], "-i")) {
+                       info_only = 1;
+               }
+               if (!strcmp(argv[a], "-q")) {
+                       setup_type = UI_SILENT;
+               }
+       }
+
+
+       /* If a setup type was not specified, try to determine automatically
+        * the best one to use out of all available types.
+        */
+       if (setup_type < 0) {
+               setup_type = discover_ui();
+       }
+       if (info_only == 1) {
+               important_message("WebCit Setup", "Welcome to WebCit setup");
+               cleanup(0);
+       }
+
+       /* Get started in a valid setup directory. */
+       strcpy(setup_directory, WEBCITDIR);
+       set_value("FIXME enter your setup directory, dumbass.",
+               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. */
+       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) {
+
+       case UI_TEXT:
+               printf("\n\n\n"
+                       "               *** WebCit setup program ***\n\n");
+               break;
+
+       }
+
+       check_inittab_entry();  /* Check /etc/inittab */
+
+       /* 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 (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.");
+               }
+       }
+       else {
+               important_message("Setup finished",
+                       "Setup is finished.  You may now start the server.");
+       }
+
+       cleanup(0);
+       return 0;
+}
index 6a2bad457162b5458809fcf46b18896b8dc7d843..eb09d72fb8f2988a0abe7f911c811458c8f5530e 100644 (file)
@@ -44,7 +44,6 @@ static byte dtable[256];      /* base64 encode / decode table */
 char *safestrncpy(char *dest, const char *src, size_t n)
 {
        if (dest == NULL || src == NULL) {
-               lprintf(1, "safestrncpy: NULL argument\n");
                abort();
        }
        strncpy(dest, src, n);