vattrsp.c 11 KB

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