author | Myhailo Danylenko <isbear@ukrpost.net> |
Sun, 05 Apr 2009 16:55:48 +0300 | |
changeset 18 | 6effa1929af7 |
parent 16 | 09b375e9ce32 |
child 19 | d775d7289fe4 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include <lua.h> |
|
3 |
#include <lauxlib.h> |
|
4 |
#include <glib.h> |
|
5 |
#include <loudmouth/loudmouth.h> |
|
6 |
#include <stdio.h> |
|
7 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
8 |
#include "config.h" |
0 | 9 |
#include "util.h" |
10 |
#include "lm_types.h" |
|
11 |
||
12 |
/// lm.ssl |
|
13 |
/// Object, containing information about ssl abilities for connection. |
|
14 |
/// Create, set parameters, and attach to connection with 'ssl' method. |
|
15 |
||
16 |
/// ssl status |
|
17 |
/// String, representing what problem have current ssl session. |
|
4 | 18 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
19 |
const string2enum_t status_lm_ssl[] = { |
0 | 20 |
{ "no cert found", LM_SSL_STATUS_NO_CERT_FOUND }, |
21 |
{ "untrusted cert", LM_SSL_STATUS_UNTRUSTED_CERT }, |
|
22 |
{ "cert expired", LM_SSL_STATUS_CERT_EXPIRED }, |
|
23 |
{ "cert not activated", LM_SSL_STATUS_CERT_NOT_ACTIVATED }, |
|
24 |
{ "cert hostname mismatch", LM_SSL_STATUS_CERT_HOSTNAME_MISMATCH }, |
|
25 |
{ "cert fingerprint mismatch", LM_SSL_STATUS_CERT_FINGERPRINT_MISMATCH }, |
|
26 |
{ "generic error", LM_SSL_STATUS_GENERIC_ERROR }, |
|
27 |
{ NULL, 0 }, // XXX |
|
28 |
}; |
|
29 |
||
30 |
/// ssl callback function |
|
31 |
/// User function, called when ssl error happens. |
|
32 |
/// XXX: add lm connection object to args? it is not in API, but can be useful, |
|
33 |
/// though, with upvalues it is not required. |
|
34 |
/// A: lm ssl object, ssl status |
|
35 |
/// R: boolean (false if connection process should be terminated) |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
36 |
LmSSLResponse callback_lm_ssl (LmSSL *ssl, LmSSLStatus status, llm_callback_t *cb) |
0 | 37 |
{ |
38 |
int ret; |
|
39 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
40 |
bless_lm_ssl (cb->L, ssl); |
0 | 41 |
// XXX lm_ssl_unref (ssl); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
42 |
luaL_pushenum (cb->L, status, status_lm_ssl); |
0 | 43 |
if (lua_pcall (cb->L, 2, 0, 0)) { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
44 |
W ("SSL callback error: %s", lua_tostring (cb->L, -1)); |
0 | 45 |
lua_pop (cb->L, 1); |
46 |
return LM_SSL_RESPONSE_CONTINUE; |
|
47 |
} |
|
48 |
ret = lua_toboolean (cb->L, -1); |
|
49 |
lua_pop (cb->L, 1); |
|
50 |
if (ret) |
|
51 |
return LM_SSL_RESPONSE_CONTINUE; |
|
52 |
else |
|
53 |
return LM_SSL_RESPONSE_STOP; |
|
54 |
} |
|
55 |
||
56 |
static void string2fingerprint (const char *string, char *buffer) |
|
57 |
{ |
|
58 |
int i; |
|
59 |
for (i = 0; i < 16; i++) { |
|
60 |
int h = g_ascii_xdigit_value ((char)string[i*3]); |
|
61 |
int l = g_ascii_xdigit_value ((char)string[i*3+1]); |
|
62 |
buffer[i] = (char) ((h >= 0 && l >= 0) ? h*16 + l : 0); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
/// lm.ssl.new |
|
67 |
/// Creates new ssl object for use with connection. |
|
68 |
/// You can specify server key fingerprint, callback function for error handling, |
|
69 |
/// both, or neither. Though, fingerprint should go before callback function. |
|
70 |
/// SSL fingerprint is a string like '01:23:45:67:89:AB:CD:EF:FE:DC:BA:98:76:54:32:10'. |
|
71 |
/// A: string (optional ssl fingerprint), ssl callback function (optional) |
|
72 |
/// R: lm ssl object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
73 |
static int new_lm_ssl (lua_State *L) |
0 | 74 |
{ |
75 |
int args = lua_gettop (L); |
|
76 |
LmSSL *ssl; |
|
77 |
if (args == 0) |
|
78 |
ssl = lm_ssl_new (NULL, NULL, NULL, NULL); |
|
79 |
else if (args == 1 && !lua_isfunction (L, 1)) { |
|
80 |
gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
81 |
const char *fingerprint = luaL_checkstring (L, 1); |
|
82 |
||
83 |
if (lua_objlen (L, 1) > 46) |
|
84 |
string2fingerprint (fingerprint, buffer); |
|
85 |
ssl = lm_ssl_new (buffer, NULL, NULL, NULL); |
|
86 |
} else { |
|
87 |
llm_callback_t *cb; |
|
88 |
gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
89 |
||
90 |
if (args > 1) { |
|
91 |
const char *fingerprint = luaL_checkstring (L, 1); |
|
92 |
if (lua_objlen (L, 1) > 46) |
|
93 |
string2fingerprint (fingerprint, buffer); |
|
94 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
95 |
} else |
|
96 |
luaL_argcheck (L, lua_isfunction (L, 1), 1, "function expected"); |
|
97 |
||
98 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
99 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
100 |
cb->L = L; |
|
101 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
102 |
ssl = lm_ssl_new ((args > 1) ? buffer : NULL, (LmSSLFunction)callback_lm_ssl, |
0 | 103 |
cb, (GDestroyNotify)llm_callback_destroy); |
104 |
} |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
105 |
bless_lm_ssl (L, ssl); |
0 | 106 |
lm_ssl_unref (ssl); // XXX |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
107 |
D ("SSL %X created", (int) ssl); |
0 | 108 |
return 1; |
109 |
} |
|
110 |
||
111 |
/// lm.ssl.bless |
|
112 |
/// Blesses given pointer to lm ssl object. |
|
113 |
/// A: lightuserdata (C lm ssl object) |
|
114 |
/// R: lm ssl object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
115 |
static int bless_lua_lm_ssl (lua_State *L) |
0 | 116 |
{ |
117 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm ssl lightuserdata expected"); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
118 |
bless_lm_ssl (L, lua_touserdata (L, 1)); |
0 | 119 |
return 1; |
120 |
} |
|
121 |
||
122 |
/// lm.ssl.supported |
|
123 |
/// Indicates if SSL is supported by loudmouth library. |
|
124 |
/// R: boolean |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
125 |
static int supported_lm_ssl (lua_State *L) |
0 | 126 |
{ |
127 |
lua_pushboolean (L, lm_ssl_is_supported ()); |
|
128 |
return 1; |
|
129 |
} |
|
130 |
||
131 |
/// ssl:fingerprint |
|
132 |
/// Returns fingerprint of remote server. |
|
133 |
/// R: string or nil |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
134 |
static int fingerprint_lm_ssl (lua_State *L) |
0 | 135 |
{ |
136 |
char buffer[48]; |
|
137 |
llm_ssl_t *object = luaL_checklm_ssl (L, 1); |
|
138 |
const gchar *fingerprint = lm_ssl_get_fingerprint (object->ssl); |
|
139 |
if (fingerprint == NULL) |
|
140 |
lua_pushnil (L); |
|
141 |
else { |
|
142 |
snprintf (buffer, 48, |
|
143 |
"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:" |
|
144 |
"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX", |
|
145 |
fingerprint[0], fingerprint[1], fingerprint[2], fingerprint[3], |
|
146 |
fingerprint[4], fingerprint[5], fingerprint[6], fingerprint[7], |
|
147 |
fingerprint[8], fingerprint[9], fingerprint[10], fingerprint[11], |
|
148 |
fingerprint[12], fingerprint[13], fingerprint[14], fingerprint[15]); |
|
149 |
lua_pushlstring (L, buffer, 47); |
|
150 |
} |
|
151 |
return 1; |
|
152 |
} |
|
153 |
||
154 |
/// ssl:pointer |
|
155 |
/// Returns pointer to underlying C structure. |
|
156 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
157 |
static int pointer_lm_ssl (lua_State *L) |
0 | 158 |
{ |
159 |
llm_ssl_t *object = luaL_checklm_ssl (L, 1); |
|
160 |
lua_pushlightuserdata (L, object->ssl); |
|
161 |
return 1; |
|
162 |
} |
|
163 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
164 |
static int gc_lm_ssl (lua_State *L) |
0 | 165 |
{ |
166 |
llm_ssl_t *object = luaL_checklm_ssl (L, 1); |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
167 |
D ("SSL %X gc called", (int) object); |
0 | 168 |
lm_ssl_unref (object->ssl); |
169 |
return 0; |
|
170 |
} |
|
171 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
172 |
const static luaL_Reg reg_f_lm_ssl[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
173 |
{ "new", new_lm_ssl }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
174 |
{ "bless", bless_lua_lm_ssl }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
175 |
{ "supported", supported_lm_ssl }, |
12 | 176 |
{ NULL, NULL }, |
0 | 177 |
}; |
178 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
179 |
const static luaL_Reg reg_m_lm_ssl[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
180 |
{ "fingerprint", fingerprint_lm_ssl }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
181 |
{ "pointer", pointer_lm_ssl }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
182 |
{ "__gc", gc_lm_ssl }, |
12 | 183 |
{ NULL, NULL }, |
0 | 184 |
}; |
185 |
||
186 |
int luaopen_lm_ssl (lua_State *L) |
|
187 |
{ |
|
188 |
luaL_newmetatable (L, "loudmouth.ssl"); |
|
189 |
lua_pushstring (L, "__index"); |
|
190 |
lua_pushvalue (L, -2); |
|
191 |
lua_settable (L, -3); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
192 |
luaL_register (L, NULL, reg_m_lm_ssl); |
0 | 193 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
194 |
lua_newtable (L); // XXX we can specify here exact amount of fields |
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
195 |
luaL_register (L, NULL, reg_f_lm_ssl); |
0 | 196 |
return 1; |
197 |
} |
|
198 |