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 * 26-99% exporting messages
26 * 100% finished exporting messages
28 * These tags are inserted into the XML stream to give the reader an approximation of its progress.
39 #include <sys/types.h>
46 #include <libcitadel.h>
49 #include "citserver.h"
55 #include "euidindex.h"
56 #include "ctdl_module.h"
58 char migr_tempfilename1[PATH_MAX];
59 char migr_tempfilename2[PATH_MAX];
60 FILE *migr_global_message_list;
62 char *ikey = NULL; // If we're importing a config key we store it here.
64 /******************************************************************************
65 * Code which implements the export appears in this section *
66 ******************************************************************************/
69 * Output a string to the client with these characters escaped: & < > " '
71 void xml_strout(char *str) {
81 client_write(HKEY("""));
83 else if (*c == '\'') {
84 client_write(HKEY("'"));
87 client_write(HKEY("<"));
90 client_write(HKEY(">"));
93 client_write(HKEY("&"));
104 * Export a user record as XML
106 void migr_export_users_backend(char *username, void *data) {
109 if (CtdlGetUser(&u, username) != 0) {
113 client_write(HKEY("<user>\n"));
114 cprintf("<u_version>%d</u_version>\n", u.version);
115 cprintf("<u_uid>%ld</u_uid>\n", (long)u.uid);
116 client_write(HKEY("<u_password>")); xml_strout(u.password); client_write(HKEY("</u_password>\n"));
117 cprintf("<u_flags>%u</u_flags>\n", u.flags);
118 cprintf("<u_timescalled>%ld</u_timescalled>\n", u.timescalled);
119 cprintf("<u_posted>%ld</u_posted>\n", u.posted);
120 cprintf("<u_axlevel>%d</u_axlevel>\n", u.axlevel);
121 cprintf("<u_usernum>%ld</u_usernum>\n", u.usernum);
122 cprintf("<u_lastcall>%ld</u_lastcall>\n", (long)u.lastcall);
123 cprintf("<u_USuserpurge>%d</u_USuserpurge>\n", u.USuserpurge);
124 client_write(HKEY("<u_fullname>")); xml_strout(u.fullname); client_write(HKEY("</u_fullname>\n"));
125 cprintf("<u_msgnum_bio>%ld</u_msgnum_bio>\n", u.msgnum_bio);
126 cprintf("<u_msgnum_pic>%ld</u_msgnum_pic>\n", u.msgnum_pic);
127 cprintf("<u_emailaddrs>%s</u_emailaddrs>\n", u.emailaddrs);
128 cprintf("<u_msgnum_inboxrules>%ld</u_msgnum_inboxrules>\n", u.msgnum_inboxrules);
129 cprintf("<u_lastproc_inboxrules>%ld</u_lastproc_inboxrules>\n", u.lastproc_inboxrules);
130 client_write(HKEY("</user>\n"));
134 void migr_export_users(void) {
135 ForEachUser(migr_export_users_backend, NULL);
139 void migr_export_room_msg(long msgnum, void *userdata) {
140 static int count = 0;
142 cprintf("%ld,", msgnum);
146 fprintf(migr_global_message_list, "%ld\n", msgnum);
150 void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
151 client_write(HKEY("<room>\n"));
152 client_write(HKEY("<QRname>"));
153 xml_strout(buf->QRname);
154 client_write(HKEY("</QRname>\n"));
155 client_write(HKEY("<QRpasswd>"));
156 xml_strout(buf->QRpasswd);
157 client_write(HKEY("</QRpasswd>\n"));
158 cprintf("<QRroomaide>%ld</QRroomaide>\n", buf->QRroomaide);
159 cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
160 cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
161 cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
162 if (buf->QRflags & QR_DIRECTORY) {
163 client_write(HKEY("<QRdirname>"));
164 xml_strout(buf->QRdirname);
165 client_write(HKEY("</QRdirname>\n"));
167 cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
168 cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
169 cprintf("<QRexpire_mode>%d</QRexpire_mode>\n", buf->QRep.expire_mode);
170 cprintf("<QRexpire_value>%d</QRexpire_value>\n", buf->QRep.expire_value);
171 cprintf("<QRnumber>%ld</QRnumber>\n", buf->QRnumber);
172 cprintf("<QRorder>%d</QRorder>\n", buf->QRorder);
173 cprintf("<QRflags2>%u</QRflags2>\n", buf->QRflags2);
174 cprintf("<QRdefaultview>%d</QRdefaultview>\n", buf->QRdefaultview);
175 cprintf("<msgnum_info>%ld</msgnum_info>\n", buf->msgnum_info);
176 cprintf("<msgnum_pic>%ld</msgnum_pic>\n", buf->msgnum_pic);
177 client_write(HKEY("</room>\n"));
179 /* message list goes inside this tag */
180 CtdlGetRoom(&CC->room, buf->QRname);
181 client_write(HKEY("<room_messages>"));
182 client_write(HKEY("<FRname>"));
183 xml_strout(buf->QRname); // buf->QRname rather than CC->room.QRname to guarantee consistency
184 client_write(HKEY("</FRname>\n"));
185 client_write(HKEY("<FRmsglist>\n"));
186 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_export_room_msg, NULL);
187 client_write(HKEY("</FRmsglist>\n"));
188 client_write(HKEY("</room_messages>\n"));
192 void migr_export_rooms(void) {
194 migr_global_message_list = fopen(migr_tempfilename1, "w");
195 if (migr_global_message_list != NULL) {
196 CtdlForEachRoom(migr_export_rooms_backend, NULL);
197 fclose(migr_global_message_list);
201 * Process the 'global' message list. (Sort it and remove dups.
202 * Dups are ok because a message may be in more than one room, but
203 * this will be handled by exporting the reference count, not by
204 * exporting the message multiple times.)
206 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
207 if (system(cmd) != 0) syslog(LOG_ERR, "Error %d", errno);
208 snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
209 if (system(cmd) != 0) syslog(LOG_ERR, "Error %d", errno);
212 snprintf(cmd, sizeof cmd, "wc -l %s", migr_tempfilename1);
213 FILE *fp = popen(cmd, "r");
215 fgets(cmd, sizeof cmd, fp);
217 total_msgs = atoi(cmd);
220 total_msgs = 1; // any nonzero just to keep it from barfing
222 syslog(LOG_DEBUG, "Total messages to be exported: %d", total_msgs);
226 void migr_export_floors(void) {
227 struct floor qfbuf, *buf;
230 for (i=0; i < MAXFLOORS; ++i) {
231 client_write(HKEY("<floor>\n"));
232 cprintf("<f_num>%d</f_num>\n", i);
233 CtdlGetFloor(&qfbuf, i);
235 cprintf("<f_flags>%u</f_flags>\n", buf->f_flags);
236 client_write(HKEY("<f_name>")); xml_strout(buf->f_name); client_write(HKEY("</f_name>\n"));
237 cprintf("<f_ref_count>%d</f_ref_count>\n", buf->f_ref_count);
238 cprintf("<f_ep_expire_mode>%d</f_ep_expire_mode>\n", buf->f_ep.expire_mode);
239 cprintf("<f_ep_expire_value>%d</f_ep_expire_value>\n", buf->f_ep.expire_value);
240 client_write(HKEY("</floor>\n"));
246 * Return nonzero if the supplied string contains only characters which are valid in a sequence set.
248 int is_sequence_set(char *s) {
253 while (ch = *c++, ch) {
254 if (!strchr("0123456789*,:", ch)) {
263 * Traverse the visits file...
265 void migr_export_visits(void) {
267 struct cdbdata *cdbv;
269 cdb_rewind(CDB_VISIT);
271 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
272 memset(&vbuf, 0, sizeof(visit));
273 memcpy(&vbuf, cdbv->ptr,
274 ((cdbv->len > sizeof(visit)) ?
275 sizeof(visit) : cdbv->len));
278 client_write(HKEY("<visit>\n"));
279 cprintf("<v_roomnum>%ld</v_roomnum>\n", vbuf.v_roomnum);
280 cprintf("<v_roomgen>%ld</v_roomgen>\n", vbuf.v_roomgen);
281 cprintf("<v_usernum>%ld</v_usernum>\n", vbuf.v_usernum);
283 client_write(HKEY("<v_seen>"));
284 if ( (!IsEmptyStr(vbuf.v_seen)) && (is_sequence_set(vbuf.v_seen)) ) {
285 xml_strout(vbuf.v_seen);
288 cprintf("%ld", vbuf.v_lastseen);
290 client_write(HKEY("</v_seen>"));
292 if ( (!IsEmptyStr(vbuf.v_answered)) && (is_sequence_set(vbuf.v_answered)) ) {
293 client_write(HKEY("<v_answered>"));
294 xml_strout(vbuf.v_answered);
295 client_write(HKEY("</v_answered>\n"));
298 cprintf("<v_flags>%u</v_flags>\n", vbuf.v_flags);
299 cprintf("<v_view>%d</v_view>\n", vbuf.v_view);
300 client_write(HKEY("</visit>\n"));
305 void migr_export_message(long msgnum) {
307 struct CtdlMessage *msg;
310 /* We can use a static buffer here because there will never be more than
311 * one of this operation happening at any given time, and it's really best
312 * to just keep it allocated once instead of torturing malloc/free.
313 * Call this function with msgnum "-1" to free the buffer when finished.
315 static int encoded_alloc = 0;
316 static char *encoded_msg = NULL;
319 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
327 /* Ok, here we go ... */
329 msg = CtdlFetchMessage(msgnum, 1);
330 if (msg == NULL) return; /* fail silently */
332 client_write(HKEY("<message>\n"));
333 GetMetaData(&smi, msgnum);
334 cprintf("<msg_msgnum>%ld</msg_msgnum>\n", msgnum);
335 cprintf("<msg_meta_refcount>%d</msg_meta_refcount>\n", smi.meta_refcount);
336 cprintf("<msg_meta_rfc822_length>%ld</msg_meta_rfc822_length>\n", smi.meta_rfc822_length);
337 client_write(HKEY("<msg_meta_content_type>")); xml_strout(smi.meta_content_type); client_write(HKEY("</msg_meta_content_type>\n"));
339 client_write(HKEY("<msg_text>"));
340 CtdlSerializeMessage(&smr, msg);
343 /* Predict the buffer size we need. Expand the buffer if necessary. */
344 int encoded_len = smr.len * 15 / 10 ;
345 if (encoded_len > encoded_alloc) {
346 encoded_alloc = encoded_len;
347 encoded_msg = realloc(encoded_msg, encoded_alloc);
350 if (encoded_msg == NULL) {
351 /* Questionable hack that hopes it'll work next time and we only lose one message */
355 /* Once we do the encoding we know the exact size */
356 encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
357 client_write(encoded_msg, encoded_len);
362 client_write(HKEY("</msg_text>\n"));
363 client_write(HKEY("</message>\n"));
367 void migr_export_openids(void) {
368 struct cdbdata *cdboi;
372 cdb_rewind(CDB_EXTAUTH);
373 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
374 if (cdboi->len > sizeof(long)) {
375 client_write(HKEY("<openid>\n"));
376 memcpy(&usernum, cdboi->ptr, sizeof(long));
377 snprintf(url, sizeof url, "%s", (cdboi->ptr)+sizeof(long) );
378 client_write(HKEY("<oid_url>"));
380 client_write(HKEY("</oid_url>\n"));
381 cprintf("<oid_usernum>%ld</oid_usernum>\n", usernum);
382 client_write(HKEY("</openid>\n"));
389 void migr_export_configs(void) {
390 struct cdbdata *cdbcfg;
395 cdb_rewind(CDB_CONFIG);
396 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
398 keylen = strlen(cdbcfg->ptr);
400 value = cdbcfg->ptr + keylen + 1;
402 client_write("<config key=\"", 13);
404 client_write("\">", 2);
406 client_write("</config>\n", 10);
412 void migr_export_messages(void) {
417 int prev_progress = 0;
421 migr_global_message_list = fopen(migr_tempfilename1, "r");
422 if (migr_global_message_list != NULL) {
423 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
424 while ((Ctx->kill_me == 0) &&
425 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
428 migr_export_message(msgnum);
431 progress = (count * 74 / total_msgs) + 25 ;
432 if ((progress > prev_progress) && (progress < 100)) {
433 cprintf("<progress>%d</progress>\n", progress);
435 prev_progress = progress;
437 fclose(migr_global_message_list);
439 if (Ctx->kill_me == 0) {
440 syslog(LOG_INFO, "Exported %d messages.", count);
443 syslog(LOG_ERR, "Export aborted due to client disconnect!");
446 migr_export_message(-1L); /* This frees the encoding buffer */
450 void migr_do_export(void) {
454 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
457 client_write(HKEY("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"));
458 client_write(HKEY("<citadel_migrate_data>\n"));
459 cprintf("<version>%d</version>\n", REV_LEVEL);
460 cprintf("<progress>%d</progress>\n", 0);
462 /* export the configuration database */
463 migr_export_configs();
464 cprintf("<progress>%d</progress>\n", 2);
466 if (Ctx->kill_me == 0) migr_export_users();
467 cprintf("<progress>%d</progress>\n", 7);
468 if (Ctx->kill_me == 0) migr_export_openids();
469 cprintf("<progress>%d</progress>\n", 12);
470 if (Ctx->kill_me == 0) migr_export_rooms();
471 cprintf("<progress>%d</progress>\n", 17);
472 if (Ctx->kill_me == 0) migr_export_floors();
473 cprintf("<progress>%d</progress>\n", 18);
474 if (Ctx->kill_me == 0) migr_export_visits();
475 cprintf("<progress>%d</progress>\n", 25);
476 if (Ctx->kill_me == 0) migr_export_messages();
477 cprintf("<progress>%d</progress>\n", 100);
478 client_write(HKEY("</citadel_migrate_data>\n"));
479 client_write(HKEY("000\n"));
484 /******************************************************************************
486 * Here's the code that implements the import side. It's going to end up *
487 * being one big loop with lots of global variables. I don't care. *
488 * You wouldn't run multiple concurrent imports anyway. If this offends your *
489 * delicate sensibilities then go rewrite it in Ruby on Rails or something. *
490 ******************************************************************************/
493 int citadel_migrate_data = 0; /* Are we inside a <citadel_migrate_data> tag pair? */
494 StrBuf *migr_chardata = NULL;
495 StrBuf *migr_MsgData = NULL;
496 struct ctdluser usbuf;
497 struct ctdlroom qrbuf;
498 char openid_url[512];
499 long openid_usernum = 0;
500 char FRname[ROOMNAMELEN];
505 long import_msgnum = 0;
508 * This callback stores up the data which appears in between tags.
510 void migr_xml_chardata(void *data, const XML_Char *s, int len) {
515 syslog(LOG_DEBUG, "chardata <\033[35m%s\033[0m>", aaa);
517 StrBufAppendBufPlain(migr_chardata, s, len, 0);
521 void migr_xml_start(void *data, const char *el, const char **attr) {
524 /*** GENERAL STUFF ***/
526 /* Reset chardata_len to zero and init buffer */
527 FlushStrBuf(migr_chardata);
528 FlushStrBuf(migr_MsgData);
530 if (!strcasecmp(el, "citadel_migrate_data")) {
531 ++citadel_migrate_data;
533 /* As soon as it looks like the input data is a genuine Citadel XML export,
534 * whack the existing database on disk to make room for the new one.
536 if (citadel_migrate_data == 1) {
537 for (i = 0; i < MAXCDB; ++i) {
544 if (citadel_migrate_data != 1) {
545 syslog(LOG_ERR, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
549 /* When we begin receiving XML for one of these record types, clear out the associated
550 * buffer so we don't accidentally carry over any data from a previous record.
552 if (!strcasecmp(el, "user")) memset(&usbuf, 0, sizeof (struct ctdluser));
553 else if (!strcasecmp(el, "openid")) memset(openid_url, 0, sizeof openid_url);
554 else if (!strcasecmp(el, "room")) memset(&qrbuf, 0, sizeof (struct ctdlroom));
555 else if (!strcasecmp(el, "room_messages")) memset(FRname, 0, sizeof FRname);
556 else if (!strcasecmp(el, "floor")) memset(&flbuf, 0, sizeof (struct floor));
557 else if (!strcasecmp(el, "visit")) memset(&vbuf, 0, sizeof (visit));
559 else if (!strcasecmp(el, "message")) {
560 memset(&smi, 0, sizeof (struct MetaData));
563 else if (!strcasecmp(el, "config")) {
569 if (!strcasecmp(attr[0], "key")) {
570 ikey = strdup(attr[1]);
579 int migr_userrecord(void *data, const char *el) {
580 if (!strcasecmp(el, "u_version")) usbuf.version = atoi(ChrPtr(migr_chardata));
581 else if (!strcasecmp(el, "u_uid")) usbuf.uid = atol(ChrPtr(migr_chardata));
582 else if (!strcasecmp(el, "u_password")) safestrncpy(usbuf.password, ChrPtr(migr_chardata), sizeof usbuf.password);
583 else if (!strcasecmp(el, "u_flags")) usbuf.flags = atoi(ChrPtr(migr_chardata));
584 else if (!strcasecmp(el, "u_timescalled")) usbuf.timescalled = atol(ChrPtr(migr_chardata));
585 else if (!strcasecmp(el, "u_posted")) usbuf.posted = atol(ChrPtr(migr_chardata));
586 else if (!strcasecmp(el, "u_axlevel")) usbuf.axlevel = atoi(ChrPtr(migr_chardata));
587 else if (!strcasecmp(el, "u_usernum")) usbuf.usernum = atol(ChrPtr(migr_chardata));
588 else if (!strcasecmp(el, "u_lastcall")) usbuf.lastcall = atol(ChrPtr(migr_chardata));
589 else if (!strcasecmp(el, "u_USuserpurge")) usbuf.USuserpurge = atoi(ChrPtr(migr_chardata));
590 else if (!strcasecmp(el, "u_fullname")) safestrncpy(usbuf.fullname, ChrPtr(migr_chardata), sizeof usbuf.fullname);
591 else if (!strcasecmp(el, "u_msgnum_bio")) usbuf.msgnum_bio = atol(ChrPtr(migr_chardata));
592 else if (!strcasecmp(el, "u_msgnum_pic")) usbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
593 else if (!strcasecmp(el, "u_emailaddrs")) safestrncpy(usbuf.emailaddrs, ChrPtr(migr_chardata), sizeof usbuf.emailaddrs);
594 else if (!strcasecmp(el, "u_msgnum_inboxrules")) usbuf.msgnum_inboxrules = atol(ChrPtr(migr_chardata));
595 else if (!strcasecmp(el, "u_lastproc_inboxrules")) usbuf.lastproc_inboxrules = atol(ChrPtr(migr_chardata));
601 int migr_roomrecord(void *data, const char *el) {
602 if (!strcasecmp(el, "QRname")) safestrncpy(qrbuf.QRname, ChrPtr(migr_chardata), sizeof qrbuf.QRname);
603 else if (!strcasecmp(el, "QRpasswd")) safestrncpy(qrbuf.QRpasswd, ChrPtr(migr_chardata), sizeof qrbuf.QRpasswd);
604 else if (!strcasecmp(el, "QRroomaide")) qrbuf.QRroomaide = atol(ChrPtr(migr_chardata));
605 else if (!strcasecmp(el, "QRhighest")) qrbuf.QRhighest = atol(ChrPtr(migr_chardata));
606 else if (!strcasecmp(el, "QRgen")) qrbuf.QRgen = atol(ChrPtr(migr_chardata));
607 else if (!strcasecmp(el, "QRflags")) qrbuf.QRflags = atoi(ChrPtr(migr_chardata));
608 else if (!strcasecmp(el, "QRdirname")) safestrncpy(qrbuf.QRdirname, ChrPtr(migr_chardata), sizeof qrbuf.QRdirname);
609 else if (!strcasecmp(el, "QRfloor")) qrbuf.QRfloor = atoi(ChrPtr(migr_chardata));
610 else if (!strcasecmp(el, "QRmtime")) qrbuf.QRmtime = atol(ChrPtr(migr_chardata));
611 else if (!strcasecmp(el, "QRexpire_mode")) qrbuf.QRep.expire_mode = atoi(ChrPtr(migr_chardata));
612 else if (!strcasecmp(el, "QRexpire_value")) qrbuf.QRep.expire_value = atoi(ChrPtr(migr_chardata));
613 else if (!strcasecmp(el, "QRnumber")) qrbuf.QRnumber = atol(ChrPtr(migr_chardata));
614 else if (!strcasecmp(el, "QRorder")) qrbuf.QRorder = atoi(ChrPtr(migr_chardata));
615 else if (!strcasecmp(el, "QRflags2")) qrbuf.QRflags2 = atoi(ChrPtr(migr_chardata));
616 else if (!strcasecmp(el, "QRdefaultview")) qrbuf.QRdefaultview = atoi(ChrPtr(migr_chardata));
617 else if (!strcasecmp(el, "msgnum_info")) qrbuf.msgnum_info = atol(ChrPtr(migr_chardata));
618 else if (!strcasecmp(el, "msgnum_pic")) qrbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
624 int migr_floorrecord(void *data, const char *el) {
625 if (!strcasecmp(el, "f_num")) floornum = atoi(ChrPtr(migr_chardata));
626 else if (!strcasecmp(el, "f_flags")) flbuf.f_flags = atoi(ChrPtr(migr_chardata));
627 else if (!strcasecmp(el, "f_name")) safestrncpy(flbuf.f_name, ChrPtr(migr_chardata), sizeof flbuf.f_name);
628 else if (!strcasecmp(el, "f_ref_count")) flbuf.f_ref_count = atoi(ChrPtr(migr_chardata));
629 else if (!strcasecmp(el, "f_ep_expire_mode")) flbuf.f_ep.expire_mode = atoi(ChrPtr(migr_chardata));
630 else if (!strcasecmp(el, "f_ep_expire_value")) flbuf.f_ep.expire_value = atoi(ChrPtr(migr_chardata));
636 int migr_visitrecord(void *data, const char *el) {
637 if (!strcasecmp(el, "v_roomnum")) vbuf.v_roomnum = atol(ChrPtr(migr_chardata));
638 else if (!strcasecmp(el, "v_roomgen")) vbuf.v_roomgen = atol(ChrPtr(migr_chardata));
639 else if (!strcasecmp(el, "v_usernum")) vbuf.v_usernum = atol(ChrPtr(migr_chardata));
641 else if (!strcasecmp(el, "v_seen")) {
642 int is_textual_seen = 0;
644 int max = StrLength(migr_chardata);
646 vbuf.v_lastseen = atol(ChrPtr(migr_chardata));
648 for (i=0; i < max; ++i)
649 if (!isdigit(ChrPtr(migr_chardata)[i]))
652 safestrncpy(vbuf.v_seen, ChrPtr(migr_chardata), sizeof vbuf.v_seen);
655 else if (!strcasecmp(el, "v_answered")) safestrncpy(vbuf.v_answered, ChrPtr(migr_chardata), sizeof vbuf.v_answered);
656 else if (!strcasecmp(el, "v_flags")) vbuf.v_flags = atoi(ChrPtr(migr_chardata));
657 else if (!strcasecmp(el, "v_view")) vbuf.v_view = atoi(ChrPtr(migr_chardata));
663 void migr_xml_end(void *data, const char *el) {
667 long *msglist = NULL;
668 int msglist_alloc = 0;
669 /*** GENERAL STUFF ***/
671 if (!strcasecmp(el, "citadel_migrate_data")) {
672 --citadel_migrate_data;
676 if (citadel_migrate_data != 1) {
677 syslog(LOG_ERR, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
681 // syslog(LOG_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
685 if (!strcasecmp(el, "config")) {
686 syslog(LOG_DEBUG, "Imported config key=%s", ikey);
689 CtdlSetConfigStr(ikey, (char *)ChrPtr(migr_chardata));
694 syslog(LOG_INFO, "Closed a <config> tag but no key name was supplied.");
699 else if ((!strncasecmp(el, HKEY("u_"))) &&
700 migr_userrecord(data, el))
701 ; /* Nothing to do anymore */
702 else if (!strcasecmp(el, "user")) {
704 syslog(LOG_INFO, "Imported user: %s", usbuf.fullname);
709 else if (!strcasecmp(el, "oid_url")) safestrncpy(openid_url, ChrPtr(migr_chardata), sizeof openid_url);
710 else if (!strcasecmp(el, "oid_usernum")) openid_usernum = atol(ChrPtr(migr_chardata));
712 else if (!strcasecmp(el, "openid")) { /* see serv_openid_rp.c for a description of the record format */
715 oid_data_len = sizeof(long) + strlen(openid_url) + 1;
716 oid_data = malloc(oid_data_len);
717 memcpy(oid_data, &openid_usernum, sizeof(long));
718 memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
719 cdb_store(CDB_EXTAUTH, openid_url, strlen(openid_url), oid_data, oid_data_len);
721 syslog(LOG_INFO, "Imported OpenID: %s (%ld)", openid_url, openid_usernum);
725 else if ((!strncasecmp(el, HKEY("QR"))) &&
726 migr_roomrecord(data, el))
727 ; /* Nothing to do anymore */
728 else if (!strcasecmp(el, "room")) {
730 syslog(LOG_INFO, "Imported room: %s", qrbuf.QRname);
733 /*** ROOM MESSAGE POINTERS ***/
735 else if (!strcasecmp(el, "FRname")) {
736 safestrncpy(FRname, ChrPtr(migr_chardata), sizeof FRname);
739 else if (!strcasecmp(el, "FRmsglist")) {
740 if (!IsEmptyStr(FRname)) {
742 msglist_alloc = 1000;
743 msglist = malloc(sizeof(long) * msglist_alloc);
745 syslog(LOG_DEBUG, "Message list for: %s", FRname);
747 ptr = ChrPtr(migr_chardata);
748 syslog(LOG_DEBUG, "\033[31m%s\033[0m", ptr);
750 while ((*ptr != 0) && (!isdigit(*ptr))) {
753 if ((*ptr != 0) && (isdigit(*ptr))) {
756 if (msgcount >= msglist_alloc) {
758 msglist = realloc(msglist, sizeof(long) * msglist_alloc);
760 msglist[msgcount++] = msgnum;
761 syslog(LOG_DEBUG, "\033[32mmsgnum %ld , count is now %d\033[0m", msgnum, msgcount);
764 while ((*ptr != 0) && (isdigit(*ptr))) {
770 CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL, 1);
775 syslog(LOG_DEBUG, "Imported %d messages.", msgcount);
776 if (server_shutting_down) {
782 else if ((!strncasecmp(el, HKEY("f_"))) &&
783 migr_floorrecord(data, el))
784 ; /* Nothing to do anymore */
786 else if (!strcasecmp(el, "floor")) {
787 CtdlPutFloor(&flbuf, floornum);
788 syslog(LOG_INFO, "Imported floor #%d (%s)", floornum, flbuf.f_name);
792 else if ((!strncasecmp(el, HKEY("v_"))) && migr_visitrecord(data, el)) {
793 ; /* Nothing to do anymore */
795 else if (!strcasecmp(el, "visit")) {
797 syslog(LOG_INFO, "Imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
802 else if (!strcasecmp(el, "msg_msgnum")) smi.meta_msgnum = import_msgnum = atol(ChrPtr(migr_chardata));
803 else if (!strcasecmp(el, "msg_meta_refcount")) smi.meta_refcount = atoi(ChrPtr(migr_chardata));
804 else if (!strcasecmp(el, "msg_meta_rfc822_length")) smi.meta_rfc822_length = atoi(ChrPtr(migr_chardata));
805 else if (!strcasecmp(el, "msg_meta_content_type")) safestrncpy(smi.meta_content_type, ChrPtr(migr_chardata), sizeof smi.meta_content_type);
807 else if (!strcasecmp(el, "msg_text")) {
809 struct CtdlMessage *msg;
811 FlushStrBuf(migr_MsgData);
812 StrBufDecodeBase64To(migr_chardata, migr_MsgData);
814 msg = CtdlDeserializeMessage(import_msgnum, -1,
815 ChrPtr(migr_MsgData),
816 StrLength(migr_MsgData));
818 rc = CtdlSaveThisMessage(msg, import_msgnum, 0);
829 "%s message #%ld, size=%d, refcount=%d, bodylength=%ld, content-type: %s",
830 (rc!= 0)?"failed to import ":"Imported ",
832 StrLength(migr_MsgData),
834 smi.meta_rfc822_length,
835 smi.meta_content_type
837 memset(&smi, 0, sizeof(smi));
840 /*** MORE GENERAL STUFF ***/
842 FlushStrBuf(migr_chardata);
849 void migr_do_import(void) {
854 migr_chardata = NewStrBufPlain(NULL, SIZ * 20);
855 migr_MsgData = NewStrBufPlain(NULL, SIZ * 20);
856 xp = XML_ParserCreate(NULL);
858 cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
861 XML_SetElementHandler(xp, migr_xml_start, migr_xml_end);
862 XML_SetCharacterDataHandler(xp, migr_xml_chardata);
866 cprintf("%d sock it to me\n", SEND_LISTING);
869 client_set_inbound_buf(SIZ * 10);
871 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
872 XML_Parse(xp, buf, strlen(buf), 0);
873 if (server_shutting_down)
874 break; // Should we break or return?
877 XML_Parse(xp, "", 0, 1);
879 FreeStrBuf(&migr_chardata);
880 FreeStrBuf(&migr_MsgData);
881 rebuild_euid_index();
882 rebuild_usersbynumber();
883 CtdlSetConfigInt("MM_fulltext_wordbreaker", -1); // Set an invalid wordbreaker to force re-indexing
888 /******************************************************************************
889 * Dispatcher, Common code *
890 ******************************************************************************/
892 * Dump out the pathnames of directories which can be copied "as is"
894 void migr_do_listdirs(void) {
895 cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
896 cprintf("files|%s\n", ctdl_file_dir);
897 cprintf("messages|%s\n", ctdl_message_dir);
898 cprintf("keys|%s\n", ctdl_key_dir);
903 /******************************************************************************
904 * Repair database integrity *
905 ******************************************************************************/
907 StrBuf *PlainMessageBuf = NULL;
908 HashList *UsedMessageIDS = NULL;
910 int migr_restore_message_metadata(long msgnum, int refcount) {
912 struct CtdlMessage *msg;
915 /* We can use a static buffer here because there will never be more than
916 * one of this operation happening at any given time, and it's really best
917 * to just keep it allocated once instead of torturing malloc/free.
918 * Call this function with msgnum "-1" to free the buffer when finished.
920 static int encoded_alloc = 0;
921 static char *encoded_msg = NULL;
924 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
928 // todo FreeStrBuf(&PlainMessageBuf); PlainMessageBuf = NULL;
933 if (PlainMessageBuf == NULL) {
934 PlainMessageBuf = NewStrBufPlain(NULL, 10*SIZ);
937 /* Ok, here we go ... */
939 msg = CtdlFetchMessage(msgnum, 1);
944 GetMetaData(&smi, msgnum);
945 smi.meta_msgnum = msgnum;
946 smi.meta_refcount = refcount;
948 /* restore the content type from the message body: */
949 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
952 safestrncpy(smi.meta_content_type, &mptr[13], sizeof smi.meta_content_type);
953 striplt(smi.meta_content_type);
954 aptr = smi.meta_content_type;
955 while (!IsEmptyStr(aptr)) {
960 memset(aptr, 0, sizeof(smi.meta_content_type) - (aptr - smi.meta_content_type));
966 CC->redirect_buffer = PlainMessageBuf;
967 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
968 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
969 CC->redirect_buffer = NULL;
972 "Setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s",
975 smi.meta_rfc822_length,
976 smi.meta_content_type
985 void migr_check_room_msg(long msgnum, void *userdata) {
986 fprintf(migr_global_message_list, "%ld %s\n", msgnum, CC->room.QRname);
990 void migr_check_rooms_backend(struct ctdlroom *buf, void *data) {
991 /* message list goes inside this tag */
992 CtdlGetRoom(&CC->room, buf->QRname);
993 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_check_room_msg, NULL);
997 void RemoveMessagesFromRooms(StrBuf *RoomNameVec, long msgnum) {
999 const char *Pos = NULL;
1000 StrBuf *oneRoom = NewStrBuf();
1002 syslog(LOG_INFO, "removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
1004 while (Pos != StrBufNOTNULL){
1005 StrBufExtract_NextToken(oneRoom, RoomNameVec, &Pos, '|');
1006 CtdlDeleteMessages(ChrPtr(oneRoom), &msgnum, 1, "");
1008 GetMetaData(&smi, msgnum);
1009 AdjRefCount(msgnum, -smi.meta_refcount);
1013 void migr_do_restore_meta(void) {
1024 migr_global_message_list = fopen(migr_tempfilename1, "w");
1025 if (migr_global_message_list != NULL) {
1026 CtdlForEachRoom(migr_check_rooms_backend, NULL);
1027 fclose(migr_global_message_list);
1031 * Process the 'global' message list. (Sort it and remove dups.
1032 * Dups are ok because a message may be in more than one room, but
1033 * this will be handled by exporting the reference count, not by
1034 * exporting the message multiple times.)
1036 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
1037 if (system(cmd) != 0) syslog(LOG_ERR, "Error %d", errno);
1039 RoomNames = NewStrBuf();
1041 migr_global_message_list = fopen(migr_tempfilename2, "r");
1042 if (migr_global_message_list != NULL) {
1043 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
1044 while ((Ctx->kill_me == 0) &&
1045 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
1052 prn = strchr(buf, ' ');
1053 if (lastnum != msgnum) {
1054 failGetMessage = migr_restore_message_metadata(lastnum, refcount);
1055 if (failGetMessage) {
1056 RemoveMessagesFromRooms(RoomNames, lastnum);
1061 StrBufPlain(RoomNames, prn + 1, -1);
1063 StrBufTrim(RoomNames);
1067 if (StrLength(RoomNames) > 0) {
1068 StrBufAppendBufPlain(RoomNames, HKEY("|"), 0);
1070 StrBufAppendBufPlain(RoomNames, prn, -1, 1);
1071 StrBufTrim(RoomNames);
1077 failGetMessage = migr_restore_message_metadata(msgnum, refcount);
1078 if (failGetMessage) {
1079 RemoveMessagesFromRooms(RoomNames, lastnum);
1081 fclose(migr_global_message_list);
1084 migr_restore_message_metadata(-1L, -1); /* This frees the encoding buffer */
1085 cprintf("%d system analysis completed", CIT_OK);
1090 /******************************************************************************
1091 * Dispatcher, Common code *
1092 ******************************************************************************/
1093 void cmd_migr(char *cmdbuf) {
1096 if (CtdlAccessCheck(ac_internal)) return;
1098 if (CtdlTrySingleUser()) {
1099 CtdlDisableHouseKeeping();
1100 CtdlMakeTempFileName(migr_tempfilename1, sizeof migr_tempfilename1);
1101 CtdlMakeTempFileName(migr_tempfilename2, sizeof migr_tempfilename2);
1103 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
1104 if (!strcasecmp(cmd, "export")) {
1107 else if (!strcasecmp(cmd, "import")) {
1110 else if (!strcasecmp(cmd, "listdirs")) {
1113 else if (!strcasecmp(cmd, "restoremeta")) {
1114 migr_do_restore_meta();
1117 cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
1120 unlink(migr_tempfilename1);
1121 unlink(migr_tempfilename2);
1123 CtdlEnableHouseKeeping();
1124 CtdlEndSingleUser();
1127 cprintf("%d The migrator is already running.\n", ERROR + RESOURCE_BUSY);
1132 /******************************************************************************
1134 ******************************************************************************/
1136 CTDL_MODULE_INIT(migrate)
1140 CtdlRegisterProtoHook(cmd_migr, "MIGR", "Across-the-wire migration");
1143 /* return our module name for the log */