summaryrefslogtreecommitdiffstats
path: root/contrib/bind/bin/named/ns_ctl.c
blob: 1950b07fc91773b584b6db788106e9f1b333c05a (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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
#if !defined(lint) && !defined(SABER)
static const char rcsid[] = "$Id: ns_ctl.c,v 8.39 2000/12/19 23:31:38 marka Exp $";
#endif /* not lint */

/*
 * Copyright (c) 1997-2000 by 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.
 */

/* Extern. */

#include "port_before.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>

#include <isc/eventlib.h>
#include <isc/logging.h>
#include <isc/memcluster.h>

#include "port_after.h"

#include "named.h"

/* Defs. */

#define CONTROL_FOUND	0x0001	/* for mark and sweep. */
#define	MAX_STR_LEN	500

struct control {
	LINK(struct control) link;
	enum { t_dead, t_inet, t_unix } type;
	struct ctl_sctx *sctx;
	u_int flags;
	union {
		struct {
			struct sockaddr_in in;
			ip_match_list allow;
		} v_inet;
#ifndef NO_SOCKADDR_UN
		struct {
			struct sockaddr_un un;
			mode_t mode;
			uid_t owner;
			gid_t group;
		} v_unix;
#endif
	} var;
};

/* Forward. */

static struct ctl_sctx *mksrvr(control, const struct sockaddr *, size_t);
static control		new_control(void);
static void		free_control(controls *, control);
static void		free_controls(controls *);
static int		match_control(control, control);
static control		find_control(controls, control);
static void		propagate_changes(const control, control);
static void		install(control);
static void		install_inet(control);
static void		install_unix(control);
static void		logger(enum ctl_severity, const char *fmt, ...);
static void		verb_connect(struct ctl_sctx *, struct ctl_sess *,
				     const struct ctl_verb *,
				     const char *, u_int, void *, void *);
static void		verb_getpid(struct ctl_sctx *, struct ctl_sess *,
				    const struct ctl_verb *,
				    const char *, u_int, void *, void *);
static void		getpid_closure(struct ctl_sctx *, struct ctl_sess *,
				       void *);
static void		verb_status(struct ctl_sctx *, struct ctl_sess *,
				    const struct ctl_verb *,
				    const char *, u_int, void *, void *);
static void		status_closure(struct ctl_sctx *, struct ctl_sess *,
				       void *);
static void		verb_stop(struct ctl_sctx *, struct ctl_sess *,
				  const struct ctl_verb *,
				  const char *, u_int, void *, void *);
static void		verb_exec(struct ctl_sctx *, struct ctl_sess *,
				  const struct ctl_verb *,
				  const char *, u_int, void *, void *);
static void		verb_reload(struct ctl_sctx *, struct ctl_sess *,
				    const struct ctl_verb *,
				    const char *, u_int, void *, void *);
static void		verb_reconfig(struct ctl_sctx *, struct ctl_sess *,
				      const struct ctl_verb *,
				      const char *, u_int, void *, void *);
static void		verb_dumpdb(struct ctl_sctx *, struct ctl_sess *,
				    const struct ctl_verb *,
				    const char *, u_int, void *, void *);
static void		verb_stats(struct ctl_sctx *, struct ctl_sess *,
				   const struct ctl_verb *,
				   const char *, u_int, void *, void *);
static void		verb_trace(struct ctl_sctx *, struct ctl_sess *,
				   const struct ctl_verb *,
				   const char *, u_int, void *, void *);
static void		trace_closure(struct ctl_sctx *, struct ctl_sess *,
				       void *);
static void		verb_notrace(struct ctl_sctx *, struct ctl_sess *,
				     const struct ctl_verb *,
				     const char *, u_int, void *, void *);
static void		verb_querylog(struct ctl_sctx *, struct ctl_sess *,
				      const struct ctl_verb *,
				      const char *, u_int, void *, void *);
static void		verb_help(struct ctl_sctx *, struct ctl_sess *,
				  const struct ctl_verb *,
				  const char *, u_int, void *, void *);
static void		verb_quit(struct ctl_sctx *, struct ctl_sess *,
				  const struct ctl_verb *,
				  const char *, u_int, void *, void *);

/* Private data. */

static controls server_controls;

static struct ctl_verb verbs[] = {
	{ "",		verb_connect,	""},
	{ "getpid",	verb_getpid,	"getpid"},
	{ "status",	verb_status,	"status"},
	{ "stop",	verb_stop,	"stop"},
	{ "exec",	verb_exec,	"exec"},
	{ "reload",	verb_reload,	"reload [zone] ..."},
	{ "reconfig",	verb_reconfig,	"reconfig [-noexpired] (just sees new/gone zones)"},
	{ "dumpdb",	verb_dumpdb,	"dumpdb"},
	{ "stats",	verb_stats,	"stats [clear]"},
	{ "trace",	verb_trace,	"trace [level]"},
	{ "notrace",	verb_notrace,	"notrace"},
	{ "querylog",	verb_querylog,	"querylog"},
	{ "qrylog",	verb_querylog,	"qrylog"},
	{ "help",	verb_help,	"help"},
	{ "quit",	verb_quit,	"quit"},
	{ NULL,		NULL,		NULL}
};

/* Public functions. */

void
ns_ctl_initialize(void) {
	INIT_LIST(server_controls);
}

void
ns_ctl_shutdown(void) {
	if (!EMPTY(server_controls))
		free_controls(&server_controls);
}

void
ns_ctl_defaults(controls *list) {
#ifdef NO_SOCKADDR_UN
	struct in_addr saddr;
	ip_match_list iml;
	ip_match_element ime;

	/*
	 * If the operating system does not support local domain sockets, 
	 * connect with ndc on 127.0.0.1, port 101, and only allow
	 * connections from 127.0.0.1.
	 */
	saddr.s_addr = htonl (INADDR_LOOPBACK);
	iml = new_ip_match_list();
	ime = new_ip_match_pattern(saddr, 32);
	add_to_ip_match_list(iml, ime);

	ns_ctl_add(list, ns_ctl_new_inet(saddr, htons (101), iml));
#else
#ifdef NEED_SECURE_DIRECTORY
	ns_ctl_add(list, ns_ctl_new_unix(_PATH_NDCSOCK, 0700, 0, 0));
#else
	ns_ctl_add(list, ns_ctl_new_unix(_PATH_NDCSOCK, 0600, 0, 0));
#endif
#endif /*NO_SOCKADDR_UN*/
}

void
ns_ctl_add(controls *list, control new) {
	if (!find_control(*list, new))
		APPEND(*list, new, link);
}

control
ns_ctl_new_inet(struct in_addr saddr, u_int sport, ip_match_list allow) {
	control new = new_control();

	INIT_LINK(new, link);
	new->type = t_inet;
	memset(&new->var.v_inet.in, 0, sizeof new->var.v_inet.in);
	new->var.v_inet.in.sin_family = AF_INET;
	new->var.v_inet.in.sin_addr = saddr;
	new->var.v_inet.in.sin_port = sport;
	new->var.v_inet.allow = allow;
	return (new);
}

#ifndef NO_SOCKADDR_UN
control
ns_ctl_new_unix(char *path, mode_t mode, uid_t owner, gid_t group) {
	control new = new_control();

	INIT_LINK(new, link);
	new->type = t_unix;
	memset(&new->var.v_unix.un, 0, sizeof new->var.v_unix.un);
	new->var.v_unix.un.sun_family = AF_UNIX;
	strncpy(new->var.v_unix.un.sun_path, path,
		sizeof new->var.v_unix.un.sun_path - 1);
	new->var.v_unix.mode = mode;
	new->var.v_unix.owner = owner;
	new->var.v_unix.group = group;
	return (new);
}
#endif

void
ns_ctl_install(controls *new) {
	control ctl, old, next;

	/* Find all the controls which aren't new or deleted. */
	for (ctl = HEAD(server_controls); ctl != NULL; ctl = NEXT(ctl, link))
		ctl->flags &= ~CONTROL_FOUND;
	for (ctl = HEAD(*new); ctl != NULL; ctl = next) {
		next = NEXT(ctl, link);
		old = find_control(server_controls, ctl);
		if (old != NULL) {
			old->flags |= CONTROL_FOUND;
			propagate_changes(ctl, old);
			if (old->sctx == NULL)
				free_control(&server_controls, old);
			free_control(new, ctl);
		}
	}

	/* Destroy any old controls which weren't found. */
	for (ctl = HEAD(server_controls); ctl != NULL; ctl = next) {
		next = NEXT(ctl, link);
		if ((ctl->flags & CONTROL_FOUND) == 0)
			free_control(&server_controls, ctl);
	}

	/* Add any new controls which were found. */
	for (ctl = HEAD(*new); ctl != NULL; ctl = next) {
		next = NEXT(ctl, link);
		UNLINK(*new, ctl, link);
		APPEND(server_controls, ctl, link);
		install(ctl);
		if (ctl->sctx == NULL)
			free_control(&server_controls, ctl);
	}
}

/* Private functions. */

static struct ctl_sctx *
mksrvr(control ctl, const struct sockaddr *sa, size_t salen) {
	return (ctl_server(ev, sa, salen, verbs, 500, 222,
			   600, 5, 10, logger, ctl));
}

static control
new_control(void) {
	control new = memget(sizeof *new);

	if (new == NULL)
		panic("memget failed in new_control()", NULL);
	new->type = t_dead;
	new->sctx = NULL;
	return (new);
}

static void
free_control(controls *list, control this) {
	int was_live = 0;
	struct stat sb;

	if (this->sctx != NULL) {
		ctl_endserver(this->sctx);
		this->sctx = NULL;
		was_live = 1;
	}
	switch (this->type) {
	case t_inet:
		if (this->var.v_inet.allow != NULL) {
			free_ip_match_list(this->var.v_inet.allow);
			this->var.v_inet.allow = NULL;
		}
		break;
#ifndef NO_SOCKADDR_UN
	case t_unix:
		/* XXX Race condition. */
		if (was_live &&
		    stat(this->var.v_unix.un.sun_path, &sb) == 0 &&
		    (S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
			/* XXX Race condition. */
			unlink(this->var.v_unix.un.sun_path);
		}
		break;
#endif
	default:
		panic("impossible type in free_control", NULL);
		/* NOTREACHED */
	}
	UNLINK(*list, this, link);
	memput(this, sizeof *this);
}

static void
free_controls(controls *list) {
	control ctl, next;

	for (ctl = HEAD(*list); ctl != NULL; ctl = next) {
		next = NEXT(ctl, link);
		free_control(list, ctl);
	}
	INIT_LIST(*list);
}

static int
match_control(control l, control r) {
	int match = 1;

	if (l->type != r->type)
		match = 0;
	else
		switch (l->type) {
		case t_inet:
			if (l->var.v_inet.in.sin_family !=
			    r->var.v_inet.in.sin_family ||
			    l->var.v_inet.in.sin_port !=
			    r->var.v_inet.in.sin_port ||
			    l->var.v_inet.in.sin_addr.s_addr !=
			    r->var.v_inet.in.sin_addr.s_addr)
				match = 0;
			break;
#ifndef NO_SOCKADDR_UN
		case t_unix:
			if (l->var.v_unix.un.sun_family !=
			    r->var.v_unix.un.sun_family ||
			    strcmp(l->var.v_unix.un.sun_path,
				   r->var.v_unix.un.sun_path) != 0)
				match = 0;
			break;
#endif
		default:
			panic("impossible type in match_control", NULL);
			/* NOTREACHED */
		}
	ns_debug(ns_log_config, 20, "match_control(): %d", match);
	return (match);
}

static control
find_control(controls list, control new) {
	control ctl;

	for (ctl = HEAD(list); ctl != NULL; ctl = NEXT(ctl, link))
		if (match_control(ctl, new))
			return (ctl);
	return (NULL);
}

static void
propagate_changes(const control diff, control base) {
	int need_install = 0;

	switch (base->type) {
	case t_inet:
		if (base->var.v_inet.allow != NULL)
			free_ip_match_list(base->var.v_inet.allow);
		base->var.v_inet.allow = diff->var.v_inet.allow;
		diff->var.v_inet.allow = NULL;
		need_install++;
		break;
#ifndef NO_SOCKADDR_UN
	case t_unix:
		if (base->var.v_unix.mode != diff->var.v_unix.mode) {
			base->var.v_unix.mode = diff->var.v_unix.mode;
			need_install++;
		}
		if (base->var.v_unix.owner != diff->var.v_unix.owner) {
			base->var.v_unix.owner = diff->var.v_unix.owner;
			need_install++;
		}
		if (base->var.v_unix.group != diff->var.v_unix.group) {
			base->var.v_unix.group = diff->var.v_unix.group;
			need_install++;
		}
		break;
#endif
	default:
		panic("impossible type in ns_ctl::propagate_changes", NULL);
		/* NOTREACHED */
	}
	if (need_install)
		install(base);
}

static void
install(control ctl) {
	switch (ctl->type) {
	case t_inet:
		install_inet(ctl);
		break;
#ifndef NO_SOCKADDR_UN
	case t_unix:
		install_unix(ctl);
		break;
#endif
	default:
		panic("impossible type in ns_ctl::install", NULL);
		/* NOTREACHED */
	}
}

static void
install_inet(control ctl) {
	if (ctl->sctx == NULL) {
		ctl->sctx = mksrvr(ctl,
				   (struct sockaddr *)&ctl->var.v_inet.in,
				   sizeof ctl->var.v_inet.in);
	}
}

#ifndef NO_SOCKADDR_UN
/*
 * Unattach an old unix domain socket if it exists.
 */
static void
unattach(control ctl) {
	int s;
	struct stat sb;

	s = socket(AF_UNIX, SOCK_STREAM, 0);
	if (s < 0) {
		ns_warning(ns_log_config,
			   "unix control \"%s\" socket failed: %s",
			   ctl->var.v_unix.un.sun_path,
			   strerror(errno));
		return;
	}

	if (stat(ctl->var.v_unix.un.sun_path, &sb) < 0) {
		switch (errno) {
		case ENOENT:	/* We exited cleanly last time */
			break;
		default:
			ns_warning(ns_log_config,
				   "unix control \"%s\" stat failed: %s",
				   ctl->var.v_unix.un.sun_path,
				   strerror(errno));
			break;
		}
		goto cleanup;
	}

	if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
		ns_warning(ns_log_config, "unix control \"%s\" not socket",
			   ctl->var.v_unix.un.sun_path);
		goto cleanup;
	}

	if (connect(s, (struct sockaddr *)&ctl->var.v_unix.un,
		    sizeof ctl->var.v_unix.un) < 0) {
		switch (errno) {
		case ECONNREFUSED:
		case ECONNRESET:
			if (unlink(ctl->var.v_unix.un.sun_path) < 0)
				ns_warning(ns_log_config,
			      "unix control \"%s\" unlink failed: %s",
					   ctl->var.v_unix.un.sun_path,
					   strerror(errno));
			break;
		default:
			ns_warning(ns_log_config,
				   "unix control \"%s\" connect failed: %s",
				   ctl->var.v_unix.un.sun_path,
				   strerror(errno));
			break;
		}
	}
  cleanup:
	close(s);
}

static void
install_unix(control ctl) {
	char *path;
#ifdef NEED_SECURE_DIRECTORY
	char *slash;

	path = savestr(ctl->var.v_unix.un.sun_path, 1);

	slash = strrchr(path, '/');
	if (slash != NULL) {
		if (slash != path)
			*slash = '\0';
		else {
			freestr(path);
			path = savestr("/", 1);
		}
	} else {
		freestr(path);
		path = savestr(".", 1);
	}
	if (mkdir(path, ctl->var.v_unix.mode) < 0) {
		if (errno != EEXIST) {
			ns_warning(ns_log_config,
				   "unix control \"%s\" mkdir failed: %s",
				   path, strerror(errno));
		}
	}
#else
	path = ctl->var.v_unix.un.sun_path;
#endif

	if (ctl->sctx == NULL) {
		unattach(ctl);
		ctl->sctx = mksrvr(ctl,
				   (struct sockaddr *)&ctl->var.v_unix.un,
				   sizeof ctl->var.v_unix.un);
	}
	if (ctl->sctx != NULL) {
		/* XXX Race condition. */
		if (chmod(path, ctl->var.v_unix.mode) < 0) {
			ns_warning(ns_log_config, "chmod(\"%s\", 0%03o): %s",
				   ctl->var.v_unix.un.sun_path,
				   ctl->var.v_unix.mode,
				   strerror(errno));
		}
		if (chown(path, ctl->var.v_unix.owner,
			  ctl->var.v_unix.group) < 0) {
			ns_warning(ns_log_config, "chown(\"%s\", %d, %d): %s",
				   ctl->var.v_unix.un.sun_path,
				   ctl->var.v_unix.owner,
				   ctl->var.v_unix.group,
				   strerror(errno));
		}
	}
#ifdef NEED_SECURE_DIRECTORY
	freestr(path);
#endif
}
#endif

static void
logger(enum ctl_severity ctlsev, const char *format, ...) {
	va_list args;
	int logsev;

	switch (ctlsev) {
	case ctl_debug:		logsev = log_debug(5);	break;
	case ctl_warning:	logsev = log_warning;	break;
	case ctl_error:		logsev = log_error;	break;
	default:		panic("invalid ctlsev in logger", NULL);
	}
	if (!log_ctx_valid)
		return;
	va_start(args, format);
	log_vwrite(log_ctx, ns_log_control, logsev, format, args);
	va_end(args);
}

static void
verb_connect(struct ctl_sctx *ctl, struct ctl_sess *sess,
	     const struct ctl_verb *verb, const char *rest,
	     u_int respflags, void *respctx, void *uctx)
{
	const struct sockaddr *sa = (struct sockaddr *)respctx;
	control nsctl = (control)uctx;

	if (sa->sa_family == AF_INET) {
		const struct sockaddr_in *in = (struct sockaddr_in *)sa;
		const ip_match_list acl = nsctl->var.v_inet.allow;

		if (!ip_address_allowed(acl, in->sin_addr)) {
			ctl_response(sess, 502, "Permission denied.",
				     CTL_EXIT, NULL, NULL, NULL, NULL, 0);
			return;
		}
	}
	ctl_response(sess, 220, server_options->version, 0, NULL, NULL, NULL,
		     NULL, 0);
}

static void
verb_getpid(struct ctl_sctx *ctl, struct ctl_sess *sess,
	    const struct ctl_verb *verb, const char *rest,
	    u_int respflags, void *respctx, void *uctx)
{
	char *msg = memget(MAX_STR_LEN);

	if (msg == NULL) {
		ctl_response(sess, 503, "(out of memory)", 0,
			     NULL, NULL, NULL, NULL, 0);
		return;
	}
	sprintf(msg, "my pid is <%ld>", (long)getpid());
	ctl_response(sess, 250, msg, 0, NULL, getpid_closure, msg, NULL, 0);
}

static void
getpid_closure(struct ctl_sctx *sctx, struct ctl_sess *sess, void *uap) {
	char *msg = uap;

	memput(msg, MAX_STR_LEN);
}

enum state {
	e_version = 0,
	e_config,
	e_nzones,
	e_debug,
	e_xfersrun,
	e_xfersdfr,
	e_qserials,
	e_qrylog,
	e_priming,
	e_finito
};

struct pvt_status {
	enum state	state;
	char		text[MAX_STR_LEN];
};

static void
verb_status(struct ctl_sctx *ctl, struct ctl_sess *sess,
	    const struct ctl_verb *verb, const char *rest,
	    u_int respflags, void *respctx, void *uctx)
{
	struct pvt_status *pvt = ctl_getcsctx(sess);

	if (pvt == NULL) {
		pvt = memget(sizeof *pvt);
		if (pvt == NULL) {
			ctl_response(sess, 505, "(out of memory)",
				     0, NULL, NULL, NULL, NULL, 0);
			return;
		}
		pvt->state = (enum state)0;
		(void)ctl_setcsctx(sess, pvt);
	}
	switch (pvt->state++) {
	case e_version:
		strncpy(pvt->text, Version, sizeof pvt->text);
		pvt->text[sizeof pvt->text - 1] = '\0';
		break;
	case e_config:
		sprintf(pvt->text, "config (%s) last loaded at age: %24s",
			conffile, ctime(&confmtime));
		break;
	case e_nzones:
		sprintf(pvt->text, "number of zones allocated: %d", nzones);
		break;
	case e_debug:
		sprintf(pvt->text, "debug level: %d", debug);
		break;
	case e_xfersrun:
		sprintf(pvt->text, "xfers running: %d", xfers_running);
		break;
	case e_xfersdfr:
		sprintf(pvt->text, "xfers deferred: %d", xfers_deferred);
		break;
	case e_qserials:
		sprintf(pvt->text, "soa queries in progress: %d",
			qserials_running);
		break;
	case e_qrylog:
		sprintf(pvt->text, "query logging is %s",
			qrylog ? "ON" : "OFF");
		break;
	case e_priming:
		if (priming)
			sprintf(pvt->text, "server is initialising itself");
		else
			sprintf(pvt->text, "server is up and running");
		break;
	case e_finito:
		return;
	}
	ctl_response(sess, 250, pvt->text,
		     (pvt->state == e_finito) ? 0 : CTL_MORE,
		     NULL, status_closure, NULL, NULL, 0);
}

static void
status_closure(struct ctl_sctx *sctx, struct ctl_sess *sess, void *uap) {
	struct pvt_status *pvt = ctl_getcsctx(sess);

	memput(pvt, sizeof *pvt);
	ctl_setcsctx(sess, NULL);
}

static void
verb_stop(struct ctl_sctx *ctl, struct ctl_sess *sess,
	  const struct ctl_verb *verb, const char *rest,
	  u_int respflags, void *respctx, void *uctx)
{
	ns_need(main_need_exit);
	ctl_response(sess, 250, "Shutdown initiated.", 0, NULL, NULL, NULL,
		     NULL, 0);
}

static void
verb_exec(struct ctl_sctx *ctl, struct ctl_sess *sess,
	  const struct ctl_verb *verb, const char *rest,
	  u_int respflags, void *respctx, void *uctx)
{
	struct stat sb;

	if (rest != NULL && *rest != '\0') {
		if (stat(rest, &sb) < 0) {
			ctl_response(sess, 503, strerror(errno),
				     0, NULL, NULL, NULL, NULL, 0);
			return;
		}
		saved_argv[0] = savestr(rest, 1);	/* Never strfreed. */
	}

	if (stat(saved_argv[0], &sb) < 0) {
		const char *save = strerror(errno);

		ns_warning(ns_log_default, "can't exec, %s: %s",
			   saved_argv[0], save);
		ctl_response(sess, 502, save, 0, NULL, NULL, NULL,
			     NULL, 0);
	} else {
		ns_need(main_need_restart);
		ctl_response(sess, 250, "Restart initiated.", 0, NULL,
			     NULL, NULL, NULL, 0);
	}
}

static void
verb_reload(struct ctl_sctx *ctl, struct ctl_sess *sess,
	    const struct ctl_verb *verb, const char *rest,
	    u_int respflags, void *respctx, void *uctx)
{
	static const char spaces[] = " \t";
	struct zoneinfo *zp;
	char *tmp = NULL, *x;
	const char *msg;
	int class, code, success;

	/* If there are no args, this is a classic reload of the config. */
	if (rest == NULL || *rest == '\0') {
		ns_need(main_need_reload);
		code = 250;
		msg = "Reload initiated.";
		goto respond;
	}

	/* Look for optional zclass argument.  Default is "in". */
	tmp = savestr(rest, 1);
	x = tmp + strcspn(tmp, spaces);
	if (*x != '\0') {
		*x++ = '\0';
		x += strspn(x, spaces);
	}
	if (x == NULL || *x == '\0')
		x = "in";
	class = sym_ston(__p_class_syms, x, &success);
	if (!success) {
		code = 507;
		msg = "unrecognized class";
		goto respond;
	}

	/* Look for the zone, and do the right thing to it. */
	zp = find_zone(tmp, class);
	if (zp == NULL) {
		code = 506;
		msg = "Zone not found.";
		goto respond;
	}
	switch (zp->z_type) {
	case z_master:
		ns_stopxfrs(zp);
		/*FALLTHROUGH*/
	case z_hint:
		block_signals();
		code = 251;
		msg = deferred_reload_unsafe(zp);
		unblock_signals();
		break;
	case z_slave:
	case z_stub:
		ns_stopxfrs(zp);
		if (zonefile_changed_p(zp))
			zp->z_serial = 0;	/* force xfer */
		addxfer(zp);
		code = 251;
		msg = "Slave transfer queued.";
		goto respond;
	case z_forward:
	case z_cache:
	default:
		msg = "Non reloadable zone.";
		code = 507;
		break;
	}

 respond:
	ctl_response(sess, code, msg, 0, NULL, NULL, NULL, NULL, 0);
	if (tmp != NULL)
		freestr(tmp);
}

static void
verb_reconfig(struct ctl_sctx *ctl, struct ctl_sess *sess,
	      const struct ctl_verb *verb, const char *rest,
	      u_int respflags, void *respctx, void *uctx)
{
	if (strcmp(rest, "-noexpired") != 0)
		ns_need(main_need_reconfig);
	else
		ns_need(main_need_noexpired);
	ctl_response(sess, 250, "Reconfig initiated.",
		     0, NULL, NULL, NULL, NULL, 0);
}

static void
verb_dumpdb(struct ctl_sctx *ctl, struct ctl_sess *sess,
	    const struct ctl_verb *verb, const char *rest,
	    u_int respflags, void *respctx, void *uctx)
{
	ns_need(main_need_dump);
	ctl_response(sess, 250, "Database dump initiated.", 0, NULL,
		     NULL, NULL, NULL, 0);
}

static void
verb_stats(struct ctl_sctx *ctl, struct ctl_sess *sess,
	   const struct ctl_verb *verb, const char *rest,
	   u_int respflags, void *respctx, void *uctx)
{
	if (rest != NULL && strcmp(rest, "clear") == 0) {
		ns_need(main_need_statsdumpandclear);
		ctl_response(sess, 250, "Statistics dump and clear initiated.",
			     0, NULL, NULL, NULL, NULL, 0);
	} else {
		ns_need(main_need_statsdump);
		ctl_response(sess, 250, "Statistics dump initiated.",
			     0, NULL, NULL, NULL, NULL, 0);
	}
}

static void
verb_trace(struct ctl_sctx *ctl, struct ctl_sess *sess,
	   const struct ctl_verb *verb, const char *rest,
	   u_int respflags, void *respctx, void *uctx)
{
	int i = atoi(rest);
	char *msg = memget(MAX_STR_LEN);

	if (msg == NULL) {
		ctl_response(sess, 503, "(out of memory)", 0,
			     NULL, NULL, NULL, NULL, 0);
		return;
	}
	if (i > 0) 
		desired_debug = i;
	else
		desired_debug++;
	ns_need(main_need_debug);
	sprintf(msg, "Debug level: %d", desired_debug);
	ctl_response(sess, 250, msg, 0, NULL, trace_closure, msg, NULL, 0);
}

static void
trace_closure(struct ctl_sctx *sctx, struct ctl_sess *sess, void *uap) {
	char *msg = uap;

	memput(msg, MAX_STR_LEN);
}

static void
verb_notrace(struct ctl_sctx *ctl, struct ctl_sess *sess,
	     const struct ctl_verb *verb, const char *rest,
	     u_int respflags, void *respctx, void *uctx)
{
	desired_debug = 0;
	ns_need(main_need_debug);
	ctl_response(sess, 250, "Debugging turned off.",
		     0, NULL, NULL, NULL, NULL, 0);
}

static void
verb_querylog(struct ctl_sctx *ctl, struct ctl_sess *sess,
	      const struct ctl_verb *verb, const char *rest,
	      u_int respflags, void *respctx, void *uctx)
{
	static const char	on[] = "Query logging is now on.",
				off[] = "Query logging is now off.";

	toggle_qrylog();
	ctl_response(sess, 250, qrylog ? on : off,
		     0, NULL, NULL, NULL, NULL, 0);
}

static void
verb_help(struct ctl_sctx *ctl, struct ctl_sess *sess,
	  const struct ctl_verb *verb, const char *rest,
	  u_int respflags, void *respctx, void *uctx)
{
	ctl_sendhelp(sess, 214);
}

static void
verb_quit(struct ctl_sctx *ctl, struct ctl_sess *sess,
	  const struct ctl_verb *verb, const char *rest,
	  u_int respflags, void *respctx, void *uctx)
{
	ctl_response(sess, 221, "End of control session.", CTL_EXIT, NULL,
		     NULL, NULL, NULL, 0);
}
OpenPOWER on IntegriCloud