summaryrefslogtreecommitdiff
path: root/common/nsupdate.c
blob: 6d7d39d0a99633f5744797ae02d89495c19899db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* nsupdate.c

   Tables of information... */

/*
 * Copyright (c) 1999 Internet Software Consortium.
 * Use is subject to license terms which appear in the file named
 * ISC-LICENSE that should have accompanied this file when you
 * received it.   If a file named ISC-LICENSE did not accompany this
 * file, or you are not sure the one you have is correct, you may
 * obtain an applicable copy of the license at:
 *
 *             http://www.isc.org/isc-license-1.0.html. 
 *
 * This file is part of the ISC DHCP distribution.   The documentation
 * associated with this file is listed in the file DOCUMENTATION,
 * included in the top-level directory of this release.
 *
 * Support and other services are available for ISC products - see
 * http://www.isc.org for more information.
 *
 * Note: file is currently based on work done by Brian Murrell, Irina
 * Goble and others.   The copyright message is not final.
 */

#ifndef lint
static char copyright[] =
"$Id: nsupdate.c,v 1.7 1999/07/31 17:57:36 mellon Exp $ Copyright (c) 1999 The Internet Software Consortium.  All rights reserved.\n";
#endif /* not lint */

#include "dhcpd.h"

#if defined (NSUPDATE)
/* Yet another new plan.
   First thing to be done is to determine if a change actually needs to be
   done.  This can be done by figuring out the hostname.domain and revname
   and then comparing them to the lease->ddns* values.  Only make the changes
   necessary which can be any combination and number of remove A, add A,
   remove PTR, add PTR */

#if 0
/* Return the reverse name of a lease. */
char *ddns_rev_name(lease, state, packet)
	struct lease *lease;
	struct lease_state *state;
	struct packet *packet;
{
	struct option_cache *oc = NULL;
	struct data_string d;
	static char revname[MAXDNAME];
	char	revdomain[MAXDNAME];

	revname[0]='\0';
	revdomain[0]='\0';

	/* Figure out what reverse domain to update
	   First take the scoped "ddns-rev-domainname" option if present
	   then assume d.c.b.a.in-addr.arpa. if not present
	   error if neither are present */
	oc = lookup_option (&server_universe, state -> options,
			    SV_DDNS_REV_DOMAIN_NAME);
	memset (&d, 0, sizeof d);
	if (oc && evaluate_option_cache (&d, packet, lease,
					 packet -> options, state -> options,
					 oc)) {
		memcpy(revdomain, d.data, d.len);
		revdomain[d.len]='\0';
		data_string_forget (&d, "nsupdate");
	} else
		strcpy(revdomain, "in-addr.arpa");
	if ( lease->ip_addr.len != 4 ) { /* IPv4 */
		log_error("unsupported IP address length: %d",
			  lease->ip_addr.len);
		return NULL;
	}
#if defined (NO_SNPRINTF)
	sprintf(revname, "%d.%d.%d.%d.%s.",
		 lease->ip_addr.iabuf[3], lease->ip_addr.iabuf[2],
		 lease->ip_addr.iabuf[1], lease->ip_addr.iabuf[0], revdomain);
#else
	snprintf(revname, MAXDNAME, "%d.%d.%d.%d.%s.",
		 lease->ip_addr.iabuf[3], lease->ip_addr.iabuf[2],
		 lease->ip_addr.iabuf[1], lease->ip_addr.iabuf[0], revdomain);
#endif
	return revname;
}

/* Return the forward name of a lease. */
char *ddns_fwd_name(lease, state, packet)
	struct lease *lease;
	struct lease_state *state;
	struct packet *packet;
{
	struct option_cache *oc = NULL;
	struct data_string d;
	static char hostname[MAXDNAME];
	char	domain[MAXDNAME];

	domain[0]='\0';
	hostname[0]='\0';

