1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* $OpenBSD: util.c,v 1.7 2015/12/24 17:47:57 mmcc Exp $ */
/*
* Copyright (c) 2009 Martin Hedenfalk <martin@bzero.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include <errno.h>
#include "ldapd.h"
int
bsnprintf(char *str, size_t size, const char *format, ...)
{
int ret;
va_list ap;
va_start(ap, format);
ret = vsnprintf(str, size, format, ap);
va_end(ap);
if (ret == -1 || ret >= (int)size)
return 0;
return 1;
}
/* Normalize a DN in preparation for searches.
* Modifies its argument.
* Currently only made lowercase, and spaces around comma is removed.
* TODO: unescape backslash escapes, handle UTF-8.
*/
void
normalize_dn(char *dn)
{
size_t n;
char *s, *p;
for (s = p = dn; *s != '\0'; s++) {
if (*s == ' ') {
if (p == dn || p[-1] == ',')
continue;
n = strspn(s, " ");
if (s[n] == '\0' || s[n] == ',')
continue;
}
*p++ = tolower((unsigned char)*s);
}
*p = '\0';
}
/* Returns true (1) if key ends with suffix.
*/
int
has_suffix(struct btval *key, const char *suffix)
{
size_t slen;
slen = strlen(suffix);
if (key->size < slen)
return 0;
return (bcmp((char *)key->data + key->size - slen, suffix, slen) == 0);
}
/* Returns true (1) if key begins with prefix.
*/
int
has_prefix(struct btval *key, const char *prefix)
{
size_t pfxlen;
pfxlen = strlen(prefix);
if (pfxlen > key->size)
return 0;
return (memcmp(key->data, prefix, pfxlen) == 0);
}
int
ber2db(struct ber_element *root, struct btval *val, int compression_level)
{
int rc;
ssize_t len;
uLongf destlen;
Bytef *dest;
void *buf;
struct ber ber;
memset(val, 0, sizeof(*val));
memset(&ber, 0, sizeof(ber));
ber.fd = -1;
ber_write_elements(&ber, root);
if ((len = ber_get_writebuf(&ber, &buf)) == -1)
return -1;
if (compression_level > 0) {
val->size = compressBound(len);
val->data = malloc(val->size + sizeof(uint32_t));
if (val->data == NULL) {
log_warn("malloc(%u)", val->size + sizeof(uint32_t));
ber_free(&ber);
return -1;
}
dest = (char *)val->data + sizeof(uint32_t);
destlen = val->size - sizeof(uint32_t);
if ((rc = compress2(dest, &destlen, buf, len,
compression_level)) != Z_OK) {
log_warn("compress returned %d", rc);
free(val->data);
ber_free(&ber);
return -1;
}
log_debug("compressed entry from %u -> %u byte",
len, destlen + sizeof(uint32_t));
*(uint32_t *)val->data = len;
val->size = destlen + sizeof(uint32_t);
val->free_data = 1;
} else {
val->data = buf;
val->size = len;
val->free_data = 1; /* XXX: take over internal br_wbuf */
ber.br_wbuf = NULL;
}
ber_free(&ber);
return 0;
}
struct ber_element *
db2ber(struct btval *val, int compression_level)
{
int rc;
uLongf len;
void *buf;
Bytef *src;
uLong srclen;
struct ber_element *elm;
struct ber ber;
assert(val != NULL);
memset(&ber, 0, sizeof(ber));
ber.fd = -1;
if (compression_level > 0) {
if (val->size < sizeof(uint32_t))
return NULL;
len = *(uint32_t *)val->data;
if ((buf = malloc(len)) == NULL) {
log_warn("malloc(%u)", len);
return NULL;
}
src = (char *)val->data + sizeof(uint32_t);
srclen = val->size - sizeof(uint32_t);
rc = uncompress(buf, &len, src, srclen);
if (rc != Z_OK) {
log_warnx("dbt_to_ber: uncompress returned %d", rc);
free(buf);
return NULL;
}
log_debug("uncompressed entry from %u -> %u byte",
val->size, len);
ber_set_readbuf(&ber, buf, len);
elm = ber_read_elements(&ber, NULL);
free(buf);
return elm;
} else {
ber_set_readbuf(&ber, val->data, val->size);
return ber_read_elements(&ber, NULL);
}
}
int
accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
int reserve)
{
if (getdtablecount() + reserve >= getdtablesize()) {
errno = EMFILE;
return -1;
}
return accept4(sockfd, addr, addrlen, SOCK_NONBLOCK);
}
|