task.c 57 KB

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