Module Name:    src
Committed By:   agc
Date:           Mon Oct 12 02:55:46 UTC 2009

Added Files:
        src/crypto/external/bsd/netpgp/dist/bindings/lua: Makefile glue.c
            netpgp.lua optparse.lua

Log Message:
Add lua language bindings for netpgp


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 \
    src/crypto/external/bsd/netpgp/dist/bindings/lua/Makefile \
    src/crypto/external/bsd/netpgp/dist/bindings/lua/glue.c \
    src/crypto/external/bsd/netpgp/dist/bindings/lua/netpgp.lua \
    src/crypto/external/bsd/netpgp/dist/bindings/lua/optparse.lua

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: src/crypto/external/bsd/netpgp/dist/bindings/lua/Makefile
diff -u /dev/null src/crypto/external/bsd/netpgp/dist/bindings/lua/Makefile:1.1
--- /dev/null	Mon Oct 12 02:55:46 2009
+++ src/crypto/external/bsd/netpgp/dist/bindings/lua/Makefile	Mon Oct 12 02:55:46 2009
@@ -0,0 +1,30 @@
+#PREFIX=/Users/agcrooks
+PREFIX=/usr
+
+LIB=luanetpgp
+SRCS=glue.c
+MKMAN=no
+CPPFLAGS+=-g -I${PREFIX}/pkg/include
+WARNS=4
+
+.include <bsd.lib.mk>
+
+OPSYS!= uname -s
+
+.if ${OPSYS} == "Darwin"
+.sinclude <bsd.warns.mk>
+
+lib${LIB}.dylib:
+	libtool -dynamic -o ${.TARGET} ${OBJS} ${PREFIX}/pkg/lib/liblua.dylib /usr/lib/libc.dylib ${PREFIX}/pkg/lib/libnetpgp.dylib
+
+t: lib${LIB}.dylib
+	cp Makefile a
+	./netpgp.lua --sign --detached a
+	./netpgp.lua --verify a.sig
+
+.else
+t:
+	cp Makefile a
+	./netpgp.lua --sign --detached a
+	./netpgp.lua --verify a.sig
+.endif
Index: src/crypto/external/bsd/netpgp/dist/bindings/lua/glue.c
diff -u /dev/null src/crypto/external/bsd/netpgp/dist/bindings/lua/glue.c:1.1
--- /dev/null	Mon Oct 12 02:55:46 2009
+++ src/crypto/external/bsd/netpgp/dist/bindings/lua/glue.c	Mon Oct 12 02:55:46 2009
@@ -0,0 +1,361 @@
+/*-
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Alistair Crooks ([email protected])
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <inttypes.h>
+#include <netpgp.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define LUA_LIB
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+#ifndef __UNCONST
+#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
+#endif /* !__UNCONST */
+
+#define DEFAULT_HASH_ALG        "SHA256"
+
+int luaopen_netpgp(lua_State *);
+
+typedef struct strarg_t {
+	const char	*s;	/* string */
+	const int	 n;	/* corresponding int value */
+} strarg_t;
+
+/* map a string onto an int */
+static int
+findtype(strarg_t *strs, const char *s)
+{
+	strarg_t	*sp;
+
+	for (sp = strs ; sp->s && strcasecmp(sp->s, s) != 0 ; sp++) {
+	}
+	return sp->n;
+}
+
+/* set the home directory value to "home/subdir" */
+static int
+set_homedir(netpgp_t *netpgp, char *home, const char *subdir, const int quiet)
+{
+	struct stat	st;
+	char		d[MAXPATHLEN];
+
+	if (home == NULL) {
+		if (!quiet) {
+			(void) fprintf(stderr, "NULL HOME directory\n");
+		}
+		return 0;
+	}
+	(void) snprintf(d, sizeof(d), "%s%s", home, (subdir) ? subdir : "");
+	if (stat(d, &st) == 0) {
+		if ((st.st_mode & S_IFMT) == S_IFDIR) {
+			netpgp_setvar(netpgp, "homedir", d);
+			return 1;
+		}
+		(void) fprintf(stderr, "netpgp: homedir \"%s\" is not a dir\n",
+					d);
+		return 0;
+	}
+	if (!quiet) {
+		(void) fprintf(stderr,
+			"netpgp: warning homedir \"%s\" not found\n", d);
+	}
+	return 1;
+}
+
+
+/* init() */
+static int
+l_new(lua_State *L)
+{
+	netpgp_t	*netpgp;
+
+	netpgp = lua_newuserdata(L, sizeof(*netpgp));
+	set_homedir(netpgp, getenv("HOME"), "/.gnupg", 1);
+	netpgp_setvar(netpgp, "hash", DEFAULT_HASH_ALG);
+	return 1;
+}
+
+/* initialise(netpgp) */
+static int
+l_init(lua_State *L)
+{
+	netpgp_t	*netpgp;
+
+	netpgp = lua_touserdata(L, 1);
+	lua_pushnumber(L, netpgp_init(netpgp));
+	return 1;
+}
+
+/* homedir(netpgp, homedir) */
+static int
+l_homedir(lua_State *L)
+{
+	const char	*home;
+	netpgp_t	*netpgp;
+
+	netpgp = lua_touserdata(L, 1);
+	home = luaL_checkstring(L, 2);
+	lua_pushnumber(L, set_homedir(netpgp, __UNCONST(home), NULL, 0));
+	return 1;
+}
+
+static strarg_t	armourtypes[] = {
+	{	"armoured",	1	},
+	{	"armored",	1	},
+	{	"armour",	1	},
+	{	"armor",	1	},
+	{	NULL,		0	}
+};
+
+/* encrypt_file(netpgp, f, output, armour) */
+static int
+l_encrypt_file(lua_State *L)
+{
+	const char	*output;
+	const char	*f;
+	netpgp_t	*netpgp;
+	int		 armour;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	netpgp_setvar(netpgp, "need userid", "1");
+	f = luaL_checkstring(L, 2);
+	output = luaL_checkstring(L, 3);
+	if (*output == 0x0) {
+		output = NULL;
+	}
+	armour = findtype(armourtypes, luaL_checkstring(L, 4));
+	ret = netpgp_encrypt_file(netpgp, netpgp_getvar(netpgp, "userid"),
+				f, __UNCONST("a.gpg"), armour);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+/* decrypt_file(netpgp, f, output, armour) */
+static int
+l_decrypt_file(lua_State *L)
+{
+	const char	*output;
+	const char	*f;
+	netpgp_t	*netpgp;
+	int		 armour;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	f = luaL_checkstring(L, 2);
+	output = luaL_checkstring(L, 3);
+	if (*output == 0x0) {
+		output = NULL;
+	}
+	armour = findtype(armourtypes, luaL_checkstring(L, 4));
+	ret = netpgp_decrypt_file(netpgp, f, __UNCONST(output), armour);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+static strarg_t	detachtypes[] = {
+	{	"detached",	1	},
+	{	"separate",	1	},
+	{	"detach",	1	},
+	{	NULL,		0	}
+};
+
+/* sign_file(netpgp, f, output, armour, detached) */
+static int
+l_sign_file(lua_State *L)
+{
+	const char	*output;
+	const char	*f;
+	const int	 binary = 0;
+	netpgp_t	*netpgp;
+	int		 detached;
+	int		 armour;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	netpgp_setvar(netpgp, "need userid", "1");
+	f = luaL_checkstring(L, 2);
+	output = luaL_checkstring(L, 3);
+	if (*output == 0x0) {
+		output = NULL;
+	}
+	armour = findtype(armourtypes, luaL_checkstring(L, 4));
+	detached = findtype(detachtypes, luaL_checkstring(L, 5));
+	ret = netpgp_sign_file(netpgp, netpgp_getvar(netpgp, "userid"),
+				f, __UNCONST(output), armour, binary,
+				detached);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+/* clearsign_file(netpgp, f, output, armour, detached) */
+static int
+l_clearsign_file(lua_State *L)
+{
+	const char	*output;
+	const char	*f;
+	const int	 cleartext = 1;
+	netpgp_t	*netpgp;
+	int		 detached;
+	int		 armour;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	netpgp_setvar(netpgp, "need userid", "1");
+	f = luaL_checkstring(L, 2);
+	output = luaL_checkstring(L, 3);
+	armour = findtype(armourtypes, luaL_checkstring(L, 4));
+	detached = findtype(detachtypes, luaL_checkstring(L, 5));
+	ret = netpgp_sign_file(netpgp, netpgp_getvar(netpgp, "userid"),
+				f, __UNCONST(output), armour, cleartext,
+				detached);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+/* verify_file(netpgp, f, armour) */
+static int
+l_verify_file(lua_State *L)
+{
+	const char	*f;
+	netpgp_t	*netpgp;
+	int		 armour;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	f = luaL_checkstring(L, 2);
+	armour = findtype(armourtypes, luaL_checkstring(L, 3));
+	ret = netpgp_verify_file(netpgp, f, NULL, armour);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+/* verify_cat_file(netpgp, f, output, armour) */
+static int
+l_verify_cat_file(lua_State *L)
+{
+	const char	*output;
+	const char	*f;
+	netpgp_t	*netpgp;
+	int		 armour;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	f = luaL_checkstring(L, 2);
+	output = luaL_checkstring(L, 3);
+	armour = findtype(armourtypes, luaL_checkstring(L, 4));
+	ret = netpgp_verify_file(netpgp, f, output, armour);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+/* list_packets(netpgp, f, armour) */
+static int
+l_list_packets(lua_State *L)
+{
+	const char	*f;
+	netpgp_t	*netpgp;
+	int		 armour;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	f = luaL_checkstring(L, 2);
+	armour = findtype(armourtypes, luaL_checkstring(L, 3));
+	ret = netpgp_list_packets(netpgp, __UNCONST(f), armour, NULL);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+/* setvar(netpgp, name, value) */
+static int
+l_setvar(lua_State *L)
+{
+	const char	*name;
+	const char	*value;
+	netpgp_t	*netpgp;
+	int		 ret;
+
+	netpgp = lua_touserdata(L, 1);
+	name = luaL_checkstring(L, 2);
+	value = luaL_checkstring(L, 3);
+	ret = netpgp_setvar(netpgp, name, value);
+	lua_pushnumber(L, ret);
+	return 1;
+}
+
+/* getvar(netpgp, name, value) */
+static int
+l_getvar(lua_State *L)
+{
+	const char	*name;
+	const char	*ret;
+	netpgp_t	*netpgp;
+
+	netpgp = lua_touserdata(L, 1);
+	name = luaL_checkstring(L, 2);
+	ret = netpgp_getvar(netpgp, name);
+	lua_pushstring(L, ret);
+	return 1;
+}
+
+const struct luaL_reg libnetpgp[] = {
+	{ "new",		l_new },
+	{ "init",		l_init },
+
+	{ "encrypt_file",	l_encrypt_file },
+	{ "decrypt_file",	l_decrypt_file },
+	{ "sign_file",		l_sign_file },
+	{ "clearsign_file",	l_clearsign_file },
+	{ "verify_file",	l_verify_file },
+	{ "verify_cat_file",	l_verify_cat_file },
+
+	{ "list_packets",	l_list_packets },
+
+	{ "getvar",		l_getvar },
+	{ "setvar",		l_setvar },
+
+	{ "homedir",		l_homedir },
+
+	{ NULL,			NULL }
+};
+
+int 
+luaopen_netpgp(lua_State *L)
+{
+	luaL_openlib(L, "netpgp", libnetpgp, 0);
+	return 1;
+}
Index: src/crypto/external/bsd/netpgp/dist/bindings/lua/netpgp.lua
diff -u /dev/null src/crypto/external/bsd/netpgp/dist/bindings/lua/netpgp.lua:1.1
--- /dev/null	Mon Oct 12 02:55:46 2009
+++ src/crypto/external/bsd/netpgp/dist/bindings/lua/netpgp.lua	Mon Oct 12 02:55:46 2009
@@ -0,0 +1,100 @@
+#! /usr/bin/env lua
+
+--
+-- Copyright (c) 2009 The NetBSD Foundation, Inc.
+-- All rights reserved.
+--
+-- This code is derived from software contributed to The NetBSD Foundation
+-- by Alistair Crooks ([email protected])
+--
+-- Redistribution and use in source and binary forms, with or without
+-- modification, are permitted provided that the following conditions
+-- are met:
+-- 1. Redistributions of source code must retain the above copyright
+--    notice, this list of conditions and the following disclaimer.
+-- 2. Redistributions in binary form must reproduce the above copyright
+--    notice, this list of conditions and the following disclaimer in the
+--    documentation and/or other materials provided with the distribution.
+--
+-- THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+-- PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+-- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+-- POSSIBILITY OF SUCH DAMAGE.
+--
+
+-- command line args
+dofile "optparse.lua"
+
+opt = OptionParser{usage="%prog [options] file", version="20090711"}                           
+
+opt.add_option{"-s", "--sign", action="store_true", dest="sign", help="--sign [--detached] [--armour] file"}
+opt.add_option{"-v", "--verify", action="store_true", dest="verify", help="--verify [--armour] file"}
+opt.add_option{"-e", "--encrypt", action="store_true", dest="encrypt", help="--encrypt [--armour] file"}
+opt.add_option{"-d", "--decrypt", action="store_true", dest="decrypt", help="--decrypt [--armour] file"}
+opt.add_option{"-h", "--homedir", action="store", dest="homedir", help="--homedir directory"}
+opt.add_option{"-o", "--output", action="store", dest="output", help="--output file"}
+opt.add_option{"-a", "--armour", action="store_true", dest="armour", help="--armour"}
+opt.add_option{"-D", "--detached", action="store_true", dest="detached", help="--detached"}
+
+-- caller lua script
+local extension = ".so"
+f = io.open("libluanetpgp.dylib", "r")
+if f then
+	extension = ".dylib"
+	io.close(f)
+end
+glupkg = package.loadlib("libluanetpgp" .. extension, "luaopen_netpgp")
+netpgp = glupkg()
+
+-- initialise
+pgp = netpgp.new()
+
+-- parse command line args
+options,args = opt.parse_args()
+
+-- set defaults
+local output = options.output or ""
+local armour = "binary"
+if options.armour then
+	armour = "armour"
+end
+local detached = "attached"
+if options.detached then
+	detached = "detached"
+end
+if options.homedir then
+	netpgp.homedir(netpgp, options.homedir)
+end
+
+-- initialise everything
+netpgp.init(pgp)
+
+local i = 1
+while i <= #args do
+	if options.encrypt then
+		-- encrypt a file
+		netpgp.encrypt_file(pgp, args[1], output, armour)
+		os.execute("ls -l " .. args[1] .. ".gpg")
+	end
+	if options.decrypt then
+		-- decrypt file
+		netpgp.decrypt_file(pgp, args[1], output, armour)
+	end
+	if options.sign then
+		-- detached signature
+		netpgp.sign_file(pgp, args[1], output, armour, detached)
+		os.execute("ls -l " .. args[1] .. ".sig")
+	end
+	if options.verify then
+		-- verification of detached signature
+		netpgp.verify_file(pgp, args[1], armour)
+	end
+	i = i + 1
+end
Index: src/crypto/external/bsd/netpgp/dist/bindings/lua/optparse.lua
diff -u /dev/null src/crypto/external/bsd/netpgp/dist/bindings/lua/optparse.lua:1.1
--- /dev/null	Mon Oct 12 02:55:46 2009
+++ src/crypto/external/bsd/netpgp/dist/bindings/lua/optparse.lua	Mon Oct 12 02:55:46 2009
@@ -0,0 +1,123 @@
+-- Lua command line option parser.
+-- Interface based on Pythons optparse.
+-- http://docs.python.org/lib/module-optparse.html
+-- (c) 2008 David Manura, Licensed under the same terms as Lua (MIT license)
+--
+-- To be used like this:                                                                  
+-- t={usage="<some usage message>", version="<version string>"}                           
+-- op=OptionParser(t)                                                                     
+-- op=add_option{"<opt>", action=<action>, dest=<dest>, help="<help message for this option>"}
+--
+-- with :
+--   <opt> the option string to be used (can be anything, if one letter opt, then should be -x val, more letters: -xy=val )
+--   <action> one of
+--   - store: store in options as key, val                                                  
+--   - store_true: stores key, true                                                         
+--   - store_false: stores key, false
+--   <dest> is the key under which the option is saved
+--                                      
+-- options,args = op.parse_args()
+--
+-- now options is the table of options (key, val) and args is the table with non-option arguments.
+-- You can use op.fail(message) for failing and op.print_help() for printing the usage as you like.
+
+function OptionParser(t)
+  local usage = t.usage
+  local version = t.version
+
+  local o = {}
+  local option_descriptions = {}
+  local option_of = {}
+
+  function o.fail(s) -- extension
+    io.stderr:write(s .. '\n')
+    os.exit(1)
+  end
+
+  function o.add_option(optdesc)
+    option_descriptions[#option_descriptions+1] = optdesc
+    for _,v in ipairs(optdesc) do
+      option_of[v] = optdesc
+    end
+  end
+  function o.parse_args()
+    -- expand options (e.g. "--input=file" -> "--input", "file")
+    local arg = {unpack(arg)}
+    for i=#arg,1,-1 do local v = arg[i]
+      local flag, val = v:match('^(%-%-%w+)=(.*)')
+      if flag then
+        arg[i] = flag
+        table.insert(arg, i+1, val)
+      end
+    end
+
+    local options = {}
+    local args = {}
+    local i = 1
+    while i <= #arg do local v = arg[i]
+      local optdesc = option_of[v]
+      if optdesc then
+        local action = optdesc.action
+        local val
+        if action == 'store' or action == nil then
+          i = i + 1
+          val = arg[i]
+          if not val then o.fail('option requires an argument ' .. v) end
+        elseif action == 'store_true' then
+          val = true
+        elseif action == 'store_false' then
+          val = false
+        end
+        options[optdesc.dest] = val
+      else
+        if v:match('^%-') then o.fail('invalid option ' .. v) end
+        args[#args+1] = v
+      end
+      i = i + 1
+    end
+    if options.help then
+      o.print_help()
+      os.exit()
+    end
+    if options.version then
+      io.stdout:write(t.version .. "\n")
+      os.exit()
+    end
+    return options, args
+  end
+
+  local function flags_str(optdesc)
+    local sflags = {}
+    local action = optdesc.action
+    for _,flag in ipairs(optdesc) do
+      local sflagend
+      if action == nil or action == 'store' then
+        local metavar = optdesc.metavar or optdesc.dest:upper()
+        sflagend = #flag == 2 and ' ' .. metavar
+                              or  '=' .. metavar
+      else
+        sflagend = ''
+      end
+      sflags[#sflags+1] = flag .. sflagend
+    end
+    return table.concat(sflags, ', ')
+  end
+
+  function o.print_help()
+    io.stdout:write("Usage: " .. usage:gsub('%%prog', arg[0]) .. "\n")
+    io.stdout:write("\n")
+    io.stdout:write("Options:\n")
+    for _,optdesc in ipairs(option_descriptions) do
+      io.stdout:write("  " .. flags_str(optdesc) ..
+                      "  " .. optdesc.help .. "\n")
+    end
+  end
+  o.add_option{"--help", action="store_true", dest="help",
+               help="show this help message and exit"}
+  if t.version then
+    o.add_option{"--version", action="store_true", dest="version",
+                 help="output version info."}
+  end
+  return o
+end
+

Reply via email to