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