2 * This module dumps and/or loads the Citadel database in XML format.
4 * Copyright (c) 1987-2021 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>
43 #include <libcitadel.h>
46 #include "citserver.h"
52 #include "euidindex.h"
53 #include "ctdl_module.h"
55 char migr_tempfilename1[PATH_MAX];
56 char migr_tempfilename2[PATH_MAX];
57 FILE *migr_global_message_list;
59 char *ikey = NULL; // If we're importing a config key we store it here.
61 /******************************************************************************
62 * Code which implements the export appears in this section *
63 ******************************************************************************/
66 * Output a string to the client with these characters escaped: & < > " '
68 void xml_strout(char *str) {
78 client_write(HKEY("""));
80 else if (*c == '\'') {
81 client_write(HKEY("'"));
84 client_write(HKEY("<"));
87 client_write(HKEY(">"));
90 client_write(HKEY("&"));
101 * Export a user record as XML
103 void migr_export_users_backend(char *username, void *data) {
106 if (CtdlGetUser(&u, username) != 0) {
110 client_write(HKEY("<user>\n"));
111 cprintf("<u_version>%d</u_version>\n", u.version);
112 cprintf("<u_uid>%ld</u_uid>\n", (long)u.uid);
113 client_write(HKEY("<u_password>")); xml_strout(u.password); client_write(HKEY("</u_password>\n"));
114 cprintf("<u_flags>%u</u_flags>\n", u.flags);
115 cprintf("<u_timescalled>%ld</u_timescalled>\n", u.timescalled);
116 cprintf("<u_posted>%ld</u_posted>\n", u.posted);
117 cprintf("<u_axlevel>%d</u_axlevel>\n", u.axlevel);
118 cprintf("<u_usernum>%ld</u_usernum>\n", u.usernum);
119 cprintf("<u_lastcall>%ld</u_lastcall>\n", (long)u.lastcall);
120 cprintf("<u_USuserpurge>%d</u_USuserpurge>\n", u.USuserpurge);
121 client_write(HKEY("<u_fullname>")); xml_strout(u.fullname); client_write(HKEY("</u_fullname>\n"));
122 cprintf("<u_msgnum_bio>%ld</u_msgnum_bio>\n", u.msgnum_bio);
123 cprintf("<u_msgnum_pic>%ld</u_msgnum_pic>\n", u.msgnum_pic);
124 cprintf("<u_emailaddrs>%s</u_emailaddrs>\n", u.emailaddrs);
125 cprintf("<u_msgnum_inboxrules>%ld</u_msgnum_inboxrules>\n", u.msgnum_inboxrules);
126 cprintf("<u_lastproc_inboxrules>%ld</u_lastproc_inboxrules>\n", u.lastproc_inboxrules);
127 client_write(HKEY("</user>\n"));
131 void migr_export_users(void) {
132 ForEachUser(migr_export_users_backend, NULL);
136 void migr_export_room_msg(long msgnum, void *userdata) {
137 cprintf("%ld\n", msgnum);
138 fprintf(migr_global_message_list, "%ld\n", msgnum);
142 void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
143 client_write(HKEY("<room>\n"));
144 client_write(HKEY("<QRname>")); xml_strout(buf->QRname); client_write(HKEY("</QRname>\n"));
145 client_write(HKEY("<QRpasswd>")); xml_strout(buf->QRpasswd); client_write(HKEY("</QRpasswd>\n"));
146 cprintf("<QRroomaide>%ld</QRroomaide>\n", buf->QRroomaide);
147 cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
148 cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
149 cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
150 if (buf->QRflags & QR_DIRECTORY) {
151 client_write(HKEY("<QRdirname>"));
152 xml_strout(buf->QRdirname);
153 client_write(HKEY("</QRdirname>\n"));
155 cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
156 cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
157 cprintf("<QRexpire_mode>%d</QRexpire_mode>\n", buf->QRep.expire_mode);
158 cprintf("<QRexpire_value>%d</QRexpire_value>\n", buf->QRep.expire_value);
159 cprintf("<QRnumber>%ld</QRnumber>\n", buf->QRnumber);
160 cprintf("<QRorder>%d</QRorder>\n", buf->QRorder);
161 cprintf("<QRflags2>%u</QRflags2>\n", buf->QRflags2);
162 cprintf("<QRdefaultview>%d</QRdefaultview>\n", buf->QRdefaultview);
163 cprintf("<msgnum_info>%ld</msgnum_info>\n", buf->msgnum_info);
164 cprintf("<msgnum_pic>%ld</msgnum_pic>\n", buf->msgnum_pic);
165 client_write(HKEY("</room>\n"));
167 /* message list goes inside this tag */
169 CtdlGetRoom(&CC->room, buf->QRname);
170 client_write(HKEY("<room_messages>"));
171 client_write(HKEY("<FRname>")); xml_strout(CC->room.QRname); client_write(HKEY("</FRname>\n"));
172 client_write(HKEY("<FRmsglist>"));
173 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_export_room_msg, NULL);
174 client_write(HKEY("</FRmsglist>\n"));
175 client_write(HKEY("</room_messages>\n"));
181 void migr_export_rooms(void) {
183 migr_global_message_list = fopen(migr_tempfilename1, "w");
184 if (migr_global_message_list != NULL) {
185 CtdlForEachRoom(migr_export_rooms_backend, NULL);
186 fclose(migr_global_message_list);
190 * Process the 'global' message list. (Sort it and remove dups.
191 * Dups are ok because a message may be in more than one room, but
192 * this will be handled by exporting the reference count, not by
193 * exporting the message multiple times.)
195 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
196 if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
197 snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
198 if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
201 snprintf(cmd, sizeof cmd, "wc -l %s", migr_tempfilename1);
202 FILE *fp = popen(cmd, "r");
204 fgets(cmd, sizeof cmd, fp);
206 total_msgs = atoi(cmd);
209 total_msgs = 1; // any nonzero just to keep it from barfing
211 syslog(LOG_DEBUG, "Total messages to be exported: %d", total_msgs);
215 void migr_export_floors(void) {
216 struct floor qfbuf, *buf;
219 for (i=0; i < MAXFLOORS; ++i) {
220 client_write(HKEY("<floor>\n"));
221 cprintf("<f_num>%d</f_num>\n", i);
222 CtdlGetFloor(&qfbuf, i);
224 cprintf("<f_flags>%u</f_flags>\n", buf->f_flags);
225 client_write(HKEY("<f_name>")); xml_strout(buf->f_name); client_write(HKEY("</f_name>\n"));
226 cprintf("<f_ref_count>%d</f_ref_count>\n", buf->f_ref_count);
227 cprintf("<f_ep_expire_mode>%d</f_ep_expire_mode>\n", buf->f_ep.expire_mode);
228 cprintf("<f_ep_expire_value>%d</f_ep_expire_value>\n", buf->f_ep.expire_value);
229 client_write(HKEY("</floor>\n"));
235 * Return nonzero if the supplied string contains only characters which are valid in a sequence set.
237 int is_sequence_set(char *s) {
242 while (ch = *c++, ch) {
243 if (!strchr("0123456789*,:", ch)) {
252 * Traverse the visits file...
254 void migr_export_visits(void) {
256 struct cdbdata *cdbv;
258 cdb_rewind(CDB_VISIT);
260 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
261 memset(&vbuf, 0, sizeof(visit));
262 memcpy(&vbuf, cdbv->ptr,
263 ((cdbv->len > sizeof(visit)) ?
264 sizeof(visit) : cdbv->len));
267 client_write(HKEY("<visit>\n"));
268 cprintf("<v_roomnum>%ld</v_roomnum>\n", vbuf.v_roomnum);
269 cprintf("<v_roomgen>%ld</v_roomgen>\n", vbuf.v_roomgen);
270 cprintf("<v_usernum>%ld</v_usernum>\n", vbuf.v_usernum);
272 client_write(HKEY("<v_seen>"));
273 if ( (!IsEmptyStr(vbuf.v_seen)) && (is_sequence_set(vbuf.v_seen)) ) {
274 xml_strout(vbuf.v_seen);
277 cprintf("%ld", vbuf.v_lastseen);
279 client_write(HKEY("</v_seen>"));
281 if ( (!IsEmptyStr(vbuf.v_answered)) && (is_sequence_set(vbuf.v_answered)) ) {
282 client_write(HKEY("<v_answered>"));
283 xml_strout(vbuf.v_answered);
284 client_write(HKEY("</v_answered>\n"));
287 cprintf("<v_flags>%u</v_flags>\n", vbuf.v_flags);
288 cprintf("<v_view>%d</v_view>\n", vbuf.v_view);
289 client_write(HKEY("</visit>\n"));
294 void migr_export_message(long msgnum) {
296 struct CtdlMessage *msg;
299 /* We can use a static buffer here because there will never be more than
300 * one of this operation happening at any given time, and it's really best
301 * to just keep it allocated once instead of torturing malloc/free.
302 * Call this function with msgnum "-1" to free the buffer when finished.
304 static int encoded_alloc = 0;
305 static char *encoded_msg = NULL;
308 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
316 /* Ok, here we go ... */
318 msg = CtdlFetchMessage(msgnum, 1);
319 if (msg == NULL) return; /* fail silently */
321 client_write(HKEY("<message>\n"));
322 GetMetaData(&smi, msgnum);
323 cprintf("<msg_msgnum>%ld</msg_msgnum>\n", msgnum);
324 cprintf("<msg_meta_refcount>%d</msg_meta_refcount>\n", smi.meta_refcount);
325 cprintf("<msg_meta_rfc822_length>%ld</msg_meta_rfc822_length>\n", smi.meta_rfc822_length);
326 client_write(HKEY("<msg_meta_content_type>")); xml_strout(smi.meta_content_type); client_write(HKEY("</msg_meta_content_type>\n"));
328 client_write(HKEY("<msg_text>"));
329 CtdlSerializeMessage(&smr, msg);
332 /* Predict the buffer size we need. Expand the buffer if necessary. */
333 int encoded_len = smr.len * 15 / 10 ;
334 if (encoded_len > encoded_alloc) {
335 encoded_alloc = encoded_len;
336 encoded_msg = realloc(encoded_msg, encoded_alloc);
339 if (encoded_msg == NULL) {
340 /* Questionable hack that hopes it'll work next time and we only lose one message */
344 /* Once we do the encoding we know the exact size */
345 encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
346 client_write(encoded_msg, encoded_len);
351 client_write(HKEY("</msg_text>\n"));
352 client_write(HKEY("</message>\n"));
356 void migr_export_openids(void) {
357 struct cdbdata *cdboi;
361 cdb_rewind(CDB_EXTAUTH);
362 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
363 if (cdboi->len > sizeof(long)) {
364 client_write(HKEY("<openid>\n"));
365 memcpy(&usernum, cdboi->ptr, sizeof(long));
366 snprintf(url, sizeof url, "%s", (cdboi->ptr)+sizeof(long) );
367 client_write(HKEY("<oid_url>"));
369 client_write(HKEY("</oid_url>\n"));
370 cprintf("<oid_usernum>%ld</oid_usernum>\n", usernum);
371 client_write(HKEY("</openid>\n"));
378 void migr_export_configs(void) {
379 struct cdbdata *cdbcfg;
384 cdb_rewind(CDB_CONFIG);
385 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
387 keylen = strlen(cdbcfg->ptr);
389 value = cdbcfg->ptr + keylen + 1;
391 client_write("<config key=\"", 13);
393 client_write("\">", 2);
395 client_write("</config>\n", 10);
401 void migr_export_messages(void) {
406 int prev_progress = 0;
410 migr_global_message_list = fopen(migr_tempfilename1, "r");
411 if (migr_global_message_list != NULL) {
412 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
413 while ((Ctx->kill_me == 0) &&
414 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
417 migr_export_message(msgnum);
420 progress = (count * 74 / total_msgs) + 25 ;
421 if ((progress > prev_progress) && (progress < 100)) {
422 cprintf("<progress>%d</progress>\n", progress);
424 prev_progress = progress;
426 fclose(migr_global_message_list);
428 if (Ctx->kill_me == 0) {
429 syslog(LOG_INFO, "Exported %d messages.", count);
432 syslog(LOG_ERR, "Export aborted due to client disconnect!");
435 migr_export_message(-1L); /* This frees the encoding buffer */
439 void migr_do_export(void) {
443 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
446 client_write(HKEY("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"));
447 client_write(HKEY("<citadel_migrate_data>\n"));
448 cprintf("<version>%d</version>\n", REV_LEVEL);
449 cprintf("<progress>%d</progress>\n", 0);
451 /* export the configuration database */
452 migr_export_configs();
453 cprintf("<progress>%d</progress>\n", 2);
455 if (Ctx->kill_me == 0) migr_export_users();
456 cprintf("<progress>%d</progress>\n", 7);
457 if (Ctx->kill_me == 0) migr_export_openids();
458 cprintf("<progress>%d</progress>\n", 12);
459 if (Ctx->kill_me == 0) migr_export_rooms();
460 cprintf("<progress>%d</progress>\n", 17);
461 if (Ctx->kill_me == 0) migr_export_floors();
462 cprintf("<progress>%d</progress>\n", 18);
463 if (Ctx->kill_me == 0) migr_export_visits();
464 cprintf("<progress>%d</progress>\n", 25);
465 if (Ctx->kill_me == 0) migr_export_messages();
466 cprintf("<progress>%d</progress>\n", 100);
467 client_write(HKEY("</citadel_migrate_data>\n"));
468 client_write(HKEY("000\n"));
473 /******************************************************************************
475 * Here's the code that implements the import side. It's going to end up *
476 * being one big loop with lots of global variables. I don't care. *
477 * You wouldn't run multiple concurrent imports anyway. If this offends your *
478 * delicate sensibilities then go rewrite it in Ruby on Rails or something. *
479 ******************************************************************************/
482 int citadel_migrate_data = 0; /* Are we inside a <citadel_migrate_data> tag pair? */
483 StrBuf *migr_chardata = NULL;
484 StrBuf *migr_MsgData = NULL;
485 struct ctdluser usbuf;
486 struct ctdlroom qrbuf;
487 char openid_url[512];
488 long openid_usernum = 0;
489 char FRname[ROOMNAMELEN];
494 long import_msgnum = 0;
497 * This callback stores up the data which appears in between tags.
499 void migr_xml_chardata(void *data, const XML_Char *s, int len)
501 StrBufAppendBufPlain(migr_chardata, s, len, 0);
505 void migr_xml_start(void *data, const char *el, const char **attr) {
508 /*** GENERAL STUFF ***/
510 /* Reset chardata_len to zero and init buffer */
511 FlushStrBuf(migr_chardata);
512 FlushStrBuf(migr_MsgData);
514 if (!strcasecmp(el, "citadel_migrate_data")) {
515 ++citadel_migrate_data;
517 /* As soon as it looks like the input data is a genuine Citadel XML export,
518 * whack the existing database on disk to make room for the new one.
520 if (citadel_migrate_data == 1) {
521 for (i = 0; i < MAXCDB; ++i) {
528 if (citadel_migrate_data != 1) {
529 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
533 /* When we begin receiving XML for one of these record types, clear out the associated
534 * buffer so we don't accidentally carry over any data from a previous record.
536 if (!strcasecmp(el, "user")) memset(&usbuf, 0, sizeof (struct ctdluser));
537 else if (!strcasecmp(el, "openid")) memset(openid_url, 0, sizeof openid_url);
538 else if (!strcasecmp(el, "room")) memset(&qrbuf, 0, sizeof (struct ctdlroom));
539 else if (!strcasecmp(el, "room_messages")) memset(FRname, 0, sizeof FRname);
540 else if (!strcasecmp(el, "floor")) memset(&flbuf, 0, sizeof (struct floor));
541 else if (!strcasecmp(el, "visit")) memset(&vbuf, 0, sizeof (visit));
543 else if (!strcasecmp(el, "message")) {
544 memset(&smi, 0, sizeof (struct MetaData));
547 else if (!strcasecmp(el, "config")) {
553 if (!strcasecmp(attr[0], "key")) {
554 ikey = strdup(attr[1]);
563 int migr_userrecord(void *data, const char *el)
565 if (!strcasecmp(el, "u_version")) usbuf.version = atoi(ChrPtr(migr_chardata));
566 else if (!strcasecmp(el, "u_uid")) usbuf.uid = atol(ChrPtr(migr_chardata));
567 else if (!strcasecmp(el, "u_password")) safestrncpy(usbuf.password, ChrPtr(migr_chardata), sizeof usbuf.password);
568 else if (!strcasecmp(el, "u_flags")) usbuf.flags = atoi(ChrPtr(migr_chardata));
569 else if (!strcasecmp(el, "u_timescalled")) usbuf.timescalled = atol(ChrPtr(migr_chardata));
570 else if (!strcasecmp(el, "u_posted")) usbuf.posted = atol(ChrPtr(migr_chardata));
571 else if (!strcasecmp(el, "u_axlevel")) usbuf.axlevel = atoi(ChrPtr(migr_chardata));
572 else if (!strcasecmp(el, "u_usernum")) usbuf.usernum = atol(ChrPtr(migr_chardata));
573 else if (!strcasecmp(el, "u_lastcall")) usbuf.lastcall = atol(ChrPtr(migr_chardata));
574 else if (!strcasecmp(el, "u_USuserpurge")) usbuf.USuserpurge = atoi(ChrPtr(migr_chardata));
575 else if (!strcasecmp(el, "u_fullname")) safestrncpy(usbuf.fullname, ChrPtr(migr_chardata), sizeof usbuf.fullname);
576 else if (!strcasecmp(el, "u_msgnum_bio")) usbuf.msgnum_bio = atol(ChrPtr(migr_chardata));
577 else if (!strcasecmp(el, "u_msgnum_pic")) usbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
578 else if (!strcasecmp(el, "u_emailaddrs")) safestrncpy(usbuf.emailaddrs, ChrPtr(migr_chardata), sizeof usbuf.emailaddrs);
579 else if (!strcasecmp(el, "u_msgnum_inboxrules")) usbuf.msgnum_inboxrules = atol(ChrPtr(migr_chardata));
580 else if (!strcasecmp(el, "u_lastproc_inboxrules")) usbuf.lastproc_inboxrules = atol(ChrPtr(migr_chardata));
586 int migr_roomrecord(void *data, const char *el)
588 if (!strcasecmp(el, "QRname")) safestrncpy(qrbuf.QRname, ChrPtr(migr_chardata), sizeof qrbuf.QRname);
589 else if (!strcasecmp(el, "QRpasswd")) safestrncpy(qrbuf.QRpasswd, ChrPtr(migr_chardata), sizeof qrbuf.QRpasswd);
590 else if (!strcasecmp(el, "QRroomaide")) qrbuf.QRroomaide = atol(ChrPtr(migr_chardata));
591 else if (!strcasecmp(el, "QRhighest")) qrbuf.QRhighest = atol(ChrPtr(migr_chardata));
592 else if (!strcasecmp(el, "QRgen")) qrbuf.QRgen = atol(ChrPtr(migr_chardata));
593 else if (!strcasecmp(el, "QRflags")) qrbuf.QRflags = atoi(ChrPtr(migr_chardata));
594 else if (!strcasecmp(el, "QRdirname")) safestrncpy(qrbuf.QRdirname, ChrPtr(migr_chardata), sizeof qrbuf.QRdirname);
595 else if (!strcasecmp(el, "QRfloor")) qrbuf.QRfloor = atoi(ChrPtr(migr_chardata));
596 else if (!strcasecmp(el, "QRmtime")) qrbuf.QRmtime = atol(ChrPtr(migr_chardata));
597 else if (!strcasecmp(el, "QRexpire_mode")) qrbuf.QRep.expire_mode = atoi(ChrPtr(migr_chardata));
598 else if (!strcasecmp(el, "QRexpire_value")) qrbuf.QRep.expire_value = atoi(ChrPtr(migr_chardata));
599 else if (!strcasecmp(el, "QRnumber")) qrbuf.QRnumber = atol(ChrPtr(migr_chardata));
600 else if (!strcasecmp(el, "QRorder")) qrbuf.QRorder = atoi(ChrPtr(migr_chardata));
601 else if (!strcasecmp(el, "QRflags2")) qrbuf.QRflags2 = atoi(ChrPtr(migr_chardata));
602 else if (!strcasecmp(el, "QRdefaultview")) qrbuf.QRdefaultview = atoi(ChrPtr(migr_chardata));
603 else if (!strcasecmp(el, "msgnum_info")) qrbuf.msgnum_info = atol(ChrPtr(migr_chardata));
604 else if (!strcasecmp(el, "msgnum_pic")) qrbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
610 int migr_floorrecord(void *data, const char *el)
612 if (!strcasecmp(el, "f_num")) floornum = atoi(ChrPtr(migr_chardata));
613 else if (!strcasecmp(el, "f_flags")) flbuf.f_flags = atoi(ChrPtr(migr_chardata));
614 else if (!strcasecmp(el, "f_name")) safestrncpy(flbuf.f_name, ChrPtr(migr_chardata), sizeof flbuf.f_name);
615 else if (!strcasecmp(el, "f_ref_count")) flbuf.f_ref_count = atoi(ChrPtr(migr_chardata));
616 else if (!strcasecmp(el, "f_ep_expire_mode")) flbuf.f_ep.expire_mode = atoi(ChrPtr(migr_chardata));
617 else if (!strcasecmp(el, "f_ep_expire_value")) flbuf.f_ep.expire_value = atoi(ChrPtr(migr_chardata));
623 int migr_visitrecord(void *data, const char *el)
625 if (!strcasecmp(el, "v_roomnum")) vbuf.v_roomnum = atol(ChrPtr(migr_chardata));
626 else if (!strcasecmp(el, "v_roomgen")) vbuf.v_roomgen = atol(ChrPtr(migr_chardata));
627 else if (!strcasecmp(el, "v_usernum")) vbuf.v_usernum = atol(ChrPtr(migr_chardata));
629 else if (!strcasecmp(el, "v_seen")) {
630 int is_textual_seen = 0;
632 int max = StrLength(migr_chardata);
634 vbuf.v_lastseen = atol(ChrPtr(migr_chardata));
636 for (i=0; i < max; ++i)
637 if (!isdigit(ChrPtr(migr_chardata)[i]))
640 safestrncpy(vbuf.v_seen, ChrPtr(migr_chardata), sizeof vbuf.v_seen);
643 else if (!strcasecmp(el, "v_answered")) safestrncpy(vbuf.v_answered, ChrPtr(migr_chardata), sizeof vbuf.v_answered);
644 else if (!strcasecmp(el, "v_flags")) vbuf.v_flags = atoi(ChrPtr(migr_chardata));
645 else if (!strcasecmp(el, "v_view")) vbuf.v_view = atoi(ChrPtr(migr_chardata));
651 void migr_xml_end(void *data, const char *el)
656 long *msglist = NULL;
657 int msglist_alloc = 0;
658 /*** GENERAL STUFF ***/
660 if (!strcasecmp(el, "citadel_migrate_data")) {
661 --citadel_migrate_data;
665 if (citadel_migrate_data != 1) {
666 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
670 // syslog(LOG_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
674 if (!strcasecmp(el, "config"))
676 syslog(LOG_DEBUG, "Imported config key=%s", ikey);
679 CtdlSetConfigStr(ikey, (char *)ChrPtr(migr_chardata));
684 syslog(LOG_INFO, "Closed a <config> tag but no key name was supplied.");
689 else if ((!strncasecmp(el, HKEY("u_"))) &&
690 migr_userrecord(data, el))
691 ; /* Nothing to do anymore */
692 else if (!strcasecmp(el, "user")) {
694 syslog(LOG_INFO, "Imported user: %s", usbuf.fullname);
699 else if (!strcasecmp(el, "oid_url")) safestrncpy(openid_url, ChrPtr(migr_chardata), sizeof openid_url);
700 else if (!strcasecmp(el, "oid_usernum")) openid_usernum = atol(ChrPtr(migr_chardata));
702 else if (!strcasecmp(el, "openid")) { /* see serv_openid_rp.c for a description of the record format */
705 oid_data_len = sizeof(long) + strlen(openid_url) + 1;
706 oid_data = malloc(oid_data_len);
707 memcpy(oid_data, &openid_usernum, sizeof(long));
708 memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
709 cdb_store(CDB_EXTAUTH, openid_url, strlen(openid_url), oid_data, oid_data_len);
711 syslog(LOG_INFO, "Imported OpenID: %s (%ld)", openid_url, openid_usernum);
715 else if ((!strncasecmp(el, HKEY("QR"))) &&
716 migr_roomrecord(data, el))
717 ; /* Nothing to do anymore */
718 else if (!strcasecmp(el, "room")) {
720 syslog(LOG_INFO, "Imported room: %s", qrbuf.QRname);
723 /*** ROOM MESSAGE POINTERS ***/
725 else if (!strcasecmp(el, "FRname")) safestrncpy(FRname, ChrPtr(migr_chardata), sizeof FRname);
727 else if (!strcasecmp(el, "FRmsglist")) {
728 if (!IsEmptyStr(FRname)) {
730 msglist_alloc = 1000;
731 msglist = malloc(sizeof(long) * msglist_alloc);
733 syslog(LOG_DEBUG, "Message list for: %s", FRname);
735 ptr = ChrPtr(migr_chardata);
737 while ((*ptr != 0) && (!isdigit(*ptr))) {
740 if ((*ptr != 0) && (isdigit(*ptr))) {
743 if (msgcount >= msglist_alloc) {
745 msglist = realloc(msglist, sizeof(long) * msglist_alloc);
747 msglist[msgcount++] = msgnum;
750 while ((*ptr != 0) && (isdigit(*ptr))) {
756 CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL, 1);
761 syslog(LOG_DEBUG, "Imported %d messages.", msgcount);
762 if (server_shutting_down) {
768 else if ((!strncasecmp(el, HKEY("f_"))) &&
769 migr_floorrecord(data, el))
770 ; /* Nothing to do anymore */
772 else if (!strcasecmp(el, "floor")) {
773 CtdlPutFloor(&flbuf, floornum);
774 syslog(LOG_INFO, "Imported floor #%d (%s)", floornum, flbuf.f_name);
778 else if ((!strncasecmp(el, HKEY("v_"))) && migr_visitrecord(data, el)) {
779 ; /* Nothing to do anymore */
781 else if (!strcasecmp(el, "visit")) {
783 syslog(LOG_INFO, "Imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
788 else if (!strcasecmp(el, "msg_msgnum")) smi.meta_msgnum = import_msgnum = atol(ChrPtr(migr_chardata));
789 else if (!strcasecmp(el, "msg_meta_refcount")) smi.meta_refcount = atoi(ChrPtr(migr_chardata));
790 else if (!strcasecmp(el, "msg_meta_rfc822_length")) smi.meta_rfc822_length = atoi(ChrPtr(migr_chardata));
791 else if (!strcasecmp(el, "msg_meta_content_type")) safestrncpy(smi.meta_content_type, ChrPtr(migr_chardata), sizeof smi.meta_content_type);
793 else if (!strcasecmp(el, "msg_text"))
796 struct CtdlMessage *msg;
798 FlushStrBuf(migr_MsgData);
799 StrBufDecodeBase64To(migr_chardata, migr_MsgData);
801 msg = CtdlDeserializeMessage(import_msgnum, -1,
802 ChrPtr(migr_MsgData),
803 StrLength(migr_MsgData));
805 rc = CtdlSaveThisMessage(msg, import_msgnum, 0);
816 "%s message #%ld, size=%d, refcount=%d, bodylength=%ld, content-type: %s",
817 (rc!= 0)?"failed to import ":"Imported ",
819 StrLength(migr_MsgData),
821 smi.meta_rfc822_length,
822 smi.meta_content_type
824 memset(&smi, 0, sizeof(smi));
827 /*** MORE GENERAL STUFF ***/
829 FlushStrBuf(migr_chardata);
836 void migr_do_import(void) {
842 migr_chardata = NewStrBufPlain(NULL, SIZ * 20);
843 migr_MsgData = NewStrBufPlain(NULL, SIZ * 20);
844 Buf = NewStrBufPlain(NULL, SIZ);
845 xp = XML_ParserCreate(NULL);
847 cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
850 XML_SetElementHandler(xp, migr_xml_start, migr_xml_end);
851 XML_SetCharacterDataHandler(xp, migr_xml_chardata);
855 cprintf("%d sock it to me\n", SEND_LISTING);
858 client_set_inbound_buf(SIZ * 10);
860 while (!Finished && client_read_random_blob(Buf, -1) >= 0) {
861 if ((StrLength(Buf) > 4) && !strcmp(ChrPtr(Buf) + StrLength(Buf) - 4, "000\n")) {
863 StrBufCutAt(Buf, StrLength(Buf) - 4, NULL);
865 if (server_shutting_down)
866 break; // Should we break or return?
868 if (StrLength(Buf) == 0)
871 XML_Parse(xp, ChrPtr(Buf), StrLength(Buf), 0);
875 XML_Parse(xp, "", 0, 1);
878 FreeStrBuf(&migr_chardata);
879 FreeStrBuf(&migr_MsgData);
880 rebuild_euid_index();
881 rebuild_usersbynumber();
882 CtdlSetConfigInt("MM_fulltext_wordbreaker", -1); // Set an invalid wordbreaker to force re-indexing
887 /******************************************************************************
888 * Dispatcher, Common code *
889 ******************************************************************************/
891 * Dump out the pathnames of directories which can be copied "as is"
893 void migr_do_listdirs(void) {
894 cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
895 cprintf("files|%s\n", ctdl_file_dir);
896 cprintf("messages|%s\n", ctdl_message_dir);
897 cprintf("keys|%s\n", ctdl_key_dir);
902 /******************************************************************************
903 * Repair database integrity *
904 ******************************************************************************/
906 StrBuf *PlainMessageBuf = NULL;
907 HashList *UsedMessageIDS = NULL;
909 int migr_restore_message_metadata(long msgnum, int refcount)
911 CitContext *CCC = MyContext();
913 struct CtdlMessage *msg;
916 /* We can use a static buffer here because there will never be more than
917 * one of this operation happening at any given time, and it's really best
918 * to just keep it allocated once instead of torturing malloc/free.
919 * Call this function with msgnum "-1" to free the buffer when finished.
921 static int encoded_alloc = 0;
922 static char *encoded_msg = NULL;
925 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
929 // todo FreeStrBuf(&PlainMessageBuf); PlainMessageBuf = NULL;
934 if (PlainMessageBuf == NULL) {
935 PlainMessageBuf = NewStrBufPlain(NULL, 10*SIZ);
938 /* Ok, here we go ... */
940 msg = CtdlFetchMessage(msgnum, 1);
945 GetMetaData(&smi, msgnum);
946 smi.meta_msgnum = msgnum;
947 smi.meta_refcount = refcount;
949 /* restore the content type from the message body: */
950 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
953 safestrncpy(smi.meta_content_type, &mptr[13], sizeof smi.meta_content_type);
954 striplt(smi.meta_content_type);
955 aptr = smi.meta_content_type;
956 while (!IsEmptyStr(aptr)) {
961 memset(aptr, 0, sizeof(smi.meta_content_type) - (aptr - smi.meta_content_type));
967 CCC->redirect_buffer = PlainMessageBuf;
968 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
969 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
970 CCC->redirect_buffer = NULL;
973 "Setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s",
976 smi.meta_rfc822_length,
977 smi.meta_content_type
988 void migr_check_room_msg(long msgnum, void *userdata) {
989 fprintf(migr_global_message_list, "%ld %s\n", msgnum, CC->room.QRname);
993 void migr_check_rooms_backend(struct ctdlroom *buf, void *data) {
995 /* message list goes inside this tag */
997 CtdlGetRoom(&CC->room, buf->QRname);
998 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_check_room_msg, NULL);
1002 void RemoveMessagesFromRooms(StrBuf *RoomNameVec, long msgnum) {
1003 struct MetaData smi;
1004 const char *Pos = NULL;
1005 StrBuf *oneRoom = NewStrBuf();
1007 syslog(LOG_INFO, "removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
1009 while (Pos != StrBufNOTNULL){
1010 StrBufExtract_NextToken(oneRoom, RoomNameVec, &Pos, '|');
1011 CtdlDeleteMessages(ChrPtr(oneRoom), &msgnum, 1, "");
1013 GetMetaData(&smi, msgnum);
1014 AdjRefCount(msgnum, -smi.meta_refcount);
1018 void migr_do_restore_meta(void) {
1029 migr_global_message_list = fopen(migr_tempfilename1, "w");
1030 if (migr_global_message_list != NULL) {
1031 CtdlForEachRoom(migr_check_rooms_backend, NULL);
1032 fclose(migr_global_message_list);
1036 * Process the 'global' message list. (Sort it and remove dups.
1037 * Dups are ok because a message may be in more than one room, but
1038 * this will be handled by exporting the reference count, not by
1039 * exporting the message multiple times.)
1041 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
1042 if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
1044 RoomNames = NewStrBuf();
1046 migr_global_message_list = fopen(migr_tempfilename2, "r");
1047 if (migr_global_message_list != NULL) {
1048 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
1049 while ((Ctx->kill_me == 0) &&
1050 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
1057 prn = strchr(buf, ' ');
1058 if (lastnum != msgnum) {
1059 failGetMessage = migr_restore_message_metadata(lastnum, refcount);
1060 if (failGetMessage) {
1061 RemoveMessagesFromRooms(RoomNames, lastnum);
1066 StrBufPlain(RoomNames, prn + 1, -1);
1068 StrBufTrim(RoomNames);
1072 if (StrLength(RoomNames) > 0) {
1073 StrBufAppendBufPlain(RoomNames, HKEY("|"), 0);
1075 StrBufAppendBufPlain(RoomNames, prn, -1, 1);
1076 StrBufTrim(RoomNames);
1082 failGetMessage = migr_restore_message_metadata(msgnum, refcount);
1083 if (failGetMessage) {
1084 RemoveMessagesFromRooms(RoomNames, lastnum);
1086 fclose(migr_global_message_list);
1089 migr_restore_message_metadata(-1L, -1); /* This frees the encoding buffer */
1090 cprintf("%d system analysis completed", CIT_OK);
1095 /******************************************************************************
1096 * Dispatcher, Common code *
1097 ******************************************************************************/
1098 void cmd_migr(char *cmdbuf) {
1101 if (CtdlAccessCheck(ac_internal)) return;
1103 if (CtdlTrySingleUser()) {
1104 CtdlDisableHouseKeeping();
1105 CtdlMakeTempFileName(migr_tempfilename1, sizeof migr_tempfilename1);
1106 CtdlMakeTempFileName(migr_tempfilename2, sizeof migr_tempfilename2);
1108 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
1109 if (!strcasecmp(cmd, "export")) {
1112 else if (!strcasecmp(cmd, "import")) {
1115 else if (!strcasecmp(cmd, "listdirs")) {
1118 else if (!strcasecmp(cmd, "restoremeta")) {
1119 migr_do_restore_meta();
1122 cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
1125 unlink(migr_tempfilename1);
1126 unlink(migr_tempfilename2);
1128 CtdlEnableHouseKeeping();
1129 CtdlEndSingleUser();
1132 cprintf("%d The migrator is already running.\n", ERROR + RESOURCE_BUSY);
1137 /******************************************************************************
1139 ******************************************************************************/
1141 CTDL_MODULE_INIT(migrate)
1145 CtdlRegisterProtoHook(cmd_migr, "MIGR", "Across-the-wire migration");
1148 /* return our module name for the log */