* Added a skeleton "setup" program to the tree. It does not work yet.
[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
389         /* Determine the fully qualified path name of citserver */
390         snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
391
392         /* If there's already an entry, then we have nothing left to do. */
393         if (strlen(init_entry) > 0) {
394                 return;
395         }
396
397         /* Otherwise, prompt the user to create an entry. */
398         snprintf(question, sizeof question,
399                 "There is no '%s' entry in /etc/inittab.\n"
400                 "Would you like to add one?",
401                 looking_for);
402         if (yesno(question) == 0)
403                 return;
404
405         /* Generate a unique entry name for /etc/inittab */
406         snprintf(entryname, sizeof entryname, "c0");
407         do {
408                 ++entryname[1];
409                 if (entryname[1] > '9') {
410                         entryname[1] = 0;
411                         ++entryname[0];
412                         if (entryname[0] > 'z') {
413                                 display_error(
414                                    "Can't generate a unique entry name");
415                                 return;
416                         }
417                 }
418                 snprintf(buf, sizeof buf,
419                      "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
420         } while (system(buf) == 0);
421
422         /* Now write it out to /etc/inittab */
423         infp = fopen("/etc/inittab", "a");
424         if (infp == NULL) {
425                 display_error(strerror(errno));
426         } else {
427                 fprintf(infp, "# Start the WebCit server...\n");
428                 fprintf(infp, "%s:2345:respawn:%s -h%s\n",
429                         entryname, looking_for, setup_directory);
430                 fclose(infp);
431                 strcpy(init_entry, entryname);
432         }
433 }
434
435
436
437
438 /* 
439  * Check to see if our server really works.  Returns 0 on success.
440  */
441 int test_server(void) {
442
443         return 0;               /* FIXME ... stubbed out */
444
445 }
446
447
448
449
450 /*
451  * Figure out what type of user interface we're going to use
452  */
453 int discover_ui(void)
454 {
455
456 #ifdef HAVE_NEWT
457         newtInit();
458         newtCls();
459         newtDrawRootText(0, 0, "WebCit Setup");
460         return UI_NEWT;
461 #endif
462         return UI_TEXT;
463 }
464
465
466
467
468
469 int main(int argc, char *argv[])
470 {
471         int a;
472         int curr;
473         char aaa[128];
474         FILE *fp;
475         int info_only = 0;
476         gid_t gid;
477
478         /* set an invalid setup type */
479         setup_type = (-1);
480
481         /* parse command line args */
482         for (a = 0; a < argc; ++a) {
483                 if (!strncmp(argv[a], "-u", 2)) {
484                         strcpy(aaa, argv[a]);
485                         strcpy(aaa, &aaa[2]);
486                         setup_type = atoi(aaa);
487                 }
488                 if (!strcmp(argv[a], "-i")) {
489                         info_only = 1;
490                 }
491                 if (!strcmp(argv[a], "-q")) {
492                         setup_type = UI_SILENT;
493                 }
494         }
495
496
497         /* If a setup type was not specified, try to determine automatically
498          * the best one to use out of all available types.
499          */
500         if (setup_type < 0) {
501                 setup_type = discover_ui();
502         }
503         if (info_only == 1) {
504                 important_message("WebCit Setup", "Welcome to WebCit setup");
505                 cleanup(0);
506         }
507
508         /* Get started in a valid setup directory. */
509         strcpy(setup_directory, WEBCITDIR);
510         set_value("FIXME enter your setup directory, dumbass.",
511                 setup_directory);
512         if (chdir(setup_directory) != 0) {
513                 important_message("WebCit Setup",
514                           "The directory you specified does not exist.");
515                 cleanup(errno);
516         }
517
518         /* See if we need to shut down the Citadel service. */
519         for (a=0; a<=3; ++a) {
520                 progress("Shutting down the WebCit service...", a, 3);
521                 if (a == 0) shutdown_service();
522                 sleep(1);
523         }
524
525         /* Make sure it's stopped. */
526         if (test_server() == 0) {
527                 important_message("WebCit Setup",
528                         "The Citadel service is still running.\n"
529                         "Please stop the service manually and run "
530                         "setup again.");
531                 cleanup(1);
532         }
533
534         /* Now begin. */
535         switch (setup_type) {
536
537         case UI_TEXT:
538                 printf("\n\n\n"
539                         "               *** WebCit setup program ***\n\n");
540                 break;
541
542         }
543
544         check_inittab_entry();  /* Check /etc/inittab */
545
546         /* See if we can start the WebCit service. */
547         if (strlen(init_entry) > 0) {
548                 for (a=0; a<=3; ++a) {
549                         progress("Starting the WebCit service...", a, 3);
550                         if (a == 0) start_the_service();
551                         sleep(1);
552                 }
553                 if (test_server() == 0) {
554                         important_message("Setup finished",
555                                 "Setup is finished.  You may now log in.");
556                 }
557                 else {
558                         important_message("Setup finished",
559                                 "Setup is finished, but the WebCit service "
560                                 "failed to start.\n"
561                                 "Go back and check your configuration.");
562                 }
563         }
564         else {
565                 important_message("Setup finished",
566                         "Setup is finished.  You may now start the server.");
567         }
568
569         cleanup(0);
570         return 0;
571 }