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
|
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%%
%% $Id$
%%
-module(mod_auth_server).
-include("httpd.hrl").
-behaviour(gen_server).
%% mod_auth exports
-export([start/2, stop/2,
add_password/4, update_password/5,
add_user/5, delete_user/5, get_user/5, list_users/4,
add_group_member/6, delete_group_member/6, list_group_members/5,
delete_group/5, list_groups/4]).
%% gen_server exports
-export([start_link/2, init/1,
handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
%% We will not make the change to use base64 in stdlib in inets just yet.
%% it will be included in the next major release of inets.
-compile({nowarn_deprecated_function, {http_base_64, encode, 1}}).
-record(state,{tab}).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% External API %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% start_link/3
%%
%% NOTE: This is called by httpd_misc_sup when the process is started
%%
start_link(Addr, Port)->
Name = make_name(Addr, Port),
gen_server:start_link({local, Name}, ?MODULE, [], [{timeout, infinity}]).
%% start/2
start(Addr, Port)->
Name = make_name(Addr, Port),
case whereis(Name) of
undefined ->
httpd_misc_sup:start_auth_server(Addr, Port);
_ -> %% Already started...
ok
end.
%% stop/2
stop(Addr, Port)->
Name = make_name(Addr, Port),
case whereis(Name) of
undefined -> %% Already stopped
ok;
_ ->
(catch httpd_misc_sup:stop_auth_server(Addr, Port))
end.
%% add_password/4
add_password(Addr, Port, Dir, Password)->
Name = make_name(Addr, Port),
Req = {add_password, Dir, Password},
call(Name, Req).
%% update_password/6
update_password(Addr, Port, Dir, Old, New) when list(New) ->
Name = make_name(Addr, Port),
Req = {update_password, Dir, Old, New},
call(Name, Req).
%% add_user/5
add_user(Addr, Port, Dir, User, Password) ->
Name = make_name(Addr, Port),
Req = {add_user, Addr, Port, Dir, User, Password},
call(Name, Req).
%% delete_user/5
delete_user(Addr, Port, Dir, UserName, Password) ->
Name = make_name(Addr, Port),
Req = {delete_user, Addr, Port, Dir, UserName, Password},
call(Name, Req).
%% get_user/5
get_user(Addr, Port, Dir, UserName, Password) ->
Name = make_name(Addr, Port),
Req = {get_user, Addr, Port, Dir, UserName, Password},
call(Name, Req).
%% list_users/4
list_users(Addr, Port, Dir, Password) ->
Name = make_name(Addr,Port),
Req = {list_users, Addr, Port, Dir, Password},
call(Name, Req).
%% add_group_member/6
add_group_member(Addr, Port, Dir, GroupName, UserName, Password) ->
Name = make_name(Addr,Port),
Req = {add_group_member, Addr, Port, Dir, GroupName, UserName, Password},
call(Name, Req).
%% delete_group_member/6
delete_group_member(Addr, Port, Dir, GroupName, UserName, Password) ->
Name = make_name(Addr,Port),
Req = {delete_group_member, Addr, Port, Dir, GroupName, UserName, Password},
call(Name, Req).
%% list_group_members/4
list_group_members(Addr, Port, Dir, Group, Password) ->
Name = make_name(Addr, Port),
Req = {list_group_members, Addr, Port, Dir, Group, Password},
call(Name, Req).
%% delete_group/5
delete_group(Addr, Port, Dir, GroupName, Password) ->
Name = make_name(Addr, Port),
Req = {delete_group, Addr, Port, Dir, GroupName, Password},
call(Name, Req).
%% list_groups/4
list_groups(Addr, Port, Dir, Password) ->
Name = make_name(Addr, Port),
Req = {list_groups, Addr, Port, Dir, Password},
call(Name, Req).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% Server call-back functions %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% init
init(_) ->
{ok,#state{tab = ets:new(auth_pwd,[set,protected])}}.
%% handle_call
%% Add a user
handle_call({add_user, Addr, Port, Dir, User, AuthPwd}, _From, State) ->
Reply = api_call(Addr, Port, Dir, add_user, User, AuthPwd, State),
{reply, Reply, State};
%% Get data about a user
handle_call({get_user, Addr, Port, Dir, User, AuthPwd}, _From, State) ->
Reply = api_call(Addr, Port, Dir, get_user, [User], AuthPwd, State),
{reply, Reply, State};
%% Add a group member
handle_call({add_group_member, Addr, Port, Dir, Group, User, AuthPwd},
_From, State) ->
Reply = api_call(Addr, Port, Dir, add_group_member, [Group, User],
AuthPwd, State),
{reply, Reply, State};
%% delete a group
handle_call({delete_group_member, Addr, Port, Dir, Group, User, AuthPwd},
_From, State)->
Reply = api_call(Addr, Port, Dir, delete_group_member, [Group, User],
AuthPwd, State),
{reply, Reply, State};
%% List all users thats standalone users
handle_call({list_users, Addr, Port, Dir, AuthPwd}, _From, State)->
Reply = api_call(Addr, Port, Dir, list_users, [], AuthPwd, State),
{reply, Reply, State};
%% Delete a user
handle_call({delete_user, Addr, Port, Dir, User, AuthPwd}, _From, State)->
Reply = api_call(Addr, Port, Dir, delete_user, [User], AuthPwd, State),
{reply, Reply, State};
%% Delete a group
handle_call({delete_group, Addr, Port, Dir, Group, AuthPwd}, _From, State)->
Reply = api_call(Addr, Port, Dir, delete_group, [Group], AuthPwd, State),
{reply, Reply, State};
%% List the current groups
handle_call({list_groups, Addr, Port, Dir, AuthPwd}, _From, State)->
Reply = api_call(Addr, Port, Dir, list_groups, [], AuthPwd, State),
{reply, Reply, State};
%% List the members of the given group
handle_call({list_group_members, Addr, Port, Dir, Group, AuthPwd},
_From, State)->
Reply = api_call(Addr, Port, Dir, list_group_members, [Group],
AuthPwd, State),
{reply, Reply, State};
%% Add password for a directory
handle_call({add_password, Dir, Password}, _From, State)->
Reply = do_add_password(Dir, Password, State),
{reply, Reply, State};
%% Update the password for a directory
handle_call({update_password, Dir, Old, New},_From,State)->
Reply =
case getPassword(State, Dir) of
OldPwd when binary(OldPwd)->
case erlang:md5(Old) of
OldPwd ->
%% The old password is right =>
%% update the password to the new
do_update_password(Dir,New,State),
ok;
_->
{error, error_new}
end;
_->
{error, error_old}
end,
{reply, Reply, State};
handle_call(stop, _From, State)->
{stop, normal, State}.
handle_info(_Info, State)->
{noreply, State}.
handle_cast(_Request, State)->
{noreply, State}.
terminate(_Reason,State) ->
ets:delete(State#state.tab),
ok.
%% code_change({down, ToVsn}, State, Extra)
%%
code_change({down, _}, #state{tab = Tab}, downgrade_to_2_6_0) ->
{ok, {state, Tab, undefined}};
%% code_change(FromVsn, State, Extra)
%%
code_change(_, {state, Tab, _}, upgrade_from_2_6_0) ->
{ok, #state{tab = Tab}}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% The functions that really changes the data in the database %%
%% of users to different directories %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% API gateway
api_call(Addr, Port, Dir, Func, Args,Password,State) ->
case controlPassword(Password,State,Dir) of
ok->
ConfigName = httpd_util:make_name("httpd_conf",Addr,Port),
case ets:match_object(ConfigName, {directory, Dir, '$1'}) of
[{directory, Dir, DirData}] ->
AuthMod = auth_mod_name(DirData),
(catch apply(AuthMod, Func, [DirData|Args]));
_ ->
{error, no_such_directory}
end;
bad_password ->
{error,bad_password}
end.
controlPassword(Password, _State, _Dir) when Password=:="DummyPassword"->
bad_password;
controlPassword(Password,State,Dir)->
case getPassword(State,Dir) of
Pwd when binary(Pwd)->
case erlang:md5(Password) of
Pwd ->
ok;
_->
bad_password
end;
_ ->
bad_password
end.
getPassword(State,Dir)->
case lookup(State#state.tab, Dir) of
[{_,Pwd}]->
Pwd;
_ ->
{error,bad_password}
end.
do_update_password(Dir, New, State) ->
ets:insert(State#state.tab, {Dir, erlang:md5(New)}).
do_add_password(Dir, Password, State) ->
case getPassword(State,Dir) of
PwdExists when binary(PwdExists) ->
{error, dir_protected};
{error, _} ->
do_update_password(Dir, Password, State)
end.
auth_mod_name(DirData) ->
case httpd_util:key1search(DirData, auth_type, plain) of
plain -> mod_auth_plain;
mnesia -> mod_auth_mnesia;
dets -> mod_auth_dets
end.
lookup(Db, Key) ->
ets:lookup(Db, Key).
make_name(Addr,Port) ->
httpd_util:make_name("httpd_auth",Addr,Port).
call(Name, Req) ->
case (catch gen_server:call(Name, Req)) of
{'EXIT', Reason} ->
{error, Reason};
Reply ->
Reply
end.
|