2 * This module dumps and/or loads the Citadel database in XML format.
4 * Copyright (c) 1987-2016 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 * Explanation of <progress> tags:
19 * 2% finished exporting configuration
20 * 7% finished exporting users
21 * 12% finished exporting openids
22 * 17% finished exporting rooms
23 * 18% finished exporting floors
24 * 25% finished exporting visits
25 * 100% finished exporting messages
36 #include <sys/types.h>
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
54 #include <libcitadel.h>
57 #include "citserver.h"
63 #include "euidindex.h"
64 #include "ctdl_module.h"
66 #define END_OF_MESSAGE "---eom---dbd---"
68 char migr_tempfilename1[PATH_MAX];
69 char migr_tempfilename2[PATH_MAX];
70 FILE *migr_global_message_list;
72 char *ikey = NULL; // If we're importing a config key we store it here.
75 /******************************************************************************
76 * Code which implements the export appears in this section *
77 ******************************************************************************/
80 * Output a string to the client with these characters escaped: & < > " '
82 void xml_strout(char *str) {
92 client_write(HKEY("""));
94 else if (*c == '\'') {
95 client_write(HKEY("'"));
98 client_write(HKEY("<"));
100 else if (*c == '>') {
101 client_write(HKEY(">"));
103 else if (*c == '&') {
104 client_write(HKEY("&"));
115 * Export a user record as XML
117 void migr_export_users_backend(struct ctdluser *buf, void *data) {
118 client_write(HKEY("<user>\n"));
119 cprintf("<u_version>%d</u_version>\n", buf->version);
120 cprintf("<u_uid>%ld</u_uid>\n", (long)buf->uid);
121 client_write(HKEY("<u_password>")); xml_strout(buf->password); client_write(HKEY("</u_password>\n"));
122 cprintf("<u_flags>%u</u_flags>\n", buf->flags);
123 cprintf("<u_timescalled>%ld</u_timescalled>\n", buf->timescalled);
124 cprintf("<u_posted>%ld</u_posted>\n", buf->posted);
125 cprintf("<u_axlevel>%d</u_axlevel>\n", buf->axlevel);
126 cprintf("<u_usernum>%ld</u_usernum>\n", buf->usernum);
127 cprintf("<u_lastcall>%ld</u_lastcall>\n", (long)buf->lastcall);
128 cprintf("<u_USuserpurge>%d</u_USuserpurge>\n", buf->USuserpurge);
129 client_write(HKEY("<u_fullname>")); xml_strout(buf->fullname); client_write(HKEY("</u_fullname>\n"));
130 cprintf("<u_msgnum_bio>%ld</u_msgnum_bio>\n", buf->msgnum_bio);
131 cprintf("<u_msgnum_pic>%ld</u_msgnum_pic>\n", buf->msgnum_pic);
132 client_write(HKEY("</user>\n"));
136 void migr_export_users(void) {
137 ForEachUser(migr_export_users_backend, NULL);
141 void migr_export_room_msg(long msgnum, void *userdata) {
142 cprintf("%ld\n", msgnum);
143 fprintf(migr_global_message_list, "%ld\n", msgnum);
147 void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
148 client_write(HKEY("<room>\n"));
149 client_write(HKEY("<QRname>")); xml_strout(buf->QRname); client_write(HKEY("</QRname>\n"));
150 client_write(HKEY("<QRpasswd>")); xml_strout(buf->QRpasswd); client_write(HKEY("</QRpasswd>\n"));
151 cprintf("<QRroomaide>%ld</QRroomaide>\n", buf->QRroomaide);
152 cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
153 cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
154 cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
155 if (buf->QRflags & QR_DIRECTORY) {
156 client_write(HKEY("<QRdirname>"));
157 xml_strout(buf->QRdirname);
158 client_write(HKEY("</QRdirname>\n"));
160 cprintf("<QRinfo>%ld</QRinfo>\n", buf->QRinfo);
161 cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
162 cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
163 cprintf("<QRexpire_mode>%d</QRexpire_mode>\n", buf->QRep.expire_mode);
164 cprintf("<QRexpire_value>%d</QRexpire_value>\n", buf->QRep.expire_value);
165 cprintf("<QRnumber>%ld</QRnumber>\n", buf->QRnumber);
166 cprintf("<QRorder>%d</QRorder>\n", buf->QRorder);
167 cprintf("<QRflags2>%u</QRflags2>\n", buf->QRflags2);
168 cprintf("<QRdefaultview>%d</QRdefaultview>\n", buf->QRdefaultview);
169 client_write(HKEY("</room>\n"));
171 /* message list goes inside this tag */
173 CtdlGetRoom(&CC->room, buf->QRname);
174 client_write(HKEY("<room_messages>"));
175 client_write(HKEY("<FRname>")); xml_strout(CC->room.QRname); client_write(HKEY("</FRname>\n"));
176 client_write(HKEY("<FRmsglist>"));
177 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_export_room_msg, NULL);
178 client_write(HKEY("</FRmsglist>\n"));
179 client_write(HKEY("</room_messages>\n"));
185 void migr_export_rooms(void) {
187 migr_global_message_list = fopen(migr_tempfilename1, "w");
188 if (migr_global_message_list != NULL) {
189 CtdlForEachRoom(migr_export_rooms_backend, NULL);
190 fclose(migr_global_message_list);
194 * Process the 'global' message list. (Sort it and remove dups.
195 * Dups are ok because a message may be in more than one room, but
196 * this will be handled by exporting the reference count, not by
197 * exporting the message multiple times.)
199 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
200 if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
201 snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
202 if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
205 snprintf(cmd, sizeof cmd, "wc -l %s", migr_tempfilename1);
206 FILE *fp = popen(cmd, "r");
208 fgets(cmd, sizeof cmd, fp);
210 total_msgs = atoi(cmd);
213 total_msgs = 1; // any nonzero just to keep it from barfing
215 syslog(LOG_DEBUG, "Total messages to be exported: %d", total_msgs);
219 void migr_export_floors(void) {
220 struct floor qfbuf, *buf;
223 for (i=0; i < MAXFLOORS; ++i) {
224 client_write(HKEY("<floor>\n"));
225 cprintf("<f_num>%d</f_num>\n", i);
226 CtdlGetFloor(&qfbuf, i);
228 cprintf("<f_flags>%u</f_flags>\n", buf->f_flags);
229 client_write(HKEY("<f_name>")); xml_strout(buf->f_name); client_write(HKEY("</f_name>\n"));
230 cprintf("<f_ref_count>%d</f_ref_count>\n", buf->f_ref_count);
231 cprintf("<f_ep_expire_mode>%d</f_ep_expire_mode>\n", buf->f_ep.expire_mode);
232 cprintf("<f_ep_expire_value>%d</f_ep_expire_value>\n", buf->f_ep.expire_value);
233 client_write(HKEY("</floor>\n"));
239 * Return nonzero if the supplied string contains only characters which are valid in a sequence set.
241 int is_sequence_set(char *s) {
246 while (ch = *c++, ch) {
247 if (!strchr("0123456789*,:", ch)) {
257 * Traverse the visits file...
259 void migr_export_visits(void) {
261 struct cdbdata *cdbv;
263 cdb_rewind(CDB_VISIT);
265 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
266 memset(&vbuf, 0, sizeof(visit));
267 memcpy(&vbuf, cdbv->ptr,
268 ((cdbv->len > sizeof(visit)) ?
269 sizeof(visit) : cdbv->len));
272 client_write(HKEY("<visit>\n"));
273 cprintf("<v_roomnum>%ld</v_roomnum>\n", vbuf.v_roomnum);
274 cprintf("<v_roomgen>%ld</v_roomgen>\n", vbuf.v_roomgen);
275 cprintf("<v_usernum>%ld</v_usernum>\n", vbuf.v_usernum);
277 client_write(HKEY("<v_seen>"));
278 if ( (!IsEmptyStr(vbuf.v_seen)) && (is_sequence_set(vbuf.v_seen)) ) {
279 xml_strout(vbuf.v_seen);
282 cprintf("%ld", vbuf.v_lastseen);
284 client_write(HKEY("</v_seen>"));
286 if ( (!IsEmptyStr(vbuf.v_answered)) && (is_sequence_set(vbuf.v_answered)) ) {
287 client_write(HKEY("<v_answered>"));
288 xml_strout(vbuf.v_answered);
289 client_write(HKEY("</v_answered>\n"));
292 cprintf("<v_flags>%u</v_flags>\n", vbuf.v_flags);
293 cprintf("<v_view>%d</v_view>\n", vbuf.v_view);
294 client_write(HKEY("</visit>\n"));
299 void migr_export_message(long msgnum) {
301 struct CtdlMessage *msg;
304 /* We can use a static buffer here because there will never be more than
305 * one of this operation happening at any given time, and it's really best
306 * to just keep it allocated once instead of torturing malloc/free.
307 * Call this function with msgnum "-1" to free the buffer when finished.
309 static int encoded_alloc = 0;
310 static char *encoded_msg = NULL;
313 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
321 /* Ok, here we go ... */
323 msg = CtdlFetchMessage(msgnum, 1, 0);
324 if (msg == NULL) return; /* fail silently */
326 client_write(HKEY("<message>\n"));
327 GetMetaData(&smi, msgnum);
328 cprintf("<msg_msgnum>%ld</msg_msgnum>\n", msgnum);
329 cprintf("<msg_meta_refcount>%d</msg_meta_refcount>\n", smi.meta_refcount);
330 cprintf("<msg_meta_rfc822_length>%ld</msg_meta_rfc822_length>\n", smi.meta_rfc822_length);
331 client_write(HKEY("<msg_meta_content_type>")); xml_strout(smi.meta_content_type); client_write(HKEY("</msg_meta_content_type>\n"));
332 client_write(HKEY("<msg_mimetype>")); xml_strout(smi.mimetype); client_write(HKEY("</msg_mimetype>\n"));
334 client_write(HKEY("<msg_text>"));
335 CtdlSerializeMessage(&smr, msg);
338 /* Predict the buffer size we need. Expand the buffer if necessary. */
339 int encoded_len = smr.len * 15 / 10 ;
340 if (encoded_len > encoded_alloc) {
341 encoded_alloc = encoded_len;
342 encoded_msg = realloc(encoded_msg, encoded_alloc);
345 if (encoded_msg == NULL) {
346 /* Questionable hack that hopes it'll work next time and we only lose one message */
350 /* Once we do the encoding we know the exact size */
351 encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
352 client_write(encoded_msg, encoded_len);
357 client_write(HKEY("</msg_text>\n"));
358 client_write(HKEY("</message>\n"));
363 void migr_export_openids(void) {
364 struct cdbdata *cdboi;
368 cdb_rewind(CDB_OPENID);
369 while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
370 if (cdboi->len > sizeof(long)) {
371 client_write(HKEY("<openid>\n"));
372 memcpy(&usernum, cdboi->ptr, sizeof(long));
373 snprintf(url, sizeof url, "%s", (cdboi->ptr)+sizeof(long) );
374 client_write(HKEY("<oid_url>"));
376 client_write(HKEY("</oid_url>\n"));
377 cprintf("<oid_usernum>%ld</oid_usernum>\n", usernum);
378 client_write(HKEY("</openid>\n"));
385 void migr_export_configs(void) {
386 struct cdbdata *cdbcfg;
391 cdb_rewind(CDB_CONFIG);
392 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
394 keylen = strlen(cdbcfg->ptr);
396 value = cdbcfg->ptr + keylen + 1;
398 client_write("<config key=\"", 13);
400 client_write("\">", 2);
402 client_write("</config>\n", 10);
410 void migr_export_messages(void) {
415 int prev_progress = 0;
419 migr_global_message_list = fopen(migr_tempfilename1, "r");
420 if (migr_global_message_list != NULL) {
421 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
422 while ((Ctx->kill_me == 0) &&
423 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
426 migr_export_message(msgnum);
429 progress = (count * 74 / total_msgs) + 25 ;
430 if ((progress > prev_progress) && (progress < 100)) {
431 cprintf("<progress>%d</progress>\n", progress);
433 prev_progress = progress;
435 fclose(migr_global_message_list);
437 if (Ctx->kill_me == 0)
438 syslog(LOG_INFO, "Exported %d messages.", count);
440 syslog(LOG_ERR, "Export aborted due to client disconnect!");
442 migr_export_message(-1L); /* This frees the encoding buffer */
447 void migr_do_export(void) {
451 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
454 client_write(HKEY("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"));
455 client_write(HKEY("<citadel_migrate_data>\n"));
456 cprintf("<version>%d</version>\n", REV_LEVEL);
457 cprintf("<progress>%d</progress>\n", 0);
459 /* export the configuration database */
460 migr_export_configs();
461 cprintf("<progress>%d</progress>\n", 2);
463 if (Ctx->kill_me == 0) migr_export_users();
464 cprintf("<progress>%d</progress>\n", 7);
465 if (Ctx->kill_me == 0) migr_export_openids();
466 cprintf("<progress>%d</progress>\n", 12);
467 if (Ctx->kill_me == 0) migr_export_rooms();
468 cprintf("<progress>%d</progress>\n", 17);
469 if (Ctx->kill_me == 0) migr_export_floors();
470 cprintf("<progress>%d</progress>\n", 18);
471 if (Ctx->kill_me == 0) migr_export_visits();
472 cprintf("<progress>%d</progress>\n", 25);
473 if (Ctx->kill_me == 0) migr_export_messages();
474 client_write(HKEY("</citadel_migrate_data>\n"));
475 cprintf("<progress>%d</progress>\n", 100);
476 client_write(HKEY("000\n"));
483 /******************************************************************************
485 * Here's the code that implements the import side. It's going to end up *
486 * being one big loop with lots of global variables. I don't care. *
487 * You wouldn't run multiple concurrent imports anyway. If this offends your *
488 * delicate sensibilities then go rewrite it in Ruby on Rails or something. *
489 ******************************************************************************/
492 int citadel_migrate_data = 0; /* Are we inside a <citadel_migrate_data> tag pair? */
493 StrBuf *migr_chardata = NULL;
494 StrBuf *migr_MsgData = NULL;
495 struct ctdluser usbuf;
496 struct ctdlroom qrbuf;
497 char openid_url[512];
498 long openid_usernum = 0;
499 char FRname[ROOMNAMELEN];
504 long import_msgnum = 0;
507 * This callback stores up the data which appears in between tags.
509 void migr_xml_chardata(void *data, const XML_Char *s, int len)
511 StrBufAppendBufPlain(migr_chardata, s, len, 0);
515 void migr_xml_start(void *data, const char *el, const char **attr) {
518 /*** GENERAL STUFF ***/
520 /* Reset chardata_len to zero and init buffer */
521 FlushStrBuf(migr_chardata);
522 FlushStrBuf(migr_MsgData);
524 if (!strcasecmp(el, "citadel_migrate_data")) {
525 ++citadel_migrate_data;
527 /* As soon as it looks like the input data is a genuine Citadel XML export,
528 * whack the existing database on disk to make room for the new one.
530 if (citadel_migrate_data == 1) {
531 for (i = 0; i < MAXCDB; ++i) {
538 if (citadel_migrate_data != 1) {
539 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
543 /* When we begin receiving XML for one of these record types, clear out the associated
544 * buffer so we don't accidentally carry over any data from a previous record.
546 if (!strcasecmp(el, "user")) memset(&usbuf, 0, sizeof (struct ctdluser));
547 else if (!strcasecmp(el, "openid")) memset(openid_url, 0, sizeof openid_url);
548 else if (!strcasecmp(el, "room")) memset(&qrbuf, 0, sizeof (struct ctdlroom));
549 else if (!strcasecmp(el, "room_messages")) memset(FRname, 0, sizeof FRname);
550 else if (!strcasecmp(el, "floor")) memset(&flbuf, 0, sizeof (struct floor));
551 else if (!strcasecmp(el, "visit")) memset(&vbuf, 0, sizeof (visit));
553 else if (!strcasecmp(el, "message")) {
554 memset(&smi, 0, sizeof (struct MetaData));
557 else if (!strcasecmp(el, "config")) {
563 if (!strcasecmp(attr[0], "key")) {
564 ikey = strdup(attr[1]);
573 int migr_userrecord(void *data, const char *el)
575 if (!strcasecmp(el, "u_version")) usbuf.version = atoi(ChrPtr(migr_chardata));
576 else if (!strcasecmp(el, "u_uid")) usbuf.uid = atol(ChrPtr(migr_chardata));
577 else if (!strcasecmp(el, "u_password")) safestrncpy(usbuf.password, ChrPtr(migr_chardata), sizeof usbuf.password);
578 else if (!strcasecmp(el, "u_flags")) usbuf.flags = atoi(ChrPtr(migr_chardata));
579 else if (!strcasecmp(el, "u_timescalled")) usbuf.timescalled = atol(ChrPtr(migr_chardata));
580 else if (!strcasecmp(el, "u_posted")) usbuf.posted = atol(ChrPtr(migr_chardata));
581 else if (!strcasecmp(el, "u_axlevel")) usbuf.axlevel = atoi(ChrPtr(migr_chardata));
582 else if (!strcasecmp(el, "u_usernum")) usbuf.usernum = atol(ChrPtr(migr_chardata));
583 else if (!strcasecmp(el, "u_lastcall")) usbuf.lastcall = atol(ChrPtr(migr_chardata));
584 else if (!strcasecmp(el, "u_USuserpurge")) usbuf.USuserpurge = atoi(ChrPtr(migr_chardata));
585 else if (!strcasecmp(el, "u_fullname")) safestrncpy(usbuf.fullname, ChrPtr(migr_chardata), sizeof usbuf.fullname);
586 else if (!strcasecmp(el, "u_msgnum_bio")) usbuf.msgnum_bio = atol(ChrPtr(migr_chardata));
587 else if (!strcasecmp(el, "u_msgnum_pic")) usbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
593 int migr_roomrecord(void *data, const char *el)
595 if (!strcasecmp(el, "QRname")) safestrncpy(qrbuf.QRname, ChrPtr(migr_chardata), sizeof qrbuf.QRname);
596 else if (!strcasecmp(el, "QRpasswd")) safestrncpy(qrbuf.QRpasswd, ChrPtr(migr_chardata), sizeof qrbuf.QRpasswd);
597 else if (!strcasecmp(el, "QRroomaide")) qrbuf.QRroomaide = atol(ChrPtr(migr_chardata));
598 else if (!strcasecmp(el, "QRhighest")) qrbuf.QRhighest = atol(ChrPtr(migr_chardata));
599 else if (!strcasecmp(el, "QRgen")) qrbuf.QRgen = atol(ChrPtr(migr_chardata));
600 else if (!strcasecmp(el, "QRflags")) qrbuf.QRflags = atoi(ChrPtr(migr_chardata));
601 else if (!strcasecmp(el, "QRdirname")) safestrncpy(qrbuf.QRdirname, ChrPtr(migr_chardata), sizeof qrbuf.QRdirname);
602 else if (!strcasecmp(el, "QRinfo")) qrbuf.QRinfo = atol(ChrPtr(migr_chardata));
603 else if (!strcasecmp(el, "QRfloor")) qrbuf.QRfloor = atoi(ChrPtr(migr_chardata));
604 else if (!strcasecmp(el, "QRmtime")) qrbuf.QRmtime = atol(ChrPtr(migr_chardata));
605 else if (!strcasecmp(el, "QRexpire_mode")) qrbuf.QRep.expire_mode = atoi(ChrPtr(migr_chardata));
606 else if (!strcasecmp(el, "QRexpire_value")) qrbuf.QRep.expire_value = atoi(ChrPtr(migr_chardata));
607 else if (!strcasecmp(el, "QRnumber")) qrbuf.QRnumber = atol(ChrPtr(migr_chardata));
608 else if (!strcasecmp(el, "QRorder")) qrbuf.QRorder = atoi(ChrPtr(migr_chardata));
609 else if (!strcasecmp(el, "QRflags2")) qrbuf.QRflags2 = atoi(ChrPtr(migr_chardata));
610 else if (!strcasecmp(el, "QRdefaultview")) qrbuf.QRdefaultview = atoi(ChrPtr(migr_chardata));
615 int migr_floorrecord(void *data, const char *el)
617 if (!strcasecmp(el, "f_num")) floornum = atoi(ChrPtr(migr_chardata));
618 else if (!strcasecmp(el, "f_flags")) flbuf.f_flags = atoi(ChrPtr(migr_chardata));
619 else if (!strcasecmp(el, "f_name")) safestrncpy(flbuf.f_name, ChrPtr(migr_chardata), sizeof flbuf.f_name);
620 else if (!strcasecmp(el, "f_ref_count")) flbuf.f_ref_count = atoi(ChrPtr(migr_chardata));
621 else if (!strcasecmp(el, "f_ep_expire_mode")) flbuf.f_ep.expire_mode = atoi(ChrPtr(migr_chardata));
622 else if (!strcasecmp(el, "f_ep_expire_value")) flbuf.f_ep.expire_value = atoi(ChrPtr(migr_chardata));
627 int migr_visitrecord(void *data, const char *el)
629 if (!strcasecmp(el, "v_roomnum")) vbuf.v_roomnum = atol(ChrPtr(migr_chardata));
630 else if (!strcasecmp(el, "v_roomgen")) vbuf.v_roomgen = atol(ChrPtr(migr_chardata));
631 else if (!strcasecmp(el, "v_usernum")) vbuf.v_usernum = atol(ChrPtr(migr_chardata));
633 else if (!strcasecmp(el, "v_seen")) {
634 int is_textual_seen = 0;
636 int max = StrLength(migr_chardata);
638 vbuf.v_lastseen = atol(ChrPtr(migr_chardata));
640 for (i=0; i < max; ++i)
641 if (!isdigit(ChrPtr(migr_chardata)[i]))
644 safestrncpy(vbuf.v_seen, ChrPtr(migr_chardata), sizeof vbuf.v_seen);
647 else if (!strcasecmp(el, "v_answered")) safestrncpy(vbuf.v_answered, ChrPtr(migr_chardata), sizeof vbuf.v_answered);
648 else if (!strcasecmp(el, "v_flags")) vbuf.v_flags = atoi(ChrPtr(migr_chardata));
649 else if (!strcasecmp(el, "v_view")) vbuf.v_view = atoi(ChrPtr(migr_chardata));
655 void migr_xml_end(void *data, const char *el)
660 long *msglist = NULL;
661 int msglist_alloc = 0;
662 /*** GENERAL STUFF ***/
664 if (!strcasecmp(el, "citadel_migrate_data")) {
665 --citadel_migrate_data;
669 if (citadel_migrate_data != 1) {
670 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
674 // syslog(LOG_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
678 if (!strcasecmp(el, "config"))
680 syslog(LOG_DEBUG, "Imported config key=%s", ikey);
683 CtdlSetConfigStr(ikey, ChrPtr(migr_chardata));
688 syslog(LOG_INFO, "Closed a <config> tag but no key name was supplied.");
693 else if ((!strncasecmp(el, HKEY("u_"))) &&
694 migr_userrecord(data, el))
695 ; /* Nothing to do anymore */
696 else if (!strcasecmp(el, "user")) {
698 syslog(LOG_INFO, "Imported user: %s", usbuf.fullname);
703 else if (!strcasecmp(el, "oid_url")) safestrncpy(openid_url, ChrPtr(migr_chardata), sizeof openid_url);
704 else if (!strcasecmp(el, "oid_usernum")) openid_usernum = atol(ChrPtr(migr_chardata));
706 else if (!strcasecmp(el, "openid")) { /* see serv_openid_rp.c for a description of the record format */
709 oid_data_len = sizeof(long) + strlen(openid_url) + 1;
710 oid_data = malloc(oid_data_len);
711 memcpy(oid_data, &openid_usernum, sizeof(long));
712 memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
713 cdb_store(CDB_OPENID, openid_url, strlen(openid_url), oid_data, oid_data_len);
715 syslog(LOG_INFO, "Imported OpenID: %s (%ld)", openid_url, openid_usernum);
719 else if ((!strncasecmp(el, HKEY("QR"))) &&
720 migr_roomrecord(data, el))
721 ; /* Nothing to do anymore */
722 else if (!strcasecmp(el, "room")) {
724 syslog(LOG_INFO, "Imported room: %s", qrbuf.QRname);
727 /*** ROOM MESSAGE POINTERS ***/
729 else if (!strcasecmp(el, "FRname")) safestrncpy(FRname, ChrPtr(migr_chardata), sizeof FRname);
731 else if (!strcasecmp(el, "FRmsglist")) {
732 if (!IsEmptyStr(FRname)) {
734 msglist_alloc = 1000;
735 msglist = malloc(sizeof(long) * msglist_alloc);
737 syslog(LOG_DEBUG, "Message list for: %s", FRname);
739 ptr = ChrPtr(migr_chardata);
741 while ((*ptr != 0) && (!isdigit(*ptr))) {
744 if ((*ptr != 0) && (isdigit(*ptr))) {
747 if (msgcount >= msglist_alloc) {
749 msglist = realloc(msglist, sizeof(long) * msglist_alloc);
751 msglist[msgcount++] = msgnum;
754 while ((*ptr != 0) && (isdigit(*ptr))) {
760 CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL, 1);
765 syslog(LOG_DEBUG, "Imported %d messages.", msgcount);
766 if (server_shutting_down) {
772 else if ((!strncasecmp(el, HKEY("f_"))) &&
773 migr_floorrecord(data, el))
774 ; /* Nothing to do anymore */
776 else if (!strcasecmp(el, "floor")) {
777 CtdlPutFloor(&flbuf, floornum);
778 syslog(LOG_INFO, "Imported floor #%d (%s)", floornum, flbuf.f_name);
782 else if ((!strncasecmp(el, HKEY("v_"))) &&
783 migr_visitrecord(data, el))
784 ; /* Nothing to do anymore */
785 else if (!strcasecmp(el, "visit")) {
787 syslog(LOG_INFO, "Imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
792 else if (!strcasecmp(el, "msg_msgnum")) smi.meta_msgnum = import_msgnum = atol(ChrPtr(migr_chardata));
793 else if (!strcasecmp(el, "msg_meta_refcount")) smi.meta_refcount = atoi(ChrPtr(migr_chardata));
794 else if (!strcasecmp(el, "msg_meta_rfc822_length")) smi.meta_rfc822_length = atoi(ChrPtr(migr_chardata));
795 else if (!strcasecmp(el, "msg_meta_content_type")) safestrncpy(smi.meta_content_type, ChrPtr(migr_chardata), sizeof smi.meta_content_type);
796 else if (!strcasecmp(el, "msg_mimetype")) safestrncpy(smi.mimetype, ChrPtr(migr_chardata), sizeof smi.mimetype);
798 else if (!strcasecmp(el, "msg_text"))
801 struct CtdlMessage *msg;
803 FlushStrBuf(migr_MsgData);
804 StrBufDecodeBase64To(migr_chardata, migr_MsgData);
806 msg = CtdlDeserializeMessage(import_msgnum, -1,
807 ChrPtr(migr_MsgData),
808 StrLength(migr_MsgData));
810 rc = CtdlSaveThisMessage(msg, import_msgnum, 0);
821 "%s message #%ld, size=%d, refcount=%d, bodylength=%ld, content-type: %s / %s",
822 (rc!= 0)?"failed to import ":"Imported ",
824 StrLength(migr_MsgData),
826 smi.meta_rfc822_length,
827 smi.meta_content_type,
829 memset(&smi, 0, sizeof(smi));
832 /*** MORE GENERAL STUFF ***/
834 FlushStrBuf(migr_chardata);
843 void migr_do_import(void) {
849 migr_chardata = NewStrBufPlain(NULL, SIZ * 20);
850 migr_MsgData = NewStrBufPlain(NULL, SIZ * 20);
851 Buf = NewStrBufPlain(NULL, SIZ);
852 xp = XML_ParserCreate(NULL);
854 cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
857 XML_SetElementHandler(xp, migr_xml_start, migr_xml_end);
858 XML_SetCharacterDataHandler(xp, migr_xml_chardata);
862 cprintf("%d sock it to me\n", SEND_LISTING);
865 client_set_inbound_buf(SIZ * 10);
867 while (!Finished && client_read_random_blob(Buf, -1) >= 0) {
868 if ((StrLength(Buf) > 4) &&
869 !strcmp(ChrPtr(Buf) + StrLength(Buf) - 4, "000\n"))
872 StrBufCutAt(Buf, StrLength(Buf) - 4, NULL);
874 if (server_shutting_down)
875 break; // Should we break or return?
877 if (StrLength(Buf) == 0)
880 XML_Parse(xp, ChrPtr(Buf), StrLength(Buf), 0);
884 XML_Parse(xp, "", 0, 1);
887 FreeStrBuf(&migr_chardata);
888 FreeStrBuf(&migr_MsgData);
889 rebuild_euid_index();
890 rebuild_usersbynumber();
891 CtdlSetConfigInt("MM_fulltext_wordbreaker", -1); // Set an invalid wordbreaker to force re-indexing
897 /******************************************************************************
898 * Dispatcher, Common code *
899 ******************************************************************************/
901 * Dump out the pathnames of directories which can be copied "as is"
903 void migr_do_listdirs(void) {
904 cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
905 cprintf("files|%s\n", ctdl_file_dir);
906 cprintf("messages|%s\n", ctdl_message_dir);
907 cprintf("keys|%s\n", ctdl_key_dir);
908 cprintf("images|%s\n", ctdl_image_dir);
909 cprintf("info|%s\n", ctdl_info_dir);
913 /******************************************************************************
914 * Repair database integrity *
915 ******************************************************************************/
917 StrBuf *PlainMessageBuf = NULL;
918 HashList *UsedMessageIDS = NULL;
920 int migr_restore_message_metadata(long msgnum, int refcount)
922 CitContext *CCC = MyContext();
924 struct CtdlMessage *msg;
927 /* We can use a static buffer here because there will never be more than
928 * one of this operation happening at any given time, and it's really best
929 * to just keep it allocated once instead of torturing malloc/free.
930 * Call this function with msgnum "-1" to free the buffer when finished.
932 static int encoded_alloc = 0;
933 static char *encoded_msg = NULL;
936 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
940 // todo FreeStrBuf(&PlainMessageBuf); PlainMessageBuf = NULL;
945 if (PlainMessageBuf == NULL) {
946 PlainMessageBuf = NewStrBufPlain(NULL, 10*SIZ);
949 /* Ok, here we go ... */
951 msg = CtdlFetchMessage(msgnum, 1, 0);
956 GetMetaData(&smi, msgnum);
957 smi.meta_msgnum = msgnum;
958 smi.meta_refcount = refcount;
960 /* restore the content type from the message body: */
961 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
964 safestrncpy(smi.meta_content_type, &mptr[13], sizeof smi.meta_content_type);
965 striplt(smi.meta_content_type);
966 aptr = smi.meta_content_type;
967 while (!IsEmptyStr(aptr)) {
972 memset(aptr, 0, sizeof(smi.meta_content_type) - (aptr - smi.meta_content_type));
978 CCC->redirect_buffer = PlainMessageBuf;
979 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
980 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
981 CCC->redirect_buffer = NULL;
985 "Setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s / %s",
988 smi.meta_rfc822_length,
989 smi.meta_content_type,
999 void migr_check_room_msg(long msgnum, void *userdata) {
1000 fprintf(migr_global_message_list, "%ld %s\n", msgnum, CC->room.QRname);
1004 void migr_check_rooms_backend(struct ctdlroom *buf, void *data) {
1006 /* message list goes inside this tag */
1008 CtdlGetRoom(&CC->room, buf->QRname);
1009 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_check_room_msg, NULL);
1012 void RemoveMessagesFromRooms(StrBuf *RoomNameVec, long msgnum) {
1013 struct MetaData smi;
1014 const char *Pos = NULL;
1015 StrBuf *oneRoom = NewStrBuf();
1017 syslog(LOG_INFO, "removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
1019 while (Pos != StrBufNOTNULL){
1020 StrBufExtract_NextToken(oneRoom, RoomNameVec, &Pos, '|');
1021 CtdlDeleteMessages(ChrPtr(oneRoom), &msgnum, 1, "");
1023 GetMetaData(&smi, msgnum);
1024 TDAP_AdjRefCount(msgnum, -smi.meta_refcount);
1027 void migr_do_restore_meta(void) {
1038 migr_global_message_list = fopen(migr_tempfilename1, "w");
1039 if (migr_global_message_list != NULL) {
1040 CtdlForEachRoom(migr_check_rooms_backend, NULL);
1041 fclose(migr_global_message_list);
1045 * Process the 'global' message list. (Sort it and remove dups.
1046 * Dups are ok because a message may be in more than one room, but
1047 * this will be handled by exporting the reference count, not by
1048 * exporting the message multiple times.)
1050 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
1051 if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
1053 RoomNames = NewStrBuf();
1055 migr_global_message_list = fopen(migr_tempfilename2, "r");
1056 if (migr_global_message_list != NULL) {
1057 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
1058 while ((Ctx->kill_me == 0) &&
1059 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
1066 prn = strchr(buf, ' ');
1067 if (lastnum != msgnum) {
1068 failGetMessage = migr_restore_message_metadata(lastnum, refcount);
1069 if (failGetMessage) {
1070 RemoveMessagesFromRooms(RoomNames, lastnum);
1075 StrBufPlain(RoomNames, prn + 1, -1);
1076 StrBufTrim(RoomNames);
1080 if (StrLength(RoomNames) > 0)
1081 StrBufAppendBufPlain(RoomNames, HKEY("|"), 0);
1082 StrBufAppendBufPlain(RoomNames, prn, -1, 1);
1083 StrBufTrim(RoomNames);
1089 failGetMessage = migr_restore_message_metadata(msgnum, refcount);
1090 if (failGetMessage) {
1091 RemoveMessagesFromRooms(RoomNames, lastnum);
1093 fclose(migr_global_message_list);
1096 migr_restore_message_metadata(-1L, -1); /* This frees the encoding buffer */
1097 cprintf("%d system analysis completed", CIT_OK);
1104 /******************************************************************************
1105 * Dispatcher, Common code *
1106 ******************************************************************************/
1107 void cmd_migr(char *cmdbuf) {
1110 if (CtdlAccessCheck(ac_internal)) return;
1112 if (CtdlTrySingleUser())
1114 CtdlDisableHouseKeeping();
1115 CtdlMakeTempFileName(migr_tempfilename1, sizeof migr_tempfilename1);
1116 CtdlMakeTempFileName(migr_tempfilename2, sizeof migr_tempfilename2);
1118 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
1119 if (!strcasecmp(cmd, "export")) {
1122 else if (!strcasecmp(cmd, "import")) {
1125 else if (!strcasecmp(cmd, "listdirs")) {
1128 else if (!strcasecmp(cmd, "restoremeta")) {
1129 migr_do_restore_meta();
1132 cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
1135 unlink(migr_tempfilename1);
1136 unlink(migr_tempfilename2);
1138 CtdlEnableHouseKeeping();
1139 CtdlEndSingleUser();
1143 cprintf("%d The migrator is already running.\n", ERROR + RESOURCE_BUSY);
1147 /******************************************************************************
1149 ******************************************************************************/
1151 CTDL_MODULE_INIT(migrate)
1155 CtdlRegisterProtoHook(cmd_migr, "MIGR", "Across-the-wire migration");
1158 /* return our module name for the log */