xref: /minix3/crypto/external/bsd/netpgp/dist/bindings/lua/netpgp.lua (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc#! /usr/bin/env lua
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc--
4*ebfedea0SLionel Sambuc-- Copyright (c) 2009 The NetBSD Foundation, Inc.
5*ebfedea0SLionel Sambuc-- All rights reserved.
6*ebfedea0SLionel Sambuc--
7*ebfedea0SLionel Sambuc-- This code is derived from software contributed to The NetBSD Foundation
8*ebfedea0SLionel Sambuc-- by Alistair Crooks (agc@netbsd.org)
9*ebfedea0SLionel Sambuc--
10*ebfedea0SLionel Sambuc-- Redistribution and use in source and binary forms, with or without
11*ebfedea0SLionel Sambuc-- modification, are permitted provided that the following conditions
12*ebfedea0SLionel Sambuc-- are met:
13*ebfedea0SLionel Sambuc-- 1. Redistributions of source code must retain the above copyright
14*ebfedea0SLionel Sambuc--    notice, this list of conditions and the following disclaimer.
15*ebfedea0SLionel Sambuc-- 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc--    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc--    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc--
19*ebfedea0SLionel Sambuc-- THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*ebfedea0SLionel Sambuc-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*ebfedea0SLionel Sambuc-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*ebfedea0SLionel Sambuc-- PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*ebfedea0SLionel Sambuc-- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*ebfedea0SLionel Sambuc-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*ebfedea0SLionel Sambuc-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*ebfedea0SLionel Sambuc-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*ebfedea0SLionel Sambuc-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*ebfedea0SLionel Sambuc-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*ebfedea0SLionel Sambuc-- POSSIBILITY OF SUCH DAMAGE.
30*ebfedea0SLionel Sambuc--
31*ebfedea0SLionel Sambuc
32*ebfedea0SLionel Sambuc-- command line args
33*ebfedea0SLionel Sambucdofile "optparse.lua"
34*ebfedea0SLionel Sambuc
35*ebfedea0SLionel Sambucopt = OptionParser{usage="%prog [options] file", version="20090711"}
36*ebfedea0SLionel Sambuc
37*ebfedea0SLionel Sambucopt.add_option{"-s", "--sign", action="store_true", dest="sign", help="--sign [--detached] [--armour] file"}
38*ebfedea0SLionel Sambucopt.add_option{"-v", "--verify", action="store_true", dest="verify", help="--verify [--armour] file"}
39*ebfedea0SLionel Sambucopt.add_option{"-e", "--encrypt", action="store_true", dest="encrypt", help="--encrypt [--armour] file"}
40*ebfedea0SLionel Sambucopt.add_option{"-d", "--decrypt", action="store_true", dest="decrypt", help="--decrypt [--armour] file"}
41*ebfedea0SLionel Sambucopt.add_option{"-h", "--homedir", action="store", dest="homedir", help="--homedir directory"}
42*ebfedea0SLionel Sambucopt.add_option{"-o", "--output", action="store", dest="output", help="--output file"}
43*ebfedea0SLionel Sambucopt.add_option{"-a", "--armour", action="store_true", dest="armour", help="--armour"}
44*ebfedea0SLionel Sambucopt.add_option{"-D", "--detached", action="store_true", dest="detached", help="--detached"}
45*ebfedea0SLionel Sambuc
46*ebfedea0SLionel Sambuc-- caller lua script
47*ebfedea0SLionel Sambuclocal extension = ".so"
48*ebfedea0SLionel Sambucf = io.open("libluanetpgp.dylib", "r")
49*ebfedea0SLionel Sambucif f then
50*ebfedea0SLionel Sambuc	extension = ".dylib"
51*ebfedea0SLionel Sambuc	io.close(f)
52*ebfedea0SLionel Sambucend
53*ebfedea0SLionel Sambucglupkg = package.loadlib("libluanetpgp" .. extension, "luaopen_netpgp")
54*ebfedea0SLionel Sambucnetpgp = glupkg()
55*ebfedea0SLionel Sambuc
56*ebfedea0SLionel Sambuc-- initialise
57*ebfedea0SLionel Sambucpgp = netpgp.new()
58*ebfedea0SLionel Sambuc
59*ebfedea0SLionel Sambuc-- parse command line args
60*ebfedea0SLionel Sambucoptions,args = opt.parse_args()
61*ebfedea0SLionel Sambuc
62*ebfedea0SLionel Sambuc-- set defaults
63*ebfedea0SLionel Sambuclocal output = options.output or ""
64*ebfedea0SLionel Sambuclocal armour = "binary"
65*ebfedea0SLionel Sambucif options.armour then
66*ebfedea0SLionel Sambuc	armour = "armour"
67*ebfedea0SLionel Sambucend
68*ebfedea0SLionel Sambuclocal detached = "attached"
69*ebfedea0SLionel Sambucif options.detached then
70*ebfedea0SLionel Sambuc	detached = "detached"
71*ebfedea0SLionel Sambucend
72*ebfedea0SLionel Sambucif options.homedir then
73*ebfedea0SLionel Sambuc	netpgp.homedir(pgp, options.homedir)
74*ebfedea0SLionel Sambucend
75*ebfedea0SLionel Sambuc
76*ebfedea0SLionel Sambuc-- initialise everything
77*ebfedea0SLionel Sambucnetpgp.init(pgp)
78*ebfedea0SLionel Sambuc
79*ebfedea0SLionel Sambuclocal i
80*ebfedea0SLionel Sambucfor i = 1, #args do
81*ebfedea0SLionel Sambuc	if options.encrypt then
82*ebfedea0SLionel Sambuc		-- encrypt a file
83*ebfedea0SLionel Sambuc		netpgp.encrypt_file(pgp, args[1], output, armour)
84*ebfedea0SLionel Sambuc		os.execute("ls -l " .. args[1] .. ".gpg")
85*ebfedea0SLionel Sambuc	end
86*ebfedea0SLionel Sambuc	if options.decrypt then
87*ebfedea0SLionel Sambuc		-- decrypt file
88*ebfedea0SLionel Sambuc		netpgp.decrypt_file(pgp, args[1], output, armour)
89*ebfedea0SLionel Sambuc	end
90*ebfedea0SLionel Sambuc	if options.sign then
91*ebfedea0SLionel Sambuc		-- detached signature
92*ebfedea0SLionel Sambuc		netpgp.sign_file(pgp, args[1], output, armour, detached)
93*ebfedea0SLionel Sambuc		os.execute("ls -l " .. args[1] .. ".sig")
94*ebfedea0SLionel Sambuc	end
95*ebfedea0SLionel Sambuc	if options.verify then
96*ebfedea0SLionel Sambuc		-- verification of detached signature
97*ebfedea0SLionel Sambuc		netpgp.verify_file(pgp, args[1], armour)
98*ebfedea0SLionel Sambuc	end
99*ebfedea0SLionel Sambucend
100