1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.jndi;
21
22
23 import java.util.Hashtable;
24
25 import javax.naming.ConfigurationException;
26 import javax.naming.Context;
27 import javax.naming.InvalidNameException;
28 import javax.naming.NamingException;
29 import javax.naming.ldap.LdapName;
30 import javax.naming.spi.InitialContextFactory;
31
32 import org.apache.directory.api.ldap.model.constants.AuthenticationLevel;
33 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
34 import org.apache.directory.api.ldap.model.name.Dn;
35 import org.apache.directory.api.ldap.util.JndiUtils;
36 import org.apache.directory.api.util.Strings;
37 import org.apache.directory.server.core.api.CoreSession;
38 import org.apache.directory.server.core.api.DirectoryService;
39 import org.apache.directory.server.i18n.I18n;
40
41
42
43
44
45
46
47
48
49 public class CoreContextFactory implements InitialContextFactory
50 {
51 public synchronized Context getInitialContext( Hashtable env ) throws NamingException
52 {
53 env = ( Hashtable<String, Object> ) env.clone();
54 Dn principalDn = null;
55
56 byte[] credential = getCredential( env );
57 String providerUrl = getProviderUrl( env );
58
59 DirectoryService service = ( DirectoryService ) env.get( DirectoryService.JNDI_KEY );
60
61 if ( service == null )
62 {
63 throw new ConfigurationException( I18n.err( I18n.ERR_477, env ) );
64 }
65
66 if ( !service.isStarted() )
67 {
68 return new DeadContext();
69 }
70
71 try
72 {
73 principalDn = new Dn( service.getSchemaManager(), getPrincipal( env ) );
74 }
75 catch ( LdapInvalidDnException lide )
76 {
77 throw new InvalidNameException( I18n.err( I18n.ERR_733, env ) );
78 }
79
80 ServerLdapContext ctx = null;
81 try
82 {
83 CoreSession session = service.getSession( principalDn, credential );
84 ctx = new ServerLdapContext( service, session, new LdapName( providerUrl ) );
85 }
86 catch ( Exception e )
87 {
88 JndiUtils.wrap( e );
89 }
90
91
92 ctx.lookup( "" );
93 return ctx;
94 }
95
96
97 public static String getProviderUrl( Hashtable<String, Object> env )
98 {
99 String providerUrl;
100 Object value;
101 value = env.get( Context.PROVIDER_URL );
102 if ( value == null )
103 {
104 value = "";
105 }
106 providerUrl = value.toString();
107
108 env.put( Context.PROVIDER_URL, providerUrl );
109
110 return providerUrl;
111 }
112
113
114 public static String getAuthentication( Hashtable<String, Object> env )
115 {
116 String authentication;
117 Object value = env.get( Context.SECURITY_AUTHENTICATION );
118 if ( value == null )
119 {
120 authentication = AuthenticationLevel.NONE.toString();
121 }
122 else
123 {
124 authentication = value.toString();
125 }
126
127 env.put( Context.SECURITY_AUTHENTICATION, authentication );
128
129 return authentication;
130 }
131
132
133 public static byte[] getCredential( Hashtable<String, Object> env ) throws javax.naming.ConfigurationException
134 {
135 byte[] credential;
136 Object value = env.get( Context.SECURITY_CREDENTIALS );
137 if ( value == null )
138 {
139 credential = null;
140 }
141 else if ( value instanceof String )
142 {
143 credential = Strings.getBytesUtf8( ( String ) value );
144 }
145 else if ( value instanceof byte[] )
146 {
147 credential = ( byte[] ) value;
148 }
149 else
150 {
151 throw new javax.naming.ConfigurationException( I18n.err( I18n.ERR_478, Context.SECURITY_CREDENTIALS ) );
152 }
153
154 if ( credential != null )
155 {
156 env.put( Context.SECURITY_CREDENTIALS, credential );
157 }
158
159 return credential;
160 }
161
162
163 public static String getPrincipal( Hashtable<String, Object> env )
164 {
165 String principal;
166 Object value = env.get( Context.SECURITY_PRINCIPAL );
167 if ( value == null )
168 {
169 principal = null;
170 }
171 else
172 {
173 principal = value.toString();
174 env.put( Context.SECURITY_PRINCIPAL, principal );
175 }
176
177 return principal;
178 }
179 }