4 * Across-the-wire migration utility for Citadel
6 * Copyright (c) 2009 citadel.org
8 * This program is licensed to you under the terms of the GNU General Public License v3
18 #include <sys/types.h>
20 #include <sys/utsname.h>
28 #include <libcitadel.h>
33 #include "citadel_dirs.h"
41 * Replacement for gets() that doesn't throw a compiler warning.
42 * We're only using it for some simple prompts, so we don't need
43 * to worry about attackers exploiting it.
45 void getz(char *buf) {
48 ptr = fgets(buf, 32767, stdin);
54 ptr = strchr(buf, '\n');
62 int main(int argc, char *argv[])
66 char relhome[PATH_MAX]="";
67 char ctdldir[PATH_MAX]=CTDLDIR;
69 char sendcommand[PATH_MAX];
73 char socket_path[PATH_MAX];
74 char remote_user[256];
75 char remote_host[256];
76 char remote_sendcommand[PATH_MAX];
80 char spinning[4] = "-\\|/" ;
82 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
83 CtdlMakeTempFileName(socket_path, sizeof socket_path);
86 printf( "-------------------------------------------\n"
87 "Over-the-wire migration utility for Citadel\n"
88 "-------------------------------------------\n"
90 "This utility is designed to migrate your Citadel installation\n"
91 "to a new host system via a network connection, without disturbing\n"
92 "the source system. The target may be a different CPU architecture\n"
93 "and/or operating system. The source system should be running\n"
94 "Citadel %d.%02d or newer, and the target system should be running\n"
95 "either the same version or a newer version. You will also need\n"
96 "the 'rsync' utility, and OpenSSH v4 or newer.\n"
98 "You must run this utility on the TARGET SYSTEM. Any existing data\n"
99 "on this system will be ERASED.\n"
101 "Do you wish to continue? "
103 EXPORT_REV_MIN / 100,
107 if ((fgets(yesno, sizeof yesno, stdin) == NULL) || (tolower(yesno[0]) != 'y')) {
111 printf("\n\nGreat! First we will check some things out here on our target\n"
112 "system to make sure it is ready to receive data.\n\n");
114 printf("Locating 'sendcommand' and checking connectivity to Citadel...\n");
115 snprintf(sendcommand, sizeof sendcommand, "%s/sendcommand", ctdl_utilbin_dir);
116 snprintf(cmd, sizeof cmd, "%s 'NOOP'", sendcommand);
117 exitcode = system(cmd);
119 printf("\nctdlmigrate was unable to attach to the Citadel server\n"
120 "here on the target system. Is Citadel running?\n\n");
123 printf("\nOK, this side is ready to go. Now we must connect to the source system.\n\n");
125 printf("Enter the host name or IP address of the source system\n"
126 "(example: ctdl.foo.org)\n"
129 printf("\nEnter the name of a user on %s who has full access to Citadel files\n"
130 "(usually root)\n--> ",
134 printf("\nEstablishing an SSH connection to the source system...\n\n");
136 snprintf(cmd, sizeof cmd, "ssh -MNf -S %s %s@%s", socket_path, remote_user, remote_host);
137 exitcode = system(cmd);
139 printf("\nctdlmigrate was unable to establish an SSH connection to the\n"
140 "source system, and cannot continue.\n\n");
144 printf("\nTesting a command over the connection...\n\n");
145 snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s 'echo Remote commands are executing successfully.'",
146 socket_path, remote_user, remote_host);
147 exitcode = system(cmd);
150 printf("Remote commands are not succeeding.\n\n");
154 printf("\nLocating the remote 'sendcommand' and Citadel installation...\n");
155 snprintf(remote_sendcommand, sizeof remote_sendcommand, "/usr/local/citadel/sendcommand");
156 snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP",
157 socket_path, remote_user, remote_host, remote_sendcommand);
158 exitcode = system(cmd);
160 snprintf(remote_sendcommand, sizeof remote_sendcommand, "/usr/sbin/sendcommand");
161 snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP",
162 socket_path, remote_user, remote_host, remote_sendcommand);
163 exitcode = system(cmd);
166 printf("\nUnable to locate Citadel programs on the remote system. Please enter\n"
167 "the name of the directory on %s which contains the 'sendcommand' program.\n"
168 "(example: /opt/foo/citadel)\n"
169 "--> ", remote_host);
171 snprintf(remote_sendcommand, sizeof remote_sendcommand, "%s/sendcommand", buf);
172 snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP",
173 socket_path, remote_user, remote_host, remote_sendcommand);
174 exitcode = system(cmd);
178 printf("ctdlmigrate was unable to attach to the remote Citadel system.\n\n");
182 printf("ctdlmigrate will now begin a database migration...\n");
184 snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s -w3600 MIGR export",
185 socket_path, remote_user, remote_host, remote_sendcommand);
186 source_artv = popen(cmd, "r");
188 printf("\n%s\n\n", strerror(errno));
192 snprintf(cmd, sizeof cmd, "%s -w3600 MIGR import", sendcommand);
193 target_artv = popen(cmd, "w");
195 printf("\n%s\n\n", strerror(errno));
199 while (fgets(buf, sizeof buf, source_artv) != NULL) {
200 if (fwrite(buf, strlen(buf), 1, target_artv) < 1) {
201 printf("%s\n", strerror(errno));
205 if ((linecount % 100) == 0) {
206 printf("%c\r", spinning[((linecount / 100) % 4)]);
211 FAIL: pclose(source_artv);
214 // FIXME handle -h on both sides
215 // FIXME kill the master ssh session
216 printf("If this program was finished we would do more. FIXME\n");