1 // This module dumps and/or loads the Citadel database in XML format.
3 // Copyright (c) 1987-2022 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // Explanation of <progress> tags:
16 // 2% finished exporting configuration
17 // 7% finished exporting users
18 // 12% finished exporting openids
19 // 17% finished exporting rooms
20 // 18% finished exporting floors
21 // 25% finished exporting visits
22 // 26-99% exporting messages
23 // 100% finished exporting messages
25 // These tags are inserted into the XML stream to give the reader an approximation of its progress.
27 #include "../../sysdep.h"
35 #include <sys/types.h>
42 #include <libcitadel.h>
43 #include "../../citadel.h"
44 #include "../../server.h"
45 #include "../../citserver.h"
46 #include "../../support.h"
47 #include "../../config.h"
48 #include "../../database.h"
49 #include "../../msgbase.h"
50 #include "../../user_ops.h"
51 #include "../../euidindex.h"
52 #include "../../ctdl_module.h"
54 char migr_tempfilename1[PATH_MAX];
55 char migr_tempfilename2[PATH_MAX];
56 FILE *migr_global_message_list;
58 char *ikey = NULL; // If we're importing a config key we store it here.
60 //*****************************************************************************
61 //* Code which implements the export appears in this section *
62 //*****************************************************************************
64 // Output a string to the client with these characters escaped: & < > " '
65 void xml_strout(char *str) {
75 client_write(HKEY("""));
77 else if (*c == '\'') {
78 client_write(HKEY("'"));
81 client_write(HKEY("<"));
84 client_write(HKEY(">"));
87 client_write(HKEY("&"));
97 // Export a user record as XML
98 void migr_export_users_backend(char *username, void *data) {
101 if (CtdlGetUser(&u, username) != 0) {
105 client_write(HKEY("<user>\n"));
106 cprintf("<u_version>%d</u_version>\n", u.version);
107 cprintf("<u_uid>%ld</u_uid>\n", (long)u.uid);
108 client_write(HKEY("<u_password>")); xml_strout(u.password); client_write(HKEY("</u_password>\n"));
109 cprintf("<u_flags>%u</u_flags>\n", u.flags);
110 cprintf("<u_timescalled>%ld</u_timescalled>\n", u.timescalled);
111 cprintf("<u_posted>%ld</u_posted>\n", u.posted);
112 cprintf("<u_axlevel>%d</u_axlevel>\n", u.axlevel);
113 cprintf("<u_usernum>%ld</u_usernum>\n", u.usernum);
114 cprintf("<u_lastcall>%ld</u_lastcall>\n", (long)u.lastcall);
115 cprintf("<u_USuserpurge>%d</u_USuserpurge>\n", u.USuserpurge);
116 client_write(HKEY("<u_fullname>")); xml_strout(u.fullname); client_write(HKEY("</u_fullname>\n"));
117 cprintf("<u_msgnum_bio>%ld</u_msgnum_bio>\n", u.msgnum_bio);
118 cprintf("<u_msgnum_pic>%ld</u_msgnum_pic>\n", u.msgnum_pic);
119 cprintf("<u_emailaddrs>%s</u_emailaddrs>\n", u.emailaddrs);
120 cprintf("<u_msgnum_inboxrules>%ld</u_msgnum_inboxrules>\n", u.msgnum_inboxrules);
121 cprintf("<u_lastproc_inboxrules>%ld</u_lastproc_inboxrules>\n", u.lastproc_inboxrules);
122 client_write(HKEY("</user>\n"));
126 void migr_export_users(void) {
127 ForEachUser(migr_export_users_backend, NULL);
131 void migr_export_room_msg(long msgnum, void *userdata) {
132 static int count = 0;
134 cprintf("%ld,", 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>"));
145 xml_strout(buf->QRname);
146 client_write(HKEY("</QRname>\n"));
147 client_write(HKEY("<QRpasswd>"));
148 xml_strout(buf->QRpasswd);
149 client_write(HKEY("</QRpasswd>\n"));
150 cprintf("<QRroomaide>%ld</QRroomaide>\n", buf->QRroomaide);
151 cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
152 cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
153 cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
154 if (buf->QRflags & QR_DIRECTORY) {
155 client_write(HKEY("<QRdirname>"));
156 xml_strout(buf->QRdirname);
157 client_write(HKEY("</QRdirname>\n"));
159 cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
160 cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
161 cprintf("<QRexpire_mode>%d</QRexpire_mode>\n", buf->QRep.expire_mode);
162 cprintf("<QRexpire_value>%d</QRexpire_value>\n", buf->QRep.expire_value);
163 cprintf("<QRnumber>%ld</QRnumber>\n", buf->QRnumber);
164 cprintf("<QRorder>%d</QRorder>\n", buf->QRorder);
165 cprintf("<QRflags2>%u</QRflags2>\n", buf->QRflags2);
166 cprintf("<QRdefaultview>%d</QRdefaultview>\n", buf->QRdefaultview);
167 cprintf("<msgnum_info>%ld</msgnum_info>\n", buf->msgnum_info);
168 cprintf("<msgnum_pic>%ld</msgnum_pic>\n", buf->msgnum_pic);
169 client_write(HKEY("</room>\n"));
171 // message list goes inside this tag
172 CtdlGetRoom(&CC->room, buf->QRname);
173 client_write(HKEY("<room_messages>"));
174 client_write(HKEY("<FRname>"));
175 xml_strout(buf->QRname); // buf->QRname rather than CC->room.QRname to guarantee consistency
176 client_write(HKEY("</FRname>\n"));
177 client_write(HKEY("<FRmsglist>\n"));
178 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_export_room_msg, NULL);
179 client_write(HKEY("</FRmsglist>\n"));
180 client_write(HKEY("</room_messages>\n"));
184 void migr_export_rooms(void) {
186 migr_global_message_list = fopen(migr_tempfilename1, "w");
187 if (migr_global_message_list != NULL) {
188 CtdlForEachRoom(migr_export_rooms_backend, NULL);
189 fclose(migr_global_message_list);
192 // Process the 'global' message list. (Sort it and remove dups.
193 // Dups are ok because a message may be in more than one room, but
194 // this will be handled by exporting the reference count, not by
195 // exporting the message multiple times.)
196 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
197 if (system(cmd) != 0) {
198 syslog(LOG_ERR, "migrate: error %d", errno);
200 snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
201 if (system(cmd) != 0) {
202 syslog(LOG_ERR, "migrate: 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, "migrate: 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"));
238 // Return nonzero if the supplied string contains only characters which are valid in a sequence set.
239 int is_sequence_set(char *s) {
244 while (ch = *c++, ch) {
245 if (!strchr("0123456789*,:", ch)) {
253 // 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;
298 long bytes_written = 0;
301 // We can use a static buffer here because there will never be more than
302 // one of this operation happening at any given time, and it's really best
303 // to just keep it allocated once instead of torturing malloc/free.
304 // Call this function with msgnum "-1" to free the buffer when finished.
306 static int encoded_alloc = 0;
307 static char *encoded_msg = NULL;
310 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
318 // Ok, here we go ...
320 msg = CtdlFetchMessage(msgnum, 1);
321 if (msg == NULL) return; // fail silently
323 client_write(HKEY("<message>\n"));
324 GetMetaData(&smi, msgnum);
325 cprintf("<msg_msgnum>%ld</msg_msgnum>\n", msgnum);
326 cprintf("<msg_meta_refcount>%d</msg_meta_refcount>\n", smi.meta_refcount);
327 cprintf("<msg_meta_rfc822_length>%ld</msg_meta_rfc822_length>\n", smi.meta_rfc822_length);
328 client_write(HKEY("<msg_meta_content_type>")); xml_strout(smi.meta_content_type); client_write(HKEY("</msg_meta_content_type>\n"));
330 client_write(HKEY("<msg_text>"));
331 CtdlSerializeMessage(&smr, msg);
334 // Predict the buffer size we need. Expand the buffer if necessary.
335 int encoded_len = smr.len * 15 / 10 ;
336 if (encoded_len > encoded_alloc) {
337 encoded_alloc = encoded_len;
338 encoded_msg = realloc(encoded_msg, encoded_alloc);
341 if (encoded_msg == NULL) {
342 // Questionable hack that hopes it'll work next time and we only lose one message
346 // Once we do the encoding we know the exact size
347 encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
349 // Careful now. If the message is gargantuan, trying to write multiple gigamegs in one
350 // big write operation can make our transport unhappy. So we'll chunk it up 10 KB at a time.
352 while ( (bytes_written < encoded_len) && (!server_shutting_down) ) {
353 this_block = encoded_len - bytes_written;
354 if (this_block > 10240) {
357 client_write(&encoded_msg[bytes_written], this_block);
358 bytes_written += this_block;
364 client_write(HKEY("</msg_text>\n"));
365 client_write(HKEY("</message>\n"));
369 void migr_export_openids(void) {
370 struct cdbdata *cdboi;
374 cdb_rewind(CDB_EXTAUTH);
375 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
376 if (cdboi->len > sizeof(long)) {
377 client_write(HKEY("<openid>\n"));
378 memcpy(&usernum, cdboi->ptr, sizeof(long));
379 snprintf(url, sizeof url, "%s", (cdboi->ptr)+sizeof(long) );
380 client_write(HKEY("<oid_url>"));
382 client_write(HKEY("</oid_url>\n"));
383 cprintf("<oid_usernum>%ld</oid_usernum>\n", usernum);
384 client_write(HKEY("</openid>\n"));
391 void migr_export_configs(void) {
392 struct cdbdata *cdbcfg;
397 cdb_rewind(CDB_CONFIG);
398 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
400 keylen = strlen(cdbcfg->ptr);
402 value = cdbcfg->ptr + keylen + 1;
404 client_write("<config key=\"", 13);
406 client_write("\">", 2);
408 client_write("</config>\n", 10);
414 void migr_export_messages(void) {
419 int prev_progress = 0;
423 migr_global_message_list = fopen(migr_tempfilename1, "r");
424 if (migr_global_message_list != NULL) {
425 syslog(LOG_INFO, "migrate: opened %s", migr_tempfilename1);
426 while ((Ctx->kill_me == 0) &&
427 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
430 migr_export_message(msgnum);
433 progress = (count * 74 / total_msgs) + 25 ;
434 if ((progress > prev_progress) && (progress < 100)) {
435 cprintf("<progress>%d</progress>\n", progress);
437 prev_progress = progress;
439 fclose(migr_global_message_list);
441 if (Ctx->kill_me == 0) {
442 syslog(LOG_INFO, "migrate: exported %d messages.", count);
445 syslog(LOG_ERR, "migrate: export aborted due to client disconnect!");
448 migr_export_message(-1L); // This frees the encoding buffer
452 void migr_do_export(void) {
456 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
459 client_write(HKEY("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"));
460 client_write(HKEY("<citadel_migrate_data>\n"));
461 cprintf("<version>%d</version>\n", REV_LEVEL);
462 cprintf("<progress>%d</progress>\n", 0);
464 // export the configuration database
465 migr_export_configs();
466 cprintf("<progress>%d</progress>\n", 2);
468 if (Ctx->kill_me == 0) migr_export_users();
469 cprintf("<progress>%d</progress>\n", 7);
470 if (Ctx->kill_me == 0) migr_export_openids();
471 cprintf("<progress>%d</progress>\n", 12);
472 if (Ctx->kill_me == 0) migr_export_rooms();
473 cprintf("<progress>%d</progress>\n", 17);
474 if (Ctx->kill_me == 0) migr_export_floors();
475 cprintf("<progress>%d</progress>\n", 18);
476 if (Ctx->kill_me == 0) migr_export_visits();
477 cprintf("<progress>%d</progress>\n", 25);
478 if (Ctx->kill_me == 0) migr_export_messages();
479 cprintf("<progress>%d</progress>\n", 100);
480 client_write(HKEY("</citadel_migrate_data>\n"));
481 client_write(HKEY("000\n"));
487 // Here's the code that implements the import side. It's going to end up
488 // being one big loop with lots of global variables. I don't care.
489 // You wouldn't run multiple concurrent imports anyway. If this offends your
490 // delicate sensibilities then go rewrite it in Ruby on Rails or something.
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;
507 // This callback stores up the data which appears in between tags.
508 void migr_xml_chardata(void *data, const XML_Char *s, int len) {
509 StrBufAppendBufPlain(migr_chardata, s, len, 0);
513 void migr_xml_start(void *data, const char *el, const char **attr) {
516 // *** GENERAL STUFF ***
518 // Reset chardata_len to zero and init buffer
519 FlushStrBuf(migr_chardata);
520 FlushStrBuf(migr_MsgData);
522 if (!strcasecmp(el, "citadel_migrate_data")) {
523 ++citadel_migrate_data;
525 // As soon as it looks like the input data is a genuine Citadel XML export,
526 // whack the existing database on disk to make room for the new one.
527 if (citadel_migrate_data == 1) {
528 syslog(LOG_INFO, "migrate: erasing existing databases so we can receive the incoming import");
529 for (i = 0; i < MAXCDB; ++i) {
536 if (citadel_migrate_data != 1) {
537 syslog(LOG_ERR, "migrate: out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
541 // When we begin receiving XML for one of these record types, clear out the associated
542 // buffer so we don't accidentally carry over any data from a previous record.
543 if (!strcasecmp(el, "user")) memset(&usbuf, 0, sizeof (struct ctdluser));
544 else if (!strcasecmp(el, "openid")) memset(openid_url, 0, sizeof openid_url);
545 else if (!strcasecmp(el, "room")) memset(&qrbuf, 0, sizeof (struct ctdlroom));
546 else if (!strcasecmp(el, "room_messages")) memset(FRname, 0, sizeof FRname);
547 else if (!strcasecmp(el, "floor")) memset(&flbuf, 0, sizeof (struct floor));
548 else if (!strcasecmp(el, "visit")) memset(&vbuf, 0, sizeof (visit));
550 else if (!strcasecmp(el, "message")) {
551 memset(&smi, 0, sizeof (struct MetaData));
554 else if (!strcasecmp(el, "config")) {
560 if (!strcasecmp(attr[0], "key")) {
561 ikey = strdup(attr[1]);
570 int migr_userrecord(void *data, const char *el) {
571 if (!strcasecmp(el, "u_version")) usbuf.version = atoi(ChrPtr(migr_chardata));
572 else if (!strcasecmp(el, "u_uid")) usbuf.uid = atol(ChrPtr(migr_chardata));
573 else if (!strcasecmp(el, "u_password")) safestrncpy(usbuf.password, ChrPtr(migr_chardata), sizeof usbuf.password);
574 else if (!strcasecmp(el, "u_flags")) usbuf.flags = atoi(ChrPtr(migr_chardata));
575 else if (!strcasecmp(el, "u_timescalled")) usbuf.timescalled = atol(ChrPtr(migr_chardata));
576 else if (!strcasecmp(el, "u_posted")) usbuf.posted = atol(ChrPtr(migr_chardata));
577 else if (!strcasecmp(el, "u_axlevel")) usbuf.axlevel = atoi(ChrPtr(migr_chardata));
578 else if (!strcasecmp(el, "u_usernum")) usbuf.usernum = atol(ChrPtr(migr_chardata));
579 else if (!strcasecmp(el, "u_lastcall")) usbuf.lastcall = atol(ChrPtr(migr_chardata));
580 else if (!strcasecmp(el, "u_USuserpurge")) usbuf.USuserpurge = atoi(ChrPtr(migr_chardata));
581 else if (!strcasecmp(el, "u_fullname")) safestrncpy(usbuf.fullname, ChrPtr(migr_chardata), sizeof usbuf.fullname);
582 else if (!strcasecmp(el, "u_msgnum_bio")) usbuf.msgnum_bio = atol(ChrPtr(migr_chardata));
583 else if (!strcasecmp(el, "u_msgnum_pic")) usbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
584 else if (!strcasecmp(el, "u_emailaddrs")) safestrncpy(usbuf.emailaddrs, ChrPtr(migr_chardata), sizeof usbuf.emailaddrs);
585 else if (!strcasecmp(el, "u_msgnum_inboxrules")) usbuf.msgnum_inboxrules = atol(ChrPtr(migr_chardata));
586 else if (!strcasecmp(el, "u_lastproc_inboxrules")) usbuf.lastproc_inboxrules = atol(ChrPtr(migr_chardata));
592 int migr_roomrecord(void *data, const char *el) {
593 if (!strcasecmp(el, "QRname")) safestrncpy(qrbuf.QRname, ChrPtr(migr_chardata), sizeof qrbuf.QRname);
594 else if (!strcasecmp(el, "QRpasswd")) safestrncpy(qrbuf.QRpasswd, ChrPtr(migr_chardata), sizeof qrbuf.QRpasswd);
595 else if (!strcasecmp(el, "QRroomaide")) qrbuf.QRroomaide = atol(ChrPtr(migr_chardata));
596 else if (!strcasecmp(el, "QRhighest")) qrbuf.QRhighest = atol(ChrPtr(migr_chardata));
597 else if (!strcasecmp(el, "QRgen")) qrbuf.QRgen = atol(ChrPtr(migr_chardata));
598 else if (!strcasecmp(el, "QRflags")) qrbuf.QRflags = atoi(ChrPtr(migr_chardata));
599 else if (!strcasecmp(el, "QRdirname")) safestrncpy(qrbuf.QRdirname, ChrPtr(migr_chardata), sizeof qrbuf.QRdirname);
600 else if (!strcasecmp(el, "QRfloor")) qrbuf.QRfloor = atoi(ChrPtr(migr_chardata));
601 else if (!strcasecmp(el, "QRmtime")) qrbuf.QRmtime = atol(ChrPtr(migr_chardata));
602 else if (!strcasecmp(el, "QRexpire_mode")) qrbuf.QRep.expire_mode = atoi(ChrPtr(migr_chardata));
603 else if (!strcasecmp(el, "QRexpire_value")) qrbuf.QRep.expire_value = atoi(ChrPtr(migr_chardata));
604 else if (!strcasecmp(el, "QRnumber")) qrbuf.QRnumber = atol(ChrPtr(migr_chardata));
605 else if (!strcasecmp(el, "QRorder")) qrbuf.QRorder = atoi(ChrPtr(migr_chardata));
606 else if (!strcasecmp(el, "QRflags2")) qrbuf.QRflags2 = atoi(ChrPtr(migr_chardata));
607 else if (!strcasecmp(el, "QRdefaultview")) qrbuf.QRdefaultview = atoi(ChrPtr(migr_chardata));
608 else if (!strcasecmp(el, "msgnum_info")) qrbuf.msgnum_info = atol(ChrPtr(migr_chardata));
609 else if (!strcasecmp(el, "msgnum_pic")) qrbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
615 int migr_floorrecord(void *data, const char *el) {
616 if (!strcasecmp(el, "f_num")) floornum = atoi(ChrPtr(migr_chardata));
617 else if (!strcasecmp(el, "f_flags")) flbuf.f_flags = atoi(ChrPtr(migr_chardata));
618 else if (!strcasecmp(el, "f_name")) safestrncpy(flbuf.f_name, ChrPtr(migr_chardata), sizeof flbuf.f_name);
619 else if (!strcasecmp(el, "f_ref_count")) flbuf.f_ref_count = atoi(ChrPtr(migr_chardata));
620 else if (!strcasecmp(el, "f_ep_expire_mode")) flbuf.f_ep.expire_mode = atoi(ChrPtr(migr_chardata));
621 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) {
628 if (!strcasecmp(el, "v_roomnum")) vbuf.v_roomnum = atol(ChrPtr(migr_chardata));
629 else if (!strcasecmp(el, "v_roomgen")) vbuf.v_roomgen = atol(ChrPtr(migr_chardata));
630 else if (!strcasecmp(el, "v_usernum")) vbuf.v_usernum = atol(ChrPtr(migr_chardata));
632 else if (!strcasecmp(el, "v_seen")) {
633 int is_textual_seen = 0;
635 int max = StrLength(migr_chardata);
637 vbuf.v_lastseen = atol(ChrPtr(migr_chardata));
639 for (i=0; i < max; ++i)
640 if (!isdigit(ChrPtr(migr_chardata)[i]))
643 safestrncpy(vbuf.v_seen, ChrPtr(migr_chardata), sizeof vbuf.v_seen);
646 else if (!strcasecmp(el, "v_answered")) safestrncpy(vbuf.v_answered, ChrPtr(migr_chardata), sizeof vbuf.v_answered);
647 else if (!strcasecmp(el, "v_flags")) vbuf.v_flags = atoi(ChrPtr(migr_chardata));
648 else if (!strcasecmp(el, "v_view")) vbuf.v_view = atoi(ChrPtr(migr_chardata));
654 void migr_xml_end(void *data, const char *el) {
658 long *msglist = NULL;
659 int msglist_alloc = 0;
660 // *** GENERAL STUFF ***
662 if (!strcasecmp(el, "citadel_migrate_data")) {
663 --citadel_migrate_data;
667 if (citadel_migrate_data != 1) {
668 syslog(LOG_ERR, "migrate: out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
672 // syslog(LOG_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
676 if (!strcasecmp(el, "config")) {
677 syslog(LOG_DEBUG, "migrate: imported config key=%s", ikey);
680 CtdlSetConfigStr(ikey, (char *)ChrPtr(migr_chardata));
685 syslog(LOG_INFO, "migrate: closed a <config> tag but no key name was supplied.");
690 else if ((!strncasecmp(el, HKEY("u_"))) &&
691 migr_userrecord(data, el))
692 ; /* Nothing to do anymore */
693 else if (!strcasecmp(el, "user")) {
695 syslog(LOG_INFO, "migrate: imported user: %s", usbuf.fullname);
700 else if (!strcasecmp(el, "oid_url")) safestrncpy(openid_url, ChrPtr(migr_chardata), sizeof openid_url);
701 else if (!strcasecmp(el, "oid_usernum")) openid_usernum = atol(ChrPtr(migr_chardata));
703 else if (!strcasecmp(el, "openid")) { // see serv_openid_rp.c for a description of the record format
706 oid_data_len = sizeof(long) + strlen(openid_url) + 1;
707 oid_data = malloc(oid_data_len);
708 memcpy(oid_data, &openid_usernum, sizeof(long));
709 memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
710 cdb_store(CDB_EXTAUTH, openid_url, strlen(openid_url), oid_data, oid_data_len);
712 syslog(LOG_INFO, "migrate: imported OpenID: %s (%ld)", openid_url, openid_usernum);
716 else if ((!strncasecmp(el, HKEY("QR"))) &&
717 migr_roomrecord(data, el))
718 ; // Nothing to do anymore
719 else if (!strcasecmp(el, "room")) {
721 syslog(LOG_INFO, "migrate: imported room: %s", qrbuf.QRname);
724 // *** ROOM MESSAGE POINTERS ***
726 else if (!strcasecmp(el, "FRname")) {
727 safestrncpy(FRname, ChrPtr(migr_chardata), sizeof FRname);
730 else if (!strcasecmp(el, "FRmsglist")) {
731 if (!IsEmptyStr(FRname)) {
733 msglist_alloc = 1000;
734 msglist = malloc(sizeof(long) * msglist_alloc);
736 syslog(LOG_DEBUG, "migrate: message list for: %s", FRname);
738 ptr = ChrPtr(migr_chardata);
740 while ((*ptr != 0) && (!isdigit(*ptr))) {
743 if ((*ptr != 0) && (isdigit(*ptr))) {
746 if (msgcount >= msglist_alloc) {
748 msglist = realloc(msglist, sizeof(long) * msglist_alloc);
750 msglist[msgcount++] = msgnum;
753 while ((*ptr != 0) && (isdigit(*ptr))) {
759 CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL, 1);
764 syslog(LOG_DEBUG, "migrate: imported %d messages.", msgcount);
765 if (server_shutting_down) {
771 else if ((!strncasecmp(el, HKEY("f_"))) &&
772 migr_floorrecord(data, el))
773 ; // Nothing to do anymore
775 else if (!strcasecmp(el, "floor")) {
776 CtdlPutFloor(&flbuf, floornum);
777 syslog(LOG_INFO, "migrate: imported floor #%d (%s)", floornum, flbuf.f_name);
781 else if ((!strncasecmp(el, HKEY("v_"))) && migr_visitrecord(data, el)) {
782 ; // Nothing to do anymore
784 else if (!strcasecmp(el, "visit")) {
786 syslog(LOG_INFO, "migrate: imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
791 else if (!strcasecmp(el, "msg_msgnum")) smi.meta_msgnum = import_msgnum = atol(ChrPtr(migr_chardata));
792 else if (!strcasecmp(el, "msg_meta_refcount")) smi.meta_refcount = atoi(ChrPtr(migr_chardata));
793 else if (!strcasecmp(el, "msg_meta_rfc822_length")) smi.meta_rfc822_length = atoi(ChrPtr(migr_chardata));
794 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_text")) {
798 struct CtdlMessage *msg;
800 FlushStrBuf(migr_MsgData);
801 StrBufDecodeBase64To(migr_chardata, migr_MsgData);
803 msg = CtdlDeserializeMessage(import_msgnum, -1,
804 ChrPtr(migr_MsgData),
805 StrLength(migr_MsgData));
807 rc = CtdlSaveThisMessage(msg, import_msgnum, 0);
818 "migrate: %s message #%ld, size=%d, ref=%d, body=%ld, content-type: %s",
819 (rc!= 0)?"failed to import":"imported",
821 StrLength(migr_MsgData),
823 smi.meta_rfc822_length,
824 smi.meta_content_type
826 memset(&smi, 0, sizeof(smi));
829 // *** MORE GENERAL STUFF ***
831 FlushStrBuf(migr_chardata);
835 // Import begins here
836 void migr_do_import(void) {
841 migr_chardata = NewStrBufPlain(NULL, SIZ * 20);
842 migr_MsgData = NewStrBufPlain(NULL, SIZ * 20);
843 xp = XML_ParserCreate(NULL);
845 cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
848 XML_SetElementHandler(xp, migr_xml_start, migr_xml_end);
849 XML_SetCharacterDataHandler(xp, migr_xml_chardata);
853 cprintf("%d sock it to me\n", SEND_LISTING);
856 client_set_inbound_buf(SIZ * 10);
858 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
859 XML_Parse(xp, buf, strlen(buf), 0);
860 if (server_shutting_down)
861 break; // Should we break or return?
864 XML_Parse(xp, "", 0, 1);
866 FreeStrBuf(&migr_chardata);
867 FreeStrBuf(&migr_MsgData);
868 rebuild_euid_index();
869 rebuild_usersbynumber();
870 CtdlSetConfigInt("MM_fulltext_wordbreaker", -1); // Set an invalid wordbreaker to force re-indexing
875 // ******************************************************************************
876 // * Dispatcher, Common code *
877 // ******************************************************************************
879 // Dump out the pathnames of directories which can be copied "as is"
880 void migr_do_listdirs(void) {
881 cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
882 cprintf("files|%s\n", ctdl_file_dir);
883 cprintf("messages|%s\n", ctdl_message_dir);
884 cprintf("keys|%s\n", ctdl_key_dir);
889 // ******************************************************************************
890 // * Repair database integrity *
891 // ******************************************************************************
893 StrBuf *PlainMessageBuf = NULL;
894 HashList *UsedMessageIDS = NULL;
896 int migr_restore_message_metadata(long msgnum, int refcount) {
898 struct CtdlMessage *msg;
901 // We can use a static buffer here because there will never be more than
902 // one of this operation happening at any given time, and it's really best
903 // to just keep it allocated once instead of torturing malloc/free.
904 // Call this function with msgnum "-1" to free the buffer when finished.
905 static int encoded_alloc = 0;
906 static char *encoded_msg = NULL;
909 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
913 // todo FreeStrBuf(&PlainMessageBuf); PlainMessageBuf = NULL;
918 if (PlainMessageBuf == NULL) {
919 PlainMessageBuf = NewStrBufPlain(NULL, 10*SIZ);
922 // Ok, here we go ...
924 msg = CtdlFetchMessage(msgnum, 1);
929 GetMetaData(&smi, msgnum);
930 smi.meta_msgnum = msgnum;
931 smi.meta_refcount = refcount;
933 // restore the content type from the message body:
934 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
937 safestrncpy(smi.meta_content_type, &mptr[13], sizeof smi.meta_content_type);
938 striplt(smi.meta_content_type);
939 aptr = smi.meta_content_type;
940 while (!IsEmptyStr(aptr)) {
945 memset(aptr, 0, sizeof(smi.meta_content_type) - (aptr - smi.meta_content_type));
951 CC->redirect_buffer = PlainMessageBuf;
952 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
953 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
954 CC->redirect_buffer = NULL;
957 "migrate: setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s",
960 smi.meta_rfc822_length,
961 smi.meta_content_type
970 void migr_check_room_msg(long msgnum, void *userdata) {
971 fprintf(migr_global_message_list, "%ld %s\n", msgnum, CC->room.QRname);
975 void migr_check_rooms_backend(struct ctdlroom *buf, void *data) {
976 // message list goes inside this tag
977 CtdlGetRoom(&CC->room, buf->QRname);
978 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_check_room_msg, NULL);
982 void RemoveMessagesFromRooms(StrBuf *RoomNameVec, long msgnum) {
984 const char *Pos = NULL;
985 StrBuf *oneRoom = NewStrBuf();
987 syslog(LOG_INFO, "migrate: removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
989 while (Pos != StrBufNOTNULL){
990 StrBufExtract_NextToken(oneRoom, RoomNameVec, &Pos, '|');
991 CtdlDeleteMessages(ChrPtr(oneRoom), &msgnum, 1, "");
993 GetMetaData(&smi, msgnum);
994 AdjRefCount(msgnum, -smi.meta_refcount);
998 void migr_do_restore_meta(void) {
1009 migr_global_message_list = fopen(migr_tempfilename1, "w");
1010 if (migr_global_message_list != NULL) {
1011 CtdlForEachRoom(migr_check_rooms_backend, NULL);
1012 fclose(migr_global_message_list);
1015 // Process the 'global' message list. (Sort it and remove dups.
1016 // Dups are ok because a message may be in more than one room, but
1017 // this will be handled by exporting the reference count, not by
1018 // exporting the message multiple times.)
1019 snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
1020 if (system(cmd) != 0) {
1021 syslog(LOG_ERR, "migrate: error %d", errno);
1024 RoomNames = NewStrBuf();
1026 migr_global_message_list = fopen(migr_tempfilename2, "r");
1027 if (migr_global_message_list != NULL) {
1028 syslog(LOG_INFO, "migrate: opened %s", migr_tempfilename1);
1029 while ((Ctx->kill_me == 0) &&
1030 (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
1037 prn = strchr(buf, ' ');
1038 if (lastnum != msgnum) {
1039 failGetMessage = migr_restore_message_metadata(lastnum, refcount);
1040 if (failGetMessage) {
1041 RemoveMessagesFromRooms(RoomNames, lastnum);
1046 StrBufPlain(RoomNames, prn + 1, -1);
1048 StrBufTrim(RoomNames);
1052 if (StrLength(RoomNames) > 0) {
1053 StrBufAppendBufPlain(RoomNames, HKEY("|"), 0);
1055 StrBufAppendBufPlain(RoomNames, prn, -1, 1);
1056 StrBufTrim(RoomNames);
1062 failGetMessage = migr_restore_message_metadata(msgnum, refcount);
1063 if (failGetMessage) {
1064 RemoveMessagesFromRooms(RoomNames, lastnum);
1066 fclose(migr_global_message_list);
1069 migr_restore_message_metadata(-1L, -1); // This frees the encoding buffer
1070 cprintf("%d system analysis completed", CIT_OK);
1075 // ******************************************************************************
1076 // * Dispatcher, Common code *
1077 // ******************************************************************************
1078 void cmd_migr(char *cmdbuf) {
1081 if (CtdlAccessCheck(ac_aide)) return;
1083 if (CtdlTrySingleUser()) {
1084 CtdlDisableHouseKeeping();
1085 CtdlMakeTempFileName(migr_tempfilename1, sizeof migr_tempfilename1);
1086 CtdlMakeTempFileName(migr_tempfilename2, sizeof migr_tempfilename2);
1088 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
1089 if (!strcasecmp(cmd, "export")) {
1092 else if (!strcasecmp(cmd, "import")) {
1095 else if (!strcasecmp(cmd, "listdirs")) {
1098 else if (!strcasecmp(cmd, "restoremeta")) {
1099 migr_do_restore_meta();
1102 cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
1105 unlink(migr_tempfilename1);
1106 unlink(migr_tempfilename2);
1108 CtdlEnableHouseKeeping();
1109 CtdlEndSingleUser();
1112 cprintf("%d The migrator is already running.\n", ERROR + RESOURCE_BUSY);
1117 // Initialization function, called from modules_init.c
1118 char *ctdl_module_init_migrate() {
1120 CtdlRegisterProtoHook(cmd_migr, "MIGR", "Across-the-wire migration");
1123 // return our module name for the log