src/server/daemon/ldap_resource.c

Sat, 11 Mar 2023 13:57:30 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 11 Mar 2023 13:57:30 +0100
changeset 463
4fd523fff13b
parent 462
72848970541a
child 464
0a29110b94ec
permissions
-rw-r--r--

add option to automatically bind new ldap sessions

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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "ldap_resource.h"
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "../util/util.h"
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
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 static ResourceType ldap_resource_type = {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 (resource_pool_init_func)ldap_resourcepool_init,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 (resource_pool_destroy_func)ldap_resourcepool_destroy,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 (resource_pool_createresource_func)ldap_resourcepool_createresource,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 (resource_pool_freeresource_func)ldap_resourcepool_freeresource,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 (resource_pool_prepare_func)ldap_resourcepool_prepare,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 (resource_pool_finish_func)ldap_resourcepool_finish,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 (resource_pool_getresourcedata_func)ldap_resourcepool_getresourcedata
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 };
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 ResourceType* ldap_get_resource_type(void) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 return &ldap_resource_type;
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 LDAP* ws_ldap_resource_create_connection(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 const char *hostname,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 int port,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 int ssl,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 int ldap_version)
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 LDAP *ld = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 ld = ldap_init(config->hostname, config->port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 if(ld) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 log_ereport(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 LOG_FAILURE,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 "ldap_resource_create_connection failed: host: %s port: %d",
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 hostname,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 char *ldap_uri = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 asprintf(&ldap_uri, "ldap://%s:%d", hostname, port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 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
73 free(ldap_uri);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 #endif
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 if(!ld) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 return NULL;
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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
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 LDAP* ws_ldap_resource_create_uri_connection(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 const char *uri,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 int ldap_version)
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 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
88 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 LDAP *ld = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 int init_ret = ldap_initialize(&ld, uri);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 if(init_ret) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 log_ereport(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 LOG_FAILURE,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 "ldap_resource_create_connection failed: uri: %s",
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 uri);
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 return ld;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 #endif
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 void ws_ldap_close(LDAP *ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 ldap_unbind(ldap);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 ldap_unbind_ext_s(ldap, NULL, NULL);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 #endif
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
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 /*
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 * Validates settings from the pb pblock
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 * and creates an LDAPResourcePool object
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 *
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 * LDAPResourcePool contains all settings necessary for creating
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 * ldap connections.
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 */
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 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
120 char *ldap_uri = pblock_findval("Uri", pb);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 char *host = pblock_findval("Host", pb);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 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
123 char *binddn = pblock_findval("Binddn", pb);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
124 char *bindpw = pblock_findval("Bindpw", pb);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
125 char *bind = pblock_findval("Bind", pb);
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
463
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
127 log_ereport(
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
128 LOG_INFORM,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
129 "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
130 rpname,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
131 ldap_uri ? "uri=" : "host=",
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
132 ldap_uri ? ldap_uri : host,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
133 ldap_uri || !port ? "" : ":",
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
134 !port ? "" : port,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
135 binddn ? binddn : "''");
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
136
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
137 if(!ldap_uri && !host) {
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 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
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 if(ldap_uri && host) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 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
143 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 int64_t port_i = 0;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 if(host) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 if(port) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 if(util_strtoint(port, &port_i)) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 if(port_i < 1 || port_i > 65535) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 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
152 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 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
155 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 port_i = LDAP_PORT;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 }
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 LDAPResourcePool *ldap_pool = pool_malloc(pool, sizeof(LDAPResourcePool));
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 if(!ldap_pool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 ldap_pool->name = rpname;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 ldap_pool->pool = pool;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 ldap_pool->ldap_uri = ldap_uri;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 ldap_pool->host = host;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 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
171 ldap_pool->binddn = binddn;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
172 ldap_pool->bindpw = bindpw;
463
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
173 WSBool bind_def = binddn != NULL;
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
174 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
175
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 return ldap_pool;
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_destroy(LDAPResourcePool *pool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 void * ldap_resourcepool_createresource(LDAPResourcePool *respool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 LDAP *ldap = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 if(respool->ldap_uri) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 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
187 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 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
189 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 if(!ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 log_ereport(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 LOG_FAILURE,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 "Resource pool %s: %s: cannot create LDAP session",
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 respool->name,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 respool->ldap_uri ? respool->ldap_uri : respool->host);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
200 if(respool->bind) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
201 struct berval *server_cred;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
202 if(ldap_resource_bind(respool, ldap, &server_cred) != LDAP_SUCCESS) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
203 log_ereport(LOG_FAILURE, "Resource pool %s: bind failed", respool->name);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
204 ws_ldap_close(ldap);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
205 return NULL;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
206 }
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
207 }
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
208
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 LDAPResource *res = pool_malloc(respool->pool, sizeof(LDAPResource));
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 if(!res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 ws_ldap_close(ldap);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 log_ereport(LOG_CATASTROPHE, "ldap_resourcepool_createresource: OOM");
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 return NULL;
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 res->ldap = ldap;
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
216 res->res_pool = respool;
461
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 return res;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 void ldap_resourcepool_freeresource(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 if(res->ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 ws_ldap_close(res->ldap);
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 pool_free(pool->pool, res);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 int ldap_resourcepool_prepare(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 return 0;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 int ldap_resourcepool_finish(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 return 0;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 void * ldap_resourcepool_getresourcedata(LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 return res->ldap;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 }
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
241
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
242
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
243 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
244 if(!respool->binddn) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
245 return -1;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
246 }
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
247
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
248 struct berval cred;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
249 cred.bv_val = respool->bindpw;
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,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
253 respool->binddn,
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 }

mercurial