task.c 84 KB

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