task.c 84 KB

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