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