config.c 104 KB
Newer Older
Ondřej Kuzník's avatar
Ondřej Kuzník committed
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
/* config.c - configuration file handling routines */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1998-2020 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

#include "portable.h"

#include <stdio.h>

#include <ac/string.h>
#include <ac/ctype.h>
#include <ac/signal.h>
#include <ac/socket.h>
#include <ac/errno.h>
#include <ac/unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

#ifndef S_ISREG
#define S_ISREG(m) ( ((m) & _S_IFMT ) == _S_IFREG )
#endif

45
#include "lload.h"
Ondřej Kuzník's avatar
Ondřej Kuzník committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "lutil.h"
#include "lutil_ldap.h"
#include "config.h"

#ifdef _WIN32
#define LUTIL_ATOULX lutil_atoullx
#define Z "I"
#else
#define LUTIL_ATOULX lutil_atoulx
#define Z "z"
#endif

#define ARGS_STEP 512

/*
 * defaults for various global variables
 */
63
64
65
#ifdef BALANCER_MODULE
char *listeners_list = NULL;
#else /* !BALANCER_MODULE */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
66
67
68
69
70
71
slap_mask_t global_allows = 0;
slap_mask_t global_disallows = 0;
int global_gentlehup = 0;
int global_idletimeout = 0;
char *global_host = NULL;

72
73
74
75
char *slapd_pid_file = NULL;
char *slapd_args_file = NULL;
#endif /* !BALANCER_MODULE */

Ondřej Kuzník's avatar
Ondřej Kuzník committed
76
77
78
static FILE *logfile;
static char *logfileName;

79
80
static struct timeval timeout_api_tv, timeout_net_tv,
        timeout_write_tv = { 10, 0 };
81

Ondřej Kuzník's avatar
Ondřej Kuzník committed
82
83
lload_features_t lload_features;

84
85
ber_len_t sockbuf_max_incoming_client = LLOAD_SB_MAX_INCOMING_CLIENT;
ber_len_t sockbuf_max_incoming_upstream = LLOAD_SB_MAX_INCOMING_UPSTREAM;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
86

87
int lload_conn_max_pdus_per_cycle = LLOAD_CONN_MAX_PDUS_PER_CYCLE_DEFAULT;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
88

89
struct timeval *lload_timeout_api = NULL;
90
struct timeval *lload_timeout_net = NULL;
91
struct timeval *lload_write_timeout = &timeout_write_tv;
92

93
94
static slap_verbmasks tlskey[];

Ondřej Kuzník's avatar
Ondřej Kuzník committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
static int fp_getline( FILE *fp, ConfigArgs *c );
static void fp_getline_init( ConfigArgs *c );

static char *strtok_quote(
        char *line,
        char *sep,
        char **quote_ptr,
        int *inquote );

typedef struct ConfigFile {
    struct ConfigFile *c_sibs;
    struct ConfigFile *c_kids;
    struct berval c_file;
    BerVarray c_dseFiles;
} ConfigFile;

static ConfigFile *cfn;

static ConfigDriver config_fname;
static ConfigDriver config_generic;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
115
static ConfigDriver config_backend;
116
static ConfigDriver config_bindconf;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
117
118
119
120
121
122
#ifdef LDAP_TCP_BUFFER
static ConfigDriver config_tcp_buffer;
#endif /* LDAP_TCP_BUFFER */
static ConfigDriver config_restrict;
static ConfigDriver config_loglevel;
static ConfigDriver config_include;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
123
static ConfigDriver config_feature;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
124
125
126
127
#ifdef HAVE_TLS
static ConfigDriver config_tls_option;
static ConfigDriver config_tls_config;
#endif
128
129
130
#ifdef BALANCER_MODULE
static ConfigDriver backend_cf_gen;
#endif /* BALANCER_MODULE */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
131

132
lload_b_head backend = LDAP_CIRCLEQ_HEAD_INITIALIZER(backend);
Ondřej Kuzník's avatar
Ondřej Kuzník committed
133
ldap_pvt_thread_mutex_t backend_mutex;
134
LloadBackend *current_backend = NULL;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
135

136
struct slap_bindconf bindconf = {};
Ondřej Kuzník's avatar
Ondřej Kuzník committed
137
struct berval lloadd_identity = BER_BVNULL;
138

Ondřej Kuzník's avatar
Ondřej Kuzník committed
139
140
141
enum {
    CFG_ACL = 1,
    CFG_BACKEND,
142
    CFG_BINDCONF,
143
    CFG_LISTEN,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    CFG_TLS_RAND,
    CFG_TLS_CIPHER,
    CFG_TLS_PROTOCOL_MIN,
    CFG_TLS_CERT_FILE,
    CFG_TLS_CERT_KEY,
    CFG_TLS_CA_PATH,
    CFG_TLS_CA_FILE,
    CFG_TLS_DH_FILE,
    CFG_TLS_VERIFY,
    CFG_TLS_CRLCHECK,
    CFG_TLS_CRL_FILE,
    CFG_CONCUR,
    CFG_THREADS,
    CFG_LOGFILE,
    CFG_MIRRORMODE,
159
    CFG_IOTHREADS,
160
161
    CFG_MAXBUF_CLIENT,
    CFG_MAXBUF_UPSTREAM,
162
    CFG_FEATURE,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
163
164
165
166
167
    CFG_THREADQS,
    CFG_TLS_ECNAME,
    CFG_TLS_CACERT,
    CFG_TLS_CERT,
    CFG_TLS_KEY,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
168
    CFG_RESCOUNT,
169
    CFG_IOTIMEOUT,
170
171
172
173
174
175
176
    CFG_URI,
    CFG_NUMCONNS,
    CFG_BINDCONNS,
    CFG_RETRY,
    CFG_MAX_PENDING_OPS,
    CFG_MAX_PENDING_CONNS,
    CFG_STARTTLS,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
177
178
179
180
181
182
183
184
185
186
187

    CFG_LAST
};

/* alphabetical ordering */

