Description: Make output stable.
 Make sure the zone ordering is the same every time, no matter what ordering
 the LDAP server provided.  Rewrite the program to keep the "stack" sorted
 instead of appending LDAP replies to the end.
Author: Petter Reinholdtsen <pere@hungry.com>
Bug-Debian: http://bugs.debian.org/710222
Last-Update: 2014-09-29

Index: ldap2zone-0.2/ldap2zone.c
===================================================================
--- ldap2zone-0.2.orig/ldap2zone.c	2014-09-29 23:13:05.359989217 +0200
+++ ldap2zone-0.2/ldap2zone.c	2014-09-29 23:45:48.368033429 +0200
@@ -39,7 +39,7 @@
     *stack = item;
 }
 
-void assstack_insertbottom(struct assstack_entry **stack, struct assstack_entry *item) {
+void assstack_insertsorted(struct assstack_entry **stack, struct assstack_entry *item) {
     struct assstack_entry *p;
     
     item->next = NULL;
@@ -47,11 +47,17 @@
 	*stack = item;
 	return;
     }
-    /* find end, should keep track of end somewhere */
-    /* really a queue, not a stack */
+    /* find right place to insert the entry, keeping list sorted with @
+       at the start. */
     p = *stack;
-    while (p->next)
+    while (p->next &&
+	   0 > strncmp((char*)p->next->key.data, (char*)item->key.data,
+		       ( p->next->key.len > item->key.len ?
+			 item->key.len : p->key.len) ) ) {
 	p = p->next;
+    }
+
+    item->next = p->next;
     p->next = item;
 }
 
@@ -207,7 +213,7 @@
 	if (name->bv_len == 1 && *(char *)name->bv_val == '@')
 	    assstack_push(stack, rr);
 	else
-	    assstack_insertbottom(stack, rr);
+	    assstack_insertsorted(stack, rr);
     }
 
     rrdata = (struct assstack_entry *) malloc(sizeof(struct assstack_entry));
@@ -240,7 +246,7 @@
     if (!strcmp(type, "SOA"))
 	assstack_push((struct assstack_entry **) &(rr->val.data), rrdata);
     else
-	assstack_insertbottom((struct assstack_entry **) &(rr->val.data), rrdata);
+	assstack_insertsorted((struct assstack_entry **) &(rr->val.data), rrdata);
     return 0;
 }
 
