ptpreop.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. * ptpreop.c - bind pre-operation plugin for Pass Through Authentication
  43. *
  44. */
  45. #include "passthru.h"
  46. static Slapi_PluginDesc pdesc = { "passthruauth", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT,
  47. "pass through authentication plugin" };
  48. /*
  49. * function prototypes
  50. */
  51. static int passthru_bindpreop( Slapi_PBlock *pb );
  52. static int passthru_bindpreop_start( Slapi_PBlock *pb );
  53. static int passthru_bindpreop_close( Slapi_PBlock *pb );
  54. /*
  55. * Plugin initialization function (which must be listed in the appropriate
  56. * slapd config file).
  57. */
  58. int
  59. passthruauth_init( Slapi_PBlock *pb )
  60. {
  61. PASSTHRU_ASSERT( pb != NULL );
  62. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  63. "=> passthruauth_init\n" );
  64. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  65. (void *)SLAPI_PLUGIN_VERSION_01 ) != 0
  66. || slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  67. (void *)&pdesc ) != 0
  68. || slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  69. (void *)passthru_bindpreop_start ) != 0
  70. || slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_BIND_FN,
  71. (void *)passthru_bindpreop ) != 0
  72. || slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN,
  73. (void *)passthru_bindpreop_close ) != 0 ) {
  74. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  75. "passthruauth_init failed\n" );
  76. return( -1 );
  77. }
  78. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  79. "<= passthruauth_init succeeded\n" );
  80. return( 0 );
  81. }
  82. /*
  83. * passthru_bindpreop_start() is called before the directory server
  84. * is fully up. We parse our configuration and initialize any mutexes, etc.
  85. */
  86. static int
  87. passthru_bindpreop_start( Slapi_PBlock *pb )
  88. {
  89. int argc, rc;
  90. char **argv;
  91. PASSTHRU_ASSERT( pb != NULL );
  92. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  93. "=> passthru_bindpreop_start\n" );
  94. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0 ||
  95. slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0 ) {
  96. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  97. "unable to get arguments\n" );
  98. return( -1 );
  99. }
  100. if (( rc = passthru_config( argc, argv )) != LDAP_SUCCESS ) {
  101. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  102. "configuration failed (%s)\n", ldap_err2string( rc ));
  103. return( -1 );
  104. }
  105. return( 0 );
  106. }
  107. /*
  108. * Called right before the Directory Server shuts down.
  109. */
  110. static int
  111. passthru_bindpreop_close( Slapi_PBlock *pb )
  112. {
  113. PASSTHRU_ASSERT( pb != NULL );
  114. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  115. "=> passthru_bindpreop_close\n" );
  116. /*
  117. * close all our open connections.
  118. * XXXmcs: free any memory, mutexes, etc.
  119. */
  120. passthru_close_all_connections( passthru_get_config() );
  121. return( 0 );
  122. }
  123. static int
  124. passthru_bindpreop( Slapi_PBlock *pb )
  125. {
  126. int rc, method, freeresctrls=1;
  127. char *normbinddn, *matcheddn;
  128. char *libldap_errmsg, *pr_errmsg, *errmsg;
  129. PassThruConfig *cfg;
  130. PassThruServer *srvr;
  131. struct berval *creds, **urls;
  132. LDAPControl **reqctrls, **resctrls;
  133. PASSTHRU_ASSERT( pb != NULL );
  134. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  135. "=> passthru_bindpreop\n" );
  136. /*
  137. * retrieve parameters for bind operation
  138. */
  139. if ( slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method ) != 0 ||
  140. slapi_pblock_get( pb, SLAPI_BIND_TARGET, &normbinddn ) != 0 ||
  141. slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &creds ) != 0 ) {
  142. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  143. "<= not handled (unable to retrieve bind parameters)\n" );
  144. return( PASSTHRU_OP_NOT_HANDLED );
  145. }
  146. if ( normbinddn == NULL ) {
  147. normbinddn = "";
  148. }
  149. /*
  150. * We only handle simple bind requests that include non-NULL binddn and
  151. * credentials. Let the Directory Server itself handle everything else.
  152. */
  153. if ( method != LDAP_AUTH_SIMPLE || *normbinddn == '\0'
  154. || creds->bv_len == 0 ) {
  155. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  156. "<= not handled (not simple bind or NULL dn/credentials)\n" );
  157. return( PASSTHRU_OP_NOT_HANDLED );
  158. }
  159. /*
  160. * Get pass through authentication configuration.
  161. */
  162. cfg = passthru_get_config();
  163. /*
  164. * Check to see if the target DN is one we should "pass through" to
  165. * another server.
  166. */
  167. if ( passthru_dn2server( cfg, normbinddn, &srvr ) != LDAP_SUCCESS ) {
  168. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  169. "<= not handled (not one of our suffixes)\n" );
  170. return( PASSTHRU_OP_NOT_HANDLED );
  171. }
  172. /*
  173. * We are now committed to handling this bind request.
  174. * Chain it off to another server.
  175. */
  176. matcheddn = errmsg = libldap_errmsg = pr_errmsg = NULL;
  177. urls = NULL;
  178. resctrls = NULL;
  179. if ( slapi_pblock_get( pb, SLAPI_REQCONTROLS, &reqctrls ) != 0 ) {
  180. rc = LDAP_OPERATIONS_ERROR;
  181. errmsg = "unable to retrieve bind controls";
  182. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM, "%s\n",
  183. errmsg );
  184. } else {
  185. int lderrno;
  186. if (( rc = passthru_simple_bind_s( pb, srvr, PASSTHRU_CONN_TRIES,
  187. normbinddn, creds, reqctrls, &lderrno, &matcheddn,
  188. &libldap_errmsg, &urls, &resctrls )) == LDAP_SUCCESS ) {
  189. rc = lderrno;
  190. errmsg = libldap_errmsg;
  191. } else if ( rc != LDAP_USER_CANCELLED ) { /* not abandoned */
  192. PRErrorCode prerr = PR_GetError();
  193. pr_errmsg = PR_smprintf( "error %d - %s %s ("
  194. SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
  195. rc, ldap_err2string( rc ), srvr->ptsrvr_url,
  196. prerr, slapd_pr_strerror(prerr));
  197. if ( NULL != pr_errmsg ) {
  198. errmsg = pr_errmsg;
  199. } else {
  200. errmsg = ldap_err2string( rc );
  201. }
  202. rc = LDAP_OPERATIONS_ERROR;
  203. }
  204. }
  205. /*
  206. * If bind succeeded, change authentication information associated
  207. * with this connection.
  208. */
  209. if ( rc == LDAP_SUCCESS ) {
  210. char *ndn = slapi_ch_strdup( normbinddn );
  211. if (slapi_pblock_set(pb, SLAPI_CONN_DN, ndn) != 0 ||
  212. slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD,
  213. SLAPD_AUTH_SIMPLE) != 0) {
  214. slapi_ch_free((void **)&ndn);
  215. rc = LDAP_OPERATIONS_ERROR;
  216. errmsg = "unable to set connection DN or AUTHTYPE";
  217. slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
  218. "%s\n", errmsg );
  219. }
  220. }
  221. if ( rc != LDAP_USER_CANCELLED ) { /* not abandoned */
  222. /*
  223. * Send a result to our client.
  224. */
  225. if ( resctrls != NULL ) {
  226. (void)slapi_pblock_set( pb, SLAPI_RESCONTROLS, resctrls );
  227. freeresctrls=0;
  228. }
  229. slapi_send_ldap_result( pb, rc, matcheddn, errmsg, 0, urls );
  230. }
  231. /*
  232. * Clean up -- free allocated memory, etc.
  233. */
  234. if ( urls != NULL ) {
  235. passthru_free_bervals( urls );
  236. }
  237. if ( libldap_errmsg != NULL ) {
  238. ldap_memfree( errmsg );
  239. }
  240. if ( pr_errmsg != NULL ) {
  241. PR_smprintf_free( pr_errmsg );
  242. }
  243. if ( freeresctrls && (resctrls != NULL) ) {
  244. ldap_controls_free( resctrls );
  245. }
  246. if ( matcheddn != NULL ) {
  247. ldap_memfree( matcheddn );
  248. }
  249. slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
  250. "<= handled (error %d - %s)\n", rc, ldap_err2string( rc ));
  251. return( PASSTHRU_OP_HANDLED );
  252. }