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 * Delete an entry from /etc/inittab
27 void delete_init_entry(char *which_entry)
38 if (inittab == NULL) return;
40 fp = fopen("/etc/inittab", "r");
41 if (fp == NULL) return;
43 while(fgets(buf, sizeof buf, fp) != NULL) {
45 if (num_tokens(buf, ':') == 4) {
46 extract_token(entry, buf, 0, ':', sizeof entry);
47 extract_token(levels, buf, 1, ':', sizeof levels);
48 extract_token(state, buf, 2, ':', sizeof state);
49 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
51 if (!strcmp(entry, which_entry)) {
52 strcpy(state, "off"); /* disable it */
56 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
57 if (inittab == NULL) {
65 fp = fopen("/etc/inittab", "w");
67 fwrite(inittab, strlen(inittab), 1, fp);
69 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
78 * Remove any /etc/inittab entries for webcit, because we don't
79 * start it that way anymore.
81 void delete_the_old_way(void) {
84 char looking_for[1024];
88 char init_entry[1024];
91 strcpy(init_entry, "");
93 /* Determine the fully qualified path name of webserver */
94 snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
96 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
97 * an entry is found which we believe starts webserver.
99 infp = fopen("/etc/inittab", "r");
103 while (fgets(buf, sizeof buf, infp) != NULL) {
104 buf[strlen(buf) - 1] = 0;
105 extract_token(entry, buf, 0, ':', sizeof entry);
106 extract_token(prog, buf, 3, ':', sizeof prog);
107 if (!strncasecmp(prog, looking_for,
108 strlen(looking_for))) {
110 strcpy(init_entry, entry);
116 /* Bail out if there's nothing to do. */
117 if (!have_entry) return;
119 delete_init_entry(init_entry);
124 void cleanup(int exitcode)
131 void title(char *text)
133 if (setup_type == UI_TEXT) {
134 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
140 int yesno(char *question)
146 switch (setup_type) {
150 printf("%s\nYes/No --> ", question);
151 fgets(buf, sizeof buf, stdin);
152 answer = tolower(buf[0]);
155 else if (answer == 'n')
157 } while ((answer < 0) || (answer > 1));
161 sprintf(buf, "exec %s --yesno '%s' 10 72",
162 getenv("CTDL_DIALOG"),
177 void set_value(char *prompt, char str[])
180 char dialog_result[PATH_MAX];
184 strcpy(setupmsg, "");
186 switch (setup_type) {
188 title("WebCit setup");
189 printf("\n%s\n", prompt);
190 printf("This is currently set to:\n%s\n", str);
191 printf("Enter new value or press return to leave unchanged:\n");
192 fgets(buf, sizeof buf, stdin);
193 buf[strlen(buf) - 1] = 0;
194 if (strlen(buf) != 0)
199 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
200 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
201 getenv("CTDL_DIALOG"),
206 fp = fopen(dialog_result, "r");
208 fgets(str, sizeof buf, fp);
209 if (str[strlen(str)-1] == 10) {
210 str[strlen(str)-1] = 0;
213 unlink(dialog_result);
221 void important_message(char *title, char *msgtext)
225 switch (setup_type) {
228 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");
229 printf(" %s \n\n%s\n\n", title, msgtext);
230 printf("Press return to continue...");
231 fgets(buf, sizeof buf, stdin);
235 sprintf(buf, "exec %s --msgbox '%s' 19 72",
236 getenv("CTDL_DIALOG"),
244 void display_error(char *error_message)
246 important_message("Error", error_message);
249 void progress(char *text, long int curr, long int cmax)
251 static long dots_printed = 0L;
254 static FILE *fp = NULL;
256 switch (setup_type) {
260 printf("%s\n", text);
261 printf("..........................");
262 printf("..........................");
263 printf("..........................\r");
266 } else if (curr == cmax) {
267 printf("\r%79s\n", "");
269 a = (curr * 100) / cmax;
272 while (dots_printed < a) {
282 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
283 getenv("CTDL_DIALOG"),
285 fp = popen(buf, "w");
291 else if (curr == cmax) {
293 fprintf(fp, "100\n");
299 a = (curr * 100) / cmax;
301 fprintf(fp, "%ld\n", a);
313 * install_init_scripts() -- Make sure "webserver" is in /etc/inittab
316 void install_init_scripts(void)
321 char https_port[128];
325 struct utsname my_utsname;
329 /* Otherwise, prompt the user to create an entry. */
330 snprintf(question, sizeof question,
331 "Would you like to automatically start WebCit at boot?"
333 if (yesno(question) == 0)
336 snprintf(question, sizeof question,
337 "On which port do you want WebCit to listen for HTTP "
338 "requests?\n\nYou can use the standard port (80) if you are "
339 "not running another\nweb server (such as Apache), otherwise "
340 "select another port.");
341 sprintf(http_port, "2000");
342 set_value(question, http_port);
344 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
347 snprintf(question, sizeof question,
348 "On which port do you want WebCit to listen for HTTPS "
349 "requests?\n\nYou can use the standard port (443) if you are "
350 "not running another\nweb server (such as Apache), otherwise "
351 "select another port.");
352 sprintf(https_port, "443");
353 set_value(question, https_port);
356 /* Find out where Citadel is. */
357 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
358 strcpy(hostname, "uds");
359 strcpy(portname, getenv("CITADEL"));
362 snprintf(question, sizeof question,
363 "Is the Citadel service running on the same host as WebCit?");
364 if (yesno(question)) {
365 sprintf(hostname, "uds");
366 sprintf(portname, "/usr/local/citadel");
367 set_value("In what directory is Citadel installed?", portname);
370 sprintf(hostname, "127.0.0.1");
371 sprintf(portname, "504");
372 set_value("Enter the host name or IP address of your "
373 "Citadel server.", hostname);
374 set_value("Enter the port number on which Citadel is "
375 "running (usually 504)", portname);
380 fp = fopen("/etc/init.d/webcit", "w");
382 display_error("Cannot create /etc/init.d/webcit");
386 fprintf(fp, "#!/bin/sh\n"
388 "WEBCIT_DIR=%s\n", setup_directory);
389 fprintf(fp, "HTTP_PORT=%s\n", http_port);
391 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
393 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
394 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
396 "test -d /var/run || exit 0\n"
400 "start) echo -n \"Starting WebCit... \"\n"
401 " if $WEBCIT_DIR/webserver "
402 "-d/var/run/webcit.pid "
404 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
411 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
412 " if $WEBCIT_DIR/webserver "
413 "-d/var/run/webcit-ssl.pid "
415 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
423 "stop) echo -n \"Stopping WebCit... \"\n"
424 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
430 " rm -f /var/run/webcit.pid 2>/dev/null\n");
432 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
433 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
439 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
445 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
452 chmod("/etc/init.d/webcit", 0755);
454 /* Set up the run levels. */
455 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
456 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");
457 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");
465 * Figure out what type of user interface we're going to use
467 int discover_ui(void)
470 /* Use "dialog" if we have it */
471 if (getenv("CTDL_DIALOG") != NULL) {
482 int main(int argc, char *argv[])
487 strcpy(suggested_url, "http://<your_host_name>:<port>/");
489 /* set an invalid setup type */
492 /* Check to see if we're running the web installer */
493 if (getenv("CITADEL_INSTALLER") != NULL) {
494 using_web_installer = 1;
497 /* parse command line args */
498 for (a = 0; a < argc; ++a) {
499 if (!strncmp(argv[a], "-u", 2)) {
500 strcpy(aaa, argv[a]);
501 strcpy(aaa, &aaa[2]);
502 setup_type = atoi(aaa);
504 if (!strcmp(argv[a], "-i")) {
507 if (!strcmp(argv[a], "-q")) {
508 setup_type = UI_SILENT;
513 /* If a setup type was not specified, try to determine automatically
514 * the best one to use out of all available types.
516 if (setup_type < 0) {
517 setup_type = discover_ui();
519 if (info_only == 1) {
520 important_message("WebCit Setup", "Welcome to WebCit setup");
524 /* Get started in a valid setup directory. */
525 strcpy(setup_directory, PREFIX);
526 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
527 strcpy(setup_directory, getenv("WEBCIT"));
530 set_value("In what directory is WebCit installed?",
533 if (chdir(setup_directory) != 0) {
534 important_message("WebCit Setup",
535 "The directory you specified does not exist.");
540 * We used to start WebCit by putting it directly into /etc/inittab.
541 * Since some systems are moving away from init, we can't do this anymore.
543 progress("Removing obsolete /etc/inittab entries...", 0, 1);
544 delete_the_old_way();
545 progress("Removing obsolete /etc/inittab entries...", 1, 1);
548 switch (setup_type) {
552 " *** WebCit setup program ***\n\n");
558 * If we're running on SysV, install init scripts.
560 if (!access("/var/run", W_OK)) {
561 install_init_scripts();
563 if (!access("/etc/init.d/webcit", X_OK)) {
564 system("/etc/init.d/webcit stop");
565 system("/etc/init.d/webcit start");
569 "Setup is finished. You may now log in.\n"
570 "Point your web browser at %s\n", suggested_url
572 important_message("Setup finished", aaa);
576 important_message("Setup finished",
577 "Setup is finished. You may now start the server.");