src/server/daemon/ldap_resource.c

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

mercurial