1 /* $Cambridge: exim/src/src/buildconfig.c,v 1.5 2005/01/04 10:00:42 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2005 */
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 reads Makefile, looking for certain OS-specific definitions which it
19 uses to define macros. Then it reads the defaults file config.h.defaults.
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
27 This program is compiled and run as part of the Make process and is not
28 normally called independently. */
35 #include <sys/types.h>
49 static char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" };
51 static int have_ipv6 = 0;
52 static int have_iconv = 0;
54 static char errno_quota[256];
55 static char ostype[256];
58 /* If any entry is an initial substring of another, the longer one must
61 static have_item have_list[] = {
62 { "HAVE_IPV6", &have_ipv6 },
63 { "HAVE_ICONV", &have_iconv },
67 static save_item save_list[] = {
68 { "ERRNO_QUOTA", errno_quota },
75 /* Subroutine to check a string for precisely one instance of "%s". If not,
79 check_percent_ess(char *value, char *name)
82 char *p = strstr(value, "%s");
83 if (p != NULL) OK = strstr(p+2, "%s") == NULL;
86 printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n"
87 "*** \"%%s\". Please review your build-time configuration.\n\n/", value,
97 main(int argc, char **argv)
101 int last_initial = 'A';
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;
112 printf("*** Buildconfig: called with incorrect arguments\n");
116 new = fopen("config.h", "wb");
119 printf("*** Buildconfig: failed to open config.h for output\n");
123 printf("Building configuration file config.h\n");
125 fprintf(new, "/*************************************************\n");
126 fprintf(new, "* Configuration header for Exim *\n");
127 fprintf(new, "*************************************************/\n\n");
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");
135 /* First, search the makefile for certain settings */
137 base = fopen("Makefile", "rb");
140 printf("*** Buildconfig: failed to open Makefile\n");
145 errno_quota[0] = 0; /* no over-riding value set */
146 ostype[0] = 0; /* just in case */
149 while (fgets(buffer, sizeof(buffer), base) != NULL)
154 char *p = buffer + (int)strlen(buffer);
156 while (p > buffer && isspace((unsigned char)p[-1])) p--;
159 while (isspace((unsigned char)*p)) p++;
161 /* Notice when we hit the user's makefile */
163 if (strcmp(p, "# From Local/Makefile") == 0)
165 in_local_makefile = 1;
169 /* Remember the last DB option setting. If we hit two in the user's
170 Makefile, complain. */
172 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
174 int len = (int)strlen(db_opts[i]);
175 if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
177 if (in_local_makefile)
179 if (use_which_db_in_local_makefile)
181 printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
182 "defined in Local/Makefile\n");
185 use_which_db_in_local_makefile = 1;
191 if (i < sizeof(db_opts)/sizeof(char *)) continue;
193 /* Items where we just save a boolean */
195 for (h = have_list; h->name != NULL; h++)
197 int len = (int)strlen(h->name);
198 if (strncmp(p, h->name, len) == 0)
201 while (isspace((unsigned char)*p)) p++;
204 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
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 */
214 if (h->name != NULL) continue;
216 /* Items where we save the complete string */
218 for (s = save_list; s->name != NULL; s++)
220 int len = (int)strlen(s->name);
221 if (strncmp(p, s->name, len) == 0)
224 while (isspace((unsigned char)*p)) p++;
227 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
230 while (isspace((unsigned char)*p)) p++;
236 fprintf(new, "#define HAVE_IPV6 %s\n",
237 have_ipv6? "TRUE" : "FALSE");
239 fprintf(new, "#define HAVE_ICONV %s\n",
240 have_iconv? "TRUE" : "FALSE");
242 if (errno_quota[0] != 0)
243 fprintf(new, "\n#define ERRNO_QUOTA %s\n", errno_quota);
245 if (strcmp(cc, "gcc") == 0 && strstr(ostype, "IRIX") != NULL)
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");
256 /* Now handle the macros listed in the defaults */
258 base = fopen("../src/config.h.defaults", "rb");
261 printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
266 while (fgets(buffer, sizeof(buffer), base) != NULL)
274 while (*p == ' ' || *p == '\t') p++;
276 if (strncmp(p, "#define ", 8) != 0) continue;
279 while (*p == ' ' || *p == '\t') p++;
281 if (*p < last_initial) fprintf(new, "\n");
284 while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
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
291 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
293 if (strcmp(name, db_opts[i]) == 0)
295 if (use_which_db == i)
296 fprintf(new, "#define %s %.*syes\n", db_opts[i],
297 21 - (int)strlen(db_opts[i]), " ");
299 fprintf(new, "/* %s not set */\n", name);
303 if (i < sizeof(db_opts)/sizeof(char *)) continue;
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. */
310 if (strcmp(name, "EXIM_UID") == 0)
315 char *username = NULL;
316 char *groupname = NULL;
318 char *user = getenv("EXIM_USER");
319 char *group = getenv("EXIM_GROUP");
321 if (user == NULL) user = getenv("EXIM_UID");
322 if (group == NULL) group = getenv("EXIM_GID");
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");
332 while (isspace((unsigned char)(*user))) user++;
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");
341 for (s = user; *s != 0; s++)
343 if (iscntrl((unsigned char)(*s)))
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);
352 /* Numeric uid given */
354 if (user[strspn(user, "0123456789")] == 0)
356 uid = (uid_t)atoi(user);
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:". */
363 else if (strncmp(user, "ref:", 4) == 0)
366 while (isspace(*user)) user++;
373 struct passwd *pw = getpwnam(user);
376 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
377 "exist.\n Please review your build-time configuration.\n\n",
387 /* Use explicit group if set. */
391 while (isspace((unsigned char)(*group))) group++;
394 printf("\n*** EXIM_GROUP is defined as an empty string in one of "
395 "the files in the\n \"Local\" directory. ");
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");
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");
408 for (s = group; *s != 0; s++)
410 if (iscntrl((unsigned char)(*s)))
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);
419 /* Group name given. This may be by reference or to be looked up now,
422 if (strncmp(group, "ref:", 4) == 0)
425 while (isspace(*group)) group++;
429 else if (username != NULL)
434 else if (group[strspn(group, "0123456789")] == 0)
436 gid = (gid_t)atoi(group);
441 struct group *gr = getgrnam(group);
444 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
445 "not exist.\n Please review your build-time configuration.\n\n",
453 /* Else trouble unless found in passwd file with user */
457 printf("\n*** No group set for Exim. Please review your build-time "
458 "configuration.\n\n");
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. */
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);
470 fprintf(new, "#define EXIM_UID %d\n", (int)uid);
471 fprintf(new, "#define EXIM_GID %d\n", (int)gid);
475 /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
476 environment for first. If the value is not numeric, we look up the user or
477 group. A lot of this code is similar to that for EXIM_USER, but it's easier
478 to keep it separate. */
480 if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
481 strcmp(name, "CONFIGURE_GROUP") == 0)
483 int isgroup = name[10] == 'G';
487 char *username = NULL;
488 char *user = getenv(name);
490 if (user == NULL) user = "";
491 while (isspace((unsigned char)(*user))) user++;
494 fprintf(new, "/* %s not set */\n", name);
498 for (s = user; *s != 0; s++)
500 if (iscntrl((unsigned char)(*s)))
502 printf("\n*** %s contains the control character 0x%02X in "
503 "one of the files\n in the \"Local\" directory. Please review "
504 "your build-time\n configuration.\n\n", name, *s);
509 /* Numeric uid given */
511 if (user[strspn(user, "0123456789")] == 0)
514 gid = (gid_t)atoi(user);
516 uid = (uid_t)atoi(user);
519 /* Name given. Normally, we look up the uid or gid right away. However,
520 people building binary distributions sometimes want to retain the name till
521 runtime. This is supported if the name begins "ref:". */
523 else if (strncmp(user, "ref:", 4) == 0)
526 while (isspace(*user)) user++;
532 struct group *gr = getgrnam(user);
535 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
536 "exist.\n Please review your build-time configuration.\n\n",
545 struct passwd *pw = getpwnam(user);
548 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
549 "exist.\n Please review your build-time configuration.\n\n",
556 /* Output user and group names or uid/gid. When names are set, uid/gid
557 are set to zero but will be replaced at runtime. */
559 if (username != NULL)
562 fprintf(new, "#define CONFIGURE_GROUPNAME \"%s\"\n", username);
564 fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username);
568 fprintf(new, "#define CONFIGURE_GROUP %d\n", (int)gid);
570 fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid);
574 /* FIXED_NEVER_USERS is another special case. Look up the uid values and
575 create suitable initialization data for a vector. */
577 if (strcmp(name, "FIXED_NEVER_USERS") == 0)
579 char *list = getenv("FIXED_NEVER_USERS");
582 fprintf(new, "#define FIXED_NEVER_USERS 0\n");
590 while (*p != 0) if (*p++ == ':') count++;
592 vector = malloc((count+1) * sizeof(uid_t));
593 vector[0] = (uid_t)count;
595 for (i = 1, j = 0; i <= count; list++, i++)
600 while (*list != 0 && *list != ':') list++;
601 strncpy(name, p, list-p);
608 else if (name[strspn(name, "0123456789")] == 0)
610 vector[j++] = (uid_t)atoi(name);
614 struct passwd *pw = getpwnam(name);
617 printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
618 "exist.\n Please review your build-time configuration.\n\n",
622 vector[j++] = pw->pw_uid;
625 fprintf(new, "#define FIXED_NEVER_USERS %d", j);
626 for (i = 0; i < j; i++) fprintf(new, ", %d", (unsigned int)vector[i]);
632 /* WITH_CONTENT_SCAN is another special case: it must be set if either it or
633 WITH_OLD_DEMIME is set. */
635 if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
637 char *wcs = getenv("WITH_CONTENT_SCAN");
638 char *wod = getenv("WITH_OLD_DEMIME");
639 if (wcs != NULL || wod != NULL)
640 fprintf(new, "#define WITH_CONTENT_SCAN yes\n");
641 else fprintf(new, "/* WITH_CONTENT_SCAN not set */\n");
645 /* Otherwise, check whether a value exists in the environment. Remember if
646 it is an AUTH setting or SUPPORT_CRYPTEQ. */
648 if ((value = getenv(name)) != NULL)
651 len = 21 - (int)strlen(name);
653 if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
654 if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
656 /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
658 if (strcmp(name, "LDAP_LIB_TYPE") == 0)
660 if (strcmp(value, "NETSCAPE") == 0 ||
661 strcmp(value, "UMICHIGAN") == 0 ||
662 strcmp(value, "OPENLDAP1") == 0 ||
663 strcmp(value, "OPENLDAP2") == 0 ||
664 strcmp(value, "SOLARIS") == 0 ||
665 strcmp(value, "SOLARIS7") == 0) /* Compatibility */
667 fprintf(new, "#define LDAP_LIB_%s\n", value);
671 printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
672 "\n*** Please review your build-time configuration.\n\n", value);
677 else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
679 if (strcmp(value, "RADIUSCLIENT") == 0 ||
680 strcmp(value, "RADLIB") == 0)
682 fprintf(new, "#define RADIUS_LIB_%s\n", value);
686 printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
687 "\n*** Please review your build-time configuration.\n\n", value);
692 /* Other macros get set to the environment value. */
696 fprintf(new, "#define %s ", name);
697 while(len-- > 0) fputc(' ', new);
699 /* LOG_FILE_PATH is now messy because it can be a path containing %s or
700 it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
702 if (strcmp(name, "LOG_FILE_PATH") == 0)
708 char *sss = strchr(ss, ':');
711 strncpy(buffer, ss, sss-ss);
712 buffer[sss-ss] = 0; /* For empty case */
714 else strcpy(buffer, ss);
715 pp = buffer + (int)strlen(buffer);
716 while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
718 if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
719 check_percent_ess(buffer, name);
720 if (sss == NULL) break;
722 while (isspace((unsigned char)*ss)) ss++;
724 fprintf(new, "\"%s\"\n", value);
727 /* Timezone values and HEADERS_CHARSET get quoted */
729 else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
730 strcmp(name, "HEADERS_CHARSET") == 0)
731 fprintf(new, "\"%s\"\n", value);
733 /* For others, quote any paths and don't quote anything else */
737 if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
738 else fprintf(new, "%s\n", value);
743 /* Value not defined in the environment; use the default */
748 while (*p == ' ' || *p == '\t') p++;
749 if (*p != '\n') fputs(buffer, new); else
752 if (strcmp(name, "BIN_DIRECTORY") == 0 ||
753 strcmp(name, "CONFIGURE_FILE") == 0)
755 printf("\n*** %s has not been defined in any of the Makefiles in the\n"
756 " \"Local\" directory. "
757 "Please review your build-time configuration.\n\n", name);
761 if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
763 char *tz = getenv("TZ");
764 fprintf(new, "#define TIMEZONE_DEFAULT ");
765 if (tz == NULL) fprintf(new, "NULL\n"); else
766 fprintf(new, "\"%s\"\n", tz);
769 else fprintf(new, "/* %s not set */\n", name);
776 /* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
781 if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
782 "#define SUPPORT_CRYPTEQ\n");
787 fprintf(new, "\n/* End of config.h */\n");
792 /* End of buildconfig.c */