cb_add.c 9.4 KB

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