task.c 57 KB

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