~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/inets/src/http_server/mod_auth_dets.erl

  • Committer: Bazaar Package Importer
  • Author(s): Erlang Packagers, Sergei Golovan
  • Date: 2006-12-03 17:07:44 UTC
  • mfrom: (2.1.11 feisty)
  • Revision ID: james.westby@ubuntu.com-20061203170744-rghjwupacqlzs6kv
Tags: 1:11.b.2-4
[ Sergei Golovan ]
Fixed erlang-base and erlang-base-hipe prerm scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/.
 
6
%% 
 
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
 
10
%% under the License.
 
11
%% 
 
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.''
 
15
%% 
 
16
%%     $Id$
 
17
%%
 
18
-module(mod_auth_dets).
 
19
 
 
20
%% dets authentication storage
 
21
 
 
22
-export([get_user/2,
 
23
         list_group_members/2,
 
24
         add_user/2,
 
25
         add_group_member/3,
 
26
         list_users/1,
 
27
         delete_user/2,
 
28
         list_groups/1,
 
29
         delete_group_member/3,
 
30
         delete_group/2,
 
31
         remove/1]).
 
32
 
 
33
-export([store_directory_data/2]).
 
34
 
 
35
-include("httpd.hrl").
 
36
-include("mod_auth.hrl").
 
37
 
 
38
store_directory_data(_Directory, DirData) ->
 
39
    ?CDEBUG("store_directory_data -> ~n"
 
40
            "     Directory: ~p~n"
 
41
            "     DirData:   ~p",
 
42
            [_Directory, DirData]),
 
43
 
 
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),
 
48
 
 
49
    PWName  = httpd_util:make_name("httpd_dets_pwdb",Addr,Port),
 
50
    case dets:open_file(PWName,[{type,set},{file,PWFile},{repair,true}]) of
 
51
        {ok, PWDB} ->
 
52
            GDBName = httpd_util:make_name("httpd_dets_groupdb",Addr,Port),
 
53
            case dets:open_file(GDBName,[{type,set},{file,GroupFile},{repair,true}]) of
 
54
                {ok, GDB} ->
 
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}),
 
59
                    {ok, NDD2};
 
60
                {error, Err}->
 
61
                    {error, {{file, GroupFile},Err}}
 
62
            end;
 
63
        {error, Err2} ->
 
64
            {error, {{file, PWFile},Err2}} 
 
65
    end.
 
66
 
 
67
%%
 
68
%% Storage format of users in the dets table:
 
69
%% {{UserName, Addr, Port, Dir}, Password, UserData}
 
70
%%
 
71
 
 
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
 
78
        [Record] ->
 
79
            {error, user_already_in_db};
 
80
        _ ->
 
81
            dets:insert(PWDB, Record),
 
82
            true
 
83
    end.
 
84
 
 
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}};
 
92
        _ ->
 
93
            {error, no_such_user}
 
94
    end.
 
95
 
 
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}, 
 
106
                               _Password, _Data} <- Records,
 
107
                              AnyAddr == Addr, AnyPort == Port, 
 
108
                              AnyDir == Dir]};
 
109
        _O ->
 
110
            ?DEBUG("list_users -> ~n"
 
111
                   "     O: ~p", [_O]),
 
112
            {ok, []}
 
113
    end.
 
114
 
 
115
delete_user(DirData, UserName) ->
 
116
    {Addr, Port, Dir} = lookup_common(DirData),
 
117
    PWDB = httpd_util:key1search(DirData, auth_user_file),
 
118
    User = {UserName, Addr, Port, Dir},
 
119
    case dets:lookup(PWDB, User) of
 
120
        [{User, _SomePassword, _UserData}] ->
 
121
            dets:delete(PWDB, User),
 
122
            {ok, Groups} = list_groups(DirData),
 
123
            lists:foreach(fun(Group) -> 
 
124
                                  delete_group_member(DirData, 
 
125
                                                      Group, UserName) end, 
 
126
                          Groups),
 
127
            true;
 
128
        _ ->
 
129
            {error, no_such_user}
 
130
    end.
 
131
 
 
132
%%
 
133
%% Storage of groups in the dets table:
 
134
%% {Group, UserList} where UserList is a list of strings.
 
