Debian Codegraph @

servers/slapd/overlays/accesslog.c

raw
1 /* accesslog.c - log operations for audit/history purposes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2005-2024 The OpenLDAP Foundation.
6 * Portions copyright 2004-2005 Symas Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Howard Chu for inclusion in
19 * OpenLDAP Software.
20 */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_ACCESSLOG
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/ctype.h>
30
31 #include <assert.h>
32
33 #include "slap.h"
34 #include "slap-config.h"
35 #include "lutil.h"
36 #include "ldap_rq.h"
37
38 #define LOG_OP_ADD 0x001
39 #define LOG_OP_DELETE 0x002
40 #define LOG_OP_MODIFY 0x004
41 #define LOG_OP_MODRDN 0x008
42 #define LOG_OP_COMPARE 0x010
43 #define LOG_OP_SEARCH 0x020
44 #define LOG_OP_BIND 0x040
45 #define LOG_OP_UNBIND 0x080
46 #define LOG_OP_ABANDON 0x100
47 #define LOG_OP_EXTENDED 0x200
48 #define LOG_OP_UNKNOWN 0x400
49
50 #define LOG_OP_WRITES (LOG_OP_ADD|LOG_OP_DELETE|LOG_OP_MODIFY|LOG_OP_MODRDN)
51 #define LOG_OP_READS (LOG_OP_COMPARE|LOG_OP_SEARCH)
52 #define LOG_OP_SESSION (LOG_OP_BIND|LOG_OP_UNBIND|LOG_OP_ABANDON)
53 #define LOG_OP_ALL (LOG_OP_READS|LOG_OP_WRITES|LOG_OP_SESSION| \
54 LOG_OP_EXTENDED|LOG_OP_UNKNOWN)
55
56 typedef struct log_attr {
57 struct log_attr *next;
58 AttributeDescription *attr;
59 } log_attr;
60
61 typedef struct log_base {
62 struct log_base *lb_next;
63 slap_mask_t lb_ops;
64 struct berval lb_base;
65 struct berval lb_line;
66 } log_base;
67
68 typedef struct log_info {
69 BackendDB *li_db;
70 struct berval li_db_suffix;
71 int li_open;
72
73 slap_mask_t li_ops;
74 int li_age;
75 int li_cycle;
76 struct re_s *li_task;
77 Filter *li_oldf;
78 Entry *li_old;
79 log_attr *li_oldattrs;
80 struct berval li_uuid;
81 int li_success;
82 log_base *li_bases;
83 BerVarray li_mincsn;
84 int *li_sids, li_numcsns;
85
86 /*
87 * Allow partial concurrency, main operation processing serialised with
88 * li_op_rmutex (there might be multiple such in progress by the same
89 * thread at a time, think overlays), the actual logging and mincsn
90 * management are serialised with li_log_mutex.
91 *
92 * ITS#9538:
93 * Any CSN assignment should happen while li_op_rmutex is held and
94 * li_log_mutex should be acquired before the former has been released.
95 */
96 ldap_pvt_thread_mutex_t li_op_rmutex;
97 ldap_pvt_thread_mutex_t li_log_mutex;
98 } log_info;
99
100 static ConfigDriver log_cf_gen;
101
102 enum {
103 LOG_DB = 1,
104 LOG_OPS,
105 LOG_PURGE,
106 LOG_SUCCESS,
107 LOG_OLD,
108 LOG_OLDATTR,
109 LOG_BASE
110 };
111
112 static ConfigTable log_cfats[] = {
113 { "logdb", "suffix", 2, 2, 0, ARG_DN|ARG_QUOTE|ARG_MAGIC|LOG_DB,
114 log_cf_gen, "( OLcfgOvAt:4.1 NAME 'olcAccessLogDB' "
115 "DESC 'Suffix of database for log content' "
116 "EQUALITY distinguishedNameMatch "
117 "SUP distinguishedName SINGLE-VALUE )", NULL, NULL },
118 { "logops", "op|writes|reads|session|all", 2, 0, 0,
119 ARG_MAGIC|LOG_OPS,
120 log_cf_gen, "( OLcfgOvAt:4.2 NAME 'olcAccessLogOps' "
121 "DESC 'Operation types to log' "
122 "EQUALITY caseIgnoreMatch "
123 "SYNTAX OMsDirectoryString )", NULL, NULL },
124 { "logpurge", "age> <interval", 3, 3, 0, ARG_MAGIC|LOG_PURGE,
125 log_cf_gen, "( OLcfgOvAt:4.3 NAME 'olcAccessLogPurge' "
126 "DESC 'Log cleanup parameters' "
127 "EQUALITY caseIgnoreMatch "
128 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
129 { "logsuccess", NULL, 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|LOG_SUCCESS,
130 log_cf_gen, "( OLcfgOvAt:4.4 NAME 'olcAccessLogSuccess' "
131 "DESC 'Log successful ops only' "
132 "EQUALITY booleanMatch "
133 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
134 { "logold", "filter", 2, 2, 0, ARG_MAGIC|LOG_OLD,
135 log_cf_gen, "( OLcfgOvAt:4.5 NAME 'olcAccessLogOld' "
136 "DESC 'Log old values when modifying entries matching the filter' "
137 "EQUALITY caseExactMatch "
138 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
139 { "logoldattr", "attrs", 2, 0, 0, ARG_MAGIC|LOG_OLDATTR,
140 log_cf_gen, "( OLcfgOvAt:4.6 NAME 'olcAccessLogOldAttr' "
141 "DESC 'Log old values of these attributes even if unmodified' "
142 "EQUALITY caseIgnoreMatch "
143 "SYNTAX OMsDirectoryString )", NULL, NULL },
144 { "logbase", "op|writes|reads|session|all< <baseDN", 3, 3, 0,
145 ARG_MAGIC|LOG_BASE,
146 log_cf_gen, "( OLcfgOvAt:4.7 NAME 'olcAccessLogBase' "
147 "DESC 'Operation types to log under a specific branch' "
148 "EQUALITY caseIgnoreMatch "
149 "SYNTAX OMsDirectoryString )", NULL, NULL },
150 { NULL }
151 };
152
153 static ConfigOCs log_cfocs[] = {
154 { "( OLcfgOvOc:4.1 "
155 "NAME 'olcAccessLogConfig' "
156 "DESC 'Access log configuration' "
157 "SUP olcOverlayConfig "
158 "MUST olcAccessLogDB "
159 "MAY ( olcAccessLogOps $ olcAccessLogPurge $ olcAccessLogSuccess $ "
160 "olcAccessLogOld $ olcAccessLogOldAttr $ olcAccessLogBase ) )",
161 Cft_Overlay, log_cfats },
162 { NULL }
163 };
164
165 static slap_verbmasks logops[] = {
166 { BER_BVC("all"), LOG_OP_ALL },
167 { BER_BVC("writes"), LOG_OP_WRITES },
168 { BER_BVC("session"), LOG_OP_SESSION },
169 { BER_BVC("reads"), LOG_OP_READS },
170 { BER_BVC("add"), LOG_OP_ADD },
171 { BER_BVC("delete"), LOG_OP_DELETE },
172 { BER_BVC("modify"), LOG_OP_MODIFY },
173 { BER_BVC("modrdn"), LOG_OP_MODRDN },
174 { BER_BVC("compare"), LOG_OP_COMPARE },
175 { BER_BVC("search"), LOG_OP_SEARCH },
176 { BER_BVC("bind"), LOG_OP_BIND },
177 { BER_BVC("unbind"), LOG_OP_UNBIND },
178 { BER_BVC("abandon"), LOG_OP_ABANDON },
179 { BER_BVC("extended"), LOG_OP_EXTENDED },
180 { BER_BVC("unknown"), LOG_OP_UNKNOWN },
181 { BER_BVNULL, 0 }
182 };
183
184 /* Start with "add" in logops */
185 #define EN_OFFSET 4
186
187 enum {
188 LOG_EN_ADD = 0,
189 LOG_EN_DELETE,
190 LOG_EN_MODIFY,
191 LOG_EN_MODRDN,
192 LOG_EN_COMPARE,
193 LOG_EN_SEARCH,
194 LOG_EN_BIND,
195 LOG_EN_UNBIND,
196 LOG_EN_ABANDON,
197 LOG_EN_EXTENDED,
198 LOG_EN_UNKNOWN,
199 LOG_EN__COUNT
200 };
201
202 static ObjectClass *log_ocs[LOG_EN__COUNT], *log_container,
203 *log_oc_read, *log_oc_write;
204
205 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
206
207 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
208 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
209 #define LOG_SCHEMA_SYN LOG_SCHEMA_ROOT ".3"
210
211 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
212 *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
213 *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
214 *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
215 *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
216 *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
217 *ad_reqId, *ad_reqMessage, *ad_reqVersion, *ad_reqDerefAliases,
218 *ad_reqReferral, *ad_reqOld, *ad_auditContext, *ad_reqEntryUUID,
219 *ad_minCSN, *ad_reqNewDN;
220
221 static int
222 logSchemaControlValidate(
223 Syntax *syntax,
224 struct berval *val );
225
226 char *mrControl[] = {
227 "objectIdentifierFirstComponentMatch",
228 NULL
229 };
230
231 static struct {
232 char *oid;
233 slap_syntax_defs_rec syn;
234 char **mrs;
235 } lsyntaxes[] = {
236 { LOG_SCHEMA_SYN ".1" ,
237 { "( " LOG_SCHEMA_SYN ".1 DESC 'Control' )",
238 SLAP_SYNTAX_HIDE,
239 NULL,
240 logSchemaControlValidate,
241 NULL },
242 mrControl },
243 { NULL }
244 };
245
246 static struct {
247 char *at;
248 AttributeDescription **ad;
249 } lattrs[] = {
250 { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
251 "DESC 'Target DN of request' "
252 "EQUALITY distinguishedNameMatch "
253 "SYNTAX OMsDN "
254 "SINGLE-VALUE )", &ad_reqDN },
255 { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
256 "DESC 'Start time of request' "
257 "EQUALITY generalizedTimeMatch "
258 "ORDERING generalizedTimeOrderingMatch "
259 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
260 "SINGLE-VALUE )", &ad_reqStart },
261 { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
262 "DESC 'End time of request' "
263 "EQUALITY generalizedTimeMatch "
264 "ORDERING generalizedTimeOrderingMatch "
265 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
266 "SINGLE-VALUE )", &ad_reqEnd },
267 { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
268 "DESC 'Type of request' "
269 "EQUALITY caseIgnoreMatch "
270 "SYNTAX OMsDirectoryString "
271 "SINGLE-VALUE )", &ad_reqType },
272 { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
273 "DESC 'Session ID of request' "
274 "EQUALITY caseIgnoreMatch "
275 "SYNTAX OMsDirectoryString "
276 "SINGLE-VALUE )", &ad_reqSession },
277 { "( " LOG_SCHEMA_AT ".6 NAME 'reqAuthzID' "
278 "DESC 'Authorization ID of requestor' "
279 "EQUALITY distinguishedNameMatch "
280 "SYNTAX OMsDN "
281 "SINGLE-VALUE )", &ad_reqAuthzID },
282 { "( " LOG_SCHEMA_AT ".7 NAME 'reqResult' "
283 "DESC 'Result code of request' "
284 "EQUALITY integerMatch "
285 "ORDERING integerOrderingMatch "
286 "SYNTAX OMsInteger "
287 "SINGLE-VALUE )", &ad_reqResult },
288 { "( " LOG_SCHEMA_AT ".8 NAME 'reqMessage' "
289 "DESC 'Error text of request' "
290 "EQUALITY caseIgnoreMatch "
291 "SUBSTR caseIgnoreSubstringsMatch "
292 "SYNTAX OMsDirectoryString "
293 "SINGLE-VALUE )", &ad_reqMessage },
294 { "( " LOG_SCHEMA_AT ".9 NAME 'reqReferral' "
295 "DESC 'Referrals returned for request' "
296 "SUP labeledURI )", &ad_reqReferral },
297 { "( " LOG_SCHEMA_AT ".10 NAME 'reqControls' "
298 "DESC 'Request controls' "
299 "EQUALITY objectIdentifierFirstComponentMatch "
300 "SYNTAX " LOG_SCHEMA_SYN ".1 "
301 "X-ORDERED 'VALUES' )", &ad_reqControls },
302 { "( " LOG_SCHEMA_AT ".11 NAME 'reqRespControls' "
303 "DESC 'Response controls of request' "
304 "EQUALITY objectIdentifierFirstComponentMatch "
305 "SYNTAX " LOG_SCHEMA_SYN ".1 "
306 "X-ORDERED 'VALUES' )", &ad_reqRespControls },
307 { "( " LOG_SCHEMA_AT ".12 NAME 'reqId' "
308 "DESC 'ID of Request to Abandon' "
309 "EQUALITY integerMatch "
310 "ORDERING integerOrderingMatch "
311 "SYNTAX OMsInteger "
312 "SINGLE-VALUE )", &ad_reqId },
313 { "( " LOG_SCHEMA_AT ".13 NAME 'reqVersion' "
314 "DESC 'Protocol version of Bind request' "
315 "EQUALITY integerMatch "
316 "ORDERING integerOrderingMatch "
317 "SYNTAX OMsInteger "
318 "SINGLE-VALUE )", &ad_reqVersion },
319 { "( " LOG_SCHEMA_AT ".14 NAME 'reqMethod' "
320 "DESC 'Bind method of request' "
321 "EQUALITY caseIgnoreMatch "
322 "SYNTAX OMsDirectoryString "
323 "SINGLE-VALUE )", &ad_reqMethod },
324 { "( " LOG_SCHEMA_AT ".15 NAME 'reqAssertion' "
325 "DESC 'Compare Assertion of request' "
326 "SYNTAX OMsDirectoryString "
327 "SINGLE-VALUE )", &ad_reqAssertion },
328 { "( " LOG_SCHEMA_AT ".16 NAME 'reqMod' "
329 "DESC 'Modifications of request' "
330 "EQUALITY octetStringMatch "
331 "SUBSTR octetStringSubstringsMatch "
332 "SYNTAX OMsOctetString )", &ad_reqMod },
333 { "( " LOG_SCHEMA_AT ".17 NAME 'reqOld' "
334 "DESC 'Old values of entry before request completed' "
335 "EQUALITY octetStringMatch "
336 "SUBSTR octetStringSubstringsMatch "
337 "SYNTAX OMsOctetString )", &ad_reqOld },
338 { "( " LOG_SCHEMA_AT ".18 NAME 'reqNewRDN' "
339 "DESC 'New RDN of request' "
340 "EQUALITY distinguishedNameMatch "
341 "SYNTAX OMsDN "
342 "SINGLE-VALUE )", &ad_reqNewRDN },
343 { "( " LOG_SCHEMA_AT ".19 NAME 'reqDeleteOldRDN' "
344 "DESC 'Delete old RDN' "
345 "EQUALITY booleanMatch "
346 "SYNTAX OMsBoolean "
347 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
348 { "( " LOG_SCHEMA_AT ".20 NAME 'reqNewSuperior' "
349 "DESC 'New superior DN of request' "
350 "EQUALITY distinguishedNameMatch "
351 "SYNTAX OMsDN "
352 "SINGLE-VALUE )", &ad_reqNewSuperior },
353 { "( " LOG_SCHEMA_AT ".21 NAME 'reqScope' "
354 "DESC 'Scope of request' "
355 "EQUALITY caseIgnoreMatch "
356 "SYNTAX OMsDirectoryString "
357 "SINGLE-VALUE )", &ad_reqScope },
358 { "( " LOG_SCHEMA_AT ".22 NAME 'reqDerefAliases' "
359 "DESC 'Disposition of Aliases in request' "
360 "EQUALITY caseIgnoreMatch "
361 "SYNTAX OMsDirectoryString "
362 "SINGLE-VALUE )", &ad_reqDerefAliases },
363 { "( " LOG_SCHEMA_AT ".23 NAME 'reqAttrsOnly' "
364 "DESC 'Attributes and values of request' "
365 "EQUALITY booleanMatch "
366 "SYNTAX OMsBoolean "
367 "SINGLE-VALUE )", &ad_reqAttrsOnly },
368 { "( " LOG_SCHEMA_AT ".24 NAME 'reqFilter' "
369 "DESC 'Filter of request' "
370 "EQUALITY caseIgnoreMatch "
371 "SUBSTR caseIgnoreSubstringsMatch "
372 "SYNTAX OMsDirectoryString "
373 "SINGLE-VALUE )", &ad_reqFilter },
374 { "( " LOG_SCHEMA_AT ".25 NAME 'reqAttr' "
375 "DESC 'Attributes of request' "
376 "EQUALITY caseIgnoreMatch "
377 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
378 { "( " LOG_SCHEMA_AT ".26 NAME 'reqSizeLimit' "
379 "DESC 'Size limit of request' "
380 "EQUALITY integerMatch "
381 "ORDERING integerOrderingMatch "
382 "SYNTAX OMsInteger "
383 "SINGLE-VALUE )", &ad_reqSizeLimit },
384 { "( " LOG_SCHEMA_AT ".27 NAME 'reqTimeLimit' "
385 "DESC 'Time limit of request' "
386 "EQUALITY integerMatch "
387 "ORDERING integerOrderingMatch "
388 "SYNTAX OMsInteger "
389 "SINGLE-VALUE )", &ad_reqTimeLimit },
390 { "( " LOG_SCHEMA_AT ".28 NAME 'reqEntries' "
391 "DESC 'Number of entries returned' "
392 "EQUALITY integerMatch "
393 "ORDERING integerOrderingMatch "
394 "SYNTAX OMsInteger "
395 "SINGLE-VALUE )", &ad_reqEntries },
396 { "( " LOG_SCHEMA_AT ".29 NAME 'reqData' "
397 "DESC 'Data of extended request' "
398 "EQUALITY octetStringMatch "
399 "SUBSTR octetStringSubstringsMatch "
400 "SYNTAX OMsOctetString "
401 "SINGLE-VALUE )", &ad_reqData },
402
403 /*
404 * from <draft-chu-ldap-logschema-01.txt>:
405 *
406
407 ( LOG_SCHEMA_AT .30 NAME 'auditContext'
408 DESC 'DN of auditContainer'
409 EQUALITY distinguishedNameMatch
410 SYNTAX 1.3.6.1.4.1.1466.115.121.1.12
411 SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )
412
413 * - removed EQUALITY matchingRule
414 * - changed directoryOperation in dSAOperation
415 */
416 { "( " LOG_SCHEMA_AT ".30 NAME 'auditContext' "
417 "DESC 'DN of auditContainer' "
418 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
419 "SINGLE-VALUE "
420 "NO-USER-MODIFICATION "
421 "USAGE dSAOperation )", &ad_auditContext },
422
423 /*
424 * ITS#6656
425 */
426 { "( " LOG_SCHEMA_AT ".31 NAME 'reqEntryUUID' "
427 "DESC 'UUID of entry' "
428 "EQUALITY UUIDMatch "
429 "ORDERING UUIDOrderingMatch "
430 "SYNTAX 1.3.6.1.1.16.1 "
431 "SINGLE-VALUE )", &ad_reqEntryUUID },
432
433 /*
434 * ITS#8486
435 */
436 { "( " LOG_SCHEMA_AT ".32 NAME 'minCSN' "
437 "DESC 'CSN set that the logs are recorded from' "
438 "EQUALITY CSNMatch "
439 "ORDERING CSNOrderingMatch "
440 "SYNTAX 1.3.6.1.4.1.4203.666.11.2.1{64} "
441 "NO-USER-MODIFICATION "
442 "USAGE dSAOperation )", &ad_minCSN },
443
444 /*
445 * ITS#9552
446 */
447 { "( " LOG_SCHEMA_AT ".33 NAME 'reqNewDN' "
448 "DESC 'New DN after rename' "
449 "EQUALITY distinguishedNameMatch "
450 "SYNTAX OMsDN "
451 "SINGLE-VALUE )", &ad_reqNewDN },
452 { NULL, NULL }
453 };
454
455 static struct {
456 char *ot;
457 ObjectClass **oc;
458 } locs[] = {
459 { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
460 "DESC 'AuditLog container' "
461 "SUP top STRUCTURAL "
462 "MAY ( cn $ reqStart $ reqEnd ) )", &log_container },
463 { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
464 "DESC 'OpenLDAP request auditing' "
465 "SUP top STRUCTURAL "
466 "MUST ( reqStart $ reqType $ reqSession ) "
467 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
468 "reqResult $ reqMessage $ reqReferral $ reqEntryUUID ) )",
469 &log_ocs[LOG_EN_UNBIND] },
470 { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
471 "DESC 'OpenLDAP read request record' "
472 "SUP auditObject STRUCTURAL )", &log_oc_read },
473 { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
474 "DESC 'OpenLDAP write request record' "
475 "SUP auditObject STRUCTURAL )", &log_oc_write },
476 { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
477 "DESC 'Abandon operation' "
478 "SUP auditObject STRUCTURAL "
479 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
480 { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
481 "DESC 'Add operation' "
482 "SUP auditWriteObject STRUCTURAL "
483 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
484 { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
485 "DESC 'Bind operation' "
486 "SUP auditObject STRUCTURAL "
487 "MUST ( reqVersion $ reqMethod ) )", &log_ocs[LOG_EN_BIND] },
488 { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
489 "DESC 'Compare operation' "
490 "SUP auditReadObject STRUCTURAL "
491 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
492 { "( " LOG_SCHEMA_OC ".8 NAME 'auditDelete' "
493 "DESC 'Delete operation' "
494 "SUP auditWriteObject STRUCTURAL "
495 "MAY reqOld )", &log_ocs[LOG_EN_DELETE] },
496 { "( " LOG_SCHEMA_OC ".9 NAME 'auditModify' "
497 "DESC 'Modify operation' "
498 "SUP auditWriteObject STRUCTURAL "
499 "MAY ( reqOld $ reqMod ) )", &log_ocs[LOG_EN_MODIFY] },
500 { "( " LOG_SCHEMA_OC ".10 NAME 'auditModRDN' "
501 "DESC 'ModRDN operation' "
502 "SUP auditWriteObject STRUCTURAL "
503 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
504 "MAY ( reqNewSuperior $ reqMod $ reqOld $ reqNewDN ) )",
505 &log_ocs[LOG_EN_MODRDN] },
506 { "( " LOG_SCHEMA_OC ".11 NAME 'auditSearch' "
507 "DESC 'Search operation' "
508 "SUP auditReadObject STRUCTURAL "
509 "MUST ( reqScope $ reqDerefAliases $ reqAttrsonly ) "
510 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
511 "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
512 { "( " LOG_SCHEMA_OC ".12 NAME 'auditExtended' "
513 "DESC 'Extended operation' "
514 "SUP auditObject STRUCTURAL "
515 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
516 { NULL, NULL }
517 };
518
519 #define RDNEQ "reqStart="
520
521 /* Our time intervals are of the form [ddd+]hh:mm[:ss]
522 * If a field is present, it must be two digits. (Except for
523 * days, which can be arbitrary width.)
524 */
525 static int
526 log_age_parse(char *agestr)
527 {
528 int t1, t2;
529 int gotdays = 0;
530 char *endptr;
531
532 t1 = strtol( agestr, &endptr, 10 );
533 /* Is there a days delimiter? */
534 if ( *endptr == '+' ) {
535 /* 32 bit time only covers about 68 years */
536 if ( t1 < 0 || t1 > 25000 )
537 return -1;
538 t1 *= 24;
539 gotdays = 1;
540 agestr = endptr + 1;
541 } else {
542 if ( agestr[2] != ':' ) {
543 /* No valid delimiter found, fail */
544 return -1;
545 }
546 t1 *= 60;
547 agestr += 3;
548 }
549
550 t2 = atoi( agestr );
551 t1 += t2;
552
553 if ( agestr[2] ) {
554 /* if there's a delimiter, it can only be a colon */
555 if ( agestr[2] != ':' )
556 return -1;
557 } else {
558 /* If we're at the end of the string, and we started with days,
559 * fail because we expected to find minutes too.
560 */
561 return gotdays ? -1 : t1 * 60;
562 }
563
564 agestr += 3;
565 t2 = atoi( agestr );
566
567 /* last field can only be seconds */
568 if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
569 return -1;
570 t1 *= 60;
571 t1 += t2;
572
573 if ( agestr[2] ) {
574 agestr += 3;
575 if ( agestr[2] )
576 return -1;
577 t1 *= 60;
578 t1 += atoi( agestr );
579 } else if ( gotdays ) {
580 /* only got days+hh:mm */
581 t1 *= 60;
582 }
583 return t1;
584 }
585
586 static void
587 log_age_unparse( int age, struct berval *agebv, size_t size )
588 {
589 int dd, hh, mm, ss, len;
590 char *ptr;
591
592 assert( size > 0 );
593
594 ss = age % 60;
595 age /= 60;
596 mm = age % 60;
597 age /= 60;
598 hh = age % 24;
599 age /= 24;
600 dd = age;
601
602 ptr = agebv->bv_val;
603
604 if ( dd ) {
605 len = snprintf( ptr, size, "%d+", dd );
606 assert( len >= 0 && (unsigned) len < size );
607 size -= len;
608 ptr += len;
609 }
610 len = snprintf( ptr, size, "%02d:%02d", hh, mm );
611 assert( len >= 0 && (unsigned) len < size );
612 size -= len;
613 ptr += len;
614 if ( ss ) {
615 len = snprintf( ptr, size, ":%02d", ss );
616 assert( len >= 0 && (unsigned) len < size );
617 size -= len;
618 ptr += len;
619 }
620
621 agebv->bv_len = ptr - agebv->bv_val;
622 }
623
624 static slap_callback nullsc;
625
626 #define PURGE_INCREMENT 100
627
628 typedef struct purge_data {
629 struct log_info *li;
630 int slots;
631 int used;
632 int mincsn_updated;
633 BerVarray dn;
634 BerVarray ndn;
635 } purge_data;
636
637 static int
638 log_old_lookup( Operation *op, SlapReply *rs )
639 {
640 purge_data *pd = op->o_callback->sc_private;
641 struct log_info *li = pd->li;
642 Attribute *a;
643
644 if ( rs->sr_type != REP_SEARCH) return 0;
645
646 if ( slapd_shutdown ) return 0;
647
648 /* Update minCSN */
649 a = attr_find( rs->sr_entry->e_attrs,
650 slap_schema.si_ad_entryCSN );
651 if ( a ) {
652 ber_len_t len = a->a_nvals[0].bv_len;
653 int i, sid;
654
655 /* Find the correct sid */
656 sid = slap_parse_csn_sid( &a->a_nvals[0] );
657 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
658 for ( i=0; i < li->li_numcsns; i++ ) {
659 if ( sid <= li->li_sids[i] ) break;
660 }
661 if ( i >= li->li_numcsns || sid != li->li_sids[i] ) {
662 Debug( LDAP_DEBUG_ANY, "log_old_lookup: "
663 "csn=%s with sid not in minCSN set!\n",
664 a->a_nvals[0].bv_val );
665 slap_insert_csn_sids( (struct sync_cookie *)&li->li_mincsn, i,
666 sid, &a->a_nvals[0] );
667 } else {
668 /* Paranoid len check, normalized CSNs are always the same length */
669 if ( len > li->li_mincsn[i].bv_len )
670 len = li->li_mincsn[i].bv_len;
671 if ( ber_bvcmp( &li->li_mincsn[i], &a->a_nvals[0] ) < 0 ) {
672 pd->mincsn_updated = 1;
673 AC_MEMCPY( li->li_mincsn[i].bv_val, a->a_nvals[0].bv_val, len );
674 }
675 }
676 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
677 }
678 if ( pd->used >= pd->slots ) {
679 pd->slots += PURGE_INCREMENT;
680 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
681 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
682 }
683 ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
684 ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
685 pd->used++;
686 return 0;
687 }
688
689 /* Periodically search for old entries in the log database and delete them */
690 static void *
691 accesslog_purge( void *ctx, void *arg )
692 {
693 struct re_s *rtask = arg;
694 struct log_info *li = rtask->arg;
695
696 Connection conn = {0};
697 OperationBuffer opbuf;
698 Operation *op;
699 SlapReply rs = {REP_RESULT};
700 slap_callback cb = { NULL, log_old_lookup, NULL, NULL, NULL };
701 Filter f;
702 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
703 purge_data pd = { .li = li };
704 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
705 char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
706 time_t old = slap_get_time();
707
708 connection_fake_init( &conn, &opbuf, ctx );
709 op = &opbuf.ob_op;
710
711 f.f_choice = LDAP_FILTER_LE;
712 f.f_ava = &ava;
713 f.f_next = NULL;
714
715 ava.aa_desc = ad_reqStart;
716 ava.aa_value.bv_val = timebuf;
717 ava.aa_value.bv_len = sizeof(timebuf);
718
719 old -= li->li_age;
720 slap_timestamp( &old, &ava.aa_value );
721
722 op->o_tag = LDAP_REQ_SEARCH;
723 op->o_bd = li->li_db;
724 op->o_dn = li->li_db->be_rootdn;
725 op->o_ndn = li->li_db->be_rootndn;
726 op->o_req_dn = li->li_db->be_suffix[0];
727 op->o_req_ndn = li->li_db->be_nsuffix[0];
728 op->o_callback = &cb;
729 op->ors_scope = LDAP_SCOPE_ONELEVEL;
730 op->ors_deref = LDAP_DEREF_NEVER;
731 op->ors_tlimit = SLAP_NO_LIMIT;
732 op->ors_slimit = SLAP_NO_LIMIT;
733 op->ors_filter = &f;
734 filter2bv_x( op, &f, &op->ors_filterstr );
735 op->ors_attrs = slap_anlist_no_attrs;
736 op->ors_attrsonly = 1;
737
738 cb.sc_private = &pd;
739
740 op->o_bd->be_search( op, &rs );
741 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
742
743 if ( pd.used ) {
744 int i;
745
746 op->o_callback = &nullsc;
747 op->o_dont_replicate = 1;
748 op->o_csn = slap_empty_bv;
749
750 if ( pd.mincsn_updated ) {
751 Modifications mod;
752 /* update context's minCSN to reflect oldest CSN */
753 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
754 mod.sml_numvals = li->li_numcsns;
755 mod.sml_values = li->li_mincsn;
756 mod.sml_nvalues = li->li_mincsn;
757 mod.sml_desc = ad_minCSN;
758 mod.sml_op = LDAP_MOD_REPLACE;
759 mod.sml_flags = SLAP_MOD_INTERNAL;
760 mod.sml_next = NULL;
761
762 op->o_tag = LDAP_REQ_MODIFY;
763 op->orm_modlist = &mod;
764 op->orm_no_opattrs = 1;
765 op->o_req_dn = li->li_db->be_suffix[0];
766 op->o_req_ndn = li->li_db->be_nsuffix[0];
767 op->o_no_schema_check = 1;
768 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
769 if ( !slapd_shutdown ) {
770 Debug( LDAP_DEBUG_SYNC, "accesslog_purge: "
771 "updating minCSN with %d values\n",
772 li->li_numcsns );
773 op->o_bd->be_modify( op, &rs );
774 }
775 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
776 }
777
778 /* delete the expired entries */
779 op->o_tag = LDAP_REQ_DELETE;
780 for (i=0; i<pd.used; i++) {
781 op->o_req_dn = pd.dn[i];
782 op->o_req_ndn = pd.ndn[i];
783 if ( !slapd_shutdown ) {
784 rs_reinit( &rs, REP_RESULT );
785 op->o_bd->be_delete( op, &rs );
786 }
787 ch_free( pd.ndn[i].bv_val );
788 ch_free( pd.dn[i].bv_val );
789 ldap_pvt_thread_pool_pausewait( &connection_pool );
790 }
791 ch_free( pd.ndn );
792 ch_free( pd.dn );
793 }
794
795 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
796 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
797 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
798
799 return NULL;
800 }
801
802 static int
803 log_cf_gen(ConfigArgs *c)
804 {
805 slap_overinst *on = (slap_overinst *)c->bi;
806 struct log_info *li = on->on_bi.bi_private;
807 int rc = 0;
808 slap_mask_t tmask = 0;
809 char agebuf[2*STRLENOF("ddddd+hh:mm:ss ")];
810 struct berval agebv, cyclebv;
811
812 switch( c->op ) {
813 case SLAP_CONFIG_EMIT:
814 switch( c->type ) {
815 case LOG_DB:
816 if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
817 value_add_one( &c->rvalue_vals, &li->li_db_suffix );
818 value_add_one( &c->rvalue_nvals, &li->li_db_suffix );
819 } else if ( li->li_db ) {
820 value_add_one( &c->rvalue_vals, li->li_db->be_suffix );
821 value_add_one( &c->rvalue_nvals, li->li_db->be_nsuffix );
822 } else {
823 snprintf( c->cr_msg, sizeof( c->cr_msg ),
824 "accesslog: \"logdb <suffix>\" must be specified" );
825 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
826 c->log, c->cr_msg, c->value_dn.bv_val );
827 rc = 1;
828 break;
829 }
830 break;
831 case LOG_OPS:
832 rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
833 break;
834 case LOG_PURGE:
835 if ( !li->li_age ) {
836 rc = 1;
837 break;
838 }
839 agebv.bv_val = agebuf;
840 log_age_unparse( li->li_age, &agebv, sizeof( agebuf ) );
841 agebv.bv_val[agebv.bv_len] = ' ';
842 agebv.bv_len++;
843 cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
844 log_age_unparse( li->li_cycle, &cyclebv, sizeof( agebuf ) - agebv.bv_len );
845 agebv.bv_len += cyclebv.bv_len;
846 value_add_one( &c->rvalue_vals, &agebv );
847 break;
848 case LOG_SUCCESS:
849 if ( li->li_success )
850 c->value_int = li->li_success;
851 else
852 rc = 1;
853 break;
854 case LOG_OLD:
855 if ( li->li_oldf ) {
856 filter2bv( li->li_oldf, &agebv );
857 ber_bvarray_add( &c->rvalue_vals, &agebv );
858 }
859 else
860 rc = 1;
861 break;
862 case LOG_OLDATTR:
863 if ( li->li_oldattrs ) {
864 log_attr *la;
865
866 for ( la = li->li_oldattrs; la; la=la->next )
867 value_add_one( &c->rvalue_vals, &la->attr->ad_cname );
868 }
869 else
870 rc = 1;
871 break;
872 case LOG_BASE:
873 if ( li->li_bases ) {
874 log_base *lb;
875
876 for ( lb = li->li_bases; lb; lb=lb->lb_next )
877 value_add_one( &c->rvalue_vals, &lb->lb_line );
878 }
879 else
880 rc = 1;
881 break;
882 }
883 break;
884 case LDAP_MOD_DELETE:
885 switch( c->type ) {
886 case LOG_DB:
887 /* noop. this should always be a valid backend. */
888 break;
889 case LOG_OPS:
890 if ( c->valx < 0 ) {
891 li->li_ops = 0;
892 } else {
893 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
894 if ( rc == 0 )
895 li->li_ops &= ~tmask;
896 }
897 break;
898 case LOG_PURGE:
899 if ( li->li_task ) {
900 struct re_s *re = li->li_task;
901 li->li_task = NULL;
902 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
903 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
904 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
905 ldap_pvt_runqueue_remove( &slapd_rq, re );
906 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
907 }
908 li->li_age = 0;
909 li->li_cycle = 0;
910 break;
911 case LOG_SUCCESS:
912 li->li_success = 0;
913 break;
914 case LOG_OLD:
915 if ( li->li_oldf ) {
916 filter_free( li->li_oldf );
917 li->li_oldf = NULL;
918 }
919 break;
920 case LOG_OLDATTR:
921 if ( c->valx < 0 ) {
922 log_attr *la, *ln;
923
924 for ( la = li->li_oldattrs; la; la = ln ) {
925 ln = la->next;
926 ch_free( la );
927 }
928 } else {
929 log_attr *la = li->li_oldattrs, **lp = &li->li_oldattrs;
930 int i;
931
932 for ( i=0; i < c->valx; i++ ) {
933 la = *lp;
934 lp = &la->next;
935 }
936 *lp = la->next;
937 ch_free( la );
938 }
939 break;
940 case LOG_BASE:
941 if ( c->valx < 0 ) {
942 log_base *lb, *ln;
943
944 for ( lb = li->li_bases; lb; lb = ln ) {
945 ln = lb->lb_next;
946 ch_free( lb );
947 }
948 } else {
949 log_base *lb = li->li_bases, **lp = &li->li_bases;
950 int i;
951
952 for ( i=0; i < c->valx; i++ ) {
953 lb = *lp;
954 lp = &lb->lb_next;
955 }
956 *lp = lb->lb_next;
957 ch_free( lb );
958 }
959 break;
960 }
961 break;
962 default:
963 switch( c->type ) {
964 case LOG_DB:
965 if ( CONFIG_ONLINE_ADD( c )) {
966 li->li_db = select_backend( &c->value_ndn, 0 );
967 if ( !li->li_db ) {
968 snprintf( c->cr_msg, sizeof( c->cr_msg ),
969 "<%s> no matching backend found for suffix",
970 c->argv[0] );
971 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
972 c->log, c->cr_msg, c->value_dn.bv_val );
973 rc = 1;
974 }
975 if ( !rc && ( li->li_db->bd_self == c->be->bd_self )) {
976 snprintf( c->cr_msg, sizeof( c->cr_msg ),
977 "<%s> invalid suffix, points to itself",
978 c->argv[0] );
979 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
980 c->log, c->cr_msg, c->value_dn.bv_val );
981 rc = 1;
982 }
983 ch_free( c->value_ndn.bv_val );
984 } else {
985 li->li_db_suffix = c->value_ndn;
986 }
987 ch_free( c->value_dn.bv_val );
988 break;
989 case LOG_OPS:
990 rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
991 if ( rc == 0 )
992 li->li_ops |= tmask;
993 break;
994 case LOG_PURGE:
995 li->li_age = log_age_parse( c->argv[1] );
996 if ( li->li_age < 1 ) {
997 rc = 1;
998 } else {
999 li->li_cycle = log_age_parse( c->argv[2] );
1000 if ( li->li_cycle < 1 ) {
1001 rc = 1;
1002 } else if ( slapMode & SLAP_SERVER_MODE ) {
1003 struct re_s *re = li->li_task;
1004 if ( re )
1005 re->interval.tv_sec = li->li_cycle;
1006 else if ( li->li_open ) {
1007 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1008 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
1009 li->li_cycle, accesslog_purge, li,
1010 "accesslog_purge", li->li_db ?
1011 li->li_db->be_suffix[0].bv_val :
1012 c->be->be_suffix[0].bv_val );
1013 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1014 }
1015 }
1016 }
1017 break;
1018 case LOG_SUCCESS:
1019 li->li_success = c->value_int;
1020 break;
1021 case LOG_OLD:
1022 li->li_oldf = str2filter( c->argv[1] );
1023 if ( !li->li_oldf ) {
1024 snprintf( c->cr_msg, sizeof( c->cr_msg ), "bad filter!" );
1025 rc = 1;
1026 }
1027 break;
1028 case LOG_OLDATTR: {
1029 int i;
1030 AttributeDescription *ad;
1031 const char *text;
1032 log_attr **lp = &li->li_oldattrs;
1033
1034 for ( i=0; *lp && ( c->valx < 0 || i < c->valx ); i++ )
1035 lp = &(*lp)->next;
1036
1037 for ( i=1; i< c->argc; i++ ) {
1038 ad = NULL;
1039 if ( slap_str2ad( c->argv[i], &ad, &text ) == LDAP_SUCCESS ) {
1040 log_attr *la = ch_malloc( sizeof( log_attr ));
1041 la->attr = ad;
1042 if ( *lp ) {
1043 la->next = (*lp)->next;
1044 } else {
1045 la->next = NULL;
1046 }
1047 *lp = la;
1048 lp = &la->next;
1049 } else {
1050 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s <%s>: %s",
1051 c->argv[0], c->argv[i], text );
1052 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
1053 "%s: %s\n", c->log, c->cr_msg );
1054 rc = ARG_BAD_CONF;
1055 break;
1056 }
1057 }
1058 }
1059 break;
1060 case LOG_BASE: {
1061 int i;
1062 slap_mask_t m = 0;
1063 log_base **lp = &li->li_bases;
1064
1065 for ( i=0; *lp && ( c->valx < 0 || i < c->valx ); i++ )
1066 lp = &(*lp)->lb_next;
1067
1068 rc = verbstring_to_mask( logops, c->argv[1], '|', &m );
1069 if ( rc == 0 ) {
1070 struct berval dn, ndn;
1071 ber_str2bv( c->argv[2], 0, 0, &dn );
1072 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
1073 if ( rc == 0 ) {
1074 log_base *lb;
1075 struct berval mbv;
1076 char *ptr;
1077 mask_to_verbstring( logops, m, '|', &mbv );
1078 lb = ch_malloc( sizeof( log_base ) + mbv.bv_len + ndn.bv_len + 3 + 1 );
1079 lb->lb_line.bv_val = (char *)(lb + 1);
1080 lb->lb_line.bv_len = mbv.bv_len + ndn.bv_len + 3;
1081 ptr = lutil_strcopy( lb->lb_line.bv_val, mbv.bv_val );
1082 *ptr++ = ' ';
1083 *ptr++ = '"';
1084 lb->lb_base.bv_val = ptr;
1085 lb->lb_base.bv_len = ndn.bv_len;
1086 ptr = lutil_strcopy( ptr, ndn.bv_val );
1087 *ptr++ = '"';
1088 lb->lb_ops = m;
1089 lb->lb_next = li->li_bases;
1090 if ( *lp ) {
1091 lb->lb_next = (*lp)->lb_next;
1092 } else {
1093 lb->lb_next = NULL;
1094 }
1095 *lp = lb;
1096 } else {
1097 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: %s",
1098 c->argv[0], c->argv[2] );
1099 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
1100 "%s: %s\n", c->log, c->cr_msg );
1101 rc = ARG_BAD_CONF;
1102 }
1103 } else {
1104 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid ops: %s",
1105 c->argv[0], c->argv[1] );
1106 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
1107 "%s: %s\n", c->log, c->cr_msg );
1108 rc = ARG_BAD_CONF;
1109 }
1110 }
1111 break;
1112 }
1113 break;
1114 }
1115 return rc;
1116 }
1117
1118 static int
1119 logSchemaControlValidate(
1120 Syntax *syntax,
1121 struct berval *valp )
1122 {
1123 struct berval val, bv;
1124 ber_len_t i;
1125 int rc = LDAP_SUCCESS;
1126
1127 assert( valp != NULL );
1128
1129 val = *valp;
1130
1131 /* check minimal size */
1132 if ( val.bv_len < STRLENOF( "{*}" ) ) {
1133 return LDAP_INVALID_SYNTAX;
1134 }
1135
1136 val.bv_len--;
1137
1138 /* check SEQUENCE boundaries */
1139 if ( val.bv_val[ 0 ] != '{' /*}*/ ||
1140 val.bv_val[ val.bv_len ] != /*{*/ '}' )
1141 {
1142 return LDAP_INVALID_SYNTAX;
1143 }
1144
1145 /* extract and check OID */
1146 for ( i = 1; i < val.bv_len; i++ ) {
1147 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1148 break;
1149 }
1150 }
1151
1152 bv.bv_val = &val.bv_val[ i ];
1153
1154 for ( i++; i < val.bv_len; i++ ) {
1155 if ( ASCII_SPACE( val.bv_val[ i ] ) )
1156 {
1157 break;
1158 }
1159 }
1160
1161 bv.bv_len = &val.bv_val[ i ] - bv.bv_val;
1162
1163 rc = numericoidValidate( NULL, &bv );
1164 if ( rc != LDAP_SUCCESS ) {
1165 return rc;
1166 }
1167
1168 if ( i == val.bv_len ) {
1169 return LDAP_SUCCESS;
1170 }
1171
1172 if ( val.bv_val[ i ] != ' ' ) {
1173 return LDAP_INVALID_SYNTAX;
1174 }
1175
1176 for ( i++; i < val.bv_len; i++ ) {
1177 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1178 break;
1179 }
1180 }
1181
1182 if ( i == val.bv_len ) {
1183 return LDAP_SUCCESS;
1184 }
1185
1186 /* extract and check criticality */
1187 if ( strncasecmp( &val.bv_val[ i ], "criticality ", STRLENOF( "criticality " ) ) == 0 )
1188 {
1189 i += STRLENOF( "criticality " );
1190 for ( ; i < val.bv_len; i++ ) {
1191 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1192 break;
1193 }
1194 }
1195
1196 if ( i == val.bv_len ) {
1197 return LDAP_INVALID_SYNTAX;
1198 }
1199
1200 bv.bv_val = &val.bv_val[ i ];
1201
1202 for ( ; i < val.bv_len; i++ ) {
1203 if ( ASCII_SPACE( val.bv_val[ i ] ) ) {
1204 break;
1205 }
1206 }
1207
1208 bv.bv_len = &val.bv_val[ i ] - bv.bv_val;
1209
1210 if ( !bvmatch( &bv, &slap_true_bv ) && !bvmatch( &bv, &slap_false_bv ) )
1211 {
1212 return LDAP_INVALID_SYNTAX;
1213 }
1214
1215 if ( i == val.bv_len ) {
1216 return LDAP_SUCCESS;
1217 }
1218
1219 if ( val.bv_val[ i ] != ' ' ) {
1220 return LDAP_INVALID_SYNTAX;
1221 }
1222
1223 for ( i++; i < val.bv_len; i++ ) {
1224 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1225 break;
1226 }
1227 }
1228
1229 if ( i == val.bv_len ) {
1230 return LDAP_SUCCESS;
1231 }
1232 }
1233
1234 /* extract and check controlValue */
1235 if ( strncasecmp( &val.bv_val[ i ], "controlValue ", STRLENOF( "controlValue " ) ) == 0 )
1236 {
1237 ber_len_t valueStart, valueLen;
1238
1239 i += STRLENOF( "controlValue " );
1240 for ( ; i < val.bv_len; i++ ) {
1241 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1242 break;
1243 }
1244 }
1245
1246 if ( i == val.bv_len ) {
1247 return LDAP_INVALID_SYNTAX;
1248 }
1249
1250 if ( val.bv_val[ i ] != '"' ) {
1251 return LDAP_INVALID_SYNTAX;
1252 }
1253
1254 i++;
1255 valueStart = i;
1256
1257 for ( ; i < val.bv_len; i++ ) {
1258 if ( val.bv_val[ i ] == '"' ) {
1259 break;
1260 }
1261
1262 if ( !ASCII_HEX( val.bv_val[ i ] ) ) {
1263 return LDAP_INVALID_SYNTAX;
1264 }
1265 }
1266
1267 if ( val.bv_val[ i ] != '"' ) {
1268 return LDAP_INVALID_SYNTAX;
1269 }
1270
1271 valueLen = i - valueStart;
1272 if ( (valueLen/2)*2 != valueLen ) {
1273 return LDAP_INVALID_SYNTAX;
1274 }
1275
1276 for ( i++; i < val.bv_len; i++ ) {
1277 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1278 break;
1279 }
1280 }
1281
1282 if ( i == val.bv_len ) {
1283 return LDAP_SUCCESS;
1284 }
1285 }
1286
1287 return LDAP_INVALID_SYNTAX;
1288 }
1289
1290 static int
1291 accesslog_ctrls(
1292 LDAPControl **ctrls,
1293 BerVarray *valsp,
1294 BerVarray *nvalsp,
1295 void *memctx )
1296 {
1297 long i, rc = 0;
1298
1299 assert( valsp != NULL );
1300 assert( ctrls != NULL );
1301
1302 *valsp = NULL;
1303 *nvalsp = NULL;
1304
1305 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1306 struct berval idx,
1307 oid,
1308 noid,
1309 bv;
1310 char *ptr,
1311 buf[ 32 ];
1312
1313 if ( ctrls[ i ]->ldctl_oid == NULL ) {
1314 return LDAP_PROTOCOL_ERROR;
1315 }
1316
1317 idx.bv_len = snprintf( buf, sizeof( buf ), "{%ld}", i );
1318 idx.bv_val = buf;
1319
1320 ber_str2bv( ctrls[ i ]->ldctl_oid, 0, 0, &oid );
1321 noid.bv_len = idx.bv_len + oid.bv_len;
1322 ptr = noid.bv_val = ber_memalloc_x( noid.bv_len + 1, memctx );
1323 ptr = lutil_strcopy( ptr, idx.bv_val );
1324 ptr = lutil_strcopy( ptr, oid.bv_val );
1325
1326 bv.bv_len = idx.bv_len + STRLENOF( "{}" ) + oid.bv_len;
1327
1328 if ( ctrls[ i ]->ldctl_iscritical ) {
1329 bv.bv_len += STRLENOF( " criticality TRUE" );
1330 }
1331
1332 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
1333 bv.bv_len += STRLENOF( " controlValue \"\"" )
1334 + 2 * ctrls[ i ]->ldctl_value.bv_len;
1335 }
1336
1337 ptr = bv.bv_val = ber_memalloc_x( bv.bv_len + 1, memctx );
1338 if ( ptr == NULL ) {
1339 ber_bvarray_free( *valsp );
1340 *valsp = NULL;
1341 ber_bvarray_free( *nvalsp );
1342 *nvalsp = NULL;
1343 return LDAP_OTHER;
1344 }
1345
1346 ptr = lutil_strcopy( ptr, idx.bv_val );
1347
1348 *ptr++ = '{' /*}*/ ;
1349 ptr = lutil_strcopy( ptr, oid.bv_val );
1350
1351 if ( ctrls[ i ]->ldctl_iscritical ) {
1352 ptr = lutil_strcopy( ptr, " criticality TRUE" );
1353 }
1354
1355 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
1356 ber_len_t j;
1357
1358 ptr = lutil_strcopy( ptr, " controlValue \"" );
1359 for ( j = 0; j < ctrls[ i ]->ldctl_value.bv_len; j++ ) {
1360 *ptr++ = SLAP_ESCAPE_HI(ctrls[ i ]->ldctl_value.bv_val[ j ]);
1361 *ptr++ = SLAP_ESCAPE_LO(ctrls[ i ]->ldctl_value.bv_val[ j ]);
1362 }
1363
1364 *ptr++ = '"';
1365 }
1366
1367 *ptr++ = '}';
1368 *ptr = '\0';
1369
1370 ber_bvarray_add_x( valsp, &bv, memctx );
1371 ber_bvarray_add_x( nvalsp, &noid, memctx );
1372 }
1373
1374 return rc;
1375
1376 }
1377
1378 static Entry *accesslog_entry( Operation *op, SlapReply *rs,
1379 log_info *li, int logop, Operation *op2 ) {
1380
1381 char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
1382 char nrdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
1383
1384 struct berval rdn, nrdn, timestamp, ntimestamp, bv;
1385 slap_verbmasks *lo = logops+logop+EN_OFFSET;
1386
1387 Entry *e = entry_alloc();
1388
1389 strcpy( rdnbuf, RDNEQ );
1390 rdn.bv_val = rdnbuf;
1391 strcpy( nrdnbuf, RDNEQ );
1392 nrdn.bv_val = nrdnbuf;
1393
1394 timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
1395 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
1396 slap_timestamp( &op->o_time, &timestamp );
1397 snprintf( timestamp.bv_val + timestamp.bv_len-1, sizeof(".123456Z"), ".%06dZ", op->o_tincr );
1398 timestamp.bv_len += STRLENOF(".123456");
1399
1400 rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
1401 ad_reqStart->ad_type->sat_equality->smr_normalize(
1402 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, ad_reqStart->ad_type->sat_syntax,
1403 ad_reqStart->ad_type->sat_equality, &timestamp, &ntimestamp,
1404 op->o_tmpmemctx );
1405
1406 strcpy( nrdn.bv_val + STRLENOF(RDNEQ), ntimestamp.bv_val );
1407 nrdn.bv_len = STRLENOF(RDNEQ)+ntimestamp.bv_len;
1408 build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
1409 build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &nrdn, NULL );
1410
1411 attr_merge_one( e, slap_schema.si_ad_objectClass,
1412 &log_ocs[logop]->soc_cname, NULL );
1413 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1414 &log_ocs[logop]->soc_cname, NULL );
1415 attr_merge_one( e, ad_reqStart, &timestamp, &ntimestamp );
1416 op->o_tmpfree( ntimestamp.bv_val, op->o_tmpmemctx );
1417
1418 slap_op_time( &op2->o_time, &op2->o_tincr );
1419
1420 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
1421 slap_timestamp( &op2->o_time, &timestamp );
1422 snprintf( timestamp.bv_val + timestamp.bv_len-1, sizeof(".123456Z"), ".%06dZ", op2->o_tincr );
1423 timestamp.bv_len += STRLENOF(".123456");
1424
1425 attr_merge_normalize_one( e, ad_reqEnd, &timestamp, op->o_tmpmemctx );
1426
1427 /* Exops have OID appended */
1428 if ( logop == LOG_EN_EXTENDED ) {
1429 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
1430 bv.bv_val = ch_malloc( bv.bv_len + 1 );
1431 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
1432 bv.bv_val[lo->word.bv_len] = '{';
1433 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
1434 op->ore_reqoid.bv_len );
1435 bv.bv_val[bv.bv_len-1] = '}';
1436 bv.bv_val[bv.bv_len] = '\0';
1437 attr_merge_one( e, ad_reqType, &bv, NULL );
1438 } else {
1439 attr_merge_one( e, ad_reqType, &lo->word, NULL );
1440 }
1441
1442 rdn.bv_len = snprintf( rdn.bv_val, sizeof( rdnbuf ), "%lu", op->o_connid );
1443 if ( rdn.bv_len < sizeof( rdnbuf ) ) {
1444 attr_merge_one( e, ad_reqSession, &rdn, NULL );
1445 } /* else? */
1446
1447 if ( BER_BVISNULL( &op->o_dn ) ) {
1448 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
1449 (struct berval *)&slap_empty_bv );
1450 } else {
1451 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
1452 }
1453
1454 /* FIXME: need to add reqControls and reqRespControls */
1455 if ( op->o_ctrls ) {
1456 BerVarray vals = NULL,
1457 nvals = NULL;
1458
1459 if ( accesslog_ctrls( op->o_ctrls, &vals, &nvals,
1460 op->o_tmpmemctx ) == LDAP_SUCCESS && vals )
1461 {
1462 attr_merge( e, ad_reqControls, vals, nvals );
1463 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1464 ber_bvarray_free_x( nvals, op->o_tmpmemctx );
1465 }
1466 }
1467
1468 if ( rs->sr_ctrls ) {
1469 BerVarray vals = NULL,
1470 nvals = NULL;
1471
1472 if ( accesslog_ctrls( rs->sr_ctrls, &vals, &nvals,
1473 op->o_tmpmemctx ) == LDAP_SUCCESS && vals )
1474 {
1475 attr_merge( e, ad_reqRespControls, vals, nvals );
1476 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1477 ber_bvarray_free_x( nvals, op->o_tmpmemctx );
1478 }
1479
1480 }
1481
1482 return e;
1483 }
1484
1485 static struct berval scopes[] = {
1486 BER_BVC("base"),
1487 BER_BVC("one"),
1488 BER_BVC("sub"),
1489 BER_BVC("subord")
1490 };
1491
1492 static struct berval derefs[] = {
1493 BER_BVC("never"),
1494 BER_BVC("searching"),
1495 BER_BVC("finding"),
1496 BER_BVC("always")
1497 };
1498
1499 static struct berval simple = BER_BVC("SIMPLE");
1500
1501 static void accesslog_val2val(AttributeDescription *ad, struct berval *val,
1502 char c_op, struct berval *dst) {
1503 char *ptr;
1504
1505 dst->bv_len = ad->ad_cname.bv_len + val->bv_len + 2;
1506 if ( c_op ) dst->bv_len++;
1507
1508 dst->bv_val = ch_malloc( dst->bv_len+1 );
1509
1510 ptr = lutil_strcopy( dst->bv_val, ad->ad_cname.bv_val );
1511 *ptr++ = ':';
1512 if ( c_op )
1513 *ptr++ = c_op;
1514 *ptr++ = ' ';
1515 AC_MEMCPY( ptr, val->bv_val, val->bv_len );
1516 dst->bv_val[dst->bv_len] = '\0';
1517 }
1518
1519 static int
1520 accesslog_op2logop( Operation *op )
1521 {
1522 switch ( op->o_tag ) {
1523 case LDAP_REQ_ADD: return LOG_EN_ADD;
1524 case LDAP_REQ_DELETE: return LOG_EN_DELETE;
1525 case LDAP_REQ_MODIFY: return LOG_EN_MODIFY;
1526 case LDAP_REQ_MODRDN: return LOG_EN_MODRDN;
1527 case LDAP_REQ_COMPARE: return LOG_EN_COMPARE;
1528 case LDAP_REQ_SEARCH: return LOG_EN_SEARCH;
1529 case LDAP_REQ_BIND: return LOG_EN_BIND;
1530 case LDAP_REQ_EXTENDED: return LOG_EN_EXTENDED;
1531 default: /* unknown operation type */
1532 break;
1533 } /* Unbind and Abandon never reach here */
1534 return LOG_EN_UNKNOWN;
1535 }
1536
1537 static int
1538 accesslog_response(Operation *op, SlapReply *rs)
1539 {
1540 slap_callback *sc = op->o_callback;
1541 slap_overinst *on = (slap_overinst *)sc->sc_private;
1542 log_info *li = on->on_bi.bi_private;
1543 Attribute *a, *last_attr;
1544 Modifications *m;
1545 struct berval *b, uuid = BER_BVNULL;
1546 int i, success;
1547 int logop;
1548 slap_verbmasks *lo;
1549 Entry *e = NULL, *old = NULL, *e_uuid = NULL;
1550 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE+8];
1551 struct berval bv;
1552 char *ptr;
1553 BerVarray vals;
1554 Operation op2 = {0};
1555 SlapReply rs2 = {REP_RESULT};
1556 char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
1557
1558 /* ITS#9051 Make sure we only remove the callback on a final response */
1559 if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED &&
1560 rs->sr_type != REP_SASL )
1561 return SLAP_CB_CONTINUE;
1562
1563 op->o_callback = sc->sc_next;
1564 op->o_tmpfree( sc, op->o_tmpmemctx );
1565
1566 logop = accesslog_op2logop( op );
1567 lo = logops+logop+EN_OFFSET;
1568
1569 /* can't do anything if logDB isn't open */
1570 if ( !li->li_db || !SLAP_DBOPEN( li->li_db ) ) {
1571 goto skip;
1572 }
1573
1574 /* These internal ops are not logged */
1575 if ( op->o_dont_replicate )
1576 goto skip;
1577
1578 /*
1579 * ITS#9051 Technically LDAP_REFERRAL and LDAP_SASL_BIND_IN_PROGRESS
1580 * are not errors, but they aren't really success either
1581 */
1582 success = rs->sr_err == LDAP_SUCCESS ||
1583 rs->sr_err == LDAP_COMPARE_TRUE ||
1584 rs->sr_err == LDAP_COMPARE_FALSE;
1585 if ( li->li_success && !success )
1586 goto skip;
1587
1588 if ( !( li->li_ops & lo->mask ) ) {
1589 log_base *lb;
1590
1591 i = 0;
1592 for ( lb = li->li_bases; lb; lb=lb->lb_next )
1593 if (( lb->lb_ops & lo->mask ) && dnIsSuffix( &op->o_req_ndn, &lb->lb_base )) {
1594 i = 1;
1595 break;
1596 }
1597 if ( !i )
1598 goto skip;
1599 }
1600
1601 op2.o_hdr = op->o_hdr;
1602 op2.o_tag = LDAP_REQ_ADD;
1603 op2.o_bd = li->li_db;
1604 op2.o_csn.bv_val = csnbuf;
1605 op2.o_csn.bv_len = sizeof(csnbuf);
1606
1607 if ( !( lo->mask & LOG_OP_WRITES ) ) {
1608 ldap_pvt_thread_mutex_lock( &li->li_op_rmutex );
1609 }
1610 if ( SLAP_LASTMOD( li->li_db ) ) {
1611 /*
1612 * Make sure we have a CSN before we release li_op_rmutex to preserve
1613 * ordering
1614 */
1615 if ( !success || BER_BVISEMPTY( &op->o_csn ) ) {
1616 slap_get_csn( &op2, &op2.o_csn, 1 );
1617 } else {
1618 if ( !( lo->mask & LOG_OP_WRITES ) ) {
1619 Debug( LDAP_DEBUG_ANY, "%s accesslog_response: "
1620 "the op had a CSN assigned, if you're replicating the "
1621 "accesslog at %s, you might lose changes\n",
1622 op->o_log_prefix, li->li_db_suffix.bv_val );
1623 assert(0);
1624 }
1625 slap_queue_csn( &op2, &op->o_csn );
1626 }
1627 }
1628
1629 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
1630 old = li->li_old;
1631 uuid = li->li_uuid;
1632 li->li_old = NULL;
1633 BER_BVZERO( &li->li_uuid );
1634 ldap_pvt_thread_mutex_unlock( &li->li_op_rmutex );
1635
1636 e = accesslog_entry( op, rs, li, logop, &op2 );
1637
1638 if ( !BER_BVISNULL( &op->o_req_ndn ))
1639 attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
1640
1641 if ( rs->sr_text ) {
1642 ber_str2bv( rs->sr_text, 0, 0, &bv );
1643 attr_merge_normalize_one( e, ad_reqMessage, &bv, op->o_tmpmemctx );
1644 }
1645 bv.bv_len = snprintf( timebuf, sizeof( timebuf ), "%d", rs->sr_err );
1646 if ( bv.bv_len < sizeof( timebuf ) ) {
1647 bv.bv_val = timebuf;
1648 attr_merge_one( e, ad_reqResult, &bv, NULL );
1649 }
1650
1651 last_attr = attr_find( e->e_attrs, ad_reqResult );
1652
1653 e_uuid = old;
1654 switch( logop ) {
1655 case LOG_EN_ADD:
1656 case LOG_EN_DELETE: {
1657 char c_op;
1658 Entry *e2;
1659
1660 if ( logop == LOG_EN_ADD ) {
1661 e2 = op->ora_e;
1662 e_uuid = op->ora_e;
1663 c_op = '+';
1664
1665 } else {
1666 if ( !old )
1667 break;
1668 e2 = old;
1669 c_op = 0;
1670 }
1671 /* count all the vals */
1672 i = 0;
1673 for ( a=e2->e_attrs; a; a=a->a_next ) {
1674 i += a->a_numvals;
1675 }
1676 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1677 i = 0;
1678 for ( a=e2->e_attrs; a; a=a->a_next ) {
1679 if ( a->a_vals ) {
1680 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1681 accesslog_val2val( a->a_desc, b, c_op, &vals[i] );
1682 }
1683 }
1684 }
1685 vals[i].bv_val = NULL;
1686 vals[i].bv_len = 0;
1687 a = attr_alloc( logop == LOG_EN_ADD ? ad_reqMod : ad_reqOld );
1688 a->a_numvals = i;
1689 a->a_vals = vals;
1690 a->a_nvals = vals;
1691 last_attr->a_next = a;
1692 break;
1693 }
1694
1695 case LOG_EN_MODRDN:
1696 case LOG_EN_MODIFY:
1697 /* count all the mods + attributes (ITS#6545) */
1698 i = 0;
1699 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1700 if ( m->sml_values ) {
1701 i += m->sml_numvals;
1702 } else if ( m->sml_op == LDAP_MOD_DELETE ||
1703 m->sml_op == SLAP_MOD_SOFTDEL ||
1704 m->sml_op == LDAP_MOD_REPLACE )
1705 {
1706 i++;
1707 }
1708 if ( m->sml_next && m->sml_desc == m->sml_next->sml_desc ) {
1709 i++;
1710 }
1711 }
1712 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1713 i = 0;
1714
1715 /* init flags on old entry */
1716 if ( old ) {
1717 for ( a = old->e_attrs; a; a = a->a_next ) {
1718 log_attr *la;
1719 a->a_flags = 0;
1720
1721 /* look for attrs that are always logged */
1722 for ( la = li->li_oldattrs; la; la = la->next ) {
1723 if ( a->a_desc == la->attr ) {
1724 a->a_flags = 1;
1725 }
1726 }
1727 }
1728 }
1729
1730 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1731 /* Mark this attribute as modified */
1732 if ( old ) {
1733 a = attr_find( old->e_attrs, m->sml_desc );
1734 if ( a ) {
1735 a->a_flags = 1;
1736 }
1737 }
1738
1739 /* don't log the RDN mods; they're explicitly logged later */
1740 if ( logop == LOG_EN_MODRDN &&
1741 ( m->sml_op == SLAP_MOD_SOFTADD ||
1742 m->sml_op == LDAP_MOD_DELETE ) )
1743 {
1744 continue;
1745 }
1746
1747 if ( m->sml_values ) {
1748 for ( b = m->sml_values; !BER_BVISNULL( b ); b++, i++ ) {
1749 char c_op;
1750
1751 switch ( m->sml_op ) {
1752 case LDAP_MOD_ADD: /* FALLTHRU */
1753 case SLAP_MOD_SOFTADD: c_op = '+'; break;
1754 case LDAP_MOD_DELETE: /* FALLTHRU */
1755 case SLAP_MOD_SOFTDEL: c_op = '-'; break;
1756 case LDAP_MOD_REPLACE: c_op = '='; break;
1757 case LDAP_MOD_INCREMENT: c_op = '#'; break;
1758
1759 /* unknown op. there shouldn't be any of these. we
1760 * don't know what to do with it, but we shouldn't just
1761 * ignore it.
1762 */
1763 default: c_op = '?'; break;
1764 }
1765 accesslog_val2val( m->sml_desc, b, c_op, &vals[i] );
1766 }
1767 } else if ( m->sml_op == LDAP_MOD_DELETE ||
1768 m->sml_op == SLAP_MOD_SOFTDEL ||
1769 m->sml_op == LDAP_MOD_REPLACE )
1770 {
1771 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
1772 vals[i].bv_val = ch_malloc( vals[i].bv_len + 1 );
1773 ptr = lutil_strcopy( vals[i].bv_val,
1774 m->sml_desc->ad_cname.bv_val );
1775 *ptr++ = ':';
1776 if ( m->sml_op == LDAP_MOD_DELETE || m->sml_op == SLAP_MOD_SOFTDEL ) {
1777 *ptr++ = '-';
1778 } else {
1779 *ptr++ = '=';
1780 }
1781 *ptr = '\0';
1782 i++;
1783 }
1784 /* ITS#6545: when the same attribute is edited multiple times,
1785 * record the transition */
1786 if ( m->sml_next && m->sml_desc == m->sml_next->sml_desc &&
1787 m->sml_op == m->sml_next->sml_op ) {
1788 ber_str2bv( ":", STRLENOF(":"), 1, &vals[i] );
1789 i++;
1790 }
1791 }
1792
1793 if ( i > 0 ) {
1794 BER_BVZERO( &vals[i] );
1795 a = attr_alloc( ad_reqMod );
1796 a->a_numvals = i;
1797 a->a_vals = vals;
1798 a->a_nvals = vals;
1799 last_attr->a_next = a;
1800 last_attr = a;
1801
1802 } else {
1803 ch_free( vals );
1804 }
1805
1806 if ( old ) {
1807 /* count all the vals */
1808 i = 0;
1809 for ( a = old->e_attrs; a != NULL; a = a->a_next ) {
1810 if ( a->a_vals && a->a_flags ) {
1811 i += a->a_numvals;
1812 }
1813 }
1814 if ( i ) {
1815 vals = ch_malloc( (i + 1) * sizeof( struct berval ) );
1816 i = 0;
1817 for ( a=old->e_attrs; a; a=a->a_next ) {
1818 if ( a->a_vals && a->a_flags ) {
1819 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1820 accesslog_val2val( a->a_desc, b, 0, &vals[i] );
1821 }
1822 }
1823 }
1824 vals[i].bv_val = NULL;
1825 vals[i].bv_len = 0;
1826 a = attr_alloc( ad_reqOld );
1827 a->a_numvals = i;
1828 a->a_vals = vals;
1829 a->a_nvals = vals;
1830 last_attr->a_next = a;
1831 }
1832 }
1833 if ( logop == LOG_EN_MODIFY ) {
1834 break;
1835 }
1836
1837 /* Now log the actual modRDN info */
1838 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
1839 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
1840 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1841 NULL );
1842 if ( op->orr_newSup ) {
1843 attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
1844 }
1845 attr_merge_one( e, ad_reqNewDN, &op->orr_newDN, &op->orr_nnewDN );
1846 break;
1847
1848 case LOG_EN_COMPARE:
1849 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
1850 op->orc_ava->aa_value.bv_len;
1851 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
1852 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
1853 *ptr++ = '=';
1854 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
1855 bv.bv_val[bv.bv_len] = '\0';
1856 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
1857 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1858 break;
1859
1860 case LOG_EN_SEARCH:
1861 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
1862 attr_merge_one( e, ad_reqDerefAliases, &derefs[op->ors_deref], NULL );
1863 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
1864 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1865 NULL );
1866 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
1867 attr_merge_normalize_one( e, ad_reqFilter, &op->ors_filterstr, op->o_tmpmemctx );
1868 if ( op->ors_attrs ) {
1869 int j;
1870 /* count them */
1871 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1872 ;
1873 vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
1874 op->o_tmpmemctx );
1875 for (i=0, j=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++) {
1876 if (!BER_BVISEMPTY(&op->ors_attrs[i].an_name)) {
1877 vals[j] = op->ors_attrs[i].an_name;
1878 j++;
1879 }
1880 }
1881 BER_BVZERO(&vals[j]);
1882 attr_merge_normalize( e, ad_reqAttr, vals, op->o_tmpmemctx );
1883 op->o_tmpfree( vals, op->o_tmpmemctx );
1884 }
1885 bv.bv_val = timebuf;
1886 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", rs->sr_nentries );
1887 if ( bv.bv_len < sizeof( timebuf ) ) {
1888 attr_merge_one( e, ad_reqEntries, &bv, NULL );
1889 } /* else? */
1890
1891 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->ors_tlimit );
1892 if ( bv.bv_len < sizeof( timebuf ) ) {
1893 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
1894 } /* else? */
1895
1896 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->ors_slimit );
1897 if ( bv.bv_len < sizeof( timebuf ) ) {
1898 attr_merge_one( e, ad_reqSizeLimit, &bv, NULL );
1899 } /* else? */
1900 break;
1901
1902 case LOG_EN_BIND:
1903 bv.bv_val = timebuf;
1904 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->o_protocol );
1905 if ( bv.bv_len < sizeof( timebuf ) ) {
1906 attr_merge_one( e, ad_reqVersion, &bv, NULL );
1907 } /* else? */
1908 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
1909 attr_merge_normalize_one( e, ad_reqMethod, &simple, op->o_tmpmemctx );
1910 } else {
1911 bv.bv_len = STRLENOF("SASL()") + op->orb_mech.bv_len;
1912 bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
1913 ptr = lutil_strcopy( bv.bv_val, "SASL(" );
1914 ptr = lutil_strcopy( ptr, op->orb_mech.bv_val );
1915 *ptr++ = ')';
1916 *ptr = '\0';
1917 attr_merge_normalize_one( e, ad_reqMethod, &bv, op->o_tmpmemctx );
1918 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1919 }
1920
1921 break;
1922
1923 case LOG_EN_EXTENDED:
1924 if ( op->ore_reqdata ) {
1925 attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
1926 }
1927 break;
1928
1929 case LOG_EN_UNKNOWN:
1930 /* we don't know its parameters, don't add any */
1931 break;
1932 }
1933
1934 if ( e_uuid || !BER_BVISNULL( &uuid ) ) {
1935 struct berval *pbv = NULL;
1936
1937 if ( !BER_BVISNULL( &uuid ) ) {
1938 pbv = &uuid;
1939
1940 } else {
1941 a = attr_find( e_uuid->e_attrs, slap_schema.si_ad_entryUUID );
1942 if ( a ) {
1943 pbv = &a->a_vals[0];
1944 }
1945 }
1946
1947 if ( pbv ) {
1948 attr_merge_normalize_one( e, ad_reqEntryUUID, pbv, op->o_tmpmemctx );
1949 }
1950
1951 if ( !BER_BVISNULL( &uuid ) ) {
1952 ber_memfree( uuid.bv_val );
1953 BER_BVZERO( &uuid );
1954 }
1955 }
1956
1957 op2.o_dn = li->li_db->be_rootdn;
1958 op2.o_ndn = li->li_db->be_rootndn;
1959 op2.o_req_dn = e->e_name;
1960 op2.o_req_ndn = e->e_nname;
1961 op2.ora_e = e;
1962 op2.o_callback = &nullsc;
1963 /* contextCSN updates may still reach here */
1964 op2.o_dont_replicate = op->o_dont_replicate;
1965
1966 op2.o_bd->be_add( &op2, &rs2 );
1967 if ( rs2.sr_err != LDAP_SUCCESS ) {
1968 Debug( LDAP_DEBUG_SYNC, "%s accesslog_response: "
1969 "got result 0x%x adding log entry %s\n",
1970 op->o_log_prefix, rs2.sr_err, op2.o_req_dn.bv_val );
1971 }
1972 if ( e == op2.ora_e ) entry_free( e );
1973 e = NULL;
1974
1975 if ( ( lo->mask & LOG_OP_WRITES ) && !BER_BVISEMPTY( &op->o_csn ) ) {
1976 Modifications mod;
1977 int i, sid = slap_parse_csn_sid( &op->o_csn );
1978
1979 for ( i=0; i < li->li_numcsns; i++ ) {
1980 if ( sid <= li->li_sids[i] ) break;
1981 }
1982 if ( i >= li->li_numcsns || sid != li->li_sids[i] ) {
1983 /* SID not in minCSN set, add */
1984 struct berval bv[2];
1985
1986 Debug( LDAP_DEBUG_TRACE, "accesslog_response: "
1987 "adding minCSN %s\n",
1988 op->o_csn.bv_val );
1989 slap_insert_csn_sids( (struct sync_cookie *)&li->li_mincsn, i,
1990 sid, &op->o_csn );
1991
1992 op2.o_tag = LDAP_REQ_MODIFY;
1993 op2.o_req_dn = li->li_db->be_suffix[0];
1994 op2.o_req_ndn = li->li_db->be_nsuffix[0];
1995
1996 bv[0] = op->o_csn;
1997 BER_BVZERO( &bv[1] );
1998
1999 mod.sml_numvals = 1;
2000 mod.sml_values = bv;
2001 mod.sml_nvalues = bv;
2002 mod.sml_desc = ad_minCSN;
2003 mod.sml_op = LDAP_MOD_ADD;
2004 mod.sml_flags = SLAP_MOD_INTERNAL;
2005 mod.sml_next = NULL;
2006
2007 op2.orm_modlist = &mod;
2008 op2.orm_no_opattrs = 1;
2009
2010 Debug( LDAP_DEBUG_SYNC, "accesslog_response: "
2011 "adding a new csn=%s into minCSN\n",
2012 bv[0].bv_val );
2013 rs_reinit( &rs2, REP_RESULT );
2014 op2.o_bd->be_modify( &op2, &rs2 );
2015 if ( rs2.sr_err != LDAP_SUCCESS ) {
2016 Debug( LDAP_DEBUG_SYNC, "accesslog_response: "
2017 "got result 0x%x adding minCSN %s\n",
2018 rs2.sr_err, op->o_csn.bv_val );
2019 }
2020 } else if ( ber_bvcmp( &op->o_csn, &li->li_mincsn[i] ) < 0 ) {
2021 Debug( LDAP_DEBUG_ANY, "accesslog_response: "
2022 "csn=%s older than existing minCSN csn=%s for this sid\n",
2023 op->o_csn.bv_val, li->li_mincsn[i].bv_val );
2024 }
2025 }
2026
2027 done:
2028 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
2029 if ( old ) entry_free( old );
2030 return SLAP_CB_CONTINUE;
2031
2032 skip:
2033 if ( lo->mask & LOG_OP_WRITES ) {
2034 /* We haven't transitioned to li_log_mutex yet */
2035 ldap_pvt_thread_mutex_unlock( &li->li_op_rmutex );
2036 }
2037 return SLAP_CB_CONTINUE;
2038 }
2039
2040 static int
2041 accesslog_op_misc( Operation *op, SlapReply *rs )
2042 {
2043 slap_callback *sc;
2044 slap_verbmasks *lo;
2045 int logop;
2046
2047 logop = accesslog_op2logop( op );
2048 lo = logops+logop+EN_OFFSET;
2049
2050 /* ignore these internal reads */
2051 if (( lo->mask & LOG_OP_READS ) && op->o_do_not_cache ) {
2052 return SLAP_CB_CONTINUE;
2053 }
2054
2055 sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
2056 sc->sc_response = accesslog_response;
2057 sc->sc_private = op->o_bd->bd_info;
2058
2059 if ( op->o_callback ) {
2060 sc->sc_next = op->o_callback->sc_next;
2061 op->o_callback->sc_next = sc;
2062 } else {
2063 op->o_callback = sc;
2064 }
2065 return SLAP_CB_CONTINUE;
2066 }
2067
2068 static int
2069 accesslog_op_mod( Operation *op, SlapReply *rs )
2070 {
2071 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2072 log_info *li = on->on_bi.bi_private;
2073 slap_verbmasks *lo;
2074 slap_callback *cb;
2075 int logop;
2076
2077 /* These internal ops are not logged */
2078 if ( op->o_dont_replicate )
2079 return SLAP_CB_CONTINUE;
2080
2081 /* can't do anything if logDB isn't open */
2082 if ( !SLAP_DBOPEN( li->li_db ))
2083 return SLAP_CB_CONTINUE;
2084
2085 logop = accesslog_op2logop( op );
2086 lo = logops+logop+EN_OFFSET;
2087
2088 if ( !( li->li_ops & lo->mask )) {
2089 log_base *lb;
2090 int i = 0;
2091
2092 for ( lb = li->li_bases; lb; lb = lb->lb_next )
2093 if (( lb->lb_ops & lo->mask ) && dnIsSuffix( &op->o_req_ndn, &lb->lb_base )) {
2094 i = 1;
2095 break;
2096 }
2097 if ( !i )
2098 return SLAP_CB_CONTINUE;
2099 }
2100
2101 cb = op->o_tmpcalloc( 1, sizeof( slap_callback ), op->o_tmpmemctx );
2102 cb->sc_cleanup = accesslog_response;
2103 cb->sc_response = accesslog_response;
2104 cb->sc_private = on;
2105 cb->sc_next = op->o_callback;
2106 op->o_callback = cb;
2107
2108 ldap_pvt_thread_mutex_lock( &li->li_op_rmutex );
2109
2110 if ( li->li_oldf && ( op->o_tag == LDAP_REQ_DELETE ||
2111 op->o_tag == LDAP_REQ_MODIFY ||
2112 ( op->o_tag == LDAP_REQ_MODRDN && li->li_oldattrs )))
2113 {
2114 int rc;
2115 Entry *e;
2116
2117 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2118 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
2119 if ( e ) {
2120 if ( test_filter( op, e, li->li_oldf ) == LDAP_COMPARE_TRUE )
2121 li->li_old = entry_dup( e );
2122 be_entry_release_rw( op, e, 0 );
2123 }
2124 op->o_bd->bd_info = (BackendInfo *)on;
2125
2126 } else {
2127 int rc;
2128 Entry *e;
2129
2130 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2131 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
2132 if ( e ) {
2133 Attribute *a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
2134 if ( a ) {
2135 ber_dupbv( &li->li_uuid, &a->a_vals[0] );
2136 }
2137 be_entry_release_rw( op, e, 0 );
2138 }
2139 op->o_bd->bd_info = (BackendInfo *)on;
2140 }
2141 return SLAP_CB_CONTINUE;
2142 }
2143
2144 /* unbinds are broadcast to all backends; we only log it if this
2145 * backend was used for the original bind.
2146 */
2147 static int
2148 accesslog_unbind( Operation *op, SlapReply *rs )
2149 {
2150 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2151 log_info *li = on->on_bi.bi_private;
2152 Operation op2 = {};
2153 char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
2154 void *cids[SLAP_MAX_CIDS];
2155 SlapReply rs2 = {REP_RESULT};
2156 Entry *e;
2157
2158 if ( op->o_conn->c_authz_backend != on->on_info->oi_origdb )
2159 return SLAP_CB_CONTINUE;
2160
2161 if ( !( li->li_ops & LOG_OP_UNBIND ) ) {
2162 log_base *lb;
2163 int i = 0;
2164
2165 for ( lb = li->li_bases; lb; lb=lb->lb_next )
2166 if (( lb->lb_ops & LOG_OP_UNBIND ) && dnIsSuffix( &op->o_ndn, &lb->lb_base )) {
2167 i = 1;
2168 break;
2169 }
2170 if ( !i )
2171 return SLAP_CB_CONTINUE;
2172 }
2173
2174 op2.o_hdr = op->o_hdr;
2175 op2.o_tag = LDAP_REQ_ADD;
2176 op2.o_bd = li->li_db;
2177 op2.o_csn.bv_val = csnbuf;
2178 op2.o_csn.bv_len = sizeof(csnbuf);
2179
2180 ldap_pvt_thread_mutex_lock( &li->li_op_rmutex );
2181
2182 if ( SLAP_LASTMOD( li->li_db ) ) {
2183 /*
2184 * Make sure we have a CSN before we release li_op_rmutex to preserve
2185 * ordering
2186 */
2187 if ( BER_BVISEMPTY( &op->o_csn ) ) {
2188 slap_get_csn( &op2, &op2.o_csn, 1 );
2189 } else {
2190 Debug( LDAP_DEBUG_ANY, "%s accesslog_unbind: "
2191 "the op had a CSN assigned, if you're replicating the "
2192 "accesslog at %s, you might lose changes\n",
2193 op->o_log_prefix, li->li_db_suffix.bv_val );
2194 assert(0);
2195 op2.o_csn = op->o_csn;
2196 }
2197 }
2198 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
2199 ldap_pvt_thread_mutex_unlock( &li->li_op_rmutex );
2200
2201 e = accesslog_entry( op, rs, li, LOG_EN_UNBIND, &op2 );
2202 op2.o_dn = li->li_db->be_rootdn;
2203 op2.o_ndn = li->li_db->be_rootndn;
2204 op2.o_req_dn = e->e_name;
2205 op2.o_req_ndn = e->e_nname;
2206 op2.ora_e = e;
2207 op2.o_callback = &nullsc;
2208 op2.o_controls = cids;
2209 memset(cids, 0, sizeof( cids ));
2210
2211 op2.o_bd->be_add( &op2, &rs2 );
2212 if ( rs2.sr_err != LDAP_SUCCESS ) {
2213 Debug( LDAP_DEBUG_SYNC, "%s accesslog_unbind: "
2214 "got result 0x%x adding log entry %s\n",
2215 op->o_log_prefix, rs2.sr_err, op2.o_req_dn.bv_val );
2216 }
2217 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
2218
2219 if ( e == op2.ora_e )
2220 entry_free( e );
2221
2222 return SLAP_CB_CONTINUE;
2223 }
2224
2225 static int
2226 accesslog_abandon( Operation *op, SlapReply *rs )
2227 {
2228 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2229 log_info *li = on->on_bi.bi_private;
2230 Operation op2 = {0};
2231 void *cids[SLAP_MAX_CIDS];
2232 SlapReply rs2 = {REP_RESULT};
2233 Entry *e;
2234 char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
2235 char buf[64];
2236 struct berval bv;
2237
2238 if ( !op->o_time )
2239 return SLAP_CB_CONTINUE;
2240
2241 if ( !( li->li_ops & LOG_OP_ABANDON )) {
2242 log_base *lb;
2243 int i = 0;
2244
2245 for ( lb = li->li_bases; lb; lb=lb->lb_next )
2246 if (( lb->lb_ops & LOG_OP_ABANDON ) && dnIsSuffix( &op->o_ndn, &lb->lb_base )) {
2247 i = 1;
2248 break;
2249 }
2250 if ( !i )
2251 return SLAP_CB_CONTINUE;
2252 }
2253
2254 op2.o_hdr = op->o_hdr;
2255 op2.o_tag = LDAP_REQ_ADD;
2256 op2.o_bd = li->li_db;
2257 op2.o_csn.bv_val = csnbuf;
2258 op2.o_csn.bv_len = sizeof(csnbuf);
2259
2260 ldap_pvt_thread_mutex_lock( &li->li_op_rmutex );
2261 if ( SLAP_LASTMOD( li->li_db ) ) {
2262 /*
2263 * Make sure we have a CSN before we release li_op_rmutex to preserve
2264 * ordering
2265 */
2266 if ( BER_BVISEMPTY( &op->o_csn ) ) {
2267 slap_get_csn( &op2, &op2.o_csn, 1 );
2268 } else {
2269 Debug( LDAP_DEBUG_ANY, "%s accesslog_abandon: "
2270 "the op had a CSN assigned, if you're replicating the "
2271 "accesslog at %s, you might lose changes\n",
2272 op->o_log_prefix, li->li_db_suffix.bv_val );
2273 assert(0);
2274 op2.o_csn = op->o_csn;
2275 }
2276 }
2277 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
2278 ldap_pvt_thread_mutex_unlock( &li->li_op_rmutex );
2279
2280 e = accesslog_entry( op, rs, li, LOG_EN_ABANDON, &op2 );
2281 bv.bv_val = buf;
2282 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", op->orn_msgid );
2283 if ( bv.bv_len < sizeof( buf ) ) {
2284 attr_merge_one( e, ad_reqId, &bv, NULL );
2285 } /* else? */
2286
2287 op2.o_dn = li->li_db->be_rootdn;
2288 op2.o_ndn = li->li_db->be_rootndn;
2289 op2.o_req_dn = e->e_name;
2290 op2.o_req_ndn = e->e_nname;
2291 op2.ora_e = e;
2292 op2.o_callback = &nullsc;
2293 op2.o_controls = cids;
2294 memset(cids, 0, sizeof( cids ));
2295
2296 op2.o_bd->be_add( &op2, &rs2 );
2297 if ( rs2.sr_err != LDAP_SUCCESS ) {
2298 Debug( LDAP_DEBUG_SYNC, "%s accesslog_abandon: "
2299 "got result 0x%x adding log entry %s\n",
2300 op->o_log_prefix, rs2.sr_err, op2.o_req_dn.bv_val );
2301 }
2302 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
2303 if ( e == op2.ora_e )
2304 entry_free( e );
2305
2306 return SLAP_CB_CONTINUE;
2307 }
2308
2309 static int
2310 accesslog_operational( Operation *op, SlapReply *rs )
2311 {
2312 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2313 log_info *li = on->on_bi.bi_private;
2314
2315 if ( op->o_sync != SLAP_CONTROL_NONE )
2316 return SLAP_CB_CONTINUE;
2317
2318 if ( rs->sr_entry != NULL
2319 && dn_match( &op->o_bd->be_nsuffix[0], &rs->sr_entry->e_nname ) )
2320 {
2321 Attribute **ap;
2322
2323 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
2324 /* just count */ ;
2325
2326 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
2327 ad_inlist( ad_auditContext, rs->sr_attrs ) )
2328 {
2329 *ap = attr_alloc( ad_auditContext );
2330 attr_valadd( *ap,
2331 &li->li_db->be_suffix[0],
2332 &li->li_db->be_nsuffix[0], 1 );
2333 }
2334 }
2335
2336 return SLAP_CB_CONTINUE;
2337 }
2338
2339 static slap_overinst accesslog;
2340
2341 static int
2342 accesslog_db_init(
2343 BackendDB *be,
2344 ConfigReply *cr
2345 )
2346 {
2347 slap_overinst *on = (slap_overinst *)be->bd_info;
2348 log_info *li = ch_calloc(1, sizeof(log_info));
2349
2350 on->on_bi.bi_private = li;
2351 ldap_pvt_thread_mutex_recursive_init( &li->li_op_rmutex );
2352 ldap_pvt_thread_mutex_init( &li->li_log_mutex );
2353 return 0;
2354 }
2355
2356 static int
2357 accesslog_db_destroy(
2358 BackendDB *be,
2359 ConfigReply *cr
2360 )
2361 {
2362 slap_overinst *on = (slap_overinst *)be->bd_info;
2363 log_info *li = on->on_bi.bi_private;
2364 log_attr *la;
2365
2366 if ( li->li_oldf )
2367 filter_free( li->li_oldf );
2368 for ( la=li->li_oldattrs; la; la=li->li_oldattrs ) {
2369 li->li_oldattrs = la->next;
2370 ch_free( la );
2371 }
2372 if ( li->li_sids )
2373 ch_free( li->li_sids );
2374 if ( li->li_mincsn )
2375 ber_bvarray_free( li->li_mincsn );
2376 if ( li->li_db_suffix.bv_val )
2377 ch_free( li->li_db_suffix.bv_val );
2378 ldap_pvt_thread_mutex_destroy( &li->li_log_mutex );
2379 ldap_pvt_thread_mutex_destroy( &li->li_op_rmutex );
2380 free( li );
2381 return LDAP_SUCCESS;
2382 }
2383
2384 /* Create the logdb's root entry if it's missing, load mincsn */
2385 static void *
2386 accesslog_db_root(
2387 void *ctx,
2388 void *arg )
2389 {
2390 struct re_s *rtask = arg;
2391 slap_overinst *on = rtask->arg;
2392 log_info *li = on->on_bi.bi_private;
2393
2394 Connection conn = {0};
2395 OperationBuffer opbuf;
2396 Operation *op;
2397
2398 Entry *e;
2399 int rc;
2400
2401 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
2402 connection_fake_init( &conn, &opbuf, ctx );
2403 op = &opbuf.ob_op;
2404 op->o_bd = li->li_db;
2405 op->o_dn = li->li_db->be_rootdn;
2406 op->o_ndn = li->li_db->be_rootndn;
2407 rc = be_entry_get_rw( op, li->li_db->be_nsuffix, NULL, NULL, 0, &e );
2408
2409 if ( e ) {
2410 Attribute *a = attr_find( e->e_attrs, ad_minCSN );
2411 if ( !a ) {
2412 /* TODO: find the lowest CSN we are safe to put in */
2413 a = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
2414 if ( a ) {
2415 SlapReply rs = {REP_RESULT};
2416 Modifications mod;
2417 BackendDB db = *li->li_db;
2418
2419 op->o_bd = &db;
2420
2421 mod.sml_numvals = a->a_numvals;
2422 mod.sml_values = a->a_vals;
2423 mod.sml_nvalues = a->a_nvals;
2424 mod.sml_desc = ad_minCSN;
2425 mod.sml_op = LDAP_MOD_REPLACE;
2426 mod.sml_flags = SLAP_MOD_INTERNAL;
2427 mod.sml_next = NULL;
2428
2429 op->o_tag = LDAP_REQ_MODIFY;
2430 op->o_req_dn = e->e_name;
2431 op->o_req_ndn = e->e_nname;
2432 op->o_callback = &nullsc;
2433 SLAP_DBFLAGS( op->o_bd ) |= SLAP_DBFLAG_NOLASTMOD;
2434
2435 Debug( LDAP_DEBUG_SYNC, "accesslog_db_root: "
2436 "setting up minCSN with %d values\n",
2437 a->a_numvals );
2438
2439 op->orm_modlist = &mod;
2440 op->orm_no_opattrs = 1;
2441 rc = op->o_bd->be_modify( op, &rs );
2442 }
2443 }
2444 if ( a ) {
2445 ber_bvarray_dup_x( &li->li_mincsn, a->a_vals, NULL );
2446 li->li_numcsns = a->a_numvals;
2447 li->li_sids = slap_parse_csn_sids( li->li_mincsn, li->li_numcsns, NULL );
2448 slap_sort_csn_sids( li->li_mincsn, li->li_sids, li->li_numcsns, NULL );
2449 }
2450 be_entry_release_rw( op, e, 0 );
2451 } else {
2452 SlapReply rs = {REP_RESULT};
2453 struct berval rdn, nrdn, attr;
2454 char *ptr;
2455 AttributeDescription *ad = NULL;
2456 const char *text = NULL;
2457 Entry *e_ctx;
2458 BackendDB db;
2459
2460 e = entry_alloc();
2461 ber_dupbv( &e->e_name, li->li_db->be_suffix );
2462 ber_dupbv( &e->e_nname, li->li_db->be_nsuffix );
2463
2464 attr_merge_one( e, slap_schema.si_ad_objectClass,
2465 &log_container->soc_cname, NULL );
2466
2467 dnRdn( &e->e_name, &rdn );
2468 dnRdn( &e->e_nname, &nrdn );
2469 ptr = ber_bvchr( &rdn, '=' );
2470
2471 assert( ptr != NULL );
2472
2473 attr.bv_val = rdn.bv_val;
2474 attr.bv_len = ptr - rdn.bv_val;
2475
2476 slap_bv2ad( &attr, &ad, &text );
2477
2478 rdn.bv_val = ptr+1;
2479 rdn.bv_len -= attr.bv_len + 1;
2480 ptr = ber_bvchr( &nrdn, '=' );
2481 nrdn.bv_len -= ptr - nrdn.bv_val + 1;
2482 nrdn.bv_val = ptr+1;
2483 attr_merge_one( e, ad, &rdn, &nrdn );
2484
2485 /* Get contextCSN from main DB */
2486 op->o_bd = on->on_info->oi_origdb;
2487 rc = be_entry_get_rw( op, op->o_bd->be_nsuffix, NULL,
2488 slap_schema.si_ad_contextCSN, 0, &e_ctx );
2489
2490 if ( e_ctx ) {
2491 Attribute *a;
2492
2493 a = attr_find( e_ctx->e_attrs, slap_schema.si_ad_contextCSN );
2494 if ( a ) {
2495 /* FIXME: contextCSN could have multiple values!
2496 * should select the one with the server's SID */
2497 attr_merge_one( e, slap_schema.si_ad_entryCSN,
2498 &a->a_vals[0], &a->a_nvals[0] );
2499 attr_merge( e, a->a_desc, a->a_vals, a->a_nvals );
2500
2501 /* Populate minCSN */
2502 attr_merge( e, ad_minCSN, a->a_vals, a->a_nvals );
2503 ber_bvarray_dup_x( &li->li_mincsn, a->a_vals, NULL );
2504 li->li_numcsns = a->a_numvals;
2505 li->li_sids = slap_parse_csn_sids( li->li_mincsn, li->li_numcsns, NULL );
2506 slap_sort_csn_sids( li->li_mincsn, li->li_sids, li->li_numcsns, NULL );
2507 }
2508 be_entry_release_rw( op, e_ctx, 0 );
2509 }
2510 db = *li->li_db;
2511 op->o_bd = &db;
2512
2513 op->o_tag = LDAP_REQ_ADD;
2514 op->ora_e = e;
2515 op->o_req_dn = e->e_name;
2516 op->o_req_ndn = e->e_nname;
2517 op->o_callback = &nullsc;
2518 SLAP_DBFLAGS( op->o_bd ) |= SLAP_DBFLAG_NOLASTMOD;
2519 rc = op->o_bd->be_add( op, &rs );
2520 if ( rs.sr_err != LDAP_SUCCESS ) {
2521 Debug( LDAP_DEBUG_SYNC, "%s accesslog_db_root: "
2522 "got result 0x%x adding log root entry %s\n",
2523 op->o_log_prefix, rs.sr_err, op->o_req_dn.bv_val );
2524 }
2525 if ( e == op->ora_e )
2526 entry_free( e );
2527 }
2528 li->li_open = 1;
2529 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
2530
2531 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2532 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
2533 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
2534
2535 if ( li->li_age && li->li_cycle ) {
2536 assert( li->li_task == NULL );
2537 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
2538 li->li_cycle, accesslog_purge, li,
2539 "accesslog_purge", li->li_db->be_suffix[0].bv_val );
2540 }
2541 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2542
2543 return NULL;
2544 }
2545
2546 static int
2547 accesslog_db_open(
2548 BackendDB *be,
2549 ConfigReply *cr
2550 )
2551 {
2552 slap_overinst *on = (slap_overinst *)be->bd_info;
2553 log_info *li = on->on_bi.bi_private;
2554
2555
2556 if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
2557 li->li_db = select_backend( &li->li_db_suffix, 0 );
2558 ch_free( li->li_db_suffix.bv_val );
2559 BER_BVZERO( &li->li_db_suffix );
2560 }
2561 if ( li->li_db == NULL ) {
2562 Debug( LDAP_DEBUG_ANY,
2563 "accesslog: \"logdb <suffix>\" missing or invalid.\n" );
2564 return 1;
2565 }
2566 if ( li->li_db->bd_self == be->bd_self ) {
2567 Debug( LDAP_DEBUG_ANY,
2568 "accesslog: \"logdb <suffix>\" is this database, cannot log to itself.\n" );
2569 return 1;
2570 }
2571
2572 if ( slapMode & SLAP_TOOL_MODE )
2573 return 0;
2574
2575 if ( BER_BVISEMPTY( &li->li_db->be_rootndn )) {
2576 ber_dupbv( &li->li_db->be_rootdn, li->li_db->be_suffix );
2577 ber_dupbv( &li->li_db->be_rootndn, li->li_db->be_nsuffix );
2578 }
2579
2580 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2581 ldap_pvt_runqueue_insert( &slapd_rq, 3600, accesslog_db_root, on,
2582 "accesslog_db_root", li->li_db->be_suffix[0].bv_val );
2583 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2584
2585 return 0;
2586 }
2587
2588 static int
2589 accesslog_db_close(
2590 BackendDB *be,
2591 ConfigReply *cr
2592 )
2593 {
2594 slap_overinst *on = (slap_overinst *)be->bd_info;
2595 log_info *li = on->on_bi.bi_private;
2596 struct re_s *re = li->li_task;
2597
2598 li->li_open = 0;
2599
2600 if ( re ) {
2601 li->li_task = NULL;
2602 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2603 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
2604 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
2605 ldap_pvt_runqueue_remove( &slapd_rq, re );
2606 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2607 }
2608
2609 return 0;
2610 }
2611
2612 enum { start = 0 };
2613
2614 static int
2615 check_rdntime_syntax (struct berval *val,
2616 int *parts,
2617 struct berval *fraction)
2618 {
2619 /*
2620 * GeneralizedTime YYYYmmddHH[MM[SS]][(./,)d...](Z|(+/-)HH[MM])
2621 * GeneralizedTime supports leap seconds, UTCTime does not.
2622 */
2623 static const int ceiling[9] = { 100, 100, 12, 31, 24, 60, 60, 24, 60 };
2624 static const int mdays[2][12] = {
2625 /* non-leap years */
2626 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
2627 /* leap years */
2628 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
2629 };
2630 char *p, *e;
2631 int part, c, c1, c2, tzoffset, leapyear = 0;
2632
2633 p = val->bv_val;
2634 e = p + val->bv_len;
2635
2636 for (part = start; part < 7 && p < e; part++) {
2637 c1 = *p;
2638 if (!ASCII_DIGIT(c1)) {
2639 break;
2640 }
2641 p++;
2642 if (p == e) {
2643 return LDAP_INVALID_SYNTAX;
2644 }
2645 c = *p++;
2646 if (!ASCII_DIGIT(c)) {
2647 return LDAP_INVALID_SYNTAX;
2648 }
2649 c += c1 * 10 - '0' * 11;
2650 if ((part | 1) == 3) {
2651 --c;
2652 if (c < 0) {
2653 return LDAP_INVALID_SYNTAX;
2654 }
2655 }
2656 if (c >= ceiling[part]) {
2657 if (! (c == 60 && part == 6 && start == 0))
2658 return LDAP_INVALID_SYNTAX;
2659 }
2660 parts[part] = c;
2661 }
2662 if (part < 5 + start) {
2663 return LDAP_INVALID_SYNTAX;
2664 }
2665 for (; part < 9; part++) {
2666 parts[part] = 0;
2667 }
2668
2669 /* leapyear check for the Gregorian calendar (year>1581) */
2670 if (parts[parts[1] == 0 ? 0 : 1] % 4 == 0) {
2671 leapyear = 1;
2672 }
2673
2674 if (parts[3] >= mdays[leapyear][parts[2]]) {
2675 return LDAP_INVALID_SYNTAX;
2676 }
2677
2678 if (start == 0) {
2679 fraction->bv_val = p;
2680 fraction->bv_len = 0;
2681 if (p < e && (*p == '.' || *p == ',')) {
2682 char *end_num;
2683 while (++p < e && ASCII_DIGIT(*p)) {
2684 /* EMPTY */;
2685 }
2686 if (p - fraction->bv_val == 1) {
2687 return LDAP_INVALID_SYNTAX;
2688 }
2689
2690 #if 0 /* don't truncate trailing zeros */
2691 for (end_num = p; end_num[-1] == '0'; --end_num) {
2692 /* EMPTY */;
2693 }
2694 c = end_num - fraction->bv_val;
2695 #else
2696 c = p - fraction->bv_val;
2697 #endif
2698 if (c != 1) fraction->bv_len = c;
2699 }
2700 }
2701
2702 if (p == e) {
2703 /* no time zone */
2704 return start == 0 ? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;
2705 }
2706
2707 tzoffset = *p++;
2708 switch (tzoffset) {
2709 case 'Z':
2710 /* UTC */
2711 break;
2712 default:
2713 return LDAP_INVALID_SYNTAX;
2714 }
2715
2716 return p != e ? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;
2717 }
2718
2719 static int
2720 rdnTimestampValidate(
2721 Syntax *syntax,
2722 struct berval *in )
2723 {
2724 int parts[9];
2725 struct berval fraction;
2726 return check_rdntime_syntax(in, parts, &fraction);
2727 }
2728
2729 static int
2730 rdnTimestampNormalize(
2731 slap_mask_t usage,
2732 Syntax *syntax,
2733 MatchingRule *mr,
2734 struct berval *val,
2735 struct berval *normalized,
2736 void *ctx )
2737 {
2738 int parts[9], rc;
2739 unsigned int len;
2740 struct berval fraction;
2741
2742 rc = check_rdntime_syntax(val, parts, &fraction);
2743 if (rc != LDAP_SUCCESS) {
2744 return rc;
2745 }
2746
2747 len = STRLENOF("YYYYmmddHHMMSSZ") + fraction.bv_len;
2748 normalized->bv_val = slap_sl_malloc( len + 1, ctx );
2749 if ( BER_BVISNULL( normalized ) ) {
2750 return LBER_ERROR_MEMORY;
2751 }
2752
2753 sprintf( normalized->bv_val, "%02d%02d%02d%02d%02d%02d%02d",
2754 parts[0], parts[1], parts[2] + 1, parts[3] + 1,
2755 parts[4], parts[5], parts[6] );
2756 if ( !BER_BVISEMPTY( &fraction ) ) {
2757 memcpy( normalized->bv_val + STRLENOF("YYYYmmddHHMMSSZ")-1,
2758 fraction.bv_val, fraction.bv_len );
2759 normalized->bv_val[STRLENOF("YYYYmmddHHMMSSZ")-1] = '.';
2760 }
2761 strcpy( normalized->bv_val + len-1, "Z" );
2762 normalized->bv_len = len;
2763
2764 return LDAP_SUCCESS;
2765 }
2766
2767
2768 int accesslog_initialize()
2769 {
2770 int i, rc;
2771 Syntax *rdnTimestampSyntax;
2772 MatchingRule *rdnTimestampMatch, *rdnTimestampOrdering;
2773
2774 accesslog.on_bi.bi_type = "accesslog";
2775 accesslog.on_bi.bi_db_init = accesslog_db_init;
2776 accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
2777 accesslog.on_bi.bi_db_open = accesslog_db_open;
2778 accesslog.on_bi.bi_db_close = accesslog_db_close;
2779
2780 accesslog.on_bi.bi_op_add = accesslog_op_mod;
2781 accesslog.on_bi.bi_op_bind = accesslog_op_misc;
2782 accesslog.on_bi.bi_op_compare = accesslog_op_misc;
2783 accesslog.on_bi.bi_op_delete = accesslog_op_mod;
2784 accesslog.on_bi.bi_op_modify = accesslog_op_mod;
2785 accesslog.on_bi.bi_op_modrdn = accesslog_op_mod;
2786 accesslog.on_bi.bi_op_search = accesslog_op_misc;
2787 accesslog.on_bi.bi_extended = accesslog_op_misc;
2788 accesslog.on_bi.bi_op_unbind = accesslog_unbind;
2789 accesslog.on_bi.bi_op_abandon = accesslog_abandon;
2790 accesslog.on_bi.bi_operational = accesslog_operational;
2791
2792 accesslog.on_bi.bi_cf_ocs = log_cfocs;
2793
2794 nullsc.sc_response = slap_null_cb;
2795
2796 rc = config_register_schema( log_cfats, log_cfocs );
2797 if ( rc ) return rc;
2798
2799 /* log schema integration */
2800 for ( i=0; lsyntaxes[i].oid; i++ ) {
2801 int code;
2802
2803 code = register_syntax( &lsyntaxes[ i ].syn );
2804 if ( code != 0 ) {
2805 Debug( LDAP_DEBUG_ANY,
2806 "accesslog_init: register_syntax failed\n" );
2807 return code;
2808 }
2809
2810 if ( lsyntaxes[i].mrs != NULL ) {
2811 code = mr_make_syntax_compat_with_mrs(
2812 lsyntaxes[i].oid, lsyntaxes[i].mrs );
2813 if ( code < 0 ) {
2814 Debug( LDAP_DEBUG_ANY,
2815 "accesslog_init: "
2816 "mr_make_syntax_compat_with_mrs "
2817 "failed\n" );
2818 return code;
2819 }
2820 }
2821 }
2822
2823 for ( i=0; lattrs[i].at; i++ ) {
2824 int code;
2825
2826 code = register_at( lattrs[i].at, lattrs[i].ad, 0 );
2827 if ( code ) {
2828 Debug( LDAP_DEBUG_ANY,
2829 "accesslog_init: register_at failed\n" );
2830 return -1;
2831 }
2832 }
2833
2834 /* Inject custom normalizer for reqStart/reqEnd */
2835 rdnTimestampMatch = ch_malloc( sizeof( MatchingRule ));
2836 rdnTimestampOrdering = ch_malloc( sizeof( MatchingRule ));
2837 rdnTimestampSyntax = ch_malloc( sizeof( Syntax ));
2838 *rdnTimestampMatch = *ad_reqStart->ad_type->sat_equality;
2839 rdnTimestampMatch->smr_normalize = rdnTimestampNormalize;
2840 *rdnTimestampOrdering = *ad_reqStart->ad_type->sat_ordering;
2841 rdnTimestampOrdering->smr_normalize = rdnTimestampNormalize;
2842 *rdnTimestampSyntax = *ad_reqStart->ad_type->sat_syntax;
2843 rdnTimestampSyntax->ssyn_validate = rdnTimestampValidate;
2844 ad_reqStart->ad_type->sat_equality = rdnTimestampMatch;
2845 ad_reqStart->ad_type->sat_ordering = rdnTimestampOrdering;
2846 ad_reqStart->ad_type->sat_syntax = rdnTimestampSyntax;
2847
2848 rdnTimestampMatch = ch_malloc( sizeof( MatchingRule ));
2849 rdnTimestampOrdering = ch_malloc( sizeof( MatchingRule ));
2850 rdnTimestampSyntax = ch_malloc( sizeof( Syntax ));
2851 *rdnTimestampMatch = *ad_reqStart->ad_type->sat_equality;
2852 *rdnTimestampOrdering = *ad_reqStart->ad_type->sat_ordering;
2853 *rdnTimestampSyntax = *ad_reqStart->ad_type->sat_syntax;
2854 ad_reqEnd->ad_type->sat_equality = rdnTimestampMatch;
2855 ad_reqEnd->ad_type->sat_ordering = rdnTimestampOrdering;
2856 ad_reqEnd->ad_type->sat_syntax = rdnTimestampSyntax;
2857
2858 for ( i=0; locs[i].ot; i++ ) {
2859 int code;
2860
2861 code = register_oc( locs[i].ot, locs[i].oc, 0 );
2862 if ( code ) {
2863 Debug( LDAP_DEBUG_ANY,
2864 "accesslog_init: register_oc failed\n" );
2865 return -1;
2866 }
2867 }
2868
2869 return overlay_register(&accesslog);
2870 }
2871
2872 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
2873 int
2874 init_module( int argc, char *argv[] )
2875 {
2876 return accesslog_initialize();
2877 }
2878 #endif
2879
2880 #endif /* SLAPD_OVER_ACCESSLOG */