/
PermissionHelper.java
278 lines (255 loc) · 12.3 KB
/
PermissionHelper.java
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2012-2024 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.helper;
import java.io.IOException;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.UserPrincipal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.crawler.client.fs.FileSystemClient;
import org.codelibs.fess.crawler.client.ftp.FtpClient;
import org.codelibs.fess.crawler.client.smb.SmbClient;
import org.codelibs.fess.crawler.entity.ResponseData;
import org.codelibs.fess.crawler.exception.CrawlingAccessException;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.util.ComponentUtil;
import jcifs.SID;
public class PermissionHelper {
private static final Logger logger = LogManager.getLogger(PermissionHelper.class);
protected String rolePrefix = "{role}";
protected String groupPrefix = "{group}";
protected String userPrefix = "{user}";
protected String allowPrefix = "(allow)";
protected String denyPrefix = "(deny)";
@Resource
protected SystemHelper systemHelper;
public String encode(final String value) {
if (StringUtil.isBlank(value)) {
return null;
}
String permission = value.trim();
String lower = permission.toLowerCase(Locale.ROOT);
final String aclPrefix;
if (lower.startsWith(allowPrefix)) {
lower = lower.substring(allowPrefix.length());
permission = permission.substring(allowPrefix.length());
aclPrefix = StringUtil.EMPTY;
} else if (lower.startsWith(denyPrefix)) {
lower = lower.substring(denyPrefix.length());
permission = permission.substring(denyPrefix.length());
aclPrefix = ComponentUtil.getFessConfig().getRoleSearchDeniedPrefix();
} else {
aclPrefix = StringUtil.EMPTY;
}
if (StringUtil.isBlank(permission)) {
return null;
}
if (lower.startsWith(userPrefix)) {
if (permission.length() > userPrefix.length()) {
return aclPrefix + systemHelper.getSearchRoleByUser(permission.substring(userPrefix.length()));
}
return null;
}
if (lower.startsWith(groupPrefix)) {
if (permission.length() > groupPrefix.length()) {
return aclPrefix + systemHelper.getSearchRoleByGroup(permission.substring(groupPrefix.length()));
}
return null;
}
if (lower.startsWith(rolePrefix)) {
if (permission.length() > rolePrefix.length()) {
return aclPrefix + systemHelper.getSearchRoleByRole(permission.substring(rolePrefix.length()));
}
return null;
}
return permission;
}
public String decode(final String value) {
if (StringUtil.isBlank(value)) {
return null;
}
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final String aclPrefix;
final String permission;
final String deniedPrefix = fessConfig.getRoleSearchDeniedPrefix();
if (value.startsWith(deniedPrefix)) {
permission = value.substring(deniedPrefix.length());
aclPrefix = denyPrefix;
} else {
permission = value;
aclPrefix = StringUtil.EMPTY;
}
if (StringUtil.isBlank(permission)) {
return null;
}
if (permission.startsWith(fessConfig.getRoleSearchUserPrefix())
&& permission.length() > fessConfig.getRoleSearchUserPrefix().length()) {
return aclPrefix + userPrefix + permission.substring(fessConfig.getRoleSearchUserPrefix().length());
}
if (permission.startsWith(fessConfig.getRoleSearchGroupPrefix())
&& permission.length() > fessConfig.getRoleSearchGroupPrefix().length()) {
return aclPrefix + groupPrefix + permission.substring(fessConfig.getRoleSearchGroupPrefix().length());
}
if (permission.startsWith(fessConfig.getRoleSearchRolePrefix())
&& permission.length() > fessConfig.getRoleSearchRolePrefix().length()) {
return aclPrefix + rolePrefix + permission.substring(fessConfig.getRoleSearchRolePrefix().length());
}
return permission;
}
public void setRolePrefix(final String rolePrefix) {
this.rolePrefix = rolePrefix;
}
public void setGroupPrefix(final String groupPrefix) {
this.groupPrefix = groupPrefix;
}
public void setUserPrefix(final String userPrefix) {
this.userPrefix = userPrefix;
}
public List<String> getSmbRoleTypeList(final ResponseData responseData) {
final List<String> roleTypeList = new ArrayList<>();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
if (fessConfig.isSmbRoleFromFile()) {
final SambaHelper sambaHelper = ComponentUtil.getSambaHelper();
final Map<String, Object> metaDataMap = responseData.getMetaDataMap();
if (responseData.getUrl().startsWith("smb:")) {
final SID[] allowedSids = (SID[]) metaDataMap.get(SmbClient.SMB_ALLOWED_SID_ENTRIES);
if (allowedSids != null) {
for (final SID sid : allowedSids) {
final String accountId = sambaHelper.getAccountId(sid);
if (accountId != null) {
roleTypeList.add(accountId);
}
}
}
final SID[] deniedSids = (SID[]) metaDataMap.get(SmbClient.SMB_DENIED_SID_ENTRIES);
if (deniedSids != null) {
for (final SID sid : deniedSids) {
final String accountId = sambaHelper.getAccountId(sid);
if (accountId != null) {
roleTypeList.add(fessConfig.getRoleSearchDeniedPrefix() + accountId);
}
}
}
if (logger.isDebugEnabled()) {
logger.debug("smbUrl:{} roleType:{}", responseData.getUrl(), roleTypeList);
}
} else if (responseData.getUrl().startsWith("smb1:")) {
final jcifs.smb1.smb1.SID[] allowedSids =
(jcifs.smb1.smb1.SID[]) metaDataMap.get(org.codelibs.fess.crawler.client.smb1.SmbClient.SMB_ALLOWED_SID_ENTRIES);
if (allowedSids != null) {
for (final jcifs.smb1.smb1.SID sid : allowedSids) {
final String accountId = sambaHelper.getAccountId(sid);
if (accountId != null) {
roleTypeList.add(accountId);
}
}
}
final jcifs.smb1.smb1.SID[] deniedSids =
(jcifs.smb1.smb1.SID[]) metaDataMap.get(org.codelibs.fess.crawler.client.smb1.SmbClient.SMB_DENIED_SID_ENTRIES);
if (deniedSids != null) {
for (final jcifs.smb1.smb1.SID sid : deniedSids) {
final String accountId = sambaHelper.getAccountId(sid);
if (accountId != null) {
roleTypeList.add(fessConfig.getRoleSearchDeniedPrefix() + accountId);
}
}
}
if (logger.isDebugEnabled()) {
logger.debug("smb1Url:{} roleType:{}", responseData.getUrl(), roleTypeList);
}
}
}
return roleTypeList;
}
public List<String> getFileRoleTypeList(final ResponseData responseData) {
final List<String> roleTypeList = new ArrayList<>();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
if (fessConfig.isFileRoleFromFile() && responseData.getUrl().startsWith("file:")) {
final Map<String, Object> metaDataMap = responseData.getMetaDataMap();
final Object fileAttributeView = metaDataMap.get(FileSystemClient.FILE_ATTRIBUTE_VIEW);
try {
if (fileAttributeView instanceof final AclFileAttributeView aclFileAttributeView) {
aclFileAttributeView.getAcl().stream().forEach(acl -> {
final UserPrincipal principal = acl.principal();
if (logger.isDebugEnabled()) {
logger.debug("Principal: [{}] {}", principal.getClass().getName(), principal);
}
if (principal instanceof final GroupPrincipal groupPrincipal) {
roleTypeList.add(systemHelper.getSearchRoleByGroup(groupPrincipal.getName()));
} else if (principal != null) {
roleTypeList.add(systemHelper.getSearchRoleByUser(principal.getName()));
}
});
} else if (fileAttributeView instanceof final PosixFileAttributeView posixFileAttributeView) {
final PosixFileAttributes attributes = posixFileAttributeView.readAttributes();
final UserPrincipal userPrincipal = attributes.owner();
if (logger.isDebugEnabled()) {
logger.debug("Principal: [{}] {}", userPrincipal.getClass().getName(), userPrincipal);
}
if (userPrincipal != null) {
roleTypeList.add(systemHelper.getSearchRoleByUser(userPrincipal.getName()));
}
final GroupPrincipal groupPrincipal = attributes.group();
if (logger.isDebugEnabled()) {
logger.debug("Principal: [{}] {}", groupPrincipal.getClass().getName(), groupPrincipal);
}
if (groupPrincipal != null) {
roleTypeList.add(systemHelper.getSearchRoleByGroup(groupPrincipal.getName()));
}
}
} catch (final IOException e) {
throw new CrawlingAccessException("Failed to access permission info", e);
}
if (logger.isDebugEnabled()) {
logger.debug("fileUrl:{} roleType:{}", responseData.getUrl(), roleTypeList);
}
}
return roleTypeList;
}
public List<String> getFtpRoleTypeList(final ResponseData responseData) {
final List<String> roleTypeList = new ArrayList<>();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
if (fessConfig.isFtpRoleFromFile() && responseData.getUrl().startsWith("ftp:")) {
final String owner = (String) responseData.getMetaDataMap().get(FtpClient.FTP_FILE_USER);
if (owner != null) {
roleTypeList.add(systemHelper.getSearchRoleByUser(owner));
}
final String group = (String) responseData.getMetaDataMap().get(FtpClient.FTP_FILE_GROUP);
if (group != null) {
roleTypeList.add(systemHelper.getSearchRoleByGroup(group));
}
if (logger.isDebugEnabled()) {
logger.debug("ftpUrl:{} roleType:{}", responseData.getUrl(), roleTypeList);
}
}
return roleTypeList;
}
public void setAllowPrefix(final String allowPrefix) {
this.allowPrefix = allowPrefix;
}
public void setDenyPrefix(final String denyPrefix) {
this.denyPrefix = denyPrefix;
}
}