f392011122142b9e2e29a90ad90bcf19f1848a82
[citadel.git] / webcit / setup.c
1 /*
2  * $Id$
3  *
4  * WebCit setup utility
5  * 
6  * (This is basically just an install wizard.  It's not required.)
7  *
8  */
9
10
11 #include <ctype.h>
12 #include <stdlib.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <stdio.h>
17 #ifdef HAVE_FCNTL_H
18 #include <fcntl.h>
19 #endif
20 #include <signal.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/socket.h>
24 #ifdef HAVE_SYS_TIME_H
25 #include <sys/time.h>
26 #endif
27 #ifdef HAVE_LIMITS_H
28 #include <limits.h>
29 #endif
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <netdb.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <pthread.h>
38 #include <signal.h>
39 #include <sys/utsname.h>
40 #include "webcit.h"
41 #include "webserver.h"
42
43
44 #ifdef HAVE_NEWT
45 #include <newt.h>
46 #endif
47
48
49 #define UI_TEXT         0       /* Default setup type -- text only */
50 #define UI_DIALOG       2       /* Use the 'dialog' program */
51 #define UI_SILENT       3       /* Silent running, for use in scripts */
52 #define UI_NEWT         4       /* Use the "newt" window library */
53
54 int setup_type;
55 char setup_directory[SIZ];
56 char init_entry[SIZ];
57 int using_web_installer = 0;
58 char suggested_url[SIZ];
59
60 /*
61  * Set an entry in inittab to the desired state
62  */
63 void set_init_entry(char *which_entry, char *new_state) {
64         char *inittab = NULL;
65         FILE *fp;
66         char buf[SIZ];
67         char entry[SIZ];
68         char levels[SIZ];
69         char state[SIZ];
70         char prog[SIZ];
71
72         inittab = strdup("");
73         if (inittab == NULL) return;
74
75         fp = fopen("/etc/inittab", "r");
76         if (fp == NULL) return;
77
78         while(fgets(buf, sizeof buf, fp) != NULL) {
79
80                 if (num_tokens(buf, ':') == 4) {
81                         extract_token(entry, buf, 0, ':', sizeof entry);
82                         extract_token(levels, buf, 1, ':', sizeof levels);
83                         extract_token(state, buf, 2, ':', sizeof state);
84                         extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
85
86                         if (!strcmp(entry, which_entry)) {
87                                 strcpy(state, new_state);
88                                 sprintf(buf, "%s:%s:%s:%s",
89                                         entry, levels, state, prog);
90                         }
91                 }
92
93                 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
94                 if (inittab == NULL) {
95                         fclose(fp);
96                         return;
97                 }
98                 
99                 strcat(inittab, buf);
100         }
101         fclose(fp);
102         fp = fopen("/etc/inittab", "w");
103         if (fp != NULL) {
104                 fwrite(inittab, strlen(inittab), 1, fp);
105                 fclose(fp);
106                 kill(1, SIGHUP);        /* Tell init to re-read /etc/inittab */
107         }
108         free(inittab);
109 }
110
111
112
113
114 /* 
115  * Shut down the Citadel service if necessary, during setup.
116  */
117 void shutdown_service(void) {
118         FILE *infp;
119         char buf[SIZ];
120         char looking_for[SIZ];
121         int have_entry = 0;
122         char entry[SIZ];
123         char prog[SIZ];
124
125         strcpy(init_entry, "");
126
127         /* Determine the fully qualified path name of webserver */
128         snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
129
130         /* Pound through /etc/inittab line by line.  Set have_entry to 1 if
131          * an entry is found which we believe starts webserver.
132          */
133         infp = fopen("/etc/inittab", "r");
134         if (infp == NULL) {
135                 return;
136         } else {
137                 while (fgets(buf, sizeof buf, infp) != NULL) {
138                         buf[strlen(buf) - 1] = 0;
139                         extract_token(entry, buf, 0, ':', sizeof entry);
140                         extract_token(prog, buf, 3, ':', sizeof prog);
141                         if (!strncasecmp(prog, looking_for,
142                            strlen(looking_for))) {
143                                 ++have_entry;
144                                 strcpy(init_entry, entry);
145                         }
146                 }
147                 fclose(infp);
148         }
149
150         /* Bail out if there's nothing to do. */
151         if (!have_entry) return;
152
153         set_init_entry(init_entry, "off");
154 }
155
156
157 /*
158  * Start the Citadel service.
159  */
160 void start_the_service(void) {
161         if (strlen(init_entry) > 0) {
162                 set_init_entry(init_entry, "respawn");
163         }
164 }
165
166
167
168 void cleanup(int exitcode)
169 {
170 #ifdef HAVE_NEWT
171         newtCls();
172         newtRefresh();
173         newtFinished();
174 #endif
175         exit(exitcode);
176 }
177
178
179
180 void title(char *text)
181 {
182         if (setup_type == UI_TEXT) {
183                 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
184         }
185 }
186
187
188
189 int yesno(char *question)
190 {
191 #ifdef HAVE_NEWT
192         newtComponent form = NULL;
193         newtComponent yesbutton = NULL;
194         newtComponent nobutton = NULL;
195 #endif
196         int i = 0;
197         int answer = 0;
198         char buf[SIZ];
199
200         switch (setup_type) {
201
202         case UI_TEXT:
203                 do {
204                         printf("%s\nYes/No --> ", question);
205                         fgets(buf, sizeof buf, stdin);
206                         answer = tolower(buf[0]);
207                         if (answer == 'y')
208                                 answer = 1;
209                         else if (answer == 'n')
210                                 answer = 0;
211                 } while ((answer < 0) || (answer > 1));
212                 break;
213
214         case UI_DIALOG:
215                 sprintf(buf, "exec %s --yesno '%s' 10 72",
216                         getenv("CTDL_DIALOG"),
217                         question);
218                 i = system(buf);
219                 if (i == 0) {
220                         answer = 1;
221                 }
222                 else {
223                         answer = 0;
224                 }
225                 break;
226
227 #ifdef HAVE_NEWT
228         case UI_NEWT:
229                 newtCenteredWindow(76, 10, "Question");
230                 form = newtForm(NULL, NULL, 0);
231                 for (i=0; i<num_tokens(question, '\n'); ++i) {
232                         extract_token(buf, question, i, '\n', sizeof buf);
233                         newtFormAddComponent(form, newtLabel(1, 1+i, buf));
234                 }
235                 yesbutton = newtButton(10, 5, "Yes");
236                 nobutton = newtButton(60, 5, "No");
237                 newtFormAddComponent(form, yesbutton);
238                 newtFormAddComponent(form, nobutton);
239                 if (newtRunForm(form) == yesbutton) {
240                         answer = 1;
241                 }
242                 else {
243                         answer = 0;
244                 }
245                 newtPopWindow();
246                 newtFormDestroy(form);  
247
248                 break;
249 #endif
250
251         }
252         return (answer);
253 }
254
255 void set_value(char *prompt, char str[])
256 {
257 #ifdef HAVE_NEWT
258         newtComponent form;
259         char *result;
260         int i;
261 #endif
262         char buf[SIZ];
263         char *dialog_result;
264         char setupmsg[SIZ];
265         FILE *fp;
266
267         strcpy(setupmsg, "");
268
269         switch (setup_type) {
270         case UI_TEXT:
271                 title("WebCit setup");
272                 printf("\n%s\n", prompt);
273                 printf("This is currently set to:\n%s\n", str);
274                 printf("Enter new value or press return to leave unchanged:\n");
275                 fgets(buf, sizeof buf, stdin);
276                 buf[strlen(buf) - 1] = 0;
277                 if (strlen(buf) != 0)
278                         strcpy(str, buf);
279                 break;
280
281         case UI_DIALOG:
282                 dialog_result = tmpnam(NULL);
283                 sprintf(buf, "exec %s --backtitle '%s' --inputbox '%s' 19 72 '%s' 2>%s",
284                         getenv("CTDL_DIALOG"),
285                         "WebCit setup",
286                         prompt,
287                         str,
288                         dialog_result);
289                 system(buf);
290                 fp = fopen(dialog_result, "r");
291                 if (fp != NULL) {
292                         fgets(str, sizeof buf, fp);
293                         if (str[strlen(str)-1] == 10) {
294                                 str[strlen(str)-1] = 0;
295                         }
296                         fclose(fp);
297                         unlink(dialog_result);
298                 }
299                 break;
300
301 #ifdef HAVE_NEWT
302         case UI_NEWT:
303
304                 newtCenteredWindow(76, 10, "WebCit setup");
305                 form = newtForm(NULL, NULL, 0);
306                 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
307                         extract_token(buf, prompt, i, '\n', sizeof buf);
308                         newtFormAddComponent(form, newtLabel(1, 1+i, buf));
309                 }
310                 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
311                                         NEWT_FLAG_RETURNEXIT));
312                 newtRunForm(form);
313                 strcpy(str, result);
314
315                 newtPopWindow();
316                 newtFormDestroy(form);  
317
318 #endif
319         }
320 }
321
322
323 void important_message(char *title, char *msgtext)
324 {
325 #ifdef HAVE_NEWT
326         newtComponent form = NULL;
327         int i = 0;
328 #endif
329         char buf[SIZ];
330
331         switch (setup_type) {
332
333         case UI_TEXT:
334                 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");
335                 printf("       %s \n\n%s\n\n", title, msgtext);
336                 printf("Press return to continue...");
337                 fgets(buf, sizeof buf, stdin);
338                 break;
339
340         case UI_DIALOG:
341                 sprintf(buf, "exec %s --backtitle '%s' --msgbox '%s' 19 72",
342                         getenv("CTDL_DIALOG"),
343                         title,
344                         msgtext);
345                 system(buf);
346                 break;
347
348 #ifdef HAVE_NEWT
349         case UI_NEWT:
350                 newtCenteredWindow(76, 10, title);
351                 form = newtForm(NULL, NULL, 0);
352                 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
353                         extract_token(buf, msgtext, i, '\n', sizeof buf);
354                         newtFormAddComponent(form, newtLabel(1, 1+i, buf));
355                 }
356                 newtFormAddComponent(form, newtButton(35, 5, "OK"));
357                 newtRunForm(form);
358                 newtPopWindow();
359                 newtFormDestroy(form);  
360                 break;
361 #endif
362
363         }
364 }
365
366
367 void display_error(char *error_message)
368 {
369         important_message("Error", error_message);
370 }
371
372 void progress(char *text, long int curr, long int cmax)
373 {
374 #ifdef HAVE_NEWT
375
376         /* These variables are static because progress() gets called
377          * multiple times during the course of whatever operation is
378          * being performed.  This makes setup non-threadsafe, but who
379          * cares?
380          */
381         static newtComponent form = NULL;
382         static newtComponent scale = NULL;
383 #endif
384         static long dots_printed = 0L;
385         long a = 0;
386         char buf[SIZ];
387         static FILE *fp = NULL;
388
389         switch (setup_type) {
390
391         case UI_TEXT:
392                 if (curr == 0) {
393                         printf("%s\n", text);
394                         printf("..........................");
395                         printf("..........................");
396                         printf("..........................\r");
397                         fflush(stdout);
398                         dots_printed = 0;
399                 } else if (curr == cmax) {
400                         printf("\r%79s\n", "");
401                 } else {
402                         a = (curr * 100) / cmax;
403                         a = a * 78;
404                         a = a / 100;
405                         while (dots_printed < a) {
406                                 printf("*");
407                                 ++dots_printed;
408                                 fflush(stdout);
409                         }
410                 }
411                 break;
412
413         case UI_DIALOG:
414                 if (curr == 0) {
415                         sprintf(buf, "exec %s --gauge '%s' 7 72 0",
416                                 getenv("CTDL_DIALOG"),
417                                 text);
418                         fp = popen(buf, "w");
419                         if (fp != NULL) {
420                                 fprintf(fp, "0\n");
421                                 fflush(fp);
422                         }
423                 } 
424                 else if (curr == cmax) {
425                         if (fp != NULL) {
426                                 fprintf(fp, "100\n");
427                                 pclose(fp);
428                                 fp = NULL;
429                         }
430                 }
431                 else {
432                         a = (curr * 100) / cmax;
433                         if (fp != NULL) {
434                                 fprintf(fp, "%ld\n", a);
435                                 fflush(fp);
436                         }
437                 }
438                 break;
439
440 #ifdef HAVE_NEWT
441         case UI_NEWT:
442                 if (curr == 0) {
443                         newtCenteredWindow(76, 8, text);
444                         form = newtForm(NULL, NULL, 0);
445                         scale = newtScale(1, 3, 74, cmax);
446                         newtFormAddComponent(form, scale);
447                         newtDrawForm(form);
448                         newtRefresh();
449                 }
450                 if ((curr > 0) && (curr <= cmax)) {
451                         newtScaleSet(scale, curr);
452                         newtRefresh();
453                 }
454                 if (curr == cmax) {
455                         newtFormDestroy(form);  
456                         newtPopWindow();
457                         newtRefresh();
458                 }
459                 break;
460 #endif
461
462         }
463 }
464
465
466
467
468 /*
469  * check_inittab_entry()  -- Make sure "webserver" is in /etc/inittab
470  *
471  */
472 void check_inittab_entry(void)
473 {
474         FILE *infp;
475         char buf[SIZ];
476         char looking_for[SIZ];
477         char question[SIZ];
478         char entryname[5];
479         char http_port[128];
480 #ifdef HAVE_OPENSSL
481         char https_port[128];
482 #endif
483         char hostname[128];
484         char portname[128];
485         struct utsname my_utsname;
486
487         /* Determine the fully qualified path name of webserver */
488         snprintf(looking_for, sizeof looking_for, "%s/webserver", setup_directory);
489
490         /* If there's already an entry, then we have nothing left to do. */
491         if (strlen(init_entry) > 0) {
492                 return;
493         }
494
495         /* Otherwise, prompt the user to create an entry. */
496         snprintf(question, sizeof question,
497                 "There is no '%s' entry in /etc/inittab.\n"
498                 "Would you like to add one?",
499                 looking_for);
500         if (yesno(question) == 0)
501                 return;
502
503         snprintf(question, sizeof question,
504                 "On which port do you want WebCit to listen for HTTP "
505                 "requests?\n\nYou can use the standard port (80) if you are "
506                 "not running another\nweb server (such as Apache), otherwise "
507                 "select another port.");
508         sprintf(http_port, "2000");
509         set_value(question, http_port);
510         uname(&my_utsname);
511         sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
512
513 #ifdef HAVE_OPENSSL
514         snprintf(question, sizeof question,
515                 "On which port do you want WebCit to listen for HTTPS "
516                 "requests?\n\nYou can use the standard port (443) if you are "
517                 "not running another\nweb server (such as Apache), otherwise "
518                 "select another port.");
519         sprintf(https_port, "443");
520         set_value(question, https_port);
521 #endif
522
523         /* Find out where Citadel is. */
524         if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
525                 strcpy(hostname, "uds");
526                 strcpy(portname, getenv("CITADEL"));
527         }
528         else {
529                 snprintf(question, sizeof question,
530                         "Is the Citadel service running on the same host as WebCit?");
531                 if (yesno(question)) {
532                         sprintf(hostname, "uds");
533                         sprintf(portname, "/usr/local/citadel");
534                         set_value("In what directory is Citadel installed?", portname);
535                 }
536                 else {
537                         sprintf(hostname, "127.0.0.1");
538                         sprintf(portname, "504");
539                         set_value("Enter the host name or IP address of your "
540                                 "Citadel server.", hostname);
541                         set_value("Enter the port number on which Citadel is "
542                                 "running (usually 504)", portname);
543                 }
544         }
545
546         /* Generate unique entry names for /etc/inittab */
547         snprintf(entryname, sizeof entryname, "c0");
548         do {
549                 ++entryname[1];
550                 if (entryname[1] > '9') {
551                         entryname[1] = 0;
552                         ++entryname[0];
553                         if (entryname[0] > 'z') {
554                                 display_error(
555                                    "Can't generate a unique entry name");
556                                 return;
557                         }
558                 }
559                 snprintf(buf, sizeof buf,
560                      "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
561         } while (system(buf) == 0);
562         
563
564         /* Now write it out to /etc/inittab */
565         infp = fopen("/etc/inittab", "a");
566         if (infp == NULL) {
567                 display_error(strerror(errno));
568         } else {
569                 fprintf(infp, "# Start the WebCit server...\n");
570                 fprintf(infp, "h%s:2345:respawn:%s -p%s %s %s\n",
571                         entryname, looking_for,
572                         http_port, hostname, portname);
573 #ifdef HAVE_OPENSSL
574                 fprintf(infp, "s%s:2345:respawn:%s -p%s -s %s %s\n",
575                         entryname, looking_for,
576                         https_port, hostname, portname);
577 #endif
578                 fclose(infp);
579                 strcpy(init_entry, entryname);
580         }
581 }
582
583
584
585
586 /*
587  * Figure out what type of user interface we're going to use
588  */
589 int discover_ui(void)
590 {
591
592         /* Use "dialog" if we have it */
593         if (getenv("CTDL_DIALOG") != NULL) {
594                 return UI_DIALOG;
595         }
596                 
597 #ifdef HAVE_NEWT
598         newtInit();
599         newtCls();
600         newtDrawRootText(0, 0, "WebCit Setup");
601         return UI_NEWT;
602 #endif
603         return UI_TEXT;
604 }
605
606
607
608
609
610 int main(int argc, char *argv[])
611 {
612         int a;
613         char aaa[256];
614         int info_only = 0;
615         strcpy(suggested_url, "http://<your_host_name>:<port>/");
616
617         /* set an invalid setup type */
618         setup_type = (-1);
619
620         /* Check to see if we're running the web installer */
621         if (getenv("CITADEL_INSTALLER") != NULL) {
622                 using_web_installer = 1;
623         }
624
625         /* parse command line args */
626         for (a = 0; a < argc; ++a) {
627                 if (!strncmp(argv[a], "-u", 2)) {
628                         strcpy(aaa, argv[a]);
629                         strcpy(aaa, &aaa[2]);
630                         setup_type = atoi(aaa);
631                 }
632                 if (!strcmp(argv[a], "-i")) {
633                         info_only = 1;
634                 }
635                 if (!strcmp(argv[a], "-q")) {
636                         setup_type = UI_SILENT;
637                 }
638         }
639
640
641         /* If a setup type was not specified, try to determine automatically
642          * the best one to use out of all available types.
643          */
644         if (setup_type < 0) {
645                 setup_type = discover_ui();
646         }
647         if (info_only == 1) {
648                 important_message("WebCit Setup", "Welcome to WebCit setup");
649                 cleanup(0);
650         }
651
652         /* Get started in a valid setup directory. */
653         strcpy(setup_directory, WEBCITDIR);
654         if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
655                 strcpy(setup_directory, getenv("WEBCIT"));
656         }
657         else {
658                 set_value("In what directory is WebCit installed?",
659                         setup_directory);
660         }
661         if (chdir(setup_directory) != 0) {
662                 important_message("WebCit Setup",
663                           "The directory you specified does not exist.");
664                 cleanup(errno);
665         }
666
667         /* See if we need to shut down the WebCit service. */
668         for (a=0; a<=3; ++a) {
669                 progress("Shutting down the WebCit service...", a, 3);
670                 if (a == 0) shutdown_service();
671                 sleep(1);
672         }
673
674         /* Now begin. */
675         switch (setup_type) {
676
677         case UI_TEXT:
678                 printf("\n\n\n"
679                         "               *** WebCit setup program ***\n\n");
680                 break;
681
682         }
683
684         check_inittab_entry();  /* Check /etc/inittab */
685
686         /* See if we can start the WebCit service. */
687         if (strlen(init_entry) > 0) {
688                 for (a=0; a<=3; ++a) {
689                         progress("Starting the WebCit service...", a, 3);
690                         if (a == 0) start_the_service();
691                         sleep(1);
692                 }
693                 sprintf(aaa,
694                         "Setup is finished.  You may now log in.\n"
695                         "Point your web browser at %s\n", suggested_url);
696                 important_message("Setup finished", aaa);
697         }
698         else {
699                 important_message("Setup finished",
700                         "Setup is finished.  You may now start the server.");
701         }
702
703         cleanup(0);
704         return 0;
705 }