From a067b7707c53c704e77aa1b5fc1451aae7caf51d Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Fri, 22 Aug 2003 00:25:40 +0000 Subject: [PATCH] * Added a skeleton "setup" program to the tree. It does not work yet. --- webcit/ChangeLog | 4 + webcit/Makefile.in | 5 +- webcit/configure.in | 18 ++ webcit/setup.c | 571 ++++++++++++++++++++++++++++++++++++++++++++ webcit/tools.c | 1 - 5 files changed, 597 insertions(+), 2 deletions(-) create mode 100644 webcit/setup.c diff --git a/webcit/ChangeLog b/webcit/ChangeLog index d89df0d28..f76deb51e 100644 --- a/webcit/ChangeLog +++ b/webcit/ChangeLog @@ -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 1998-12-03 Nathan Bryant * webserver.c: warning fix + diff --git a/webcit/Makefile.in b/webcit/Makefile.in index 90f84e421..efb52db2b 100644 --- a/webcit/Makefile.in +++ b/webcit/Makefile.in @@ -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 \ diff --git a/webcit/configure.in b/webcit/configure.in index 73aa21989..f7720f4e4 100644 --- a/webcit/configure.in +++ b/webcit/configure.in @@ -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 index 000000000..ebf91c88c --- /dev/null +++ b/webcit/setup.c @@ -0,0 +1,571 @@ +/* + * $Id$ + * + * WebCit setup utility + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "webcit.h" + +#ifdef HAVE_NEWT +#include +#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 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; +} diff --git a/webcit/tools.c b/webcit/tools.c index 6a2bad457..eb09d72fb 100644 --- a/webcit/tools.c +++ b/webcit/tools.c @@ -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); -- 2.39.2