/
sipuserdb_mysql.erl
executable file
·512 lines (477 loc) · 17.3 KB
/
sipuserdb_mysql.erl
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
%%%-------------------------------------------------------------------
%%% File : sipuserdb_mysql.erl
%%% @author Magnus Ahltorp <ahltorp@nada.kth.se>
%%% @doc MySQL sipuserdb module.
%%%
%%% @since 4 Aug 2005 by Magnus Ahltorp <ahltorp@nada.kth.se>
%%% @end
%%%-------------------------------------------------------------------
-module(sipuserdb_mysql).
%%-compile(export_all).
-behaviour(sipuserdb).
%%--------------------------------------------------------------------
%% External exports - sipuserdb callbacks
%%--------------------------------------------------------------------
-export([yxa_init/0,
get_user_with_address/1,
get_users_for_address_of_record/1,
get_users_for_addresses_of_record/1,
get_users_for_url/1,
get_addresses_for_user/1,
get_addresses_for_users/1,
get_password_for_user/1,
get_classes_for_user/1,
get_telephonenumber_for_user/1,
get_forwards_for_users/1,
get_forward_for_user/1
]).
%%--------------------------------------------------------------------
%% External exports - module specific exported functions
%%--------------------------------------------------------------------
-export([make_sql_statement/2
]).
%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("siprecords.hrl").
%%====================================================================
%% External functions
%%====================================================================
%%--------------------------------------------------------------------
%% @spec () ->
%% Spec |
%% []
%%
%% Spec = term() "OTP supervisor child specification"
%%
%% @doc Perform any necessary startup initialization and return an
%% OTP supervisor child spec if we want to add to
%% sipserver_sup's list. If this sipuserdb_module needs to
%% be persistent, it should be a gen_server and init should
%% just return a spec so that the gen_server is started by
%% the supervisor.
%% @private
%% @end
%%--------------------------------------------------------------------
yxa_init() ->
{ok, [Host, User, Password, Db]} =
get_mysql_params([sipuserdb_mysql_host,
sipuserdb_mysql_user,
sipuserdb_mysql_password,
sipuserdb_mysql_database
]),
%% create a logging function that makes the MySQL subsystem logs
%% end up in the standard YXA log system.
LogFun = fun(Level, Fmt, Args) ->
logger:log(Level, Fmt, Args)
end,
%% mysql server port is optional
MysqlArgs =
case yxa_config:get_env(sipuserdb_mysql_port) of
{ok, Port} ->
[yxa, Host, Port, User, Password, Db, LogFun];
none ->
[yxa, Host, User, Password, Db, LogFun]
end,
[{mysql_dispatcher, {mysql, start_link, MysqlArgs},
permanent, 2000, worker, [mysql_dispatcher]}
].
%% part of yxa_init/0
get_mysql_params(L) ->
get_mysql_params(L, []).
%% part of yxa_init/0
get_mysql_params([H | T], Res) ->
case yxa_config:get_env(H) of
{ok, Value} ->
get_mysql_params(T, [Value | Res]);
none ->
Msg = lists:concat(["sipuserdb_mysql requires you to set the configuration parameter ",
H]),
%% throw as atom to get printed readably in the startup error message
throw(list_to_atom(Msg))
end;
get_mysql_params([], Res) ->
{ok, lists:reverse(Res)}.
%%--------------------------------------------------------------------
%% @spec (Address) ->
%% Username |
%% nomatch |
%% error
%%
%% Address = string() "an address in string format."
%%
%% Username = string()
%%
%% @doc Looks up exactly one user with an Address. Used for
%% example in REGISTER. If there are multiple users with
%% this address in our database, this function returns
%% 'error'.
%% @end
%%--------------------------------------------------------------------
get_user_with_address(Address) ->
Query1 = make_sql_statement(sipuserdb_mysql_get_user,
Address),
case mysql:fetch(yxa, Query1) of
{ok, _, []} ->
Query2 = make_sql_statement(sipuserdb_mysql_get_user_for_address,
Address),
case mysql:fetch(yxa, Query2) of
{ok, _, []} ->
logger:log(debug, "userdb-mysql: No user with name or address ~p", [Address]),
nomatch;
{ok, _, [[User]]} ->
User;
{ok, _, Users} ->
logger:log(debug, "userdb-mysql: More than one user with address ~p (~p)", [Address, lists:append(Users)]),
error;
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for address ~p: ~p", [Address, Reason]),
error
end;
{ok, _, [[User]]} ->
User;
{ok, _, Users} ->
logger:log(debug, "userdb-mysql: More than one user with username ~p (~p)", [Address, lists:append(Users)]),
error;
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for address ~p: ~p", [Address, Reason]),
error
end.
%%--------------------------------------------------------------------
%% @spec (Address) ->
%% Users |
%% error
%%
%% Address = string() "an address in string format."
%%
%% Users = [string()]
%%
%% @doc Get all usernames of users matching an address. Used to
%% find out to which users we should send a request.
%% @end
%%--------------------------------------------------------------------
get_users_for_address_of_record(Address) ->
get_users_for_addresses_of_record([Address]).
%%--------------------------------------------------------------------
%% @spec (In) ->
%% Users
%%
%% In = [string()] "addresses in string format."
%%
%% Users = [string()]
%%
%% @doc Iterate over a list of addresses of record, return all
%% users matching one or more of the addresses, without
%% duplicates.
%% @end
%%--------------------------------------------------------------------
get_users_for_addresses_of_record(AddressList) ->
Query1 = make_sql_statement(sipuserdb_mysql_get_user_for_address,
AddressList),
case mysql:fetch(yxa, Query1) of
{ok, _, Users} ->
Res = lists:usort(lists:append(Users)),
logger:log(debug, "userdb-mysql: Found user(s) ~p for address(es) ~p", [Res, AddressList]),
Res;
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for address(es) ~p: ~p", [AddressList, Reason]),
error
end.
%%--------------------------------------------------------------------
%% @spec (In) ->
%% Addresses
%%
%% In = [string()] "usernames"
%%
%% Addresses = [string()]
%%
%% @doc Iterate over a list of users, return all their addresses
%% without duplicates by using the next function,
%% get_addresses_for_user/1.
%% @end
%%--------------------------------------------------------------------
get_addresses_for_users(In) when is_list(In) ->
Addresses =
lists:foldl(fun(User, Acc) ->
case get_addresses_for_user(User) of
nomatch ->
Acc;
A when is_list(A) ->
[A | Acc]
end
end, [], In),
lists:usort(lists:append(Addresses)).
%%--------------------------------------------------------------------
%% @spec (Username) ->
%% Addresses |
%% error
%%
%% Username = string()
%%
%% Addresses = [string()]
%%
%% @doc Get all possible addresses of a user. Both configured
%% ones, and implicit ones. Used for example to check if a
%% request from a user has an acceptable From: header.
%% @end
%%--------------------------------------------------------------------
get_addresses_for_user(User) ->
Query1 = make_sql_statement(sipuserdb_mysql_get_addresses_for_user,
User),
case mysql:fetch(yxa, Query1) of
{ok, _, []} ->
logger:log(debug, "userdb-mysql: No addressses for user ~p", [User]),
%% Check if there is a user with that name, if so we canonify the username
Query2 = make_sql_statement(sipuserdb_mysql_get_user,
User),
case mysql:fetch(yxa, Query2) of
{ok, _, []} ->
logger:log(debug, "userdb-mysql: No such user ~p", [User]),
nomatch;
{ok, _, _} ->
[local:canonify_user(User)];
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for username ~p: ~p", [User, Reason]),
error
end;
{ok, _, Addresses} ->
FlatAddresses = lists:append(Addresses),
logger:log(debug, "userdb-mysql: Found address(es) ~p for user ~p",
[FlatAddresses, User]),
CanonL = [local:canonify_user(User)],
AddrL = local:canonify_addresses(FlatAddresses),
All = lists:append([CanonL, AddrL]),
lists:usort(All);
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for username ~p: ~p", [User, Reason]),
error
end.
%%--------------------------------------------------------------------
%% @spec (URL) ->
%% Usernames
%%
%% URL = #sipurl{}
%%
%% Usernames = [string()]
%%
%% @doc Given an URL that is typically the Request-URI of an
%% incoming request, make a list of implicit user addresses
%% and return a list of all users matching any of these
%% addresses. This is located in here since user database
%% backends can have their own way of deriving addresses
%% from a Request-URI.
%% @end
%%--------------------------------------------------------------------
get_users_for_url(URL) when is_record(URL, sipurl) ->
Addresses = local:lookup_url_to_addresses(sipuserdb_mysql, URL),
logger:log(debug, "userdb-mysql: Looking for users matching address(es) ~p derived from URL ~p",
[Addresses, sipurl:print(URL)]),
get_users_for_addresses_of_record(Addresses).
%% Attribute fetching functions :
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%--------------------------------------------------------------------
%% @spec (Username) ->
%% Password |
%% nomatch |
%% error
%%
%% Username = string()
%%
%% Password = string()
%%
%% @doc Returns the password for a user.
%% @end
%%--------------------------------------------------------------------
get_password_for_user(User) ->
Query1 = make_sql_statement(sipuserdb_mysql_get_password_for_user,
User),
case mysql:fetch(yxa, Query1) of
{ok, _, []} ->
logger:log(debug, "userdb-mysql: No password found for user ~p", [User]),
nomatch;
{ok, _, [Res]} ->
lists:append(Res);
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for user ~p: ~p", [User, Reason]),
error
end.
%%--------------------------------------------------------------------
%% @spec (Username) ->
%% Classes |
%% nomatch |
%% error
%%
%% Username = string()
%%
%% Classes = [atom()]
%%
%% @doc Returns a list of classes allowed for a user. Classes are
%% used by pstnproxy to determine if it should allow a call
%% to a PSTN number (of a certain class) from a user or not.
%% @end
%%--------------------------------------------------------------------
get_classes_for_user(User) ->
Query1 = make_sql_statement(sipuserdb_mysql_get_classes_for_user,
User),
case mysql:fetch(yxa, Query1) of
{ok, _, Classes} ->
lists:map(fun ([Class]) ->
list_to_atom(Class)
end, Classes);
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for username ~p: ~p", [User, Reason]),
error
end.
%%--------------------------------------------------------------------
%% @spec (Username) ->
%% Number |
%% nomatch |
%% error
%%
%% Username = string()
%%
%% Number = string()
%%
%% @doc Return the telephone number for a user. Return the number
%% as a string which is probably an E.164 number or just a
%% string with digits. The numbering plan in the number
%% return is not specified.
%% @end
%%--------------------------------------------------------------------
get_telephonenumber_for_user(User) ->
Query1 = make_sql_statement(sipuserdb_mysql_get_telephonenumber_for_user,
User),
case mysql:fetch(yxa, Query1) of
{ok, _, []} ->
logger:log(debug, "userdb-mysql: No numbers for user ~p", [User]),
nomatch;
{ok, _, [["tel:" ++ Rest] | _]} ->
Rest;
{ok, _, [[FirstNumber] | _]} when is_list(FirstNumber) ->
FirstNumber;
{error, Reason} ->
logger:log(error, "userdb-mysql: Error for username ~p: ~p", [User, Reason]),
error
end.
%%--------------------------------------------------------------------
%% @spec (In) ->
%% ForwardList
%%
%% In = [string()] "list of usernames"
%%
%% ForwardList = [#sipproxy_forward{}]
%%
%% @doc Return a list of forward addresses for a list of users.
%% Uses the next function, get_forward_for_user/1.
%% @end
%%--------------------------------------------------------------------
get_forwards_for_users(_In) ->
nomatch.
%%--------------------------------------------------------------------
%% @spec (Username) ->
%% ForwardList |
%% nomatch |
%% error
%%
%% Username = string()
%%
%% ForwardList = [#sipproxy_forward{}]
%%
%% @doc Return the forward address(es) for a user.
%% @end
%%--------------------------------------------------------------------
get_forward_for_user(_User) ->
nomatch.
%%--------------------------------------------------------------------
%% @spec (In, Args) ->
%% Query
%%
%% In = CfgKey | Template
%% CfgKey = atom()
%% Template = string()
%% Args = [term()] "SQL query key(s)"
%%
%% Query = string()
%%
%% @doc Construct an SQL query statement given a configuration
%% parameter name or a string template. If Args consists of
%% more than one element, SQL ' or ' will be used.
%% @end
%%--------------------------------------------------------------------
make_sql_statement(CfgKey, Args) when is_atom(CfgKey) ->
case local:sipuserdb_mysql_make_sql_statement(CfgKey, Args) of
{ok, Res} when is_list(Res) ->
logger:log(debug, "userdb-mysql: ~p -> ~p (from local)", [CfgKey, Res]),
Res;
undefined ->
{ok, Template} = yxa_config:get_env(CfgKey),
Res = make_sql_statement(Template, Args),
logger:log(debug, "userdb-mysql: ~p -> ~p", [CfgKey, Res]),
Res
end;
make_sql_statement(Template, Args) when is_list(Template), is_list(Args) ->
make_sql_statement2(Template, Args, []).
%%====================================================================
%% Internal functions
%%====================================================================
%%--------------------------------------------------------------------
%% @spec (Template, Args, []) ->
%% Query
%%
%% Template = string()
%% Args = [term()] "SQL query key(s)"
%%
%% Query = string()
%%
%% @doc Construct an SQL query statement given a template,
%% replacing all occurrences of '?' with either Args (if it
%% is a single-value argument) or an SQL ' or ' list if Args
%% is multi-value. The latter of course requires that we can
%% identify the SQL variable name.
%% @end
%%--------------------------------------------------------------------
make_sql_statement2([$? | T], Args, Res) ->
%% ok, ? found
NewRes = make_sql_statement_insert(Args, Res),
%% keep looking for more ? until T is empty
make_sql_statement2(T, Args, NewRes);
make_sql_statement2([H | T], Args, Res) ->
%% non-?
make_sql_statement2(T, Args, [H | Res]);
make_sql_statement2([], _Args, Res) ->
lists:reverse(binary_to_list( list_to_binary(Res) )).
make_sql_statement_insert([H | _T] = Args, Res) when is_list(H) ->
%% Args is a list, make "foo = bar OR foo = baz" statement
%% get the 'foo' token, since Res is reversed this is at the
%% beginning of Res
{ok, SqlVar, Rest} = make_sql_statement_insert_get_sqlvar(Res),
OrStrR = lists:reverse( sqlwhere(SqlVar, Args) ),
[OrStrR | Rest];
make_sql_statement_insert(Arg, Res) when is_list(Arg) ->
%% Arg is a single element
This = lists:reverse( mysql:quote(Arg) ),
[This | Res].
make_sql_statement_insert_get_sqlvar([32 | T]) -> %% 32 is ' '
%% space before sql-var starts, ignore
make_sql_statement_insert_get_sqlvar(T);
make_sql_statement_insert_get_sqlvar([61 | T]) -> %% 61 is '-'
%% equal-sign before sql-var starts, ignore
make_sql_statement_insert_get_sqlvar(T);
make_sql_statement_insert_get_sqlvar([H | T]) ->
%% sql-var starts here
make_sql_statement_insert_get_sqlvar2(T, [H]);
make_sql_statement_insert_get_sqlvar([]) ->
throw({error, "invalid SQL-statement, no distinguishable variable name start"}).
make_sql_statement_insert_get_sqlvar2([32 | T], Res) ->
%% we are finished
{ok, Res, [32 | T]};
make_sql_statement_insert_get_sqlvar2([H | T], Res) ->
%% not finished, keep looking
make_sql_statement_insert_get_sqlvar2(T, [H | Res]);
make_sql_statement_insert_get_sqlvar2([], _Res) ->
throw({error, "invalid SQL-statement, no distinguishable variable name end"}).
%% part of make_sql_statement_insert
sqlwhere(Field, [Value]) ->
Field ++ " = " ++ mysql:quote(Value);
sqlwhere(Field, [Value | Rest]) ->
Field ++ " = " ++ mysql:quote(Value) ++ " or " ++ sqlwhere(Field, Rest).