- printf("\nEstablishing an SSH connection to the source system...\n\n");
- unlink(socket_path);
- snprintf(cmd, sizeof cmd, "ssh -MNf -S %s %s@%s", socket_path, remote_user, remote_host);
- exitcode = system(cmd);
- if (exitcode != 0) {
- printf("\nctdlmigrate was unable to establish an SSH connection to the\n"
- "source system, and cannot continue.\n\n");
- exit(exitcode);
- }
-
- printf("\nTesting a command over the connection...\n\n");
- snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s 'echo Remote commands are executing successfully.'",
- socket_path, remote_user, remote_host);
- exitcode = system(cmd);
- printf("\n");
- if (exitcode != 0) {
- printf("Remote commands are not succeeding.\n\n");
- exit(exitcode);
- }
-
- printf("\nLocating the remote 'sendcommand' and Citadel installation...\n");
- snprintf(remote_sendcommand, sizeof remote_sendcommand, "/usr/local/citadel/sendcommand");
- snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP",
- socket_path, remote_user, remote_host, remote_sendcommand);
- exitcode = system(cmd);
- if (exitcode != 0) {
- snprintf(remote_sendcommand, sizeof remote_sendcommand, "/usr/sbin/sendcommand");
- snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP",
- socket_path, remote_user, remote_host, remote_sendcommand);
- exitcode = system(cmd);
- }
- if (exitcode != 0) {
- printf("\nUnable to locate Citadel programs on the remote system. Please enter\n"
- "the name of the directory on %s which contains the 'sendcommand' program.\n"
- "(example: /opt/foo/citadel)\n"
- "--> ", remote_host);
- gets(buf);
- snprintf(remote_sendcommand, sizeof remote_sendcommand, "%s/sendcommand", buf);
- snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP",
- socket_path, remote_user, remote_host, remote_sendcommand);
- exitcode = system(cmd);
- }
- printf("\n");
- if (exitcode != 0) {
- printf("ctdlmigrate was unable to attach to the remote Citadel system.\n\n");
- exit(exitcode);
- }
-
- printf("ctdlmigrate will now begin a database migration...\n");
-
- snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s -w3600 MIGR export",
- socket_path, remote_user, remote_host, remote_sendcommand);
- source_artv = popen(cmd, "r");
- if (!source_artv) {
- printf("\n%s\n\n", strerror(errno));
- exit(2);
- }
-
- snprintf(cmd, sizeof cmd, "%s -w3600 MIGR import", sendcommand);
- target_artv = popen(cmd, "w");
- if (!target_artv) {
- printf("\n%s\n\n", strerror(errno));
- exit(3);
- }
-
- while (fgets(buf, sizeof buf, source_artv) != NULL) {
- fwrite(buf, strlen(buf), 1, target_artv);
- ++linecount;
- if ((linecount % 100) == 0) {
- printf("%c\r", spinning[((linecount / 100) % 4)]);
- fflush(stdout);
- }
- }
-
- pclose(source_artv);
- pclose(target_artv);
-
- // FIXME handle -h on both sides
- // FIXME kill the master ssh session
- printf("If this program was finished we would do more. FIXME\n");
- exit(0);
+ if (!cmdexit) {
+ printf("\n");
+ printf("OK, this side is ready to go. Now we must connect to the source system.\n\n");
+ printf("Enter the host name or IP address of the source system\n");
+ printf("(example: ctdl.foo.org)\n");
+ getz(remote_host, sizeof remote_host, NULL, "--> ");
+
+ while (IsEmptyStr(remote_user)) {
+ printf("\n");
+ printf("Enter the name of a user on %s who has full access to Citadel files.\n", remote_host);
+ getz(remote_user, sizeof remote_user, "root", "--> ");
+ }
+
+ printf("\n");
+ printf("Establishing an SSH connection to the source system...\n");
+ sprintf(socket_path, "/tmp/ctdlmigrate-socket.%ld.%d", time(NULL), getpid());
+ unlink(socket_path);
+
+ snprintf(cmd, sizeof cmd, "ssh -MNf -S %s -l %s %s", socket_path, remote_user, remote_host);
+ sshpid = fork();
+ if (sshpid < 0) {
+ printf("%s\n", strerror(errno));
+ cmdexit = errno;
+ }
+ else if (sshpid == 0) {
+ execl("/bin/bash", "bash", "-c", cmd, (char *) NULL);
+ exit(1);
+ }
+ else { // Wait for SSH to go into the background
+ waitpid(sshpid, &cmdexit, 0);
+ }
+ }
+
+ if (!cmdexit) {
+ printf("\nTesting a command over the connection...\n\n");
+ snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s 'echo Remote commands are executing successfully.'",
+ socket_path, remote_user, remote_host);
+ cmdexit = system(cmd);
+ printf("\n");
+ if (cmdexit != 0) {
+ printf("\033[31mRemote commands are not succeeding.\033[0m\n\n");
+ }
+ }
+
+ if (!cmdexit) {
+ printf("\nLocating the remote 'sendcommand' and Citadel installation...\n");
+ snprintf(remote_sendcommand, sizeof remote_sendcommand, "/usr/local/citadel/sendcommand");
+ snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP", socket_path, remote_user, remote_host, remote_sendcommand);
+ cmdexit = system(cmd);
+
+ if (cmdexit) {
+ snprintf(remote_sendcommand, sizeof remote_sendcommand, "/usr/sbin/sendcommand");
+ snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP", socket_path, remote_user, remote_host, remote_sendcommand);
+ cmdexit = system(cmd);
+ }
+
+ if (cmdexit) {
+ printf("\n");
+ printf("Unable to locate Citadel programs on the remote system. Please enter\n"
+ "the name of the directory on %s which contains the 'sendcommand' program.\n"
+ "(example: /opt/foo/citadel)\n", remote_host);
+ getz(remote_host, sizeof remote_host, "/usr/local/citadel", "--> ");
+ snprintf(remote_sendcommand, sizeof remote_sendcommand, "%s/sendcommand", buf);
+ snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s NOOP", socket_path, remote_user, remote_host, remote_sendcommand);
+ cmdexit = system(cmd);
+ if (!cmdexit) {
+ printf("ctdlmigrate was unable to attach to the remote Citadel system.\n\n");
+ }
+ }
+ }
+
+ if (!cmdexit) {
+ printf( "\033[2J\033[H\n"
+ " \033[32m╔═══════════════════════════════════════════════════════════════════╗\n"
+ " ║ ║\n"
+ " ║ \033[33mMigrating from: %-50s\033[32m║\n"
+ " ║ ║\n"
+ " ╠═══════════════════════════════════════════════════════════════════╣\n"
+ " ║ ║\n"
+ " ║ Lines received: 0 Percent complete: 0 ║\n"
+ " ║ ║\n"
+ " ╚═══════════════════════════════════════════════════════════════════╝\033[0m\n"
+ "\n", remote_host
+ );
+
+ snprintf(cmd, sizeof cmd, "ssh -S %s %s@%s %s -w3600 MIGR export", socket_path, remote_user, remote_host, remote_sendcommand);
+ sourcefp = popen(cmd, "r");
+ if (!sourcefp) {
+ cmdexit = errno;
+ printf("\n%s\n\n", strerror(errno));
+ }
+ }
+
+ if (!cmdexit) {
+ serv_puts(local_admin_socket, "MIGR import");
+ serv_gets(local_admin_socket, buf);
+ if (buf[0] != '4') {
+ printf("\n\033[31m%s\033[0m\n", buf);
+ cmdexit = 3;
+ }
+ }
+
+ if (!cmdexit) {
+ char *ptr;
+ time_t last_update = time(NULL);
+ while (ptr = fgets(buf, SIZ, sourcefp), (ptr != NULL)) {
+ ptr = strchr(buf, '\n');
+ if (ptr) *ptr = 0; // remove the newline character
+ ++linecount;
+ if (!strncasecmp(buf, "<progress>", 10)) {
+ progress = atoi(&buf[10]);
+ printf("\033[8;75H\033[33m%d\033[0m\033[20;0H\n", progress);
+ }
+ if (time(NULL) != last_update) {
+ last_update = time(NULL);
+ printf("\033[8;29H\033[33m%d\033[0m\033[20;0H\n", linecount);
+ }
+ serv_puts(local_admin_socket, buf);
+ }
+
+ serv_puts(local_admin_socket, "000");
+ }
+
+ if ( (cmdexit == 0) && (progress < 100) ) {
+ printf("\033[31mERROR: source stream ended before 100 percent of data was received.\033[0m\n");
+ ctdlmigrate_exit(86);
+ }
+
+ // FIXME restart the local server now
+
+ ctdlmigrate_exit(cmdexit);