static ConfigTable config_back_cf_table[] = {
    /* This attr is read-only */
    { "", "", 0, 0, 0,
        ARG_MAGIC,
        &config_fname,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
188
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
189
190
191
192
    },
    { "argsfile", "file", 2, 2, 0,
        ARG_STRING,
        &slapd_args_file,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
193
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
194
195
    },
    { "concurrency", "level", 2, 2, 0,
196
        ARG_UINT|ARG_MAGIC|CFG_CONCUR,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
197
        &config_generic,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
198
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
199
    },
200
    /* conf-file only option */
201
    { "backend-server", "backend options", 2, 0, 0,
202
        ARG_MAGIC|CFG_BACKEND,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
203
        &config_backend,
204
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
205
    },
206
207
208
    { "bindconf", "backend credentials", 2, 0, 0,
        ARG_MAGIC|CFG_BINDCONF,
        &config_bindconf,
209
210
211
        "( OLcfgBkAt:13.2 "
            "NAME 'olcBkLloadBindconf' "
            "DESC 'Backend credentials' "
212
213
            /* No EQUALITY since this is a compound attribute (and needs
             * splitting up anyway - which is a TODO) */
214
215
216
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
217
    },
Ondřej Kuzník's avatar
Ondřej Kuzník committed
218
219
220
221
222
223
224
225
    { "gentlehup", "on|off", 2, 2, 0,
#ifdef SIGHUP
        ARG_ON_OFF,
        &global_gentlehup,
#else
        ARG_IGNORED,
        NULL,
#endif
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
226
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
227
228
    },
    { "idletimeout", "timeout", 2, 2, 0,
229
        ARG_UINT,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
230
        &global_idletimeout,
231
232
233
234
235
236
237
        "( OLcfgBkAt:13.3 "
            "NAME 'olcBkLloadIdleTimeout' "
            "DESC 'Connection idle timeout' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
238
239
240
241
    },
    { "include", "file", 2, 2, 0,
        ARG_MAGIC,
        &config_include,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
242
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
243
    },
244
245
    { "io-threads", "count", 2, 0, 0,
        ARG_UINT|ARG_MAGIC|CFG_IOTHREADS,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
246
        &config_generic,
247
248
        "( OLcfgBkAt:13.4 "
            "NAME 'olcBkLloadIOThreads' "
249
250
            "DESC 'I/O thread count' "
            "EQUALITY integerMatch "
251
252
253
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
254
    },
255
256
#ifdef BALANCER_MODULE
    { "listen", "uri list", 2, 2, 0,
257
258
        ARG_STRING|ARG_MAGIC|CFG_LISTEN,
        &config_generic,
259
260
261
262
263
264
265
266
        "( OLcfgBkAt:13.5 "
            "NAME 'olcBkLloadListen' "
            "DESC 'A list of listener adresses' "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
    },
#endif /* BALANCER_MODULE */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
267
268
269
    { "logfile", "file", 2, 2, 0,
        ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
        &config_generic,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
270
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
271
272
273
274
    },
    { "loglevel", "level", 2, 0, 0,
        ARG_MAGIC,
        &config_loglevel,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
275
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
276
277
278
279
    },
    { "pidfile", "file", 2, 2, 0,
        ARG_STRING,
        &slapd_pid_file,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
280
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
281
282
283
284
    },
    { "restrict", "op_list", 2, 0, 0,
        ARG_MAGIC,
        &config_restrict,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
285
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
286
    },
287
    { "sockbuf_max_incoming_client", "max", 2, 2, 0,
288
289
        ARG_BER_LEN_T|ARG_MAGIC|CFG_MAXBUF_CLIENT,
        &config_generic,
290
291
292
        "( OLcfgBkAt:13.6 "
            "NAME 'olcBkLloadSockbufMaxClient' "
            "DESC 'The maximum LDAP PDU size accepted coming from clients' "
293
            "EQUALITY integerMatch "
294
295
296
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
297
    },
298
    { "sockbuf_max_incoming_upstream", "max", 2, 2, 0,
299
300
        ARG_BER_LEN_T|ARG_MAGIC|CFG_MAXBUF_UPSTREAM,
        &config_generic,
301
302
303
        "( OLcfgBkAt:13.7 "
            "NAME 'olcBkLloadSockbufMaxUpstream' "
            "DESC 'The maximum LDAP PDU size accepted coming from upstream' "
304
            "EQUALITY integerMatch "
305
306
307
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
308
309
310
311
312
313
314
315
316
    },
    { "tcp-buffer", "[listener=<listener>] [{read|write}=]size", 0, 0, 0,
#ifdef LDAP_TCP_BUFFER
        ARG_MAGIC,
        &config_tcp_buffer,
#else
        ARG_IGNORED,
        NULL,
#endif
317
318
319
320
321
322
323
        "( OLcfgBkAt:13.8 "
            "NAME 'olcBkLloadTcpBuffer' "
            "DESC 'TCP Buffer size' "
            "EQUALITY caseIgnoreMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
324
325
    },
    { "threads", "count", 2, 2, 0,
326
        ARG_UINT|ARG_MAGIC|CFG_THREADS,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
327
        &config_generic,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
328
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
329
330
    },
    { "threadqueues", "count", 2, 2, 0,
331
        ARG_UINT|ARG_MAGIC|CFG_THREADQS,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
332
        &config_generic,
Nadezhda Ivanova's avatar
Nadezhda Ivanova committed
333
        NULL, NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
334
    },
Ondřej Kuzník's avatar
Ondřej Kuzník committed
335
    { "max_pdus_per_cycle", "count", 2, 2, 0,
336
        ARG_UINT|ARG_MAGIC|CFG_RESCOUNT,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
337
        &config_generic,
338
339
340
341
342
343
344
        "( OLcfgBkAt:13.9 "
            "NAME 'olcBkLloadMaxPDUPerCycle' "
            "DESC 'Maximum number of PDUs to handle in a single cycle' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
345
    },
Ondřej Kuzník's avatar
Ondřej Kuzník committed
346
    { "feature", "name", 2, 0, 0,
347
        ARG_MAGIC|CFG_FEATURE,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
348
        &config_feature,
349
350
351
352
353
354
        "( OLcfgBkAt:13.10 "
            "NAME 'olcBkLloadFeature' "
            "DESC 'Lload features enabled' "
            "EQUALITY caseIgnoreMatch "
            "SYNTAX OMsDirectoryString )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
355
    },
Ondřej Kuzník's avatar
Ondřej Kuzník committed
356
357
358
359
360
361
362
363
    { "TLSCACertificate", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_CACERT|ARG_BINARY|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
364
365
366
367
368
369
370
        "( OLcfgBkAt:13.11 "
            "NAME 'olcBkLloadTLSCACertificate' "
            "DESC 'X.509 certificate, must use ;binary' "
            "EQUALITY certificateExactMatch "
            "SYNTAX 1.3.6.1.4.1.1466.115.121.1.8 "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
371
372
373
374
375
376
377
378
379
    },
    { "TLSCACertificateFile", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
380
381
382
383
384
385
        "( OLcfgBkAt:13.12 "
            "NAME 'olcBkLloadTLSCACertificateFile' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
386
387
388
389
390
391
392
393
394
    },
    { "TLSCACertificatePath", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
395
396
397
398
399
400
        "( OLcfgBkAt:13.13 "
            "NAME 'olcBkLloadTLSCACertificatePath' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
401
402
403
404
405
406
407
408
409
    },
    { "TLSCertificate", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_CERT|ARG_BINARY|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
410
411
412
413
414
415
416
        "( OLcfgBkAt:13.14 "
            "NAME 'olcBkLloadTLSCertificate' "
            "DESC 'X.509 certificate, must use ;binary' "
            "EQUALITY certificateExactMatch "
            "SYNTAX 1.3.6.1.4.1.1466.115.121.1.8 "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
417
418
419
420
421
422
423
424
425
    },
    { "TLSCertificateFile", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
426
427
428
429
430
431
        "( OLcfgBkAt:13.15 "
            "NAME 'olcBkLloadTLSCertificateFile' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
432
433
434
435
436
437
438
439
440
    },
    { "TLSCertificateKey", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_KEY|ARG_BINARY|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
441
442
443
444
445
446
447
        "( OLcfgBkAt:13.16 "
            "NAME 'olcBkLloadTLSCertificateKey' "
            "DESC 'X.509 privateKey, must use ;binary' "
            "EQUALITY privateKeyMatch "
            "SYNTAX 1.2.840.113549.1.8.1.1 "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
448
449
450
451
452
453
454
455
456
    },
    { "TLSCertificateKeyFile", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
457
458
459
460
461
462
        "( OLcfgBkAt:13.17 "
            "NAME 'olcBkLloadTLSCertificateKeyFile' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
463
464
465
466
467
468
469
470
471
    },
    { "TLSCipherSuite", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
472
473
474
475
476
477
        "( OLcfgBkAt:13.18 "
            "NAME 'olcBkLloadTLSCipherSuite' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
478
479
480
481
482
483
484
485
486
    },
    { "TLSCRLCheck", NULL, 2, 2, 0,
#if defined(HAVE_TLS) && defined(HAVE_OPENSSL)
        CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC,
        &config_tls_config,
#else
        ARG_IGNORED,
        NULL,
#endif
487
488
489
490
491
492
        "( OLcfgBkAt:13.19 "
            "NAME 'olcBkLloadTLSCRLCheck' "
            "EQUALITY caseIgnoreMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
493
494
495
496
497
498
499
500
501
    },
    { "TLSCRLFile", NULL, 2, 2, 0,
#if defined(HAVE_GNUTLS)
        CFG_TLS_CRL_FILE|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
502
503
504
505
506
507
        "( OLcfgBkAt:13.20 "
            "NAME 'olcBkLloadTLSCRLFile' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
508
509
510
511
512
513
514
515
516
    },
    { "TLSRandFile", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_RAND|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
517
518
519
520
521
522
        "( OLcfgBkAt:13.21 "
            "NAME 'olcBkLloadTLSRandFile' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
523
524
525
526
527
528
529
530
531
    },
    { "TLSVerifyClient", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC,
        &config_tls_config,
#else
        ARG_IGNORED,
        NULL,
#endif
532
533
534
535
536
537
        "( OLcfgBkAt:13.22 "
            "NAME 'olcBkLloadVerifyClient' "
            "EQUALITY caseIgnoreMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
538
539
540
541
542
543
544
545
546
    },
    { "TLSDHParamFile", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_DH_FILE|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
547
548
549
550
551
552
        "( OLcfgBkAt:13.23 "
            "NAME 'olcBkLloadTLSDHParamFile' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
553
554
555
556
557
558
559
560
561
    },
    { "TLSECName", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_ECNAME|ARG_STRING|ARG_MAGIC,
        &config_tls_option,
#else
        ARG_IGNORED,
        NULL,
#endif
562
563
564
565
566
567
        "( OLcfgBkAt:13.24 "
            "NAME 'olcBkLloadTLSECName' "
            "EQUALITY caseExactMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
568
569
570
571
572
573
574
575
576
    },
    { "TLSProtocolMin", NULL, 2, 2, 0,
#ifdef HAVE_TLS
        CFG_TLS_PROTOCOL_MIN|ARG_STRING|ARG_MAGIC,
        &config_tls_config,
#else
        ARG_IGNORED,
        NULL,
#endif
577
578
579
580
581
582
        "( OLcfgBkAt:13.25 "
            "NAME 'olcBkLloadTLSProtocolMin' "
            "EQUALITY caseIgnoreMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
Ondřej Kuzník's avatar
Ondřej Kuzník committed
583
    },
584
    { "iotimeout", "ms timeout", 2, 2, 0,
585
        ARG_UINT|ARG_MAGIC|CFG_IOTIMEOUT,
586
        &config_generic,
587
588
589
590
591
592
593
        "( OLcfgBkAt:13.26 "
            "NAME 'olcBkLloadIOTimeout' "
            "DESC 'I/O timeout threshold in miliseconds' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
594
    },
Ondřej Kuzník's avatar
Ondřej Kuzník committed
595

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    /* cn=config only options */
#ifdef BALANCER_MODULE
    { "", "uri", 2, 2, 0,
        ARG_BERVAL|ARG_MAGIC|CFG_URI,
        &backend_cf_gen,
        "( OLcfgBkAt:13.27 "
            "NAME 'olcBkLloadBackendUri' "
            "DESC 'URI to contact the server on' "
            "EQUALITY caseIgnoreMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
    },
    { "", NULL, 2, 2, 0,
        ARG_UINT|ARG_MAGIC|CFG_NUMCONNS,
        &backend_cf_gen,
        "( OLcfgBkAt:13.28 "
            "NAME 'olcBkLloadNumconns' "
            "DESC 'Number of regular connections to maintain' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
    },
    { "", NULL, 2, 2, 0,
        ARG_UINT|ARG_MAGIC|CFG_BINDCONNS,
        &backend_cf_gen,
        "( OLcfgBkAt:13.29 "
            "NAME 'olcBkLloadBindconns' "
            "DESC 'Number of bind connections to maintain' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
    },
    { "", NULL, 2, 2, 0,
        ARG_UINT|ARG_MAGIC|CFG_RETRY,
        &backend_cf_gen,
        "( OLcfgBkAt:13.30 "
            "NAME 'olcBkLloadRetry' "
            "DESC 'Number of seconds to wait before trying to reconnect' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
    },
    { "", NULL, 2, 2, 0,
        ARG_UINT|ARG_MAGIC|CFG_MAX_PENDING_OPS,
        &backend_cf_gen,
        "( OLcfgBkAt:13.31 "
            "NAME 'olcBkLloadMaxPendingOps' "
            "DESC 'Maximum number of pending operations for this backend' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
    },
    { "", NULL, 2, 2, 0,
        ARG_UINT|ARG_MAGIC|CFG_MAX_PENDING_CONNS,
        &backend_cf_gen,
        "( OLcfgBkAt:13.32 "
            "NAME 'olcBkLloadMaxPendingConns' "
            "DESC 'Maximum number of pending operations on each connection' "
            "EQUALITY integerMatch "
            "SYNTAX OMsInteger "
            "SINGLE-VALUE )",
        NULL, NULL
    },
    { "", NULL, 2, 2, 0,
        ARG_BERVAL|ARG_MAGIC|CFG_STARTTLS,
        &backend_cf_gen,
        "( OLcfgBkAt:13.34 "
            "NAME 'olcBkLloadStartTLS' "
            "DESC 'Whether StartTLS should be attempted on the connection' "
            "EQUALITY caseIgnoreMatch "
            "SYNTAX OMsDirectoryString "
            "SINGLE-VALUE )",
        NULL, NULL
    },
#endif /* BALANCER_MODULE */

Ondřej Kuzník's avatar
Ondřej Kuzník committed
677
678
679
    { NULL, NULL, 0, 0, 0, ARG_IGNORED, NULL }
};

