bulk_import.c 6.8 KB

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