cb_utils.c 12 KB

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