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 struct databuf *g_databuf = NULL;
78 typedef struct filter_s {
82 gint64 first; /* first byte used */
83 gint64 size; /* number of byte use in the buffer */
84 gint64 allocated_size ; /* allocated size of the buffer */
85 event_handle_t *event;
88 static char *handle = NULL;
90 static char *errstr = NULL;
91 static off_t dumpbytes;
92 static off_t dumpsize, headersize, origsize;
94 static comp_t srvcompress = COMP_NONE;
95 char *srvcompprog = NULL;
96 char *clntcompprog = NULL;
98 static encrypt_t srvencrypt = ENCRYPT_NONE;
99 char *srv_encrypt = NULL;
100 char *clnt_encrypt = NULL;
101 char *srv_decrypt_opt = NULL;
102 char *clnt_decrypt_opt = NULL;
103 static kencrypt_type dumper_kencrypt;
105 static FILE *errf = NULL;
106 static char *hostname = NULL;
107 am_feature_t *their_features = NULL;
108 static char *diskname = NULL;
109 static char *qdiskname = NULL, *b64disk;
110 static char *device = NULL, *b64device;
111 static char *options = NULL;
112 static char *progname = NULL;
113 static char *amandad_path=NULL;
114 static char *client_username=NULL;
115 static char *client_port=NULL;
116 static char *ssh_keys=NULL;
117 static char *auth=NULL;
118 static data_path_t data_path=DATA_PATH_AMANDA;
119 static char *dataport_list = NULL;
121 static char *dumpdate = NULL;
122 static char *dumper_timestamp = NULL;
123 static time_t conf_dtimeout;
124 static int indexfderror;
125 static int set_datafd;
126 static char *dle_str = NULL;
127 static char *errfname = NULL;
128 static int errf_lines = 0;
129 static int max_warnings = 0;
131 static dumpfile_t file;
135 security_stream_t *fd;
144 #define NSTREAMS (int)(sizeof(streams) / sizeof(streams[0]))
146 static am_feature_t *our_features = NULL;
147 static char *our_feature_string = NULL;
149 /* buffer to keep partial line from the MESG stream */
151 char *buf; /* buffer holding msg data */
152 size_t size; /* size of alloced buffer */
156 /* local functions */
157 int main(int, char **);
158 static int do_dump(struct databuf *);
159 static void check_options(char *);
160 static void xml_check_options(char *optionstr);
161 static void finish_tapeheader(dumpfile_t *);
162 static ssize_t write_tapeheader(int, dumpfile_t *);
163 static void databuf_init(struct databuf *, int);
164 static int databuf_write(struct databuf *, const void *, size_t);
165 static int databuf_flush(struct databuf *);
166 static void process_dumpeof(void);
167 static void process_dumpline(const char *);
168 static void add_msg_data(const char *, size_t);
169 static void parse_info_line(char *);
170 static int log_msgout(logtype_t);
171 static char * dumper_get_security_conf (char *, void *);
173 static int runcompress(int, pid_t *, comp_t, char *);
174 static int runencrypt(int, pid_t *, encrypt_t);
176 static void sendbackup_response(void *, pkt_t *, security_handle_t *);
177 static int startup_dump(const char *, const char *, const char *, int,
178 const char *, const char *, const char *,
179 const char *, const char *, const char *,
180 const char *, const char *);
181 static void stop_dump(void);
183 static void read_indexfd(void *, void *, ssize_t);
184 static void read_datafd(void *, void *, ssize_t);
185 static void read_mesgfd(void *, void *, ssize_t);
186 static void timeout(time_t);
187 static void timeout_callback(void *);
193 char *compmode = NULL;
194 char *compend = NULL;
195 char *encryptmode = NULL;
196 char *encryptend = NULL;
197 char *decryptmode = NULL;
198 char *decryptend = NULL;
200 /* parse the compression option */
201 if (strstr(options, "srvcomp-best;") != NULL)
202 srvcompress = COMP_BEST;
203 else if (strstr(options, "srvcomp-fast;") != NULL)
204 srvcompress = COMP_FAST;
205 else if ((compmode = strstr(options, "srvcomp-cust=")) != NULL) {
206 compend = strchr(compmode, ';');
208 srvcompress = COMP_SERVER_CUST;
210 srvcompprog = stralloc(compmode + strlen("srvcomp-cust="));
213 } else if ((compmode = strstr(options, "comp-cust=")) != NULL) {
214 compend = strchr(compmode, ';');
216 srvcompress = COMP_CUST;
218 clntcompprog = stralloc(compmode + strlen("comp-cust="));
223 srvcompress = COMP_NONE;
227 /* now parse the encryption option */
228 if ((encryptmode = strstr(options, "encrypt-serv-cust=")) != NULL) {
229 encryptend = strchr(encryptmode, ';');
231 srvencrypt = ENCRYPT_SERV_CUST;
233 srv_encrypt = stralloc(encryptmode + strlen("encrypt-serv-cust="));
236 } else if ((encryptmode = strstr(options, "encrypt-cust=")) != NULL) {
237 encryptend = strchr(encryptmode, ';');
239 srvencrypt = ENCRYPT_CUST;
241 clnt_encrypt = stralloc(encryptmode + strlen("encrypt-cust="));
245 srvencrypt = ENCRYPT_NONE;
247 /* get the decryption option parameter */
248 if ((decryptmode = strstr(options, "server-decrypt-option=")) != NULL) {
249 decryptend = strchr(decryptmode, ';');
252 srv_decrypt_opt = stralloc(decryptmode + strlen("server-decrypt-option="));
255 } else if ((decryptmode = strstr(options, "client-decrypt-option=")) != NULL) {
256 decryptend = strchr(decryptmode, ';');
259 clnt_decrypt_opt = stralloc(decryptmode + strlen("client-decrypt-option="));
264 if (strstr(options, "kencrypt;") != NULL) {
265 dumper_kencrypt = KENCRYPT_WILL_DO;
267 dumper_kencrypt = KENCRYPT_NONE;
280 o = oo = vstralloc("<dle>", strchr(optionstr,'<'), "</dle>", NULL);
282 dle = amxml_parse_node_CHAR(o, &errmsg);
284 error("amxml_parse_node_CHAR failed: %s\n", errmsg);
287 if (dle->compress == COMP_SERVER_FAST) {
288 srvcompress = COMP_FAST;
289 } else if (dle->compress == COMP_SERVER_BEST) {
290 srvcompress = COMP_BEST;
291 } else if (dle->compress == COMP_SERVER_CUST) {
292 srvcompress = COMP_SERVER_CUST;
293 srvcompprog = g_strdup(dle->compprog);
294 } else if (dle->compress == COMP_CUST) {
295 srvcompress = COMP_CUST;
296 clntcompprog = g_strdup(dle->compprog);
298 srvcompress = COMP_NONE;
301 if (dle->encrypt == ENCRYPT_CUST) {
302 srvencrypt = ENCRYPT_CUST;
303 clnt_encrypt = g_strdup(dle->clnt_encrypt);
304 clnt_decrypt_opt = g_strdup(dle->clnt_decrypt_opt);
305 } else if (dle->encrypt == ENCRYPT_SERV_CUST) {
306 srvencrypt = ENCRYPT_SERV_CUST;
307 srv_encrypt = g_strdup(dle->srv_encrypt);
308 srv_decrypt_opt = g_strdup(dle->srv_decrypt_opt);
310 srvencrypt = ENCRYPT_NONE;
322 static struct databuf db;
323 struct cmdargs *cmdargs = NULL;
326 in_port_t header_port;
330 config_overrides_t *cfg_ovr = NULL;
331 char *cfg_opt = NULL;
334 if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
335 printf("dumper-%s\n", VERSION);
340 * Configure program for internationalization:
341 * 1) Only set the message locale for now.
342 * 2) Set textdomain for all amanda related programs to "amanda"
343 * We don't want to be forced to support dozens of message catalogs.
345 setlocale(LC_MESSAGES, "C");
346 textdomain("amanda");
348 /* drop root privileges */
349 dumper_setuid = set_root_privs(0);
355 dbopen(DBG_SUBDIR_SERVER);
357 /* Don't die when child closes pipe */
358 signal(SIGPIPE, SIG_IGN);
360 add_amanda_log_handler(amanda_log_stderr);
361 add_amanda_log_handler(amanda_log_trace_log);
363 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
366 set_config_overrides(cfg_ovr);
367 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
369 if (!dumper_setuid) {
370 error(_("dumper must be run setuid root"));
373 if (config_errors(NULL) >= CFGERR_ERRORS) {
374 g_critical(_("errors processing config file"));
377 safe_cd(); /* do this *after* config_init() */
379 check_running_as(RUNNING_AS_ROOT | RUNNING_AS_UID_ONLY);
381 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
383 our_features = am_init_feature_set();
384 our_feature_string = am_feature_to_string(our_features);
386 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
388 _("%s: pid %ld executable %s version %s\n"),
389 get_pname(), (long) getpid(),
393 /* now, make sure we are a valid user */
395 signal(SIGPIPE, SIG_IGN);
397 conf_dtimeout = (time_t)getconf_int(CNF_DTIMEOUT);
403 free_cmdargs(cmdargs);
407 switch(cmdargs->cmd) {
409 if(cmdargs->argc < 2)
410 error(_("error [dumper START: not enough args: timestamp]"));
411 dumper_timestamp = newstralloc(dumper_timestamp, cmdargs->argv[1]);
441 a = 1; /* skip "PORT-DUMP" */
443 if(a >= cmdargs->argc) {
444 error(_("error [dumper PORT-DUMP: not enough args: handle]"));
447 handle = newstralloc(handle, cmdargs->argv[a++]);
449 if(a >= cmdargs->argc) {
450 error(_("error [dumper PORT-DUMP: not enough args: port]"));
453 header_port = (in_port_t)atoi(cmdargs->argv[a++]);
455 if(a >= cmdargs->argc) {
456 error(_("error [dumper PORT-DUMP: not enough args: hostname]"));
459 hostname = newstralloc(hostname, cmdargs->argv[a++]);
461 if(a >= cmdargs->argc) {
462 error(_("error [dumper PORT-DUMP: not enough args: features]"));
465 am_release_feature_set(their_features);
466 their_features = am_string_to_feature(cmdargs->argv[a++]);
468 if(a >= cmdargs->argc) {
469 error(_("error [dumper PORT-DUMP: not enough args: diskname]"));
472 diskname = newstralloc(diskname, cmdargs->argv[a++]);
473 if (qdiskname != NULL)
475 qdiskname = quote_string(diskname);
476 b64disk = amxml_format_tag("disk", diskname);
478 if(a >= cmdargs->argc) {
479 error(_("error [dumper PORT-DUMP: not enough args: device]"));
482 device = newstralloc(device, cmdargs->argv[a++]);
483 b64device = amxml_format_tag("diskdevice", device);
484 if(strcmp(device,"NODEVICE") == 0)
487 if(a >= cmdargs->argc) {
488 error(_("error [dumper PORT-DUMP: not enough args: level]"));
491 level = atoi(cmdargs->argv[a++]);
493 if(a >= cmdargs->argc) {
494 error(_("error [dumper PORT-DUMP: not enough args: dumpdate]"));
497 dumpdate = newstralloc(dumpdate, cmdargs->argv[a++]);
499 if(a >= cmdargs->argc) {
500 error(_("error [dumper PORT-DUMP: not enough args: program]"));
503 progname = newstralloc(progname, cmdargs->argv[a++]);
505 if(a >= cmdargs->argc) {
506 error(_("error [dumper PORT-DUMP: not enough args: amandad_path]"));
509 amandad_path = newstralloc(amandad_path, cmdargs->argv[a++]);
511 if(a >= cmdargs->argc) {
512 error(_("error [dumper PORT-DUMP: not enough args: client_username]"));
514 client_username = newstralloc(client_username, cmdargs->argv[a++]);
516 if(a >= cmdargs->argc) {
517 error(_("error [dumper PORT-DUMP: not enough args: client_port]"));
519 client_port = newstralloc(client_port, cmdargs->argv[a++]);
521 if(a >= cmdargs->argc) {
522 error(_("error [dumper PORT-DUMP: not enough args: ssh_keys]"));
524 ssh_keys = newstralloc(ssh_keys, cmdargs->argv[a++]);
526 if(a >= cmdargs->argc) {
527 error(_("error [dumper PORT-DUMP: not enough args: auth]"));
529 auth = newstralloc(auth, cmdargs->argv[a++]);
531 if(a >= cmdargs->argc) {
532 error(_("error [dumper PORT-DUMP: not enough args: data_path]"));
534 data_path = data_path_from_string(cmdargs->argv[a++]);
536 if(a >= cmdargs->argc) {
537 error(_("error [dumper PORT-DUMP: not enough args: dataport_list]"));
539 dataport_list = newstralloc(dataport_list, cmdargs->argv[a++]);
541 if(a >= cmdargs->argc) {
542 error(_("error [dumper PORT-DUMP: not enough args: max_warnings]"));
544 max_warnings = atoi(cmdargs->argv[a++]);
546 if(a >= cmdargs->argc) {
547 error(_("error [dumper PORT-DUMP: not enough args: options]"));
549 options = newstralloc(options, cmdargs->argv[a++]);
551 if(a != cmdargs->argc) {
552 error(_("error [dumper PORT-DUMP: too many args: %d != %d]"),
557 /* Double-check that 'localhost' resolves properly */
558 if ((res = resolve_hostname("localhost", 0, NULL, NULL) != 0)) {
559 errstr = newvstrallocf(errstr,
560 _("could not resolve localhost: %s"),
562 q = quote_string(errstr);
563 putresult(FAILED, "%s %s\n", handle, q);
564 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
565 dumper_timestamp, level, errstr);
570 /* connect outf to chunker/taper port */
572 g_debug(_("Sending header to localhost:%d\n"), header_port);
573 outfd = stream_client("localhost", header_port,
574 STREAM_BUFSIZE, 0, NULL, 0);
577 errstr = newvstrallocf(errstr, _("port open: %s"),
579 q = quote_string(errstr);
580 putresult(FAILED, "%s %s\n", handle, q);
581 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
582 dumper_timestamp, level, errstr);
586 databuf_init(&db, outfd);
589 if (am_has_feature(their_features, fe_req_xml))
590 xml_check_options(options); /* note: modifies globals */
592 check_options(options); /* note: modifies globals */
594 rc = startup_dump(hostname,
607 q = quote_string(errstr);
608 putresult(rc == 2? FAILED : TRYAGAIN, "%s %s\n",
611 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
612 dumper_timestamp, level, errstr);
616 /* try to clean up any defunct processes, since Amanda doesn't
617 wait() for them explicitly */
618 while(waitpid(-1, NULL, WNOHANG)> 0);
621 amfree(amandad_path);
622 amfree(client_username);
632 if(cmdargs->argc >= 1) {
633 q = quote_string(cmdargs->argv[0]);
635 q = stralloc(_("(no input?)"));
637 putresult(BAD_COMMAND, "%s\n", q);
644 } while(cmdargs->cmd != QUIT);
645 free_cmdargs(cmdargs);
647 log_add(L_INFO, "pid-done %ld", (long)getpid());
649 am_release_feature_set(our_features);
650 amfree(our_feature_string);
652 amfree(dumper_timestamp);
661 amfree(clntcompprog);
663 amfree(clnt_encrypt);
664 amfree(srv_decrypt_opt);
665 amfree(clnt_decrypt_opt);
669 return (0); /* exit */
674 * Initialize a databuf. Takes a writeable file descriptor.
683 db->datain = db->dataout = db->datalimit = NULL;
684 db->compresspid = -1;
690 * Updates the buffer pointer for the input data buffer. The buffer is
691 * written regardless of how much data is present, since we know we
692 * are writing to a socket (to chunker) and there is no need to maintain
701 db->buf = (char *)buf;
702 db->datain = db->datalimit = db->buf + size;
703 db->dataout = db->buf;
704 return databuf_flush(db);
708 * Write out the buffer to chunker.
718 * If there's no data, do nothing.
720 if (db->dataout >= db->datain) {
725 * Write out the buffer
727 written = full_write(db->fd, db->dataout,
728 (size_t)(db->datain - db->dataout));
730 db->dataout += written;
731 dumpbytes += (off_t)written;
733 if (dumpbytes >= (off_t)1024) {
734 dumpsize += (dumpbytes / (off_t)1024);
735 dumpbytes %= (off_t)1024;
738 int save_errno = errno;
739 m = vstrallocf(_("data write: %s"), strerror(save_errno));
741 errstr = quote_string(m);
746 db->datain = db->dataout = db->buf;
750 static int dump_result;
752 #define GOT_INFO_ENDLINE (1 << 0)
753 #define GOT_SIZELINE (1 << 1)
754 #define GOT_ENDLINE (1 << 2)
755 #define HEADER_DONE (1 << 3)
759 process_dumpeof(void)
761 /* process any partial line in msgbuf? !!! */
762 add_msg_data(NULL, 0);
763 if(!ISSET(status, GOT_SIZELINE) && dump_result < 2) {
764 /* make a note if there isn't already a failure */
766 _("? %s: strange [missing size line from sendbackup]\n"),
769 errstr = stralloc(_("missing size line from sendbackup"));
771 dump_result = max(dump_result, 2);
774 if(!ISSET(status, GOT_ENDLINE) && dump_result < 2) {
776 _("? %s: strange [missing end line from sendbackup]\n"),
779 errstr = stralloc(_("missing end line from sendbackup"));
781 dump_result = max(dump_result, 2);
786 * Parse an information line from the client.
787 * We ignore unknown parameters and only remember the last
794 static const struct {
799 { "BACKUP", file.program, SIZEOF(file.program) },
800 { "APPLICATION", file.application, SIZEOF(file.application) },
801 { "RECOVER_CMD", file.recover_cmd, SIZEOF(file.recover_cmd) },
802 { "COMPRESS_SUFFIX", file.comp_suffix, SIZEOF(file.comp_suffix) },
803 { "SERVER_CUSTOM_COMPRESS", file.srvcompprog, SIZEOF(file.srvcompprog) },
804 { "CLIENT_CUSTOM_COMPRESS", file.clntcompprog, SIZEOF(file.clntcompprog) },
805 { "SERVER_ENCRYPT", file.srv_encrypt, SIZEOF(file.srv_encrypt) },
806 { "CLIENT_ENCRYPT", file.clnt_encrypt, SIZEOF(file.clnt_encrypt) },
807 { "SERVER_DECRYPT_OPTION", file.srv_decrypt_opt, SIZEOF(file.srv_decrypt_opt) },
808 { "CLIENT_DECRYPT_OPTION", file.clnt_decrypt_opt, SIZEOF(file.clnt_decrypt_opt) }
813 if (strcmp(str, "end") == 0) {
814 SET(status, GOT_INFO_ENDLINE);
818 name = strtok(str, "=");
821 value = strtok(NULL, "");
825 for (i = 0; i < SIZEOF(fields) / SIZEOF(fields[0]); i++) {
826 if (strcmp(name, fields[i].name) == 0) {
827 strncpy(fields[i].value, value, fields[i].len - 1);
828 fields[i].value[fields[i].len - 1] = '\0';
844 /* normal backup output line */
847 /* sendbackup detected something strange */
848 dump_result = max(dump_result, 1);
851 /* a sendbackup line, just check them all since there are only 5 */
852 tok = strtok(buf, " ");
853 if (tok == NULL || strcmp(tok, "sendbackup:") != 0)
856 tok = strtok(NULL, " ");
860 if (strcmp(tok, "start") == 0) {
864 if (strcmp(tok, "size") == 0) {
865 tok = strtok(NULL, "");
867 origsize = OFF_T_ATOI(tok);
868 SET(status, GOT_SIZELINE);
873 if (strcmp(tok, "no-op") == 0) {
878 if (strcmp(tok, "end") == 0) {
879 SET(status, GOT_ENDLINE);
883 if (strcmp(tok, "warning") == 0) {
884 dump_result = max(dump_result, 1);
888 if (strcmp(tok, "error") == 0) {
889 SET(status, GOT_ENDLINE);
890 dump_result = max(dump_result, 2);
892 tok = strtok(NULL, "");
893 if (!errstr) { /* report first error line */
894 if (tok == NULL || *tok != '[') {
895 errstr = newvstrallocf(errstr, _("bad remote error: %s"),
900 tok++; /* skip over '[' */
901 if ((enderr = strchr(tok, ']')) != NULL)
903 errstr = newstralloc(errstr, tok);
909 if (strcmp(tok, "info") == 0) {
910 tok = strtok(NULL, "");
912 parse_info_line(tok);
915 /* else we fall through to bad line */
919 g_fprintf(errf, "??");
920 dump_result = max(dump_result, 1);
923 g_fprintf(errf, "%s\n", str);
937 buflen = strlen(msg.buf);
942 * If our argument is NULL, then we need to flush out any remaining
948 g_fprintf(errf,_("? %s: error [partial line in msgbuf: %zu bytes]\n"),
949 get_pname(), buflen);
950 g_fprintf(errf,_("? %s: error [partial line in msgbuf: \"%s\"]\n"),
951 get_pname(), msg.buf);
957 * Expand the buffer if it can't hold the new contents.
959 if ((buflen + len + 1) > msg.size) {
963 /* round up to next y, where y is a power of 2 */
964 #define ROUND(x, y) (((x) + (y) - 1) & ~((y) - 1))
966 newsize = ROUND(buflen + (ssize_t)len + 1, 256);
967 newbuf = alloc(newsize);
969 if (msg.buf != NULL) {
970 strncpy(newbuf, msg.buf, newsize);
979 * If there was a partial line from the last call, then
980 * append the new data to the end.
982 strncat(msg.buf, str, len);
985 * Process all lines in the buffer
986 * scanning line for unqouted newline.
988 for (ch = line = msg.buf; *ch != '\0'; ch++) {
991 * Found a newline. Terminate and process line.
994 process_dumpline(line);
1000 * If we did not process all of the data, move it to the front
1001 * of the buffer so it is there next time.
1003 if (*line != '\0') {
1004 buflen = strlen(line);
1005 memmove(msg.buf, line, (size_t)buflen + 1);
1021 if (fseeko(errf, 0L, SEEK_SET) < 0) {
1022 dbprintf(_("log_msgout: warning - seek failed: %s\n"), strerror(errno));
1024 while ((line = agets(errf)) != NULL) {
1025 if (max_warnings > 0 && errf_lines >= max_warnings && count >= max_warnings) {
1026 log_add(typ, "Look in the '%s' file for full error messages", errfname);
1030 if (line[0] != '\0') {
1031 log_add(typ, "%s", line);
1044 * Fill in the rest of the tape header
1051 assert(ISSET(status, HEADER_DONE));
1053 file->type = F_DUMPFILE;
1054 strncpy(file->datestamp, dumper_timestamp, sizeof(file->datestamp) - 1);
1055 strncpy(file->name, hostname, SIZEOF(file->name) - 1);
1056 strncpy(file->disk, diskname, SIZEOF(file->disk) - 1);
1057 file->dumplevel = level;
1058 file->blocksize = DISK_BLOCK_BYTES;
1061 * If we're doing the compression here, we need to override what
1062 * sendbackup told us the compression was.
1064 if (srvcompress != COMP_NONE) {
1065 file->compressed = 1;
1066 #ifndef UNCOMPRESS_OPT
1067 #define UNCOMPRESS_OPT ""
1069 if (srvcompress == COMP_SERVER_CUST) {
1070 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1071 " %s %s |", srvcompprog, "-d");
1072 strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1);
1073 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1074 strncpy(file->srvcompprog, srvcompprog, SIZEOF(file->srvcompprog) - 1);
1075 file->srvcompprog[SIZEOF(file->srvcompprog) - 1] = '\0';
1076 } else if ( srvcompress == COMP_CUST ) {
1077 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1078 " %s %s |", clntcompprog, "-d");
1079 strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1);
1080 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1081 strncpy(file->clntcompprog, clntcompprog, SIZEOF(file->clntcompprog));
1082 file->clntcompprog[SIZEOF(file->clntcompprog) - 1] = '\0';
1084 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1085 " %s %s |", UNCOMPRESS_PATH, UNCOMPRESS_OPT);
1086 strncpy(file->comp_suffix, COMPRESS_SUFFIX,SIZEOF(file->comp_suffix) - 1);
1087 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1090 if (file->comp_suffix[0] == '\0') {
1091 file->compressed = 0;
1092 assert(SIZEOF(file->comp_suffix) >= 2);
1093 strncpy(file->comp_suffix, "N", SIZEOF(file->comp_suffix) - 1);
1094 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1096 file->compressed = 1;
1099 /* take care of the encryption header here */
1100 if (srvencrypt != ENCRYPT_NONE) {
1102 if (srvencrypt == ENCRYPT_SERV_CUST) {
1103 if (srv_decrypt_opt) {
1104 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1105 " %s %s |", srv_encrypt, srv_decrypt_opt);
1106 strncpy(file->srv_decrypt_opt, srv_decrypt_opt, SIZEOF(file->srv_decrypt_opt) - 1);
1107 file->srv_decrypt_opt[SIZEOF(file->srv_decrypt_opt) - 1] = '\0';
1109 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1110 " %s |", srv_encrypt);
1111 file->srv_decrypt_opt[0] = '\0';
1113 strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1);
1114 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1115 strncpy(file->srv_encrypt, srv_encrypt, SIZEOF(file->srv_encrypt) - 1);
1116 file->srv_encrypt[SIZEOF(file->srv_encrypt) - 1] = '\0';
1117 } else if ( srvencrypt == ENCRYPT_CUST ) {
1118 if (clnt_decrypt_opt) {
1119 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1120 " %s %s |", clnt_encrypt, clnt_decrypt_opt);
1121 strncpy(file->clnt_decrypt_opt, clnt_decrypt_opt,
1122 SIZEOF(file->clnt_decrypt_opt));
1123 file->clnt_decrypt_opt[SIZEOF(file->clnt_decrypt_opt) - 1] = '\0';
1125 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1126 " %s |", clnt_encrypt);
1127 file->clnt_decrypt_opt[0] = '\0';
1129 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1130 " %s %s |", clnt_encrypt, clnt_decrypt_opt);
1131 strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1);
1132 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1133 strncpy(file->clnt_encrypt, clnt_encrypt, SIZEOF(file->clnt_encrypt) - 1);
1134 file->clnt_encrypt[SIZEOF(file->clnt_encrypt) - 1] = '\0';
1137 if (file->encrypt_suffix[0] == '\0') {
1138 file->encrypted = 0;
1139 assert(SIZEOF(file->encrypt_suffix) >= 2);
1140 strncpy(file->encrypt_suffix, "N", SIZEOF(file->encrypt_suffix) - 1);
1141 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1147 file->dle_str = stralloc(dle_str);
1149 file->dle_str = NULL;
1153 * Send an Amanda dump header to the output file.
1163 if (debug_dumper > 1)
1164 dump_dumpfile_t(file);
1165 buffer = build_header(file, NULL, DISK_BLOCK_BYTES);
1166 if (!buffer) /* this shouldn't happen */
1167 error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES);
1169 written = full_write(outfd, buffer, DISK_BLOCK_BYTES);
1171 if(written == DISK_BLOCK_BYTES)
1183 char *indexfile_tmp = NULL;
1184 char *indexfile_real = NULL;
1185 char level_str[NUM_STR_SIZE];
1190 double dumptime; /* Time dump took in secs */
1191 pid_t indexpid = -1;
1197 if (msg.buf) msg.buf[0] = '\0'; /* reset msg buffer */
1200 dumpbytes = dumpsize = headersize = origsize = (off_t)0;
1203 g_snprintf(level_str, SIZEOF(level_str), "%d", level);
1204 time_str = get_timestamp_from_time(0);
1205 fn = sanitise_filename(diskname);
1207 errfname = newvstralloc(errfname,
1209 "/log.error", NULL);
1210 mkdir(errfname, 0700);
1211 errfname = newvstralloc(errfname,
1213 "/log.error/", hostname,
1221 if((errf = fopen(errfname, "w+")) == NULL) {
1222 errstr = newvstrallocf(errstr, "errfile open \"%s\": %s",
1223 errfname, strerror(errno));
1228 if (streams[INDEXFD].fd != NULL) {
1229 indexfile_real = getindexfname(hostname, diskname, dumper_timestamp, level);
1230 indexfile_tmp = stralloc2(indexfile_real, ".tmp");
1232 if (mkpdir(indexfile_tmp, 0755, (uid_t)-1, (gid_t)-1) == -1) {
1233 errstr = newvstrallocf(errstr,
1234 _("err create %s: %s"),
1237 amfree(indexfile_real);
1238 amfree(indexfile_tmp);
1241 indexout = open(indexfile_tmp, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1242 if (indexout == -1) {
1243 errstr = newvstrallocf(errstr, _("err open %s: %s"),
1244 indexfile_tmp, strerror(errno));
1247 if (runcompress(indexout, &indexpid, COMP_BEST, "index compress") < 0) {
1254 * Schedule the indexfd for relaying to the index file
1256 security_stream_read(streams[INDEXFD].fd, read_indexfd, &indexout);
1260 * We only need to process messages initially. Once we have done
1261 * the header, we will start processing data too.
1263 security_stream_read(streams[MESGFD].fd, read_mesgfd, db);
1267 * Setup a read timeout
1269 timeout(conf_dtimeout);
1272 * Start the event loop. This will exit when all three events
1273 * (read the mesgfd, read the datafd, and timeout) are removed.
1277 if (!ISSET(status, HEADER_DONE)) {
1278 dump_result = max(dump_result, 2);
1279 if (!errstr) errstr = stralloc(_("got no header information"));
1282 dumpsize -= headersize; /* don't count the header */
1283 if (dumpsize <= (off_t)0 && data_path == DATA_PATH_AMANDA) {
1284 dumpsize = (off_t)0;
1285 dump_result = max(dump_result, 2);
1286 if (!errstr) errstr = stralloc(_("got no data"));
1289 if (data_path == DATA_PATH_DIRECTTCP) {
1290 dumpsize = origsize;
1293 if (!ISSET(status, HEADER_DONE)) {
1294 dump_result = max(dump_result, 2);
1295 if (!errstr) errstr = stralloc(_("got no header information"));
1298 if (dumpsize == 0 && data_path == DATA_PATH_AMANDA) {
1299 dump_result = max(dump_result, 2);
1300 if (!errstr) errstr = stralloc(_("got no data"));
1303 if (indexfile_tmp) {
1304 amwait_t index_status;
1306 /*@i@*/ aclose(indexout);
1307 waitpid(indexpid,&index_status,0);
1308 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1309 if (rename(indexfile_tmp, indexfile_real) != 0) {
1310 log_add(L_WARNING, _("could not rename \"%s\" to \"%s\": %s"),
1311 indexfile_tmp, indexfile_real, strerror(errno));
1313 amfree(indexfile_tmp);
1314 amfree(indexfile_real);
1317 /* copy the header in a file on the index dir */
1321 char *f = getheaderfname(hostname, diskname, dumper_timestamp, level);
1324 s = build_header(&file, NULL, DISK_BLOCK_BYTES);
1332 if (db->compresspid != -1 && dump_result < 2) {
1333 amwait_t wait_status;
1334 char *errmsg = NULL;
1336 waitpid(db->compresspid, &wait_status, 0);
1337 if (WIFSIGNALED(wait_status)) {
1338 errmsg = g_strdup_printf(_("%s terminated with signal %d"),
1339 "compress", WTERMSIG(wait_status));
1340 } else if (WIFEXITED(wait_status)) {
1341 if (WEXITSTATUS(wait_status) != 0) {
1342 errmsg = g_strdup_printf(_("%s exited with status %d"),
1343 "compress", WEXITSTATUS(wait_status));
1346 errmsg = g_strdup_printf(_("%s got bad exit"),
1350 g_fprintf(errf, _("? %s\n"), errmsg);
1351 g_debug("%s", errmsg);
1352 dump_result = max(dump_result, 2);
1358 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1359 db->compresspid = -1;
1362 if (db->encryptpid != -1 && dump_result < 2) {
1363 amwait_t wait_status;
1364 char *errmsg = NULL;
1366 waitpid(db->encryptpid, &wait_status, 0);
1367 if (WIFSIGNALED(wait_status)) {
1368 errmsg = g_strdup_printf(_("%s terminated with signal %d"),
1369 "encrypt", WTERMSIG(wait_status));
1370 } else if (WIFEXITED(wait_status)) {
1371 if (WEXITSTATUS(wait_status) != 0) {
1372 errmsg = g_strdup_printf(_("%s exited with status %d"),
1373 "encrypt", WEXITSTATUS(wait_status));
1376 errmsg = g_strdup_printf(_("%s got bad exit"),
1380 g_fprintf(errf, _("? %s\n"), errmsg);
1381 g_debug("%s", errmsg);
1382 dump_result = max(dump_result, 2);
1388 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1389 db->encryptpid = -1;
1392 if (dump_result > 1)
1395 runtime = stopclock();
1396 dumptime = g_timeval_to_double(runtime);
1399 errstr = alloc(128);
1400 g_snprintf(errstr, 128, _("sec %s kb %lld kps %3.1lf orig-kb %lld"),
1401 walltime_str(runtime),
1402 (long long)dumpsize,
1403 (isnormal(dumptime) ? ((double)dumpsize / (double)dumptime) : 0.0),
1404 (long long)origsize);
1405 m = vstrallocf("[%s]", errstr);
1406 q = quote_string(m);
1408 putresult(DONE, _("%s %lld %lld %lu %s\n"), handle,
1409 (long long)origsize,
1410 (long long)dumpsize,
1411 (unsigned long)((double)dumptime+0.5), q);
1414 switch(dump_result) {
1416 log_add(L_SUCCESS, "%s %s %s %d [%s]", hostname, qdiskname, dumper_timestamp, level, errstr);
1421 log_start_multiline();
1422 log_add(L_STRANGE, "%s %s %d [%s]", hostname, qdiskname, level, errstr);
1423 to_unlink = log_msgout(L_STRANGE);
1424 log_end_multiline();
1437 if (data_path == DATA_PATH_AMANDA)
1441 dumpfile_free_data(&file);
1446 m = vstrallocf("[%s]", errstr);
1447 q = quote_string(m);
1448 putresult(FAILED, "%s %s\n", handle, q);
1453 /* kill all child process */
1454 if (db->compresspid != -1) {
1455 g_fprintf(stderr,_("%s: kill compress command\n"),get_pname());
1456 if (kill(db->compresspid, SIGTERM) < 0) {
1457 if (errno != ESRCH) {
1458 g_fprintf(stderr,_("%s: can't kill compress command: %s\n"),
1459 get_pname(), strerror(errno));
1461 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1465 waitpid(db->compresspid,NULL,0);
1466 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1470 if (db->encryptpid != -1) {
1471 g_fprintf(stderr,_("%s: kill encrypt command\n"),get_pname());
1472 if (kill(db->encryptpid, SIGTERM) < 0) {
1473 if (errno != ESRCH) {
1474 g_fprintf(stderr,_("%s: can't kill encrypt command: %s\n"),
1475 get_pname(), strerror(errno));
1477 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1481 waitpid(db->encryptpid,NULL,0);
1482 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1486 if (indexpid != -1) {
1487 g_fprintf(stderr,_("%s: kill index command\n"),get_pname());
1488 if (kill(indexpid, SIGTERM) < 0) {
1489 if (errno != ESRCH) {
1490 g_fprintf(stderr,_("%s: can't kill index command: %s\n"),
1491 get_pname(),strerror(errno));
1493 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1497 waitpid(indexpid,NULL,0);
1498 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1502 log_start_multiline();
1503 log_add(L_FAIL, _("%s %s %s %d [%s]"), hostname, qdiskname, dumper_timestamp,
1506 to_unlink = log_msgout(L_FAIL);
1508 log_end_multiline();
1518 if (indexfile_tmp) {
1519 unlink(indexfile_tmp);
1520 amfree(indexfile_tmp);
1521 amfree(indexfile_real);
1525 dumpfile_free_data(&file);
1531 * Callback for reads on the mesgfd stream
1539 struct databuf *db = cookie;
1545 errstr = newvstrallocf(errstr, _("mesg read: %s"),
1546 security_stream_geterror(streams[MESGFD].fd));
1553 * EOF. Just shut down the mesg stream.
1556 security_stream_close(streams[MESGFD].fd);
1557 streams[MESGFD].fd = NULL;
1559 * If the data fd and index fd has also shut down, then we're done.
1561 if ((set_datafd == 0 || streams[DATAFD].fd == NULL) &&
1562 streams[INDEXFD].fd == NULL)
1567 assert(buf != NULL);
1568 add_msg_data(buf, (size_t)size);
1569 security_stream_read(streams[MESGFD].fd, read_mesgfd, cookie);
1573 if (ISSET(status, GOT_INFO_ENDLINE) && !ISSET(status, HEADER_DONE)) {
1574 /* Use the first in the dataport_list */
1575 in_port_t data_port;
1576 char *data_host = dataport_list;
1579 s = strchr(dataport_list, ',');
1580 if (s) *s = '\0'; /* use first data_port */
1581 s = strrchr(dataport_list, ':');
1584 data_port = atoi(s);
1586 SET(status, HEADER_DONE);
1587 /* time to do the header */
1588 finish_tapeheader(&file);
1589 if (write_tapeheader(db->fd, &file)) {
1590 errstr = newvstrallocf(errstr, _("write_tapeheader: %s"),
1597 if (data_path == DATA_PATH_AMANDA) {
1598 g_debug(_("Sending data to %s:%d\n"), data_host, data_port);
1599 db->fd = stream_client(data_host, data_port,
1600 STREAM_BUFSIZE, 0, NULL, 0);
1602 errstr = newvstrallocf(errstr,
1603 _("Can't open data output stream: %s"),
1611 dumpsize += (off_t)DISK_BLOCK_KB;
1612 headersize += (off_t)DISK_BLOCK_KB;
1614 if (srvencrypt == ENCRYPT_SERV_CUST) {
1615 if (runencrypt(db->fd, &db->encryptpid, srvencrypt) < 0) {
1622 * Now, setup the compress for the data output, and start
1623 * reading the datafd.
1625 if ((srvcompress != COMP_NONE) && (srvcompress != COMP_CUST)) {
1626 if (runcompress(db->fd, &db->compresspid, srvcompress, "data compress") < 0) {
1632 security_stream_read(streams[DATAFD].fd, read_datafd, db);
1637 * Reset the timeout for future reads
1639 timeout(conf_dtimeout);
1643 * Callback for reads on the datafd stream
1651 struct databuf *db = cookie;
1656 * The read failed. Error out
1659 errstr = newvstrallocf(errstr, _("data read: %s"),
1660 security_stream_geterror(streams[DATAFD].fd));
1667 /* The header had better be written at this point */
1668 assert(ISSET(status, HEADER_DONE));
1671 * EOF. Stop and return.
1675 if (dumpbytes != (off_t)0) {
1676 dumpsize += (off_t)1;
1678 security_stream_close(streams[DATAFD].fd);
1679 streams[DATAFD].fd = NULL;
1682 * If the mesg fd and index fd has also shut down, then we're done.
1684 if (streams[MESGFD].fd == NULL && streams[INDEXFD].fd == NULL)
1690 * We read something. Add it to the databuf and reschedule for
1693 assert(buf != NULL);
1694 if (databuf_write(db, buf, (size_t)size) < 0) {
1695 int save_errno = errno;
1696 errstr = newvstrallocf(errstr, _("data write: %s"), strerror(save_errno));
1703 * Reset the timeout for future reads
1705 timeout(conf_dtimeout);
1707 security_stream_read(streams[DATAFD].fd, read_datafd, cookie);
1711 * Callback for reads on the index stream
1721 assert(cookie != NULL);
1722 fd = *(int *)cookie;
1725 errstr = newvstrallocf(errstr, _("index read: %s"),
1726 security_stream_geterror(streams[INDEXFD].fd));
1733 * EOF. Stop and return.
1736 security_stream_close(streams[INDEXFD].fd);
1737 streams[INDEXFD].fd = NULL;
1739 * If the mesg fd has also shut down, then we're done.
1741 if ((set_datafd == 0 || streams[DATAFD].fd == NULL) &&
1742 streams[MESGFD].fd == NULL)
1748 assert(buf != NULL);
1751 * We ignore error while writing to the index file.
1753 if (full_write(fd, buf, (size_t)size) < (size_t)size) {
1754 /* Ignore error, but schedule another read. */
1755 if(indexfderror == 0) {
1757 log_add(L_INFO, _("Index corrupted for %s:%s"), hostname, qdiskname);
1760 security_stream_read(streams[INDEXFD].fd, read_indexfd, cookie);
1764 handle_filter_stderr(
1767 filter_t *filter = cookie;
1772 event_release(filter->event);
1774 if (filter->buffer == NULL) {
1775 /* allocate initial buffer */
1776 filter->buffer = g_malloc(2048);
1779 filter->allocated_size = 2048;
1780 } else if (filter->first > 0) {
1781 if (filter->allocated_size - filter->size - filter->first < 1024) {
1782 memmove(filter->buffer, filter->buffer + filter->first,
1786 } else if (filter->allocated_size - filter->size < 1024) {
1787 /* double the size of the buffer */
1788 filter->allocated_size *= 2;
1789 filter->buffer = g_realloc(filter->buffer, filter->allocated_size);
1792 nread = read(filter->fd, filter->buffer + filter->first + filter->size,
1793 filter->allocated_size - filter->first - filter->size - 2);
1797 if (filter->size > 0 && filter->buffer[filter->first + filter->size - 1] != '\n') {
1798 /* Add a '\n' at end of buffer */
1799 filter->buffer[filter->first + filter->size] = '\n';
1803 filter->size += nread;
1806 /* process all complete lines */
1807 b = filter->buffer + filter->first;
1808 filter->buffer[filter->first + filter->size] = '\0';
1809 while (b < filter->buffer + filter->first + filter->size &&
1810 (p = strchr(b, '\n')) != NULL) {
1812 g_fprintf(errf, _("? %s: %s\n"), filter->name, b);
1813 if (errstr == NULL) {
1814 errstr = stralloc(b);
1817 filter->first += len;
1818 filter->size -= len;
1820 dump_result = max(dump_result, 1);
1824 g_free(filter->buffer);
1827 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1828 handle_filter_stderr, filter);
1833 * Startup a timeout in the event handler. If the arg is 0,
1834 * then remove the timeout.
1840 static event_handle_t *ev_timeout = NULL;
1843 * First, remove a timeout if one is active.
1845 if (ev_timeout != NULL) {
1846 event_release(ev_timeout);
1851 * Now, schedule a new one if 'seconds' is greater than 0
1854 ev_timeout = event_register((event_id_t)seconds, EV_TIME, timeout_callback, NULL);
1858 * This is the callback for timeout(). If this is reached, then we
1859 * have a data timeout.
1865 (void)unused; /* Quiet unused parameter warning */
1867 assert(unused == NULL);
1868 errstr = newstralloc(errstr, _("data timeout"));
1874 * This is called when everything needs to shut down so event_loop()
1881 struct cmdargs *cmdargs = NULL;
1883 /* Check if I have a pending ABORT command */
1884 cmdargs = get_pending_cmd();
1886 if (cmdargs->cmd != ABORT) {
1887 error(_("beurk %d"), cmdargs->cmd);
1890 errstr = stralloc(cmdargs->argv[1]);
1891 free_cmdargs(cmdargs);
1894 for (i = 0; i < NSTREAMS; i++) {
1895 if (streams[i].fd != NULL) {
1896 security_stream_close(streams[i].fd);
1897 streams[i].fd = NULL;
1901 aclose(g_databuf->fd);
1907 * Runs compress with the first arg as its stdout. Returns
1908 * 0 on success or negative if error, and it's pid via the second
1909 * argument. The outfd arg is dup2'd to the pipe to the compress
1919 int outpipe[2], rval;
1924 assert(pid != NULL);
1926 /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */
1927 if (pipe(outpipe) < 0) {
1928 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1932 /* errpipe[0] is pipe's output, outpipe[1] is input. */
1933 if (pipe(errpipe) < 0) {
1934 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1938 if (comptype != COMP_SERVER_CUST) {
1939 g_debug("execute: %s %s", COMPRESS_PATH,
1940 comptype == COMP_BEST ? COMPRESS_BEST_OPT : COMPRESS_FAST_OPT);
1942 g_debug("execute: %s", srvcompprog);
1944 switch (*pid = fork()) {
1946 errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno));
1953 rval = dup2(outpipe[1], outfd);
1955 errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno));
1959 filter = g_new0(filter_t, 1);
1960 filter->fd = errpipe[0];
1961 filter->name = name;
1962 filter->buffer = NULL;
1964 filter->allocated_size = 0;
1965 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1966 handle_filter_stderr, filter);
1971 if (dup2(outpipe[0], 0) < 0) {
1972 error(_("err dup2 in: %s"), strerror(errno));
1975 if (dup2(outfd, 1) == -1) {
1976 error(_("err dup2 out: %s"), strerror(errno));
1979 if (dup2(errpipe[1], 2) == -1) {
1980 error(_("err dup2 err: %s"), strerror(errno));
1983 if (comptype != COMP_SERVER_CUST) {
1984 char *base = stralloc(COMPRESS_PATH);
1985 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1989 execlp(COMPRESS_PATH, COMPRESS_PATH, ( comptype == COMP_BEST ?
1990 COMPRESS_BEST_OPT : COMPRESS_FAST_OPT), (char *)NULL);
1991 error(_("error: couldn't exec %s: %s"), COMPRESS_PATH, strerror(errno));
1993 } else if (*srvcompprog) {
1994 char *base = stralloc(srvcompprog);
1995 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1999 execlp(srvcompprog, srvcompprog, (char *)0);
2000 error(_("error: couldn't exec server custom compression '%s'.\n"), srvcompprog);
2009 * Runs encrypt with the first arg as its stdout. Returns
2010 * 0 on success or negative if error, and it's pid via the second
2011 * argument. The outfd arg is dup2'd to the pipe to the encrypt
2018 encrypt_t encrypttype)
2020 int outpipe[2], rval;
2025 assert(pid != NULL);
2027 /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */
2028 if (pipe(outpipe) < 0) {
2029 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
2033 /* errpipe[0] is pipe's output, outpipe[1] is input. */
2034 if (pipe(errpipe) < 0) {
2035 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
2039 g_debug("execute: %s", srv_encrypt);
2040 switch (*pid = fork()) {
2042 errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno));
2050 rval = dup2(outpipe[1], outfd);
2052 errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno));
2056 filter = g_new0(filter_t, 1);
2057 filter->fd = errpipe[0];
2058 base = g_strdup(srv_encrypt);
2059 filter->name = g_strdup(basename(base));
2061 filter->buffer = NULL;
2063 filter->allocated_size = 0;
2064 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
2065 handle_filter_stderr, filter);
2070 if (dup2(outpipe[0], 0) < 0) {
2071 error(_("err dup2 in: %s"), strerror(errno));
2074 if (dup2(outfd, 1) < 0 ) {
2075 error(_("err dup2 out: %s"), strerror(errno));
2078 if (dup2(errpipe[1], 2) == -1) {
2079 error(_("err dup2 err: %s"), strerror(errno));
2083 base = stralloc(srv_encrypt);
2084 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
2087 if ((encrypttype == ENCRYPT_SERV_CUST) && *srv_encrypt) {
2089 execlp(srv_encrypt, srv_encrypt, (char *)0);
2090 error(_("error: couldn't exec server custom encryption '%s'.\n"), srv_encrypt);
2100 /* -------------------- */
2103 sendbackup_response(
2106 security_handle_t * sech)
2108 int ports[NSTREAMS], *response_error = datap, i;
2113 assert(response_error != NULL);
2114 assert(sech != NULL);
2116 security_close_connection(sech, hostname);
2119 errstr = newvstrallocf(errstr, _("[request failed: %s]"),
2120 security_geterror(sech));
2121 *response_error = 1;
2126 memset(ports, 0, SIZEOF(ports));
2127 if (pkt->type == P_NAK) {
2128 #if defined(PACKET_DEBUG)
2129 g_fprintf(stderr, _("got nak response:\n----\n%s\n----\n\n"), pkt->body);
2132 tok = strtok(pkt->body, " ");
2133 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2136 tok = strtok(NULL, "\n");
2138 errstr = newvstrallocf(errstr, "NAK: %s", tok);
2139 *response_error = 1;
2142 errstr = newvstrallocf(errstr, "request NAK");
2143 *response_error = 2;
2148 if (pkt->type != P_REP) {
2149 errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"),
2150 pkt_type2str(pkt->type), pkt->body);
2151 *response_error = 1;
2155 dbprintf(_("got response:\n----\n%s\n----\n\n"), pkt->body);
2157 for(i = 0; i < NSTREAMS; i++) {
2159 streams[i].fd = NULL;
2163 while((tok = strtok(p, " \n")) != NULL) {
2167 * Error response packets have "ERROR" followed by the error message
2168 * followed by a newline.
2170 if (strcmp(tok, "ERROR") == 0) {
2171 tok = strtok(NULL, "\n");
2173 tok = _("[bogus error packet]");
2174 errstr = newvstrallocf(errstr, "%s", tok);
2175 *response_error = 2;
2180 * Regular packets have CONNECT followed by three streams
2182 if (strcmp(tok, "CONNECT") == 0) {
2185 * Parse the three stream specifiers out of the packet.
2187 for (i = 0; i < NSTREAMS; i++) {
2188 tok = strtok(NULL, " ");
2189 if (tok == NULL || strcmp(tok, streams[i].name) != 0) {
2191 _("CONNECT token is \"%s\": expected \"%s\""),
2192 tok ? tok : "(null)",
2196 tok = strtok(NULL, " \n");
2197 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2199 _("CONNECT %s token is \"%s\": expected a port number"),
2200 streams[i].name, tok ? tok : "(null)");
2208 * OPTIONS [options string] '\n'
2210 if (strcmp(tok, "OPTIONS") == 0) {
2211 tok = strtok(NULL, "\n");
2213 extra = vstrallocf(_("OPTIONS token is missing"));
2217 while((p = strchr(tok, ';')) != NULL) {
2220 if(strncmp_const_skip(tok, "features=", tok, ch) == 0) {
2221 char *u = strchr(tok, ';');
2225 am_release_feature_set(their_features);
2226 if((their_features = am_string_to_feature(tok)) == NULL) {
2227 errstr = newvstrallocf(errstr,
2228 _("OPTIONS: bad features value: %s"),
2240 extra = vstrallocf(_("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""),
2241 tok ? tok : "(null)");
2245 if (dumper_kencrypt == KENCRYPT_WILL_DO)
2246 dumper_kencrypt = KENCRYPT_YES;
2249 * Connect the streams to their remote ports
2251 for (i = 0; i < NSTREAMS; i++) {
2254 streams[i].fd = security_stream_client(sech, ports[i]);
2255 if (streams[i].fd == NULL) {
2256 errstr = newvstrallocf(errstr,
2257 _("[could not connect %s stream: %s]"),
2259 security_geterror(sech));
2265 * Authenticate the streams
2267 for (i = 0; i < NSTREAMS; i++) {
2268 if (streams[i].fd == NULL)
2270 if (security_stream_auth(streams[i].fd) < 0) {
2271 errstr = newvstrallocf(errstr,
2272 _("[could not authenticate %s stream: %s]"),
2274 security_stream_geterror(streams[i].fd));
2280 * The MESGFD and DATAFD streams are mandatory. If we didn't get
2283 if (streams[MESGFD].fd == NULL || streams[DATAFD].fd == NULL) {
2284 errstr = newvstrallocf(errstr, _("[couldn't open MESG or INDEX streams]"));
2288 /* everything worked */
2289 *response_error = 0;
2293 errstr = newvstrallocf(errstr,
2294 _("[parse of reply message failed: %s]"),
2295 extra ? extra : _("(no additional information)"));
2297 *response_error = 2;
2302 *response_error = 1;
2306 dumper_get_security_conf(
2310 (void)arg; /* Quiet unused parameter warning */
2312 if(!string || !*string)
2315 if(strcmp(string, "krb5principal")==0) {
2316 return(getconf_str(CNF_KRB5PRINCIPAL));
2317 } else if(strcmp(string, "krb5keytab")==0) {
2318 return(getconf_str(CNF_KRB5KEYTAB));
2319 } else if(strcmp(string, "amandad_path")==0) {
2320 return (amandad_path);
2321 } else if(strcmp(string, "client_username")==0) {
2322 return (client_username);
2323 } else if(strcmp(string, "client_port")==0) {
2324 return (client_port);
2325 } else if(strcmp(string, "ssh_keys")==0) {
2327 } else if(strcmp(string, "kencrypt")==0) {
2328 if (dumper_kencrypt == KENCRYPT_YES)
2338 const char *hostname,
2342 const char *dumpdate,
2343 const char *progname,
2344 const char *amandad_path,
2345 const char *client_username,
2346 const char *client_port,
2347 const char *ssh_keys,
2349 const char *options)
2351 char level_string[NUM_STR_SIZE];
2354 const security_driver_t *secdrv;
2355 char *application_api;
2361 (void)disk; /* Quiet unused parameter warning */
2362 (void)amandad_path; /* Quiet unused parameter warning */
2363 (void)client_username; /* Quiet unused parameter warning */
2364 (void)client_port; /* Quiet unused parameter warning */
2365 (void)ssh_keys; /* Quiet unused parameter warning */
2366 (void)auth; /* Quiet unused parameter warning */
2368 has_features = am_has_feature(their_features, fe_req_options_features);
2369 has_hostname = am_has_feature(their_features, fe_req_options_hostname);
2370 has_config = am_has_feature(their_features, fe_req_options_config);
2371 has_device = am_has_feature(their_features, fe_sendbackup_req_device);
2374 * Default to bsd authentication if none specified. This is gross.
2376 * Options really need to be pre-parsed into some sort of structure
2377 * much earlier, and then flattened out again before transmission.
2380 g_snprintf(level_string, SIZEOF(level_string), "%d", level);
2381 if(strcmp(progname, "DUMP") == 0
2382 || strcmp(progname, "GNUTAR") == 0) {
2383 application_api = "";
2385 application_api = "BACKUP ";
2387 req = vstralloc("SERVICE sendbackup\n",
2389 has_features ? "features=" : "",
2390 has_features ? our_feature_string : "",
2391 has_features ? ";" : "",
2392 has_hostname ? "hostname=" : "",
2393 has_hostname ? hostname : "",
2394 has_hostname ? ";" : "",
2395 has_config ? "config=" : "",
2396 has_config ? get_config_name() : "",
2397 has_config ? ";" : "",
2402 if (am_has_feature(their_features, fe_req_xml)) {
2405 vstrextend(&p, "<dle>\n", NULL);
2406 if (*application_api != '\0') {
2407 vstrextend(&p, " <program>APPLICATION</program>\n", NULL);
2409 vstrextend(&p, " <program>", progname, "</program>\n", NULL);
2411 vstrextend(&p, " ", b64disk, "\n", NULL);
2412 if (device && has_device) {
2413 vstrextend(&p, " ", b64device, "\n",
2416 vstrextend(&p, " <level>", level_string, "</level>\n", NULL);
2417 vstrextend(&p, options+1, "</dle>\n", NULL);
2418 pclean = clean_dle_str_for_client(p, their_features);
2419 vstrextend(&req, pclean, NULL);
2422 } else if (*application_api != '\0') {
2423 errstr = newvstrallocf(errstr,
2424 _("[does not support application-api]"));
2434 " ", device && has_device ? device : "",
2437 " OPTIONS ", options,
2442 dbprintf(_("send request:\n----\n%s\n----\n\n"), req);
2443 secdrv = security_getdriver(auth);
2444 if (secdrv == NULL) {
2445 errstr = newvstrallocf(errstr,
2446 _("[could not find security driver '%s']"), auth);
2451 protocol_sendreq(hostname, secdrv, dumper_get_security_conf, req,
2452 STARTUP_TIMEOUT, sendbackup_response, &response_error);
2457 return (response_error);