]> git.cworth.org Git - obsolete/notmuch-wiki/blob - remoteusage/aboriginal.mdwn
tab->8 spaces
[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, uncomment next line.
27         #BASH_XTRACEFD=6; exec 6>>remote-errors; echo -- >&6; set -x
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
77
78         ./remote-notmuch.sh help
79
80 Note that the '~' in the ssh command line above is inside single quotes
81 for a reason. In this case shell never expand it to `$HOME` -- ssh does
82 it by not reading `$HOME` but checking the real user home directory
83 from `/etc/passwd`.  For security purposes this is just how it should
84 be.
85
86 ## Tune
87
88 The path `'~'/.ssh/master-user@host:22` might look too generic to be
89 used as is as the control socket after initial testing (but it can
90 be used). It is presented as a template for what could be configured
91 to `$HOME/.ssh/config`. For example:
92
93         Host *
94             ControlPath ~/.ssh/master-%h@%p:%r
95
96 is a good entry to be written in `$HOME/.ssh/config`;
97 [[remoteusage|remoteusage]] uses the same. Now, let's say you'd
98 make your pre-made ssh connection with command
99
100         ssh -M alice@example.org
101
102 After configuring
103 `readonly SSH_CONTROL_SOCK='~'/.ssh/master-alice@example.org:22`
104 to the `./remote-notmuch.sh` wrapper script testing with
105 `./remote-notmuch.sh help` should work fine.
106
107 ## Configure Emacs on the client computer ##
108
109 See the section *Configure Emacs on the client computer* in
110 [[remoteusage|remoteusage]] how to do this. The instructions are the same.
111
112
113 ## Creating master connection
114
115 As mentioned so many times, using this solution requires one pre-made
116 ssh connection in "master" mode. The simplest way is to dedicate one
117 terminal for the connection with shell access to the remote machine:
118
119         ssh -M -S '~'/.ssh/master-user@host:22 [user@]remotehost
120
121 One possibility is to have this dedicated terminal in a way that the
122 connection has (for example 1 hour) timeout:
123
124         ssh -M -S '~'/.ssh/master-user@host:22 [user@]remotehost sleep 3600
125
126 The above holds the terminal. The next alternative puts the command in
127 background:
128
129         ssh -f -M -S '~'/.ssh/master-user@host:22 [user@]remotehost sleep 3600
130
131 If you don't want this to timeout so soon, use a longer sleep, like 99999999
132 (8 9:s, 1157 days, a bit more than 3 years).
133
134 A more "exotic" solution would be to make a shell script running on remote
135 machine, checking/inotifying when new mail arrives. When mail arrives it
136 could send message back to local host, where a graphical client (to be written)
137 pops up on display providing info about received mail (and exiting this
138 graphical client connection to remote host is terminated).
139
140 ## Troubleshooting
141
142 If you experience strange output when using from emacs first attempt to just
143 run
144
145         ./remote-notmuch.sh help
146
147 from command line and observe output. If it looks as it should be next uncomment
148 the line
149
150         #BASH_XTRACEFD=6; exec 6>>remote-errors; echo -- >&6; set -x
151
152 in `./remote-notmuch.sh` and attempt to use it from emacs again -- and then
153 examine the contents of `remote-errors` in the working directory emacs was
154 started.