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