xref: /minix3/crypto/external/bsd/netpgp/dist/bindings/lua/glue.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*-
2*ebfedea0SLionel Sambuc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
3*ebfedea0SLionel Sambuc  * All rights reserved.
4*ebfedea0SLionel Sambuc  *
5*ebfedea0SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
6*ebfedea0SLionel Sambuc  * by Alistair Crooks (agc@netbsd.org)
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
15*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
16*ebfedea0SLionel Sambuc  *
17*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18*ebfedea0SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19*ebfedea0SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20*ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21*ebfedea0SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*ebfedea0SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*ebfedea0SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*ebfedea0SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
28*ebfedea0SLionel Sambuc  */
29*ebfedea0SLionel Sambuc #include <sys/types.h>
30*ebfedea0SLionel Sambuc #include <sys/param.h>
31*ebfedea0SLionel Sambuc #include <sys/stat.h>
32*ebfedea0SLionel Sambuc 
33*ebfedea0SLionel Sambuc #include <inttypes.h>
34*ebfedea0SLionel Sambuc #include <netpgp.h>
35*ebfedea0SLionel Sambuc #include <string.h>
36*ebfedea0SLionel Sambuc #include <stdio.h>
37*ebfedea0SLionel Sambuc #include <stdlib.h>
38*ebfedea0SLionel Sambuc #include <unistd.h>
39*ebfedea0SLionel Sambuc 
40*ebfedea0SLionel Sambuc #define LUA_LIB
41*ebfedea0SLionel Sambuc #include <lua.h>
42*ebfedea0SLionel Sambuc #include <lauxlib.h>
43*ebfedea0SLionel Sambuc #include <lualib.h>
44*ebfedea0SLionel Sambuc 
45*ebfedea0SLionel Sambuc #ifndef __UNCONST
46*ebfedea0SLionel Sambuc #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
47*ebfedea0SLionel Sambuc #endif /* !__UNCONST */
48*ebfedea0SLionel Sambuc 
49*ebfedea0SLionel Sambuc #define DEFAULT_HASH_ALG        "SHA256"
50*ebfedea0SLionel Sambuc 
51*ebfedea0SLionel Sambuc int luaopen_netpgp(lua_State *);
52*ebfedea0SLionel Sambuc 
53*ebfedea0SLionel Sambuc typedef struct strarg_t {
54*ebfedea0SLionel Sambuc 	const char	*s;	/* string */
55*ebfedea0SLionel Sambuc 	const int	 n;	/* corresponding int value */
56*ebfedea0SLionel Sambuc } strarg_t;
57*ebfedea0SLionel Sambuc 
58*ebfedea0SLionel Sambuc /* map a string onto an int */
59*ebfedea0SLionel Sambuc static int
findtype(strarg_t * strs,const char * s)60*ebfedea0SLionel Sambuc findtype(strarg_t *strs, const char *s)
61*ebfedea0SLionel Sambuc {
62*ebfedea0SLionel Sambuc 	strarg_t	*sp;
63*ebfedea0SLionel Sambuc 
64*ebfedea0SLionel Sambuc 	for (sp = strs ; sp->s && strcasecmp(sp->s, s) != 0 ; sp++) {
65*ebfedea0SLionel Sambuc 	}
66*ebfedea0SLionel Sambuc 	return sp->n;
67*ebfedea0SLionel Sambuc }
68*ebfedea0SLionel Sambuc 
69*ebfedea0SLionel Sambuc /* set the home directory value to "home/subdir" */
70*ebfedea0SLionel Sambuc static int
set_homedir(netpgp_t * netpgp,char * home,const char * subdir,const int quiet)71*ebfedea0SLionel Sambuc set_homedir(netpgp_t *netpgp, char *home, const char *subdir, const int quiet)
72*ebfedea0SLionel Sambuc {
73*ebfedea0SLionel Sambuc 	struct stat	st;
74*ebfedea0SLionel Sambuc 	char		d[MAXPATHLEN];
75*ebfedea0SLionel Sambuc 
76*ebfedea0SLionel Sambuc 	if (home == NULL) {
77*ebfedea0SLionel Sambuc 		if (!quiet) {
78*ebfedea0SLionel Sambuc 			(void) fprintf(stderr, "NULL HOME directory\n");
79*ebfedea0SLionel Sambuc 		}
80*ebfedea0SLionel Sambuc 		return 0;
81*ebfedea0SLionel Sambuc 	}
82*ebfedea0SLionel Sambuc 	(void) snprintf(d, sizeof(d), "%s%s", home, (subdir) ? subdir : "");
83*ebfedea0SLionel Sambuc 	if (stat(d, &st) == 0) {
84*ebfedea0SLionel Sambuc 		if ((st.st_mode & S_IFMT) == S_IFDIR) {
85*ebfedea0SLionel Sambuc 			netpgp_setvar(netpgp, "homedir", d);
86*ebfedea0SLionel Sambuc 			return 1;
87*ebfedea0SLionel Sambuc 		}
88*ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "netpgp: homedir \"%s\" is not a dir\n",
89*ebfedea0SLionel Sambuc 					d);
90*ebfedea0SLionel Sambuc 		return 0;
91*ebfedea0SLionel Sambuc 	}
92*ebfedea0SLionel Sambuc 	if (!quiet) {
93*ebfedea0SLionel Sambuc 		(void) fprintf(stderr,
94*ebfedea0SLionel Sambuc 			"netpgp: warning homedir \"%s\" not found\n", d);
95*ebfedea0SLionel Sambuc 	}
96*ebfedea0SLionel Sambuc 	return 1;
97*ebfedea0SLionel Sambuc }
98*ebfedea0SLionel Sambuc 
99*ebfedea0SLionel Sambuc 
100*ebfedea0SLionel Sambuc /* init() */
101*ebfedea0SLionel Sambuc static int
l_new(lua_State * L)102*ebfedea0SLionel Sambuc l_new(lua_State *L)
103*ebfedea0SLionel Sambuc {
104*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
105*ebfedea0SLionel Sambuc 
106*ebfedea0SLionel Sambuc 	netpgp = lua_newuserdata(L, sizeof(*netpgp));
107*ebfedea0SLionel Sambuc 	(void) memset(netpgp, 0x0, sizeof(*netpgp));
108*ebfedea0SLionel Sambuc 	set_homedir(netpgp, getenv("HOME"), "/.gnupg", 1);
109*ebfedea0SLionel Sambuc 	netpgp_setvar(netpgp, "hash", DEFAULT_HASH_ALG);
110*ebfedea0SLionel Sambuc 	return 1;
111*ebfedea0SLionel Sambuc }
112*ebfedea0SLionel Sambuc 
113*ebfedea0SLionel Sambuc /* initialise(netpgp) */
114*ebfedea0SLionel Sambuc static int
l_init(lua_State * L)115*ebfedea0SLionel Sambuc l_init(lua_State *L)
116*ebfedea0SLionel Sambuc {
117*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
118*ebfedea0SLionel Sambuc 
119*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
120*ebfedea0SLionel Sambuc 	lua_pushnumber(L, netpgp_init(netpgp));
121*ebfedea0SLionel Sambuc 	return 1;
122*ebfedea0SLionel Sambuc }
123*ebfedea0SLionel Sambuc 
124*ebfedea0SLionel Sambuc /* homedir(netpgp, homedir) */
125*ebfedea0SLionel Sambuc static int
l_homedir(lua_State * L)126*ebfedea0SLionel Sambuc l_homedir(lua_State *L)
127*ebfedea0SLionel Sambuc {
128*ebfedea0SLionel Sambuc 	const char	*home;
129*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
130*ebfedea0SLionel Sambuc 
131*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
132*ebfedea0SLionel Sambuc 	home = luaL_checkstring(L, 2);
133*ebfedea0SLionel Sambuc 	lua_pushnumber(L, set_homedir(netpgp, __UNCONST(home), NULL, 0));
134*ebfedea0SLionel Sambuc 	return 1;
135*ebfedea0SLionel Sambuc }
136*ebfedea0SLionel Sambuc 
137*ebfedea0SLionel Sambuc static strarg_t	armourtypes[] = {
138*ebfedea0SLionel Sambuc 	{	"armoured",	1	},
139*ebfedea0SLionel Sambuc 	{	"armored",	1	},
140*ebfedea0SLionel Sambuc 	{	"armour",	1	},
141*ebfedea0SLionel Sambuc 	{	"armor",	1	},
142*ebfedea0SLionel Sambuc 	{	NULL,		0	}
143*ebfedea0SLionel Sambuc };
144*ebfedea0SLionel Sambuc 
145*ebfedea0SLionel Sambuc /* encrypt_file(netpgp, f, output, armour) */
146*ebfedea0SLionel Sambuc static int
l_encrypt_file(lua_State * L)147*ebfedea0SLionel Sambuc l_encrypt_file(lua_State *L)
148*ebfedea0SLionel Sambuc {
149*ebfedea0SLionel Sambuc 	const char	*output;
150*ebfedea0SLionel Sambuc 	const char	*f;
151*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
152*ebfedea0SLionel Sambuc 	int		 armour;
153*ebfedea0SLionel Sambuc 	int		 ret;
154*ebfedea0SLionel Sambuc 
155*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
156*ebfedea0SLionel Sambuc 	netpgp_setvar(netpgp, "need userid", "1");
157*ebfedea0SLionel Sambuc 	f = luaL_checkstring(L, 2);
158*ebfedea0SLionel Sambuc 	output = luaL_checkstring(L, 3);
159*ebfedea0SLionel Sambuc 	if (*output == 0x0) {
160*ebfedea0SLionel Sambuc 		output = NULL;
161*ebfedea0SLionel Sambuc 	}
162*ebfedea0SLionel Sambuc 	armour = findtype(armourtypes, luaL_checkstring(L, 4));
163*ebfedea0SLionel Sambuc 	ret = netpgp_encrypt_file(netpgp, netpgp_getvar(netpgp, "userid"),
164*ebfedea0SLionel Sambuc 				f, __UNCONST("a.gpg"), armour);
165*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
166*ebfedea0SLionel Sambuc 	return 1;
167*ebfedea0SLionel Sambuc }
168*ebfedea0SLionel Sambuc 
169*ebfedea0SLionel Sambuc /* decrypt_file(netpgp, f, output, armour) */
170*ebfedea0SLionel Sambuc static int
l_decrypt_file(lua_State * L)171*ebfedea0SLionel Sambuc l_decrypt_file(lua_State *L)
172*ebfedea0SLionel Sambuc {
173*ebfedea0SLionel Sambuc 	const char	*output;
174*ebfedea0SLionel Sambuc 	const char	*f;
175*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
176*ebfedea0SLionel Sambuc 	int		 armour;
177*ebfedea0SLionel Sambuc 	int		 ret;
178*ebfedea0SLionel Sambuc 
179*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
180*ebfedea0SLionel Sambuc 	f = luaL_checkstring(L, 2);
181*ebfedea0SLionel Sambuc 	output = luaL_checkstring(L, 3);
182*ebfedea0SLionel Sambuc 	if (*output == 0x0) {
183*ebfedea0SLionel Sambuc 		output = NULL;
184*ebfedea0SLionel Sambuc 	}
185*ebfedea0SLionel Sambuc 	armour = findtype(armourtypes, luaL_checkstring(L, 4));
186*ebfedea0SLionel Sambuc 	ret = netpgp_decrypt_file(netpgp, f, __UNCONST(output), armour);
187*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
188*ebfedea0SLionel Sambuc 	return 1;
189*ebfedea0SLionel Sambuc }
190*ebfedea0SLionel Sambuc 
191*ebfedea0SLionel Sambuc static strarg_t	detachtypes[] = {
192*ebfedea0SLionel Sambuc 	{	"detached",	1	},
193*ebfedea0SLionel Sambuc 	{	"separate",	1	},
194*ebfedea0SLionel Sambuc 	{	"detach",	1	},
195*ebfedea0SLionel Sambuc 	{	NULL,		0	}
196*ebfedea0SLionel Sambuc };
197*ebfedea0SLionel Sambuc 
198*ebfedea0SLionel Sambuc /* sign_file(netpgp, f, output, armour, detached) */
199*ebfedea0SLionel Sambuc static int
l_sign_file(lua_State * L)200*ebfedea0SLionel Sambuc l_sign_file(lua_State *L)
201*ebfedea0SLionel Sambuc {
202*ebfedea0SLionel Sambuc 	const char	*output;
203*ebfedea0SLionel Sambuc 	const char	*f;
204*ebfedea0SLionel Sambuc 	const int	 binary = 0;
205*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
206*ebfedea0SLionel Sambuc 	int		 detached;
207*ebfedea0SLionel Sambuc 	int		 armour;
208*ebfedea0SLionel Sambuc 	int		 ret;
209*ebfedea0SLionel Sambuc 
210*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
211*ebfedea0SLionel Sambuc 	netpgp_setvar(netpgp, "need userid", "1");
212*ebfedea0SLionel Sambuc 	f = luaL_checkstring(L, 2);
213*ebfedea0SLionel Sambuc 	output = luaL_checkstring(L, 3);
214*ebfedea0SLionel Sambuc 	if (*output == 0x0) {
215*ebfedea0SLionel Sambuc 		output = NULL;
216*ebfedea0SLionel Sambuc 	}
217*ebfedea0SLionel Sambuc 	armour = findtype(armourtypes, luaL_checkstring(L, 4));
218*ebfedea0SLionel Sambuc 	detached = findtype(detachtypes, luaL_checkstring(L, 5));
219*ebfedea0SLionel Sambuc 	ret = netpgp_sign_file(netpgp, netpgp_getvar(netpgp, "userid"),
220*ebfedea0SLionel Sambuc 				f, __UNCONST(output), armour, binary,
221*ebfedea0SLionel Sambuc 				detached);
222*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
223*ebfedea0SLionel Sambuc 	return 1;
224*ebfedea0SLionel Sambuc }
225*ebfedea0SLionel Sambuc 
226*ebfedea0SLionel Sambuc /* clearsign_file(netpgp, f, output, armour, detached) */
227*ebfedea0SLionel Sambuc static int
l_clearsign_file(lua_State * L)228*ebfedea0SLionel Sambuc l_clearsign_file(lua_State *L)
229*ebfedea0SLionel Sambuc {
230*ebfedea0SLionel Sambuc 	const char	*output;
231*ebfedea0SLionel Sambuc 	const char	*f;
232*ebfedea0SLionel Sambuc 	const int	 cleartext = 1;
233*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
234*ebfedea0SLionel Sambuc 	int		 detached;
235*ebfedea0SLionel Sambuc 	int		 armour;
236*ebfedea0SLionel Sambuc 	int		 ret;
237*ebfedea0SLionel Sambuc 
238*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
239*ebfedea0SLionel Sambuc 	netpgp_setvar(netpgp, "need userid", "1");
240*ebfedea0SLionel Sambuc 	f = luaL_checkstring(L, 2);
241*ebfedea0SLionel Sambuc 	output = luaL_checkstring(L, 3);
242*ebfedea0SLionel Sambuc 	armour = findtype(armourtypes, luaL_checkstring(L, 4));
243*ebfedea0SLionel Sambuc 	detached = findtype(detachtypes, luaL_checkstring(L, 5));
244*ebfedea0SLionel Sambuc 	ret = netpgp_sign_file(netpgp, netpgp_getvar(netpgp, "userid"),
245*ebfedea0SLionel Sambuc 				f, __UNCONST(output), armour, cleartext,
246*ebfedea0SLionel Sambuc 				detached);
247*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
248*ebfedea0SLionel Sambuc 	return 1;
249*ebfedea0SLionel Sambuc }
250*ebfedea0SLionel Sambuc 
251*ebfedea0SLionel Sambuc /* verify_file(netpgp, f, armour) */
252*ebfedea0SLionel Sambuc static int
l_verify_file(lua_State * L)253*ebfedea0SLionel Sambuc l_verify_file(lua_State *L)
254*ebfedea0SLionel Sambuc {
255*ebfedea0SLionel Sambuc 	const char	*f;
256*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
257*ebfedea0SLionel Sambuc 	int		 armour;
258*ebfedea0SLionel Sambuc 	int		 ret;
259*ebfedea0SLionel Sambuc 
260*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
261*ebfedea0SLionel Sambuc 	f = luaL_checkstring(L, 2);
262*ebfedea0SLionel Sambuc 	armour = findtype(armourtypes, luaL_checkstring(L, 3));
263*ebfedea0SLionel Sambuc 	ret = netpgp_verify_file(netpgp, f, NULL, armour);
264*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
265*ebfedea0SLionel Sambuc 	return 1;
266*ebfedea0SLionel Sambuc }
267*ebfedea0SLionel Sambuc 
268*ebfedea0SLionel Sambuc /* verify_cat_file(netpgp, f, output, armour) */
269*ebfedea0SLionel Sambuc static int
l_verify_cat_file(lua_State * L)270*ebfedea0SLionel Sambuc l_verify_cat_file(lua_State *L)
271*ebfedea0SLionel Sambuc {
272*ebfedea0SLionel Sambuc 	const char	*output;
273*ebfedea0SLionel Sambuc 	const char	*f;
274*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
275*ebfedea0SLionel Sambuc 	int		 armour;
276*ebfedea0SLionel Sambuc 	int		 ret;
277*ebfedea0SLionel Sambuc 
278*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
279*ebfedea0SLionel Sambuc 	f = luaL_checkstring(L, 2);
280*ebfedea0SLionel Sambuc 	output = luaL_checkstring(L, 3);
281*ebfedea0SLionel Sambuc 	armour = findtype(armourtypes, luaL_checkstring(L, 4));
282*ebfedea0SLionel Sambuc 	ret = netpgp_verify_file(netpgp, f, output, armour);
283*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
284*ebfedea0SLionel Sambuc 	return 1;
285*ebfedea0SLionel Sambuc }
286*ebfedea0SLionel Sambuc 
287*ebfedea0SLionel Sambuc /* list_packets(netpgp, f, armour) */
288*ebfedea0SLionel Sambuc static int
l_list_packets(lua_State * L)289*ebfedea0SLionel Sambuc l_list_packets(lua_State *L)
290*ebfedea0SLionel Sambuc {
291*ebfedea0SLionel Sambuc 	const char	*f;
292*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
293*ebfedea0SLionel Sambuc 	int		 armour;
294*ebfedea0SLionel Sambuc 	int		 ret;
295*ebfedea0SLionel Sambuc 
296*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
297*ebfedea0SLionel Sambuc 	f = luaL_checkstring(L, 2);
298*ebfedea0SLionel Sambuc 	armour = findtype(armourtypes, luaL_checkstring(L, 3));
299*ebfedea0SLionel Sambuc 	ret = netpgp_list_packets(netpgp, __UNCONST(f), armour, NULL);
300*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
301*ebfedea0SLionel Sambuc 	return 1;
302*ebfedea0SLionel Sambuc }
303*ebfedea0SLionel Sambuc 
304*ebfedea0SLionel Sambuc /* setvar(netpgp, name, value) */
305*ebfedea0SLionel Sambuc static int
l_setvar(lua_State * L)306*ebfedea0SLionel Sambuc l_setvar(lua_State *L)
307*ebfedea0SLionel Sambuc {
308*ebfedea0SLionel Sambuc 	const char	*name;
309*ebfedea0SLionel Sambuc 	const char	*value;
310*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
311*ebfedea0SLionel Sambuc 	int		 ret;
312*ebfedea0SLionel Sambuc 
313*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
314*ebfedea0SLionel Sambuc 	name = luaL_checkstring(L, 2);
315*ebfedea0SLionel Sambuc 	value = luaL_checkstring(L, 3);
316*ebfedea0SLionel Sambuc 	ret = netpgp_setvar(netpgp, name, value);
317*ebfedea0SLionel Sambuc 	lua_pushnumber(L, ret);
318*ebfedea0SLionel Sambuc 	return 1;
319*ebfedea0SLionel Sambuc }
320*ebfedea0SLionel Sambuc 
321*ebfedea0SLionel Sambuc /* getvar(netpgp, name, value) */
322*ebfedea0SLionel Sambuc static int
l_getvar(lua_State * L)323*ebfedea0SLionel Sambuc l_getvar(lua_State *L)
324*ebfedea0SLionel Sambuc {
325*ebfedea0SLionel Sambuc 	const char	*name;
326*ebfedea0SLionel Sambuc 	const char	*ret;
327*ebfedea0SLionel Sambuc 	netpgp_t	*netpgp;
328*ebfedea0SLionel Sambuc 
329*ebfedea0SLionel Sambuc 	netpgp = lua_touserdata(L, 1);
330*ebfedea0SLionel Sambuc 	name = luaL_checkstring(L, 2);
331*ebfedea0SLionel Sambuc 	ret = netpgp_getvar(netpgp, name);
332*ebfedea0SLionel Sambuc 	lua_pushstring(L, ret);
333*ebfedea0SLionel Sambuc 	return 1;
334*ebfedea0SLionel Sambuc }
335*ebfedea0SLionel Sambuc 
336*ebfedea0SLionel Sambuc const struct luaL_reg libluanetpgp[] = {
337*ebfedea0SLionel Sambuc 	{ "new",		l_new },
338*ebfedea0SLionel Sambuc 	{ "init",		l_init },
339*ebfedea0SLionel Sambuc 
340*ebfedea0SLionel Sambuc 	{ "encrypt_file",	l_encrypt_file },
341*ebfedea0SLionel Sambuc 	{ "decrypt_file",	l_decrypt_file },
342*ebfedea0SLionel Sambuc 	{ "sign_file",		l_sign_file },
343*ebfedea0SLionel Sambuc 	{ "clearsign_file",	l_clearsign_file },
344*ebfedea0SLionel Sambuc 	{ "verify_file",	l_verify_file },
345*ebfedea0SLionel Sambuc 	{ "verify_cat_file",	l_verify_cat_file },
346*ebfedea0SLionel Sambuc 
347*ebfedea0SLionel Sambuc 	{ "list_packets",	l_list_packets },
348*ebfedea0SLionel Sambuc 
349*ebfedea0SLionel Sambuc 	{ "getvar",		l_getvar },
350*ebfedea0SLionel Sambuc 	{ "setvar",		l_setvar },
351*ebfedea0SLionel Sambuc 
352*ebfedea0SLionel Sambuc 	{ "homedir",		l_homedir },
353*ebfedea0SLionel Sambuc 
354*ebfedea0SLionel Sambuc 	{ NULL,			NULL }
355*ebfedea0SLionel Sambuc };
356*ebfedea0SLionel Sambuc 
357*ebfedea0SLionel Sambuc int
luaopen_netpgp(lua_State * L)358*ebfedea0SLionel Sambuc luaopen_netpgp(lua_State *L)
359*ebfedea0SLionel Sambuc {
360*ebfedea0SLionel Sambuc 	luaL_openlib(L, "netpgp", libluanetpgp, 0);
361*ebfedea0SLionel Sambuc 	return 1;
362*ebfedea0SLionel Sambuc }
363