680
#ifdef BALANCER_MODULE
681
682
683
684
685
686
static ConfigCfAdd lload_cfadd;
static ConfigLDAPadd lload_backend_ldadd;
#ifdef SLAP_CONFIG_DELETE
static ConfigLDAPdel lload_backend_lddel;
#endif /* SLAP_CONFIG_DELETE */

687
688
static ConfigOCs lloadocs[] = {
    { "( OLcfgBkOc:13.1 "
689
        "NAME 'olcBkLloadConfig' "
690
691
        "DESC 'Lload backend configuration' "
        "SUP olcBackendConfig "
692
        "MUST ( olcBkLloadBindconf "
693
694
695
696
            "$ olcBkLloadIOThreads "
            "$ olcBkLloadListen "
            "$ olcBkLloadSockbufMaxClient "
            "$ olcBkLloadSockbufMaxUpstream "
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
            "$ olcBkLloadMaxPDUPerCycle "
            "$ olcBkLloadIOTimeout ) "
        "MAY ( olcBkLloadFeature "
            "$ olcBkLloadTcpBuffer "
            "$ olcBkLloadTLSCACertificateFile "
            "$ olcBkLloadTLSCACertificatePath "
            "$ olcBkLloadTLSCertificateFile "
            "$ olcBkLloadTLSCertificateKeyFile "
            "$ olcBkLloadTLSCipherSuite "
            "$ olcBkLloadTLSCRLCheck "
            "$ olcBkLloadTLSRandFile "
            "$ olcBkLloadVerifyClient "
            "$ olcBkLloadTLSDHParamFile "
            "$ olcBkLloadTLSECName "
            "$ olcBkLloadTLSProtocolMin "
            "$ olcBkLloadTLSCRLFile "
713
714
        ") )",
        Cft_Backend, config_back_cf_table,
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
        NULL,
        lload_cfadd,
    },
    { "( OLcfgBkOc:13.2 "
        "NAME 'olcBkLloadBackendConfig' "
        "DESC 'Lload backend server configuration' "
        "SUP olcConfig STRUCTURAL "
        "MUST ( cn "
            "$ olcBkLloadBackendUri "
            "$ olcBkLloadNumconns "
            "$ olcBkLloadBindconns "
            "$ olcBkLloadRetry "
            "$ olcBkLloadMaxPendingOps "
            "$ olcBkLloadMaxPendingConns ) "
        "MAY ( olcBkLloadStartTLS "
        ") )",
        Cft_Misc, config_back_cf_table,
        lload_backend_ldadd,
        NULL,
#ifdef SLAP_CONFIG_DELETE
        lload_backend_lddel,
#endif /* SLAP_CONFIG_DELETE */
    },
738
739
740
741
    { NULL, 0, NULL }
};
#endif /* BALANCER_MODULE */

