aboutsummaryrefslogtreecommitdiffstats
path: root/dumper.c
blob: f11acc63407886e117ff71a44ee38ce3726ad851 (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
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
#include "config.h"
#include "conf.h"
#include "err.h"
#include "lock.h"
#include "strutil.h"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#ifdef NEED_LIBUTIL
#include <libutil.h>
#endif

#ifdef NEED_STRINGS
#include <strings.h>
#endif

RCSID("$Id: dumper.c,v 1.1.1.1 1999/02/02 23:29:39 shmit Exp $");

struct diskinfo {
	char vol[MAXPATHLEN];		/* Volume name. */
	int dumplevel;			/* Level of dump, if applicable. */
	ssize_t size;			/* How big dump is expected to be. */
	auth_t authtype;		/* Type of auth for dump. */
	struct diskinfo *next;
};
typedef struct diskinfo diskinfo_t;

struct hostinfo {
	char host[MAXHOSTNAMELEN];	/* Hostname. */
	int socket;			/* The socket for the dump. */
	int port;			/* Port on which to send dump. */
	int pin[2], pout[2], perr[2];	/* Connection to RSH pipe. */
	diskinfo_t *disks;		/* Disk information. */
	struct hostinfo *next;
};
typedef struct hostinfo hostinfo_t;

int taperpipe[2];
short exit_status = -1;

/*
 * If something terminates abnormally, this will be called.
 */
void
sig_pipe(int signo)
{
	log_err("SIGPIPE caught.\n");
	exit_status = -3;
}

/*
 * Clean up a child.
 */
void
sig_child(int signo)
{
	int status;

	if (wait(&status) == -1)
		log_err("Warning: couldn't clean up child.\n");

	exit_status = WEXITSTATUS(status);
}

/*
 * Tell the taper to write a file to tape.
 */
void
write_to_tape(const char *spooldir, const char *host, const char *filen)
{
	char buffer[MAXLINE];
	struct sigaction sa, osa;

	/*
	 * If SIGPIPE comes when we're trying to write to the taper
	 * descriptor, silently ignore it.
	 */
        sa.sa_flags = 0;
        sigemptyset(&sa.sa_mask);
        sa.sa_handler = SIG_IGN;
        sigaction(SIGPIPE, &sa, &osa);

	log_info("%s: telling taper to write `%s/%s:%s' to tape.\n",
		 host, spooldir, host, filen);
	sprintf(buffer, DISK_REQ "%s/%s:%s\n", spooldir, host, filen);

	write(taperpipe[1], buffer, strlen(buffer));

	/* Put the old handler back. */
        sigaction(SIGPIPE, &osa, NULL);
}

/*
 * Create a file on the holding which will contain the dump.
 */
inline int
open_stagefile(const char *spooldir, const char *host, const char *name)
{
	char filen[MAXPATHLEN];

	/* TODO: sanity check the name argument. */

	log_info("%s: dumping output to `%s/%s:%s'.\n",
		 host, spooldir, host, name);
	snprintf(filen, sizeof(filen), "%s/%s:%s", spooldir, host, name);
	return open(filen, O_WRONLY|O_CREAT|O_TRUNC, 0666);
}

/*
 * Clear out remaining data on a file descriptor.
 */
int
flush(const char *host, int infd, int outfd, ssize_t *total)
{
	char buffer[MAXLINE];
	ssize_t readb;

	log_info("%s: flushing remaining output.\n", host);

	if (fcntl(infd, F_SETFL, 0) == -1) {
		log_err("%s: couldn't set input to non-blocking: %s.\n",
			host, strerror(errno));
		return 0;
	}

READ:
	while ((readb = read(infd, buffer, sizeof(buffer))) > 0) {
		ssize_t wrote = 0;

		*total += readb;
		while (wrote < readb) {
			ssize_t n;

		WRITE:
			n = write(outfd, buffer, readb-wrote);
			if (n == -1) {
				if (errno == EINTR)
					goto WRITE;
				log_err("TODO: fix this message.\n");
				return 0;
			}
			wrote += n;
		}
	}
	if (readb == -1) {
		if (errno == EINTR || errno == EAGAIN)
			goto READ;
		log_err("%s: error occurred while flushing"
			" remaining output: %s.\n", host, strerror(errno));
		return 0;
	}

	return 1;
}

/*
 * Say `Hello' to the remote host and wait for a 'How are you'.
 */
int
do_handshake(int infd, int outfd, char *host)
{
	char buffer[MAXLINE];
	ssize_t readb;

read_handshake:
	readb = read(infd, buffer, sizeof(buffer));
	if (readb == -1) {
		if (errno == EINTR)
			goto read_handshake;
		log_err("Master: couldn't read handshake from %s: %s.\n",
			host, strerror(errno));
		return -1;
	}
	buffer[readb-1] = '\0';

	if (strncmp(buffer, INIT_REQ, sizeof(buffer))) {
		log_err("Master: Bad protocol from %s: handshake: %s.\n",
			host, buffer);
		return -1;
	}

	if (write(outfd, INIT_ACK "\n", sizeof(INIT_ACK)) == -1) {
		log_err("Master: couldn't send handshake ACK to %s: %s.\n",
			host, strerror(errno));
		return -1;
	}

	return 0;
}

/*
 * Handle error conditions on the remote host.
 */
int
do_error(int infd, const char *host)
{
	if (fcntl(infd, F_SETFL, 0) == -1) {
		log_err("%s: couldn't set fd to"
			" blocking mode.\n", host);
		return 0;
	}

	readerr(infd, host);
	return 1;
}

/*
 * Ask the remote host to send us a disk.
 */
int
do_newfile(diskinfo_t *disk, hostinfo_t *host, const char *spooldir,
	   int *infd, int *outfd)
{
	char buffer[MAXLINE], word[MAXLINE], filen[MAXPATHLEN];
	char *i = filen;
	ssize_t readb;

	switch (disk->authtype) {
	case NOAUTH:
		snprintf(buffer, sizeof(buffer),
			 "%s %s %s %s %d %s %s\n",
			  DUMP_SENDME, DISK_NAME, disk->vol,
			  DISK_LEVEL, disk->dumplevel,
			  DISK_AUTH, AUTH_NOAUTH);
		break;
	case RSH:
		snprintf(buffer, sizeof(buffer),
			 "%s %s %s %s %d %s %s\n",
			  DUMP_SENDME, DISK_NAME, disk->vol,
			  DISK_LEVEL, disk->dumplevel,
			  DISK_AUTH, AUTH_RSH);
		break;
	default:
		log_err("%s: unknown auth type for %s: %d.\n",
			host->host, disk->vol, disk->authtype);
		break;
	}

	if (write(host->pin[1], buffer, strlen(buffer)) == -1) {
		log_err("%s: couldn't send instructions: %s.\n",
			host->host, strerror(errno));
		return -1;
	}

READ:
	readb = read(host->pout[0], buffer, sizeof(buffer));
	if (readb == -1) {
		if (errno == EINTR)
			goto READ;

		log_err("%s: couldn't read ACK for disk `%s': %s.\n",
			host->host, disk->vol, strerror(errno));
		return -1;
	} else if (!readb) {
		log_err("%s: rsh client died while setting up connection.\n",
			host->host);
		return -1;
	}
	buffer[readb] = '\0';

	i = getword(buffer, word);
	if (!strncmp(word, DUMP_ERROR, sizeof(word))) {
		do_error(host->perr[0], host->host);
		return -1;
	} else if (strncmp(word, REQ_ACK, sizeof(word))) {
		log_err("%s: don't understand `%s'.\n", host->host, buffer);
		return -1;
	}

	i = filen;
	strncpy(filen, disk->vol, sizeof(filen));
	while ((i = index(i, '/')))
		*i = '_';

	*outfd = open_stagefile(spooldir, host->host, filen);
	if (*outfd == -1)
		return -1;

	switch (disk->authtype) {
	case NOAUTH: {
		struct hostent *he;
		struct sockaddr_in sa;

		*infd = socket(PF_INET, SOCK_STREAM, 0);
		if (*infd == -1) {
			log_err("%s: couldn't create socket: %s.\n",
				host->host, strerror(errno));
			return -1;
		}

		/* TODO: make sure we're not an IP address. */
		he = gethostbyname(host->host);
		if (!he) {
			log_err("%s: couldn't resolve hostname `%s'.\n",
				host->host, host->host);
			return -1;
		}
		sa.sin_family = AF_INET;
		sa.sin_port = htons(host->port);
		memcpy(&sa.sin_addr.s_addr, he->h_addr,
		       sizeof(sa.sin_addr.s_addr));

		if (connect(*infd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
			log_err("%s: couldn't connect: %s.\n",
				host->host, strerror(errno));
			return -1;
		}
	}; break;
	case RSH:
		log_err("%s: RSH auth not supported right now.\n", host->host);
		return -1;
		break;
	default:
		log_err("%s: don't understand AUTH type: %d.\n",
			host->host, disk->authtype);
		return -1;
	}

	return 0;
}

/*
 * Read a disk from a stream and spool it to the holding disk.
 */
int
read_backup(diskinfo_t *disk, hostinfo_t *host,
	    const char *spooldir, int timeout)
{
	char buffer[MAXLINE];
	fd_set readfds, exceptfds;
	ssize_t readb, total = 0;
	time_t start;
	struct timeval tv;
	int infd, outfd;
	int done = 0, error = 0;

	fcntl(host->pout[0], F_SETFL, 0);
	if (do_newfile(disk, host, spooldir, &infd, &outfd) == -1) {
		if (exit_status != -1)
			return -1;
		else
			return 0;
	}
	fcntl(host->pout[0], F_SETFL, O_NONBLOCK);

	start = time(NULL);
	while (exit_status == -1) {
		FD_ZERO(&readfds);
		FD_ZERO(&exceptfds);
		FD_SET(infd, &readfds);
		FD_SET(infd, &exceptfds);
		FD_SET(host->pout[0], &readfds);
		FD_SET(host->pout[0], &exceptfds);
		FD_SET(host->perr[0], &readfds);
		FD_SET(host->perr[0], &exceptfds);
		tv.tv_sec = timeout;
		tv.tv_usec = 0;

		if (select(FD_SETSIZE, &readfds, NULL, &exceptfds, &tv) == -1) {
			if (errno == EINTR)
				continue;
			log_err("%s: select failed: %s.\n",
				host->host, strerror(errno));
			return -1;
		}

		/* TODO: check exceptfds. */

		if (FD_ISSET(host->pout[0], &readfds)) {
			char word[MAXLINE];
			char *p;

		READSTDOUT:
			readb = read(host->pout[0], buffer, sizeof(buffer));
			if (readb == -1) {
				if (errno == EINTR)
					goto READSTDOUT;
				log_err("%s: read died with: %s.\n",
					host->host, strerror(errno));
				break;
			} else if (!readb) {
				log_err("%s: rsh client died.\n", host->host);
				break;
			}

			p = getword(buffer, word);
			if (!strncmp(word, DUMP_ERROR, sizeof(word))) {
				if (*p == '\n')
					p++;
				do_error(host->perr[0], host->host);
				error = 1;
				break;
			} else if (!strncmp(word, DUMP_DONE, sizeof(word))) {
				flush(host->host, infd, outfd, &total);
				log_info("%s: Wrote %ld bytes in %d seconds.\n",
					 host->host, total, time(NULL)-start);
				done = 1;
				break;
			} else {
				log_err("%s: No header on following output;"
					" assuming error:\n", host->host);
				do_error(host->pout[0], host->host);
				error = 1;
				break;
			}
		}

		if (FD_ISSET(host->perr[0], &readfds)) {
		READSTDERR:
			readb = read(host->perr[0], buffer, sizeof(buffer));
			if (readb == -1) {
				if (errno == EINTR)
					goto READSTDERR;
				log_err("%s: read of stderr died with: %s.\n",
					host->host, strerror(errno));
				break;
			} else if (!readb) {
				log_err("%s: rsh client died.\n", host->host);
			}

			readerr(host->perr[0], host->host);
		}

		if (FD_ISSET(infd, &readfds)) {
		READDATA:
			readb = read(infd, buffer, sizeof(buffer));
			if (readb == -1) {
				if (errno == EINTR)
					goto READDATA;
				log_err("%s: read died with: %s.\n",
					host->host, strerror(errno));
				break;
			} else if (!readb)
				break;

			write(outfd, buffer, readb);
			total += readb;
		}
	}

	/* If we haven't seen any control information, scan for it now. */
	if (!done && !error) {
		ssize_t readb;

		/* TODO: check stderr too. */
		fcntl(host->pout[0], F_SETFL, 0);
	READ:
		readb = read(host->pout[0], buffer, sizeof(buffer));
		if (readb == -1) {
			if (errno == EINTR)
				goto READ;

			log_err("%s: read returned -1: %s.\n",
				host->host, strerror(errno));
		} else if (readb) {
			char word[MAXLINE];
			char *p;

			p = getword(buffer, word);
			if (!strncmp(word, DUMP_ERROR, sizeof(word))) {
				if (*p == '\n')
					p++;
				do_error(host->perr[0], host->host);
				return -1;
			} else if (!strncmp(word, DUMP_DONE, sizeof(word))) {
				flush(host->host, infd, outfd, &total);
				log_info("%s: Wrote %ld bytes in %d seconds.\n",
					 host->host, total, time(NULL)-start);
				done = 1;
			} else {
				log_err("%s: No header on following output;"
					" assuming error:\n", host->host);
				do_error(host->pout[0], host->host);
				return -1;
			}
		}
	}

	if (done) {
		char filen[MAXLINE];
		char *i;

		strncpy(filen, disk->vol, sizeof(filen));

		i = filen;
		while ((i = index(i, '/')))
			*i = '_';

		write_to_tape(spooldir, host->host, filen);
		return 0;
	}

	if (error)
		return -1;

	/*
	 * Exit status is grabbed by the wait function in sig_child.
	 * If we've come this far, we haven't recieved the DONE command
	 * from run-dump, so try and get some information and log it.
	 */
	switch (exit_status) {
	case -1:
		log_err("%s: premature death of rsh client.\n", host->host);
		break;
	case -2:
		log_err("%s: connection timed out.\n", host->host);
		break;
	case -3:
		log_err("%s: SIGPIPE received: this shoudn't have happened.\n",
			host->host);
		break;
	default:
		log_err("%s: run-dump exit status: %d.\n",
			host->host, exit_status);
	}

	return -1;
}

/*
 * Open up a connection to a host and back it up.
 */
int
do_backup(hostinfo_t *host, const char *spooldir, long timeout)
{
	diskinfo_t *disk = host->disks;
	pid_t pid;
	int status;
	struct sigaction sa, opsa, ocsa;

	log_info("%s: starting backup.\n", host->host);

	/* Set up signal handlers. */
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = sig_pipe;
	sigaction(SIGPIPE, &sa, &opsa);
	sa.sa_handler = sig_child;
	sigaction(SIGCHLD, &sa, &ocsa);

	/* Set up the RSH master pipe and save file descriptors. */
	if (pipe(host->pin) == -1 || pipe(host->pout) == -1 ||
	    pipe(host->perr) == -1) {
		log_err("Master: couldn't allocate pipes for %s: %s.\n",
			host, strerror(errno));
		return -1;
	}

	pid = fork();
	if (pid < 0){
		log_err("%s: Couldn't fork: %s.\n",
			host->host, strerror(errno));
		return -1;
	} else if (!pid) {
		close(host->pin[1]);
		close(host->pout[0]);
		close(host->perr[0]);
		if (dup2(host->pin[0], STDIN_FILENO) == -1 ||
		    dup2(host->pout[1], STDOUT_FILENO) == -1 ||
		    dup2(host->perr[1], STDERR_FILENO) == -1)
			exit(1);

		execlp(PATH_RSH, PATH_RSH, host, "run-dump", NULL);
		err("Couldn't exec run-dump: %s.\n", strerror(errno));
		exit(1);
	}
	close(host->pin[0]);
	close(host->pout[1]);
	close(host->perr[1]);

	if (do_handshake(host->pout[0], host->pin[1], host->host) == -1)
		return -1;

	if (host->port) {
		char buffer[MAXLINE], word[MAXLINE];
		char *p = buffer;
		ssize_t readb;

		snprintf(buffer, sizeof(buffer), "%s %d\n",
			 PORT_REQ, host->port);
		write(host->pin[1], buffer, strlen(buffer));

	READ:
		readb = read(host->pout[0], buffer, sizeof(buffer));
		if (readb == -1) {
			if (errno == EINTR)
				goto READ;
			log_err("%s: read died: %s.\n",
				host->host, strerror(errno));
			return -1;
		}

		p = getword(buffer, word);
		if (!strncmp(word, DUMP_ERROR, sizeof(word))) {
			do_error(host->perr[0], host->host);
			return -1;
		} else if (strncmp(word, REQ_ACK, sizeof(word))) {
			log_err("%s: couldn't negotiate port.\n",
				host->host);
			return -1;
		}
	}

	/* TODO: this should probably happen before the handshake. */
	if (fcntl(host->pout[0], F_SETFL, O_NONBLOCK) == -1 ||
	    fcntl(host->perr[0], F_SETFL, O_NONBLOCK) == -1) {
		log_err("%s: couldn't put pipes into non blocking mode: %s.\n",
			host->host, strerror(errno));
		return -1;
	}

	while (disk) {
		if (read_backup(disk, host, spooldir, timeout) == -1)
			break;
		disk = disk->next;
	}

	/* Put the old handlers back. */
	sigaction(SIGCHLD, &ocsa, NULL);
	sigaction(SIGPIPE, &opsa, NULL);

	if (!disk)
		/* TODO: check return value? */
		write(host->pin[1], DUMP_DONE "\n", sizeof(DUMP_DONE));

	/* Potentially there is stuff on the stderr stream, so grab it now. */
	do_error(host->perr[0], host->host);

	/* Potentially, the child is still alive. */
	(void)wait(&status);

	close(host->pin[1]); close(host->pout[0]); close(host->perr[0]);

	log_info("%s: finished backup.\n", host->host);

	if (disk)
		return -1;

	return 0;
}

/*
 * Parse buffer into fields appropriate for diskinfo.
 */
int
add_disk(char *buffer, hostinfo_t *host)
{
	char word[MAXLINE];
	diskinfo_t *disk;
	char *p = buffer;

	disk = malloc(sizeof(diskinfo_t));
	if (!disk) {
		log_err("Master: couldn't allocate %d bytes.\n",
			sizeof(diskinfo_t));
		return -1;
	}

	disk->vol[0] = '\0';
	disk->dumplevel = 0;
	disk->size = 0;
	disk->authtype = RSH;
	disk->next = host->disks;

	while ((p = getword(p, word))) {
		if (!strncmp(word, DISK_NAME, sizeof(word))) {
			p = getword(p, disk->vol);
		} else if (!strncmp(word, DISK_AUTH, sizeof(word))) {
			p = getword(p, word);
			if (!strncmp(word, AUTH_NOAUTH, sizeof(word)))
				disk->authtype = NOAUTH;
			else if (!strncmp(word, AUTH_RSH, sizeof(word)))
				disk->authtype = RSH;
			else {
				log_err("Master: recieved AUTH field I don't"
					" recognize from %s: %s.\n",
					host->host, word);
				write(host->pin[1], REQ_NACK "\n",
				      sizeof(REQ_NACK));
				free(disk);
				return -1;
			}
		} else if (!strncmp(word, DISK_SIZE, sizeof(word))) {
			p = getword(p, word);
		} else {
			log_err("Master: recieved DISK field I don't"
				" recognize from %s: %s.\n",
				host->host, word);
			write(host->pin[1], REQ_NACK "\n", sizeof(REQ_NACK));
			free(disk);
			return -1;
		}
	}

	write(host->pin[1], REQ_ACK "\n", sizeof(REQ_ACK));
	host->disks = disk;
	return 0;
}

/*
 * Get a list of disks to dump with estimates (if applicable), and
 * a port to use for unauthenticated backups.
 */
int
init_connection(hostinfo_t *host)
{
	char line[MAXLINE];
	ssize_t readb;
	int done = 0;

	if (do_handshake(host->pout[0], host->pin[1], host->host) == -1)
		return -1;

	for (;;) {
		char *p = line;
		char word[MAXLINE];

	READ:
		readb = read(host->pout[0], line, sizeof(line));
		if (readb == -1) {
			if (errno == EINTR)
				goto READ;
			log_err("Master: couldn't read from %s: %s.\n",
				host->host, strerror(errno));
			return -1;
		}
		if (readb == 0) {
			log_err("Master: connection to %s"
				" prematurely closed.\n", host->host);
			return -1;
		}

		line[readb] = '\0';
		p = getword(line, word);
		if (!strncmp(word, PORT_REQ, sizeof(word))) {
			p = getword(p, word);
			if (!p) {
				log_err("Master: recieved a PORT"
					" request without an"
					" argument.\n");
				return -1;
			}
			host->port = atoi(word);
			if (write(host->pin[1], REQ_ACK "\n",
			    sizeof(REQ_ACK)) == -1) {
				log_err("Master: couldn't ACK PORT"
					" request: %s.\n",
					strerror(errno));
				return -1;
			}
			continue;
		}

		if (!strncmp(word, DISK_REQ, sizeof(word))) {
			if (add_disk(p, host) == -1)
				break;
			continue;
		}

		if (!strncmp(word, DUMP_DONE, sizeof(word))) {
			done = 1;
			break;
		}

		if (!strncmp(word, DUMP_ERROR, sizeof(word))) {
			if (*p == '\n')
				p++;
			do_error(host->perr[0], host->host);
			break;
		}

		log_err("Master: bad protocol from %s: recieved"
			" command I don't understand: %s.\n",
			host->host, word);
		break;
	}

	if (!done) {
		log_err("Master: connection to %s prematurely terminated.\n",
			host->host);
		return -1;
	}

	return 0;
}

/*
 * Open a connection to a host, get an estimate, and close the connection.
 */
int
addhost(hostinfo_t **hosts, const char *host)
{
	hostinfo_t *curhost;
	pid_t pid;
	int status;
	struct sigaction sa, opsa, ocsa;

	log_info("Master: getting estimate for %s.\n", host);

	curhost = malloc(sizeof(hostinfo_t));
	if (!curhost) {
		log_err("Master: couldn't allocate %d bytes for %s.\n",
			sizeof(hostinfo_t), host);
		return -1;
	}

	strncpy(curhost->host, host, sizeof(curhost->host));
	curhost->port = 0; curhost->socket = 0; curhost->disks = NULL;

	if (pipe(curhost->pin) == -1 || pipe(curhost->pout) == -1 ||
	    pipe(curhost->perr) == -1) {
		log_err("Master: couldn't allocate pipes for %s: %s.\n",
			host, strerror(errno));
		free(curhost);
		return -1;
	}

	/* Set up signal handlers. */
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = sig_pipe;
	sigaction(SIGPIPE, &sa, &opsa);
	sa.sa_handler = sig_child;
	sigaction(SIGCHLD, &sa, &ocsa);

	/* Now set up the RSH pipe and save file descriptors. */
	pid = fork();
	if (pid < 0){
		free(curhost);
		return -1;
	} else if (!pid) {
		close(curhost->pin[1]);
		close(curhost->pout[0]);
		close(curhost->perr[0]);
		if (dup2(curhost->pin[0], STDIN_FILENO) == -1 ||
		    dup2(curhost->pout[1], STDOUT_FILENO) == -1 ||
		    dup2(curhost->perr[1], STDERR_FILENO) == -1)
			exit(1);

		execlp(PATH_RSH, PATH_RSH, host, "run-estimate", NULL);
		err("Couldn't exec run-dump: %s.\n", strerror(errno));
		exit(1);
	}
	close(curhost->pin[0]);
	close(curhost->pout[1]);
	close(curhost->perr[1]);

	/* Figure out what the client wants dumped and how. */
	if (init_connection(curhost) == -1) {
		free(curhost);
		kill(pid, SIGHUP);
		return -1;
	}

	/* Close the RSH connection. */
	close(curhost->pin[1]);
	close(curhost->pout[0]);
	close(curhost->perr[0]);

	/* Put the old handlers back. */
	sigaction(SIGCHLD, &ocsa, NULL);
	sigaction(SIGPIPE, &opsa, NULL);

	/*
	 * Potentially, the child has died after clobbering the signal
	 * handler, so hang out here and wait for it.
	 */

	/* TODO: check out exist status. */
	(void)wait(&status);

	/* Finished cleanly, add to list. */
	curhost->next = *hosts;
	*hosts = curhost;

	return 0;
}

int
main(int argc, char *argv[])
{
	FILE *host_list;
	const config_t *option;
	hostinfo_t *hosts = NULL, *curhost;
	char *spooldir;
	char infofilen[MAXPATHLEN], errorfilen[MAXPATHLEN];
	char host[MAXHOSTNAMELEN];
	int i = 0, retries, status;
	pid_t taperpid, pid;
	long timeout;

	if (read_config(SERVER_CONFIG_FILE) == -1)
		return 1;

	/*
	 * TODO: if an error occurs causing abnormal termination, call the
	 * reporter anyway.
	 */

	option = findopt(LOGDIR);
	if (!option) {
		fprintf(stderr,
			"Warning: `" LOGDIR "' has not been set,"
			" using %s.\n", getcwd(NULL, MAXPATHLEN));
		strcpy(infofilen, "./");
		strcpy(errorfilen, "./");
	} else {
		strcpy(infofilen, option->strvalue);
		strcat(infofilen, "/");
		strcpy(errorfilen, option->strvalue);
		strcat(errorfilen, "/");
	}

	option = findopt(INFOLOG);
	if (!option) {
		fprintf(stderr,
			"Warning: `" INFOLOG "' has not been set,"
			" using stdout.\n");
		strcpy(infofilen, "-");
	} else
		strcat(infofilen, option->strvalue);

	option = findopt(ERRORLOG);
	if (!option) {
		fprintf(stderr,
			"Warning: `" ERRORLOG "' has not been set,"
			" using stderr.\n");
		strcpy(errorfilen, "-");
	} else
		strcat(errorfilen, option->strvalue);

	initlog(infofilen, errorfilen);

	option = findopt(HOSTLIST);
	if (!option) {
		log_err("Master: hostlist hasn't been specified.\n");
		return 1;
	}
	host_list = fopen(option->strvalue, "r");
	if (!host_list) {
		log_err("Master: couldn't open host list.\n");
		return 2;
	}

	option = findopt(SPOOLDIR);
	if (!option) {
		log_err("Master: spooldir hasn't been specified.\n");
		return 1;
	}
	spooldir = option->strvalue;

	option = findopt(TIMEOUT);
	if (!option) {
		log_err("Master: timeout hasn't been specified.\n");
		return 1;
	}
	timeout = option->numvalue;

	/* TODO: create lock here. */

	/* Initialize host information by contacting said host. */
	while (fgets(host, sizeof(host), host_list)) {
		host[strlen(host)-1] = '\0';

		if (addhost(&hosts, host) == -1)
			log_err("Master: couldn't initiate connection"
				" with %s.\n", host);
	}
	fclose(host_list);

	/*
	 * TODO: do backup scheduling here.
	 */

	/* Start up the taper. */
	if (pipe(taperpipe) == -1) {
		log_err("Master: couldn't create pipes for taper.\n");
		return 1;
	}

	taperpid = fork();
	if (taperpid == -1) {
		log_err("Master: couldn't fork for taper.\n");
		return 1;
	} else if (!taperpid) {
		close(taperpipe[1]);
		dup2(taperpipe[0], STDIN_FILENO);

		(void)execlp(LIBEXECDIR "/taper", "taper", NULL);
		log_err("Master: couldn't exec taper: %s.\n", strerror(errno));
		exit(1);
	}
	close(taperpipe[0]);

	/*
	 * Main backup loop, open up connections to each host and
	 * back them up.
	 */
	setproctitle("Master");
	curhost = hosts;
	while (curhost) {
		retries = 0;
	FORK:
		pid = fork(); i++;
		if (pid < 0) {
			i--;
			if (errno == EAGAIN) {
				if (++retries > MAXRETRIES) {
					log_err("Fatal: couldn't fork, "
						"retries exceeded.\n");
					break;
				}
				log_err("Master: couldn't fork, sleeping.\n");
				sleep(5);
				goto FORK;
			}
			log_err("Master: couldn't fork, dying.\n");
			break;
		} else if (!pid) {
			setproctitle("Running backup of %s", curhost->host);

			if (do_backup(curhost, spooldir, timeout)) {
				log_err("Master: error occurred while"
					" backing up %s.\n", curhost->host);
				exit(2);
			}
			exit(0);
		}
		curhost = curhost->next;
	}

	/* Now hang out and wait for everything to finish up. */
	while (i) {
		if (wait(&status) == taperpid) {
			log_err("Master: taper died!\n");
		} else
			i--;
	}

	log_info("Master: waiting for taper to finish.\n");

	/* Allow these things to fail silently when the taper is dead. */
	(void)write(taperpipe[1], DUMP_DONE "\n", sizeof(DUMP_DONE)+1);
	(void)waitpid(taperpid, &status, 0);

	/* Now fire off something to generate and send reports. */
	switch (vfork()) {
	case 0:
		(void)execlp(LIBEXECDIR "/report", "report", NULL);
		perror("Error: couldn't exec report generator");
		_exit(3);
	case -1:
		perror("Couldn't spawn report generator");
		return 1;
	}

	/* TODO: unlock everything. */

	return 0;
}