cb_utils.c 10 KB

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