Imported Upstream version 3.1.0
[debian/amanda] / common-src / amcrypt-ossl.sh
1 #!@SHELL@
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 #
17 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
18 #
19 # Permission to use, copy, modify, distribute, and sell this software and its
20 # documentation for any purpose is hereby granted without fee, provided that
21 # the above copyright notice appear in all copies and that both that
22 # copyright notice and this permission notice appear in supporting
23 # documentation, and that the name of U.M. not be used in advertising or
24 # publicity pertaining to distribution of the software without specific,
25 # written prior permission.  U.M. makes no representations about the
26 # suitability of this software for any purpose.  It is provided "as is"
27 # without express or implied warranty.
28 #
29 # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
30 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
31 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
33 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
34 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 #
36 # Copyright (c) 2006  Ben Slusky <sluskyb@paranoiacs.org>
37
38
39 # amcrypt-ossl.sh - crypto helper using OpenSSL
40 # Usage: amcrypt-ossl.sh [-d]
41 #
42
43 prefix="@prefix@"
44 exec_prefix="@exec_prefix@"
45 sbindir="@sbindir@"
46 amlibexecdir="@amlibexecdir@"
47 . "${amlibexecdir}/amanda-sh-lib.sh"
48
49 # change these as needed
50 OPENSSL=                        # whatever's in $PATH
51 CIPHER=aes-256-cbc              # see `openssl help` for more ciphers
52 AMANDA_HOME=~@CLIENT_LOGIN@
53 RANDFILE=$AMANDA_HOME/.rnd
54 export RANDFILE
55 PASSPHRASE=$AMANDA_HOME/.am_passphrase  # required
56
57 # where might openssl be?
58 PATH=/bin:/usr/bin:/usr/local/bin:/usr/ssl/bin:/usr/local/ssl/bin:/opt/csw/bin
59 export PATH
60 ME=`basename "$0"`
61
62 if [ -z "${OPENSSL:=`which openssl`}" ]; then
63         echo `_ '%s: openssl not found' "${ME}"` >&2
64         exit 1
65 elif [ ! -x "${OPENSSL}" ]; then
66         echo `_ "%s: can't execute %s (%s)" "${ME}" "openssl" "${OPENSSL}"` >&2
67         exit 1
68 fi
69
70 # we'll need to pad the datastream to a multiple of the cipher block size prior
71 # to encryption. 96 bytes (= 768 bits) should be good for any cipher.
72 pad() {
73         perl -pe 'BEGIN { $bs = 96; $/ = \8192 } $nbytes = ($nbytes + length) % $bs; END { print "\0" x ($bs - $nbytes) }'
74 }
75
76 if [ "$1" = -d ]; then
77         # decrypt
78         "${OPENSSL}" enc -d "-${CIPHER}" -nopad -salt -pass fd:3 3< "${PASSPHRASE}"
79 else
80         # encrypt
81         pad | "${OPENSSL}" enc -e "-${CIPHER}" -nopad -salt -pass fd:3 3< "${PASSPHRASE}"
82 fi
83