From ac1aec3ac249a94049d42d3d75005857e74d62d2 Mon Sep 17 00:00:00 2001
Message-Id: <ac1aec3ac249a94049d42d3d75005857e74d62d2.1715097779.git.mdw@distorted.org.uk>
From: Mark Wooding <mdw@chiark.greenend.org.uk>
Date: Thu, 23 Feb 2012 03:05:45 +0000
Subject: [PATCH] keys.keeper-cards: Allow output as TeX source.
Organization: Straylight/Edgeware

From: Mark Wooding <mdw@distorted.org.uk>

This still requires `qrencode', but doesn't require a huge TeX
installation.  Leave TeX recommended, but promote `qrencode' to a
dependency.
---
 Makefile.am                   |   3 +
 debian/control                |   4 +-
 debian/distorted-keys.install |   1 +
 keeper-cards.tex              | 162 ++++++++++++++++++++++++++++
 keys.keeper-cards             | 197 +++++++---------------------------
 5 files changed, 207 insertions(+), 160 deletions(-)
 create mode 100644 keeper-cards.tex

diff --git a/Makefile.am b/Makefile.am
index 1bf380f..9948953 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -125,6 +125,9 @@ EXTRA_DIST		+= keys.conf
 KEYS_CONFIG		+= admin.users admin.groups
 EXTRA_DIST		+= admin.users admin.groups
 
+KEYS_CONFIG		+= keeper-cards.tex
+EXTRA_DIST		+= keeper-cards.tex
+
 ###--------------------------------------------------------------------------
 ### Crypto operations.
 
diff --git a/debian/control b/debian/control
index 892543c..5b48960 100644
--- a/debian/control
+++ b/debian/control
@@ -19,8 +19,8 @@ Description: Underlying machinery for distorted.org.uk key-management system.
 
 Package: distorted-keys
 Architecture: all
-Depends: distorted-keys-base, python (>= 2.5), userv, adduser
-Suggests: texlive-latex-recommended, qrencode
+Depends: distorted-keys-base, python (>= 2.5), userv, adduser, qrencode
+Suggests: texlive-latex-recommended
 Description: Basic key-management system with secure recovery features.
  The primary purpose of the distorted.org.uk key management system is
  to provide a secure way of recovering important cryptographic keys,
diff --git a/debian/distorted-keys.install b/debian/distorted-keys.install
index 3098aa4..e1c761b 100644
--- a/debian/distorted-keys.install
+++ b/debian/distorted-keys.install
@@ -6,4 +6,5 @@ usr/lib/distorted-keys/keys.*
 usr/lib/distorted-keys/cryptop.*
 etc/userv/default.d/distorted-keys
 etc/distorted-keys/admin.*
+etc/distorted-keys/keeper-cards.tex
 etc/distorted-keys/profile.d
