-
Notifications
You must be signed in to change notification settings - Fork 909
/
ld_fld.c
1126 lines (971 loc) · 25.8 KB
/
ld_fld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
*
* LDAP Database Driver for SER
*
* Copyright (C) 2008 iptelorg GmbH
*
* This file is part of SER, a free SIP server.
*
* SER is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* SER is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/** \addtogroup ldap
* @{
*/
/** \file
* Data field conversion and type checking functions.
*/
#define _XOPEN_SOURCE 4 /* bsd */
#define _XOPEN_SOURCE_EXTENDED 1 /* solaris */
#define _SVID_SOURCE 1 /* timegm */
#define _BSD_SOURCE /* snprintf */
#define _DEFAULT_SOURCE 1 /* _BSD_SOURCE is deprecated */
#include "ld_fld.h"
#include "../../lib/srdb2/db_drv.h"
#include "../../mem/mem.h"
#include "../../dprint.h"
#include "../../ut.h"
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <stdint.h>
#include <string.h>
#include <time.h> /* strptime, XOPEN issue must be >= 4 */
/**
* Reallocatable string buffer.
*/
struct sbuf {
char *s; /**< allocated memory itself */
int len; /**< used memory */
int size; /**< total size of allocated memory */
int increment; /**< increment when realloc is necessary */
};
#define TEST_RESIZE \
if (rsize > sb->size) { \
asize = rsize - sb->size; \
new_size = sb->size + (asize / sb->increment + \
(asize % sb->increment > 0)) * sb->increment; \
newp = pkg_malloc(new_size); \
if (!newp) { \
ERR("ldap: No memory left\n"); \
return -1; \
} \
if (sb->s) { \
memcpy(newp, sb->s, sb->len); \
pkg_free(sb->s); \
} \
sb->s = newp; \
sb->size = new_size; \
}
static inline int sb_add(struct sbuf *sb, char* str, int len)
{
int new_size = 0, asize;
int rsize = sb->len + len;
char *newp;
TEST_RESIZE;
memcpy(sb->s + sb->len, str, len);
sb->len += len;
return 0;
}
static inline int sb_add_esc(struct sbuf *sb, char* str, int len)
{
int new_size = 0, asize, i;
int rsize = sb->len + len * 3;
char *newp, *w;
TEST_RESIZE;
w = sb->s + sb->len;
for(i = 0; i < len; i++) {
switch(str[i]) {
case '*':
*w++ = '\\'; *w++ = '2'; *w++ = 'A';
sb->len += 3;
break;
case '(':
*w++ = '\\'; *w++ = '2'; *w++ = '8';
sb->len += 3;
break;
case ')':
*w++ = '\\'; *w++ = '2'; *w++ = '9';
sb->len += 3;
break;
case '\\':
*w++ = '\\'; *w++ = '5'; *w++ = 'C';
sb->len += 3;
break;
case '\0':
*w++ = '\\'; *w++ = '0'; *w++ = '0';
sb->len += 3;
break;
default:
*w++ = str[i];
sb->len++;
break;
}
}
return 0;
}
/** Frees memory used by a ld_fld structure.
* This function frees all memory used by a ld_fld structure
* @param fld Generic db_fld_t* structure being freed.
* @param payload The ldap extension structure to be freed
*/
static void ld_fld_free(db_fld_t* fld, struct ld_fld* payload)
{
db_drv_free(&payload->gen);
if (payload->values) ldap_value_free_len(payload->values);
payload->values = NULL;
if (payload->filter) pkg_free(payload->filter);
payload->filter = NULL;
pkg_free(payload);
}
int ld_fld(db_fld_t* fld, char* table)
{
struct ld_fld* res;
res = (struct ld_fld*)pkg_malloc(sizeof(struct ld_fld));
if (res == NULL) {
ERR("ldap: No memory left\n");
return -1;
}
memset(res, '\0', sizeof(struct ld_fld));
if (db_drv_init(&res->gen, ld_fld_free) < 0) goto error;
DB_SET_PAYLOAD(fld, res);
return 0;
error:
if (res) pkg_free(res);
return -1;
}
int ld_resolve_fld(db_fld_t* fld, struct ld_cfg* cfg)
{
int i;
struct ld_fld* lfld;
if (fld == NULL || cfg == NULL) return 0;
for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
lfld = DB_GET_PAYLOAD(fld + i);
lfld->attr.s = ld_find_attr_name(&lfld->syntax, cfg, fld[i].name);
if (lfld->attr.s == NULL) lfld->attr.s = fld[i].name;
if (lfld->attr.s) lfld->attr.len = strlen(lfld->attr.s);
}
return 0;
}
static inline int ldap_int2db_int(int* dst, str* src)
{
if (str2sint(src, dst) != 0) {
ERR("ldap: Error while converting value '%.*s' to integer\n",
src->len, ZSW(src->s));
return -1;
}
return 0;
}
static inline int ldap_bit2db_int(int* dst, str* src)
{
int i, v;
if (src->len > 32) {
WARN("ldap: bitString '%.*s'B is longer than 32 bits, truncating\n",
src->len, ZSW(src->s));
}
v = 0;
for(i = 0; i < src->len; i++) {
v <<= 1;
v += src->s[i] - '0';
}
*dst = v;
return 0;
}
static inline int ldap_gentime2db_datetime(time_t* dst, str* src)
{
struct tm time;
if (src->len < 12) return -1;
/* It is necessary to zero tm structure first */
memset(&time, '\0', sizeof(struct tm));
/* YYYYMMDDHHMMSS[.sss][ 'Z' | ( {'+'|'-'} ZZZZ) ] */
strptime(src->s, "%Y%m%d%H%M%S", &time); /* Note: frac of seconds are lost in time_t representation */
if (src->s[src->len-1] == 'Z' || src->s[src->len-5] == '-' || src->s[src->len-5] == '+') {
/* GMT or specified TZ, no daylight saving time */
#ifdef HAVE_TIMEGM
*dst = timegm(&time);
#else
*dst = _timegm(&time);
#endif /* HAVE_TIMEGM */
if (src->s[src->len-1] != 'Z') {
/* timezone is specified */
memset(&time, '\0', sizeof(struct tm));
strptime(src->s + src->len - 4, "%H%M", &time);
switch (src->s[src->len-5]) {
case '-':
*dst -= time.tm_hour*3600+time.tm_min*60;
break;
case '+':
*dst += time.tm_hour*3600+time.tm_min*60;
break;
default:
;
}
}
}
else {
/* it's local time */
/* Daylight saving information got lost in the database
* so let timegm to guess it. This eliminates the bug when
* contacts reloaded from the database have different time
* of expiration by one hour when daylight saving is used
*/
time.tm_isdst = -1;
*dst = timelocal(&time);
}
return 0;
}
static inline int ldap_str2db_double(double* dst, char* src)
{
*dst = atof(src);
return 0;
}
static inline int ldap_str2db_float(float* dst, char* src)
{
*dst = (float)atof(src);
return 0;
}
static inline int ldap_fld2db_fld(db_fld_t* fld, str v) {
switch(fld->type) {
case DB_CSTR:
fld->v.cstr = v.s;
break;
case DB_STR:
case DB_BLOB:
fld->v.lstr.s = v.s;
fld->v.lstr.len = v.len;
break;
case DB_INT:
case DB_BITMAP:
if (v.s[0] == '\'' && v.s[v.len - 1] == 'B' &&
v.s[v.len - 2] == '\'') {
v.s++;
v.len -= 3;
if (ldap_bit2db_int(&fld->v.int4, &v) != 0) {
ERR("ldap: Error while converting bit string '%.*s'\n",
v.len, ZSW(v.s));
return -1;
}
break;
}
if (v.len == 4 && !strncasecmp("TRUE", v.s, v.len)) {
fld->v.int4 = 1;
break;
}
if (v.len == 5 && !strncasecmp("FALSE", v.s, v.len)) {
fld->v.int4 = 0;
break;
}
if (ldap_int2db_int(&fld->v.int4, &v) != 0) {
ERR("ldap: Error while converting %.*s to integer\n",
v.len, ZSW(v.s));
return -1;
}
break;
case DB_DATETIME:
if (ldap_gentime2db_datetime(&fld->v.time, &v) != 0) {
ERR("ldap: Error while converting LDAP time value '%.*s'\n",
v.len, ZSW(v.s));
return -1;
}
break;
case DB_FLOAT:
/* We know that the ldap library zero-terminated v.s */
if (ldap_str2db_float(&fld->v.flt, v.s) != 0) {
ERR("ldap: Error while converting '%.*s' to float\n",
v.len, ZSW(v.s));
return -1;
}
break;
case DB_DOUBLE:
/* We know that the ldap library zero-terminated v.s */
if (ldap_str2db_double(&fld->v.dbl, v.s) != 0) {
ERR("ldap: Error while converting '%.*s' to double\n",
v.len, ZSW(v.s));
return -1;
}
break;
default:
ERR("ldap: Unsupported field type: %d\n", fld->type);
return -1;
}
return 0;
}
int ld_ldap2fldinit(db_fld_t* fld, LDAP* ldap, LDAPMessage* msg)
{
return ld_ldap2fldex(fld, ldap, msg, 1);
}
int ld_ldap2fld(db_fld_t* fld, LDAP* ldap, LDAPMessage* msg)
{
return ld_ldap2fldex(fld, ldap, msg, 0);
}
int ld_incindex(db_fld_t* fld) {
int i;
struct ld_fld* lfld;
if (fld == NULL) return 0;
i = 0;
while (!DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i])) {
lfld = DB_GET_PAYLOAD(fld + i);
lfld->index++;
/* the index limit has been reached */
if (lfld->index >= lfld->valuesnum) {
lfld->index = 0;
} else {
return 0;
}
i++;
}
/* there is no more value combination left */
return 1;
}
#define CMP_NUM(fld_v,match_v,fld) \
if (fld_v.fld == match_v.fld) \
op = 0x02; \
else if (fld_v.fld < match_v.fld) \
op = 0x01; \
else if (fld_v.fld > match_v.fld) \
op = 0x04;
int ld_ldap2fldex(db_fld_t* fld, LDAP* ldap, LDAPMessage* msg, int init)
{
int i;
struct ld_fld* lfld;
str v;
if (fld == NULL || msg == NULL) return 0;
for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) {
lfld = DB_GET_PAYLOAD(fld + i);
if (init) {
if (fld[i].type == DB_NONE) {
switch (lfld->syntax) {
case LD_SYNTAX_STRING:
fld[i].type = DB_STR;
break;
case LD_SYNTAX_INT:
case LD_SYNTAX_BOOL:
case LD_SYNTAX_BIT:
fld[i].type = DB_INT;
break;
case LD_SYNTAX_FLOAT:
fld[i].type = DB_FLOAT;
break;
case LD_SYNTAX_GENTIME:
fld[i].type = DB_DATETIME;
break;
case LD_SYNTAX_BIN:
fld[i].type = DB_BITMAP;
break;
}
}
/* free the values of the previous object */
if (lfld->values) ldap_value_free_len(lfld->values);
lfld->values = ldap_get_values_len(ldap, msg, lfld->attr.s);
lfld->index = 0;
if (lfld->values == NULL || lfld->values[0] == NULL) {
fld[i].flags |= DB_NULL;
/* index == 0 means no value available */
lfld->valuesnum = 0;
if (lfld->client_side_filtering && lfld->filter) {
int j;
/* if the all filter conditions requires NULL value then we can accept the record */
for (j=0; lfld->filter[j]; j++) {
if (lfld->filter[j]->flags & DB_NULL && lfld->filter[j]->op == DB_EQ) {
continue;
}
return 1; /* get next record */
}
}
} else {
/* init the number of values */
fld[i].flags &= ~DB_NULL;
lfld->valuesnum = ldap_count_values_len(lfld->values);
if ((lfld->valuesnum > 1 || lfld->client_side_filtering) && lfld->filter) {
/* in case of multivalue we must check if value fits in filter criteria.
LDAP returns record (having each multivalue) if one particular
multivalue fits in filter provided to LDAP search. We need
filter out these values manually. It not perfect because
LDAP filtering may be based on different rule/locale than
raw (ASCII,...) comparision.
We reorder values so we'll have interesting values located from top up to valuesnum at the end.
The same algorithm is applied for client side filtering
*/
do {
int passed, j;
for (j=0, passed = 1; lfld->filter[j] && passed; j++) {
int op; /* b0..less, b1..equal, b2..greater, zero..non equal */
op = 0x00;
if (lfld->filter[j]->flags & DB_NULL) {
/* always non equal because field is not NULL */
}
else {
v.s = lfld->values[lfld->index]->bv_val;
v.len = lfld->values[lfld->index]->bv_len;
if (ldap_fld2db_fld(fld + i, v) < 0) {
passed = 0;
break; /* for loop */
}
else {
db_fld_val_t v;
int t;
static char buf[30];
t = lfld->filter[j]->type;
/* we need compare value provided in match condition with value returned by LDAP.
The match value should be the same type as LDAP value obtained during
db_cmd(). We implement some basic conversions.
*/
v = lfld->filter[j]->v;
if (t == DB_CSTR) {
v.lstr.s = v.cstr;
v.lstr.len = strlen(v.lstr.s);
t = DB_STR;
}
switch (fld[i].type) {
case DB_CSTR:
fld[i].v.lstr.s = fld[i].v.cstr;
fld[i].v.lstr.len = strlen(fld[i].v.lstr.s);
fld[i].type = DB_STR;
/* no break */
case DB_STR:
switch (t) {
case DB_INT:
v.lstr.len = snprintf(buf, sizeof(buf)-1, "%d", v.int4);
v.lstr.s = buf;
break;
/* numeric conversion for double/float not supported because of non unique string representation */
default:
goto skip_conv;
}
break;
case DB_INT:
switch (t) {
case DB_DOUBLE:
if ((double)(int)v.dbl != (double)v.dbl)
goto skip_conv;
v.int4 = v.dbl;
break;
case DB_FLOAT:
if ((float)(int)v.flt != (float)v.flt)
goto skip_conv;
v.int4 = v.flt;
break;
case DB_STR:
if (v.lstr.len > 0) {
char c, *p;
int n;
c = v.lstr.s[v.lstr.len];
v.lstr.s[v.lstr.len] = '\0';
n = strtol(v.lstr.s, &p, 10);
v.lstr.s[v.lstr.len] = c;
if ((*p) != '\0') {
goto skip_conv;
}
v.int4 = n;
}
break;
default:
goto skip_conv;
}
break;
case DB_FLOAT:
switch (t) {
case DB_DOUBLE:
v.flt = v.dbl;
break;
case DB_INT:
v.flt = v.int4;
break;
#ifdef __USE_ISOC99
case DB_STR:
if (v.lstr.len > 0) {
char c, *p;
float n;
c = v.lstr.s[v.lstr.len];
v.lstr.s[v.lstr.len] = '\0';
n = strtof(v.lstr.s, &p);
v.lstr.s[v.lstr.len] = c;
if ((*p) != '\0') {
goto skip_conv;
}
v.flt = n;
}
break;
#endif
default:
goto skip_conv;
}
break;
case DB_DOUBLE:
switch (t) {
case DB_FLOAT:
v.dbl = v.flt;
break;
case DB_INT:
v.dbl = v.int4;
break;
case DB_STR:
if (v.lstr.len > 0) {
char c, *p;
double n;
c = v.lstr.s[v.lstr.len];
v.lstr.s[v.lstr.len] = '\0';
n = strtod(v.lstr.s, &p);
v.lstr.s[v.lstr.len] = c;
if ((*p) != '\0') {
goto skip_conv;
}
v.dbl = n;
}
break;
default:
goto skip_conv;
}
break;
case DB_BLOB:
case DB_BITMAP:
case DB_DATETIME:
default:
goto skip_conv;
}
t = fld[i].type;
skip_conv:
if (t == fld[i].type) {
switch (fld[i].type) {
case DB_CSTR: /* impossible, already converted to DB_STR */
case DB_STR:
if (fld[i].v.lstr.len == v.lstr.len) {
op = strncmp(fld[i].v.lstr.s, v.lstr.s, v.lstr.len);
if (op < 0)
op = 0x01;
else if (op > 0)
op = 0x04;
else
op = 0x02;
}
else if (fld[i].v.lstr.len < v.lstr.len) {
op = strncmp(fld[i].v.lstr.s, v.lstr.s, fld[i].v.lstr.len);
if (op < 0)
op = 0x01;
else
op = 0x04;
}
else /* if (fld[i].v.lstr.len > v.lstr.len) */ {
op = strncmp(fld[i].v.lstr.s, v.lstr.s, v.lstr.len);
if (op > 0)
op = 0x04;
else
op = 0x01;
}
break;
case DB_BLOB:
if (fld[i].v.blob.len == v.blob.len && memcmp(fld[i].v.blob.s, v.blob.s, v.blob.len) == 0)
op = 0x02;
break;
case DB_INT:
CMP_NUM(fld[i].v, v, int4);
break;
case DB_BITMAP:
CMP_NUM(fld[i].v, v, bitmap);
break;
case DB_DATETIME:
CMP_NUM(fld[i].v, v, time);
break;
case DB_FLOAT:
CMP_NUM(fld[i].v, v, flt);
break;
case DB_DOUBLE:
CMP_NUM(fld[i].v, v, dbl);
break;
default:
;
}
}
}
}
switch (lfld->filter[j]->op) {
case DB_EQ:
passed = op == 0x02;
break;
case DB_NE:
passed = (op & 0x02) == 0;
break;
case DB_LT:
passed = op == 0x01;
break;
case DB_LEQ:
passed = op == 0x01 || op == 0x02;
break;
case DB_GT:
passed = op == 0x04;
break;
case DB_GEQ:
passed = op == 0x04 || op == 0x02;
break;
default:
;
}
}
if (passed) {
lfld->index++;
}
else {
char *save_bvval;
int save_bvlen;
int i;
/* shift following values, push useless value at the end and decrease num of values */
save_bvval = lfld->values[lfld->index]->bv_val;
save_bvlen = lfld->values[lfld->index]->bv_len;
for (i=lfld->index+1; i < lfld->valuesnum; i++) {
lfld->values[i-1]->bv_val = lfld->values[i]->bv_val;
lfld->values[i-1]->bv_len = lfld->values[i]->bv_len;
}
lfld->values[lfld->valuesnum-1]->bv_val = save_bvval;
lfld->values[lfld->valuesnum-1]->bv_len = save_bvlen;
lfld->valuesnum--;
}
} while (lfld->index < lfld->valuesnum);
if (lfld->valuesnum == 0) {
return 1; /* get next record */
}
}
}
/* pointer to the current value */
lfld->index = 0;
}
/* this is an empty value */
if (!lfld->valuesnum)
continue;
v.s = lfld->values[lfld->index]->bv_val;
v.len = lfld->values[lfld->index]->bv_len;
if (ldap_fld2db_fld(fld + i, v) < 0)
return -1;
}
return 0;
}
static inline int db_int2ldap_str(struct sbuf* buf, db_fld_t* fld)
{
int len;
char tmp[INT2STR_MAX_LEN + 1];
len = snprintf(tmp, INT2STR_MAX_LEN + 1, "%-d", fld->v.int4);
if (len < 0 || len >= INT2STR_MAX_LEN + 1) {
BUG("ldap: Error while converting integer to string\n");
return -1;
}
return sb_add(buf, tmp, len);
}
static inline int db_datetime2ldap_gentime(struct sbuf* buf, db_fld_t* fld)
{
char tmp[16];
struct tm* t;
t = gmtime(&fld->v.time);
if (strftime(tmp, sizeof(tmp), "%Y%m%d%H%M%SZ", t) != sizeof(tmp)-1) {
ERR("ldap: Error while converting time_t value to LDAP format\n");
return -1;
}
return sb_add(buf, tmp, sizeof(tmp)-1);
}
static inline int db_int2ldap_bool(struct sbuf* buf, db_fld_t* fld)
{
if (fld->v.int4)
return sb_add(buf, "TRUE", 4);
else
return sb_add(buf, "FALSE", 5);
}
static inline int db_uint2ldap_int(struct sbuf* buf, db_fld_t* fld)
{
char* num;
int len, v;
v = fld->v.int4;
num = int2str(v, &len);
return sb_add(buf, num, len);
}
static inline int db_bit2ldap_bitstr(struct sbuf* buf, db_fld_t* fld)
{
int rv, i;
rv = 0;
rv |= sb_add(buf, "'", 1);
i = 1 << (sizeof(fld->v.int4) * 8 - 1);
while(i) {
rv |= sb_add(buf, (fld->v.int4 & i)?"1":"0", 1);
i = i >> 1;
}
rv |= sb_add(buf, "'B", 2);
return rv;
}
static inline int db_float2ldap_str(struct sbuf* buf, db_fld_t* fld)
{
char tmp[16];
int len;
len = snprintf(tmp, 16, "%-10.2f", fld->v.flt);
if (len < 0 || len >= 16) {
BUG("ldap: Error while converting float to string\n");
return -1;
}
return sb_add(buf, tmp, len);
}
static inline int db_double2ldap_str(struct sbuf* buf, db_fld_t* fld)
{
char tmp[16];
int len;
len = snprintf(tmp, 16, "%-10.2f", fld->v.dbl);
if (len < 0 || len >= 16) {
BUG("ldap: Error while converting double to string\n");
return -1;
}
return sb_add(buf, tmp, len);
}
static inline int ld_db2ldap(struct sbuf* buf, db_fld_t* fld) {
struct ld_fld* lfld;
lfld = DB_GET_PAYLOAD(fld);
switch(fld->type) {
case DB_CSTR:
if (sb_add_esc(buf, fld->v.cstr,
fld->v.cstr ? strlen(fld->v.cstr) : 0) != 0) goto error;
break;
case DB_STR:
if (sb_add_esc(buf, fld->v.lstr.s, fld->v.lstr.len) != 0) goto error;
break;
case DB_INT:
switch(lfld->syntax) {
case LD_SYNTAX_STRING:
case LD_SYNTAX_INT:
case LD_SYNTAX_FLOAT:
if (db_int2ldap_str(buf, fld))
goto error;
break;
case LD_SYNTAX_GENTIME:
if (db_datetime2ldap_gentime(buf, fld))
goto error;
break;
case LD_SYNTAX_BIT:
if (db_bit2ldap_bitstr(buf, fld))
goto error;
break;
case LD_SYNTAX_BOOL:
if (db_int2ldap_bool(buf, fld))
goto error;
break;
default:
ERR("ldap: Cannot convert integer field %s "
"to LDAP attribute %.*s\n",
fld->name, lfld->attr.len, ZSW(lfld->attr.s));
goto error;
}
break;
case DB_BITMAP:
switch(lfld->syntax) {
case LD_SYNTAX_INT:
if (db_uint2ldap_int(buf, fld))
goto error;
break;
case LD_SYNTAX_BIT:
case LD_SYNTAX_STRING:
if (db_bit2ldap_bitstr(buf, fld))
goto error;
break;
default:
ERR("ldap: Cannot convert bitmap field %s "
"to LDAP attribute %.*s\n",
fld->name, lfld->attr.len, ZSW(lfld->attr.s));
goto error;
}
break;
case DB_DATETIME:
switch(lfld->syntax) {
case LD_SYNTAX_STRING:
case LD_SYNTAX_GENTIME:
if (db_datetime2ldap_gentime(buf, fld))
goto error;
break;
case LD_SYNTAX_INT:
if (db_uint2ldap_int(buf, fld))
goto error;
break;
default:
ERR("ldap: Cannot convert datetime field %s "
"to LDAP attribute %.*s\n",
fld->name, lfld->attr.len, ZSW(lfld->attr.s));
goto error;
}
break;
case DB_FLOAT:
switch(lfld->syntax) {
case LD_SYNTAX_STRING:
case LD_SYNTAX_FLOAT:
if (db_float2ldap_str(buf, fld))
goto error;
break;
default:
ERR("ldap: Cannot convert float field %s "
"to LDAP attribute %.*s\n",
fld->name, lfld->attr.len, ZSW(lfld->attr.s));
goto error;
}
case DB_DOUBLE:
switch(lfld->syntax) {
case LD_SYNTAX_STRING:
case LD_SYNTAX_FLOAT:
if (db_float2ldap_str(buf, fld))
goto error;
break;
default:
ERR("ldap: Cannot convert double field %s "
"to LDAP attribute %.*s\n",
fld->name, lfld->attr.len, ZSW(lfld->attr.s));
goto error;
break;
}
break;
case DB_BLOB:
switch(lfld->syntax) {
case LD_SYNTAX_STRING:
case LD_SYNTAX_BIN:
if (sb_add_esc(buf, fld->v.lstr.s, fld->v.lstr.len) < 0)
goto error;
break;
default:
ERR("ldap: Cannot convert binary field %s "
"to LDAP attribute %.*s\n",
fld->name, lfld->attr.len, ZSW(lfld->attr.s));
goto error;
}
break;
default:
BUG("ldap: Unsupported field type encountered: %d\n", fld->type);
goto error;
}
return 0;
error:
return -1;
}
/* skip fields belonging to a field which is requested for filtering at client side */
inline static void skip_client_side_filtering_fields(db_cmd_t* cmd, db_fld_t **fld) {
struct ld_fld* lfld;
db_fld_t *f;
try_next:
if (DB_FLD_EMPTY(*fld) || DB_FLD_LAST(**fld)) return;
for (f=cmd->result; !DB_FLD_EMPTY(f) && !DB_FLD_LAST(*f); f++) {
lfld = DB_GET_PAYLOAD(f);
if (lfld->client_side_filtering && lfld->filter) {
int j;
for (j = 0; lfld->filter[j]; j++) {