stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / migrate / serv_migrate.c
1 /*
2  * This module dumps and/or loads the Citadel database in XML format.
3  *
4  * Copyright (c) 1987-2020 by the citadel.org team
5  *
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.
8  *
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.
13  */
14
15 /*
16  * Explanation of <progress> tags:
17  *
18  * 0%              nothing
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
26  */
27
28 #include "sysdep.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <pwd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <sys/wait.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <limits.h>
42 #include <expat.h>
43 #include <libcitadel.h>
44 #include "citadel.h"
45 #include "server.h"
46 #include "citserver.h"
47 #include "support.h"
48 #include "config.h"
49 #include "database.h"
50 #include "msgbase.h"
51 #include "user_ops.h"
52 #include "euidindex.h"
53 #include "ctdl_module.h"
54
55 #define END_OF_MESSAGE  "---eom---dbd---"
56
57 char migr_tempfilename1[PATH_MAX];
58 char migr_tempfilename2[PATH_MAX];
59 FILE *migr_global_message_list;
60 int total_msgs = 0;
61 char *ikey = NULL;                      // If we're importing a config key we store it here.
62
63
64 /******************************************************************************
65  *        Code which implements the export appears in this section            *
66  ******************************************************************************/
67
68 /*
69  * Output a string to the client with these characters escaped:  & < > " '
70  */
71 void xml_strout(char *str) {
72
73         char *c = str;
74
75         if (str == NULL) {
76                 return;
77         }
78
79         while (*c != 0) {
80                 if (*c == '\"') {
81                         client_write(HKEY("&quot;"));
82                 }
83                 else if (*c == '\'') {
84                         client_write(HKEY("&apos;"));
85                 }
86                 else if (*c == '<') {
87                         client_write(HKEY("&lt;"));
88                 }
89                 else if (*c == '>') {
90                         client_write(HKEY("&gt;"));
91                 }
92                 else if (*c == '&') {
93                         client_write(HKEY("&amp;"));
94                 }
95                 else {
96                         client_write(c, 1);
97                 }
98                 ++c;
99         }
100 }
101
102
103 /*
104  * Export a user record as XML
105  */
106 void migr_export_users_backend(char *username, void *data) {
107
108         struct ctdluser u;
109         if (CtdlGetUser(&u, username) != 0) {
110                 return;
111         }
112
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"));
131 }
132
133
134 void migr_export_users(void) {
135         ForEachUser(migr_export_users_backend, NULL);
136 }
137
138
139 void migr_export_room_msg(long msgnum, void *userdata) {
140         cprintf("%ld\n", msgnum);
141         fprintf(migr_global_message_list, "%ld\n", msgnum);
142 }
143
144
145 void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
146         client_write(HKEY("<room>\n"));
147         client_write(HKEY("<QRname>")); xml_strout(buf->QRname);        client_write(HKEY("</QRname>\n"));
148         client_write(HKEY("<QRpasswd>"));       xml_strout(buf->QRpasswd);      client_write(HKEY("</QRpasswd>\n"));
149         cprintf("<QRroomaide>%ld</QRroomaide>\n", buf->QRroomaide);
150         cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
151         cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
152         cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
153         if (buf->QRflags & QR_DIRECTORY) {
154                 client_write(HKEY("<QRdirname>"));
155                 xml_strout(buf->QRdirname);
156                 client_write(HKEY("</QRdirname>\n"));
157         }
158         cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
159         cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
160         cprintf("<QRexpire_mode>%d</QRexpire_mode>\n", buf->QRep.expire_mode);
161         cprintf("<QRexpire_value>%d</QRexpire_value>\n", buf->QRep.expire_value);
162         cprintf("<QRnumber>%ld</QRnumber>\n", buf->QRnumber);
163         cprintf("<QRorder>%d</QRorder>\n", buf->QRorder);
164         cprintf("<QRflags2>%u</QRflags2>\n", buf->QRflags2);
165         cprintf("<QRdefaultview>%d</QRdefaultview>\n", buf->QRdefaultview);
166         cprintf("<msgnum_info>%ld</msgnum_info>\n", buf->msgnum_info);
167         cprintf("<msgnum_pic>%ld</msgnum_pic>\n", buf->msgnum_pic);
168         client_write(HKEY("</room>\n"));
169
170         /* message list goes inside this tag */
171
172         CtdlGetRoom(&CC->room, buf->QRname);
173         client_write(HKEY("<room_messages>"));
174         client_write(HKEY("<FRname>")); xml_strout(CC->room.QRname);    client_write(HKEY("</FRname>\n"));
175         client_write(HKEY("<FRmsglist>"));
176         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_export_room_msg, NULL);
177         client_write(HKEY("</FRmsglist>\n"));
178         client_write(HKEY("</room_messages>\n"));
179
180
181 }
182
183
184 void migr_export_rooms(void) {
185         char cmd[SIZ];
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);
190         }
191
192         /*
193          * Process the 'global' message list.  (Sort it and remove dups.
194          * Dups are ok because a message may be in more than one room, but
195          * this will be handled by exporting the reference count, not by
196          * exporting the message multiple times.)
197          */
198         snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
199         if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
200         snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
201         if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
202
203
204         snprintf(cmd, sizeof cmd, "wc -l %s", migr_tempfilename1);
205         FILE *fp = popen(cmd, "r");
206         if (fp) {
207                 fgets(cmd, sizeof cmd, fp);
208                 pclose(fp);
209                 total_msgs = atoi(cmd);
210         }
211         else {
212                 total_msgs = 1; // any nonzero just to keep it from barfing
213         }
214         syslog(LOG_DEBUG, "Total messages to be exported: %d", total_msgs);
215 }
216
217
218 void migr_export_floors(void) {
219         struct floor qfbuf, *buf;
220         int i;
221
222         for (i=0; i < MAXFLOORS; ++i) {
223                 client_write(HKEY("<floor>\n"));
224                 cprintf("<f_num>%d</f_num>\n", i);
225                 CtdlGetFloor(&qfbuf, i);
226                 buf = &qfbuf;
227                 cprintf("<f_flags>%u</f_flags>\n", buf->f_flags);
228                 client_write(HKEY("<f_name>")); xml_strout(buf->f_name); client_write(HKEY("</f_name>\n"));
229                 cprintf("<f_ref_count>%d</f_ref_count>\n", buf->f_ref_count);
230                 cprintf("<f_ep_expire_mode>%d</f_ep_expire_mode>\n", buf->f_ep.expire_mode);
231                 cprintf("<f_ep_expire_value>%d</f_ep_expire_value>\n", buf->f_ep.expire_value);
232                 client_write(HKEY("</floor>\n"));
233         }
234 }
235
236
237 /*
238  * Return nonzero if the supplied string contains only characters which are valid in a sequence set.
239  */
240 int is_sequence_set(char *s) {
241         if (!s) return(0);
242
243         char *c = s;
244         char ch;
245         while (ch = *c++, ch) {
246                 if (!strchr("0123456789*,:", ch)) {
247                         return(0);
248                 }
249         }
250         return(1);
251 }
252
253
254 /* 
255  *  Traverse the visits file...
256  */
257 void migr_export_visits(void) {
258         visit vbuf;
259         struct cdbdata *cdbv;
260
261         cdb_rewind(CDB_VISIT);
262
263         while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
264                 memset(&vbuf, 0, sizeof(visit));
265                 memcpy(&vbuf, cdbv->ptr,
266                        ((cdbv->len > sizeof(visit)) ?
267                         sizeof(visit) : cdbv->len));
268                 cdb_free(cdbv);
269
270                 client_write(HKEY("<visit>\n"));
271                 cprintf("<v_roomnum>%ld</v_roomnum>\n", vbuf.v_roomnum);
272                 cprintf("<v_roomgen>%ld</v_roomgen>\n", vbuf.v_roomgen);
273                 cprintf("<v_usernum>%ld</v_usernum>\n", vbuf.v_usernum);
274
275                 client_write(HKEY("<v_seen>"));
276                 if ( (!IsEmptyStr(vbuf.v_seen)) && (is_sequence_set(vbuf.v_seen)) ) {
277                         xml_strout(vbuf.v_seen);
278                 }
279                 else {
280                         cprintf("%ld", vbuf.v_lastseen);
281                 }
282                 client_write(HKEY("</v_seen>"));
283
284                 if ( (!IsEmptyStr(vbuf.v_answered)) && (is_sequence_set(vbuf.v_answered)) ) {
285                         client_write(HKEY("<v_answered>"));
286                         xml_strout(vbuf.v_answered);
287                         client_write(HKEY("</v_answered>\n"));
288                 }
289
290                 cprintf("<v_flags>%u</v_flags>\n", vbuf.v_flags);
291                 cprintf("<v_view>%d</v_view>\n", vbuf.v_view);
292                 client_write(HKEY("</visit>\n"));
293         }
294 }
295
296
297 void migr_export_message(long msgnum) {
298         struct MetaData smi;
299         struct CtdlMessage *msg;
300         struct ser_ret smr;
301
302         /* We can use a static buffer here because there will never be more than
303          * one of this operation happening at any given time, and it's really best
304          * to just keep it allocated once instead of torturing malloc/free.
305          * Call this function with msgnum "-1" to free the buffer when finished.
306          */
307         static int encoded_alloc = 0;
308         static char *encoded_msg = NULL;
309
310         if (msgnum < 0) {
311                 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
312                         free(encoded_msg);
313                         encoded_alloc = 0;
314                         encoded_msg = NULL;
315                 }
316                 return;
317         }
318
319         /* Ok, here we go ... */
320
321         msg = CtdlFetchMessage(msgnum, 1);
322         if (msg == NULL) return;        /* fail silently */
323
324         client_write(HKEY("<message>\n"));
325         GetMetaData(&smi, msgnum);
326         cprintf("<msg_msgnum>%ld</msg_msgnum>\n", msgnum);
327         cprintf("<msg_meta_refcount>%d</msg_meta_refcount>\n", smi.meta_refcount);
328         cprintf("<msg_meta_rfc822_length>%ld</msg_meta_rfc822_length>\n", smi.meta_rfc822_length);
329         client_write(HKEY("<msg_meta_content_type>")); xml_strout(smi.meta_content_type); client_write(HKEY("</msg_meta_content_type>\n"));
330
331         client_write(HKEY("<msg_text>"));
332         CtdlSerializeMessage(&smr, msg);
333         CM_Free(msg);
334
335         /* Predict the buffer size we need.  Expand the buffer if necessary. */
336         int encoded_len = smr.len * 15 / 10 ;
337         if (encoded_len > encoded_alloc) {
338                 encoded_alloc = encoded_len;
339                 encoded_msg = realloc(encoded_msg, encoded_alloc);
340         }
341
342         if (encoded_msg == NULL) {
343                 /* Questionable hack that hopes it'll work next time and we only lose one message */
344                 encoded_alloc = 0;
345         }
346         else {
347                 /* Once we do the encoding we know the exact size */
348                 encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
349                 client_write(encoded_msg, encoded_len);
350         }
351
352         free(smr.ser);
353
354         client_write(HKEY("</msg_text>\n"));
355         client_write(HKEY("</message>\n"));
356 }
357
358
359 void migr_export_openids(void) {
360         struct cdbdata *cdboi;
361         long usernum;
362         char url[512];
363
364         cdb_rewind(CDB_EXTAUTH);
365         while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
366                 if (cdboi->len > sizeof(long)) {
367                         client_write(HKEY("<openid>\n"));
368                         memcpy(&usernum, cdboi->ptr, sizeof(long));
369                         snprintf(url, sizeof url, "%s", (cdboi->ptr)+sizeof(long) );
370                         client_write(HKEY("<oid_url>"));
371                         xml_strout(url);
372                         client_write(HKEY("</oid_url>\n"));
373                         cprintf("<oid_usernum>%ld</oid_usernum>\n", usernum);
374                         client_write(HKEY("</openid>\n"));
375                 }
376                 cdb_free(cdboi);
377         }
378 }
379
380
381 void migr_export_configs(void) {
382         struct cdbdata *cdbcfg;
383         int keylen = 0;
384         char *key = NULL;
385         char *value = NULL;
386
387         cdb_rewind(CDB_CONFIG);
388         while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
389
390                 keylen = strlen(cdbcfg->ptr);
391                 key = cdbcfg->ptr;
392                 value = cdbcfg->ptr + keylen + 1;
393
394                 client_write("<config key=\"", 13);
395                 xml_strout(key);
396                 client_write("\">", 2);
397                 xml_strout(value);
398                 client_write("</config>\n", 10);
399                 cdb_free(cdbcfg);
400         }
401 }
402
403
404 void migr_export_messages(void) {
405         char buf[SIZ];
406         long msgnum;
407         int count = 0;
408         int progress = 0;
409         int prev_progress = 0;
410         CitContext *Ctx;
411
412         Ctx = CC;
413         migr_global_message_list = fopen(migr_tempfilename1, "r");
414         if (migr_global_message_list != NULL) {
415                 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
416                 while ((Ctx->kill_me == 0) && 
417                        (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
418                         msgnum = atol(buf);
419                         if (msgnum > 0L) {
420                                 migr_export_message(msgnum);
421                                 ++count;
422                         }
423                         progress = (count * 74 / total_msgs) + 25 ;
424                         if ((progress > prev_progress) && (progress < 100)) {
425                                 cprintf("<progress>%d</progress>\n", progress);
426                         }
427                         prev_progress = progress;
428                 }
429                 fclose(migr_global_message_list);
430         }
431         if (Ctx->kill_me == 0) {
432                 syslog(LOG_INFO, "Exported %d messages.", count);
433         }
434         else {
435                 syslog(LOG_ERR, "Export aborted due to client disconnect!");
436         }
437
438         migr_export_message(-1L);       /* This frees the encoding buffer */
439 }
440
441
442 void migr_do_export(void) {
443         CitContext *Ctx;
444
445         Ctx = CC;
446         cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
447         Ctx->dont_term = 1;
448
449         client_write(HKEY("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"));
450         client_write(HKEY("<citadel_migrate_data>\n"));
451         cprintf("<version>%d</version>\n", REV_LEVEL);
452         cprintf("<progress>%d</progress>\n", 0);
453
454         /* export the configuration database */
455         migr_export_configs();
456         cprintf("<progress>%d</progress>\n", 2);
457         
458         if (Ctx->kill_me == 0)  migr_export_users();
459         cprintf("<progress>%d</progress>\n", 7);
460         if (Ctx->kill_me == 0)  migr_export_openids();
461         cprintf("<progress>%d</progress>\n", 12);
462         if (Ctx->kill_me == 0)  migr_export_rooms();
463         cprintf("<progress>%d</progress>\n", 17);
464         if (Ctx->kill_me == 0)  migr_export_floors();
465         cprintf("<progress>%d</progress>\n", 18);
466         if (Ctx->kill_me == 0)  migr_export_visits();
467         cprintf("<progress>%d</progress>\n", 25);
468         if (Ctx->kill_me == 0)  migr_export_messages();
469         client_write(HKEY("</citadel_migrate_data>\n"));
470         cprintf("<progress>%d</progress>\n", 100);
471         client_write(HKEY("000\n"));
472         Ctx->dont_term = 0;
473 }
474
475
476 /******************************************************************************
477  *                              Import code                                   *
478  *    Here's the code that implements the import side.  It's going to end up  *
479  *        being one big loop with lots of global variables.  I don't care.    *
480  * You wouldn't run multiple concurrent imports anyway.  If this offends your *
481  * delicate sensibilities  then go rewrite it in Ruby on Rails or something.  *
482  ******************************************************************************/
483
484
485 int citadel_migrate_data = 0;           /* Are we inside a <citadel_migrate_data> tag pair? */
486 StrBuf *migr_chardata = NULL;
487 StrBuf *migr_MsgData = NULL;
488 struct ctdluser usbuf;
489 struct ctdlroom qrbuf;
490 char openid_url[512];
491 long openid_usernum = 0;
492 char FRname[ROOMNAMELEN];
493 struct floor flbuf;
494 int floornum = 0;
495 visit vbuf;
496 struct MetaData smi;
497 long import_msgnum = 0;
498
499 /*
500  * This callback stores up the data which appears in between tags.
501  */
502 void migr_xml_chardata(void *data, const XML_Char *s, int len)
503 {
504         StrBufAppendBufPlain(migr_chardata, s, len, 0);
505 }
506
507
508 void migr_xml_start(void *data, const char *el, const char **attr) {
509         int i;
510
511         /*** GENERAL STUFF ***/
512
513         /* Reset chardata_len to zero and init buffer */
514         FlushStrBuf(migr_chardata);
515         FlushStrBuf(migr_MsgData);
516
517         if (!strcasecmp(el, "citadel_migrate_data")) {
518                 ++citadel_migrate_data;
519
520                 /* As soon as it looks like the input data is a genuine Citadel XML export,
521                  * whack the existing database on disk to make room for the new one.
522                  */
523                 if (citadel_migrate_data == 1) {
524                         for (i = 0; i < MAXCDB; ++i) {
525                                 cdb_trunc(i);
526                         }
527                 }
528                 return;
529         }
530
531         if (citadel_migrate_data != 1) {
532                 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!", el);
533                 return;
534         }
535
536         /* When we begin receiving XML for one of these record types, clear out the associated
537          * buffer so we don't accidentally carry over any data from a previous record.
538          */
539         if (!strcasecmp(el, "user"))                    memset(&usbuf, 0, sizeof (struct ctdluser));
540         else if (!strcasecmp(el, "openid"))             memset(openid_url, 0, sizeof openid_url);
541         else if (!strcasecmp(el, "room"))               memset(&qrbuf, 0, sizeof (struct ctdlroom));
542         else if (!strcasecmp(el, "room_messages"))      memset(FRname, 0, sizeof FRname);
543         else if (!strcasecmp(el, "floor"))              memset(&flbuf, 0, sizeof (struct floor));
544         else if (!strcasecmp(el, "visit"))              memset(&vbuf, 0, sizeof (visit));
545
546         else if (!strcasecmp(el, "message")) {
547                 memset(&smi, 0, sizeof (struct MetaData));
548                 import_msgnum = 0;
549         }
550         else if (!strcasecmp(el, "config")) {
551                 if (ikey != NULL) {
552                         free(ikey);
553                         ikey = NULL;
554                 }
555                 while (*attr) {
556                         if (!strcasecmp(attr[0], "key")) {
557                                 ikey = strdup(attr[1]);
558                         }
559                         attr += 2;
560                 }
561         }
562
563 }
564
565
566 int migr_userrecord(void *data, const char *el)
567 {
568         if (!strcasecmp(el, "u_version"))                       usbuf.version = atoi(ChrPtr(migr_chardata));
569         else if (!strcasecmp(el, "u_uid"))                      usbuf.uid = atol(ChrPtr(migr_chardata));
570         else if (!strcasecmp(el, "u_password"))                 safestrncpy(usbuf.password, ChrPtr(migr_chardata), sizeof usbuf.password);
571         else if (!strcasecmp(el, "u_flags"))                    usbuf.flags = atoi(ChrPtr(migr_chardata));
572         else if (!strcasecmp(el, "u_timescalled"))              usbuf.timescalled = atol(ChrPtr(migr_chardata));
573         else if (!strcasecmp(el, "u_posted"))                   usbuf.posted = atol(ChrPtr(migr_chardata));
574         else if (!strcasecmp(el, "u_axlevel"))                  usbuf.axlevel = atoi(ChrPtr(migr_chardata));
575         else if (!strcasecmp(el, "u_usernum"))                  usbuf.usernum = atol(ChrPtr(migr_chardata));
576         else if (!strcasecmp(el, "u_lastcall"))                 usbuf.lastcall = atol(ChrPtr(migr_chardata));
577         else if (!strcasecmp(el, "u_USuserpurge"))              usbuf.USuserpurge = atoi(ChrPtr(migr_chardata));
578         else if (!strcasecmp(el, "u_fullname"))                 safestrncpy(usbuf.fullname, ChrPtr(migr_chardata), sizeof usbuf.fullname);
579         else if (!strcasecmp(el, "u_msgnum_bio"))               usbuf.msgnum_bio = atol(ChrPtr(migr_chardata));
580         else if (!strcasecmp(el, "u_msgnum_pic"))               usbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
581         else if (!strcasecmp(el, "u_emailaddrs"))               safestrncpy(usbuf.emailaddrs, ChrPtr(migr_chardata), sizeof usbuf.emailaddrs);
582         else if (!strcasecmp(el, "u_msgnum_inboxrules"))        usbuf.msgnum_inboxrules = atol(ChrPtr(migr_chardata));
583         else if (!strcasecmp(el, "u_lastproc_inboxrules"))      usbuf.lastproc_inboxrules = atol(ChrPtr(migr_chardata));
584         else return 0;
585         return 1;
586 }
587
588
589 int migr_roomrecord(void *data, const char *el)
590 {
591         if (!strcasecmp(el, "QRname"))                          safestrncpy(qrbuf.QRname, ChrPtr(migr_chardata), sizeof qrbuf.QRname);
592         else if (!strcasecmp(el, "QRpasswd"))                   safestrncpy(qrbuf.QRpasswd, ChrPtr(migr_chardata), sizeof qrbuf.QRpasswd);
593         else if (!strcasecmp(el, "QRroomaide"))                 qrbuf.QRroomaide = atol(ChrPtr(migr_chardata));
594         else if (!strcasecmp(el, "QRhighest"))                  qrbuf.QRhighest = atol(ChrPtr(migr_chardata));
595         else if (!strcasecmp(el, "QRgen"))                      qrbuf.QRgen = atol(ChrPtr(migr_chardata));
596         else if (!strcasecmp(el, "QRflags"))                    qrbuf.QRflags = atoi(ChrPtr(migr_chardata));
597         else if (!strcasecmp(el, "QRdirname"))                  safestrncpy(qrbuf.QRdirname, ChrPtr(migr_chardata), sizeof qrbuf.QRdirname);
598         else if (!strcasecmp(el, "QRfloor"))                    qrbuf.QRfloor = atoi(ChrPtr(migr_chardata));
599         else if (!strcasecmp(el, "QRmtime"))                    qrbuf.QRmtime = atol(ChrPtr(migr_chardata));
600         else if (!strcasecmp(el, "QRexpire_mode"))              qrbuf.QRep.expire_mode = atoi(ChrPtr(migr_chardata));
601         else if (!strcasecmp(el, "QRexpire_value"))             qrbuf.QRep.expire_value = atoi(ChrPtr(migr_chardata));
602         else if (!strcasecmp(el, "QRnumber"))                   qrbuf.QRnumber = atol(ChrPtr(migr_chardata));
603         else if (!strcasecmp(el, "QRorder"))                    qrbuf.QRorder = atoi(ChrPtr(migr_chardata));
604         else if (!strcasecmp(el, "QRflags2"))                   qrbuf.QRflags2 = atoi(ChrPtr(migr_chardata));
605         else if (!strcasecmp(el, "QRdefaultview"))              qrbuf.QRdefaultview = atoi(ChrPtr(migr_chardata));
606         else if (!strcasecmp(el, "msgnum_info"))                qrbuf.msgnum_info = atol(ChrPtr(migr_chardata));
607         else if (!strcasecmp(el, "msgnum_pic"))                 qrbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
608         else return 0;
609         return 1;
610 }
611
612
613 int migr_floorrecord(void *data, const char *el)
614 {
615         if (!strcasecmp(el, "f_num"))                           floornum = atoi(ChrPtr(migr_chardata));
616         else if (!strcasecmp(el, "f_flags"))                    flbuf.f_flags = atoi(ChrPtr(migr_chardata));
617         else if (!strcasecmp(el, "f_name"))                     safestrncpy(flbuf.f_name, ChrPtr(migr_chardata), sizeof flbuf.f_name);
618         else if (!strcasecmp(el, "f_ref_count"))                flbuf.f_ref_count = atoi(ChrPtr(migr_chardata));
619         else if (!strcasecmp(el, "f_ep_expire_mode"))           flbuf.f_ep.expire_mode = atoi(ChrPtr(migr_chardata));
620         else if (!strcasecmp(el, "f_ep_expire_value"))          flbuf.f_ep.expire_value = atoi(ChrPtr(migr_chardata));
621         else return 0;
622         return 1;
623 }
624
625
626 int migr_visitrecord(void *data, const char *el)
627 {
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));
631
632         else if (!strcasecmp(el, "v_seen")) {
633                 int is_textual_seen = 0;
634                 int i;
635                 int max = StrLength(migr_chardata);
636
637                 vbuf.v_lastseen = atol(ChrPtr(migr_chardata));
638                 is_textual_seen = 0;
639                 for (i=0; i < max; ++i) 
640                         if (!isdigit(ChrPtr(migr_chardata)[i]))
641                                 is_textual_seen = 1;
642                 if (is_textual_seen)
643                         safestrncpy(vbuf.v_seen, ChrPtr(migr_chardata), sizeof vbuf.v_seen);
644         }
645
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));
649         else return 0;
650         return 1;
651 }
652
653
654 void migr_xml_end(void *data, const char *el)
655 {
656         const char *ptr;
657         int msgcount = 0;
658         long msgnum = 0L;
659         long *msglist = NULL;
660         int msglist_alloc = 0;
661         /*** GENERAL STUFF ***/
662
663         if (!strcasecmp(el, "citadel_migrate_data")) {
664                 --citadel_migrate_data;
665                 return;
666         }
667
668         if (citadel_migrate_data != 1) {
669                 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!", el);
670                 return;
671         }
672
673         // syslog(LOG_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
674
675         /*** CONFIG ***/
676
677         if (!strcasecmp(el, "config"))
678         {
679                 syslog(LOG_DEBUG, "Imported config key=%s", ikey);
680
681                 if (ikey != NULL) {
682                         CtdlSetConfigStr(ikey, (char *)ChrPtr(migr_chardata));
683                         free(ikey);
684                         ikey = NULL;
685                 }
686                 else {
687                         syslog(LOG_INFO, "Closed a <config> tag but no key name was supplied.");
688                 }
689         }
690
691         /*** USER ***/
692         else if ((!strncasecmp(el, HKEY("u_"))) && 
693                  migr_userrecord(data, el))
694                 ; /* Nothing to do anymore */
695         else if (!strcasecmp(el, "user")) {
696                 CtdlPutUser(&usbuf);
697                 syslog(LOG_INFO, "Imported user: %s", usbuf.fullname);
698         }
699
700         /*** OPENID ***/
701
702         else if (!strcasecmp(el, "oid_url"))                    safestrncpy(openid_url, ChrPtr(migr_chardata), sizeof openid_url);
703         else if (!strcasecmp(el, "oid_usernum"))                openid_usernum = atol(ChrPtr(migr_chardata));
704
705         else if (!strcasecmp(el, "openid")) {                   /* see serv_openid_rp.c for a description of the record format */
706                 char *oid_data;
707                 int oid_data_len;
708                 oid_data_len = sizeof(long) + strlen(openid_url) + 1;
709                 oid_data = malloc(oid_data_len);
710                 memcpy(oid_data, &openid_usernum, sizeof(long));
711                 memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
712                 cdb_store(CDB_EXTAUTH, openid_url, strlen(openid_url), oid_data, oid_data_len);
713                 free(oid_data);
714                 syslog(LOG_INFO, "Imported OpenID: %s (%ld)", openid_url, openid_usernum);
715         }
716
717         /*** ROOM ***/
718         else if ((!strncasecmp(el, HKEY("QR"))) && 
719                  migr_roomrecord(data, el))
720                 ; /* Nothing to do anymore */
721         else if (!strcasecmp(el, "room")) {
722                 CtdlPutRoom(&qrbuf);
723                 syslog(LOG_INFO, "Imported room: %s", qrbuf.QRname);
724         }
725
726         /*** ROOM MESSAGE POINTERS ***/
727
728         else if (!strcasecmp(el, "FRname"))                     safestrncpy(FRname, ChrPtr(migr_chardata), sizeof FRname);
729
730         else if (!strcasecmp(el, "FRmsglist")) {
731                 if (!IsEmptyStr(FRname)) {
732                         msgcount = 0;
733                         msglist_alloc = 1000;
734                         msglist = malloc(sizeof(long) * msglist_alloc);
735
736                         syslog(LOG_DEBUG, "Message list for: %s", FRname);
737
738                         ptr = ChrPtr(migr_chardata);
739                         while (*ptr != 0) {
740                                 while ((*ptr != 0) && (!isdigit(*ptr))) {
741                                         ++ptr;
742                                 }
743                                 if ((*ptr != 0) && (isdigit(*ptr))) {
744                                         msgnum = atol(ptr);
745                                         if (msgnum > 0L) {
746                                                 if (msgcount >= msglist_alloc) {
747                                                         msglist_alloc *= 2;
748                                                         msglist = realloc(msglist, sizeof(long) * msglist_alloc);
749                                                 }
750                                                 msglist[msgcount++] = msgnum;
751                                                 }
752                                         }
753                                         while ((*ptr != 0) && (isdigit(*ptr))) {
754                                                 ++ptr;
755                                         }
756                                 }
757                         }
758                         if (msgcount > 0) {
759                                 CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL, 1);
760                         }
761                         free(msglist);
762                         msglist = NULL;
763                         msglist_alloc = 0;
764                         syslog(LOG_DEBUG, "Imported %d messages.", msgcount);
765                         if (server_shutting_down) {
766                                 return;
767                 }
768         }
769
770         /*** FLOORS ***/
771         else if ((!strncasecmp(el, HKEY("f_"))) && 
772                  migr_floorrecord(data, el))
773                 ; /* Nothing to do anymore */
774
775         else if (!strcasecmp(el, "floor")) {
776                 CtdlPutFloor(&flbuf, floornum);
777                 syslog(LOG_INFO, "Imported floor #%d (%s)", floornum, flbuf.f_name);
778         }
779
780         /*** VISITS ***/
781         else if ((!strncasecmp(el, HKEY("v_"))) && migr_visitrecord(data, el)) {
782                 ; /* Nothing to do anymore */
783         }
784         else if (!strcasecmp(el, "visit")) {
785                 put_visit(&vbuf);
786                 syslog(LOG_INFO, "Imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
787         }
788
789         /*** MESSAGES ***/
790         
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);
795
796         else if (!strcasecmp(el, "msg_text"))
797         {
798                 long rc;
799                 struct CtdlMessage *msg;
800
801                 FlushStrBuf(migr_MsgData);
802                 StrBufDecodeBase64To(migr_chardata, migr_MsgData);
803
804                 msg = CtdlDeserializeMessage(import_msgnum, -1,
805                                              ChrPtr(migr_MsgData), 
806                                              StrLength(migr_MsgData));
807                 if (msg != NULL) {
808                         rc = CtdlSaveThisMessage(msg, import_msgnum, 0);
809                         if (rc == 0) {
810                                 PutMetaData(&smi);
811                         }
812                         CM_Free(msg);
813                 }
814                 else {
815                         rc = -1;
816                 }
817
818                 syslog(LOG_INFO,
819                        "%s message #%ld, size=%d, refcount=%d, bodylength=%ld, content-type: %s",
820                        (rc!= 0)?"failed to import ":"Imported ",
821                        import_msgnum,
822                        StrLength(migr_MsgData),
823                        smi.meta_refcount,
824                        smi.meta_rfc822_length,
825                        smi.meta_content_type
826                 );
827                 memset(&smi, 0, sizeof(smi));
828         }
829
830         /*** MORE GENERAL STUFF ***/
831
832         FlushStrBuf(migr_chardata);
833 }
834
835
836 /*
837  * Import begins here
838  */
839 void migr_do_import(void) {
840         StrBuf *Buf;
841         XML_Parser xp;
842         int Finished = 0;
843         
844         unbuffer_output();
845         migr_chardata = NewStrBufPlain(NULL, SIZ * 20);
846         migr_MsgData = NewStrBufPlain(NULL, SIZ * 20);
847         Buf = NewStrBufPlain(NULL, SIZ);
848         xp = XML_ParserCreate(NULL);
849         if (!xp) {
850                 cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
851                 return;
852         }
853         XML_SetElementHandler(xp, migr_xml_start, migr_xml_end);
854         XML_SetCharacterDataHandler(xp, migr_xml_chardata);
855
856         CC->dont_term = 1;
857
858         cprintf("%d sock it to me\n", SEND_LISTING);
859         unbuffer_output();
860
861         client_set_inbound_buf(SIZ * 10);
862
863         while (!Finished && client_read_random_blob(Buf, -1) >= 0) {
864                 if ((StrLength(Buf) > 4) && !strcmp(ChrPtr(Buf) + StrLength(Buf) - 4, "000\n")) {
865                         Finished = 1;
866                         StrBufCutAt(Buf, StrLength(Buf) - 4, NULL);
867                 }
868                 if (server_shutting_down)
869                         break;  // Should we break or return?
870                 
871                 if (StrLength(Buf) == 0)
872                         continue;
873
874                 XML_Parse(xp, ChrPtr(Buf), StrLength(Buf), 0);
875                 FlushStrBuf(Buf);
876         }
877
878         XML_Parse(xp, "", 0, 1);
879         XML_ParserFree(xp);
880         FreeStrBuf(&Buf);
881         FreeStrBuf(&migr_chardata);
882         FreeStrBuf(&migr_MsgData);
883         rebuild_euid_index();
884         rebuild_usersbynumber();
885         CtdlSetConfigInt("MM_fulltext_wordbreaker", -1);        // Set an invalid wordbreaker to force re-indexing
886         CC->dont_term = 0;
887 }
888
889
890 /******************************************************************************
891  *                         Dispatcher, Common code                            *
892  ******************************************************************************/
893 /*
894  * Dump out the pathnames of directories which can be copied "as is"
895  */
896 void migr_do_listdirs(void) {
897         cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
898         cprintf("files|%s\n",           ctdl_file_dir);
899         cprintf("messages|%s\n",        ctdl_message_dir);
900         cprintf("keys|%s\n",            ctdl_key_dir);
901         cprintf("000\n");
902 }
903
904
905 /******************************************************************************
906  *                    Repair database integrity                               *
907  ******************************************************************************/
908
909 StrBuf *PlainMessageBuf = NULL;
910 HashList *UsedMessageIDS = NULL;
911
912 int migr_restore_message_metadata(long msgnum, int refcount)
913 {
914         CitContext *CCC = MyContext();
915         struct MetaData smi;
916         struct CtdlMessage *msg;
917         char *mptr = NULL;
918
919         /* We can use a static buffer here because there will never be more than
920          * one of this operation happening at any given time, and it's really best
921          * to just keep it allocated once instead of torturing malloc/free.
922          * Call this function with msgnum "-1" to free the buffer when finished.
923          */
924         static int encoded_alloc = 0;
925         static char *encoded_msg = NULL;
926
927         if (msgnum < 0) {
928                 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
929                         free(encoded_msg);
930                         encoded_alloc = 0;
931                         encoded_msg = NULL;
932                         // todo FreeStrBuf(&PlainMessageBuf); PlainMessageBuf = NULL;
933                 }
934                 return 0;
935         }
936
937         if (PlainMessageBuf == NULL) {
938                 PlainMessageBuf = NewStrBufPlain(NULL, 10*SIZ);
939         }
940
941         /* Ok, here we go ... */
942
943         msg = CtdlFetchMessage(msgnum, 1);
944         if (msg == NULL) {
945                 return 1;
946         }
947
948         GetMetaData(&smi, msgnum);
949         smi.meta_msgnum = msgnum;
950         smi.meta_refcount = refcount;
951         
952         /* restore the content type from the message body: */
953         mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
954         if (mptr != NULL) {
955                 char *aptr;
956                 safestrncpy(smi.meta_content_type, &mptr[13], sizeof smi.meta_content_type);
957                 striplt(smi.meta_content_type);
958                 aptr = smi.meta_content_type;
959                 while (!IsEmptyStr(aptr)) {
960                         if ((*aptr == ';')
961                             || (*aptr == ' ')
962                             || (*aptr == 13)
963                             || (*aptr == 10)) {
964                                 memset(aptr, 0, sizeof(smi.meta_content_type) - (aptr - smi.meta_content_type));
965                         }
966                         else aptr++;
967                 }
968         }
969
970         CCC->redirect_buffer = PlainMessageBuf;
971         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
972         smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
973         CCC->redirect_buffer = NULL;
974
975         syslog(LOG_INFO,
976                "Setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s",
977                smi.meta_msgnum,
978                smi.meta_refcount,
979                smi.meta_rfc822_length,
980                smi.meta_content_type
981         );
982
983         PutMetaData(&smi);
984
985         CM_Free(msg);
986
987         return 0;
988 }
989
990
991 void migr_check_room_msg(long msgnum, void *userdata) {
992         fprintf(migr_global_message_list, "%ld %s\n", msgnum, CC->room.QRname);
993 }
994
995
996 void migr_check_rooms_backend(struct ctdlroom *buf, void *data) {
997
998         /* message list goes inside this tag */
999
1000         CtdlGetRoom(&CC->room, buf->QRname);
1001         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_check_room_msg, NULL);
1002 }
1003
1004
1005 void RemoveMessagesFromRooms(StrBuf *RoomNameVec, long msgnum) {
1006         struct MetaData smi;
1007         const char *Pos = NULL;
1008         StrBuf *oneRoom = NewStrBuf();
1009
1010         syslog(LOG_INFO, "removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
1011
1012         while (Pos != StrBufNOTNULL){
1013                 StrBufExtract_NextToken(oneRoom, RoomNameVec, &Pos, '|');
1014                 CtdlDeleteMessages(ChrPtr(oneRoom), &msgnum, 1, "");
1015         };
1016         GetMetaData(&smi, msgnum);
1017         AdjRefCount(msgnum, -smi.meta_refcount);
1018 }
1019
1020
1021 void migr_do_restore_meta(void) {
1022         char buf[SIZ];
1023         int failGetMessage;
1024         long msgnum = 0;
1025         int lastnum = 0;
1026         int refcount = 0;
1027         CitContext *Ctx;
1028         char *prn;
1029         StrBuf *RoomNames;
1030         char cmd[SIZ];
1031
1032         migr_global_message_list = fopen(migr_tempfilename1, "w");
1033         if (migr_global_message_list != NULL) {
1034                 CtdlForEachRoom(migr_check_rooms_backend, NULL);
1035                 fclose(migr_global_message_list);
1036         }
1037
1038         /*
1039          * Process the 'global' message list.  (Sort it and remove dups.
1040          * Dups are ok because a message may be in more than one room, but
1041          * this will be handled by exporting the reference count, not by
1042          * exporting the message multiple times.)
1043          */
1044         snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
1045         if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
1046
1047         RoomNames = NewStrBuf();
1048         Ctx = CC;
1049         migr_global_message_list = fopen(migr_tempfilename2, "r");
1050         if (migr_global_message_list != NULL) {
1051                 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
1052                 while ((Ctx->kill_me == 0) && 
1053                        (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
1054                         msgnum = atol(buf);
1055                         if (msgnum == 0L) 
1056                                 continue;
1057                         if (lastnum == 0) {
1058                                 lastnum = msgnum;
1059                         }
1060                         prn = strchr(buf, ' ');
1061                         if (lastnum != msgnum) {
1062                                 failGetMessage = migr_restore_message_metadata(lastnum, refcount);
1063                                 if (failGetMessage) {
1064                                         RemoveMessagesFromRooms(RoomNames, lastnum);
1065                                 }
1066                                 refcount = 1;
1067                                 lastnum = msgnum;
1068                                 if (prn != NULL) {
1069                                         StrBufPlain(RoomNames, prn + 1, -1);
1070                                 }
1071                                 StrBufTrim(RoomNames);
1072                         }
1073                         else {
1074                                 if (prn != NULL) {
1075                                         if (StrLength(RoomNames) > 0) {
1076                                                 StrBufAppendBufPlain(RoomNames, HKEY("|"), 0);
1077                                         }
1078                                         StrBufAppendBufPlain(RoomNames, prn, -1, 1);
1079                                         StrBufTrim(RoomNames);
1080                                 }
1081                                 refcount ++;
1082                         }
1083                         lastnum = msgnum;
1084                 }
1085                 failGetMessage = migr_restore_message_metadata(msgnum, refcount);
1086                 if (failGetMessage) {
1087                         RemoveMessagesFromRooms(RoomNames, lastnum);
1088                 }
1089                 fclose(migr_global_message_list);
1090         }
1091
1092         migr_restore_message_metadata(-1L, -1); /* This frees the encoding buffer */
1093         cprintf("%d system analysis completed", CIT_OK);
1094         Ctx->kill_me = 1;
1095 }
1096
1097
1098 /******************************************************************************
1099  *                         Dispatcher, Common code                            *
1100  ******************************************************************************/
1101 void cmd_migr(char *cmdbuf) {
1102         char cmd[32];
1103         
1104         if (CtdlAccessCheck(ac_internal)) return;
1105         
1106         if (CtdlTrySingleUser()) {
1107                 CtdlDisableHouseKeeping();
1108                 CtdlMakeTempFileName(migr_tempfilename1, sizeof migr_tempfilename1);
1109                 CtdlMakeTempFileName(migr_tempfilename2, sizeof migr_tempfilename2);
1110
1111                 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
1112                 if (!strcasecmp(cmd, "export")) {
1113                         migr_do_export();
1114                 }
1115                 else if (!strcasecmp(cmd, "import")) {
1116                         migr_do_import();
1117                 }
1118                 else if (!strcasecmp(cmd, "listdirs")) {
1119                         migr_do_listdirs();
1120                 }
1121                 else if (!strcasecmp(cmd, "restoremeta")) {
1122                         migr_do_restore_meta();
1123                 }
1124                 else {
1125                         cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
1126                 }
1127
1128                 unlink(migr_tempfilename1);
1129                 unlink(migr_tempfilename2);
1130
1131                 CtdlEnableHouseKeeping();
1132                 CtdlEndSingleUser();
1133         }
1134         else {
1135                 cprintf("%d The migrator is already running.\n", ERROR + RESOURCE_BUSY);
1136         }
1137 }
1138
1139
1140 /******************************************************************************
1141  *                              Module Hook                                  *
1142  ******************************************************************************/
1143
1144 CTDL_MODULE_INIT(migrate)
1145 {
1146         if (!threading)
1147         {
1148                 CtdlRegisterProtoHook(cmd_migr, "MIGR", "Across-the-wire migration");
1149         }
1150         
1151         /* return our module name for the log */
1152         return "migrate";
1153 }