View Javadoc
1   /*
2    * Copyright 2012-2025 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 java.util.Locale;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.codelibs.fess.mylasta.direction.FessConfig;
23  import org.codelibs.fess.util.ComponentUtil;
24  import org.codelibs.jcifs.smb.SID;
25  
26  import jakarta.annotation.PostConstruct;
27  
28  /**
29   * Helper class for Samba-related operations.
30   */
31  public class SambaHelper {
32  
33      private static final Logger logger = LogManager.getLogger(SambaHelper.class);
34  
35      /**
36       * SID type for an alias.
37       */
38      public static final int SID_TYPE_ALIAS = 4;
39  
40      /**
41       * SID type for a deleted account.
42       */
43      public static final int SID_TYPE_DELETED = 6;
44  
45      /**
46       * SID type for a domain group.
47       */
48      public static final int SID_TYPE_DOM_GRP = 2;
49  
50      /**
51       * SID type for a domain.
52       */
53      public static final int SID_TYPE_DOMAIN = 3;
54  
55      /**
56       * SID type for an invalid SID.
57       */
58      public static final int SID_TYPE_INVALID = 7;
59  
60      /**
61       * SID type for an unknown SID.
62       */
63      public static final int SID_TYPE_UNKNOWN = 8;
64  
65      /**
66       * SID type for a non-use SID.
67       */
68      public static final int SID_TYPE_USE_NONE = 0;
69  
70      /**
71       * SID type for a user.
72       */
73      public static final int SID_TYPE_USER = 1;
74  
75      /**
76       * SID type for a well-known group.
77       */
78      public static final int SID_TYPE_WKN_GRP = 5;
79  
80      /**
81       * The Fess configuration.
82       */
83      protected FessConfig fessConfig;
84  
85      /**
86       * Constructor.
87       */
88      public SambaHelper() {
89          super();
90      }
91  
92      /**
93       * Initializes the SambaHelper.
94       */
95      @PostConstruct
96      public void init() {
97          if (logger.isDebugEnabled()) {
98              logger.debug("Initializing {}", this.getClass().getSimpleName());
99          }
100         fessConfig = ComponentUtil.getFessConfig();
101     }
102 
103     /**
104      * Gets the account ID from a SID.
105      * @param sid The SID.
106      * @return The account ID.
107      */
108     public String getAccountId(final SID sid) {
109         final int type = sid.getType();
110         if (logger.isDebugEnabled()) {
111             try {
112                 logger.debug("Processing SID: {} {} {}", type, sid, sid.toDisplayString());
113             } catch (final Exception e) {
114                 // ignore
115             }
116         }
117         final Integer id = fessConfig.getAvailableSmbSidType(type);
118         if (id != null) {
119             return createSearchRole(id, sid.getAccountName());
120         }
121         if (logger.isDebugEnabled()) {
122             logger.debug("Ignored SID: {} {}", type, sid);
123         }
124         return null;
125     }
126 
127     /**
128      * Gets the account ID from a SID.
129      * @param sid The SID.
130      * @return The account ID.
131      */
132     public String getAccountId(final org.codelibs.jcifs.smb1.SID sid) {
133         final int type = sid.getType();
134         if (logger.isDebugEnabled()) {
135             try {
136                 logger.debug("Processing SID: {} {} {}", type, sid, sid.toDisplayString());
137             } catch (final Exception e) {
138                 // ignore
139             }
140         }
141         final Integer id = fessConfig.getAvailableSmbSidType(type);
142         if (id != null) {
143             return createSearchRole(id, sid.getAccountName());
144         }
145         if (logger.isDebugEnabled()) {
146             logger.debug("Ignored SID: {} {}", type, sid);
147         }
148         return null;
149     }
150 
151     /**
152      * Creates a search role.
153      * @param type The SID type.
154      * @param name The account name.
155      * @return The search role.
156      */
157     protected String createSearchRole(final int type, final String name) {
158         if (fessConfig.isLdapLowercasePermissionName()) {
159             return type + fessConfig.getCanonicalLdapName(name).toLowerCase(Locale.ROOT);
160         }
161         return type + fessConfig.getCanonicalLdapName(name);
162     }
163 }