~ubuntu-branches/ubuntu/jaunty/couchdb/jaunty

« back to all changes in this revision

Viewing changes to src/couch_inets/mod_auth_plain.erl

  • Committer: Bazaar Package Importer
  • Author(s): Noah Slater
  • Date: 2008-05-24 16:30:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080524163021-bpkh6s1090i37xy1
Tags: 0.7.3~svn650270-2
* Added release partitioning to database and log directories.
* Corrected postrm maintainer script to not remove logs.

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_plain).
19
 
 
20
 
-include("httpd.hrl").
21
 
-include("mod_auth.hrl").
22
 
 
23
 
-define(VMODULE,"AUTH_PLAIN").
24
 
 
25
 
%% Internal API
26
 
-export([store_directory_data/2]).
27
 
 
28
 
 
29
 
-export([get_user/2, 
30
 
         list_group_members/2, 
31
 
         add_user/2, 
32
 
         add_group_member/3, 
33
 
         list_users/1, 
34
 
         delete_user/2, 
35
 
         list_groups/1, 
36
 
         delete_group_member/3, 
37
 
         delete_group/2, 
38
 
         remove/1]).
39
 
 
40
 
%%
41
 
%% API
42
 
%%
43
 
 
44
 
%%
45
 
%% Storage format of users in the ets table:
46
 
%% {UserName, Password, UserData}
47
 
%%
48
 
 
49
 
