-
Notifications
You must be signed in to change notification settings - Fork 2
/
zone2ldap.c
1107 lines (949 loc) · 27.6 KB
/
zone2ldap.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
/*
* Copyright (C) 2001 Jeff McNeil <[email protected]>
* Copyright (C) 2004 Turbo Fredriksson <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* Change Log
* Thu Nov 4 09:24:31 CET 2004 - Turbo Fredriksson
* - Added SASL authentication. Acomplished by stealing
* sasl.c from the OpenLDAP source tree.
* - Added full offline support. No linking against any LDAP
* libraries etc. The header 'ldap.h' is still needed though,
* since we're outputing LDIF's.
*
* Tue Nov 2 09:49:05 CET 2004 - Turbo Fredriksson
* - Added StartTLS extended operations
* - Added LDAP URI support
* - Added OpenLDAPaci attribute(s) additions to objects.
* - Adding offline generation of zone object(s).
* - Fixing object class generation - newer OpenLDAP is very
* picky about the objects. Only one structural objectclass is
* _required/allowed_. This means that each of the base structure
* objects must have the objectclass 'domain' and the attribute 'dc'
* defined. Remove the 'top' objectclass. Redundant.
*
* Tue May 1 19:19:54 EDT 2001 - Jeff McNeil
* Update to objectClass code, and add_to_rr_list function
* (I need to rename that) to support the dNSZone schema,
* ditched dNSDomain2 schema support. Version 0.3-ALPHA
*
* $Id: zone2ldap.c,v 1.5 2007-06-29 21:29:03 turbo Exp $
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <isc/buffer.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/result.h>
#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/result.h>
#include <dns/rdatatype.h>
#include <ldap.h>
#define DNS_OBJECT 6
#define DNS_TOP 2
#define VERSION "0.4-ALPHA/TF.3"
#define NO_SPEC 0
#define WI_SPEC 1
/* Global Zone Pointer */
char *gbl_zone = NULL;
typedef struct LDAP_INFO
{
char *dn;
LDAPMod **attrs;
struct LDAP_INFO *next;
int attrcnt;
}
ldap_info;
/* usage Info */
void usage ();
/* Add to the ldap dit */
void add_ldap_values (ldap_info * ldinfo);
/* Add the OpenLDAPACI attribute(s). */
LDAPMod *add_aci_values ();
#ifndef OFFLINE
/* Init an ldap connection */
void init_ldap_conn ();
/* Ldap error checking */
void ldap_result_check (char *msg, char *dn, int err);
#endif
/* Put a hostname into a char ** array */
char **hostname_to_dn_list (char *hostname, char *zone, unsigned int flags);
/* Find out how many items are in a char ** array */
int get_attr_list_size (char **tmp);
/* Get a DN */
char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag);
/* Add to RR list */
void add_to_rr_list (char *dn, char *name, char *type, char *data,
unsigned int ttl, unsigned int flags);
/* Error checking */
void isc_result_check (isc_result_t res, char *errorstr);
/* Generate LDIF Format files */
void generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata,
unsigned int ttl);
/* head pointer to the list */
ldap_info *ldap_info_base = NULL;
/* Wrapper for add or LDIF generation */
void do_ldap_add (LDAP *ld, char *dn, LDAPMod *attrs[]);
/* Output as LDIF instead of writing to LDAP server */
void output_ldif (char *dn, LDAPMod *attrs[]);
char *argzone, *ldapbase, *binddn[256];
#ifndef OFFLINE
char *bindpw = NULL, *ldapsystem = NULL;
int use_uri = 0, use_ssl = 0, use_tls = 0, use_ldif = 0;
#else
int use_ldif = 1;
#endif
int use_aci = 0, verbose = 0;
static char *objectClasses[] = { "dNSZone", NULL };
static char *topObjectClasses[] = { "domain", NULL };
#ifndef OFFLINE
#if defined(SECUREBIND_SASL)
#include <slapd/lutil_ldap.h>
#include <sasl/sasl.h>
unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
char *sasl_secprops = NULL;
char *sasl_realm = NULL;
char *sasl_authc_id = NULL;
char *sasl_authz_id = NULL;
char *sasl_mech = NULL;
int authmethod = LDAP_AUTH_SASL;
#endif
LDAP *conn;
#else
int authmethod = -1;
#endif
unsigned int debug = 0;
#ifdef DEBUG
debug = 1;
#endif
int
main (int *argc, char **argv)
{
isc_mem_t *isc_ctx = NULL;
isc_result_t result;
char *basedn;
ldap_info *tmp;
LDAPMod *base_attrs[4];
isc_buffer_t buff;
char *zonefile;
char fullbasedn[1024];
char *ctmp, *dc = NULL;
dns_fixedname_t fixedzone, fixedname;
dns_rdataset_t rdataset;
char **dc_list;
dns_rdata_t rdata = DNS_RDATA_INIT;
dns_rdatasetiter_t *riter;
dns_name_t *zone, *name;
dns_db_t *db = NULL;
dns_dbiterator_t *dbit = NULL;
dns_dbnode_t *node;
extern char *optarg;
extern int optind, opterr, optopt;
int create_base = 0;
int topt;
int binddn_counter = 0;
if ((int) argc < 2)
{
usage ();
exit (-1);
}
while ((topt = getopt ((int) argc, argv, "D:w:b:z:f:h:H:ZL?dcvoO:QR:U:X:Y:IVx")) != -1)
{
switch (topt)
{
case 'V':
printf("%s\n", VERSION);
exit(0);
case 'c':
create_base++;
break;
case 'd':
debug++;
break;
case 'D':
binddn[binddn_counter] = strdup (optarg);
binddn_counter++;
break;
#ifndef OFFLINE
case 'w':
bindpw = strdup (optarg);
break;
case 'b':
ldapbase = strdup (optarg);
break;
#endif
case 'z':
argzone = strdup (optarg);
// We wipe argzone all to hell when we parse it for the DN */
gbl_zone = strdup(argzone);
break;
case 'f':
zonefile = strdup (optarg);
break;
#ifndef OFFLINE
case 'h':
ldapsystem = strdup (optarg);
break;
case 'H':
/* Use LDAP URI to connect to LDAP server. */
ldapsystem = strdup (optarg);
if(!strncasecmp(ldapsystem, "ldaps://", 8))
use_ssl = 1;
else if(!strncasecmp(ldapsystem, "ldapi://", 8) ||
!strncasecmp(ldapsystem, "ldap://", 7))
use_uri = 1;
break;
#endif
case 'o':
/* Include OpenLDAPaci attribute(s) in each object created. */
use_aci = 1;
break;
#ifndef OFFLINE
case 'Z':
/* Issue StartTLS (Transport Layer Security) extended operation. */
if(!use_ssl)
++use_tls;
else {
fprintf (stderr, "Can't combine both SSL and TLS (ldaps:// specified in 'ldapserver')\n");
exit (-1);
}
break;
case 'L':
/* print entries in LDIF format */
use_ldif = 1;
break;
#if defined(SECUREBIND_SASL)
case 'O':
/* SASL security properties */
sasl_secprops = strdup (optarg);
break;
case 'R':
/* SASL realm */
sasl_realm = strdup (optarg);
break;
case 'U':
/* Username for SASL bind. The syntax of the username depends
* on the actual SASL mechanism used.
*/
sasl_authc_id = strdup (optarg);
break;
case 'X':
/* Requested authorization ID for SASL bind. Authzid must be
* one of the following formats:
* dn:<distinguished name>
* or
* u:<username>
*/
sasl_authz_id = strdup (optarg);
break;
case 'Y':
/* the SASL mechanism to be used for authentication. If it's
* not specified, the program will choose the best mechanism
* the server knows.
*/
sasl_mech = strdup (optarg);
break;
case 'Q':
/* SASL Quiet mode */
sasl_flags = LDAP_SASL_QUIET;
break;
case 'I':
/* SASL Interactive mode. */
sasl_flags = LDAP_SASL_INTERACTIVE;
break;
case 'x':
authmethod = -1;
break;
#endif
#endif
case 'v':
verbose = 1;
break;
case '?':
default:
usage ();
exit (0);
}
}
if ((argzone == NULL) || (zonefile == NULL)
#ifndef OFFLINE
|| (ldapbase == NULL)
#endif
)
{
usage ();
exit (-1);
}
#if defined(SECUREBIND_SASL)
if ((authmethod != LDAP_AUTH_SASL) && ((binddn[0] == NULL) || (bindpw == NULL))) {
printf ("Doing simple bind, but neither binddn and/or bindpw is supplied.\n");
usage ();
exit (-1);
}
#endif
if (debug)
printf ("Initializing ISC Routines, parsing zone file\n");
result = isc_mem_create (0, 0, &isc_ctx);
isc_result_check (result, "isc_mem_create");
isc_buffer_init (&buff, argzone, strlen (argzone));
isc_buffer_add (&buff, strlen (argzone));
dns_fixedname_init (&fixedzone);
zone = dns_fixedname_name (&fixedzone);
result = dns_name_fromtext (zone, &buff, dns_rootname, ISC_FALSE, NULL);
isc_result_check (result, "dns_name_fromtext");
/* It is required to initialize the hash before dns_db_create in BIND 9 */
result = isc_hash_create(isc_ctx, NULL, DNS_NAME_MAXWIRE);
isc_result_check (result, "isc_hash_create");
result =
dns_db_create (isc_ctx, "rbt", zone, dns_dbtype_zone, dns_rdataclass_in,
0, NULL, &db);
isc_result_check (result, "dns_db_create");
result = dns_db_load (db, zonefile);
isc_result_check (result, "Check Zone Syntax: dns_db_load");
result = dns_db_createiterator (db, ISC_FALSE, &dbit);
isc_result_check (result, "dns_db_createiterator");
result = dns_dbiterator_first (dbit);
isc_result_check (result, "dns_dbiterator_first");
dns_fixedname_init (&fixedname);
name = dns_fixedname_name (&fixedname);
dns_rdataset_init (&rdataset);
dns_rdata_init (&rdata);
while (result == ISC_R_SUCCESS)
{
node = NULL;
result = dns_dbiterator_current (dbit, &node, name);
if (result == ISC_R_NOMORE)
break;
isc_result_check (result, "dns_dbiterator_current");
riter = NULL;
result = dns_db_allrdatasets (db, node, NULL, 0, &riter);
isc_result_check (result, "dns_db_allrdatasets");
result = dns_rdatasetiter_first (riter);
//isc_result_check(result, "dns_rdatasetiter_first");
while (result == ISC_R_SUCCESS)
{
dns_rdatasetiter_current (riter, &rdataset);
result = dns_rdataset_first (&rdataset);
isc_result_check (result, "dns_rdatasetiter_current");
while (result == ISC_R_SUCCESS)
{
dns_rdataset_current (&rdataset, &rdata);
generate_ldap (name, &rdata, rdataset.ttl);
dns_rdata_reset (&rdata);
result = dns_rdataset_next (&rdataset);
}
dns_rdataset_disassociate (&rdataset);
result = dns_rdatasetiter_next (riter);
}
dns_rdatasetiter_destroy (&riter);
result = dns_dbiterator_next (dbit);
}
#ifndef OFFLINE
if (!use_ldif)
/* Initialize the LDAP Connection */
init_ldap_conn ();
#endif
if (create_base)
{
if (debug || verbose)
printf ("Creating base zone DN %s\n", argzone);
dc_list = hostname_to_dn_list (argzone, argzone, DNS_TOP);
basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC);
for (ctmp = &basedn[strlen (basedn)]; ctmp >= &basedn[0]; ctmp--)
{
if ((*ctmp == ',') || (ctmp == &basedn[0]))
{
/* ------------------ */
/* Object class setup */
base_attrs[0] = (LDAPMod *) malloc (sizeof (LDAPMod));
base_attrs[0]->mod_op = LDAP_MOD_ADD;
base_attrs[0]->mod_type = "objectClass";
base_attrs[0]->mod_values = (char **) calloc (sizeof (char *), 2);
if (base_attrs[0]->mod_values == (char **)NULL) exit(-1);
base_attrs[0]->mod_values = topObjectClasses;
/* ------------------ */
/* Domain component setup */
base_attrs[1] = (LDAPMod *) malloc (sizeof (LDAPMod));
base_attrs[1]->mod_op = LDAP_MOD_ADD;
base_attrs[1]->mod_type = "dc";
base_attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
if (base_attrs[1]->mod_values == (char **)NULL) exit(-1);
{
/* Get the DC value */
int i;
char *tmp = strdup(ctmp);
if(*ctmp == ',')
dc = tmp+4;
else
dc = tmp+3;
for(i=0; dc[i]; i++) {
if(dc[i] == ',') {
dc[i] = '\0';
break;
}
}
}
base_attrs[1]->mod_values[0] = dc;
base_attrs[1]->mod_values[1] = NULL;
/* ------------------ */
if(use_aci) {
base_attrs[2] = add_aci_values();
base_attrs[3] = NULL;
} else
base_attrs[2] = NULL;
/* ------------------ */
if (ldapbase)
{
if (ctmp != &basedn[0])
sprintf (fullbasedn, "%s,%s", ctmp + 1, ldapbase);
else
sprintf (fullbasedn, "%s,%s", ctmp, ldapbase);
}
else
{
if (ctmp != &basedn[0])
sprintf (fullbasedn, "%s", ctmp + 1);
else
sprintf (fullbasedn, "%s", ctmp);
}
/* ------------------ */
#ifndef OFFLINE
do_ldap_add (conn, fullbasedn, base_attrs);
#else
output_ldif (fullbasedn, base_attrs);
#endif
}
}
}
else
{
if (debug)
printf ("Skipping zone base dn creation for %s\n", argzone);
}
for (tmp = ldap_info_base; tmp != NULL; tmp = tmp->next)
{
if (debug || verbose)
printf ("Adding DN: %s\n", tmp->dn);
#ifndef OFFLINE
add_ldap_values (tmp);
#else
output_ldif (tmp->dn, tmp->attrs);
#endif
}
if (debug || verbose)
printf("Operation Complete.\n");
return 0;
}
/* Check the status of an isc_result_t after any isc routines.
* I should probably rename this function, as not to cause any
* confusion with the isc* routines. Will exit on error. */
void
isc_result_check (isc_result_t res, char *errorstr)
{
if (res != ISC_R_SUCCESS)
{
fprintf (stderr, " %s: %s\n", errorstr, isc_result_totext (res));
exit (-1);
}
}
/* Takes DNS information, in bind data structure format, and adds textual
* zone information to the LDAP run queue. */
void
generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
{
unsigned char name[DNS_NAME_MAXTEXT + 1];
unsigned int len;
unsigned char type[20];
unsigned char data[2048];
char **dc_list;
char *dn;
isc_buffer_t buff;
isc_result_t result;
isc_buffer_init (&buff, name, sizeof (name));
result = dns_name_totext (dnsname, ISC_TRUE, &buff);
isc_result_check (result, "dns_name_totext");
name[isc_buffer_usedlength (&buff)] = 0;
isc_buffer_init (&buff, type, sizeof (type));
result = dns_rdatatype_totext (rdata->type, &buff);
isc_result_check (result, "dns_rdatatype_totext");
type[isc_buffer_usedlength (&buff)] = 0;
isc_buffer_init (&buff, data, sizeof (data));
result = dns_rdata_totext (rdata, NULL, &buff);
isc_result_check (result, "dns_rdata_totext");
data[isc_buffer_usedlength (&buff)] = 0;
dc_list = hostname_to_dn_list (name, argzone, DNS_OBJECT);
len = (get_attr_list_size (dc_list) - 2);
dn = build_dn_from_dc_list (dc_list, ttl, WI_SPEC);
if (debug)
printf ("Adding %s (%s %s) to run queue list.\n", dn, type, data);
add_to_rr_list (dn, dc_list[len], type, data, ttl, DNS_OBJECT);
}
/* Locate an item in the Run queue linked list, by DN. Used by functions
* which add items to the run queue.
*/
ldap_info *
locate_by_dn (char *dn)
{
ldap_info *tmp;
for (tmp = ldap_info_base; tmp != (ldap_info *) NULL; tmp = tmp->next)
{
if (!strncmp (tmp->dn, dn, strlen (dn)))
return tmp;
}
return (ldap_info *) NULL;
}
/* Take textual zone data, and add to the LDAP Run queue. This works like so:
* If locate_by_dn does not return, alloc a new ldap_info structure, and then
* calloc a LDAPMod array, fill in the default "everyone needs this" information,
* including object classes and dc's. If it locate_by_dn does return, then we'll
* realloc for more LDAPMod structs, and appened the new data. If an LDAPMod exists
* for the parameter we're adding, then we'll realloc the mod_values array, and
* add the new value to the existing LDAPMod. Finnaly, it assures linkage exists
* within the Run queue linked ilst*/
void
add_to_rr_list (char *dn, char *name, char *type,
char *data, unsigned int ttl, unsigned int flags)
{
int i;
int x;
ldap_info *tmp;
int attrlist;
char ldap_type_buffer[128];
char charttl[64];
if ((tmp = locate_by_dn (dn)) == NULL)
{
/* There wasn't one already there, so we need to allocate a new one,
* and stick it on the list */
tmp = (ldap_info *) malloc (sizeof (ldap_info));
if (tmp == (ldap_info *) NULL)
{
fprintf (stderr, "malloc: %s\n", strerror (errno));
#ifndef OFFLINE
ldap_unbind_s (conn);
#endif
exit (-1);
}
tmp->dn = strdup (dn);
tmp->attrs = (LDAPMod **) calloc (sizeof (LDAPMod *), flags);
if (tmp->attrs == (LDAPMod **) NULL)
{
fprintf (stderr, "calloc: %s\n", strerror (errno));
#ifndef OFFLINE
ldap_unbind_s (conn);
#endif
exit (-1);
}
for (i = 0; i < flags; i++)
{
tmp->attrs[i] = (LDAPMod *) malloc (sizeof (LDAPMod));
if (tmp->attrs[i] == (LDAPMod *) NULL)
{
fprintf (stderr, "malloc: %s\n", strerror (errno));
exit (-1);
}
}
tmp->attrs[0]->mod_op = LDAP_MOD_ADD;
tmp->attrs[0]->mod_type = "objectClass";
if (flags == DNS_OBJECT)
tmp->attrs[0]->mod_values = objectClasses;
else
{
tmp->attrs[0]->mod_values = topObjectClasses;
tmp->attrs[1] = NULL;
tmp->attrcnt = 2;
tmp->next = ldap_info_base;
ldap_info_base = tmp;
return;
}
tmp->attrs[1]->mod_op = LDAP_MOD_ADD;
tmp->attrs[1]->mod_type = "relativeDomainName";
tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
if (tmp->attrs[1]->mod_values == (char **)NULL)
exit(-1);
tmp->attrs[1]->mod_values[0] = strdup (name);
tmp->attrs[1]->mod_values[2] = NULL;
sprintf (ldap_type_buffer, "%sRecord", type);
tmp->attrs[2]->mod_op = LDAP_MOD_ADD;
tmp->attrs[2]->mod_type = strdup (ldap_type_buffer);
tmp->attrs[2]->mod_values = (char **) calloc (sizeof (char *), 2);
if (tmp->attrs[2]->mod_values == (char **)NULL)
exit(-1);
tmp->attrs[2]->mod_values[0] = strdup (data);
tmp->attrs[2]->mod_values[1] = NULL;
tmp->attrs[3]->mod_op = LDAP_MOD_ADD;
tmp->attrs[3]->mod_type = "dNSTTL";
tmp->attrs[3]->mod_values = (char **) calloc (sizeof (char *), 2);
if (tmp->attrs[3]->mod_values == (char **)NULL)
exit(-1);
sprintf (charttl, "%d", ttl);
tmp->attrs[3]->mod_values[0] = strdup (charttl);
tmp->attrs[3]->mod_values[1] = NULL;
tmp->attrs[4]->mod_op = LDAP_MOD_ADD;
tmp->attrs[4]->mod_type = "zoneName";
tmp->attrs[4]->mod_values = (char **)calloc(sizeof(char *), 2);
tmp->attrs[4]->mod_values[0] = gbl_zone;
tmp->attrs[4]->mod_values[1] = NULL;
tmp->attrs[5] = add_aci_values ();
tmp->attrs[6] = NULL;
tmp->attrcnt = flags;
tmp->next = ldap_info_base;
ldap_info_base = tmp;
}
else
{
for (i = 0; tmp->attrs[i] != NULL; i++)
{
sprintf (ldap_type_buffer, "%sRecord", type);
if (!strncmp
(ldap_type_buffer, tmp->attrs[i]->mod_type,
strlen (tmp->attrs[i]->mod_type)))
{
attrlist = get_attr_list_size (tmp->attrs[i]->mod_values);
tmp->attrs[i]->mod_values =
(char **) realloc (tmp->attrs[i]->mod_values,
sizeof (char *) * (attrlist + 1));
if (tmp->attrs[i]->mod_values == (char **) NULL)
{
fprintf (stderr, "realloc: %s\n", strerror (errno));
#ifndef OFFLINE
ldap_unbind_s (conn);
#endif
exit (-1);
}
for (x = 0; tmp->attrs[i]->mod_values[x] != NULL; x++);
tmp->attrs[i]->mod_values[x] = strdup (data);
tmp->attrs[i]->mod_values[x + 1] = NULL;
return;
}
}
tmp->attrs =
(LDAPMod **) realloc (tmp->attrs,
sizeof (LDAPMod) * ++(tmp->attrcnt));
if (tmp->attrs == NULL)
{
fprintf (stderr, "realloc: %s\n", strerror (errno));
#ifndef OFFLINE
ldap_unbind_s (conn);
#endif
exit (-1);
}
for (x = 0; tmp->attrs[x] != NULL; x++);
tmp->attrs[x] = (LDAPMod *) malloc (sizeof (LDAPMod));
tmp->attrs[x]->mod_op = LDAP_MOD_ADD;
tmp->attrs[x]->mod_type = strdup (ldap_type_buffer);
tmp->attrs[x]->mod_values = (char **) calloc (sizeof (char *), 2);
tmp->attrs[x]->mod_values[0] = strdup (data);
tmp->attrs[x]->mod_values[1] = NULL;
tmp->attrs[x + 1] = NULL;
}
}
/* Size of a mod_values list, plus the terminating NULL field. */
int
get_attr_list_size (char **tmp)
{
int i = 0;
char **ftmp = tmp;
while (*ftmp != NULL)
{
i++;
ftmp++;
}
return ++i;
}
/* take a hostname, and split it into a char ** of the dc parts,
* example, we have www.domain.com, this function will return:
* array[0] = com, array[1] = domain, array[2] = www. */
char **
hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
{
char *tmp;
static char *dn_buffer[64];
int i = 0;
char *zname;
char *hnamebuff;
zname = strdup (hostname);
if (flags == DNS_OBJECT)
{
if (strlen (zname) != strlen (zone))
{
tmp = &zname[strlen (zname) - strlen (zone)];
*--tmp = '\0';
hnamebuff = strdup (zname);
zname = ++tmp;
}
else
hnamebuff = "@";
}
else
{
zname = zone;
hnamebuff = NULL;
}
for (tmp = strrchr (zname, '.'); tmp != (char *) 0;
tmp = strrchr (zname, '.'))
{
*tmp++ = '\0';
dn_buffer[i++] = tmp;
}
dn_buffer[i++] = zname;
dn_buffer[i++] = hnamebuff;
dn_buffer[i] = NULL;
return dn_buffer;
}
/* build an sdb compatible LDAP DN from a "dc_list" (char **).
* will append dNSTTL information to each RR Record, with the
* exception of "@"/SOA. */
char *
build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag)
{
int size;
int x;
static char dn[1024];
char tmp[128];
bzero (tmp, sizeof (tmp));
bzero (dn, sizeof (dn));
size = get_attr_list_size (dc_list);
for (x = size - 2; x > 0; x--)
{
if (flag == WI_SPEC)
{
if (x == (size - 2) && (strncmp (dc_list[x], "@", 1) == 0) && (ttl))
sprintf (tmp, "relativeDomainName=%s + dNSTTL=%d,", dc_list[x], ttl);
else if (x == (size - 2))
sprintf(tmp, "relativeDomainName=%s,",dc_list[x]);
else
sprintf(tmp,"dc=%s,", dc_list[x]);
}
else
{
sprintf(tmp, "dc=%s,", dc_list[x]);
}
strncat (dn, tmp, sizeof (dn) - strlen (dn));
}
sprintf (tmp, "dc=%s", dc_list[0]);
strncat (dn, tmp, sizeof (dn) - strlen (dn));
fflush(NULL);
return dn;
}
#ifndef OFFLINE
/* Initialize LDAP Conn */
void
init_ldap_conn ()
{
int result, version;
#if defined(SECUREBIND_SASL)
void *defaults;
char *id = NULL;
if (authmethod == LDAP_AUTH_SASL)
defaults = lutil_sasl_defaults (conn, sasl_mech, sasl_realm, sasl_authc_id, bindpw, sasl_authz_id);
#endif
if(use_tls
#if defined(SECUREBIND_SASL)
|| (authmethod == LDAP_AUTH_SASL)
#endif
)
{
/* Make sure we use LDAPv3 when trying TLS or SASL (a requirenment). */
version = LDAP_VERSION3;
if(ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, &version) != LDAP_OPT_SUCCESS ) {
fprintf (stderr, "Failed setting LDAP version to LDAPv3: %s\n", strerror (errno));
exit (-1);
}
}
if (debug) {
#if defined(SECUREBIND_SASL)
if (authmethod == LDAP_AUTH_SASL) {
if(sasl_authc_id)
id = sasl_authc_id;
else if(sasl_authz_id)
id = sasl_authz_id;
printf ("Initializing LDAP Connection to %s as %s@%s\n", ldapsystem, id, sasl_realm);
} else
#endif
printf ("Initializing LDAP Connection to %s as %s\n", ldapsystem, binddn[0]);
/*
if (ber_set_option (NULL, LBER_OPT_DEBUG_LEVEL, "-1") != LBER_OPT_SUCCESS)
fprintf (stderr, "Could not set LBER_OPT_DEBUG_LEVEL -1\n");
if (ldap_set_option (NULL, LDAP_OPT_DEBUG_LEVEL, "-1") != LDAP_OPT_SUCCESS)
fprintf (stderr, "Could not set LDAP_OPT_DEBUG_LEVEL -1\n");
*/
}
if (use_uri || use_ssl) {
/* Connect to LDAP server using either LDAP URI (ldapi) or SSL (ldaps). */
result = ldap_initialize(&conn, ldapsystem);
if(result != LDAP_SUCCESS) {
fprintf (stderr, "Error with ldap_initialize(): %s\n", strerror (errno));
exit (-1);
}
} else {
conn = ldap_open (ldapsystem, LDAP_PORT);
if (conn == NULL)
{
fprintf (stderr, "Error opening Ldap connection: %s\n",
strerror (errno));
exit (-1);
}
}
/* Directly stolen from the ldapsearch.c file in OpenLDAP (v2.0.27) */
if (use_tls) {
if (debug)
printf ("Issuing StartTLS extended operations\n");
/* Initialize the TLS connection */
if(ldap_start_tls_s(conn, NULL, NULL) != LDAP_SUCCESS) {
if(use_tls > 1) {
fprintf (stderr, "Starting TLS failed: %s\n", strerror (errno));
exit (-1);
} else
ldap_perror(conn, "ldap_start_tls_s");
}
}
#if defined(SECUREBIND_SASL)
if (authmethod == LDAP_AUTH_SASL) {
if (sasl_secprops != NULL ) {
result = ldap_set_option (conn, LDAP_OPT_X_SASL_SECPROPS, (void *) sasl_secprops );
if( result != LDAP_OPT_SUCCESS ) {
fprintf (stderr, "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n", sasl_secprops );
exit (-1);
}
}
result = ldap_sasl_interactive_bind_s (conn, binddn[0], sasl_mech, NULL, NULL,
sasl_flags, lutil_sasl_interact, defaults);
lutil_sasl_freedefs (defaults);
if (result != LDAP_SUCCESS ) {
ldap_perror (conn, "ldap_sasl_interactive_bind_s");
exit (-1);
}
} else {
#endif
result = ldap_simple_bind_s (conn, binddn[0], bindpw);
ldap_result_check ("ldap_simple_bind_s", "LDAP Bind", result);
#if defined(SECUREBIND_SASL)
}
#endif
}
/* Like isc_result_check, only for LDAP */
void
ldap_result_check (char *msg, char *dn, int err)
{
if ((err != LDAP_SUCCESS) && (err != LDAP_ALREADY_EXISTS))
{
fprintf(stderr, "Error while adding %s (%s):\n",
dn, msg);
ldap_perror (conn, dn);
ldap_unbind_s (conn);
exit (-1);
}
}
/* For running the ldap_info run queue. */
void
add_ldap_values (ldap_info * ldinfo)
{
int result;
char dnbuffer[1024];