1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
16
%% $Id: mod_auth_dets.erl,v 1.1 2008/12/17 09:53:35 mikpe Exp $
18
-module(mod_auth_dets).
20
%% dets authentication storage
29
delete_group_member/3,
33
-export([store_directory_data/2]).
35
-include("httpd.hrl").
36
-include("mod_auth.hrl").
38
store_directory_data(Directory, DirData) ->
39
?CDEBUG("store_directory_data -> ~n"
42
[Directory, DirData]),
44
PWFile = httpd_util:key1search(DirData, auth_user_file),
45
GroupFile = httpd_util:key1search(DirData, auth_group_file),
46
Addr = httpd_util:key1search(DirData, bind_address),
47
Port = httpd_util:key1search(DirData, port),
49
PWName = httpd_util:make_name("httpd_dets_pwdb",Addr,Port),
50
case dets:open_file(PWName,[{type,set},{file,PWFile},{repair,true}]) of
52
GDBName = httpd_util:make_name("httpd_dets_groupdb",Addr,Port),
53
case dets:open_file(GDBName,[{type,set},{file,GroupFile},{repair,true}]) of
55
NDD1 = lists:keyreplace(auth_user_file, 1, DirData,
56
{auth_user_file, PWDB}),
57
NDD2 = lists:keyreplace(auth_group_file, 1, NDD1,
58
{auth_group_file, GDB}),
61
{error, {{file, GroupFile},Err}}
64
{error, {{file, PWFile},Err2}}
68
%% Storage format of users in the dets table:
69
%% {{UserName, Addr, Port, Dir}, Password, UserData}
72
add_user(DirData, UStruct) ->
73
{Addr, Port, Dir} = lookup_common(DirData),
74
PWDB = httpd_util:key1search(DirData, auth_user_file),
75
Record = {{UStruct#httpd_user.username, Addr, Port, Dir},
76
UStruct#httpd_user.password, UStruct#httpd_user.user_data},
77
case dets:lookup(PWDB, UStruct#httpd_user.username) of
79
{error, user_already_in_db};
81
dets:insert(PWDB, Record),
85
get_user(DirData, UserName) ->
86
{Addr, Port, Dir} = lookup_common(DirData),
87
PWDB = httpd_util:key1search(DirData, auth_user_file),
88
User = {UserName, Addr, Port, Dir},
89
case dets:lookup(PWDB, User) of
90
[{User, Password, UserData}] ->
91
{ok, #httpd_user{username=UserName, password=Password, user_data=UserData}};
96
list_users(DirData) ->
97
?DEBUG("list_users -> ~n"
98
" DirData: ~p", [DirData]),
99
{Addr, Port, Dir} = lookup_common(DirData),
100
PWDB = httpd_util:key1search(DirData, auth_user_file),
101
case dets:traverse(PWDB, fun(X) -> {continue, X} end) of %% SOOOO Ugly !
102
Records when list(Records) ->
103
?DEBUG("list_users -> ~n"
104
" Records: ~p", [Records]),
105
{ok, [UserName || {{UserName, AnyAddr, AnyPort, AnyDir}, Password, _Data} <- Records,
106
AnyAddr == Addr, AnyPort == Port, AnyDir == Dir]};
108
?DEBUG("list_users -> ~n"
113
delete_user(DirData, UserName) ->
114
{Addr, Port, Dir} = lookup_common(DirData),
115
PWDB = httpd_util:key1search(DirData, auth_user_file),
116
User = {UserName, Addr, Port, Dir},
117
case dets:lookup(PWDB, User) of
118
[{User, SomePassword, UserData}] ->
119
dets:delete(PWDB, User),
120
lists:foreach(fun(Group) -> delete_group_member(DirData, Group, UserName) end,
121
list_groups(DirData)),
124
{error, no_such_user}
128
%% Storage of groups in the dets table:
129
%% {Group, UserList} where UserList is a list of strings.
131
add_group_member(DirData, GroupName, UserName) ->
132
{Addr, Port, Dir} = lookup_common(DirData),
133
GDB = httpd_util:key1search(DirData, auth_group_file),
134
Group = {GroupName, Addr, Port, Dir},
135
case dets:lookup(GDB, Group) of
137
case lists:member(UserName, Users) of
141
dets:insert(GDB, {Group, [UserName|Users]}),
145
dets:insert(GDB, {Group, [UserName]}),
151
list_group_members(DirData, GroupName) ->
152
{Addr, Port, Dir} = lookup_common(DirData),
153
GDB = httpd_util:key1search(DirData, auth_group_file),
154
Group = {GroupName, Addr, Port, Dir},
155
case dets:lookup(GDB, Group) of
159
{error, no_such_group}
162
list_groups(DirData) ->
163
{Addr, Port, Dir} = lookup_common(DirData),
164
GDB = httpd_util:key1search(DirData, auth_group_file),
165
case dets:match(GDB, {'$1', '_'}) of
168
List when list(List) ->
169
Groups = lists:flatten(List),
170
{ok, [GroupName || {GroupName, AnyAddr, AnyPort, AnyDir} <- Groups,
171
AnyAddr == Addr, AnyPort == Port, AnyDir == Dir]};
176
delete_group_member(DirData, GroupName, UserName) ->
177
{Addr, Port, Dir} = lookup_common(DirData),
178
GDB = httpd_util:key1search(DirData, auth_group_file),
179
Group = {GroupName, Addr, Port, Dir},
180
case dets:lookup(GDB, GroupName) of
182
case lists:member(UserName, Users) of
184
dets:delete(GDB, Group),
185
dets:insert(GDB, {Group,
186
lists:delete(UserName, Users)}),
189
{error, no_such_group_member}
192
{error, no_such_group}
195
delete_group(DirData, GroupName) ->
196
{Addr, Port, Dir} = lookup_common(DirData),
197
GDB = httpd_util:key1search(DirData, auth_group_file),
198
Group = {GroupName, Addr, Port, Dir},
199
case dets:lookup(GDB, Group) of
201
dets:delete(GDB, Group),
204
{error, no_such_group}
207
lookup_common(DirData) ->
208
Dir = httpd_util:key1search(DirData, path),
209
Port = httpd_util:key1search(DirData, port),
210
Addr = httpd_util:key1search(DirData, bind_address),
215
%% Closes dets tables used by this auth mod.
218
PWDB = httpd_util:key1search(DirData, auth_user_file),
219
GDB = httpd_util:key1search(DirData, auth_group_file),