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