task.c 61 KB

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