src/server/safs/ldap.c

Fri, 29 Nov 2024 18:10:26 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 29 Nov 2024 18:10:26 +0100
changeset 564
f19fd264c69c
parent 465
d22ff46c171c
permissions
-rw-r--r--

fix wrong column name usage by pg prop extensions

464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2023 Olaf Wintermann. All rights reserved.
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0a29110b94ec add ldap-query saf
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
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "ldap.h"
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <time.h>
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <limits.h>
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "../util/util.h"
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 static int get_ldap_scope(const char *str) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 // search scope: base, onelevel, subtree, children
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 if(!strcmp(str, "base")) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 return LDAP_SCOPE_BASE;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 } else if(!strcmp(str, "onelevel")) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 return LDAP_SCOPE_ONELEVEL;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 } else if(!strcmp(str, "subtree")) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 return LDAP_SCOPE_SUBTREE;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 } else if(!strcmp(str, "children")) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 return LDAP_SCOPE_CHILDREN;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 return -1;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
50 int service_ldap_search(pblock *pb, Session *sn, Request *rq) {
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 char *resource_name = pblock_findval("resource", pb);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 char *basedn = pblock_findval("basedn", pb);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 char *binddn = pblock_findval("bindnd", pb);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 char *bindpw = pblock_findval("bindpw", pb);
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
55 char *filter = pblock_findval("filter", pb);
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
56 char *empty_query_error = pblock_findval("empty_filter_error", pb);
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 char *empty_result_error = pblock_findval("empty_result_error", pb);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 char *scope_str = pblock_findval("scope", pb);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 char *timeout_str = pblock_findval("timeout", pb);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 char *sizelimit_str = pblock_findval("sizelimit", pb);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
62 int status_empty_filter = WS_SAFS_LDAP_EMPTY_FILTER_ERROR;
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 int status_empty_result = WS_SAFS_LDAP_EMPTY_RESULT_ERROR;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 if(empty_query_error) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 int64_t status = 0;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 util_strtoint(empty_query_error, &status);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 if(status < 200 || status > 999) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
69 log_ereport(LOG_MISCONFIG, "ldap-search: empty_query_error parameter must be an integer between 200 and 999");
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 }
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
72 status_empty_filter = status;
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 if(empty_result_error) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 int64_t status = 0;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 util_strtoint(empty_result_error, &status);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 if(status < 200 || status > 999) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
78 log_ereport(LOG_MISCONFIG, "ldap-search: empty_result_error parameter must be an integer between 200 and 999");
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 status_empty_result = status;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 // should we sent an empty response in case of an empty query/result
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 // or the standard error message?
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
86 WSBool empty_query_response = status_empty_filter < 300 ? TRUE : FALSE;
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 WSBool empty_result_response = status_empty_result < 300 ? TRUE : FALSE;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 int scope = WS_SAFS_LDAP_DEFAULT_SCOPE;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 if(scope_str) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 scope = get_ldap_scope(scope_str);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 if(scope < 0) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
93 log_ereport(LOG_MISCONFIG, "ldap-search: unknown scope %s", scope_str);
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 int timeout = WS_SAFS_LDAP_DEFAULT_TIMEOUT;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 if(timeout_str) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 int64_t t;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 if(util_strtoint(timeout_str, &t)) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 if(t < 0 || t > WS_SAFS_LDAP_MAX_TIMEOUT) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
102 log_ereport(LOG_MISCONFIG, "ldap-search: timeout out of range");
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 timeout = t;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 } else {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
107 log_ereport(LOG_MISCONFIG, "ldap-search: timeout %s is not a number", timeout_str);
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 int sizelimit = WS_SAFS_LDAP_DEFAULT_SIZELIMIT;
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
111 if(sizelimit_str) {
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 int64_t v;
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
113 if(util_strtoint(sizelimit_str, &v)) {
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 if(v > INT_MAX) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
115 log_ereport(LOG_MISCONFIG, "ldap-search: sizelimit out of range");
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 sizelimit = v;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 } else {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
120 log_ereport(LOG_MISCONFIG, "ldap-search: sizelimit %s is not a number", timeout_str);
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 if(!resource_name) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
126 log_ereport(LOG_MISCONFIG, "ldap-search: missing resource parameter");
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 if(!basedn) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
130 log_ereport(LOG_MISCONFIG, "ldap-search: missing basedn parameter");
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
134 if(!filter) {
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
135 // alternatively get filter from rq->vars
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
136 filter = pblock_findval("ldap_filter", rq->vars);
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
137 log_ereport(LOG_DEBUG, "ldap-search: no filter parameter, rq.vars ldap_filter: %s", filter);
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
138 if(!filter) {
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
139 // no ldap filter
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
140 protocol_status(sn, rq, status_empty_filter, NULL);
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 if(empty_query_response) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 pblock_nvinsert("content-length", "0", rq->srvhdrs);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 http_start_response(sn, rq);
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
144 } else {
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
145 log_ereport(LOG_FAILURE, "ldap-search: no filter specified");
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 return REQ_PROCEED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 // get the resource
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 ResourceData *resdata = resourcepool_lookup(sn, rq, resource_name, 0);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 if(!resdata) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
154 log_ereport(LOG_FAILURE, "ldap-search: cannot get resource %s", resource_name);
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 LDAP *ldap = resdata->data;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 // optionally, use binddn
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 if(binddn) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 struct berval *server_cred;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 if(ws_ldap_bind(ldap, binddn, bindpw ? bindpw : "", &server_cred) != LDAP_SUCCESS) {
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
163 log_ereport(LOG_FAILURE, "ldap-search: resource %s: cannot bind %s", resource_name, binddn);
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 resourcepool_free(sn, rq, resdata);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 // search
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 LDAPMessage *result;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 struct timeval ts;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 ts.tv_sec = timeout;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 ts.tv_usec = 0;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 int r = ldap_search_ext_s(
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 ldap,
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 basedn,
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 LDAP_SCOPE_SUBTREE,
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
179 filter,
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 NULL,
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 0,
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 NULL, // server controls
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 NULL, // client controls
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 &ts,
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 sizelimit, // size limit
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 &result);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 if(r != LDAP_SUCCESS) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 if(result) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 ldap_msgfree(result);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 }
465
d22ff46c171c rename ldap-query to ldap-search, rename query parameter to filter
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
192 log_ereport(LOG_FAILURE, "ldap-search: ldap error: %s", ldap_err2string(r));
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 return REQ_ABORTED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 // send http header
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 protocol_status(sn, rq, 200, NULL);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 LDAPMessage *msg = ldap_first_entry(ldap, result);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 if(!msg) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 protocol_status(sn, rq, status_empty_result, NULL);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 if(empty_result_response) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 pblock_nvinsert("content-length", "0", rq->srvhdrs);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 } else {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 protocol_status(sn, rq, 200, NULL);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 http_start_response(sn, rq);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 while(msg) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 // dn
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 char *dn = ldap_get_dn(ldap, msg);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 if(dn) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 net_printf(sn->csd, "dn: %s\n", dn);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 ldap_memfree(dn);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 // get attributes
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 BerElement *ber = NULL;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 char *attribute = attribute=ldap_first_attribute(ldap, msg, &ber);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 while(attribute != NULL) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 struct berval **values = ldap_get_values_len(ldap, msg, attribute);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 if(values) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 int count = ldap_count_values_len(values);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 for(int i=0;i<count;i++) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 cxstring value = cx_strn(values[i]->bv_val, values[i]->bv_len);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 net_printf(sn->csd, "%s: %.*s\n", attribute, (int)value.length, value.ptr);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 ldap_value_free_len(values);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 ldap_memfree(attribute);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 attribute = ldap_next_attribute(ldap, msg, ber);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 if(ber) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 ber_free(ber, 0);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 net_printf(sn->csd, "\n");
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 msg = ldap_next_entry(ldap, msg);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 }
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 ldap_msgfree(result);
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 return REQ_PROCEED;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 }

mercurial