* ctdlmigrate utility (not finished)
[citadel.git] / citadel / ctdlmigrate.c
1 /*
2  * $Id: $
3  *
4  * Across-the-wire migration utility for Citadel
5  *
6  * Copyright (c) 2009 citadel.org
7  *
8  * This program is licensed to you under the terms of the GNU General Public License v3
9  *
10  */
11
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/utsname.h>
21 #include <sys/wait.h>
22 #include <signal.h>
23 #include <netdb.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <pwd.h>
27 #include <time.h>
28 #include <libcitadel.h>
29 #include "citadel.h"
30 #include "axdefs.h"
31 #include "sysdep.h"
32 #include "config.h"
33 #include "citadel_dirs.h"
34 #if HAVE_BACKTRACE
35 #include <execinfo.h>
36 #endif
37
38
39
40
41 int main(int argc, char *argv[])
42 {
43         int relh=0;
44         int home=0;
45         char relhome[PATH_MAX]="";
46         char ctdldir[PATH_MAX]=CTDLDIR;
47         char yesno[5];
48         char sendcommand[PATH_MAX];
49         int exitcode;
50         char cmd[PATH_MAX];
51         char socket_path[PATH_MAX];
52         char remote_user[256];
53         char remote_host[256];
54         
55         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
56         CtdlMakeTempFileName(socket_path, sizeof socket_path);
57
58         system("clear");
59         printf( "-------------------------------------------\n"
60                 "Over-the-wire migration utility for Citadel\n"
61                 "-------------------------------------------\n"
62                 "\n"
63                 "This utility is designed to migrate your Citadel installation\n"
64                 "to a new host system via a network connection, without disturbing\n"
65                 "the source system.  The target may be a different CPU architecture\n"
66                 "and/or operating system.  The source system should be running\n"
67                 "Citadel %d.%02d or newer, and the target system should be running\n"
68                 "either the same version or a newer version.  You will also need\n"
69                 "the 'rsync' utility, and OpenSSH v4 or newer.\n"
70                 "\n"
71                 "You must run this utility on the TARGET SYSTEM.  Any existing data\n"
72                 "on this system will be ERASED.\n"
73                 "\n"
74                 "Do you wish to continue? "
75                 ,
76                 EXPORT_REV_MIN / 100,
77                 EXPORT_REV_MIN % 100
78         );
79
80         if ((fgets(yesno, sizeof yesno, stdin) == NULL) || (tolower(yesno[0]) != 'y')) {
81                 exit(0);
82         }
83
84         printf("\n\nGreat!  First we will check some things out here on our target\n"
85                 "system to make sure it is ready to receive data.\n\n");
86
87         printf("Locating 'sendcommand' and checking connectivity to Citadel...\n");
88         snprintf(sendcommand, sizeof sendcommand, "%s/sendcommand", ctdl_utilbin_dir);
89         snprintf(cmd, sizeof cmd, "%s 'NOOP'", sendcommand);
90         exitcode = system(cmd);
91         if (exitcode != 0) {
92                 printf("\nctdlmigrate was unable to attach to the Citadel server\n"
93                         "here on the target system.  Is Citadel running?\n\n");
94                 exit(1);
95         }
96         printf("\nOK, this side is ready to go.  Now we must connect to the source system.\n\n");
97
98         printf("Enter the host name or IP address of the source system\n"
99                 "(example: ctdl.foo.org)\n"
100                 "--> ");
101         gets(remote_host);
102         printf("\nEnter the name of a user on %s who has full access to Citadel files\n"
103                 "(usually root)\n--> ",
104                 remote_host);
105         gets(remote_user);
106
107         printf("\nEstablishing an SSH connection to the source system...\n\n");
108         unlink(socket_path);
109         snprintf(cmd, sizeof cmd, "ssh -MNf -S %s %s@%s", socket_path, remote_user, remote_host);
110         exitcode = system(cmd);
111         if (exitcode != 0) {
112                 printf("\nctdlmigrate was unable to establish an SSH connection to the\n"
113                         "source system, and cannot continue.\n\n");
114                 exit(exitcode);
115         }
116
117         printf("\nTesting a command over the connection...\n\n");
118         snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s 'echo Remote commands are executing successfully.'",
119                 socket_path, remote_user, remote_host);
120         exitcode = system(cmd);
121         printf("\n");
122         if (exitcode != 0) {
123                 printf("Remote commands are not succeeding.\n\n");
124                 exit(exitcode);
125         }
126
127
128
129         // FIXME kill the master ssh session
130         printf("If this program was finished we would do more.  FIXME\n");
131         exit(0);
132 }