cb_modify.c 8.1 KB

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