vattrsp.c 13 KB

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