ptconfig.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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;
  96. char **suffixarray;
  97. PassThruServer *prevsrvr, *srvr;
  98. PassThruSuffix *suffix, *prevsuffix;
  99. LDAPURLDesc *ludp;
  100. if ( inited ) {
  101. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  102. "only one pass through plugin instance can be used\n" );
  103. return( LDAP_PARAM_ERROR );
  104. }
  105. inited = 1;
  106. /*
  107. * It doesn't make sense to configure a pass through plugin without
  108. * providing at least one remote server. Return an error if attempted.
  109. */
  110. if ( argc < 1 ) {
  111. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  112. "no pass through servers found in configuration"
  113. " (at least one must be listed)\n" );
  114. return( LDAP_PARAM_ERROR );
  115. }
  116. /*
  117. * Parse argv[] values.
  118. */
  119. prevsrvr = NULL;
  120. for ( i = 0; i < argc; ++i ) {
  121. char *p = NULL;
  122. srvr = (PassThruServer *)slapi_ch_calloc( 1, sizeof( PassThruServer ));
  123. srvr->ptsrvr_url = slapi_ch_strdup( argv[i] );
  124. /* since the ldap url may contain both spaces (to delimit multiple hosts)
  125. and commas (in suffixes), we have to search for the first space
  126. after the last /, then look for any commas after that
  127. This assumes the ldap url looks like this:
  128. ldap(s)://host:port host:port .... host:port/suffixes
  129. That is, it assumes there is always a trailing slash on the ldapurl
  130. and that the url does not look like this: ldap://host
  131. also assumes suffixes do not have any / in them
  132. */
  133. if ((p = strrchr(srvr->ptsrvr_url, '/'))) { /* look for last / */
  134. p = strchr(p, ' '); /* look for first space after last / */
  135. if (p) {
  136. if (!strchr(p, ',')) { /* no comma */
  137. p = NULL; /* just use defaults */
  138. }
  139. }
  140. }
  141. if (!p) {
  142. /*
  143. * use defaults for maxconnections, maxconcurrency, timeout,
  144. * LDAP version, and connlifetime.
  145. */
  146. srvr->ptsrvr_maxconnections = PASSTHRU_DEF_SRVR_MAXCONNECTIONS;
  147. srvr->ptsrvr_maxconcurrency = PASSTHRU_DEF_SRVR_MAXCONCURRENCY;
  148. srvr->ptsrvr_timeout = (struct timeval *)slapi_ch_calloc( 1,
  149. sizeof( struct timeval ));
  150. srvr->ptsrvr_timeout->tv_sec = PASSTHRU_DEF_SRVR_TIMEOUT;
  151. srvr->ptsrvr_ldapversion = PASSTHRU_DEF_SRVR_PROTOCOL_VERSION;
  152. using_def_connlifetime = 1;
  153. } else {
  154. /*
  155. * parse parameters. format is:
  156. * maxconnections,maxconcurrency,timeout,ldapversion
  157. * OR maxconnections,maxconcurrency,timeout,ldapversion,lifetime
  158. */
  159. *p++ = '\0'; /* p points at space preceding optional arguments */
  160. rc = sscanf( p, "%d,%d,%d,%d,%d", &srvr->ptsrvr_maxconnections,
  161. &srvr->ptsrvr_maxconcurrency, &tosecs,
  162. &srvr->ptsrvr_ldapversion, &srvr->ptsrvr_connlifetime );
  163. if ( rc < 4 ) {
  164. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  165. "server parameters should be in the form "
  166. "\"maxconnections,maxconcurrency,timeout,ldapversion,"
  167. "connlifetime\" (got \"%s\")\n", p );
  168. return( LDAP_PARAM_ERROR );
  169. } else if ( rc < 5 ) {
  170. using_def_connlifetime = 1;
  171. srvr->ptsrvr_connlifetime = PASSTHRU_DEF_SRVR_CONNLIFETIME;
  172. } else {
  173. using_def_connlifetime = 0;
  174. }
  175. if ( srvr->ptsrvr_ldapversion != LDAP_VERSION2
  176. && srvr->ptsrvr_ldapversion != LDAP_VERSION3 ) {
  177. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  178. "LDAP protocol version should be %d or %d (got %d)\n",
  179. LDAP_VERSION2, LDAP_VERSION3,
  180. srvr->ptsrvr_ldapversion );
  181. return( LDAP_PARAM_ERROR );
  182. }
  183. if ( srvr->ptsrvr_maxconnections <= 0 ) {
  184. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  185. "maximum connections must be greater than "
  186. "zero (got %d)\n", srvr->ptsrvr_maxconnections );
  187. return( LDAP_PARAM_ERROR );
  188. }
  189. if ( srvr->ptsrvr_maxconcurrency <= 0 ) {
  190. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  191. "maximum concurrency must be greater than "
  192. "zero (got %d)\n", srvr->ptsrvr_maxconcurrency );
  193. return( LDAP_PARAM_ERROR );
  194. }
  195. if ( tosecs <= 0 ) {
  196. srvr->ptsrvr_timeout = NULL;
  197. } else {
  198. srvr->ptsrvr_timeout = (struct timeval *)slapi_ch_calloc( 1,
  199. sizeof( struct timeval ));
  200. srvr->ptsrvr_timeout->tv_sec = tosecs;
  201. }
  202. }
  203. /*
  204. * parse the LDAP URL
  205. */
  206. if (( rc = ldap_url_parse( srvr->ptsrvr_url, &ludp )) != 0 ) {
  207. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  208. "unable to parse LDAP URL \"%s\" (%s)\n",
  209. srvr->ptsrvr_url, passthru_urlparse_err2string( rc ));
  210. return( LDAP_PARAM_ERROR );
  211. }
  212. if ( ludp->lud_dn == NULL || *ludp->lud_dn == '\0' ) {
  213. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  214. "missing suffix in LDAP URL \"%s\"\n",
  215. srvr->ptsrvr_url );
  216. return( LDAP_PARAM_ERROR );
  217. }
  218. srvr->ptsrvr_hostname = slapi_ch_strdup( ludp->lud_host );
  219. srvr->ptsrvr_port = ludp->lud_port;
  220. srvr->ptsrvr_secure =
  221. (( ludp->lud_options & LDAP_URL_OPT_SECURE ) != 0 );
  222. /*
  223. * If a space-separated list of hosts is configured for failover,
  224. * use a different (non infinite) default for connection lifetime.
  225. */
  226. if ( using_def_connlifetime &&
  227. strchr( srvr->ptsrvr_hostname, ' ' ) != NULL ) {
  228. srvr->ptsrvr_connlifetime =
  229. PASSTHRU_DEF_SRVR_FAILOVERCONNLIFETIME;
  230. }
  231. /*
  232. * split the DN into multiple suffixes (separated by ';')
  233. */
  234. if (( suffixarray = ldap_str2charray( ludp->lud_dn, ";" )) == NULL ) {
  235. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  236. "unable to parse suffix string \"%s\" within \"%s\"\n",
  237. ludp->lud_dn, srvr->ptsrvr_url );
  238. return( LDAP_PARAM_ERROR );
  239. }
  240. /*
  241. * free our LDAP URL descriptor
  242. */
  243. ldap_free_urldesc( ludp );
  244. ludp = NULL;
  245. /*
  246. * reorganize the suffixes into a linked list and normalize them
  247. */
  248. prevsuffix = NULL;
  249. for ( j = 0; suffixarray[ j ] != NULL; ++j ) {
  250. /*
  251. * allocate a new PassThruSuffix structure and fill it in.
  252. */
  253. suffix = (PassThruSuffix *)slapi_ch_malloc(
  254. sizeof( PassThruSuffix ));
  255. suffix->ptsuffix_normsuffix =
  256. slapi_dn_normalize( suffixarray[ j ] );
  257. suffixarray[ j ] = NULL;
  258. suffix->ptsuffix_len = strlen( suffix->ptsuffix_normsuffix );
  259. suffix->ptsuffix_next = NULL;
  260. /*
  261. * add to end of list
  262. */
  263. if ( prevsuffix == NULL ) {
  264. srvr->ptsrvr_suffixes = suffix;
  265. } else {
  266. prevsuffix->ptsuffix_next = suffix;
  267. }
  268. prevsuffix = suffix;
  269. }
  270. ldap_memfree( suffixarray );
  271. /*
  272. * create mutexes and condition variables for this server
  273. */
  274. if (( srvr->ptsrvr_connlist_mutex = slapi_new_mutex()) == NULL ||
  275. ( srvr->ptsrvr_connlist_cv = slapi_new_condvar(
  276. srvr->ptsrvr_connlist_mutex )) == NULL ) {
  277. return( LDAP_LOCAL_ERROR );
  278. }
  279. /*
  280. * add this server to the end of our list
  281. */
  282. if ( prevsrvr == NULL ) {
  283. theConfig.ptconfig_serverlist = srvr;
  284. } else {
  285. prevsrvr->ptsrvr_next = srvr;
  286. }
  287. prevsrvr = srvr;
  288. #ifdef PASSTHRU_VERBOSE_LOGGING
  289. /*
  290. * log configuration for debugging purposes
  291. */
  292. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  293. "PTA server host: \"%s\", port: %d, secure: %d,"
  294. " maxconnections: %d, maxconcurrency: %d, timeout: %d,"
  295. " ldversion: %d, connlifetime: %d\n",
  296. srvr->ptsrvr_hostname, srvr->ptsrvr_port,
  297. srvr->ptsrvr_secure, srvr->ptsrvr_maxconnections,
  298. srvr->ptsrvr_maxconcurrency,
  299. srvr->ptsrvr_timeout == NULL ? -1
  300. : srvr->ptsrvr_timeout->tv_sec, srvr->ptsrvr_ldapversion,
  301. srvr->ptsrvr_connlifetime );
  302. for ( prevsuffix = srvr->ptsrvr_suffixes; prevsuffix != NULL;
  303. prevsuffix = prevsuffix->ptsuffix_next ) {
  304. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  305. " normalized suffix: \"%s\"\n",
  306. prevsuffix->ptsuffix_normsuffix );
  307. }
  308. #endif
  309. }
  310. return( LDAP_SUCCESS );
  311. }
  312. /*
  313. * Get the pass though configuration data. For now, there is only one
  314. * configuration and it is global to the plugin.
  315. */
  316. PassThruConfig *
  317. passthru_get_config( void )
  318. {
  319. return( &theConfig );
  320. }