]> git.cworth.org Git - obsolete/notmuch-wiki/blob - remoteusage/aboriginal.mdwn
ccaded1c6101895c9416d5372f4790fb602b2170
[obsolete/notmuch-wiki] / remoteusage / aboriginal.mdwn
1 ## Remoteusage without password-free login requirement
2
3 This is alternative to [[remoteusage|remoteusage]] where password-free
4 login is not a requirement. See [[remoteusage|remoteusage]] page for
5 other requirements and general information.
6
7 This solution uses one pre-made ssh connection where the client is put
8 into "master" mode (-M) for connection sharing. The wrapper script then
9 uses the control socket created by this pre-made ssh connection for
10 its own connection. As long as master ssh connection is live, slave
11 can use it. Disconnecting master all future attempts to connect
12 from the script will fail.
13
14 At the end of this document there is information for some possible ways
15 how master ssh connection can be done.
16
17 ## The script
18
19 Write the following code to a file, for example `remote-notmuch.sh`.
20
21         #!/bin/bash
22
23         # http://notmuchmail.org/remoteusage/aboriginal/
24
25         set -eu
26         # To trace execution, outcomment next line. Note that emacs input may ...
27         #exec 2>>remote-errors; echo -- >&2; set -x # ... change (no stderr).
28
29         readonly SSH_CONTROL_SOCK='~'/.ssh/master-user@host:22
30
31         readonly notmuch=notmuch
32
33         printf -v ARGS '%q ' "$@" # bash feature
34
35         readonly SSH_CONTROL_ARGS='-oControlMaster=no -S '$SSH_CONTROL_SOCK
36
37         if ssh -q $SSH_CONTROL_ARGS 0.1 $notmuch $ARGS
38         then exit 0
39         else ev=$?
40         fi
41
42         # continuing here in case ssh exited with nonzero value.
43
44         case $* in
45          'config get user.primary_email') echo 'nobody@nowhere.invalid'; exit 0 ;;
46          'config get user.name') echo 'nobody'; exit 0 ;;
47          'count'*'--batch'*) while read line; do echo 1; done; exit 0 ;;
48          'count'*) echo 1; exit 0 ;;
49          'search-tags'*) echo 'errors'; exit 0 ;;
50          'search'*'--output=tags'*) echo 'errors'; exit 0 ;;
51         esac
52
53         if ssh $SSH_CONTROL_ARGS -O check 0.1
54         then
55          echo ' Control socket is alive but something failed during data transmission.'
56          exit $ev
57         fi
58
59         echo " See`sed '1d;2d;s/.//;q' "$0"` for help."
60
61 Note the `0.1` in ssh command line. It is used to avoid any opportunistic
62 behaviour ssh might do; for example if control socket is not alive ssh
63 would attempt to do it's own ssh connection to remote ssh server. As
64 address `0.1` is invalid this attempt will fail early.
65
66 ## Test
67
68 Easiest way to test this script is to run the pre-made ssh connection
69 using the following command line:
70
71         ssh -M -S '~'/.ssh/master-user@host:22 [user@]remotehost sleep 600
72
73 (replace `[user@]remotehost` with your login info). Doing this the
74 above wrapper script can be run unmodified. After the above command has
75 been run on **one terminal**, enter `chmod +x remote-notmuch.sh` in
76 **another terminal** and then test the script with `./remote-notmuch.sh help`
77
78 Note that the '~' is inside single quotes for a reason. In this
79 case shell never expand it to `$HOME` -- ssh does it by not reading
80 `$HOME` but checking the real user home directory from `/etc/passwd`.
81 For security purposes this is just how it should be.
82
83 ## Tune
84
85 The path `'~'/.ssh/master-user@host:22` might look too generic to be
86 used as is as the control socket after initial testing (but it can
87 be used). It is presented as a template for what could be configured
88 to `$HOME/.ssh/config`. For example:
89
90         Host *
91             ControlPath ~/.ssh/master-%h@%p:%r
92
93 is a good entry to be written in `$HOME/.ssh/config`;
94 [[remoteusage|remoteusage]] uses the same. Now, let's say you'd
95 make your pre-made ssh connection with command
96
97         ssh -M alice@example.org
98
99 After configuring
100 `readonly SSH_CONTROL_SOCK='~'/.ssh/master-alice@example.org:22`
101 to the `./remote-notmuch.sh` wrapper script testing with
102 `./remote-notmuch.sh help` should work fine.
103
104 ## Configure Emacs on the client computer ##
105
106 See the section *Configure Emacs on the client computer* in
107 [[remoteusage|remoteusage]] how to do this. The instructions are the same.
108
109
110 ## Creating master connection
111
112 As mentioned so many times, using this solution requires one pre-made
113 ssh connection in "master" mode. The simplest way is to dedicate one
114 terminal for the connection with shell access to the remote machine:
115
116         ssh -M -S '~'/.ssh/master-user@host:22 [user@]remotehost
117
118 One possibility is to have this dedicated terminal in a way that the
119 connection has (for example 1 hour) timeout:
120
121         ssh -M -S '~'/.ssh/master-user@host:22 [user@]remotehost sleep 3600
122
123 The above holds the terminal. The next alternative puts the command in
124 background:
125
126         ssh -f -M -S '~'/.ssh/master-user@host:22 [user@]remotehost sleep 3600
127
128 If you don't want this to timeout so soon, use a longer sleep, like 99999999
129 (8 9:s, 1157 days, a bit more than 3 years).
130
131 A more "exotic" solution would be to make a shell script running on remote
132 machine, checking/inotifying when new mail arrives. When mail arrives it
133 could send message back to local host, where a graphical client (to be written)
134 pops up on display providing info about received mail (and exiting this
135 graphical client connection to remote host is terminated).