init.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. #include "portable.h"
  16. #include <stdio.h>
  17. #include <ac/stdlib.h>
  18. #ifdef HAVE_GETEUID
  19. #include <ac/unistd.h>
  20. #endif
  21. #include <ac/socket.h>
  22. #include <ac/string.h>
  23. #include <ac/ctype.h>
  24. #include <ac/time.h>
  25. #ifdef HAVE_LIMITS_H
  26. #include <limits.h>
  27. #endif
  28. #include "ldap-int.h"
  29. #include "ldap_defaults.h"
  30. #include "lutil.h"
  31. struct ldapoptions ldap_int_global_options =
  32. { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE
  33. LDAP_LDO_NULLARG
  34. LDAP_LDO_SOURCEIP_NULLARG
  35. LDAP_LDO_CONNECTIONLESS_NULLARG
  36. LDAP_LDO_TLS_NULLARG
  37. LDAP_LDO_SASL_NULLARG
  38. LDAP_LDO_MUTEX_NULLARG };
  39. #define ATTR_NONE 0
  40. #define ATTR_BOOL 1
  41. #define ATTR_INT 2
  42. #define ATTR_KV 3
  43. #define ATTR_STRING 4
  44. #define ATTR_OPTION 5
  45. #define ATTR_SASL 6
  46. #define ATTR_TLS 7
  47. #define ATTR_OPT_TV 8
  48. #define ATTR_OPT_INT 9
  49. struct ol_keyvalue {
  50. const char * key;
  51. int value;
  52. };
  53. static const struct ol_keyvalue deref_kv[] = {
  54. {"never", LDAP_DEREF_NEVER},
  55. {"searching", LDAP_DEREF_SEARCHING},
  56. {"finding", LDAP_DEREF_FINDING},
  57. {"always", LDAP_DEREF_ALWAYS},
  58. {NULL, 0}
  59. };
  60. static const struct ol_attribute {
  61. int useronly;
  62. int type;
  63. const char * name;
  64. const void * data;
  65. size_t offset;
  66. } attrs[] = {
  67. {0, ATTR_OPT_TV, "TIMEOUT", NULL, LDAP_OPT_TIMEOUT},
  68. {0, ATTR_OPT_TV, "NETWORK_TIMEOUT", NULL, LDAP_OPT_NETWORK_TIMEOUT},
  69. {0, ATTR_OPT_INT, "VERSION", NULL, LDAP_OPT_PROTOCOL_VERSION},
  70. {0, ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
  71. offsetof(struct ldapoptions, ldo_deref)},
  72. {0, ATTR_INT, "SIZELIMIT", NULL,
  73. offsetof(struct ldapoptions, ldo_sizelimit)},
  74. {0, ATTR_INT, "TIMELIMIT", NULL,
  75. offsetof(struct ldapoptions, ldo_timelimit)},
  76. {1, ATTR_STRING, "BINDDN", NULL,
  77. offsetof(struct ldapoptions, ldo_defbinddn)},
  78. {0, ATTR_STRING, "BASE", NULL,
  79. offsetof(struct ldapoptions, ldo_defbase)},
  80. {0, ATTR_INT, "PORT", NULL, /* deprecated */
  81. offsetof(struct ldapoptions, ldo_defport)},
  82. {0, ATTR_OPTION, "HOST", NULL, LDAP_OPT_HOST_NAME}, /* deprecated */
  83. {0, ATTR_OPTION, "URI", NULL, LDAP_OPT_URI}, /* replaces HOST/PORT */
  84. {0, ATTR_OPTION, "SOCKET_BIND_ADDRESSES", NULL, LDAP_OPT_SOCKET_BIND_ADDRESSES},
  85. {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
  86. {0, ATTR_OPT_INT, "KEEPALIVE_IDLE", NULL, LDAP_OPT_X_KEEPALIVE_IDLE},
  87. {0, ATTR_OPT_INT, "KEEPALIVE_PROBES", NULL, LDAP_OPT_X_KEEPALIVE_PROBES},
  88. {0, ATTR_OPT_INT, "KEEPALIVE_INTERVAL", NULL, LDAP_OPT_X_KEEPALIVE_INTERVAL},
  89. #if 0
  90. /* This should only be allowed via ldap_set_option(3) */
  91. {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
  92. #endif
  93. #ifdef HAVE_CYRUS_SASL
  94. {0, ATTR_STRING, "SASL_MECH", NULL,
  95. offsetof(struct ldapoptions, ldo_def_sasl_mech)},
  96. {0, ATTR_STRING, "SASL_REALM", NULL,
  97. offsetof(struct ldapoptions, ldo_def_sasl_realm)},
  98. {1, ATTR_STRING, "SASL_AUTHCID", NULL,
  99. offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
  100. {1, ATTR_STRING, "SASL_AUTHZID", NULL,
  101. offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
  102. {0, ATTR_SASL, "SASL_SECPROPS", NULL, LDAP_OPT_X_SASL_SECPROPS},
  103. {0, ATTR_BOOL, "SASL_NOCANON", NULL, LDAP_BOOL_SASL_NOCANON},
  104. {0, ATTR_SASL, "SASL_CBINDING", NULL, LDAP_OPT_X_SASL_CBINDING},
  105. #endif
  106. #ifdef HAVE_TLS
  107. {1, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
  108. {1, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
  109. {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
  110. {0, ATTR_TLS, "TLS_CACERTDIR", NULL, LDAP_OPT_X_TLS_CACERTDIR},
  111. {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
  112. {0, ATTR_TLS, "TLS_REQSAN", NULL, LDAP_OPT_X_TLS_REQUIRE_SAN},
  113. {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
  114. {0, ATTR_TLS, "TLS_CIPHER_SUITE", NULL, LDAP_OPT_X_TLS_CIPHER_SUITE},
  115. {0, ATTR_TLS, "TLS_PROTOCOL_MIN", NULL, LDAP_OPT_X_TLS_PROTOCOL_MIN},
  116. {0, ATTR_TLS, "TLS_PROTOCOL_MAX", NULL, LDAP_OPT_X_TLS_PROTOCOL_MAX},
  117. {0, ATTR_TLS, "TLS_PEERKEY_HASH", NULL, LDAP_OPT_X_TLS_PEERKEY_HASH},
  118. {0, ATTR_TLS, "TLS_ECNAME", NULL, LDAP_OPT_X_TLS_ECNAME},
  119. #ifdef HAVE_OPENSSL
  120. {0, ATTR_TLS, "TLS_CRLCHECK", NULL, LDAP_OPT_X_TLS_CRLCHECK},
  121. #endif
  122. #ifdef HAVE_GNUTLS
  123. {0, ATTR_TLS, "TLS_CRLFILE", NULL, LDAP_OPT_X_TLS_CRLFILE},
  124. #endif
  125. #endif
  126. {0, ATTR_NONE, NULL, NULL, 0}
  127. };
  128. #define MAX_LDAP_ATTR_LEN sizeof("SOCKET_BIND_ADDRESSES")
  129. #define MAX_LDAP_ENV_PREFIX_LEN 8
  130. static int
  131. ldap_int_conf_option(
  132. struct ldapoptions *gopts,
  133. char *cmd, char *opt, int userconf )
  134. {
  135. int i;
  136. for(i=0; attrs[i].type != ATTR_NONE; i++) {
  137. void *p;
  138. if( !userconf && attrs[i].useronly ) {
  139. continue;
  140. }
  141. if(strcasecmp(cmd, attrs[i].name) != 0) {
  142. continue;
  143. }
  144. switch(attrs[i].type) {
  145. case ATTR_BOOL:
  146. if((strcasecmp(opt, "on") == 0)
  147. || (strcasecmp(opt, "yes") == 0)
  148. || (strcasecmp(opt, "true") == 0))
  149. {
  150. LDAP_BOOL_SET(gopts, attrs[i].offset);
  151. } else {
  152. LDAP_BOOL_CLR(gopts, attrs[i].offset);
  153. }
  154. break;
  155. case ATTR_INT: {
  156. char *next;
  157. long l;
  158. p = &((char *) gopts)[attrs[i].offset];
  159. l = strtol( opt, &next, 10 );
  160. if ( next != opt && next[ 0 ] == '\0' ) {
  161. * (int*) p = l;
  162. }
  163. } break;
  164. case ATTR_KV: {
  165. const struct ol_keyvalue *kv;
  166. for(kv = attrs[i].data;
  167. kv->key != NULL;
  168. kv++) {
  169. if(strcasecmp(opt, kv->key) == 0) {
  170. p = &((char *) gopts)[attrs[i].offset];
  171. * (int*) p = kv->value;
  172. break;
  173. }
  174. }
  175. } break;
  176. case ATTR_STRING:
  177. p = &((char *) gopts)[attrs[i].offset];
  178. if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
  179. * (char**) p = LDAP_STRDUP(opt);
  180. break;
  181. case ATTR_OPTION:
  182. ldap_set_option( NULL, attrs[i].offset, opt );
  183. break;
  184. case ATTR_SASL:
  185. #ifdef HAVE_CYRUS_SASL
  186. ldap_int_sasl_config( gopts, attrs[i].offset, opt );
  187. #endif
  188. break;
  189. case ATTR_TLS:
  190. #ifdef HAVE_TLS
  191. ldap_pvt_tls_config( NULL, attrs[i].offset, opt );
  192. #endif
  193. break;
  194. case ATTR_OPT_TV: {
  195. struct timeval tv;
  196. char *next;
  197. tv.tv_usec = 0;
  198. tv.tv_sec = strtol( opt, &next, 10 );
  199. if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
  200. (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
  201. }
  202. } break;
  203. case ATTR_OPT_INT: {
  204. long l;
  205. char *next;
  206. l = strtol( opt, &next, 10 );
  207. if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
  208. int v = (int)l;
  209. (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
  210. }
  211. } break;
  212. }
  213. break;
  214. }
  215. if ( attrs[i].type == ATTR_NONE ) {
  216. Debug1( LDAP_DEBUG_TRACE, "ldap_pvt_tls_config: "
  217. "unknown option '%s'",
  218. cmd );
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. int
  224. ldap_pvt_conf_option(
  225. char *cmd, char *opt, int userconf )
  226. {
  227. struct ldapoptions *gopts;
  228. int rc = LDAP_OPT_ERROR;
  229. /* Get pointer to global option structure */
  230. gopts = LDAP_INT_GLOBAL_OPT();
  231. if (NULL == gopts) {
  232. return LDAP_NO_MEMORY;
  233. }
  234. if ( gopts->ldo_valid != LDAP_INITIALIZED ) {
  235. ldap_int_initialize(gopts, NULL);
  236. if ( gopts->ldo_valid != LDAP_INITIALIZED )
  237. return LDAP_LOCAL_ERROR;
  238. }
  239. return ldap_int_conf_option( gopts, cmd, opt, userconf );
  240. }
  241. static void openldap_ldap_init_w_conf(
  242. const char *file, int userconf )
  243. {
  244. char linebuf[ AC_LINE_MAX ];
  245. FILE *fp;
  246. int i;
  247. char *cmd, *opt;
  248. char *start, *end;
  249. struct ldapoptions *gopts;
  250. if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
  251. return; /* Could not allocate mem for global options */
  252. }
  253. if (file == NULL) {
  254. /* no file name */
  255. return;
  256. }
  257. Debug1(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file );
  258. fp = fopen(file, "r");
  259. if(fp == NULL) {
  260. /* could not open file */
  261. return;
  262. }
  263. Debug1(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file );
  264. while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
  265. /* skip lines starting with '#' */
  266. if(*start == '#') continue;
  267. /* trim leading white space */
  268. while((*start != '\0') && isspace((unsigned char) *start))
  269. start++;
  270. /* anything left? */
  271. if(*start == '\0') continue;
  272. /* trim trailing white space */
  273. end = &start[strlen(start)-1];
  274. while(isspace((unsigned char)*end)) end--;
  275. end[1] = '\0';
  276. /* anything left? */
  277. if(*start == '\0') continue;
  278. /* parse the command */
  279. cmd=start;
  280. while((*start != '\0') && !isspace((unsigned char)*start)) {
  281. start++;
  282. }
  283. if(*start == '\0') {
  284. /* command has no argument */
  285. continue;
  286. }
  287. *start++ = '\0';
  288. /* we must have some whitespace to skip */
  289. while(isspace((unsigned char)*start)) start++;
  290. opt = start;
  291. ldap_int_conf_option( gopts, cmd, opt, userconf );
  292. }
  293. fclose(fp);
  294. }
  295. static void openldap_ldap_init_w_sysconf(const char *file)
  296. {
  297. openldap_ldap_init_w_conf( file, 0 );
  298. }
  299. static void openldap_ldap_init_w_userconf(const char *file)
  300. {
  301. char *home;
  302. char *path = NULL;
  303. if (file == NULL) {
  304. /* no file name */
  305. return;
  306. }
  307. home = getenv("HOME");
  308. if (home != NULL) {
  309. Debug1(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
  310. home );
  311. path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
  312. } else {
  313. Debug0(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n" );
  314. }
  315. if(home != NULL && path != NULL) {
  316. /* we assume UNIX path syntax is used... */
  317. /* try ~/file */
  318. sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
  319. openldap_ldap_init_w_conf(path, 1);
  320. /* try ~/.file */
  321. sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
  322. openldap_ldap_init_w_conf(path, 1);
  323. }
  324. if(path != NULL) {
  325. LDAP_FREE(path);
  326. }
  327. /* try file */
  328. openldap_ldap_init_w_conf(file, 1);
  329. }
  330. static void openldap_ldap_init_w_env(
  331. struct ldapoptions *gopts,
  332. const char *prefix)
  333. {
  334. char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
  335. int len;
  336. int i;
  337. void *p;
  338. char *value;
  339. if (prefix == NULL) {
  340. prefix = LDAP_ENV_PREFIX;
  341. }
  342. strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
  343. buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
  344. len = strlen(buf);
  345. for(i=0; attrs[i].type != ATTR_NONE; i++) {
  346. strcpy(&buf[len], attrs[i].name);
  347. value = getenv(buf);
  348. if(value == NULL) {
  349. continue;
  350. }
  351. switch(attrs[i].type) {
  352. case ATTR_BOOL:
  353. if((strcasecmp(value, "on") == 0)
  354. || (strcasecmp(value, "yes") == 0)
  355. || (strcasecmp(value, "true") == 0))
  356. {
  357. LDAP_BOOL_SET(gopts, attrs[i].offset);
  358. } else {
  359. LDAP_BOOL_CLR(gopts, attrs[i].offset);
  360. }
  361. break;
  362. case ATTR_INT:
  363. p = &((char *) gopts)[attrs[i].offset];
  364. * (int*) p = atoi(value);
  365. break;
  366. case ATTR_KV: {
  367. const struct ol_keyvalue *kv;
  368. for(kv = attrs[i].data;
  369. kv->key != NULL;
  370. kv++) {
  371. if(strcasecmp(value, kv->key) == 0) {
  372. p = &((char *) gopts)[attrs[i].offset];
  373. * (int*) p = kv->value;
  374. break;
  375. }
  376. }
  377. } break;
  378. case ATTR_STRING:
  379. p = &((char *) gopts)[attrs[i].offset];
  380. if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
  381. if (*value == '\0') {
  382. * (char**) p = NULL;
  383. } else {
  384. * (char**) p = LDAP_STRDUP(value);
  385. }
  386. break;
  387. case ATTR_OPTION:
  388. ldap_set_option( NULL, attrs[i].offset, value );
  389. break;
  390. case ATTR_SASL:
  391. #ifdef HAVE_CYRUS_SASL
  392. ldap_int_sasl_config( gopts, attrs[i].offset, value );
  393. #endif
  394. break;
  395. case ATTR_TLS:
  396. #ifdef HAVE_TLS
  397. ldap_pvt_tls_config( NULL, attrs[i].offset, value );
  398. #endif
  399. break;
  400. case ATTR_OPT_TV: {
  401. struct timeval tv;
  402. char *next;
  403. tv.tv_usec = 0;
  404. tv.tv_sec = strtol( value, &next, 10 );
  405. if ( next != value && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
  406. (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
  407. }
  408. } break;
  409. case ATTR_OPT_INT: {
  410. long l;
  411. char *next;
  412. l = strtol( value, &next, 10 );
  413. if ( next != value && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
  414. int v = (int)l;
  415. (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
  416. }
  417. } break;
  418. }
  419. }
  420. }
  421. #if defined(__GNUC__)
  422. /* Declare this function as a destructor so that it will automatically be
  423. * invoked either at program exit (if libldap is a static library) or
  424. * at unload time (if libldap is a dynamic library).
  425. *
  426. * Sorry, don't know how to handle this for non-GCC environments.
  427. */
  428. static void ldap_int_destroy_global_options(void)
  429. __attribute__ ((destructor));
  430. #endif
  431. static void
  432. ldap_int_destroy_global_options(void)
  433. {
  434. struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
  435. if ( gopts == NULL )
  436. return;
  437. gopts->ldo_valid = LDAP_UNINITIALIZED;
  438. if ( gopts->ldo_defludp ) {
  439. ldap_free_urllist( gopts->ldo_defludp );
  440. gopts->ldo_defludp = NULL;
  441. }
  442. if ( gopts->ldo_local_ip_addrs.local_ip_addrs ) {
  443. LDAP_FREE( gopts->ldo_local_ip_addrs.local_ip_addrs );
  444. gopts->ldo_local_ip_addrs.local_ip_addrs = NULL;
  445. }
  446. #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
  447. WSACleanup( );
  448. #endif
  449. #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
  450. if ( ldap_int_hostname ) {
  451. LDAP_FREE( ldap_int_hostname );
  452. ldap_int_hostname = NULL;
  453. }
  454. #endif
  455. #ifdef HAVE_CYRUS_SASL
  456. if ( gopts->ldo_def_sasl_authcid ) {
  457. LDAP_FREE( gopts->ldo_def_sasl_authcid );
  458. gopts->ldo_def_sasl_authcid = NULL;
  459. }
  460. #endif
  461. }
  462. /*
  463. * Initialize the global options structure with default values.
  464. */
  465. void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
  466. {
  467. if (dbglvl)
  468. gopts->ldo_debug = *dbglvl;
  469. else
  470. gopts->ldo_debug = 0;
  471. gopts->ldo_version = LDAP_VERSION2;
  472. gopts->ldo_deref = LDAP_DEREF_NEVER;
  473. gopts->ldo_timelimit = LDAP_NO_LIMIT;
  474. gopts->ldo_sizelimit = LDAP_NO_LIMIT;
  475. gopts->ldo_tm_api.tv_sec = -1;
  476. gopts->ldo_tm_net.tv_sec = -1;
  477. memset( &gopts->ldo_local_ip_addrs, 0,
  478. sizeof( gopts->ldo_local_ip_addrs ) );
  479. /* ldo_defludp will be freed by the termination handler
  480. */
  481. ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
  482. gopts->ldo_defport = LDAP_PORT;
  483. #if !defined(__GNUC__) && !defined(PIC)
  484. /* Do this only for a static library, and only if we can't
  485. * arrange for it to be executed as a library destructor
  486. */
  487. atexit(ldap_int_destroy_global_options);
  488. #endif
  489. gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
  490. gopts->ldo_rebind_proc = NULL;
  491. gopts->ldo_rebind_params = NULL;
  492. LDAP_BOOL_ZERO(gopts);
  493. LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
  494. #ifdef LDAP_CONNECTIONLESS
  495. gopts->ldo_peer = NULL;
  496. gopts->ldo_cldapdn = NULL;
  497. gopts->ldo_is_udp = 0;
  498. #endif
  499. #ifdef HAVE_CYRUS_SASL
  500. gopts->ldo_def_sasl_mech = NULL;
  501. gopts->ldo_def_sasl_realm = NULL;
  502. gopts->ldo_def_sasl_authcid = NULL;
  503. gopts->ldo_def_sasl_authzid = NULL;
  504. memset( &gopts->ldo_sasl_secprops,
  505. '\0', sizeof(gopts->ldo_sasl_secprops) );
  506. gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
  507. gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
  508. gopts->ldo_sasl_secprops.security_flags =
  509. SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
  510. #endif
  511. #ifdef HAVE_TLS
  512. gopts->ldo_tls_connect_cb = NULL;
  513. gopts->ldo_tls_connect_arg = NULL;
  514. gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
  515. gopts->ldo_tls_require_san = LDAP_OPT_X_TLS_ALLOW;
  516. #endif
  517. gopts->ldo_keepalive_probes = 0;
  518. gopts->ldo_keepalive_interval = 0;
  519. gopts->ldo_keepalive_idle = 0;
  520. gopts->ldo_tcp_user_timeout = 0;
  521. #ifdef LDAP_R_COMPILE
  522. ldap_pvt_thread_mutex_init( &gopts->ldo_mutex );
  523. #endif
  524. gopts->ldo_valid = LDAP_INITIALIZED;
  525. return;
  526. }
  527. #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
  528. char * ldap_int_hostname = NULL;
  529. #endif
  530. #ifdef LDAP_R_COMPILE
  531. int ldap_int_stackguard;
  532. #endif
  533. void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
  534. {
  535. #ifdef LDAP_R_COMPILE
  536. static ldap_pvt_thread_mutex_t init_mutex;
  537. LDAP_PVT_MUTEX_FIRSTCREATE( init_mutex );
  538. LDAP_MUTEX_LOCK( &init_mutex );
  539. #endif
  540. if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
  541. /* someone else got here first */
  542. goto done;
  543. }
  544. ldap_int_error_init();
  545. ldap_int_utils_init();
  546. #ifdef HAVE_WINSOCK2
  547. { WORD wVersionRequested;
  548. WSADATA wsaData;
  549. wVersionRequested = MAKEWORD( 2, 0 );
  550. if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
  551. /* Tell the user that we couldn't find a usable */
  552. /* WinSock DLL. */
  553. goto done;
  554. }
  555. /* Confirm that the WinSock DLL supports 2.0.*/
  556. /* Note that if the DLL supports versions greater */
  557. /* than 2.0 in addition to 2.0, it will still return */
  558. /* 2.0 in wVersion since that is the version we */
  559. /* requested. */
  560. if ( LOBYTE( wsaData.wVersion ) != 2 ||
  561. HIBYTE( wsaData.wVersion ) != 0 )
  562. {
  563. /* Tell the user that we couldn't find a usable */
  564. /* WinSock DLL. */
  565. WSACleanup( );
  566. goto done;
  567. }
  568. } /* The WinSock DLL is acceptable. Proceed. */
  569. #elif defined(HAVE_WINSOCK)
  570. { WSADATA wsaData;
  571. if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
  572. goto done;
  573. }
  574. }
  575. #endif
  576. #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
  577. LDAP_MUTEX_LOCK( &ldap_int_hostname_mutex );
  578. {
  579. char *name = ldap_int_hostname;
  580. ldap_int_hostname = ldap_pvt_get_fqdn( name );
  581. if ( name != NULL && name != ldap_int_hostname ) {
  582. LDAP_FREE( name );
  583. }
  584. }
  585. LDAP_MUTEX_UNLOCK( &ldap_int_hostname_mutex );
  586. #endif
  587. #ifndef HAVE_POLL
  588. if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
  589. #endif
  590. #ifdef HAVE_CYRUS_SASL
  591. if ( ldap_int_sasl_init() != 0 ) {
  592. goto done;
  593. }
  594. #endif
  595. ldap_int_initialize_global_options(gopts, dbglvl);
  596. if( getenv("LDAPNOINIT") != NULL ) {
  597. goto done;
  598. }
  599. #ifdef LDAP_R_COMPILE
  600. if( getenv("LDAPSTACKGUARD") != NULL ) {
  601. ldap_int_stackguard = 1;
  602. }
  603. #endif
  604. #ifdef HAVE_CYRUS_SASL
  605. {
  606. /* set authentication identity to current user name */
  607. char *user = getenv("USER");
  608. if( user == NULL ) user = getenv("USERNAME");
  609. if( user == NULL ) user = getenv("LOGNAME");
  610. if( user != NULL ) {
  611. gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
  612. }
  613. }
  614. #endif
  615. openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
  616. #ifdef HAVE_GETEUID
  617. if ( geteuid() != getuid() )
  618. goto done;
  619. #endif
  620. openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
  621. {
  622. char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
  623. if( altfile != NULL ) {
  624. Debug2(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
  625. LDAP_ENV_PREFIX "CONF", altfile );
  626. openldap_ldap_init_w_sysconf( altfile );
  627. }
  628. else
  629. Debug1(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
  630. LDAP_ENV_PREFIX "CONF" );
  631. }
  632. {
  633. char *altfile = getenv(LDAP_ENV_PREFIX "RC");
  634. if( altfile != NULL ) {
  635. Debug2(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
  636. LDAP_ENV_PREFIX "RC", altfile );
  637. openldap_ldap_init_w_userconf( altfile );
  638. }
  639. else
  640. Debug1(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
  641. LDAP_ENV_PREFIX "RC" );
  642. }
  643. openldap_ldap_init_w_env(gopts, NULL);
  644. done:;
  645. #ifdef LDAP_R_COMPILE
  646. LDAP_MUTEX_UNLOCK( &init_mutex );
  647. #endif
  648. }