| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Create data from an LDAP directory service to be used for tinydns | 
					
						
							| 
									
										
										
										
											2005-12-11 15:29:00 +00:00
										 |  |  |  * $Id$ | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  |  * Copyright 2005-2006 by Ben Klang <ben@alkaloid.net> | 
					
						
							| 
									
										
										
										
											2005-12-08 17:27:56 +00:00
										 |  |  |  * Copyright 2000-2005 by Jacob Rief <jacob.rief@tiscover.com> | 
					
						
							|  |  |  |  * License: GPL version 2. See http://www.fsf.org for details
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <lber.h>
 | 
					
						
							|  |  |  | #include <ldap.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <assert.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | #include <getopt.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-12 21:50:07 +00:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | #define UPDATE_INTERVAL 59
 | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | #define LDAP_CONF "/etc/ldap.conf"
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | #define OUTPUT_DATA 1
 | 
					
						
							|  |  |  | #define OUTPUT_DB 2
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | #define MAXHOSTS 10
 | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | #define DEF_SEARCHTIMEOUT 40
 | 
					
						
							|  |  |  | #define DEF_RECLIMIT 5000
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char tinydns_textfile[256]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | static char tinydns_texttemp[256]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | static LDAP* ldap_con; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | static FILE* namedmaster; | 
					
						
							|  |  |  | static FILE* namedzone; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | static FILE* tinyfile; | 
					
						
							|  |  |  | static FILE* ldifout; | 
					
						
							|  |  |  | static time_t time_now; | 
					
						
							|  |  |  | static char* const* main_argv; | 
					
						
							|  |  |  | static int main_argc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void print_version(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	printf("ldap2dns version %s\n", VERSION); | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	printf("  Copyright 2005-2006 by Ben Klang <ben@alkaloid.net>\n"); | 
					
						
							|  |  |  | 	printf("  Copyright 2000-2005 by Jacob Rief <jacob.rief@tiscover.com>\n"); | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	printf("  Released under the terms of the GPL.\n"); | 
					
						
							| 
									
										
										
										
											2005-12-19 06:08:17 +00:00
										 |  |  | 	printf("  http://projects.alkaloid.net\n"); | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | 	printf("\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void die_ldap(int err) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-06-04 01:45:56 +00:00
										 |  |  | 	fprintf(stderr, "Fatal LDAP error: %s\n", ldap_err2string(err)); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char domainname[64]; | 
					
						
							|  |  |  | 	char zonemaster[64]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	char class[16]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	char adminmailbox[64]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 	char serial[12]; | 
					
						
							|  |  |  | 	char refresh[12]; | 
					
						
							|  |  |  | 	char retry[12]; | 
					
						
							|  |  |  | 	char expire[12]; | 
					
						
							|  |  |  | 	char minimum[12]; | 
					
						
							|  |  |  | 	char ttl[12]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	char timestamp[20]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	char location[2]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | } zone; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | static struct | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char locname[3]; | 
					
						
							|  |  |  | 	char member[256][16]; | 
					
						
							|  |  |  | } loc_rec; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | struct resourcerecord | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char cn[64]; | 
					
						
							|  |  |  | 	char dnsdomainname[64]; | 
					
						
							|  |  |  | 	char class[16]; | 
					
						
							|  |  |  | 	char type[16]; | 
					
						
							|  |  |  | 	char ipaddr[256][32]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	char cipaddr[32]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	char cname[64]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 	char ttl[12]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	char timestamp[20]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 	char preference[12]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	char location[2]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | #if defined DRAFT_RFC
 | 
					
						
							|  |  |  | 	char rr[1024]; | 
					
						
							|  |  |  | 	char aliasedobjectname[256]; | 
					
						
							|  |  |  | 	char macaddress[32]; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:28:28 +00:00
										 |  |  | 	int dnssrvpriority; | 
					
						
							|  |  |  | 	int dnssrvweight; | 
					
						
							|  |  |  | 	int dnssrvport; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char searchbase[128]; | 
					
						
							|  |  |  | 	char binddn[128]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 	char hostname[MAXHOSTS][128]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	char urildap[MAXHOSTS][128]; | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 	unsigned short port[MAXHOSTS]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	char password[128]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 	int usedhosts; | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 	int useduris; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	int is_daemon; | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 	unsigned int update_iv; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	unsigned int output; | 
					
						
							|  |  |  | 	int verbose; | 
					
						
							|  |  |  | 	char ldifname[128]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	char exec_command[128]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	int use_tls[MAXHOSTS]; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	struct timeval searchtimeout; | 
					
						
							|  |  |  | 	int reclimit; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | } options; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void die_exit(const char* message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (message) | 
					
						
							|  |  |  | 		fprintf(stderr, "Fatal error: %s\n", message); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		fprintf(stderr, "Fatal memory error\n"); | 
					
						
							|  |  |  | 	exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void set_datadir(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char* ev = getenv("TINYDNSDIR"); | 
					
						
							|  |  |  | 	int len; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	tinydns_textfile[0] = '\0'; | 
					
						
							|  |  |  | 	tinydns_texttemp[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	if (ev && (len = strlen(ev))<240) { | 
					
						
							|  |  |  | 		strncpy(tinydns_textfile, ev, 240); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		strncpy(tinydns_texttemp, ev, 240); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		if (ev[len-1]!='/') { | 
					
						
							|  |  |  | 			tinydns_textfile[len] = '/'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			tinydns_texttemp[len] = '/'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	strcat(tinydns_textfile, "data"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	strcat(tinydns_texttemp, "data.temp"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void print_usage(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	print_version(); | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	printf("usage: ldap2dns[d] [-o data|db] [-h host] [-p port] [-H hostURI] \\\n"); | 
					
						
							|  |  |  | 	printf("\t\t[-D binddn] [-w password] [-L[filename]] [-u numsecs] \\\n"); | 
					
						
							|  |  |  | 	printf("\t\t[-b searchbase] [-v[v]] [-V] [-t timeout] [-M maxrecords]\n"); | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	printf(" *\tldap2dns formats DNS information from an LDAP server for tinydns or BIND\n"); | 
					
						
							|  |  |  | 	printf(" *\tldap2dnsd runs backgrounded refreshing the data on regular intervals\n"); | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	printf("options:\n"); | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | 	printf("  -D binddn\tUse the distinguished name binddn to bind to the LDAP directory\n"); | 
					
						
							|  |  |  | 	printf("  -w bindpasswd\tUse bindpasswd as the password for simple authentication\n"); | 
					
						
							|  |  |  | 	printf("  -b\t\tSearch base to use instead of default\n"); | 
					
						
							|  |  |  | 	printf("  -o data\tGenerate a tinydns compatible \"data\" file\n"); | 
					
						
							|  |  |  | 	printf("  -o db\t\tGenerate a BIND compatible zone files\n"); | 
					
						
							|  |  |  | 	printf("  -L [filename]\tPrint output in LDIF format for reimport\n"); | 
					
						
							|  |  |  | 	printf("  -h host\tHostname of LDAP server, defaults to localhost\n"); | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 	printf("  -p port\tPort number to connect to LDAP server, defaults to %d\n", LDAP_PORT); | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | 	printf("  -H hostURI\tURI (ldap://hostname or ldaps://hostname of LDAP server\n"); | 
					
						
							|  |  |  | 	printf("  -u numsecs\tUpdate DNS data after numsecs. Defaults to %d. Daemon mode only\n\t\t", UPDATE_INTERVAL); | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	printf("  -e \"exec-cmd\"\tCommand to execute after data is generated\n"); | 
					
						
							|  |  |  | 	printf("  -v\t\trun in verbose mode, repeat for more verbosity\n"); | 
					
						
							|  |  |  | 	printf("  -V\t\tprint version and exit\n\n"); | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	printf("Note: Zone data are only updated after zone serials increment.\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | static void parse_hosts(char* buf) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  |         int i, k; | 
					
						
							|  |  |  |         unsigned short port; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  |         char value[128], rest[512]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         options.usedhosts = 0; | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  |         options.useduris = 0; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  |         for (i = 0; i<MAXHOSTS; i++) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		if (!strncasecmp(buf, "ldaps://", 8) || !strncasecmp(buf, "ldap://", 7)) { | 
					
						
							|  |  |  | 			// LDAP-URI is given/found, at the moment only the standard-ports 389 and 636 are supported
 | 
					
						
							|  |  |  | 			if (!strncasecmp(buf, "ldap://", 7)) | 
					
						
							|  |  |  | 				options.use_tls[i] = 1; | 
					
						
							|  |  |  | 			if ((k = sscanf(buf, "%128s %512[A-Za-z0-9 .:/_+-]", value, rest))>=1) { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  |                 strncpy(options.urildap[i], value, sizeof(options.urildap[i])); | 
					
						
							|  |  |  |                 options.urildap[i][ sizeof(options.urildap[i]) -1 ] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 				options.useduris++; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				if (k==1) | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				buf = rest; | 
					
						
							|  |  |  | 			} else break; | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 		} else if ((k = sscanf(buf, "%128s:%hd %512[A-Za-z0-9 .:_+-]", value, &port, rest))>=2) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  |                         strcpy(options.hostname[i], value); | 
					
						
							|  |  |  |                         options.port[i] = port; | 
					
						
							|  |  |  |                         options.usedhosts++; | 
					
						
							|  |  |  |                         if (k==2) | 
					
						
							|  |  |  |                                 break; | 
					
						
							|  |  |  |                         buf = rest; | 
					
						
							|  |  |  |                 } else if ((k = sscanf(buf, "%128s %512[A-Za-z0-9 .:_+-]", value, rest))>=1) { | 
					
						
							|  |  |  |                         strcpy(options.hostname[i], value); | 
					
						
							|  |  |  |                         options.port[i] = LDAP_PORT; | 
					
						
							|  |  |  |                         options.usedhosts++; | 
					
						
							|  |  |  |                         if (k==1) | 
					
						
							|  |  |  |                                 break; | 
					
						
							|  |  |  |                         buf = rest; | 
					
						
							|  |  |  |                 } else break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | static int parse_options() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	extern char* optarg; | 
					
						
							|  |  |  | 	extern int optind, opterr, optopt; | 
					
						
							|  |  |  | 	char buf[256], value[128]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	int len; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	int c; | 
					
						
							|  |  |  | 	int digit_optind = 0; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 	FILE* ldap_conf,*fp; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	char* ev; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	strcpy(options.searchbase, ""); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 	strcpy(options.hostname[0], "localhost"); | 
					
						
							|  |  |  | 	options.port[0] = LDAP_PORT; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	options.searchtimeout.tv_sec = DEF_SEARCHTIMEOUT; | 
					
						
							|  |  |  | 	options.reclimit = DEF_RECLIMIT; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	if (ldap_conf = fopen(LDAP_CONF, "r")) { | 
					
						
							|  |  |  | 		while(fgets(buf, 256, ldap_conf)!=0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 			int i; | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			if (sscanf(buf, "BASE %128s", value)==1){ | 
					
						
							|  |  |  | 				strncpy(options.searchbase, value, sizeof(options.searchbase)); | 
					
						
							|  |  |  | 				options.searchbase[sizeof(options.searchbase) -1] = '\0'; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			if (sscanf(buf, "URI %512[A-Za-z0-9 .:/_+-]", value)==1) | 
					
						
							|  |  |  | 				parse_hosts(value); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 			if (sscanf(buf, "HOST %512[A-Za-z0-9 .:_+-]", value)==1) | 
					
						
							|  |  |  | 				parse_hosts(value); | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			if (sscanf(buf, "PORT %hd", &len)==1) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 				for (i = 0; i<MAXHOSTS; i++) | 
					
						
							|  |  |  | 					options.port[i] = len; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 			if (sscanf(buf, "BINDDN %128s", value)==1) { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 				strncpy(options.binddn, value, sizeof(options.binddn)); | 
					
						
							|  |  |  | 				options.binddn[ sizeof(options.binddn) -1] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 				if (sscanf(buf, "BINDPW %128s", value)==1) | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 					strncpy(options.password, value, sizeof(options.password)); | 
					
						
							|  |  |  | 					options.password[ sizeof(options.password) -1 ] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		fclose(ldap_conf); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	strcpy(options.binddn, ""); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	len = strlen(main_argv[0]); | 
					
						
							|  |  |  | 	if (strcmp(main_argv[0]+len-9, "ldap2dnsd")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		options.is_daemon = 1; | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 		options.update_iv = UPDATE_INTERVAL; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		options.is_daemon = 0; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		options.update_iv = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ev = getenv("LDAP2DNS_UPDATE"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 	if (ev && sscanf(ev, "%d", &len)==1 && len>0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		options.update_iv = len; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	options.output = 0; | 
					
						
							|  |  |  | 	ev = getenv("LDAP2DNS_OUTPUT"); | 
					
						
							|  |  |  | 	if (ev) { | 
					
						
							|  |  |  | 		if (strcmp(ev, "data")==0) | 
					
						
							|  |  |  | 			options.output = OUTPUT_DATA; | 
					
						
							|  |  |  | 		else if (strcmp(ev, "db")==0) | 
					
						
							|  |  |  | 			options.output = OUTPUT_DB; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 	ev = getenv("LDAP2DNS_BINDDN"); | 
					
						
							|  |  |  | 	if (ev) { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 		strncpy(options.binddn, ev, sizeof(options.binddn)); | 
					
						
							|  |  |  | 		options.binddn[ sizeof(options.binddn)-1] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 		ev = getenv("LDAP2DNS_PASSWORD"); | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 		if (ev){ | 
					
						
							|  |  |  | 			strncpy(options.password, ev, sizeof(options.password)); | 
					
						
							|  |  |  | 			options.password[ sizeof(options.password) -1 ] = '\0'; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	options.verbose = 0; | 
					
						
							|  |  |  | 	options.ldifname[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	strcpy(options.password, ""); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	strcpy(options.exec_command, ""); | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	while (1) { | 
					
						
							|  |  |  | 		int this_option_optind = optind ? optind : 1; | 
					
						
							|  |  |  | 		int option_index = 0; | 
					
						
							|  |  |  | 		static struct option long_options[] = { | 
					
						
							|  |  |  | 			// name, has_arg, flag, val
 | 
					
						
							|  |  |  | 			{"help", 0, 0, '?'}, | 
					
						
							|  |  |  | 			{"binddn", 1, 0, 'D'}, | 
					
						
							|  |  |  | 			{"bindpw", 1, 0, 'w'}, | 
					
						
							|  |  |  | 			{"basedn", 1, 0, 'b'}, | 
					
						
							|  |  |  | 			{"output", 1, 0, 'o'}, | 
					
						
							|  |  |  | 			{"ldif", 1, 0, 'L'}, | 
					
						
							|  |  |  | 			{"host", 1, 0, 'h'}, | 
					
						
							|  |  |  | 			{"port", 1, 0, 'p'}, | 
					
						
							|  |  |  | 			{"uri", 1, 0, 'H'}, | 
					
						
							|  |  |  | 			{"update", 1, 0, 'u'}, | 
					
						
							|  |  |  | 			{"exec", 1, 0, 'e'}, | 
					
						
							|  |  |  | 			{"verbose", 0, 0, 'v'}, | 
					
						
							|  |  |  | 			{"version", 0, 0, 'V'}, | 
					
						
							|  |  |  | 			{"timeout", 1, 0, 't'}, | 
					
						
							|  |  |  | 			{"maxrecords", 1, 0, 'M'}, | 
					
						
							|  |  |  | 			{0, 0, 0, 0} | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-20 18:15:45 +00:00
										 |  |  | 		c = getopt_long(main_argc, main_argv, "b:D:e:h:H:o:p:u:M:m:t:Vv::w:L::", long_options, &option_index); | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (c == -1) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		if (optarg && strlen(optarg)>127) { | 
					
						
							|  |  |  | 			fprintf(stderr, "argument %s too long\n", optarg); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		switch (c) { | 
					
						
							|  |  |  | 	    case 'b': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(options.searchbase, optarg, sizeof(options.searchbase)); | 
					
						
							|  |  |  | 			options.searchbase[ sizeof(options.searchbase) -1] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'u': | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			if (sscanf(optarg, "%d", &options.update_iv)!=1) | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 				options.update_iv = UPDATE_INTERVAL; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			if (options.update_iv<=0) options.update_iv = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'D': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(options.binddn, optarg, sizeof(options.binddn)); | 
					
						
							|  |  |  | 			options.binddn[ sizeof(options.binddn) -1 ] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'h': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(options.hostname[0], optarg, sizeof(options.hostname[0])); | 
					
						
							|  |  |  | 			options.hostname[0][ sizeof(options.hostname[0]) -1 ] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 			options.usedhosts = 1; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		case 'H': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(options.urildap[0], optarg, sizeof(options.urildap[0])); | 
					
						
							|  |  |  | 			options.urildap[0][ sizeof( options.urildap[0] ) -1 ] = '\0'; | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 			options.useduris = 1; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'L': | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			if (optarg==NULL) | 
					
						
							|  |  |  | 				strcpy(options.ldifname, "-"); | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			else{ | 
					
						
							|  |  |  | 				strncpy(options.ldifname, optarg, sizeof(options.ldifname)); | 
					
						
							|  |  |  | 				options.ldifname[ sizeof( options.ldifname ) -1 ] = '\0'; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'o': | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 			options.output = 0; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			if (strcmp(optarg, "data")==0) | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 				options.output = OUTPUT_DATA; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			else if (strcmp(optarg, "db")==0) | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 				options.output = OUTPUT_DB; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'p': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			if (sscanf(optarg, "%hd", &options.port[0])!=1) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 				options.port[0] = LDAP_PORT; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'v': | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			if (optarg && optarg[0]=='v') | 
					
						
							|  |  |  | 				options.verbose = 3; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				options.verbose = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'V': | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			print_version(); | 
					
						
							|  |  |  | 			exit(0); | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'w': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(options.password, optarg, sizeof(options.password)); | 
					
						
							| 
									
										
										
										
											2006-05-30 19:34:35 +00:00
										 |  |  | 			options.password[ sizeof( options.password ) -1 ] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 			memset(optarg, 'x', strlen(options.password)); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	    case 'e': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(options.exec_command, optarg, sizeof(options.exec_command)); | 
					
						
							|  |  |  | 			options.exec_command[ sizeof( options.exec_command ) -1 ] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 		case 't': | 
					
						
							|  |  |  | 			if (sscanf(optarg, "%hd", &options.searchtimeout.tv_sec)!=1) | 
					
						
							|  |  |  | 				options.searchtimeout.tv_sec = DEF_SEARCHTIMEOUT; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 'M': | 
					
						
							|  |  |  | 			if (sscanf(optarg, "%hd", &options.reclimit)!=1) | 
					
						
							|  |  |  | 				options.reclimit = DEF_RECLIMIT; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case '?': | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			print_usage(); | 
					
						
							|  |  |  | 			exit(1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	if (options.is_daemon==0 && options.update_iv>0) | 
					
						
							|  |  |  | 		options.is_daemon = 2; /* foreground daemon */ | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int expand_domainname(char target[64], const char* source, int slen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (slen>64) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	if (source[slen-1]=='.') { | 
					
						
							|  |  |  | 		strncpy(target, source, slen-1); | 
					
						
							|  |  |  | 		target[slen-1] = '\0'; | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	strncpy(target, source, slen); | 
					
						
							|  |  |  | 	target[slen] = '\0'; | 
					
						
							|  |  |  | 	if (zone.domainname[0]) { | 
					
						
							|  |  |  | 		if (zone.domainname[0]!='.') | 
					
						
							|  |  |  | 			strcat(target, "."); | 
					
						
							|  |  |  | 		strcat(target, zone.domainname); | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int expand_reverse(char target[64], const char* source) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | static void write_rr(struct resourcerecord* rr, int ipdx, int znix) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char ip[4]; | 
					
						
							|  |  |  | 	char buf[4]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:28:28 +00:00
										 |  |  | 	char *tmp; | 
					
						
							|  |  |  | 	char *p; | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (strcasecmp(rr->class, "IN")) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	if (strcasecmp(rr->type, "NS")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (tinyfile) { | 
					
						
							|  |  |  | 			if (znix==0) { | 
					
						
							|  |  |  | 				if (ipdx<=0 && rr->cipaddr[0]) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "&%s::%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 					if (rr->cname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 						fprintf(tinyfile, "=%s:%s:%s:%s:%s\n", rr->cname, rr->cipaddr, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 					if (ipdx==0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 						fprintf(tinyfile, "+%s:%s:%s:%s:%s\n", rr->cname, rr->ipaddr[0], rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				} else if (ipdx<0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "&%s::%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				else if (ipdx==0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "&%s:%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->ipaddr[0], rr->cname, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				else if (ipdx>0 && rr->cname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "+%s:%s:%s:%s:%s\n", rr->cname, rr->ipaddr[ipdx], rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			} else if (ipdx<=0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				fprintf(tinyfile, "&%s::%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (namedzone) { | 
					
						
							|  |  |  | 			fprintf(namedzone, "%s.\tIN NS\t%s.\n", rr->dnsdomainname, rr->cname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			if (ipdx>=0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				fprintf(namedzone, "%s.\tIN A\t%s\n", rr->cname, rr->ipaddr[ipdx]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	} else if (strcasecmp(rr->type, "MX")==0) { | 
					
						
							|  |  |  | 		if (tinyfile) { | 
					
						
							|  |  |  | 			if (znix==0) { | 
					
						
							|  |  |  | 				if (ipdx<=0 && rr->cipaddr[0]) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "@%s::%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->preference, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 					if (rr->cname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 						fprintf(tinyfile, "=%s:%s:%s:%s:%s\n", rr->cname, rr->cipaddr, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 					if (ipdx==0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 						fprintf(tinyfile, "+%s:%s:%s:%s:%s\n", rr->cname, rr->ipaddr[0], rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				} else if (ipdx<0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "@%s::%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->preference, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				else if (ipdx==0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "@%s:%s:%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->ipaddr[0], rr->cname, rr->preference, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				else if (ipdx>0 && rr->cname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					fprintf(tinyfile, "+%s:%s:%s:%s:%s\n", rr->cname, rr->ipaddr[ipdx], rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			} else if (ipdx<=0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				fprintf(tinyfile, "@%s::%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->preference, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (namedzone) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 			fprintf(namedzone, "%s.\tIN MX\t%s %s.\n", rr->dnsdomainname, rr->preference, rr->cname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			if (ipdx>=0) | 
					
						
							|  |  |  | 				fprintf(namedzone, "%s.\tIN A\t%s\n", rr->cname, rr->ipaddr[ipdx]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	} else if ( strcasecmp(rr->type, "A")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (tinyfile) { | 
					
						
							|  |  |  | 			if (ipdx<=0 && rr->cipaddr[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				fprintf(tinyfile, "%s%s:%s:%s:%s:%s\n", (znix==0 ? "=" : "+"), rr->dnsdomainname, rr->cipaddr, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			if (ipdx>=0) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				fprintf(tinyfile, "+%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->ipaddr[ipdx], rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (namedzone) { | 
					
						
							|  |  |  | 			if (ipdx<=0 && rr->cipaddr[0]) | 
					
						
							|  |  |  | 				fprintf(namedzone, "%s.\tIN A\t%s\n", rr->dnsdomainname, rr->cipaddr); | 
					
						
							|  |  |  | 			if (ipdx>=0) | 
					
						
							|  |  |  | 				fprintf(namedzone, "%s.\tIN A\t%s\n", rr->dnsdomainname, rr->ipaddr[ipdx]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else if (strcasecmp(rr->type, "PTR")==0) { | 
					
						
							|  |  |  | 		int ip[4] = {0, 0, 0, 0}; | 
					
						
							|  |  |  | 		char buf[64]; | 
					
						
							|  |  |  | 		if (ipdx>0) { | 
					
						
							|  |  |  | 			/* does not make to have more than one IPaddr for a PTR record */ | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (ipdx==0 && sscanf(rr->ipaddr[0], "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3])==4) { | 
					
						
							|  |  |  | 			/* lazy user, used DNSipaddr for reverse lookup */ | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa", ip[3], ip[2], ip[1], ip[0]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(buf, rr->dnsdomainname, sizeof(buf)); | 
					
						
							|  |  |  | 			buf[ sizeof(buf) -1 ] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (tinyfile) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			fprintf(tinyfile, "^%s:%s:%s:%s:%s\n", buf, rr->cname, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (namedzone) | 
					
						
							|  |  |  | 			fprintf(namedzone, "%s.\tIN PTR\t%s.\n", buf, rr->cname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	} else if (strcasecmp(rr->type, "CNAME")==0) { | 
					
						
							|  |  |  | 		if (tinyfile) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			fprintf(tinyfile, "C%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (namedzone) | 
					
						
							|  |  |  | 			fprintf(namedzone, "%s.\tIN CNAME\t%s.\n", rr->dnsdomainname, rr->cname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	} else if (strcasecmp(rr->type, "TXT")==0) { | 
					
						
							|  |  |  | 		if (tinyfile) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			fprintf(tinyfile, "'%s:%s:%s:%s:%s\n", rr->dnsdomainname, rr->cname, rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (namedzone) | 
					
						
							|  |  |  | 			fprintf(namedzone, "%s.\tIN TXT\t%s.\n", rr->dnsdomainname, rr->cname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:28:28 +00:00
										 |  |  | 	} else if (strcasecmp(rr->type, "SRV")==0) { | 
					
						
							| 
									
										
										
										
											2006-06-08 16:48:46 +00:00
										 |  |  | 		if (tinyfile) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:28:28 +00:00
										 |  |  | 			fprintf(tinyfile, ":%s:33:\\%03o\\%03o\\%03o\\%03o\\%03o\\%03o", rr->dnsdomainname, rr->dnssrvpriority >> 8, rr->dnssrvpriority & 0xff, rr->dnssrvweight >> 8, rr->dnssrvweight & 0xff, rr->dnssrvport >> 8, rr->dnssrvport & 0xff); | 
					
						
							|  |  |  | 			tmp = strdup(rr->cname); | 
					
						
							|  |  |  | 			while (p = strchr(tmp, '.')) { | 
					
						
							|  |  |  | 				*p = '\0'; | 
					
						
							|  |  |  | 				p++; | 
					
						
							|  |  |  | 				fprintf(tinyfile, "\\%03o%s", strlen(tmp), tmp); | 
					
						
							|  |  |  | 				tmp = p; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			fprintf(tinyfile, "\\%03o%s", strlen(tmp), tmp); | 
					
						
							|  |  |  | 			fprintf(tinyfile, "\\000:%s:%s:%s\n", rr->ttl, rr->timestamp, rr->location); | 
					
						
							| 
									
										
										
										
											2006-06-08 16:48:46 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (namedzone) { | 
					
						
							|  |  |  | 			fprintf(namedzone, "%s\tIN SRV\t%d\t%d\t%d\t%s.\n", rr->dnsdomainname, rr->dnssrvpriority, rr->dnssrvweight, rr->dnssrvport, rr->cname); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined DRAFT_RFC
 | 
					
						
							|  |  |  | static void parse_rr(struct resourcerecord* rr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char word1[64]; | 
					
						
							|  |  |  | 	char word2[64]; | 
					
						
							|  |  |  | 	int ip[4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sscanf(rr->rr, "%16s %16s %64s %64s", rr->class, rr->type, word1, word2); | 
					
						
							|  |  |  | 	if (strcasecmp(rr->type, "NS")==0) { | 
					
						
							|  |  |  | 		if (sscanf(word1, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3])==4) { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			snprintf(rr->ipaddr[0], sizeof(rr->ipaddr[0]), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			int len = strlen(word1); | 
					
						
							|  |  |  | 			expand_domainname(rr->cname, word1, len); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else if (strcasecmp(rr->type, "MX")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 		if (sscanf(word1, "%s", rr->preference)!=1) | 
					
						
							|  |  |  | 			rr->preference[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		if (sscanf(word2, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3])==4) { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			snprintf(rr->ipaddr[0], sizeof(rr->ipaddr[0]), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			int len = strlen(word2); | 
					
						
							|  |  |  | 			expand_domainname(rr->cname, word2, len); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else if (strcasecmp(rr->type, "A")==0) { | 
					
						
							|  |  |  | 		if (sscanf(word1, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3])==4) | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			snprintf(rr->ipaddr[0], sizeof(rr->ipaddr[0]), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		else | 
					
						
							|  |  |  | 			rr->ipaddr[0][0] = '\0'; | 
					
						
							|  |  |  | 	} else if (strcasecmp(rr->type, "PTR")==0) { | 
					
						
							|  |  |  | 		expand_reverse(rr->dnsdomainname, word1); | 
					
						
							|  |  |  | 	} else if (strcasecmp(rr->type, "CNAME")==0) { | 
					
						
							|  |  |  | 		int len = strlen(word1); | 
					
						
							|  |  |  | 		expand_reverse(rr->cname, word1); | 
					
						
							|  |  |  | 	} else if (strcasecmp(rr->type, "TXT")==0) { | 
					
						
							|  |  |  | 		strncpy(rr->cname, word1, 64); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | static void read_resourcerecords(char* dn, int znix) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	LDAPMessage* res = NULL; | 
					
						
							|  |  |  | 	LDAPMessage* m; | 
					
						
							|  |  |  | 	int ldaperr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	if ( (ldaperr = ldap_search_ext_s(ldap_con, dn, LDAP_SCOPE_SUBTREE, "objectclass=DNSrrset", NULL, 0, NULL, NULL, &options.searchtimeout, options.reclimit, &res))!=LDAP_SUCCESS ) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		die_ldap(ldaperr); | 
					
						
							| 
									
										
										
										
											2006-06-13 16:42:14 +00:00
										 |  |  | 	if (ldap_count_entries(ldap_con, res) < 1) { | 
					
						
							|  |  |  | 		fprintf(stderr, "[**] Warning: No records returned from search.  Check for correct credentials,\n[**] LDAP hostname, and search base DN.\n\n"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	for (m = ldap_first_entry(ldap_con, res); m; m = ldap_next_entry(ldap_con, m)) { | 
					
						
							|  |  |  | 		BerElement* ber = NULL; | 
					
						
							|  |  |  | 		char* attr; | 
					
						
							|  |  |  | 		char* dn = ldap_get_dn(ldap_con, m); | 
					
						
							|  |  |  | 		struct resourcerecord rr; | 
					
						
							|  |  |  | 		int ipaddresses = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (options.ldifname[0]) | 
					
						
							|  |  |  | 			fprintf(ldifout, "dn: %s\n", dn); | 
					
						
							|  |  |  | 		rr.cn[0] = '\0'; | 
					
						
							|  |  |  | 		strncpy(rr.dnsdomainname, zone.domainname, 64); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		strncpy(rr.class, "IN", 3); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		rr.type[0] = '\0'; | 
					
						
							|  |  |  | 		rr.cname[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		rr.cipaddr[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 		rr.ttl[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		rr.timestamp[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 		rr.preference[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		rr.location[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | #if defined DRAFT_RFC
 | 
					
						
							|  |  |  | 		rr.aliasedobjectname[0] = '\0'; | 
					
						
							|  |  |  | 		rr.rr[0] = '\0'; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:28:28 +00:00
										 |  |  |                 rr.dnssrvpriority = 0; | 
					
						
							|  |  |  |                 rr.dnssrvweight = 0; | 
					
						
							|  |  |  |                 rr.dnssrvport = 0; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		for (attr = ldap_first_attribute(ldap_con, m, &ber); attr; attr = ldap_next_attribute(ldap_con, m, ber)) { | 
					
						
							|  |  |  | 			int len = strlen(attr); | 
					
						
							|  |  |  | 			struct berval** bvals; | 
					
						
							|  |  |  | 			char* dnsnname = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if ( (bvals = ldap_get_values_len(ldap_con, m, attr))!=NULL ) { | 
					
						
							|  |  |  | 				if (bvals[0] && bvals[0]->bv_len>0) { | 
					
						
							|  |  |  | 					if (strcasecmp(attr, "objectclass")==0) { | 
					
						
							|  |  |  | 						if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "cn")==0) { | 
					
						
							|  |  |  | 						strncpy(rr.cn, bvals[0]->bv_val, 64); | 
					
						
							|  |  |  | 						if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, rr.cn); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSdomainname")==0) { | 
					
						
							|  |  |  | 						if (!expand_domainname(rr.dnsdomainname, bvals[0]->bv_val, bvals[0]->bv_len)) | 
					
						
							|  |  |  | 							rr.dnsdomainname[0] = '\0';; | 
					
						
							|  |  |  | 						if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSclass")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%16s", rr.class)!=1) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							rr.class[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, rr.class); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNStype")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%16s", rr.type)!=1) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							rr.type[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, rr.type); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSipaddr")==0) { | 
					
						
							|  |  |  | 						int ip[4]; | 
					
						
							|  |  |  | 						for (ipaddresses = 0; bvals[ipaddresses] && ipaddresses<256; ipaddresses++) { | 
					
						
							|  |  |  | 							rr.ipaddr[ipaddresses][0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 							if (sscanf(bvals[ipaddresses]->bv_val, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3])==4) { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 								snprintf(rr.ipaddr[ipaddresses], sizeof(rr.ipaddr[ipaddresses]), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 								if (options.ldifname[0]) | 
					
						
							|  |  |  | 									fprintf(ldifout, "%s: %s\n", attr, rr.ipaddr[ipaddresses]); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNScipaddr")==0) { | 
					
						
							|  |  |  | 						int ip[4]; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3])==4) { | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 							snprintf(rr.cipaddr, sizeof(rr.cipaddr), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 							if (options.ldifname[0]) | 
					
						
							|  |  |  | 								fprintf(ldifout, "%s: %s\n", attr, rr.cipaddr); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNScname")==0) { | 
					
						
							|  |  |  | 						if (!expand_domainname(rr.cname, bvals[0]->bv_val, bvals[0]->bv_len)) | 
					
						
							|  |  |  | 							rr.cname[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSttl")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%12s", rr.ttl)!=1) | 
					
						
							|  |  |  | 							rr.ttl[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, rr.ttl); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNStimestamp")==0) { | 
					
						
							|  |  |  | 						if (sscanf(bvals[0]->bv_val, "%16s", &rr.timestamp)!=1) | 
					
						
							|  |  |  | 							rr.timestamp[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, rr.timestamp); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSpreference")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%s", rr.preference)!=1) | 
					
						
							|  |  |  | 							rr.preference[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNSlocation")==0) { | 
					
						
							|  |  |  | 						if (sscanf(bvals[0]->bv_val, "%s", rr.location)!=1) | 
					
						
							|  |  |  | 							rr.location[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | #if defined DRAFT_RFC
 | 
					
						
							|  |  |  | 					else if (strcasecmp(attr, "DNSrr")==0) { | 
					
						
							|  |  |  | 						strncpy(rr.rr, bvals[0]->bv_val, 1024); | 
					
						
							|  |  |  | 						if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, rr.rr); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSaliasedobjectname")==0) { | 
					
						
							|  |  |  | 						if (sscanf(bvals[0]->bv_val, "%256s", rr.aliasedobjectname)!=1) | 
					
						
							|  |  |  | 							rr.aliasedobjectname[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, rr.aliasedobjectname); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSmacaddress")==0) { | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:28:28 +00:00
										 |  |  | 					else if (strcasecmp(attr, "DNSsrvpriority")==0) { | 
					
						
							|  |  |  | 						if (!(rr.dnssrvpriority = atoi(bvals[0]->bv_val))) | 
					
						
							|  |  |  |                                                         rr.dnssrvpriority = 0; | 
					
						
							|  |  |  |                                                 else if (options.ldifname[0]) | 
					
						
							|  |  |  |                                                         fprintf(ldifout, "%s: %d\n", attr, rr.dnssrvpriority); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSsrvweight")==0) { | 
					
						
							|  |  |  | 						if (!(rr.dnssrvweight = atoi(bvals[0]->bv_val))) | 
					
						
							|  |  |  |                                                         rr.dnssrvweight = 0; | 
					
						
							|  |  |  |                                                 else if (options.ldifname[0]) | 
					
						
							|  |  |  |                                                         fprintf(ldifout, "%s: %d\n", attr, rr.dnssrvweight); | 
					
						
							|  |  |  |                                         } else if (strcasecmp(attr, "DNSsrvport")==0) { | 
					
						
							|  |  |  | 						if (!(rr.dnssrvport = atoi(bvals[0]->bv_val))) | 
					
						
							|  |  |  |                                                         rr.dnssrvport = 0; | 
					
						
							|  |  |  |                                                 else if (options.ldifname[0]) | 
					
						
							|  |  |  |                                                         fprintf(ldifout, "%s: %d\n", attr, rr.dnssrvport); | 
					
						
							|  |  |  |                                         } | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				ldap_value_free_len(bvals); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | #if defined DRAFT_RFC
 | 
					
						
							|  |  |  | 		if (rr.rr[0]) { | 
					
						
							|  |  |  | 			parse_rr(&rr); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 		do { | 
					
						
							|  |  |  | 			ipaddresses--; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			write_rr(&rr, ipaddresses, znix); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		} while (ipaddresses>0); | 
					
						
							|  |  |  | #if defined DRAFT_RFC
 | 
					
						
							|  |  |  | 		if (rr.aliasedobjectname[0]) | 
					
						
							|  |  |  | 			read_resourcerecords(rr.aliasedobjectname); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 		if (options.ldifname[0]) | 
					
						
							|  |  |  | 			fprintf(ldifout, "\n"); | 
					
						
							|  |  |  | 		if (options.verbose&2) | 
					
						
							|  |  |  | 			printf("\trr: %s %s %s\n", rr.class, rr.type, rr.dnsdomainname); | 
					
						
							|  |  |  | 		free(dn); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ldap_msgfree(res); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void write_zone(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int len; | 
					
						
							|  |  |  | 	char soa[20]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tinyfile) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		fprintf(tinyfile, "Z%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n", | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 		    zone.domainname, zone.zonemaster, zone.adminmailbox, | 
					
						
							|  |  |  | 		    zone.serial, zone.refresh, zone.retry, zone.expire, | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		    zone.minimum, zone.ttl, zone.timestamp, zone.location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	if (namedmaster) { | 
					
						
							|  |  |  | 		fprintf(namedmaster, "zone \"%s\" %s {\n\ttype master;\n\tfile \"%s.db\";\n};\n", | 
					
						
							|  |  |  | 		    zone.domainname, zone.class, zone.domainname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	if (namedzone) { | 
					
						
							| 
									
										
										
										
											2006-06-13 17:03:41 +00:00
										 |  |  | 		fprintf(namedzone, ";\n; Automatically generated by ldap2dns v%s - DO NOT EDIT!\n;\n\n", VERSION); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 		if (zone.ttl[0]) | 
					
						
							|  |  |  | 			fprintf(namedzone, "$TTL %s\n", zone.ttl); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			fprintf(namedzone, "$TTL 3600\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		fprintf(namedzone, "%s. IN SOA ", zone.domainname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		len = strlen(zone.zonemaster); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		fprintf(namedzone, (zone.zonemaster[len-1]=='.') ? "%s " : "%s. ", zone.zonemaster); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		len = strlen(zone.adminmailbox); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		fprintf(namedzone, (zone.adminmailbox[len-1]=='.') ? "%s " : "%s. ", zone.adminmailbox); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 		fprintf(namedzone, "(\n\t%s\t; Serial\n\t%s\t; Refresh\n\t%s\t; Retry\n\t%s\t; Expire\n\t%s )\t; Minimum\n", zone.serial, zone.refresh, zone.retry, zone.expire, zone.minimum);  | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (options.ldifname[0]) | 
					
						
							|  |  |  | 		fprintf(ldifout, "\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void calc_checksum(int* num, int* sum) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	LDAPMessage* res = NULL; | 
					
						
							|  |  |  | 	LDAPMessage* m; | 
					
						
							|  |  |  | 	int ldaperr; | 
					
						
							|  |  |  | 	char* attr_list[2] = { "DNSserial", NULL }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*num = *sum = 0; | 
					
						
							| 
									
										
										
										
											2006-07-14 22:17:36 +00:00
										 |  |  | 	if ( ldaperr = ldap_search_ext_s(ldap_con, options.searchbase[0] ? options.searchbase : NULL, LDAP_SCOPE_ONELEVEL, "objectclass=DNSzone", attr_list, 0, NULL, NULL, &options.searchtimeout, options.reclimit, &res)!=LDAP_SUCCESS ) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		die_ldap(ldaperr); | 
					
						
							| 
									
										
										
										
											2006-06-13 16:42:14 +00:00
										 |  |  | 	if (ldap_count_entries(ldap_con, res) < 1) { | 
					
						
							|  |  |  | 		fprintf(stderr, "[**] Warning: No records returned from search.  Check for correct credentials,\n[**] LDAP hostname, and search base DN.\n\n"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	for (m = ldap_first_entry(ldap_con, res); m; m = ldap_next_entry(ldap_con, m)) { | 
					
						
							|  |  |  | 		BerElement* ber = NULL; | 
					
						
							|  |  |  | 		char* attr = ldap_first_attribute(ldap_con, m, &ber); | 
					
						
							|  |  |  | 		if (attr) { | 
					
						
							|  |  |  |                         struct berval** bvals = ldap_get_values_len(ldap_con, m, attr); | 
					
						
							|  |  |  |                         if (bvals!=NULL) { | 
					
						
							|  |  |  | 				unsigned tmp; | 
					
						
							|  |  |  | 				if (sscanf(bvals[0]->bv_val, "%u", &tmp)==1) { | 
					
						
							|  |  |  | 					(*num)++; | 
					
						
							|  |  |  | 					*sum += tmp; | 
					
						
							|  |  |  | 				}	 | 
					
						
							|  |  |  | 				ldap_value_free_len(bvals); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ber_free(ber, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ldap_msgfree(res); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void read_dnszones(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	LDAPMessage* res = NULL; | 
					
						
							|  |  |  | 	LDAPMessage* m; | 
					
						
							|  |  |  | 	int ldaperr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tinyfile) | 
					
						
							| 
									
										
										
										
											2006-06-13 17:03:41 +00:00
										 |  |  | 		fprintf(tinyfile, "#\n# Automatically generated by ldap2dns v%s - DO NOT EDIT!\n#\n\n", VERSION); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	if (namedmaster) | 
					
						
							| 
									
										
										
										
											2006-06-13 17:03:41 +00:00
										 |  |  | 		fprintf(namedmaster, "#\n# Automatically generated by ldap2dns v%s - DO NOT EDIT!\n#\n\n", VERSION); | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	if ( (ldaperr = ldap_search_ext_s(ldap_con, options.searchbase[0] ? options.searchbase : NULL, LDAP_SCOPE_SUBTREE, "objectclass=DNSzone", NULL, 0, NULL, NULL, &options.searchtimeout, options.reclimit, &res))!=LDAP_SUCCESS ) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		die_ldap(ldaperr); | 
					
						
							| 
									
										
										
										
											2006-06-13 16:42:14 +00:00
										 |  |  | 	if (ldap_count_entries(ldap_con, res) < 1) { | 
					
						
							|  |  |  | 		fprintf(stderr, "[**] Warning: No records returned from search.  Check for correct credentials,\n[**] LDAP hostname, and search base DN.\n\n"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	for (m = ldap_first_entry(ldap_con, res); m; m = ldap_next_entry(ldap_con, m)) { | 
					
						
							|  |  |  | 		BerElement* ber = NULL; | 
					
						
							|  |  |  | 		char* attr; | 
					
						
							|  |  |  | 		char* dn; | 
					
						
							|  |  |  | 		int i, zonenames = 0; | 
					
						
							|  |  |  | 		char zdn[256][64]; | 
					
						
							|  |  |  | 		char ldif0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		strncpy(zone.class, "IN", 3); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 		zone.serial[0] = '\0'; | 
					
						
							|  |  |  | 		zone.refresh[0] = '\0'; | 
					
						
							|  |  |  | 		zone.retry[0] = '\0'; | 
					
						
							|  |  |  | 		zone.expire[0] = '\0'; | 
					
						
							|  |  |  | 		zone.minimum[0] = '\0'; | 
					
						
							|  |  |  | 		zone.ttl[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		zone.timestamp[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		zone.location[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		dn = ldap_get_dn(ldap_con, m); | 
					
						
							|  |  |  | 		if (options.ldifname[0]) | 
					
						
							|  |  |  | 			fprintf(ldifout, "dn: %s\n", dn); | 
					
						
							|  |  |  | 		for (attr = ldap_first_attribute(ldap_con, m, &ber); attr; attr = ldap_next_attribute(ldap_con, m, ber)) { | 
					
						
							|  |  |  | 			struct berval** bvals = ldap_get_values_len(ldap_con, m, attr); | 
					
						
							|  |  |  | 			if (bvals!=NULL) { | 
					
						
							|  |  |  | 				if (bvals[0] && bvals[0]->bv_len>0) { | 
					
						
							|  |  |  | 					if (strcasecmp(attr, "objectclass")==0 | 
					
						
							|  |  |  | 					    || strcasecmp(attr, "DNSclass")==0 | 
					
						
							|  |  |  | 					    || strcasecmp(attr, "DNStype")==0 | 
					
						
							|  |  |  | 					    || strcasecmp(attr, "cn")==0) { | 
					
						
							|  |  |  | 						if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSzonename")==0) { | 
					
						
							|  |  |  | 						for (zonenames = 0; bvals[zonenames] && zonenames<256; zonenames++) { | 
					
						
							|  |  |  | 							if (sscanf(bvals[zonenames]->bv_val, "%64s", &zdn[zonenames])!=1) | 
					
						
							|  |  |  | 								zdn[zonenames][0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 							else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 								fprintf(ldifout, "%s: %s\n", attr, zdn[zonenames]); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSserial")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%12s", zone.serial)!=1) | 
					
						
							|  |  |  | 							zone.serial[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.serial); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNSrefresh")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%12s", zone.refresh)!=1) | 
					
						
							|  |  |  | 							zone.refresh[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.refresh); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNSretry")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%12s", zone.retry)!=1) | 
					
						
							|  |  |  | 							zone.retry[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.retry); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNSexpire")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%12s", zone.expire)!=1) | 
					
						
							|  |  |  | 							zone.expire[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.expire); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNSminimum")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%12s", zone.minimum)!=1) | 
					
						
							|  |  |  | 							zone.minimum[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.minimum); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNSadminmailbox")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%64s", zone.adminmailbox)!=1) | 
					
						
							|  |  |  | 							zone.adminmailbox[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.adminmailbox); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSzonemaster")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%64s", zone.zonemaster)!=1) | 
					
						
							|  |  |  | 							zone.zonemaster[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.zonemaster); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSttl")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%12s", zone.ttl)!=1) | 
					
						
							|  |  |  | 							zone.ttl[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.ttl); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNStimestamp")==0) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						if (sscanf(bvals[0]->bv_val, "%16s", zone.timestamp)!=1) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							zone.timestamp[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:19:49 +00:00
										 |  |  | 						else if (options.ldifname[0]) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.timestamp); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 					} else if (strcasecmp(attr, "DNSlocation")==0) { | 
					
						
							|  |  |  | 						if (sscanf(bvals[0]->bv_val, "%2s", zone.location)!=1) | 
					
						
							|  |  |  | 							zone.location[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, zone.location); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				ldap_value_free_len(bvals); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ldif0 = options.ldifname[0]; | 
					
						
							|  |  |  | 		for (i = 0; i<zonenames; i++) { | 
					
						
							|  |  |  | 			strncpy(zone.domainname, zdn[i], 64); | 
					
						
							|  |  |  | 			if (i>0) | 
					
						
							|  |  |  | 				options.ldifname[0] = '\0'; | 
					
						
							|  |  |  | 			if (options.verbose&1) | 
					
						
							|  |  |  | 				printf("zonename: %s\n", zone.domainname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			if (options.output&OUTPUT_DB) { | 
					
						
							|  |  |  | 				char namedzonename[128]; | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 				snprintf(namedzonename, sizeof(namedzonename), "%s.db", zone.domainname); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 				if ( !(namedzone = fopen(namedzonename, "w")) ) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 					die_exit("Unable to open db-file for writing"); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			write_zone(); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			read_resourcerecords(dn, i); | 
					
						
							|  |  |  | 			if (namedzone) | 
					
						
							|  |  |  | 				fclose(namedzone); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			if (options.verbose&2) | 
					
						
							|  |  |  | 				printf("\n"); | 
					
						
							|  |  |  | 			if (options.ldifname[0]) | 
					
						
							|  |  |  | 				fprintf(ldifout, "\n"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		options.ldifname[0] = ldif0; | 
					
						
							|  |  |  | 		free(dn); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ldap_msgfree(res); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | static void write_loccode(int lidx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (tinyfile) { | 
					
						
							|  |  |  | 		fprintf(tinyfile, "%%%s:%s\n", loc_rec.locname, loc_rec.member[lidx]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (options.ldifname[0]) | 
					
						
							|  |  |  | 		fprintf(ldifout, "\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void read_loccodes(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	LDAPMessage* res = NULL; | 
					
						
							|  |  |  | 	LDAPMessage* m; | 
					
						
							|  |  |  | 	int ldaperr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-12 21:43:55 +00:00
										 |  |  | 	if ( (ldaperr = ldap_search_ext_s(ldap_con, options.searchbase[0] ? options.searchbase : NULL, LDAP_SCOPE_SUBTREE, "objectclass=DNSloccodes", NULL, 0, NULL, NULL, &options.searchtimeout, options.reclimit, &res))!=LDAP_SUCCESS ) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		die_ldap(ldaperr); | 
					
						
							| 
									
										
										
										
											2006-06-13 17:03:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// We aren't going to warn for zero records here as many installs do
 | 
					
						
							|  |  |  | 	// not use location codes at all
 | 
					
						
							|  |  |  | 	if ((ldap_count_entries(ldap_con, res) > 0) && tinyfile) { | 
					
						
							|  |  |  | 		fprintf(tinyfile, "#\n# Location Codes (if any) - generated by ldap2dns v%s - DO NOT EDIT!\n#\n\n", VERSION); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2006-06-13 16:42:14 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-06-13 17:03:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	for (m = ldap_first_entry(ldap_con, res); m; m = ldap_next_entry(ldap_con, m)) { | 
					
						
							|  |  |  | 		BerElement* ber = NULL; | 
					
						
							|  |  |  | 		char* attr; | 
					
						
							|  |  |  | 		char* dn = ldap_get_dn(ldap_con, m); | 
					
						
							|  |  |  | 		int i, locmembers = 0; | 
					
						
							|  |  |  | 		char l_members[256][15]; | 
					
						
							|  |  |  | 		//char loc[256][64];
 | 
					
						
							|  |  |  | 		char loc[2]; | 
					
						
							|  |  |  | 		char ldif0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		loc_rec.locname[0] = '\0'; | 
					
						
							|  |  |  | 		if (options.ldifname[0]) | 
					
						
							|  |  |  | 			fprintf(ldifout, "dn: %s\n", dn); | 
					
						
							|  |  |  | 		for (attr = ldap_first_attribute(ldap_con, m, &ber); attr; attr = ldap_next_attribute(ldap_con, m, ber)) { | 
					
						
							|  |  |  | 			struct berval** bvals = ldap_get_values_len(ldap_con, m, attr); | 
					
						
							|  |  |  | 			if (bvals!=NULL) { | 
					
						
							|  |  |  | 				if (bvals[0] && bvals[0]->bv_len>0) { | 
					
						
							|  |  |  | 					if (strcasecmp(attr, "objectclass")==0 | 
					
						
							|  |  |  | 					    || strcasecmp(attr, "cn")==0) { | 
					
						
							|  |  |  | 						if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSlocation")==0) { | 
					
						
							|  |  |  | 						if (sscanf(bvals[0]->bv_val, "%2s", loc_rec.locname)!=1) | 
					
						
							|  |  |  | 							loc_rec.locname[0] = '\0'; | 
					
						
							|  |  |  | 						else if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, loc_rec.locname); | 
					
						
							|  |  |  | 					} else if (strcasecmp(attr, "DNSipaddr")==0) { | 
					
						
							|  |  |  | 						for (locmembers = 0; bvals[locmembers] && locmembers<256; locmembers++) { | 
					
						
							|  |  |  | 							if (sscanf(bvals[locmembers]->bv_val, "%15s", loc_rec.member[locmembers])!=1) | 
					
						
							|  |  |  | 								loc_rec.member[locmembers][0] = '\0'; | 
					
						
							|  |  |  | 							else if (options.ldifname[0]) | 
					
						
							|  |  |  | 								fprintf(ldifout, "%s: %s\n", attr, loc_rec.member[locmembers]); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						if (options.ldifname[0]) | 
					
						
							|  |  |  | 							fprintf(ldifout, "%s: %s\n", attr, bvals[0]->bv_val); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				ldap_value_free_len(bvals); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ldif0 = options.ldifname[0]; | 
					
						
							|  |  |  | 		if (options.verbose&1) | 
					
						
							|  |  |  | 			printf("locationcodename: %s (%d members)\n", loc_rec.locname, locmembers); | 
					
						
							|  |  |  | 		for (i = 0; i<locmembers; i++) { | 
					
						
							|  |  |  | 			if (i>0) | 
					
						
							|  |  |  | 				options.ldifname[0] = '\0'; | 
					
						
							|  |  |  | 			write_loccode(i); | 
					
						
							|  |  |  | 			if (options.ldifname[0]) | 
					
						
							|  |  |  | 				fprintf(ldifout, "\n"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		options.ldifname[0] = ldif0; | 
					
						
							|  |  |  | 		free(dn); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ldap_msgfree(res); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | static int do_connect() | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 	int i, version, res; | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 	struct berval* creds = malloc(sizeof(struct berval)); | 
					
						
							|  |  |  | 	struct berval* msgid = malloc(sizeof(struct berval)); | 
					
						
							|  |  |  | 	if (options.useduris < 1) { | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | 		fprintf(stderr, "\n[!!] Must define at least one LDAP host with which to connect.\n\n"); | 
					
						
							| 
									
										
										
										
											2006-06-08 18:02:06 +00:00
										 |  |  | 		fprintf(stderr, "Use --help to see usage information\n"); | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | 		exit(1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 	for (i = 0; i<options.useduris; i++) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		if ( strlen(options.urildap[i]) > 0) { | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 			res = ldap_initialize(&ldap_con, options.urildap[i]); | 
					
						
							|  |  |  | 			if (options.verbose&1 && res == LDAP_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				printf("ldap_initialization successful (%s)\n", options.urildap[i]); | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 			} else if ( res != LDAP_SUCCESS ) { | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 				fprintf(stderr, "ldap_initialization to %s failed %d\n", options.urildap[i], ldap_err2string(res)); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				ldap_con = NULL; | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 				return res; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			version = LDAP_VERSION3; | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 			if ( (res = ldap_set_option(ldap_con, LDAP_OPT_PROTOCOL_VERSION, &version)) != LDAP_SUCCESS ) { | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 				fprintf(stderr, "ldap_set_option to %s failed with err %s!\n", options.urildap[i], ldap_err2string(res)); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				ldap_con = NULL; | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 				return res; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 			if ( options.use_tls[i] && (res = ldap_start_tls_s( ldap_con, NULL, NULL )) != LDAP_SUCCESS ) { | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 				fprintf(stderr, "ldap_start_tls_s to %s failed with err %s!\n", options.urildap[i], ldap_err2string(res)); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				ldap_con = NULL; | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 				return res; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Yes, you really do use ldap_sasl_bind_s() when doing a simple
 | 
					
						
							|  |  |  | 			// bind. This is apparently the "new" way, if not entirely obvious
 | 
					
						
							|  |  |  | 			if (strlen(options.binddn)) { | 
					
						
							|  |  |  | 				if (strlen(options.password)) { | 
					
						
							|  |  |  | 					creds->bv_len = strlen(options.password); | 
					
						
							|  |  |  | 					creds->bv_val = options.password; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// FIXME: Allow *real* SASL binds
 | 
					
						
							|  |  |  | 				if ((res = ldap_sasl_bind_s(ldap_con, options.binddn, NULL, creds, NULL, NULL, &msgid)) != LDAP_SUCCESS) { | 
					
						
							|  |  |  | 					fprintf(stderr, "LDAP bind problem:\n\t%s\n", ldap_err2string(res)); | 
					
						
							| 
									
										
										
										
											2006-06-04 01:45:56 +00:00
										 |  |  | 					fprintf(stderr, "Attempting to continue with anonymous credentials.\n"); | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 					res = LDAP_SUCCESS; | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | void hosts2uri(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i, t; | 
					
						
							|  |  |  | 	// Convert any old host:port sets into URIs.  This allows us
 | 
					
						
							|  |  |  | 	// to use the more modern ldap_initialize() instead of the
 | 
					
						
							|  |  |  | 	// deprecated ldap_init()
 | 
					
						
							|  |  |  | 	for (i = 0; i<options.usedhosts; i++) { | 
					
						
							|  |  |  | 		if ( strlen(options.hostname[i]) > 0) { | 
					
						
							|  |  |  | 			t = options.useduris++; | 
					
						
							|  |  |  | 			snprintf(options.urildap[t], | 
					
						
							|  |  |  | 				sizeof(options.urildap[t]), | 
					
						
							|  |  |  | 				"ldap://%s:%d", | 
					
						
							|  |  |  | 				options.hostname[i], | 
					
						
							|  |  |  | 				options.port[i] ? options.port[i] : LDAP_PORT); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char** argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int soa_numzones; | 
					
						
							|  |  |  | 	int soa_checksum; | 
					
						
							| 
									
										
										
										
											2006-06-04 01:45:56 +00:00
										 |  |  | 	int old_numzones; | 
					
						
							|  |  |  | 	int old_checksum; | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	umask(022); | 
					
						
							|  |  |  | 	main_argc = argc; | 
					
						
							|  |  |  | 	main_argv = argv; | 
					
						
							|  |  |  | 	parse_options(); | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 	if (!options.output) { | 
					
						
							|  |  |  | 		fprintf(stderr, "[!!]\tMust select an output type (\"db\" or \"data\")\n"); | 
					
						
							| 
									
										
										
										
											2006-06-08 18:02:06 +00:00
										 |  |  | 		fprintf(stderr, "Use --help to see usage information\n"); | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 		exit(1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!strlen(options.searchbase)) { | 
					
						
							|  |  |  | 		fprintf(stderr, "[!!]\tMust provide the base DN for the search.\n"); | 
					
						
							| 
									
										
										
										
											2006-06-08 18:02:06 +00:00
										 |  |  | 		fprintf(stderr, "Use --help to see usage information\n"); | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 		exit(1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 	/* Initialization complete.  If we're in daemon mode, fork and continue */ | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	if (options.is_daemon) { | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 		if (options.is_daemon==1 && fork()) { | 
					
						
							|  |  |  | 			if (options.verbose) | 
					
						
							|  |  |  | 				fprintf(stdout, "Sending process to background."); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			exit(0); | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-05-30 19:34:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		fprintf(stdout, "ldap2dns v%s starting up", VERSION); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		/* lowest priority */ | 
					
						
							|  |  |  | 		nice(19); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	set_datadir(); | 
					
						
							|  |  |  | 	for (;;) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 		int ldaperr = -1; | 
					
						
							| 
									
										
										
										
											2006-06-03 20:40:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		hosts2uri(); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2006-06-02 21:19:33 +00:00
										 |  |  | 		res = do_connect(); | 
					
						
							|  |  |  | 		if (res != LDAP_SUCCESS || ldap_con == NULL) { | 
					
						
							| 
									
										
										
										
											2006-05-30 21:53:50 +00:00
										 |  |  | 			fprintf(stderr, "Warning - Problem while connecting to LDAP server:\n\t%s\n", ldap_err2string(res)); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			if (options.is_daemon==0) | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			sleep(options.update_iv); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-06-04 01:45:56 +00:00
										 |  |  | 		//if (options.is_daemon) {
 | 
					
						
							|  |  |  | 			calc_checksum(&old_numzones, &old_checksum); | 
					
						
							|  |  |  | 			if (old_numzones!=soa_numzones || old_checksum!=soa_checksum) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 				if (options.verbose&1) | 
					
						
							|  |  |  | 					printf("DNSserial has changed in LDAP zone(s)\n"); | 
					
						
							| 
									
										
										
										
											2006-06-04 01:45:56 +00:00
										 |  |  | 				soa_numzones = old_numzones; | 
					
						
							|  |  |  | 				soa_checksum = old_checksum; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				goto skip; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-06-04 01:45:56 +00:00
										 |  |  | 		//}
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		if (options.ldifname[0]) { | 
					
						
							|  |  |  | 			if (options.ldifname[0]=='-') | 
					
						
							|  |  |  | 				ldifout = stdout; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				ldifout = fopen(options.ldifname, "w"); | 
					
						
							|  |  |  | 			if (!ldifout) | 
					
						
							|  |  |  | 				die_exit("Unable to open LDIF-file for writing"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		time(&time_now); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if ( options.output&OUTPUT_DATA && !(tinyfile = fopen(tinydns_texttemp, "w")) ) | 
					
						
							|  |  |  | 			die_exit("Unable to open file 'data.temp' for writing"); | 
					
						
							|  |  |  | 		if ( options.output&OUTPUT_DB && !(namedmaster = fopen("named.zones", "w")) ) | 
					
						
							|  |  |  | 			die_exit("Unable to open file 'named.zones' for writing"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		read_loccodes(); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		read_dnszones(); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (namedmaster) | 
					
						
							|  |  |  | 			fclose(namedmaster); | 
					
						
							|  |  |  | 		if (tinyfile) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			fclose(tinyfile); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			if (soa_numzones==0 || soa_checksum==0) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			if (rename(tinydns_texttemp, tinydns_textfile)==-1) | 
					
						
							|  |  |  | 				die_exit("Unable to move 'data.temp' to 'data'"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		if (options.ldifname[0] && ldifout) | 
					
						
							|  |  |  | 			fclose(ldifout); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		if (options.exec_command[0]) | 
					
						
							|  |  |  | 			system(options.exec_command); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	    skip: | 
					
						
							| 
									
										
										
										
											2006-06-12 21:48:34 +00:00
										 |  |  | 		if ( (ldaperr = ldap_unbind_ext_s(ldap_con, NULL, NULL))!=LDAP_SUCCESS ) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			die_ldap(ldaperr); | 
					
						
							|  |  |  | 		if (options.is_daemon==0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		sleep(options.update_iv); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |