cb_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "cb.h"
  13. /* return the rootdn configured in the server */
  14. char * cb_get_rootdn() {
  15. char * ret=slapi_get_rootdn();
  16. if (ret == NULL)
  17. ret = slapi_ch_strdup(CB_DIRECTORY_MANAGER_DN);
  18. if (ret)
  19. slapi_dn_ignore_case(ret); /* UTF8-aware */
  20. return ret;
  21. }
  22. void
  23. cb_send_ldap_result(Slapi_PBlock *pb, int err, char *matched,char *text, int nentries, struct berval **urls )
  24. {
  25. cb_set_acl_policy(pb);
  26. slapi_send_ldap_result( pb, err, matched, text, nentries ,urls);
  27. }
  28. Slapi_Entry * cb_LDAPMessage2Entry(LDAP * ld, LDAPMessage * msg, int attrsonly) {
  29. Slapi_Entry * e = slapi_entry_alloc();
  30. char * a=NULL;
  31. BerElement * ber=NULL;
  32. if ( e == NULL ) return NULL;
  33. if (msg == NULL) {
  34. slapi_entry_free(e);
  35. return NULL;
  36. }
  37. /*
  38. * dn not allocated by slapi
  39. * attribute type and values ARE allocated
  40. */
  41. slapi_entry_set_dn( e, ldap_get_dn( ld, msg ) );
  42. for ( a = ldap_first_attribute( ld, msg, &ber ); a!=NULL;
  43. a=ldap_next_attribute( ld, msg, ber ) ) {
  44. if(attrsonly) {
  45. slapi_entry_add_value(e, a, (Slapi_Value *)NULL);
  46. ldap_memfree(a);
  47. } else {
  48. struct berval ** aVal = ldap_get_values_len( ld, msg, a);
  49. slapi_entry_add_values( e, a, aVal);
  50. ldap_memfree(a);
  51. ldap_value_free_len(aVal);
  52. }
  53. }
  54. if ( NULL != ber )
  55. ber_free( ber, 0 );
  56. return e;
  57. }
  58. struct berval ** referrals2berval(char ** referrals) {
  59. int i;
  60. struct berval ** val=NULL;
  61. if (referrals == NULL)
  62. return NULL;
  63. for (i=0;referrals[i];i++) {}
  64. val = (struct berval **) slapi_ch_calloc(1,(i+1)*sizeof(struct berval *));
  65. for (i=0;referrals[i];i++) {
  66. val[i]=(struct berval *) slapi_ch_malloc(sizeof(struct berval));
  67. val[i]->bv_len= strlen(referrals[i]);
  68. val[i]->bv_val = slapi_ch_strdup(referrals[i]);
  69. }
  70. return val;
  71. }
  72. /*
  73. ** Return LDAP_SUCCESS if an internal operation needs to be forwarded to
  74. ** the farm server. We check chaining policy for internal operations
  75. ** We also check max hop count for loop detection for both internal
  76. ** and external operations
  77. */
  78. int
  79. cb_forward_operation(Slapi_PBlock * pb)
  80. {
  81. Slapi_Operation *op = NULL;
  82. Slapi_Backend *be;
  83. struct slapi_componentid *cid = NULL;
  84. char *pname;
  85. cb_backend_instance *cb;
  86. int retcode;
  87. LDAPControl **ctrls=NULL;
  88. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  89. if (NULL == op) {
  90. slapi_log_error(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM, "No operation is set.\n");
  91. return LDAP_UNWILLING_TO_PERFORM;
  92. }
  93. /* Loop detection */
  94. slapi_pblock_get( pb, SLAPI_REQCONTROLS, &ctrls );
  95. if ( NULL != ctrls ) {
  96. struct berval *ctl_value=NULL;
  97. int iscritical=0;
  98. if (slapi_control_present(ctrls,CB_LDAP_CONTROL_CHAIN_SERVER,&ctl_value,&iscritical) &&
  99. BV_HAS_DATA(ctl_value)) {
  100. /* Decode control data */
  101. /* hop INTEGER (0 .. maxInt) */
  102. ber_int_t hops = 0;
  103. int rc;
  104. BerElement *ber = NULL;
  105. if ((ber = ber_init(ctl_value)) == NULL) {
  106. slapi_log_error(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  107. "cb_forward_operation: ber_init: Memory allocation failed");
  108. if (iscritical)
  109. return LDAP_UNAVAILABLE_CRITICAL_EXTENSION; /* RFC 4511 4.1.11 */
  110. else
  111. return LDAP_NO_MEMORY;
  112. }
  113. rc = ber_scanf(ber,"i",&hops);
  114. if (LBER_ERROR == rc) {
  115. slapi_log_error(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  116. "Loop detection control badly encoded.");
  117. ber_free(ber,1);
  118. if (iscritical)
  119. return LDAP_UNAVAILABLE_CRITICAL_EXTENSION; /* RFC 4511 4.1.11 */
  120. else
  121. return LDAP_LOOP_DETECT;
  122. }
  123. if (hops <= 0) {
  124. slapi_log_error(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  125. "Max hop count exceeded. Loop detected.\n");
  126. ber_free(ber,1);
  127. if (iscritical)
  128. return LDAP_UNAVAILABLE_CRITICAL_EXTENSION; /* RFC 4511 4.1.11 */
  129. else
  130. return LDAP_LOOP_DETECT;
  131. }
  132. ber_free(ber,1);
  133. }
  134. }
  135. if ( !operation_is_flag_set(op, OP_FLAG_INTERNAL))
  136. return LDAP_SUCCESS;
  137. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &cid);
  138. if ( cid == NULL ) {
  139. /* programming error in the front-end */
  140. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  141. "NULL component identity in an internal operation.");
  142. return LDAP_UNWILLING_TO_PERFORM;
  143. }
  144. pname=cid->sci_component_name;
  145. if (cb_debug_on()) {
  146. slapi_log_error(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
  147. "internal op received from %s component \n",pname ? pname : "NULL");
  148. }
  149. /* First, make sure chaining is not denied */
  150. if (operation_is_flag_set(op, SLAPI_OP_FLAG_NEVER_CHAIN))
  151. return LDAP_UNWILLING_TO_PERFORM;
  152. /* unidentified caller. should not happen */
  153. if (pname == NULL)
  154. return LDAP_UNWILLING_TO_PERFORM;
  155. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  156. cb = cb_get_instance(be);
  157. /* Local policy */
  158. slapi_rwlock_rdlock(cb->rwl_config_lock);
  159. if ( cb->chaining_components != NULL ) {
  160. retcode=charray_inlist(cb->chaining_components,pname);
  161. slapi_rwlock_unlock(cb->rwl_config_lock);
  162. if ( retcode )
  163. retcode=LDAP_SUCCESS;
  164. else
  165. retcode=LDAP_UNWILLING_TO_PERFORM;
  166. return retcode;
  167. }
  168. slapi_rwlock_unlock(cb->rwl_config_lock);
  169. /* Global policy */
  170. slapi_rwlock_rdlock(cb->backend_type->config.rwl_config_lock);
  171. retcode=charray_inlist(cb->backend_type->config.chaining_components,pname);
  172. slapi_rwlock_unlock(cb->backend_type->config.rwl_config_lock);
  173. if ( retcode )
  174. retcode=LDAP_SUCCESS;
  175. else
  176. retcode=LDAP_UNWILLING_TO_PERFORM;
  177. return retcode;
  178. }
  179. /* better atol -- it understands a trailing multiplier k/m/g
  180. * for example, "32k" will be returned as 32768
  181. */
  182. long cb_atol(char *str)
  183. {
  184. long multiplier = 1;
  185. char *x = str;
  186. /* find possible trailing k/m/g */
  187. while ((*x >= '0') && (*x <= '9')) x++;
  188. switch (*x) {
  189. case 'g':
  190. case 'G':
  191. multiplier *= 1024;
  192. case 'm':
  193. case 'M':
  194. multiplier *= 1024;
  195. case 'k':
  196. case 'K':
  197. multiplier *= 1024;
  198. }
  199. return (atol(str) * multiplier);
  200. }
  201. int cb_atoi(char *str)
  202. {
  203. return (int)cb_atol(str);
  204. }
  205. /* This function is used by the instance modify callback to add a new
  206. * suffix. It return LDAP_SUCCESS on success.
  207. */
  208. int cb_add_suffix(cb_backend_instance *inst, struct berval **bvals, int apply_mod, char *returntext)
  209. {
  210. Slapi_DN *suffix;
  211. int x;
  212. returntext[0] = '\0';
  213. for (x = 0; bvals[x]; x++) {
  214. suffix=slapi_sdn_new_dn_byval(bvals[x]->bv_val);
  215. if (!slapi_be_issuffix(inst->inst_be, suffix) && apply_mod) {
  216. slapi_be_addsuffix(inst->inst_be, suffix);
  217. }
  218. slapi_sdn_free(&suffix);
  219. }
  220. return LDAP_SUCCESS;
  221. }
  222. static int debug_on=0;
  223. int cb_debug_on()
  224. {
  225. return debug_on;
  226. }
  227. void cb_set_debug(int on) {
  228. debug_on=on;
  229. }
  230. /* this function is called when state of a backend changes */
  231. /* The purpose of this function is to handle the associated_be_is_disabled
  232. flag in the cb instance structure. The associated database is used to
  233. perform local acl evaluations. The associated database can be
  234. 1) The chaining backend is the backend of a sub suffix, and the
  235. parent suffix has a local backend
  236. 2) Entry distribution is being used to distribute write operations to
  237. a chaining backend and other operations to a local backend
  238. (e.g. a replication hub or consumer)
  239. If the associated local backend is being initialized (import), it will be
  240. disabled, and it will be impossible to evaluate local acls. In this case,
  241. we still want to be able to chain operations to a farm server or another
  242. database chain. But the current code will not allow cascading without
  243. local acl evaluation (cb_controls.c). associated_be_is_disabled allows
  244. us to relax that restriction while the associated backend is disabled
  245. */
  246. /*
  247. The first thing we need to do is to determine what our associated backends
  248. are. An associated backend is defined as a backend used by the same
  249. suffix which uses this cb instance or a backend used by any
  250. parent suffix of the suffix which uses this cb instance
  251. We first see if the be_name is for a local database. If not, then just return.
  252. So for the given be_name, we find the suffix which uses it, then the mapping tree
  253. entry for that suffix. Then
  254. get cb instances used by the suffix and set associated_be_is_disabled
  255. get cb instances used by sub suffixes of this suffix and
  256. set associated_be_is_disabled
  257. */
  258. void
  259. cb_be_state_change (void *handle, char *be_name, int old_be_state, int new_be_state)
  260. {
  261. const Slapi_DN *tmpsdn;
  262. Slapi_DN *the_be_suffix;
  263. char *cookie = NULL;
  264. Slapi_Backend *chainbe;
  265. Slapi_Backend *the_be = slapi_be_select_by_instance_name(be_name);
  266. /* no backend? */
  267. if (!the_be) {
  268. return;
  269. }
  270. /* ignore chaining backends - associated backends must be local */
  271. if (slapi_be_is_flag_set(the_be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  272. return;
  273. }
  274. /* get the suffix for the local backend */
  275. tmpsdn = slapi_be_getsuffix(the_be, 0);
  276. if (!tmpsdn) {
  277. return;
  278. } else {
  279. the_be_suffix = slapi_sdn_dup(tmpsdn);
  280. }
  281. /* now, iterate through the chaining backends */
  282. for (chainbe = slapi_get_first_backend(&cookie);
  283. chainbe; chainbe = slapi_get_next_backend(cookie)) {
  284. /* only look at chaining backends */
  285. if (slapi_be_is_flag_set(chainbe, SLAPI_BE_FLAG_REMOTE_DATA)) {
  286. /* get the suffix */
  287. const Slapi_DN *tmpcbsuf = slapi_be_getsuffix(chainbe, 0);
  288. if (tmpcbsuf) {
  289. /* make a copy - to be safe */
  290. Slapi_DN *cbsuffix = slapi_sdn_dup(tmpcbsuf);
  291. /* if the suffixes are equal, or the_be_suffix is a suffix
  292. of cbsuffix, apply the flag */
  293. if (!slapi_sdn_compare(cbsuffix, the_be_suffix) ||
  294. slapi_sdn_issuffix(cbsuffix, the_be_suffix)) {
  295. cb_backend_instance *cbinst = cb_get_instance(chainbe);
  296. if (cbinst) {
  297. /* the backend is disabled if the state is not ON */
  298. cbinst->associated_be_is_disabled = (new_be_state != SLAPI_BE_STATE_ON);
  299. slapi_log_error(SLAPI_LOG_PLUGIN, "chainbe", "cb_be_state_change: set the "
  300. "state of chainbe for %s to %d\n",
  301. slapi_sdn_get_dn(cbsuffix), (new_be_state != SLAPI_BE_STATE_ON));
  302. }
  303. }
  304. slapi_sdn_free(&cbsuffix);
  305. }
  306. }
  307. }
  308. /* clean up */
  309. slapi_sdn_free(&the_be_suffix);
  310. slapi_ch_free_string(&cookie);
  311. }