View Javadoc
1   /*
2    * Copyright 2012-2019 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.stream.Collectors;
25  
26  import javax.annotation.Resource;
27  
28  import org.codelibs.core.lang.StringUtil;
29  import org.codelibs.fess.crawler.client.fs.FileSystemClient;
30  import org.codelibs.fess.crawler.client.ftp.FtpClient;
31  import org.codelibs.fess.crawler.client.smb.SmbClient;
32  import org.codelibs.fess.crawler.entity.ResponseData;
33  import org.codelibs.fess.mylasta.direction.FessConfig;
34  import org.codelibs.fess.util.ComponentUtil;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  import jcifs.SID;
39  
40  public class PermissionHelper {
41      private static final Logger logger = LoggerFactory.getLogger(PermissionHelper.class);
42  
43      protected String rolePrefix = "{role}";
44  
45      protected String groupPrefix = "{group}";
46  
47      protected String userPrefix = "{user}";
48  
49      protected String allowPrefix = "(allow)";
50  
51      protected String denyPrefix = "(deny)";
52  
53      @Resource
54      protected SystemHelper systemHelper;
55  
56      public String encode(final String value) {
57          if (StringUtil.isBlank(value)) {
58              return null;
59          }
60  
61          String permission = value.trim();
62          String lower = permission.toLowerCase(Locale.ROOT);
63          final String aclPrefix;
64          if (lower.startsWith(allowPrefix)) {
65              lower = lower.substring(allowPrefix.length());
66              permission = permission.substring(allowPrefix.length());
67              aclPrefix = StringUtil.EMPTY;
68          } else if (lower.startsWith(denyPrefix)) {
69              lower = lower.substring(denyPrefix.length());
70              permission = permission.substring(denyPrefix.length());
71              aclPrefix = ComponentUtil.getFessConfig().getRoleSearchDeniedPrefix();
72          } else {
73              aclPrefix = StringUtil.EMPTY;
74          }
75          if (StringUtil.isBlank(permission)) {
76              return null;
77          }
78          if (lower.startsWith(userPrefix)) {
79              if (permission.length() > userPrefix.length()) {
80                  return aclPrefix + systemHelper.getSearchRoleByUser(permission.substring(userPrefix.length()));
81              } else {
82                  return null;
83              }
84          } else if (lower.startsWith(groupPrefix)) {
85              if (permission.length() > groupPrefix.length()) {
86                  return aclPrefix + systemHelper.getSearchRoleByGroup(permission.substring(groupPrefix.length()));
87              } else {
88                  return null;
89              }
90          } else if (lower.startsWith(rolePrefix)) {
91              if (permission.length() > rolePrefix.length()) {
92                  return aclPrefix + systemHelper.getSearchRoleByRole(permission.substring(rolePrefix.length()));
93              } else {
94                  return null;
95              }
96          }
97          return permission;
98      }
99  
100     public String decode(final String value) {
101         if (StringUtil.isBlank(value)) {
102             return null;
103         }
104 
105         final FessConfig fessConfig = ComponentUtil.getFessConfig();
106         final String aclPrefix;
107         final String permission;
108         final String deniedPrefix = fessConfig.getRoleSearchDeniedPrefix();
109         if (value.startsWith(deniedPrefix)) {
110             permission = value.substring(deniedPrefix.length());
111             aclPrefix = denyPrefix;
112         } else {
113             permission = value;
114             aclPrefix = StringUtil.EMPTY;
115         }
116         if (StringUtil.isBlank(permission)) {
117             return null;
118         }
119         if (permission.startsWith(fessConfig.getRoleSearchUserPrefix())
120                 && permission.length() > fessConfig.getRoleSearchUserPrefix().length()) {
121             return aclPrefix + userPrefix + permission.substring(fessConfig.getRoleSearchUserPrefix().length());
122         } else if (permission.startsWith(fessConfig.getRoleSearchGroupPrefix())
123                 && permission.length() > fessConfig.getRoleSearchGroupPrefix().length()) {
124             return aclPrefix + groupPrefix + permission.substring(fessConfig.getRoleSearchGroupPrefix().length());
125         } else if (permission.startsWith(fessConfig.getRoleSearchRolePrefix())
126                 && permission.length() > fessConfig.getRoleSearchRolePrefix().length()) {
127             return aclPrefix + rolePrefix + permission.substring(fessConfig.getRoleSearchRolePrefix().length());
128         }
129         return permission;
130     }
131 
132     public void setRolePrefix(final String rolePrefix) {
133         this.rolePrefix = rolePrefix;
134     }
135 
136     public void setGroupPrefix(final String groupPrefix) {
137         this.groupPrefix = groupPrefix;
138     }
139 
140     public void setUserPrefix(final String userPrefix) {
141         this.userPrefix = userPrefix;
142     }
143 
144     public List<String> getSmbRoleTypeList(final ResponseData responseData) {
145         final List<String> roleTypeList = new ArrayList<>();
146         final FessConfig fessConfig = ComponentUtil.getFessConfig();
147         if (fessConfig.isSmbRoleFromFile()) {
148             final SambaHelper sambaHelper = ComponentUtil.getSambaHelper();
149             final Map<String, Object> metaDataMap = responseData.getMetaDataMap();
150             if (responseData.getUrl().startsWith("smb:")) {
151                 final SID[] allowedSids = (SID[]) metaDataMap.get(SmbClient.SMB_ALLOWED_SID_ENTRIES);
152                 if (allowedSids != null) {
153                     for (final SID sid : allowedSids) {
154                         final String accountId = sambaHelper.getAccountId(sid);
155                         if (accountId != null) {
156                             roleTypeList.add(accountId);
157                         }
158                     }
159                 }
160                 final SID[] deniedSids = (SID[]) metaDataMap.get(SmbClient.SMB_DENIED_SID_ENTRIES);
161                 if (deniedSids != null) {
162                     for (final SID sid : deniedSids) {
163                         final String accountId = sambaHelper.getAccountId(sid);
164                         if (accountId != null) {
165                             roleTypeList.add(fessConfig.getRoleSearchDeniedPrefix() + accountId);
166                         }
167                     }
168                 }
169                 if (logger.isDebugEnabled()) {
170                     logger.debug("smbUrl:" + responseData.getUrl() + " roleType:" + roleTypeList.toString());
171                 }
172             } else if (responseData.getUrl().startsWith("smb1:")) {
173                 final jcifs.smb1.smb1.SID[] allowedSids =
174                         (jcifs.smb1.smb1.SID[]) metaDataMap.get(org.codelibs.fess.crawler.client.smb1.SmbClient.SMB_ALLOWED_SID_ENTRIES);
175                 if (allowedSids != null) {
176                     for (final jcifs.smb1.smb1.SID sid : allowedSids) {
177                         final String accountId = sambaHelper.getAccountId(sid);
178                         if (accountId != null) {
179                             roleTypeList.add(accountId);
180                         }
181                     }
182                 }
183                 final jcifs.smb1.smb1.SID[] deniedSids =
184                         (jcifs.smb1.smb1.SID[]) metaDataMap.get(org.codelibs.fess.crawler.client.smb1.SmbClient.SMB_DENIED_SID_ENTRIES);
185                 if (deniedSids != null) {
186                     for (final jcifs.smb1.smb1.SID sid : deniedSids) {
187                         final String accountId = sambaHelper.getAccountId(sid);
188                         if (accountId != null) {
189                             roleTypeList.add(fessConfig.getRoleSearchDeniedPrefix() + accountId);
190                         }
191                     }
192                 }
193                 if (logger.isDebugEnabled()) {
194                     logger.debug("smb1Url:" + responseData.getUrl() + " roleType:" + roleTypeList.toString());
195                 }
196             }
197         }
198         return roleTypeList;
199     }
200 
201     public List<String> getFileRoleTypeList(final ResponseData responseData) {
202         final List<String> roleTypeList = new ArrayList<>();
203         final FessConfig fessConfig = ComponentUtil.getFessConfig();
204         if (fessConfig.isFileRoleFromFile() && responseData.getUrl().startsWith("file:")) {
205             final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
206             final String owner = (String) responseData.getMetaDataMap().get(FileSystemClient.FS_FILE_USER);
207             if (owner != null) {
208                 roleTypeList.add(systemHelper.getSearchRoleByUser(owner));
209             }
210             final String[] groups = (String[]) responseData.getMetaDataMap().get(FileSystemClient.FS_FILE_GROUPS);
211             roleTypeList.addAll(stream(groups).get(stream -> stream.map(systemHelper::getSearchRoleByGroup).collect(Collectors.toList())));
212             if (logger.isDebugEnabled()) {
213                 logger.debug("fileUrl:" + responseData.getUrl() + " roleType:" + roleTypeList.toString());
214             }
215         }
216         return roleTypeList;
217     }
218 
219     public List<String> getFtpRoleTypeList(final ResponseData responseData) {
220         final List<String> roleTypeList = new ArrayList<>();
221         final FessConfig fessConfig = ComponentUtil.getFessConfig();
222         if (fessConfig.isFtpRoleFromFile() && responseData.getUrl().startsWith("ftp:")) {
223             final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
224             final String owner = (String) responseData.getMetaDataMap().get(FtpClient.FTP_FILE_USER);
225             if (owner != null) {
226                 roleTypeList.add(systemHelper.getSearchRoleByUser(owner));
227             }
228             final String group = (String) responseData.getMetaDataMap().get(FtpClient.FTP_FILE_GROUP);
229             if (group != null) {
230                 roleTypeList.add(systemHelper.getSearchRoleByGroup(group));
231             }
232             if (logger.isDebugEnabled()) {
233                 logger.debug("ftpUrl:" + responseData.getUrl() + " roleType:" + roleTypeList.toString());
234             }
235         }
236         return roleTypeList;
237     }
238 
239     public void setAllowPrefix(String allowPrefix) {
240         this.allowPrefix = allowPrefix;
241     }
242 
243     public void setDenyPrefix(String denyPrefix) {
244         this.denyPrefix = denyPrefix;
245     }
246 }