📄 zone2ldap.c
字号:
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*/voidadd_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)); ldap_unbind_s (conn); 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)); ldap_unbind_s (conn); 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] = 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)); ldap_unbind_s (conn); 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)); ldap_unbind_s (conn); 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. */intget_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;}/* Initialize LDAP Conn */voidinit_ldap_conn (){ int result; conn = ldap_open (ldapsystem, LDAP_PORT); if (conn == NULL) { fprintf (stderr, "Error opening Ldap connection: %s\n", strerror (errno)); exit (-1); } result = ldap_simple_bind_s (conn, binddn, bindpw); ldap_result_check ("ldap_simple_bind_s", "LDAP Bind", result);}/* Like isc_result_check, only for LDAP */voidldap_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. */voidadd_ldap_values (ldap_info * ldinfo){ int result; char dnbuffer[1024]; if (ldapbase != NULL) sprintf (dnbuffer, "%s,%s", ldinfo->dn, ldapbase); else sprintf (dnbuffer, "%s", ldinfo->dn); result = ldap_add_s (conn, dnbuffer, ldinfo->attrs); ldap_result_check ("ldap_add_s", dnbuffer, result);}/* name says it all */voidusage (){ fprintf (stderr, "zone2ldap -D [BIND DN] -w [BIND PASSWORD] -b [BASE DN] -z [ZONE] -f [ZONE FILE] -h [LDAP HOST] [-c Create LDAP Base structure][-d Debug Output (lots !)] \n ");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -