cb_modify.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. static void cb_remove_illegal_mods(cb_backend_instance * inst, LDAPMod **mods);
  43. /*
  44. * Perform a modify operation
  45. *
  46. * Returns:
  47. * 0 - success
  48. * <0 - fail
  49. *
  50. */
  51. int
  52. chaining_back_modify ( Slapi_PBlock *pb )
  53. {
  54. Slapi_Backend *be;
  55. cb_backend_instance *cb;
  56. LDAPControl **ctrls, **serverctrls;
  57. int rc,parse_rc,msgid,i;
  58. LDAP *ld=NULL;
  59. char **referrals=NULL;
  60. LDAPMod ** mods;
  61. LDAPMessage * res;
  62. const char *dn = NULL;
  63. Slapi_DN *sdn = NULL;
  64. char *matched_msg, *error_msg;
  65. char *cnxerrbuf=NULL;
  66. time_t endtime = 0;
  67. cb_outgoing_conn *cnx;
  68. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  69. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  70. return -1;
  71. }
  72. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  73. cb = cb_get_instance(be);
  74. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_MODIFY);
  75. /* Check wether the chaining BE is available or not */
  76. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  77. return -1;
  78. }
  79. slapi_pblock_get( pb, SLAPI_MODIFY_TARGET_SDN, &sdn );
  80. dn = slapi_sdn_get_dn(sdn);
  81. if (cb_debug_on()) {
  82. slapi_log_error( SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,"modify: target:<%s>\n",dn);
  83. }
  84. ctrls=serverctrls=NULL;
  85. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  86. slapi_pblock_get( pb, SLAPI_REQCONTROLS, &ctrls );
  87. /* Check acls */
  88. if ( cb->local_acl && !cb->associated_be_is_disabled ) {
  89. char * errbuf=NULL;
  90. Slapi_Entry *te = slapi_entry_alloc();
  91. slapi_entry_set_sdn(te, sdn); /* sdn: copied */
  92. rc = slapi_acl_check_mods( pb, te, mods, &errbuf);
  93. slapi_entry_free(te);
  94. if ( rc != LDAP_SUCCESS ) {
  95. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  96. slapi_ch_free((void **)&errbuf);
  97. return -1;
  98. }
  99. }
  100. /* Grab a connection handle */
  101. rc = cb_get_connection(cb->pool, &ld, &cnx, NULL, &cnxerrbuf);
  102. if (LDAP_SUCCESS != rc) {
  103. static int warned_get_conn = 0;
  104. if (!warned_get_conn) {
  105. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  106. "cb_get_connection failed (%d) %s\n",
  107. rc, ldap_err2string(rc));
  108. warned_get_conn = 1;
  109. }
  110. cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  111. cnxerrbuf, 0, NULL);
  112. slapi_ch_free_string(&cnxerrbuf);
  113. /* ping the farm.
  114. * If the farm is unreachable, we increment the counter */
  115. cb_ping_farm(cb, NULL, 0);
  116. return -1;
  117. }
  118. /* Control management */
  119. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  120. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  121. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  122. /* Don't free mods here: are freed at the do_modify level */
  123. return -1;
  124. }
  125. if ( slapi_op_abandoned( pb )) {
  126. cb_release_op_connection(cb->pool,ld,0);
  127. /* Don't free mods here: are freed at the do_modify level */
  128. if ( NULL != ctrls)
  129. ldap_controls_free(ctrls);
  130. return -1;
  131. }
  132. /* Remove illegal attributes from the mods */
  133. cb_remove_illegal_mods(cb,mods);
  134. /* heart-beat management */
  135. if (cb->max_idle_time>0)
  136. endtime=current_time() + cb->max_idle_time;
  137. /* Send LDAP operation to the remote host */
  138. rc = ldap_modify_ext( ld, dn, mods, ctrls, NULL, &msgid );
  139. if ( NULL != ctrls)
  140. ldap_controls_free(ctrls);
  141. if ( rc != LDAP_SUCCESS ) {
  142. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, ldap_err2string(rc), 0, NULL);
  143. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  144. return -1;
  145. }
  146. while ( 1 ) {
  147. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  148. /* connection handle released */
  149. return -1;
  150. }
  151. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  152. switch ( rc ) {
  153. case -1:
  154. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  155. ldap_err2string(rc), 0, NULL);
  156. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  157. if (res)
  158. ldap_msgfree(res);
  159. return -1;
  160. case 0:
  161. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  162. /* does not respond. give up and return a*/
  163. /* error to the client. */
  164. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  165. ldap_err2string(rc), 0, NULL);*/
  166. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  167. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  168. if (res)
  169. ldap_msgfree(res);
  170. return -1;
  171. }
  172. #ifdef CB_YIELD
  173. DS_Sleep(PR_INTERVAL_NO_WAIT);
  174. #endif
  175. break;
  176. default:
  177. matched_msg=error_msg=NULL;
  178. serverctrls=NULL;
  179. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  180. &error_msg, &referrals, &serverctrls, 1 );
  181. if ( parse_rc != LDAP_SUCCESS ) {
  182. static int warned_parse_rc = 0;
  183. if (!warned_parse_rc) {
  184. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  185. "%s%s%s\n",
  186. matched_msg?matched_msg:"",
  187. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  188. ldap_err2string(parse_rc));
  189. warned_parse_rc = 1;
  190. }
  191. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  192. ENDUSERMSG, 0, NULL );
  193. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  194. slapi_ch_free((void **)&matched_msg);
  195. slapi_ch_free((void **)&error_msg);
  196. if (serverctrls)
  197. ldap_controls_free(serverctrls);
  198. /* jarnou: free referrals */
  199. if (referrals)
  200. charray_free(referrals);
  201. return -1;
  202. }
  203. if ( rc != LDAP_SUCCESS ) {
  204. struct berval ** refs = referrals2berval(referrals);
  205. static int warned_rc = 0;
  206. if (!warned_rc && error_msg) {
  207. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  208. "%s%s%s\n",
  209. matched_msg?matched_msg:"",
  210. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  211. error_msg );
  212. warned_rc = 1;
  213. }
  214. cb_send_ldap_result( pb, rc, matched_msg, ENDUSERMSG, 0, refs);
  215. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  216. slapi_ch_free((void **)&matched_msg);
  217. slapi_ch_free((void **)&error_msg);
  218. if (refs)
  219. ber_bvecfree(refs);
  220. if (referrals)
  221. charray_free(referrals);
  222. if (serverctrls)
  223. ldap_controls_free(serverctrls);
  224. return -1;
  225. }
  226. cb_release_op_connection(cb->pool,ld,0);
  227. /* Add control response sent by the farm server */
  228. for (i=0; serverctrls && serverctrls[i];i++)
  229. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  230. /* SLAPI_ADD_RESCONTROL dups controls */
  231. if (serverctrls)
  232. ldap_controls_free(serverctrls);
  233. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  234. slapi_ch_free((void **)&matched_msg);
  235. slapi_ch_free((void **)&error_msg);
  236. if (referrals)
  237. charray_free(referrals);
  238. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  239. return 0;
  240. }
  241. }
  242. /* Never reached */
  243. /* return 0; */
  244. }
  245. /* Function removes mods which are not allowed over-the-wire */
  246. static void
  247. cb_remove_illegal_mods(cb_backend_instance *inst, LDAPMod **mods)
  248. {
  249. int i, j;
  250. LDAPMod *tmp;
  251. if ( inst->illegal_attributes != NULL ) { /* Unlikely to happen */
  252. slapi_rwlock_wrlock(inst->rwl_config_lock);
  253. for (j=0; inst->illegal_attributes[j]; j++) {
  254. for ( i = 0; mods[i] != NULL; i++ ) {
  255. if (slapi_attr_types_equivalent(inst->illegal_attributes[j],mods[i]->mod_type)) {
  256. tmp = mods[i];
  257. for ( j = i; mods[j] != NULL; j++ ) {
  258. mods[j] = mods[j + 1];
  259. }
  260. slapi_ch_free( (void**)&(tmp->mod_type) );
  261. if ( tmp->mod_bvalues != NULL ) {
  262. ber_bvecfree( tmp->mod_bvalues );
  263. }
  264. slapi_ch_free( (void**)&tmp );
  265. i--;
  266. }
  267. }
  268. }
  269. slapi_rwlock_unlock(inst->rwl_config_lock);
  270. }
  271. }