1 /* $Cambridge: exim/src/src/buildconfig.c,v 1.14 2008/01/17 13:03:35 tom Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2007 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 /*************************************************
12 * Build configuration header for Exim *
13 *************************************************/
15 /* This auxiliary program builds the file config.h by the following
18 First, it determines the size of off_t and time_t variables, and generates
19 macro code to define OFF_T_FMT and TIME_T_FMT as suitable formats, if they are
20 not already defined in the system-specific header file.
22 Then it reads Makefile, looking for certain OS-specific definitions which it
23 uses to define some specific macros. Finally, it reads the defaults file
26 The defaults file contains normal C #define statements for various macros; if
27 the name of a macro is found in the environment, the environment value replaces
28 the default. If the default #define does not contain any value, then that macro
29 is not copied to the created file unless there is some value in the
32 This program is compiled and run as part of the Make process and is not
33 normally called independently. */
40 #include <sys/types.h>
54 static char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" };
56 static int have_ipv6 = 0;
57 static int have_iconv = 0;
59 static char errno_quota[256];
60 static char ostype[256];
63 /* If any entry is an initial substring of another, the longer one must
66 static have_item have_list[] = {
67 { "HAVE_IPV6", &have_ipv6 },
68 { "HAVE_ICONV", &have_iconv },
72 static save_item save_list[] = {
73 { "ERRNO_QUOTA", errno_quota },
80 /* Subroutine to check a string for precisely one instance of "%s". If not,
84 check_percent_ess(char *value, char *name)
87 char *p = strstr(value, "%s");
88 if (p != NULL) OK = strstr(p+2, "%s") == NULL;
91 printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n"
92 "*** \"%%s\". Please review your build-time configuration.\n\n/", value,
102 main(int argc, char **argv)
104 off_t test_off_t = 0;
105 time_t test_time_t = 0;
108 int last_initial = 'A';
111 int in_local_makefile = 0;
112 int use_which_db = 0;
113 int use_which_db_in_local_makefile = 0;
114 int support_crypteq = 0;
119 printf("*** Buildconfig: called with incorrect arguments\n");
123 new = fopen("config.h", "wb");
126 printf("*** Buildconfig: failed to open config.h for output\n");
130 printf("Building configuration file config.h\n");
132 fprintf(new, "/*************************************************\n");
133 fprintf(new, "* Configuration header for Exim *\n");
134 fprintf(new, "*************************************************/\n\n");
136 fprintf(new, "/* This file was automatically generated from Makefile and "
137 "config.h.defaults,\n");
138 fprintf(new, "using values specified in the configuration file Local/Makefile.\n");
139 fprintf(new, "Do not edit it. Instead, edit Local/Makefile and "
140 "rerun make. */\n\n");
142 /* First, deal with the printing format for off_t variables. We assume that if
143 the size of off_t is greater than 4, "%lld" will be available as a format for
144 printing long long variables, and there will be support for the long long type.
145 This assumption is known to be OK for the common operating systems. */
147 fprintf(new, "#ifndef OFF_T_FMT\n");
148 if (sizeof(test_off_t) > 4)
150 fprintf(new, "#define OFF_T_FMT \"%%lld\"\n");
151 fprintf(new, "#define LONGLONG_T long long int\n");
155 fprintf(new, "#define OFF_T_FMT \"%%ld\"\n");
156 fprintf(new, "#define LONGLONG_T long int\n");
158 fprintf(new, "#endif\n\n");
160 /* Now do the same thing for time_t variables. If the length is greater than
161 4, we want to assume long long support (even if off_t was less than 4). If the
162 length is 4 or less, we can leave LONGLONG_T to whatever was defined above for
165 fprintf(new, "#ifndef TIME_T_FMT\n");
166 if (sizeof(test_time_t) > 4)
168 fprintf(new, "#define TIME_T_FMT \"%%lld\"\n");
169 fprintf(new, "#undef LONGLONG_T\n");
170 fprintf(new, "#define LONGLONG_T long long int\n");
174 fprintf(new, "#define TIME_T_FMT \"%%ld\"\n");
176 fprintf(new, "#endif\n\n");
178 /* Now search the makefile for certain settings */
180 base = fopen("Makefile", "rb");
183 printf("*** Buildconfig: failed to open Makefile\n");
188 errno_quota[0] = 0; /* no over-riding value set */
189 ostype[0] = 0; /* just in case */
192 while (fgets(buffer, sizeof(buffer), base) != NULL)
197 char *p = buffer + (int)strlen(buffer);
199 while (p > buffer && isspace((unsigned char)p[-1])) p--;
202 while (isspace((unsigned char)*p)) p++;
204 /* Notice when we hit the user's makefile */
206 if (strcmp(p, "# From Local/Makefile") == 0)
208 in_local_makefile = 1;
212 /* Remember the last DB option setting. If we hit two in the user's
213 Makefile, complain. */
215 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
217 int len = (int)strlen(db_opts[i]);
218 if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
220 if (in_local_makefile)
222 if (use_which_db_in_local_makefile)
224 printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
225 "defined in Local/Makefile\n");
228 use_which_db_in_local_makefile = 1;
234 if (i < sizeof(db_opts)/sizeof(char *)) continue;
236 /* Items where we just save a boolean */
238 for (h = have_list; h->name != NULL; h++)
240 int len = (int)strlen(h->name);
241 if (strncmp(p, h->name, len) == 0)
244 while (isspace((unsigned char)*p)) p++;
247 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
250 while (isspace((unsigned char)*p)) p++;
251 if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1;
252 else *(h->flag) = 0; /* Must reset in case multiple instances */
257 if (h->name != NULL) continue;
259 /* Items where we save the complete string */
261 for (s = save_list; s->name != NULL; s++)
263 int len = (int)strlen(s->name);
264 if (strncmp(p, s->name, len) == 0)
267 while (isspace((unsigned char)*p)) p++;
270 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
273 while (isspace((unsigned char)*p)) p++;
279 fprintf(new, "#define HAVE_IPV6 %s\n",
280 have_ipv6? "TRUE" : "FALSE");
282 fprintf(new, "#define HAVE_ICONV %s\n",
283 have_iconv? "TRUE" : "FALSE");
285 if (errno_quota[0] != 0)
286 fprintf(new, "\n#define ERRNO_QUOTA %s\n", errno_quota);
288 if (strcmp(cc, "gcc") == 0 &&
289 (strstr(ostype, "IRIX") != NULL || strstr(ostype, "AIX") != NULL))
291 fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */");
292 fprintf(new, "\n/* bug when using gcc on an IRIX or AIX system. */");
293 fprintf(new, "\n#define USE_INET_NTOA_FIX");
300 /* Now handle the macros listed in the defaults */
302 base = fopen("../src/config.h.defaults", "rb");
305 printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
310 while (fgets(buffer, sizeof(buffer), base) != NULL)
318 while (*p == ' ' || *p == '\t') p++;
320 if (strncmp(p, "#define ", 8) != 0) continue;
323 while (*p == ' ' || *p == '\t') p++;
325 if (*p < last_initial) fprintf(new, "\n");
328 while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
331 /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only
332 one of them set. The scan of the Makefile has saved which was the last one
335 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
337 if (strcmp(name, db_opts[i]) == 0)
339 if (use_which_db == i)
340 fprintf(new, "#define %s %.*syes\n", db_opts[i],
341 21 - (int)strlen(db_opts[i]), " ");
343 fprintf(new, "/* %s not set */\n", name);
347 if (i < sizeof(db_opts)/sizeof(char *)) continue;
349 /* EXIM_USER is a special case. We look in the environment for EXIM_USER or
350 EXIM_UID (the latter for backward compatibility with Exim 3). If the value is
351 not numeric, we look up the user, and default the GID if found. Otherwise,
352 EXIM_GROUP or EXIM_GID must be in the environment. */
354 if (strcmp(name, "EXIM_UID") == 0)
359 char *username = NULL;
360 char *groupname = NULL;
362 char *user = getenv("EXIM_USER");
363 char *group = getenv("EXIM_GROUP");
365 if (user == NULL) user = getenv("EXIM_UID");
366 if (group == NULL) group = getenv("EXIM_GID");
370 printf("\n*** EXIM_USER has not been defined in any of the Makefiles in "
371 "the\n \"Local\" directory. Please review your build-time "
372 "configuration.\n\n");
376 while (isspace((unsigned char)(*user))) user++;
379 printf("\n*** EXIM_USER is defined as an empty string in one of the "
380 "files\n in the \"Local\" directory. Please review your build-time"
381 "\n configuration.\n\n");
385 for (s = user; *s != 0; s++)
387 if (iscntrl((unsigned char)(*s)))
389 printf("\n*** EXIM_USER contains the control character 0x%02X in one "
390 "of the files\n in the \"Local\" directory. Please review your "
391 "build-time\n configuration.\n\n", *s);
396 /* Numeric uid given */
398 if (user[strspn(user, "0123456789")] == 0)
400 uid = (uid_t)atoi(user);
403 /* User name given. Normally, we look up the uid right away. However,
404 people building binary distributions sometimes want to retain the name till
405 runtime. This is supported if the name begins "ref:". */
407 else if (strncmp(user, "ref:", 4) == 0)
410 while (isspace(*user)) user++;
417 struct passwd *pw = getpwnam(user);
420 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
421 "exist.\n Please review your build-time configuration.\n\n",
431 /* Use explicit group if set. */
435 while (isspace((unsigned char)(*group))) group++;
438 printf("\n*** EXIM_GROUP is defined as an empty string in one of "
439 "the files in the\n \"Local\" directory. ");
442 printf("If you want the Exim group to be taken from the\n "
443 "password data for the Exim user, just remove the EXIM_GROUP "
444 "setting.\n Otherwise, p");
446 else printf("EXIM_USER is defined numerically, so there is no"
447 "\n default for EXIM_GROUP and you must set it explicitly.\n P");
448 printf("lease review your build-time configuration.\n\n");
452 for (s = group; *s != 0; s++)
454 if (iscntrl((unsigned char)(*s)))
456 printf("\n*** EXIM_GROUP contains the control character 0x%02X in one "
457 "of the files\n in the \"Local\" directory. Please review your "
458 "build-time\n configuration.\n\n", *s);
463 /* Group name given. This may be by reference or to be looked up now,
466 if (strncmp(group, "ref:", 4) == 0)
469 while (isspace(*group)) group++;
473 else if (username != NULL)
478 else if (group[strspn(group, "0123456789")] == 0)
480 gid = (gid_t)atoi(group);
485 struct group *gr = getgrnam(group);
488 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
489 "not exist.\n Please review your build-time configuration.\n\n",
497 /* Else trouble unless found in passwd file with user */
501 printf("\n*** No group set for Exim. Please review your build-time "
502 "configuration.\n\n");
506 /* Output user and group names or uid/gid. When names are set, uid/gid
507 are set to zero but will be replaced at runtime. */
509 if (username != NULL)
510 fprintf(new, "#define EXIM_USERNAME \"%s\"\n", username);
511 if (groupname != NULL)
512 fprintf(new, "#define EXIM_GROUPNAME \"%s\"\n", groupname);
514 fprintf(new, "#define EXIM_UID %d\n", (int)uid);
515 fprintf(new, "#define EXIM_GID %d\n", (int)gid);
519 /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
520 environment for first. If the value is not numeric, we look up the user or
521 group. A lot of this code is similar to that for EXIM_USER, but it's easier
522 to keep it separate. */
524 if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
525 strcmp(name, "CONFIGURE_GROUP") == 0)
527 int isgroup = name[10] == 'G';
531 char *username = NULL;
532 char *user = getenv(name);
534 if (user == NULL) user = "";
535 while (isspace((unsigned char)(*user))) user++;
538 fprintf(new, "/* %s not set */\n", name);
542 for (s = user; *s != 0; s++)
544 if (iscntrl((unsigned char)(*s)))
546 printf("\n*** %s contains the control character 0x%02X in "
547 "one of the files\n in the \"Local\" directory. Please review "
548 "your build-time\n configuration.\n\n", name, *s);
553 /* Numeric uid given */
555 if (user[strspn(user, "0123456789")] == 0)
558 gid = (gid_t)atoi(user);
560 uid = (uid_t)atoi(user);
563 /* Name given. Normally, we look up the uid or gid right away. However,
564 people building binary distributions sometimes want to retain the name till
565 runtime. This is supported if the name begins "ref:". */
567 else if (strncmp(user, "ref:", 4) == 0)
570 while (isspace(*user)) user++;
576 struct group *gr = getgrnam(user);
579 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
580 "exist.\n Please review your build-time configuration.\n\n",
589 struct passwd *pw = getpwnam(user);
592 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
593 "exist.\n Please review your build-time configuration.\n\n",
600 /* Output user and group names or uid/gid. When names are set, uid/gid
601 are set to zero but will be replaced at runtime. */
603 if (username != NULL)
606 fprintf(new, "#define CONFIGURE_GROUPNAME \"%s\"\n", username);
608 fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username);
612 fprintf(new, "#define CONFIGURE_GROUP %d\n", (int)gid);
614 fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid);
618 /* FIXED_NEVER_USERS is another special case. Look up the uid values and
619 create suitable initialization data for a vector. */
621 if (strcmp(name, "FIXED_NEVER_USERS") == 0)
623 char *list = getenv("FIXED_NEVER_USERS");
626 fprintf(new, "#define FIXED_NEVER_USERS 0\n");
634 while (*p != 0) if (*p++ == ':') count++;
636 vector = malloc((count+1) * sizeof(uid_t));
637 vector[0] = (uid_t)count;
639 for (i = 1, j = 0; i <= count; list++, i++)
644 while (*list != 0 && *list != ':') list++;
645 strncpy(name, p, list-p);
652 else if (name[strspn(name, "0123456789")] == 0)
654 vector[j++] = (uid_t)atoi(name);
658 struct passwd *pw = getpwnam(name);
661 printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
662 "exist.\n Please review your build-time configuration.\n\n",
666 vector[j++] = pw->pw_uid;
669 fprintf(new, "#define FIXED_NEVER_USERS %d", j);
670 for (i = 0; i < j; i++) fprintf(new, ", %d", (unsigned int)vector[i]);
676 /* WITH_CONTENT_SCAN is another special case: it must be set if either it or
677 WITH_OLD_DEMIME is set. */
679 if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
681 char *wcs = getenv("WITH_CONTENT_SCAN");
682 char *wod = getenv("WITH_OLD_DEMIME");
683 char *dcc = getenv("EXPERIMENTAL_DCC");
684 if (wcs != NULL || wod != NULL || dcc != NULL)
685 fprintf(new, "#define WITH_CONTENT_SCAN yes\n");
686 else fprintf(new, "/* WITH_CONTENT_SCAN not set */\n");
690 /* Otherwise, check whether a value exists in the environment. Remember if
691 it is an AUTH setting or SUPPORT_CRYPTEQ. */
693 if ((value = getenv(name)) != NULL)
696 len = 21 - (int)strlen(name);
698 if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
699 if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
701 /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
703 if (strcmp(name, "LDAP_LIB_TYPE") == 0)
705 if (strcmp(value, "NETSCAPE") == 0 ||
706 strcmp(value, "UMICHIGAN") == 0 ||
707 strcmp(value, "OPENLDAP1") == 0 ||
708 strcmp(value, "OPENLDAP2") == 0 ||
709 strcmp(value, "SOLARIS") == 0 ||
710 strcmp(value, "SOLARIS7") == 0) /* Compatibility */
712 fprintf(new, "#define LDAP_LIB_%s\n", value);
716 printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
717 "\n*** Please review your build-time configuration.\n\n", value);
722 else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
724 if (strcmp(value, "RADIUSCLIENT") == 0 ||
725 strcmp(value, "RADIUSCLIENTNEW") == 0 ||
726 strcmp(value, "RADLIB") == 0)
728 fprintf(new, "#define RADIUS_LIB_%s\n", value);
732 printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
733 "\n*** Please review your build-time configuration.\n\n", value);
738 /* Other macros get set to the environment value. */
742 fprintf(new, "#define %s ", name);
743 while(len-- > 0) fputc(' ', new);
745 /* LOG_FILE_PATH is now messy because it can be a path containing %s or
746 it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
748 if (strcmp(name, "LOG_FILE_PATH") == 0)
754 char *sss = strchr(ss, ':');
757 strncpy(buffer, ss, sss-ss);
758 buffer[sss-ss] = 0; /* For empty case */
760 else strcpy(buffer, ss);
761 pp = buffer + (int)strlen(buffer);
762 while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
764 if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
765 check_percent_ess(buffer, name);
766 if (sss == NULL) break;
768 while (isspace((unsigned char)*ss)) ss++;
770 fprintf(new, "\"%s\"\n", value);
773 /* Timezone values and HEADERS_CHARSET get quoted */
775 else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
776 strcmp(name, "HEADERS_CHARSET") == 0)
777 fprintf(new, "\"%s\"\n", value);
779 /* For others, quote any paths and don't quote anything else */
783 if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
784 else fprintf(new, "%s\n", value);
789 /* Value not defined in the environment; use the default */
794 while (*p == ' ' || *p == '\t') p++;
795 if (*p != '\n') fputs(buffer, new); else
798 if (strcmp(name, "BIN_DIRECTORY") == 0 ||
799 strcmp(name, "CONFIGURE_FILE") == 0)
801 printf("\n*** %s has not been defined in any of the Makefiles in the\n"
802 " \"Local\" directory. "
803 "Please review your build-time configuration.\n\n", name);
807 if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
809 char *tz = getenv("TZ");
810 fprintf(new, "#define TIMEZONE_DEFAULT ");
811 if (tz == NULL) fprintf(new, "NULL\n"); else
812 fprintf(new, "\"%s\"\n", tz);
815 else fprintf(new, "/* %s not set */\n", name);
822 /* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
827 if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
828 "#define SUPPORT_CRYPTEQ\n");
833 fprintf(new, "\n/* End of config.h */\n");
838 /* End of buildconfig.c */