task.c 62 KB

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