1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef EXTENSIONS_CONFIG_LDAP_LDAPACCESS_HXX_
25 #define EXTENSIONS_CONFIG_LDAP_LDAPACCESS_HXX_
26 
27 #include "sal/config.h"
28 
29 #include <map>
30 
31 #include "wrapldapinclude.hxx"
32 #include <com/sun/star/ldap/LdapGenericException.hpp>
33 
34 #include <com/sun/star/ldap/LdapConnectionException.hpp>
35 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 #include <osl/module.h>
37 
38 namespace extensions { namespace config { namespace ldap {
39 
40 namespace css = com::sun::star ;
41 namespace uno = css::uno ;
42 namespace lang = css::lang ;
43 namespace ldap = css::ldap ;
44 //------------------------------------------------------------------------------
45 // LdapUserProfile classes
46 struct LdapUserProfile;
47 class LdapUserProfileMap;
48 
49 typedef LDAP_API(int) (LDAP_CALL *t_ldap_unbind_s)( LDAP *ld );
50 typedef LDAP_API(int) (LDAP_CALL *t_ldap_simple_bind_s)( LDAP *ld, const char *who,	const char *passwd );
51 typedef LDAP_API(int) (LDAP_CALL *t_ldap_set_option)( LDAP *ld, int option,	const void *optdata );
52 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_err2string)( int err );
53 typedef LDAP_API(LDAP *) (LDAP_CALL *t_ldap_init)( const char *defhost, int defport );
54 typedef LDAP_API(int) (LDAP_CALL *t_ldap_msgfree)( LDAPMessage *lm );
55 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_get_dn)( LDAP *ld, LDAPMessage *entry );
56 typedef LDAP_API(LDAPMessage *) (LDAP_CALL *t_ldap_first_entry)( LDAP *ld, 	LDAPMessage *chain );
57 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_first_attribute)( LDAP *ld, LDAPMessage *entry, void **ptr );
58 typedef LDAP_API(char *) (LDAP_CALL *t_ldap_next_attribute)( LDAP *ld, LDAPMessage *entry, void *ptr );
59 typedef LDAP_API(int) (LDAP_CALL *t_ldap_search_s)( LDAP *ld, const char *base, int scope,	const char *filter, char **attrs, int attrsonly, LDAPMessage **res );
60 typedef LDAP_API(void) (LDAP_CALL *t_ldap_value_free)( char **vals );
61 typedef LDAP_API(char **) (LDAP_CALL *t_ldap_get_values)( LDAP *ld, LDAPMessage *entry,	const char *target );
62 typedef LDAP_API(void) (LDAP_CALL *t_ldap_memfree)( void *p );
63 //------------------------------------------------------------------------------
64 /** Struct containing the information on LDAP connection */
65 struct LdapDefinition
66 {
67 	/** LDAP server name */
68     rtl::OString mServer ;
69     /** LDAP server port number */
70     sal_Int32 mPort ;
71     /** Repository base DN */
72     rtl::OString mBaseDN ;
73     /** DN to use for "anonymous" connection */
74     rtl::OString mAnonUser ;
75     /** Credentials to use for "anonymous" connection */
76     rtl::OString mAnonCredentials ;
77 	/** User Entity Object Class */
78 	rtl::OString mUserObjectClass;
79 	/** User Entity Unique Attribute */
80 	rtl::OString mUserUniqueAttr;
81  } ;
82 
83 typedef std::map< rtl::OUString, rtl::OUString > LdapData; // key/value pairs
84 
85 /** Class encapulating all LDAP functionality */
86 class LdapConnection
87 {
88     friend struct LdapMessageHolder;
89 public:
90 
91     /** Default constructor */
LdapConnection(void)92     LdapConnection(void) : mConnection(NULL),mLdapDefinition() {}
93     /** Destructor, releases the connection */
94     ~LdapConnection(void) ;
95 	/** Make connection to LDAP server */
96 	void  connectSimple(const LdapDefinition& aDefinition)
97         throw (ldap::LdapConnectionException,
98                 ldap::LdapGenericException);
99 
100 	/**
101         Gets LdapUserProfile from LDAP repository for specified user
102         @param aUser    name of logged on user
103         @param aUserProfileMap  Map containing LDAP->00o mapping
104        @param aUserProfile     struct for holding OOo values
105 
106          @throws com::sun::star::ldap::LdapGenericException
107                   if an LDAP error occurs.
108     */
109     void getUserProfile(const rtl::OUString& aUser, LdapData * data)
110          throw (lang::IllegalArgumentException,
111                  ldap::LdapConnectionException,
112                  ldap::LdapGenericException);
113 
114     /** finds DN of user
115 		@return  DN of User
116 	*/
117 	rtl::OString findUserDn(const rtl::OString& aUser)
118         throw (lang::IllegalArgumentException,
119                 ldap::LdapConnectionException,
120                 ldap::LdapGenericException);
121 
122     void loadModule();
123 
124     static t_ldap_err2string        s_p_err2string;
125 private:
126 
127 	void initConnection()
128          throw (ldap::LdapConnectionException);
129     void disconnect();
130 	/**
131       Indicates whether the connection is in a valid state.
132 	  @return   sal_True if connection is valid, sal_False otherwise
133       */
isValid(void) const134     bool isValid(void) const { return mConnection != NULL ; }
135 
136     void  connectSimple()
137         throw (ldap::LdapConnectionException,
138                 ldap::LdapGenericException);
139 
140     /** LDAP connection object */
141     LDAP* mConnection ;
142 	LdapDefinition mLdapDefinition;
143 
144     static oslModule		        s_Ldap_Module;
145     static t_ldap_value_free        s_p_value_free;
146     static t_ldap_get_values        s_p_get_values;
147     static t_ldap_unbind_s          s_p_unbind_s;
148     static t_ldap_simple_bind_s	    s_p_simple_bind_s;
149     static t_ldap_set_option        s_p_set_option;
150     static t_ldap_init              s_p_init;
151     static t_ldap_msgfree           s_p_msgfree;
152     static t_ldap_get_dn            s_p_get_dn;
153     static t_ldap_first_entry       s_p_first_entry;
154     static t_ldap_first_attribute   s_p_first_attribute;
155     static t_ldap_next_attribute    s_p_next_attribute;
156     static t_ldap_search_s          s_p_search_s;
157 
158     static t_ldap_memfree           s_p_memfree;
159 
160 } ;
161 //------------------------------------------------------------------------------
162 }} }
163 
164 #endif // EXTENSIONS_CONFIG_LDAP_LDAPUSERPROFILE_HXX_
165