src/server/daemon/ldap_resource.c

Mon, 13 Mar 2023 22:39:51 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 13 Mar 2023 22:39:51 +0100
changeset 470
467ed0f559af
parent 464
0a29110b94ec
child 475
d01ea909d906
permissions
-rw-r--r--

refactor ldap user authentication, use new filter config

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 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 char *ldap_uri = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 asprintf(&ldap_uri, "ldap://%s:%d", hostname, port);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 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
64 free(ldap_uri);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 #endif
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
66
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
67 if(ld) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
68 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
69 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
71 return ld;
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
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 LDAP* ws_ldap_resource_create_uri_connection(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 const char *uri,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 int ldap_version)
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 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 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
80 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 LDAP *ld = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 int init_ret = ldap_initialize(&ld, uri);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 if(init_ret) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 log_ereport(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 LOG_FAILURE,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 "ldap_resource_create_connection failed: uri: %s",
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 uri);
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 return ld;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 #endif
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
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 void ws_ldap_close(LDAP *ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 #ifdef SOLARIS
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 ldap_unbind(ldap);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 #else
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 ldap_unbind_ext_s(ldap, NULL, NULL);
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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 /*
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 * Validates settings from the pb pblock
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 * and creates an LDAPResourcePool object
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 * LDAPResourcePool contains all settings necessary for creating
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 * ldap connections.
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 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
112 char *ldap_uri = pblock_findval("Uri", pb);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 char *host = pblock_findval("Host", pb);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 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
115 char *binddn = pblock_findval("Binddn", pb);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
116 char *bindpw = pblock_findval("Bindpw", pb);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
117 char *bind = pblock_findval("Bind", pb);
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118
463
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
119 log_ereport(
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
120 LOG_INFORM,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
121 "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
122 rpname,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
123 ldap_uri ? "uri=" : "host=",
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
124 ldap_uri ? ldap_uri : host,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
125 ldap_uri || !port ? "" : ":",
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
126 !port ? "" : port,
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
127 binddn ? binddn : "''");
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
128
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
129 if(!ldap_uri && !host) {
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 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
131 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 if(ldap_uri && host) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 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
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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 int64_t port_i = 0;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 if(host) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 if(port) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 if(util_strtoint(port, &port_i)) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 if(port_i < 1 || port_i > 65535) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 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
144 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 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
147 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 port_i = LDAP_PORT;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 LDAPResourcePool *ldap_pool = pool_malloc(pool, sizeof(LDAPResourcePool));
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 if(!ldap_pool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 return NULL;
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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 ldap_pool->name = rpname;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 ldap_pool->pool = pool;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 ldap_pool->ldap_uri = ldap_uri;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 ldap_pool->host = host;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 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
163 ldap_pool->binddn = binddn;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
164 ldap_pool->bindpw = bindpw;
463
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
165 WSBool bind_def = binddn != NULL;
4fd523fff13b add option to automatically bind new ldap sessions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 462
diff changeset
166 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
167
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 return ldap_pool;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 void ldap_resourcepool_destroy(LDAPResourcePool *pool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 // unused
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_createresource(LDAPResourcePool *respool) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 LDAP *ldap = NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 if(respool->ldap_uri) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 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
179 } else {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 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
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 if(!ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 log_ereport(
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 LOG_FAILURE,
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
186 "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
187 respool->name,
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 respool->ldap_uri ? respool->ldap_uri : respool->host);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 return NULL;
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
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
192 if(respool->bind) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
193 struct berval *server_cred;
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
194 int r = ldap_resource_bind(respool, ldap, &server_cred);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
195 if(r != LDAP_SUCCESS) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
196 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
197 ws_ldap_close(ldap);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
198 return NULL;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
199 }
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
200 }
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
201
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 LDAPResource *res = pool_malloc(respool->pool, sizeof(LDAPResource));
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 if(!res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 ws_ldap_close(ldap);
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 log_ereport(LOG_CATASTROPHE, "ldap_resourcepool_createresource: OOM");
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 return NULL;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 res->ldap = ldap;
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
209 res->res_pool = respool;
461
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 return res;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 }
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 void ldap_resourcepool_freeresource(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 if(res->ldap) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 ws_ldap_close(res->ldap);
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 pool_free(pool->pool, 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 int ldap_resourcepool_prepare(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 return 0;
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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 int ldap_resourcepool_finish(LDAPResourcePool *pool, LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 // unused
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 return 0;
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
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 void * ldap_resourcepool_getresourcedata(LDAPResource *res) {
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 return res->ldap;
9b20b8f3582b add ldap resource pool type implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
234
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
235
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
236 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
237 if(!respool->binddn) {
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
238 return -1;
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
239 }
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
240 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
241 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
242
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
243 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
244 struct berval cred;
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
245 cred.bv_val = (char*)bindpw;
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
246 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
247 return ldap_sasl_bind_s(
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
248 ldap,
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
249 binddn,
462
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
250 LDAP_SASL_SIMPLE,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
251 &cred,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
252 NULL,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
253 NULL,
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
254 server_cred);
72848970541a add bind parameters to ldap resource pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 461
diff changeset
255 }
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 463
diff changeset
256

mercurial