	/* Figure out the domain name of a lease.
	   First take the scoped "ddns-domainname" option if present
	   then the dhcp option "domain-name" if present
	   error if neither are present */
	oc = lookup_option (&server_universe, state -> options,
			    SV_DDNS_DOMAIN_NAME);
	memset (&d, 0, sizeof d);
	if (oc && evaluate_option_cache (&d, packet, lease,
					 packet -> options, state -> options,
					 oc)) {
		memcpy(domain, d.data, d.len);
		domain[d.len]='\0';
		data_string_forget (&d, "nsupdate");
	} else {
		oc = lookup_option (&dhcp_universe, state -> options,
				    DHO_DOMAIN_NAME);
		memset (&d, 0, sizeof d);
		if (oc && evaluate_option_cache (&d, packet, lease,
						 packet -> options,
						 state -> options,
						 oc)) {
			memcpy(domain, d.data, d.len);
			domain[d.len]='\0';
			data_string_forget (&d, "nsupdate");
		} else {
			log_info("nsupdate failed: %s",
				 "unknown domain for update");
			return NULL;
		}
	}

	/* Figure out what host in the domain to assign to.
	   First take the scoped "ddns-hostname" option
	   then the dhcp option "host-name" if present
	   then the dhcp host_decl "name" if present
	   error if neither are present. */
	oc = lookup_option (&server_universe, state -> options,
			    SV_DDNS_HOST_NAME);
	memset (&d, 0, sizeof d);
	if (oc && evaluate_option_cache (&d, packet, lease,
					 packet -> options, state -> options,
					 oc)) {
		memcpy(hostname, d.data, d.len);
		hostname[d.len]='\0';
		data_string_forget (&d, "nsupdate");
	} else {
		oc = lookup_option (&dhcp_universe,
				    packet -> options, DHO_HOST_NAME);
		memset (&d, 0, sizeof d);
		if (oc && evaluate_option_cache (&d, packet, lease,
						 packet -> options,
						 state -> options, oc)) {
			memcpy(hostname, d.data, d.len);
			hostname[d.len]='\0';
			data_string_forget (&d, "nsupdate");
		} else {
			if (lease -> host && lease -> host -> name &&
			    strcmp(lease -> host -> name, "")!=0)
				strcpy(hostname, lease -> host -> name);
			else {
				log_info("nsupdate failed: %s",
					 "unknown hostname for update");
				return NULL;
			}
		}
	}
	if ( !res_hnok(hostname) ) {
		log_error("nsupdate: Bad hostname \"%s\"", hostname);
		return NULL;
	}

#if defined (NO_SNPRINTF)
	if (sprintf(hostname, "%s.%s.", hostname, domain) < 0) {
		log_error("nsupdate: Build FQDN failed");
		return NULL;
	}
#else
	if (snprintf(hostname, MAXDNAME, "%s.%s.", hostname, domain) < 0) {
		log_error("nsupdate: Build FQDN failed");
		return NULL;
	}
#endif
	return hostname;
}
#endif

static int nsupdateA(hostname, ip_addr, ttl, opcode)
	char *hostname;
	char *ip_addr;
	u_int32_t ttl;
	int	opcode;
{
	int 	z;
	ns_updrec	*u, *n;

	switch (opcode) {
	case ADD:
		if (!(u = res_mkupdrec(S_PREREQ, hostname, C_IN, T_A, 0))) 
			return 0;
		u->r_opcode = NXRRSET; u->r_data = NULL; u->r_size = 0;
		if (!(n = res_mkupdrec(S_UPDATE, hostname, C_IN, T_A, ttl))) {
			res_freeupdrec(u);
			return 0;
		}
		n->r_opcode = opcode;
		n->r_data = ip_addr;
		n->r_size = strlen(n->r_data);
		u->r_next = n;
		z = res_update(u);
		log_info("add %s: %s %d IN A %s",
			 z == 1 ? "succeeded" : "failed", hostname, ttl,
			 n->r_data);
		res_freeupdrec(u); 
		res_freeupdrec(n);
/* do we really need to do this?  If so, it needs to be done elsewehere as
   this function is strictly for manipulating the A record
		if (z < 1) 
			return 0;
*/
		/* delete all PTR RRs with the same ip address. Wow! */
/*
		if (!(u = res_mkupdrec(S_UPDATE, revname, C_IN, T_PTR, 0)))
			return 0;
		u->r_opcode = DELETE; u->r_data = NULL; u->r_size = 0;
		log_info("cleaning all PTR RRs for %s", revname);
		res_update(u);
		res_freeupdrec(u);
*/
		break;
	case	DELETE:
		ttl = 0;
		if (!(u = res_mkupdrec(S_UPDATE, hostname, C_IN, T_A, ttl)))
			return 0;
		u->r_opcode = opcode;
		u->r_data = ip_addr;
		u->r_size = strlen(u->r_data);
		z = res_update(u);
		log_info("delete %s: %s %d IN A %s",
			 z == 1 ? "succeeded" : "failed",
			 hostname, ttl, u->r_data);
		res_freeupdrec(u);
		break;
	}
	return z;
}

