| 
									
										
										
										
											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$ | 
					
						
							| 
									
										
										
										
											2005-12-08 17:27:56 +00:00
										 |  |  |  * Copyright 2000-2005 by Jacob Rief <jacob.rief@tiscover.com> | 
					
						
							|  |  |  |  * Copyright 2005 by Ben Klang <ben@alkaloid.net> | 
					
						
							|  |  |  |  * 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-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
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	printf("ldap2dns, version %s\n", VERSION); | 
					
						
							| 
									
										
										
										
											2005-12-08 17:27:56 +00:00
										 |  |  | 	printf("  Copyright 2000-2005 by Jacob Rief <jacob.rief@tiscover.com>\n\n"); | 
					
						
							| 
									
										
										
										
											2005-12-19 06:08:17 +00:00
										 |  |  | 	printf("  Copyright 2005 by Ben Klang <ben@alkaloid.net>\n"); | 
					
						
							|  |  |  | 	printf("  http://projects.alkaloid.net\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void die_ldap(int err) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	fprintf(stderr, "Fatal error: %s\n", ldap_err2string(err)); | 
					
						
							|  |  |  | 	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; | 
					
						
							| 
									
										
										
										
											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]; | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	printf("usage: ldap2dns[d] [-D binddn] [-b searchbase] [-o data|db] [-h host] [-p port] [-H hostURI] " | 
					
						
							|  |  |  | 		   "[-w password] [-L[filename]] [-u numsecs] [-v[v]] [-V]\n\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	printf("ldap2dns connects to an LDAP server reads the DNS information stored in objectclasses\n" | 
					
						
							|  |  |  | 		"\t\tDNSzone and DNSrrset and writes a file to be used by tinydns or named.\n" | 
					
						
							|  |  |  | 		"\t\tldap2dnsd starts as background-job and continouesly updates DNS information.\n"); | 
					
						
							|  |  |  | 	printf("options:\n"); | 
					
						
							|  |  |  | 	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"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	printf("    -b Use searchbase as the starting point for the search instead of the default\n"); | 
					
						
							|  |  |  | 	printf("    -o data\tGenerate a \"data\" file to be processed by tinydns-data\n"); | 
					
						
							|  |  |  | 	printf("    -o db\tFor each zone generate a \"<zonename>.db\" file to be used by named\n"); | 
					
						
							|  |  |  | 	printf("    -L[filename] Print output in LDIF format for reimport\n"); | 
					
						
							|  |  |  | 	printf("    -h host\tHostname of LDAP server, defaults to localhost\n"); | 
					
						
							|  |  |  | 	printf("    -p port\tPortnumber to connect to LDAP server, defaults to %d\n", LDAP_PORT); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	printf("    -H hostURI\tURI (ldap://hostname or ldaps://hostname of LDAP server\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	printf("    -u numsecs\tUpdate DNS data after numsecs. Defaults to %d if started as daemon.\n\t\t" | 
					
						
							|  |  |  | 		"Important notice: data.cdb is rewritten only after DNSserial in DNSzone is increased.\n", | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 		UPDATE_INTERVAL); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	printf("    -e \"exec-cmd\" This command is executed after ldap2dns regenerated its data files\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	printf("    -v\t\trun in verbose mode\n"); | 
					
						
							|  |  |  | 	printf("    -vv\t\teven more verbose\n"); | 
					
						
							|  |  |  | 	printf("    -V\t\tprint version and exit\n\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |         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'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 				options.usedhosts++; | 
					
						
							|  |  |  | 				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; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											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, ""); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	while ( (len = getopt(main_argc, main_argv, "b:D:e:h:H:o:p:u:V:v::w:L::"))>0 ) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		if (optarg && strlen(optarg)>127) { | 
					
						
							|  |  |  | 			fprintf(stderr, "argument %s too long\n", optarg); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 		switch (len) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		    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; | 
					
						
							|  |  |  | 		    case 'u': | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 		    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; | 
					
						
							|  |  |  | 		    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'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			options.usedhosts = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		    case 'L': | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 		    case 'o': | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 			if (strcmp(optarg, "data")==0) | 
					
						
							|  |  |  | 				options.output |= OUTPUT_DATA; | 
					
						
							|  |  |  | 			else if (strcmp(optarg, "db")==0) | 
					
						
							|  |  |  | 				options.output |= OUTPUT_DB; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		    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; | 
					
						
							|  |  |  | 		    case 'v': | 
					
						
							|  |  |  | 			if (optarg && optarg[0]=='v') | 
					
						
							|  |  |  | 				options.verbose = 3; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				options.verbose = 1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		    case 'V': | 
					
						
							|  |  |  | 			print_version(); | 
					
						
							|  |  |  | 			exit(0); | 
					
						
							|  |  |  | 		    case 'w': | 
					
						
							| 
									
										
										
										
											2005-12-22 20:35:09 +00:00
										 |  |  | 			strncpy(options.password, optarg, sizeof(options.password)); | 
					
						
							|  |  |  | 			options.password[ sizeof( options.password ) ] = '\0'; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:23:05 +00:00
										 |  |  | 			memset(optarg, 'x', strlen(options.password)); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +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; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		    default: | 
					
						
							|  |  |  | 			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) { | 
					
						
							|  |  |  | 		if (tinyfile) | 
					
						
							|  |  |  | 			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); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	if ( (ldaperr = ldap_search_s(ldap_con, dn, LDAP_SCOPE_SUBTREE, "objectclass=DNSrrset", NULL, 0, &res))!=LDAP_SUCCESS ) | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 		die_ldap(ldaperr); | 
					
						
							|  |  |  | 	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) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		fprintf(namedzone, "; Automatically generated by ldap2dns - DO NOT EDIT!\n"); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 	if ( ldaperr = ldap_search_s(ldap_con, options.searchbase[0] ? options.searchbase : NULL, LDAP_SCOPE_SUBTREE, "objectclass=DNSzone", attr_list, 0, &res)!=LDAP_SUCCESS ) | 
					
						
							|  |  |  | 		die_ldap(ldaperr); | 
					
						
							|  |  |  | 	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) | 
					
						
							|  |  |  | 		fprintf(tinyfile, "# Automatically generated by ldap2dns - DO NOT EDIT!\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:17:15 +00:00
										 |  |  | 	if (namedmaster) | 
					
						
							| 
									
										
										
										
											2006-05-10 02:38:02 +00:00
										 |  |  | 		fprintf(namedmaster, "# Automatically generated by ldap2dns - DO NOT EDIT!\n"); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 	if ( (ldaperr = ldap_search_s(ldap_con, options.searchbase[0] ? options.searchbase : NULL, LDAP_SCOPE_SUBTREE, "objectclass=DNSzone", NULL, 0, &res))!=LDAP_SUCCESS ) | 
					
						
							|  |  |  | 		die_ldap(ldaperr); | 
					
						
							|  |  |  | 	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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tinyfile) | 
					
						
							|  |  |  | 		fprintf(tinyfile, "# Location Codes (if any) - generated by ldap2dns - DO NOT EDIT!\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ( (ldaperr = ldap_search_s(ldap_con, options.searchbase[0] ? options.searchbase : NULL,  | 
					
						
							|  |  |  | 								  LDAP_SCOPE_SUBTREE,  | 
					
						
							|  |  |  | 								  "objectclass=DNSloccodes",  | 
					
						
							|  |  |  | 								  NULL,  | 
					
						
							|  |  |  | 								  0,  | 
					
						
							|  |  |  | 								  &res) | 
					
						
							|  |  |  | 			 )!=LDAP_SUCCESS ) | 
					
						
							|  |  |  | 		die_ldap(ldaperr); | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | static int connect() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 	int i, rc, version; | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 	for (i = 0; i<options.usedhosts; i++) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 		if ( strlen(options.urildap[i]) > 0) { | 
					
						
							|  |  |  | 			rc = ldap_initialize(&ldap_con, options.urildap[i]); | 
					
						
							|  |  |  | 			if (options.verbose&1 && rc == LDAP_SUCCESS) { | 
					
						
							|  |  |  | 				printf("ldap_initialization successful (%s)\n", options.urildap[i]); | 
					
						
							|  |  |  | 			} else if ( rc != LDAP_SUCCESS ) { | 
					
						
							|  |  |  | 				printf("ldap_initialization to %s failed %d\n", options.urildap[i], ldap_err2string(rc)); | 
					
						
							|  |  |  | 				ldap_con = NULL; | 
					
						
							|  |  |  | 				return 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			version = LDAP_VERSION3; | 
					
						
							|  |  |  | 			if ( (rc=ldap_set_option(ldap_con, LDAP_OPT_PROTOCOL_VERSION, &version)) != LDAP_SUCCESS ) { | 
					
						
							|  |  |  | 				printf("ldap_set_option to %s failed with err %s!\n", options.urildap[i], ldap_err2string(rc)); | 
					
						
							|  |  |  | 				ldap_con = NULL; | 
					
						
							|  |  |  | 				return 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if ( options.use_tls[i] && (rc=ldap_start_tls_s( ldap_con, NULL, NULL )) != LDAP_SUCCESS ) { | 
					
						
							|  |  |  | 				printf("ldap_start_tls_s to %s failed with err %s!\n", options.urildap[i], ldap_err2string(rc)); | 
					
						
							|  |  |  | 				ldap_con = NULL; | 
					
						
							|  |  |  | 				return 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ldap_con = ldap_init(options.hostname[i], options.port[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 		if (ldap_simple_bind_s(ldap_con, options.binddn, options.password)==LDAP_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			if (options.verbose&1 && strlen(options.urildap[i]) > 0) { | 
					
						
							|  |  |  | 				printf("Connected to %s as \"%s\"\n", options.urildap[i], options.binddn); | 
					
						
							|  |  |  | 			} else if (options.verbose&1) { | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 				printf("Connected to %s:%d as \"%s\"\n", options.hostname[i], options.port[i], options.binddn); | 
					
						
							| 
									
										
										
										
											2005-12-02 04:26:14 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-12-02 04:21:08 +00:00
										 |  |  | 			return 1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ldap_con = NULL; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-02 04:09:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char** argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int soa_numzones; | 
					
						
							|  |  |  | 	int soa_checksum; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	umask(022); | 
					
						
							|  |  |  | 	main_argc = argc; | 
					
						
							|  |  |  | 	main_argv = argv; | 
					
						
							|  |  |  | 	parse_options(); | 
					
						
							| 
									
										
										
										
											2006-03-15 20:08:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fprintf(stdout, "ldap2dns v%s starting up", VERSION); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* 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
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 		if (!connect()) { | 
					
						
							|  |  |  | 			fprintf(stderr, "Warning - Could not connect to any LDAP server\n"); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (options.is_daemon) { | 
					
						
							|  |  |  | 			int num, sum; | 
					
						
							|  |  |  | 			calc_checksum(&num, &sum); | 
					
						
							|  |  |  | 			if (num!=soa_numzones || sum!=soa_checksum) { | 
					
						
							|  |  |  | 				if (options.verbose&1) | 
					
						
							|  |  |  | 					printf("DNSserial has changed in LDAP zone(s)\n"); | 
					
						
							|  |  |  | 				soa_numzones = num; | 
					
						
							|  |  |  | 				soa_checksum = sum; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				goto skip; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		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: | 
					
						
							|  |  |  | 		if ( (ldaperr = ldap_unbind_s(ldap_con))!=LDAP_SUCCESS ) | 
					
						
							|  |  |  | 			die_ldap(ldaperr); | 
					
						
							|  |  |  | 		if (options.is_daemon==0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		sleep(options.update_iv); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |