1
0

testbind.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /************************************************************
  39. testbind.c
  40. This source file provides an example of a pre-operation plug-in
  41. function that handles authentication.
  42. Note that the Directory Server front-end handles bind
  43. operations requested by the root DN. The server does not
  44. invoke your plug-in function if the client is authenicating
  45. as the root DN.
  46. To test this plug-in function, stop the server, edit the dse.ldif file
  47. (in the <server_root>/slapd-<server_id>/config directory)
  48. and add the following lines before restarting the server :
  49. dn: cn=Test Bind,cn=plugins,cn=config
  50. objectClass: top
  51. objectClass: nsSlapdPlugin
  52. objectClass: extensibleObject
  53. cn: Test Bind
  54. nsslapd-pluginPath: <server_root>/plugins/slapd/slapi/examples/libtest-plugin.so
  55. nsslapd-pluginInitfunc: testbind_init
  56. nsslapd-pluginType: preoperation
  57. nsslapd-pluginEnabled: on
  58. nsslapd-plugin-depends-on-type: database
  59. nsslapd-pluginId: test-bind
  60. ************************************************************/
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include "slapi-plugin.h"
  64. Slapi_PluginDesc bindpdesc = { "test-bind", "Fedora Project", "1.0.2",
  65. "sample bind pre-operation plugin" };
  66. static Slapi_ComponentId *plugin_id = NULL;
  67. /* Pre-operation plug-in function */
  68. int
  69. test_bind( Slapi_PBlock *pb )
  70. {
  71. char *dn, *attrs[2] = { SLAPI_USERPWD_ATTR, NULL };
  72. int method, rc = LDAP_SUCCESS;
  73. struct berval *credentials;
  74. struct berval **pwvals;
  75. Slapi_DN *sdn = NULL;
  76. Slapi_Entry *e = NULL;
  77. Slapi_Attr *attr = NULL;
  78. /* Log a message to the server error log. */
  79. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  80. "Pre-operation bind function called.\n" );
  81. /* Gets parameters available when processing an LDAP bind
  82. operation. */
  83. if ( slapi_pblock_get( pb, SLAPI_BIND_TARGET, &dn ) != 0 ||
  84. slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method ) != 0 ||
  85. slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &credentials ) != 0 ) {
  86. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  87. "Could not get parameters for bind operation\n" );
  88. slapi_send_ldap_result( pb, LDAP_OPERATIONS_ERROR,
  89. NULL, NULL, 0, NULL );
  90. return( 1 );
  91. }
  92. /* Check the authentication method */
  93. switch( method ) {
  94. case LDAP_AUTH_SIMPLE:
  95. /* First, get the entry specified by the DN. */
  96. sdn = slapi_sdn_new_dn_byref( dn );
  97. rc = slapi_search_internal_get_entry( sdn, attrs, &e,
  98. plugin_id );
  99. slapi_sdn_free( &sdn );
  100. if ( rc != LDAP_SUCCESS ) {
  101. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  102. "Could not find entry %s (error %d)\n",
  103. dn, rc );
  104. break;
  105. }
  106. /* Next, check credentials against the userpassword attribute
  107. of that entry. */
  108. if ( e != NULL ) {
  109. Slapi_Value *credval, **pwvals;
  110. int i, hint, valcount;
  111. if ( slapi_entry_attr_find( e, SLAPI_USERPWD_ATTR,
  112. &attr ) != 0 || slapi_attr_get_numvalues( attr,
  113. &valcount ) != 0 ) {
  114. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  115. "Entry has no %s attribute values\n",
  116. SLAPI_USERPWD_ATTR );
  117. rc = LDAP_INAPPROPRIATE_AUTH;
  118. break;
  119. }
  120. credval = slapi_value_new_berval( credentials );
  121. pwvals = (Slapi_Value **)slapi_ch_calloc( valcount,
  122. sizeof( Slapi_Value * ));
  123. i = 0;
  124. for ( hint = slapi_attr_first_value( attr, &pwvals[i] );
  125. hint != -1; hint = slapi_attr_next_value( attr,
  126. hint, &pwvals[i] )) {
  127. ++i;
  128. }
  129. if ( slapi_pw_find_sv( pwvals, credval ) != 0 ) {
  130. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  131. "Credentials are not correct\n" );
  132. rc = LDAP_INVALID_CREDENTIALS;
  133. }
  134. slapi_value_free( &credval );
  135. slapi_ch_free( (void **)&pwvals );
  136. if ( LDAP_SUCCESS != rc ) {
  137. break;
  138. }
  139. } else {
  140. /* This should not happen. The previous section of code
  141. already checks for this case. */
  142. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  143. "Could find entry for %s\n", dn );
  144. rc = LDAP_NO_SUCH_OBJECT;
  145. break;
  146. }
  147. /* Set the DN and authentication method for the connection. */
  148. if ( slapi_pblock_set( pb, SLAPI_CONN_DN,
  149. slapi_ch_strdup( dn ) ) != 0 ||
  150. slapi_pblock_set( pb, SLAPI_CONN_AUTHMETHOD,
  151. SLAPD_AUTH_SIMPLE ) != 0 ) {
  152. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  153. "Failed to set DN and method for connection\n" );
  154. rc = LDAP_OPERATIONS_ERROR;
  155. break;
  156. }
  157. /* Send a "success" result code back to the client. */
  158. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  159. "Authenticated: %s\n", dn );
  160. rc = LDAP_SUCCESS;
  161. break;
  162. /* If NONE is specified, the client is requesting to bind anonymously.
  163. Normally, this case should be handled by the server's front-end
  164. before it calls this plug-in function. Just in case this does
  165. get through to the plug-in function, you can handle this by
  166. sending a successful result code back to the client and returning
  167. 1.
  168. */
  169. case LDAP_AUTH_NONE:
  170. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  171. "Authenticating anonymously\n" );
  172. rc = LDAP_SUCCESS;
  173. break;
  174. /* This plug-in does not support any other method of authentication */
  175. case LDAP_AUTH_SASL:
  176. default:
  177. slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
  178. "Unsupported authentication method requested: %d\n",
  179. method );
  180. rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
  181. break;
  182. }
  183. slapi_send_ldap_result( pb, rc, NULL, NULL, 0, NULL );
  184. return( 1 );
  185. }
  186. /* Pre-operation plug-in function */
  187. int
  188. test_search( Slapi_PBlock *pb )
  189. {
  190. char *reqdn;
  191. /* Log a message to the server error log. */
  192. slapi_log_error( SLAPI_LOG_PLUGIN, "test_search",
  193. "Pre-operation search function called.\n" );
  194. /* Get requestor of search operation. This is not critical
  195. to performing the search (this plug-in just serves as
  196. confirmation that the bind plug-in works), so return 0
  197. if this fails. */
  198. if ( slapi_pblock_get( pb, SLAPI_REQUESTOR_DN, &reqdn ) != 0 ) {
  199. slapi_log_error( SLAPI_LOG_PLUGIN, "test_search",
  200. "Could not get requestor parameter for search operation\n" );
  201. return( 0 );
  202. }
  203. /* Indicate who is requesting the search */
  204. if ( reqdn != NULL && *reqdn != '\0' ) {
  205. slapi_log_error( SLAPI_LOG_PLUGIN, "test_search",
  206. "Search requested by %s\n", reqdn );
  207. } else {
  208. slapi_log_error( SLAPI_LOG_PLUGIN, "test_search",
  209. "Search requested by anonymous client\n" );
  210. }
  211. return( 0 );
  212. }
  213. /* Initialization function */
  214. #ifdef _WIN32
  215. __declspec(dllexport)
  216. #endif
  217. int
  218. testbind_init( Slapi_PBlock *pb )
  219. {
  220. /* Retrieve and save the plugin identity to later pass to
  221. internal operations */
  222. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
  223. slapi_log_error( SLAPI_LOG_PLUGIN, "testbind_init",
  224. "Failed to retrieve SLAPI_PLUGIN_IDENTITY\n" );
  225. return( -1 );
  226. }
  227. /* Register the pre-operation bind function and specify
  228. the server plug-in version. */
  229. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  230. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  231. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  232. (void *)&bindpdesc ) != 0 ||
  233. slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_BIND_FN,
  234. (void *) test_bind ) != 0 ||
  235. slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_SEARCH_FN,
  236. (void *) test_search ) != 0 ) {
  237. slapi_log_error( SLAPI_LOG_PLUGIN, "testbind_init",
  238. "Failed to set version and functions\n" );
  239. return( -1 );
  240. }
  241. return( 0 );
  242. }