Ondřej Kuzník's avatar
Ondřej Kuzník committed
742
743
744
static int
config_generic( ConfigArgs *c )
{
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
    enum lcf_daemon flag = 0;
    int rc = LDAP_SUCCESS;

    lload_change.type = c->op;
    lload_change.object = LLOAD_DAEMON;

    if ( c->op == SLAP_CONFIG_EMIT ) {
        switch ( c->type ) {
            case CFG_IOTHREADS:
                c->value_uint = lload_daemon_threads;
                break;
#ifdef BALANCER_MODULE
            case CFG_LISTEN:
                /* TODO fix this, make it multivalued */
                rc = snprintf( c->cr_msg, sizeof(c->cr_msg), "\"%s\"",
                             listeners_list ) >= sizeof(c->cr_msg);
                c->value_string = ch_strdup( c->cr_msg );
                break;
#endif /* BALANCER_MODULE */
            case CFG_MAXBUF_CLIENT:
                c->value_uint = sockbuf_max_incoming_client;
                break;
            case CFG_MAXBUF_UPSTREAM:
                c->value_uint = sockbuf_max_incoming_upstream;
                break;
            case CFG_RESCOUNT:
                c->value_uint = lload_conn_max_pdus_per_cycle;
                break;
            case CFG_IOTIMEOUT:
                c->value_uint = 1000 * lload_write_timeout->tv_sec +
                        lload_write_timeout->tv_usec / 1000;
                break;
            default:
                rc = 1;
                break;
        }
        return rc;

    } else if ( c->op == LDAP_MOD_DELETE ) {
        /* We only need to worry about deletions to multi-value or MAY
         * attributes that belong to the lloadd module - we don't have any at
         * the moment */
        return rc;
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
790
791
    switch ( c->type ) {
        case CFG_CONCUR:
792
            ldap_pvt_thread_set_concurrency( c->value_uint );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
793
            break;
794
795
796
797
798
799
800
801
#ifdef BALANCER_MODULE
        case CFG_LISTEN:
            /* Todo this is not good - we need validity checks,
             * check if already allocated, if it's being modified stop old
             * listeners, etc */
            listeners_list = c->value_string;
            break;
#endif /* BALANCER_MODULE */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
802
        case CFG_THREADS:
803
            if ( c->value_uint < 2 ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
804
805
                snprintf( c->cr_msg, sizeof(c->cr_msg),
                        "threads=%d smaller than minimum value 2",
806
                        c->value_uint );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
807
                Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
808
809
                return 1;

810
            } else if ( c->value_uint > 2 * SLAP_MAX_WORKER_THREADS ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
811
812
813
                snprintf( c->cr_msg, sizeof(c->cr_msg),
                        "warning, threads=%d larger than twice the default "
                        "(2*%d=%d); YMMV",
814
                        c->value_uint, SLAP_MAX_WORKER_THREADS,
Ondřej Kuzník's avatar
Ondřej Kuzník committed
815
                        2 * SLAP_MAX_WORKER_THREADS );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
816
                Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
817
818
819
            }
            if ( slapMode & SLAP_SERVER_MODE )
                ldap_pvt_thread_pool_maxthreads(
820
821
                        &connection_pool, c->value_uint );
            connection_pool_max = c->value_uint; /* save for reference */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
822
823
824
            break;

        case CFG_THREADQS:
825
            if ( c->value_uint < 1 ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
826
827
                snprintf( c->cr_msg, sizeof(c->cr_msg),
                        "threadqueues=%d smaller than minimum value 1",
828
                        c->value_uint );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
829
                Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
830
831
832
                return 1;
            }
            if ( slapMode & SLAP_SERVER_MODE )
833
834
                ldap_pvt_thread_pool_queues( &connection_pool, c->value_uint );
            connection_pool_queues = c->value_uint; /* save for reference */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
835
836
            break;

837
        case CFG_IOTHREADS: {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
838
839
840
841
842
843
844
            int mask = 0;
            /* use a power of two */
            while ( c->value_uint > 1 ) {
                c->value_uint >>= 1;
                mask <<= 1;
                mask |= 1;
            }
845
846
847
848
849
850
851
852
853
854
            if ( !lloadd_inited ) {
                lload_daemon_mask = mask;
                lload_daemon_threads = mask + 1;
                flag = LLOAD_DAEMON_MOD_THREADS;
            } else {
                snprintf( c->cr_msg, sizeof(c->cr_msg),
                        "io thread changes will not take effect until "
                        "restart" );
                Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg );
            }
Ondřej Kuzník's avatar
Ondřej Kuzník committed
855
856
857
858
859
860
861
862
863
        } break;

        case CFG_LOGFILE: {
            if ( logfileName ) ch_free( logfileName );
            logfileName = c->value_string;
            logfile = fopen( logfileName, "w" );
            if ( logfile ) lutil_debug_file( logfile );
        } break;

Ondřej Kuzník's avatar
Ondřej Kuzník committed
864
        case CFG_RESCOUNT:
865
            lload_conn_max_pdus_per_cycle = c->value_uint;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
866
867
            break;

868
        case CFG_IOTIMEOUT:
869
870
871
            if ( c->value_uint > 0 ) {
                timeout_write_tv.tv_sec = c->value_uint / 1000;
                timeout_write_tv.tv_usec = 1000 * ( c->value_uint % 1000 );
872
873
874
875
876
                lload_write_timeout = &timeout_write_tv;
            } else {
                lload_write_timeout = NULL;
            }
            break;
877
878
879
880
881
882
        case CFG_MAXBUF_CLIENT:
            sockbuf_max_incoming_client = c->value_uint;
            break;
        case CFG_MAXBUF_UPSTREAM:
            sockbuf_max_incoming_upstream = c->value_uint;
            break;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
883
        default:
Ondřej Kuzník's avatar
Ondřej Kuzník committed
884
            Debug( LDAP_DEBUG_ANY, "%s: unknown CFG_TYPE %d\n",
Ondřej Kuzník's avatar
Ondřej Kuzník committed
885
886
887
                    c->log, c->type );
            return 1;
    }
888
889
890

    lload_change.flags.daemon |= flag;

Ondřej Kuzník's avatar
Ondřej Kuzník committed
891
892
893
    return 0;
}