add_user(DirData, #httpd_user{username = User} = UStruct) ->
50
 
    PWDB = httpd_util:key1search(DirData, auth_user_file),
51
 
    Record = {User,
52
 
              UStruct#httpd_user.password, 
53
 
              UStruct#httpd_user.user_data}, 
54
 
    case ets:lookup(PWDB, User) of
55
 
        [{User, _SomePassword, _SomeData}] ->
56
 
            {error, user_already_in_db};
57
 
        _ ->
58
 
            ets:insert(PWDB, Record),
59
 
            true
60
 
    end.
61
 
 
62
 
get_user(DirData, User) ->
63
 
    PWDB = httpd_util:key1search(DirData, auth_user_file),
64
 
    case ets:lookup(PWDB, User) of
65
 
        [{User, PassWd, Data}] ->
66
 
            {ok, #httpd_user{username=User, password=PassWd, user_data=Data}};
67
 
        _ ->
68
 
            {error, no_such_user}
69
 
    end.
70
 
 
71
 
list_users(DirData) ->
72
 
    PWDB = httpd_util:key1search(DirData, auth_user_file),
73
 
    Records = ets:match(PWDB, '$1'), 
74
 
    {ok, lists:foldr(fun({User, _PassWd, _Data}, A) -> [User | A] end, 
75
 
                     [], lists:flatten(Records))}.
76
 
 
77
 
delete_user(DirData, UserName) ->
78
 
    PWDB = httpd_util:key1search(DirData, auth_user_file),
79
 
    case ets:lookup(PWDB, UserName) of
80
 
        [{UserName, _SomePassword, _SomeData}] ->
81
 
            ets:delete(PWDB, UserName),
82
 
            {ok, Groups}  = list_groups(DirData),
83
 
            lists:foreach(fun(Group) -> 
84
 
                                  delete_group_member(DirData, 
85
 
                                                      Group, UserName) 
86
 
                          end, Groups);
87
 
        _ ->
88
 
            {error, no_such_user}
89
 
    end.
90
 
 
91
 
%%
92
 
%% Storage of groups in the ets table:
93
 
%% {Group, UserList} where UserList is a list of strings.
94
 
%%
95
 
  
96
 
add_group_member(DirData, Group, UserName) ->
97
 
    GDB = httpd_util:key1search(DirData, auth_group_file),
98
 
    case ets:lookup(GDB, Group) of
99
 
        [{Group, Users}] ->
100
 
            case lists:member(UserName, Users) of
101
 
                true ->
102
 
                    true;
103
 
                false ->
104
 
                    ets:insert(GDB, {Group, [UserName|Users]}),
105
 
                    true
106
 
            end;
107
 
        [] ->
108
 
            ets:insert(GDB, {Group, [UserName]}),
109
 
            true;
110
 
        Other ->
111
 
            {error, Other}
112
 
    end.
113
 
 
114
 
list_group_members(DirData, Group) ->
115
 
    GDB = httpd_util:key1search(DirData, auth_group_file),
116
 
    case ets:lookup(GDB, Group) of
117
 
        [{Group, Users}] ->
118
 
            {ok, Users};
119
 
        _ ->
120
 
            {error, no_such_group}
121
 
    end.
122
 
 
123
 
list_groups(DirData) ->
124
 
    GDB = httpd_util:key1search(DirData, auth_group_file),
125
 
    Groups = ets:match(GDB, '$1'), 
126
 
    {ok, httpd_util:uniq(lists:foldr(fun({G, _}, A) -> [G|A] end,
127
 
                                     [], lists:flatten(Groups)))}.
128
 
 
129
 
delete_group_member(DirData, Group, User) ->
130
 
    GDB = httpd_util:key1search(DirData, auth_group_file),
131
 
    case ets:lookup(GDB, Group) of
132
 
        [{Group, Users}] when is_list(Users) ->
133
 
            case lists:member(User, Users) of
134
 
                true ->
135
 
                    ets:delete(GDB, Group),
136
 
                    ets:insert(GDB, {Group, lists:delete(User, Users)}),
137
 
                    true;
138
 
                false ->
139
 
                    {error, no_such_group_member}
140
 
            end;
141
 
        _ ->
142
 
            {error, no_such_group}
143
 
    end.
144
 
 
145
 
delete_group(DirData, Group) ->
146
 
    GDB = httpd_util:key1search(DirData, auth_group_file),
147
 
    case ets:lookup(GDB, Group) of
148
 
        [{Group, _Users}] ->
149
 
            ets:delete(GDB, Group),
150
 
            true;
151
 
        _ ->
152
 
            {error, no_such_group}
153
 
    end.
154
 
 
155
 
store_directory_data(_Directory, DirData) ->
156
 
    PWFile = httpd_util:key1search(DirData, auth_user_file),
157
 
    GroupFile = httpd_util:key1search(DirData, auth_group_file),
158
 
    case load_passwd(PWFile) of
159
 
        {ok, PWDB} ->
160
 
            case load_group(GroupFile) of
161
 
                {ok, GRDB} ->
162
 
                    %% Address and port is included in the file names...
163
 
                    Addr = httpd_util:key1search(DirData, bind_address),
164
 
                    Port = httpd_util:key1search(DirData, port),
165
 
                    {ok, PasswdDB} = store_passwd(Addr,Port,PWDB),
166
 
                    {ok, GroupDB}  = store_group(Addr,Port,GRDB),
167
 
                    NDD1 = lists:keyreplace(auth_user_file, 1, DirData, 
168
 
                                            {auth_user_file, PasswdDB}),
169
 
                    NDD2 = lists:keyreplace(auth_group_file, 1, NDD1, 
170
 
                                            {auth_group_file, GroupDB}),
171
 
                    {ok, NDD2};
172
 
                Err ->
173
 
                    {error, Err}
174
 
            end;
175
 
        Err2 ->
176
 
            {error, Err2}
177
 
    end.
178
 
 
179
 
 
180
 
 
181
 
%% load_passwd
182
 
 
183
 
load_passwd(AuthUserFile) ->
184
 
    case file:open(AuthUserFile, read) of
185
 
        {ok,Stream} ->
186
 
            parse_passwd(Stream, []);
187
 
        {error, _} ->
188
 
            {error, ?NICE("Can't open "++AuthUserFile)}
189
 
    end.
190
 
 
191
 
parse_passwd(Stream,PasswdList) ->
192
 
    Line =
193
 
        case io:get_line(Stream, '') of
194
 
            eof ->
195
 
                eof;
196
 
            String ->
197
 
                httpd_conf:clean(String)
198
 
        end,
199
 
    parse_passwd(Stream, PasswdList, Line).
200
 
 
201
 
parse_passwd(Stream, PasswdList, eof) ->
202
 
    file:close(Stream),
203
 
    {ok, PasswdList};
204
 
parse_passwd(Stream, PasswdList, "") ->
205
 
    parse_passwd(Stream, PasswdList);
206
 
parse_passwd(Stream, PasswdList, [$#|_]) ->
207
 
    parse_passwd(Stream, PasswdList);
208
 
parse_passwd(Stream, PasswdList, Line) ->      
209
 
    case regexp:split(Line,":") of
210
 
        {ok, [User,Password]} ->
211
 
            parse_passwd(Stream, [{User,Password, []}|PasswdList]);
212
 
        {ok,_} ->
213
 
            {error, ?NICE(Line)}
214
 
    end.
215
 
 
216
 
%% load_group
217
 
 
218
 
load_group(AuthGroupFile) ->
219
 
    case file:open(AuthGroupFile, read) of
220
 
        {ok, Stream} ->
221
 
            parse_group(Stream,[]);
222
 
        {error, _} ->
223
 
            {error, ?NICE("Can't open "++AuthGroupFile)}
224
 
    end.
225
 
 
226
 
parse_group(Stream, GroupList) ->
227
 
    Line=
228
 
        case io:get_line(Stream,'') of
229
 
            eof ->
230
 
                eof;
231
 
            String ->
232
 
                httpd_conf:clean(String)
233
 
        end,
234
 
    parse_group(Stream, GroupList, Line).
235
 
 
236
 
parse_group(Stream, GroupList, eof) ->
237
 
    file:close(Stream),
238
 
    {ok, GroupList};
239
 
parse_group(Stream, GroupList, "") ->
240
 
    parse_group(Stream, GroupList);
241
 
parse_group(Stream, GroupList, [$#|_]) ->
242
 
    parse_group(Stream, GroupList);
243
 
parse_group(Stream, GroupList, Line) ->      
244
 
    case regexp:split(Line, ":") of
245
 
        {ok, [Group,Users]} ->
246
 
            {ok, UserList} = regexp:split(Users," "),
247
 
            parse_group(Stream, [{Group,UserList}|GroupList]);
248
 
        {ok, _} ->
249
 
            {error, ?NICE(Line)}
250
 
    end.
251
 
 
252
 
 
253
 
%% store_passwd
254
 
 
255
 
store_passwd(Addr,Port,PasswdList) ->
256
 
    Name = httpd_util:make_name("httpd_passwd",Addr,Port),
257
 
    PasswdDB = ets:new(Name, [set, public]),
258
 
    store_passwd(PasswdDB, PasswdList).
259
 
 
260
 
store_passwd(PasswdDB, []) ->
261
 
    {ok, PasswdDB};
262
 
store_passwd(PasswdDB, [User|Rest]) ->
263
 
    ets:insert(PasswdDB, User),
264
 
    store_passwd(PasswdDB, Rest).
265
 
 
266
 
%% store_group
267
 
 
268
 
store_group(Addr,Port,GroupList) ->
269
 
    Name = httpd_util:make_name("httpd_group",Addr,Port),
270
 
    GroupDB = ets:new(Name, [set, public]),
271
 
    store_group(GroupDB, GroupList).
272
 
 
273
 
 
274
 
store_group(GroupDB,[]) ->
275
 
    {ok, GroupDB};
276
 
store_group(GroupDB,[User|Rest]) ->
277
 
    ets:insert(GroupDB, User),
278
 
    store_group(GroupDB, Rest).
279
 
 
280
 
 
281
 
%% remove/1
282
 
%%
283
 
%% Deletes ets tables used by this auth mod.
284
 
%%
285
 
remove(DirData) ->
286
 
    PWDB = httpd_util:key1search(DirData, auth_user_file),
287
 
    GDB = httpd_util:key1search(DirData, auth_group_file),
288
 
    ets:delete(PWDB),
289
 
    ets:delete(GDB).
290
 
 
291
 
 
292
 
 
293
 
 
294
 
 
295