summaryrefslogtreecommitdiff
path: root/server/bdump.c
blob: 9781dd1a83def75eee0b79409ce743d1e6ae9e5c (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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains functions for dumping server state between servers.
 *
 *	Created by:	John T. Kohl
 *
 *	$Source$
 *	$Author$
 *
 *	Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include <zephyr/mit-copyright.h>

#ifndef lint
#ifndef SABER
static char rcsid_bdump_s_c[] = "$Header$";
#endif SABER
#endif lint

#include "zserver.h"
#include <sys/socket.h>
#ifdef lint
/* so it shuts up about struct iovec */
#include <sys/uio.h>
#endif lint
#include <signal.h>

/*
 * External functions are:
 *
 * void bdump_offer(who)
 *	strut sockaddr_in *who;
 *
 * void bdump_send()
 *
 * void bdump_get(notice, auth, who, server)
 *	ZNotice_t *notice;
 *	int auth;
 *	struct sockaddr_in *who;
 *	ZServerDesc_t *server;
 *
 * Code_t bdump_send_list_tcp(kind, port, class, inst, opcode,
 *			    sender, recip, lyst, num)
 *	ZNotice_Kind_t kind;
 *	u_short port;
 *	char *class, *inst, *opcode, *sender, *recip;
 *	char *lyst[];
 *	int num;
 */

static void close_bdump(), cleanup();
static Code_t bdump_send_loop(), bdump_ask_for(), bdump_recv_loop();
static Code_t get_packet(), extract_sin(), send_done(), send_list();
static Code_t send_host_register(), sbd_loop(), gbd_loop(), send_normal_tcp();
static int get_tgt();

static timer bdump_timer;
static long ticket_time = 0L;
static char my_realm[REALM_SZ] = "\0";
static int bdump_inited = 0;

int bdumping = 0;

/*
 * Functions for performing a brain dump between servers.
 */

/*
 * offer the brain dump to another server
 */

void
bdump_offer(who)
struct sockaddr_in *who;
{
	Code_t retval;
	char buf[512], *addr, *lyst[2];

#ifdef KERBEROS
	/* 
	 * when using Kerberos server-server authentication, we can
	 * use any random local address 
	 */
	if ((bdump_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0){
		syslog(LOG_ERR,"bdump socket: %m");
		bdump_socket = 0;
		return;
	}
	bzero((caddr_t) &bdump_sin, sizeof(bdump_sin));
	bdump_sin.sin_port = htons((u_short)((getpid()*8)&0xfff)+(((int)random()>>4)&0xf)+1024);
	bdump_sin.sin_addr = my_addr;
	bdump_sin.sin_family = AF_INET;
	do {
		if ((retval = bind(bdump_socket, (struct sockaddr *) &bdump_sin, sizeof(bdump_sin))) < 0) {
			if (errno == EADDRINUSE)
				bdump_sin.sin_port = htons(ntohs(bdump_sin.sin_port) + 1);
			else {
				syslog(LOG_ERR, "bdump bind %d: %m", htons(bdump_sin.sin_port));
				(void) close(bdump_socket);
				bdump_socket = 0;
				return;
			}
		}
	} while (retval < 0);
#else
	int bdump_port = IPPORT_RESERVED - 1;
	/*
	 * when not using Kerberos, we can't use any old port, we use
	 * Internet reserved ports instead (rresvport)
	 */
	if ((bdump_socket = rresvport(&bdump_port)) < 0) {
		syslog(LOG_ERR,"bdump socket: %m");
		bdump_socket = 0;
		return;
	}
	bzero((caddr_t) &bdump_sin, sizeof(bdump_sin));
	bdump_sin.sin_port = htons((unsigned short)bdump_port);
	bdump_sin.sin_addr = my_addr;
	bdump_sin.sin_family = AF_INET;

#endif KERBEROS
	(void) listen(bdump_socket, 1);

	bdump_timer = timer_set_rel(20L, close_bdump, (caddr_t) 0);
	FD_SET(bdump_socket, &interesting);
	nfildes = max(bdump_socket, srv_socket) + 1;


	addr = inet_ntoa(bdump_sin.sin_addr);
	(void) sprintf(buf, "%d", ntohs(bdump_sin.sin_port));
	lyst[0] = addr;
	lyst[1] = buf;

	if ((retval = ZSetDestAddr(who)) != ZERR_NONE) {
		syslog(LOG_WARNING, "obd set addr: %s",
		       error_message(retval));
		return;
	}

	/* myname is the hostname */
	/* the class instance is the version number, here it is "1" */
	(void) send_list(ACKED, sock_sin.sin_port, ZEPHYR_ADMIN_CLASS, "1",
		  ADMIN_BDUMP, myname, "", lyst, 2);
	
	return;
}

/*
 * Accept a connection, and send the brain dump to the other server
 */

void
bdump_send()
{
	int sock;
	struct sockaddr_in from;
	ZServerDesc_t *server;
	Code_t retval;
	int fromlen = sizeof(from);
#ifdef KERBEROS
	KTEXT_ST ticket;
	AUTH_DAT kdata;
#else
	unsigned short fromport;
#endif KERBEROS

	zdbug((LOG_DEBUG, "bdump_send"));
	/* accept the connection, and send the brain dump */
	if ((sock = accept(bdump_socket, (struct sockaddr *)&from,
			   &fromlen)) < 0) {
		syslog(LOG_ERR,"accept: %m");
		return;
	}

#ifndef KERBEROS
	fromport = ntohs(from.sin_port);
#endif KERBEROS

	(void) signal(SIGPIPE, SIG_IGN); /* so we can detect failures */

	from.sin_port = sock_sin.sin_port; /* we don't care what port
					    it came from, and we need to
					    fake out server_which_server() */
	server = server_which_server(&from);
	if (!server) {
		syslog(LOG_ERR,"unknown server?");
		server = limbo_server;
	}
	zdbug((LOG_DEBUG, "sbd connected"));

	bdumping = 1;
	if (bdump_socket) {
		/* shut down the listening socket and the timer */
		FD_CLR(bdump_socket, &interesting);
		(void) close(bdump_socket);
		nfildes = srv_socket + 1;
		bdump_socket = 0;
		timer_reset(bdump_timer);
	}

	/* Now begin the brain dump.  Set the Zephyr port to be the
	   TCP connection.  This will work since recvfrom and sendto
	   work even on TCP connections, but the sockaddr_in addresses
	   are ignored */
	(void) ZSetFD(sock);
	/* receive the authenticator */
#ifdef KERBEROS

	if ((retval = GetKerberosData(sock, from.sin_addr, &kdata, "zephyr",
				      ZEPHYR_SRVTAB)) != KSUCCESS) {
		syslog(LOG_ERR, "sbd getkdata: %s",krb_err_txt[retval]);
		cleanup(server, sock);
		return;
	}
	if (strcmp(kdata.pname,"zephyr") || strcmp(kdata.pinst,"zephyr")) {
		syslog(LOG_ERR, "sbd peer not zephyr: %s.%s@%s",
		       kdata.pname, kdata.pinst,kdata.prealm);
		cleanup(server, sock);
		return;
	}
	/* authenticate back */
	if (get_tgt()) {
		cleanup(server, sock);
		return;
	}
	if ((retval = SendKerberosData(sock, &ticket, "zephyr", "zephyr"))
	    != KSUCCESS) {
		syslog(LOG_ERR,"bdump_send: %s",krb_err_txt[retval]);
		cleanup(server, sock);
		return;
	}
#else
	if ((fromport > IPPORT_RESERVED) ||
	    (fromport < (IPPORT_RESERVED / 2))) {
		syslog(LOG_ERR,"bad port from peer: %d",fromport);
		cleanup(server, sock);
		return;
	}
#endif KERBEROS

	if ((retval = sbd_loop(&from)) != ZERR_NONE) {
		syslog(LOG_WARNING, "sbd_loop failed: %s",
		       error_message(retval));
		cleanup(server, sock);
		return;
	} else {
		if ((retval = gbd_loop(server)) != ZERR_NONE) {
			syslog(LOG_WARNING, "gbd_loop failed: %s",
			       error_message(retval));
			cleanup(server, sock);
			return;
		} else {
			zdbug((LOG_DEBUG, "sbd finished"));
			if (server != limbo_server) {
				/* set this guy to be up,
				   and schedule a hello */
				server->zs_state = SERV_UP;
				timer_reset(server->zs_timer);
				server->zs_timer = timer_set_rel(0L, server_timo, (caddr_t) server);
			}
		}
	}
	zdbug((LOG_DEBUG,"cleanup sbd"));
	(void) ZSetFD(srv_socket);
	(void) close(sock);
	(void) signal(SIGPIPE, SIG_DFL);
	bdump_inited = 1;
	bdumping = 0;
	return;
}

/*ARGSUSED*/
void
bdump_get(notice, auth, who, server)
ZNotice_t *notice;
int auth;
struct sockaddr_in *who;
ZServerDesc_t *server;
{
	struct sockaddr_in from;
	int sock = -1;
	Code_t retval;
#ifdef KERBEROS
	KTEXT_ST ticket;
	AUTH_DAT kdata;
#else
	int reserved_port = IPPORT_RESERVED - 1;
#endif KERBEROS

	zdbug((LOG_DEBUG, "bdump avail"));

	/* version number 1 is the same as no version number */
	if (strcmp(notice->z_class_inst, "1")
	     && strcmp(notice->z_class_inst, "")) {
		syslog(LOG_WARNING,
		       "Incompatible bdump version '%s' from %s",
		       notice->z_class_inst,
		       inet_ntoa(who->sin_addr));
		return;
	}
	bdumping = 1;
	(void) signal(SIGPIPE, SIG_IGN); /* so we can detect problems */

	if (bdump_socket) {
		/* We cannot go get a brain dump when someone may
		   potentially be connecting to us (if that other
		   server is the server to whom we are connecting,
		   we will deadlock. so we shut down the listening
		   socket and the timer */
		FD_CLR(bdump_socket, &interesting);
		(void) close(bdump_socket);
		nfildes = srv_socket + 1;
		bdump_socket = 0;
		timer_reset(bdump_timer);
	}

	if ((retval = extract_sin(notice, &from)) != ZERR_NONE) {
		syslog(LOG_ERR, "gbd sin: %s", error_message(retval));
		(void) signal(SIGPIPE, SIG_DFL);
		bdumping = 0;
		return;
	}
#ifndef KERBEROS
	if (ntohs(from.sin_port) > IPPORT_RESERVED ||
	    ntohs(from.sin_port) < IPPORT_RESERVED / 2) {
		syslog(LOG_ERR, "gbd port not reserved: %d",
		       ntohs(from.sin_port));
		cleanup(server, sock);
		return;
	}
	if ((sock = rresvport(&reserved_port)) < 0) {
#else
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#endif KERBEROS
		syslog(LOG_ERR, "gbd socket: %m");
		cleanup(server, sock);
		return;
	}
	if (connect(sock, (struct sockaddr *) &from, sizeof(from))) {
		syslog(LOG_ERR, "gbd connect: %m");
		cleanup(server, sock);
		return;
	}
	zdbug((LOG_DEBUG, "gbd connected"));

	/* Now begin the brain dump.  Set the Zephyr port to be the
	   TCP connection. */
	(void) ZSetFD(sock);

#ifdef KERBEROS
	/* send an authenticator */
	if (get_tgt()) {
		cleanup(server, sock);
		return;
	}
	if ((retval = SendKerberosData(sock, &ticket, "zephyr", "zephyr"))
	    != KSUCCESS) {
		syslog(LOG_ERR,"bdump_send: %s",krb_err_txt[retval]);
		cleanup(server, sock);
		return;
	}
	zdbug((LOG_DEBUG,"skd ok"));

	/* get his authenticator */
	if ((retval = GetKerberosData(sock, from.sin_addr, &kdata, "zephyr",
				      ZEPHYR_SRVTAB)) != KSUCCESS) {
		syslog(LOG_ERR, "sbd getkdata: %s",krb_err_txt[retval]);
		cleanup(server, sock);
		return;
	}
	if (strcmp(kdata.pname,"zephyr") || strcmp(kdata.pinst,"zephyr")) {
		syslog(LOG_ERR, "sbd peer not zephyr: %s.%s@%s",
		       kdata.pname, kdata.pinst,kdata.prealm);
		cleanup(server, sock);
		return;
	}
#endif KERBEROS
	if ((retval = gbd_loop(server)) != ZERR_NONE) {
		syslog(LOG_WARNING, "gbd_loop failed: %s",
		       error_message(retval));
		cleanup(server, sock);
		return;
	} else {
		zdbug((LOG_DEBUG,"gbdl ok"));
		if ((retval = sbd_loop(&from)) != ZERR_NONE) {
			syslog(LOG_WARNING, "sbd_loop failed: %s",
			       error_message(retval));
			cleanup(server, sock);
		} else {
			zdbug((LOG_DEBUG, "gbd finished"));
			/* set this guy to be up,
			   and schedule a hello */
			server->zs_state = SERV_UP;
			timer_reset(server->zs_timer);
			server->zs_timer = timer_set_rel(0L, server_timo, (caddr_t) server);
		}
	}

	zdbug((LOG_DEBUG,"cleanup gbd"));
	(void) ZSetFD(srv_socket);
	(void) close(sock);
	(void) signal(SIGPIPE, SIG_DFL);
	bdump_inited = 1;
	bdumping = 0;
	return;
}

/*
 * Send a list off as the specified notice
 */

Code_t
bdump_send_list_tcp(kind, port, class, inst, opcode, sender, recip, lyst, num)
ZNotice_Kind_t kind;
u_short port;
char *class, *inst, *opcode, *sender, *recip;
char *lyst[];
int num;
{
	ZNotice_t notice;
	register ZNotice_t *pnotice; /* speed hack */
	ZPacket_t pack;
	int packlen, count;
	Code_t retval;
	u_short length;

	pnotice = &notice;

	pnotice->z_kind = kind;

	pnotice->z_port = port;
	pnotice->z_class = class;
	pnotice->z_class_inst = inst;
	pnotice->z_opcode = opcode;
	pnotice->z_sender = sender;
	pnotice->z_recipient = recip;
	pnotice->z_default_format = 0;

	packlen = sizeof(pack);
	
	if ((retval = ZFormatNoticeList(pnotice, lyst, num, pack, packlen, &packlen, ZNOAUTH)) != ZERR_NONE)
		return(retval);
	
	length = htons((u_short) packlen);

	if ((count = write(ZGetFD(), (caddr_t) &length, sizeof(length))) != sizeof(length))
		if (count < 0)
			return(count);
		else {
			syslog(LOG_WARNING, "slt xmit: %d vs %d",sizeof(length),count);
			return(ZSRV_PKSHORT);
		}

	if ((count = write(ZGetFD(), pack, packlen)) != packlen)
		if (count < 0)
			return(count);
		else {
			syslog(LOG_WARNING, "slt xmit: %d vs %d",packlen, count);
			return(ZSRV_PKSHORT);
		}
	return(ZERR_NONE);
}

static void
cleanup(server, sock)
ZServerDesc_t *server;
int sock;
{
	zdbug((LOG_DEBUG, "cleanup"));
	if (server != limbo_server) {
		server->zs_state = SERV_DEAD;
		timer_reset(server->zs_timer);
		server->zs_timer =
			timer_set_rel(0L, server_timo, (caddr_t) server);
	}
	(void) close(sock);
	(void) ZSetFD(srv_socket);
	(void) signal(SIGPIPE, SIG_DFL);
	bdumping = 0;
	return;
}

#ifdef KERBEROS
static int
get_tgt()
{
	int retval;

	if (!*my_realm)
		if ((retval = get_krbrlm(my_realm, 1)) != KSUCCESS) {
			syslog(LOG_ERR,"krbrlm: %s",
			       krb_err_txt[retval]);
			return(1);
		}
	/* have they expired ? */
	if (ticket_time < NOW - (96L * 5L) + 15L) { /* +15 for leeway */
		zdbug((LOG_DEBUG,"get new tickets: %d %d %d",
		       ticket_time, NOW, NOW - (96L * 5L) + 15L));
		(void) dest_tkt();
		if ((retval =
		     get_svc_in_tkt("zephyr","zephyr",my_realm,"zephyr","zephyr",
				    96, ZEPHYR_SRVTAB)) != KSUCCESS) {
			syslog(LOG_ERR,"get_tkt: %s",
			       krb_err_txt[retval]);
			ticket_time = 0L;
			return(1);
		} else
			ticket_time = NOW;
	}
	return(0);
}
#endif KERBEROS

static Code_t
sbd_loop(from)
struct sockaddr_in *from;
{
	ZNotice_t bd_notice;
	ZPacket_t pack;
	ZServerDesc_t *server;
	int packlen = sizeof(pack);
	Code_t retval;
	struct sockaddr_in bogus_from;
	char *zeph_version = NULL;

	bogus_from = *from;
	bogus_from.sin_port = sock_sin.sin_port;

	while (1) {
		packlen = sizeof(pack);
		if ((retval = get_packet(pack, packlen, &packlen)) != ZERR_NONE) {
			syslog(LOG_ERR, "sbd notice get: %s",
			       error_message(retval));
			return(retval);
		}
		if ((retval = ZParseNotice(pack, packlen, &bd_notice)) != ZERR_NONE) {
			syslog(LOG_ERR, "sbd notice parse: %s",
			       error_message(retval));
			return(retval);
		}
		if (!zeph_version)
			zeph_version = strsave(bd_notice.z_version);
#ifdef DEBUG
		if (zdebug) {
			char buf[4096];
		
			(void) sprintf(buf, "bdump:%s '%s' '%s' '%s' '%s' '%s'",
				       pktypes[(int) bd_notice.z_kind],
				       bd_notice.z_class,
				       bd_notice.z_class_inst,
				       bd_notice.z_opcode,
				       bd_notice.z_sender,
				       bd_notice.z_recipient);
			syslog(LOG_DEBUG, buf);
		}
#endif DEBUG
		if (!strcmp(bd_notice.z_class_inst, ADMIN_LIMBO)) {
			/* he wants limbo */
			zdbug((LOG_DEBUG, "limbo req"));
			if ((retval = bdump_send_loop(limbo_server,
						      zeph_version))
			    != ZERR_NONE)
				return(retval);
			continue;
		} else if (!strcmp(bd_notice.z_class_inst, ADMIN_ME)) {
			/* he wants his state */
			zdbug((LOG_DEBUG, "his state req"));
			if (server = server_which_server(&bogus_from)) {
				if ((retval = bdump_send_loop(server,
							      zeph_version))
				    != ZERR_NONE)
					return(retval);
			} else {
				syslog(LOG_ERR,"sbd_loop: no state");
				if ((retval = send_done()) != ZERR_NONE)
					return(retval);
			}
			continue;
		} else if (!strcmp(bd_notice.z_class_inst, ADMIN_YOU)) {
			/* he wants my state */
			zdbug((LOG_DEBUG, "my state req"));
			if ((retval = bdump_send_loop(me_server, zeph_version))
			    != ZERR_NONE)
				return(retval);
			break;
		} else if (!strcmp(bd_notice.z_class_inst, ADMIN_DONE)) {
			break;
		} else {
			/* what does he want? */
			zdbug((LOG_DEBUG, "unknown req"));
			break;
		}
	}
	return(ZERR_NONE);
}

static Code_t
gbd_loop(server)
ZServerDesc_t *server;
{
	Code_t retval;

	/* if we have no hosts in the 'limbo' state (on the limbo server),
	   ask for the other server to send us the limbo state. */
	if (otherservers[limbo_server_idx()].zs_hosts->q_forw ==
	    otherservers[limbo_server_idx()].zs_hosts) {
		if ((retval = bdump_ask_for(ADMIN_LIMBO)) != ZERR_NONE)
			return(retval);
		if ((retval = bdump_recv_loop(&otherservers[limbo_server_idx()])) != ZERR_NONE)
			return(retval);
	}

	if (!bdump_inited) {
		if ((retval = bdump_ask_for(ADMIN_ME)) != ZERR_NONE)
			return(retval);
		if ((retval = bdump_recv_loop(me_server)) != ZERR_NONE)
			return(retval);
	}
	if ((retval = bdump_ask_for(ADMIN_YOU)) != ZERR_NONE)
		return(retval);
	retval = bdump_recv_loop(server);
	return(retval);
}
/*
 * The braindump offer wasn't taken, so we retract it.
 */

/*ARGSUSED*/
static void
close_bdump(arg)
caddr_t arg;
{
	if (bdump_socket) {
		FD_CLR(bdump_socket, &interesting);
		(void) close(bdump_socket);
		nfildes = srv_socket + 1;
		bdump_socket = 0;
		zdbug((LOG_DEBUG, "bdump not used"));
	} else {
		zdbug((LOG_DEBUG, "bdump not open"));
	}
	return;
}

/*
 * Ask the other server to send instruction packets for class instance
 * inst
 */

static Code_t
bdump_ask_for(inst)
char *inst;
{
	Code_t retval;

	/* myname is the hostname */
	retval = send_normal_tcp(ACKED, bdump_sin.sin_port, ZEPHYR_ADMIN_CLASS,
				 inst, ADMIN_BDUMP, myname, "",
				 (char *) NULL, 0);
	return(retval);
}

/*
 * Start receiving instruction notices from the brain dump socket
 */

static Code_t
bdump_recv_loop(server)
ZServerDesc_t *server;
{
	ZNotice_t notice;
	ZPacket_t packet;
	int len;
	Code_t retval;
	ZClient_t *client = NULLZCNT;
	struct sockaddr_in current_who;
	int who_valid = 0;
	register char *cp;

	zdbug((LOG_DEBUG, "bdump recv loop"));
	
	/* XXX do the inverse, registering stuff on the fly */
	while (1) {
		len = sizeof(packet);
		if ((retval = get_packet(packet, len, &len)) != ZERR_NONE) {
			syslog(LOG_ERR, "brl get pkt: %s",
			       error_message(retval));
			return(retval);
		}
		if ((retval = ZParseNotice(packet, len, &notice)) != ZERR_NONE) {
			syslog(LOG_ERR, "brl notice parse: %s",
			       error_message(retval));
			return(retval);
		}
#ifdef DEBUG
		if (zdebug) {
			char buf[4096];
		
			(void) sprintf(buf, "bdump:%s '%s' '%s' '%s' '%s' '%s'",
				       pktypes[(int) notice.z_kind],
				       notice.z_class,
				       notice.z_class_inst,
				       notice.z_opcode,
				       notice.z_sender,
				       notice.z_recipient);
			syslog(LOG_DEBUG, buf);
		}
#endif DEBUG
		if (notice.z_kind == HMCTL) {
			/* host register */
			if ((retval = extract_sin(&notice, &current_who)) !=
			    ZERR_NONE) {
				syslog(LOG_ERR, "brl hmctl sin: %s",
				       error_message(retval));
				return(retval);
			}
			who_valid = 1;
			/* 1 = tell it we are authentic */
			hostm_dispatch(&notice, 1, &current_who, server);
		} else if (!strcmp(notice.z_opcode, ADMIN_DONE)) {
			/* end of brain dump */
			return(ZERR_NONE);
		} else if (!who_valid) {
			syslog(LOG_ERR, "brl: no current host");
			return(ZSRV_HNOTFOUND);
		} else if (!strcmp(notice.z_class, LOGIN_CLASS)) {
			/* 1 = tell it we are authentic */
			ulogin_dispatch(&notice, 1, &current_who, server);
		} else if (!strcmp(notice.z_opcode, ADMIN_NEWCLT)) {
			/* register a new client */
			notice.z_port = htons((u_short)atoi(notice.z_message));
			if ((retval = client_register(&notice,
						      &current_who,
						      &client,
						      server)) != ZERR_NONE) {
				syslog(LOG_ERR,"brl register failed: %s",
				       error_message(retval));
				return(retval);
			}
#ifdef KERBEROS
			bzero((caddr_t) client->zct_cblock,
					      sizeof(C_Block));
			if (*notice.z_class_inst) {
				/* a C_Block is there */
				cp = notice.z_message +
					strlen(notice.z_message) + 1;
				if ((retval = ZReadAscii(cp,strlen(cp),client->zct_cblock,sizeof(C_Block))) != ZERR_NONE) {
					bzero((caddr_t) client->zct_cblock,
					      sizeof(C_Block));
					syslog(LOG_ERR,"brl bad cblk read: %s (%s)",
					       error_message(retval),
					       cp);
				}
			}
#endif KERBEROS
		} else if (!strcmp(notice.z_opcode, CLIENT_SUBSCRIBE)) { 
			/* a subscription packet */
			if (!client) {
				syslog(LOG_ERR, "brl no client");
				return(ZSRV_NOCLT);
			}
			if ((retval = subscr_subscribe(client, &notice)) != ZERR_NONE) {
				syslog(LOG_WARNING, "brl subscr failed: %s",
				       error_message(retval));
				return(retval);
			}
		} else {
			syslog(LOG_ERR, "brl bad opcode %s",notice.z_opcode);
			return(ZSRV_UNKNOWNOPCODE);
		}
	}
}

/*
 * Send all the state from server to the peer.
 */

static Code_t
bdump_send_loop(server, vers)
register ZServerDesc_t *server;
char *vers;
{
	register ZHostList_t *host;
	register ZClientList_t *clist;
	Code_t retval;

	zdbug((LOG_DEBUG, "bdump send loop"));

	for (host = server->zs_hosts->q_forw;
	     host != server->zs_hosts;
	     host = host->q_forw) {
		/* for each host */
		if ((retval = send_host_register(host)) != ZERR_NONE)
			return(retval);
		if ((retval = uloc_send_locations(host, vers)) != ZERR_NONE)
			return(retval);
		if (!host->zh_clients)
			continue;
		for (clist = host->zh_clients->q_forw;
		     clist != host->zh_clients;
		     clist = clist->q_forw) {
			/* for each client */
			if (!clist->zclt_client->zct_subs)
				continue;
			if ((retval = subscr_send_subs(clist->zclt_client,
						       vers)) != ZERR_NONE)
				return(retval);
		}
	}
	retval = send_done();
	return(retval);
}

/*
 * Send a host boot packet to the other server
 */

static Code_t
send_host_register(host)
ZHostList_t *host;
{
	char buf[512], *addr, *lyst[2];
	Code_t retval;

	zdbug((LOG_DEBUG, "bdump_host_register"));
	addr = inet_ntoa(host->zh_addr.sin_addr);
	(void) sprintf(buf, "%d", ntohs(host->zh_addr.sin_port));
	lyst[0] = addr;
	lyst[1] = buf;

	/* myname is the hostname */
	if ((retval = bdump_send_list_tcp(HMCTL, bdump_sin.sin_port, ZEPHYR_CTL_CLASS, ZEPHYR_CTL_HM, HM_BOOT, myname, "", lyst, 2)) != ZERR_NONE)
		syslog(LOG_ERR, "shr send: %s",error_message(retval));
	return(retval);
}

/*
 * Send a sync indicating end of this host
 */

static Code_t
send_done()
{
	Code_t retval;

	zdbug((LOG_DEBUG, "send_done"));
	retval = send_normal_tcp(SERVACK, bdump_sin.sin_port,
				 ZEPHYR_ADMIN_CLASS, "", ADMIN_DONE, myname,
				 "", (char *) NULL, 0);
	return(retval);
}


/*
 * Send a list off as the specified notice
 */

static Code_t
send_list(kind, port, class, inst, opcode, sender, recip, lyst, num)
ZNotice_Kind_t kind;
u_short port;
char *class, *inst, *opcode, *sender, *recip;
char *lyst[];
int num;
{
	ZNotice_t notice;
	register ZNotice_t *pnotice; /* speed hack */
	ZPacket_t pack;
	int packlen;
	Code_t retval;

	pnotice = &notice;

	pnotice->z_kind = kind;

	pnotice->z_port = port;
	pnotice->z_class = class;
	pnotice->z_class_inst = inst;
	pnotice->z_opcode = opcode;
	pnotice->z_sender = sender;
	pnotice->z_recipient = recip;
	pnotice->z_default_format = 0;
	
	packlen = sizeof(pack);
	
	if ((retval = ZFormatNoticeList(pnotice, lyst, num, pack, packlen, &packlen, ZNOAUTH)) != ZERR_NONE) {
		syslog(LOG_WARNING, "sl format: %s", error_message(retval));
		return(retval);
	}
	
	if ((retval = ZSendPacket(pack, packlen)) != ZERR_NONE) {
		syslog(LOG_WARNING, "sl xmit: %s", error_message(retval));
		return(retval);
	}
	return(ZERR_NONE);
}

/*
 * Send a message off as the specified notice, via TCP
 */

static Code_t
send_normal_tcp(kind, port, class, inst, opcode, sender, recip, message, len)
ZNotice_Kind_t kind;
u_short port;
char *class, *inst, *opcode, *sender, *recip;
char *message;
int len;
{
	ZNotice_t notice;
	register ZNotice_t *pnotice; /* speed hack */
	ZPacket_t pack;
	int packlen, count;
	Code_t retval;
	u_short length;

	pnotice = &notice;

	pnotice->z_kind = kind;

	pnotice->z_port = port;
	pnotice->z_class = class;
	pnotice->z_class_inst = inst;
	pnotice->z_opcode = opcode;
	pnotice->z_sender = sender;
	pnotice->z_recipient = recip;
	pnotice->z_default_format = 0;
	pnotice->z_message = message;
	pnotice->z_message_len = len;

	packlen = sizeof(pack);
	
	if ((retval = ZFormatNotice(pnotice, pack, packlen, &packlen, ZNOAUTH)) != ZERR_NONE) {
		syslog(LOG_WARNING, "sn format: %s", error_message(retval));
		return(retval);
	}

	length = htons((u_short) packlen);

	if ((count = write(ZGetFD(), (caddr_t) &length, sizeof(length))) != sizeof(length)) {
		if (count < 0) {
			syslog(LOG_WARNING, "snt xmit/len: %m");
			return(errno);
		} else {
			syslog(LOG_WARNING, "snt xmit: %d vs %d",sizeof(length),count);
			return(ZSRV_LEN);
		}
	}
	if ((count = write(ZGetFD(), pack, packlen)) != packlen)
		if (count < 0) {
			syslog(LOG_WARNING, "snt xmit: %m");
			return(errno);
		} else {
			syslog(LOG_WARNING, "snt xmit: %d vs %d",packlen, count);
			return(ZSRV_LEN);
		}
	return(ZERR_NONE);
}

/*
 * get a packet from the TCP socket
 * return 0 if successful, error code else
 */

static Code_t
get_packet(packet, len, retlen)
caddr_t packet;
int len;
int *retlen;				/* RETURN */
{
	u_short length;
	int result;

	if ((result = read(ZGetFD(), (caddr_t) &length, sizeof(u_short))) < sizeof(short)) {
		if (result < 0)
			return(errno);
		else {
			syslog(LOG_ERR, "get_pkt len: %d vs %d", result, sizeof(short));
			return(ZSRV_LEN);
		}
	}
	
	length = ntohs(length);
	if (len < length)
		return(ZSRV_BUFSHORT);
	if ((result = read(ZGetFD(), packet, (int) length)) < length) {
		if (result < 0)
			return(errno);
		else {
			syslog(LOG_ERR, "get_pkt: %d vs %d",result, length);
			return(ZSRV_LEN);
		}
	}
	*retlen = (int) length;
	return(ZERR_NONE);
}

static Code_t
extract_sin(notice, target)
ZNotice_t *notice;
struct sockaddr_in *target;
{
	register char *cp = notice->z_message;
	char *buf;

	buf = cp;
	if (!notice->z_message_len || *buf == '\0') {
		zdbug((LOG_DEBUG,"no addr"));
		return(ZSRV_PKSHORT);
	}
	target->sin_addr.s_addr = inet_addr(cp);
	cp += (strlen(cp) + 1); /* past the null */
	if ((cp >= notice->z_message + notice->z_message_len)
	    || (*cp == '\0')) {
		zdbug((LOG_DEBUG, "no port"));
		return(ZSRV_PKSHORT);
	}
	target->sin_port = htons((u_short) atoi(cp));
	target->sin_family = AF_INET;
	return(ZERR_NONE);
}