Ondřej Kuzník's avatar
Ondřej Kuzník committed
894
static int
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
lload_backend_finish( ConfigArgs *ca )
{
    LloadBackend *b = ca->ca_private;

    if ( ca->reply.err != LDAP_SUCCESS ) {
        /* Not reached since cleanup is only called on success */
        goto fail;
    }

    if ( b->b_numconns <= 0 || b->b_numbindconns <= 0 ) {
        Debug( LDAP_DEBUG_ANY, "lload_backend_finish: "
                "invalid connection pool configuration\n" );
        goto fail;
    }

    if ( b->b_retry_timeout < 0 ) {
        Debug( LDAP_DEBUG_ANY, "lload_backend_finish: "
                "invalid retry timeout configuration\n" );
        goto fail;
    }

    b->b_retry_tv.tv_sec = b->b_retry_timeout / 1000;
    b->b_retry_tv.tv_usec = ( b->b_retry_timeout % 1000 ) * 1000;

    /* daemon_base is only allocated after initial configuration happens, those
     * events are allocated on startup, we only deal with online Adds */
    if ( !b->b_retry_event && daemon_base ) {
        struct event *event;
        assert( CONFIG_ONLINE_ADD( ca ) );
        event = evtimer_new( daemon_base, backend_connect, b );
        if ( !event ) {
            Debug( LDAP_DEBUG_ANY, "lload_backend_finish: "
                    "failed to allocate retry event\n" );
            lload_backend_destroy( b );
            return -1;
        }
        b->b_retry_event = event;
    }

    return LDAP_SUCCESS;

fail:
    LDAP_CIRCLEQ_REMOVE( &backend, b, b_next );
    lload_backend_destroy( b );
    return -1;
}

static LloadBackend *
backend_alloc( void )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
944
{
945
    LloadBackend *b;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
946

947
    b = ch_calloc( 1, sizeof(LloadBackend) );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
948

949
950
    LDAP_CIRCLEQ_INIT( &b->b_conns );
    LDAP_CIRCLEQ_INIT( &b->b_bindconns );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
951
    LDAP_CIRCLEQ_INIT( &b->b_preparing );
952

Ondřej Kuzník's avatar
Ondřej Kuzník committed
953
954
955
    b->b_numconns = 1;
    b->b_numbindconns = 1;

Ondřej Kuzník's avatar
Ondřej Kuzník committed
956
957
    b->b_retry_timeout = 5000;

958
    ldap_pvt_thread_mutex_init( &b->b_mutex );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
959

960
961
962
    LDAP_CIRCLEQ_INSERT_TAIL( &backend, b, b_next );
    return b;
}
Ondřej Kuzník's avatar
Ondřej Kuzník committed
963

964
965
966
967
968
969
static int
backend_config_url( LloadBackend *b, struct berval *uri )
{
    LDAPURLDesc *lud = NULL;
    char *host = NULL;
    int rc, port, proto, tls;
970

971
    /* Effect no changes until we've checked everything */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
972

973
    rc = ldap_url_parse( uri->bv_val, &lud );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
974
    if ( rc != LDAP_URL_SUCCESS ) {
975
        Debug( LDAP_DEBUG_ANY, "backend_config_url: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
976
                "listen URL \"%s\" parse error=%d\n",
977
                uri->bv_val, rc );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
978
979
980
981
        rc = -1;
        goto done;
    }

982
983
984
985
986
987
988
989
    tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
    if ( tls ) {
#ifdef HAVE_TLS
        /* Specifying ldaps:// overrides starttls= settings */
        tls = LLOAD_LDAPS;
#else /* ! HAVE_TLS */

        Debug( LDAP_DEBUG_ANY, "backend_config_url: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
990
                "TLS not supported (%s)\n",
991
                uri->bv_val );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
992
993
        rc = -1;
        goto done;
994
#endif /* ! HAVE_TLS */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
995
996
997
    }

    if ( !lud->lud_port ) {
998
        port = tls ? LDAPS_PORT : LDAP_PORT;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
999
    } else {
1000
        port = lud->lud_port;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1001
1002
    }

1003
1004
    proto = ldap_pvt_url_scheme2proto( lud->lud_scheme );
    if ( proto == LDAP_PROTO_IPC ) {
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1005
1006
#ifdef LDAP_PF_LOCAL
        if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1007
            host = LDAPI_SOCK;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1008
1009
1010
        }
#else /* ! LDAP_PF_LOCAL */

1011
        Debug( LDAP_DEBUG_ANY, "backend_config_url: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1012
1013
1014
1015
1016
1017
1018
                "URL scheme not supported: %s",
                url );
        rc = -1;
        goto done;
#endif /* ! LDAP_PF_LOCAL */
    } else {
        if ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) {
1019
            Debug( LDAP_DEBUG_ANY, "backend_config_url: "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1020
                    "backend url missing hostname: '%s'\n",
1021
                    uri->bv_val );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1022
1023
1024
1025
            rc = -1;
            goto done;
        }
    }
1026
1027
    if ( !host ) {
        host = lud->lud_host;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1028
1029
    }

1030
1031
1032
1033
1034
1035
1036
1037
    if ( b->b_host ) {
        ch_free( b->b_host );
    }

    b->b_proto = proto;
    b->b_tls = tls;
    b->b_port = port;
    b->b_host = ch_strdup( host );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1038
1039
1040

done:
    ldap_free_urldesc( lud );
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
    return rc;
}

static int
config_backend( ConfigArgs *c )
{
    LloadBackend *b;
    int i, rc = 0;

    b = backend_alloc();

    for ( i = 1; i < c->argc; i++ ) {
        if ( lload_backend_parse( c->argv[i], b ) ) {
            Debug( LDAP_DEBUG_ANY, "config_backend: "
                    "error parsing backend configuration item '%s'\n",
                    c->argv[i] );
            return -1;
        }
    }

    if ( BER_BVISNULL( &b->b_uri ) ) {
        Debug( LDAP_DEBUG_ANY, "config_backend: "
                "backend address not specified\n" );
        rc = -1;
        goto done;
    }

    if ( backend_config_url( b, &b->b_uri ) ) {
        rc = -1;
        goto done;
    }

    c->ca_private = b;
    rc = lload_backend_finish( c );
done:
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1076
1077
1078
1079
1080
1081
    if ( rc ) {
        ch_free( b );
    }
    return rc;
}

