author | Myhailo Danylenko <isbear@ukrpost.net> |
Wed, 28 Nov 2012 01:56:15 +0200 | |
changeset 54 | 6bef2082e5f9 |
parent 42 | 61d0ab29f17f |
child 57 | 37ed3c7ac1b6 |
permissions | -rw-r--r-- |
23 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
||
4 |
This file is part of lua-lm. |
|
5 |
||
6 |
lua-lm is free software: you can redistribute it and/or modify |
|
7 |
it under the terms of the GNU General Public License as published by |
|
8 |
the Free Software Foundation, either version 2 of the License, or |
|
9 |
(at your option) any later version. |
|
10 |
||
11 |
This program is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
GNU General Public License for more details. |
|
15 |
||
16 |
You should have received a copy of the GNU General Public License |
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
0 | 18 |
|
19 |
#include <lua.h> |
|
20 |
#include <lauxlib.h> |
|
21 |
#include <glib.h> |
|
22 |
#include <loudmouth/loudmouth.h> |
|
23 |
#include <stdio.h> |
|
24 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
4
diff
changeset
|
25 |
#include "config.h" |
0 | 26 |
#include "util.h" |
27 |
#include "lm_types.h" |
|
28 |
||
29 |
/// lm.ssl |
|
30 |
/// Object, containing information about ssl abilities for connection. |
|
31 |
/// Create, set parameters, and attach to connection with 'ssl' method. |
|
32 |
||
33 |
/// ssl status |
|
34 |
/// String, representing what problem have current ssl session. |
|
4 | 35 |
/// G: |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
36 |
const string2enum_t status_lm_ssl[] = { |
0 | 37 |
{ "no cert found", LM_SSL_STATUS_NO_CERT_FOUND }, |
38 |
{ "untrusted cert", LM_SSL_STATUS_UNTRUSTED_CERT }, |
|
39 |
{ "cert expired", LM_SSL_STATUS_CERT_EXPIRED }, |
|
40 |
{ "cert not activated", LM_SSL_STATUS_CERT_NOT_ACTIVATED }, |
|
41 |
{ "cert hostname mismatch", LM_SSL_STATUS_CERT_HOSTNAME_MISMATCH }, |
|
42 |
{ "cert fingerprint mismatch", LM_SSL_STATUS_CERT_FINGERPRINT_MISMATCH }, |
|
43 |
{ "generic error", LM_SSL_STATUS_GENERIC_ERROR }, |
|
44 |
{ NULL, 0 }, // XXX |
|
45 |
}; |
|
46 |
||
47 |
/// ssl callback function |
|
48 |
/// User function, called when ssl error happens. |
|
42 | 49 |
/// A: userdata (lm ssl object), argument enum field (ssl status) |
0 | 50 |
/// 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
|
51 |
LmSSLResponse callback_lm_ssl (LmSSL *ssl, LmSSLStatus status, llm_callback_t *cb) |
0 | 52 |
{ |
53 |
int ret; |
|
54 |
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
|
55 |
bless_lm_ssl (cb->L, ssl); |
0 | 56 |
// XXX lm_ssl_unref (ssl); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
57 |
luaL_pushenum (cb->L, status, status_lm_ssl); |
42 | 58 |
if (lua_pcall (cb->L, 2, 1, 0)) { |
9
50f55d494efb
Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
59 |
W ("SSL callback error: %s", lua_tostring (cb->L, -1)); |
0 | 60 |
lua_pop (cb->L, 1); |
61 |
return LM_SSL_RESPONSE_CONTINUE; |
|
62 |
} |
|
63 |
ret = lua_toboolean (cb->L, -1); |
|
64 |
lua_pop (cb->L, 1); |
|
65 |
if (ret) |
|
66 |
return LM_SSL_RESPONSE_CONTINUE; |
|
67 |
else |
|
68 |
return LM_SSL_RESPONSE_STOP; |
|
69 |
} |
|
70 |
||
71 |
static void string2fingerprint (const char *string, char *buffer) |
|
72 |
{ |
|
73 |
int i; |
|
74 |
for (i = 0; i < 16; i++) { |
|
75 |
int h = g_ascii_xdigit_value ((char)string[i*3]); |
|
76 |
int l = g_ascii_xdigit_value ((char)string[i*3+1]); |
|
77 |
buffer[i] = (char) ((h >= 0 && l >= 0) ? h*16 + l : 0); |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
/// lm.ssl.new |
|
82 |
/// Creates new ssl object for use with connection. |
|
83 |
/// You can specify server key fingerprint, callback function for error handling, |
|
84 |
/// both, or neither. Though, fingerprint should go before callback function. |
|
85 |
/// SSL fingerprint is a string like '01:23:45:67:89:AB:CD:EF:FE:DC:BA:98:76:54:32:10'. |
|
86 |
/// A: string (optional ssl fingerprint), ssl callback function (optional) |
|
42 | 87 |
/// R: userdata (lm ssl object) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
88 |
static int new_lm_ssl (lua_State *L) |
0 | 89 |
{ |
90 |
int args = lua_gettop (L); |
|
91 |
LmSSL *ssl; |
|
92 |
if (args == 0) |
|
93 |
ssl = lm_ssl_new (NULL, NULL, NULL, NULL); |
|
94 |
else if (args == 1 && !lua_isfunction (L, 1)) { |
|
95 |
gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
96 |
const char *fingerprint = luaL_checkstring (L, 1); |
|
97 |
||
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
42
diff
changeset
|
98 |
if (lua_rawlen (L, 1) > 46) |
0 | 99 |
string2fingerprint (fingerprint, buffer); |
100 |
ssl = lm_ssl_new (buffer, NULL, NULL, NULL); |
|
101 |
} else { |
|
102 |
llm_callback_t *cb; |
|
103 |
gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; |
|
104 |
||
105 |
if (args > 1) { |
|
106 |
const char *fingerprint = luaL_checkstring (L, 1); |
|
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
42
diff
changeset
|
107 |
if (lua_rawlen (L, 1) > 46) |
0 | 108 |
string2fingerprint (fingerprint, buffer); |
109 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
110 |
} else |
|
111 |
luaL_argcheck (L, lua_isfunction (L, 1), 1, "function expected"); |
|
112 |
||
113 |
cb = luaL_malloc (L, sizeof (llm_callback_t)); |
|
114 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
115 |
cb->L = L; |
|
116 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
117 |
ssl = lm_ssl_new ((args > 1) ? buffer : NULL, (LmSSLFunction)callback_lm_ssl, |
0 | 118 |
cb, (GDestroyNotify)llm_callback_destroy); |
119 |
} |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
120 |
bless_lm_ssl (L, ssl); |
0 | 121 |
lm_ssl_unref (ssl); // XXX |
38
34a2b880615c
Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
122 |
D ("SSL %p created", ssl); |
0 | 123 |
return 1; |
124 |
} |
|
125 |
||
126 |
/// lm.ssl.bless |
|
127 |
/// Blesses given pointer to lm ssl object. |
|
128 |
/// A: lightuserdata (C lm ssl object) |
|
42 | 129 |
/// R: userdata (lm ssl object) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
130 |
static int bless_lua_lm_ssl (lua_State *L) |
0 | 131 |
{ |
132 |
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
|
133 |
bless_lm_ssl (L, lua_touserdata (L, 1)); |
0 | 134 |
return 1; |
135 |
} |
|
136 |
||
137 |
/// lm.ssl.supported |
|
138 |
/// Indicates if SSL is supported by loudmouth library. |
|
139 |
/// R: boolean |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
140 |
static int supported_lm_ssl (lua_State *L) |
0 | 141 |
{ |
142 |
lua_pushboolean (L, lm_ssl_is_supported ()); |
|
143 |
return 1; |
|
144 |
} |
|
145 |
||
146 |
/// ssl:fingerprint |
|
147 |
/// Returns fingerprint of remote server. |
|
148 |
/// R: string or nil |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
149 |
static int fingerprint_lm_ssl (lua_State *L) |
0 | 150 |
{ |
151 |
char buffer[48]; |
|
152 |
llm_ssl_t *object = luaL_checklm_ssl (L, 1); |
|
153 |
const gchar *fingerprint = lm_ssl_get_fingerprint (object->ssl); |
|
154 |
if (fingerprint == NULL) |
|
155 |
lua_pushnil (L); |
|
156 |
else { |
|
157 |
snprintf (buffer, 48, |
|
158 |
"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:" |
|
159 |
"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX", |
|
160 |
fingerprint[0], fingerprint[1], fingerprint[2], fingerprint[3], |
|
161 |
fingerprint[4], fingerprint[5], fingerprint[6], fingerprint[7], |
|
162 |
fingerprint[8], fingerprint[9], fingerprint[10], fingerprint[11], |
|
163 |
fingerprint[12], fingerprint[13], fingerprint[14], fingerprint[15]); |
|
164 |
lua_pushlstring (L, buffer, 47); |
|
165 |
} |
|
166 |
return 1; |
|
167 |
} |
|
168 |
||
42 | 169 |
/// ssl:tls |
170 |
/// Sets or returns use of starttls by this ssl object. |
|
171 |
/// A: boolean (use starttls), boolean (require starttls) |
|
172 |
/// or |
|
173 |
/// R: boolean (use starttls), boolean (require starttls) |
|
174 |
static int tls_lm_ssl (lua_State *L) |
|
175 |
{ |
|
176 |
llm_ssl_t *object = luaL_checklm_ssl (L, 1); |
|
177 |
if (lua_gettop (L) > 1) { |
|
178 |
gboolean use = lua_toboolean (L, 2); |
|
179 |
gboolean require = lua_toboolean (L, 3); |
|
180 |
lm_ssl_use_starttls (object -> ssl, use, require); |
|
181 |
return 0; |
|
182 |
} else { |
|
183 |
lua_pushboolean (L, lm_ssl_get_use_starttls (object -> ssl)); |
|
184 |
lua_pushboolean (L, lm_ssl_get_require_starttls (object -> ssl)); |
|
185 |
return 2; |
|
186 |
} |
|
187 |
} |
|
188 |
||
0 | 189 |
/// ssl:pointer |
190 |
/// Returns pointer to underlying C structure. |
|
191 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
192 |
static int pointer_lm_ssl (lua_State *L) |
0 | 193 |
{ |
194 |
llm_ssl_t *object = luaL_checklm_ssl (L, 1); |
|
195 |
lua_pushlightuserdata (L, object->ssl); |
|
196 |
return 1; |
|
197 |
} |
|
198 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
199 |
static int gc_lm_ssl (lua_State *L) |
0 | 200 |
{ |
201 |
llm_ssl_t *object = luaL_checklm_ssl (L, 1); |
|
38
34a2b880615c
Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
202 |
D ("SSL %p gc called", object); |
0 | 203 |
lm_ssl_unref (object->ssl); |
204 |
return 0; |
|
205 |
} |
|
206 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
207 |
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
|
208 |
{ "new", new_lm_ssl }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
209 |
{ "bless", bless_lua_lm_ssl }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
210 |
{ "supported", supported_lm_ssl }, |
12 | 211 |
{ NULL, NULL }, |
0 | 212 |
}; |
213 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
214 |
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
|
215 |
{ "fingerprint", fingerprint_lm_ssl }, |
42 | 216 |
{ "tls", tls_lm_ssl }, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
217 |
{ "pointer", pointer_lm_ssl }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
218 |
{ "__gc", gc_lm_ssl }, |
12 | 219 |
{ NULL, NULL }, |
0 | 220 |
}; |
221 |
||
222 |
int luaopen_lm_ssl (lua_State *L) |
|
223 |
{ |
|
224 |
luaL_newmetatable (L, "loudmouth.ssl"); |
|
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
225 |
lua_pushvalue (L, -1); |
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
226 |
lua_setfield (L, -2, "__index"); |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
42
diff
changeset
|
227 |
luaL_setfuncs (L, reg_m_lm_ssl, 0); |
0 | 228 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
229 |
lua_newtable (L); // XXX we can specify here exact amount of fields |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
42
diff
changeset
|
230 |
luaL_setfuncs (L, reg_f_lm_ssl, 0); |
0 | 231 |
return 1; |
232 |
} |
|
233 |