cb_utils.c 12 KB

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