bulk_import.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /*
  39. * Copyright (c) 1995 Regents of the University of Michigan.
  40. * All rights reserved.
  41. *
  42. * Redistribution and use in source and binary forms are permitted
  43. * provided that this notice is preserved and that due credit is given
  44. * to the University of Michigan at Ann Arbor. The name of the University
  45. * may not be used to endorse or promote products derived from this
  46. * software without specific prior written permission. This software
  47. * is provided ``as is'' without express or implied warranty.
  48. */
  49. /* The functions in this file allow plugins to perform bulk import of data
  50. comming over ldap connection. Note that the code will not work if
  51. there is now active connection since import state is stored in the
  52. connection extension */
  53. #include "slap.h"
  54. /* forward declarations */
  55. static int process_bulk_import_op (Slapi_PBlock *pb, int state, Slapi_Entry *e);
  56. /* This function initiates bulk import. The pblock must contain
  57. SLAPI_LDIF2DB_GENERATE_UNIQUEID -- currently always set to TIME_BASED
  58. SLAPI_CONNECTION -- connection over which bulk import is coming
  59. SLAPI_BACKEND -- the backend being imported
  60. or
  61. SLAPI_TARGET_DN that contains root of the imported area.
  62. The function returns LDAP_SUCCESS or LDAP error code
  63. */
  64. int slapi_start_bulk_import (Slapi_PBlock *pb)
  65. {
  66. return (process_bulk_import_op (pb, SLAPI_BI_STATE_START, NULL));
  67. }
  68. /* This function stops bulk import. The pblock must contain
  69. SLAPI_CONNECTION -- connection over which bulk import is coming
  70. SLAPI_BACKEND -- the backend being imported
  71. or
  72. SLAPI_TARGET_DN that contains root of the imported area.
  73. The function returns LDAP_SUCCESS or LDAP error code
  74. */
  75. int slapi_stop_bulk_import (Slapi_PBlock *pb)
  76. {
  77. return (process_bulk_import_op (pb, SLAPI_BI_STATE_DONE, NULL));
  78. }
  79. /* This function adds an entry to the bulk import. The pblock must contain
  80. SLAPI_CONNECTION -- connection over which bulk import is coming
  81. SLAPI_BACKEND -- optional backend pointer; if missing computed based on entry dn
  82. The function returns LDAP_SUCCESS or LDAP error code
  83. */
  84. int slapi_import_entry (Slapi_PBlock *pb, Slapi_Entry *e)
  85. {
  86. return (process_bulk_import_op (pb, SLAPI_BI_STATE_ADD, e));
  87. }
  88. static int
  89. process_bulk_import_op (Slapi_PBlock *pb, int state, Slapi_Entry *e)
  90. {
  91. int rc;
  92. Slapi_Backend *be = NULL;
  93. char *dn = NULL;
  94. Slapi_DN sdn;
  95. const Slapi_DN *target_sdn = NULL;
  96. if (pb == NULL)
  97. {
  98. slapi_log_error(SLAPI_LOG_FATAL, NULL, "process_bulk_import_op: NULL pblock\n");
  99. return LDAP_OPERATIONS_ERROR;
  100. }
  101. if (state == SLAPI_BI_STATE_ADD && e == NULL)
  102. {
  103. slapi_log_error(SLAPI_LOG_FATAL, NULL, "process_bulk_import_op: NULL entry\n");
  104. return LDAP_OPERATIONS_ERROR;
  105. }
  106. slapi_pblock_get (pb, SLAPI_BACKEND, &be);
  107. if (be == NULL)
  108. {
  109. /* try to get dn to select backend */
  110. if (e)
  111. {
  112. target_sdn = slapi_entry_get_sdn_const (e);
  113. be = slapi_be_select (target_sdn);
  114. }
  115. else
  116. {
  117. slapi_sdn_init(&sdn);
  118. slapi_pblock_get (pb, SLAPI_TARGET_DN, &dn);
  119. if (dn)
  120. {
  121. slapi_sdn_init_dn_byref(&sdn, dn);
  122. be = slapi_be_select (&sdn);
  123. target_sdn = &sdn;
  124. }
  125. }
  126. if (be)
  127. {
  128. if (state == SLAPI_BI_STATE_START && (!slapi_be_issuffix(be, target_sdn)))
  129. {
  130. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  131. "process_bulk_import_op: wrong backend suffix\n");
  132. return LDAP_OPERATIONS_ERROR;
  133. }
  134. slapi_pblock_set (pb, SLAPI_BACKEND, be);
  135. }
  136. else
  137. {
  138. slapi_log_error(SLAPI_LOG_FATAL, NULL, "process_bulk_import_op: NULL backend\n");
  139. return LDAP_OPERATIONS_ERROR;
  140. }
  141. if (NULL == e)
  142. slapi_sdn_done (&sdn);
  143. }
  144. if (be->be_wire_import == NULL)
  145. {
  146. slapi_log_error(SLAPI_LOG_FATAL, NULL, "slapi_start_bulk_import: "
  147. "bulk import is not supported by this (%s) backend\n",
  148. be->be_type);
  149. return LDAP_NOT_SUPPORTED;
  150. }
  151. /* set required parameters */
  152. slapi_pblock_set (pb, SLAPI_BULK_IMPORT_STATE, &state);
  153. if (e)
  154. slapi_pblock_set (pb, SLAPI_BULK_IMPORT_ENTRY, e);
  155. rc = be->be_wire_import (pb);
  156. if (rc != 0)
  157. {
  158. if (rc != LDAP_BUSY)
  159. slapi_log_error(SLAPI_LOG_FATAL, NULL, "slapi_start_bulk_import: "
  160. "failed; error = %d\n", rc);
  161. return (LDAP_BUSY == rc ? LDAP_BUSY : LDAP_OPERATIONS_ERROR);
  162. }
  163. return LDAP_SUCCESS;
  164. }