static int nsupdatePTR(revname, hostname, ttl, opcode)
	char *hostname;
	char *revname;
	u_int32_t ttl;
	int	opcode;
{
	int 	z;
	ns_updrec	*u;

	if (opcode == DELETE)
		ttl = 0;
	if (!(u = res_mkupdrec(S_UPDATE, revname, C_IN, T_PTR, ttl)))
		return 0;
	u->r_opcode = opcode;
	u->r_data = hostname;
	u->r_size = strlen(u->r_data);
	z = res_update(u);
	log_info("%s %s: %s %d IN PTR %s", 
		 opcode == ADD ? "add" : 
				 "delete", z == 1 ? "succeeded" : "failed",
		 revname, ttl, hostname);
	res_freeupdrec(u);
	return z;
}

#if 0
void nsupdate(lease, state, packet, opcode)
	struct lease *lease;
	struct lease_state *state;
	struct packet *packet;
	int	opcode;
{
	char	*hostname, *revname;
	u_int32_t	ttl = 0;

return;
	if (!(opcode == ADD || opcode == DELETE))
		return;

	if (!packet){
		log_info("invalid pointer at %s:%d",
			 __FILE__, __LINE__-2);
		return;
	}
	if (!packet -> options) {
		log_info("invalid pointer at %s:%d",
			 __FILE__, __LINE__-2);
		return;
	}
	if (!lease){
		log_info("invalid pointer at %s:%d",
			 __FILE__, __LINE__-2);
		return;
	}
	
	switch (opcode) {
	case ADD:
		if (!state) {
			log_info("invalid pointer at %s:%d",
				 __FILE__, __LINE__-2);
			return;
		}
		if (!state -> options) {
			log_info("invalid pointer at %s:%d",
				 __FILE__, __LINE__-2);
			return;
		}

		hostname = ddns_fwd_name(lease, state, packet);
		if (!hostname)	/* there is nothing we can do now */
			return;
		revname = ddns_rev_name(lease, state, packet);
		
		if (state -> offered_expiry > lease -> timestamp)
			ttl = state -> offered_expiry -
			lease->timestamp;
		else
			log_error("nsupdate: ttl < 0");

		/* delete an existing A if the one to be added is different */
		if (lease -> ddns_fwd_name &&
		    strcmp(hostname, lease -> ddns_fwd_name)) {
			int y;
			y=nsupdateA(lease -> ddns_fwd_name,
				    piaddr(lease->ip_addr), ttl, DELETE);

			/* delete an existing PTR if new one is different */
			if (lease -> ddns_rev_name &&
			    (strcmp(hostname, lease -> ddns_fwd_name) ||
			     strcmp(revname, lease -> ddns_rev_name)) &&
			    nsupdatePTR(revname, lease -> ddns_fwd_name,
					      ttl, DELETE)) {
				/* clear the forward DNS name pointer */
				if (lease -> ddns_rev_name)
					dfree(lease -> ddns_rev_name,
					      "nsupdate");
				lease -> ddns_rev_name = 0;
			}
			if (y) {
				/* clear the forward DNS name pointer */
				if (lease -> ddns_fwd_name)
					dfree(lease -> ddns_fwd_name,
					      "nsupdate");
				lease -> ddns_fwd_name = 0;
			}
		}
		/* only update if there is no A record there already */
		if (!lease -> ddns_fwd_name) {
			int z;
		    	z=nsupdateA(hostname, piaddr(lease->ip_addr), ttl, ADD);
			if (z < 1)
				return;

			/* remember this in the lease structure for release */
			lease -> ddns_fwd_name = dmalloc(strlen(hostname) + 1,
						 	 "nsupdate");
			strcpy (lease -> ddns_fwd_name, hostname);
		}

		if (!revname)	/* there is nothing more we can do now */
			return;

	/* This is where a deletion of all PTRs for this addy could
	   go, but I am reluctant to overburden the DHCP and DNS
	   servers with requests that should be invalid if this is
	   really a problem then somebody else is inserting PTRs and
	   they should stop, rather than this being turned into a
	   garbage cleanup routine */

		if (!lease -> ddns_rev_name) {
			/* add a PTR RR */
			if (nsupdatePTR(revname, hostname, ttl, ADD)) {
				/* remember in the lease struct for a release */
				lease -> ddns_rev_name =
				       dmalloc(strlen(revname) + 1, "nsupdate");
				strcpy (lease -> ddns_rev_name, revname);
			}
		}
		break;
	case DELETE:
		ttl = 0;
		if (lease -> ddns_fwd_name) {
			int y;
			y = nsupdateA(lease -> ddns_fwd_name,
				      piaddr(lease->ip_addr), ttl, DELETE);

			if (lease -> ddns_rev_name &&
			    nsupdatePTR(lease -> ddns_rev_name,
					lease -> ddns_fwd_name,
					ttl, opcode)) {
				/* clear the reverse DNS name pointer */
				if (lease -> ddns_rev_name)
					dfree(lease -> ddns_rev_name,
					      "nsupdate");
				lease -> ddns_rev_name = 0;
			}

			if (y) {
				/* clear the forward DNS name pointer */
				if (lease -> ddns_fwd_name)
					dfree(lease -> ddns_fwd_name,
					      "nsupdate");
				lease -> ddns_fwd_name = 0;
			}
		}
		break;
	}
	
	return;
}
#endif

