task.c 57 KB

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