1082
1083
1084
1085
1086
static int
config_bindconf( ConfigArgs *c )
{
    int i;

1087
1088
1089
1090
1091
1092
1093
1094
    lload_change.type = c->op;
    lload_change.object = LLOAD_DAEMON;

    if ( c->op == SLAP_CONFIG_EMIT ) {
        struct berval bv;

        lload_bindconf_unparse( &bindconf, &bv );

Ondřej Kuzník's avatar
Ondřej Kuzník committed
1095
        for ( i = 0; isspace( (unsigned char)bv.bv_val[i] ); i++ )
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
            /* count spaces */;

        if ( i ) {
            bv.bv_len -= i;
            AC_MEMCPY( bv.bv_val, &bv.bv_val[i], bv.bv_len + 1 );
        }

        value_add_one( &c->rvalue_vals, &bv );
        ber_memfree( bv.bv_val );
        return LDAP_SUCCESS;
    } else if ( c->op == LDAP_MOD_DELETE ) {
        /* It's a MUST single-valued attribute, noop for now */
        lload_bindconf_free( &bindconf );
        return LDAP_SUCCESS;
    }

1112
    for ( i = 1; i < c->argc; i++ ) {
1113
        if ( lload_bindconf_parse( c->argv[i], &bindconf ) ) {
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
            Debug( LDAP_DEBUG_ANY, "config_bindconf: "
                    "error parsing backend configuration item '%s'\n",
                    c->argv[i] );
            return -1;
        }
    }

    if ( bindconf.sb_method == LDAP_AUTH_SASL ) {
#ifndef HAVE_CYRUS_SASL
        Debug( LDAP_DEBUG_ANY, "config_bindconf: "
                "no sasl support available\n" );
        return -1;
#else /* HAVE_CYRUS_SASL */
        Debug( LDAP_DEBUG_ANY, "config_bindconf: "
                "no sasl support yet\n" );
        return -1;
#endif
    }

Ondřej Kuzník's avatar
Ondřej Kuzník committed
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
    if ( !BER_BVISNULL( &bindconf.sb_authzId ) ) {
        ber_dupbv( &lloadd_identity, &bindconf.sb_authzId );
    } else if ( !BER_BVISNULL( &bindconf.sb_authcId ) ) {
        ber_dupbv( &lloadd_identity, &bindconf.sb_authcId );
    } else if ( !BER_BVISNULL( &bindconf.sb_binddn ) ) {
        char *ptr;

        lloadd_identity.bv_len = STRLENOF("dn:") + bindconf.sb_binddn.bv_len;
        lloadd_identity.bv_val = ch_malloc( lloadd_identity.bv_len + 1 );

        ptr = lutil_strcopy( lloadd_identity.bv_val, "dn:" );
        ptr = lutil_strncopy(
                ptr, bindconf.sb_binddn.bv_val, bindconf.sb_binddn.bv_len );
        *ptr = '\0';
    }
1148

1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
    if ( bindconf.sb_timeout_api ) {
        timeout_api_tv.tv_sec = bindconf.sb_timeout_api;
        lload_timeout_api = &timeout_api_tv;
        if ( lload_timeout_event ) {
            event_add( lload_timeout_event, lload_timeout_api );
        }
    } else {
        lload_timeout_api = NULL;
        if ( lload_timeout_event ) {
            event_del( lload_timeout_event );
        }
    }

1162
1163
1164
1165
1166
1167
1168
    if ( bindconf.sb_timeout_net ) {
        timeout_net_tv.tv_sec = bindconf.sb_timeout_net;
        lload_timeout_net = &timeout_net_tv;
    } else {
        lload_timeout_net = NULL;
    }

1169
1170
#ifdef HAVE_TLS
    if ( bindconf.sb_tls_do_init ) {
1171
        lload_bindconf_tls_set( &bindconf, lload_tls_backend_ld );
1172
1173
    }
#endif /* HAVE_TLS */
1174
1175
1176
    return 0;
}

Ondřej Kuzník's avatar
Ondřej Kuzník committed
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
static int
config_fname( ConfigArgs *c )
{
    return 0;
}

/*
 * [listener=<listener>] [{read|write}=]<size>
 */

#ifdef LDAP_TCP_BUFFER
static BerVarray tcp_buffer;
int tcp_buffer_num;

#define SLAP_TCP_RMEM ( 0x1U )
#define SLAP_TCP_WMEM ( 0x2U )

static int
tcp_buffer_parse(
        struct berval *val,
        int argc,
        char **argv,
        int *size,
        int *rw,
1201
        LloadListener **l )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
{
    int i, rc = LDAP_SUCCESS;
    LDAPURLDesc *lud = NULL;
    char *ptr;

    if ( val != NULL && argv == NULL ) {
        char *s = val->bv_val;

        argv = ldap_str2charray( s, " \t" );
        if ( argv == NULL ) {
            return LDAP_OTHER;
        }
    }

    i = 0;
    if ( strncasecmp( argv[i], "listener=", STRLENOF("listener=") ) == 0 ) {
        char *url = argv[i] + STRLENOF("listener=");

        if ( ldap_url_parse( url, &lud ) ) {
            rc = LDAP_INVALID_SYNTAX;
            goto done;
        }

1225
        *l = lload_config_check_my_url( url, lud );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
        if ( *l == NULL ) {
            rc = LDAP_NO_SUCH_ATTRIBUTE;
            goto done;
        }

        i++;
    }

    ptr = argv[i];
    if ( strncasecmp( ptr, "read=", STRLENOF("read=") ) == 0 ) {
        *rw |= SLAP_TCP_RMEM;
        ptr += STRLENOF("read=");

    } else if ( strncasecmp( ptr, "write=", STRLENOF("write=") ) == 0 ) {
        *rw |= SLAP_TCP_WMEM;
        ptr += STRLENOF("write=");

    } else {
        *rw |= ( SLAP_TCP_RMEM | SLAP_TCP_WMEM );
    }

    /* accept any base */
    if ( lutil_atoix( size, ptr, 0 ) ) {
        rc = LDAP_INVALID_SYNTAX;
        goto done;
    }

done:;
    if ( val != NULL && argv != NULL ) {
        ldap_charray_free( argv );
    }

    if ( lud != NULL ) {
        ldap_free_urldesc( lud );
    }

    return rc;
}

1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
#ifdef BALANCER_MODULE
static int
tcp_buffer_delete_one( struct berval *val )
{
    int rc = 0;
    int size = -1, rw = 0;
    LloadListener *l = NULL;

    rc = tcp_buffer_parse( val, 0, NULL, &size, &rw, &l );
    if ( rc != 0 ) {
        return rc;
    }

    if ( l != NULL ) {
        int i;
        LloadListener **ll = lloadd_get_listeners();

        for ( i = 0; ll[i] != NULL; i++ ) {
            if ( ll[i] == l ) break;
        }

        if ( ll[i] == NULL ) {
            return LDAP_NO_SUCH_ATTRIBUTE;
        }

        if ( rw & SLAP_TCP_RMEM ) l->sl_tcp_rmem = -1;
        if ( rw & SLAP_TCP_WMEM ) l->sl_tcp_wmem = -1;

        for ( i++; ll[i] != NULL && bvmatch( &l->sl_url, &ll[i]->sl_url );
                i++ ) {
            if ( rw & SLAP_TCP_RMEM ) ll[i]->sl_tcp_rmem = -1;
            if ( rw & SLAP_TCP_WMEM ) ll[i]->sl_tcp_wmem = -1;
        }

    } else {
        /* NOTE: this affects listeners without a specific setting,
         * does not reset all listeners.  If a listener without
         * specific settings was assigned a buffer because of
         * a global setting, it will not be reset.  In any case,
         * buffer changes will only take place at restart. */
        if ( rw & SLAP_TCP_RMEM ) slapd_tcp_rmem = -1;
        if ( rw & SLAP_TCP_WMEM ) slapd_tcp_wmem = -1;
    }

    return rc;
}

