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