1#!/bin/sh
2
3# Copyright (C) 2015 by Yuri Victorovich. All rights reserved.
4
5###
6### This is the telnet-like application to control the vm-to-tor
7### virtual machine.
8### It expects the virtual machine name in the form tapN.
9### It can only run when vm-to-tor was started with the following
10### parameters set in /etc/rc.conf:
11### * vm_to_tor_control_socket="YES"
12### * vm_to_tor_allow_cookie_auth="YES"
13###
14
15## check usage and extract command line arguments
16
17if [ "$#" -ne 1 -o -z "$1" ]; then
18  echo "Usage:     ${0##*/} tapN"
19  exit 1
20fi
21
22VM=$1
23
24## check user
25
26if [ $(id -u) != 0 -a $(id -un) != "_tor" ]; then
27  echo "Only root and _tor users can run ${0##*/}" >&2
28  exit 1
29fi
30
31## directory base of the VM, check that virtual machine exists
32
33VM_DIR=/var/tmp/vm-to-tor/${VM}
34
35if [ ! -d "${VM_DIR}" ]; then
36  echo "Virtual machine $VM not found" 2>&1
37  exit 1
38fi
39
40## check if control socket and cookie exist
41
42if [ ! -S "${VM_DIR}/ctrl" ]; then
43  echo "No control socket found for virtual machine $VM" 2>&1
44  exit 1
45fi
46if [ ! -f "${VM_DIR}/data/control_auth_cookie" -o $(ls -l "${VM_DIR}/data/control_auth_cookie" | awk '{ print $5}') != 32 ]; then
47  echo "No control socket cookie found for virtual machine $VM" 2>&1
48  exit 1
49fi
50
51## check if the VM runs
52
53if [ ! -s ${VM_DIR}/tor.pid -o "$(procstat $(cat ${VM_DIR}/tor.pid) | tail -1 | sed -E 's/^[[:space:]]*([0-9]+).*/\1/g' 2>/dev/null)" != "$(cat ${VM_DIR}/tor.pid)" ]; then
54  echo "Virtual machine $VM is not running" 2>&1
55  exit 1
56fi
57
58## Run: automatically authenticate and accept user termonal input
59
60(echo "(auto-login)" >&2; echo -n "AUTHENTICATE " ; hexdump -e '32/1 "%02x""\n"' "${VM_DIR}/data/control_auth_cookie"; cat) | \
61  nc -U "${VM_DIR}/ctrl"
62
63