/* public function to update an A record if needed */
int updateA(struct data_string *lhs, struct data_string *rhs, unsigned int ttl,
	struct lease *lease)
{

	static char hostname[MAXDNAME];
	static char ipaddr[MAXDNAME];

	hostname[0] = '\0';
	strncat(hostname, lhs->data, lhs->len);
	hostname[lhs->len] = '\0';
	
	ipaddr[0] = '\0';
	strncat(ipaddr, rhs->data, rhs->len);
	ipaddr[rhs->len] = '\0';
	
	/* delete an existing A if the one to be added is different */
	if (lease -> ddns_fwd_name &&
	    strcmp(hostname, lease -> ddns_fwd_name)) {
		int y;
		y=nsupdateA(lease -> ddns_fwd_name, ipaddr, 0, DELETE);

		if (y) {
			/* clear the forward DNS name pointer */
			if (lease -> ddns_fwd_name)
				dfree(lease -> ddns_fwd_name, "nsupdate");
			lease -> ddns_fwd_name = 0;
		}
	}
	/* only update if there is no A record there already */
	if (!lease -> ddns_fwd_name) {
		int y;
	    	y=nsupdateA(hostname, ipaddr, ttl, ADD);
		if (y < 1)
			return 0;

		/* remember this in the lease structure for release */
		lease -> ddns_fwd_name = dmalloc(strlen(hostname) + 1,
					 	 "nsupdate");
		strcpy (lease -> ddns_fwd_name, hostname);
	}

	return 1;
}

/* public function to update an A record if needed */
int updatePTR(struct data_string *lhs, struct data_string *rhs,
	      unsigned int ttl, struct lease *lease)
{

	static char hostname[MAXDNAME];
	static char revname[MAXDNAME];

	revname[0] = '\0';
	strncat(revname, lhs->data, lhs->len);
	revname[lhs->len] = '\0';

	hostname[0] = '\0';
	strncat(hostname, rhs->data, rhs->len);
	hostname[rhs->len] = '\0';
	
	/* delete an existing PTR if the one to be added is different */
	if (lease -> ddns_rev_name &&
	    strcmp(revname, lease -> ddns_rev_name)) {
		int y;
		y=nsupdatePTR(revname, hostname, 0, DELETE);

		if (y) {
			/* clear the reverse DNS name pointer */
			if (lease -> ddns_rev_name)
				dfree(lease -> ddns_rev_name, "nsupdate");
			lease -> ddns_rev_name = 0;
		}
	}
	/* only update if there is no PTR record there already */
	if (!lease -> ddns_rev_name) {
		int y;
	    	y=nsupdatePTR(revname, hostname, ttl, ADD);
		if (y < 1)
			return 0;

		/* remember this in the lease structure for release */
		lease -> ddns_rev_name = dmalloc(strlen(revname) + 1,
					 	 "nsupdate");
		strcpy (lease -> ddns_rev_name, revname);
	}

	return 1;
}
#endif /* defined (NSUPDATE) */