aboutsummaryrefslogtreecommitdiffstats
path: root/util-src
diff options
context:
space:
mode:
Diffstat (limited to 'util-src')
-rw-r--r--util-src/Makefile11
-rw-r--r--util-src/encodings.c150
-rw-r--r--util-src/pposix.c87
-rw-r--r--util-src/signal.c6
-rw-r--r--util-src/windows.c35
5 files changed, 274 insertions, 15 deletions
diff --git a/util-src/Makefile b/util-src/Makefile
index 4b2606dc..1ca934ad 100644
--- a/util-src/Makefile
+++ b/util-src/Makefile
@@ -7,16 +7,25 @@ LUA_LIB?=lua$(LUA_SUFFIX)
IDN_LIB?=idn
OPENSSL_LIB?=crypto
CC?=gcc
+CXX?=g++
LD?=gcc
.SUFFIXES: .c .o .so
+encodings.so: encodings.o
+ MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET;
+ $(CC) -o $@ $< $(LDFLAGS) $(IDNA_LIBS)
+
+hashes.so: hashes.o
+ MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET;
+ $(CC) -o $@ $< $(LDFLAGS) -l$(OPENSSL_LIB)
+
.c.o:
$(CC) $(CFLAGS) -I$(LUA_INCDIR) -c -o $@ $<
.o.so:
MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET;
- $(LD) $(LDFLAGS) -o $@ $< -L$(LUA_LIBDIR) -llua$(LUA_SUFFIX) -lidn -lcrypto
+ $(LD) -o $@ $< $(LDFLAGS)
all: encodings.so hashes.so pposix.so signal.so
diff --git a/util-src/encodings.c b/util-src/encodings.c
index f2109d0c..2a4653fb 100644
--- a/util-src/encodings.c
+++ b/util-src/encodings.c
@@ -12,12 +12,11 @@
* Lua library for base64, stringprep and idna encodings
*/
-// Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way
+/* Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way */
#define _CRT_SECURE_NO_DEPRECATE
#include <string.h>
#include <stdlib.h>
-
#include "lua.h"
#include "lauxlib.h"
@@ -118,6 +117,8 @@ static const luaL_Reg Reg_base64[] =
};
/***************** STRINGPREP *****************/
+#ifndef USE_STRINGPREP_ICU
+/****************** libidn ********************/
#include <stringprep.h>
@@ -134,16 +135,16 @@ static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
s = lua_tolstring(L, 1, &len);
if (len >= 1024) {
lua_pushnil(L);
- return 1; // TODO return error message
+ return 1; /* TODO return error message */
}
strcpy(string, s);
- ret = stringprep(string, 1024, 0, profile);
+ ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
if (ret == STRINGPREP_OK) {
lua_pushstring(L, string);
return 1;
} else {
lua_pushnil(L);
- return 1; // TODO return error message
+ return 1; /* TODO return error message */
}
}
@@ -164,7 +165,85 @@ static const luaL_Reg Reg_stringprep[] =
{ NULL, NULL }
};
+#else
+#include <unicode/usprep.h>
+#include <unicode/ustring.h>
+#include <unicode/utrace.h>
+
+static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
+{
+ size_t input_len;
+ int32_t unprepped_len, prepped_len, output_len;
+ const char *input;
+ char output[1024];
+
+ UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
+ UChar prepped[1024];
+
+ UErrorCode err = U_ZERO_ERROR;
+
+ if(!lua_isstring(L, 1)) {
+ lua_pushnil(L);
+ return 1;
+ }
+ input = lua_tolstring(L, 1, &input_len);
+ if (input_len >= 1024) {
+ lua_pushnil(L);
+ return 1;
+ }
+ u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
+ prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
+ if (U_FAILURE(err)) {
+ lua_pushnil(L);
+ return 1;
+ } else {
+ u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
+ if(output_len < 1024)
+ lua_pushlstring(L, output, output_len);
+ else
+ lua_pushnil(L);
+ return 1;
+ }
+}
+
+UStringPrepProfile *icu_nameprep;
+UStringPrepProfile *icu_nodeprep;
+UStringPrepProfile *icu_resourceprep;
+UStringPrepProfile *icu_saslprep;
+
+/* initialize global ICU stringprep profiles */
+void init_icu()
+{
+ UErrorCode err = U_ZERO_ERROR;
+ utrace_setLevel(UTRACE_VERBOSE);
+ icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
+ icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
+ icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
+ icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
+ if (U_FAILURE(err)) fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
+}
+
+#define MAKE_PREP_FUNC(myFunc, prep) \
+static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
+
+MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep) /** stringprep.nameprep(s) */
+MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep) /** stringprep.nodeprep(s) */
+MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep) /** stringprep.resourceprep(s) */
+MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep) /** stringprep.saslprep(s) */
+
+static const luaL_Reg Reg_stringprep[] =
+{
+ { "nameprep", Lstringprep_nameprep },
+ { "nodeprep", Lstringprep_nodeprep },
+ { "resourceprep", Lstringprep_resourceprep },
+ { "saslprep", Lstringprep_saslprep },
+ { NULL, NULL }
+};
+#endif
+
/***************** IDNA *****************/
+#ifndef USE_STRINGPREP_ICU
+/****************** libidn ********************/
#include <idna.h>
#include <idn-free.h>
@@ -182,7 +261,7 @@ static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
} else {
lua_pushnil(L);
idn_free(output);
- return 1; // TODO return error message
+ return 1; /* TODO return error message */
}
}
@@ -199,9 +278,63 @@ static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
} else {
lua_pushnil(L);
idn_free(output);
- return 1; // TODO return error message
+ return 1; /* TODO return error message */
+ }
+}
+#else
+#include <unicode/ustdio.h>
+#include <unicode/uidna.h>
+/* IDNA2003 or IDNA2008 ? ? ? */
+static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
+{
+ size_t len;
+ int32_t ulen, dest_len, output_len;
+ const char *s = luaL_checklstring(L, 1, &len);
+ UChar ustr[1024];
+ UErrorCode err = U_ZERO_ERROR;
+ UChar dest[1024];
+ char output[1024];
+
+ u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
+ dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
+ if (U_FAILURE(err)) {
+ lua_pushnil(L);
+ return 1;
+ } else {
+ u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
+ if(output_len < 1024)
+ lua_pushlstring(L, output, output_len);
+ else
+ lua_pushnil(L);
+ return 1;
+ }
+}
+
+static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
+{
+ size_t len;
+ int32_t ulen, dest_len, output_len;
+ const char *s = luaL_checklstring(L, 1, &len);
+ UChar* ustr;
+ UErrorCode err = U_ZERO_ERROR;
+ UChar dest[1024];
+ char output[1024];
+
+ u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
+ dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
+ if (U_FAILURE(err)) {
+ lua_pushnil(L);
+ return 1;
+ } else {
+ u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
+ if(output_len < 1024)
+ lua_pushlstring(L, output, output_len);
+ else
+ lua_pushnil(L);
+ return 1;
}
}
+#endif
static const luaL_Reg Reg_idna[] =
{
@@ -219,6 +352,9 @@ static const luaL_Reg Reg[] =
LUALIB_API int luaopen_util_encodings(lua_State *L)
{
+#ifdef USE_STRINGPREP_ICU
+ init_icu();
+#endif
luaL_register(L, "encodings", Reg);
lua_pushliteral(L, "base64");
diff --git a/util-src/pposix.c b/util-src/pposix.c
index 9f16f178..ffd21288 100644
--- a/util-src/pposix.c
+++ b/util-src/pposix.c
@@ -13,7 +13,7 @@
* POSIX support functions for Lua
*/
-#define MODULE_VERSION "0.3.3"
+#define MODULE_VERSION "0.3.5"
#include <stdlib.h>
#include <math.h>
@@ -22,6 +22,7 @@
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/utsname.h>
#include <fcntl.h>
#include <syslog.h>
@@ -359,6 +360,62 @@ int lc_setgid(lua_State* L)
return 2;
}
+int lc_initgroups(lua_State* L)
+{
+ int ret;
+ gid_t gid;
+ struct passwd *p;
+
+ if(!lua_isstring(L, 1))
+ {
+ lua_pushnil(L);
+ lua_pushstring(L, "invalid-username");
+ return 2;
+ }
+ p = getpwnam(lua_tostring(L, 1));
+ if(!p)
+ {
+ lua_pushnil(L);
+ lua_pushstring(L, "no-such-user");
+ return 2;
+ }
+ if(lua_gettop(L) < 2)
+ lua_pushnil(L);
+ switch(lua_type(L, 2))
+ {
+ case LUA_TNIL:
+ gid = p->pw_gid;
+ break;
+ case LUA_TNUMBER:
+ gid = lua_tointeger(L, 2);
+ break;
+ default:
+ lua_pushnil(L);
+ lua_pushstring(L, "invalid-gid");
+ return 2;
+ }
+ ret = initgroups(lua_tostring(L, 1), gid);
+ switch(errno)
+ {
+ case 0:
+ lua_pushboolean(L, 1);
+ lua_pushnil(L);
+ break;
+ case ENOMEM:
+ lua_pushnil(L);
+ lua_pushstring(L, "no-memory");
+ break;
+ case EPERM:
+ lua_pushnil(L);
+ lua_pushstring(L, "permission-denied");
+ break;
+ default:
+ lua_pushnil(L);
+ lua_pushstring(L, "unknown-error");
+ }
+ return 2;
+}
+
int lc_umask(lua_State* L)
{
char old_mode_string[7];
@@ -497,6 +554,29 @@ int lc_abort(lua_State* L)
return 0;
}
+int lc_uname(lua_State* L)
+{
+ struct utsname uname_info;
+ if(uname(&uname_info) != 0)
+ {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ return 2;
+ }
+ lua_newtable(L);
+ lua_pushstring(L, uname_info.sysname);
+ lua_setfield(L, -2, "sysname");
+ lua_pushstring(L, uname_info.nodename);
+ lua_setfield(L, -2, "nodename");
+ lua_pushstring(L, uname_info.release);
+ lua_setfield(L, -2, "release");
+ lua_pushstring(L, uname_info.version);
+ lua_setfield(L, -2, "version");
+ lua_pushstring(L, uname_info.machine);
+ lua_setfield(L, -2, "machine");
+ return 1;
+}
+
/* Register functions */
int luaopen_util_pposix(lua_State *L)
@@ -517,6 +597,7 @@ int luaopen_util_pposix(lua_State *L)
{ "setuid", lc_setuid },
{ "setgid", lc_setgid },
+ { "initgroups", lc_initgroups },
{ "umask", lc_umask },
@@ -525,6 +606,8 @@ int luaopen_util_pposix(lua_State *L)
{ "setrlimit", lc_setrlimit },
{ "getrlimit", lc_getrlimit },
+ { "uname", lc_uname },
+
{ NULL, NULL }
};
@@ -537,4 +620,4 @@ int luaopen_util_pposix(lua_State *L)
lua_setfield(L, -2, "_VERSION");
return 1;
-};
+}
diff --git a/util-src/signal.c b/util-src/signal.c
index 2d13383f..961d2d3e 100644
--- a/util-src/signal.c
+++ b/util-src/signal.c
@@ -165,13 +165,13 @@ static struct signal_event *last_event = NULL;
static void sighook(lua_State *L, lua_Debug *ar)
{
+ struct signal_event *event;
/* restore the old hook */
lua_sethook(L, Hsig, Hmask, Hcount);
lua_pushstring(L, LUA_SIGNAL);
lua_gettable(L, LUA_REGISTRYINDEX);
- struct signal_event *event;
while((event = signal_queue))
{
lua_pushnumber(L, event->Nsig);
@@ -326,7 +326,7 @@ static int l_raise(lua_State *L)
return 1;
}
-#if defined _POSIX_SOURCE || (defined(sun) || defined(__sun))
+#if defined(__unix__) || defined(__APPLE__)
/* define some posix only functions */
@@ -373,7 +373,7 @@ static int l_kill(lua_State *L)
static const struct luaL_Reg lsignal_lib[] = {
{"signal", l_signal},
{"raise", l_raise},
-#if defined _POSIX_SOURCE || (defined(sun) || defined(__sun))
+#if defined(__unix__) || defined(__APPLE__)
{"kill", l_kill},
#endif
{NULL, NULL}
diff --git a/util-src/windows.c b/util-src/windows.c
index 12bd7ce9..121cc471 100644
--- a/util-src/windows.c
+++ b/util-src/windows.c
@@ -38,14 +38,45 @@ static int Lget_nameservers(lua_State *L) {
}
return 1;
} else {
- luaL_error(L, "DnsQueryConfig returned %d", status);
- return 0; // unreachable, but prevents a compiler warning
+ lua_pushnil(L);
+ lua_pushfstring(L, "DnsQueryConfig returned %d", status);
+ return 2;
}
}
+static int lerror(lua_State *L, char* string) {
+ lua_pushnil(L);
+ lua_pushfstring(L, "%s: %d", string, GetLastError());
+ return 2;
+}
+
+static int Lget_consolecolor(lua_State *L) {
+ HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
+ WORD color; DWORD read_len;
+
+ CONSOLE_SCREEN_BUFFER_INFO info;
+
+ if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
+ if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo");
+ if (!ReadConsoleOutputAttribute(console, &color, sizeof(WORD), info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute");
+
+ lua_pushnumber(L, color);
+ return 1;
+}
+static int Lset_consolecolor(lua_State *L) {
+ int color = luaL_checkint(L, 1);
+ HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
+ if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
+ if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute");
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
static const luaL_Reg Reg[] =
{
{ "get_nameservers", Lget_nameservers },
+ { "get_consolecolor", Lget_consolecolor },
+ { "set_consolecolor", Lset_consolecolor },
{ NULL, NULL }
};