-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
JndiManager.java
301 lines (279 loc) · 14.1 KB
/
JndiManager.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.logging.log4j.core.net;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import org.apache.logging.log4j.core.appender.AbstractManager;
import org.apache.logging.log4j.core.appender.ManagerFactory;
import org.apache.logging.log4j.core.util.JndiCloser;
import org.apache.logging.log4j.core.util.NetUtils;
import org.apache.logging.log4j.util.PropertiesUtil;
/**
* Manages a JNDI {@link javax.naming.directory.DirContext}.
*
* @since 2.1
*/
public class JndiManager extends AbstractManager {
public static final String ALLOWED_HOSTS = "allowedLdapHosts";
public static final String ALLOWED_CLASSES = "allowedLdapClasses";
public static final String ALLOWED_PROTOCOLS = "allowedJndiProtocols";
private static final JndiManagerFactory FACTORY = new JndiManagerFactory();
private static final String PREFIX = "log4j2.";
private static final String LDAP = "ldap";
private static final String LDAPS = "ldaps";
private static final String JAVA = "java";
private static final List<String> permanentAllowedHosts = NetUtils.getLocalIps();
private static final List<String> permanentAllowedClasses = Arrays.asList(Boolean.class.getName(),
Byte.class.getName(), Character.class.getName(), Double.class.getName(), Float.class.getName(),
Integer.class.getName(), Long.class.getName(), Short.class.getName(), String.class.getName());
private static final List<String> permanentAllowedProtocols = Arrays.asList(JAVA, LDAP, LDAPS);
private static final String SERIALIZED_DATA = "javaSerializedData";
private static final String CLASS_NAME = "javaClassName";
private static final String REFERENCE_ADDRESS = "javaReferenceAddress";
private static final String OBJECT_FACTORY = "javaFactory";
private final List<String> allowedHosts;
private final List<String> allowedClasses;
private final List<String> allowedProtocols;
private final DirContext context;
private JndiManager(final String name, final DirContext context, final List<String> allowedHosts,
final List<String> allowedClasses, final List<String> allowedProtocols) {
super(null, name);
this.context = context;
this.allowedHosts = allowedHosts;
this.allowedClasses = allowedClasses;
this.allowedProtocols = allowedProtocols;
}
/**
* Gets the default JndiManager using the default {@link javax.naming.InitialContext}.
*
* @return the default JndiManager
*/
public static JndiManager getDefaultManager() {
return getManager(JndiManager.class.getName(), FACTORY, null);
}
/**
* Gets a named JndiManager using the default {@link javax.naming.InitialContext}.
*
* @param name the name of the JndiManager instance to create or use if available
* @return a default JndiManager
*/
public static JndiManager getDefaultManager(final String name) {
return getManager(name, FACTORY, null);
}
/**
* Gets a JndiManager with the provided configuration information.
*
* @param initialContextFactoryName Fully qualified class name of an implementation of
* {@link javax.naming.spi.InitialContextFactory}.
* @param providerURL The provider URL to use for the JNDI connection (specific to the above factory).
* @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory
* class that will create a URL context factory
* @param securityPrincipal The name of the identity of the Principal.
* @param securityCredentials The security credentials of the Principal.
* @param additionalProperties Any additional JNDI environment properties to set or {@code null} for none.
* @return the JndiManager for the provided parameters.
*/
public static JndiManager getJndiManager(final String initialContextFactoryName,
final String providerURL,
final String urlPkgPrefixes,
final String securityPrincipal,
final String securityCredentials,
final Properties additionalProperties) {
final Properties properties = createProperties(initialContextFactoryName, providerURL, urlPkgPrefixes,
securityPrincipal, securityCredentials, additionalProperties);
return getManager(createManagerName(), FACTORY, properties);
}
/**
* Gets a JndiManager with the provided configuration information.
*
* @param properties JNDI properties, usually created by calling {@link #createProperties(String, String, String, String, String, Properties)}.
* @return the JndiManager for the provided parameters.
* @see #createProperties(String, String, String, String, String, Properties)
* @since 2.9
*/
public static JndiManager getJndiManager(final Properties properties) {
return getManager(createManagerName(), FACTORY, properties);
}
private static String createManagerName() {
return JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
}
/**
* Creates JNDI Properties with the provided configuration information.
*
* @param initialContextFactoryName
* Fully qualified class name of an implementation of {@link javax.naming.spi.InitialContextFactory}.
* @param providerURL
* The provider URL to use for the JNDI connection (specific to the above factory).
* @param urlPkgPrefixes
* A colon-separated list of package prefixes for the class name of the factory class that will create a
* URL context factory
* @param securityPrincipal
* The name of the identity of the Principal.
* @param securityCredentials
* The security credentials of the Principal.
* @param additionalProperties
* Any additional JNDI environment properties to set or {@code null} for none.
* @return the Properties for the provided parameters.
* @since 2.9
*/
public static Properties createProperties(final String initialContextFactoryName, final String providerURL,
final String urlPkgPrefixes, final String securityPrincipal, final String securityCredentials,
final Properties additionalProperties) {
if (initialContextFactoryName == null) {
return null;
}
final Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
if (providerURL != null) {
properties.setProperty(Context.PROVIDER_URL, providerURL);
} else {
LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated "
+ "provider URL. This is likely to cause problems.", initialContextFactoryName);
}
if (urlPkgPrefixes != null) {
properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
}
if (securityPrincipal != null) {
properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
if (securityCredentials != null) {
properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
} else {
LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
securityPrincipal);
}
}
if (additionalProperties != null) {
properties.putAll(additionalProperties);
}
return properties;
}
@Override
protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
return JndiCloser.closeSilently(this.context);
}
/**
* Looks up a named object through this JNDI context.
*
* @param name name of the object to look up.
* @param <T> the type of the object.
* @return the named object if it could be located.
* @throws NamingException if a naming exception is encountered
*/
@SuppressWarnings("unchecked")
public synchronized <T> T lookup(final String name) throws NamingException {
try {
URI uri = new URI(name);
if (uri.getScheme() != null) {
if (!allowedProtocols.contains(uri.getScheme().toLowerCase(Locale.ROOT))) {
LOGGER.warn("Log4j JNDI does not allow protocol {}", uri.getScheme());
return null;
}
if (LDAP.equalsIgnoreCase(uri.getScheme()) || LDAPS.equalsIgnoreCase(uri.getScheme())) {
if (!allowedHosts.contains(uri.getHost())) {
LOGGER.warn("Attempt to access ldap server not in allowed list");
return null;
}
Attributes attributes = this.context.getAttributes(name);
if (attributes != null) {
// In testing the "key" for attributes seems to be lowercase while the attribute id is
// camelcase, but that may just be true for the test LDAP used here. This copies the Attributes
// to a Map ignoring the "key" and using the Attribute's id as the key in the Map so it matches
// the Java schema.
Map<String, Attribute> attributeMap = new HashMap<>();
NamingEnumeration<? extends Attribute> enumeration = attributes.getAll();
while (enumeration.hasMore()) {
Attribute attribute = enumeration.next();
attributeMap.put(attribute.getID(), attribute);
}
Attribute classNameAttr = attributeMap.get(CLASS_NAME);
if (attributeMap.get(SERIALIZED_DATA) != null) {
if (classNameAttr != null) {
String className = classNameAttr.get().toString();
if (!allowedClasses.contains(className)) {
LOGGER.warn("Deserialization of {} is not allowed", className);
return null;
}
} else {
LOGGER.warn("No class name provided for {}", name);
return null;
}
} else if (attributeMap.get(REFERENCE_ADDRESS) != null
|| attributeMap.get(OBJECT_FACTORY) != null) {
LOGGER.warn("Referenceable class is not allowed for {}", name);
return null;
}
}
}
}
} catch (URISyntaxException ex) {
// This is OK.
}
return (T) this.context.lookup(name);
}
private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
@Override
public JndiManager createManager(final String name, final Properties data) {
String hosts = data != null ? data.getProperty(ALLOWED_HOSTS) : null;
String classes = data != null ? data.getProperty(ALLOWED_CLASSES) : null;
String protocols = data != null ? data.getProperty(ALLOWED_PROTOCOLS) : null;
List<String> allowedHosts = new ArrayList<>();
List<String> allowedClasses = new ArrayList<>();
List<String> allowedProtocols = new ArrayList<>();
addAll(hosts, allowedHosts, permanentAllowedHosts, ALLOWED_HOSTS, data);
addAll(classes, allowedClasses, permanentAllowedClasses, ALLOWED_CLASSES, data);
addAll(protocols, allowedProtocols, permanentAllowedProtocols, ALLOWED_PROTOCOLS, data);
try {
return new JndiManager(name, new InitialDirContext(data), allowedHosts, allowedClasses,
allowedProtocols);
} catch (final NamingException e) {
LOGGER.error("Error creating JNDI InitialContext.", e);
return null;
}
}
private void addAll(String toSplit, List<String> list, List<String> permanentList, String propertyName,
Properties data) {
if (toSplit != null) {
list.addAll(Arrays.asList(toSplit.split("\\s*,\\s*")));
data.remove(propertyName);
}
toSplit = PropertiesUtil.getProperties().getStringProperty(PREFIX + propertyName);
if (toSplit != null) {
list.addAll(Arrays.asList(toSplit.split("\\s*,\\s*")));
}
list.addAll(permanentList);
}
}
@Override
public String toString() {
return "JndiManager [context=" + context + ", count=" + count + "]";
}
}