import-threads.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  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. * the threads that make up an import:
  43. * producer (1)
  44. * foreman (1)
  45. * worker (N: 1 for each index)
  46. *
  47. * a wire import (aka "fast replica" import) won't have a producer thread.
  48. */
  49. #include "back-ldbm.h"
  50. #include "vlv_srch.h"
  51. #include "import.h"
  52. #ifdef XP_WIN32
  53. #define STDIN_FILENO 0
  54. #endif
  55. static void import_wait_for_space_in_fifo(ImportJob *job, size_t new_esize);
  56. static int import_get_and_add_parent_rdns(ImportWorkerInfo *info, ldbm_instance *inst, DB *db, ID id, ID *total_id, Slapi_RDN *srdn, int *curr_entry);
  57. static struct backentry *import_make_backentry(Slapi_Entry *e, ID id)
  58. {
  59. struct backentry *ep = backentry_alloc();
  60. if (NULL != ep) {
  61. ep->ep_entry = e;
  62. ep->ep_id = id;
  63. }
  64. return ep;
  65. }
  66. static void import_decref_entry(struct backentry *ep)
  67. {
  68. PR_AtomicDecrement(&(ep->ep_refcnt));
  69. PR_ASSERT(ep->ep_refcnt >= 0);
  70. }
  71. /* generate uniqueid if requested */
  72. static void import_generate_uniqueid(ImportJob *job, Slapi_Entry *e)
  73. {
  74. const char *uniqueid = slapi_entry_get_uniqueid(e);
  75. int rc;
  76. if (!uniqueid && (job->uuid_gen_type != SLAPI_UNIQUEID_GENERATE_NONE)) {
  77. char *newuniqueid;
  78. /* generate id based on dn */
  79. if (job->uuid_gen_type == SLAPI_UNIQUEID_GENERATE_NAME_BASED) {
  80. char *dn = slapi_entry_get_dn(e);
  81. rc = slapi_uniqueIDGenerateFromNameString(&newuniqueid,
  82. job->uuid_namespace, dn, strlen(dn));
  83. } else {
  84. /* time based */
  85. rc = slapi_uniqueIDGenerateString(&newuniqueid);
  86. }
  87. if (rc == UID_SUCCESS) {
  88. slapi_entry_set_uniqueid (e, newuniqueid);
  89. } else {
  90. char ebuf[BUFSIZ];
  91. LDAPDebug( LDAP_DEBUG_ANY,
  92. "import_generate_uniqueid: failed to generate "
  93. "uniqueid for %s; error=%d.\n",
  94. escape_string(slapi_entry_get_dn_const(e), ebuf), rc, 0 );
  95. }
  96. }
  97. }
  98. /********** BETTER LDIF PARSER **********/
  99. /* like the function in libldif, except this one doesn't need to use
  100. * FILE (which breaks on various platforms for >4G files or large numbers
  101. * of open files)
  102. */
  103. #define LDIF_BUFFER_SIZE 8192
  104. typedef struct {
  105. char *b; /* buffer */
  106. size_t size; /* how full the buffer is */
  107. size_t offset; /* where the current entry starts */
  108. } ldif_context;
  109. static void import_init_ldif(ldif_context *c)
  110. {
  111. c->size = c->offset = 0;
  112. c->b = NULL;
  113. }
  114. static void import_free_ldif(ldif_context *c)
  115. {
  116. if (c->b)
  117. FREE(c->b);
  118. import_init_ldif(c);
  119. }
  120. static char *import_get_entry(ldif_context *c, int fd, int *lineno)
  121. {
  122. int ret;
  123. int done = 0, got_lf = 0;
  124. size_t bufSize = 0, bufOffset = 0, i;
  125. char *buf = NULL;
  126. while (!done) {
  127. /* If there's no data in the buffer, get some */
  128. if ((c->size == 0) || (c->offset == c->size)) {
  129. /* Do we even have a buffer ? */
  130. if (! c->b) {
  131. c->b = slapi_ch_malloc(LDIF_BUFFER_SIZE);
  132. if (! c->b)
  133. return NULL;
  134. }
  135. ret = read(fd, c->b, LDIF_BUFFER_SIZE);
  136. if (ret < 0) {
  137. /* Must be error */
  138. goto error;
  139. } else if (ret == 0) {
  140. /* eof */
  141. if (buf) {
  142. /* last entry */
  143. buf[bufOffset] = 0;
  144. return buf;
  145. }
  146. return NULL;
  147. } else {
  148. /* read completed OK */
  149. c->size = ret;
  150. c->offset = 0;
  151. }
  152. }
  153. /* skip blank lines at start of entry */
  154. if (bufOffset == 0) {
  155. size_t n;
  156. char *p;
  157. for (n = c->offset, p = c->b + n; n < c->size; n++, p++) {
  158. if (!(*p == '\r' || *p == '\n' || *p == ' '|| *p == '\t'))
  159. break;
  160. }
  161. c->offset = n;
  162. if (c->offset == c->size) continue;
  163. }
  164. i = c->offset;
  165. while (!done && (i < c->size)) {
  166. /* scan forward in the buffer, looking for the end of the entry */
  167. while ((i < c->size) && (c->b[i] != '\n'))
  168. i++;
  169. if ((i < c->size) && (c->b[i] == '\n')) {
  170. if (got_lf && ((i == 0) || ((i == 1) && (c->b[0] == '\r')))) {
  171. /* saw an lf at the end of the last buffer */
  172. i++, (*lineno)++;
  173. done = 1;
  174. got_lf = 0;
  175. break;
  176. }
  177. got_lf = 0;
  178. (*lineno)++;
  179. /* is this the end? (need another linefeed) */
  180. if (++i < c->size) {
  181. if (c->b[i] == '\n') {
  182. /* gotcha! */
  183. i++, (*lineno)++;
  184. done = 1;
  185. } else if (c->b[i] == '\r') {
  186. if (++i < c->size) {
  187. if (c->b[i] == '\n') {
  188. /* gotcha! (nt) */
  189. i++, (*lineno)++;
  190. done = 1;
  191. }
  192. } else {
  193. got_lf = 1;
  194. }
  195. }
  196. } else {
  197. /* lf at the very end of the buffer */
  198. got_lf = 1;
  199. }
  200. }
  201. }
  202. /* copy what we did so far into the output buffer */
  203. /* (first, make sure the output buffer is large enough) */
  204. if (bufSize - bufOffset < i - c->offset + 1) {
  205. char *newbuf = NULL;
  206. size_t newsize = (buf ? bufSize*2 : LDIF_BUFFER_SIZE);
  207. newbuf = slapi_ch_malloc(newsize);
  208. if (! newbuf)
  209. goto error;
  210. /* copy over the old data (if there was any) */
  211. if (buf) {
  212. memmove(newbuf, buf, bufOffset);
  213. slapi_ch_free((void **)&buf);
  214. }
  215. buf = newbuf;
  216. bufSize = newsize;
  217. }
  218. memmove(buf + bufOffset, c->b + c->offset, i - c->offset);
  219. bufOffset += (i - c->offset);
  220. c->offset = i;
  221. }
  222. /* add terminating NUL char */
  223. buf[bufOffset] = 0;
  224. return buf;
  225. error:
  226. if (buf)
  227. slapi_ch_free((void **)&buf);
  228. return NULL;
  229. }
  230. /********** THREADS **********/
  231. /*
  232. * Description:
  233. * 1) return the ldif version #
  234. * 2) replace "version: 1" with "#ersion: 1"
  235. * to pretend like a comment for the str2entry
  236. */
  237. static int
  238. import_get_version(char *str)
  239. {
  240. char *s;
  241. char *type;
  242. char *valuecharptr;
  243. char *mystr, *ms;
  244. int offset;
  245. #if defined(USE_OPENLDAP)
  246. ber_len_t valuelen;
  247. #else
  248. int valuelen;
  249. #endif
  250. int my_version = 0;
  251. int retmalloc = 0;
  252. if ((s = strstr(str, "version:")) == NULL)
  253. return 0;
  254. offset = s - str;
  255. mystr = ms = slapi_ch_strdup(str);
  256. while ( (s = ldif_getline( &ms )) != NULL ) {
  257. if ( (retmalloc = ldif_parse_line( s, &type, &valuecharptr, &valuelen )) >= 0 ) {
  258. if (!strcasecmp(type, "version")) {
  259. my_version = atoi(valuecharptr);
  260. *(str + offset) = '#';
  261. /* the memory below was not allocated by the slapi_ch_ functions */
  262. if (retmalloc) slapi_ch_free((void **) &valuecharptr);
  263. break;
  264. }
  265. }
  266. /* the memory below was not allocated by the slapi_ch_ functions */
  267. if (retmalloc) slapi_ch_free((void **) &valuecharptr);
  268. }
  269. slapi_ch_free((void **)&mystr);
  270. return my_version;
  271. }
  272. /*
  273. * add CreatorsName, ModifiersName, CreateTimestamp, ModifyTimestamp to entry
  274. */
  275. static void
  276. import_add_created_attrs(Slapi_Entry *e)
  277. {
  278. char buf[20];
  279. struct berval bv;
  280. struct berval *bvals[2];
  281. time_t curtime;
  282. struct tm ltm;
  283. bvals[0] = &bv;
  284. bvals[1] = NULL;
  285. bv.bv_val = "";
  286. bv.bv_len = 0;
  287. if ( !attrlist_find(e->e_attrs,"creatorsname") ) {
  288. slapi_entry_attr_replace(e, "creatorsname", bvals);
  289. }
  290. if ( !attrlist_find(e->e_attrs,"modifiersname") ) {
  291. slapi_entry_attr_replace(e, "modifiersname", bvals);
  292. }
  293. curtime = current_time();
  294. #ifdef _WIN32
  295. {
  296. struct tm *pt;
  297. pt = gmtime(&curtime);
  298. memcpy(&ltm, pt, sizeof(struct tm));
  299. }
  300. #else
  301. gmtime_r(&curtime, &ltm);
  302. #endif
  303. strftime(buf, sizeof(buf), "%Y%m%d%H%M%SZ", &ltm);
  304. bv.bv_val = buf;
  305. bv.bv_len = strlen(bv.bv_val);
  306. if ( !attrlist_find(e->e_attrs,"createtimestamp") ) {
  307. slapi_entry_attr_replace(e, "createtimestamp", bvals);
  308. }
  309. if ( !attrlist_find(e->e_attrs,"modifytimestamp") ) {
  310. slapi_entry_attr_replace(e, "modifytimestamp", bvals);
  311. }
  312. }
  313. /* producer thread:
  314. * read through the given file list, parsing entries (str2entry), assigning
  315. * them IDs and queueing them on the entry FIFO. other threads will do
  316. * the indexing.
  317. */
  318. void
  319. import_producer(void *param)
  320. {
  321. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  322. ImportJob *job = info->job;
  323. ID id = job->first_ID, id_filestart = id;
  324. Slapi_Entry *e = NULL;
  325. struct backentry *ep = NULL, *old_ep = NULL;
  326. ldbm_instance *inst = job->inst;
  327. backend *be = inst->inst_be;
  328. PRIntervalTime sleeptime;
  329. char *estr = NULL;
  330. int str2entry_flags = 0;
  331. int finished = 0;
  332. int detected_eof = 0;
  333. int fd, curr_file, curr_lineno;
  334. char *curr_filename = NULL;
  335. int idx;
  336. ldif_context c;
  337. int my_version = 0;
  338. size_t newesize = 0;
  339. PR_ASSERT(info != NULL);
  340. PR_ASSERT(inst != NULL);
  341. if ( job->flags & FLAG_ABORT ) {
  342. goto error;
  343. }
  344. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  345. /* pause until we're told to run */
  346. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)) {
  347. info->state = WAITING;
  348. DS_Sleep(sleeptime);
  349. }
  350. info->state = RUNNING;
  351. import_init_ldif(&c);
  352. /* jumpstart by opening the first file */
  353. curr_file = 0;
  354. fd = -1;
  355. detected_eof = finished = 0;
  356. /* we loop around reading the input files and processing each ntry
  357. * as we read it.
  358. */
  359. while (! finished) {
  360. Slapi_Attr *attr = NULL;
  361. int flags = 0;
  362. int prev_lineno = 0;
  363. int lines_in_entry = 0;
  364. int syntax_err = 0;
  365. if (job->flags & FLAG_ABORT) {
  366. goto error;
  367. }
  368. /* move on to next file? */
  369. if (detected_eof) {
  370. /* check if the file can still be read, whine if so... */
  371. if (read(fd, (void *)&idx, 1) > 0) {
  372. import_log_notice(job, "WARNING: Unexpected end of file found "
  373. "at line %d of file \"%s\"", curr_lineno,
  374. curr_filename);
  375. }
  376. if (fd == STDIN_FILENO) {
  377. import_log_notice(job, "Finished scanning file stdin (%lu "
  378. "entries)", (u_long)(id-id_filestart));
  379. } else {
  380. import_log_notice(job, "Finished scanning file \"%s\" (%lu "
  381. "entries)", curr_filename, (u_long)(id-id_filestart));
  382. }
  383. close(fd);
  384. fd = -1;
  385. detected_eof = 0;
  386. id_filestart = id;
  387. curr_file++;
  388. if (job->task) {
  389. job->task->task_progress++;
  390. slapi_task_status_changed(job->task);
  391. }
  392. if (job->input_filenames[curr_file] == NULL) {
  393. /* done! */
  394. finished = 1;
  395. break;
  396. }
  397. }
  398. /* separate from above, because this is also triggered when we
  399. * start (to open the first file)
  400. */
  401. if (fd < 0) {
  402. curr_lineno = 0;
  403. curr_filename = job->input_filenames[curr_file];
  404. if (strcmp(curr_filename, "-") == 0) {
  405. fd = STDIN_FILENO;
  406. } else {
  407. int o_flag = O_RDONLY;
  408. #ifdef XP_WIN32
  409. /* 613041 Somehow the windows low level io lose "\n"
  410. at a very particular situation using O_TEXT mode read.
  411. I think it is a windows bug for O_TEXT mode read.
  412. Use O_BINARY instead, which honestly returns chars
  413. without any translation.
  414. */
  415. o_flag |= O_BINARY;
  416. #endif
  417. fd = dblayer_open_huge_file(curr_filename, o_flag, 0);
  418. }
  419. if (fd < 0) {
  420. import_log_notice(job, "Could not open LDIF file \"%s\", errno %d (%s)",
  421. curr_filename, errno, slapd_system_strerror(errno));
  422. goto error;
  423. }
  424. if (fd == STDIN_FILENO) {
  425. import_log_notice(job, "Processing file stdin");
  426. } else {
  427. import_log_notice(job, "Processing file \"%s\"", curr_filename);
  428. }
  429. }
  430. if (job->flags & FLAG_ABORT) {
  431. goto error;
  432. }
  433. str2entry_flags = SLAPI_STR2ENTRY_TOMBSTONE_CHECK |
  434. SLAPI_STR2ENTRY_REMOVEDUPVALS |
  435. SLAPI_STR2ENTRY_EXPAND_OBJECTCLASSES |
  436. SLAPI_STR2ENTRY_ADDRDNVALS |
  437. SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF;
  438. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)){
  439. info->state = WAITING;
  440. DS_Sleep(sleeptime);
  441. }
  442. info->state = RUNNING;
  443. prev_lineno = curr_lineno;
  444. estr = import_get_entry(&c, fd, &curr_lineno);
  445. lines_in_entry = curr_lineno - prev_lineno;
  446. if (!estr) {
  447. /* error reading entry, or end of file */
  448. detected_eof = 1;
  449. continue;
  450. }
  451. if (0 == my_version && 0 == strncmp(estr, "version:", 8)) {
  452. my_version = import_get_version(estr);
  453. str2entry_flags |= SLAPI_STR2ENTRY_INCLUDE_VERSION_STR;
  454. }
  455. /* If there are more than so many lines in the entry, we tell
  456. * str2entry to optimize for a large entry.
  457. */
  458. if (lines_in_entry > STR2ENTRY_ATTRIBUTE_PRESENCE_CHECK_THRESHOLD) {
  459. flags = str2entry_flags | SLAPI_STR2ENTRY_BIGENTRY;
  460. } else {
  461. flags = str2entry_flags;
  462. }
  463. if (!(str2entry_flags & SLAPI_STR2ENTRY_INCLUDE_VERSION_STR) &&
  464. entryrdn_get_switch()) { /* subtree-rename: on */
  465. char *dn = NULL;
  466. int rc = 0; /* estr should start with "dn: " */
  467. if (strncmp(estr, "dn: ", 4) &&
  468. NULL == strstr(estr, "\ndn: ")) { /* in case comments precedes
  469. the entry */
  470. import_log_notice(job, "WARNING: skipping bad LDIF entry (not "
  471. "starting with \"dn: \") ending line %d of file \"%s\"",
  472. curr_lineno, curr_filename);
  473. FREE(estr);
  474. continue;
  475. }
  476. rc = get_value_from_string((const char *)estr, "dn", &dn);
  477. if (rc) {
  478. import_log_notice(job, "WARNING: skipping bad LDIF entry (dn "
  479. "has no value\n");
  480. FREE(estr);
  481. continue;
  482. }
  483. e = slapi_str2entry_ext(dn, estr, flags);
  484. slapi_ch_free_string(&dn);
  485. } else {
  486. e = slapi_str2entry(estr, flags);
  487. }
  488. FREE(estr);
  489. if (! e) {
  490. if (!(str2entry_flags & SLAPI_STR2ENTRY_INCLUDE_VERSION_STR)) {
  491. import_log_notice(job, "WARNING: skipping bad LDIF entry "
  492. "ending line %d of file \"%s\"", curr_lineno,
  493. curr_filename);
  494. }
  495. continue;
  496. }
  497. if (0 == my_version) {
  498. /* after the first entry version string won't be given */
  499. my_version = -1;
  500. }
  501. if (! import_entry_belongs_here(e, inst->inst_be)) {
  502. /* silently skip */
  503. if (e) {
  504. slapi_entry_free(e);
  505. }
  506. continue;
  507. }
  508. if (slapi_entry_schema_check(NULL, e) != 0) {
  509. char ebuf[BUFSIZ];
  510. import_log_notice(job, "WARNING: skipping entry \"%s\" which "
  511. "violates schema, ending line %d of file "
  512. "\"%s\"", escape_string(slapi_entry_get_dn(e), ebuf),
  513. curr_lineno, curr_filename);
  514. if (e) {
  515. slapi_entry_free(e);
  516. }
  517. job->skipped++;
  518. continue;
  519. }
  520. /* If we are importing pre-encrypted attributes, we need
  521. * to skip syntax checks for the encrypted values. */
  522. if (!(job->encrypt) && inst->attrcrypt_configured) {
  523. Slapi_Attr *attr = NULL;
  524. Slapi_Entry *e_copy = NULL;
  525. /* Scan through the entry to see if any present
  526. * attributes are configured for encryption. */
  527. slapi_entry_first_attr(e, &attr);
  528. while (attr) {
  529. char *type = NULL;
  530. struct attrinfo *ai = NULL;
  531. slapi_attr_get_type(attr, &type);
  532. /* Check if this type is configured for encryption. */
  533. ainfo_get(be, type, &ai);
  534. if (ai->ai_attrcrypt != NULL) {
  535. /* Make a copy of the entry to use for syntax
  536. * checking if a copy has not been made yet. */
  537. if (e_copy == NULL) {
  538. e_copy = slapi_entry_dup(e);
  539. }
  540. /* Delete the enrypted attribute from the copy. */
  541. slapi_entry_attr_delete(e_copy, type);
  542. }
  543. slapi_entry_next_attr(e, attr, &attr);
  544. }
  545. if (e_copy) {
  546. syntax_err = slapi_entry_syntax_check(NULL, e_copy, 0);
  547. slapi_entry_free(e_copy);
  548. } else {
  549. syntax_err = slapi_entry_syntax_check(NULL, e, 0);
  550. }
  551. } else {
  552. syntax_err = slapi_entry_syntax_check(NULL, e, 0);
  553. }
  554. /* Check attribute syntax */
  555. if (syntax_err != 0)
  556. {
  557. char ebuf[BUFSIZ];
  558. import_log_notice(job, "WARNING: skipping entry \"%s\" which "
  559. "violates attribute syntax, ending line %d of "
  560. "file \"%s\"", escape_string(slapi_entry_get_dn(e), ebuf),
  561. curr_lineno, curr_filename);
  562. if (e) {
  563. slapi_entry_free(e);
  564. }
  565. job->skipped++;
  566. continue;
  567. }
  568. /* generate uniqueid if necessary */
  569. import_generate_uniqueid(job, e);
  570. if (g_get_global_lastmod()) {
  571. import_add_created_attrs(e);
  572. }
  573. ep = import_make_backentry(e, id);
  574. if (!ep)
  575. goto error;
  576. /* check for include/exclude subtree lists */
  577. if (! ldbm_back_ok_to_dump(backentry_get_ndn(ep),
  578. job->include_subtrees,
  579. job->exclude_subtrees)) {
  580. backentry_free(&ep);
  581. continue;
  582. }
  583. /* not sure what this does, but it looked like it could be
  584. * simplified. if it's broken, it's my fault. -robey
  585. */
  586. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  587. Slapi_Value **va = attr_get_present_values(attr);
  588. pw_encodevals( (Slapi_Value **)va ); /* jcm - cast away const */
  589. }
  590. if (job->flags & FLAG_ABORT) {
  591. backentry_free(&ep);
  592. goto error;
  593. }
  594. /* Now we have this new entry, all decoded
  595. * Next thing we need to do is:
  596. * (1) see if the appropriate fifo location contains an
  597. * entry which had been processed by the indexers.
  598. * If so, proceed.
  599. * If not, spin waiting for it to become free.
  600. * (2) free the old entry and store the new one there.
  601. * (3) Update the job progress indicators so the indexers
  602. * can use the new entry.
  603. */
  604. idx = id % job->fifo.size;
  605. old_ep = job->fifo.item[idx].entry;
  606. if (old_ep) {
  607. /* for the slot to be recycled, it needs to be already absorbed
  608. * by the foreman (id >= ready_EID), and all the workers need to
  609. * be finished with it (refcount = 0).
  610. */
  611. while (((old_ep->ep_refcnt > 0) ||
  612. (old_ep->ep_id >= job->ready_EID))
  613. && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  614. info->state = WAITING;
  615. DS_Sleep(sleeptime);
  616. }
  617. if (job->flags & FLAG_ABORT){
  618. backentry_free(&ep);
  619. goto error;
  620. }
  621. info->state = RUNNING;
  622. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  623. job->fifo.item[idx].entry = NULL;
  624. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  625. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  626. else
  627. job->fifo.c_bsize = 0;
  628. backentry_free(&old_ep);
  629. }
  630. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  631. if (newesize > job->fifo.bsize) { /* entry too big */
  632. char ebuf[BUFSIZ];
  633. import_log_notice(job, "WARNING: skipping entry \"%s\" "
  634. "ending line %d of file \"%s\"",
  635. escape_string(slapi_entry_get_dn(e), ebuf),
  636. curr_lineno, curr_filename);
  637. import_log_notice(job, "REASON: entry too large (%ld bytes) for "
  638. "the buffer size (%lu bytes)", newesize, job->fifo.bsize);
  639. backentry_free(&ep);
  640. job->skipped++;
  641. continue;
  642. }
  643. /* Now check if fifo has enough space for the new entry */
  644. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  645. import_wait_for_space_in_fifo( job, newesize );
  646. }
  647. /* We have enough space */
  648. job->fifo.item[idx].filename = curr_filename;
  649. job->fifo.item[idx].line = curr_lineno;
  650. job->fifo.item[idx].entry = ep;
  651. job->fifo.item[idx].bad = 0;
  652. job->fifo.item[idx].esize = newesize;
  653. /* Add the entry size to total fifo size */
  654. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  655. /* Update the job to show our progress */
  656. job->lead_ID = id;
  657. if ((id - info->first_ID) <= job->fifo.size) {
  658. job->trailing_ID = info->first_ID;
  659. } else {
  660. job->trailing_ID = id - job->fifo.size;
  661. }
  662. /* Update our progress meter too */
  663. info->last_ID_processed = id;
  664. id++;
  665. if (job->flags & FLAG_ABORT){
  666. goto error;
  667. }
  668. if (info->command == STOP) {
  669. if (fd >= 0)
  670. close(fd);
  671. finished = 1;
  672. }
  673. }
  674. import_free_ldif(&c);
  675. info->state = FINISHED;
  676. return;
  677. error:
  678. info->state = ABORTED;
  679. }
  680. static int
  681. index_set_entry_to_fifo(ImportWorkerInfo *info, Slapi_Entry *e,
  682. ID id, ID *total_id, int curr_entry)
  683. {
  684. int rc = -1;
  685. ImportJob *job = info->job;
  686. int idx;
  687. struct backentry *ep = NULL, *old_ep = NULL;
  688. size_t newesize = 0;
  689. Slapi_Attr *attr = NULL;
  690. PRIntervalTime sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  691. /* generate uniqueid if necessary */
  692. import_generate_uniqueid(job, e);
  693. ep = import_make_backentry(e, id);
  694. if (NULL == ep) {
  695. goto bail;
  696. }
  697. /* not sure what this does, but it looked like it could be
  698. * simplified. if it's broken, it's my fault. -robey
  699. */
  700. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  701. Slapi_Value **va = attr_get_present_values(attr);
  702. pw_encodevals( (Slapi_Value **)va ); /* jcm - cast away const */
  703. }
  704. if (job->flags & FLAG_ABORT) {
  705. goto bail;
  706. }
  707. /* Now we have this new entry, all decoded
  708. * Next thing we need to do is:
  709. * (1) see if the appropriate fifo location contains an
  710. * entry which had been processed by the indexers.
  711. * If so, proceed.
  712. * If not, spin waiting for it to become free.
  713. * (2) free the old entry and store the new one there.
  714. * (3) Update the job progress indicators so the indexers
  715. * can use the new entry.
  716. */
  717. idx = (*total_id) % job->fifo.size;
  718. old_ep = job->fifo.item[idx].entry;
  719. if (old_ep) {
  720. /* for the slot to be recycled, it needs to be already absorbed
  721. * by the foreman ((*total_id) >= ready_EID), and all the workers
  722. * need to be finished with it (refcount = 0).
  723. */
  724. while (((old_ep->ep_refcnt > 0) || (old_ep->ep_id >= job->ready_EID))
  725. && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  726. info->state = WAITING;
  727. DS_Sleep(sleeptime);
  728. }
  729. if (job->flags & FLAG_ABORT) {
  730. goto bail;
  731. }
  732. info->state = RUNNING;
  733. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  734. job->fifo.item[idx].entry = NULL;
  735. if (job->fifo.c_bsize > job->fifo.item[idx].esize) {
  736. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  737. } else {
  738. job->fifo.c_bsize = 0;
  739. }
  740. backentry_free(&old_ep);
  741. }
  742. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  743. if (newesize > job->fifo.bsize) { /* entry too big */
  744. char ebuf[BUFSIZ];
  745. import_log_notice(job, "WARNING: skipping entry \"%s\"",
  746. escape_string(slapi_entry_get_dn(e), ebuf));
  747. import_log_notice(job, "REASON: entry too large (%lu bytes) for "
  748. "the buffer size (%lu bytes)", newesize, job->fifo.bsize);
  749. backentry_free(&ep);
  750. job->skipped++;
  751. rc = 0; /* go to the next loop */
  752. }
  753. /* Now check if fifo has enough space for the new entry */
  754. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  755. import_wait_for_space_in_fifo( job, newesize );
  756. }
  757. /* We have enough space */
  758. job->fifo.item[idx].filename = ID2ENTRY LDBM_FILENAME_SUFFIX;
  759. job->fifo.item[idx].line = curr_entry;
  760. job->fifo.item[idx].entry = ep;
  761. job->fifo.item[idx].bad = 0;
  762. job->fifo.item[idx].esize = newesize;
  763. /* Add the entry size to total fifo size */
  764. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  765. /* Update the job to show our progress */
  766. job->lead_ID = *total_id;
  767. if ((*total_id - info->first_ID) <= job->fifo.size) {
  768. job->trailing_ID = info->first_ID;
  769. } else {
  770. job->trailing_ID = *total_id - job->fifo.size;
  771. }
  772. /* Update our progress meter too */
  773. info->last_ID_processed = *total_id;
  774. (*total_id)++;
  775. rc = 0; /* done */
  776. bail:
  777. return rc;
  778. }
  779. /* producer thread for re-indexing:
  780. * read id2entry, parsing entries (str2entry) (needed???), assigning
  781. * them IDs (again, needed???) and queueing them on the entry FIFO.
  782. * other threads will do the indexing -- same as in import.
  783. */
  784. void
  785. index_producer(void *param)
  786. {
  787. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  788. ImportJob *job = info->job;
  789. ID id = job->first_ID;
  790. Slapi_Entry *e = NULL;
  791. ldbm_instance *inst = job->inst;
  792. PRIntervalTime sleeptime;
  793. int finished = 0;
  794. int rc = 0;
  795. /* vars for Berkeley DB */
  796. DB_ENV *env = NULL;
  797. DB *db = NULL;
  798. DBC *dbc = NULL;
  799. DBT key = {0};
  800. DBT data = {0};
  801. int db_rval = -1;
  802. backend *be = inst->inst_be;
  803. int isfirst = 1;
  804. int curr_entry = 0;
  805. PR_ASSERT(info != NULL);
  806. PR_ASSERT(inst != NULL);
  807. PR_ASSERT(be != NULL);
  808. if ( job->flags & FLAG_ABORT )
  809. goto error;
  810. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  811. /* pause until we're told to run */
  812. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)) {
  813. info->state = WAITING;
  814. DS_Sleep(sleeptime);
  815. }
  816. info->state = RUNNING;
  817. /* open id2entry with dedicated db env and db handler */
  818. if ( dblayer_get_aux_id2entry( be, &db, &env ) != 0 || db == NULL ||
  819. env == NULL) {
  820. LDAPDebug( LDAP_DEBUG_ANY, "Could not open id2entry\n", 0, 0, 0 );
  821. goto error;
  822. }
  823. /* get a cursor to we can walk over the table */
  824. db_rval = db->cursor(db, NULL, &dbc, 0);
  825. if ( 0 != db_rval ) {
  826. LDAPDebug( LDAP_DEBUG_ANY,
  827. "Failed to get cursor for reindexing\n", 0, 0, 0 );
  828. dblayer_release_id2entry(be, db);
  829. goto error;
  830. }
  831. /* we loop around reading the input files and processing each entry
  832. * as we read it.
  833. */
  834. finished = 0;
  835. while (!finished) {
  836. ID temp_id;
  837. if (job->flags & FLAG_ABORT) {
  838. goto error;
  839. }
  840. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)){
  841. info->state = WAITING;
  842. DS_Sleep(sleeptime);
  843. }
  844. info->state = RUNNING;
  845. key.flags = DB_DBT_MALLOC;
  846. data.flags = DB_DBT_MALLOC;
  847. if (isfirst)
  848. {
  849. db_rval = dbc->c_get(dbc, &key, &data, DB_FIRST);
  850. isfirst = 0;
  851. }
  852. else
  853. {
  854. db_rval = dbc->c_get(dbc, &key, &data, DB_NEXT);
  855. }
  856. if (0 != db_rval) {
  857. if (DB_NOTFOUND != db_rval) {
  858. LDAPDebug(LDAP_DEBUG_ANY, "%s: Failed to read database, "
  859. "errno=%d (%s)\n", inst->inst_name, db_rval,
  860. dblayer_strerror(db_rval));
  861. if (job->task) {
  862. slapi_task_log_notice(job->task,
  863. "%s: Failed to read database, err %d (%s)",
  864. inst->inst_name, db_rval,
  865. dblayer_strerror(db_rval));
  866. }
  867. }
  868. break;
  869. }
  870. curr_entry++;
  871. temp_id = id_stored_to_internal((char *)key.data);
  872. /* call post-entry plugin */
  873. plugin_call_entryfetch_plugins((char **) &data.dptr, &data.dsize);
  874. if (entryrdn_get_switch()) {
  875. char *rdn = NULL;
  876. /* rdn is allocated in get_value_from_string */
  877. rc = get_value_from_string((const char *)data.dptr, "rdn", &rdn);
  878. if (rc) {
  879. /* data.dptr may not include rdn: ..., try "dn: ..." */
  880. e = slapi_str2entry( data.dptr, 0 );
  881. if (job->flags & FLAG_DN2RDN) {
  882. int len = 0;
  883. int options = SLAPI_DUMP_STATEINFO | SLAPI_DUMP_UNIQUEID |
  884. SLAPI_DUMP_RDN_ENTRY;
  885. slapi_ch_free(&(data.data));
  886. data.dptr = slapi_entry2str_with_options(e, &len, options);
  887. data.dsize = len + 1;
  888. /* store it */
  889. rc = db->put( db, NULL, &key, &data, 0);
  890. if (rc) {
  891. LDAPDebug2Args( LDAP_DEBUG_TRACE,
  892. "index_producer: converting an entry "
  893. "from dn format to rdn format failed "
  894. "(dn: %s, ID: %d)\n",
  895. slapi_entry_get_dn_const(e), temp_id);
  896. slapi_ch_free(&(data.data));
  897. goto error;
  898. }
  899. }
  900. } else {
  901. char *dn = NULL;
  902. struct backdn *bdn =
  903. dncache_find_id(&inst->inst_dncache, temp_id);
  904. if (bdn) {
  905. /* don't free dn */
  906. dn = (char *)slapi_sdn_get_dn(bdn->dn_sdn);
  907. CACHE_RETURN(&inst->inst_dncache, &bdn);
  908. } else {
  909. Slapi_DN *sdn = NULL;
  910. rc = entryrdn_lookup_dn(be, rdn, temp_id, &dn, NULL);
  911. if (rc) {
  912. /* We cannot use the entryrdn index;
  913. * Compose dn from the entries in id2entry */
  914. Slapi_RDN psrdn = {0};
  915. char *pid_str = NULL;
  916. char *pdn = NULL;
  917. LDAPDebug2Args( LDAP_DEBUG_TRACE,
  918. "index_producer: entryrdn is not available; "
  919. "composing dn (rdn: %s, ID: %d)\n",
  920. rdn, temp_id);
  921. rc = get_value_from_string((const char *)data.dptr,
  922. LDBM_PARENTID_STR, &pid_str);
  923. if (rc) {
  924. rc = 0; /* assume this is a suffix */
  925. } else {
  926. ID pid = (ID)strtol(pid_str, (char **)NULL, 10);
  927. /* if pid is larger than the current pid temp_id,
  928. * the parent entry hasn't */
  929. rc = import_get_and_add_parent_rdns(info, inst, db,
  930. pid, &id, &psrdn, &curr_entry);
  931. if (rc) {
  932. LDAPDebug2Args( LDAP_DEBUG_ANY,
  933. "ldbm2index: Failed to compose dn for "
  934. "(rdn: %s, ID: %d)\n", rdn, temp_id);
  935. slapi_ch_free_string(&rdn);
  936. slapi_rdn_done(&psrdn);
  937. continue;
  938. }
  939. /* Generate DN string from Slapi_RDN */
  940. rc = slapi_rdn_get_dn(&psrdn, &pdn);
  941. slapi_rdn_done(&psrdn);
  942. if (rc) {
  943. LDAPDebug2Args( LDAP_DEBUG_ANY,
  944. "ldbm2index: Failed to compose dn for "
  945. "(rdn: %s, ID: %d) from Slapi_RDN\n",
  946. rdn, temp_id);
  947. slapi_ch_free_string(&rdn);
  948. continue;
  949. }
  950. }
  951. dn = slapi_ch_smprintf("%s%s%s",
  952. rdn, pdn?",":"", pdn?pdn:"");
  953. slapi_ch_free_string(&pdn);
  954. }
  955. /* dn is not dup'ed in slapi_sdn_new_dn_byref.
  956. * It's set to bdn and put in the dn cache. */
  957. sdn = slapi_sdn_new_dn_byref(dn);
  958. bdn = backdn_init(sdn, temp_id, 0);
  959. CACHE_ADD( &inst->inst_dncache, bdn, NULL );
  960. CACHE_RETURN(&inst->inst_dncache, &bdn);
  961. slapi_log_error(SLAPI_LOG_CACHE, "ldbm2index",
  962. "entryrdn_lookup_dn returned: %s, "
  963. "and set to dn cache\n", dn);
  964. }
  965. e = slapi_str2entry_ext( dn, data.dptr, 0 );
  966. slapi_ch_free_string(&rdn);
  967. }
  968. } else {
  969. e = slapi_str2entry(data.data, 0);
  970. if ( NULL == e ) {
  971. if (job->task) {
  972. slapi_task_log_notice(job->task,
  973. "%s: WARNING: skipping badly formatted entry (id %lu)",
  974. inst->inst_name, (u_long)temp_id);
  975. }
  976. LDAPDebug(LDAP_DEBUG_ANY,
  977. "%s: WARNING: skipping badly formatted entry (id %lu)\n",
  978. inst->inst_name, (u_long)temp_id, 0);
  979. continue;
  980. }
  981. }
  982. slapi_ch_free(&(key.data));
  983. slapi_ch_free(&(data.data));
  984. rc = index_set_entry_to_fifo(info, e, temp_id, &id, curr_entry);
  985. if (rc) {
  986. goto error;
  987. }
  988. if (job->flags & FLAG_ABORT) {
  989. goto error;
  990. }
  991. if (info->command == STOP) {
  992. finished = 1;
  993. }
  994. }
  995. dbc->c_close(dbc);
  996. dblayer_release_aux_id2entry( be, db, env );
  997. info->state = FINISHED;
  998. return;
  999. error:
  1000. dbc->c_close(dbc);
  1001. dblayer_release_aux_id2entry( be, db, env );
  1002. info->state = ABORTED;
  1003. }
  1004. static void
  1005. import_wait_for_space_in_fifo(ImportJob *job, size_t new_esize)
  1006. {
  1007. struct backentry *temp_ep = NULL;
  1008. size_t i;
  1009. int slot_found;
  1010. PRIntervalTime sleeptime;
  1011. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  1012. /* Now check if fifo has enough space for the new entry */
  1013. while ((job->fifo.c_bsize + new_esize) > job->fifo.bsize) {
  1014. for ( i = 0, slot_found = 0 ; i < job->fifo.size ; i++ ) {
  1015. temp_ep = job->fifo.item[i].entry;
  1016. if (temp_ep) {
  1017. if (temp_ep->ep_refcnt == 0 && temp_ep->ep_id <= job->ready_EID) {
  1018. job->fifo.item[i].entry = NULL;
  1019. if (job->fifo.c_bsize > job->fifo.item[i].esize)
  1020. job->fifo.c_bsize -= job->fifo.item[i].esize;
  1021. else
  1022. job->fifo.c_bsize = 0;
  1023. backentry_free(&temp_ep);
  1024. slot_found = 1;
  1025. }
  1026. }
  1027. }
  1028. if ( slot_found == 0 )
  1029. DS_Sleep(sleeptime);
  1030. }
  1031. }
  1032. /* helper function for the foreman: */
  1033. static int foreman_do_parentid(ImportJob *job, struct backentry *entry,
  1034. struct attrinfo *parentid_ai)
  1035. {
  1036. backend *be = job->inst->inst_be;
  1037. Slapi_Value **svals = NULL;
  1038. Slapi_Attr *attr = NULL;
  1039. int idl_disposition = 0;
  1040. int ret = 0;
  1041. if (slapi_entry_attr_find(entry->ep_entry, LDBM_PARENTID_STR, &attr) == 0) {
  1042. svals = attr_get_present_values(attr);
  1043. ret = index_addordel_values_ext_sv(be, LDBM_PARENTID_STR, svals, NULL, entry->ep_id,
  1044. BE_INDEX_ADD, NULL, &idl_disposition, NULL);
  1045. if (idl_disposition != IDL_INSERT_NORMAL) {
  1046. char *attr_value = slapi_value_get_berval(svals[0])->bv_val;
  1047. ID parent_id = atol(attr_value);
  1048. if (idl_disposition == IDL_INSERT_NOW_ALLIDS) {
  1049. import_subcount_mother_init(job->mothers, parent_id,
  1050. idl_get_allidslimit(parentid_ai)+1);
  1051. } else if (idl_disposition == IDL_INSERT_ALLIDS) {
  1052. import_subcount_mother_count(job->mothers, parent_id);
  1053. }
  1054. }
  1055. if (ret != 0) {
  1056. import_log_notice(job, "ERROR: Can't update parentid index "
  1057. "(error %d)", ret);
  1058. return ret;
  1059. }
  1060. }
  1061. return 0;
  1062. }
  1063. /* helper function for the foreman: */
  1064. static int
  1065. foreman_do_entrydn(ImportJob *job, FifoItem *fi)
  1066. {
  1067. backend *be = job->inst->inst_be;
  1068. struct berval bv;
  1069. int err = 0, ret = 0;
  1070. IDList *IDL;
  1071. /* insert into the entrydn index */
  1072. bv.bv_val = (void*)backentry_get_ndn(fi->entry); /* jcm - Had to cast away const */
  1073. bv.bv_len = strlen(bv.bv_val);
  1074. /* We need to check here whether the DN is already present in
  1075. * the entrydn index. If it is then the input ldif
  1076. * contained a duplicate entry, which it isn't allowed to */
  1077. /* Due to popular demand, we only warn on this, given the
  1078. * tendency for customers to want to import dirty data */
  1079. /* So, we do an index read first */
  1080. err = 0;
  1081. IDL = index_read(be, LDBM_ENTRYDN_STR, indextype_EQUALITY, &bv, NULL, &err);
  1082. /* Did this work ? */
  1083. if (NULL != IDL) {
  1084. /* IMPOSTER ! Get thee hence... */
  1085. import_log_notice(job, "WARNING: Skipping duplicate entry "
  1086. "\"%s\" found at line %d of file \"%s\"",
  1087. slapi_entry_get_dn(fi->entry->ep_entry),
  1088. fi->line, fi->filename);
  1089. idl_free(IDL);
  1090. /* skip this one */
  1091. fi->bad = 1;
  1092. job->skipped++;
  1093. return -1; /* skip to next entry */
  1094. }
  1095. if ((ret = index_addordel_string(be, LDBM_ENTRYDN_STR,
  1096. bv.bv_val,
  1097. fi->entry->ep_id,
  1098. BE_INDEX_ADD|BE_INDEX_NORMALIZED, NULL)) != 0) {
  1099. import_log_notice(job, "Error writing entrydn index "
  1100. "(error %d: %s)",
  1101. ret, dblayer_strerror(ret));
  1102. return ret;
  1103. }
  1104. return 0;
  1105. }
  1106. /* helper function for the foreman: */
  1107. static int
  1108. foreman_do_entryrdn(ImportJob *job, FifoItem *fi)
  1109. {
  1110. backend *be = job->inst->inst_be;
  1111. int ret = 0;
  1112. if ((ret = entryrdn_index_entry(be, fi->entry, BE_INDEX_ADD, NULL)) != 0) {
  1113. import_log_notice(job, "Error writing entryrdn index "
  1114. "(error %d: %s)",
  1115. ret, dblayer_strerror(ret));
  1116. return ret;
  1117. }
  1118. return 0;
  1119. }
  1120. /* foreman thread:
  1121. * i go through the FIFO just like the other worker threads, but i'm
  1122. * responsible for the interrelated indexes: entrydn, entryrdn, id2entry,
  1123. * and the operational attributes (plus the parentid index).
  1124. */
  1125. void
  1126. import_foreman(void *param)
  1127. {
  1128. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  1129. ImportJob *job = info->job;
  1130. ldbm_instance *inst = job->inst;
  1131. backend *be = inst->inst_be;
  1132. PRIntervalTime sleeptime;
  1133. int finished = 0;
  1134. ID id = info->first_ID;
  1135. int ret = 0;
  1136. struct attrinfo *parentid_ai;
  1137. Slapi_PBlock *pb = slapi_pblock_new();
  1138. PR_ASSERT(info != NULL);
  1139. PR_ASSERT(inst != NULL);
  1140. if (job->flags & FLAG_ABORT) {
  1141. goto error;
  1142. }
  1143. /* the pblock is used only by add_op_attrs */
  1144. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  1145. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  1146. info->state = RUNNING;
  1147. ainfo_get(be, LDBM_PARENTID_STR, &parentid_ai);
  1148. while (! finished) {
  1149. FifoItem *fi = NULL;
  1150. int parent_status = 0;
  1151. if (job->flags & FLAG_ABORT) {
  1152. goto error;
  1153. }
  1154. while ( ((info->command == PAUSE) || (id > job->lead_ID)) &&
  1155. (info->command != STOP) && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  1156. /* Check to see if we've been told to stop */
  1157. info->state = WAITING;
  1158. DS_Sleep(sleeptime);
  1159. }
  1160. if (info->command == STOP) {
  1161. finished = 1;
  1162. continue;
  1163. }
  1164. if (job->flags & FLAG_ABORT) {
  1165. goto error;
  1166. }
  1167. info->state = RUNNING;
  1168. /* Read that entry from the cache */
  1169. fi = import_fifo_fetch(job, id, 0);
  1170. if (NULL == fi) {
  1171. import_log_notice(job, "WARNING: entry id %d is missing", id);
  1172. continue;
  1173. }
  1174. if (NULL == fi->entry) {
  1175. import_log_notice(job, "WARNING: entry for id %d is missing", id);
  1176. continue;
  1177. }
  1178. /* first, fill in any operational attributes */
  1179. /* add_op_attrs wants a pblock for some reason. */
  1180. if (add_op_attrs(pb, inst->inst_li, fi->entry, &parent_status) != 0) {
  1181. import_log_notice(job, "ERROR: Could not add op attrs to "
  1182. "entry ending at line %d of file \"%s\"",
  1183. fi->line, fi->filename);
  1184. goto error;
  1185. }
  1186. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  1187. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  1188. /*
  1189. * Only check for a parent and add to the entry2dn index if
  1190. * the entry is not a tombstone.
  1191. */
  1192. if (job->flags & FLAG_ABORT) {
  1193. goto error;
  1194. }
  1195. if (parent_status == IMPORT_ADD_OP_ATTRS_NO_PARENT) {
  1196. /* If this entry is a suffix entry, this is not a problem */
  1197. /* However, if it is not, this is an error---it means that
  1198. * someone tried to import an entry before importing its parent
  1199. * we reject the entry but carry on since we've not stored
  1200. * anything related to this entry.
  1201. */
  1202. if (! slapi_be_issuffix(inst->inst_be, backentry_get_sdn(fi->entry))) {
  1203. import_log_notice(job, "WARNING: Skipping entry \"%s\" "
  1204. "which has no parent, ending at line %d "
  1205. "of file \"%s\"",
  1206. slapi_entry_get_dn(fi->entry->ep_entry),
  1207. fi->line, fi->filename);
  1208. /* skip this one */
  1209. fi->bad = 1;
  1210. job->skipped++;
  1211. goto cont; /* below */
  1212. }
  1213. }
  1214. if (job->flags & FLAG_ABORT) {
  1215. goto error;
  1216. }
  1217. if (entryrdn_get_switch()) { /* subtree-rename: on */
  1218. /* insert into the entryrdn index */
  1219. ret = foreman_do_entryrdn(job, fi);
  1220. } else {
  1221. /* insert into the entrydn index */
  1222. ret = foreman_do_entrydn(job, fi);
  1223. if (ret == -1)
  1224. goto cont; /* skip entry */
  1225. }
  1226. if (ret != 0) {
  1227. goto error;
  1228. }
  1229. }
  1230. if (job->flags & FLAG_ABORT) {
  1231. goto error;
  1232. }
  1233. if (!(job->flags & FLAG_REINDEXING))/* reindex reads data from id2entry */
  1234. {
  1235. /* insert into the id2entry index
  1236. * (that isn't really an index -- it's the storehouse of the entries
  1237. * themselves.)
  1238. */
  1239. ret = id2entry_add_ext(be, fi->entry, NULL, job->encrypt);
  1240. if (ret) {
  1241. /* DB_RUNRECOVERY usually occurs if disk fills */
  1242. if (LDBM_OS_ERR_IS_DISKFULL(ret)) {
  1243. import_log_notice(job, "ERROR: OUT OF SPACE ON DISK or FILE TOO LARGE -- "
  1244. "Could not store the entry ending at line "
  1245. "%d of file \"%s\"",
  1246. fi->line, fi->filename);
  1247. } else if (ret == DB_RUNRECOVERY) {
  1248. import_log_notice(job, "FATAL ERROR: (LARGEFILE SUPPORT NOT ENABLED? OUT OF SPACE ON DISK?) -- "
  1249. "Could not store the entry ending at line "
  1250. "%d of file \"%s\"",
  1251. fi->line, fi->filename);
  1252. } else {
  1253. import_log_notice(job, "ERROR: Could not store the entry "
  1254. "ending at line %d of file \"%s\" -- "
  1255. "error %d", fi->line, fi->filename, ret);
  1256. }
  1257. goto error;
  1258. }
  1259. }
  1260. if (job->flags & FLAG_ABORT) {
  1261. goto error;
  1262. }
  1263. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  1264. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  1265. /* parentid index
  1266. * (we have to do this here, because the parentID is dependent on
  1267. * looking up by entrydn/entryrdn.)
  1268. * Only add to the parent index if the entry is not a tombstone.
  1269. */
  1270. ret = foreman_do_parentid(job, fi->entry, parentid_ai);
  1271. if (ret != 0)
  1272. goto error;
  1273. /* Lastly, before we're finished with the entry, pass it to the
  1274. vlv code to see whether it's within the scope a VLV index. */
  1275. vlv_grok_new_import_entry(fi->entry, be);
  1276. }
  1277. if (job->flags & FLAG_ABORT) {
  1278. goto error;
  1279. }
  1280. /* Remove the entry from the cache (Put in the cache in id2entry_add) */
  1281. if (!(job->flags & FLAG_REINDEXING)) {
  1282. /* reindex reads data from id2entry */
  1283. CACHE_REMOVE(&inst->inst_cache, fi->entry);
  1284. }
  1285. fi->entry->ep_refcnt = job->number_indexers;
  1286. cont:
  1287. if (job->flags & FLAG_ABORT) {
  1288. goto error;
  1289. }
  1290. job->ready_ID = id;
  1291. job->ready_EID = fi->entry->ep_id;
  1292. info->last_ID_processed = id;
  1293. id++;
  1294. if (job->flags & FLAG_ABORT){
  1295. goto error;
  1296. }
  1297. }
  1298. slapi_pblock_destroy(pb);
  1299. info->state = FINISHED;
  1300. return;
  1301. error:
  1302. slapi_pblock_destroy(pb);
  1303. info->state = ABORTED;
  1304. }
  1305. /* worker thread:
  1306. * given an attribute, this worker plows through the entry FIFO, building
  1307. * up the attribute index.
  1308. */
  1309. void
  1310. import_worker(void *param)
  1311. {
  1312. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  1313. ImportJob *job = info->job;
  1314. ldbm_instance *inst = job->inst;
  1315. backend *be = inst->inst_be;
  1316. PRIntervalTime sleeptime;
  1317. int finished = 0;
  1318. ID id = info->first_ID;
  1319. int ret = 0;
  1320. int idl_disposition = 0;
  1321. struct vlvIndex* vlv_index = NULL;
  1322. void *substring_key_buffer = NULL;
  1323. FifoItem *fi = NULL;
  1324. int is_objectclass_attribute;
  1325. int is_nsuniqueid_attribute;
  1326. int is_nscpentrydn_attribute;
  1327. void *attrlist_cursor;
  1328. PR_ASSERT(NULL != info);
  1329. PR_ASSERT(NULL != inst);
  1330. if (job->flags & FLAG_ABORT) {
  1331. goto error;
  1332. }
  1333. if (INDEX_VLV == info->index_info->ai->ai_indexmask) {
  1334. vlv_index = vlv_find_indexname(info->index_info->name, be);
  1335. if (NULL == vlv_index) {
  1336. goto error;
  1337. }
  1338. }
  1339. /*
  1340. * If the entry is a Tombstone, then we only add it to the nsuniqeid index,
  1341. * the nscpEntryDN index, and the idlist for (objectclass=tombstone). These
  1342. * flags are just handy for working out what to do in this case.
  1343. */
  1344. is_objectclass_attribute =
  1345. (strcasecmp(info->index_info->name, "objectclass") == 0);
  1346. is_nsuniqueid_attribute =
  1347. (strcasecmp(info->index_info->name, SLAPI_ATTR_UNIQUEID) == 0);
  1348. is_nscpentrydn_attribute =
  1349. (strcasecmp(info->index_info->name, SLAPI_ATTR_NSCP_ENTRYDN) == 0);
  1350. if (1 != idl_get_idl_new()) {
  1351. /* Is there substring indexing going on here ? */
  1352. if ( (INDEX_SUB & info->index_info->ai->ai_indexmask) &&
  1353. (info->index_buffer_size > 0) ) {
  1354. /* Then make a key buffer thing */
  1355. ret = index_buffer_init(info->index_buffer_size, 0,
  1356. &substring_key_buffer);
  1357. if (0 != ret) {
  1358. import_log_notice(job, "IMPORT FAIL 1 (error %d)", ret);
  1359. }
  1360. }
  1361. }
  1362. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  1363. info->state = RUNNING;
  1364. info->last_ID_processed = id-1;
  1365. while (! finished) {
  1366. struct backentry *ep = NULL;
  1367. Slapi_Value **svals = NULL;
  1368. Slapi_Attr *attr = NULL;
  1369. if (job->flags & FLAG_ABORT) {
  1370. goto error;
  1371. }
  1372. /* entry can be NULL if it turned out to be bogus */
  1373. while (!finished && !ep) {
  1374. /* This worker thread must wait if the command flag is "PAUSE" or
  1375. * the entry corresponds to the current entry treated by the foreman
  1376. * thread, and the state is neither STOP nor ABORT
  1377. */
  1378. while (((info->command == PAUSE) || (id > job->ready_ID)) &&
  1379. (info->command != STOP) && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  1380. /* Check to see if we've been told to stop */
  1381. info->state = WAITING;
  1382. DS_Sleep(sleeptime);
  1383. }
  1384. if (info->command == STOP) {
  1385. finished = 1;
  1386. continue;
  1387. }
  1388. if (job->flags & FLAG_ABORT) {
  1389. goto error;
  1390. }
  1391. info->state = RUNNING;
  1392. /* Read that entry from the cache */
  1393. fi = import_fifo_fetch(job, id, 1);
  1394. ep = fi ? fi->entry : NULL;
  1395. if (!ep) {
  1396. /* skipping an entry that turned out to be bad */
  1397. info->last_ID_processed = id;
  1398. id++;
  1399. }
  1400. }
  1401. if (finished)
  1402. continue;
  1403. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  1404. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  1405. /* This is not a tombstone entry. */
  1406. /* Is this a VLV index ? */
  1407. if (job->flags & FLAG_ABORT) {
  1408. goto error;
  1409. }
  1410. if (INDEX_VLV == info->index_info->ai->ai_indexmask) {
  1411. /* Yes, call VLV code -- needs pblock to find backend */
  1412. Slapi_PBlock *pb = slapi_pblock_new();
  1413. PR_ASSERT(NULL != vlv_index);
  1414. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  1415. vlv_update_index(vlv_index, NULL, inst->inst_li, pb, NULL, ep);
  1416. slapi_pblock_destroy(pb);
  1417. } else {
  1418. /* No, process regular index */
  1419. /* Look for the attribute we're indexing and its subtypes */
  1420. /* For each attr write to the index */
  1421. attrlist_cursor = NULL;
  1422. while ((attr = attrlist_find_ex(ep->ep_entry->e_attrs,
  1423. info->index_info->name,
  1424. NULL,
  1425. NULL,
  1426. &attrlist_cursor)) != NULL) {
  1427. if (job->flags & FLAG_ABORT) {
  1428. goto error;
  1429. }
  1430. if(valueset_isempty(&(attr->a_present_values))) continue;
  1431. svals = attr_get_present_values(attr);
  1432. ret = index_addordel_values_ext_sv(be, info->index_info->name,
  1433. svals, NULL, ep->ep_id, BE_INDEX_ADD | (job->encrypt ? 0 : BE_INDEX_DONT_ENCRYPT), NULL, &idl_disposition,
  1434. substring_key_buffer);
  1435. if (0 != ret) {
  1436. /* Something went wrong, eg disk filled up */
  1437. goto error;
  1438. }
  1439. }
  1440. }
  1441. } else {
  1442. /* This is a Tombstone entry... we only add it to the nsuniqueid
  1443. * index, the nscpEntryDN index, and the idlist for (objectclass=nstombstone).
  1444. */
  1445. if (job->flags & FLAG_ABORT) {
  1446. goto error;
  1447. }
  1448. if (is_nsuniqueid_attribute) {
  1449. ret = index_addordel_string(be, SLAPI_ATTR_UNIQUEID,
  1450. slapi_entry_get_uniqueid(ep->ep_entry), ep->ep_id,
  1451. BE_INDEX_ADD, NULL);
  1452. if (0 != ret) {
  1453. /* Something went wrong, eg disk filled up */
  1454. goto error;
  1455. }
  1456. }
  1457. if (is_objectclass_attribute) {
  1458. ret = index_addordel_string(be, SLAPI_ATTR_OBJECTCLASS,
  1459. SLAPI_ATTR_VALUE_TOMBSTONE, ep->ep_id, BE_INDEX_ADD, NULL);
  1460. if (0 != ret) {
  1461. /* Something went wrong, eg disk filled up */
  1462. goto error;
  1463. }
  1464. }
  1465. if (is_nscpentrydn_attribute) {
  1466. attrlist_cursor = NULL;
  1467. while ((attr = attrlist_find_ex(ep->ep_entry->e_attrs,
  1468. SLAPI_ATTR_NSCP_ENTRYDN,
  1469. NULL,
  1470. NULL,
  1471. &attrlist_cursor)) != NULL) {
  1472. if (job->flags & FLAG_ABORT) {
  1473. goto error;
  1474. }
  1475. if(valueset_isempty(&(attr->a_present_values))) continue;
  1476. svals = attr_get_present_values(attr);
  1477. ret = index_addordel_values_ext_sv(be, info->index_info->name,
  1478. svals, NULL, ep->ep_id, BE_INDEX_ADD | (job->encrypt ? 0 : BE_INDEX_DONT_ENCRYPT), NULL, &idl_disposition,
  1479. substring_key_buffer);
  1480. if (0 != ret) {
  1481. /* Something went wrong, eg disk filled up */
  1482. goto error;
  1483. }
  1484. }
  1485. }
  1486. }
  1487. import_decref_entry(ep);
  1488. info->last_ID_processed = id;
  1489. id++;
  1490. if (job->flags & FLAG_ABORT) {
  1491. goto error;
  1492. }
  1493. }
  1494. if (job->flags & FLAG_ABORT) {
  1495. goto error;
  1496. }
  1497. /* If we were buffering index keys, now flush them */
  1498. if (substring_key_buffer) {
  1499. ret = index_buffer_flush(substring_key_buffer,
  1500. inst->inst_be, NULL,
  1501. info->index_info->ai);
  1502. if (0 != ret) {
  1503. goto error;
  1504. }
  1505. index_buffer_terminate(substring_key_buffer);
  1506. }
  1507. info->state = FINISHED;
  1508. return;
  1509. error:
  1510. if (ret == DB_RUNRECOVERY) {
  1511. LDAPDebug(LDAP_DEBUG_ANY,"cannot import; database recovery needed\n",
  1512. 0,0,0);
  1513. } else if (ret == DB_LOCK_DEADLOCK) {
  1514. /* can this occur? */
  1515. }
  1516. info->state = ABORTED;
  1517. }
  1518. /*
  1519. * import entries to a backend, over the wire -- entries will arrive
  1520. * asynchronously, so this method has no "producer" thread. instead, the
  1521. * front-end drops new entries in as they arrive.
  1522. *
  1523. * this is sometimes called "fast replica initialization".
  1524. *
  1525. * some of this code is duplicated from ldif2ldbm, but i don't think we
  1526. * can avoid it.
  1527. */
  1528. static int bulk_import_start(Slapi_PBlock *pb)
  1529. {
  1530. struct ldbminfo *li = NULL;
  1531. ImportJob *job = NULL;
  1532. backend *be = NULL;
  1533. PRThread *thread = NULL;
  1534. int ret = 0;
  1535. job = CALLOC(ImportJob);
  1536. if (job == NULL) {
  1537. LDAPDebug(LDAP_DEBUG_ANY, "not enough memory to do import job\n",
  1538. 0, 0, 0);
  1539. return -1;
  1540. }
  1541. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  1542. PR_ASSERT(be != NULL);
  1543. li = (struct ldbminfo *)(be->be_database->plg_private);
  1544. job->inst = (ldbm_instance *)be->be_instance_info;
  1545. /* check if an import/restore is already ongoing... */
  1546. PR_Lock(job->inst->inst_config_mutex);
  1547. if (job->inst->inst_flags & INST_FLAG_BUSY) {
  1548. PR_Unlock(job->inst->inst_config_mutex);
  1549. LDAPDebug(LDAP_DEBUG_ANY, "ldbm: '%s' is already in the middle of "
  1550. "another task and cannot be disturbed.\n",
  1551. job->inst->inst_name, 0, 0);
  1552. FREE(job);
  1553. return SLAPI_BI_ERR_BUSY;
  1554. }
  1555. job->inst->inst_flags |= INST_FLAG_BUSY;
  1556. PR_Unlock(job->inst->inst_config_mutex);
  1557. /* take backend offline */
  1558. slapi_mtn_be_disable(be);
  1559. /* get uniqueid info */
  1560. slapi_pblock_get(pb, SLAPI_LDIF2DB_GENERATE_UNIQUEID, &job->uuid_gen_type);
  1561. if (job->uuid_gen_type == SLAPI_UNIQUEID_GENERATE_NAME_BASED) {
  1562. char *namespaceid;
  1563. slapi_pblock_get(pb, SLAPI_LDIF2DB_NAMESPACEID, &namespaceid);
  1564. job->uuid_namespace = slapi_ch_strdup(namespaceid);
  1565. }
  1566. job->flags = 0; /* don't use files */
  1567. job->flags |= FLAG_INDEX_ATTRS;
  1568. job->flags |= FLAG_ONLINE;
  1569. job->starting_ID = 1;
  1570. job->first_ID = 1;
  1571. job->mothers = CALLOC(import_subcount_stuff);
  1572. /* how much space should we allocate to index buffering? */
  1573. job->job_index_buffer_size = import_get_index_buffer_size();
  1574. if (job->job_index_buffer_size == 0) {
  1575. /* 10% of the allocated cache size + one meg */
  1576. job->job_index_buffer_size = (job->inst->inst_li->li_dbcachesize/10) +
  1577. (1024*1024);
  1578. }
  1579. import_subcount_stuff_init(job->mothers);
  1580. job->wire_lock = PR_NewLock();
  1581. job->wire_cv = PR_NewCondVar(job->wire_lock);
  1582. /* COPIED from ldif2ldbm.c : */
  1583. /* shutdown this instance of the db */
  1584. cache_clear(&job->inst->inst_cache, CACHE_TYPE_ENTRY);
  1585. if (entryrdn_get_switch()) {
  1586. cache_clear(&job->inst->inst_dncache, CACHE_TYPE_DN);
  1587. }
  1588. dblayer_instance_close(be);
  1589. /* Delete old database files */
  1590. dblayer_delete_instance_dir(be);
  1591. /* it's okay to fail -- it might already be gone */
  1592. /* dblayer_instance_start will init the id2entry index. */
  1593. /* it also (finally) fills in inst_dir_name */
  1594. ret = dblayer_instance_start(be, DBLAYER_IMPORT_MODE);
  1595. if (ret != 0)
  1596. goto fail;
  1597. /* END OF COPIED SECTION */
  1598. PR_Lock(job->wire_lock);
  1599. vlv_init(job->inst);
  1600. /* create thread for import_main, so we can return */
  1601. thread = PR_CreateThread(PR_USER_THREAD, import_main, (void *)job,
  1602. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1603. PR_JOINABLE_THREAD,
  1604. SLAPD_DEFAULT_THREAD_STACKSIZE);
  1605. if (thread == NULL) {
  1606. PRErrorCode prerr = PR_GetError();
  1607. LDAPDebug(LDAP_DEBUG_ANY, "unable to spawn import thread, "
  1608. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1609. prerr, slapd_pr_strerror(prerr), 0);
  1610. PR_Unlock(job->wire_lock);
  1611. ret = -2;
  1612. goto fail;
  1613. }
  1614. job->main_thread = thread;
  1615. slapi_set_object_extension(li->li_bulk_import_object, pb->pb_conn,
  1616. li->li_bulk_import_handle, job);
  1617. /* wait for the import_main to signal that it's ready for entries */
  1618. /* (don't want to send the success code back to the LDAP client until
  1619. * we're ready for the adds to start rolling in)
  1620. */
  1621. PR_WaitCondVar(job->wire_cv, PR_INTERVAL_NO_TIMEOUT);
  1622. PR_Unlock(job->wire_lock);
  1623. return 0;
  1624. fail:
  1625. PR_Lock(job->inst->inst_config_mutex);
  1626. job->inst->inst_flags &= ~INST_FLAG_BUSY;
  1627. PR_Unlock(job->inst->inst_config_mutex);
  1628. import_free_job(job);
  1629. FREE(job);
  1630. return ret;
  1631. }
  1632. /* returns 0 on success, or < 0 on error
  1633. *
  1634. * on error, the import process is aborted -- so if this returns an error,
  1635. * don't try to queue any more entries or you'll be sorry. The caller
  1636. * is also responsible for free'ing the passed in entry on error. The
  1637. * entry will be consumed on success.
  1638. */
  1639. static int bulk_import_queue(ImportJob *job, Slapi_Entry *entry)
  1640. {
  1641. struct backentry *ep = NULL, *old_ep = NULL;
  1642. int idx;
  1643. ID id = 0;
  1644. Slapi_Attr *attr = NULL;
  1645. size_t newesize = 0;
  1646. PR_Lock(job->wire_lock);
  1647. /* Let's do this inside the lock !*/
  1648. id = job->lead_ID + 1;
  1649. /* generate uniqueid if necessary */
  1650. import_generate_uniqueid(job, entry);
  1651. /* make into backentry */
  1652. ep = import_make_backentry(entry, id);
  1653. if (!ep) {
  1654. import_abort_all(job, 1);
  1655. PR_Unlock(job->wire_lock);
  1656. return -1;
  1657. }
  1658. /* encode the password */
  1659. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  1660. Slapi_Value **va = attr_get_present_values(attr);
  1661. pw_encodevals( (Slapi_Value **)va ); /* jcm - had to cast away const */
  1662. }
  1663. /* Now we have this new entry, all decoded
  1664. * Next thing we need to do is:
  1665. * (1) see if the appropriate fifo location contains an
  1666. * entry which had been processed by the indexers.
  1667. * If so, proceed.
  1668. * If not, spin waiting for it to become free.
  1669. * (2) free the old entry and store the new one there.
  1670. * (3) Update the job progress indicators so the indexers
  1671. * can use the new entry.
  1672. */
  1673. idx = id % job->fifo.size;
  1674. old_ep = job->fifo.item[idx].entry;
  1675. if (old_ep) {
  1676. while ((old_ep->ep_refcnt > 0) && !(job->flags & FLAG_ABORT))
  1677. {
  1678. DS_Sleep(PR_MillisecondsToInterval(import_sleep_time));
  1679. }
  1680. /* the producer could be running thru the fifo while
  1681. * everyone else is cycling to a new pass...
  1682. * double-check that this entry is < ready_EID
  1683. */
  1684. while ((old_ep->ep_id >= job->ready_EID) && !(job->flags & FLAG_ABORT))
  1685. {
  1686. DS_Sleep(PR_MillisecondsToInterval(import_sleep_time));
  1687. }
  1688. if (job->flags & FLAG_ABORT) {
  1689. backentry_clear_entry(ep); /* entry is released in the frontend on failure*/
  1690. backentry_free( &ep ); /* release the backend wrapper, here */
  1691. PR_Unlock(job->wire_lock);
  1692. return -2;
  1693. }
  1694. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  1695. job->fifo.item[idx].entry = NULL;
  1696. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  1697. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  1698. else
  1699. job->fifo.c_bsize = 0;
  1700. backentry_free(&old_ep);
  1701. }
  1702. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  1703. if (newesize > job->fifo.bsize) { /* entry too big */
  1704. char ebuf[BUFSIZ];
  1705. import_log_notice(job, "WARNING: skipping entry \"%s\"",
  1706. escape_string(slapi_entry_get_dn(ep->ep_entry), ebuf));
  1707. import_log_notice(job, "REASON: entry too large (%lu bytes) for "
  1708. "the import buffer size (%lu bytes). Try increasing nsslapd-cachememsize.", newesize, job->fifo.bsize);
  1709. backentry_clear_entry(ep); /* entry is released in the frontend on failure*/
  1710. backentry_free( &ep ); /* release the backend wrapper, here */
  1711. PR_Unlock(job->wire_lock);
  1712. return -1;
  1713. }
  1714. /* Now check if fifo has enough space for the new entry */
  1715. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  1716. import_wait_for_space_in_fifo( job, newesize );
  1717. }
  1718. /* We have enough space */
  1719. job->fifo.item[idx].filename = "(bulk import)";
  1720. job->fifo.item[idx].line = 0;
  1721. job->fifo.item[idx].entry = ep;
  1722. job->fifo.item[idx].bad = 0;
  1723. job->fifo.item[idx].esize = newesize;
  1724. /* Add the entry size to total fifo size */
  1725. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  1726. /* Update the job to show our progress */
  1727. job->lead_ID = id;
  1728. if ((id - job->starting_ID) <= job->fifo.size) {
  1729. job->trailing_ID = job->starting_ID;
  1730. } else {
  1731. job->trailing_ID = id - job->fifo.size;
  1732. }
  1733. PR_Unlock(job->wire_lock);
  1734. return 0;
  1735. }
  1736. void *factory_constructor(void *object, void *parent)
  1737. {
  1738. return NULL;
  1739. }
  1740. void factory_destructor(void *extension, void *object, void *parent)
  1741. {
  1742. ImportJob *job = (ImportJob *)extension;
  1743. PRThread *thread;
  1744. if (extension == NULL)
  1745. return;
  1746. /* connection was destroyed while we were still storing the extension --
  1747. * this is bad news and means we have a bulk import that needs to be
  1748. * aborted!
  1749. */
  1750. thread = job->main_thread;
  1751. LDAPDebug(LDAP_DEBUG_ANY, "ERROR bulk import abandoned\n",
  1752. 0, 0, 0);
  1753. import_abort_all(job, 1);
  1754. /* wait for import_main to finish... */
  1755. PR_JoinThread(thread);
  1756. /* extension object is free'd by import_main */
  1757. return;
  1758. }
  1759. /* plugin entry function for replica init
  1760. *
  1761. * For the SLAPI_BI_STATE_ADD state:
  1762. * On success (rc=0), the entry in pb->pb_import_entry will be
  1763. * consumed. For any other return value, the caller is
  1764. * responsible for freeing the entry in the pb.
  1765. */
  1766. int ldbm_back_wire_import(Slapi_PBlock *pb)
  1767. {
  1768. struct ldbminfo *li;
  1769. backend *be = NULL;
  1770. ImportJob *job = NULL;
  1771. PRThread *thread;
  1772. int state;
  1773. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  1774. PR_ASSERT(be != NULL);
  1775. li = (struct ldbminfo *)(be->be_database->plg_private);
  1776. slapi_pblock_get(pb, SLAPI_BULK_IMPORT_STATE, &state);
  1777. if (state == SLAPI_BI_STATE_START) {
  1778. /* starting a new import */
  1779. return bulk_import_start(pb);
  1780. }
  1781. PR_ASSERT(pb->pb_conn != NULL);
  1782. if (pb->pb_conn != NULL) {
  1783. job = (ImportJob *)slapi_get_object_extension(li->li_bulk_import_object, pb->pb_conn, li->li_bulk_import_handle);
  1784. }
  1785. if ((job == NULL) || (pb->pb_conn == NULL)) {
  1786. /* import might be aborting */
  1787. return -1;
  1788. }
  1789. if (state == SLAPI_BI_STATE_ADD) {
  1790. /* continuing previous import */
  1791. if (! import_entry_belongs_here(pb->pb_import_entry,
  1792. job->inst->inst_be)) {
  1793. /* silently skip */
  1794. /* We need to consume pb->pb_import_entry on success, so we free it here. */
  1795. slapi_entry_free(pb->pb_import_entry);
  1796. return 0;
  1797. }
  1798. return bulk_import_queue(job, pb->pb_import_entry);
  1799. }
  1800. thread = job->main_thread;
  1801. if (state == SLAPI_BI_STATE_DONE) {
  1802. /* finished with an import */
  1803. job->flags |= FLAG_PRODUCER_DONE;
  1804. /* "job" struct may vanish at any moment after we set the DONE
  1805. * flag, so keep a copy of the thread id in 'thread' for safekeeping.
  1806. */
  1807. /* wait for import_main to finish... */
  1808. PR_JoinThread(thread);
  1809. slapi_set_object_extension(li->li_bulk_import_object, pb->pb_conn,
  1810. li->li_bulk_import_handle, NULL);
  1811. return 0;
  1812. }
  1813. /* ??? unknown state */
  1814. LDAPDebug(LDAP_DEBUG_ANY,
  1815. "ERROR: ldbm_back_wire_import: unknown state %d\n",
  1816. state, 0, 0);
  1817. return -1;
  1818. }
  1819. /*
  1820. * backup index configuration
  1821. * this function is called from dblayer_backup (ldbm2archive)
  1822. * [547427] index config must not change between backup and restore
  1823. */
  1824. #define DSE_INDEX "dse_index.ldif"
  1825. #define DSE_INSTANCE "dse_instance.ldif"
  1826. #define DSE_INDEX_FILTER "(objectclass=nsIndex)"
  1827. #define DSE_INSTANCE_FILTER "(objectclass=nsBackendInstance)"
  1828. static int
  1829. dse_conf_backup_core(struct ldbminfo *li, char *dest_dir, char *file_name, char *filter)
  1830. {
  1831. Slapi_PBlock *srch_pb = NULL;
  1832. Slapi_Entry **entries = NULL;
  1833. Slapi_Entry **ep = NULL;
  1834. Slapi_Attr *attr = NULL;
  1835. char *attr_name;
  1836. char *filename = NULL;
  1837. PRFileDesc *prfd = NULL;
  1838. int rval = 0;
  1839. int dlen = 0;
  1840. PRInt32 prrval;
  1841. char tmpbuf[BUFSIZ];
  1842. char *tp = NULL;
  1843. dlen = strlen(dest_dir);
  1844. if (0 == dlen)
  1845. {
  1846. filename = file_name;
  1847. }
  1848. else
  1849. {
  1850. filename = slapi_ch_smprintf("%s/%s", dest_dir, file_name);
  1851. }
  1852. LDAPDebug(LDAP_DEBUG_TRACE, "dse_conf_backup(%s): backup file %s\n",
  1853. filter, filename, 0);
  1854. /* Open the file to write */
  1855. if ((prfd = PR_Open(filename, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE,
  1856. SLAPD_DEFAULT_FILE_MODE)) == NULL)
  1857. {
  1858. LDAPDebug(LDAP_DEBUG_ANY,
  1859. "dse_conf_backup(%s): open %s failed: (%s)\n",
  1860. filter, filename, slapd_pr_strerror(PR_GetError()));
  1861. rval = -1;
  1862. goto out;
  1863. }
  1864. srch_pb = slapi_pblock_new();
  1865. slapi_search_internal_set_pb(srch_pb, li->li_plugin->plg_dn,
  1866. LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, li->li_identity, 0);
  1867. slapi_search_internal_pb(srch_pb);
  1868. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1869. for (ep = entries; ep != NULL && *ep != NULL; ep++)
  1870. {
  1871. size_t l = strlen(slapi_entry_get_dn_const(*ep)) + 5 /* "dn: \n" */;
  1872. LDAPDebug(LDAP_DEBUG_TRACE, "\ndn: %s\n",
  1873. slapi_entry_get_dn_const(*ep), 0, 0);
  1874. if (l <= BUFSIZ)
  1875. tp = tmpbuf;
  1876. else
  1877. tp = (char *)slapi_ch_malloc(l); /* should be very rare ... */
  1878. sprintf(tp, "dn: %s\n", slapi_entry_get_dn_const(*ep));
  1879. prrval = PR_Write(prfd, tp, l);
  1880. if ((size_t)prrval != l)
  1881. {
  1882. LDAPDebug(LDAP_DEBUG_ANY,
  1883. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1884. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1885. rval = -1;
  1886. if (l > BUFSIZ)
  1887. slapi_ch_free_string(&tp);
  1888. goto out;
  1889. }
  1890. if (l > BUFSIZ)
  1891. slapi_ch_free_string(&tp);
  1892. for (slapi_entry_first_attr(*ep, &attr); attr;
  1893. slapi_entry_next_attr(*ep, attr, &attr))
  1894. {
  1895. int i;
  1896. Slapi_Value *sval = NULL;
  1897. const struct berval *attr_val;
  1898. int attr_name_len;
  1899. slapi_attr_get_type(attr, &attr_name);
  1900. /* numsubordinates should not be backed up */
  1901. if (!strcasecmp(LDBM_NUMSUBORDINATES_STR, attr_name))
  1902. continue;
  1903. attr_name_len = strlen(attr_name);
  1904. for (i = slapi_attr_first_value(attr, &sval); i != -1;
  1905. i = slapi_attr_next_value(attr, i, &sval))
  1906. {
  1907. attr_val = slapi_value_get_berval(sval);
  1908. l = strlen(attr_val->bv_val) + attr_name_len + 3; /* : \n" */
  1909. LDAPDebug(LDAP_DEBUG_TRACE, "%s: %s\n", attr_name,
  1910. attr_val->bv_val, 0);
  1911. if (l <= BUFSIZ)
  1912. tp = tmpbuf;
  1913. else
  1914. tp = (char *)slapi_ch_malloc(l);
  1915. sprintf(tp, "%s: %s\n", attr_name, attr_val->bv_val);
  1916. prrval = PR_Write(prfd, tp, l);
  1917. if ((size_t)prrval != l)
  1918. {
  1919. LDAPDebug(LDAP_DEBUG_ANY,
  1920. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1921. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1922. rval = -1;
  1923. if (l > BUFSIZ)
  1924. slapi_ch_free_string(&tp);
  1925. goto out;
  1926. }
  1927. if (l > BUFSIZ)
  1928. slapi_ch_free_string(&tp);
  1929. }
  1930. }
  1931. if (ep+1 != NULL && *(ep+1) != NULL)
  1932. {
  1933. prrval = PR_Write(prfd, "\n", 1);
  1934. if ((int)prrval != 1)
  1935. {
  1936. LDAPDebug(LDAP_DEBUG_ANY,
  1937. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1938. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1939. rval = -1;
  1940. goto out;
  1941. }
  1942. }
  1943. }
  1944. out:
  1945. slapi_free_search_results_internal(srch_pb);
  1946. if (srch_pb)
  1947. {
  1948. slapi_pblock_destroy(srch_pb);
  1949. }
  1950. if (0 != dlen)
  1951. {
  1952. slapi_ch_free_string(&filename);
  1953. }
  1954. if (prfd)
  1955. {
  1956. prrval = PR_Close(prfd);
  1957. if (PR_SUCCESS != prrval)
  1958. {
  1959. LDAPDebug( LDAP_DEBUG_ANY,
  1960. "Fatal Error---Failed to back up dse indexes %d (%s)\n",
  1961. PR_GetError(), slapd_pr_strerror(PR_GetError()), 0);
  1962. rval = -1;
  1963. }
  1964. }
  1965. return rval;
  1966. }
  1967. int
  1968. dse_conf_backup(struct ldbminfo *li, char *dest_dir)
  1969. {
  1970. int rval = 0;
  1971. rval = dse_conf_backup_core(li, dest_dir, DSE_INSTANCE, DSE_INSTANCE_FILTER);
  1972. rval += dse_conf_backup_core(li, dest_dir, DSE_INDEX, DSE_INDEX_FILTER);
  1973. return rval;
  1974. }
  1975. /*
  1976. * read the backed up index configuration
  1977. * adjust them if the current configuration is different from it.
  1978. * this function is called from dblayer_restore (archive2ldbm)
  1979. * these functions are placed here to borrow import_get_entry
  1980. * [547427] index config must not change between backup and restore
  1981. */
  1982. int
  1983. dse_conf_verify_core(struct ldbminfo *li, char *src_dir, char *file_name, char *filter, char *log_str, char *entry_filter)
  1984. {
  1985. char *filename = NULL;
  1986. int rval = 0;
  1987. ldif_context c;
  1988. int fd = -1;
  1989. int curr_lineno = 0;
  1990. int finished = 0;
  1991. int backup_entry_len = 256;
  1992. char *search_scope = NULL;
  1993. Slapi_Entry **backup_entries = NULL;
  1994. Slapi_Entry **bep = NULL;
  1995. Slapi_Entry **curr_entries = NULL;
  1996. Slapi_PBlock srch_pb;
  1997. filename = slapi_ch_smprintf("%s/%s", src_dir, file_name);
  1998. if (PR_SUCCESS != PR_Access(filename, PR_ACCESS_READ_OK))
  1999. {
  2000. LDAPDebug(LDAP_DEBUG_ANY,
  2001. "Warning: config backup file %s not found in backup\n",
  2002. file_name, 0, 0);
  2003. rval = 0;
  2004. goto out;
  2005. }
  2006. fd = dblayer_open_huge_file(filename, O_RDONLY, 0);
  2007. if (fd < 0)
  2008. {
  2009. LDAPDebug(LDAP_DEBUG_ANY,
  2010. "Warning: can't open config backup file: %s\n", filename, 0, 0);
  2011. rval = -1;
  2012. goto out;
  2013. }
  2014. import_init_ldif(&c);
  2015. bep = backup_entries = (Slapi_Entry **)slapi_ch_calloc(1,
  2016. backup_entry_len * sizeof(Slapi_Entry *));
  2017. while (!finished)
  2018. {
  2019. char *estr = NULL;
  2020. Slapi_Entry *e = NULL;
  2021. estr = import_get_entry(&c, fd, &curr_lineno);
  2022. if (!estr)
  2023. break;
  2024. if (entry_filter != NULL) /* Single instance restoration */
  2025. {
  2026. if (NULL == strstr(estr, entry_filter))
  2027. continue;
  2028. }
  2029. e = slapi_str2entry(estr, 0);
  2030. slapi_ch_free_string(&estr);
  2031. if (!e) {
  2032. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: skipping bad LDIF entry "
  2033. "ending line %d of file \"%s\"", curr_lineno, filename, 0);
  2034. continue;
  2035. }
  2036. if (bep - backup_entries >= backup_entry_len)
  2037. {
  2038. backup_entries = (Slapi_Entry **)slapi_ch_realloc((char *)backup_entries,
  2039. 2 * backup_entry_len * sizeof(Slapi_Entry *));
  2040. bep = backup_entries + backup_entry_len;
  2041. backup_entry_len *= 2;
  2042. }
  2043. *bep = e;
  2044. bep++;
  2045. }
  2046. /* 623986: terminate the list if we reallocated backup_entries */
  2047. if (backup_entry_len > 256)
  2048. *bep = NULL;
  2049. pblock_init(&srch_pb);
  2050. if (entry_filter != NULL)
  2051. { /* Single instance restoration */
  2052. search_scope = slapi_ch_smprintf("%s,%s", entry_filter, li->li_plugin->plg_dn);
  2053. } else { /* Normal restoration */
  2054. search_scope = slapi_ch_strdup(li->li_plugin->plg_dn);
  2055. }
  2056. slapi_search_internal_set_pb(&srch_pb, search_scope,
  2057. LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, li->li_identity, 0);
  2058. slapi_search_internal_pb(&srch_pb);
  2059. slapi_pblock_get(&srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &curr_entries);
  2060. if (0 != slapi_entries_diff(backup_entries, curr_entries, 1 /* test_all */,
  2061. log_str, 1 /* force_update */, li->li_identity))
  2062. {
  2063. LDAPDebug(LDAP_DEBUG_ANY, "WARNING!!: current %s is "
  2064. "different from backed up configuration; "
  2065. "The backup is restored.\n", log_str, 0, 0);
  2066. }
  2067. slapi_free_search_results_internal(&srch_pb);
  2068. pblock_done(&srch_pb);
  2069. import_free_ldif(&c);
  2070. out:
  2071. for (bep = backup_entries; bep && *bep; bep++)
  2072. slapi_entry_free(*bep);
  2073. slapi_ch_free((void **)&backup_entries);
  2074. slapi_ch_free_string(&filename);
  2075. slapi_ch_free_string(&search_scope);
  2076. if (fd > 0)
  2077. close(fd);
  2078. return rval;
  2079. }
  2080. int
  2081. dse_conf_verify(struct ldbminfo *li, char *src_dir, char *bename)
  2082. {
  2083. int rval;
  2084. char *entry_filter = NULL;
  2085. char *instance_entry_filter = NULL;
  2086. if (bename != NULL) /* This was a restore of a single backend */
  2087. {
  2088. /* Entry filter string */
  2089. entry_filter = slapi_ch_smprintf("cn=%s", bename);
  2090. /* Instance search filter */
  2091. instance_entry_filter = slapi_ch_smprintf("(&%s(cn=%s))", DSE_INSTANCE_FILTER, bename);
  2092. } else {
  2093. instance_entry_filter = slapi_ch_strdup(DSE_INSTANCE_FILTER);
  2094. }
  2095. rval = dse_conf_verify_core(li, src_dir, DSE_INSTANCE, instance_entry_filter,
  2096. "Instance Config", entry_filter);
  2097. rval += dse_conf_verify_core(li, src_dir, DSE_INDEX, DSE_INDEX_FILTER,
  2098. "Index Config", entry_filter);
  2099. slapi_ch_free_string(&entry_filter);
  2100. slapi_ch_free_string(&instance_entry_filter);
  2101. return rval;
  2102. }
  2103. static int
  2104. import_get_and_add_parent_rdns(ImportWorkerInfo *info,
  2105. ldbm_instance *inst,
  2106. DB *db,
  2107. ID id,
  2108. ID *total_id,
  2109. Slapi_RDN *srdn,
  2110. int *curr_entry)
  2111. {
  2112. int rc = -1;
  2113. struct backdn *bdn = NULL;
  2114. struct ldbminfo *li = NULL;
  2115. Slapi_Entry *e = NULL;
  2116. char *dn = NULL;
  2117. if (!entryrdn_get_switch()) { /* entryrdn specific function */
  2118. return rc;
  2119. }
  2120. if (NULL == inst || NULL == srdn) {
  2121. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2122. "import_get_and_add_parent_rdns: Empty %s\n",
  2123. NULL==inst?"inst":NULL==srdn?"srdn":"unknown");
  2124. return rc;
  2125. }
  2126. li = inst->inst_li;
  2127. /* first, try the dn cache */
  2128. bdn = dncache_find_id(&inst->inst_dncache, id);
  2129. if (bdn) {
  2130. Slapi_RDN mysrdn = {0};
  2131. /* Luckily, found the parent in the dn cache! */
  2132. if (slapi_rdn_get_rdn(srdn)) { /* srdn is already in use */
  2133. rc = slapi_rdn_init_all_dn(&mysrdn, slapi_sdn_get_dn(bdn->dn_sdn));
  2134. if (rc) {
  2135. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2136. "import_get_and_add_parent_rdns: "
  2137. "Failed to convert DN %s to RDN\n",
  2138. slapi_sdn_get_dn(bdn->dn_sdn));
  2139. slapi_rdn_done(&mysrdn);
  2140. CACHE_RETURN(&inst->inst_dncache, &bdn);
  2141. return rc;
  2142. }
  2143. rc = slapi_rdn_add_srdn_to_all_rdns(srdn, &mysrdn);
  2144. if (rc) {
  2145. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2146. "import_get_and_add_parent_rdns: "
  2147. "Failed to merge Slapi_RDN %s to RDN\n",
  2148. slapi_sdn_get_dn(bdn->dn_sdn));
  2149. }
  2150. slapi_rdn_done(&mysrdn);
  2151. } else { /* srdn is empty */
  2152. rc = slapi_rdn_init_all_dn(srdn, slapi_sdn_get_dn(bdn->dn_sdn));
  2153. if (rc) {
  2154. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2155. "import_get_and_add_parent_rdns: "
  2156. "Failed to convert DN %s to RDN\n",
  2157. slapi_sdn_get_dn(bdn->dn_sdn));
  2158. CACHE_RETURN(&inst->inst_dncache, &bdn);
  2159. return rc;
  2160. }
  2161. }
  2162. CACHE_RETURN(&inst->inst_dncache, &bdn);
  2163. return rc;
  2164. } else {
  2165. DBT key, data;
  2166. char *rdn = NULL;
  2167. char *pid_str = NULL;
  2168. ID storedid;
  2169. Slapi_RDN mysrdn = {0};
  2170. /* not in the dn cache; read id2entry */
  2171. if (NULL == db) {
  2172. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2173. "import_get_and_add_parent_rdns: Empty db\n");
  2174. return rc;
  2175. }
  2176. id_internal_to_stored(id, (char *)&storedid);
  2177. key.size = key.ulen = sizeof(ID);
  2178. key.data = &storedid;
  2179. key.flags = DB_DBT_USERMEM;
  2180. memset(&data, 0, sizeof(data));
  2181. data.flags = DB_DBT_MALLOC;
  2182. rc = db->get(db, NULL, &key, &data, 0);
  2183. if (rc) {
  2184. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2185. "import_get_and_add_parent_rdns: Failed to "
  2186. "position at ID " ID_FMT "\n", id);
  2187. return rc;
  2188. }
  2189. /* rdn is allocated in get_value_from_string */
  2190. rc = get_value_from_string((const char *)data.dptr, "rdn", &rdn);
  2191. if (rc) {
  2192. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2193. "import_get_and_add_parent_rdns: "
  2194. "Failed to get rdn of entry " ID_FMT "\n", id);
  2195. goto bail;
  2196. }
  2197. /* rdn is set to srdn */
  2198. rc = slapi_rdn_init_all_dn(&mysrdn, rdn);
  2199. if (rc < 0) { /* expect rc == 1 since we are setting "rdn" not "dn" */
  2200. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2201. "import_get_and_add_parent_rdns: "
  2202. "Failed to add rdn %s of entry " ID_FMT "\n", rdn, id);
  2203. goto bail;
  2204. }
  2205. rc = get_value_from_string((const char *)data.dptr,
  2206. LDBM_PARENTID_STR, &pid_str);
  2207. if (rc) {
  2208. rc = 0; /* assume this is a suffix */
  2209. } else {
  2210. ID pid = (ID)strtol(pid_str, (char **)NULL, 10);
  2211. rc = import_get_and_add_parent_rdns(info, inst, db, pid, total_id,
  2212. &mysrdn, curr_entry);
  2213. if (rc) {
  2214. slapi_ch_free_string(&rdn);
  2215. goto bail;
  2216. }
  2217. }
  2218. dn = NULL;
  2219. rc = slapi_rdn_get_dn(&mysrdn, &dn);
  2220. if (rc) {
  2221. LDAPDebug2Args( LDAP_DEBUG_ANY,
  2222. "import_get_and_add_parent_rdns: "
  2223. "Failed to compose dn for (rdn: %s, ID: %d) "
  2224. "from Slapi_RDN\n", rdn, id);
  2225. goto bail;
  2226. }
  2227. e = slapi_str2entry_ext( dn, data.dptr, 0 );
  2228. (*curr_entry)++;
  2229. rc = index_set_entry_to_fifo(info, e, id, total_id, *curr_entry);
  2230. if (rc) {
  2231. goto bail;
  2232. }
  2233. rc = slapi_rdn_add_srdn_to_all_rdns(srdn, &mysrdn);
  2234. if (rc) {
  2235. slapi_log_error(SLAPI_LOG_FATAL, "ldif2dbm",
  2236. "import_get_and_add_parent_rdns: "
  2237. "Failed to merge Slapi_RDN %s to RDN\n",
  2238. slapi_sdn_get_dn(bdn->dn_sdn));
  2239. }
  2240. bail:
  2241. slapi_ch_free(&data.data);
  2242. slapi_ch_free_string(&rdn);
  2243. return rc;
  2244. }
  2245. }