task.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * directory online tasks (import, export, backup, restore)
  14. */
  15. #include "slap.h"
  16. /***********************************
  17. * Static Global Variables
  18. ***********************************/
  19. /* don't panic, this is only used when creating new tasks or removing old
  20. * ones...
  21. */
  22. static Slapi_Task *global_task_list = NULL;
  23. static PRLock *global_task_lock = NULL;
  24. static int shutting_down = 0;
  25. /***********************************
  26. * Private Defines
  27. ***********************************/
  28. #define TASK_BASE_DN "cn=tasks,cn=config"
  29. #define TASK_IMPORT_DN "cn=import,cn=tasks,cn=config"
  30. #define TASK_EXPORT_DN "cn=export,cn=tasks,cn=config"
  31. #define TASK_BACKUP_DN "cn=backup,cn=tasks,cn=config"
  32. #define TASK_RESTORE_DN "cn=restore,cn=tasks,cn=config"
  33. #define TASK_INDEX_DN "cn=index,cn=tasks,cn=config"
  34. #define TASK_UPGRADEDB_DN "cn=upgradedb,cn=tasks,cn=config"
  35. #define TASK_TOMBSTONE_FIXUP_DN "cn=fixup tombstones,cn=tasks,cn=config"
  36. #define TASK_LOG_NAME "nsTaskLog"
  37. #define TASK_STATUS_NAME "nsTaskStatus"
  38. #define TASK_EXITCODE_NAME "nsTaskExitCode"
  39. #define TASK_PROGRESS_NAME "nsTaskCurrentItem"
  40. #define TASK_WORK_NAME "nsTaskTotalItems"
  41. #define DEFAULT_TTL "120" /* seconds */
  42. #define TASK_SYSCONFIG_FILE_ATTR "sysconfigfile" /* sysconfig reload task file attr */
  43. #define TASK_SYSCONFIG_LOGCHANGES_ATTR "logchanges"
  44. #define TASK_TOMBSTONE_FIXUP "fixup tombstones task"
  45. #define TASK_TOMBSTONE_FIXUP_BACKEND "backend"
  46. #define TASK_TOMBSTONE_FIXUP_SUFFIX "suffix"
  47. #define TASK_TOMBSTONE_FIXUP_STRIPCSN "stripcsn"
  48. #define LOG_BUFFER 256
  49. /* if the cumul. log gets larger than this, it's truncated: */
  50. #define MAX_SCROLLBACK_BUFFER 8192
  51. #define NEXTMOD(_type, _val) do { \
  52. modlist[cur].mod_op = LDAP_MOD_REPLACE; \
  53. modlist[cur].mod_type = (_type); \
  54. modlist[cur].mod_values = (char **)slapi_ch_malloc(2*sizeof(char *)); \
  55. modlist[cur].mod_values[0] = (_val); \
  56. modlist[cur].mod_values[1] = NULL; \
  57. mod[cur] = &modlist[cur]; \
  58. cur++; \
  59. } while (0)
  60. /***********************************
  61. * Static Function Prototypes
  62. ***********************************/
  63. static Slapi_Task *new_task(const char *dn, void *plugin);
  64. static void destroy_task(time_t when, void *arg);
  65. static int task_modify(Slapi_PBlock *pb, Slapi_Entry *e,
  66. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  67. static int task_deny(Slapi_PBlock *pb, Slapi_Entry *e,
  68. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  69. static void task_generic_destructor(Slapi_Task *task);
  70. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  71. const char *default_val);
  72. static Slapi_Entry *get_internal_entry(Slapi_PBlock *pb, char *dn);
  73. static void modify_internal_entry(char *dn, LDAPMod **mods);
  74. static void fixup_tombstone_task_destructor(Slapi_Task *task);
  75. /***********************************
  76. * Public Functions
  77. ***********************************/
  78. /*
  79. * slapi_new_task: create a new task, fill in DN, and setup modify callback
  80. * argument:
  81. * dn: task dn
  82. * result:
  83. * Success: Slapi_Task object
  84. * Failure: NULL
  85. */
  86. Slapi_Task *
  87. slapi_new_task(const char *dn)
  88. {
  89. return new_task(dn, NULL);
  90. }
  91. Slapi_Task *
  92. slapi_plugin_new_task(const char *dn, void *plugin)
  93. {
  94. return new_task(dn, plugin);
  95. }
  96. /* slapi_destroy_task: destroy a task
  97. * argument:
  98. * task: task to destroy
  99. * result:
  100. * none
  101. */
  102. void
  103. slapi_destroy_task(void *arg)
  104. {
  105. if (arg) {
  106. destroy_task(1, arg);
  107. }
  108. }
  109. /*
  110. * Sets the initial task state and updated status
  111. */
  112. void slapi_task_begin(Slapi_Task *task, int total_work)
  113. {
  114. if (task) {
  115. task->task_work = total_work;
  116. task->task_progress = 0;
  117. task->task_state = SLAPI_TASK_RUNNING;
  118. slapi_task_status_changed(task);
  119. }
  120. }
  121. /*
  122. * Increments task progress and updates status
  123. */
  124. void slapi_task_inc_progress(Slapi_Task *task)
  125. {
  126. if (task) {
  127. task->task_progress++;
  128. slapi_task_status_changed(task);
  129. }
  130. }
  131. /*
  132. * Sets completed task state and updates status
  133. */
  134. void slapi_task_finish(Slapi_Task *task, int rc)
  135. {
  136. if (task) {
  137. task->task_exitcode = rc;
  138. task->task_state = SLAPI_TASK_FINISHED;
  139. slapi_plugin_op_finished(task->origin_plugin);
  140. slapi_task_status_changed(task);
  141. }
  142. }
  143. /*
  144. * Cancels a task
  145. */
  146. void slapi_task_cancel(Slapi_Task *task, int rc)
  147. {
  148. if (task) {
  149. task->task_exitcode = rc;
  150. task->task_state = SLAPI_TASK_CANCELLED;
  151. slapi_task_status_changed(task);
  152. }
  153. }
  154. /*
  155. * Get the current state of a task
  156. */
  157. int slapi_task_get_state(Slapi_Task *task)
  158. {
  159. if (task) {
  160. return task->task_state;
  161. }
  162. return 0; /* return value not currently used */
  163. }
  164. /* this changes the 'nsTaskStatus' value, which is transient (anything logged
  165. * here wipes out any previous status)
  166. */
  167. void slapi_task_log_status(Slapi_Task *task, char *format, ...)
  168. {
  169. va_list ap;
  170. if (! task->task_status)
  171. task->task_status = (char *)slapi_ch_malloc(10 * LOG_BUFFER);
  172. if (! task->task_status)
  173. return; /* out of memory? */
  174. va_start(ap, format);
  175. PR_vsnprintf(task->task_status, (10 * LOG_BUFFER), format, ap);
  176. va_end(ap);
  177. slapi_task_status_changed(task);
  178. }
  179. void slapi_task_log_notice_ext(Slapi_Task *task, char *format, va_list ap)
  180. {
  181. char buffer[LOG_BUFFER];
  182. size_t len;
  183. PR_vsnprintf(buffer, LOG_BUFFER, format, ap);
  184. if (task->task_log_lock) {
  185. PR_Lock(task->task_log_lock);
  186. }
  187. len = 2 + strlen(buffer) + (task->task_log ? strlen(task->task_log) : 0);
  188. if ((len > MAX_SCROLLBACK_BUFFER) && task->task_log) {
  189. size_t i;
  190. char *newbuf;
  191. /* start from middle of buffer, and find next linefeed */
  192. i = strlen(task->task_log)/2;
  193. while (task->task_log[i] && (task->task_log[i] != '\n'))
  194. i++;
  195. if (task->task_log[i])
  196. i++;
  197. len = strlen(task->task_log) - i + 2 + strlen(buffer);
  198. newbuf = (char *)slapi_ch_malloc(len);
  199. strcpy(newbuf, task->task_log + i);
  200. slapi_ch_free((void **)&task->task_log);
  201. task->task_log = newbuf;
  202. } else {
  203. if (! task->task_log) {
  204. task->task_log = (char *)slapi_ch_malloc(len);
  205. task->task_log[0] = 0;
  206. } else {
  207. task->task_log = (char *)slapi_ch_realloc(task->task_log, len);
  208. }
  209. }
  210. if (task->task_log[0])
  211. strcat(task->task_log, "\n");
  212. strcat(task->task_log, buffer);
  213. if (task->task_log_lock) {
  214. PR_Unlock(task->task_log_lock);
  215. }
  216. slapi_task_status_changed(task);
  217. }
  218. void slapi_task_log_status_ext(Slapi_Task *task, char *format, va_list ap)
  219. {
  220. if (! task->task_status)
  221. task->task_status = (char *)slapi_ch_malloc(10 * LOG_BUFFER);
  222. if (! task->task_status)
  223. return; /* out of memory? */
  224. PR_vsnprintf(task->task_status, (10 * LOG_BUFFER), format, ap);
  225. slapi_task_status_changed(task);
  226. }
  227. /* this adds a line to the 'nsTaskLog' value, which is cumulative (anything
  228. * logged here is added to the end)
  229. */
  230. void slapi_task_log_notice(Slapi_Task *task, char *format, ...)
  231. {
  232. va_list ap;
  233. char buffer[LOG_BUFFER];
  234. size_t len;
  235. va_start(ap, format);
  236. PR_vsnprintf(buffer, LOG_BUFFER, format, ap);
  237. va_end(ap);
  238. if (task->task_log_lock) {
  239. PR_Lock(task->task_log_lock);
  240. }
  241. len = 2 + strlen(buffer) + (task->task_log ? strlen(task->task_log) : 0);
  242. if ((len > MAX_SCROLLBACK_BUFFER) && task->task_log) {
  243. size_t i;
  244. char *newbuf;
  245. /* start from middle of buffer, and find next linefeed */
  246. i = strlen(task->task_log)/2;
  247. while (task->task_log[i] && (task->task_log[i] != '\n'))
  248. i++;
  249. if (task->task_log[i])
  250. i++;
  251. len = strlen(task->task_log) - i + 2 + strlen(buffer);
  252. newbuf = (char *)slapi_ch_malloc(len);
  253. strcpy(newbuf, task->task_log + i);
  254. slapi_ch_free((void **)&task->task_log);
  255. task->task_log = newbuf;
  256. } else {
  257. if (! task->task_log) {
  258. task->task_log = (char *)slapi_ch_malloc(len);
  259. task->task_log[0] = 0;
  260. } else {
  261. task->task_log = (char *)slapi_ch_realloc(task->task_log, len);
  262. }
  263. }
  264. if (task->task_log[0])
  265. strcat(task->task_log, "\n");
  266. strcat(task->task_log, buffer);
  267. if (task->task_log_lock) {
  268. PR_Unlock(task->task_log_lock);
  269. }
  270. slapi_task_status_changed(task);
  271. }
  272. /* update attributes in the entry under "cn=tasks" to match the current
  273. * status of the task. */
  274. void slapi_task_status_changed(Slapi_Task *task)
  275. {
  276. LDAPMod modlist[20];
  277. LDAPMod *mod[20];
  278. int cur = 0, i;
  279. char s1[20], s2[20], s3[20];
  280. if (shutting_down) {
  281. /* don't care about task status updates anymore */
  282. return;
  283. }
  284. if (task->task_log_lock) {
  285. PR_Lock(task->task_log_lock);
  286. }
  287. NEXTMOD(TASK_LOG_NAME, task->task_log);
  288. if (task->task_log_lock) {
  289. PR_Unlock(task->task_log_lock);
  290. }
  291. NEXTMOD(TASK_STATUS_NAME, task->task_status);
  292. sprintf(s1, "%d", task->task_exitcode);
  293. sprintf(s2, "%d", task->task_progress);
  294. sprintf(s3, "%d", task->task_work);
  295. NEXTMOD(TASK_PROGRESS_NAME, s2);
  296. NEXTMOD(TASK_WORK_NAME, s3);
  297. /* only add the exit code when the job is done */
  298. if ((task->task_state == SLAPI_TASK_FINISHED) ||
  299. (task->task_state == SLAPI_TASK_CANCELLED)) {
  300. NEXTMOD(TASK_EXITCODE_NAME, s1);
  301. /* make sure the console can tell the task has ended */
  302. if (task->task_progress != task->task_work) {
  303. task->task_progress = task->task_work;
  304. }
  305. }
  306. mod[cur] = NULL;
  307. modify_internal_entry(task->task_dn, mod);
  308. for (i = 0; i < cur; i++)
  309. slapi_ch_free((void **)&modlist[i].mod_values);
  310. /*
  311. * Removed (task->task_state == SLAPI_TASK_CANCELLED) from
  312. * task_state checking to fix bz 515805.
  313. */
  314. if ((task->task_state == SLAPI_TASK_FINISHED) &&
  315. !(task->task_flags & SLAPI_TASK_DESTROYING)) {
  316. Slapi_PBlock *pb = slapi_pblock_new();
  317. Slapi_Entry *e;
  318. int ttl;
  319. time_t expire;
  320. e = get_internal_entry(pb, task->task_dn);
  321. if (e == NULL)
  322. return;
  323. ttl = atoi(fetch_attr(e, "ttl", DEFAULT_TTL));
  324. if (ttl > 3600)
  325. ttl = 3600; /* be reasonable. */
  326. expire = time(NULL) + ttl;
  327. task->task_flags |= SLAPI_TASK_DESTROYING;
  328. /* queue an event to destroy the state info */
  329. slapi_eq_once(destroy_task, (void *)task, expire);
  330. slapi_free_search_results_internal(pb);
  331. slapi_pblock_destroy(pb);
  332. }
  333. }
  334. /*
  335. * Stash some opaque task specific data in the task for later use.
  336. */
  337. void slapi_task_set_data(Slapi_Task *task, void *data)
  338. {
  339. if (task) {
  340. task->task_private = data;
  341. }
  342. }
  343. /*
  344. * Retrieve some opaque task specific data from the task.
  345. */
  346. void * slapi_task_get_data(Slapi_Task *task)
  347. {
  348. if (task) {
  349. return task->task_private;
  350. }
  351. return NULL; /* return value not currently used */
  352. }
  353. /*
  354. * Increment the task reference count
  355. */
  356. void slapi_task_inc_refcount(Slapi_Task *task)
  357. {
  358. if (task) {
  359. task->task_refcount++;
  360. }
  361. }
  362. /*
  363. * Decrement the task reference count
  364. */
  365. void slapi_task_dec_refcount(Slapi_Task *task)
  366. {
  367. if (task) {
  368. task->task_refcount--;
  369. }
  370. }
  371. /*
  372. * Returns the task reference count
  373. */
  374. int slapi_task_get_refcount(Slapi_Task *task)
  375. {
  376. if (task) {
  377. return task->task_refcount;
  378. }
  379. return 0; /* return value not currently used */
  380. }
  381. int
  382. slapi_plugin_task_unregister_handler(const char *name, dseCallbackFn func)
  383. {
  384. char *base = NULL;
  385. int rc = 0;
  386. base = slapi_create_dn_string("cn=%s,%s", name, TASK_BASE_DN);
  387. slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, base,
  388. LDAP_SCOPE_SUBTREE, "(objectclass=*)", func);
  389. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  390. base, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny);
  391. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  392. base, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny);
  393. slapi_ch_free_string(&base);
  394. return rc;
  395. }
  396. int
  397. slapi_plugin_task_register_handler(const char *name, dseCallbackFn func, Slapi_PBlock *plugin_pb)
  398. {
  399. Slapi_PBlock *add_pb = NULL;
  400. Slapi_Operation *op;
  401. LDAPMod *mods[3];
  402. LDAPMod mod[3];
  403. const char *objectclass[3];
  404. const char *cnvals[2];
  405. char *dn = NULL;
  406. int ret = -1;
  407. int x;
  408. dn = slapi_create_dn_string("cn=%s,%s", name, TASK_BASE_DN);
  409. if (NULL == dn) {
  410. LDAPDebug1Arg( LDAP_DEBUG_ANY,
  411. "slapi_task_register_handler: "
  412. "failed to create task dn for %s\n", name);
  413. return ret;
  414. }
  415. add_pb = slapi_pblock_new();
  416. if (add_pb == NULL) {
  417. goto out;
  418. }
  419. /* this is painful :( */
  420. mods[0] = &mod[0];
  421. mod[0].mod_op = LDAP_MOD_ADD;
  422. mod[0].mod_type = "objectClass";
  423. mod[0].mod_values = (char **)objectclass;
  424. objectclass[0] = "top";
  425. objectclass[1] = "extensibleObject";
  426. objectclass[2] = NULL;
  427. mods[1] = &mod[1];
  428. mod[1].mod_op = LDAP_MOD_ADD;
  429. mod[1].mod_type = "cn";
  430. mod[1].mod_values = (char **)cnvals;
  431. cnvals[0] = name;
  432. cnvals[1] = NULL;
  433. mods[2] = NULL;
  434. slapi_add_internal_set_pb(add_pb, dn, mods, NULL,
  435. plugin_get_default_component_id(), 0);
  436. x = 1;
  437. slapi_pblock_set(add_pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &x);
  438. /* Make sure these adds don't appear in the audit and change logs */
  439. slapi_pblock_get(add_pb, SLAPI_OPERATION, &op);
  440. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  441. slapi_add_internal_pb(add_pb);
  442. slapi_pblock_get(add_pb, SLAPI_PLUGIN_INTOP_RESULT, &x);
  443. if ((x != LDAP_SUCCESS) && (x != LDAP_ALREADY_EXISTS)) {
  444. LDAPDebug(LDAP_DEBUG_ANY,
  445. "Can't create task node '%s' (error %d)\n",
  446. name, x, 0);
  447. ret = x;
  448. goto out;
  449. }
  450. /* register add callback */
  451. slapi_config_register_callback_plugin(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP,
  452. dn, LDAP_SCOPE_SUBTREE, "(objectclass=*)", func, plugin_pb, plugin_pb);
  453. /* deny modify/delete of the root task entry */
  454. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  455. dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  456. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  457. dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  458. ret = 0;
  459. out:
  460. slapi_ch_free_string(&dn);
  461. if (add_pb) {
  462. slapi_pblock_destroy(add_pb);
  463. }
  464. return ret;
  465. }
  466. /* name is, for example, "import" */
  467. int slapi_task_register_handler(const char *name, dseCallbackFn func)
  468. {
  469. return slapi_plugin_task_register_handler(name, func, NULL);
  470. }
  471. void slapi_task_set_destructor_fn(Slapi_Task *task, TaskCallbackFn func)
  472. {
  473. if (task) {
  474. task->destructor = func;
  475. }
  476. }
  477. void slapi_task_set_cancel_fn(Slapi_Task *task, TaskCallbackFn func)
  478. {
  479. if (task) {
  480. task->cancel = func;
  481. }
  482. }
  483. /***********************************
  484. * Static Helper Functions
  485. ***********************************/
  486. /* create a new task, fill in DN, and setup modify callback */
  487. static Slapi_Task *
  488. new_task(const char *rawdn, void *plugin)
  489. {
  490. Slapi_Task *task = NULL;
  491. char *dn = NULL;
  492. if (rawdn == NULL) {
  493. return NULL;
  494. }
  495. dn = slapi_create_dn_string("%s", rawdn);
  496. if (NULL == dn) {
  497. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  498. "new_task failed: invalid task dn: %s\n", rawdn);
  499. return NULL;
  500. }
  501. task = (Slapi_Task *)slapi_ch_calloc(1, sizeof(Slapi_Task));
  502. PR_Lock(global_task_lock);
  503. task->next = global_task_list;
  504. global_task_list = task;
  505. PR_Unlock(global_task_lock);
  506. task->task_dn = dn;
  507. task->task_state = SLAPI_TASK_SETUP;
  508. task->task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  509. task->destructor = NULL;
  510. task->cancel = NULL;
  511. task->task_private = NULL;
  512. task->origin_plugin = plugin;
  513. slapi_plugin_op_started(task->origin_plugin);
  514. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, dn,
  515. LDAP_SCOPE_BASE, "(objectclass=*)", task_modify, (void *)task);
  516. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, dn,
  517. LDAP_SCOPE_BASE, "(objectclass=*)", task_deny, NULL);
  518. /* don't add entries under this one */
  519. #if 0
  520. /* don't know why, but this doesn't work. it makes the current add
  521. * operation fail. :(
  522. */
  523. slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, dn,
  524. LDAP_SCOPE_SUBTREE, "(objectclass=*)", task_deny, NULL);
  525. #endif
  526. /* To protect task_log to be realloced if it's in use */
  527. task->task_log_lock = PR_NewLock();
  528. return task;
  529. }
  530. /* called by the event queue to destroy a task */
  531. static void
  532. destroy_task(time_t when, void *arg)
  533. {
  534. Slapi_Task *task = (Slapi_Task *)arg;
  535. Slapi_Task *t1;
  536. Slapi_PBlock *pb;
  537. if (task == NULL) return;
  538. pb = slapi_pblock_new();
  539. /* Call the custom destructor callback if one was provided,
  540. * then perform the internal task destruction. */
  541. if (task->destructor != NULL) {
  542. (*task->destructor)(task);
  543. }
  544. task_generic_destructor(task);
  545. /* if when == 0, we're already locked (called during shutdown) */
  546. if (when != 0) {
  547. PR_Lock(global_task_lock);
  548. }
  549. if (global_task_list == task) {
  550. global_task_list = task->next;
  551. } else {
  552. for (t1 = global_task_list; t1; t1 = t1->next) {
  553. if (t1->next == task) {
  554. t1->next = task->next;
  555. break;
  556. }
  557. }
  558. }
  559. if (when != 0) {
  560. PR_Unlock(global_task_lock);
  561. }
  562. slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  563. task->task_dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_modify);
  564. slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  565. task->task_dn, LDAP_SCOPE_BASE, "(objectclass=*)", task_deny);
  566. slapi_delete_internal_set_pb(pb, task->task_dn, NULL, NULL,
  567. (void *)plugin_get_default_component_id(), 0);
  568. slapi_delete_internal_pb(pb);
  569. slapi_pblock_destroy(pb);
  570. slapi_ch_free((void **)&task->task_dn);
  571. slapi_ch_free((void **)&task);
  572. }
  573. /* extract a single value from the entry (as a string) -- if it's not in the
  574. * entry, the default will be returned (which can be NULL).
  575. * you do not need to free anything returned by this.
  576. */
  577. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  578. const char *default_val)
  579. {
  580. Slapi_Attr *attr;
  581. Slapi_Value *val = NULL;
  582. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  583. return default_val;
  584. slapi_attr_first_value(attr, &val);
  585. return slapi_value_get_string(val);
  586. }
  587. /* supply the pblock, destroy it when you're done */
  588. static Slapi_Entry *get_internal_entry(Slapi_PBlock *pb, char *dn)
  589. {
  590. Slapi_Entry **entries = NULL;
  591. int ret = 0;
  592. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
  593. NULL, 0, NULL, NULL, (void *)plugin_get_default_component_id(), 0);
  594. slapi_search_internal_pb(pb);
  595. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  596. if (ret != LDAP_SUCCESS) {
  597. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't find task entry '%s'\n",
  598. dn, 0, 0);
  599. return NULL;
  600. }
  601. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  602. if ((NULL == entries) || (NULL == entries[0])) {
  603. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't find task entry '%s'\n",
  604. dn, 0, 0);
  605. return NULL;
  606. }
  607. return entries[0];
  608. }
  609. static void modify_internal_entry(char *dn, LDAPMod **mods)
  610. {
  611. Slapi_PBlock pb;
  612. Slapi_Operation *op;
  613. int ret = 0;
  614. int tries = 0;
  615. int dont_write_file = 1;
  616. do {
  617. pblock_init(&pb);
  618. slapi_modify_internal_set_pb(&pb, dn, mods, NULL, NULL,
  619. (void *)plugin_get_default_component_id(), 0);
  620. /* all modifications to the cn=tasks subtree are transient --
  621. * we erase them all when the server starts up next time, so there's
  622. * no need to save them in the dse file.
  623. */
  624. slapi_pblock_set(&pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
  625. /* Make sure these mods are not logged in audit or changelog */
  626. slapi_pblock_get(&pb, SLAPI_OPERATION, &op);
  627. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  628. slapi_modify_internal_pb(&pb);
  629. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  630. if (ret != LDAP_SUCCESS) {
  631. /* could be waiting for another thread to finish adding this
  632. * entry -- try at least 3 times before giving up.
  633. */
  634. tries++;
  635. if (tries == 3) {
  636. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't modify task "
  637. "entry '%s'; %s (%d)\n", dn, ldap_err2string(ret), ret);
  638. pblock_done(&pb);
  639. return;
  640. }
  641. DS_Sleep(PR_SecondsToInterval(1));
  642. }
  643. pblock_done(&pb);
  644. } while (ret != LDAP_SUCCESS);
  645. }
  646. static void task_generic_destructor(Slapi_Task *task)
  647. {
  648. if (task->task_log) {
  649. slapi_ch_free((void **)&task->task_log);
  650. }
  651. if (task->task_status) {
  652. slapi_ch_free((void **)&task->task_status);
  653. }
  654. if (task->task_log_lock) {
  655. PR_DestroyLock(task->task_log_lock);
  656. task->task_log_lock = NULL;
  657. }
  658. task->task_log = task->task_status = NULL;
  659. }
  660. /********** actual task callbacks **********/
  661. static int task_deny(Slapi_PBlock *pb, Slapi_Entry *e,
  662. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  663. {
  664. /* internal operations (conn=NULL) are allowed to do whatever they want */
  665. if (pb->pb_conn == NULL) {
  666. *returncode = LDAP_SUCCESS;
  667. return SLAPI_DSE_CALLBACK_OK;
  668. }
  669. *returncode = LDAP_UNWILLING_TO_PERFORM;
  670. return SLAPI_DSE_CALLBACK_ERROR;
  671. }
  672. static int task_modify(Slapi_PBlock *pb, Slapi_Entry *e,
  673. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  674. {
  675. Slapi_Task *task = (Slapi_Task *)arg;
  676. LDAPMod **mods;
  677. int i;
  678. /* the connection block will be NULL for internal operations */
  679. if (pb->pb_conn == NULL) {
  680. *returncode = LDAP_SUCCESS;
  681. return SLAPI_DSE_CALLBACK_OK;
  682. }
  683. /* ignore eAfter, just scan the mods for anything unacceptable */
  684. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  685. for (i = 0; (mods != NULL) && (mods[i] != NULL); i++) {
  686. /* for some reason, "modifiersName" and "modifyTimestamp" are
  687. * stuck in by the server */
  688. if ((strcasecmp(mods[i]->mod_type, "ttl") != 0) &&
  689. (strcasecmp(mods[i]->mod_type, "nsTaskCancel") != 0) &&
  690. !slapi_attr_is_last_mod(mods[i]->mod_type))
  691. {
  692. /* you aren't allowed to change this! */
  693. *returncode = LDAP_UNWILLING_TO_PERFORM;
  694. return SLAPI_DSE_CALLBACK_ERROR;
  695. }
  696. }
  697. /* okay, we've decided to accept these changes. now look at the new
  698. * entry and absorb any new values.
  699. */
  700. if (strcasecmp(fetch_attr(eAfter, "nsTaskCancel", "false"), "true") == 0) {
  701. /* cancel this task, if not already */
  702. if (task->task_state != SLAPI_TASK_CANCELLED) {
  703. task->task_state = SLAPI_TASK_CANCELLED;
  704. if (task->cancel) {
  705. (*task->cancel)(task);
  706. LDAPDebug(LDAP_DEBUG_ANY, "Cancelling task '%s'\n",
  707. fetch_attr(eAfter, "cn", "?"), 0, 0);
  708. }
  709. }
  710. }
  711. /* we fetch ttl from the entry when it's needed */
  712. *returncode = LDAP_SUCCESS;
  713. return SLAPI_DSE_CALLBACK_OK;
  714. }
  715. static int task_import_add(Slapi_PBlock *pb, Slapi_Entry *e,
  716. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  717. {
  718. Slapi_Attr *attr;
  719. Slapi_Value *val = NULL;
  720. Slapi_Backend *be = NULL;
  721. const char *instance_name;
  722. char **ldif_file = NULL, **include = NULL, **exclude = NULL;
  723. int idx, rv = 0;
  724. const char *do_attr_indexes, *uniqueid_kind_str;
  725. int uniqueid_kind = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  726. Slapi_PBlock mypb;
  727. Slapi_Task *task;
  728. char *nameFrombe_name = NULL;
  729. const char *encrypt_on_import = NULL;
  730. if (fetch_attr(e, "cn", NULL) == NULL) {
  731. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  732. return SLAPI_DSE_CALLBACK_ERROR;
  733. }
  734. instance_name = fetch_attr(e, "nsInstance", NULL);
  735. encrypt_on_import = fetch_attr(e, "nsImportEncrypt", NULL);
  736. /* include/exclude suffixes */
  737. if (slapi_entry_attr_find(e, "nsIncludeSuffix", &attr) == 0) {
  738. for (idx = slapi_attr_first_value(attr, &val);
  739. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  740. rv = charray_normdn_add(&include,
  741. (char *)slapi_value_get_string(val),
  742. "nsIncludeSuffix");
  743. if (0 != rv) {
  744. *returncode = LDAP_PARAM_ERROR;
  745. return SLAPI_DSE_CALLBACK_ERROR;
  746. }
  747. }
  748. }
  749. if (slapi_entry_attr_find(e, "nsExcludeSuffix", &attr) == 0) {
  750. for (idx = slapi_attr_first_value(attr, &val);
  751. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  752. rv = charray_normdn_add(&exclude,
  753. (char *)slapi_value_get_string(val),
  754. "nsExcludeSuffix");
  755. if (0 != rv) {
  756. *returncode = LDAP_PARAM_ERROR;
  757. return SLAPI_DSE_CALLBACK_ERROR;
  758. }
  759. }
  760. }
  761. /*
  762. * if instance is given, just use it to get the backend.
  763. * otherwise, we use included/excluded suffix list to specify a backend.
  764. */
  765. if (NULL == instance_name) {
  766. char **instances, **ip;
  767. int counter;
  768. if (slapi_lookup_instance_name_by_suffixes(include, exclude,
  769. &instances) < 0) {
  770. LDAPDebug(LDAP_DEBUG_ANY,
  771. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  772. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  773. return SLAPI_DSE_CALLBACK_ERROR;
  774. }
  775. if (instances) {
  776. for (ip = instances, counter = 0; ip && *ip; ip++, counter++)
  777. ;
  778. if (counter == 1){
  779. instance_name = *instances;
  780. nameFrombe_name = *instances;
  781. }
  782. else if (counter == 0) {
  783. LDAPDebug(LDAP_DEBUG_ANY,
  784. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  785. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  786. return SLAPI_DSE_CALLBACK_ERROR;
  787. } else {
  788. LDAPDebug(LDAP_DEBUG_ANY,
  789. "ERROR: Multiple backend instances are specified: "
  790. "%s, %s, ...\n", instances[0], instances[1], 0);
  791. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  792. return SLAPI_DSE_CALLBACK_ERROR;
  793. }
  794. } else {
  795. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  796. return SLAPI_DSE_CALLBACK_ERROR;
  797. }
  798. }
  799. /* lookup the backend */
  800. be = slapi_be_select_by_instance_name(instance_name);
  801. if (be == NULL) {
  802. LDAPDebug(LDAP_DEBUG_ANY, "can't import to nonexistent backend %s\n",
  803. instance_name, 0, 0);
  804. slapi_ch_free_string(&nameFrombe_name);
  805. *returncode = LDAP_NO_SUCH_OBJECT;
  806. return SLAPI_DSE_CALLBACK_ERROR;
  807. }
  808. /* refuse to do an import on pre-V3 plugins. plugin api V3 is the one
  809. * for DS 5.0 where the import/export stuff changed a lot.
  810. */
  811. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  812. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an import with pre-V3 "
  813. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  814. *returncode = LDAP_UNWILLING_TO_PERFORM;
  815. slapi_ch_free_string(&nameFrombe_name);
  816. return SLAPI_DSE_CALLBACK_ERROR;
  817. }
  818. if (be->be_database->plg_ldif2db == NULL) {
  819. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no ldif2db function defined for "
  820. "backend %s\n", be->be_database->plg_name, 0, 0);
  821. *returncode = LDAP_UNWILLING_TO_PERFORM;
  822. slapi_ch_free_string(&nameFrombe_name);
  823. return SLAPI_DSE_CALLBACK_ERROR;
  824. }
  825. /* get ldif filenames -- from here on, memory has been allocated */
  826. if (slapi_entry_attr_find(e, "nsFilename", &attr) != 0) {
  827. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  828. slapi_ch_free_string(&nameFrombe_name);
  829. return SLAPI_DSE_CALLBACK_ERROR;
  830. }
  831. for (idx = slapi_attr_first_value(attr, &val);
  832. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  833. charray_add(&ldif_file, slapi_ch_strdup(slapi_value_get_string(val)));
  834. }
  835. do_attr_indexes = fetch_attr(e, "nsImportIndexAttrs", "true");
  836. uniqueid_kind_str = fetch_attr(e, "nsUniqueIdGenerator", NULL);
  837. if (uniqueid_kind_str != NULL) {
  838. if (strcasecmp(uniqueid_kind_str, "none") == 0) {
  839. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_NONE;
  840. } else if (strcasecmp(uniqueid_kind_str, "deterministic") == 0) {
  841. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_NAME_BASED;
  842. } else {
  843. /* default - time based */
  844. uniqueid_kind = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  845. }
  846. }
  847. /* allocate new task now */
  848. task = slapi_new_task(slapi_entry_get_ndn(e));
  849. if (task == NULL) {
  850. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  851. rv = LDAP_OPERATIONS_ERROR;
  852. goto out;
  853. }
  854. memset(&mypb, 0, sizeof(mypb));
  855. mypb.pb_backend = be;
  856. mypb.pb_plugin = be->be_database;
  857. mypb.pb_removedupvals = atoi(fetch_attr(e, "nsImportChunkSize", "0"));
  858. mypb.pb_ldif2db_noattrindexes =
  859. !(strcasecmp(do_attr_indexes, "true") == 0);
  860. mypb.pb_ldif_generate_uniqueid = uniqueid_kind;
  861. mypb.pb_ldif_namespaceid =
  862. (char *)fetch_attr(e, "nsUniqueIdGeneratorNamespace", NULL);
  863. mypb.pb_instance_name = (char *)instance_name;
  864. mypb.pb_ldif_files = ldif_file;
  865. mypb.pb_ldif_include = include;
  866. mypb.pb_ldif_exclude = exclude;
  867. mypb.pb_task = task;
  868. mypb.pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  869. if (NULL != encrypt_on_import && 0 == strcasecmp(encrypt_on_import, "true") ) {
  870. mypb.pb_ldif_encrypt = 1;
  871. }
  872. rv = (*mypb.pb_plugin->plg_ldif2db)(&mypb);
  873. if (rv == 0) {
  874. slapi_entry_attr_set_charptr(e, TASK_LOG_NAME, "");
  875. slapi_entry_attr_set_charptr(e, TASK_STATUS_NAME, "");
  876. slapi_entry_attr_set_int(e, TASK_PROGRESS_NAME, task->task_progress);
  877. slapi_entry_attr_set_int(e, TASK_WORK_NAME, task->task_work);
  878. }
  879. out:
  880. slapi_ch_free_string(&nameFrombe_name);
  881. charray_free(ldif_file);
  882. charray_free(include);
  883. charray_free(exclude);
  884. if (rv != 0) {
  885. *returncode = LDAP_OPERATIONS_ERROR;
  886. destroy_task(1, task);
  887. return SLAPI_DSE_CALLBACK_ERROR;
  888. }
  889. *returncode = LDAP_SUCCESS;
  890. return SLAPI_DSE_CALLBACK_OK;
  891. }
  892. static void task_export_thread(void *arg)
  893. {
  894. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  895. char **instance_names = (char **)pb->pb_instance_name;
  896. char **inp;
  897. char *ldif_file = pb->pb_ldif_file;
  898. char *this_ldif_file = NULL;
  899. Slapi_Backend *be = NULL;
  900. int rv = -1;
  901. int count;
  902. Slapi_Task *task = pb->pb_task;
  903. g_incr_active_threadcnt();
  904. for (count = 0, inp = instance_names; *inp; inp++, count++)
  905. ;
  906. slapi_task_begin(task, count);
  907. for (inp = instance_names; *inp; inp++) {
  908. int release_me = 0;
  909. /* lookup the backend */
  910. be = slapi_be_select_by_instance_name((const char *)*inp);
  911. if (be == NULL) {
  912. /* shouldn't happen */
  913. LDAPDebug(LDAP_DEBUG_ANY, "ldbm2ldif: backend '%s' is AWOL!\n",
  914. (const char *)*inp, 0, 0);
  915. continue;
  916. }
  917. pb->pb_backend = be;
  918. pb->pb_plugin = be->be_database;
  919. pb->pb_instance_name = (char *)*inp;
  920. /* ldif_file name for each? */
  921. if (pb->pb_ldif_printkey & EXPORT_APPENDMODE) {
  922. if (inp == instance_names) { /* first export */
  923. pb->pb_ldif_printkey |= EXPORT_APPENDMODE_1;
  924. } else {
  925. pb->pb_ldif_printkey &= ~EXPORT_APPENDMODE_1;
  926. }
  927. } else {
  928. if (strcmp(ldif_file, "-")) { /* not '-' */
  929. char *p;
  930. char sep = '/';
  931. this_ldif_file = (char *)slapi_ch_malloc(strlen(ldif_file) +
  932. strlen(*inp) + 2);
  933. p = strrchr(ldif_file, sep);
  934. if (NULL == p) {
  935. sprintf(this_ldif_file, "%s_%s", *inp, ldif_file);
  936. } else {
  937. char *q;
  938. q = p + 1;
  939. *p = '\0';
  940. sprintf(this_ldif_file, "%s%c%s_%s",
  941. ldif_file, sep, *inp, q);
  942. *p = sep;
  943. }
  944. pb->pb_ldif_file = this_ldif_file;
  945. release_me = 1;
  946. }
  947. }
  948. slapi_task_log_notice(task, "Beginning export of '%s'", *inp);
  949. LDAPDebug(LDAP_DEBUG_ANY, "Beginning export of '%s'\n", *inp, 0, 0);
  950. rv = (*pb->pb_plugin->plg_db2ldif)(pb);
  951. if (rv != 0) {
  952. slapi_task_log_notice(task, "backend '%s' export failed (%d)",
  953. *inp, rv);
  954. LDAPDebug(LDAP_DEBUG_ANY,
  955. "ldbm2ldif: backend '%s' export failed (%d)\n",
  956. (const char *)*inp, rv, 0);
  957. }
  958. if (release_me) {
  959. slapi_ch_free((void **)&this_ldif_file);
  960. }
  961. if (rv != 0)
  962. break;
  963. slapi_task_inc_progress(task);
  964. }
  965. /* free the memory now */
  966. charray_free(instance_names);
  967. slapi_ch_free((void **)&ldif_file);
  968. charray_free(pb->pb_ldif_include);
  969. charray_free(pb->pb_ldif_exclude);
  970. slapi_pblock_destroy(pb);
  971. if (rv == 0) {
  972. slapi_task_log_notice(task, "Export finished.");
  973. LDAPDebug(LDAP_DEBUG_ANY, "Export finished.\n", 0, 0, 0);
  974. } else {
  975. slapi_task_log_notice(task, "Export failed.");
  976. LDAPDebug(LDAP_DEBUG_ANY, "Export failed.\n", 0, 0, 0);
  977. }
  978. slapi_task_finish(task, rv);
  979. g_decr_active_threadcnt();
  980. }
  981. static int task_export_add(Slapi_PBlock *pb, Slapi_Entry *e,
  982. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  983. {
  984. Slapi_Attr *attr;
  985. Slapi_Value *val = NULL;
  986. Slapi_Backend *be = NULL;
  987. char *ldif_file = NULL;
  988. char **instance_names = NULL, **inp;
  989. char **include = NULL, **exclude = NULL;
  990. int idx, rv = SLAPI_DSE_CALLBACK_OK;
  991. int export_replica_flag = 0;
  992. int ldif_printkey_flag = 0;
  993. int dump_uniqueid_flag = 0;
  994. int instance_cnt = 0;
  995. const char *my_ldif_file;
  996. const char *use_one_file;
  997. const char *export_replica;
  998. const char *ldif_printkey;
  999. const char *dump_uniqueid;
  1000. Slapi_PBlock *mypb = NULL;
  1001. Slapi_Task *task = NULL;
  1002. PRThread *thread;
  1003. const char *decrypt_on_export = NULL;
  1004. *returncode = LDAP_SUCCESS;
  1005. if (fetch_attr(e, "cn", NULL) == NULL) {
  1006. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1007. rv = SLAPI_DSE_CALLBACK_ERROR;
  1008. goto out;
  1009. }
  1010. decrypt_on_export = fetch_attr(e, "nsExportDecrypt", NULL);
  1011. /* nsInstances -- from here on, memory has been allocated */
  1012. if (slapi_entry_attr_find(e, "nsInstance", &attr) == 0) {
  1013. for (idx = slapi_attr_first_value(attr, &val);
  1014. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1015. charray_add(&instance_names,
  1016. slapi_ch_strdup(slapi_value_get_string(val)));
  1017. instance_cnt++;
  1018. }
  1019. }
  1020. /* include/exclude suffixes */
  1021. if (slapi_entry_attr_find(e, "nsIncludeSuffix", &attr) == 0) {
  1022. for (idx = slapi_attr_first_value(attr, &val);
  1023. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1024. rv = charray_normdn_add(&include,
  1025. (char *)slapi_value_get_string(val),
  1026. "nsIncludeSuffix");
  1027. if (0 != rv) {
  1028. *returncode = LDAP_PARAM_ERROR;
  1029. goto out;
  1030. }
  1031. }
  1032. }
  1033. if (slapi_entry_attr_find(e, "nsExcludeSuffix", &attr) == 0) {
  1034. for (idx = slapi_attr_first_value(attr, &val);
  1035. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1036. rv = charray_normdn_add(&exclude,
  1037. (char *)slapi_value_get_string(val),
  1038. "nsExcludeSuffix");
  1039. if (0 != rv) {
  1040. *returncode = LDAP_PARAM_ERROR;
  1041. goto out;
  1042. }
  1043. }
  1044. }
  1045. if (NULL == instance_names) {
  1046. char **ip;
  1047. if (slapi_lookup_instance_name_by_suffixes(include, exclude,
  1048. &instance_names) < 0) {
  1049. LDAPDebug(LDAP_DEBUG_ANY,
  1050. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  1051. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1052. rv = SLAPI_DSE_CALLBACK_ERROR;
  1053. goto out;
  1054. }
  1055. if (instance_names) {
  1056. for (ip = instance_names, instance_cnt = 0; ip && *ip;
  1057. ip++, instance_cnt++)
  1058. ;
  1059. if (instance_cnt == 0) {
  1060. LDAPDebug(LDAP_DEBUG_ANY,
  1061. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  1062. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1063. rv = SLAPI_DSE_CALLBACK_ERROR;
  1064. goto out;
  1065. }
  1066. } else {
  1067. LDAPDebug(LDAP_DEBUG_ANY,
  1068. "ERROR: No backend instance is specified.\n", 0, 0, 0);
  1069. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1070. rv = SLAPI_DSE_CALLBACK_ERROR;
  1071. goto out;
  1072. }
  1073. }
  1074. /* ldif file name */
  1075. if ((my_ldif_file = fetch_attr(e, "nsFilename", NULL)) == NULL) {
  1076. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1077. rv = SLAPI_DSE_CALLBACK_ERROR;
  1078. goto out;
  1079. }
  1080. ldif_file = slapi_ch_strdup(my_ldif_file);
  1081. /* if true, multiple backends are dumped into one ldif file */
  1082. use_one_file = fetch_attr(e, "nsUseOneFile", "true");
  1083. if (strcasecmp(use_one_file, "true") == 0) {
  1084. ldif_printkey_flag |= EXPORT_APPENDMODE;
  1085. }
  1086. /* -r: export replica */
  1087. export_replica = fetch_attr(e, "nsExportReplica", "false");
  1088. if (!strcasecmp(export_replica, "true")) /* true */
  1089. export_replica_flag = 1;
  1090. /* -N: eq "false" ==> does not print out key value */
  1091. ldif_printkey = fetch_attr(e, "nsPrintKey", "true");
  1092. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1093. ldif_printkey_flag |= EXPORT_PRINTKEY;
  1094. /* -C: eq "true" ==> use only id2entry file */
  1095. ldif_printkey = fetch_attr(e, "nsUseId2Entry", "false");
  1096. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1097. ldif_printkey_flag |= EXPORT_ID2ENTRY_ONLY;
  1098. /* if "true" ==> 8-bit strings are not base64 encoded */
  1099. ldif_printkey = fetch_attr(e, "nsMinimalEncoding", "false");
  1100. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1101. ldif_printkey_flag |= EXPORT_MINIMAL_ENCODING;
  1102. /* -U: eq "true" ==> does not fold the output */
  1103. ldif_printkey = fetch_attr(e, "nsNoWrap", "false");
  1104. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1105. ldif_printkey_flag |= EXPORT_NOWRAP;
  1106. /* -1: eq "true" ==> does not print version line */
  1107. ldif_printkey = fetch_attr(e, "nsNoVersionLine", "false");
  1108. if (!strcasecmp(ldif_printkey, "true")) /* true */
  1109. ldif_printkey_flag |= EXPORT_NOVERSION;
  1110. /* -u: eq "false" ==> does not dump unique id */
  1111. dump_uniqueid = fetch_attr(e, "nsDumpUniqId", "true");
  1112. if (!strcasecmp(dump_uniqueid, "true")) /* true */
  1113. dump_uniqueid_flag = 1;
  1114. /* check that all the backends are ok */
  1115. for (inp = instance_names; *inp; inp++) {
  1116. /* lookup the backend */
  1117. be = slapi_be_select_by_instance_name((const char *)*inp);
  1118. if (be == NULL) {
  1119. LDAPDebug(LDAP_DEBUG_ANY,
  1120. "can't export to nonexistent backend %s\n", *inp, 0, 0);
  1121. *returncode = LDAP_NO_SUCH_OBJECT;
  1122. rv = SLAPI_DSE_CALLBACK_ERROR;
  1123. goto out;
  1124. }
  1125. /* refuse to do an export on pre-V3 plugins. plugin api V3 is the one
  1126. * for DS 5.0 where the import/export stuff changed a lot.
  1127. */
  1128. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1129. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an export with pre-V3 "
  1130. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1131. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1132. rv = SLAPI_DSE_CALLBACK_ERROR;
  1133. goto out;
  1134. }
  1135. if (be->be_database->plg_db2ldif == NULL) {
  1136. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no db2ldif function defined for "
  1137. "backend %s\n", be->be_database->plg_name, 0, 0);
  1138. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1139. rv = SLAPI_DSE_CALLBACK_ERROR;
  1140. goto out;
  1141. }
  1142. }
  1143. /* allocate new task now */
  1144. task = slapi_new_task(slapi_entry_get_ndn(e));
  1145. if (task == NULL) {
  1146. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1147. *returncode = LDAP_OPERATIONS_ERROR;
  1148. rv = SLAPI_DSE_CALLBACK_ERROR;
  1149. goto out;
  1150. }
  1151. mypb = slapi_pblock_new();
  1152. if (mypb == NULL) {
  1153. *returncode = LDAP_OPERATIONS_ERROR;
  1154. rv = SLAPI_DSE_CALLBACK_ERROR;
  1155. goto out;
  1156. }
  1157. mypb->pb_ldif_include = include;
  1158. mypb->pb_ldif_exclude = exclude;
  1159. mypb->pb_ldif_printkey = ldif_printkey_flag;
  1160. mypb->pb_ldif_dump_replica = export_replica_flag;
  1161. mypb->pb_ldif_dump_uniqueid = dump_uniqueid_flag;
  1162. mypb->pb_ldif_file = ldif_file;
  1163. /* horrible hack */
  1164. mypb->pb_instance_name = (char *)instance_names;
  1165. mypb->pb_task = task;
  1166. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1167. if (NULL != decrypt_on_export && 0 == strcasecmp(decrypt_on_export, "true") ) {
  1168. mypb->pb_ldif_encrypt = 1;
  1169. }
  1170. /* start the export as a separate thread */
  1171. thread = PR_CreateThread(PR_USER_THREAD, task_export_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 ldbm2ldif thread!\n", 0, 0, 0);
  1177. *returncode = LDAP_OPERATIONS_ERROR;
  1178. rv = SLAPI_DSE_CALLBACK_ERROR;
  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. charray_free(instance_names);
  1186. charray_free(include);
  1187. charray_free(exclude);
  1188. if (ldif_file != NULL) {
  1189. slapi_ch_free((void **)&ldif_file);
  1190. }
  1191. if (task) {
  1192. destroy_task(1, task);
  1193. }
  1194. return rv;
  1195. }
  1196. static void task_backup_thread(void *arg)
  1197. {
  1198. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1199. Slapi_Task *task = pb->pb_task;
  1200. int rv;
  1201. g_incr_active_threadcnt();
  1202. slapi_task_begin(task, 1);
  1203. slapi_task_log_notice(task, "Beginning backup of '%s'",
  1204. pb->pb_plugin->plg_name);
  1205. LDAPDebug(LDAP_DEBUG_ANY, "Beginning backup of '%s'\n",
  1206. pb->pb_plugin->plg_name, 0, 0);
  1207. rv = (*pb->pb_plugin->plg_db2archive)(pb);
  1208. if (rv != 0) {
  1209. slapi_task_log_notice(task, "Backup failed (error %d)", rv);
  1210. slapi_task_log_status(task, "Backup failed (error %d)", rv);
  1211. LDAPDebug(LDAP_DEBUG_ANY, "Backup failed (error %d)\n", rv, 0, 0);
  1212. } else {
  1213. slapi_task_log_notice(task, "Backup finished.");
  1214. slapi_task_log_status(task, "Backup finished.");
  1215. LDAPDebug(LDAP_DEBUG_ANY, "Backup finished.\n", 0, 0, 0);
  1216. }
  1217. slapi_task_finish(task, rv);
  1218. slapi_ch_free((void **)&pb->pb_seq_val);
  1219. slapi_pblock_destroy(pb);
  1220. g_decr_active_threadcnt();
  1221. }
  1222. static int task_backup_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1223. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1224. {
  1225. Slapi_Backend *be = NULL;
  1226. PRThread *thread = NULL;
  1227. const char *archive_dir = NULL;
  1228. const char *my_database_type = NULL;
  1229. const char *database_type = "ldbm database";
  1230. char *cookie = NULL;
  1231. int rv = SLAPI_DSE_CALLBACK_OK;
  1232. Slapi_PBlock *mypb = NULL;
  1233. Slapi_Task *task = NULL;
  1234. *returncode = LDAP_SUCCESS;
  1235. if (fetch_attr(e, "cn", NULL) == NULL) {
  1236. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1237. rv = SLAPI_DSE_CALLBACK_ERROR;
  1238. goto out;
  1239. }
  1240. /* archive dir name */
  1241. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1242. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1243. rv = SLAPI_DSE_CALLBACK_ERROR;
  1244. goto out;
  1245. }
  1246. /* database type */
  1247. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1248. if (NULL != my_database_type)
  1249. database_type = my_database_type;
  1250. /* get backend that has db2archive and the database type matches. */
  1251. be = slapi_get_first_backend(&cookie);
  1252. while (be) {
  1253. if (NULL != be->be_database->plg_db2archive &&
  1254. !strcasecmp(database_type, be->be_database->plg_name))
  1255. break;
  1256. be = (backend *)slapi_get_next_backend (cookie);
  1257. }
  1258. slapi_ch_free_string(&cookie);
  1259. if (NULL == be || NULL == be->be_database->plg_db2archive) {
  1260. LDAPDebug(LDAP_DEBUG_ANY,
  1261. "ERROR: no db2archive function defined.\n", 0, 0, 0);
  1262. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1263. rv = SLAPI_DSE_CALLBACK_ERROR;
  1264. goto out;
  1265. }
  1266. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1267. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an backup with pre-V3 "
  1268. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1269. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1270. rv = SLAPI_DSE_CALLBACK_ERROR;
  1271. goto out;
  1272. }
  1273. /* allocate new task now */
  1274. task = slapi_new_task(slapi_entry_get_ndn(e));
  1275. if (task == NULL) {
  1276. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1277. *returncode = LDAP_OPERATIONS_ERROR;
  1278. rv = SLAPI_DSE_CALLBACK_ERROR;
  1279. goto out;
  1280. }
  1281. mypb = slapi_pblock_new();
  1282. if (mypb == NULL) {
  1283. *returncode = LDAP_OPERATIONS_ERROR;
  1284. rv = SLAPI_DSE_CALLBACK_ERROR;
  1285. goto out;
  1286. }
  1287. mypb->pb_seq_val = slapi_ch_strdup(archive_dir);
  1288. mypb->pb_plugin = be->be_database;
  1289. mypb->pb_task = task;
  1290. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1291. /* start the backup as a separate thread */
  1292. thread = PR_CreateThread(PR_USER_THREAD, task_backup_thread,
  1293. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1294. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1295. if (thread == NULL) {
  1296. LDAPDebug(LDAP_DEBUG_ANY,
  1297. "unable to create backup thread!\n", 0, 0, 0);
  1298. *returncode = LDAP_OPERATIONS_ERROR;
  1299. rv = SLAPI_DSE_CALLBACK_ERROR;
  1300. slapi_ch_free((void **)&mypb->pb_seq_val);
  1301. slapi_pblock_destroy(mypb);
  1302. goto out;
  1303. }
  1304. /* thread successful -- don't free the pb, let the thread do that. */
  1305. return SLAPI_DSE_CALLBACK_OK;
  1306. out:
  1307. if (task) {
  1308. destroy_task(1, task);
  1309. }
  1310. return rv;
  1311. }
  1312. static void task_restore_thread(void *arg)
  1313. {
  1314. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1315. Slapi_Task *task = pb->pb_task;
  1316. int rv;
  1317. g_incr_active_threadcnt();
  1318. slapi_task_begin(task, 1);
  1319. slapi_task_log_notice(task, "Beginning restore to '%s'",
  1320. pb->pb_plugin->plg_name);
  1321. LDAPDebug(LDAP_DEBUG_ANY, "Beginning restore to '%s'\n",
  1322. pb->pb_plugin->plg_name, 0, 0);
  1323. rv = (*pb->pb_plugin->plg_archive2db)(pb);
  1324. if (rv != 0) {
  1325. slapi_task_log_notice(task, "Restore failed (error %d)", rv);
  1326. slapi_task_log_status(task, "Restore failed (error %d)", rv);
  1327. LDAPDebug(LDAP_DEBUG_ANY, "Restore failed (error %d)\n", rv, 0, 0);
  1328. } else {
  1329. slapi_task_log_notice(task, "Restore finished.");
  1330. slapi_task_log_status(task, "Restore finished.");
  1331. LDAPDebug(LDAP_DEBUG_ANY, "Restore finished.\n", 0, 0, 0);
  1332. }
  1333. slapi_task_finish(task, rv);
  1334. slapi_ch_free((void **)&pb->pb_seq_val);
  1335. slapi_ch_free_string(&pb->pb_instance_name);
  1336. slapi_pblock_destroy(pb);
  1337. g_decr_active_threadcnt();
  1338. }
  1339. static int task_restore_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1340. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1341. {
  1342. Slapi_Backend *be = NULL;
  1343. const char *instance_name = NULL;
  1344. const char *archive_dir = NULL;
  1345. const char *my_database_type = NULL;
  1346. const char *database_type = "ldbm database";
  1347. char *cookie = NULL;
  1348. int rv = SLAPI_DSE_CALLBACK_OK;
  1349. Slapi_PBlock *mypb = NULL;
  1350. Slapi_Task *task = NULL;
  1351. PRThread *thread = NULL;
  1352. *returncode = LDAP_SUCCESS;
  1353. if (fetch_attr(e, "cn", NULL) == NULL) {
  1354. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1355. rv = SLAPI_DSE_CALLBACK_ERROR;
  1356. goto out;
  1357. }
  1358. /* archive dir name */
  1359. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1360. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1361. rv = SLAPI_DSE_CALLBACK_ERROR;
  1362. goto out;
  1363. }
  1364. /* database type */
  1365. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1366. if (NULL != my_database_type)
  1367. database_type = my_database_type;
  1368. instance_name = fetch_attr(e, "nsInstance", NULL);
  1369. /* get backend that has archive2db and the database type matches. */
  1370. be = slapi_get_first_backend (&cookie);
  1371. while (be) {
  1372. if (NULL != be->be_database->plg_archive2db &&
  1373. !strcasecmp(database_type, be->be_database->plg_name))
  1374. break;
  1375. be = (backend *)slapi_get_next_backend (cookie);
  1376. }
  1377. slapi_ch_free_string(&cookie);
  1378. if (NULL == be || NULL == be->be_database->plg_archive2db) {
  1379. LDAPDebug(LDAP_DEBUG_ANY,
  1380. "ERROR: no archive2db function defined.\n", 0, 0, 0);
  1381. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1382. rv = SLAPI_DSE_CALLBACK_ERROR;
  1383. goto out;
  1384. }
  1385. /* refuse to do an export on pre-V3 plugins. plugin api V3 is the one
  1386. * for DS 5.0 where the import/export stuff changed a lot.
  1387. */
  1388. if (! SLAPI_PLUGIN_IS_V3(be->be_database)) {
  1389. LDAPDebug(LDAP_DEBUG_ANY, "can't perform an restore with pre-V3 "
  1390. "backend plugin %s\n", be->be_database->plg_name, 0, 0);
  1391. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1392. rv = SLAPI_DSE_CALLBACK_ERROR;
  1393. goto out;
  1394. }
  1395. /* allocate new task now */
  1396. task = slapi_new_task(slapi_entry_get_ndn(e));
  1397. if (task == NULL) {
  1398. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1399. *returncode = LDAP_OPERATIONS_ERROR;
  1400. rv = SLAPI_DSE_CALLBACK_ERROR;
  1401. goto out;
  1402. }
  1403. mypb = slapi_pblock_new();
  1404. if (mypb == NULL) {
  1405. *returncode = LDAP_OPERATIONS_ERROR;
  1406. rv = SLAPI_DSE_CALLBACK_ERROR;
  1407. goto out;
  1408. }
  1409. mypb->pb_seq_val = slapi_ch_strdup(archive_dir);
  1410. mypb->pb_plugin = be->be_database;
  1411. if (NULL != instance_name)
  1412. mypb->pb_instance_name = slapi_ch_strdup(instance_name);
  1413. mypb->pb_task = task;
  1414. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1415. /* start the restore as a separate thread */
  1416. thread = PR_CreateThread(PR_USER_THREAD, task_restore_thread,
  1417. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1418. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1419. if (thread == NULL) {
  1420. LDAPDebug(LDAP_DEBUG_ANY,
  1421. "unable to create restore thread!\n", 0, 0, 0);
  1422. *returncode = LDAP_OPERATIONS_ERROR;
  1423. rv = SLAPI_DSE_CALLBACK_ERROR;
  1424. slapi_ch_free((void **)&mypb->pb_seq_val);
  1425. slapi_ch_free_string(&pb->pb_instance_name);
  1426. slapi_pblock_destroy(mypb);
  1427. goto out;
  1428. }
  1429. /* thread successful -- don't free the pb, let the thread do that. */
  1430. return SLAPI_DSE_CALLBACK_OK;
  1431. out:
  1432. if (task) {
  1433. destroy_task(1, task);
  1434. }
  1435. return rv;
  1436. }
  1437. static void task_index_thread(void *arg)
  1438. {
  1439. Slapi_PBlock *pb = (Slapi_PBlock *)arg;
  1440. Slapi_Task *task = pb->pb_task;
  1441. int rv;
  1442. g_incr_active_threadcnt();
  1443. slapi_task_begin(task, 1);
  1444. rv = (*pb->pb_plugin->plg_db2index)(pb);
  1445. if (rv != 0) {
  1446. slapi_task_log_notice(task, "Index failed (error %d)", rv);
  1447. slapi_task_log_status(task, "Index failed (error %d)", rv);
  1448. LDAPDebug(LDAP_DEBUG_ANY, "Index failed (error %d)\n", rv, 0, 0);
  1449. }
  1450. slapi_task_finish(task, rv);
  1451. charray_free(pb->pb_db2index_attrs);
  1452. slapi_ch_free((void **)&pb->pb_instance_name);
  1453. slapi_pblock_destroy(pb);
  1454. g_decr_active_threadcnt();
  1455. }
  1456. static int task_index_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1457. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg)
  1458. {
  1459. const char *instance_name;
  1460. int rv = SLAPI_DSE_CALLBACK_OK;
  1461. Slapi_Backend *be = NULL;
  1462. Slapi_Task *task = NULL;
  1463. Slapi_Attr *attr;
  1464. Slapi_Value *val = NULL;
  1465. char **indexlist = NULL;
  1466. int idx;
  1467. Slapi_PBlock *mypb = NULL;
  1468. PRThread *thread = NULL;
  1469. *returncode = LDAP_SUCCESS;
  1470. if (fetch_attr(e, "cn", NULL) == NULL) {
  1471. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1472. rv = SLAPI_DSE_CALLBACK_ERROR;
  1473. goto out;
  1474. }
  1475. if ((instance_name = fetch_attr(e, "nsInstance", NULL)) == NULL) {
  1476. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1477. rv = SLAPI_DSE_CALLBACK_ERROR;
  1478. goto out;
  1479. }
  1480. /* lookup the backend */
  1481. be = slapi_be_select_by_instance_name(instance_name);
  1482. if (be == NULL) {
  1483. LDAPDebug(LDAP_DEBUG_ANY, "can't import to nonexistent backend %s\n",
  1484. instance_name, 0, 0);
  1485. *returncode = LDAP_NO_SUCH_OBJECT;
  1486. return SLAPI_DSE_CALLBACK_ERROR;
  1487. }
  1488. if (be->be_database->plg_db2index == NULL) {
  1489. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no db2index function defined for "
  1490. "backend %s\n", be->be_database->plg_name, 0, 0);
  1491. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1492. return SLAPI_DSE_CALLBACK_ERROR;
  1493. }
  1494. /* normal indexes */
  1495. if (slapi_entry_attr_find(e, "nsIndexAttribute", &attr) == 0) {
  1496. for (idx = slapi_attr_first_value(attr, &val);
  1497. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1498. const char *indexname = slapi_value_get_string(val);
  1499. char *index = slapi_ch_smprintf("t%s", indexname);
  1500. if (index != NULL) {
  1501. charray_add(&indexlist, index);
  1502. }
  1503. }
  1504. }
  1505. /* vlv indexes */
  1506. if (slapi_entry_attr_find(e, "nsIndexVlvAttribute", &attr) == 0) {
  1507. for (idx = slapi_attr_first_value(attr, &val);
  1508. idx >= 0; idx = slapi_attr_next_value(attr, idx, &val)) {
  1509. const char *indexname = slapi_value_get_string(val);
  1510. char *index = slapi_ch_smprintf("T%s", indexname);
  1511. if (index != NULL) {
  1512. charray_add(&indexlist, index);
  1513. }
  1514. }
  1515. }
  1516. if (NULL == indexlist) {
  1517. LDAPDebug(LDAP_DEBUG_ANY, "no index is specified!\n", 0, 0, 0);
  1518. *returncode = LDAP_OPERATIONS_ERROR;
  1519. rv = SLAPI_DSE_CALLBACK_OK;
  1520. goto out;
  1521. }
  1522. /* allocate new task now */
  1523. task = slapi_new_task(slapi_entry_get_ndn(e));
  1524. if (task == NULL) {
  1525. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1526. *returncode = LDAP_OPERATIONS_ERROR;
  1527. rv = SLAPI_DSE_CALLBACK_ERROR;
  1528. goto out;
  1529. }
  1530. mypb = slapi_pblock_new();
  1531. if (mypb == NULL) {
  1532. *returncode = LDAP_OPERATIONS_ERROR;
  1533. rv = SLAPI_DSE_CALLBACK_ERROR;
  1534. goto out;
  1535. }
  1536. mypb->pb_backend = be;
  1537. mypb->pb_plugin = be->be_database;
  1538. mypb->pb_instance_name = slapi_ch_strdup(instance_name);
  1539. mypb->pb_db2index_attrs = indexlist;
  1540. mypb->pb_task = task;
  1541. mypb->pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1542. /* start the db2index as a separate thread */
  1543. thread = PR_CreateThread(PR_USER_THREAD, task_index_thread,
  1544. (void *)mypb, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1545. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1546. if (thread == NULL) {
  1547. LDAPDebug(LDAP_DEBUG_ANY,
  1548. "unable to create index thread!\n", 0, 0, 0);
  1549. rv = SLAPI_DSE_CALLBACK_ERROR;
  1550. slapi_ch_free((void **)&mypb->pb_instance_name);
  1551. slapi_pblock_destroy(mypb);
  1552. goto out;
  1553. }
  1554. /* thread successful -- don't free the pb, let the thread do that. */
  1555. return SLAPI_DSE_CALLBACK_OK;
  1556. out:
  1557. if (task) {
  1558. destroy_task(1, task);
  1559. }
  1560. if (indexlist) {
  1561. charray_free(indexlist);
  1562. }
  1563. return rv;
  1564. }
  1565. static int
  1566. task_upgradedb_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1567. int *returncode, char *returntext, void *arg)
  1568. {
  1569. int rv = SLAPI_DSE_CALLBACK_OK;
  1570. Slapi_Backend *be = NULL;
  1571. Slapi_Task *task = NULL;
  1572. Slapi_PBlock mypb;
  1573. const char *archive_dir = NULL;
  1574. const char *force = NULL;
  1575. const char *database_type = "ldbm database";
  1576. const char *my_database_type = NULL;
  1577. char *cookie = NULL;
  1578. *returncode = LDAP_SUCCESS;
  1579. if (fetch_attr(e, "cn", NULL) == NULL) {
  1580. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1581. rv = SLAPI_DSE_CALLBACK_ERROR;
  1582. goto out;
  1583. }
  1584. /* archive dir name */
  1585. if ((archive_dir = fetch_attr(e, "nsArchiveDir", NULL)) == NULL) {
  1586. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1587. rv = SLAPI_DSE_CALLBACK_ERROR;
  1588. goto out;
  1589. }
  1590. /* database type */
  1591. my_database_type = fetch_attr(e, "nsDatabaseType", NULL);
  1592. if (NULL != my_database_type)
  1593. database_type = my_database_type;
  1594. /* force to reindex? */
  1595. force = fetch_attr(e, "nsForceToReindex", NULL);
  1596. /* get backend that has db2archive and the database type matches. */
  1597. be = slapi_get_first_backend(&cookie);
  1598. while (be) {
  1599. if (NULL != be->be_database->plg_upgradedb)
  1600. break;
  1601. be = (backend *)slapi_get_next_backend (cookie);
  1602. }
  1603. slapi_ch_free_string(&cookie);
  1604. if (NULL == be) {
  1605. LDAPDebug(LDAP_DEBUG_ANY,
  1606. "ERROR: no upgradedb is defined.\n", 0, 0, 0);
  1607. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1608. rv = SLAPI_DSE_CALLBACK_ERROR;
  1609. goto out;
  1610. }
  1611. if (NULL == be->be_database->plg_upgradedb ||
  1612. strcasecmp(database_type, be->be_database->plg_name)) {
  1613. LDAPDebug(LDAP_DEBUG_ANY,
  1614. "ERROR: no upgradedb is defined in %s.\n",
  1615. be->be_database->plg_name, 0, 0);
  1616. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1617. rv = SLAPI_DSE_CALLBACK_ERROR;
  1618. goto out;
  1619. }
  1620. /* allocate new task now */
  1621. task = slapi_new_task(slapi_entry_get_ndn(e));
  1622. if (task == NULL) {
  1623. LDAPDebug(LDAP_DEBUG_ANY, "unable to allocate new task!\n", 0, 0, 0);
  1624. *returncode = LDAP_OPERATIONS_ERROR;
  1625. rv = SLAPI_DSE_CALLBACK_ERROR;
  1626. goto out;
  1627. }
  1628. /* NGK - This could use some cleanup to use the SLAPI task API, such as slapi_task_begin() */
  1629. task->task_work = 1;
  1630. task->task_progress = 0;
  1631. memset(&mypb, 0, sizeof(mypb));
  1632. mypb.pb_backend = be;
  1633. mypb.pb_plugin = be->be_database;
  1634. if (force && 0 == strcasecmp(force, "true"))
  1635. mypb.pb_seq_type = SLAPI_UPGRADEDB_FORCE; /* force; reindex all regardless the dbversion */
  1636. mypb.pb_seq_val = slapi_ch_strdup(archive_dir);
  1637. mypb.pb_task = task;
  1638. mypb.pb_task_flags = SLAPI_TASK_RUNNING_AS_TASK;
  1639. rv = (mypb.pb_plugin->plg_upgradedb)(&mypb);
  1640. if (rv == 0) {
  1641. slapi_entry_attr_set_charptr(e, TASK_LOG_NAME, "");
  1642. slapi_entry_attr_set_charptr(e, TASK_STATUS_NAME, "");
  1643. slapi_entry_attr_set_int(e, TASK_PROGRESS_NAME, task->task_progress);
  1644. slapi_entry_attr_set_int(e, TASK_WORK_NAME, task->task_work);
  1645. }
  1646. out:
  1647. slapi_ch_free((void **)&mypb.pb_seq_val);
  1648. if (rv != 0) {
  1649. if (task)
  1650. destroy_task(1, task);
  1651. *returncode = LDAP_OPERATIONS_ERROR;
  1652. return SLAPI_DSE_CALLBACK_ERROR;
  1653. }
  1654. *returncode = LDAP_SUCCESS;
  1655. return SLAPI_DSE_CALLBACK_OK;
  1656. }
  1657. /*
  1658. * sysconfig reload task
  1659. *
  1660. * dn: cn=keytab_update,cn=sysconfig reload,cn=tasks,cn=config
  1661. * objectclass: top
  1662. * objectclass: extensibleObject
  1663. * cn: keytab_update
  1664. * sysconfigfile: /etc/sysconfig/dirsrv-localhost
  1665. * logchanges: <boolean>
  1666. *
  1667. * Reload environment variables from the instance sysconfig file, or
  1668. * any file using the following formats:
  1669. *
  1670. * VARIABLE=value
  1671. * export VARIABLE=value
  1672. * set VARIABLE value
  1673. * unset VARIABLE
  1674. * setenv VARIABLE value
  1675. * unsetenv VARIABLE
  1676. */
  1677. static int
  1678. task_sysconfig_reload_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode,
  1679. char *returntext, void *arg)
  1680. {
  1681. FILE *file = NULL;
  1682. char *filename = NULL;
  1683. PRBool logchanges = 0;
  1684. int rc = SLAPI_DSE_CALLBACK_OK;
  1685. *returncode = LDAP_SUCCESS;
  1686. if(( filename = slapi_entry_attr_get_charptr(e, TASK_SYSCONFIG_FILE_ATTR))){
  1687. file = fopen ( filename, "r" );
  1688. } else {
  1689. *returncode = LDAP_OPERATIONS_ERROR;
  1690. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE, "missing required attribute \"%s\".",
  1691. TASK_SYSCONFIG_FILE_ATTR);
  1692. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: %s\n", returntext, 0, 0);
  1693. rc = SLAPI_DSE_CALLBACK_ERROR;
  1694. goto done;
  1695. }
  1696. /* see if we should log the changes made */
  1697. logchanges = slapi_entry_attr_get_bool(e, TASK_SYSCONFIG_LOGCHANGES_ATTR);
  1698. if ( file != NULL ){
  1699. char line[4096];
  1700. char *s = NULL;
  1701. /* fgets() reads in at most one less than size characters */
  1702. char *end_of_line = line + sizeof(line) - 1;
  1703. if(logchanges){
  1704. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: processing file (%s)\n",
  1705. filename, 0 , 0);
  1706. }
  1707. while ( fgets ( line, sizeof line, file ) != NULL ){
  1708. if(line[0] == '#'){
  1709. /* skip comments */
  1710. continue;
  1711. } else {
  1712. char env_value[sizeof(line)];
  1713. char env_var[sizeof(line)];
  1714. int using_setenv = 0;
  1715. int value_index = 0;
  1716. int start_value = 0;
  1717. int var_index = 0;
  1718. int inquotes = 0;
  1719. memset(env_var, 0, sizeof(env_var));
  1720. memset(env_value, 0, sizeof(env_value));
  1721. /*
  1722. * Remove leading spaces and tabs
  1723. */
  1724. for (s = line; s && *s; s++){
  1725. if(*s != ' ' && *s != '\t'){
  1726. break;
  1727. }
  1728. }
  1729. /*
  1730. * Check for "export", "setenv", and "unsetenv" assignments
  1731. */
  1732. if(strncmp(s, "export ", 7) == 0 ||
  1733. strncmp(s, "set ", 4) == 0 ||
  1734. strncmp(s, "unset ", 6) == 0 ||
  1735. strncmp(s, "setenv ", 7) == 0 ||
  1736. strncmp(s, "unsetenv ", 9) == 0 )
  1737. {
  1738. if(*s == 's' || *s == 'u'){ /* */
  1739. /*
  1740. * Using setenv/unsetenv/set/unset, so we need to handle spaces
  1741. * differently for these assignments.
  1742. */
  1743. using_setenv = 1;
  1744. }
  1745. if(strncmp(s, "export ", 7) == 0){
  1746. /* strip off "export " */
  1747. s = s + 7;
  1748. } else if(strncmp(s, "set ", 4) == 0){
  1749. /* strip off "set " */
  1750. s = s + 4;
  1751. } else if(strncmp(s, "unset ", 6) == 0){
  1752. /* strip off "unset " */
  1753. s = s + 6;
  1754. } else if(strncmp(s, "setenv ", 7) == 0){
  1755. /* strip off "setenv " */
  1756. s = s + 7;
  1757. } else if(strncmp(s, "unsetenv ", 9) == 0){
  1758. /* strip off "unsetenv " */
  1759. s = s + 9;
  1760. }
  1761. while(*s == ' ' || *s == '\t'){
  1762. /* remove any extra spaces/tabs between the assignment cmd and the name */
  1763. s++;
  1764. }
  1765. }
  1766. /*
  1767. * Start parsing the names and values
  1768. */
  1769. for (; s && (s < end_of_line) && *s; s++){
  1770. /*
  1771. * If using "setenv", allow the first space/tab only, and start on the env value
  1772. */
  1773. if(using_setenv && (*s == ' ' || *s == '\t')){
  1774. using_setenv = 0; /* finished doing special space parsing for setenv */
  1775. start_value = 1; /* start working on the value */
  1776. while(*s == ' ' || *s == '\t'){
  1777. /* remove any extra spaces/tabs between variable name and value */
  1778. s++;
  1779. }
  1780. } else if( ((*s == ';' || *s == ' ') && !inquotes) || *s == '\0' || *s == '\n' || *s == '\r'){
  1781. /* we're done processing the value */
  1782. break;
  1783. }
  1784. /* need to handle quoted values */
  1785. if(*s == '"'){
  1786. if(inquotes){
  1787. inquotes = 0;
  1788. } else {
  1789. inquotes = 1;
  1790. }
  1791. }
  1792. if(start_value){
  1793. /* Build the environment variable value */
  1794. env_value[value_index] = *s;
  1795. value_index++;
  1796. } else if(*s == '='){
  1797. /* Start on the environment variable value next */
  1798. start_value = 1;
  1799. } else {
  1800. /* Build the environment variable name. skip "export" */
  1801. env_var[var_index] = *s;
  1802. var_index++;
  1803. }
  1804. }
  1805. if(var_index > 0){
  1806. /* Update the environment variable */
  1807. if(setenv(env_var, env_value, 1) != 0){
  1808. *returncode = LDAP_OPERATIONS_ERROR;
  1809. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"failed to set (%s)", env_var);
  1810. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: %s\n",returntext,0,0);
  1811. rc = SLAPI_DSE_CALLBACK_ERROR;
  1812. break;
  1813. }
  1814. if(logchanges){
  1815. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: set (%s) to (%s)\n",
  1816. env_var, env_value , 0);
  1817. }
  1818. }
  1819. }
  1820. }
  1821. fclose ( file );
  1822. } else {
  1823. *returncode = LDAP_OPERATIONS_ERROR;
  1824. PR_snprintf (returntext, SLAPI_DSE_RETURNTEXT_SIZE,"failed to open file \"%s\" (%s)",
  1825. filename, strerror(errno));
  1826. LDAPDebug(LDAP_DEBUG_ANY, "sysconfig reload task: %s\n", returntext, 0, 0);
  1827. rc = SLAPI_DSE_CALLBACK_ERROR;
  1828. }
  1829. done:
  1830. slapi_ch_free_string(&filename);
  1831. return rc;
  1832. }
  1833. /*
  1834. * Add the nsTombstoneCSN attribute/value to the entry.
  1835. */
  1836. static int
  1837. fixup_tombstone(Slapi_PBlock *pb, char *suffix, Slapi_Entry *e, int *fixup_count)
  1838. {
  1839. LDAPMod mod;
  1840. LDAPMod *mods[2];
  1841. const CSN *tombstone_csn = NULL;
  1842. char deletion_csn_str[CSN_STRSIZE];
  1843. char *val[2];
  1844. int rc = LDAP_SUCCESS;
  1845. if((tombstone_csn = entry_get_deletion_csn(e))){
  1846. slapi_log_error(SLAPI_LOG_REPL, TASK_TOMBSTONE_FIXUP,
  1847. "Fixing tombstone (%s)\n", slapi_entry_get_dn(e));
  1848. /* We have an entry tombstone that needs fixing */
  1849. slapi_pblock_init(pb);
  1850. csn_as_string(tombstone_csn, PR_FALSE, deletion_csn_str);
  1851. mods[0] = &mod;
  1852. mods[1] = 0;
  1853. val[0] = deletion_csn_str;
  1854. val[1] = 0;
  1855. mod.mod_op = LDAP_MOD_ADD;
  1856. mod.mod_type = SLAPI_ATTR_TOMBSTONE_CSN;
  1857. mod.mod_values = val;
  1858. slapi_modify_internal_set_pb_ext( pb, slapi_entry_get_sdn(e),
  1859. mods, 0, 0, (void *)plugin_get_default_component_id(),
  1860. OP_FLAG_TOMBSTONE_ENTRY | OP_FLAG_TOMBSTONE_FIXUP);
  1861. slapi_modify_internal_pb(pb);
  1862. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1863. pblock_done(pb);
  1864. if(rc == LDAP_SUCCESS){
  1865. (*fixup_count)++;
  1866. }
  1867. }
  1868. return rc;
  1869. }
  1870. /*
  1871. * Strip out nsTombstoneCSN so the task can be run again to remove them. Used
  1872. * solely for testing the fixup task.
  1873. */
  1874. static void
  1875. strip_tombstone(Slapi_PBlock *pb, char *suffix, Slapi_Entry *e, int *strip_count)
  1876. {
  1877. LDAPMod mod;
  1878. LDAPMod *mods[2];
  1879. int rc = 0;
  1880. slapi_log_error(SLAPI_LOG_REPL, TASK_TOMBSTONE_FIXUP,
  1881. "Stripping tombstone (%s)\n", slapi_entry_get_dn(e));
  1882. /* We have an entry tombstone that needs stripping */
  1883. slapi_pblock_init(pb);
  1884. mods[0] = &mod;
  1885. mods[1] = 0;
  1886. mod.mod_op = LDAP_MOD_DELETE;
  1887. mod.mod_type = SLAPI_ATTR_TOMBSTONE_CSN;
  1888. mod.mod_values = NULL;
  1889. slapi_modify_internal_set_pb_ext( pb, slapi_entry_get_sdn(e),
  1890. mods, 0, 0, (void *)plugin_get_default_component_id(),
  1891. OP_FLAG_TOMBSTONE_ENTRY | OP_FLAG_TOMBSTONE_FIXUP);
  1892. slapi_modify_internal_pb(pb);
  1893. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1894. pblock_done(pb);
  1895. if(rc == LDAP_SUCCESS){
  1896. (*strip_count)++;
  1897. } else {
  1898. slapi_log_error(SLAPI_LOG_REPL, TASK_TOMBSTONE_FIXUP,
  1899. "Stripping tombstone (%s) failed, error %d\n", slapi_entry_get_dn(e), rc);
  1900. }
  1901. }
  1902. struct task_tombstone_data
  1903. {
  1904. char **base;
  1905. int stripcsn;
  1906. Slapi_Task *task;
  1907. };
  1908. /*
  1909. * Fix tombstone thread - add missing nsTombstoneCSN
  1910. */
  1911. static void
  1912. task_fixup_tombstone_thread(void *arg)
  1913. {
  1914. struct task_tombstone_data *task_data = arg;
  1915. Slapi_Entry **entries = NULL;
  1916. Slapi_Task *task = task_data->task;
  1917. char **base = task_data->base;
  1918. char *filter = NULL;
  1919. int fixup_count = 0;
  1920. int rc, i, j;
  1921. if (!task) {
  1922. return; /* no task */
  1923. }
  1924. slapi_task_inc_refcount(task);
  1925. slapi_log_error(SLAPI_LOG_PLUGIN, TASK_TOMBSTONE_FIXUP,
  1926. "fixup_tombstone_task_thread --> refcount incremented.\n" );
  1927. slapi_task_begin(task, 1);
  1928. slapi_task_log_notice(task, "Beginning tombstone fixup task...\n");
  1929. slapi_log_error(SLAPI_LOG_REPL, TASK_TOMBSTONE_FIXUP,
  1930. "Beginning tombstone fixup task...\n");
  1931. if(task_data->stripcsn){
  1932. /* find tombstones with nsTombstoneCSN */
  1933. filter = "(&(nstombstonecsn=*)(objectclass=nsTombstone))";
  1934. } else {
  1935. /* find tombstones missing nsTombstoneCSN */
  1936. filter = "(&(!(nstombstonecsn=*))(objectclass=nsTombstone))";
  1937. }
  1938. /* Okay check the specified backends only */
  1939. for(i = 0; base && base[i]; i++){
  1940. Slapi_PBlock *search_pb = NULL;
  1941. if (slapi_is_shutting_down()) {
  1942. rc = -1;
  1943. goto bail;
  1944. }
  1945. search_pb = slapi_pblock_new();
  1946. /* find entries that need fixing... */
  1947. slapi_search_internal_set_pb(search_pb, base[i], LDAP_SCOPE_SUBTREE,
  1948. filter, NULL, 0, NULL, NULL, plugin_get_default_component_id(), 0);
  1949. slapi_search_internal_pb(search_pb);
  1950. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1951. if (rc != LDAP_SUCCESS) {
  1952. slapi_task_log_notice(task,
  1953. "Failed to search backend for tombstones, error %d\n", rc);
  1954. slapi_log_error(SLAPI_LOG_REPL, TASK_TOMBSTONE_FIXUP,
  1955. "Failed to search backend for tombstones, error %d\n", rc);
  1956. slapi_pblock_destroy(search_pb);
  1957. goto bail;
  1958. }
  1959. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1960. if (entries) {
  1961. Slapi_PBlock *fixup_pb = slapi_pblock_new();
  1962. /* process all the tombstone entries */
  1963. for (j = 0; entries[j]; j++){
  1964. if(task_data->stripcsn){
  1965. /* strip nsTombstoneCSN - used to testing */
  1966. strip_tombstone(fixup_pb, base[i], entries[j], &fixup_count);
  1967. } else if((rc = fixup_tombstone(fixup_pb, base[i], entries[j], &fixup_count))){
  1968. /* Failed to update tombstone, log it and move on... */
  1969. slapi_task_log_notice(task,
  1970. "Failed to update tombstone entry (%s) error %d\n",
  1971. slapi_entry_get_dn(entries[j]), rc);
  1972. slapi_log_error(SLAPI_LOG_FATAL, TASK_TOMBSTONE_FIXUP,
  1973. "Failed to update tombstone entry (%s) error %d\n",
  1974. slapi_entry_get_dn(entries[j]), rc);
  1975. }
  1976. }
  1977. slapi_free_search_results_internal(search_pb);
  1978. slapi_pblock_destroy(fixup_pb);
  1979. }
  1980. slapi_pblock_destroy(search_pb);
  1981. slapi_task_inc_progress(task);
  1982. }
  1983. slapi_task_log_notice(task, "%s %d tombstones.\n",
  1984. task_data->stripcsn ? "Stripped" : "Fixed", fixup_count);
  1985. slapi_log_error(SLAPI_LOG_REPL, TASK_TOMBSTONE_FIXUP, "%s %d tombstones.\n",
  1986. task_data->stripcsn ? "Stripped" : "Fixed", fixup_count);
  1987. slapi_task_inc_progress(task);
  1988. bail:
  1989. slapi_task_finish(task, rc);
  1990. slapi_task_dec_refcount(task);
  1991. slapi_log_error(SLAPI_LOG_PLUGIN, TASK_TOMBSTONE_FIXUP,
  1992. "fixup_tombstone_task_thread <-- refcount decremented.\n" );
  1993. }
  1994. /*
  1995. * task_fixup_tombstones_add
  1996. *
  1997. * Check all the existing tombstones and add nsTombstoneCSN if missing.
  1998. *
  1999. * dn: cn=fixem,cn=fixup tombstones,cn=tasks,cn=config
  2000. * objectclass: top
  2001. * objectclass: extensibleObject
  2002. * cn: fixem
  2003. * backend: userRoot
  2004. * suffix: dc=example,dc=com
  2005. * stripcsn: yes
  2006. *
  2007. * backend & suffix are optional. If skipped, all backends/suffixes are
  2008. * checked. Multiple suffixes can also be specified.
  2009. *
  2010. * Hidden option: "stripcsn" is strictly used to verify the fixup task: run
  2011. * the task using the strip option to strip tombstones of "nsTombstoneCSN",
  2012. * then run task, without the strip option, to add "nsTombstoneCSN" back.
  2013. */
  2014. static int
  2015. task_fixup_tombstones_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2016. int *returncode, char *returntext, void *arg)
  2017. {
  2018. Slapi_Backend *be = NULL;
  2019. Slapi_Task *task = NULL;
  2020. struct task_tombstone_data *task_data = NULL;
  2021. const Slapi_DN *base_sdn = NULL;
  2022. PRThread *thread = NULL;
  2023. char **backend = NULL;
  2024. char **suffix = NULL;
  2025. char **base = NULL;
  2026. char *stripcsn = NULL;
  2027. int i;
  2028. /*
  2029. * Get the task options. We will store all the "backends" in the suffix array.
  2030. */
  2031. if((suffix = slapi_entry_attr_get_charray(e, TASK_TOMBSTONE_FIXUP_SUFFIX))){
  2032. for (i = 0; suffix && suffix[i]; i++){
  2033. char *dn = slapi_create_dn_string("%s", suffix[i]);
  2034. if(dn){
  2035. if(slapi_dn_syntax_check(pb, dn, 1)){
  2036. /* invalid suffix name */
  2037. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2038. "Invalid DN (%s) used for \"suffix\"\n", suffix[i]);
  2039. *returncode = LDAP_INVALID_DN_SYNTAX;
  2040. goto done;
  2041. } else {
  2042. slapi_ch_array_add(&base, dn);
  2043. }
  2044. }
  2045. }
  2046. }
  2047. if((backend = slapi_entry_attr_get_charray(e, TASK_TOMBSTONE_FIXUP_BACKEND))){
  2048. for (i = 0; backend && backend[i]; i++){
  2049. if((be = slapi_be_select_by_instance_name(backend[i]))){
  2050. if((base_sdn = slapi_be_getsuffix(be, 0))){
  2051. slapi_ch_array_add(&base, slapi_ch_strdup(slapi_sdn_get_ndn(base_sdn)));
  2052. } else {
  2053. /* failed to get a suffix */
  2054. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2055. "Failed to find a suffix for the backend(%s)\n", backend[i]);
  2056. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2057. goto done;
  2058. }
  2059. } else {
  2060. /* Failed to find a backend */
  2061. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2062. "Failed to find a backend using (%s)\n", backend[i]);
  2063. *returncode = LDAP_UNWILLING_TO_PERFORM;
  2064. goto done;
  2065. }
  2066. }
  2067. }
  2068. /*
  2069. * If suffix is NULL, we check all the backends
  2070. */
  2071. if(base == NULL){
  2072. char *cookie = NULL;
  2073. /* Gather all the backends */
  2074. be = slapi_get_first_backend(&cookie);
  2075. while(be){
  2076. if((base_sdn = slapi_be_getsuffix(be, 0)) && !be->be_private){
  2077. const char *suf = slapi_sdn_get_ndn(base_sdn);
  2078. /* Need to skip the retro changelog */
  2079. if(strcmp(suf, "cn=changelog"))
  2080. {
  2081. slapi_ch_array_add(&base, slapi_ch_strdup(suf));
  2082. }
  2083. }
  2084. be = slapi_get_next_backend(cookie);
  2085. }
  2086. slapi_ch_free_string(&cookie);
  2087. }
  2088. task = slapi_new_task(slapi_entry_get_ndn(e));
  2089. /* register our destructor for cleaning up our private data */
  2090. slapi_task_set_destructor_fn(task, fixup_tombstone_task_destructor);
  2091. task_data = (struct task_tombstone_data *)slapi_ch_calloc(1, sizeof(struct task_tombstone_data));
  2092. task_data->base = base;
  2093. task_data->task = task;
  2094. if((stripcsn = slapi_entry_attr_get_charptr(e, TASK_TOMBSTONE_FIXUP_STRIPCSN))){
  2095. if(strcasecmp(stripcsn, "yes") == 0 || strcasecmp(stripcsn, "on") == 0){
  2096. task_data->stripcsn = 1;
  2097. }
  2098. slapi_ch_free_string(&stripcsn);
  2099. }
  2100. /* start the db2index as a separate thread */
  2101. thread = PR_CreateThread(PR_USER_THREAD, task_fixup_tombstone_thread,
  2102. (void *)task_data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2103. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2104. if (thread == NULL) {
  2105. LDAPDebug(LDAP_DEBUG_ANY,
  2106. "task_fixup_tombstones_add: unable to create index thread!\n", 0, 0, 0);
  2107. *returncode = LDAP_OPERATIONS_ERROR;
  2108. slapi_task_finish(task, *returncode);
  2109. slapi_ch_array_free(base);
  2110. slapi_ch_free((void **)&task_data);
  2111. return SLAPI_DSE_CALLBACK_ERROR;
  2112. }
  2113. done:
  2114. slapi_ch_array_free(suffix);
  2115. slapi_ch_array_free(backend);
  2116. if (*returncode != LDAP_SUCCESS){
  2117. return SLAPI_DSE_CALLBACK_ERROR;
  2118. }
  2119. return SLAPI_DSE_CALLBACK_OK;
  2120. }
  2121. static void
  2122. fixup_tombstone_task_destructor(Slapi_Task *task)
  2123. {
  2124. slapi_log_error(SLAPI_LOG_PLUGIN, TASK_TOMBSTONE_FIXUP,
  2125. "fixup_tombstone_task_destructor -->\n" );
  2126. if (task) {
  2127. struct task_tombstone_data *mydata = (struct task_tombstone_data *)slapi_task_get_data(task);
  2128. while (slapi_task_get_refcount(task) > 0) {
  2129. /* Yield to wait for the fixup task finishes. */
  2130. DS_Sleep (PR_MillisecondsToInterval(100));
  2131. }
  2132. if (mydata) {
  2133. slapi_ch_array_free(mydata->base);
  2134. slapi_ch_free((void **)&mydata);
  2135. }
  2136. }
  2137. slapi_log_error(SLAPI_LOG_PLUGIN, TASK_TOMBSTONE_FIXUP,
  2138. "fixup_tombstone_task_destructor <--\n" );
  2139. }
  2140. /* cleanup old tasks that may still be in the DSE from a previous session
  2141. * (this can happen if the server crashes [no matter how unlikely we like
  2142. * to think that is].)
  2143. */
  2144. void task_cleanup(void)
  2145. {
  2146. Slapi_PBlock *pb = slapi_pblock_new();
  2147. Slapi_Entry **entries = NULL;
  2148. int ret = 0, i, x;
  2149. Slapi_DN *rootDN;
  2150. slapi_search_internal_set_pb(pb, TASK_BASE_DN, LDAP_SCOPE_SUBTREE,
  2151. "(objectclass=*)", NULL, 0, NULL, NULL,
  2152. (void *)plugin_get_default_component_id(), 0);
  2153. slapi_search_internal_pb(pb);
  2154. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2155. if (ret != LDAP_SUCCESS) {
  2156. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: entire cn=tasks tree seems to "
  2157. "be AWOL!\n", 0, 0, 0);
  2158. slapi_pblock_destroy(pb);
  2159. return;
  2160. }
  2161. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2162. if (NULL == entries) {
  2163. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: entire cn=tasks tree seems to "
  2164. "be AWOL!\n", 0, 0, 0);
  2165. slapi_pblock_destroy(pb);
  2166. return;
  2167. }
  2168. rootDN = slapi_sdn_new_dn_byval(TASK_BASE_DN);
  2169. /* rotate through entries, skipping the base dn */
  2170. for (i = 0; entries[i] != NULL; i++) {
  2171. const Slapi_DN *sdn = slapi_entry_get_sdn_const(entries[i]);
  2172. Slapi_PBlock *mypb;
  2173. Slapi_Operation *op;
  2174. if (slapi_sdn_compare(sdn, rootDN) == 0)
  2175. continue;
  2176. mypb = slapi_pblock_new();
  2177. if (mypb == NULL) {
  2178. continue;
  2179. }
  2180. slapi_delete_internal_set_pb(mypb, slapi_sdn_get_dn(sdn), NULL, NULL,
  2181. plugin_get_default_component_id(), 0);
  2182. /* Make sure these deletes don't appear in the audit and change logs */
  2183. slapi_pblock_get(mypb, SLAPI_OPERATION, &op);
  2184. operation_set_flag(op, OP_FLAG_ACTION_NOLOG);
  2185. x = 1;
  2186. slapi_pblock_set(mypb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &x);
  2187. slapi_delete_internal_pb(mypb);
  2188. slapi_pblock_destroy(mypb);
  2189. }
  2190. slapi_sdn_free(&rootDN);
  2191. slapi_free_search_results_internal(pb);
  2192. slapi_pblock_destroy(pb);
  2193. }
  2194. void task_init(void)
  2195. {
  2196. global_task_lock = PR_NewLock();
  2197. if (global_task_lock == NULL) {
  2198. LDAPDebug(LDAP_DEBUG_ANY, "unable to create global tasks lock! "
  2199. "(that's bad)\n", 0, 0, 0);
  2200. return;
  2201. }
  2202. slapi_task_register_handler("import", task_import_add);
  2203. slapi_task_register_handler("export", task_export_add);
  2204. slapi_task_register_handler("backup", task_backup_add);
  2205. slapi_task_register_handler("restore", task_restore_add);
  2206. slapi_task_register_handler("index", task_index_add);
  2207. slapi_task_register_handler("upgradedb", task_upgradedb_add);
  2208. slapi_task_register_handler("sysconfig reload", task_sysconfig_reload_add);
  2209. slapi_task_register_handler("fixup tombstones", task_fixup_tombstones_add);
  2210. }
  2211. /* called when the server is shutting down -- abort all existing tasks */
  2212. void task_shutdown(void)
  2213. {
  2214. Slapi_Task *task;
  2215. int found_any = 0;
  2216. /* first, cancel all tasks */
  2217. PR_Lock(global_task_lock);
  2218. shutting_down = 1;
  2219. for (task = global_task_list; task; task = task->next) {
  2220. if ((task->task_state != SLAPI_TASK_CANCELLED) &&
  2221. (task->task_state != SLAPI_TASK_FINISHED)) {
  2222. task->task_state = SLAPI_TASK_CANCELLED;
  2223. if (task->cancel) {
  2224. LDAPDebug(LDAP_DEBUG_ANY, "Cancelling task '%s'\n",
  2225. task->task_dn, 0, 0);
  2226. (*task->cancel)(task);
  2227. found_any = 1;
  2228. }
  2229. }
  2230. }
  2231. if (found_any) {
  2232. /* give any tasks 1 second to say their last rites */
  2233. DS_Sleep(PR_SecondsToInterval( 1 ));
  2234. }
  2235. while (global_task_list) {
  2236. destroy_task(0, global_task_list);
  2237. }
  2238. PR_Unlock(global_task_lock);
  2239. }