vattrsp.c 13 KB

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