135
%%
 
136
add_group_member(DirData, GroupName, UserName) ->
 
137
    {Addr, Port, Dir} = lookup_common(DirData),
 
138
    GDB = httpd_util:key1search(DirData, auth_group_file),
 
139
    Group = {GroupName, Addr, Port, Dir},
 
140
    case dets:lookup(GDB, Group) of
 
141
        [{Group, Users}] ->
 
142
            case lists:member(UserName, Users) of
 
143
                true ->
 
144
                    true;
 
145
                false ->
 
146
                    dets:insert(GDB, {Group, [UserName|Users]}),
 
147
                    true
 
148
            end;
 
149
        [] ->
 
150
            dets:insert(GDB, {Group, [UserName]}),
 
151
            true;
 
152
        Other ->
 
153
            {error, Other}
 
154
    end.
 
155
 
 
156
list_group_members(DirData, GroupName) ->
 
157
    {Addr, Port, Dir} = lookup_common(DirData),
 
158
    GDB = httpd_util:key1search(DirData, auth_group_file),
 
159
    Group = {GroupName, Addr, Port, Dir},
 
160
    case dets:lookup(GDB, Group) of
 
161
        [{Group, Users}] ->
 
162
            {ok, Users};
 
163
        _ ->
 
164
            {error, no_such_group}
 
165
    end.
 
166
 
 
167
list_groups(DirData) ->
 
168
    {Addr, Port, Dir} = lookup_common(DirData),
 
169
    GDB  = httpd_util:key1search(DirData, auth_group_file),
 
170
    case dets:match(GDB, {'$1', '_'}) of
 
171
        [] ->
 
172
            {ok, []};
 
173
        List when list(List) ->
 
174
            Groups = lists:flatten(List),
 
175
            {ok, [GroupName || 
 
176
                     {GroupName, AnyAddr, AnyPort, AnyDir} <- Groups,
 
177
                     AnyAddr == Addr, AnyPort == Port, AnyDir == Dir]};
 
178
        _ ->
 
179
            {ok, []}
 
180
    end.
 
181
 
 
182
delete_group_member(DirData, GroupName, UserName) ->
 
183
    {Addr, Port, Dir} = lookup_common(DirData),
 
184
    GDB = httpd_util:key1search(DirData, auth_group_file),
 
185
    Group = {GroupName, Addr, Port, Dir},
 
186
    case dets:lookup(GDB, GroupName) of
 
187
        [{Group, Users}] ->
 
188
            case lists:member(UserName, Users) of
 
189
                true ->
 
190
                    dets:delete(GDB, Group),
 
191
                    dets:insert(GDB, {Group,
 
192
                                      lists:delete(UserName, Users)}),
 
193
                    true;
 
194
                false ->
 
195
                    {error, no_such_group_member}
 
196
            end;
 
197
        _ ->
 
198
            {error, no_such_group}
 
199
    end.
 
200
 
 
201
delete_group(DirData, GroupName) ->
 
202
    {Addr, Port, Dir} = lookup_common(DirData),
 
203
    GDB = httpd_util:key1search(DirData, auth_group_file),
 
204
    Group = {GroupName, Addr, Port, Dir},
 
205
    case dets:lookup(GDB, Group) of
 
206
        [{Group, _Users}] ->
 
207
            dets:delete(GDB, Group),
 
208
            true;
 
209
        _ ->
 
210
            {error, no_such_group}
 
211
    end.
 
212
 
 
213
lookup_common(DirData) ->
 
214
    Dir  = httpd_util:key1search(DirData, path),
 
215
    Port = httpd_util:key1search(DirData, port),
 
216
    Addr = httpd_util:key1search(DirData, bind_address),
 
217
    {Addr, Port, Dir}.
 
218
 
 
219
%% remove/1
 
220
%%
 
221
%% Closes dets tables used by this auth mod.
 
222
%%
 
223
remove(DirData) ->
 
224
    PWDB = httpd_util:key1search(DirData, auth_user_file),
 
225
    GDB = httpd_util:key1search(DirData, auth_group_file),
 
226
    dets:close(GDB),
 
227
    dets:close(PWDB),
 
228
    ok.