bulk_import.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /*
  13. * Copyright (c) 1995 Regents of the University of Michigan.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms are permitted
  17. * provided that this notice is preserved and that due credit is given
  18. * to the University of Michigan at Ann Arbor. The name of the University
  19. * may not be used to endorse or promote products derived from this
  20. * software without specific prior written permission. This software
  21. * is provided ``as is'' without express or implied warranty.
  22. */
  23. /* The functions in this file allow plugins to perform bulk import of data
  24. comming over ldap connection. Note that the code will not work if
  25. there is now active connection since import state is stored in the
  26. connection extension */
  27. #include "slap.h"
  28. /* forward declarations */
  29. static int process_bulk_import_op (Slapi_PBlock *pb, int state, Slapi_Entry *e);
  30. /* This function initiates bulk import. The pblock must contain
  31. SLAPI_LDIF2DB_GENERATE_UNIQUEID -- currently always set to TIME_BASED
  32. SLAPI_CONNECTION -- connection over which bulk import is coming
  33. SLAPI_BACKEND -- the backend being imported
  34. or
  35. SLAPI_TARGET_SDN that contains root of the imported area.
  36. The function returns LDAP_SUCCESS or LDAP error code
  37. */
  38. int slapi_start_bulk_import (Slapi_PBlock *pb)
  39. {
  40. return (process_bulk_import_op (pb, SLAPI_BI_STATE_START, NULL));
  41. }
  42. /* This function stops bulk import. The pblock must contain
  43. SLAPI_CONNECTION -- connection over which bulk import is coming
  44. SLAPI_BACKEND -- the backend being imported
  45. or
  46. SLAPI_TARGET_SDN that contains root of the imported area.
  47. The function returns LDAP_SUCCESS or LDAP error code
  48. */
  49. int slapi_stop_bulk_import (Slapi_PBlock *pb)
  50. {
  51. return (process_bulk_import_op (pb, SLAPI_BI_STATE_DONE, NULL));
  52. }
  53. /* This function adds an entry to the bulk import. The pblock must contain
  54. SLAPI_CONNECTION -- connection over which bulk import is coming
  55. SLAPI_BACKEND -- optional backend pointer; if missing computed based on entry dn
  56. The function returns LDAP_SUCCESS or LDAP error code
  57. */
  58. int slapi_import_entry (Slapi_PBlock *pb, Slapi_Entry *e)
  59. {
  60. return (process_bulk_import_op (pb, SLAPI_BI_STATE_ADD, e));
  61. }
  62. static int
  63. process_bulk_import_op (Slapi_PBlock *pb, int state, Slapi_Entry *e)
  64. {
  65. int rc;
  66. Slapi_Backend *be = NULL;
  67. Slapi_DN *target_sdn = NULL;
  68. if (pb == NULL)
  69. {
  70. slapi_log_error(SLAPI_LOG_FATAL, NULL, "process_bulk_import_op: NULL pblock\n");
  71. return LDAP_OPERATIONS_ERROR;
  72. }
  73. if (state == SLAPI_BI_STATE_ADD && e == NULL)
  74. {
  75. slapi_log_error(SLAPI_LOG_FATAL, NULL, "process_bulk_import_op: NULL entry\n");
  76. return LDAP_OPERATIONS_ERROR;
  77. }
  78. slapi_pblock_get (pb, SLAPI_BACKEND, &be);
  79. if (be == NULL)
  80. {
  81. /* try to get dn to select backend */
  82. if (e)
  83. {
  84. target_sdn = slapi_entry_get_sdn(e);
  85. }
  86. else
  87. {
  88. slapi_pblock_get (pb, SLAPI_TARGET_SDN, &target_sdn);
  89. if (NULL == target_sdn) {
  90. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  91. "process_bulk_import_op: NULL target sdn\n");
  92. return LDAP_OPERATIONS_ERROR;
  93. }
  94. }
  95. be = slapi_be_select (target_sdn);
  96. if (be)
  97. {
  98. if (state == SLAPI_BI_STATE_START && (!slapi_be_issuffix(be, target_sdn)))
  99. {
  100. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  101. "process_bulk_import_op: wrong backend suffix\n");
  102. return LDAP_OPERATIONS_ERROR;
  103. }
  104. slapi_pblock_set (pb, SLAPI_BACKEND, be);
  105. }
  106. else
  107. {
  108. slapi_log_error(SLAPI_LOG_FATAL, NULL, "process_bulk_import_op: NULL backend\n");
  109. return LDAP_OPERATIONS_ERROR;
  110. }
  111. }
  112. if (be->be_wire_import == NULL)
  113. {
  114. slapi_log_error(SLAPI_LOG_FATAL, NULL, "slapi_start_bulk_import: "
  115. "bulk import is not supported by this (%s) backend\n",
  116. be->be_type);
  117. return LDAP_NOT_SUPPORTED;
  118. }
  119. /* set required parameters */
  120. slapi_pblock_set (pb, SLAPI_BULK_IMPORT_STATE, &state);
  121. if (e)
  122. slapi_pblock_set (pb, SLAPI_BULK_IMPORT_ENTRY, e);
  123. rc = be->be_wire_import (pb);
  124. if (rc != 0)
  125. {
  126. /* The caller will free the entry (e), so we just
  127. * leave it alone here. */
  128. slapi_log_error(SLAPI_LOG_FATAL, NULL, "slapi_start_bulk_import: "
  129. "failed; error = %d\n", rc);
  130. return LDAP_OPERATIONS_ERROR;
  131. }
  132. return LDAP_SUCCESS;
  133. }