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
|
/*
* Copyright 2013 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit;
import java.io.File;
import java.util.Set;
import java.util.TreeSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import waffle.windows.auth.IWindowsAccount;
import waffle.windows.auth.IWindowsAuthProvider;
import waffle.windows.auth.IWindowsComputer;
import waffle.windows.auth.IWindowsIdentity;
import waffle.windows.auth.impl.WindowsAuthProviderImpl;
import com.gitblit.Constants.AccountType;
import com.gitblit.manager.IRuntimeManager;
import com.gitblit.models.UserModel;
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.StringUtils;
import com.sun.jna.platform.win32.Win32Exception;
/**
* Implementation of a Windows user service.
*
* @author James Moger
*/
public class WindowsUserService extends GitblitUserService {
private final Logger logger = LoggerFactory.getLogger(WindowsUserService.class);
private IStoredSettings settings;
private IWindowsAuthProvider waffle;
public WindowsUserService() {
super();
}
@Override
public void setup(IStoredSettings settings) {
this.settings = settings;
String file = settings.getString(Keys.realm.windows.backingUserService, "${baseFolder}/users.conf");
IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
File realmFile = runtimeManager.getFileOrFolder(file);
serviceImpl = createUserService(realmFile);
logger.info("Windows User Service backed by " + serviceImpl.toString());
waffle = new WindowsAuthProviderImpl();
IWindowsComputer computer = waffle.getCurrentComputer();
logger.info(" name = " + computer.getComputerName());
logger.info(" status = " + describeJoinStatus(computer.getJoinStatus()));
logger.info(" memberOf = " + computer.getMemberOf());
//logger.info(" groups = " + Arrays.asList(computer.getGroups()));
}
protected String describeJoinStatus(String value) {
if ("NetSetupUnknownStatus".equals(value)) {
return "unknown";
} else if ("NetSetupUnjoined".equals(value)) {
return "not joined";
} else if ("NetSetupWorkgroupName".equals(value)) {
return "joined to a workgroup";
} else if ("NetSetupDomainName".equals(value)) {
return "joined to a domain";
}
return value;
}
@Override
public boolean supportsCredentialChanges() {
return false;
}
@Override
public boolean supportsDisplayNameChanges() {
return false;
}
@Override
public boolean supportsEmailAddressChanges() {
return true;
}
@Override
public boolean supportsTeamMembershipChanges() {
return true;
}
@Override
protected AccountType getAccountType() {
return AccountType.WINDOWS;
}
@Override
public UserModel authenticate(String username, char[] password) {
if (isLocalAccount(username)) {
// local account, bypass Windows authentication
return super.authenticate(username, password);
}
String defaultDomain = settings.getString(Keys.realm.windows.defaultDomain, null);
if (StringUtils.isEmpty(defaultDomain)) {
// ensure that default domain is null
defaultDomain = null;
}
if (defaultDomain != null) {
// sanitize username
if (username.startsWith(defaultDomain + "\\")) {
// strip default domain from domain\ username
username = username.substring(defaultDomain.length() + 1);
} else if (username.endsWith("@" + defaultDomain)) {
// strip default domain from username@domain
username = username.substring(0, username.lastIndexOf('@'));
}
}
IWindowsIdentity identity = null;
try {
if (username.indexOf('@') > -1 || username.indexOf('\\') > -1) {
// manually specified domain
identity = waffle.logonUser(username, new String(password));
} else {
// no domain specified, use default domain
identity = waffle.logonDomainUser(username, defaultDomain, new String(password));
}
} catch (Win32Exception e) {
logger.error(e.getMessage());
return null;
}
if (identity.isGuest() && !settings.getBoolean(Keys.realm.windows.allowGuests, false)) {
logger.warn("Guest account access is disabled");
identity.dispose();
return null;
}
UserModel user = getUserModel(username);
if (user == null) // create user object for new authenticated user
user = new UserModel(username.toLowerCase());
// create a user cookie
if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
user.cookie = StringUtils.getSHA1(user.username + new String(password));
}
// update user attributes from Windows identity
user.accountType = getAccountType();
String fqn = identity.getFqn();
if (fqn.indexOf('\\') > -1) {
user.displayName = fqn.substring(fqn.lastIndexOf('\\') + 1);
} else {
user.displayName = fqn;
}
user.password = Constants.EXTERNAL_ACCOUNT;
Set<String> groupNames = new TreeSet<String>();
for (IWindowsAccount group : identity.getGroups()) {
groupNames.add(group.getFqn());
}
if (groupNames.contains("BUILTIN\\Administrators")) {
// local administrator
user.canAdmin = true;
}
// TODO consider mapping Windows groups to teams
// push the changes to the backing user service
super.updateUserModel(user);
// cleanup resources
identity.dispose();
return user;
}
}
|