static int
tcp_buffer_delete( BerVarray vals )
{
    int i;

    for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
        tcp_buffer_delete_one( &vals[i] );
    }

    return 0;
}
#endif /* BALANCER_MODULE */

Ondřej Kuzník's avatar
Ondřej Kuzník committed
1325
static int
1326
tcp_buffer_unparse( int size, int rw, LloadListener *l, struct berval *val )
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
{
    char buf[sizeof("2147483648")], *ptr;

    /* unparse for later use */
    val->bv_len = snprintf( buf, sizeof(buf), "%d", size );
    if ( l != NULL ) {
        val->bv_len += STRLENOF( "listener="
                                 " " ) +
                l->sl_url.bv_len;
    }

    if ( rw != ( SLAP_TCP_RMEM | SLAP_TCP_WMEM ) ) {
        if ( rw & SLAP_TCP_RMEM ) {
            val->bv_len += STRLENOF("read=");
        } else if ( rw & SLAP_TCP_WMEM ) {
            val->bv_len += STRLENOF("write=");
        }
    }

    val->bv_val = SLAP_MALLOC( val->bv_len + 1 );

    ptr = val->bv_val;

    if ( l != NULL ) {
        ptr = lutil_strcopy( ptr, "listener=" );
        ptr = lutil_strncopy( ptr, l->sl_url.bv_val, l->sl_url.bv_len );
        *ptr++ = ' ';
    }

    if ( rw != ( SLAP_TCP_RMEM | SLAP_TCP_WMEM ) ) {
        if ( rw & SLAP_TCP_RMEM ) {
            ptr = lutil_strcopy( ptr, "read=" );
        } else if ( rw & SLAP_TCP_WMEM ) {
            ptr = lutil_strcopy( ptr, "write=" );
        }
    }

    ptr = lutil_strcopy( ptr, buf );
    *ptr = '\0';

    assert( val->bv_val + val->bv_len == ptr );

    return LDAP_SUCCESS;
}

static int
tcp_buffer_add_one( int argc, char **argv )
{
    int rc = 0;
    int size = -1, rw = 0;
1377
    LloadListener *l = NULL;
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395

    struct berval val;

    /* parse */
    rc = tcp_buffer_parse( NULL, argc, argv, &size, &rw, &l );
    if ( rc != 0 ) {
        return rc;
    }

    /* unparse for later use */
    rc = tcp_buffer_unparse( size, rw, l, &val );
    if ( rc != LDAP_SUCCESS ) {
        return rc;
    }

    /* use parsed values */
    if ( l != NULL ) {
        int i;
1396
        LloadListener **ll = lloadd_get_listeners();
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438

        for ( i = 0; ll[i] != NULL; i++ ) {
            if ( ll[i] == l ) break;
        }

        if ( ll[i] == NULL ) {
            return LDAP_NO_SUCH_ATTRIBUTE;
        }

        /* buffer only applies to TCP listeners;
         * we do not do any check here, and delegate them
         * to setsockopt(2) */
        if ( rw & SLAP_TCP_RMEM ) l->sl_tcp_rmem = size;
        if ( rw & SLAP_TCP_WMEM ) l->sl_tcp_wmem = size;

        for ( i++; ll[i] != NULL && bvmatch( &l->sl_url, &ll[i]->sl_url );
                i++ ) {
            if ( rw & SLAP_TCP_RMEM ) ll[i]->sl_tcp_rmem = size;
            if ( rw & SLAP_TCP_WMEM ) ll[i]->sl_tcp_wmem = size;
        }

    } else {
        /* NOTE: this affects listeners without a specific setting,
         * does not set all listeners */
        if ( rw & SLAP_TCP_RMEM ) slapd_tcp_rmem = size;
        if ( rw & SLAP_TCP_WMEM ) slapd_tcp_wmem = size;
    }

    tcp_buffer = SLAP_REALLOC(
            tcp_buffer, sizeof(struct berval) * ( tcp_buffer_num + 2 ) );
    /* append */
    tcp_buffer[tcp_buffer_num] = val;

    tcp_buffer_num++;
    BER_BVZERO( &tcp_buffer[tcp_buffer_num] );

    return rc;
}

static int
config_tcp_buffer( ConfigArgs *c )
{
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
    int rc = LDAP_SUCCESS;

#ifdef BALANCER_MODULE
    if ( c->op == SLAP_CONFIG_EMIT ) {
        if ( tcp_buffer == NULL || BER_BVISNULL( &tcp_buffer[0] ) ) {
            return 1;
        }
        value_add( &c->rvalue_vals, tcp_buffer );
        value_add( &c->rvalue_nvals, tcp_buffer );

        return 0;
    } else if ( c->op == LDAP_MOD_DELETE ) {
        if ( !c->line ) {
            tcp_buffer_delete( tcp_buffer );
            ber_bvarray_free( tcp_buffer );
            tcp_buffer = NULL;
            tcp_buffer_num = 0;

        } else {
            int size = -1, rw = 0;
            LloadListener *l = NULL;

            struct berval val = BER_BVNULL;

            int i;

            if ( tcp_buffer_num == 0 ) {
                return 1;
            }

            /* parse */
            rc = tcp_buffer_parse(
                    NULL, c->argc - 1, &c->argv[1], &size, &rw, &l );
            if ( rc != 0 ) {
                return 1;
            }

            /* unparse for later use */
            rc = tcp_buffer_unparse( size, rw, l, &val );
            if ( rc != LDAP_SUCCESS ) {
                return 1;
            }

            for ( i = 0; !BER_BVISNULL( &tcp_buffer[i] ); i++ ) {
                if ( bvmatch( &tcp_buffer[i], &val ) ) {
                    break;
                }
            }

            if ( BER_BVISNULL( &tcp_buffer[i] ) ) {
                /* not found */
                rc = 1;
                goto done;
            }

            tcp_buffer_delete_one( &tcp_buffer[i] );
            ber_memfree( tcp_buffer[i].bv_val );
            for ( ; i < tcp_buffer_num; i++ ) {
                tcp_buffer[i] = tcp_buffer[i + 1];
            }
            tcp_buffer_num--;

done:;
            if ( !BER_BVISNULL( &val ) ) {
                SLAP_FREE(val.bv_val);
            }
        }

        return rc;
    }
#endif /* BALANCER_MODULE */
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741

    rc = tcp_buffer_add_one( c->argc - 1, &c->argv[1] );
    if ( rc ) {
        snprintf( c->cr_msg, sizeof(c->cr_msg), "<%s> unable to add value #%d",
                c->argv[0], tcp_buffer_num );
        Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg );
        return 1;
    }

    return 0;
}
#endif /* LDAP_TCP_BUFFER */

