1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "ldapaccess.hxx"
28cdf0e10cSrcweir #include "ldapuserprofilebe.hxx"
29cdf0e10cSrcweir #include <osl/file.hxx>
30cdf0e10cSrcweir #include <osl/module.hxx>
31cdf0e10cSrcweir #include <osl/process.h>
32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33cdf0e10cSrcweir #include <rtl/byteseq.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #ifndef INCLUDED_RTL_INSTANCE_HXX_
36cdf0e10cSrcweir #include <rtl/instance.hxx>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
39cdf0e10cSrcweir #include <com/sun/star/beans/Optional.hpp>
40cdf0e10cSrcweir #include <osl/security.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //==============================================================================
43cdf0e10cSrcweir namespace extensions { namespace config { namespace ldap {
44cdf0e10cSrcweir 
LdapUserProfileBe(const uno::Reference<uno::XComponentContext> & xContext)45cdf0e10cSrcweir LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
46cdf0e10cSrcweir : LdapProfileMutexHolder(),
47cdf0e10cSrcweir   BackendBase(mMutex)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     LdapDefinition aDefinition;
50cdf0e10cSrcweir     rtl::OUString loggedOnUser;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     // This whole rigmarole is to prevent an infinite recursion where reading
53cdf0e10cSrcweir     // the configuration for the backend would create another instance of the
54cdf0e10cSrcweir     // backend, which would try and read the configuration which would...
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         osl::Mutex & aInitMutex = rtl::Static< osl::Mutex, LdapUserProfileBe >::get();
57cdf0e10cSrcweir         osl::MutexGuard aInitGuard(aInitMutex);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir         static bool bReentrantCall; // = false
60cdf0e10cSrcweir         OSL_ENSURE(!bReentrantCall, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         if (!bReentrantCall)
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             try
65cdf0e10cSrcweir             {
66cdf0e10cSrcweir                 bReentrantCall = true ;
67cdf0e10cSrcweir                 if (!readLdapConfiguration(
68cdf0e10cSrcweir                         css::uno::Reference< css::lang::XMultiServiceFactory >(
69cdf0e10cSrcweir                             xContext->getServiceManager(),
70cdf0e10cSrcweir                             css::uno::UNO_QUERY_THROW),
71cdf0e10cSrcweir                         &aDefinition, &loggedOnUser))
72cdf0e10cSrcweir                 {
73cdf0e10cSrcweir                     throw css::uno::RuntimeException(
74cdf0e10cSrcweir                         rtl::OUString::createFromAscii("LdapUserProfileBe- LDAP not configured"),
75cdf0e10cSrcweir                         NULL);
76cdf0e10cSrcweir                 }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir                 bReentrantCall = false ;
79cdf0e10cSrcweir             }
80cdf0e10cSrcweir             catch (...)
81cdf0e10cSrcweir             {
82cdf0e10cSrcweir                 bReentrantCall = false;
83cdf0e10cSrcweir                 throw;
84cdf0e10cSrcweir             }
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     LdapConnection connection;
89cdf0e10cSrcweir     connection.loadModule();
90cdf0e10cSrcweir     connection.connectSimple(aDefinition);
91cdf0e10cSrcweir     connection.getUserProfile(loggedOnUser, &data_);
92cdf0e10cSrcweir }
93cdf0e10cSrcweir //------------------------------------------------------------------------------
~LdapUserProfileBe()94cdf0e10cSrcweir LdapUserProfileBe::~LdapUserProfileBe()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir }
97cdf0e10cSrcweir //------------------------------------------------------------------------------
98cdf0e10cSrcweir 
readLdapConfiguration(css::uno::Reference<css::lang::XMultiServiceFactory> const & factory,LdapDefinition * definition,rtl::OUString * loggedOnUser)99cdf0e10cSrcweir bool LdapUserProfileBe::readLdapConfiguration(
100cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > const & factory,
101cdf0e10cSrcweir     LdapDefinition * definition, rtl::OUString * loggedOnUser)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     OSL_ASSERT(factory.is() && definition != 0 && loggedOnUser != 0);
104cdf0e10cSrcweir     const rtl::OUString kConfigurationProviderService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationProvider")) ;
105cdf0e10cSrcweir     const rtl::OUString kReadOnlyViewService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")) ;
106cdf0e10cSrcweir     const rtl::OUString kComponent( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.LDAP/UserDirectory"));
107cdf0e10cSrcweir     const rtl::OUString kServerDefiniton(RTL_CONSTASCII_USTRINGPARAM ("ServerDefinition"));
108cdf0e10cSrcweir     const rtl::OUString kServer(RTL_CONSTASCII_USTRINGPARAM ("Server"));
109cdf0e10cSrcweir     const rtl::OUString kPort(RTL_CONSTASCII_USTRINGPARAM("Port"));
110cdf0e10cSrcweir     const rtl::OUString kBaseDN(RTL_CONSTASCII_USTRINGPARAM("BaseDN"));
111cdf0e10cSrcweir     const rtl::OUString kUser(RTL_CONSTASCII_USTRINGPARAM("SearchUser"));
112cdf0e10cSrcweir     const rtl::OUString kPassword(RTL_CONSTASCII_USTRINGPARAM("SearchPassword"));
113cdf0e10cSrcweir     const rtl::OUString kUserObjectClass(RTL_CONSTASCII_USTRINGPARAM("UserObjectClass"));
114cdf0e10cSrcweir     const rtl::OUString kUserUniqueAttr(RTL_CONSTASCII_USTRINGPARAM("UserUniqueAttribute"));
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	uno::Reference< XInterface > xIface;
117cdf0e10cSrcweir     try
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
120cdf0e10cSrcweir                                                         factory->createInstance(kConfigurationProviderService),
121cdf0e10cSrcweir                                                         uno::UNO_QUERY);
122cdf0e10cSrcweir 	    OSL_ENSURE(xCfgProvider.is(),"LdapUserProfileBe: could not create the configuration provider");
123cdf0e10cSrcweir 	    if (!xCfgProvider.is())
124cdf0e10cSrcweir             return false;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         css::beans::NamedValue aPath(rtl::OUString::createFromAscii("nodepath"), uno::makeAny(kComponent) );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         uno::Sequence< uno::Any > aArgs(1);
129cdf0e10cSrcweir         aArgs[0] <<=  aPath;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         xIface = xCfgProvider->createInstanceWithArguments(kReadOnlyViewService, aArgs);
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
134cdf0e10cSrcweir         xAccess->getByName(kServerDefiniton) >>= xIface;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         if (!getLdapStringParam(xChildAccess, kServer, definition->mServer))
139cdf0e10cSrcweir             return false;
140cdf0e10cSrcweir         if (!getLdapStringParam(xChildAccess, kBaseDN, definition->mBaseDN))
141cdf0e10cSrcweir             return false;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         definition->mPort=0;
144cdf0e10cSrcweir         xChildAccess->getByName(kPort) >>= definition->mPort ;
145cdf0e10cSrcweir 	    if (definition->mPort == 0)
146cdf0e10cSrcweir 		    return false;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         if (!getLdapStringParam(xAccess, kUserObjectClass, definition->mUserObjectClass))
149cdf0e10cSrcweir             return false;
150cdf0e10cSrcweir         if (!getLdapStringParam(xAccess, kUserUniqueAttr, definition->mUserUniqueAttr))
151cdf0e10cSrcweir             return false;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         getLdapStringParam(xAccess, kUser, definition->mAnonUser);
154cdf0e10cSrcweir         getLdapStringParam(xAccess, kPassword, definition->mAnonCredentials);
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir     catch (uno::Exception & e)
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         OSL_TRACE("LdapUserProfileBackend: access to configuration data failed: %s",
159cdf0e10cSrcweir                 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
160cdf0e10cSrcweir         return false;
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     osl::Security aSecurityContext;
164cdf0e10cSrcweir 	if (!aSecurityContext.getUserName(*loggedOnUser))
165cdf0e10cSrcweir 		OSL_TRACE("LdapUserProfileBackend - could not get Logged on user from system");
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     sal_Int32 nIndex = loggedOnUser->indexOf('/');
168cdf0e10cSrcweir 	if (nIndex > 0)
169cdf0e10cSrcweir 		*loggedOnUser = loggedOnUser->copy(nIndex+1);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     //Remember to remove
172cdf0e10cSrcweir     OSL_TRACE("Logged on user is %s", rtl::OUStringToOString(*loggedOnUser,RTL_TEXTENCODING_ASCII_US).getStr());
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     return true;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir //------------------------------------------------------------------------------
getLdapStringParam(uno::Reference<container::XNameAccess> & xAccess,const rtl::OUString & aLdapSetting,rtl::OString & aServerParameter)178cdf0e10cSrcweir bool LdapUserProfileBe::getLdapStringParam(
179cdf0e10cSrcweir 	uno::Reference<container::XNameAccess>& xAccess,
180cdf0e10cSrcweir 	const rtl::OUString& aLdapSetting,
181cdf0e10cSrcweir 	rtl::OString& aServerParameter)
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     rtl::OUString sParam;
184cdf0e10cSrcweir     xAccess->getByName(aLdapSetting) >>= sParam;
185cdf0e10cSrcweir     aServerParameter = rtl::OUStringToOString(sParam, RTL_TEXTENCODING_ASCII_US);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     return aServerParameter.getLength() != 0;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir //------------------------------------------------------------------------------
setPropertyValue(rtl::OUString const &,css::uno::Any const &)190cdf0e10cSrcweir void LdapUserProfileBe::setPropertyValue(
191cdf0e10cSrcweir     rtl::OUString const &, css::uno::Any const &)
192cdf0e10cSrcweir     throw (
193cdf0e10cSrcweir         css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
194cdf0e10cSrcweir         css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
195cdf0e10cSrcweir         css::uno::RuntimeException)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     throw css::lang::IllegalArgumentException(
198cdf0e10cSrcweir         rtl::OUString(
199cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("setPropertyValue not supported")),
200cdf0e10cSrcweir         static_cast< cppu::OWeakObject * >(this), -1);
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
getPropertyValue(rtl::OUString const & PropertyName)203cdf0e10cSrcweir css::uno::Any LdapUserProfileBe::getPropertyValue(
204cdf0e10cSrcweir     rtl::OUString const & PropertyName)
205cdf0e10cSrcweir     throw (
206cdf0e10cSrcweir         css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
207cdf0e10cSrcweir         css::uno::RuntimeException)
208cdf0e10cSrcweir {
209cdf0e10cSrcweir     for (sal_Int32 i = 0;;) {
210cdf0e10cSrcweir         sal_Int32 j = PropertyName.indexOf(',', i);
211cdf0e10cSrcweir         if (j == -1) {
212cdf0e10cSrcweir             j = PropertyName.getLength();
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir         if (j == i) {
215cdf0e10cSrcweir             throw css::beans::UnknownPropertyException(
216cdf0e10cSrcweir                 PropertyName, static_cast< cppu::OWeakObject * >(this));
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir         LdapData::iterator k(data_.find(PropertyName.copy(i, j - i)));
219cdf0e10cSrcweir         if (k != data_.end()) {
220cdf0e10cSrcweir             return css::uno::makeAny(
221cdf0e10cSrcweir                 css::beans::Optional< css::uno::Any >(
222cdf0e10cSrcweir                     true, css::uno::makeAny(k->second)));
223cdf0e10cSrcweir         }
224cdf0e10cSrcweir         if (j == PropertyName.getLength()) {
225cdf0e10cSrcweir             break;
226cdf0e10cSrcweir         }
227cdf0e10cSrcweir         i = j + 1;
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir     return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir //------------------------------------------------------------------------------
getLdapUserProfileBeName(void)233cdf0e10cSrcweir rtl::OUString SAL_CALL LdapUserProfileBe::getLdapUserProfileBeName(void) {
234cdf0e10cSrcweir 	return rtl::OUString::createFromAscii("com.sun.star.comp.configuration.backend.LdapUserProfileBe") ;
235cdf0e10cSrcweir }
236cdf0e10cSrcweir //------------------------------------------------------------------------------
237cdf0e10cSrcweir 
getImplementationName(void)238cdf0e10cSrcweir rtl::OUString SAL_CALL LdapUserProfileBe::getImplementationName(void)
239cdf0e10cSrcweir     throw (uno::RuntimeException)
240cdf0e10cSrcweir {
241cdf0e10cSrcweir     return getLdapUserProfileBeName() ;
242cdf0e10cSrcweir }
243cdf0e10cSrcweir //------------------------------------------------------------------------------
244cdf0e10cSrcweir 
getLdapUserProfileBeServiceNames(void)245cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL LdapUserProfileBe::getLdapUserProfileBeServiceNames(void)
246cdf0e10cSrcweir {
247cdf0e10cSrcweir     uno::Sequence<rtl::OUString> aServices(1) ;
248cdf0e10cSrcweir     aServices[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.backend.LdapUserProfileBe")) ;
249cdf0e10cSrcweir     return aServices ;
250cdf0e10cSrcweir }
251cdf0e10cSrcweir //------------------------------------------------------------------------------
252cdf0e10cSrcweir 
supportsService(const rtl::OUString & aServiceName)253cdf0e10cSrcweir sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const rtl::OUString& aServiceName)
254cdf0e10cSrcweir     throw (uno::RuntimeException)
255cdf0e10cSrcweir {
256cdf0e10cSrcweir     uno::Sequence< rtl::OUString > const svc = getLdapUserProfileBeServiceNames();
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	for(sal_Int32 i = 0; i < svc.getLength(); ++i )
259cdf0e10cSrcweir 		if(svc[i] == aServiceName)
260cdf0e10cSrcweir 			return true;
261cdf0e10cSrcweir 	return false;
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir //------------------------------------------------------------------------------
265cdf0e10cSrcweir 
266cdf0e10cSrcweir uno::Sequence<rtl::OUString>
getSupportedServiceNames(void)267cdf0e10cSrcweir SAL_CALL LdapUserProfileBe::getSupportedServiceNames(void)
268cdf0e10cSrcweir     throw (uno::RuntimeException)
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     return getLdapUserProfileBeServiceNames() ;
271cdf0e10cSrcweir }
272cdf0e10cSrcweir // ---------------------------------------------------------------------------------------
273cdf0e10cSrcweir }}}
274cdf0e10cSrcweir // ---------------------------------------------------------------------------------------
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 
277