cb_add.c 7.6 KB

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