6 * (This is basically just an install wizard. It's not required.)
16 #include <sys/types.h>
18 #include <sys/utsname.h>
33 #define UI_TEXT 0 /* Default setup type -- text only */
34 #define UI_SILENT 3 /* Silent running, for use in scripts */
35 #define UI_NEWT 4 /* Use the "newt" window library */
38 char setup_directory[SIZ];
40 int using_web_installer = 0;
44 * Set an entry in inittab to the desired state
46 void set_init_entry(char *which_entry, char *new_state) {
56 if (inittab == NULL) return;
58 fp = fopen("/etc/inittab", "r");
59 if (fp == NULL) return;
61 while(fgets(buf, sizeof buf, fp) != NULL) {
63 if (num_tokens(buf, ':') == 4) {
64 extract_token(entry, buf, 0, ':');
65 extract_token(levels, buf, 1, ':');
66 extract_token(state, buf, 2, ':');
67 extract_token(prog, buf, 3, ':'); /* includes 0x0a LF */
69 if (!strcmp(entry, which_entry)) {
70 strcpy(state, new_state);
71 sprintf(buf, "%s:%s:%s:%s",
72 entry, levels, state, prog);
76 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
77 if (inittab == NULL) {
85 fp = fopen("/etc/inittab", "w");
87 fwrite(inittab, strlen(inittab), 1, fp);
89 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
98 * Shut down the Citadel service if necessary, during setup.
100 void shutdown_service(void) {
103 char looking_for[SIZ];
108 strcpy(init_entry, "");
110 /* Determine the fully qualified path name of webserver */
111 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
113 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
114 * an entry is found which we believe starts webserver.
116 infp = fopen("/etc/inittab", "r");
120 while (fgets(buf, sizeof buf, infp) != NULL) {
121 buf[strlen(buf) - 1] = 0;
122 extract_token(entry, buf, 0, ':');
123 extract_token(prog, buf, 3, ':');
124 if (!strncasecmp(prog, looking_for,
125 strlen(looking_for))) {
127 strcpy(init_entry, entry);
133 /* Bail out if there's nothing to do. */
134 if (!have_entry) return;
136 set_init_entry(init_entry, "off");
141 * Start the Citadel service.
143 void start_the_service(void) {
144 if (strlen(init_entry) > 0) {
145 set_init_entry(init_entry, "respawn");
151 void cleanup(int exitcode)
163 void title(char *text)
165 if (setup_type == UI_TEXT) {
166 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
172 int yesno(char *question)
175 newtComponent form = NULL;
176 newtComponent yesbutton = NULL;
177 newtComponent nobutton = NULL;
183 switch (setup_type) {
187 printf("%s\nYes/No --> ", question);
188 fgets(buf, sizeof buf, stdin);
189 answer = tolower(buf[0]);
192 else if (answer == 'n')
194 } while ((answer < 0) || (answer > 1));
199 newtCenteredWindow(76, 10, "Question");
200 form = newtForm(NULL, NULL, 0);
201 for (i=0; i<num_tokens(question, '\n'); ++i) {
202 extract_token(buf, question, i, '\n');
203 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
205 yesbutton = newtButton(10, 5, "Yes");
206 nobutton = newtButton(60, 5, "No");
207 newtFormAddComponent(form, yesbutton);
208 newtFormAddComponent(form, nobutton);
209 if (newtRunForm(form) == yesbutton) {
216 newtFormDestroy(form);
225 void set_value(char *prompt, char str[])
235 strcpy(setupmsg, "");
237 switch (setup_type) {
239 title("WebCit setup");
240 printf("\n%s\n", prompt);
241 printf("This is currently set to:\n%s\n", str);
242 printf("Enter new value or press return to leave unchanged:\n");
243 fgets(buf, sizeof buf, stdin);
244 buf[strlen(buf) - 1] = 0;
245 if (strlen(buf) != 0)
251 newtCenteredWindow(76, 10, "WebCit setup");
252 form = newtForm(NULL, NULL, 0);
253 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
254 extract_token(buf, prompt, i, '\n');
255 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
257 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
258 NEWT_FLAG_RETURNEXIT));
263 newtFormDestroy(form);
270 void important_message(char *title, char *msgtext)
273 newtComponent form = NULL;
278 switch (setup_type) {
281 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");
282 printf(" %s \n\n%s\n\n", title, msgtext);
283 printf("Press return to continue...");
284 fgets(buf, sizeof buf, stdin);
289 newtCenteredWindow(76, 10, title);
290 form = newtForm(NULL, NULL, 0);
291 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
292 extract_token(buf, msgtext, i, '\n');
293 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
295 newtFormAddComponent(form, newtButton(35, 5, "OK"));
298 newtFormDestroy(form);
306 void display_error(char *error_message)
308 important_message("Error", error_message);
311 void progress(char *text, long int curr, long int cmax)
315 /* These variables are static because progress() gets called
316 * multiple times during the course of whatever operation is
317 * being performed. This makes setup non-threadsafe, but who
320 static newtComponent form = NULL;
321 static newtComponent scale = NULL;
323 static long dots_printed = 0L;
326 switch (setup_type) {
330 printf("%s\n", text);
331 printf("..........................");
332 printf("..........................");
333 printf("..........................\r");
336 } else if (curr == cmax) {
337 printf("\r%79s\n", "");
339 a = (curr * 100) / cmax;
342 while (dots_printed < a) {
353 newtCenteredWindow(76, 8, text);
354 form = newtForm(NULL, NULL, 0);
355 scale = newtScale(1, 3, 74, cmax);
356 newtFormAddComponent(form, scale);
360 if ((curr > 0) && (curr <= cmax)) {
361 newtScaleSet(scale, curr);
365 newtFormDestroy(form);
379 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
382 void check_inittab_entry(void)
386 char looking_for[SIZ];
390 char https_port[128];
394 /* Determine the fully qualified path name of webserver */
395 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
397 /* If there's already an entry, then we have nothing left to do. */
398 if (strlen(init_entry) > 0) {
402 /* Otherwise, prompt the user to create an entry. */
403 snprintf(question, sizeof question,
404 "There is no '%s' entry in /etc/inittab.\n"
405 "Would you like to add one?",
407 if (yesno(question) == 0)
410 snprintf(question, sizeof question,
411 "On which port do you want WebCit to listen for HTTP "
412 "requests?\n\nYou can use the standard port (80) if you are "
413 "not running another\nweb server (such as Apache), otherwise "
414 "select another port.");
415 sprintf(http_port, "2000");
416 set_value(question, http_port);
418 snprintf(question, sizeof question,
419 "On which port do you want WebCit to listen for HTTPS "
420 "requests?\n\nYou can use the standard port (443) if you are "
421 "not running another\nweb server (such as Apache), otherwise "
422 "select another port.");
423 sprintf(https_port, "443");
424 set_value(question, https_port);
426 /* Find out where Citadel is. */
427 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
428 strcpy(hostname, "uds");
429 strcpy(portname, getenv("CITADEL"));
432 snprintf(question, sizeof question,
433 "Is the Citadel service running on the same host as WebCit?");
434 if (yesno(question)) {
435 sprintf(hostname, "uds");
436 sprintf(portname, "/usr/local/citadel");
437 set_value("In what directory is Citadel installed?", portname);
440 sprintf(hostname, "127.0.0.1");
441 sprintf(portname, "504");
442 set_value("Enter the host name or IP address of your "
443 "Citadel server.", hostname);
444 set_value("Enter the port number on which Citadel is "
445 "running (usually 504)", portname);
449 /* Generate a unique entry name for /etc/inittab */
450 snprintf(entryname, sizeof entryname, "c0");
453 if (entryname[1] > '9') {
456 if (entryname[0] > 'z') {
458 "Can't generate a unique entry name");
462 snprintf(buf, sizeof buf,
463 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
464 } while (system(buf) == 0);
466 /* Now write it out to /etc/inittab */
467 infp = fopen("/etc/inittab", "a");
469 display_error(strerror(errno));
471 fprintf(infp, "# Start the WebCit server...\n");
472 fprintf(infp, "%s:2345:respawn:%s -p%s -s%s %s %s\n",
473 entryname, looking_for,
474 http_port, https_port, hostname, portname);
476 strcpy(init_entry, entryname);
484 * Figure out what type of user interface we're going to use
486 int discover_ui(void)
492 newtDrawRootText(0, 0, "WebCit Setup");
502 int main(int argc, char *argv[])
508 /* set an invalid setup type */
511 /* Check to see if we're running the web installer */
512 if (getenv("CITADEL_INSTALLER") != NULL) {
513 using_web_installer = 1;
516 /* parse command line args */
517 for (a = 0; a < argc; ++a) {
518 if (!strncmp(argv[a], "-u", 2)) {
519 strcpy(aaa, argv[a]);
520 strcpy(aaa, &aaa[2]);
521 setup_type = atoi(aaa);
523 if (!strcmp(argv[a], "-i")) {
526 if (!strcmp(argv[a], "-q")) {
527 setup_type = UI_SILENT;
532 /* If a setup type was not specified, try to determine automatically
533 * the best one to use out of all available types.
535 if (setup_type < 0) {
536 setup_type = discover_ui();
538 if (info_only == 1) {
539 important_message("WebCit Setup", "Welcome to WebCit setup");
543 /* Get started in a valid setup directory. */
544 strcpy(setup_directory, WEBCITDIR);
545 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
546 strcpy(setup_directory, getenv("WEBCIT"));
549 set_value("In what directory is WebCit installed?",
552 if (chdir(setup_directory) != 0) {
553 important_message("WebCit Setup",
554 "The directory you specified does not exist.");
558 /* See if we need to shut down the WebCit service. */
559 for (a=0; a<=3; ++a) {
560 progress("Shutting down the WebCit service...", a, 3);
561 if (a == 0) shutdown_service();
566 switch (setup_type) {
570 " *** WebCit setup program ***\n\n");
575 check_inittab_entry(); /* Check /etc/inittab */
577 /* See if we can start the WebCit service. */
578 if (strlen(init_entry) > 0) {
579 for (a=0; a<=3; ++a) {
580 progress("Starting the WebCit service...", a, 3);
581 if (a == 0) start_the_service();
584 important_message("Setup finished",
585 "Setup is finished. You may now log in.");
588 important_message("Setup finished",
589 "Setup is finished. You may now start the server.");