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