2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Alistair Crooks (agc@netbsd.org)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/param.h>
46 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
47 #endif /* !__UNCONST */
49 #define DEFAULT_HASH_ALG "SHA256"
51 int luaopen_netpgp(lua_State
*);
53 typedef struct strarg_t
{
54 const char *s
; /* string */
55 const int n
; /* corresponding int value */
58 /* map a string onto an int */
60 findtype(strarg_t
*strs
, const char *s
)
64 for (sp
= strs
; sp
->s
&& strcasecmp(sp
->s
, s
) != 0 ; sp
++) {
69 /* set the home directory value to "home/subdir" */
71 set_homedir(netpgp_t
*netpgp
, char *home
, const char *subdir
, const int quiet
)
78 (void) fprintf(stderr
, "NULL HOME directory\n");
82 (void) snprintf(d
, sizeof(d
), "%s%s", home
, (subdir
) ? subdir
: "");
83 if (stat(d
, &st
) == 0) {
84 if ((st
.st_mode
& S_IFMT
) == S_IFDIR
) {
85 netpgp_setvar(netpgp
, "homedir", d
);
88 (void) fprintf(stderr
, "netpgp: homedir \"%s\" is not a dir\n",
93 (void) fprintf(stderr
,
94 "netpgp: warning homedir \"%s\" not found\n", d
);
106 netpgp
= lua_newuserdata(L
, sizeof(*netpgp
));
107 (void) memset(netpgp
, 0x0, sizeof(*netpgp
));
108 set_homedir(netpgp
, getenv("HOME"), "/.gnupg", 1);
109 netpgp_setvar(netpgp
, "hash", DEFAULT_HASH_ALG
);
113 /* initialise(netpgp) */
119 netpgp
= lua_touserdata(L
, 1);
120 lua_pushnumber(L
, netpgp_init(netpgp
));
124 /* homedir(netpgp, homedir) */
126 l_homedir(lua_State
*L
)
131 netpgp
= lua_touserdata(L
, 1);
132 home
= luaL_checkstring(L
, 2);
133 lua_pushnumber(L
, set_homedir(netpgp
, __UNCONST(home
), NULL
, 0));
137 static strarg_t armourtypes
[] = {
145 /* encrypt_file(netpgp, f, output, armour) */
147 l_encrypt_file(lua_State
*L
)
155 netpgp
= lua_touserdata(L
, 1);
156 netpgp_setvar(netpgp
, "need userid", "1");
157 f
= luaL_checkstring(L
, 2);
158 output
= luaL_checkstring(L
, 3);
159 if (*output
== 0x0) {
162 armour
= findtype(armourtypes
, luaL_checkstring(L
, 4));
163 ret
= netpgp_encrypt_file(netpgp
, netpgp_getvar(netpgp
, "userid"),
164 f
, __UNCONST("a.gpg"), armour
);
165 lua_pushnumber(L
, ret
);
169 /* decrypt_file(netpgp, f, output, armour) */
171 l_decrypt_file(lua_State
*L
)
179 netpgp
= lua_touserdata(L
, 1);
180 f
= luaL_checkstring(L
, 2);
181 output
= luaL_checkstring(L
, 3);
182 if (*output
== 0x0) {
185 armour
= findtype(armourtypes
, luaL_checkstring(L
, 4));
186 ret
= netpgp_decrypt_file(netpgp
, f
, __UNCONST(output
), armour
);
187 lua_pushnumber(L
, ret
);
191 static strarg_t detachtypes
[] = {
198 /* sign_file(netpgp, f, output, armour, detached) */
200 l_sign_file(lua_State
*L
)
204 const int binary
= 0;
210 netpgp
= lua_touserdata(L
, 1);
211 netpgp_setvar(netpgp
, "need userid", "1");
212 f
= luaL_checkstring(L
, 2);
213 output
= luaL_checkstring(L
, 3);
214 if (*output
== 0x0) {
217 armour
= findtype(armourtypes
, luaL_checkstring(L
, 4));
218 detached
= findtype(detachtypes
, luaL_checkstring(L
, 5));
219 ret
= netpgp_sign_file(netpgp
, netpgp_getvar(netpgp
, "userid"),
220 f
, __UNCONST(output
), armour
, binary
,
222 lua_pushnumber(L
, ret
);
226 /* clearsign_file(netpgp, f, output, armour, detached) */
228 l_clearsign_file(lua_State
*L
)
232 const int cleartext
= 1;
238 netpgp
= lua_touserdata(L
, 1);
239 netpgp_setvar(netpgp
, "need userid", "1");
240 f
= luaL_checkstring(L
, 2);
241 output
= luaL_checkstring(L
, 3);
242 armour
= findtype(armourtypes
, luaL_checkstring(L
, 4));
243 detached
= findtype(detachtypes
, luaL_checkstring(L
, 5));
244 ret
= netpgp_sign_file(netpgp
, netpgp_getvar(netpgp
, "userid"),
245 f
, __UNCONST(output
), armour
, cleartext
,
247 lua_pushnumber(L
, ret
);
251 /* verify_file(netpgp, f, armour) */
253 l_verify_file(lua_State
*L
)
260 netpgp
= lua_touserdata(L
, 1);
261 f
= luaL_checkstring(L
, 2);
262 armour
= findtype(armourtypes
, luaL_checkstring(L
, 3));
263 ret
= netpgp_verify_file(netpgp
, f
, NULL
, armour
);
264 lua_pushnumber(L
, ret
);
268 /* verify_cat_file(netpgp, f, output, armour) */
270 l_verify_cat_file(lua_State
*L
)
278 netpgp
= lua_touserdata(L
, 1);
279 f
= luaL_checkstring(L
, 2);
280 output
= luaL_checkstring(L
, 3);
281 armour
= findtype(armourtypes
, luaL_checkstring(L
, 4));
282 ret
= netpgp_verify_file(netpgp
, f
, output
, armour
);
283 lua_pushnumber(L
, ret
);
287 /* list_packets(netpgp, f, armour) */
289 l_list_packets(lua_State
*L
)
296 netpgp
= lua_touserdata(L
, 1);
297 f
= luaL_checkstring(L
, 2);
298 armour
= findtype(armourtypes
, luaL_checkstring(L
, 3));
299 ret
= netpgp_list_packets(netpgp
, __UNCONST(f
), armour
, NULL
);
300 lua_pushnumber(L
, ret
);
304 /* setvar(netpgp, name, value) */
306 l_setvar(lua_State
*L
)
313 netpgp
= lua_touserdata(L
, 1);
314 name
= luaL_checkstring(L
, 2);
315 value
= luaL_checkstring(L
, 3);
316 ret
= netpgp_setvar(netpgp
, name
, value
);
317 lua_pushnumber(L
, ret
);
321 /* getvar(netpgp, name, value) */
323 l_getvar(lua_State
*L
)
329 netpgp
= lua_touserdata(L
, 1);
330 name
= luaL_checkstring(L
, 2);
331 ret
= netpgp_getvar(netpgp
, name
);
332 lua_pushstring(L
, ret
);
336 const struct luaL_reg libluanetpgp
[] = {
340 { "encrypt_file", l_encrypt_file
},
341 { "decrypt_file", l_decrypt_file
},
342 { "sign_file", l_sign_file
},
343 { "clearsign_file", l_clearsign_file
},
344 { "verify_file", l_verify_file
},
345 { "verify_cat_file", l_verify_cat_file
},
347 { "list_packets", l_list_packets
},
349 { "getvar", l_getvar
},
350 { "setvar", l_setvar
},
352 { "homedir", l_homedir
},
358 luaopen_netpgp(lua_State
*L
)
360 luaL_openlib(L
, "netpgp", libluanetpgp
, 0);