task.c 62 KB

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