mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-31 06:25:31 +00:00
Reduced code duplication. Code duplication is evil.
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* $Id: rndc-confgen.c,v 1.4 2001/08/03 22:12:42 gson Exp $ */
|
/* $Id: rndc-confgen.c,v 1.5 2001/08/03 22:33:02 gson Exp $ */
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
@@ -76,6 +76,45 @@ Usage:\n\
|
|||||||
exit (status);
|
exit (status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Write an rndc.key file to 'keyfile'. If 'user' is non-NULL,
|
||||||
|
* make that user the owner of the file. The key will have
|
||||||
|
* the name 'keyname' and the secret in the buffer 'secret'.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
write_key_file(const char *keyfile, const char *user,
|
||||||
|
const char *keyname, isc_buffer_t *secret )
|
||||||
|
{
|
||||||
|
FILE *fd;
|
||||||
|
|
||||||
|
fd = safe_create(keyfile);
|
||||||
|
if (fd == NULL) {
|
||||||
|
fprintf(stderr, "unable to create \"%s\"\n", keyfile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (user != NULL) {
|
||||||
|
if (set_user(fd, user) == -1) {
|
||||||
|
fprintf(stderr, "unable to set file owner\n");
|
||||||
|
fclose(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fprintf(fd, "key \"%s\" {\n\talgorithm hmac-md5;\n"
|
||||||
|
"\tsecret \"%.*s\";\n};\n", keyname,
|
||||||
|
(int)isc_buffer_usedlength(secret),
|
||||||
|
(char *)isc_buffer_base(secret));
|
||||||
|
fflush(fd);
|
||||||
|
if (ferror(fd)) {
|
||||||
|
fprintf(stderr, "write to %s failed\n", keyfile);
|
||||||
|
fclose(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fclose(fd)) {
|
||||||
|
fprintf(stderr, "fclose(%s) failed\n", keyfile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv) {
|
main(int argc, char **argv) {
|
||||||
isc_boolean_t show_final_mem = ISC_FALSE;
|
isc_boolean_t show_final_mem = ISC_FALSE;
|
||||||
@@ -104,8 +143,6 @@ main(int argc, char **argv) {
|
|||||||
char *user = NULL;
|
char *user = NULL;
|
||||||
isc_boolean_t keyonly = ISC_FALSE;
|
isc_boolean_t keyonly = ISC_FALSE;
|
||||||
int len;
|
int len;
|
||||||
FILE *fd;
|
|
||||||
char *buf;
|
|
||||||
|
|
||||||
keydef = keyfile = RNDC_KEYFILE;
|
keydef = keyfile = RNDC_KEYFILE;
|
||||||
|
|
||||||
@@ -241,74 +278,22 @@ main(int argc, char **argv) {
|
|||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
if (keyonly) {
|
if (keyonly) {
|
||||||
fd = safe_create(keyfile);
|
write_key_file(keyfile, chrootdir == NULL ? user : NULL,
|
||||||
if (fd == NULL) {
|
keyname, &key_txtbuffer);
|
||||||
fprintf(stderr, "unable to create \"%s\"\n", keyfile);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (user != NULL && chrootdir == NULL) {
|
|
||||||
if (set_user(fd, user) == -1) {
|
|
||||||
fprintf(stderr, "unable to set file owner\n");
|
|
||||||
fclose(fd);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fprintf(fd, "key \"%s\" {\n\talgorithm hmac-md5;\n"
|
|
||||||
"\tsecret \"%.*s\";\n};\n", keyname,
|
|
||||||
(int)isc_buffer_usedlength(&key_txtbuffer),
|
|
||||||
(char *)isc_buffer_base(&key_txtbuffer));
|
|
||||||
fflush(fd);
|
|
||||||
if (ferror(fd)) {
|
|
||||||
fprintf(stderr, "write to %s failed\n", keyfile);
|
|
||||||
fclose(fd);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (fclose(fd)) {
|
|
||||||
fprintf(stderr, "fclose(%s) failed\n", keyfile);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (chrootdir == NULL)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
len = strlen(chrootdir) + strlen(keyfile) + 2;
|
if (chrootdir != NULL) {
|
||||||
buf = isc_mem_get(mctx, len);
|
char *buf;
|
||||||
if (buf != NULL) {
|
len = strlen(chrootdir) + strlen(keyfile) + 2;
|
||||||
fprintf(stderr, "isc_mem_get(%d) failed\n", len);
|
buf = isc_mem_get(mctx, len);
|
||||||
goto cleanup;
|
if (buf != NULL) {
|
||||||
}
|
fprintf(stderr, "isc_mem_get(%d) failed\n", len);
|
||||||
snprintf(buf, len, "%s/%s", chrootdir, keyfile);
|
|
||||||
fd = safe_create(buf);
|
|
||||||
if (fd == NULL) {
|
|
||||||
fprintf(stderr, "unable to create \"%s\"\n",
|
|
||||||
buf);
|
|
||||||
isc_mem_put(mctx, buf, len);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (user != NULL) {
|
|
||||||
if (set_user(fd, user) == -1) {
|
|
||||||
fprintf(stderr, "unable to set file owner\n");
|
|
||||||
fclose(fd);
|
|
||||||
isc_mem_put(mctx, buf, len);
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
snprintf(buf, len, "%s/%s", chrootdir, keyfile);
|
||||||
fprintf(fd, "key \"%s\" {\n\talgorithm hmac-md5;\n"
|
|
||||||
"\tsecret \"%.*s\";\n};\n", keyname,
|
write_key_file(buf, user, keyname, &key_txtbuffer);
|
||||||
(int)isc_buffer_usedlength(&key_txtbuffer),
|
|
||||||
(char *)isc_buffer_base(&key_txtbuffer));
|
|
||||||
fflush(fd);
|
|
||||||
if (ferror(fd)) {
|
|
||||||
fprintf(stderr, "write to %s failed\n", buf);
|
|
||||||
fclose(fd);
|
|
||||||
isc_mem_put(mctx, buf, len);
|
isc_mem_put(mctx, buf, len);
|
||||||
goto cleanup;
|
|
||||||
}
|
}
|
||||||
if (fclose(fd)) {
|
|
||||||
fprintf(stderr, "fclose(%s) failed\n", buf);
|
|
||||||
isc_mem_put(mctx, buf, len);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
isc_mem_put(mctx, buf, len);
|
|
||||||
} else {
|
} else {
|
||||||
printf("\
|
printf("\
|
||||||
# Start of rndc.conf\n\
|
# Start of rndc.conf\n\
|
||||||
|
Reference in New Issue
Block a user