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