diff --git a/keeper-cards.tex b/keeper-cards.tex
new file mode 100644
index 0000000..106a196
--- /dev/null
+++ b/keeper-cards.tex
@@ -0,0 +1,162 @@
+%%% -*-latex-*-
+%%%
+%%% Typeset cards for keeper secrets.
+%%%
+%%% (c) 2012 Mark Wooding
+%%%
+
+%%%----- Licensing notice ---------------------------------------------------
+%%%
+%%% This file is part of the distorted.org.uk key management suite.
+%%%
+%%% distorted-keys is free software; you can redistribute it and/or modify
+%%% it under the terms of the GNU General Public License as published by
+%%% the Free Software Foundation; either version 2 of the License, or
+%%% (at your option) any later version.
+%%%
+%%% distorted-keys is distributed in the hope that it will be useful,
+%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
+%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+%%% GNU General Public License for more details.
+%%%
+%%% You should have received a copy of the GNU General Public License
+%%% along with distorted-keys; if not, write to the Free Software Foundation,
+%%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+\documentclass[a4paper, landscape, 12pt]{article}
+\usepackage[utf8]{inputenc}
+\usepackage[T1]{fontenc}
+%%\usepackage[palatino, helvetica, courier, maths = cmr]{mdwfonts}
+\usepackage{graphicx}
+
+%% Report errors with enough context that we can debug them.
+\errorcontextlines=999
+
+%% Basic layout for the cards.  We use the paragraph filling machinery, but
+%% don't actually need most of the trimmings.
+\parindent=0pt
+\parfillskip=0pt
+\pagestyle{empty}
+
+%% Page layout: try to use most of the page.  The document class will already
+%% have set up the paper size, but we do the rest here.
+\hoffset=-1in \voffset=-1in
+\oddsidemargin=20mm
+\textwidth=\paperwidth \advance\textwidth by -2\oddsidemargin
+\topmargin=20mm
+\headheight=0pt \headsep=0pt
+\textheight=\paperheight \advance\textheight by -2\topmargin
+\AtBeginDocument{\special{papersize=\the\paperwidth,\the\paperheight}}
+
+%% Parameters for the cards and guide rules.
+\newdimen\cardwd \cardwd=82mm
+\newdimen\cardht \cardht=49mm
+\newdimen\guidelen \guidelen=10mm
+\newdimen\rulewd \rulewd=0.6pt
+
+%% Typesetting the secret as text.  The macro \snarf TOKEN T0 T1 ... T7
+%% gathers T0 T1 ... T7 into a single argument and passes them to TOKEN, as
+%% long as T0 is not \relax.  We use this to process the secret text in a
+%% continuation-passing style.
+\def\snarf#1#2{%
+  \ifx#2\relax\let\next\empty%
+  \else\def\next{\snarfdo#1#2}%
+  \fi%
+  \next%
+}
+\def\snarfdo#1#2#3#4#5#6#7#8#9{#1{#2#3#4#5#6#7#8#9}}
+
+%% Print the left and right halves of the line, with a separator.  Use boxes
+%% for the lines so that TeX will work out the width of the enclosing vbox
+%% for us.  The basic usage is \line TEXT \relax ... \relax, with eight
+%% \relax tokens: this is enough to complete both \snarf calls.
+\def\line{\snarf\lineleft}
+\def\lineleft#1{\hbox\bgroup#1 \snarf\lineright}
+\def\lineright#1{#1\egroup\line}
+
+%% Typeset a card containing a secret.  Usage is \card{INDEX}{SECRET}.
+\def\card#1#2{%
+  %%
+  %% Make sure we're setting a paragraph.
+  \leavevmode%
+  %%
+  %% Initial material: a stretchy space on the left.
+  \hbox{}\nobreak\hfil%
+  %%
+  %% An alignment for the guide markers surrounding the actual card.
+  \vbox{\halign{&##\cr%
+    %%
+    %% Top left guides.
+    \vrule width \guidelen height \rulewd depth 0pt%
+    \vrule width \rulewd depth 0pt height \guidelen%
+    &%
+    %%
+    %% Top centre gap.
+    \hfil%
+    &%
+    %%
+    %% Top right guides.
+    \vrule width \rulewd depth 0pt height \guidelen%
+    \vrule width \guidelen height \rulewd depth 0pt%
+    \cr%
+    %%
+    %% Left gap.
+    &%
+    %%
+    %% The actual card.
+    \vbox to \cardht{%
+      %%
+      %% We actually do more or less sensible typesetting.  TeX will set the
+      %% box width from the hsize, and we should leave a small margin all
+      %% around.
+      \parfillskip=0pt plus 1fil%
+      \leftskip=1em \rightskip=1em%
+      \hsize=\cardwd%
+      %%
+      %% The heading.
+      \hrule height 0pt \prevdepth = 0pt%
+      \medskip%
+      {\large\bfseries\textsf{\keeper} secret #1/\total}%
+      %%
+      %% The QR-code and the text of the secret.
+      \vfil%
+      $%
+      \vcenter{\hbox{\includegraphics[scale = 2.4]{#1.eps}}}%
+      \hfil%
+      \vcenter{\ttfamily%
+        \line#2%
+        \relax\relax\relax\relax\relax\relax\relax\relax%
+      }%
+      $%
+      %%
+      %% And we're done.
+      \vfil%
+    }%
+    &%
+    %%
+    %% Right gap.
+    \cr%
+    %%
+    %% Bottom left guides.
+    \vrule width \guidelen depth \rulewd height 0pt%
+    \vrule width \rulewd depth \guidelen height 0pt%
+    &%
+    %% Bottom centre gap.
+    \hfil%
+    &%
+    %% Bottom right guides.
+    \vrule width \rulewd depth \guidelen height 0pt%
+    \vrule width \guidelen depth \rulewd height 0pt%
+    \cr%
+    %%
+    %% Leave a small vertical space at the bottom to separate lines of cards.
+    \strut \cr%
+  }}%
+  %%
+  %% End material: a stretchy space to match the one at the start, and then
+  %% allow a break.
+  \nobreak\hfil\hbox{}%
+  \penalty0%
+}
+
+%%%----- That's all, folks --------------------------------------------------
diff --git a/keys.keeper-cards b/keys.keeper-cards
index 01c1444..5ae1ae1 100755
--- a/keys.keeper-cards
+++ b/keys.keeper-cards
@@ -28,21 +28,36 @@ case "${KEYSLIB+t}" in t) ;; *) echo >&2 "$0: KEYSLIB unset"; exit 1 ;; esac
 . "$KEYSLIB"/keyfunc.sh
 
 defhelp <<HELP
-KEEPER [INDICES ...]
+[-pt] KEEPER [INDICES ...]
 Typeset cards for a set of keeper secrets.
 
-This program writes a PostScript file to standard output which will contain
-key nubs from the keeper set KEEPER, specifically the keys with the given
-INDICES.  Elements of the list are either simple integers or ranges
-[LOW]-[HIGH]; if LOW is omitted, it means 0, and if HIGH is omitted, it means
-the highest possible index.  If no INDICES are given then all secret keys are
-written.
+This program creates a set of printable cards containing key nubs from the
+keeper set KEEPER, specifically the keys with the given INDICES.  Elements of
+the list are either simple integers or ranges [LOW]-[HIGH]; if LOW is
+omitted, it means 0, and if HIGH is omitted, it means the highest possible
+index.  If no INDICES are given then all secret keys are written.
 
 The public keys are found in $KEYS/keeper/KEEPER/I.pub;
 key nubs are read from the safe place where \`keys new-keeper' left
 them.
+
+If the \`-p' option is given, then the output is a PostScript file which
+can be printed immediately.  If the \`-t' option is given, then the output
+is a \`tar' archive containing a TeX source file and images, which must
+be processed using \`latex' and \`dvips'.  The default is \`-t'.
 HELP
 
+## Read options.
+mode=tar
+while getopts "pt" opt; do
+  case "$opt" in
+    p) mode=ps ;;
+    t) mode=tar ;;
+    *) usage_err ;;
+  esac
+done
+shift $(( $OPTIND - 1 ))
+
 ## Parse the command line.
 case $# in 0) usage_err ;; esac
 keeper=$1; shift
@@ -97,149 +112,7 @@ done
 mktmp
 umask 077
 exec 3>$tmp/$keeper.tex
-cat >&3 <<'EOF'
-\documentclass[a4paper, landscape, 12pt]{article}
-\usepackage[utf8]{inputenc}
-\usepackage[T1]{fontenc}
-EOF
-if kpsewhich mdwfonts.sty >/dev/null; then
-  cat >&3 <<'EOF'
-\usepackage[palatino, helvetica, courier, maths = cmr]{mdwfonts}
-EOF
-fi
-cat >&3 <<'EOF'
-\usepackage{graphicx}
-
-%% Report errors with enough context that we can debug them.
-\errorcontextlines=999
-
-%% Basic layout for the cards.  We use the paragraph filling machinery, but
-%% don't actually need most of the trimmings.
-\parindent=0pt
-\parfillskip=0pt
-\pagestyle{empty}
-
-%% Page layout: try to use most of the page.  The document class will already
-%% have set up the paper size, but we do the rest here.
-\hoffset=-1in \voffset=-1in
-\oddsidemargin=20mm
-\textwidth=\paperwidth \advance\textwidth by -2\oddsidemargin
-\topmargin=20mm
-\headheight=0pt \headsep=0pt
-\textheight=\paperheight \advance\textheight by -2\topmargin
-\AtBeginDocument{\special{papersize=\the\paperwidth,\the\paperheight}}
-
-%% Parameters for the cards and guide rules.
-\newdimen\cardwd \cardwd=82mm
-\newdimen\cardht \cardht=49mm
-\newdimen\guidelen \guidelen=10mm
-\newdimen\rulewd \rulewd=0.6pt
-
-%% Typesetting the secret as text.  The macro \snarf TOKEN T0 T1 ... T7
-%% gathers T0 T1 ... T7 into a single argument and passes them to TOKEN, as
-%% long as T0 is not \relax.  We use this to process the secret text in a
-%% continuation-passing style.
-\def\snarf#1#2{%
-  \ifx#2\relax\let\next\empty%
-  \else\def\next{\snarfdo#1#2}%
-  \fi%
-  \next%
-}
-\def\snarfdo#1#2#3#4#5#6#7#8#9{#1{#2#3#4#5#6#7#8#9}}
-
-%% Print the left and right halves of the line, with a separator.  Use boxes
-%% for the lines so that TeX will work out the width of the enclosing vbox
-%% for us.  The basic usage is \line TEXT \relax ... \relax, with eight
-%% \relax tokens: this is enough to complete both \snarf calls.
-\def\line{\snarf\lineleft}
-\def\lineleft#1{\hbox\bgroup#1 \snarf\lineright}
-\def\lineright#1{#1\egroup\line}
-
-%% Typeset a card containing a secret.  Usage is \card{INDEX}{SECRET}.
-\def\card#1#2{%
-  %%
-  %% Make sure we're setting a paragraph.
-  \leavevmode%
-  %%
-  %% Initial material: a stretchy space on the left.
-  \hbox{}\nobreak\hfil%
-  %%
-  %% An alignment for the guide markers surrounding the actual card.
-  \vbox{\halign{&##\cr%
-    %%
-    %% Top left guides.
-    \vrule width \guidelen height \rulewd depth 0pt%
-    \vrule width \rulewd depth 0pt height \guidelen%
-    &%
-    %%
-    %% Top centre gap.
-    \hfil%
-    &%
-    %%
-    %% Top right guides.
-    \vrule width \rulewd depth 0pt height \guidelen%
-    \vrule width \guidelen height \rulewd depth 0pt%
-    \cr%
-    %%
-    %% Left gap.
-    &%
-    %%
-    %% The actual card.
-    \vbox to \cardht{%
-      %%
-      %% We actually do more or less sensible typesetting.  TeX will set the
-      %% box width from the hsize, and we should leave a small margin all
-      %% around.
-      \parfillskip=0pt plus 1fil%
-      \leftskip=1em \rightskip=1em%
-      \hsize=\cardwd%
-      %%
-      %% The heading.
-      \hrule height 0pt \prevdepth = 0pt%
-      \medskip%
-      {\large\bfseries\textsf{\keeper} secret #1/\total}%
-      %%
-      %% The QR-code and the text of the secret.
-      \vfil%
-      $%
-      \vcenter{\hbox{\includegraphics[scale = 2.4]{#1.eps}}}%
-      \hfil%
-      \vcenter{\ttfamily%
-	\line#2%
-	\relax\relax\relax\relax\relax\relax\relax\relax%
-      }%
-      $%
-      %%
-      %% And we're done.
-      \vfil%
-    }%
-    &%
-    %%
-    %% Right gap.
-    \cr%
-    %%
-    %% Bottom left guides.
-    \vrule width \guidelen depth \rulewd height 0pt%
-    \vrule width \rulewd depth \guidelen height 0pt%
-    &%
-    %% Bottom centre gap.
-    \hfil%
-    &%
-    %% Bottom right guides.
-    \vrule width \rulewd depth \guidelen height 0pt%
-    \vrule width \guidelen depth \rulewd height 0pt%
-    \cr%
-    %%
-    %% Leave a small vertical space at the bottom to separate lines of cards.
-    \strut \cr%
-  }}%
-  %%
-  %% End material: a stretchy space to match the one at the start, and then
-  %% allow a break.
-  \nobreak\hfil\hbox{}%
-  \penalty0%
-}
-EOF
+cat >&3 $ETC/keeper-cards.tex
 
 ## Write the basic configuration stuff.
 cat >&3 <<EOF
@@ -276,13 +149,21 @@ cat >&3 <<'EOF'
 \end{document}
 EOF
 exec 3>&-
-if ! (cd $tmp
-    exec </dev/null >tex.out 2>&1
-    latex $keeper.tex && dvips -o$keeper.ps $keeper.dvi); then
-  echo >&2 "$quis: document formatting failed"
-  sed >&2 's/^/| /' $tmp/tex.out
-  exit 1
-fi
-cat $tmp/$keeper.ps
+
+case $mode in
+  ps)
+    if ! (cd $tmp
+	exec </dev/null >tex.out 2>&1
+	latex $keeper.tex && dvips -o$keeper.ps $keeper.dvi); then
+      echo >&2 "$quis: document formatting failed"
+      sed >&2 's/^/| /' $tmp/tex.out
+      exit 1
+    fi
+    cat $tmp/$keeper.ps
+    ;;
+  tar)
+    (cd $tmp; tar cf - $keeper.tex *.eps)
+    ;;
+esac
 
 ###----- That's all, folks --------------------------------------------------
-- 
[mdw]

