src/server/daemon/ldap_resource.c

Wed, 05 Jun 2024 19:50:44 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 05 Jun 2024 19:50:44 +0200
changeset 537
ad44e72fbf50
parent 475
d01ea909d906
permissions
-rw-r--r--

add extra nullptr check in the event loop to handle the case when the finish ptr is set to NULL after it was already scheduled

461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2023 Olaf Wintermann. All rights reserved.
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
475
d01ea909d906 set _GNU_SOURCE in ldap_resource.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
29 #ifdef __gnu_linux__
d01ea909d906 set _GNU_SOURCE in ldap_resource.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
30 #define _GNU_SOURCE
d01ea909d906 set _GNU_SOURCE in ldap_resource.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
31 #endif
d01ea909d906 set _GNU_SOURCE in ldap_resource.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
32
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "ldap_resource.h"
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "../util/util.h"
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 static ResourceType ldap_resource_type = {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 (resource_pool_init_func)ldap_resourcepool_init,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 (resource_pool_destroy_func)ldap_resourcepool_destroy,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 (resource_pool_createresource_func)ldap_resourcepool_createresource,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 (resource_pool_freeresource_func)ldap_resourcepool_freeresource,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 (resource_pool_prepare_func)ldap_resourcepool_prepare,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 (resource_pool_finish_func)ldap_resourcepool_finish,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 (resource_pool_getresourcedata_func)ldap_resourcepool_getresourcedata
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 };
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 ResourceType* ldap_get_resource_type(void) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 return &ldap_resource_type;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 LDAP* ws_ldap_resource_create_connection(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 const char *hostname,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 int port,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 int ssl,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 int ldap_version)
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 LDAP *ld = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 ld = ldap_init(config->hostname, config->port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 char *ldap_uri = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 asprintf(&ldap_uri, "ldap://%s:%d", hostname, port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 ld = ws_ldap_resource_create_uri_connection(ldap_uri, ldap_version);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 free(ldap_uri);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 #endif
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
70
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
71 if(ld) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
72 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
75 return ld;
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 LDAP* ws_ldap_resource_create_uri_connection(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 const char *uri,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 int ldap_version)
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 log_ereport(LOG_FAILURE, "ldap_resource_create_connection_uri is not implemented on Solaris yet");
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 LDAP *ld = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 int init_ret = ldap_initialize(&ld, uri);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 if(init_ret) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 log_ereport(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 LOG_FAILURE,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 "ldap_resource_create_connection failed: uri: %s",
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 uri);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 return ld;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 #endif
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 void ws_ldap_close(LDAP *ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 ldap_unbind(ldap);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 ldap_unbind_ext_s(ldap, NULL, NULL);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 #endif
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 /*
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 * Validates settings from the pb pblock
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 * and creates an LDAPResourcePool object
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 *
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 * LDAPResourcePool contains all settings necessary for creating
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 * ldap connections.
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 */
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 void * ldap_resourcepool_init(pool_handle_t *pool, const char *rpname, pblock *pb) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 char *ldap_uri = pblock_findval("Uri", pb);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 char *host = pblock_findval("Host", pb);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 char *port = pblock_findval("Port", pb);
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
119 char *binddn = pblock_findval("Binddn", pb);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
120 char *bindpw = pblock_findval("Bindpw", pb);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
121 char *bind = pblock_findval("Bind", pb);
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
463
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
123 log_ereport(
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
124 LOG_INFORM,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
125 "create ldap resource pool: name=%s : %s%s%s%s : binddn=%s",
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
126 rpname,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
127 ldap_uri ? "uri=" : "host=",
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
128 ldap_uri ? ldap_uri : host,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
129 ldap_uri || !port ? "" : ":",
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
130 !port ? "" : port,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
131 binddn ? binddn : "''");
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
132
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
133 if(!ldap_uri && !host) {
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 log_ereport(LOG_MISCONFIG, "Resource pool %s: No host or ldap uri specified", rpname);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 if(ldap_uri && host) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 log_ereport(LOG_MISCONFIG, "Resource pool %s: Either Uri or Host must be specified, not both", rpname);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 int64_t port_i = 0;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 if(host) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 if(port) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 if(util_strtoint(port, &port_i)) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 if(port_i < 1 || port_i > 65535) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 log_ereport(LOG_MISCONFIG, "Resource pool %s: Port %s is out of range", rpname, port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 log_ereport(LOG_MISCONFIG, "Resource pool %s: Port '%s' is not a valid number", rpname, port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 port_i = LDAP_PORT;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 LDAPResourcePool *ldap_pool = pool_malloc(pool, sizeof(LDAPResourcePool));
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 if(!ldap_pool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 ldap_pool->name = rpname;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 ldap_pool->pool = pool;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 ldap_pool->ldap_uri = ldap_uri;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 ldap_pool->host = host;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 ldap_pool->port = (int)port_i;
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
167 ldap_pool->binddn = binddn;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
168 ldap_pool->bindpw = bindpw;
463
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
169 WSBool bind_def = binddn != NULL;
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
170 ldap_pool->bind = bind ? util_getboolean(bind, bind_def) : bind_def;
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 return ldap_pool;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 void ldap_resourcepool_destroy(LDAPResourcePool *pool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 void * ldap_resourcepool_createresource(LDAPResourcePool *respool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 LDAP *ldap = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 if(respool->ldap_uri) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 ldap = ws_ldap_resource_create_uri_connection(respool->ldap_uri, LDAP_VERSION3);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 ldap = ws_ldap_resource_create_connection(respool->host, respool->port, FALSE, LDAP_VERSION3);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 if(!ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 log_ereport(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 LOG_FAILURE,
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
190 "resource pool %s: %s: cannot create LDAP session",
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 respool->name,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 respool->ldap_uri ? respool->ldap_uri : respool->host);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
196 if(respool->bind) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
197 struct berval *server_cred;
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
198 int r = ldap_resource_bind(respool, ldap, &server_cred);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
199 if(r != LDAP_SUCCESS) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
200 log_ereport(LOG_FAILURE, "resource pool %s: bind failed: %s", respool->name, ldap_err2string(r));
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
201 ws_ldap_close(ldap);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
202 return NULL;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
203 }
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
204 }
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
205
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 LDAPResource *res = pool_malloc(respool->pool, sizeof(LDAPResource));
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 if(!res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 ws_ldap_close(ldap);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 log_ereport(LOG_CATASTROPHE, "ldap_resourcepool_createresource: OOM");
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 res->ldap = ldap;
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
213 res->res_pool = respool;
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 return res;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 void ldap_resourcepool_freeresource(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 if(res->ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 ws_ldap_close(res->ldap);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 pool_free(pool->pool, res);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 int ldap_resourcepool_prepare(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 return 0;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 int ldap_resourcepool_finish(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 return 0;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 void * ldap_resourcepool_getresourcedata(LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 return res->ldap;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 }
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
238
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
239
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
240 int ldap_resource_bind(LDAPResourcePool *respool, LDAP *ldap, struct berval **server_cred) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
241 if(!respool->binddn) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
242 return -1;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
243 }
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
244 return ws_ldap_bind(ldap, respool->binddn, respool->bindpw, server_cred);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
245 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
246
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
247 int ws_ldap_bind(LDAP *ldap, const char *binddn, const char *bindpw, struct berval **server_cred) {
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
248 struct berval cred;
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
249 cred.bv_val = (char*)bindpw;
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
250 cred.bv_len = strlen(cred.bv_val);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
251 return ldap_sasl_bind_s(
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
252 ldap,
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
253 binddn,
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
254 LDAP_SASL_SIMPLE,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
255 &cred,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
256 NULL,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
257 NULL,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
258 server_cred);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
259 }
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
260

mercurial