View Javadoc
1   /*
2    * Copyright 2012-2021 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.apache.logging.log4j.LogManager;
29  import org.apache.logging.log4j.Logger;
30  import org.codelibs.core.lang.StringUtil;
31  import org.codelibs.fess.crawler.client.fs.FileSystemClient;
32  import org.codelibs.fess.crawler.client.ftp.FtpClient;
33  import org.codelibs.fess.crawler.client.smb.SmbClient;
34  import org.codelibs.fess.crawler.entity.ResponseData;
35  import org.codelibs.fess.mylasta.direction.FessConfig;
36  import org.codelibs.fess.util.ComponentUtil;
37  
38  import jcifs.SID;
39  
40  public class PermissionHelper {
41      private static final Logger logger = LogManager.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              }
82              return null;
83          }
84          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          }
91          if (lower.startsWith(rolePrefix)) {
92              if (permission.length() > rolePrefix.length()) {
93                  return aclPrefix + systemHelper.getSearchRoleByRole(permission.substring(rolePrefix.length()));
94              } else {
95                  return null;
96              }
97          }
98          return permission;
99      }
100 
101     public String decode(final String value) {
102         if (StringUtil.isBlank(value)) {
103             return null;
104         }
105 
106         final FessConfig fessConfig = ComponentUtil.getFessConfig();
107         final String aclPrefix;
108         final String permission;
109         final String deniedPrefix = fessConfig.getRoleSearchDeniedPrefix();
110         if (value.startsWith(deniedPrefix)) {
111             permission = value.substring(deniedPrefix.length());
112             aclPrefix = denyPrefix;
113         } else {
114             permission = value;
115             aclPrefix = StringUtil.EMPTY;
116         }
117         if (StringUtil.isBlank(permission)) {
118             return null;
119         }
120         if (permission.startsWith(fessConfig.getRoleSearchUserPrefix())
121                 && permission.length() > fessConfig.getRoleSearchUserPrefix().length()) {
122             return aclPrefix + userPrefix + permission.substring(fessConfig.getRoleSearchUserPrefix().length());
123         }
124         if (permission.startsWith(fessConfig.getRoleSearchGroupPrefix())
125                 && permission.length() > fessConfig.getRoleSearchGroupPrefix().length()) {
126             return aclPrefix + groupPrefix + permission.substring(fessConfig.getRoleSearchGroupPrefix().length());
127         }
128         if (permission.startsWith(fessConfig.getRoleSearchRolePrefix())
129                 && permission.length() > fessConfig.getRoleSearchRolePrefix().length()) {
130             return aclPrefix + rolePrefix + permission.substring(fessConfig.getRoleSearchRolePrefix().length());
131         }
132         return permission;
133     }
134 
135     public void setRolePrefix(final String rolePrefix) {
136         this.rolePrefix = rolePrefix;
137     }
138 
139     public void setGroupPrefix(final String groupPrefix) {
140         this.groupPrefix = groupPrefix;
141     }
142 
143     public void setUserPrefix(final String userPrefix) {
144         this.userPrefix = userPrefix;
145     }
146 
147     public List<String> getSmbRoleTypeList(final ResponseData responseData) {
148         final List<String> roleTypeList = new ArrayList<>();
149         final FessConfig fessConfig = ComponentUtil.getFessConfig();
150         if (fessConfig.isSmbRoleFromFile()) {
151             final SambaHelper sambaHelper = ComponentUtil.getSambaHelper();
152             final Map<String, Object> metaDataMap = responseData.getMetaDataMap();
153             if (responseData.getUrl().startsWith("smb:")) {
154                 final SID[] allowedSids = (SID[]) metaDataMap.get(SmbClient.SMB_ALLOWED_SID_ENTRIES);
155                 if (allowedSids != null) {
156                     for (final SID sid : allowedSids) {
157                         final String accountId = sambaHelper.getAccountId(sid);
158                         if (accountId != null) {
159                             roleTypeList.add(accountId);
160                         }
161                     }
162                 }
163                 final SID[] deniedSids = (SID[]) metaDataMap.get(SmbClient.SMB_DENIED_SID_ENTRIES);
164                 if (deniedSids != null) {
165                     for (final SID sid : deniedSids) {
166                         final String accountId = sambaHelper.getAccountId(sid);
167                         if (accountId != null) {
168                             roleTypeList.add(fessConfig.getRoleSearchDeniedPrefix() + accountId);
169                         }
170                     }
171                 }
172                 if (logger.isDebugEnabled()) {
173                     logger.debug("smbUrl:{} roleType:{}", responseData.getUrl(), roleTypeList);
174                 }
175             } else if (responseData.getUrl().startsWith("smb1:")) {
176                 final jcifs.smb1.smb1.SID[] allowedSids =
177                         (jcifs.smb1.smb1.SID[]) metaDataMap.get(org.codelibs.fess.crawler.client.smb1.SmbClient.SMB_ALLOWED_SID_ENTRIES);
178                 if (allowedSids != null) {
179                     for (final jcifs.smb1.smb1.SID sid : allowedSids) {
180                         final String accountId = sambaHelper.getAccountId(sid);
181                         if (accountId != null) {
182                             roleTypeList.add(accountId);
183                         }
184                     }
185                 }
186                 final jcifs.smb1.smb1.SID[] deniedSids =
187                         (jcifs.smb1.smb1.SID[]) metaDataMap.get(org.codelibs.fess.crawler.client.smb1.SmbClient.SMB_DENIED_SID_ENTRIES);
188                 if (deniedSids != null) {
189                     for (final jcifs.smb1.smb1.SID sid : deniedSids) {
190                         final String accountId = sambaHelper.getAccountId(sid);
191                         if (accountId != null) {
192                             roleTypeList.add(fessConfig.getRoleSearchDeniedPrefix() + accountId);
193                         }
194                     }
195                 }
196                 if (logger.isDebugEnabled()) {
197                     logger.debug("smb1Url:{} roleType:{}", responseData.getUrl(), roleTypeList);
198                 }
199             }
200         }
201         return roleTypeList;
202     }
203 
204     public List<String> getFileRoleTypeList(final ResponseData responseData) {
205         final List<String> roleTypeList = new ArrayList<>();
206         final FessConfig fessConfig = ComponentUtil.getFessConfig();
207         if (fessConfig.isFileRoleFromFile() && responseData.getUrl().startsWith("file:")) {
208             final String owner = (String) responseData.getMetaDataMap().get(FileSystemClient.FS_FILE_USER);
209             if (owner != null) {
210                 roleTypeList.add(systemHelper.getSearchRoleByUser(owner));
211             }
212             final String[] groups = (String[]) responseData.getMetaDataMap().get(FileSystemClient.FS_FILE_GROUPS);
213             roleTypeList.addAll(stream(groups).get(stream -> stream.map(systemHelper::getSearchRoleByGroup).collect(Collectors.toList())));
214             if (logger.isDebugEnabled()) {
215                 logger.debug("fileUrl:{} roleType:{}", responseData.getUrl(), roleTypeList);
216             }
217         }
218         return roleTypeList;
219     }
220 
221     public List<String> getFtpRoleTypeList(final ResponseData responseData) {
222         final List<String> roleTypeList = new ArrayList<>();
223         final FessConfig fessConfig = ComponentUtil.getFessConfig();
224         if (fessConfig.isFtpRoleFromFile() && responseData.getUrl().startsWith("ftp:")) {
225             final String owner = (String) responseData.getMetaDataMap().get(FtpClient.FTP_FILE_USER);
226             if (owner != null) {
227                 roleTypeList.add(systemHelper.getSearchRoleByUser(owner));
228             }
229             final String group = (String) responseData.getMetaDataMap().get(FtpClient.FTP_FILE_GROUP);
230             if (group != null) {
231                 roleTypeList.add(systemHelper.getSearchRoleByGroup(group));
232             }
233             if (logger.isDebugEnabled()) {
234                 logger.debug("ftpUrl:{} roleType:{}", responseData.getUrl(), roleTypeList);
235             }
236         }
237         return roleTypeList;
238     }
239 
240     public void setAllowPrefix(final String allowPrefix) {
241         this.allowPrefix = allowPrefix;
242     }
243 
244     public void setDenyPrefix(final String denyPrefix) {
245         this.denyPrefix = denyPrefix;
246     }
247 }