chiark / gitweb /
userv/distorted-keys.in: Reformat, with backslashes in their own column.
[distorted-keys] / keys.keeper-cards
1 #! /bin/sh
2 ###
3 ### Issue cards containing a bunch of keeper secrets
4 ###
5 ### (c) 2011 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the distorted.org.uk key management suite.
11 ###
12 ### distorted-keys is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### distorted-keys is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with distorted-keys; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 set -e
27 case "${KEYSLIB+t}" in t) ;; *) echo >&2 "$0: KEYSLIB unset"; exit 1 ;; esac
28 . "$KEYSLIB"/keyfunc.sh
29
30 defhelp <<HELP
31 KEEPER [INDICES ...]
32 Typeset cards for a set of keeper secrets.
33
34 This program writes a file KEEPER.ps which will contain private keys from the
35 keeper set KEEPER, specifically the keys with the given INDICES.  Elements of
36 the list are either simple integers or ranges [LOW]-[HIGH]; if LOW is
37 omitted, it means 0, and if HIGH is omitted, it means the highest possible
38 index.  If no INDICES are given then all secret keys are written.
39
40 The public keys are found in $KEYS/keeper/KEEPER/I.pub;
41 private keys are read from KEEPER/I in the current directory.
42 HELP
43
44 ## Parse the command line.
45 case $# in 0) usage_err ;; esac
46 keeper=$1; shift
47 checkword "keeper set label" "$keeper"
48 read n hunoz <$KEYS/keeper/$keeper/meta
49
50 ## Build a colon-separated list of the indices we actually want.
51 want=:
52 case $# in 0) set 0- ;; esac
53 for range in "$@"; do
54   case "$range" in
55     *[!-0-9]* | *[!0-9]*-* | *-*[!0-9]*)
56       echo >&2 "$quis: bad index range \`$range'"
57       exit 1
58       ;;
59     *-*)
60       low=${range%-*} high=${range#*-}
61       ;;
62     *)
63       low=$range high=$range
64       ;;
65   esac
66   case "$low" in ?*) ;; *) low=0 ;; esac
67   case "$high" in ?*) ;; *) high=$(( $n - 1 )) ;; esac
68   if [ 0 -gt $low -o $low -gt $high -o $high -ge $n ]; then
69     echo >&2 "$quis: invalid index range \`$range'"
70     exit 1
71   fi
72   i=$(( $low + 0 ))
73   while [ $i -le $high ]; do
74     case $want in *:"$i":*) ;; *) want=$want$i: ;; esac
75     i=$(( $i + 1 ))
76   done
77 done
78
79 ## Start working on the output file.  This will contain deep secrets, so
80 ## don't leave stuff easily readable.
81 mktmp
82 umask 077
83 exec 3>$tmp/$keeper.tex
84 cat >&3 <<'EOF'
85 \documentclass[a4paper, landscape, 12pt]{article}
86 \usepackage[utf8]{inputenc}
87 \usepackage[T1]{fontenc}
88 \usepackage[palatino, helvetica, courier, maths = cmr]{mdwfonts}
89 \usepackage{graphicx}
90
91 %% Report errors with enough context that we can debug them.
92 \errorcontextlines=999
93
94 %% Basic layout for the cards.  We use the paragraph filling machinery, but
95 %% don't actually need most of the trimmings.
96 \parindent=0pt
97 \parfillskip=0pt
98 \pagestyle{empty}
99
100 %% Page layout: try to use most of the page.  The document class will already
101 %% have set up the paper size, but we do the rest here.
102 \hoffset=-1in \voffset=-1in
103 \oddsidemargin=20mm
104 \textwidth=\paperwidth \advance\textwidth by -2\oddsidemargin
105 \topmargin=20mm
106 \headheight=0pt \headsep=0pt
107 \textheight=\paperheight \advance\textheight by -2\topmargin
108 \AtBeginDocument{\special{papersize=\the\paperwidth,\the\paperheight}}
109
110 %% Parameters for the cards and guide rules.
111 \newdimen\cardwd \cardwd=82mm
112 \newdimen\cardht \cardht=49mm
113 \newdimen\guidelen \guidelen=10mm
114 \newdimen\rulewd \rulewd=0.6pt
115
116 %% Typesetting the secret as text.  The macro \snarf TOKEN T0 T1 ... T7
117 %% gathers T0 T1 ... T7 into a single argument and passes them to TOKEN, as
118 %% long as T0 is not \relax.  We use this to process the secret text in a
119 %% continuation-passing style.
120 \def\snarf#1#2{%
121   \ifx#2\relax\let\next\empty%
122   \else\def\next{\snarfdo#1#2}%
123   \fi%
124   \next%
125 }
126 \def\snarfdo#1#2#3#4#5#6#7#8#9{#1{#2#3#4#5#6#7#8#9}}
127
128 %% Print the left and right halves of the line, with a separator.  Use boxes
129 %% for the lines so that TeX will work out the width of the enclosing vbox
130 %% for us.  The basic usage is \line TEXT \relax ... \relax, with eight
131 %% \relax tokens: this is enough to complete both \snarf calls.
132 \def\line{\snarf\lineleft}
133 \def\lineleft#1{\hbox\bgroup#1 \snarf\lineright}
134 \def\lineright#1{#1\egroup\line}
135
136 %% Typeset a card containing a secret.  Usage is \card{INDEX}{SECRET}.
137 \def\card#1#2{%
138   %%
139   %% Make sure we're setting a paragraph.
140   \leavevmode%
141   %%
142   %% Initial material: a stretchy space on the left.
143   \hbox{}\nobreak\hfil%
144   %%
145   %% An alignment for the guide markers surrounding the actual card.
146   \vbox{\halign{&##\cr%
147     %%
148     %% Top left guides.
149     \vrule width \guidelen height \rulewd depth 0pt%
150     \vrule width \rulewd depth 0pt height \guidelen%
151     &%
152     %%
153     %% Top centre gap.
154     \hfil%
155     &%
156     %%
157     %% Top right guides.
158     \vrule width \rulewd depth 0pt height \guidelen%
159     \vrule width \guidelen height \rulewd depth 0pt%
160     \cr%
161     %%
162     %% Left gap.
163     &%
164     %%
165     %% The actual card.
166     \vbox to \cardht{%
167       %%
168       %% We actually do more or less sensible typesetting.  TeX will set the
169       %% box width from the hsize, and we should leave a small margin all
170       %% around.
171       \parfillskip=0pt plus 1fil%
172       \leftskip=1em \rightskip=1em%
173       \hsize=\cardwd%
174       %%
175       %% The heading.
176       \hrule height 0pt \prevdepth = 0pt%
177       \medskip%
178       {\large\bfseries\textsf{\keeper} secret #1/\total}%
179       %%
180       %% The QR-code and the text of the secret.
181       \vfil%
182       $%
183       \vcenter{\hbox{\includegraphics[scale = 2.4]{#1.eps}}}%
184       \hfil%
185       \vcenter{\ttfamily%
186         \line#2%
187         \relax\relax\relax\relax\relax\relax\relax\relax%
188       }%
189       $%
190       %%
191       %% And we're done.
192       \vfil%
193     }%
194     &%
195     %%
196     %% Right gap.
197     \cr%
198     %%
199     %% Bottom left guides.
200     \vrule width \guidelen depth \rulewd height 0pt%
201     \vrule width \rulewd depth \guidelen height 0pt%
202     &%
203     %% Bottom centre gap.
204     \hfil%
205     &%
206     %% Bottom right guides.
207     \vrule width \rulewd depth \guidelen height 0pt%
208     \vrule width \guidelen depth \rulewd height 0pt%
209     \cr%
210     %%
211     %% Leave a small vertical space at the bottom to separate lines of cards.
212     \strut \cr%
213   }}%
214   %%
215   %% End material: a stretchy space to match the one at the start, and then
216   %% allow a break.
217   \nobreak\hfil\hbox{}%
218   \penalty0%
219 }
220 EOF
221
222 ## Write the basic configuration stuff.
223 cat >&3 <<EOF
224
225 %% General configuration for the cards.
226 \def\keeper{$keeper}
227 \def\total{$n}
228 EOF
229
230 ## Start the document body.
231 cat >&3 <<'EOF'
232
233 %% The actual content.
234 \begin{document}
235 EOF
236
237 ## Work through the requested indices.
238 i=0
239 while [ $i -lt $n ]; do
240   case $want in
241     *:"$i":*)
242       read secret <$keeper/$i
243       tr -d '\n' <$keeper/$i | qrencode -m0 -s1 -o$tmp/$i.png
244       convert $tmp/$i.png $tmp/$i.eps
245       cat >&3 <<EOF
246 \card{$i}{$secret}
247 EOF
248   esac
249   i=$(( $i + 1 ))
250 done
251
252 ## Wrap up and build the document.
253 cat >&3 <<'EOF'
254 \end{document}
255 EOF
256 exec 3>&-
257 if ! (cd $tmp
258     exec </dev/null >tex.out 2>&1
259     latex $keeper.tex && dvips -o$keeper.ps $keeper.dvi); then
260   echo >&2 "$quis: document formatting failed"
261   sed >&2 's/^/| /' $tmp/tex.out
262   exit 1
263 fi
264 cp $tmp/$keeper.ps .
265
266 ###----- That's all, folks --------------------------------------------------