-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql.lua
More file actions
395 lines (318 loc) · 10.1 KB
/
Copy pathmysql.lua
File metadata and controls
395 lines (318 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
--[[
MasterLua Authserver MySQL Database backend, based on the one of Hopmod Authserver and some functions from the stats module of Hopmod
]]
require "luasql_mysql"
local connection = {}
connection.settings = {}
local msg = {}
msg.no_domain = "Domain, %s does not exist."
msg.domain = "Domain, %s already exists."
msg.no_user = "User, %s@%s does not exist."
msg.user = "User, %s@%s already exists."
msg.same = "%s is named %s."
msg.mysql = {}
msg.mysql.unknown = "Connection to the mysql database at %s:%s failed."
msg.mysql.no_insert = "Inserting %s failed."
msg.mysql.no_delete = "Deleting %s failed."
msg.mysql.no_update = "Updating %s failed."
msg.mysql.no_select = "Fetching %s failed."
local function readWholeFile(filename)
local file, err = io.open(filename)
if not file then error(err) end
return file:read("*a")
end
local function execute_statement(statement)
local cursor, errorinfo = connection:execute(statement)
if not cursor then
connection:close()
connection = nil
return nil, errorinfo
end
return cursor
end
local function add_user(name, domain_id, pubkey, domain)
if not mysql.exec(connection, "START TRANSACTION")
then
return msg.mysql.unknown
end
local cursor = mysql.exec(connection, string.format("INSERT INTO users (name, domain_id, pubkey) VALUES('%s', %i, '%s')", mysql.escape_string(name), domain_id, mysql.escape_string(pubkey)))
if not cursor
then
return string.format(msg.mysql.no_insert, string.format("%s@%s", name, domain))
end
if not mysql.exec(connection, "COMMIT")
then
return msg.mysql.unknown
end
return
end
local function install_db(connection, settings)
local schema = readWholeFile(settings.schema)
for statement in string.gmatch(schema, "CREATE TABLE[^;]+") do
local cursor, err = execute_statement(statement)
if not cursor then error(err) end
end
end
local function external_load(settings)
connection = luasql.mysql():connect(settings.name, settings.user, settings.pass, settings.host, settings.port)
if not connection then
return nil, string.format(msg.mysql.unknown, settings.host, settings.port)
end
if settings.install then
install_db(connection, settings)
end
local domains_and_users = {}
users_query = execute_statement("SELECT DISTINCT name, domain, pubkey, rights FROM users")
row = users_query:fetch({}, "a")
while row
do
if not domains_and_users[row.domain] then
domains_and_users[row.domain] = {}
end
domains_and_users[row.domain][row.name:lower()] = {row.pubkey,row.rights}
row = users_query:fetch (row, "a")
end
return domains_and_users
end
local function external_add_user(name, domain, pubkey)
local did = domain_id(domain)
if not did
then
return string.format(msg.no_domain, domain)
end
if is_user(name, did)
then
string.format(msg.user, name, domain)
end
return add_user(name, did, pubkey, domain)
end
local function external_del_user(name, domain)
local did = domain_id(domain)
if not did
then
return string.format(msg.no_domain, domain)
end
if not is_user(name, did)
then
return string.format(msg.no_user, name, domain)
end
if not mysql.exec(connection, "START TRANSACTION")
then
return msg.mysql.unknown
end
local cursor = mysql.exec(connection, string.format("DELETE FROM users WHERE domain_id = %i AND name = '%s'", did, mysql.escape_string(name)))
if not cursor
then
return string.format(msg.mysql.no_delete, string.format("%s@%s", name, domain))
end
if not mysql.exec(connection, "COMMIT")
then
return msg.mysql.unknown
end
return
end
local function external_change_user_name(name, domain, new_name)
if name == new_name
then
return nil, string.format(msg.same, name, new_name)
end
local did = domain_id(domain)
if not did
then
return nil, string.format(msg.no_domain, domain)
end
if not is_user(name, did)
then
return nil, string.format(msg.no_user, name, domain)
end
if is_user(new_name, did)
then
return nil, string.format(msg.user, new_name, domain)
end
if not mysql.exec(connection, "START TRANSACTION")
then
return nil, msg.mysql.unknown
end
local cursor = mysql.exec(connection, string.format("UPDATE users SET name = '%s' WHERE domain_id = %i AND name = '%s'", mysql.escape_string(new_name), did, mysql.escape_string(name)))
if not cursor
then
return nil, string.format(msg.mysql.no_update, string.format("%s@%s", name, domain))
end
if not mysql.exec(connection, "COMMIT")
then
return nil, msg.mysql.unknown
end
local key = pubkey(new_name, did)
if not key
then
return nil, string.format(msg.mysql.no_select, "pubkey")
end
return key
end
local function external_change_user_key(name, domain, new_pubkey)
local did = domain_id(domain)
if not did
then
return string.format(msg.no_domain, domain)
end
if not is_user(name, did)
then
return string.format(msg.no_user, name, domain)
end
if not mysql.exec(connection, "START TRANSACTION")
then
return msg.mysql.unknown
end
local cursor = mysql.exec(connection, string.format("UPDATE users SET pubkey = '%s' WHERE domain_id = %i AND name = '%s'", mysql.escape_string(new_pubkey), did, mysql.escape_string(name)))
if not cursor
then
return string.format(msg.mysql.no_update, string.format("%s@%s", name, domain))
end
if not mysql.exec(connection, "COMMIT")
then
return msg.mysql.unknown
end
return
end
local function external_change_user_domain(name, domain, new_domain)
if domain == new_domain
then
return nil, string.format(msg.same, domain, new_domain)
end
local did = domain_id(domain)
if not did
then
return nil, string.format(msg.no_domain, domain)
end
local new_did = domain_id(new_domain)
if not new_did
then
return nil, string.format(msg.no_domain, new_domain)
end
if not is_user(name, did)
then
return nil, string.format(msg.no_user, name, domain)
end
if is_user(name, new_did)
then
return nil, string.format(msg.user, name, new_domain)
end
if not mysql.exec(connection, "START TRANSACTION")
then
return nil, msg.mysql.unknown
end
local cursor = mysql.exec(connection, string.format("UPDATE users SET domain_id = %i WHERE domain_id = %i AND name = '%s'", new_did, did, mysql.escape_string(name)))
if not cursor
then
return nil, string.format(msg.mysql.no_update, string.format("%s@%s", name, domain))
end
if not mysql.exec(connection, "COMMIT")
then
return nil, msg.mysql.unknown
end
local key = pubkey(name, new_did)
if not key
then
return nil, string.format(msg.mysql.no_select, "pubkey")
end
return key
end
-- internal.change_domain_name(domain, new_domain)
-- nil, err or users, nil
-- users[name] = pubkey
local function external_change_domain_name(domain, new_domain)
if domain == new_domain
then
return nil, string.format(msg.same, domain, new_domain)
end
local did = domain_id(domain)
if not did
then
return nil, string.format(msg.no_domain, domain)
end
local users = list_users(did)
local new_did = domain_id(new_domain)
if not new_did
then
local domain_case = mysql.row(mysql.exec(connection, string.format("SELECT case_insensitive FROM domains WHERE id = %i", did)))
if not domain_case
then
return nil, string.format(msg.mysql.no_select, "case_insensitive")
else
if domain_case.case_insensitive == "1"
then
domain_case = true
else
domain_case = nil
end
end
local add_domain_err = add_domain(new_domain, domain_case)
if add_domain_err
then
return nil, add_domain_err
end
new_did = domain_id(new_domain)
if not new_did
then
return nil, string.format(msg.mysql.no_select, "domain_id")
end
else
local domain_case = mysql.row(mysql.exec(connection, string.format("SELECT case_insensitive FROM domains WHERE id = %i", did)))
if not domain_case
then
return nil, string.format(msg.mysql.no_select, "case_insensitive")
else
if domain_case.case_insensitive == "1"
then
domain_case = true
else
domain_case = false
end
end
local new_domain_case = mysql.row(mysql.exec(connection, string.format("SELECT case_insensitive FROM domains WHERE id = %i", new_did)))
if not new_domain_case
then
return nil, string.format(msg.mysql.no_select, "case_insensitive")
else
if new_domain_case.case_insensitive == "1"
then
new_domain_case = true
else
new_domain_case = false
end
end
if (domain_case and not new_domain_case) or (not domain_case and new_domain_case)
then
return nil, "New domain already exists and has not the same case sensitive setting."
end
for name, _ in pairs(users)
do
if is_user(name, new_did)
then
return nil, string.format(msg.user, name, new_domain)
end
end
end
for name, key in pairs(users)
do
add_user(name, new_did, key, new_domain)
end
del_domain(did, domain)
return users
end
local function external_close_connection()
mysql.close(connection)
end
return {is_user = external_is_user,
add_user = external_add_user,
del_user = external_del_user,
change_user_name = external_change_user_name,
change_user_key = external_change_user_key,
change_user_domain = external_change_user_domain,
list_users = external_list_users,
add_domain = external_add_domain,
del_domain = external_del_domain,
change_domain_name = external_change_domain_name,
change_domain_sensitivity = external_change_domain_sensitivity,
load = external_load,
close = external_close_connection}