+/*
+ * Replacement for gets() that doesn't throw a compiler warning.
+ * We're only using it for some simple prompts, so we don't need
+ * to worry about attackers exploiting it.
+ */
+void getz(char *buf) {
+ char *ptr;
+
+ ptr = fgets(buf, SIZ, stdin);
+ if (!ptr) {
+ buf[0] = 0;
+ return;
+ }
+
+ ptr = strchr(buf, '\n');
+ if (ptr) *ptr = 0;
+}
+
+
+// These variables are used by both main() and ctdlmigrate_exit()
+// They are global so that ctdlmigrate_exit can be called from a signal handler
+char socket_path[PATH_MAX];
+pid_t sshpid = (-1);
+
+void ctdlmigrate_exit(int cmdexit) {
+ unlink(socket_path);
+ if (sshpid > 0) {
+ printf("Shutting down the SSH session...\n");
+ kill(sshpid, SIGKILL);
+ }
+ printf("\n\n\033[3%dmExit code %d\033[0m\n", (cmdexit ? 1 : 2), cmdexit);
+ exit(cmdexit);
+}
+
+
+int uds_connectsock(char *sockpath) {
+ int s;
+ struct sockaddr_un addr;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, sockpath);
+
+ s = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (s < 0) {
+ fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
+ ctdlmigrate_exit(3);
+ }
+
+ if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
+ close(s);
+ ctdlmigrate_exit(3);
+ }
+
+ return s;
+}
+
+
+/*
+ * input binary data from socket
+ */
+void serv_read(int serv_sock, char *buf, int bytes) {
+ int len, rlen;
+
+ len = 0;
+ while (len < bytes) {
+ rlen = read(serv_sock, &buf[len], bytes - len);
+ if (rlen < 1) {
+ return;
+ }
+ len = len + rlen;
+ }
+}
+
+
+/*
+ * send binary to server
+ */
+void serv_write(int serv_sock, char *buf, int nbytes) {
+ int bytes_written = 0;
+ int retval;
+ while (bytes_written < nbytes) {
+ retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
+ if (retval < 1) {
+ return;
+ }
+ bytes_written = bytes_written + retval;
+ }
+}
+
+
+/*
+ * input string from socket - implemented in terms of serv_read()
+ */
+void serv_gets(int serv_sock, char *buf) {
+ int i;
+
+ /* Read one character at a time.
+ */
+ for (i = 0;; i++) {
+ serv_read(serv_sock, &buf[i], 1);
+ if (buf[i] == '\n' || i == (SIZ-1))
+ break;
+ }
+
+ /* If we got a long line, discard characters until the newline.
+ */
+ if (i == (SIZ-1)) {
+ while (buf[i] != '\n') {
+ serv_read(serv_sock, &buf[i], 1);
+ }
+ }
+
+ /* Strip all trailing nonprintables (crlf)
+ */
+ buf[i] = 0;
+}