2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 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: dumper.c,v 1.190 2006/08/30 19:53:57 martinea Exp $
28 * requests remote amandad processes to dump filesystems
41 #include "fileheader.h"
42 #include "amfeatures.h"
43 #include "server_util.h"
45 #include "timestamp.h"
48 #define dumper_debug(i,x) do { \
49 if ((i) <= debug_dumper) { \
62 #define CONNECT_TIMEOUT 5*60
64 #define STARTUP_TIMEOUT 60
67 int fd; /* file to flush to */
69 char *datain; /* data buffer markers */
72 pid_t compresspid; /* valid if fd is pipe to compress */
73 pid_t encryptpid; /* valid if fd is pipe to encrypt */
76 typedef struct filter_s {
80 gint64 first; /* first byte used */
81 gint64 size; /* number of byte use in the buffer */
82 gint64 allocated_size ; /* allocated size of the buffer */
83 event_handle_t *event;
86 static char *handle = NULL;
88 static char *errstr = NULL;
89 static off_t dumpbytes;
90 static off_t dumpsize, headersize, origsize;
92 static comp_t srvcompress = COMP_NONE;
93 char *srvcompprog = NULL;
94 char *clntcompprog = NULL;
96 static encrypt_t srvencrypt = ENCRYPT_NONE;
97 char *srv_encrypt = NULL;
98 char *clnt_encrypt = NULL;
99 char *srv_decrypt_opt = NULL;
100 char *clnt_decrypt_opt = NULL;
101 static kencrypt_type dumper_kencrypt;
103 static FILE *errf = NULL;
104 static char *hostname = NULL;
105 am_feature_t *their_features = NULL;
106 static char *diskname = NULL;
107 static char *qdiskname = NULL, *b64disk;
108 static char *device = NULL, *b64device;
109 static char *options = NULL;
110 static char *progname = NULL;
111 static char *amandad_path=NULL;
112 static char *client_username=NULL;
113 static char *client_port=NULL;
114 static char *ssh_keys=NULL;
115 static char *auth=NULL;
116 static data_path_t data_path=DATA_PATH_AMANDA;
117 static char *dataport_list = NULL;
119 static char *dumpdate = NULL;
120 static char *dumper_timestamp = NULL;
121 static time_t conf_dtimeout;
122 static int indexfderror;
123 static int set_datafd;
124 static char *dle_str = NULL;
125 static char *errfname = NULL;
126 static int errf_lines = 0;
128 static dumpfile_t file;
132 security_stream_t *fd;
141 #define NSTREAMS (int)(sizeof(streams) / sizeof(streams[0]))
143 static am_feature_t *our_features = NULL;
144 static char *our_feature_string = NULL;
146 /* buffer to keep partial line from the MESG stream */
148 char *buf; /* buffer holding msg data */
149 size_t size; /* size of alloced buffer */
153 /* local functions */
154 int main(int, char **);
155 static int do_dump(struct databuf *);
156 static void check_options(char *);
157 static void xml_check_options(char *optionstr);
158 static void finish_tapeheader(dumpfile_t *);
159 static ssize_t write_tapeheader(int, dumpfile_t *);
160 static void databuf_init(struct databuf *, int);
161 static int databuf_write(struct databuf *, const void *, size_t);
162 static int databuf_flush(struct databuf *);
163 static void process_dumpeof(void);
164 static void process_dumpline(const char *);
165 static void add_msg_data(const char *, size_t);
166 static void parse_info_line(char *);
167 static int log_msgout(logtype_t);
168 static char * dumper_get_security_conf (char *, void *);
170 static int runcompress(int, pid_t *, comp_t, char *);
171 static int runencrypt(int, pid_t *, encrypt_t);
173 static void sendbackup_response(void *, pkt_t *, security_handle_t *);
174 static int startup_dump(const char *, const char *, const char *, int,
175 const char *, const char *, const char *,
176 const char *, const char *, const char *,
177 const char *, const char *);
178 static void stop_dump(void);
180 static void read_indexfd(void *, void *, ssize_t);
181 static void read_datafd(void *, void *, ssize_t);
182 static void read_mesgfd(void *, void *, ssize_t);
183 static void timeout(time_t);
184 static void timeout_callback(void *);
190 char *compmode = NULL;
191 char *compend = NULL;
192 char *encryptmode = NULL;
193 char *encryptend = NULL;
194 char *decryptmode = NULL;
195 char *decryptend = NULL;
197 /* parse the compression option */
198 if (strstr(options, "srvcomp-best;") != NULL)
199 srvcompress = COMP_BEST;
200 else if (strstr(options, "srvcomp-fast;") != NULL)
201 srvcompress = COMP_FAST;
202 else if ((compmode = strstr(options, "srvcomp-cust=")) != NULL) {
203 compend = strchr(compmode, ';');
205 srvcompress = COMP_SERVER_CUST;
207 srvcompprog = stralloc(compmode + strlen("srvcomp-cust="));
210 } else if ((compmode = strstr(options, "comp-cust=")) != NULL) {
211 compend = strchr(compmode, ';');
213 srvcompress = COMP_CUST;
215 clntcompprog = stralloc(compmode + strlen("comp-cust="));
220 srvcompress = COMP_NONE;
224 /* now parse the encryption option */
225 if ((encryptmode = strstr(options, "encrypt-serv-cust=")) != NULL) {
226 encryptend = strchr(encryptmode, ';');
228 srvencrypt = ENCRYPT_SERV_CUST;
230 srv_encrypt = stralloc(encryptmode + strlen("encrypt-serv-cust="));
233 } else if ((encryptmode = strstr(options, "encrypt-cust=")) != NULL) {
234 encryptend = strchr(encryptmode, ';');
236 srvencrypt = ENCRYPT_CUST;
238 clnt_encrypt = stralloc(encryptmode + strlen("encrypt-cust="));
242 srvencrypt = ENCRYPT_NONE;
244 /* get the decryption option parameter */
245 if ((decryptmode = strstr(options, "server-decrypt-option=")) != NULL) {
246 decryptend = strchr(decryptmode, ';');
249 srv_decrypt_opt = stralloc(decryptmode + strlen("server-decrypt-option="));
252 } else if ((decryptmode = strstr(options, "client-decrypt-option=")) != NULL) {
253 decryptend = strchr(decryptmode, ';');
256 clnt_decrypt_opt = stralloc(decryptmode + strlen("client-decrypt-option="));
261 if (strstr(options, "kencrypt;") != NULL) {
262 dumper_kencrypt = KENCRYPT_WILL_DO;
264 dumper_kencrypt = KENCRYPT_NONE;
277 o = oo = vstralloc("<dle>", strchr(optionstr,'<'), "</dle>", NULL);
279 dle = amxml_parse_node_CHAR(o, &errmsg);
281 error("amxml_parse_node_CHAR failed: %s\n", errmsg);
284 if (dle->compress == COMP_SERVER_FAST) {
285 srvcompress = COMP_FAST;
286 } else if (dle->compress == COMP_SERVER_BEST) {
287 srvcompress = COMP_BEST;
288 } else if (dle->compress == COMP_SERVER_CUST) {
289 srvcompress = COMP_SERVER_CUST;
290 srvcompprog = g_strdup(dle->compprog);
291 } else if (dle->compress == COMP_CUST) {
292 srvcompress = COMP_CUST;
293 clntcompprog = g_strdup(dle->compprog);
295 srvcompress = COMP_NONE;
298 if (dle->encrypt == ENCRYPT_CUST) {
299 srvencrypt = ENCRYPT_CUST;
300 clnt_encrypt = g_strdup(dle->clnt_encrypt);
301 clnt_decrypt_opt = g_strdup(dle->clnt_decrypt_opt);
302 } else if (dle->encrypt == ENCRYPT_SERV_CUST) {
303 srvencrypt = ENCRYPT_SERV_CUST;
304 srv_encrypt = g_strdup(dle->srv_encrypt);
305 srv_decrypt_opt = g_strdup(dle->srv_decrypt_opt);
307 srvencrypt = ENCRYPT_NONE;
319 static struct databuf db;
320 struct cmdargs *cmdargs = NULL;
323 in_port_t header_port;
327 config_overrides_t *cfg_ovr = NULL;
328 char *cfg_opt = NULL;
332 * Configure program for internationalization:
333 * 1) Only set the message locale for now.
334 * 2) Set textdomain for all amanda related programs to "amanda"
335 * We don't want to be forced to support dozens of message catalogs.
337 setlocale(LC_MESSAGES, "C");
338 textdomain("amanda");
340 /* drop root privileges */
341 dumper_setuid = set_root_privs(0);
347 dbopen(DBG_SUBDIR_SERVER);
349 /* Don't die when child closes pipe */
350 signal(SIGPIPE, SIG_IGN);
352 add_amanda_log_handler(amanda_log_stderr);
353 add_amanda_log_handler(amanda_log_trace_log);
355 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
358 set_config_overrides(cfg_ovr);
359 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
361 if (!dumper_setuid) {
362 error(_("dumper must be run setuid root"));
365 if (config_errors(NULL) >= CFGERR_ERRORS) {
366 g_critical(_("errors processing config file"));
369 safe_cd(); /* do this *after* config_init() */
371 check_running_as(RUNNING_AS_ROOT | RUNNING_AS_UID_ONLY);
373 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
375 our_features = am_init_feature_set();
376 our_feature_string = am_feature_to_string(our_features);
378 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
380 _("%s: pid %ld executable %s version %s\n"),
381 get_pname(), (long) getpid(),
385 /* now, make sure we are a valid user */
387 signal(SIGPIPE, SIG_IGN);
389 conf_dtimeout = (time_t)getconf_int(CNF_DTIMEOUT);
395 free_cmdargs(cmdargs);
399 switch(cmdargs->cmd) {
401 if(cmdargs->argc < 2)
402 error(_("error [dumper START: not enough args: timestamp]"));
403 dumper_timestamp = newstralloc(dumper_timestamp, cmdargs->argv[1]);
433 a = 1; /* skip "PORT-DUMP" */
435 if(a >= cmdargs->argc) {
436 error(_("error [dumper PORT-DUMP: not enough args: handle]"));
439 handle = newstralloc(handle, cmdargs->argv[a++]);
441 if(a >= cmdargs->argc) {
442 error(_("error [dumper PORT-DUMP: not enough args: port]"));
445 header_port = (in_port_t)atoi(cmdargs->argv[a++]);
447 if(a >= cmdargs->argc) {
448 error(_("error [dumper PORT-DUMP: not enough args: hostname]"));
451 hostname = newstralloc(hostname, cmdargs->argv[a++]);
453 if(a >= cmdargs->argc) {
454 error(_("error [dumper PORT-DUMP: not enough args: features]"));
457 am_release_feature_set(their_features);
458 their_features = am_string_to_feature(cmdargs->argv[a++]);
460 if(a >= cmdargs->argc) {
461 error(_("error [dumper PORT-DUMP: not enough args: diskname]"));
464 diskname = newstralloc(diskname, cmdargs->argv[a++]);
465 if (qdiskname != NULL)
467 qdiskname = quote_string(diskname);
468 b64disk = amxml_format_tag("disk", diskname);
470 if(a >= cmdargs->argc) {
471 error(_("error [dumper PORT-DUMP: not enough args: device]"));
474 device = newstralloc(device, cmdargs->argv[a++]);
475 b64device = amxml_format_tag("diskdevice", device);
476 if(strcmp(device,"NODEVICE") == 0)
479 if(a >= cmdargs->argc) {
480 error(_("error [dumper PORT-DUMP: not enough args: level]"));
483 level = atoi(cmdargs->argv[a++]);
485 if(a >= cmdargs->argc) {
486 error(_("error [dumper PORT-DUMP: not enough args: dumpdate]"));
489 dumpdate = newstralloc(dumpdate, cmdargs->argv[a++]);
491 if(a >= cmdargs->argc) {
492 error(_("error [dumper PORT-DUMP: not enough args: program]"));
495 progname = newstralloc(progname, cmdargs->argv[a++]);
497 if(a >= cmdargs->argc) {
498 error(_("error [dumper PORT-DUMP: not enough args: amandad_path]"));
501 amandad_path = newstralloc(amandad_path, cmdargs->argv[a++]);
503 if(a >= cmdargs->argc) {
504 error(_("error [dumper PORT-DUMP: not enough args: client_username]"));
506 client_username = newstralloc(client_username, cmdargs->argv[a++]);
508 if(a >= cmdargs->argc) {
509 error(_("error [dumper PORT-DUMP: not enough args: client_port]"));
511 client_port = newstralloc(client_port, cmdargs->argv[a++]);
513 if(a >= cmdargs->argc) {
514 error(_("error [dumper PORT-DUMP: not enough args: ssh_keys]"));
516 ssh_keys = newstralloc(ssh_keys, cmdargs->argv[a++]);
518 if(a >= cmdargs->argc) {
519 error(_("error [dumper PORT-DUMP: not enough args: auth]"));
521 auth = newstralloc(auth, cmdargs->argv[a++]);
523 if(a >= cmdargs->argc) {
524 error(_("error [dumper PORT-DUMP: not enough args: data_path]"));
526 data_path = data_path_from_string(cmdargs->argv[a++]);
528 if(a >= cmdargs->argc) {
529 error(_("error [dumper PORT-DUMP: not enough args: dataport_list]"));
531 dataport_list = newstralloc(dataport_list, cmdargs->argv[a++]);
533 if(a >= cmdargs->argc) {
534 error(_("error [dumper PORT-DUMP: not enough args: options]"));
536 options = newstralloc(options, cmdargs->argv[a++]);
538 if(a != cmdargs->argc) {
539 error(_("error [dumper PORT-DUMP: too many args: %d != %d]"),
544 /* Double-check that 'localhost' resolves properly */
545 if ((res = resolve_hostname("localhost", 0, NULL, NULL) != 0)) {
546 errstr = newvstrallocf(errstr,
547 _("could not resolve localhost: %s"),
549 q = quote_string(errstr);
550 putresult(FAILED, "%s %s\n", handle, q);
551 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
552 dumper_timestamp, level, errstr);
557 /* connect outf to chunker/taper port */
559 g_debug(_("Sending header to localhost:%d\n"), header_port);
560 outfd = stream_client("localhost", header_port,
561 STREAM_BUFSIZE, 0, NULL, 0);
564 errstr = newvstrallocf(errstr, _("port open: %s"),
566 q = quote_string(errstr);
567 putresult(FAILED, "%s %s\n", handle, q);
568 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
569 dumper_timestamp, level, errstr);
573 databuf_init(&db, outfd);
575 if (am_has_feature(their_features, fe_req_xml))
576 xml_check_options(options); /* note: modifies globals */
578 check_options(options); /* note: modifies globals */
580 rc = startup_dump(hostname,
593 q = quote_string(errstr);
594 putresult(rc == 2? FAILED : TRYAGAIN, "%s %s\n",
597 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
598 dumper_timestamp, level, errstr);
602 /* try to clean up any defunct processes, since Amanda doesn't
603 wait() for them explicitly */
604 while(waitpid(-1, NULL, WNOHANG)> 0);
607 amfree(amandad_path);
608 amfree(client_username);
618 if(cmdargs->argc >= 1) {
619 q = quote_string(cmdargs->argv[0]);
621 q = stralloc(_("(no input?)"));
623 putresult(BAD_COMMAND, "%s\n", q);
630 } while(cmdargs->cmd != QUIT);
631 free_cmdargs(cmdargs);
633 log_add(L_INFO, "pid-done %ld", (long)getpid());
635 am_release_feature_set(our_features);
636 amfree(our_feature_string);
638 amfree(dumper_timestamp);
647 amfree(clntcompprog);
649 amfree(clnt_encrypt);
650 amfree(srv_decrypt_opt);
651 amfree(clnt_decrypt_opt);
655 return (0); /* exit */
660 * Initialize a databuf. Takes a writeable file descriptor.
669 db->datain = db->dataout = db->datalimit = NULL;
670 db->compresspid = -1;
676 * Updates the buffer pointer for the input data buffer. The buffer is
677 * written regardless of how much data is present, since we know we
678 * are writing to a socket (to chunker) and there is no need to maintain
687 db->buf = (char *)buf;
688 db->datain = db->datalimit = db->buf + size;
689 db->dataout = db->buf;
690 return databuf_flush(db);
694 * Write out the buffer to chunker.
704 * If there's no data, do nothing.
706 if (db->dataout >= db->datain) {
711 * Write out the buffer
713 written = full_write(db->fd, db->dataout,
714 (size_t)(db->datain - db->dataout));
716 db->dataout += written;
717 dumpbytes += (off_t)written;
719 if (dumpbytes >= (off_t)1024) {
720 dumpsize += (dumpbytes / (off_t)1024);
721 dumpbytes %= (off_t)1024;
724 int save_errno = errno;
725 m = vstrallocf(_("data write: %s"), strerror(save_errno));
727 errstr = quote_string(m);
732 db->datain = db->dataout = db->buf;
736 static int dump_result;
738 #define GOT_INFO_ENDLINE (1 << 0)
739 #define GOT_SIZELINE (1 << 1)
740 #define GOT_ENDLINE (1 << 2)
741 #define HEADER_DONE (1 << 3)
745 process_dumpeof(void)
747 /* process any partial line in msgbuf? !!! */
748 add_msg_data(NULL, 0);
749 if(!ISSET(status, GOT_SIZELINE) && dump_result < 2) {
750 /* make a note if there isn't already a failure */
752 _("? %s: strange [missing size line from sendbackup]\n"),
755 errstr = stralloc(_("missing size line from sendbackup"));
757 dump_result = max(dump_result, 2);
760 if(!ISSET(status, GOT_ENDLINE) && dump_result < 2) {
762 _("? %s: strange [missing end line from sendbackup]\n"),
765 errstr = stralloc(_("missing end line from sendbackup"));
767 dump_result = max(dump_result, 2);
772 * Parse an information line from the client.
773 * We ignore unknown parameters and only remember the last
780 static const struct {
785 { "BACKUP", file.program, SIZEOF(file.program) },
786 { "APPLICATION", file.application, SIZEOF(file.application) },
787 { "RECOVER_CMD", file.recover_cmd, SIZEOF(file.recover_cmd) },
788 { "COMPRESS_SUFFIX", file.comp_suffix, SIZEOF(file.comp_suffix) },
789 { "SERVER_CUSTOM_COMPRESS", file.srvcompprog, SIZEOF(file.srvcompprog) },
790 { "CLIENT_CUSTOM_COMPRESS", file.clntcompprog, SIZEOF(file.clntcompprog) },
791 { "SERVER_ENCRYPT", file.srv_encrypt, SIZEOF(file.srv_encrypt) },
792 { "CLIENT_ENCRYPT", file.clnt_encrypt, SIZEOF(file.clnt_encrypt) },
793 { "SERVER_DECRYPT_OPTION", file.srv_decrypt_opt, SIZEOF(file.srv_decrypt_opt) },
794 { "CLIENT_DECRYPT_OPTION", file.clnt_decrypt_opt, SIZEOF(file.clnt_decrypt_opt) }
799 if (strcmp(str, "end") == 0) {
800 SET(status, GOT_INFO_ENDLINE);
804 name = strtok(str, "=");
807 value = strtok(NULL, "");
811 for (i = 0; i < SIZEOF(fields) / SIZEOF(fields[0]); i++) {
812 if (strcmp(name, fields[i].name) == 0) {
813 strncpy(fields[i].value, value, fields[i].len - 1);
814 fields[i].value[fields[i].len - 1] = '\0';
830 /* normal backup output line */
833 /* sendbackup detected something strange */
834 dump_result = max(dump_result, 1);
837 /* a sendbackup line, just check them all since there are only 5 */
838 tok = strtok(buf, " ");
839 if (tok == NULL || strcmp(tok, "sendbackup:") != 0)
842 tok = strtok(NULL, " ");
846 if (strcmp(tok, "start") == 0) {
850 if (strcmp(tok, "size") == 0) {
851 tok = strtok(NULL, "");
853 origsize = OFF_T_ATOI(tok);
854 SET(status, GOT_SIZELINE);
859 if (strcmp(tok, "no-op") == 0) {
864 if (strcmp(tok, "end") == 0) {
865 SET(status, GOT_ENDLINE);
869 if (strcmp(tok, "warning") == 0) {
870 dump_result = max(dump_result, 1);
874 if (strcmp(tok, "error") == 0) {
875 SET(status, GOT_ENDLINE);
876 dump_result = max(dump_result, 2);
878 tok = strtok(NULL, "");
879 if (!errstr) { /* report first error line */
880 if (tok == NULL || *tok != '[') {
881 errstr = newvstrallocf(errstr, _("bad remote error: %s"),
886 tok++; /* skip over '[' */
887 if ((enderr = strchr(tok, ']')) != NULL)
889 errstr = newstralloc(errstr, tok);
895 if (strcmp(tok, "info") == 0) {
896 tok = strtok(NULL, "");
898 parse_info_line(tok);
901 /* else we fall through to bad line */
905 g_fprintf(errf, "??");
906 dump_result = max(dump_result, 1);
909 g_fprintf(errf, "%s\n", str);
923 buflen = strlen(msg.buf);
928 * If our argument is NULL, then we need to flush out any remaining
934 g_fprintf(errf,_("? %s: error [partial line in msgbuf: %zu bytes]\n"),
935 get_pname(), buflen);
936 g_fprintf(errf,_("? %s: error [partial line in msgbuf: \"%s\"]\n"),
937 get_pname(), msg.buf);
943 * Expand the buffer if it can't hold the new contents.
945 if ((buflen + len + 1) > msg.size) {
949 /* round up to next y, where y is a power of 2 */
950 #define ROUND(x, y) (((x) + (y) - 1) & ~((y) - 1))
952 newsize = ROUND(buflen + (ssize_t)len + 1, 256);
953 newbuf = alloc(newsize);
955 if (msg.buf != NULL) {
956 strncpy(newbuf, msg.buf, newsize);
965 * If there was a partial line from the last call, then
966 * append the new data to the end.
968 strncat(msg.buf, str, len);
971 * Process all lines in the buffer
972 * scanning line for unqouted newline.
974 for (ch = line = msg.buf; *ch != '\0'; ch++) {
977 * Found a newline. Terminate and process line.
980 process_dumpline(line);
986 * If we did not process all of the data, move it to the front
987 * of the buffer so it is there next time.
990 buflen = strlen(line);
991 memmove(msg.buf, line, (size_t)buflen + 1);
1006 if (fseeko(errf, 0L, SEEK_SET) < 0) {
1007 dbprintf(_("log_msgout: warning - seek failed: %s\n"), strerror(errno));
1009 while ((line = agets(errf)) != NULL) {
1010 if (errf_lines >= 100 && count >= 20)
1012 if (line[0] != '\0') {
1013 log_add(typ, "%s", line);
1020 if (errf_lines >= 100) {
1021 log_add(typ, "Look in the '%s' file for full error messages", errfname);
1024 return errf_lines < 100;
1030 * Fill in the rest of the tape header
1037 assert(ISSET(status, HEADER_DONE));
1039 file->type = F_DUMPFILE;
1040 strncpy(file->datestamp, dumper_timestamp, sizeof(file->datestamp) - 1);
1041 strncpy(file->name, hostname, SIZEOF(file->name) - 1);
1042 strncpy(file->disk, diskname, SIZEOF(file->disk) - 1);
1043 file->dumplevel = level;
1044 file->blocksize = DISK_BLOCK_BYTES;
1047 * If we're doing the compression here, we need to override what
1048 * sendbackup told us the compression was.
1050 if (srvcompress != COMP_NONE) {
1051 file->compressed = 1;
1052 #ifndef UNCOMPRESS_OPT
1053 #define UNCOMPRESS_OPT ""
1055 if (srvcompress == COMP_SERVER_CUST) {
1056 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1057 " %s %s |", srvcompprog, "-d");
1058 strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1);
1059 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1060 strncpy(file->srvcompprog, srvcompprog, SIZEOF(file->srvcompprog) - 1);
1061 file->srvcompprog[SIZEOF(file->srvcompprog) - 1] = '\0';
1062 } else if ( srvcompress == COMP_CUST ) {
1063 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1064 " %s %s |", clntcompprog, "-d");
1065 strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1);
1066 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1067 strncpy(file->clntcompprog, clntcompprog, SIZEOF(file->clntcompprog));
1068 file->clntcompprog[SIZEOF(file->clntcompprog) - 1] = '\0';
1070 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1071 " %s %s |", UNCOMPRESS_PATH, UNCOMPRESS_OPT);
1072 strncpy(file->comp_suffix, COMPRESS_SUFFIX,SIZEOF(file->comp_suffix) - 1);
1073 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1076 if (file->comp_suffix[0] == '\0') {
1077 file->compressed = 0;
1078 assert(SIZEOF(file->comp_suffix) >= 2);
1079 strncpy(file->comp_suffix, "N", SIZEOF(file->comp_suffix) - 1);
1080 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1082 file->compressed = 1;
1085 /* take care of the encryption header here */
1086 if (srvencrypt != ENCRYPT_NONE) {
1088 if (srvencrypt == ENCRYPT_SERV_CUST) {
1089 if (srv_decrypt_opt) {
1090 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1091 " %s %s |", srv_encrypt, srv_decrypt_opt);
1092 strncpy(file->srv_decrypt_opt, srv_decrypt_opt, SIZEOF(file->srv_decrypt_opt) - 1);
1093 file->srv_decrypt_opt[SIZEOF(file->srv_decrypt_opt) - 1] = '\0';
1095 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1096 " %s |", srv_encrypt);
1097 file->srv_decrypt_opt[0] = '\0';
1099 strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1);
1100 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1101 strncpy(file->srv_encrypt, srv_encrypt, SIZEOF(file->srv_encrypt) - 1);
1102 file->srv_encrypt[SIZEOF(file->srv_encrypt) - 1] = '\0';
1103 } else if ( srvencrypt == ENCRYPT_CUST ) {
1104 if (clnt_decrypt_opt) {
1105 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1106 " %s %s |", clnt_encrypt, clnt_decrypt_opt);
1107 strncpy(file->clnt_decrypt_opt, clnt_decrypt_opt,
1108 SIZEOF(file->clnt_decrypt_opt));
1109 file->clnt_decrypt_opt[SIZEOF(file->clnt_decrypt_opt) - 1] = '\0';
1111 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1112 " %s |", clnt_encrypt);
1113 file->clnt_decrypt_opt[0] = '\0';
1115 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1116 " %s %s |", clnt_encrypt, clnt_decrypt_opt);
1117 strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1);
1118 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1119 strncpy(file->clnt_encrypt, clnt_encrypt, SIZEOF(file->clnt_encrypt) - 1);
1120 file->clnt_encrypt[SIZEOF(file->clnt_encrypt) - 1] = '\0';
1123 if (file->encrypt_suffix[0] == '\0') {
1124 file->encrypted = 0;
1125 assert(SIZEOF(file->encrypt_suffix) >= 2);
1126 strncpy(file->encrypt_suffix, "N", SIZEOF(file->encrypt_suffix) - 1);
1127 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1133 file->dle_str = stralloc(dle_str);
1135 file->dle_str = NULL;
1139 * Send an Amanda dump header to the output file.
1149 if (debug_dumper > 1)
1150 dump_dumpfile_t(file);
1151 buffer = build_header(file, NULL, DISK_BLOCK_BYTES);
1152 if (!buffer) /* this shouldn't happen */
1153 error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES);
1155 written = full_write(outfd, buffer, DISK_BLOCK_BYTES);
1157 if(written == DISK_BLOCK_BYTES)
1169 char *indexfile_tmp = NULL;
1170 char *indexfile_real = NULL;
1171 char level_str[NUM_STR_SIZE];
1176 double dumptime; /* Time dump took in secs */
1177 pid_t indexpid = -1;
1183 if (msg.buf) msg.buf[0] = '\0'; /* reset msg buffer */
1186 dumpbytes = dumpsize = headersize = origsize = (off_t)0;
1189 g_snprintf(level_str, SIZEOF(level_str), "%d", level);
1190 time_str = get_timestamp_from_time(0);
1191 fn = sanitise_filename(diskname);
1193 errfname = newvstralloc(errfname,
1195 "/log.error", NULL);
1196 mkdir(errfname, 0700);
1197 errfname = newvstralloc(errfname,
1199 "/log.error/", hostname,
1207 if((errf = fopen(errfname, "w+")) == NULL) {
1208 errstr = newvstrallocf(errstr, "errfile open \"%s\": %s",
1209 errfname, strerror(errno));
1214 if (streams[INDEXFD].fd != NULL) {
1215 indexfile_real = getindexfname(hostname, diskname, dumper_timestamp, level);
1216 indexfile_tmp = stralloc2(indexfile_real, ".tmp");
1218 if (mkpdir(indexfile_tmp, 0755, (uid_t)-1, (gid_t)-1) == -1) {
1219 errstr = newvstrallocf(errstr,
1220 _("err create %s: %s"),
1223 amfree(indexfile_real);
1224 amfree(indexfile_tmp);
1227 indexout = open(indexfile_tmp, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1228 if (indexout == -1) {
1229 errstr = newvstrallocf(errstr, _("err open %s: %s"),
1230 indexfile_tmp, strerror(errno));
1233 if (runcompress(indexout, &indexpid, COMP_BEST, "index compress") < 0) {
1240 * Schedule the indexfd for relaying to the index file
1242 security_stream_read(streams[INDEXFD].fd, read_indexfd, &indexout);
1246 * We only need to process messages initially. Once we have done
1247 * the header, we will start processing data too.
1249 security_stream_read(streams[MESGFD].fd, read_mesgfd, db);
1253 * Setup a read timeout
1255 timeout(conf_dtimeout);
1258 * Start the event loop. This will exit when all three events
1259 * (read the mesgfd, read the datafd, and timeout) are removed.
1263 if (!ISSET(status, HEADER_DONE)) {
1264 dump_result = max(dump_result, 2);
1265 if (!errstr) errstr = stralloc(_("got no header information"));
1268 dumpsize -= headersize; /* don't count the header */
1269 if (dumpsize <= (off_t)0 && data_path == DATA_PATH_AMANDA) {
1270 dumpsize = (off_t)0;
1271 dump_result = max(dump_result, 2);
1272 if (!errstr) errstr = stralloc(_("got no data"));
1275 if (data_path == DATA_PATH_DIRECTTCP) {
1276 dumpsize = origsize;
1279 if (!ISSET(status, HEADER_DONE)) {
1280 dump_result = max(dump_result, 2);
1281 if (!errstr) errstr = stralloc(_("got no header information"));
1284 if (dumpsize == 0 && data_path == DATA_PATH_AMANDA) {
1285 dump_result = max(dump_result, 2);
1286 if (!errstr) errstr = stralloc(_("got no data"));
1289 if (dump_result > 1)
1292 runtime = stopclock();
1293 dumptime = g_timeval_to_double(runtime);
1296 errstr = alloc(128);
1297 g_snprintf(errstr, 128, _("sec %s kb %lld kps %3.1lf orig-kb %lld"),
1298 walltime_str(runtime),
1299 (long long)dumpsize,
1300 (isnormal(dumptime) ? ((double)dumpsize / (double)dumptime) : 0.0),
1301 (long long)origsize);
1302 m = vstrallocf("[%s]", errstr);
1303 q = quote_string(m);
1305 putresult(DONE, _("%s %lld %lld %lu %s\n"), handle,
1306 (long long)origsize,
1307 (long long)dumpsize,
1308 (unsigned long)((double)dumptime+0.5), q);
1311 switch(dump_result) {
1313 log_add(L_SUCCESS, "%s %s %s %d [%s]", hostname, qdiskname, dumper_timestamp, level, errstr);
1318 log_start_multiline();
1319 log_add(L_STRANGE, "%s %s %d [%s]", hostname, qdiskname, level, errstr);
1320 to_unlink = log_msgout(L_STRANGE);
1321 log_end_multiline();
1334 if (data_path == DATA_PATH_AMANDA)
1337 if (indexfile_tmp) {
1338 amwait_t index_status;
1340 /*@i@*/ aclose(indexout);
1341 waitpid(indexpid,&index_status,0);
1342 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1343 if (rename(indexfile_tmp, indexfile_real) != 0) {
1344 log_add(L_WARNING, _("could not rename \"%s\" to \"%s\": %s"),
1345 indexfile_tmp, indexfile_real, strerror(errno));
1347 amfree(indexfile_tmp);
1348 amfree(indexfile_real);
1351 if(db->compresspid != -1) {
1352 waitpid(db->compresspid,NULL,0);
1353 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1355 if(db->encryptpid != -1) {
1356 waitpid(db->encryptpid,NULL,0);
1357 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1361 dumpfile_free_data(&file);
1366 m = vstrallocf("[%s]", errstr);
1367 q = quote_string(m);
1368 putresult(FAILED, "%s %s\n", handle, q);
1373 /* kill all child process */
1374 if (db->compresspid != -1) {
1375 g_fprintf(stderr,_("%s: kill compress command\n"),get_pname());
1376 if (kill(db->compresspid, SIGTERM) < 0) {
1377 if (errno != ESRCH) {
1378 g_fprintf(stderr,_("%s: can't kill compress command: %s\n"),
1379 get_pname(), strerror(errno));
1381 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1385 waitpid(db->compresspid,NULL,0);
1386 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1390 if (db->encryptpid != -1) {
1391 g_fprintf(stderr,_("%s: kill encrypt command\n"),get_pname());
1392 if (kill(db->encryptpid, SIGTERM) < 0) {
1393 if (errno != ESRCH) {
1394 g_fprintf(stderr,_("%s: can't kill encrypt command: %s\n"),
1395 get_pname(), strerror(errno));
1397 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1401 waitpid(db->encryptpid,NULL,0);
1402 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1406 if (indexpid != -1) {
1407 g_fprintf(stderr,_("%s: kill index command\n"),get_pname());
1408 if (kill(indexpid, SIGTERM) < 0) {
1409 if (errno != ESRCH) {
1410 g_fprintf(stderr,_("%s: can't kill index command: %s\n"),
1411 get_pname(),strerror(errno));
1413 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1417 waitpid(indexpid,NULL,0);
1418 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1422 log_start_multiline();
1423 log_add(L_FAIL, _("%s %s %s %d [%s]"), hostname, qdiskname, dumper_timestamp,
1426 to_unlink = log_msgout(L_FAIL);
1428 log_end_multiline();
1438 if (indexfile_tmp) {
1439 unlink(indexfile_tmp);
1440 amfree(indexfile_tmp);
1441 amfree(indexfile_real);
1448 * Callback for reads on the mesgfd stream
1456 struct databuf *db = cookie;
1462 errstr = newvstrallocf(errstr, _("mesg read: %s"),
1463 security_stream_geterror(streams[MESGFD].fd));
1470 * EOF. Just shut down the mesg stream.
1473 security_stream_close(streams[MESGFD].fd);
1474 streams[MESGFD].fd = NULL;
1476 * If the data fd and index fd has also shut down, then we're done.
1478 if ((set_datafd == 0 || streams[DATAFD].fd == NULL) &&
1479 streams[INDEXFD].fd == NULL)
1484 assert(buf != NULL);
1485 add_msg_data(buf, (size_t)size);
1486 security_stream_read(streams[MESGFD].fd, read_mesgfd, cookie);
1490 if (ISSET(status, GOT_INFO_ENDLINE) && !ISSET(status, HEADER_DONE)) {
1491 /* Use the first in the dataport_list */
1492 in_port_t data_port;
1493 char *data_host = dataport_list;
1494 char *s= strchr(dataport_list, ':');
1497 data_port = atoi(s);
1499 SET(status, HEADER_DONE);
1500 /* time to do the header */
1501 finish_tapeheader(&file);
1502 if (write_tapeheader(db->fd, &file)) {
1503 errstr = newvstrallocf(errstr, _("write_tapeheader: %s"),
1507 dumpfile_free_data(&file);
1510 dumpfile_free_data(&file);
1512 if (data_path == DATA_PATH_AMANDA) {
1513 g_debug(_("Sending data to %s:%d\n"), data_host, data_port);
1514 db->fd = stream_client(data_host, data_port,
1515 STREAM_BUFSIZE, 0, NULL, 0);
1517 errstr = newvstrallocf(errstr,
1518 _("Can't open data output stream: %s"),
1526 dumpsize += (off_t)DISK_BLOCK_KB;
1527 headersize += (off_t)DISK_BLOCK_KB;
1529 if (srvencrypt == ENCRYPT_SERV_CUST) {
1530 if (runencrypt(db->fd, &db->encryptpid, srvencrypt) < 0) {
1537 * Now, setup the compress for the data output, and start
1538 * reading the datafd.
1540 if ((srvcompress != COMP_NONE) && (srvcompress != COMP_CUST)) {
1541 if (runcompress(db->fd, &db->compresspid, srvcompress, "data compress") < 0) {
1547 security_stream_read(streams[DATAFD].fd, read_datafd, db);
1552 * Reset the timeout for future reads
1554 timeout(conf_dtimeout);
1558 * Callback for reads on the datafd stream
1566 struct databuf *db = cookie;
1571 * The read failed. Error out
1574 errstr = newvstrallocf(errstr, _("data read: %s"),
1575 security_stream_geterror(streams[DATAFD].fd));
1582 /* The header had better be written at this point */
1583 assert(ISSET(status, HEADER_DONE));
1586 * EOF. Stop and return.
1590 if (dumpbytes != (off_t)0) {
1591 dumpsize += (off_t)1;
1593 security_stream_close(streams[DATAFD].fd);
1594 streams[DATAFD].fd = NULL;
1597 * If the mesg fd and index fd has also shut down, then we're done.
1599 if (streams[MESGFD].fd == NULL && streams[INDEXFD].fd == NULL)
1605 * We read something. Add it to the databuf and reschedule for
1608 assert(buf != NULL);
1609 if (databuf_write(db, buf, (size_t)size) < 0) {
1610 int save_errno = errno;
1611 errstr = newvstrallocf(errstr, _("data write: %s"), strerror(save_errno));
1618 * Reset the timeout for future reads
1620 timeout(conf_dtimeout);
1622 security_stream_read(streams[DATAFD].fd, read_datafd, cookie);
1626 * Callback for reads on the index stream
1636 assert(cookie != NULL);
1637 fd = *(int *)cookie;
1640 errstr = newvstrallocf(errstr, _("index read: %s"),
1641 security_stream_geterror(streams[INDEXFD].fd));
1648 * EOF. Stop and return.
1651 security_stream_close(streams[INDEXFD].fd);
1652 streams[INDEXFD].fd = NULL;
1654 * If the mesg fd has also shut down, then we're done.
1656 if ((set_datafd == 0 || streams[DATAFD].fd == NULL) &&
1657 streams[MESGFD].fd == NULL)
1663 assert(buf != NULL);
1666 * We ignore error while writing to the index file.
1668 if (full_write(fd, buf, (size_t)size) < (size_t)size) {
1669 /* Ignore error, but schedule another read. */
1670 if(indexfderror == 0) {
1672 log_add(L_INFO, _("Index corrupted for %s:%s"), hostname, qdiskname);
1675 security_stream_read(streams[INDEXFD].fd, read_indexfd, cookie);
1679 handle_filter_stderr(
1682 filter_t *filter = cookie;
1687 event_release(filter->event);
1689 if (filter->buffer == NULL) {
1690 /* allocate initial buffer */
1691 filter->buffer = g_malloc(2048);
1694 filter->allocated_size = 2048;
1695 } else if (filter->first > 0) {
1696 if (filter->allocated_size - filter->size - filter->first < 1024) {
1697 memmove(filter->buffer, filter->buffer + filter->first,
1701 } else if (filter->allocated_size - filter->size < 1024) {
1702 /* double the size of the buffer */
1703 filter->allocated_size *= 2;
1704 filter->buffer = g_realloc(filter->buffer, filter->allocated_size);
1707 nread = read(filter->fd, filter->buffer + filter->first + filter->size,
1708 filter->allocated_size - filter->first - filter->size - 2);
1711 dump_result = max(dump_result, 2);
1716 if (filter->size > 0 && filter->buffer[filter->first + filter->size - 1] != '\n') {
1717 /* Add a '\n' at end of buffer */
1718 filter->buffer[filter->first + filter->size] = '\n';
1722 filter->size += nread;
1725 /* process all complete lines */
1726 b = filter->buffer + filter->first;
1727 filter->buffer[filter->first + filter->size] = '\0';
1728 while (b < filter->buffer + filter->first + filter->size &&
1729 (p = strchr(b, '\n')) != NULL) {
1731 g_fprintf(errf, _("? %s: %s\n"), filter->name, b);
1732 if (errstr == NULL) {
1733 errstr = stralloc(b);
1736 filter->first += len;
1737 filter->size -= len;
1742 g_free(filter->buffer);
1745 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1746 handle_filter_stderr, filter);
1751 * Startup a timeout in the event handler. If the arg is 0,
1752 * then remove the timeout.
1758 static event_handle_t *ev_timeout = NULL;
1761 * First, remove a timeout if one is active.
1763 if (ev_timeout != NULL) {
1764 event_release(ev_timeout);
1769 * Now, schedule a new one if 'seconds' is greater than 0
1772 ev_timeout = event_register((event_id_t)seconds, EV_TIME, timeout_callback, NULL);
1776 * This is the callback for timeout(). If this is reached, then we
1777 * have a data timeout.
1783 (void)unused; /* Quiet unused parameter warning */
1785 assert(unused == NULL);
1786 errstr = newstralloc(errstr, _("data timeout"));
1792 * This is called when everything needs to shut down so event_loop()
1799 struct cmdargs *cmdargs = NULL;
1801 /* Check if I have a pending ABORT command */
1802 cmdargs = get_pending_cmd();
1804 if (cmdargs->cmd != ABORT) {
1805 error(_("beurk %d"), cmdargs->cmd);
1808 errstr = stralloc(cmdargs->argv[1]);
1809 free_cmdargs(cmdargs);
1812 for (i = 0; i < NSTREAMS; i++) {
1813 if (streams[i].fd != NULL) {
1814 security_stream_close(streams[i].fd);
1815 streams[i].fd = NULL;
1824 * Runs compress with the first arg as its stdout. Returns
1825 * 0 on success or negative if error, and it's pid via the second
1826 * argument. The outfd arg is dup2'd to the pipe to the compress
1836 int outpipe[2], rval;
1841 assert(pid != NULL);
1843 /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */
1844 if (pipe(outpipe) < 0) {
1845 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1849 /* errpipe[0] is pipe's output, outpipe[1] is input. */
1850 if (pipe(errpipe) < 0) {
1851 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1855 switch (*pid = fork()) {
1857 errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno));
1864 rval = dup2(outpipe[1], outfd);
1866 errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno));
1870 filter = g_new0(filter_t, 1);
1871 filter->fd = errpipe[0];
1872 filter->name = name;
1873 filter->buffer = NULL;
1875 filter->allocated_size = 0;
1876 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1877 handle_filter_stderr, filter);
1878 g_debug("event register %s %d", name, filter->fd);
1883 if (dup2(outpipe[0], 0) < 0) {
1884 error(_("err dup2 in: %s"), strerror(errno));
1887 if (dup2(outfd, 1) == -1) {
1888 error(_("err dup2 out: %s"), strerror(errno));
1891 if (dup2(errpipe[1], 2) == -1) {
1892 error(_("err dup2 err: %s"), strerror(errno));
1895 if (comptype != COMP_SERVER_CUST) {
1896 char *base = stralloc(COMPRESS_PATH);
1897 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1901 execlp(COMPRESS_PATH, COMPRESS_PATH, ( comptype == COMP_BEST ?
1902 COMPRESS_BEST_OPT : COMPRESS_FAST_OPT), (char *)NULL);
1903 error(_("error: couldn't exec %s: %s"), COMPRESS_PATH, strerror(errno));
1905 } else if (*srvcompprog) {
1906 char *base = stralloc(srvcompprog);
1907 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1911 execlp(srvcompprog, srvcompprog, (char *)0);
1912 error(_("error: couldn't exec server custom compression '%s'.\n"), srvcompprog);
1921 * Runs encrypt with the first arg as its stdout. Returns
1922 * 0 on success or negative if error, and it's pid via the second
1923 * argument. The outfd arg is dup2'd to the pipe to the encrypt
1930 encrypt_t encrypttype)
1932 int outpipe[2], rval;
1937 assert(pid != NULL);
1939 /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */
1940 if (pipe(outpipe) < 0) {
1941 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1945 /* errpipe[0] is pipe's output, outpipe[1] is input. */
1946 if (pipe(errpipe) < 0) {
1947 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1951 switch (*pid = fork()) {
1953 errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno));
1960 rval = dup2(outpipe[1], outfd);
1962 errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno));
1966 filter = g_new0(filter_t, 1);
1967 filter->fd = errpipe[0];
1968 filter->name = "encrypt";
1969 filter->buffer = NULL;
1971 filter->allocated_size = 0;
1972 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1973 handle_filter_stderr, filter);
1974 g_debug("event register %s %d", "encrypt data", filter->fd);
1978 if (dup2(outpipe[0], 0) < 0) {
1979 error(_("err dup2 in: %s"), strerror(errno));
1982 if (dup2(outfd, 1) < 0 ) {
1983 error(_("err dup2 out: %s"), strerror(errno));
1986 if (dup2(errpipe[1], 2) == -1) {
1987 error(_("err dup2 err: %s"), strerror(errno));
1991 base = stralloc(srv_encrypt);
1992 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1995 if ((encrypttype == ENCRYPT_SERV_CUST) && *srv_encrypt) {
1997 execlp(srv_encrypt, srv_encrypt, (char *)0);
1998 error(_("error: couldn't exec server custom encryption '%s'.\n"), srv_encrypt);
2008 /* -------------------- */
2011 sendbackup_response(
2014 security_handle_t * sech)
2016 int ports[NSTREAMS], *response_error = datap, i;
2021 assert(response_error != NULL);
2022 assert(sech != NULL);
2024 security_close_connection(sech, hostname);
2027 errstr = newvstrallocf(errstr, _("[request failed: %s]"),
2028 security_geterror(sech));
2029 *response_error = 1;
2034 memset(ports, 0, SIZEOF(ports));
2035 if (pkt->type == P_NAK) {
2036 #if defined(PACKET_DEBUG)
2037 g_fprintf(stderr, _("got nak response:\n----\n%s\n----\n\n"), pkt->body);
2040 tok = strtok(pkt->body, " ");
2041 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2044 tok = strtok(NULL, "\n");
2046 errstr = newvstrallocf(errstr, "NAK: %s", tok);
2047 *response_error = 1;
2050 errstr = newvstrallocf(errstr, "request NAK");
2051 *response_error = 2;
2056 if (pkt->type != P_REP) {
2057 errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"),
2058 pkt_type2str(pkt->type), pkt->body);
2059 *response_error = 1;
2063 dbprintf(_("got response:\n----\n%s\n----\n\n"), pkt->body);
2065 for(i = 0; i < NSTREAMS; i++) {
2067 streams[i].fd = NULL;
2071 while((tok = strtok(p, " \n")) != NULL) {
2075 * Error response packets have "ERROR" followed by the error message
2076 * followed by a newline.
2078 if (strcmp(tok, "ERROR") == 0) {
2079 tok = strtok(NULL, "\n");
2081 tok = _("[bogus error packet]");
2082 errstr = newvstrallocf(errstr, "%s", tok);
2083 *response_error = 2;
2088 * Regular packets have CONNECT followed by three streams
2090 if (strcmp(tok, "CONNECT") == 0) {
2093 * Parse the three stream specifiers out of the packet.
2095 for (i = 0; i < NSTREAMS; i++) {
2096 tok = strtok(NULL, " ");
2097 if (tok == NULL || strcmp(tok, streams[i].name) != 0) {
2099 _("CONNECT token is \"%s\": expected \"%s\""),
2100 tok ? tok : "(null)",
2104 tok = strtok(NULL, " \n");
2105 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2107 _("CONNECT %s token is \"%s\": expected a port number"),
2108 streams[i].name, tok ? tok : "(null)");
2116 * OPTIONS [options string] '\n'
2118 if (strcmp(tok, "OPTIONS") == 0) {
2119 tok = strtok(NULL, "\n");
2121 extra = vstrallocf(_("OPTIONS token is missing"));
2125 while((p = strchr(tok, ';')) != NULL) {
2128 if(strncmp_const_skip(tok, "features=", tok, ch) == 0) {
2129 char *u = strchr(tok, ';');
2132 am_release_feature_set(their_features);
2133 if((their_features = am_string_to_feature(tok)) == NULL) {
2134 errstr = newvstrallocf(errstr,
2135 _("OPTIONS: bad features value: %s"),
2147 extra = vstrallocf(_("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""),
2148 tok ? tok : "(null)");
2152 if (dumper_kencrypt == KENCRYPT_WILL_DO)
2153 dumper_kencrypt = KENCRYPT_YES;
2156 * Connect the streams to their remote ports
2158 for (i = 0; i < NSTREAMS; i++) {
2161 streams[i].fd = security_stream_client(sech, ports[i]);
2162 if (streams[i].fd == NULL) {
2163 errstr = newvstrallocf(errstr,
2164 _("[could not connect %s stream: %s]"),
2166 security_geterror(sech));
2172 * Authenticate the streams
2174 for (i = 0; i < NSTREAMS; i++) {
2175 if (streams[i].fd == NULL)
2177 if (security_stream_auth(streams[i].fd) < 0) {
2178 errstr = newvstrallocf(errstr,
2179 _("[could not authenticate %s stream: %s]"),
2181 security_stream_geterror(streams[i].fd));
2187 * The MESGFD and DATAFD streams are mandatory. If we didn't get
2190 if (streams[MESGFD].fd == NULL || streams[DATAFD].fd == NULL) {
2191 errstr = newvstrallocf(errstr, _("[couldn't open MESG or INDEX streams]"));
2195 /* everything worked */
2196 *response_error = 0;
2200 errstr = newvstrallocf(errstr,
2201 _("[parse of reply message failed: %s]"),
2202 extra ? extra : _("(no additional information)"));
2204 *response_error = 2;
2209 *response_error = 1;
2213 dumper_get_security_conf(
2217 (void)arg; /* Quiet unused parameter warning */
2219 if(!string || !*string)
2222 if(strcmp(string, "krb5principal")==0) {
2223 return(getconf_str(CNF_KRB5PRINCIPAL));
2224 } else if(strcmp(string, "krb5keytab")==0) {
2225 return(getconf_str(CNF_KRB5KEYTAB));
2226 } else if(strcmp(string, "amandad_path")==0) {
2227 return (amandad_path);
2228 } else if(strcmp(string, "client_username")==0) {
2229 return (client_username);
2230 } else if(strcmp(string, "client_port")==0) {
2231 return (client_port);
2232 } else if(strcmp(string, "ssh_keys")==0) {
2234 } else if(strcmp(string, "kencrypt")==0) {
2235 if (dumper_kencrypt == KENCRYPT_YES)
2245 const char *hostname,
2249 const char *dumpdate,
2250 const char *progname,
2251 const char *amandad_path,
2252 const char *client_username,
2253 const char *client_port,
2254 const char *ssh_keys,
2256 const char *options)
2258 char level_string[NUM_STR_SIZE];
2262 const security_driver_t *secdrv;
2263 char *application_api;
2269 (void)disk; /* Quiet unused parameter warning */
2270 (void)amandad_path; /* Quiet unused parameter warning */
2271 (void)client_username; /* Quiet unused parameter warning */
2272 (void)client_port; /* Quiet unused parameter warning */
2273 (void)ssh_keys; /* Quiet unused parameter warning */
2274 (void)auth; /* Quiet unused parameter warning */
2276 has_features = am_has_feature(their_features, fe_req_options_features);
2277 has_hostname = am_has_feature(their_features, fe_req_options_hostname);
2278 has_config = am_has_feature(their_features, fe_req_options_config);
2279 has_device = am_has_feature(their_features, fe_sendbackup_req_device);
2282 * Default to bsd authentication if none specified. This is gross.
2284 * Options really need to be pre-parsed into some sort of structure
2285 * much earlier, and then flattened out again before transmission.
2288 g_snprintf(level_string, SIZEOF(level_string), "%d", level);
2289 if(strcmp(progname, "DUMP") == 0
2290 || strcmp(progname, "GNUTAR") == 0) {
2291 application_api = "";
2293 application_api = "BACKUP ";
2295 req = vstralloc("SERVICE sendbackup\n",
2297 has_features ? "features=" : "",
2298 has_features ? our_feature_string : "",
2299 has_features ? ";" : "",
2300 has_hostname ? "hostname=" : "",
2301 has_hostname ? hostname : "",
2302 has_hostname ? ";" : "",
2303 has_config ? "config=" : "",
2304 has_config ? get_config_name() : "",
2305 has_config ? ";" : "",
2310 if (am_has_feature(their_features, fe_req_xml)) {
2313 vstrextend(&p, "<dle>\n", NULL);
2314 if (*application_api != '\0') {
2315 vstrextend(&p, " <program>APPLICATION</program>\n", NULL);
2317 vstrextend(&p, " <program>", progname, "</program>\n", NULL);
2319 vstrextend(&p, " ", b64disk, "\n", NULL);
2320 if (device && has_device) {
2321 vstrextend(&p, " ", b64device, "\n",
2324 vstrextend(&p, " <level>", level_string, "</level>\n", NULL);
2325 vstrextend(&p, options+1, "</dle>\n", NULL);
2326 pclean = clean_dle_str_for_client(p);
2327 vstrextend(&req, pclean, NULL);
2330 } else if (*application_api != '\0') {
2331 errstr = newvstrallocf(errstr,
2332 _("[does not support application-api]"));
2336 authopt = strstr(options, "auth=");
2343 " ", device && has_device ? device : "",
2346 " OPTIONS ", options,
2351 dbprintf(_("send request:\n----\n%s\n----\n\n"), req);
2352 secdrv = security_getdriver(auth);
2353 if (secdrv == NULL) {
2354 errstr = newvstrallocf(errstr,
2355 _("[could not find security driver '%s']"), auth);
2360 protocol_sendreq(hostname, secdrv, dumper_get_security_conf, req,
2361 STARTUP_TIMEOUT, sendbackup_response, &response_error);
2366 return (response_error);