ptconfig.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * ptconfig.c - configuration-related code for Pass Through Authentication
  43. *
  44. */
  45. #include "passthru.h"
  46. /*
  47. * Configuration is a bit complicated to fit into a single slapd config file
  48. * line, but for now that's how it works. The format is:
  49. *
  50. * plugin preoperation on PTA NSHOME/passthru-plugin.so passthruauth_init ARGS
  51. *
  52. * where each ARGS provides configuration for one host. Each ARG should
  53. * be of the form:
  54. *
  55. * "ldap://hosts/suffixes maxconns,maxconcurrency,timeout,ldver,connlifetime"
  56. * OR
  57. * "ldaps://hosts/suffixes maxconns,maxconcurrency,timeout,ldver,connlifetime"
  58. *
  59. * where:
  60. * hosts is a space-separated list of remote servers (with optional port
  61. * numbers) to be used. Each one is tried in order when opening an
  62. * LDAP connection.
  63. * suffixes is a semicolon separated list of DNs (if a DN contains a
  64. * semicolon it must be represented \3B),
  65. * maxconns is a limit on how many connections will be made,
  66. * maxconcurrency is a limit on how many operations can share a connection,
  67. * timeout is a time limit in seconds for bind operations to complete (use
  68. * 0 to specify an infinite limit).
  69. * ldver is the LDAP protocol version to use to talk to the server (2 or 3)
  70. * connlifetime is a time limit time in seconds for a connection to be
  71. * used before it is closed and reopened (use 0 to specify an infinite
  72. * limit). connlifetime can be omitted in which case a default value
  73. * is used; this is for compatibility with DS 4.0 which did not support
  74. * connlifetime.
  75. */
  76. /*
  77. * function prototypes
  78. */
  79. /*
  80. * static variables
  81. */
  82. /* for now, there is only one configuration and it is global to the plugin */
  83. static PassThruConfig theConfig;
  84. static int inited = 0;
  85. /*
  86. * Read configuration and create a configuration data structure.
  87. * This is called after the server has configured itself so we can check
  88. * for things like collisions between our suffixes and backend's suffixes.
  89. * Returns an LDAP error code (LDAP_SUCCESS if all goes well).
  90. * XXXmcs: this function leaks memory if any errors occur.
  91. */
  92. int
  93. passthru_config( int argc, char **argv )
  94. {
  95. int i, j, rc, tosecs, using_def_connlifetime, starttls = 0;
  96. char **suffixarray;
  97. PassThruServer *prevsrvr, *srvr = NULL;
  98. PassThruSuffix *suffix, *prevsuffix;
  99. LDAPURLDesc *ludp;
  100. int ret = LDAP_SUCCESS;
  101. if ( inited ) {
  102. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  103. "only one pass through plugin instance can be used\n" );
  104. ret = LDAP_PARAM_ERROR;
  105. goto error;
  106. }
  107. inited = 1;
  108. /*
  109. * It doesn't make sense to configure a pass through plugin without
  110. * providing at least one remote server. Return an error if attempted.
  111. */
  112. if ( argc < 1 ) {
  113. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  114. "no pass through servers found in configuration"
  115. " (at least one must be listed)\n" );
  116. ret = LDAP_PARAM_ERROR;
  117. goto error;
  118. }
  119. /*
  120. * Parse argv[] values.
  121. */
  122. prevsrvr = NULL;
  123. for ( i = 0; i < argc; ++i ) {
  124. int secure = 0;
  125. char *p = NULL;
  126. srvr = (PassThruServer *)slapi_ch_calloc( 1, sizeof( PassThruServer ));
  127. srvr->ptsrvr_url = slapi_ch_strdup( argv[i] );
  128. /* since the ldap url may contain both spaces (to delimit multiple hosts)
  129. and commas (in suffixes), we have to search for the first space
  130. after the last /, then look for any commas after that
  131. This assumes the ldap url looks like this:
  132. ldap(s)://host:port host:port .... host:port/suffixes
  133. That is, it assumes there is always a trailing slash on the ldapurl
  134. and that the url does not look like this: ldap://host
  135. also assumes suffixes do not have any / in them
  136. */
  137. if ((p = strrchr(srvr->ptsrvr_url, '/'))) { /* look for last / */
  138. p = strchr(p, ' '); /* look for first space after last / */
  139. if (p) {
  140. if (!strchr(p, ',')) { /* no comma */
  141. p = NULL; /* just use defaults */
  142. }
  143. }
  144. }
  145. if (!p) {
  146. /*
  147. * use defaults for maxconnections, maxconcurrency, timeout,
  148. * LDAP version, and connlifetime.
  149. */
  150. srvr->ptsrvr_maxconnections = PASSTHRU_DEF_SRVR_MAXCONNECTIONS;
  151. srvr->ptsrvr_maxconcurrency = PASSTHRU_DEF_SRVR_MAXCONCURRENCY;
  152. srvr->ptsrvr_timeout = (struct timeval *)slapi_ch_calloc( 1,
  153. sizeof( struct timeval ));
  154. srvr->ptsrvr_timeout->tv_sec = PASSTHRU_DEF_SRVR_TIMEOUT;
  155. srvr->ptsrvr_ldapversion = PASSTHRU_DEF_SRVR_PROTOCOL_VERSION;
  156. using_def_connlifetime = 1;
  157. } else {
  158. /*
  159. * parse parameters. format is:
  160. * maxconnections,maxconcurrency,timeout,ldapversion
  161. * OR maxconnections,maxconcurrency,timeout,ldapversion,lifetime
  162. * OR maxconnections,maxconcurrency,timeout,ldapversion,lifetime,starttls
  163. */
  164. *p++ = '\0'; /* p points at space preceding optional arguments */
  165. rc = sscanf( p, "%d,%d,%d,%d,%d,%d", &srvr->ptsrvr_maxconnections,
  166. &srvr->ptsrvr_maxconcurrency, &tosecs,
  167. &srvr->ptsrvr_ldapversion, &srvr->ptsrvr_connlifetime,
  168. &starttls);
  169. if ( rc < 4 ) {
  170. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  171. "server parameters should be in the form "
  172. "\"maxconnections,maxconcurrency,timeout,ldapversion,"
  173. "connlifetime\" (got \"%s\")\n", p );
  174. ret = LDAP_PARAM_ERROR;
  175. goto error;
  176. } else if ( rc < 5 ) {
  177. using_def_connlifetime = 1;
  178. srvr->ptsrvr_connlifetime = PASSTHRU_DEF_SRVR_CONNLIFETIME;
  179. starttls = 0;
  180. } else if ( rc < 6 ) {
  181. using_def_connlifetime = 0; /* lifetime specified */
  182. starttls = 0; /* but not starttls */
  183. } else { /* all 6 args supplied */
  184. using_def_connlifetime = 0; /* lifetime specified */
  185. /* and starttls */
  186. }
  187. if ( srvr->ptsrvr_ldapversion != LDAP_VERSION2
  188. && srvr->ptsrvr_ldapversion != LDAP_VERSION3 ) {
  189. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  190. "LDAP protocol version should be %d or %d (got %d)\n",
  191. LDAP_VERSION2, LDAP_VERSION3,
  192. srvr->ptsrvr_ldapversion );
  193. ret = LDAP_PARAM_ERROR;
  194. goto error;
  195. }
  196. if ( srvr->ptsrvr_maxconnections <= 0 ) {
  197. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  198. "maximum connections must be greater than "
  199. "zero (got %d)\n", srvr->ptsrvr_maxconnections );
  200. ret = LDAP_PARAM_ERROR;
  201. goto error;
  202. }
  203. if ( srvr->ptsrvr_maxconcurrency <= 0 ) {
  204. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  205. "maximum concurrency must be greater than "
  206. "zero (got %d)\n", srvr->ptsrvr_maxconcurrency );
  207. ret = LDAP_PARAM_ERROR;
  208. goto error;
  209. }
  210. if ( tosecs <= 0 ) {
  211. srvr->ptsrvr_timeout = NULL;
  212. } else {
  213. srvr->ptsrvr_timeout = (struct timeval *)slapi_ch_calloc( 1,
  214. sizeof( struct timeval ));
  215. srvr->ptsrvr_timeout->tv_sec = tosecs;
  216. }
  217. }
  218. /*
  219. * parse the LDAP URL
  220. */
  221. if (( rc = slapi_ldap_url_parse( srvr->ptsrvr_url, &ludp, 1, &secure )) != 0 ) {
  222. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  223. "unable to parse LDAP URL \"%s\" (%s)\n",
  224. srvr->ptsrvr_url, slapi_urlparse_err2string( rc ));
  225. ret = LDAP_PARAM_ERROR;
  226. goto error;
  227. }
  228. if ( ludp->lud_dn == NULL || *ludp->lud_dn == '\0' ) {
  229. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  230. "missing suffix in LDAP URL \"%s\"\n",
  231. srvr->ptsrvr_url );
  232. ret = LDAP_PARAM_ERROR;
  233. goto error;
  234. }
  235. srvr->ptsrvr_hostname = slapi_ch_strdup( ludp->lud_host );
  236. srvr->ptsrvr_port = ludp->lud_port;
  237. srvr->ptsrvr_secure = secure;
  238. if (starttls) {
  239. srvr->ptsrvr_secure = 2;
  240. }
  241. /*
  242. * If a space-separated list of hosts is configured for failover,
  243. * use a different (non infinite) default for connection lifetime.
  244. */
  245. if ( using_def_connlifetime &&
  246. strchr( srvr->ptsrvr_hostname, ' ' ) != NULL ) {
  247. srvr->ptsrvr_connlifetime =
  248. PASSTHRU_DEF_SRVR_FAILOVERCONNLIFETIME;
  249. }
  250. /*
  251. * split the DN into multiple suffixes (separated by ';')
  252. */
  253. if (( suffixarray = slapi_str2charray( ludp->lud_dn, ";" )) == NULL ) {
  254. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  255. "unable to parse suffix string \"%s\" within \"%s\"\n",
  256. ludp->lud_dn, srvr->ptsrvr_url );
  257. ret = LDAP_PARAM_ERROR;
  258. goto error;
  259. }
  260. /*
  261. * free our LDAP URL descriptor
  262. */
  263. ldap_free_urldesc( ludp );
  264. ludp = NULL;
  265. /*
  266. * reorganize the suffixes into a linked list and normalize them
  267. */
  268. prevsuffix = NULL;
  269. for ( j = 0; suffixarray[ j ] != NULL; ++j ) {
  270. /*
  271. * allocate a new PassThruSuffix structure and fill it in.
  272. */
  273. suffix = (PassThruSuffix *)slapi_ch_malloc(
  274. sizeof( PassThruSuffix ));
  275. suffix->ptsuffix_normsuffix =
  276. slapi_dn_normalize( suffixarray[ j ] );
  277. suffixarray[ j ] = NULL;
  278. suffix->ptsuffix_len = strlen( suffix->ptsuffix_normsuffix );
  279. suffix->ptsuffix_next = NULL;
  280. /*
  281. * add to end of list
  282. */
  283. if ( prevsuffix == NULL ) {
  284. srvr->ptsrvr_suffixes = suffix;
  285. } else {
  286. prevsuffix->ptsuffix_next = suffix;
  287. }
  288. prevsuffix = suffix;
  289. }
  290. ldap_memfree( suffixarray );
  291. /*
  292. * create mutexes and condition variables for this server
  293. */
  294. if (( srvr->ptsrvr_connlist_mutex = slapi_new_mutex()) == NULL ||
  295. ( srvr->ptsrvr_connlist_cv = slapi_new_condvar(
  296. srvr->ptsrvr_connlist_mutex )) == NULL ) {
  297. ret = LDAP_LOCAL_ERROR;
  298. goto error;
  299. }
  300. /*
  301. * add this server to the end of our list
  302. */
  303. if ( prevsrvr == NULL ) {
  304. theConfig.ptconfig_serverlist = srvr;
  305. } else {
  306. prevsrvr->ptsrvr_next = srvr;
  307. }
  308. prevsrvr = srvr;
  309. #ifdef PASSTHRU_VERBOSE_LOGGING
  310. /*
  311. * log configuration for debugging purposes
  312. */
  313. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  314. "PTA server host: \"%s\", port: %d, secure: %d,"
  315. " maxconnections: %d, maxconcurrency: %d, timeout: %d,"
  316. " ldversion: %d, connlifetime: %d\n",
  317. srvr->ptsrvr_hostname, srvr->ptsrvr_port,
  318. srvr->ptsrvr_secure, srvr->ptsrvr_maxconnections,
  319. srvr->ptsrvr_maxconcurrency,
  320. srvr->ptsrvr_timeout == NULL ? -1
  321. : srvr->ptsrvr_timeout->tv_sec, srvr->ptsrvr_ldapversion,
  322. srvr->ptsrvr_connlifetime );
  323. for ( prevsuffix = srvr->ptsrvr_suffixes; prevsuffix != NULL;
  324. prevsuffix = prevsuffix->ptsuffix_next ) {
  325. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  326. " normalized suffix: \"%s\"\n",
  327. prevsuffix->ptsuffix_normsuffix );
  328. }
  329. #endif
  330. }
  331. goto done;
  332. error:
  333. slapi_ch_free((void**)&srvr);
  334. done:
  335. return ret;
  336. }
  337. /*
  338. * Get the pass though configuration data. For now, there is only one
  339. * configuration and it is global to the plugin.
  340. */
  341. PassThruConfig *
  342. passthru_get_config( void )
  343. {
  344. return( &theConfig );
  345. }