1 /*
2 * Copyright (c) 2002-2025, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.util.ldap;
35
36 import java.util.Hashtable;
37
38 import javax.naming.Context;
39 import javax.naming.NamingEnumeration;
40 import javax.naming.NamingException;
41 import javax.naming.directory.DirContext;
42 import javax.naming.directory.InitialDirContext;
43 import javax.naming.directory.SearchControls;
44 import javax.naming.directory.SearchResult;
45
46 /**
47 * This class provides LDAP generic connection methods
48 */
49 public final class LdapUtil
50 {
51 /**
52 * Constructor
53 */
54 private LdapUtil( )
55 {
56 // empty contructor
57 }
58
59 /**
60 * LDAP Context search
61 *
62 * @param strLDAPContext
63 * the context
64 * @param strLDAPUrl
65 * the url
66 * @param strAdminDN
67 * the admin dn to use
68 * @param strAdminPassword
69 * the admin password to use
70 * @return a DirContext object
71 * @throws NamingException
72 * in case of error
73 */
74 public static DirContext getContext( String strLDAPContext, String strLDAPUrl, String strAdminDN, String strAdminPassword ) throws NamingException
75 {
76 Hashtable<String, String> env = new Hashtable<>( );
77
78 env.put( Context.INITIAL_CONTEXT_FACTORY, strLDAPContext );
79 env.put( Context.PROVIDER_URL, strLDAPUrl );
80 env.put( Context.SECURITY_PRINCIPAL, strAdminDN );
81 env.put( Context.SECURITY_CREDENTIALS, strAdminPassword );
82
83 return new InitialDirContext( env );
84 }
85
86 /**
87 * Close the context given in parameter
88 *
89 * @param context
90 * the LDAP context as a DirContext object
91 * @throws NamingException
92 * in case of error
93 */
94 public static void freeContext( DirContext context ) throws NamingException
95 {
96 if ( context != null )
97 {
98 context.close( );
99 }
100 }
101
102 /**
103 *
104 * @param strLDAPContext
105 * the context
106 * @param strLDAPUrl
107 * the url
108 * @param strDN
109 * the dn of the user to connect
110 * @param strPassword
111 * the password of the user to connect
112 * @return a DirContext object
113 * @throws NamingException
114 * in case of error
115 */
116 public static DirContext bindUser( String strLDAPContext, String strLDAPUrl, String strDN, String strPassword ) throws NamingException
117 {
118 DirContext context = null;
119
120 Hashtable<String, String> env = new Hashtable<>( );
121
122 env.put( Context.INITIAL_CONTEXT_FACTORY, strLDAPContext );
123 env.put( Context.PROVIDER_URL, strLDAPUrl );
124 env.put( Context.SECURITY_PRINCIPAL, strDN );
125 env.put( Context.SECURITY_CREDENTIALS, strPassword );
126
127 context = new InitialDirContext( env );
128
129 return context;
130 }
131
132 /**
133 *
134 * @param context
135 * the context
136 * @param strFilter
137 * the filter for the search
138 * @param strUserDN
139 * the user dn
140 * @param strDNBase
141 * the base dn
142 * @param sc
143 * the SearchControls
144 * @return an enumeration of the elements found
145 * @throws NamingException
146 * in case of error
147 */
148 public static NamingEnumeration<SearchResult> searchUsers( DirContext context, String strFilter, String strUserDN, String strDNBase, SearchControls sc )
149 throws NamingException
150 {
151 return context.search( strUserDN + strDNBase, strFilter, sc );
152 }
153 }