static int
config_restrict( ConfigArgs *c )
{
    slap_mask_t restrictops = 0;
    int i;
    slap_verbmasks restrictable_ops[] = {
        { BER_BVC("bind"), SLAP_RESTRICT_OP_BIND },
        { BER_BVC("add"), SLAP_RESTRICT_OP_ADD },
        { BER_BVC("modify"), SLAP_RESTRICT_OP_MODIFY },
        { BER_BVC("rename"), SLAP_RESTRICT_OP_RENAME },
        { BER_BVC("modrdn"), 0 },
        { BER_BVC("delete"), SLAP_RESTRICT_OP_DELETE },
        { BER_BVC("search"), SLAP_RESTRICT_OP_SEARCH },
        { BER_BVC("compare"), SLAP_RESTRICT_OP_COMPARE },
        { BER_BVC("read"), SLAP_RESTRICT_OP_READS },
        { BER_BVC("write"), SLAP_RESTRICT_OP_WRITES },
        { BER_BVC("extended"), SLAP_RESTRICT_OP_EXTENDED },
        { BER_BVC("extended=" LDAP_EXOP_START_TLS), SLAP_RESTRICT_EXOP_START_TLS },
        { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD), SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
        { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I), SLAP_RESTRICT_EXOP_WHOAMI },
        { BER_BVC("extended=" LDAP_EXOP_X_CANCEL), SLAP_RESTRICT_EXOP_CANCEL },
        { BER_BVC("all"), SLAP_RESTRICT_OP_ALL },
        { BER_BVNULL, 0 }
    };

    i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
    if ( i ) {
        snprintf( c->cr_msg, sizeof(c->cr_msg), "<%s> unknown operation",
                c->argv[0] );
        Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
                c->log, c->cr_msg, c->argv[i] );
        return 1;
    }
    if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
        restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
    return 0;
}

static slap_verbmasks *loglevel_ops;

static int
loglevel_init( void )
{
    slap_verbmasks lo[] = {
        { BER_BVC("Any"), (slap_mask_t)LDAP_DEBUG_ANY },
        { BER_BVC("Trace"), LDAP_DEBUG_TRACE },
        { BER_BVC("Packets"), LDAP_DEBUG_PACKETS },
        { BER_BVC("Args"), LDAP_DEBUG_ARGS },
        { BER_BVC("Conns"), LDAP_DEBUG_CONNS },
        { BER_BVC("BER"), LDAP_DEBUG_BER },
        { BER_BVC("Filter"), LDAP_DEBUG_FILTER },
        { BER_BVC("Config"), LDAP_DEBUG_CONFIG },
        { BER_BVC("ACL"), LDAP_DEBUG_ACL },
        { BER_BVC("Stats"), LDAP_DEBUG_STATS },
        { BER_BVC("Stats2"), LDAP_DEBUG_STATS2 },
        { BER_BVC("Shell"), LDAP_DEBUG_SHELL },
        { BER_BVC("Parse"), LDAP_DEBUG_PARSE },
        { BER_BVC("Sync"), LDAP_DEBUG_SYNC },
        { BER_BVC("None"), LDAP_DEBUG_NONE },
        { BER_BVNULL, 0 }
    };

    return slap_verbmasks_init( &loglevel_ops, lo );
}

static void
loglevel_destroy( void )
{
    if ( loglevel_ops ) {
        (void)slap_verbmasks_destroy( loglevel_ops );
    }
    loglevel_ops = NULL;
}

int
str2loglevel( const char *s, int *l )
{
    int i;

    if ( loglevel_ops == NULL ) {
        loglevel_init();
    }

    i = verb_to_mask( s, loglevel_ops );

    if ( BER_BVISNULL( &loglevel_ops[i].word ) ) {
        return -1;
    }

    *l = loglevel_ops[i].mask;

    return 0;
}

int
loglevel2bvarray( int l, BerVarray *bva )
{
    if ( loglevel_ops == NULL ) {
        loglevel_init();
    }

    if ( l == 0 ) {
        struct berval bv = BER_BVC("0");
        return value_add_one( bva, &bv );
    }

    return mask_to_verbs( loglevel_ops, l, bva );
}

int
loglevel_print( FILE *out )
{
    int i;

    if ( loglevel_ops == NULL ) {
        loglevel_init();
    }

    fprintf( out, "Installed log subsystems:\n\n" );
    for ( i = 0; !BER_BVISNULL( &loglevel_ops[i].word ); i++ ) {
        unsigned mask = loglevel_ops[i].mask & 0xffffffffUL;
        fprintf( out,
                ( mask == ( (slap_mask_t)-1 & 0xffffffffUL ) ?
                                "\t%-30s (-1, 0xffffffff)\n" :
                                "\t%-30s (%u, 0x%x)\n" ),
                loglevel_ops[i].word.bv_val, mask, mask );
    }

    fprintf( out,
            "\nNOTE: custom log subsystems may be later installed "
            "by specific code\n\n" );

    return 0;
}

static int config_syslog;

static int
config_loglevel( ConfigArgs *c )
{
    int i;

    if ( loglevel_ops == NULL ) {
        loglevel_init();
    }

    if ( c->op == SLAP_CONFIG_EMIT ) {
        /* Get default or commandline slapd setting */
        if ( ldap_syslog && !config_syslog ) config_syslog = ldap_syslog;
        return loglevel2bvarray( config_syslog, &c->rvalue_vals );

    } else if ( c->op == LDAP_MOD_DELETE ) {
        if ( !c->line ) {
            config_syslog = 0;
        } else {
            i = verb_to_mask( c->line, loglevel_ops );
            config_syslog &= ~loglevel_ops[i].mask;
        }
        if ( slapMode & SLAP_SERVER_MODE ) {
            ldap_syslog = config_syslog;
        }
        return 0;
    }

    for ( i = 1; i < c->argc; i++ ) {
        int level;

        if ( isdigit( (unsigned char)c->argv[i][0] ) || c->argv[i][0] == '-' ) {
            if ( lutil_atoix( &level, c->argv[i], 0 ) != 0 ) {
                snprintf( c->cr_msg, sizeof(c->cr_msg),
                        "<%s> unable to parse level",
                        c->argv[0] );
                Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
                        c->log, c->cr_msg, c->argv[i] );
                return 1;
            }
        } else {
            if ( str2loglevel( c->argv[i], &level ) ) {
                snprintf( c->cr_msg, sizeof(c->cr_msg), "<%s> unknown level",
                        c->argv[0] );
                Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
                        c->log, c->cr_msg, c->argv[i] );
                return 1;
            }
        }
        /* Explicitly setting a zero clears all the levels */
        if ( level )
            config_syslog |= level;
        else
            config_syslog = 0;
    }
    if ( slapMode & SLAP_SERVER_MODE ) {
        ldap_syslog = config_syslog;
    }
    return 0;
}

static int
config_include( ConfigArgs *c )
{
    int savelineno = c->lineno;
    int rc;
    ConfigFile *cf;
    ConfigFile *cfsave = cfn;
    ConfigFile *cf2 = NULL;

    /* Leftover from RE23. No dynamic config for include files */
    if ( c->op == SLAP_CONFIG_EMIT || c->op == LDAP_MOD_DELETE ) return 1;

    cf = ch_calloc( 1, sizeof(ConfigFile) );
    if ( cfn->c_kids ) {
        for ( cf2 = cfn->c_kids; cf2 && cf2->c_sibs; cf2 = cf2->c_sibs )
            /* empty */;
        cf2->c_sibs = cf;
    } else {
        cfn->c_kids = cf;
    }
    cfn = cf;
    ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
1742
1743
    rc = lload_read_config_file(
            c->argv[1], c->depth + 1, c, config_back_cf_table );
Ondřej Kuzník's avatar
Ondřej Kuzník committed
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758