vattrsp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include <stdio.h>
  42. #include <string.h>
  43. #include "portable.h"
  44. #include "nspr.h"
  45. #include "slapi-plugin.h"
  46. #include "slapi-private.h"
  47. #include "vattr_spi.h"
  48. /* the global plugin handle */
  49. static volatile vattr_sp_handle *vattr_handle = NULL;
  50. /* get file mode flags for unix */
  51. #ifndef _WIN32
  52. #include <sys/stat.h>
  53. #endif
  54. #define VATTRSP_PLUGIN_SUBSYSTEM "vattrsp-template-plugin" /* used for logging */
  55. /* function prototypes */
  56. int vattrsp_init( Slapi_PBlock *pb );
  57. static int vattrsp_start( Slapi_PBlock *pb );
  58. static int vattrsp_close( Slapi_PBlock *pb );
  59. static int vattrsp_vattr_get(
  60. vattr_sp_handle *handle,
  61. vattr_context *c,
  62. Slapi_Entry *e,
  63. char *type,
  64. Slapi_ValueSet** results,
  65. int *type_name_disposition,
  66. char** actual_type_name,
  67. int flags,
  68. int *free_flags,
  69. void *hint
  70. );
  71. static int vattrsp_vattr_compare(
  72. vattr_sp_handle *handle,
  73. vattr_context *c,
  74. Slapi_Entry *e,
  75. char *type,
  76. Slapi_Value *test_this,
  77. int* result,
  78. int flags,
  79. void *hint
  80. );
  81. static int vattrsp_vattr_types(
  82. vattr_sp_handle *handle,
  83. Slapi_Entry *e,
  84. vattr_type_list_context *type_context,
  85. int flags
  86. );
  87. static Slapi_PluginDesc pdesc = { "vattrexamplesp", VENDOR, DS_PACKAGE_VERSION,
  88. "vattr service provider example plugin" };
  89. static void * vattrsp_plugin_identity = NULL;
  90. #ifdef _WIN32
  91. int *module_ldap_debug = 0;
  92. void plugin_init_debug_level(int *level_ptr)
  93. {
  94. module_ldap_debug = level_ptr;
  95. }
  96. #endif
  97. /*
  98. * Plugin identity mgmt
  99. * --------------------
  100. * Used for internal search api's
  101. */
  102. void vattrsp_set_plugin_identity(void * identity)
  103. {
  104. vattrsp_plugin_identity=identity;
  105. }
  106. void * vattrsp_get_plugin_identity()
  107. {
  108. return vattrsp_plugin_identity;
  109. }
  110. /*
  111. * vattrsp_init
  112. * --------
  113. * adds our callbacks to the list
  114. * this is called even if the plugin is disabled
  115. * so do not do anything here which enables the
  116. * plugin functionality - also no other plugin has been started yet
  117. * so you cant use the functionality of other plugins here
  118. *
  119. * When does this get called?
  120. * At server start up. This is the first function in
  121. * the plugin to get called, and no guarantees are made
  122. * about whether the init() function of other plugins
  123. * have been called. It is really only safe to register
  124. * the standard SLAPI_PLUGIN_* interfaces here.
  125. *
  126. * returns 0 on success
  127. */
  128. int vattrsp_init( Slapi_PBlock *pb )
  129. {
  130. int ret = 0;
  131. void * plugin_identity=NULL;
  132. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"--> vattrsp_init\n");
  133. /*
  134. * Store the plugin identity for later use.
  135. * Used for internal operations
  136. */
  137. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  138. PR_ASSERT (plugin_identity);
  139. vattrsp_set_plugin_identity(plugin_identity);
  140. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  141. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  142. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  143. (void *) vattrsp_start ) != 0 ||
  144. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  145. (void *) vattrsp_close ) != 0 ||
  146. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  147. (void *)&pdesc ) != 0 )
  148. {
  149. slapi_log_error( SLAPI_LOG_FATAL, VATTRSP_PLUGIN_SUBSYSTEM,
  150. "vattrsp_init: failed to register plugin\n" );
  151. ret = -1;
  152. }
  153. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"<-- vattrsp_init\n");
  154. return ret;
  155. }
  156. /*
  157. * vattrsp_start
  158. * ---------
  159. * This is called after vattrsp_init, this is where plugin init starts.
  160. * Dependencies on other plugins have been satisfied so
  161. * feel free to use them e.g. statechange plugin to keep
  162. * an eye on configuration changes
  163. *
  164. * pb should contain SLAPI_TARGET_DN, which is the dn
  165. * of the entry representing this plugin, usually in
  166. * cn=plugins, cn=config. Use this to get configuration
  167. * specific to your plugin from the entry
  168. *
  169. * When does this get called?
  170. * At server start up, after the vattrsp_init() function is
  171. * called and when the start function of all plugins this one
  172. * depends on have been called. It is safe to rely
  173. * on dependencies from now on e.g. perform search operations
  174. *
  175. * returns 0 on success
  176. */
  177. int vattrsp_start( Slapi_PBlock *pb )
  178. {
  179. int ret = 0;
  180. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"--> vattrsp_start\n");
  181. /* register this vattr service provider with vattr subsystem */
  182. if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
  183. vattrsp_vattr_get,
  184. vattrsp_vattr_compare,
  185. vattrsp_vattr_types) != 0)
  186. {
  187. slapi_log_error( SLAPI_LOG_FATAL, VATTRSP_PLUGIN_SUBSYSTEM,
  188. "vattrsp_start: cannot register as service provider\n" );
  189. ret = -1;
  190. goto out;
  191. }
  192. /* register a vattr */
  193. /* TODO: change dummyAttr to your attribute type,
  194. * or write some configuration code which discovers
  195. * the attributes to register
  196. */
  197. slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle, "dummyAttr", NULL, NULL);
  198. out:
  199. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"<-- vattrsp_start\n");
  200. return ret;
  201. }
  202. /*
  203. * vattrsp_close
  204. * ---------
  205. * closes down the plugin
  206. *
  207. * When does this get called?
  208. * On server shutdown
  209. *
  210. * returns 0 on success
  211. */
  212. int vattrsp_close( Slapi_PBlock *pb )
  213. {
  214. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"--> vattrsp_close\n");
  215. /* clean up stuff here */
  216. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"<-- vattrsp_close\n");
  217. return 0;
  218. }
  219. /*
  220. * vattrsp_vattr_get
  221. * -----------------
  222. * vattr subsystem is requesting the value(s) for "type"
  223. *
  224. * When does this get called?
  225. * Whenever a client requests the attribute "type"
  226. * this may be indirectly by a request for all
  227. * attributes
  228. *
  229. * returns 0 on success
  230. */
  231. int vattrsp_vattr_get(
  232. vattr_sp_handle *handle, /* pass through to subsequent vattr calls */
  233. vattr_context *c, /* opaque context, pass through to subsequent vattr calls */
  234. Slapi_Entry *e, /* the entry that the values are for */
  235. char *type, /* the type that the values are requested for */
  236. Slapi_ValueSet** results, /* put the values here */
  237. int *type_name_disposition, /* whether the type is a sub-type etc. */
  238. char** actual_type_name, /* maybe you call this another type */
  239. int flags, /* see slapi-plugin.h to support these flags */
  240. int *free_flags, /* let vattr subsystem know if you supplied value copies it must free */
  241. void *hint /* opaque hint, pass through to subsequent vattr calls */
  242. )
  243. {
  244. int ret = SLAPI_VIRTUALATTRS_NOT_FOUND;
  245. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"--> vattrsp_vattr_get\n");
  246. /* usual to schema check an attribute
  247. * there may be sanity checks which can
  248. * be done prior to this "relatively"
  249. * slow function to ensure least work done
  250. * to fail
  251. */
  252. if(!slapi_vattr_schema_check_type(e, type))
  253. return ret;
  254. /* TODO: do your thing, resolve the attribute */
  255. /* some vattr sps may look after more than one
  256. * attribute, this one does not, so no need to
  257. * check against "type", we know its our "dummyAttr"
  258. */
  259. {
  260. /* TODO: replace this with your resolver */
  261. Slapi_Value *val = slapi_value_new_string("dummyValue");
  262. *results = slapi_valueset_new();
  263. slapi_valueset_add_value(*results, val);
  264. ret = 0;
  265. }
  266. if(ret == 0)
  267. {
  268. /* TODO: set *free_flags
  269. * if allocated memory for values
  270. * use: SLAPI_VIRTUALATTRS_RETURNED_COPIES
  271. *
  272. * otherwise, if you can guarantee that
  273. * this value will live beyond this operation
  274. * use: SLAPI_VIRTUALATTRS_RETURNED_POINTERS
  275. */
  276. *free_flags = SLAPI_VIRTUALATTRS_RETURNED_COPIES;
  277. /* say the type is the same as the one requested
  278. * could be your vattr needs to switch types, say to
  279. * make one type look like another or something, but
  280. * that is unusual
  281. */
  282. *actual_type_name = slapi_ch_strdup(type);
  283. /* TODO: set *type_name_disposition
  284. * if the type matched exactly or it
  285. * is an alias for the type then
  286. * use: SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS
  287. *
  288. * otherwise, the actual_type_name is a subtype
  289. * of type
  290. * use: SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE
  291. */
  292. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
  293. }
  294. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"<-- vattrsp_cache_vattr_get\n");
  295. return ret;
  296. }
  297. /*
  298. * vattrsp_vattr_compare
  299. * ---------------------
  300. * vattr subsystem is requesting that the values are compared
  301. *
  302. * When does this get called?
  303. * When an LDAP compare operation against this
  304. * type is requested by the client
  305. *
  306. * returns 0 on success
  307. */
  308. int vattrsp_vattr_compare(
  309. vattr_sp_handle *handle, /* pass through to subsequent vattr calls */
  310. vattr_context *c, /* opaque context, pass through to subsequent vattr calls */
  311. Slapi_Entry *e, /* the entry that the values are for */
  312. char *type, /* the type that the values belong to */
  313. Slapi_Value *test_this, /* the value to compare against */
  314. int* result, /* 1 for matched, 0 for not matched */
  315. int flags, /* see slapi-plugin.h to support these flags */
  316. void *hint /* opaque hint, pass through to subsequent vattr calls */
  317. )
  318. {
  319. int ret = SLAPI_VIRTUALATTRS_NOT_FOUND; /* assume failure */
  320. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"--> vattrsp_vattr_compare\n");
  321. /* TODO: do your thing, compare the attribute */
  322. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"<-- vattrsp_vattr_compare\n");
  323. return ret;
  324. }
  325. /*
  326. * vattrsp_vattr_types
  327. * -------------------
  328. * vattr subsystem is requesting the types that you
  329. * promise to supply values for if it calls
  330. * vattrsp_vattr_get() with each type.
  331. * Guesses are not good enough, the wrong answer
  332. * effects what happens when all attributes
  333. * are requested in the LDAP search operation.
  334. * If you do not own up to an attribute,
  335. * vattrsp_vattr_get() will never get called for it.
  336. * If you say you will supply an attribute but
  337. * vattrsp_vattr_get() does not supply a value
  338. * then the attribute is returned, but *with* *no*
  339. * *values*
  340. *
  341. * When does this get called?
  342. * Only when all attributes are requested by the client
  343. * and just prior to multiple calls to vattrsp_vattr_get()
  344. *
  345. * returns 0 on success
  346. */
  347. int vattrsp_vattr_types(
  348. vattr_sp_handle *handle, /* pass through to subsequent vattr calls */
  349. Slapi_Entry *e, /* the entry that the types should have values for */
  350. vattr_type_list_context *type_context, /* where we put the types */
  351. int flags /* see slapi-plugin.h to support these flags */
  352. )
  353. {
  354. int ret = 0; /* assume success */
  355. char *attr = "dummyAttr"; /* an attribute type that we will deliver */
  356. int props = 0; /* properties of the attribute, make this SLAPI_ATTR_FLAG_OPATTR for operational attributes */
  357. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"--> vattrsp_vattr_types\n");
  358. /* TODO: for each type you will supply... */
  359. if(ret)
  360. {
  361. /* ...do this */
  362. /* entry contains this attr */
  363. vattr_type_thang thang = {0};
  364. thang.type_name = strcpy(thang.type_name,attr);
  365. thang.type_flags = props;
  366. /* add the type to the type context */
  367. slapi_vattrspi_add_type(type_context,&thang,0);
  368. }
  369. slapi_log_error( SLAPI_LOG_TRACE, VATTRSP_PLUGIN_SUBSYSTEM,"<-- vattrsp_vattr_types\n");
  370. return ret;
  371. }