nsupdate.c revision bd6504aa9aa16a912412fbe010046aaf4bf23621
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: nsupdate.c,v 1.97 2001/07/11 06:30:54 bwelling Exp $ */
#include <config.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <isc/commandline.h>
#include <isc/sockaddr.h>
#include <dns/callbacks.h>
#include <dns/dispatch.h>
#include <dns/fixedname.h>
#include <dns/masterdump.h>
#include <dns/rdataclass.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatastruct.h>
#include <dns/rdatatype.h>
#ifdef HAVE_ADDRINFO
#ifdef HAVE_GETADDRINFO
#ifdef HAVE_GAISTRERROR
#define USE_GETADDRINFO
#endif
#endif
#endif
#ifndef USE_GETADDRINFO
extern int h_errno;
#endif
#define NAMEBUF 512
#define WORDLEN 512
#define FIND_TIMEOUT 5
#define DNSDEFAULTPORT 53
#define RESOLV_CONF "/etc/resolv.conf"
static dns_fixedname_t fuserzone;
static lwres_conf_t *lwconf;
static isc_sockaddr_t *servers;
static int ns_inuse = 0;
static int ns_total = 0;
static int requests = 0;
typedef struct nsu_requestinfo {
static void
#define STATUS_MORE (isc_uint16_t)0
static void
exit(1);
}
static void
if (debugging) {
}
}
static void
if (ddebugging) {
}
}
static inline void
if (result != ISC_R_SUCCESS)
}
static void *
}
static void
}
static char *
char *s;
const char *d;
return (NULL);
break;
}
if (dc == 0)
break;
}
for (s = string; *s != '\0'; s++) {
sc = *s;
*s++ = '\0';
*stringp = s;
return (string);
}
}
}
return (string);
}
static unsigned int
int i = 0;
while (*s != 0) {
if (*s++ == '.') {
i++;
*last_was_dot = ISC_TRUE;
} else
}
return (i);
}
static void
reset_system(void) {
ddebug("reset_system()");
/* If the update message is still around, destroy it */
else {
&updatemsg);
}
}
static void
setup_key(void) {
int secretlen;
char *secretstr;
char *s;
debug("Creating key...");
fatal("key option must specify keyname:secret");
secretstr = s + 1;
debug("namefromtext");
fatal("out of memory");
if (result != ISC_R_SUCCESS) {
goto failure;
}
debug("close");
} else {
if (result != ISC_R_SUCCESS) {
goto failure;
}
fatal("out of memory");
if (result != ISC_R_SUCCESS) {
goto failure;
}
}
debug("keycreate");
if (result != ISC_R_SUCCESS) {
char *str;
else
}
return;
}
static void
doshutdown(void) {
if (userserver != NULL)
debug("Freeing key");
}
if (is_dst_up) {
debug("Destroy DST lib");
}
debug("Detach from entropy");
}
ddebug("Destroying request manager");
ddebug("Freeing the dispatchers");
if (have_ipv4)
if (have_ipv6)
ddebug("Shutting down dispatch manager");
}
static void
maybeshutdown(void) {
ddebug("Shutting down request manager");
if (requests != 0)
return;
doshutdown();
}
static void
ddebug("shutdown_program()");
}
static void
setup_system(void) {
int i;
ddebug("setup_system()");
result = isc_net_probeipv4();
if (result == ISC_R_SUCCESS)
result = isc_net_probeipv6();
if (result == ISC_R_SUCCESS)
fatal("could not find either IPv4 or IPv6");
if (lwresult != LWRES_R_SUCCESS)
fatal("lwres_context_create failed");
if (lwresult != LWRES_R_SUCCESS)
"an error was encountered in %s\n", RESOLV_CONF);
if (ns_total <= 0)
fatal("no valid servers found");
fatal("out of memory");
for (i = 0; i < ns_total; i++) {
} else {
}
}
if (have_ipv6) {
4, 2, 3, 5,
}
if (have_ipv4) {
4, 2, 3, 5,
}
NULL);
}
else
setup_key();
}
static void
#ifdef USE_GETADDRINFO
int result;
#else
#endif
ddebug("get_address()");
/*
* Assume we have v4 if we don't have v6, since setup_libs
* fatal()'s out if we don't have either.
*/
else {
#ifdef USE_GETADDRINFO
if (!have_ipv6)
else if (!have_ipv4)
else
debug ("before getaddrinfo()");
if (result != 0) {
fatal("couldn't find server '%s': %s",
}
#else
debug ("before gethostbyname()");
fatal("couldn't find server '%s' (h_errno=%d)",
port);
#endif
}
}
static void
int ch;
debug("parse_args");
switch (ch) {
case 'd':
break;
case 'D': /* was -dd */
break;
case 'M': /* was -dm */
break;
case 'y':
break;
case 'v':
break;
case 'k':
break;
default:
"[-y keyname:secret | -k keyfile] [-v]\n");
exit(1);
}
}
argv[0]);
exit(1);
}
} else {
"r", &input);
if (result != ISC_R_SUCCESS) {
exit(1);
}
}
}
}
static isc_uint16_t
char *word;
unsigned int dots;
dns_name_t *rn;
if (*word == 0) {
return (STATUS_SYNTAX);
}
rn = dns_rootname;
else
return (STATUS_MORE);
}
static isc_uint16_t
{
dns_name_t *rn;
cmdline++;
if (*cmdline != 0) {
else
do {
"space for the rdata\n");
exit(1);
}
bufsz *= 2;
} while (result == ISC_R_NOSPACE);
if (result != ISC_R_SUCCESS) {
return (STATUS_SYNTAX);
}
} else {
}
return (STATUS_MORE);
}
static isc_uint16_t
char *word;
ddebug("make_prereq()");
/*
* Read the owner name
*/
if (retval != STATUS_MORE)
return (retval);
/*
* If this is an rrset prereq, read the class or type.
*/
if (isrrset) {
if (*word == 0) {
goto failure;
}
if (result == ISC_R_SUCCESS) {
/*
* Now read the type.
*/
if (*word == 0) {
goto failure;
}
if (result != ISC_R_SUCCESS) {
goto failure;
}
} else {
if (result != ISC_R_SUCCESS) {
goto failure;
}
}
} else
if (isrrset && ispositive) {
if (retval != STATUS_MORE)
goto failure;
} else
if (ispositive) {
else
} else
return (STATUS_MORE);
return (STATUS_SYNTAX);
}
static isc_uint16_t
evaluate_prereq(char *cmdline) {
char *word;
ddebug("evaluate_prereq()");
if (*word == 0) {
return (STATUS_SYNTAX);
}
} else {
return (STATUS_SYNTAX);
}
}
static isc_uint16_t
evaluate_server(char *cmdline) {
long port;
if (*word == 0) {
return (STATUS_SYNTAX);
}
if (*word == 0)
else {
char *endp;
if (*endp != 0) {
return (STATUS_SYNTAX);
"(1 to 65535)\n", word);
return (STATUS_SYNTAX);
}
}
if (userserver == NULL) {
if (userserver == NULL)
fatal("out of memory");
}
return (STATUS_MORE);
}
static isc_uint16_t
evaluate_local(char *cmdline) {
long port;
if (*word == 0) {
return (STATUS_SYNTAX);
}
if (*word == 0)
port = 0;
else {
char *endp;
if (*endp != 0) {
return (STATUS_SYNTAX);
"(1 to 65535)\n", word);
return (STATUS_SYNTAX);
}
}
fatal("out of memory");
}
else {
return (STATUS_SYNTAX);
}
return (STATUS_MORE);
}
static isc_uint16_t
evaluate_key(char *cmdline) {
char *namestr;
char *secretstr;
isc_buffer_t b;
int secretlen;
if (*namestr == 0) {
return (STATUS_SYNTAX);
}
if (result != ISC_R_SUCCESS) {
return (STATUS_SYNTAX);
}
if (*secretstr == 0) {
return (STATUS_SYNTAX);
}
fatal("out of memory");
if (result != ISC_R_SUCCESS) {
return (STATUS_SYNTAX);
}
if (result != ISC_R_SUCCESS) {
return (STATUS_SYNTAX);
}
return (STATUS_MORE);
}
static isc_uint16_t
evaluate_zone(char *cmdline) {
char *word;
isc_buffer_t b;
if (*word == 0) {
return (STATUS_SYNTAX);
}
NULL);
if (result != ISC_R_SUCCESS) {
return (STATUS_SYNTAX);
}
return (STATUS_MORE);
}
static isc_uint16_t
long ttl;
char *word;
char *endp;
ddebug("update_addordelete()");
/*
* Read the owner name.
*/
if (retval != STATUS_MORE)
return (retval);
/*
* If this is an add, read the TTL and verify that it's in range.
* If it's a delete, ignore a TTL if present (for compatibility).
*/
if (*word == 0) {
if (!isdelete) {
goto failure;
}
else {
ttl = 0;
goto doneparsing;
}
}
if (*endp != '\0') {
if (isdelete) {
ttl = 0;
goto parseclass;
} else {
goto failure;
}
}
if (isdelete)
ttl = 0;
/*
* The errno test is needed to catch when strtol()
* overflows on a platform where sizeof(int) ==
* sizeof(long), because ttl will be set to LONG_MAX,
* which will be equal to TTL_MAX.
*/
goto failure;
}
/*
* Read the class or type.
*/
if (*word == 0) {
if (isdelete) {
goto doneparsing;
} else {
goto failure;
}
}
if (result == ISC_R_SUCCESS) {
/*
* Now read the type.
*/
if (*word == 0) {
if (isdelete) {
goto doneparsing;
} else {
goto failure;
}
}
if (result != ISC_R_SUCCESS) {
goto failure;
}
} else {
if (result != ISC_R_SUCCESS) {
goto failure;
}
}
rdata);
if (retval != STATUS_MORE)
goto failure;
if (isdelete) {
else
} else {
goto failure;
}
}
return (STATUS_MORE);
return (STATUS_SYNTAX);
}
static isc_uint16_t
evaluate_update(char *cmdline) {
char *word;
ddebug("evaluate_update()");
if (*word == 0) {
return (STATUS_SYNTAX);
}
else {
return (STATUS_SYNTAX);
}
}
static void
int bufsz;
ddebug("show_message()");
do {
"buffer to display message\n");
exit(1);
}
bufsz *= 2;
} while (result == ISC_R_NOSPACE);
if (result != ISC_R_SUCCESS) {
return;
}
printf("Outgoing update query:\n%.*s",
(int)isc_buffer_usedlength(buf),
(char*)isc_buffer_base(buf));
}
static isc_uint16_t
get_next_command(void) {
char cmdlinebuf[MAXCMD];
char *cmdline;
char *word;
ddebug("get_next_command()");
if (interactive)
return (STATUS_QUIT);
return (STATUS_QUIT);
if (*word == 0)
return (STATUS_SEND);
if (word[0] == ';')
return (STATUS_MORE);
return (STATUS_QUIT);
return (evaluate_prereq(cmdline));
return (evaluate_update(cmdline));
return (evaluate_server(cmdline));
return (evaluate_local(cmdline));
return (evaluate_zone(cmdline));
return (STATUS_SEND);
return (STATUS_MORE);
}
return (evaluate_key(cmdline));
return (STATUS_SYNTAX);
}
static isc_boolean_t
user_interaction(void) {
ddebug("user_interaction()");
result = get_next_command();
if (result == STATUS_SEND)
return (ISC_TRUE);
return (ISC_FALSE);
}
static void
done_update(void) {
ddebug("done_update()");
}
static void
ddebug("update_completed()");
requests--;
if (shuttingdown) {
return;
}
goto done;
}
switch (result) {
case ISC_R_SUCCESS:
break;
case DNS_R_CLOCKSKEW:
case DNS_R_EXPECTEDTSIG:
case DNS_R_TSIGERRORSET:
case DNS_R_TSIGVERIFYFAILURE:
case DNS_R_UNEXPECTEDTSIG:
break;
default:
}
if (debugging) {
int bufsz;
do {
"enough buffer to display message\n");
exit(1);
}
bufsz *= 2;
} while (result == ISC_R_NOSPACE);
(int)isc_buffer_usedlength(buf),
(char*)isc_buffer_base(buf));
}
done:
done_update();
}
static void
{
unsigned int options = 0;
ddebug("send_update()");
if (usevc)
requests++;
}
static void
int pass = 0;
ddebug("recvsoa()");
requests--;
if (shuttingdown) {
return;
}
if (eresult != ISC_R_SUCCESS) {
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
if (userserver != NULL)
fatal("could not talk to specified name server");
fatal("could not talk to any default name server");
return;
}
ddebug("About to create rcvmsg");
if (debugging) {
int bufsz;
do {
"space for debugging message\n");
exit(1);
}
} while (result == ISC_R_NOSPACE);
(int)isc_buffer_usedlength(buf),
(char*)isc_buffer_base(buf));
}
fatal("response to SOA query was unsuccessful");
if (pass == 0)
else if (pass == 1)
else
fatal("response to SOA query didn't contain an SOA");
if (result != ISC_R_SUCCESS) {
pass++;
goto lookforsoa;
}
while (result == ISC_R_SUCCESS) {
&soaset);
if (result == ISC_R_SUCCESS)
break;
if (section == DNS_SECTION_ANSWER) {
&tset) == ISC_R_SUCCESS
||
&tset) == ISC_R_SUCCESS
)
{
break;
}
}
}
pass++;
goto lookforsoa;
}
if (seencname) {
unsigned int nlabels;
if (nlabels == 1)
fatal("could not find enclosing zone");
if (userserver != NULL)
else
&request);
goto out;
}
if (debugging) {
char namestr[DNS_NAME_FORMATSIZE];
}
else
if (debugging) {
char namestr[DNS_NAME_FORMATSIZE];
}
if (userserver != NULL)
else {
serveraddr = &tempaddr;
}
out:
ddebug("Out of recvsoa");
}
static void
{
fatal("out of memory");
requests++;
}
static void
start_update(void) {
ddebug("start_update()");
if (result != ISC_R_SUCCESS) {
done_update();
return;
}
if (result != ISC_R_SUCCESS) {
done_update();
return;
}
return;
}
&soaquery);
if (userserver != NULL)
else {
ns_inuse = 0;
}
}
static void
cleanup(void) {
ddebug("cleanup()");
ddebug("Shutting down task manager");
ddebug("Destroying event");
ddebug("Shutting down socket manager");
ddebug("Shutting down timer manager");
ddebug("Destroying memory context");
if (memdebugging)
}
static void
if (shuttingdown) {
return;
}
if (global_event == NULL)
reset_system();
more = user_interaction();
if (!more) {
return;
}
start_update();
return;
}
int
setup_system();
(void)isc_app_run();
cleanup();
if (seenerror)
return (2);
else
return (0);
}