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