]> git.cworth.org Git - notmuch-wiki/blob - remoteusage/aboriginal.mdwn
Ah, referrers are linked!
[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 ## The script
15
16 Write the following code to a file, for example `remote-notmuch.sh`.
17
18         #!/bin/bash
19
20         # http://notmuchmail.org/remoteusage/aboriginal/
21
22         set -eu
23         # To trace execution, outcomment next line. Note that emacs input may ...
24         #exec 2>>remote-errors; echo -- >&2; set -x # ... change (no stderr).
25
26         readonly SSH_CONTROL_SOCK='~'/.ssh/master-user@host:22
27
28         readonly notmuch=notmuch
29
30         printf -v ARGS '%q ' "$@" # bash feature
31
32         readonly SSH_CONTROL_ARGS='-oControlMaster=no -S '$SSH_CONTROL_SOCK
33
34         if ssh -q $SSH_CONTROL_ARGS 0.1 $notmuch $ARGS
35         then exit 0
36         else ev=$?
37         fi
38
39         # continuing here in case ssh exited with nonzero value.
40
41         case $* in
42          'config get user.primary_email') echo 'nobody@nowhere.invalid'; exit 0 ;;
43          'config get user.name') echo 'nobody'; exit 0 ;;
44          'count'*'--batch'*) while read line; do echo 1; done; exit 0 ;;
45          'count'*) echo 1; exit 0 ;;
46          'search-tags'*) echo 'errors'; exit 0 ;;
47          'search'*'--output=tags'*) echo 'errors'; exit 0 ;;
48         esac
49
50         if ssh $SSH_CONTROL_ARGS -O check 0.1
51         then
52          echo ' Control socket is alive but something failed during data transmission.'
53          exit $ev
54         fi
55
56         echo " See`sed '1d;2d;s/.//;q' "$0"` for help."
57
58 Note the `0.1` in ssh command line. It is used to avoid any opportunistic
59 behaviour ssh might do; for example if control socket is not alive ssh
60 would attempt to do it's own ssh connection to remote ssh server. As
61 address `0.1` is invalid this attempt will fail early.
62
63 ## Test
64
65 Easiest way to test this script is to run the pre-made ssh connection
66 using the following command line:
67
68         ssh -M -S '~'/.ssh/master-user@host:22 [user@]remotehost
69
70 (replace `[user@]remotehost` with your login info). Doing this the
71 above script can be run unmodified. After the above command has been
72 run on one terminal, enter `chmod +x remote-notmuch.sh` in another
73 terminal and then test the script with `./remote-notmuch.sh help`
74
75 Note that the '~' is inside single quotes for a reason. In this
76 case shell never expand it to `$HOME` -- ssh does it by not reading
77 `$HOME` but checking the real user home directory from `/etc/passwd`.
78 For security purposes this is just how it should be.
79
80 ## Tune
81
82 The path `'~'/.ssh/master-user@host:22` might look too generic to be
83 used as is as the control socket after initial testing (but it can
84 be used). It is presented as a template for what could be configured
85 to `$HOME/.ssh/config`. For example:
86
87         Host *
88             ControlPath ~/.ssh/master-%h@%p:%r
89
90 is a good entry to be written in `$HOME/.ssh/config`;
91 [[remoteusage|remoteusage]] uses the same. Now, let's say you'd
92 make your pre-made ssh connection with command
93
94         ssh -M alice@example.org
95
96 After configuring
97 `readonly SSH_CONTROL_SOCK='~'/.ssh/master-alice@example.org:22`
98 to the `./remote-notmuch.sh` wrapper script testing with
99 `./remote-notmuch.sh help` should work fine.
100
101 ## Configure Emacs on the client computer ##
102
103 See the section *Configure Emacs on the client computer* in
104 [[remoteusage|remoteusage]] how to do this. The instructions are the same.