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