2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "OFSHttpPasswd.h"
24 #include "SoHTTPAuthenticator.h"
27 #if defined (__APPLE__) || defined(__FreeBSD__)
30 # if defined(__OpenBSD__)
38 Note: a user-folder is different to an authenticator (though a user
39 folder can be a authenticator itself) ! A user-folder manages the whole
40 user-database while an authenticator decodes HTTP authentication info,
41 checks a password against a user and retrieves only authentication related
42 information on a user.
44 So: a user-folder is strongly related to an authenticator, but isn't
45 usually the actual authenticator object (which usually inherits from
49 // TODO: implement ...
51 @interface OFSHttpPasswdAuthenticator : SoHTTPAuthenticator
53 OFSHttpPasswd *passwd; /* non-retained */
56 - (id)initWithObject:(id)_obj;
61 @implementation OFSHttpPasswd
63 static BOOL debugOn = NO;
64 static NSArray *plainRoles = nil;
65 static NSArray *rootRoles = nil;
68 if (plainRoles == nil) {
69 plainRoles = [[NSArray alloc] initWithObjects:
70 SoRole_Authenticated, SoRole_Anonymous, nil];
72 if (rootRoles == nil) {
73 rootRoles = [[NSArray alloc] initWithObjects:
74 SoRole_Manager, SoRole_Authenticated,
75 SoRole_Anonymous, nil];
80 [self->content release];
81 [self->authenticator detach];
82 [self->authenticator release];
86 - (id)authenticatorInContext:(id)_ctx {
87 if (self->authenticator == nil) {
89 [[OFSHttpPasswdAuthenticator alloc] initWithObject:self];
91 return self->authenticator;
94 /* loading htpasswd */
96 - (NSException *)primaryLoad {
97 NSMutableDictionary *md;
102 [self->content release]; self->content = nil;
104 s = [self contentAsString];
105 lines = [s componentsSeparatedByString:@"\n"];
106 count = [lines count];
107 md = [NSMutableDictionary dictionaryWithCapacity:(count + 1)];
109 for (i = 0; i < count; i++) {
112 NSString *login, *pwd;
114 s = [lines objectAtIndex:i];
115 r = [s rangeOfString:@":"];
116 if (r.length == 0) continue;
118 login = [s substringToIndex:r.location];
119 pwd = [s substringFromIndex:(r.location + r.length)];
121 [md setObject:pwd forKey:login];
123 self->content = [md copy];
127 - (NSString *)cryptedPasswordForLogin:(NSString *)_login {
130 if ([_login length] < 1)
133 return [self->content objectForKey:_login];
135 if ((error = [self primaryLoad]))
138 return [self->content objectForKey:_login];
141 /* authenticator implementation */
143 - (BOOL)checkLogin:(NSString *)_login password:(NSString *)_pwd {
144 NSString *cryptedPwd;
149 [self debugWithFormat:@"check '%@' against pwd ...", _login];
151 if ((cryptedPwd = [self cryptedPasswordForLogin:_login]) == nil) {
152 [self debugWithFormat:@" user '%@' not available in htpasswd", _login];
157 [self debugWithFormat:@" check crypted pwd of user '%@' ...", _login];
159 // salt is user-pwd itself (crypt(pwd, cryptedpwd))
160 cp = crypt([_pwd cString], [cryptedPwd cString]);
161 cpo = cp ? [NSString stringWithCString:cp] : nil;
163 return [cryptedPwd isEqualToString:cpo];
166 - (NSString *)authRealm {
167 return [(WOApplication *)[WOApplication application] name];
170 - (BOOL)isRootLogin:(NSString *)_login {
171 return [_login isEqualToString:@"root"];
173 - (NSArray *)rolesForLogin:(NSString *)_login {
174 return [self isRootLogin:_login] ? rootRoles : plainRoles;
179 - (BOOL)isDebuggingEnabled {
183 @end /* OFSHttpPasswd */
185 @implementation OFSHttpPasswdAuthenticator
187 - (id)initWithObject:(id)_obj {
188 NSAssert(_obj, @"missing htpasswd user folder in argument ...");
189 if ((self = [super init])) {
195 return [self initWithObject:nil];
202 /* implement using folder itself ... */
204 - (BOOL)checkLogin:(NSString *)_login password:(NSString *)_pwd {
205 return [self->passwd checkLogin:_login password:_pwd];
208 - (NSString *)authRealm {
209 return [self->passwd authRealm];
212 - (NSArray *)rolesForLogin:(NSString *)_login {
213 return [self->passwd rolesForLogin:_login];
218 - (BOOL)isDebuggingEnabled {
222 @end /* OFSHttpPasswdAuthenticator */