6dd0d51120ebcb49038a897cee2b01e060a80d3c
[exim.git] / src / src / buildconfig.c
1 /* $Cambridge: exim/src/src/buildconfig.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2004 */
8 /* See the file NOTICE for conditions of use and distribution. */
9
10
11 /*************************************************
12 *       Build configuration header for Exim      *
13 *************************************************/
14
15 /* This auxiliary program builds the file config.h by the following
16 process:
17
18 First it reads Makefile, looking for certain OS-specific definitions which it
19 uses to define macros. Then it reads the defaults file config.h.defaults.
20
21 The defaults file contains normal C #define statements for various macros; if
22 the name of a macro is found in the environment, the environment value replaces
23 the default. If the default #define does not contain any value, then that macro
24 is not copied to the created file unless there is some value in the
25 environment.
26
27 This program is compiled and run as part of the Make process and is not
28 normally called independently. */
29
30
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <pwd.h>
37 #include <grp.h>
38
39 typedef struct {
40   char *name;
41   int *flag;
42 } have_item;
43
44 typedef struct {
45   char *name;
46   char *data;
47 } save_item;
48
49 static char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" };
50
51 static int have_ipv6 = 0;
52 static int have_iconv = 0;
53
54 static char errno_quota[256];
55 static char ostype[256];
56 static char cc[256];
57
58 /* If any entry is an initial substring of another, the longer one must
59 appear first. */
60
61 static have_item have_list[] = {
62   { "HAVE_IPV6",      &have_ipv6 },
63   { "HAVE_ICONV",     &have_iconv },
64   { NULL, NULL}
65 };
66
67 static save_item save_list[] = {
68   { "ERRNO_QUOTA",    errno_quota },
69   { "OSTYPE",         ostype },
70   { "CC",             cc },
71   { NULL, NULL}
72 };
73
74
75 /* Subroutine to check a string for precisely one instance of "%s". If not,
76 bomb out. */
77
78 void
79 check_percent_ess(char *value, char *name)
80 {
81 int OK = 0;
82 char *p = strstr(value, "%s");
83 if (p != NULL) OK = strstr(p+2, "%s") == NULL;
84 if (!OK)
85   {
86   printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n"
87     "*** \"%%s\". Please review your build-time configuration.\n\n/", value,
88     name);
89   exit(1);
90   }
91 }
92
93
94 /* Main program */
95
96 int
97 main(int argc, char **argv)
98 {
99 FILE *base;
100 FILE *new;
101 int last_initial = 'A';
102 int linecount = 0;
103 int have_auth = 0;
104 int in_local_makefile = 0;
105 int use_which_db = 0;
106 int use_which_db_in_local_makefile = 0;
107 int support_crypteq = 0;
108 char buffer[1024];
109
110 if (argc != 1)
111   {
112   printf("*** Buildconfig: called with incorrect arguments\n");
113   exit(1);
114   }
115
116 new = fopen("config.h", "wb");
117 if (new == NULL)
118   {
119   printf("*** Buildconfig: failed to open config.h for output\n");
120   exit(1);
121   }
122
123 printf("Building configuration file config.h\n");
124
125 fprintf(new, "/*************************************************\n");
126 fprintf(new, "*           Configuration header for Exim        *\n");
127 fprintf(new, "*************************************************/\n\n");
128
129 fprintf(new, "/* This file was automatically generated from Makefile and "
130   "config.h.defaults,\n");
131 fprintf(new, "using values specified in the configuration file Local/Makefile.\n");
132 fprintf(new, "Do not edit it. Instead, edit Local/Makefile and "
133   "rerun make. */\n\n");
134
135 /* First, search the makefile for certain settings */
136
137 base = fopen("Makefile", "rb");
138 if (base == NULL)
139   {
140   printf("*** Buildconfig: failed to open Makefile\n");
141   fclose(new);
142   exit(1);
143   }
144
145 errno_quota[0] = 0;    /* no over-riding value set */
146 ostype[0] = 0;         /* just in case */
147 cc[0] = 0;
148
149 while (fgets(buffer, sizeof(buffer), base) != NULL)
150   {
151   int i;
152   have_item *h;
153   save_item *s;
154   char *p = buffer + (int)strlen(buffer);
155   linecount++;
156   while (p > buffer && isspace((unsigned char)p[-1])) p--;
157   *p = 0;
158   p = buffer;
159   while (isspace((unsigned char)*p)) p++;
160
161   /* Notice when we hit the user's makefile */
162
163   if (strcmp(p, "# From Local/Makefile") == 0)
164     {
165     in_local_makefile = 1;
166     continue;
167     }
168
169   /* Remember the last DB option setting. If we hit two in the user's
170   Makefile, complain. */
171
172   for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
173     {
174     int len = (int)strlen(db_opts[i]);
175     if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
176       {
177       if (in_local_makefile)
178         {
179         if (use_which_db_in_local_makefile)
180           {
181           printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
182             "defined in Local/Makefile\n");
183           exit(1);
184           }
185         use_which_db_in_local_makefile = 1;
186         }
187       use_which_db = i;
188       break;
189       }
190     }
191   if (i < sizeof(db_opts)/sizeof(char *)) continue;
192
193   /* Items where we just save a boolean */
194
195   for (h = have_list; h->name != NULL; h++)
196     {
197     int len = (int)strlen(h->name);
198     if (strncmp(p, h->name, len) == 0)
199       {
200       p += len;
201       while (isspace((unsigned char)*p)) p++;
202       if (*p++ != '=')
203         {
204         printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
205         exit(1);
206         }
207       while (isspace((unsigned char)*p)) p++;
208       if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1;
209         else *(h->flag) = 0;   /* Must reset in case multiple instances */
210       break;
211       }
212     }
213
214   if (h->name != NULL) continue;
215
216   /* Items where we save the complete string */
217
218   for (s = save_list; s->name != NULL; s++)
219     {
220     int len = (int)strlen(s->name);
221     if (strncmp(p, s->name, len) == 0)
222       {
223       p += len;
224       while (isspace((unsigned char)*p)) p++;
225       if (*p++ != '=')
226         {
227         printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
228         exit(1);
229         }
230       while (isspace((unsigned char)*p)) p++;
231       strcpy(s->data, p);
232       }
233     }
234   }
235
236 fprintf(new, "#define HAVE_IPV6             %s\n",
237   have_ipv6? "TRUE" : "FALSE");
238
239 fprintf(new, "#define HAVE_ICONV            %s\n",
240   have_iconv? "TRUE" : "FALSE");
241
242 if (errno_quota[0] != 0)
243   fprintf(new, "\n#define ERRNO_QUOTA           %s\n", errno_quota);
244
245 if (strcmp(cc, "gcc") == 0 && strstr(ostype, "IRIX") != NULL)
246   {
247   fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */");
248   fprintf(new, "\n/* bug when using gcc on an IRIX system. */");
249   fprintf(new, "\n#define USE_INET_NTOA_FIX");
250   }
251
252 fprintf(new, "\n");
253 fclose(base);
254
255
256 /* Now handle the macros listed in the defaults */
257
258 base = fopen("../src/config.h.defaults", "rb");
259 if (base == NULL)
260   {
261   printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
262   fclose(new);
263   exit(1);
264   }
265
266 while (fgets(buffer, sizeof(buffer), base) != NULL)
267   {
268   int i;
269   char name[256];
270   char *value;
271   char *p = buffer;
272   char *q = name;
273
274   while (*p == ' ' || *p == '\t') p++;
275
276   if (strncmp(p, "#define ", 8) != 0) continue;
277
278   p += 8;
279   while (*p == ' ' || *p == '\t') p++;
280
281   if (*p < last_initial) fprintf(new, "\n");
282   last_initial = *p;
283
284   while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
285   *q = 0;
286
287   /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only
288   one of them set. The scan of the Makefile has saved which was the last one
289   encountered. */
290
291   for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
292     {
293     if (strcmp(name, db_opts[i]) == 0)
294       {
295       if (use_which_db == i)
296         fprintf(new, "#define %s %.*syes\n", db_opts[i],
297           21 - (int)strlen(db_opts[i]), "                         ");
298       else
299         fprintf(new, "/* %s not set */\n", name);
300       break;
301       }
302     }
303   if (i < sizeof(db_opts)/sizeof(char *)) continue;
304
305   /* EXIM_USER is a special case. We look in the environment for EXIM_USER or
306   EXIM_UID (the latter for backward compatibility with Exim 3). If the value is
307   not numeric, we look up the user, and default the GID if found. Otherwise,
308   EXIM_GROUP or EXIM_GID must be in the environment. */
309
310   if (strcmp(name, "EXIM_UID") == 0)
311     {
312     uid_t uid = 0;
313     gid_t gid = 0;
314     int gid_set = 0;
315     char *username = NULL;
316     char *groupname = NULL;
317     char *s;
318     char *user = getenv("EXIM_USER");
319     char *group = getenv("EXIM_GROUP");
320
321     if (user == NULL) user = getenv("EXIM_UID");
322     if (group == NULL) group = getenv("EXIM_GID");
323
324     if (user == NULL)
325       {
326       printf("\n*** EXIM_USER has not been defined in any of the Makefiles in "
327         "the\n    \"Local\" directory. Please review your build-time "
328         "configuration.\n\n");
329       return 1;
330       }
331
332     while (isspace((unsigned char)(*user))) user++;
333     if (*user == 0)
334       {
335       printf("\n*** EXIM_USER is defined as an empty string in one of the "
336         "files\n    in the \"Local\" directory. Please review your build-time"
337         "\n    configuration.\n\n");
338       return 1;
339       }
340
341     for (s = user; *s != 0; s++)
342       {
343       if (iscntrl((unsigned char)(*s)))
344         {
345         printf("\n*** EXIM_USER contains the control character 0x%02X in one "
346           "of the files\n    in the \"Local\" directory. Please review your "
347           "build-time\n    configuration.\n\n", *s);
348         return 1;
349         }
350       }
351
352     /* Numeric uid given */
353
354     if (user[strspn(user, "0123456789")] == 0)
355       {
356       uid = (uid_t)atoi(user);
357       }
358
359     /* User name given. Normally, we look up the uid right away. However,
360     people building binary distributions sometimes want to retain the name till
361     runtime. This is supported if the name begins "ref:". */
362
363     else if (strncmp(user, "ref:", 4) == 0)
364       {
365       user += 4;
366       while (isspace(*user)) user++;
367       username = user;
368       gid_set = 1;
369       }
370
371     else
372       {
373       struct passwd *pw = getpwnam(user);
374       if (pw == NULL)
375         {
376         printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
377           "exist.\n    Please review your build-time configuration.\n\n",
378           user);
379         return 1;
380         }
381
382       uid = pw->pw_uid;
383       gid = pw->pw_gid;
384       gid_set = 1;
385       }
386
387     /* Use explicit group if set. */
388
389     if (group != NULL)
390       {
391       while (isspace((unsigned char)(*group))) group++;
392       if (*group == 0)
393         {
394         printf("\n*** EXIM_GROUP is defined as an empty string in one of "
395           "the files in the\n    \"Local\" directory. ");
396         if (gid_set)
397           {
398           printf("If you want the Exim group to be taken from the\n    "
399             "password data for the Exim user, just remove the EXIM_GROUP "
400             "setting.\n    Otherwise, p");
401           }
402         else printf("EXIM_USER is defined numerically, so there is no"
403           "\n    default for EXIM_GROUP and you must set it explicitly.\n    P");
404         printf("lease review your build-time configuration.\n\n");
405         return 1;
406         }
407
408       for (s = group; *s != 0; s++)
409         {
410         if (iscntrl((unsigned char)(*s)))
411           {
412           printf("\n*** EXIM_GROUP contains the control character 0x%02X in one "
413             "of the files\n    in the \"Local\" directory. Please review your "
414             "build-time\n    configuration.\n\n", *s);
415           return 1;
416           }
417         }
418
419       /* Group name given. This may be by reference or to be looked up now,
420       as for user. */
421
422       if (strncmp(group, "ref:", 4) == 0)
423         {
424         group += 4;
425         while (isspace(*group)) group++;
426         groupname = group;
427         }
428
429       else if (username != NULL)
430         {
431         groupname = group;
432         }
433
434       else if (group[strspn(group, "0123456789")] == 0)
435         {
436         gid = (gid_t)atoi(group);
437         }
438
439       else
440         {
441         struct group *gr = getgrnam(group);
442         if (gr == NULL)
443           {
444           printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
445             "not exist.\n   Please review your build-time configuration.\n\n",
446             group);
447           return 1;
448           }
449         gid = gr->gr_gid;
450         }
451       }
452
453     /* Else trouble unless found in passwd file with user */
454
455     else if (!gid_set)
456       {
457       printf("\n*** No group set for Exim. Please review your build-time "
458         "configuration.\n\n");
459       return 1;
460       }
461
462     /* Output user and group names or uid/gid. When names are set, uid/gid
463     are set to zero but will be replaced at runtime. */
464
465     if (username != NULL)
466       fprintf(new, "#define EXIM_USERNAME         \"%s\"\n", username);
467     if (groupname != NULL)
468       fprintf(new, "#define EXIM_GROUPNAME        \"%s\"\n", groupname);
469
470     fprintf(new, "#define EXIM_UID              %d\n", (int)uid);
471     fprintf(new, "#define EXIM_GID              %d\n", (int)gid);
472     continue;
473     }
474
475   /* CONFIGURE_OWNER is a special case. We look in the environment for
476   CONFIGURE_OWNER. If the value is not numeric, we look up the user. A lot of
477   this code is similar to that for EXIM_USER, but we aren't interested in a gid
478   here, and it's all optional, so just keep it separate. */
479
480   if (strcmp(name, "CONFIGURE_OWNER") == 0)
481     {
482     uid_t uid = 0;
483     char *s;
484     char *username = NULL;
485     char *user = getenv("CONFIGURE_OWNER");
486
487     if (user == NULL) user = "";
488     while (isspace((unsigned char)(*user))) user++;
489     if (*user == 0)
490       {
491       fprintf(new, "/* %s not set */\n", name);
492       continue;
493       }
494
495     for (s = user; *s != 0; s++)
496       {
497       if (iscntrl((unsigned char)(*s)))
498         {
499         printf("\n*** CONFIGURE_OWNER contains the control character 0x%02X in "
500           "one of the files\n    in the \"Local\" directory. Please review "
501           "your build-time\n    configuration.\n\n", *s);
502         return 1;
503         }
504       }
505
506     /* Numeric uid given */
507
508     if (user[strspn(user, "0123456789")] == 0)
509       {
510       uid = (uid_t)atoi(user);
511       }
512
513     /* User name given. Normally, we look up the uid right away. However,
514     people building binary distributions sometimes want to retain the name till
515     runtime. This is supported if the name begins "ref:". */
516
517     else if (strncmp(user, "ref:", 4) == 0)
518       {
519       user += 4;
520       while (isspace(*user)) user++;
521       username = user;
522       }
523
524     else
525       {
526       struct passwd *pw = getpwnam(user);
527       if (pw == NULL)
528         {
529         printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
530           "exist.\n    Please review your build-time configuration.\n\n",
531           user);
532         return 1;
533         }
534
535       uid = pw->pw_uid;
536       }
537
538     /* Output user and group names or uid/gid. When names are set, uid/gid
539     are set to zero but will be replaced at runtime. */
540
541     if (username != NULL)
542       fprintf(new, "#define CONFIGURE_OWNERNAME         \"%s\"\n", username);
543     fprintf(new, "#define CONFIGURE_OWNER              %d\n", (int)uid);
544     continue;
545     }
546
547   /* FIXED_NEVER_USERS is another special case. Look up the uid values and
548   create suitable initialization data for a vector. */
549
550   if (strcmp(name, "FIXED_NEVER_USERS") == 0)
551     {
552     char *list = getenv("FIXED_NEVER_USERS");
553     if (list == NULL)
554       {
555       fprintf(new, "#define FIXED_NEVER_USERS     0\n");
556       }
557     else
558       {
559       int count = 1;
560       int i;
561       uid_t *vector;
562       char *p = list;
563       while (*p != 0) if (*p++ == ':') count++;
564
565       vector = malloc((count+1) * sizeof(uid_t));
566       vector[0] = (uid_t)count;
567
568       for (i = 1; i <= count; list++, i++)
569         {
570         char name[64];
571         p = list;
572         while (*list != 0 && *list != ':') list++;
573         strncpy(name, p, list-p);
574         name[list-p] = 0;
575
576         if (name[strspn(name, "0123456789")] == 0)
577           {
578           vector[i] = (uid_t)atoi(name);
579           }
580         else
581           {
582           struct passwd *pw = getpwnam(name);
583           if (pw == NULL)
584             {
585             printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
586               "exist.\n    Please review your build-time configuration.\n\n",
587               name);
588             return 1;
589             }
590           vector[i] = pw->pw_uid;
591           }
592         }
593       fprintf(new, "#define FIXED_NEVER_USERS     %d, ", count);
594       for (i = 1; i <= count - 1; i++)
595         fprintf(new, "%d, ", (unsigned int)vector[i]);
596       fprintf(new, "%d\n", (unsigned int)vector[i]);
597       }
598     continue;
599     }
600
601   /* Otherwise, check whether a value exists in the environment. Remember if
602   it is an AUTH setting or SUPPORT_CRYPTEQ. */
603
604   if ((value = getenv(name)) != NULL)
605     {
606     int len;
607     len = 21 - (int)strlen(name);
608
609     if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
610     if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
611
612     /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
613
614     if (strcmp(name, "LDAP_LIB_TYPE") == 0)
615       {
616       if (strcmp(value, "NETSCAPE") == 0 ||
617           strcmp(value, "UMICHIGAN") == 0 ||
618           strcmp(value, "OPENLDAP1") == 0 ||
619           strcmp(value, "OPENLDAP2") == 0 ||
620           strcmp(value, "SOLARIS") == 0 ||
621           strcmp(value, "SOLARIS7") == 0)              /* Compatibility */
622         {
623         fprintf(new, "#define LDAP_LIB_%s\n", value);
624         }
625       else
626         {
627         printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
628           "\n*** Please review your build-time configuration.\n\n", value);
629         return 1;
630         }
631       }
632
633     else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
634       {
635       if (strcmp(value, "RADIUSCLIENT") == 0 ||
636           strcmp(value, "RADLIB") == 0)
637         {
638         fprintf(new, "#define RADIUS_LIB_%s\n", value);
639         }
640       else
641         {
642         printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
643           "\n*** Please review your build-time configuration.\n\n", value);
644         return 1;
645         }
646       }
647
648     /* Other macros get set to the environment value. */
649
650     else
651       {
652       fprintf(new, "#define %s ", name);
653       while(len-- > 0) fputc(' ', new);
654
655       /* LOG_FILE_PATH is now messy because it can be a path containing %s or
656       it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
657
658       if (strcmp(name, "LOG_FILE_PATH") == 0)
659         {
660         char *ss = value;
661         for(;;)
662           {
663           char *pp;
664           char *sss = strchr(ss, ':');
665           if (sss != NULL)
666             {
667             strncpy(buffer, ss, sss-ss);
668             buffer[sss-ss] = 0;  /* For empty case */
669             }
670           else strcpy(buffer, ss);
671           pp = buffer + (int)strlen(buffer);
672           while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
673           *pp = 0;
674           if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
675             check_percent_ess(buffer, name);
676           if (sss == NULL) break;
677           ss = sss + 1;
678           while (isspace((unsigned char)*ss)) ss++;
679           }
680         fprintf(new, "\"%s\"\n", value);
681         }
682
683       /* Timezone values and HEADERS_CHARSET get quoted */
684
685       else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
686                strcmp(name, "HEADERS_CHARSET") == 0)
687         fprintf(new, "\"%s\"\n", value);
688
689       /* For others, quote any paths and don't quote anything else */
690
691       else
692         {
693         if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
694           else fprintf(new, "%s\n", value);
695         }
696       }
697     }
698
699   /* Value not defined in the environment; use the default */
700
701   else
702     {
703     char *t = p;
704     while (*p == ' ' || *p == '\t') p++;
705     if (*p != '\n') fputs(buffer, new); else
706       {
707       *t = 0;
708       if (strcmp(name, "BIN_DIRECTORY")   == 0 ||
709           strcmp(name, "CONFIGURE_FILE")  == 0)
710         {
711         printf("\n*** %s has not been defined in any of the Makefiles in the\n"
712           "    \"Local\" directory. "
713           "Please review your build-time configuration.\n\n", name);
714         return 1;
715         }
716
717       if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
718         {
719         char *tz = getenv("TZ");
720         fprintf(new, "#define TIMEZONE_DEFAULT      ");
721         if (tz == NULL) fprintf(new, "NULL\n"); else
722           fprintf(new, "\"%s\"\n", tz);
723         }
724
725       else fprintf(new, "/* %s not set */\n", name);
726       }
727     }
728   }
729
730 fclose(base);
731
732 /* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
733 defined. */
734
735 if (have_auth)
736   {
737   if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
738     "#define SUPPORT_CRYPTEQ\n");
739   }
740
741 /* End off */
742
743 fprintf(new, "\n/* End of config.h */\n");
744 fclose(new);
745 return 0;
746 }
747
748 /* End of buildconfig.c */