2e2c49
/*
2e2c49
 * Copyright (C) 2004, 2005 Stig Venaas <venaas@uninett.no>
2e2c49
 * $Id: ldap2zone.c,v 1.1 2007/07/24 15:18:00 atkac Exp $
2e2c49
 *
2e2c49
 * Permission to use, copy, modify, and distribute this software for any
2e2c49
 * purpose with or without fee is hereby granted, provided that the above
2e2c49
 * copyright notice and this permission notice appear in all copies.
2e2c49
 */
2e2c49
2e2c49
#define LDAP_DEPRECATED 1
2e2c49
2e2c49
#include <sys/types.h>
2e2c49
#include <stdio.h>
2e2c49
#include <stdlib.h>
2e2c49
#include <ctype.h>
2e2c49
2e2c49
#include <ldap.h>
2e2c49
2e2c49
struct string {
2e2c49
    void *data;
2e2c49
    size_t len;
2e2c49
};
2e2c49
2e2c49
struct assstack_entry {
2e2c49
    struct string key;
2e2c49
    struct string val;
2e2c49
    struct assstack_entry *next;
2e2c49
};
2e2c49
2e2c49
struct assstack_entry *assstack_find(struct assstack_entry *stack, struct string *key);
2e2c49
void assstack_push(struct assstack_entry **stack, struct assstack_entry *item);
2e2c49
void assstack_insertbottom(struct assstack_entry **stack, struct assstack_entry *item);
2e2c49
void printsoa(struct string *soa);
2e2c49
void printrrs(char *defaultttl, struct assstack_entry *item);
2e2c49
void print_zone(char *defaultttl, struct assstack_entry *stack);
2e2c49
void usage(char *name);
2e2c49
void err(char *name, const char *msg);
2e2c49
int putrr(struct assstack_entry **stack, struct berval *name, char *type, char *ttl, struct berval *val);
2e2c49
2e2c49
struct assstack_entry *assstack_find(struct assstack_entry *stack, struct string *key) {
2e2c49
    for (; stack; stack = stack->next)
2e2c49
	if (stack->key.len == key->len && !memcmp(stack->key.data, key->data, key->len))
2e2c49
	    return stack;
2e2c49
    return NULL;
2e2c49
}
2e2c49
2e2c49
void assstack_push(struct assstack_entry **stack, struct assstack_entry *item) {
2e2c49
    item->next = *stack;
2e2c49
    *stack = item;
2e2c49
}
2e2c49
2e2c49
void assstack_insertbottom(struct assstack_entry **stack, struct assstack_entry *item) {
2e2c49
    struct assstack_entry *p;
2e2c49
    
2e2c49
    item->next = NULL;
2e2c49
    if (!*stack) {
2e2c49
	*stack = item;
2e2c49
	return;
2e2c49
    }
2e2c49
    /* find end, should keep track of end somewhere */
2e2c49
    /* really a queue, not a stack */
2e2c49
    p = *stack;
2e2c49
    while (p->next)
2e2c49
	p = p->next;
2e2c49
    p->next = item;
2e2c49
}
2e2c49
2e2c49
void printsoa(struct string *soa) {
2e2c49
    char *s;
2e2c49
    size_t i;
2e2c49
    
2e2c49
    s = (char *)soa->data;
2e2c49
    i = 0;
2e2c49
    while (i < soa->len) {
2e2c49
	putchar(s[i]);
2e2c49
	if (s[i++] == ' ')
2e2c49
	    break;
2e2c49
    }
2e2c49
    while (i < soa->len) {
2e2c49
	putchar(s[i]);
2e2c49
	if (s[i++] == ' ')
2e2c49
	    break;
2e2c49
    } 
2e2c49
    printf("(\n\t\t\t\t");
2e2c49
    while (i < soa->len) {
2e2c49
	putchar(s[i]);
2e2c49
	if (s[i++] == ' ')
2e2c49
	    break;
2e2c49
    }
2e2c49
    printf("; Serialnumber\n\t\t\t\t");
2e2c49
    while (i < soa->len) {
2e2c49
	if (s[i] == ' ')
2e2c49
	    break;
2e2c49
	putchar(s[i++]);
2e2c49
    }
2e2c49
    i++;
2e2c49
    printf("\t; Refresh\n\t\t\t\t");
2e2c49
    while (i < soa->len) {
2e2c49
	if (s[i] == ' ')
2e2c49
	    break;
2e2c49
	putchar(s[i++]);
2e2c49
    }
2e2c49
    i++;
2e2c49
    printf("\t; Retry\n\t\t\t\t");
2e2c49
    while (i < soa->len) {
2e2c49
	if (s[i] == ' ')
2e2c49
	    break;
2e2c49
	putchar(s[i++]);
2e2c49
    }
2e2c49
    i++;
2e2c49
    printf("\t; Expire\n\t\t\t\t");
2e2c49
    while (i < soa->len) {
2e2c49
	putchar(s[i++]);
2e2c49
    }
2e2c49
    printf(" )\t; Minimum TTL\n");
2e2c49
}
2e2c49
2e2c49
void printrrs(char *defaultttl, struct assstack_entry *item) {
2e2c49
    struct assstack_entry *stack;
2e2c49
    char *s;
2e2c49
    int first;
2e2c49
    size_t i;
2e2c49
    char *ttl, *type;
2e2c49
    int top;
2e2c49
    
2e2c49
    s = (char *)item->key.data;
2e2c49
2e2c49
    if (item->key.len == 1 && *s == '@') {
2e2c49
	top = 1;
2e2c49
	printf("@\t");
2e2c49
    } else {
2e2c49
	top = 0;
2e2c49
	for (i = 0; i < item->key.len; i++)
2e2c49
	    putchar(s[i]);
2e2c49
	if (item->key.len < 8)
2e2c49
	    putchar('\t');
2e2c49
	putchar('\t');
2e2c49
    }
2e2c49
    
2e2c49
    first = 1;
2e2c49
    for (stack = (struct assstack_entry *) item->val.data; stack; stack = stack->next) {
2e2c49
	ttl = (char *)stack->key.data;
2e2c49
	s = strchr(ttl, ' ');
2e2c49
	*s++ = '\0';
2e2c49
	type = s;
2e2c49
	
2e2c49
	if (first)
2e2c49
	    first = 0;
2e2c49
        else
2e2c49
	    printf("\t\t");
2e2c49
	    
2e2c49
	if (strcmp(defaultttl, ttl))
2e2c49
	    printf("%s", ttl);
2e2c49
	putchar('\t');
2e2c49
	
2e2c49
	if (top) {
2e2c49
	    top = 0;
2e2c49
	    printf("IN\t%s\t", type);
2e2c49
	    /* Should always be SOA here */
2e2c49
	    if (!strcmp(type, "SOA")) {
2e2c49
		printsoa(&stack->val);
2e2c49
		continue;
2e2c49
	    }
2e2c49
	} else
2e2c49
	    printf("%s\t", type);
2e2c49
2e2c49
	s = (char *)stack->val.data;
2e2c49
	for (i = 0; i < stack->val.len; i++)
2e2c49
	    putchar(s[i]);
2e2c49
	putchar('\n');
2e2c49
    }
2e2c49
}
2e2c49
2e2c49
void print_zone(char *defaultttl, struct assstack_entry *stack) {
2e2c49
    printf("$TTL %s\n", defaultttl);
2e2c49
    for (; stack; stack = stack->next)
2e2c49
	printrrs(defaultttl, stack);
2e2c49
};
2e2c49
2e2c49
void usage(char *name) {
2e2c49
    fprintf(stderr, "Usage:%s zone-name LDAP-URL default-ttl [serial]\n", name);
2e2c49
    exit(1);
2e2c49
};
2e2c49
2e2c49
void err(char *name, const char *msg) {
2e2c49
    fprintf(stderr, "%s: %s\n", name, msg);
2e2c49
    exit(1);
2e2c49
};
2e2c49
2e2c49
int putrr(struct assstack_entry **stack, struct berval *name, char *type, char *ttl, struct berval *val) {
2e2c49
    struct string key;
2e2c49
    struct assstack_entry *rr, *rrdata;
2e2c49
    
2e2c49
    /* Do nothing if name or value have 0 length */
2e2c49
    if (!name->bv_len || !val->bv_len)
2e2c49
	return 0;
2e2c49
2e2c49
    /* see if already have an entry for this name */
2e2c49
    key.len = name->bv_len;
2e2c49
    key.data = name->bv_val;
2e2c49
2e2c49
    rr = assstack_find(*stack, &key);
2e2c49
    if (!rr) {
2e2c49
	/* Not found, create and push new entry */
2e2c49
	rr = (struct assstack_entry *) malloc(sizeof(struct assstack_entry));
2e2c49
	if (!rr)
2e2c49
	    return -1;
2e2c49
	rr->key.len = name->bv_len;
2e2c49
	rr->key.data = (void *) malloc(rr->key.len);
2e2c49
	if (!rr->key.data) {
2e2c49
	    free(rr);
2e2c49
	    return -1;
2e2c49
	}
2e2c49
	memcpy(rr->key.data, name->bv_val, name->bv_len);
2e2c49
	rr->val.len = sizeof(void *);
2e2c49
	rr->val.data = NULL;
2e2c49
	if (name->bv_len == 1 && *(char *)name->bv_val == '@')
2e2c49
	    assstack_push(stack, rr);
2e2c49
	else
2e2c49
	    assstack_insertbottom(stack, rr);
2e2c49
    }
2e2c49
2e2c49
    rrdata = (struct assstack_entry *) malloc(sizeof(struct assstack_entry));
2e2c49
    if (!rrdata) {
2e2c49
	free(rr->key.data);
2e2c49
	free(rr);
2e2c49
	return -1;
2e2c49
    }
2e2c49
    rrdata->key.len = strlen(type) + strlen(ttl) + 1;
2e2c49
    rrdata->key.data = (void *) malloc(rrdata->key.len);
2e2c49
    if (!rrdata->key.data) {
2e2c49
	free(rrdata);
2e2c49
	free(rr->key.data);
2e2c49
	free(rr);
2e2c49
	return -1;
2e2c49
    }
2e2c49
    sprintf((char *)rrdata->key.data, "%s %s", ttl, type);
2e2c49
	
2e2c49
    rrdata->val.len = val->bv_len;
2e2c49
    rrdata->val.data = (void *) malloc(val->bv_len);
2e2c49
    if (!rrdata->val.data) {
2e2c49
	free(rrdata->key.data);
2e2c49
	free(rrdata);
2e2c49
	free(rr->key.data);
2e2c49
	free(rr);
2e2c49
	return -1;
2e2c49
    }
2e2c49
    memcpy(rrdata->val.data, val->bv_val, val->bv_len);
2e2c49
2e2c49
    if (!strcmp(type, "SOA"))
2e2c49
	assstack_push((struct assstack_entry **) &(rr->val.data), rrdata);
2e2c49
    else
2e2c49
	assstack_insertbottom((struct assstack_entry **) &(rr->val.data), rrdata);
2e2c49
    return 0;
2e2c49
}
2e2c49
2e2c49
int main(int argc, char **argv) {
2e2c49
    char *s, *hostporturl, *base = NULL;
2e2c49
    char *ttl, *defaultttl;
2e2c49
    LDAP *ld;
2e2c49
    char *fltr = NULL;
2e2c49
    LDAPMessage *res, *e;
2e2c49
    char *a, **ttlvals, **soavals, *serial;
2e2c49
    struct berval **vals, **names;
2e2c49
    char type[64];
2e2c49
    BerElement *ptr;
2e2c49
    int i, j, rc, msgid;
2e2c49
    struct assstack_entry *zone = NULL;
2e2c49
    
2e2c49
    if (argc < 4 || argc > 5)
2e2c49
        usage(argv[0]);
2e2c49
2e2c49
    hostporturl = argv[2];
2e2c49
2e2c49
    if (hostporturl != strstr( hostporturl, "ldap"))
2e2c49
	err(argv[0], "Not an LDAP URL");
2e2c49
2e2c49
    s = strchr(hostporturl, ':');
2e2c49
2e2c49
    if (!s || strlen(s) < 3 || s[1] != '/' || s[2] != '/')
2e2c49
	err(argv[0], "Not an LDAP URL");
2e2c49
2e2c49
    s = strchr(s+3, '/');
2e2c49
    if (s) {
2e2c49
	*s++ = '\0';
2e2c49
	base = s;
2e2c49
	s = strchr(base, '?');
2e2c49
	if (s)
2e2c49
	    err(argv[0], "LDAP URL can only contain host, port and base");
2e2c49
    }
2e2c49
2e2c49
    defaultttl = argv[3];
2e2c49
    
2e2c49
    rc = ldap_initialize(&ld, hostporturl);
2e2c49
    if (rc != LDAP_SUCCESS)
2e2c49
	err(argv[0], "ldap_initialize() failed");
2e2c49
2e2c49
    if (argc == 5) {
2e2c49
	/* serial number specified, check if different from one in SOA */
2e2c49
	fltr = (char *)malloc(strlen(argv[1]) + strlen("(&(relativeDomainName=@)(zoneName=))") + 1);
2e2c49
	sprintf(fltr, "(&(relativeDomainName=@)(zoneName=%s))", argv[1]);
2e2c49
	msgid = ldap_search(ld, base, LDAP_SCOPE_SUBTREE, fltr, NULL, 0);
2e2c49
	if (msgid == -1)
2e2c49
	    err(argv[0], "ldap_search() failed");
2e2c49
2e2c49
	while ((rc = ldap_result(ld, msgid, 0, NULL, &res)) != LDAP_RES_SEARCH_RESULT ) {
2e2c49
	    /* not supporting continuation references at present */
2e2c49
	    if (rc != LDAP_RES_SEARCH_ENTRY)
2e2c49
		err(argv[0], "ldap_result() returned cont.ref? Exiting");
2e2c49
2e2c49
	    /* only one entry per result message */
2e2c49
	    e = ldap_first_entry(ld, res);
2e2c49
	    if (e == NULL) {
2e2c49
		ldap_msgfree(res);
2e2c49
		err(argv[0], "ldap_first_entry() failed");
2e2c49
	    }
2e2c49
	
2e2c49
	    soavals = ldap_get_values(ld, e, "SOARecord");
2e2c49
	    if (soavals)
2e2c49
		break;
2e2c49
	}
2e2c49
2e2c49
	ldap_msgfree(res);
2e2c49
	if (!soavals) {
2e2c49
		err(argv[0], "No SOA Record found");
2e2c49
	}
2e2c49
	
2e2c49
	/* We have a SOA, compare serial numbers */
2e2c49
	/* Only checkinf first value, should be only one */
2e2c49
	s = strchr(soavals[0], ' ');
2e2c49
	s++;
2e2c49
	s = strchr(s, ' ');
2e2c49
	s++;
2e2c49
	serial = s;
2e2c49
	s = strchr(s, ' ');
2e2c49
	*s = '\0';
2e2c49
	if (!strcmp(serial, argv[4])) {
2e2c49
	    ldap_value_free(soavals);
2e2c49
	    err(argv[0], "serial numbers match");
2e2c49
	}
2e2c49
	ldap_value_free(soavals);
2e2c49
    }
2e2c49
2e2c49
    if (!fltr)
2e2c49
	fltr = (char *)malloc(strlen(argv[1]) + strlen("(zoneName=)") + 1);
2e2c49
    if (!fltr)
2e2c49
	err(argv[0], "Malloc failed");
2e2c49
    sprintf(fltr, "(zoneName=%s)", argv[1]);
2e2c49
2e2c49
    msgid = ldap_search(ld, base, LDAP_SCOPE_SUBTREE, fltr, NULL, 0);
2e2c49
    if (msgid == -1)
2e2c49
	err(argv[0], "ldap_search() failed");
2e2c49
2e2c49
    while ((rc = ldap_result(ld, msgid, 0, NULL, &res)) != LDAP_RES_SEARCH_RESULT ) {
2e2c49
	/* not supporting continuation references at present */
2e2c49
	if (rc != LDAP_RES_SEARCH_ENTRY)
2e2c49
	    err(argv[0], "ldap_result() returned cont.ref? Exiting");
2e2c49
2e2c49
	/* only one entry per result message */
2e2c49
	e = ldap_first_entry(ld, res);
2e2c49
	if (e == NULL) {
2e2c49
	    ldap_msgfree(res);
2e2c49
	    err(argv[0], "ldap_first_entry() failed");
2e2c49
	}
2e2c49
	
2e2c49
	names = ldap_get_values_len(ld, e, "relativeDomainName");
2e2c49
	if (!names)
2e2c49
	    continue;
2e2c49
	
2e2c49
	ttlvals = ldap_get_values(ld, e, "dNSTTL");
2e2c49
	ttl = ttlvals ? ttlvals[0] : defaultttl;
2e2c49
2e2c49
	for (a = ldap_first_attribute(ld, e, &ptr); a != NULL; a = ldap_next_attribute(ld, e, ptr)) {
2e2c49
	    char *s;
2e2c49
2e2c49
	    for (s = a; *s; s++)
2e2c49
		*s = toupper(*s);
2e2c49
	    s = strstr(a, "RECORD");
2e2c49
	    if ((s == NULL) || (s == a) || (s - a >= (signed int)sizeof(type))) {
2e2c49
		ldap_memfree(a);
2e2c49
		continue;
2e2c49
	    }
2e2c49
			
2e2c49
	    strncpy(type, a, s - a);
2e2c49
	    type[s - a] = '\0';
2e2c49
	    vals = ldap_get_values_len(ld, e, a);
2e2c49
	    if (vals) {
2e2c49
		for (i = 0; vals[i]; i++)
2e2c49
		    for (j = 0; names[j]; j++)
2e2c49
			if (putrr(&zone, names[j], type, ttl, vals[i]))
2e2c49
			    err(argv[0], "malloc failed");
2e2c49
		ldap_value_free_len(vals);
2e2c49
	    }
2e2c49
	    ldap_memfree(a);
2e2c49
	}
2e2c49
2e2c49
	if (ptr)
2e2c49
	    ber_free(ptr, 0);
2e2c49
	if (ttlvals)
2e2c49
	    ldap_value_free(ttlvals);
2e2c49
	ldap_value_free_len(names);
2e2c49
	/* free this result */
2e2c49
	ldap_msgfree(res);
2e2c49
    }
2e2c49
2e2c49
    /* free final result */
2e2c49
    ldap_msgfree(res);
2e2c49
2e2c49
    print_zone(defaultttl, zone);
2e2c49
    return 0;
2e2c49
}