1
0

task.c 86 KB

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