2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
26 /* $Id: amidxtaped.c,v 1.73 2006/07/25 19:06:46 martinea Exp $
28 * This daemon extracts a dump image off a tape for amrecover and
29 * returns it over the network. It basically, reads a number of
30 * arguments from stdin (it is invoked via inet), one per line,
31 * preceeded by the number of them, and forms them into an argv
32 * structure, then execs amrestore
44 #include "amfeatures.h"
50 static char *pgm = "amidxtaped"; /* in case argv[0] is not set */
52 extern char *rst_conf_logfile;
53 extern char *config_dir;
55 static int get_lock = 0;
56 static int from_amandad;
58 static am_feature_t *our_features = NULL;
59 static am_feature_t *their_features = NULL;
60 static g_option_t *g_options = NULL;
61 static int ctlfdin, ctlfdout, datafdout;
62 static char *amandad_auth = NULL;
64 static char *get_client_line(void);
65 static void check_security_buffer(char *);
66 static char *get_client_line_fd(int);
69 static pid_t parent_pid = -1;
70 static void cleanup(void);
72 int main(int argc, char **argv);
74 /* get a line from client - line terminated by \r\n */
78 static char *line = NULL;
84 if((part = agets(stdin)) == NULL) {
86 dbprintf(("%s: read error: %s\n",
87 debug_prefix_time(NULL), strerror(errno)));
89 dbprintf(("%s: EOF reached\n", debug_prefix_time(NULL)));
92 dbprintf(("%s: unprocessed input:\n", debug_prefix_time(NULL)));
93 dbprintf(("-----\n"));
94 dbprintf(("%s\n", line));
95 dbprintf(("-----\n"));
104 strappend(line, part);
110 if((len = strlen(line)) > 0 && line[len-1] == '\r') {
111 line[len-1] = '\0'; /* zap the '\r' */
115 * Hmmm. We got a "line" from agets(), which means it saw
116 * a '\n' (or EOF, etc), but there was not a '\r' before it.
117 * Put a '\n' back in the buffer and loop for more.
119 strappend(line, "\n");
121 dbprintf(("%s: > %s\n", debug_prefix_time(NULL), line));
125 /* get a line from client - line terminated by \r\n */
130 static char *line = NULL;
131 static size_t line_size = 0;
137 if(line == NULL) { /* first time only, allocate initial buffer */
138 s = line = alloc(128);
142 nb = read(fd, &c, 1);
145 if ((nb <= 0) && ((errno == EINTR) || (errno == EAGAIN))) {
146 /* Keep looping if failure is temporary */
149 dbprintf(("%s: Control pipe read error - %s\n",
150 pgm, strerror(errno)));
154 if(len >= line_size-1) { /* increase buffer size */
156 line = realloc(line, line_size);
158 error("Memory reallocation failure");
165 if(len > 0 && *(s-1) == '\r') { /* remove '\r' */
181 check_security_buffer(
185 struct sockaddr_in addr;
189 dbprintf(("%s: check_security_buffer(buffer='%s')\n",
190 debug_prefix(NULL), buffer));
193 if (getpeername(0, (struct sockaddr *)&addr, &i) == -1) {
194 error("getpeername: %s", strerror(errno));
197 if ((addr.sin_family != (sa_family_t)AF_INET)
198 || (ntohs(addr.sin_port) == 20)) {
199 error("connection rejected from %s family %d port %d",
200 inet_ntoa(addr.sin_addr), addr.sin_family, htons(addr.sin_port));
204 /* do the security thing */
208 skip_whitespace(s, ch);
210 error("cannot parse SECURITY line");
214 skip_non_whitespace(s, ch);
216 if (strcmp(fp, "SECURITY") != 0) {
217 error("cannot parse SECURITY line");
220 skip_whitespace(s, ch);
221 if (!check_security(&addr, s-1, 0, &errstr)) {
222 error("security check failed: %s", errstr);
234 in_port_t data_port = (in_port_t)-1;
236 struct sockaddr_in addr;
237 match_list_t *match_list;
238 tapelist_t *tapes = NULL;
239 char *their_feature_string = NULL;
240 rst_flags_t *rst_flags;
242 FILE *prompt_stream = NULL;
244 char *re_config = NULL;
249 safe_fd(DATA_FD_OFFSET, 4);
252 /* Don't die when child closes pipe */
253 signal(SIGPIPE, SIG_IGN);
255 rst_flags = new_rst_flags();
256 rst_flags->mask_splits = 1; /* for older clients */
257 rst_flags->amidxtaped = 1;
258 our_features = am_init_feature_set();
259 their_features = am_set_default_feature_set();
262 * When called via inetd, it is not uncommon to forget to put the
263 * argv[0] value on the config line. On some systems (e.g. Solaris)
264 * this causes argv and/or argv[0] to be NULL, so we have to be
265 * careful getting our name.
267 if (argc >= 1 && argv != NULL && argv[0] != NULL) {
268 if((pgm = strrchr(argv[0], '/')) != NULL) {
277 if(argv && argv[1] && strcmp(argv[1], "amandad") == 0) {
280 amandad_auth = argv[2];
289 /* we'd rather not run as root */
292 if(client_uid == (uid_t) -1) {
293 error("error [cannot find user %s in passwd file]\n", CLIENT_LOGIN);
298 initgroups(CLIENT_LOGIN, client_gid);
304 #endif /* FORCE_USERID */
307 /* close stderr first so that debug file becomes it - amrestore
308 chats to stderr, which we don't want going to client */
309 /* if no debug file, ship to bit bucket */
310 (void)close(STDERR_FILENO);
311 dbopen(DBG_SUBDIR_SERVER);
313 dbprintf(("%s: version %s\n", pgm, version()));
315 if(dbfd() != -1 && dbfd() != STDERR_FILENO)
317 if(dup2(dbfd(),STDERR_FILENO) != STDERR_FILENO)
319 perror("amidxtaped can't redirect stderr to the debug file");
320 dbprintf(("%s: can't redirect stderr to the debug file\n",
321 debug_prefix_time(NULL)));
326 if ((i = open("/dev/null", O_WRONLY)) == -1 ||
327 (i != STDERR_FILENO &&
328 (dup2(i, STDERR_FILENO) != STDERR_FILENO ||
330 perror("amidxtaped can't redirect stderr");
335 if (! (argc >= 1 && argv != NULL && argv[0] != NULL)) {
336 dbprintf(("%s: WARNING: argv[0] not defined: check inetd.conf\n",
337 debug_prefix_time(NULL)));
340 if(from_amandad == 0) {
341 socklen = SIZEOF(addr);
342 if (getpeername(0, (struct sockaddr *)&addr, &socklen) == -1) {
343 error("getpeername: %s", strerror(errno));
346 if ((addr.sin_family != (sa_family_t)AF_INET)
347 || (ntohs(addr.sin_port) == 20)) {
348 error("connection rejected from %s family %d port %d",
349 inet_ntoa(addr.sin_addr), addr.sin_family,
350 htons(addr.sin_port));
354 /* do the security thing */
356 buf = stralloc(get_client_line());
357 check_security_buffer(buf);
360 ctlfdout = DATA_FD_OFFSET + 0;
361 ctlfdin = DATA_FD_OFFSET + 1;
362 datafdout = DATA_FD_OFFSET + 2;
363 close(DATA_FD_OFFSET +3);
365 /* read the REQ packet */
366 for(; (line = agets(stdin)) != NULL; free(line)) {
367 #define sc "OPTIONS "
368 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
370 g_options = parse_g_options(line+8, 1);
371 if(!g_options->hostname) {
372 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
373 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
374 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
380 if(amandad_auth && g_options->auth) {
381 if(strcasecmp(amandad_auth, g_options->auth) != 0) {
382 printf("ERROR recover program ask for auth=%s while amidxtaped is configured for '%s'\n",
383 g_options->auth, amandad_auth);
384 error("ERROR recover program ask for auth=%s while amidxtaped is configured for '%s'",
385 g_options->auth, amandad_auth);
389 /* send the REP packet */
390 printf("CONNECT CTL %d DATA %d\n", DATA_FD_OFFSET, DATA_FD_OFFSET+1);
394 if ((dup2(ctlfdout, fileno(stdout)) < 0)
395 || (dup2(ctlfdin, fileno(stdin)) < 0)) {
396 error("amandad: Failed to setup stdin or stdout");
401 /* get the number of arguments */
402 match_list = alloc(SIZEOF(match_list_t));
403 match_list->next = NULL;
404 match_list->hostname = "";
405 match_list->datestamp = "";
406 match_list->level = "";
407 match_list->diskname = "";
409 for (re_end = 0; re_end == 0; ) {
411 buf = stralloc(get_client_line());
412 if(strncmp(buf, "LABEL=", 6) == 0) {
413 tapes = unmarshal_tapelist_str(buf+6);
415 else if(strncmp(buf, "FSF=", 4) == 0) {
416 rst_flags->fsf = OFF_T_ATOI(buf + 4);
418 else if(strncmp(buf, "HEADER", 6) == 0) {
419 rst_flags->headers = 1;
421 else if(strncmp(buf, "FEATURES=", 9) == 0) {
422 char *our_feature_string = NULL;
423 their_feature_string = stralloc(buf+9);
424 am_release_feature_set(their_features);
425 their_features = am_string_to_feature(their_feature_string);
426 amfree(their_feature_string);
427 our_feature_string = am_feature_to_string(our_features);
428 if(from_amandad == 1)
429 printf("FEATURES=%s\r\n", our_feature_string);
431 printf("%s", our_feature_string);
433 amfree(our_feature_string);
435 else if(strncmp(buf, "DEVICE=", 7) == 0) {
436 rst_flags->alt_tapedev= stralloc(buf+7);
438 else if(strncmp(buf, "HOST=", 5) == 0) {
439 match_list->hostname = stralloc(buf+5);
441 else if(strncmp(buf, "DISK=", 5) == 0) {
442 match_list->diskname = stralloc(buf+5);
444 else if(strncmp(buf, "DATESTAMP=", 10) == 0) {
445 match_list->datestamp = stralloc(buf+10);
447 else if(strncmp(buf, "END", 3) == 0) {
450 else if(strncmp(buf, "CONFIG=", 7) == 0) {
451 re_config = stralloc(buf+7);
453 else if(buf[0] != '\0' && buf[0] >= '0' && buf[0] <= '9') {
454 /* XXX does nothing? amrestore_nargs = atoi(buf); */
460 if(!tapes && rst_flags->alt_tapedev){
461 dbprintf(("%s: Looks like we're restoring from a holding file...\n", debug_prefix_time(NULL)));
462 tapes = unmarshal_tapelist_str(rst_flags->alt_tapedev);
464 amfree(rst_flags->alt_tapedev);
465 rst_flags->alt_tapedev = NULL;
470 config_dir = vstralloc(CONFIG_DIR, "/", re_config, "/", NULL);
471 conffile = stralloc2(config_dir, CONFFILE_NAME);
472 if (read_conffile(conffile)) {
473 dbprintf(("%s: config '%s' not found\n",
474 debug_prefix_time(NULL), re_config));
480 dbrename(config_name, DBG_SUBDIR_SERVER);
484 (!rst_flags->alt_tapedev ||
485 (re_config && ( strcmp(rst_flags->alt_tapedev,
486 getconf_str(CNF_AMRECOVER_CHANGER)) == 0 ||
487 strcmp(rst_flags->alt_tapedev,
488 getconf_str(CNF_TPCHANGER)) == 0 ) ) ) ) {
489 /* We need certain options, if restoring from more than one tape */
490 if(tapes->next && !am_has_feature(their_features, fe_recover_splits)) {
491 error("%s: Client must support split dumps to restore requested data.", get_pname());
494 dbprintf(("%s: Restoring from changer, checking labels\n", get_pname()));
495 rst_flags->check_labels = 1;
499 /* If we'll be stepping on the tape server's devices, lock them. */
501 (use_changer || (rst_flags->alt_tapedev &&
502 strcmp(rst_flags->alt_tapedev,
503 getconf_str(CNF_TAPEDEV)) == 0) ) ) {
504 dbprintf(("%s: Locking devices\n", get_pname()));
505 parent_pid = getpid();
507 get_lock = lock_logfile();
510 /* Init the tape changer */
511 if(tapes && use_changer && changer_init() == 0) {
512 dbprintf(("%s: No changer available\n", debug_prefix_time(NULL)));
515 /* Read the default block size from the tape type */
516 if(re_config && (conf_tapetype = getconf_str(CNF_TAPETYPE)) != NULL) {
517 tape = lookup_tapetype(conf_tapetype);
518 rst_flags->blocksize = tapetype_get_blocksize(tape) * 1024;
521 if(rst_flags->fsf && re_config &&
522 getconf_boolean(CNF_AMRECOVER_DO_FSF) == 0) {
523 rst_flags->fsf = (off_t)0;
526 if (!use_changer && re_config &&
527 getconf_boolean(CNF_AMRECOVER_CHECK_LABEL) == 0) {
528 rst_flags->check_labels = 0;
531 /* establish a distinct data connection for dumpfile data */
532 if(am_has_feature(their_features, fe_recover_splits)) {
533 if(from_amandad == 1) {
534 rst_flags->pipe_to_fd = datafdout;
535 prompt_stream = stdout;
541 dbprintf(("%s: Client understands split dumpfiles\n",get_pname()));
543 if((data_sock = stream_server(&data_port, STREAM_BUFSIZE,
544 STREAM_BUFSIZE, 0)) < 0){
545 error("%s: could not create data socket: %s", get_pname(),
549 dbprintf(("%s: Local port %d set aside for data\n", get_pname(), data_port));
551 /* tell client where to connect */
552 printf("CONNECT %hu\n", (unsigned short)data_port);
555 if((data_fd = stream_accept(data_sock, TIMEOUT, STREAM_BUFSIZE,
556 STREAM_BUFSIZE)) < 0){
557 error("stream_accept failed for client data connection: %s\n",
562 buf = get_client_line_fd(data_fd);
564 check_security_buffer(buf);
565 rst_flags->pipe_to_fd = data_fd;
566 prompt_stream = stdout;
570 rst_flags->pipe_to_fd = fileno(stdout);
571 prompt_stream = stderr;
573 dbprintf(("%s: Sending output to file descriptor %d\n",
574 get_pname(), rst_flags->pipe_to_fd));
579 (use_changer || (rst_flags->alt_tapedev &&
580 strcmp(rst_flags->alt_tapedev,
581 getconf_str(CNF_TAPEDEV)) == 0) ) ) {
582 send_message(prompt_stream, rst_flags, their_features,
583 "%s exists: amdump or amflush is already running, "
584 "or you must run amcleanup",
586 error("%s exists: amdump or amflush is already running, "
587 "or you must run amcleanup",
591 /* make sure our restore flags aren't crazy */
592 if (check_rst_flags(rst_flags) == -1) {
593 if (rst_flags->pipe_to_fd != -1)
594 aclose(rst_flags->pipe_to_fd);
595 send_message(prompt_stream, rst_flags, their_features,
596 "restore flags are crazy");
600 /* actual restoration */
601 search_tapes(prompt_stream, use_changer, tapes, match_list, rst_flags,
603 dbprintf(("%s: Restoration finished\n", debug_prefix_time(NULL)));
606 if(rst_flags->pipe_to_fd != -1) aclose(rst_flags->pipe_to_fd);
607 free_tapelist(tapes);
609 am_release_feature_set(their_features);
611 amfree(rst_flags->alt_tapedev);
613 amfree(match_list->hostname);
614 amfree(match_list->diskname);
615 amfree(match_list->datestamp);
626 if(parent_pid == getpid()) {
627 if(get_lock) unlink(rst_conf_logfile);