6 * (This is basically just an install wizard. It's not required.)
12 #include "webserver.h"
15 #define UI_TEXT 0 /* Default setup type -- text only */
16 #define UI_DIALOG 2 /* Use the 'dialog' program */
17 #define UI_SILENT 3 /* Silent running, for use in scripts */
20 char setup_directory[SIZ];
21 int using_web_installer = 0;
22 char suggested_url[SIZ];
25 int lprintf(int loglevel, const char *format, ...){return 0;}
26 void RegisterNS(const char *NSName, long len,
29 WCHandlerFunc HandlerFunc,
30 int ContextRequired){}
31 pthread_key_t MyConKey;
33 #include "wc_gettext.h"
37 * Delete an entry from /etc/inittab
39 void delete_init_entry(char *which_entry)
50 if (inittab == NULL) return;
52 fp = fopen("/etc/inittab", "r");
53 if (fp == NULL) return;
55 while(fgets(buf, sizeof buf, fp) != NULL) {
57 if (num_tokens(buf, ':') == 4) {
58 extract_token(entry, buf, 0, ':', sizeof entry);
59 extract_token(levels, buf, 1, ':', sizeof levels);
60 extract_token(state, buf, 2, ':', sizeof state);
61 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
63 if (!strcmp(entry, which_entry)) {
64 strcpy(state, "off"); /* disable it */
68 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
69 if (inittab == NULL) {
77 fp = fopen("/etc/inittab", "w");
79 fwrite(inittab, strlen(inittab), 1, fp);
81 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
90 * Remove any /etc/inittab entries for webcit, because we don't
91 * start it that way anymore.
93 void delete_the_old_way(void) {
96 char looking_for[1024];
100 char init_entry[1024];
103 strcpy(init_entry, "");
105 /* Determine the fully qualified path name of webcit */
106 snprintf(looking_for, sizeof looking_for, "%s/webcit ", setup_directory);
108 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
109 * an entry is found which we believe starts webcit.
111 infp = fopen("/etc/inittab", "r");
115 while (fgets(buf, sizeof buf, infp) != NULL) {
116 buf[strlen(buf) - 1] = 0;
117 extract_token(entry, buf, 0, ':', sizeof entry);
118 extract_token(prog, buf, 3, ':', sizeof prog);
119 if (!strncasecmp(prog, looking_for,
120 strlen(looking_for))) {
122 strcpy(init_entry, entry);
128 /* Bail out if there's nothing to do. */
129 if (!have_entry) return;
131 delete_init_entry(init_entry);
136 void cleanup(int exitcode)
143 void title(char *text)
145 if (setup_type == UI_TEXT) {
146 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
153 int yesno(char *question, int default_value)
159 switch (setup_type) {
163 printf("%s\nYes/No [%s] --> ",
165 ( default_value ? "Yes" : "No" )
167 fgets(buf, sizeof buf, stdin);
168 answer = tolower(buf[0]);
169 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
170 answer = default_value;
171 else if (answer == 'y')
173 else if (answer == 'n')
175 } while ((answer < 0) || (answer > 1));
179 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
180 getenv("CTDL_DIALOG"),
181 ( default_value ? "" : "--defaultno" ),
199 void set_value(char *prompt, char str[])
202 char dialog_result[PATH_MAX];
206 strcpy(setupmsg, "");
208 switch (setup_type) {
210 title("WebCit setup");
211 printf("\n%s\n", prompt);
212 printf("This is currently set to:\n%s\n", str);
213 printf("Enter new value or press return to leave unchanged:\n");
214 fgets(buf, sizeof buf, stdin);
215 buf[strlen(buf) - 1] = 0;
216 if (strlen(buf) != 0)
221 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
222 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
223 getenv("CTDL_DIALOG"),
228 fp = fopen(dialog_result, "r");
230 fgets(str, sizeof buf, fp);
231 if (str[strlen(str)-1] == 10) {
232 str[strlen(str)-1] = 0;
235 unlink(dialog_result);
243 void important_message(char *title, char *msgtext)
247 switch (setup_type) {
250 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");
251 printf(" %s \n\n%s\n\n", title, msgtext);
252 printf("Press return to continue...");
253 fgets(buf, sizeof buf, stdin);
257 sprintf(buf, "exec %s --msgbox '%s' 19 72",
258 getenv("CTDL_DIALOG"),
266 void display_error(char *error_message)
268 important_message("Error", error_message);
271 void progress(char *text, long int curr, long int cmax)
273 static long dots_printed = 0L;
276 static FILE *fp = NULL;
278 switch (setup_type) {
282 printf("%s\n", text);
283 printf("..........................");
284 printf("..........................");
285 printf("..........................\r");
288 } else if (curr == cmax) {
289 printf("\r%79s\n", "");
291 a = (curr * 100) / cmax;
294 while (dots_printed < a) {
304 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
305 getenv("CTDL_DIALOG"),
307 fp = popen(buf, "w");
313 else if (curr == cmax) {
315 fprintf(fp, "100\n");
321 a = (curr * 100) / cmax;
323 fprintf(fp, "%ld\n", a);
335 * install_init_scripts() -- Create and deploy SysV init scripts.
338 void install_init_scripts(void)
344 char https_port[128];
349 struct utsname my_utsname;
350 struct stat etcinitd;
352 char *initfile = "/etc/init.d/webcit";
354 fp = fopen(initfile, "r");
356 if (yesno("WebCit already appears to be configured to start at boot.\n"
357 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
364 /* Otherwise, prompt the user to create an entry. */
365 snprintf(question, sizeof question,
366 "Would you like to automatically start WebCit at boot?"
368 if (yesno(question, 1) == 0)
372 sprintf(http_port, "2000");
374 sprintf(https_port, "443");
376 sprintf(hostname, "uds");
377 sprintf(portname, "/usr/local/citadel");
379 /* This is a very hackish way of learning the port numbers used
380 * in a previous install, if we are upgrading: read them out of
381 * the existing init script.
383 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
386 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
388 initfile = WEBCITDIR"/webcit.init";
390 initfile = "/etc/rc.d/init.d/webcit";
393 fp = fopen(initfile, "r");
395 while (fgets(buf, sizeof buf, fp) != NULL) {
396 if (strlen(buf) > 0) {
397 buf[strlen(buf)-1] = 0; /* strip trailing cr */
399 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
400 safestrncpy(http_port, &buf[10], sizeof http_port);
403 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
404 safestrncpy(https_port, &buf[11], sizeof https_port);
407 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
408 safestrncpy(hostname, &buf[14], sizeof hostname);
410 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
411 safestrncpy(portname, &buf[14], sizeof portname);
417 /* Now ask for the port numbers */
418 snprintf(question, sizeof question,
419 "On which port do you want WebCit to listen for HTTP "
420 "requests?\n\nYou can use the standard port (80) if you are "
421 "not running another\nweb server (such as Apache), otherwise "
422 "select another port.");
423 set_value(question, http_port);
425 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
428 snprintf(question, sizeof question,
429 "On which port do you want WebCit to listen for HTTPS "
430 "requests?\n\nYou can use the standard port (443) if you are "
431 "not running another\nweb server (such as Apache), otherwise "
432 "select another port.");
433 set_value(question, https_port);
436 /* Find out where Citadel is. */
437 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
438 strcpy(hostname, "uds");
439 strcpy(portname, getenv("CITADEL"));
442 snprintf(question, sizeof question,
443 "Is the Citadel service running on the same host as WebCit?");
444 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
445 strcpy(hostname, "uds");
446 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
447 set_value("In what directory is Citadel installed?", portname);
450 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
451 if (atoi(portname) == 0) strcpy(portname, "504");
452 set_value("Enter the host name or IP address of your "
453 "Citadel server.", hostname);
454 set_value("Enter the port number on which Citadel is "
455 "running (usually 504)", portname);
460 /// fp = fopen(initfile, "w");
461 fp = stderr;/// TODO: weg
463 fprintf(fp, "#!/bin/sh\n"
465 "WEBCIT_DIR=%s\n", setup_directory);
466 fprintf(fp, "HTTP_PORT=%s\n", http_port);
468 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
470 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
471 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
476 "start) echo -n \"Starting WebCit... \"\n"
477 " if $WEBCIT_DIR/webcit "
478 "-D/var/run/webcit.pid "
479 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
486 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
487 " if $WEBCIT_DIR/webcit "
488 "-D/var/run/webcit-ssl.pid "
489 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
497 "stop) echo -n \"Stopping WebCit... \"\n"
498 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
504 " rm -f /var/run/webcit.pid 2>/dev/null\n");
506 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
507 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
513 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
519 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
526 chmod(initfile, 0755);
528 /* Set up the run levels. */
529 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
530 snprintf(command, sizeof(command), "for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/S84webcit ; done 2>/dev/null", initfile);
532 snprintf(command, sizeof(command), "for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/K15webcit ; done 2>/dev/null", initfile);
541 * Figure out what type of user interface we're going to use
543 int discover_ui(void)
546 /* Use "dialog" if we have it */
547 if (getenv("CTDL_DIALOG") != NULL) {
558 int main(int argc, char *argv[])
563 strcpy(suggested_url, "http://<your_host_name>:<port>/");
565 /* set an invalid setup type */
568 /* Check to see if we're running the web installer */
569 if (getenv("CITADEL_INSTALLER") != NULL) {
570 using_web_installer = 1;
573 /* parse command line args */
574 for (a = 0; a < argc; ++a) {
575 if (!strncmp(argv[a], "-u", 2)) {
576 strcpy(aaa, argv[a]);
577 strcpy(aaa, &aaa[2]);
578 setup_type = atoi(aaa);
580 if (!strcmp(argv[a], "-i")) {
583 if (!strcmp(argv[a], "-q")) {
584 setup_type = UI_SILENT;
589 /* If a setup type was not specified, try to determine automatically
590 * the best one to use out of all available types.
592 if (setup_type < 0) {
593 setup_type = discover_ui();
595 if (info_only == 1) {
596 important_message("WebCit Setup", "Welcome to WebCit setup");
600 /* Get started in a valid setup directory. */
601 strcpy(setup_directory, WEBCITDIR);
602 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
603 strcpy(setup_directory, getenv("WEBCIT"));
606 set_value("In what directory is WebCit installed?",
609 if (chdir(setup_directory) != 0) {
610 important_message("WebCit Setup",
611 "The directory you specified does not exist.");
616 * We used to start WebCit by putting it directly into /etc/inittab.
617 * Since some systems are moving away from init, we can't do this anymore.
619 progress("Removing obsolete /etc/inittab entries...", 0, 1);
620 delete_the_old_way();
621 progress("Removing obsolete /etc/inittab entries...", 1, 1);
624 switch (setup_type) {
628 " *** WebCit setup program ***\n\n");
634 * If we're running on SysV, install init scripts.
636 if (!access("/var/run", W_OK)) {
637 install_init_scripts();
639 if (!access("/etc/init.d/webcit", X_OK)) {
640 system("/etc/init.d/webcit stop");
641 system("/etc/init.d/webcit start");
645 "Setup is finished. You may now log in.\n"
646 "Point your web browser at %s\n", suggested_url
648 important_message("Setup finished", aaa);
652 important_message("Setup finished",
653 "Setup is finished. You may now start the server.");