task.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  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. * directory online tasks (import, export, backup, restore)
  43. */
  44. #include "slap.h"
  45. /***********************************
  46. * Static Global Variables
  47. ***********************************/
  48. /* don't panic, this is only used when creating new tasks or removing old
  49. * ones...
  50. */
  51. static Slapi_Task *global_task_list = NULL;
  52. static PRLock *global_task_lock = NULL;
  53. static int shutting_down = 0;
  54. /***********************************
  55. * Private Defines
  56. ***********************************/
  57. #define TASK_BASE_DN "cn=tasks,cn=config"
  58. #define TASK_IMPORT_DN "cn=import,cn=tasks,cn=config"
  59. #define TASK_EXPORT_DN "cn=export,cn=tasks,cn=config"
  60. #define TASK_BACKUP_DN "cn=backup,cn=tasks,cn=config"
  61. #define TASK_RESTORE_DN "cn=restore,cn=tasks,cn=config"
  62. #define TASK_INDEX_DN "cn=index,cn=tasks,cn=config"
  63. #define TASK_UPGRADEDB_DN "cn=upgradedb,cn=tasks,cn=config"
  64. #define TASK_LOG_NAME "nsTaskLog"
  65. #define TASK_STATUS_NAME "nsTaskStatus"
  66. #define TASK_EXITCODE_NAME "nsTaskExitCode"
  67. #define TASK_PROGRESS_NAME "nsTaskCurrentItem"
  68. #define TASK_WORK_NAME "nsTaskTotalItems"
  69. #define DEFAULT_TTL "120" /* seconds */
  70. #define LOG_BUFFER 256
  71. /* if the cumul. log gets larger than this, it's truncated: */
  72. #define MAX_SCROLLBACK_BUFFER 8192
  73. #define NEXTMOD(_type, _val) do { \
  74. modlist[cur].mod_op = LDAP_MOD_REPLACE; \
  75. modlist[cur].mod_type = (_type); \
  76. modlist[cur].mod_values = (char **)slapi_ch_malloc(2*sizeof(char *)); \
  77. modlist[cur].mod_values[0] = (_val); \
  78. modlist[cur].mod_values[1] = NULL; \
  79. mod[cur] = &modlist[cur]; \
  80. cur++; \
  81. } while (0)
  82. /***********************************
  83. * Static Function Prototypes
  84. ***********************************/
  85. static Slapi_Task *new_task(const char *dn);
  86. static void destroy_task(time_t when, void *arg);
  87. static int task_modify(Slapi_PBlock *pb, Slapi_Entry *e,
  88. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  89. static int task_deny(Slapi_PBlock *pb, Slapi_Entry *e,
  90. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  91. static void task_generic_destructor(Slapi_Task *task);
  92. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  93. const char *default_val);
  94. static Slapi_Entry *get_internal_entry(Slapi_PBlock *pb, char *dn);
  95. static void modify_internal_entry(char *dn, LDAPMod **mods);
  96. /***********************************
  97. * Public Functions
  98. ***********************************/
  99. /*
  100. * slapi_new_task: create a new task, fill in DN, and setup modify callback
  101. * argument:
  102. * dn: task dn
  103. * result:
  104. * Success: Slapi_Task object
  105. * Failure: NULL
  106. */
  107. Slapi_Task *
  108. slapi_new_task(const char *dn)
  109. {
  110. return new_task(dn);
  111. }
  112. /* slapi_destroy_task: destroy a task
  113. * argument:
  114. * task: task to destroy
  115. * result:
  116. * none
  117. */
  118. void
  119. slapi_destroy_task(void *arg)
  120. {
  121. if (arg) {
  122. destroy_task(1, arg);
  123. }
  124. }
  125. /*
  126. * Sets the initial task state and updated status
  127. */
  128. void slapi_task_begin(Slapi_Task *task, int total_work)
  129. {
  130. if (task) {
  131. task->task_work = total_work;
  132. task->task_progress = 0;
  133. task->task_state = SLAPI_TASK_RUNNING;
  134. slapi_task_status_changed(task);
  135. }
  136. }
  137. /*
  138. * Increments task progress and updates status
  139. */
  140. void slapi_task_inc_progress(Slapi_Task *task)
  141. {
  142. if (task) {
  143. task->task_progress++;
  144. slapi_task_status_changed(task);
  145. }
  146. }
  147. /*
  148. * Sets completed task state and updates status
  149. */
  150. void slapi_task_finish(Slapi_Task *task, int rc)
  151. {
  152. if (task) {
  153. task->task_exitcode = rc;
  154. task->task_state = SLAPI_TASK_FINISHED;
  155. slapi_task_status_changed(task);
  156. }
  157. }
  158. /*
  159. * Cancels a task
  160. */
  161. void slapi_task_cancel(Slapi_Task *task, int rc)
  162. {
  163. if (task) {
  164. task->task_exitcode = rc;
  165. task->task_state = SLAPI_TASK_CANCELLED;
  166. slapi_task_status_changed(task);
  167. }
  168. }
  169. /*
  170. * Get the current state of a task
  171. */
  172. int slapi_task_get_state(Slapi_Task *task)
  173. {
  174. if (task) {
  175. return task->task_state;
  176. }
  177. return 0; /* return value not currently used */
  178. }
  179. /* this changes the 'nsTaskStatus' value, which is transient (anything logged
  180. * here wipes out any previous status)
  181. */
  182. void slapi_task_log_status(Slapi_Task *task, char *format, ...)
  183. {
  184. va_list ap;
  185. if (! task->task_status)
  186. task->task_status = (char *)slapi_ch_malloc(10 * LOG_BUFFER);
  187. if (! task->task_status)
  188. return; /* out of memory? */
  189. va_start(ap, format);
  190. PR_vsnprintf(task->task_status, (10 * LOG_BUFFER), format, ap);
  191. va_end(ap);
  192. slapi_task_status_changed(task);
  193. }
  194. /* this adds a line to the 'nsTaskLog' value, which is cumulative (anything
  195. * logged here is added to the end)
  196. */
  197. void slapi_task_log_notice(Slapi_Task *task, char *format, ...)
  198. {
  199. va_list ap;
  200. char buffer[LOG_BUFFER];
  201. size_t len;
  202. va_start(ap, format);
  203. PR_vsnprintf(buffer, LOG_BUFFER, format, ap);
  204. va_end(ap);
  205. if (task->task_log_lock) {
  206. PR_Lock(task->task_log_lock);
  207. }
  208. len = 2 + strlen(buffer) + (task->task_log ? strlen(task->task_log) : 0);
  209. if ((len > MAX_SCROLLBACK_BUFFER) && task->task_log) {
  210. size_t i;
  211. char *newbuf;
  212. /* start from middle of buffer, and find next linefeed */
  213. i = strlen(task->task_log)/2;
  214. while (task->task_log[i] && (task->task_log[i] != '\n'))
  215. i++;
  216. if (task->task_log[i])
  217. i++;
  218. len = strlen(task->task_log) - i + 2 + strlen(buffer);
  219. newbuf = (char *)slapi_ch_malloc(len);
  220. strcpy(newbuf, task->task_log + i);
  221. slapi_ch_free((void **)&task->task_log);
  222. task->task_log = newbuf;
  223. } else {
  224. if (! task->task_log) {
  225. task->task_log = (char *)slapi_ch_malloc(len);
  226. task->task_log[0] = 0;
  227. } else {
  228. task->task_log = (char *)slapi_ch_realloc(task->task_log, len);
  229. }
  230. }
  231. if (task->task_log[0])
  232. strcat(task->task_log, "\n");
  233. strcat(task->task_log, buffer);
  234. if (task->task_log_lock) {
  235. PR_Unlock(task->task_log_lock);
  236. }
  237. slapi_task_status_changed(task);
  238. }
  239. /* update attributes in the entry under "cn=tasks" to match the current
  240. * status of the task. */
  241. void slapi_task_status_changed(Slapi_Task *task)
  242. {
  243. LDAPMod modlist[20];
  244. LDAPMod *mod[20];
  245. int cur = 0, i;
  246. char s1[20], s2[20], s3[20];
  247. if (shutting_down) {
  248. /* don't care about task status updates anymore */
  249. return;
  250. }
  251. if (task->task_log_lock) {
  252. PR_Lock(task->task_log_lock);
  253. }
  254. NEXTMOD(TASK_LOG_NAME, task->task_log);
  255. if (task->task_log_lock) {
  256. PR_Unlock(task->task_log_lock);
  257. }
  258. NEXTMOD(TASK_STATUS_NAME, task->task_status);
  259. sprintf(s1, "%d", task->task_exitcode);
  260. sprintf(s2, "%d", task->task_progress);
  261. sprintf(s3, "%d", task->task_work);
  262. NEXTMOD(TASK_PROGRESS_NAME, s2);
  263. NEXTMOD(TASK_WORK_NAME, s3);
  264. /* only add the exit code when the job is done */
  265. if ((task->task_state == SLAPI_TASK_FINISHED) ||
  266. (task->task_state == SLAPI_TASK_CANCELLED)) {
  267. NEXTMOD(TASK_EXITCODE_NAME, s1);
  268. /* make sure the console can tell the task has ended */
  269. if (task->task_progress != task->task_work) {
  270. task->task_progress = task->task_work;
  271. }
  272. }
  273. mod[cur] = NULL;
  274. modify_internal_entry(task->task_dn, mod);
  275. for (i = 0; i < cur; i++)
  276. slapi_ch_free((void **)&modlist[i].mod_values);
  277. /*
  278. * Removed (task->task_state == SLAPI_TASK_CANCELLED) from
  279. * task_state checking to fix bz 515805.
  280. */
  281. if ((task->task_state == SLAPI_TASK_FINISHED) &&
  282. !(task->task_flags & SLAPI_TASK_DESTROYING)) {
  283. Slapi_PBlock *pb = slapi_pblock_new();
  284. Slapi_Entry *e;
  285. int ttl;
  286. time_t expire;
  287. e = get_internal_entry(pb, task->task_dn);
  288. if (e == NULL)
  289. return;
  290. ttl = atoi(fetch_attr(e, "ttl", DEFAULT_TTL));
  291. if (ttl > 3600)
  292. ttl = 3600; /* be reasonable. */
  293. expire = time(NULL) + ttl;
  294. task->task_flags |= SLAPI_TASK_DESTROYING;
  295. /* queue an event to destroy the state info */
  296. slapi_eq_once(destroy_task, (void *)task, expire);
  297. slapi_free_search_results_internal(pb);
  298. slapi_pblock_destroy(pb);
  299. }
  300. }
  301. /*
  302. * Stash some opaque task specific data in the task for later use.
  303. */
  304. void slapi_task_set_data(Slapi_Task *task, void *data)
  305. {
  306. if (task) {
  307. task->task_private = data;
  308. }
  309. }
  310. /*
  311. * Retrieve some opaque task specific data from the task.
  312. */
  313. void * slapi_task_get_data(Slapi_Task *task)
  314. {
  315. if (task) {
  316. return task->task_private;
  317. }
  318. return NULL; /* return value not currently used */
  319. }
  320. /*
  321. * Increment the task reference count
  322. */
  323. void slapi_task_inc_refcount(Slapi_Task *task)
  324. {
  325. if (task) {
  326. task->task_refcount++;
  327. }
  328. }
  329. /*
  330. * Decrement the task reference count
  331. */
  332. void slapi_task_dec_refcount(Slapi_Task *task)
  333. {
  334. if (task) {
  335. task->task_refcount--;
  336. }
  337. }
  338. /*
  339. * Returns the task reference count
  340. */
  341. int slapi_task_get_refcount(Slapi_Task *task)
  342. {
  343. if (task) {
  344. return task->task_refcount;
  345. }
  346. return 0; /* return value not currently used */
  347. }
  348. /* name is, for example, "import" */
  349. int slapi_task_register_handler(const char *name, dseCallbackFn func)
  350. {
  351. char *dn = NULL;
  352. Slapi_PBlock *pb = NULL;
  353. Slapi_Operation *op;
  354. LDAPMod *mods[3];
  355. LDAPMod mod[3];
  356. const char *objectclass[3];
  357. const char *cnvals[2];
  358. int ret = -1;
  359. int x;
  360. dn = slapi_create_dn_string("cn=%s,%s", name, TASK_BASE_DN);
  361. if (NULL == dn) {
  362. LDAPDebug1Arg( LDAP_DEBUG_ANY,
  363. "slapi_task_register_handler: "
  364. "failed to create task dn for %s\n", name);
  365. return ret;
  366. }
  367. pb = slapi_pblock_new();
  368. if (pb == NULL) {
  369. goto out;
  370. }
  371. /* this is painful :( */
  372. mods[0] = &mod[0];
  373. mod[0].mod_op = LDAP_MOD_ADD;
  374. mod[0].mod_type = "objectClass";
  375. mod[0].mod_values = (char **)objectclass;
  376. objectclass[0] = "top";
  377. objectclass[1] = "extensibleObject";
  378. objectclass[2] = NULL;
  379. mods[1] = &mod[1];
  380. mod[1].mod_op = LDAP_MOD_ADD;
  381. mod[1].mod_type = "cn";
  382. mod[1].mod_values = (char **)cnvals;
  383. cnvals[0] = name;
  384. cnvals[1] = NULL;
  385. mods[2] = NULL;
  386. slapi_add_internal_set_pb(pb, dn, mods, NULL,
  387. plugin_get_default_component_id(), 0);
  388. x = 1;
  389. slapi_pblock_set(pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &x);
  390. /* Make sure these adds don't appear in the audit and change logs */
  391. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  392. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  393. slapi_add_internal_pb(pb);
  394. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &x);
  395. if ((x != LDAP_SUCCESS) && (x != LDAP_ALREADY_EXISTS)) {
  396. LDAPDebug(LDAP_DEBUG_ANY,
  397. "Can't create task node '%s' (error %d)\n",
  398. name, x, 0);
  399. ret = x;
  400. goto out;
  401. }
  402. /* register add callback */
  403. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP,
  404. dn, LDAP_SCOPE_SUBTREE, "(objectclass=*)", func, NULL);
  405. /* deny modify/delete of the root task entry */
  406. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  407. dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  408. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  409. dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  410. ret = 0;
  411. out:
  412. slapi_ch_free_string(&dn);
  413. if (pb) {
  414. slapi_pblock_destroy(pb);
  415. }
  416. return ret;
  417. }
  418. void slapi_task_set_destructor_fn(Slapi_Task *task, TaskCallbackFn func)
  419. {
  420. if (task) {
  421. task->destructor = func;
  422. }
  423. }
  424. void slapi_task_set_cancel_fn(Slapi_Task *task, TaskCallbackFn func)
  425. {
  426. if (task) {
  427. task->cancel = func;
  428. }
  429. }
  430. /***********************************
  431. * Static Helper Functions
  432. ***********************************/
  433. /* create a new task, fill in DN, and setup modify callback */
  434. static Slapi_Task *
  435. new_task(const char *rawdn)
  436. {
  437. Slapi_Task *task = NULL;
  438. char *dn = NULL;
  439. if (rawdn == NULL) {
  440. return NULL;
  441. }
  442. dn = slapi_create_dn_string("%s", rawdn);
  443. if (NULL == dn) {
  444. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  445. "new_task failed: invalid task dn: %s\n", rawdn);
  446. return NULL;
  447. }
  448. task = (Slapi_Task *)slapi_ch_calloc(1, sizeof(Slapi_Task));
  449. PR_Lock(global_task_lock);
  450. task->next = global_task_list;
  451. global_task_list = task;
  452. PR_Unlock(global_task_lock);
  453. task->task_dn = dn;
  454. task->task_state = SLAPI_TASK_SETUP;
  455. task->task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  456. task->destructor = NULL;
  457. task->cancel = NULL;
  458. task->task_private = NULL;
  459. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, dn,
  460. LDAP_SCOPE_BASE, "(objectclass=*)", task_modify, (void *)task);
  461. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, dn,
  462. LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  463. /* don't add entries under this one */
  464. #if 0
  465. /* don't know why, but this doesn't work. it makes the current add
  466. * operation fail. :(
  467. */
  468. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, dn,
  469. LDAP_SCOPE_SUBTREE, "(objectclass=*)", task_deny, NULL);
  470. #endif
  471. /* To protect task_log to be realloced if it's in use */
  472. task->task_log_lock = PR_NewLock();
  473. return task;
  474. }
  475. /* called by the event queue to destroy a task */
  476. static void
  477. destroy_task(time_t when, void *arg)
  478. {
  479. Slapi_Task *task = (Slapi_Task *)arg;
  480. Slapi_Task *t1;
  481. Slapi_PBlock *pb = slapi_pblock_new();
  482. /* Call the custom destructor callback if one was provided,
  483. * then perform the internal task destruction. */
  484. if (task->destructor != NULL) {
  485. (*task->destructor)(task);
  486. }
  487. task_generic_destructor(task);
  488. /* if when == 0, we're already locked (called during shutdown) */
  489. if (when != 0) {
  490. PR_Lock(global_task_lock);
  491. }
  492. if (global_task_list == task) {
  493. global_task_list = task->next;
  494. } else {
  495. for (t1 = global_task_list; t1; t1 = t1->next) {
  496. if (t1->next == task) {
  497. t1->next = task->next;
  498. break;
  499. }
  500. }
  501. }
  502. if (when != 0) {
  503. PR_Unlock(global_task_lock);
  504. }
  505. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  506. task->task_dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_modify);
  507. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  508. task->task_dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny);
  509. slapi_delete_internal_set_pb(pb, task->task_dn, NULL, NULL,
  510. (void *)plugin_get_default_component_id(), 0);
  511. slapi_delete_internal_pb(pb);
  512. slapi_pblock_destroy(pb);
  513. slapi_ch_free((void **)&task->task_dn);
  514. slapi_ch_free((void **)&task);
  515. }
  516. /* extract a single value from the entry (as a string) -- if it's not in the
  517. * entry, the default will be returned (which can be NULL).
  518. * you do not need to free anything returned by this.
  519. */
  520. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  521. const char *default_val)
  522. {
  523. Slapi_Attr *attr;
  524. Slapi_Value *val = NULL;
  525. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  526. return default_val;
  527. slapi_attr_first_value(attr, &val);
  528. return slapi_value_get_string(val);
  529. }
  530. /* supply the pblock, destroy it when you're done */
  531. static Slapi_Entry *get_internal_entry(Slapi_PBlock *pb, char *dn)
  532. {
  533. Slapi_Entry **entries = NULL;
  534. int ret = 0;
  535. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
  536. NULL, 0, NULL, NULL, (void *)plugin_get_default_component_id(), 0);
  537. slapi_search_internal_pb(pb);
  538. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  539. if (ret != LDAP_SUCCESS) {
  540. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't find task entry '%s'\n",
  541. dn, 0, 0);
  542. return NULL;
  543. }
  544. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  545. if ((NULL == entries) || (NULL == entries[0])) {
  546. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't find task entry '%s'\n",
  547. dn, 0, 0);
  548. return NULL;
  549. }
  550. return entries[0];
  551. }
  552. static void modify_internal_entry(char *dn, LDAPMod **mods)
  553. {
  554. Slapi_PBlock pb;
  555. Slapi_Operation *op;
  556. int ret = 0;
  557. int tries = 0;
  558. int dont_write_file = 1;
  559. do {
  560. pblock_init(&pb);
  561. slapi_modify_internal_set_pb(&pb, dn, mods, NULL, NULL,
  562. (void *)plugin_get_default_component_id(), 0);
  563. /* all modifications to the cn=tasks subtree are transient --
  564. * we erase them all when the server starts up next time, so there's
  565. * no need to save them in the dse file.
  566. */
  567. slapi_pblock_set(&pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
  568. /* Make sure these mods are not logged in audit or changelog */
  569. slapi_pblock_get(&pb, SLAPI_OPERATION, &op);
  570. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  571. slapi_modify_internal_pb(&pb);
  572. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  573. if (ret != LDAP_SUCCESS) {
  574. /* could be waiting for another thread to finish adding this
  575. * entry -- try at least 3 times before giving up.
  576. */
  577. tries++;
  578. if (tries == 3) {
  579. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't modify task "
  580. "entry '%s'; %s (%d)\n", dn, ldap_err2string(ret), ret);
  581. pblock_done(&pb);
  582. return;
  583. }
  584. DS_Sleep(PR_SecondsToInterval(1));
  585. }
  586. pblock_done(&pb);
  587. } while (ret != LDAP_SUCCESS);
  588. }
  589. static void task_generic_destructor(Slapi_Task *task)
  590. {
  591. if (task->task_log) {
  592. slapi_ch_free((void **)&task->task_log);
  593. }
  594. if (task->task_status) {
  595. slapi_ch_free((void **)&task->task_status);
  596. }
  597. if (task->task_log_lock) {
  598. PR_DestroyLock(task->task_log_lock);
  599. task->task_log_lock = NULL;
  600. }
  601. task->task_log = task->task_status = NULL;
  602. }
  603. /********** actual task callbacks **********/
  604. static int task_deny(Slapi_PBlock *pb, Slapi_Entry *e,
  605. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  606. {
  607. /* internal operations (conn=NULL) are allowed to do whatever they want */
  608. if (pb->pb_conn == NULL) {
  609. *returncode = LDAP_SUCCESS;
  610. return SLAPI_DSE_CALLBACK_OK;
  611. }
  612. *returncode = LDAP_UNWILLING_TO_PERFORM;
  613. return SLAPI_DSE_CALLBACK_ERROR;
  614. }
  615. static int task_modify(Slapi_PBlock *pb, Slapi_Entry *e,
  616. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  617. {
  618. Slapi_Task *task = (Slapi_Task *)arg;
  619. LDAPMod **mods;
  620. int i;
  621. /* the connection block will be NULL for internal operations */
  622. if (pb->pb_conn == NULL) {
  623. *returncode = LDAP_SUCCESS;
  624. return SLAPI_DSE_CALLBACK_OK;
  625. }
  626. /* ignore eAfter, just scan the mods for anything unacceptable */
  627. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  628. for (i = 0; mods[i] != NULL; i++) {
  629. /* for some reason, "modifiersName" and "modifyTimestamp" are
  630. * stuck in by the server */
  631. if ((strcasecmp(mods[i]->mod_type, "ttl") != 0) &&
  632. (strcasecmp(mods[i]->mod_type, "nsTaskCancel") != 0) &&
  633. (strcasecmp(mods[i]->mod_type, "modifiersName") != 0) &&
  634. (strcasecmp(mods[i]->mod_type, "modifyTimestamp") != 0)) {
  635. /* you aren't allowed to change this! */
  636. *returncode = LDAP_UNWILLING_TO_PERFORM;
  637. return SLAPI_DSE_CALLBACK_ERROR;
  638. }
  639. }
  640. /* okay, we've decided to accept these changes. now look at the new
  641. * entry and absorb any new values.
  642. */
  643. if (strcasecmp(fetch_attr(eAfter, "nsTaskCancel", "false"), "true") == 0) {
  644. /* cancel this task, if not already */
  645. if (task->task_state != SLAPI_TASK_CANCELLED) {
  646. task->task_state = SLAPI_TASK_CANCELLED;
  647. if (task->cancel) {
  648. (*task->cancel)(task);
  649. LDAPDebug(LDAP_DEBUG_ANY, "Cancelling task '%s'\n",
  650. fetch_attr(eAfter, "cn", "?"), 0, 0);
  651. }
  652. }
  653. }
  654. /* we fetch ttl from the entry when it's needed */
  655. *returncode = LDAP_SUCCESS;
  656. return SLAPI_DSE_CALLBACK_OK;
  657. }
  658. static int task_import_add(Slapi_PBlock *pb, Slapi_Entry *e,
  659. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  660. {
  661. Slapi_Attr *attr;
  662. Slapi_Value *val = NULL;
  663. Slapi_Backend *be = NULL;
  664. const char *instance_name;
  665. char **ldif_file = NULL, **include = NULL, **exclude = NULL;
  666. int idx, rv = 0;
  667. const char *do_attr_indexes, *uniqueid_kind_str;
  668. int uniqueid_kind = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  669. Slapi_PBlock mypb;
  670. Slapi_Task *task;
  671. char *nameFrombe_name = NULL;
  672. const char *encrypt_on_import = NULL;
  673. if (fetch_attr(e, "cn", NULL) == NULL) {
  674. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  675. return SLAPI_DSE_CALLBACK_ERROR;
  676. }
  677. instance_name = fetch_attr(e, "nsInstance", NULL);
  678. encrypt_on_import = fetch_attr(e, "nsImportEncrypt", NULL);
  679. /* include/exclude suffixes */
  680. if (slapi_entry_attr_find(e, "nsIncludeSuffix", &attr) == 0) {
  681. for (idx = slapi_attr_first_value(attr, &val);
  682. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  683. charray_add(&include, slapi_ch_strdup(slapi_value_get_string(val)));
  684. }
  685. }
  686. if (slapi_entry_attr_find(e, "nsExcludeSuffix", &attr) == 0) {
  687. for (idx = slapi_attr_first_value(attr, &val);
  688. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  689. charray_add(&exclude, slapi_ch_strdup(slapi_value_get_string(val)));
  690. }
  691. }
  692. /*
  693. * if instance is given, just use it to get the backend.
  694. * otherwise, we use included/excluded suffix list to specify a backend.
  695. */
  696. if (NULL == instance_name) {
  697. char **instances, **ip;
  698. int counter;
  699. if (slapi_lookup_instance_name_by_suffixes(include, exclude,
  700. &instances) < 0) {
  701. LDAPDebug(LDAP_DEBUG_ANY,
  702. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  703. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  704. return SLAPI_DSE_CALLBACK_ERROR;
  705. }
  706. if (instances) {
  707. for (ip = instances, counter = 0; ip && *ip; ip++, counter++)
  708. ;
  709. if (counter == 1){
  710. instance_name = *instances;
  711. nameFrombe_name = *instances;
  712. }
  713. else if (counter == 0) {
  714. LDAPDebug(LDAP_DEBUG_ANY,
  715. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  716. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  717. return SLAPI_DSE_CALLBACK_ERROR;
  718. } else {
  719. LDAPDebug(LDAP_DEBUG_ANY,
  720. "ERROR: Multiple backend instances are specified: "
  721. "%s, %s, ...\n", instances[0], instances[1], 0);
  722. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  723. return SLAPI_DSE_CALLBACK_ERROR;
  724. }
  725. } else {
  726. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  727. return SLAPI_DSE_CALLBACK_ERROR;
  728. }
  729. }
  730. /* lookup the backend */
  731. be = slapi_be_select_by_instance_name(instance_name);
  732. if (be == NULL) {
  733. LDAPDebug(LDAP_DEBUG_ANY, "can't import to nonexistent backend %s\n",
  734. instance_name, 0, 0);
  735. slapi_ch_free_string(&nameFrombe_name);
  736. *returncode = LDAP_NO_SUCH_OBJECT;
  737. return SLAPI_DSE_CALLBACK_ERROR;
  738. }
  739. /* refuse to do an import on pre-V3 plugins. plugin api V3 is the one
  740. * for DS 5.0 where the import/export stuff changed a lot.
  741. */
  742. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  743. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an import with pre-V3 "
  744. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  745. *returncode = LDAP_UNWILLING_TO_PERFORM;
  746. slapi_ch_free_string(&nameFrombe_name);
  747. return SLAPI_DSE_CALLBACK_ERROR;
  748. }
  749. if (be->be_database->plg_ldif2db == NULL) {
  750. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no ldif2db function defined for "
  751. "backend %s\n", be->be_database->plg_name, 0, 0);
  752. *returncode = LDAP_UNWILLING_TO_PERFORM;
  753. slapi_ch_free_string(&nameFrombe_name);
  754. return SLAPI_DSE_CALLBACK_ERROR;
  755. }
  756. /* get ldif filenames -- from here on, memory has been allocated */
  757. if (slapi_entry_attr_find(e, "nsFilename", &attr) != 0) {
  758. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  759. slapi_ch_free_string(&nameFrombe_name);
  760. return SLAPI_DSE_CALLBACK_ERROR;
  761. }
  762. for (idx = slapi_attr_first_value(attr, &val);
  763. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  764. charray_add(&ldif_file, slapi_ch_strdup(slapi_value_get_string(val)));
  765. }
  766. do_attr_indexes = fetch_attr(e, "nsImportIndexAttrs", "true");
  767. uniqueid_kind_str = fetch_attr(e, "nsUniqueIdGenerator", NULL);
  768. if (uniqueid_kind_str != NULL) {
  769. if (strcasecmp(uniqueid_kind_str, "none") == 0) {
  770. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_NONE;
  771. } else if (strcasecmp(uniqueid_kind_str, "deterministic") == 0) {
  772. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_NAME_BASED;
  773. } else {
  774. /* default - time based */
  775. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  776. }
  777. }
  778. /* allocate new task now */
  779. task = slapi_new_task(slapi_entry_get_ndn(e));
  780. if (task == NULL) {
  781. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  782. rv = LDAP_OPERATIONS_ERROR;
  783. goto out;
  784. }
  785. memset(&mypb, 0, sizeof(mypb));
  786. mypb.pb_backend = be;
  787. mypb.pb_plugin = be->be_database;
  788. mypb.pb_removedupvals = atoi(fetch_attr(e, "nsImportChunkSize", "0"));
  789. mypb.pb_ldif2db_noattrindexes =
  790. !(strcasecmp(do_attr_indexes, "true") == 0);
  791. mypb.pb_ldif_generate_uniqueid = uniqueid_kind;
  792. mypb.pb_ldif_namespaceid =
  793. (char *)fetch_attr(e, "nsUniqueIdGeneratorNamespace", NULL);
  794. mypb.pb_instance_name = (char *)instance_name;
  795. mypb.pb_ldif_files = ldif_file;
  796. mypb.pb_ldif_include = include;
  797. mypb.pb_ldif_exclude = exclude;
  798. mypb.pb_task = task;
  799. mypb.pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  800. if (NULL != encrypt_on_import && 0 == strcasecmp(encrypt_on_import, "true") ) {
  801. mypb.pb_ldif_encrypt = 1;
  802. }
  803. rv = (*mypb.pb_plugin->plg_ldif2db)(&mypb);
  804. if (rv == 0) {
  805. slapi_entry_attr_set_charptr(e, TASK_LOG_NAME, "");
  806. slapi_entry_attr_set_charptr(e, TASK_STATUS_NAME, "");
  807. slapi_entry_attr_set_int(e, TASK_PROGRESS_NAME, task->task_progress);
  808. slapi_entry_attr_set_int(e, TASK_WORK_NAME, task->task_work);
  809. }
  810. out:
  811. slapi_ch_free_string(&nameFrombe_name);
  812. charray_free(ldif_file);
  813. charray_free(include);
  814. charray_free(exclude);
  815. if (rv != 0) {
  816. *returncode = LDAP_OPERATIONS_ERROR;
  817. destroy_task(1, task);
  818. return SLAPI_DSE_CALLBACK_ERROR;
  819. }
  820. *returncode = LDAP_SUCCESS;
  821. return SLAPI_DSE_CALLBACK_OK;
  822. }
  823. static void task_export_thread(void *arg)
  824. {
  825. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  826. char **instance_names = (char **)pb->pb_instance_name;
  827. char **inp;
  828. char *ldif_file = pb->pb_ldif_file;
  829. char *this_ldif_file = NULL;
  830. Slapi_Backend *be = NULL;
  831. int rv = -1;
  832. int count;
  833. Slapi_Task *task = pb->pb_task;
  834. g_incr_active_threadcnt();
  835. for (count = 0, inp = instance_names; *inp; inp++, count++)
  836. ;
  837. slapi_task_begin(task, count);
  838. for (inp = instance_names; *inp; inp++) {
  839. int release_me = 0;
  840. /* lookup the backend */
  841. be = slapi_be_select_by_instance_name((const char *)*inp);
  842. if (be == NULL) {
  843. /* shouldn't happen */
  844. LDAPDebug(LDAP_DEBUG_ANY, "ldbm2ldif: backend '%s' is AWOL!\n",
  845. (const char *)*inp, 0, 0);
  846. continue;
  847. }
  848. pb->pb_backend = be;
  849. pb->pb_plugin = be->be_database;
  850. pb->pb_instance_name = (char *)*inp;
  851. /* ldif_file name for each? */
  852. if (pb->pb_ldif_printkey & EXPORT_APPENDMODE) {
  853. if (inp == instance_names) { /* first export */
  854. pb->pb_ldif_printkey |= EXPORT_APPENDMODE_1;
  855. } else {
  856. pb->pb_ldif_printkey &= ~EXPORT_APPENDMODE_1;
  857. }
  858. } else {
  859. if (strcmp(ldif_file, "-")) { /* not '-' */
  860. char *p;
  861. #if defined( _WIN32 )
  862. char sep = '\\';
  863. if (NULL != strchr(ldif_file, '/'))
  864. sep = '/';
  865. #else
  866. char sep = '/';
  867. #endif
  868. this_ldif_file = (char *)slapi_ch_malloc(strlen(ldif_file) +
  869. strlen(*inp) + 2);
  870. p = strrchr(ldif_file, sep);
  871. if (NULL == p) {
  872. sprintf(this_ldif_file, "%s_%s", *inp, ldif_file);
  873. } else {
  874. char *q;
  875. q = p + 1;
  876. *p = '\0';
  877. sprintf(this_ldif_file, "%s%c%s_%s",
  878. ldif_file, sep, *inp, q);
  879. *p = sep;
  880. }
  881. pb->pb_ldif_file = this_ldif_file;
  882. release_me = 1;
  883. }
  884. }
  885. slapi_task_log_notice(task, "Beginning export of '%s'", *inp);
  886. LDAPDebug(LDAP_DEBUG_ANY, "Beginning export of '%s'\n", *inp, 0, 0);
  887. rv = (*pb->pb_plugin->plg_db2ldif)(pb);
  888. if (rv != 0) {
  889. slapi_task_log_notice(task, "backend '%s' export failed (%d)",
  890. *inp, rv);
  891. LDAPDebug(LDAP_DEBUG_ANY,
  892. "ldbm2ldif: backend '%s' export failed (%d)\n",
  893. (const char *)*inp, rv, 0);
  894. }
  895. if (release_me) {
  896. slapi_ch_free((void **)&this_ldif_file);
  897. }
  898. if (rv != 0)
  899. break;
  900. slapi_task_inc_progress(task);
  901. }
  902. /* free the memory now */
  903. charray_free(instance_names);
  904. slapi_ch_free((void **)&ldif_file);
  905. charray_free(pb->pb_ldif_include);
  906. charray_free(pb->pb_ldif_exclude);
  907. slapi_pblock_destroy(pb);
  908. if (rv == 0) {
  909. slapi_task_log_notice(task, "Export finished.");
  910. LDAPDebug(LDAP_DEBUG_ANY, "Export finished.\n", 0, 0, 0);
  911. } else {
  912. slapi_task_log_notice(task, "Export failed.");
  913. LDAPDebug(LDAP_DEBUG_ANY, "Export failed.\n", 0, 0, 0);
  914. }
  915. slapi_task_finish(task, rv);
  916. g_decr_active_threadcnt();
  917. }
  918. static int task_export_add(Slapi_PBlock *pb, Slapi_Entry *e,
  919. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  920. {
  921. Slapi_Attr *attr;
  922. Slapi_Value *val = NULL;
  923. Slapi_Backend *be = NULL;
  924. char *ldif_file = NULL;
  925. char **instance_names = NULL, **inp;
  926. char **include = NULL, **exclude = NULL;
  927. int idx, rv = SLAPI_DSE_CALLBACK_OK;
  928. int export_replica_flag = 0;
  929. int ldif_printkey_flag = 0;
  930. int dump_uniqueid_flag = 0;
  931. int instance_cnt = 0;
  932. const char *my_ldif_file;
  933. const char *use_one_file;
  934. const char *export_replica;
  935. const char *ldif_printkey;
  936. const char *dump_uniqueid;
  937. Slapi_PBlock *mypb = NULL;
  938. Slapi_Task *task = NULL;
  939. PRThread *thread;
  940. const char *decrypt_on_export = NULL;
  941. *returncode = LDAP_SUCCESS;
  942. if (fetch_attr(e, "cn", NULL) == NULL) {
  943. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  944. rv = SLAPI_DSE_CALLBACK_ERROR;
  945. goto out;
  946. }
  947. decrypt_on_export = fetch_attr(e, "nsExportDecrypt", NULL);
  948. /* nsInstances -- from here on, memory has been allocated */
  949. if (slapi_entry_attr_find(e, "nsInstance", &attr) == 0) {
  950. for (idx = slapi_attr_first_value(attr, &val);
  951. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  952. charray_add(&instance_names,
  953. slapi_ch_strdup(slapi_value_get_string(val)));
  954. instance_cnt++;
  955. }
  956. }
  957. /* include/exclude suffixes */
  958. if (slapi_entry_attr_find(e, "nsIncludeSuffix", &attr) == 0) {
  959. for (idx = slapi_attr_first_value(attr, &val);
  960. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  961. charray_add(&include, slapi_ch_strdup(slapi_value_get_string(val)));
  962. }
  963. }
  964. if (slapi_entry_attr_find(e, "nsExcludeSuffix", &attr) == 0) {
  965. for (idx = slapi_attr_first_value(attr, &val);
  966. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  967. charray_add(&exclude, slapi_ch_strdup(slapi_value_get_string(val)));
  968. }
  969. }
  970. if (NULL == instance_names) {
  971. char **ip;
  972. if (slapi_lookup_instance_name_by_suffixes(include, exclude,
  973. &instance_names) < 0) {
  974. LDAPDebug(LDAP_DEBUG_ANY,
  975. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  976. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  977. rv = SLAPI_DSE_CALLBACK_ERROR;
  978. goto out;
  979. }
  980. if (instance_names) {
  981. for (ip = instance_names, instance_cnt = 0; ip && *ip;
  982. ip++, instance_cnt++)
  983. ;
  984. if (instance_cnt == 0) {
  985. LDAPDebug(LDAP_DEBUG_ANY,
  986. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  987. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  988. rv = SLAPI_DSE_CALLBACK_ERROR;
  989. goto out;
  990. }
  991. } else {
  992. LDAPDebug(LDAP_DEBUG_ANY,
  993. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  994. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  995. rv = SLAPI_DSE_CALLBACK_ERROR;
  996. goto out;
  997. }
  998. }
  999. /* ldif file name */
  1000. if ((my_ldif_file = fetch_attr(e, "nsFilename", NULL)) == NULL) {
  1001. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1002. rv = SLAPI_DSE_CALLBACK_ERROR;
  1003. goto out;
  1004. }
  1005. ldif_file = slapi_ch_strdup(my_ldif_file);
  1006. /* if true, multiple backends are dumped into one ldif file */
  1007. use_one_file = fetch_attr(e, "nsUseOneFile", "true");
  1008. if (strcasecmp(use_one_file, "true") == 0) {
  1009. ldif_printkey_flag |= EXPORT_APPENDMODE;
  1010. }
  1011. /* -r: export replica */
  1012. export_replica = fetch_attr(e, "nsExportReplica", "false");
  1013. if (!strcasecmp(export_replica, "true")) /* true */
  1014. export_replica_flag = 1;
  1015. /* -N: eq "false" ==> does not print out key value */
  1016. ldif_printkey = fetch_attr(e, "nsPrintKey", "true");
  1017. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1018. ldif_printkey_flag |= EXPORT_PRINTKEY;
  1019. /* -C: eq "true" ==> use only id2entry file */
  1020. ldif_printkey = fetch_attr(e, "nsUseId2Entry", "false");
  1021. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1022. ldif_printkey_flag |= EXPORT_ID2ENTRY_ONLY;
  1023. /* if "true" ==> 8-bit strings are not base64 encoded */
  1024. ldif_printkey = fetch_attr(e, "nsMinimalEncoding", "false");
  1025. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1026. ldif_printkey_flag |= EXPORT_MINIMAL_ENCODING;
  1027. /* -U: eq "true" ==> does not fold the output */
  1028. ldif_printkey = fetch_attr(e, "nsNoWrap", "false");
  1029. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1030. ldif_printkey_flag |= EXPORT_NOWRAP;
  1031. /* -1: eq "true" ==> does not print version line */
  1032. ldif_printkey = fetch_attr(e, "nsNoVersionLine", "false");
  1033. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1034. ldif_printkey_flag |= EXPORT_NOVERSION;
  1035. /* -u: eq "false" ==> does not dump unique id */
  1036. dump_uniqueid = fetch_attr(e, "nsDumpUniqId", "true");
  1037. if (!strcasecmp(dump_uniqueid, "true")) /* true */
  1038. dump_uniqueid_flag = 1;
  1039. /* check that all the backends are ok */
  1040. for (inp = instance_names; *inp; inp++) {
  1041. /* lookup the backend */
  1042. be = slapi_be_select_by_instance_name((const char *)*inp);
  1043. if (be == NULL) {
  1044. LDAPDebug(LDAP_DEBUG_ANY,
  1045. "can't export to nonexistent backend %s\n", *inp, 0, 0);
  1046. *returncode = LDAP_NO_SUCH_OBJECT;
  1047. rv = SLAPI_DSE_CALLBACK_ERROR;
  1048. goto out;
  1049. }
  1050. /* refuse to do an export on pre-V3 plugins. plugin api V3 is the one
  1051. * for DS 5.0 where the import/export stuff changed a lot.
  1052. */
  1053. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1054. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an export with pre-V3 "
  1055. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1056. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1057. rv = SLAPI_DSE_CALLBACK_ERROR;
  1058. goto out;
  1059. }
  1060. if (be->be_database->plg_db2ldif == NULL) {
  1061. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no db2ldif function defined for "
  1062. "backend %s\n", be->be_database->plg_name, 0, 0);
  1063. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1064. rv = SLAPI_DSE_CALLBACK_ERROR;
  1065. goto out;
  1066. }
  1067. }
  1068. /* allocate new task now */
  1069. task = slapi_new_task(slapi_entry_get_ndn(e));
  1070. if (task == NULL) {
  1071. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1072. *returncode = LDAP_OPERATIONS_ERROR;
  1073. rv = SLAPI_DSE_CALLBACK_ERROR;
  1074. goto out;
  1075. }
  1076. mypb = slapi_pblock_new();
  1077. if (mypb == NULL) {
  1078. *returncode = LDAP_OPERATIONS_ERROR;
  1079. rv = SLAPI_DSE_CALLBACK_ERROR;
  1080. goto out;
  1081. }
  1082. mypb->pb_ldif_include = include;
  1083. mypb->pb_ldif_exclude = exclude;
  1084. mypb->pb_ldif_printkey = ldif_printkey_flag;
  1085. mypb->pb_ldif_dump_replica = export_replica_flag;
  1086. mypb->pb_ldif_dump_uniqueid = dump_uniqueid_flag;
  1087. mypb->pb_ldif_file = ldif_file;
  1088. /* horrible hack */
  1089. mypb->pb_instance_name = (char *)instance_names;
  1090. mypb->pb_task = task;
  1091. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1092. if (NULL != decrypt_on_export && 0 == strcasecmp(decrypt_on_export, "true") ) {
  1093. mypb->pb_ldif_encrypt = 1;
  1094. }
  1095. /* start the export as a separate thread */
  1096. thread = PR_CreateThread(PR_USER_THREAD, task_export_thread,
  1097. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1098. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1099. if (thread == NULL) {
  1100. LDAPDebug(LDAP_DEBUG_ANY,
  1101. "unable to create ldbm2ldif thread!\n", 0, 0, 0);
  1102. *returncode = LDAP_OPERATIONS_ERROR;
  1103. rv = SLAPI_DSE_CALLBACK_ERROR;
  1104. slapi_pblock_destroy(mypb);
  1105. goto out;
  1106. }
  1107. /* thread successful -- don't free the pb, let the thread do that. */
  1108. return SLAPI_DSE_CALLBACK_OK;
  1109. out:
  1110. charray_free(instance_names);
  1111. charray_free(include);
  1112. charray_free(exclude);
  1113. if (ldif_file != NULL) {
  1114. slapi_ch_free((void **)&ldif_file);
  1115. }
  1116. if (task) {
  1117. destroy_task(1, task);
  1118. }
  1119. return rv;
  1120. }
  1121. static void task_backup_thread(void *arg)
  1122. {
  1123. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1124. Slapi_Task *task = pb->pb_task;
  1125. int rv;
  1126. g_incr_active_threadcnt();
  1127. slapi_task_begin(task, 1);
  1128. slapi_task_log_notice(task, "Beginning backup of '%s'",
  1129. pb->pb_plugin->plg_name);
  1130. LDAPDebug(LDAP_DEBUG_ANY, "Beginning backup of '%s'\n",
  1131. pb->pb_plugin->plg_name, 0, 0);
  1132. rv = (*pb->pb_plugin->plg_db2archive)(pb);
  1133. if (rv != 0) {
  1134. slapi_task_log_notice(task, "Backup failed (error %d)", rv);
  1135. slapi_task_log_status(task, "Backup failed (error %d)", rv);
  1136. LDAPDebug(LDAP_DEBUG_ANY, "Backup failed (error %d)\n", rv, 0, 0);
  1137. } else {
  1138. slapi_task_log_notice(task, "Backup finished.");
  1139. slapi_task_log_status(task, "Backup finished.");
  1140. LDAPDebug(LDAP_DEBUG_ANY, "Backup finished.\n", 0, 0, 0);
  1141. }
  1142. slapi_task_finish(task, rv);
  1143. slapi_ch_free((void **)&pb->pb_seq_val);
  1144. slapi_pblock_destroy(pb);
  1145. g_decr_active_threadcnt();
  1146. }
  1147. static int task_backup_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1148. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1149. {
  1150. Slapi_Backend *be = NULL;
  1151. PRThread *thread = NULL;
  1152. const char *archive_dir = NULL;
  1153. const char *my_database_type = NULL;
  1154. const char *database_type = "ldbm database";
  1155. char *cookie = NULL;
  1156. int rv = SLAPI_DSE_CALLBACK_OK;
  1157. Slapi_PBlock *mypb = NULL;
  1158. Slapi_Task *task = NULL;
  1159. *returncode = LDAP_SUCCESS;
  1160. if (fetch_attr(e, "cn", NULL) == NULL) {
  1161. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1162. rv = SLAPI_DSE_CALLBACK_ERROR;
  1163. goto out;
  1164. }
  1165. /* archive dir name */
  1166. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1167. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1168. rv = SLAPI_DSE_CALLBACK_ERROR;
  1169. goto out;
  1170. }
  1171. /* database type */
  1172. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1173. if (NULL != my_database_type)
  1174. database_type = my_database_type;
  1175. /* get backend that has db2archive and the database type matches. */
  1176. cookie = NULL;
  1177. be = slapi_get_first_backend(&cookie);
  1178. while (be) {
  1179. if (NULL != be->be_database->plg_db2archive &&
  1180. !strcasecmp(database_type, be->be_database->plg_name))
  1181. break;
  1182. be = (backend *)slapi_get_next_backend (cookie);
  1183. }
  1184. slapi_ch_free((void **)&cookie);
  1185. if (NULL == be || NULL == be->be_database->plg_db2archive) {
  1186. LDAPDebug(LDAP_DEBUG_ANY,
  1187. "ERROR: no db2archive function defined.\n", 0, 0, 0);
  1188. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1189. rv = SLAPI_DSE_CALLBACK_ERROR;
  1190. goto out;
  1191. }
  1192. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1193. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an backup with pre-V3 "
  1194. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1195. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1196. rv = SLAPI_DSE_CALLBACK_ERROR;
  1197. goto out;
  1198. }
  1199. /* allocate new task now */
  1200. task = slapi_new_task(slapi_entry_get_ndn(e));
  1201. if (task == NULL) {
  1202. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1203. *returncode = LDAP_OPERATIONS_ERROR;
  1204. rv = SLAPI_DSE_CALLBACK_ERROR;
  1205. goto out;
  1206. }
  1207. mypb = slapi_pblock_new();
  1208. if (mypb == NULL) {
  1209. *returncode = LDAP_OPERATIONS_ERROR;
  1210. rv = SLAPI_DSE_CALLBACK_ERROR;
  1211. goto out;
  1212. }
  1213. mypb->pb_seq_val = slapi_ch_strdup(archive_dir);
  1214. mypb->pb_plugin = be->be_database;
  1215. mypb->pb_task = task;
  1216. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1217. /* start the backup as a separate thread */
  1218. thread = PR_CreateThread(PR_USER_THREAD, task_backup_thread,
  1219. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1220. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1221. if (thread == NULL) {
  1222. LDAPDebug(LDAP_DEBUG_ANY,
  1223. "unable to create backup thread!\n", 0, 0, 0);
  1224. *returncode = LDAP_OPERATIONS_ERROR;
  1225. rv = SLAPI_DSE_CALLBACK_ERROR;
  1226. slapi_ch_free((void **)&mypb->pb_seq_val);
  1227. slapi_pblock_destroy(mypb);
  1228. goto out;
  1229. }
  1230. /* thread successful -- don't free the pb, let the thread do that. */
  1231. return SLAPI_DSE_CALLBACK_OK;
  1232. out:
  1233. if (task) {
  1234. destroy_task(1, task);
  1235. }
  1236. return rv;
  1237. }
  1238. static void task_restore_thread(void *arg)
  1239. {
  1240. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1241. Slapi_Task *task = pb->pb_task;
  1242. int rv;
  1243. g_incr_active_threadcnt();
  1244. slapi_task_begin(task, 1);
  1245. slapi_task_log_notice(task, "Beginning restore to '%s'",
  1246. pb->pb_plugin->plg_name);
  1247. LDAPDebug(LDAP_DEBUG_ANY, "Beginning restore to '%s'\n",
  1248. pb->pb_plugin->plg_name, 0, 0);
  1249. rv = (*pb->pb_plugin->plg_archive2db)(pb);
  1250. if (rv != 0) {
  1251. slapi_task_log_notice(task, "Restore failed (error %d)", rv);
  1252. slapi_task_log_status(task, "Restore failed (error %d)", rv);
  1253. LDAPDebug(LDAP_DEBUG_ANY, "Restore failed (error %d)\n", rv, 0, 0);
  1254. } else {
  1255. slapi_task_log_notice(task, "Restore finished.");
  1256. slapi_task_log_status(task, "Restore finished.");
  1257. LDAPDebug(LDAP_DEBUG_ANY, "Restore finished.\n", 0, 0, 0);
  1258. }
  1259. slapi_task_finish(task, rv);
  1260. slapi_ch_free((void **)&pb->pb_seq_val);
  1261. slapi_pblock_destroy(pb);
  1262. g_decr_active_threadcnt();
  1263. }
  1264. static int task_restore_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1265. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1266. {
  1267. Slapi_Backend *be = NULL;
  1268. const char *instance_name = NULL;
  1269. const char *archive_dir = NULL;
  1270. const char *my_database_type = NULL;
  1271. const char *database_type = "ldbm database";
  1272. char *cookie = NULL;
  1273. int rv = SLAPI_DSE_CALLBACK_OK;
  1274. Slapi_PBlock *mypb = NULL;
  1275. Slapi_Task *task = NULL;
  1276. PRThread *thread = NULL;
  1277. *returncode = LDAP_SUCCESS;
  1278. if (fetch_attr(e, "cn", NULL) == NULL) {
  1279. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1280. rv = SLAPI_DSE_CALLBACK_ERROR;
  1281. goto out;
  1282. }
  1283. /* archive dir name */
  1284. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1285. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1286. rv = SLAPI_DSE_CALLBACK_ERROR;
  1287. goto out;
  1288. }
  1289. /* database type */
  1290. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1291. if (NULL != my_database_type)
  1292. database_type = my_database_type;
  1293. instance_name = fetch_attr(e, "nsInstance", NULL);
  1294. /* get backend that has archive2db and the database type matches. */
  1295. cookie = NULL;
  1296. be = slapi_get_first_backend (&cookie);
  1297. while (be) {
  1298. if (NULL != be->be_database->plg_archive2db &&
  1299. !strcasecmp(database_type, be->be_database->plg_name))
  1300. break;
  1301. be = (backend *)slapi_get_next_backend (cookie);
  1302. }
  1303. slapi_ch_free((void **)&cookie);
  1304. if (NULL == be || NULL == be->be_database->plg_archive2db) {
  1305. LDAPDebug(LDAP_DEBUG_ANY,
  1306. "ERROR: no db2archive function defined.\n", 0, 0, 0);
  1307. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1308. rv = SLAPI_DSE_CALLBACK_ERROR;
  1309. goto out;
  1310. }
  1311. /* refuse to do an export on pre-V3 plugins. plugin api V3 is the one
  1312. * for DS 5.0 where the import/export stuff changed a lot.
  1313. */
  1314. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1315. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an restore with pre-V3 "
  1316. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1317. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1318. rv = SLAPI_DSE_CALLBACK_ERROR;
  1319. goto out;
  1320. }
  1321. /* allocate new task now */
  1322. task = slapi_new_task(slapi_entry_get_ndn(e));
  1323. if (task == NULL) {
  1324. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1325. *returncode = LDAP_OPERATIONS_ERROR;
  1326. rv = SLAPI_DSE_CALLBACK_ERROR;
  1327. goto out;
  1328. }
  1329. mypb = slapi_pblock_new();
  1330. if (mypb == NULL) {
  1331. *returncode = LDAP_OPERATIONS_ERROR;
  1332. rv = SLAPI_DSE_CALLBACK_ERROR;
  1333. goto out;
  1334. }
  1335. mypb->pb_seq_val = slapi_ch_strdup(archive_dir);
  1336. mypb->pb_plugin = be->be_database;
  1337. if (NULL != instance_name)
  1338. mypb->pb_instance_name = slapi_ch_strdup(instance_name);
  1339. mypb->pb_task = task;
  1340. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1341. /* start the restore as a separate thread */
  1342. thread = PR_CreateThread(PR_USER_THREAD, task_restore_thread,
  1343. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1344. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1345. if (thread == NULL) {
  1346. LDAPDebug(LDAP_DEBUG_ANY,
  1347. "unable to create restore thread!\n", 0, 0, 0);
  1348. *returncode = LDAP_OPERATIONS_ERROR;
  1349. rv = SLAPI_DSE_CALLBACK_ERROR;
  1350. slapi_ch_free((void **)&mypb->pb_seq_val);
  1351. slapi_pblock_destroy(mypb);
  1352. goto out;
  1353. }
  1354. /* thread successful -- don't free the pb, let the thread do that. */
  1355. return SLAPI_DSE_CALLBACK_OK;
  1356. out:
  1357. if (task) {
  1358. destroy_task(1, task);
  1359. }
  1360. return rv;
  1361. }
  1362. static void task_index_thread(void *arg)
  1363. {
  1364. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1365. Slapi_Task *task = pb->pb_task;
  1366. int rv;
  1367. g_incr_active_threadcnt();
  1368. slapi_task_begin(task, 1);
  1369. rv = (*pb->pb_plugin->plg_db2index)(pb);
  1370. if (rv != 0) {
  1371. slapi_task_log_notice(task, "Index failed (error %d)", rv);
  1372. slapi_task_log_status(task, "Index failed (error %d)", rv);
  1373. LDAPDebug(LDAP_DEBUG_ANY, "Index failed (error %d)\n", rv, 0, 0);
  1374. }
  1375. slapi_task_finish(task, rv);
  1376. charray_free(pb->pb_db2index_attrs);
  1377. slapi_ch_free((void **)&pb->pb_instance_name);
  1378. slapi_pblock_destroy(pb);
  1379. g_decr_active_threadcnt();
  1380. }
  1381. static int task_index_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1382. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1383. {
  1384. const char *instance_name;
  1385. int rv = SLAPI_DSE_CALLBACK_OK;
  1386. Slapi_Backend *be = NULL;
  1387. Slapi_Task *task = NULL;
  1388. Slapi_Attr *attr;
  1389. Slapi_Value *val = NULL;
  1390. char **indexlist = NULL;
  1391. int idx;
  1392. Slapi_PBlock *mypb = NULL;
  1393. PRThread *thread = NULL;
  1394. *returncode = LDAP_SUCCESS;
  1395. if (fetch_attr(e, "cn", NULL) == NULL) {
  1396. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1397. rv = SLAPI_DSE_CALLBACK_ERROR;
  1398. goto out;
  1399. }
  1400. if ((instance_name = fetch_attr(e, "nsInstance", NULL)) == NULL) {
  1401. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1402. rv = SLAPI_DSE_CALLBACK_ERROR;
  1403. goto out;
  1404. }
  1405. /* lookup the backend */
  1406. be = slapi_be_select_by_instance_name(instance_name);
  1407. if (be == NULL) {
  1408. LDAPDebug(LDAP_DEBUG_ANY, "can't import to nonexistent backend %s\n",
  1409. instance_name, 0, 0);
  1410. *returncode = LDAP_NO_SUCH_OBJECT;
  1411. return SLAPI_DSE_CALLBACK_ERROR;
  1412. }
  1413. if (be->be_database->plg_db2index == NULL) {
  1414. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no db2index function defined for "
  1415. "backend %s\n", be->be_database->plg_name, 0, 0);
  1416. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1417. return SLAPI_DSE_CALLBACK_ERROR;
  1418. }
  1419. /* normal indexes */
  1420. if (slapi_entry_attr_find(e, "nsIndexAttribute", &attr) == 0) {
  1421. for (idx = slapi_attr_first_value(attr, &val);
  1422. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1423. const char *indexname = slapi_value_get_string(val);
  1424. char *index = slapi_ch_smprintf("t%s", indexname);
  1425. if (index != NULL) {
  1426. charray_add(&indexlist, index);
  1427. }
  1428. }
  1429. }
  1430. /* vlv indexes */
  1431. if (slapi_entry_attr_find(e, "nsIndexVlvAttribute", &attr) == 0) {
  1432. for (idx = slapi_attr_first_value(attr, &val);
  1433. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1434. const char *indexname = slapi_value_get_string(val);
  1435. char *index = slapi_ch_smprintf("T%s", indexname);
  1436. if (index != NULL) {
  1437. charray_add(&indexlist, index);
  1438. }
  1439. }
  1440. }
  1441. if (NULL == indexlist) {
  1442. LDAPDebug(LDAP_DEBUG_ANY, "no index is specified!\n", 0, 0, 0);
  1443. *returncode = LDAP_OPERATIONS_ERROR;
  1444. rv = SLAPI_DSE_CALLBACK_OK;
  1445. goto out;
  1446. }
  1447. /* allocate new task now */
  1448. task = slapi_new_task(slapi_entry_get_ndn(e));
  1449. if (task == NULL) {
  1450. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1451. *returncode = LDAP_OPERATIONS_ERROR;
  1452. rv = SLAPI_DSE_CALLBACK_ERROR;
  1453. goto out;
  1454. }
  1455. mypb = slapi_pblock_new();
  1456. if (mypb == NULL) {
  1457. *returncode = LDAP_OPERATIONS_ERROR;
  1458. rv = SLAPI_DSE_CALLBACK_ERROR;
  1459. goto out;
  1460. }
  1461. mypb->pb_backend = be;
  1462. mypb->pb_plugin = be->be_database;
  1463. mypb->pb_instance_name = slapi_ch_strdup(instance_name);
  1464. mypb->pb_db2index_attrs = indexlist;
  1465. mypb->pb_task = task;
  1466. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1467. /* start the db2index as a separate thread */
  1468. thread = PR_CreateThread(PR_USER_THREAD, task_index_thread,
  1469. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1470. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1471. if (thread == NULL) {
  1472. LDAPDebug(LDAP_DEBUG_ANY,
  1473. "unable to create index thread!\n", 0, 0, 0);
  1474. rv = SLAPI_DSE_CALLBACK_ERROR;
  1475. slapi_ch_free((void **)&mypb->pb_instance_name);
  1476. slapi_pblock_destroy(mypb);
  1477. goto out;
  1478. }
  1479. /* thread successful -- don't free the pb, let the thread do that. */
  1480. return SLAPI_DSE_CALLBACK_OK;
  1481. out:
  1482. if (task) {
  1483. destroy_task(1, task);
  1484. }
  1485. if (indexlist) {
  1486. charray_free(indexlist);
  1487. }
  1488. return rv;
  1489. }
  1490. static int
  1491. task_upgradedb_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1492. int *returncode, char *returntext, void *arg)
  1493. {
  1494. int rv = SLAPI_DSE_CALLBACK_OK;
  1495. Slapi_Backend *be = NULL;
  1496. Slapi_Task *task = NULL;
  1497. Slapi_PBlock mypb;
  1498. const char *archive_dir = NULL;
  1499. const char *force = NULL;
  1500. const char *database_type = "ldbm database";
  1501. const char *my_database_type = NULL;
  1502. char *cookie = NULL;
  1503. *returncode = LDAP_SUCCESS;
  1504. if (fetch_attr(e, "cn", NULL) == NULL) {
  1505. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1506. rv = SLAPI_DSE_CALLBACK_ERROR;
  1507. goto out;
  1508. }
  1509. /* archive dir name */
  1510. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1511. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1512. rv = SLAPI_DSE_CALLBACK_ERROR;
  1513. goto out;
  1514. }
  1515. /* database type */
  1516. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1517. if (NULL != my_database_type)
  1518. database_type = my_database_type;
  1519. /* force to reindex? */
  1520. force = fetch_attr(e, "nsForceToReindex", NULL);
  1521. /* get backend that has db2archive and the database type matches. */
  1522. cookie = NULL;
  1523. be = slapi_get_first_backend(&cookie);
  1524. while (be) {
  1525. if (NULL != be->be_database->plg_upgradedb)
  1526. break;
  1527. be = (backend *)slapi_get_next_backend (cookie);
  1528. }
  1529. slapi_ch_free((void **)&cookie);
  1530. if (NULL == be || NULL == be->be_database->plg_upgradedb ||
  1531. strcasecmp(database_type, be->be_database->plg_name)) {
  1532. LDAPDebug(LDAP_DEBUG_ANY,
  1533. "ERROR: no upgradedb is defined in %s.\n",
  1534. be->be_database->plg_name, 0, 0);
  1535. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1536. rv = SLAPI_DSE_CALLBACK_ERROR;
  1537. goto out;
  1538. }
  1539. /* allocate new task now */
  1540. task = slapi_new_task(slapi_entry_get_ndn(e));
  1541. if (task == NULL) {
  1542. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1543. *returncode = LDAP_OPERATIONS_ERROR;
  1544. rv = SLAPI_DSE_CALLBACK_ERROR;
  1545. goto out;
  1546. }
  1547. /* NGK - This could use some cleanup to use the SLAPI task API, such as slapi_task_begin() */
  1548. task->task_work = 1;
  1549. task->task_progress = 0;
  1550. memset(&mypb, 0, sizeof(mypb));
  1551. mypb.pb_backend = be;
  1552. mypb.pb_plugin = be->be_database;
  1553. if (force && 0 == strcasecmp(force, "true"))
  1554. mypb.pb_seq_type = SLAPI_UPGRADEDB_FORCE; /* force; reindex all regardless the dbversion */
  1555. mypb.pb_seq_val = slapi_ch_strdup(archive_dir);
  1556. mypb.pb_task = task;
  1557. mypb.pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1558. rv = (mypb.pb_plugin->plg_upgradedb)(&mypb);
  1559. if (rv == 0) {
  1560. slapi_entry_attr_set_charptr(e, TASK_LOG_NAME, "");
  1561. slapi_entry_attr_set_charptr(e, TASK_STATUS_NAME, "");
  1562. slapi_entry_attr_set_int(e, TASK_PROGRESS_NAME, task->task_progress);
  1563. slapi_entry_attr_set_int(e, TASK_WORK_NAME, task->task_work);
  1564. }
  1565. out:
  1566. slapi_ch_free((void **)&mypb.pb_seq_val);
  1567. if (rv != 0) {
  1568. if (task)
  1569. destroy_task(1, task);
  1570. *returncode = LDAP_OPERATIONS_ERROR;
  1571. return SLAPI_DSE_CALLBACK_ERROR;
  1572. }
  1573. *returncode = LDAP_SUCCESS;
  1574. return SLAPI_DSE_CALLBACK_OK;
  1575. }
  1576. /* cleanup old tasks that may still be in the DSE from a previous session
  1577. * (this can happen if the server crashes [no matter how unlikely we like
  1578. * to think that is].)
  1579. */
  1580. void task_cleanup(void)
  1581. {
  1582. Slapi_PBlock *pb = slapi_pblock_new();
  1583. Slapi_Entry **entries = NULL;
  1584. int ret = 0, i, x;
  1585. Slapi_DN *rootDN;
  1586. slapi_search_internal_set_pb(pb, TASK_BASE_DN, LDAP_SCOPE_SUBTREE,
  1587. "(objectclass=*)", NULL, 0, NULL, NULL,
  1588. (void *)plugin_get_default_component_id(), 0);
  1589. slapi_search_internal_pb(pb);
  1590. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1591. if (ret != LDAP_SUCCESS) {
  1592. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: entire cn=tasks tree seems to "
  1593. "be AWOL!\n", 0, 0, 0);
  1594. slapi_pblock_destroy(pb);
  1595. return;
  1596. }
  1597. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1598. if (NULL == entries) {
  1599. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: entire cn=tasks tree seems to "
  1600. "be AWOL!\n", 0, 0, 0);
  1601. slapi_pblock_destroy(pb);
  1602. return;
  1603. }
  1604. rootDN = slapi_sdn_new_dn_byval(TASK_BASE_DN);
  1605. /* rotate through entries, skipping the base dn */
  1606. for (i = 0; entries[i] != NULL; i++) {
  1607. const Slapi_DN *sdn = slapi_entry_get_sdn_const(entries[i]);
  1608. Slapi_PBlock *mypb;
  1609. Slapi_Operation *op;
  1610. if (slapi_sdn_compare(sdn, rootDN) == 0)
  1611. continue;
  1612. mypb = slapi_pblock_new();
  1613. if (mypb == NULL) {
  1614. continue;
  1615. }
  1616. slapi_delete_internal_set_pb(mypb, slapi_sdn_get_dn(sdn), NULL, NULL,
  1617. plugin_get_default_component_id(), 0);
  1618. /* Make sure these deletes don't appear in the audit and change logs */
  1619. slapi_pblock_get(mypb, SLAPI_OPERATION, &op);
  1620. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  1621. x = 1;
  1622. slapi_pblock_set(mypb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &x);
  1623. slapi_delete_internal_pb(mypb);
  1624. slapi_pblock_destroy(mypb);
  1625. }
  1626. slapi_sdn_free(&rootDN);
  1627. slapi_free_search_results_internal(pb);
  1628. slapi_pblock_destroy(pb);
  1629. }
  1630. void task_init(void)
  1631. {
  1632. global_task_lock = PR_NewLock();
  1633. if (global_task_lock == NULL) {
  1634. LDAPDebug(LDAP_DEBUG_ANY, "unable to create global tasks lock! "
  1635. "(that's bad)\n", 0, 0, 0);
  1636. return;
  1637. }
  1638. slapi_task_register_handler("import", task_import_add);
  1639. slapi_task_register_handler("export", task_export_add);
  1640. slapi_task_register_handler("backup", task_backup_add);
  1641. slapi_task_register_handler("restore", task_restore_add);
  1642. slapi_task_register_handler("index", task_index_add);
  1643. slapi_task_register_handler("upgradedb", task_upgradedb_add);
  1644. }
  1645. /* called when the server is shutting down -- abort all existing tasks */
  1646. void task_shutdown(void)
  1647. {
  1648. Slapi_Task *task;
  1649. int found_any = 0;
  1650. /* first, cancel all tasks */
  1651. PR_Lock(global_task_lock);
  1652. shutting_down = 1;
  1653. for (task = global_task_list; task; task = task->next) {
  1654. if ((task->task_state != SLAPI_TASK_CANCELLED) &&
  1655. (task->task_state != SLAPI_TASK_FINISHED)) {
  1656. task->task_state = SLAPI_TASK_CANCELLED;
  1657. if (task->cancel) {
  1658. LDAPDebug(LDAP_DEBUG_ANY, "Cancelling task '%s'\n",
  1659. task->task_dn, 0, 0);
  1660. (*task->cancel)(task);
  1661. found_any = 1;
  1662. }
  1663. }
  1664. }
  1665. if (found_any) {
  1666. /* give any tasks 1 second to say their last rites */
  1667. DS_Sleep(PR_SecondsToInterval( 1 ));
  1668. }
  1669. while (global_task_list) {
  1670. destroy_task(0, global_task_list);
  1671. }
  1672. PR_Unlock(global_task_lock);
  1673. }