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;
126 static dumpfile_t file;
130 security_stream_t *fd;
139 #define NSTREAMS (int)(sizeof(streams) / sizeof(streams[0]))
141 static am_feature_t *our_features = NULL;
142 static char *our_feature_string = NULL;
144 /* buffer to keep partial line from the MESG stream */
146 char *buf; /* buffer holding msg data */
147 size_t size; /* size of alloced buffer */
151 /* local functions */
152 int main(int, char **);
153 static int do_dump(struct databuf *);
154 static void check_options(char *);
155 static void xml_check_options(char *optionstr);
156 static void finish_tapeheader(dumpfile_t *);
157 static ssize_t write_tapeheader(int, dumpfile_t *);
158 static void databuf_init(struct databuf *, int);
159 static int databuf_write(struct databuf *, const void *, size_t);
160 static int databuf_flush(struct databuf *);
161 static void process_dumpeof(void);
162 static void process_dumpline(const char *);
163 static void add_msg_data(const char *, size_t);
164 static void parse_info_line(char *);
165 static void log_msgout(logtype_t);
166 static char * dumper_get_security_conf (char *, void *);
168 static int runcompress(int, pid_t *, comp_t, char *);
169 static int runencrypt(int, pid_t *, encrypt_t);
171 static void sendbackup_response(void *, pkt_t *, security_handle_t *);
172 static int startup_dump(const char *, const char *, const char *, int,
173 const char *, const char *, const char *,
174 const char *, const char *, const char *,
175 const char *, const char *);
176 static void stop_dump(void);
178 static void read_indexfd(void *, void *, ssize_t);
179 static void read_datafd(void *, void *, ssize_t);
180 static void read_mesgfd(void *, void *, ssize_t);
181 static void timeout(time_t);
182 static void timeout_callback(void *);
188 char *compmode = NULL;
189 char *compend = NULL;
190 char *encryptmode = NULL;
191 char *encryptend = NULL;
192 char *decryptmode = NULL;
193 char *decryptend = NULL;
195 /* parse the compression option */
196 if (strstr(options, "srvcomp-best;") != NULL)
197 srvcompress = COMP_BEST;
198 else if (strstr(options, "srvcomp-fast;") != NULL)
199 srvcompress = COMP_FAST;
200 else if ((compmode = strstr(options, "srvcomp-cust=")) != NULL) {
201 compend = strchr(compmode, ';');
203 srvcompress = COMP_SERVER_CUST;
205 srvcompprog = stralloc(compmode + strlen("srvcomp-cust="));
208 } else if ((compmode = strstr(options, "comp-cust=")) != NULL) {
209 compend = strchr(compmode, ';');
211 srvcompress = COMP_CUST;
213 clntcompprog = stralloc(compmode + strlen("comp-cust="));
218 srvcompress = COMP_NONE;
222 /* now parse the encryption option */
223 if ((encryptmode = strstr(options, "encrypt-serv-cust=")) != NULL) {
224 encryptend = strchr(encryptmode, ';');
226 srvencrypt = ENCRYPT_SERV_CUST;
228 srv_encrypt = stralloc(encryptmode + strlen("encrypt-serv-cust="));
231 } else if ((encryptmode = strstr(options, "encrypt-cust=")) != NULL) {
232 encryptend = strchr(encryptmode, ';');
234 srvencrypt = ENCRYPT_CUST;
236 clnt_encrypt = stralloc(encryptmode + strlen("encrypt-cust="));
240 srvencrypt = ENCRYPT_NONE;
242 /* get the decryption option parameter */
243 if ((decryptmode = strstr(options, "server-decrypt-option=")) != NULL) {
244 decryptend = strchr(decryptmode, ';');
247 srv_decrypt_opt = stralloc(decryptmode + strlen("server-decrypt-option="));
250 } else if ((decryptmode = strstr(options, "client-decrypt-option=")) != NULL) {
251 decryptend = strchr(decryptmode, ';');
254 clnt_decrypt_opt = stralloc(decryptmode + strlen("client-decrypt-option="));
259 if (strstr(options, "kencrypt;") != NULL) {
260 dumper_kencrypt = KENCRYPT_WILL_DO;
262 dumper_kencrypt = KENCRYPT_NONE;
275 o = oo = vstralloc("<dle>", strchr(optionstr,'<'), "</dle>", NULL);
277 dle = amxml_parse_node_CHAR(o, &errmsg);
279 error("amxml_parse_node_CHAR failed: %s\n", errmsg);
282 if (dle->compress == COMP_SERVER_FAST) {
283 srvcompress = COMP_FAST;
284 } else if (dle->compress == COMP_SERVER_BEST) {
285 srvcompress = COMP_BEST;
286 } else if (dle->compress == COMP_SERVER_CUST) {
287 srvcompress = COMP_SERVER_CUST;
288 srvcompprog = g_strdup(dle->compprog);
289 } else if (dle->compress == COMP_CUST) {
290 srvcompress = COMP_CUST;
291 clntcompprog = g_strdup(dle->compprog);
293 srvcompress = COMP_NONE;
296 if (dle->encrypt == ENCRYPT_CUST) {
297 srvencrypt = ENCRYPT_CUST;
298 clnt_encrypt = g_strdup(dle->clnt_encrypt);
299 clnt_decrypt_opt = g_strdup(dle->clnt_decrypt_opt);
300 } else if (dle->encrypt == ENCRYPT_SERV_CUST) {
301 srvencrypt = ENCRYPT_SERV_CUST;
302 srv_encrypt = g_strdup(dle->srv_encrypt);
303 srv_decrypt_opt = g_strdup(dle->srv_decrypt_opt);
305 srvencrypt = ENCRYPT_NONE;
317 static struct databuf db;
318 struct cmdargs *cmdargs = NULL;
321 in_port_t header_port;
325 config_overrides_t *cfg_ovr = NULL;
326 char *cfg_opt = NULL;
330 * Configure program for internationalization:
331 * 1) Only set the message locale for now.
332 * 2) Set textdomain for all amanda related programs to "amanda"
333 * We don't want to be forced to support dozens of message catalogs.
335 setlocale(LC_MESSAGES, "C");
336 textdomain("amanda");
338 /* drop root privileges */
339 dumper_setuid = set_root_privs(0);
345 dbopen(DBG_SUBDIR_SERVER);
347 /* Don't die when child closes pipe */
348 signal(SIGPIPE, SIG_IGN);
350 add_amanda_log_handler(amanda_log_stderr);
351 add_amanda_log_handler(amanda_log_trace_log);
353 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
356 set_config_overrides(cfg_ovr);
357 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
359 if (!dumper_setuid) {
360 error(_("dumper must be run setuid root"));
363 if (config_errors(NULL) >= CFGERR_ERRORS) {
364 g_critical(_("errors processing config file"));
367 safe_cd(); /* do this *after* config_init() */
369 check_running_as(RUNNING_AS_ROOT | RUNNING_AS_UID_ONLY);
371 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
373 our_features = am_init_feature_set();
374 our_feature_string = am_feature_to_string(our_features);
376 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
378 _("%s: pid %ld executable %s version %s\n"),
379 get_pname(), (long) getpid(),
383 /* now, make sure we are a valid user */
385 signal(SIGPIPE, SIG_IGN);
387 conf_dtimeout = (time_t)getconf_int(CNF_DTIMEOUT);
393 free_cmdargs(cmdargs);
397 switch(cmdargs->cmd) {
399 if(cmdargs->argc < 2)
400 error(_("error [dumper START: not enough args: timestamp]"));
401 dumper_timestamp = newstralloc(dumper_timestamp, cmdargs->argv[1]);
431 a = 1; /* skip "PORT-DUMP" */
433 if(a >= cmdargs->argc) {
434 error(_("error [dumper PORT-DUMP: not enough args: handle]"));
437 handle = newstralloc(handle, cmdargs->argv[a++]);
439 if(a >= cmdargs->argc) {
440 error(_("error [dumper PORT-DUMP: not enough args: port]"));
443 header_port = (in_port_t)atoi(cmdargs->argv[a++]);
445 if(a >= cmdargs->argc) {
446 error(_("error [dumper PORT-DUMP: not enough args: hostname]"));
449 hostname = newstralloc(hostname, cmdargs->argv[a++]);
451 if(a >= cmdargs->argc) {
452 error(_("error [dumper PORT-DUMP: not enough args: features]"));
455 am_release_feature_set(their_features);
456 their_features = am_string_to_feature(cmdargs->argv[a++]);
458 if(a >= cmdargs->argc) {
459 error(_("error [dumper PORT-DUMP: not enough args: diskname]"));
462 diskname = newstralloc(diskname, cmdargs->argv[a++]);
463 if (qdiskname != NULL)
465 qdiskname = quote_string(diskname);
466 b64disk = amxml_format_tag("disk", diskname);
468 if(a >= cmdargs->argc) {
469 error(_("error [dumper PORT-DUMP: not enough args: device]"));
472 device = newstralloc(device, cmdargs->argv[a++]);
473 b64device = amxml_format_tag("diskdevice", device);
474 if(strcmp(device,"NODEVICE") == 0)
477 if(a >= cmdargs->argc) {
478 error(_("error [dumper PORT-DUMP: not enough args: level]"));
481 level = atoi(cmdargs->argv[a++]);
483 if(a >= cmdargs->argc) {
484 error(_("error [dumper PORT-DUMP: not enough args: dumpdate]"));
487 dumpdate = newstralloc(dumpdate, cmdargs->argv[a++]);
489 if(a >= cmdargs->argc) {
490 error(_("error [dumper PORT-DUMP: not enough args: program]"));
493 progname = newstralloc(progname, cmdargs->argv[a++]);
495 if(a >= cmdargs->argc) {
496 error(_("error [dumper PORT-DUMP: not enough args: amandad_path]"));
499 amandad_path = newstralloc(amandad_path, cmdargs->argv[a++]);
501 if(a >= cmdargs->argc) {
502 error(_("error [dumper PORT-DUMP: not enough args: client_username]"));
504 client_username = newstralloc(client_username, cmdargs->argv[a++]);
506 if(a >= cmdargs->argc) {
507 error(_("error [dumper PORT-DUMP: not enough args: client_port]"));
509 client_port = newstralloc(client_port, cmdargs->argv[a++]);
511 if(a >= cmdargs->argc) {
512 error(_("error [dumper PORT-DUMP: not enough args: ssh_keys]"));
514 ssh_keys = newstralloc(ssh_keys, cmdargs->argv[a++]);
516 if(a >= cmdargs->argc) {
517 error(_("error [dumper PORT-DUMP: not enough args: auth]"));
519 auth = newstralloc(auth, cmdargs->argv[a++]);
521 if(a >= cmdargs->argc) {
522 error(_("error [dumper PORT-DUMP: not enough args: data_path]"));
524 data_path = data_path_from_string(cmdargs->argv[a++]);
526 if(a >= cmdargs->argc) {
527 error(_("error [dumper PORT-DUMP: not enough args: dataport_list]"));
529 dataport_list = newstralloc(dataport_list, cmdargs->argv[a++]);
531 if(a >= cmdargs->argc) {
532 error(_("error [dumper PORT-DUMP: not enough args: options]"));
534 options = newstralloc(options, cmdargs->argv[a++]);
536 if(a != cmdargs->argc) {
537 error(_("error [dumper PORT-DUMP: too many args: %d != %d]"),
542 /* Double-check that 'localhost' resolves properly */
543 if ((res = resolve_hostname("localhost", 0, NULL, NULL) != 0)) {
544 errstr = newvstrallocf(errstr,
545 _("could not resolve localhost: %s"),
547 q = quote_string(errstr);
548 putresult(FAILED, "%s %s\n", handle, q);
549 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
550 dumper_timestamp, level, errstr);
555 /* connect outf to chunker/taper port */
557 g_debug(_("Sending header to localhost:%d\n"), header_port);
558 outfd = stream_client("localhost", header_port,
559 STREAM_BUFSIZE, 0, NULL, 0);
562 errstr = newvstrallocf(errstr, _("port open: %s"),
564 q = quote_string(errstr);
565 putresult(FAILED, "%s %s\n", handle, q);
566 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
567 dumper_timestamp, level, errstr);
571 databuf_init(&db, outfd);
573 if (am_has_feature(their_features, fe_req_xml))
574 xml_check_options(options); /* note: modifies globals */
576 check_options(options); /* note: modifies globals */
578 rc = startup_dump(hostname,
591 q = quote_string(errstr);
592 putresult(rc == 2? FAILED : TRYAGAIN, "%s %s\n",
595 log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname,
596 dumper_timestamp, level, errstr);
600 /* try to clean up any defunct processes, since Amanda doesn't
601 wait() for them explicitly */
602 while(waitpid(-1, NULL, WNOHANG)> 0);
605 amfree(amandad_path);
606 amfree(client_username);
616 if(cmdargs->argc >= 1) {
617 q = quote_string(cmdargs->argv[0]);
619 q = stralloc(_("(no input?)"));
621 putresult(BAD_COMMAND, "%s\n", q);
628 } while(cmdargs->cmd != QUIT);
629 free_cmdargs(cmdargs);
631 log_add(L_INFO, "pid-done %ld", (long)getpid());
633 am_release_feature_set(our_features);
634 amfree(our_feature_string);
636 amfree(dumper_timestamp);
645 amfree(clntcompprog);
647 amfree(clnt_encrypt);
648 amfree(srv_decrypt_opt);
649 amfree(clnt_decrypt_opt);
653 return (0); /* exit */
658 * Initialize a databuf. Takes a writeable file descriptor.
667 db->datain = db->dataout = db->datalimit = NULL;
668 db->compresspid = -1;
674 * Updates the buffer pointer for the input data buffer. The buffer is
675 * written regardless of how much data is present, since we know we
676 * are writing to a socket (to chunker) and there is no need to maintain
685 db->buf = (char *)buf;
686 db->datain = db->datalimit = db->buf + size;
687 db->dataout = db->buf;
688 return databuf_flush(db);
692 * Write out the buffer to chunker.
702 * If there's no data, do nothing.
704 if (db->dataout >= db->datain) {
709 * Write out the buffer
711 written = full_write(db->fd, db->dataout,
712 (size_t)(db->datain - db->dataout));
714 db->dataout += written;
715 dumpbytes += (off_t)written;
717 if (dumpbytes >= (off_t)1024) {
718 dumpsize += (dumpbytes / (off_t)1024);
719 dumpbytes %= (off_t)1024;
722 int save_errno = errno;
723 m = vstrallocf(_("data write: %s"), strerror(save_errno));
725 errstr = quote_string(m);
730 db->datain = db->dataout = db->buf;
734 static int dump_result;
736 #define GOT_INFO_ENDLINE (1 << 0)
737 #define GOT_SIZELINE (1 << 1)
738 #define GOT_ENDLINE (1 << 2)
739 #define HEADER_DONE (1 << 3)
743 process_dumpeof(void)
745 /* process any partial line in msgbuf? !!! */
746 add_msg_data(NULL, 0);
747 if(!ISSET(status, GOT_SIZELINE) && dump_result < 2) {
748 /* make a note if there isn't already a failure */
750 _("? %s: strange [missing size line from sendbackup]\n"),
753 errstr = stralloc(_("missing size line from sendbackup"));
755 dump_result = max(dump_result, 2);
758 if(!ISSET(status, GOT_ENDLINE) && dump_result < 2) {
760 _("? %s: strange [missing end line from sendbackup]\n"),
763 errstr = stralloc(_("missing end line from sendbackup"));
765 dump_result = max(dump_result, 2);
770 * Parse an information line from the client.
771 * We ignore unknown parameters and only remember the last
778 static const struct {
783 { "BACKUP", file.program, SIZEOF(file.program) },
784 { "APPLICATION", file.application, SIZEOF(file.application) },
785 { "RECOVER_CMD", file.recover_cmd, SIZEOF(file.recover_cmd) },
786 { "COMPRESS_SUFFIX", file.comp_suffix, SIZEOF(file.comp_suffix) },
787 { "SERVER_CUSTOM_COMPRESS", file.srvcompprog, SIZEOF(file.srvcompprog) },
788 { "CLIENT_CUSTOM_COMPRESS", file.clntcompprog, SIZEOF(file.clntcompprog) },
789 { "SERVER_ENCRYPT", file.srv_encrypt, SIZEOF(file.srv_encrypt) },
790 { "CLIENT_ENCRYPT", file.clnt_encrypt, SIZEOF(file.clnt_encrypt) },
791 { "SERVER_DECRYPT_OPTION", file.srv_decrypt_opt, SIZEOF(file.srv_decrypt_opt) },
792 { "CLIENT_DECRYPT_OPTION", file.clnt_decrypt_opt, SIZEOF(file.clnt_decrypt_opt) }
797 if (strcmp(str, "end") == 0) {
798 SET(status, GOT_INFO_ENDLINE);
802 name = strtok(str, "=");
805 value = strtok(NULL, "");
809 for (i = 0; i < SIZEOF(fields) / SIZEOF(fields[0]); i++) {
810 if (strcmp(name, fields[i].name) == 0) {
811 strncpy(fields[i].value, value, fields[i].len - 1);
812 fields[i].value[fields[i].len - 1] = '\0';
828 /* normal backup output line */
831 /* sendbackup detected something strange */
832 dump_result = max(dump_result, 1);
835 /* a sendbackup line, just check them all since there are only 5 */
836 tok = strtok(buf, " ");
837 if (tok == NULL || strcmp(tok, "sendbackup:") != 0)
840 tok = strtok(NULL, " ");
844 if (strcmp(tok, "start") == 0) {
848 if (strcmp(tok, "size") == 0) {
849 tok = strtok(NULL, "");
851 origsize = OFF_T_ATOI(tok);
852 SET(status, GOT_SIZELINE);
857 if (strcmp(tok, "no-op") == 0) {
862 if (strcmp(tok, "end") == 0) {
863 SET(status, GOT_ENDLINE);
867 if (strcmp(tok, "warning") == 0) {
868 dump_result = max(dump_result, 1);
872 if (strcmp(tok, "error") == 0) {
873 SET(status, GOT_ENDLINE);
874 dump_result = max(dump_result, 2);
876 tok = strtok(NULL, "");
877 if (!errstr) { /* report first error line */
878 if (tok == NULL || *tok != '[') {
879 errstr = newvstrallocf(errstr, _("bad remote error: %s"),
884 tok++; /* skip over '[' */
885 if ((enderr = strchr(tok, ']')) != NULL)
887 errstr = newstralloc(errstr, tok);
893 if (strcmp(tok, "info") == 0) {
894 tok = strtok(NULL, "");
896 parse_info_line(tok);
899 /* else we fall through to bad line */
903 g_fprintf(errf, "??");
904 dump_result = max(dump_result, 1);
907 g_fprintf(errf, "%s\n", str);
920 buflen = strlen(msg.buf);
925 * If our argument is NULL, then we need to flush out any remaining
931 g_fprintf(errf,_("? %s: error [partial line in msgbuf: %zu bytes]\n"),
932 get_pname(), buflen);
933 g_fprintf(errf,_("? %s: error [partial line in msgbuf: \"%s\"]\n"),
934 get_pname(), msg.buf);
940 * Expand the buffer if it can't hold the new contents.
942 if ((buflen + len + 1) > msg.size) {
946 /* round up to next y, where y is a power of 2 */
947 #define ROUND(x, y) (((x) + (y) - 1) & ~((y) - 1))
949 newsize = ROUND(buflen + (ssize_t)len + 1, 256);
950 newbuf = alloc(newsize);
952 if (msg.buf != NULL) {
953 strncpy(newbuf, msg.buf, newsize);
962 * If there was a partial line from the last call, then
963 * append the new data to the end.
965 strncat(msg.buf, str, len);
968 * Process all lines in the buffer
969 * scanning line for unqouted newline.
971 for (ch = line = msg.buf; *ch != '\0'; ch++) {
974 * Found a newline. Terminate and process line.
977 process_dumpline(line);
983 * If we did not process all of the data, move it to the front
984 * of the buffer so it is there next time.
987 buflen = strlen(line);
988 memmove(msg.buf, line, (size_t)buflen + 1);
1002 if (fseeko(errf, 0L, SEEK_SET) < 0) {
1003 dbprintf(_("log_msgout: warning - seek failed: %s\n"), strerror(errno));
1005 while ((line = agets(errf)) != NULL) {
1006 if (line[0] != '\0') {
1007 log_add(typ, "%s", line);
1018 * Fill in the rest of the tape header
1025 assert(ISSET(status, HEADER_DONE));
1027 file->type = F_DUMPFILE;
1028 strncpy(file->datestamp, dumper_timestamp, sizeof(file->datestamp) - 1);
1029 strncpy(file->name, hostname, SIZEOF(file->name) - 1);
1030 strncpy(file->disk, diskname, SIZEOF(file->disk) - 1);
1031 file->dumplevel = level;
1032 file->blocksize = DISK_BLOCK_BYTES;
1035 * If we're doing the compression here, we need to override what
1036 * sendbackup told us the compression was.
1038 if (srvcompress != COMP_NONE) {
1039 file->compressed = 1;
1040 #ifndef UNCOMPRESS_OPT
1041 #define UNCOMPRESS_OPT ""
1043 if (srvcompress == COMP_SERVER_CUST) {
1044 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1045 " %s %s |", srvcompprog, "-d");
1046 strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1);
1047 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1048 strncpy(file->srvcompprog, srvcompprog, SIZEOF(file->srvcompprog) - 1);
1049 file->srvcompprog[SIZEOF(file->srvcompprog) - 1] = '\0';
1050 } else if ( srvcompress == COMP_CUST ) {
1051 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1052 " %s %s |", clntcompprog, "-d");
1053 strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1);
1054 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1055 strncpy(file->clntcompprog, clntcompprog, SIZEOF(file->clntcompprog));
1056 file->clntcompprog[SIZEOF(file->clntcompprog) - 1] = '\0';
1058 g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd),
1059 " %s %s |", UNCOMPRESS_PATH, UNCOMPRESS_OPT);
1060 strncpy(file->comp_suffix, COMPRESS_SUFFIX,SIZEOF(file->comp_suffix) - 1);
1061 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1064 if (file->comp_suffix[0] == '\0') {
1065 file->compressed = 0;
1066 assert(SIZEOF(file->comp_suffix) >= 2);
1067 strncpy(file->comp_suffix, "N", SIZEOF(file->comp_suffix) - 1);
1068 file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0';
1070 file->compressed = 1;
1073 /* take care of the encryption header here */
1074 if (srvencrypt != ENCRYPT_NONE) {
1076 if (srvencrypt == ENCRYPT_SERV_CUST) {
1077 if (srv_decrypt_opt) {
1078 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1079 " %s %s |", srv_encrypt, srv_decrypt_opt);
1080 strncpy(file->srv_decrypt_opt, srv_decrypt_opt, SIZEOF(file->srv_decrypt_opt) - 1);
1081 file->srv_decrypt_opt[SIZEOF(file->srv_decrypt_opt) - 1] = '\0';
1083 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1084 " %s |", srv_encrypt);
1085 file->srv_decrypt_opt[0] = '\0';
1087 strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1);
1088 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1089 strncpy(file->srv_encrypt, srv_encrypt, SIZEOF(file->srv_encrypt) - 1);
1090 file->srv_encrypt[SIZEOF(file->srv_encrypt) - 1] = '\0';
1091 } else if ( srvencrypt == ENCRYPT_CUST ) {
1092 if (clnt_decrypt_opt) {
1093 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1094 " %s %s |", clnt_encrypt, clnt_decrypt_opt);
1095 strncpy(file->clnt_decrypt_opt, clnt_decrypt_opt,
1096 SIZEOF(file->clnt_decrypt_opt));
1097 file->clnt_decrypt_opt[SIZEOF(file->clnt_decrypt_opt) - 1] = '\0';
1099 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1100 " %s |", clnt_encrypt);
1101 file->clnt_decrypt_opt[0] = '\0';
1103 g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd),
1104 " %s %s |", clnt_encrypt, clnt_decrypt_opt);
1105 strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1);
1106 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1107 strncpy(file->clnt_encrypt, clnt_encrypt, SIZEOF(file->clnt_encrypt) - 1);
1108 file->clnt_encrypt[SIZEOF(file->clnt_encrypt) - 1] = '\0';
1111 if (file->encrypt_suffix[0] == '\0') {
1112 file->encrypted = 0;
1113 assert(SIZEOF(file->encrypt_suffix) >= 2);
1114 strncpy(file->encrypt_suffix, "N", SIZEOF(file->encrypt_suffix) - 1);
1115 file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0';
1121 file->dle_str = stralloc(dle_str);
1123 file->dle_str = NULL;
1127 * Send an Amanda dump header to the output file.
1137 if (debug_dumper > 1)
1138 dump_dumpfile_t(file);
1139 buffer = build_header(file, NULL, DISK_BLOCK_BYTES);
1140 if (!buffer) /* this shouldn't happen */
1141 error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES);
1143 written = full_write(outfd, buffer, DISK_BLOCK_BYTES);
1145 if(written == DISK_BLOCK_BYTES)
1157 char *indexfile_tmp = NULL;
1158 char *indexfile_real = NULL;
1159 char level_str[NUM_STR_SIZE];
1163 double dumptime; /* Time dump took in secs */
1164 char *errfname = NULL;
1165 pid_t indexpid = -1;
1170 if (msg.buf) msg.buf[0] = '\0'; /* reset msg buffer */
1173 dumpbytes = dumpsize = headersize = origsize = (off_t)0;
1176 g_snprintf(level_str, SIZEOF(level_str), "%d", level);
1177 fn = sanitise_filename(diskname);
1178 errfname = newvstralloc(errfname,
1186 if((errf = fopen(errfname, "w+")) == NULL) {
1187 errstr = newvstrallocf(errstr, "errfile open \"%s\": %s",
1188 errfname, strerror(errno));
1192 unlink(errfname); /* so it goes away on close */
1195 if (streams[INDEXFD].fd != NULL) {
1196 indexfile_real = getindexfname(hostname, diskname, dumper_timestamp, level);
1197 indexfile_tmp = stralloc2(indexfile_real, ".tmp");
1199 if (mkpdir(indexfile_tmp, 0755, (uid_t)-1, (gid_t)-1) == -1) {
1200 errstr = newvstrallocf(errstr,
1201 _("err create %s: %s"),
1204 amfree(indexfile_real);
1205 amfree(indexfile_tmp);
1208 indexout = open(indexfile_tmp, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1209 if (indexout == -1) {
1210 errstr = newvstrallocf(errstr, _("err open %s: %s"),
1211 indexfile_tmp, strerror(errno));
1214 if (runcompress(indexout, &indexpid, COMP_BEST, "index compress") < 0) {
1221 * Schedule the indexfd for relaying to the index file
1223 security_stream_read(streams[INDEXFD].fd, read_indexfd, &indexout);
1227 * We only need to process messages initially. Once we have done
1228 * the header, we will start processing data too.
1230 security_stream_read(streams[MESGFD].fd, read_mesgfd, db);
1234 * Setup a read timeout
1236 timeout(conf_dtimeout);
1239 * Start the event loop. This will exit when all three events
1240 * (read the mesgfd, read the datafd, and timeout) are removed.
1244 if (!ISSET(status, HEADER_DONE)) {
1245 dump_result = max(dump_result, 2);
1246 if (!errstr) errstr = stralloc(_("got no header information"));
1249 dumpsize -= headersize; /* don't count the header */
1250 if (dumpsize <= (off_t)0 && data_path == DATA_PATH_AMANDA) {
1251 dumpsize = (off_t)0;
1252 dump_result = max(dump_result, 2);
1253 if (!errstr) errstr = stralloc(_("got no data"));
1256 if (data_path == DATA_PATH_DIRECTTCP) {
1257 dumpsize = origsize;
1260 if (!ISSET(status, HEADER_DONE)) {
1261 dump_result = max(dump_result, 2);
1262 if (!errstr) errstr = stralloc(_("got no header information"));
1265 if (dumpsize == 0 && data_path == DATA_PATH_AMANDA) {
1266 dump_result = max(dump_result, 2);
1267 if (!errstr) errstr = stralloc(_("got no data"));
1270 if (dump_result > 1)
1273 runtime = stopclock();
1274 dumptime = g_timeval_to_double(runtime);
1277 errstr = alloc(128);
1278 g_snprintf(errstr, 128, _("sec %s kb %lld kps %3.1lf orig-kb %lld"),
1279 walltime_str(runtime),
1280 (long long)dumpsize,
1281 (isnormal(dumptime) ? ((double)dumpsize / (double)dumptime) : 0.0),
1282 (long long)origsize);
1283 m = vstrallocf("[%s]", errstr);
1284 q = quote_string(m);
1286 putresult(DONE, _("%s %lld %lld %lu %s\n"), handle,
1287 (long long)origsize,
1288 (long long)dumpsize,
1289 (unsigned long)((double)dumptime+0.5), q);
1292 switch(dump_result) {
1294 log_add(L_SUCCESS, "%s %s %s %d [%s]", hostname, qdiskname, dumper_timestamp, level, errstr);
1299 log_start_multiline();
1300 log_add(L_STRANGE, "%s %s %d [%s]", hostname, qdiskname, level, errstr);
1301 log_msgout(L_STRANGE);
1302 log_end_multiline();
1307 if (errf) afclose(errf);
1309 if (data_path == DATA_PATH_AMANDA)
1312 if (indexfile_tmp) {
1313 amwait_t index_status;
1315 /*@i@*/ aclose(indexout);
1316 waitpid(indexpid,&index_status,0);
1317 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1318 if (rename(indexfile_tmp, indexfile_real) != 0) {
1319 log_add(L_WARNING, _("could not rename \"%s\" to \"%s\": %s"),
1320 indexfile_tmp, indexfile_real, strerror(errno));
1322 amfree(indexfile_tmp);
1323 amfree(indexfile_real);
1326 if(db->compresspid != -1) {
1327 waitpid(db->compresspid,NULL,0);
1328 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1330 if(db->encryptpid != -1) {
1331 waitpid(db->encryptpid,NULL,0);
1332 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1336 dumpfile_free_data(&file);
1341 m = vstrallocf("[%s]", errstr);
1342 q = quote_string(m);
1343 putresult(FAILED, "%s %s\n", handle, q);
1348 /* kill all child process */
1349 if (db->compresspid != -1) {
1350 g_fprintf(stderr,_("%s: kill compress command\n"),get_pname());
1351 if (kill(db->compresspid, SIGTERM) < 0) {
1352 if (errno != ESRCH) {
1353 g_fprintf(stderr,_("%s: can't kill compress command: %s\n"),
1354 get_pname(), strerror(errno));
1356 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1360 waitpid(db->compresspid,NULL,0);
1361 log_add(L_INFO, "pid-done %ld", (long)db->compresspid);
1365 if (db->encryptpid != -1) {
1366 g_fprintf(stderr,_("%s: kill encrypt command\n"),get_pname());
1367 if (kill(db->encryptpid, SIGTERM) < 0) {
1368 if (errno != ESRCH) {
1369 g_fprintf(stderr,_("%s: can't kill encrypt command: %s\n"),
1370 get_pname(), strerror(errno));
1372 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1376 waitpid(db->encryptpid,NULL,0);
1377 log_add(L_INFO, "pid-done %ld", (long)db->encryptpid);
1381 if (indexpid != -1) {
1382 g_fprintf(stderr,_("%s: kill index command\n"),get_pname());
1383 if (kill(indexpid, SIGTERM) < 0) {
1384 if (errno != ESRCH) {
1385 g_fprintf(stderr,_("%s: can't kill index command: %s\n"),
1386 get_pname(),strerror(errno));
1388 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1392 waitpid(indexpid,NULL,0);
1393 log_add(L_INFO, "pid-done %ld", (long)indexpid);
1397 log_start_multiline();
1398 log_add(L_FAIL, _("%s %s %s %d [%s]"), hostname, qdiskname, dumper_timestamp,
1403 log_end_multiline();
1405 if (errf) afclose(errf);
1407 if (indexfile_tmp) {
1408 unlink(indexfile_tmp);
1409 amfree(indexfile_tmp);
1410 amfree(indexfile_real);
1417 * Callback for reads on the mesgfd stream
1425 struct databuf *db = cookie;
1431 errstr = newvstrallocf(errstr, _("mesg read: %s"),
1432 security_stream_geterror(streams[MESGFD].fd));
1439 * EOF. Just shut down the mesg stream.
1442 security_stream_close(streams[MESGFD].fd);
1443 streams[MESGFD].fd = NULL;
1445 * If the data fd and index fd has also shut down, then we're done.
1447 if ((set_datafd == 0 || streams[DATAFD].fd == NULL) &&
1448 streams[INDEXFD].fd == NULL)
1453 assert(buf != NULL);
1454 add_msg_data(buf, (size_t)size);
1455 security_stream_read(streams[MESGFD].fd, read_mesgfd, cookie);
1459 if (ISSET(status, GOT_INFO_ENDLINE) && !ISSET(status, HEADER_DONE)) {
1460 /* Use the first in the dataport_list */
1461 in_port_t data_port;
1462 char *data_host = dataport_list;
1463 char *s= strchr(dataport_list, ':');
1466 data_port = atoi(s);
1468 SET(status, HEADER_DONE);
1469 /* time to do the header */
1470 finish_tapeheader(&file);
1471 if (write_tapeheader(db->fd, &file)) {
1472 errstr = newvstrallocf(errstr, _("write_tapeheader: %s"),
1476 dumpfile_free_data(&file);
1479 dumpfile_free_data(&file);
1481 if (data_path == DATA_PATH_AMANDA) {
1482 g_debug(_("Sending data to %s:%d\n"), data_host, data_port);
1483 db->fd = stream_client(data_host, data_port,
1484 STREAM_BUFSIZE, 0, NULL, 0);
1486 errstr = newvstrallocf(errstr,
1487 _("Can't opendata output stream: %s"),
1495 dumpsize += (off_t)DISK_BLOCK_KB;
1496 headersize += (off_t)DISK_BLOCK_KB;
1498 if (srvencrypt == ENCRYPT_SERV_CUST) {
1499 if (runencrypt(db->fd, &db->encryptpid, srvencrypt) < 0) {
1506 * Now, setup the compress for the data output, and start
1507 * reading the datafd.
1509 if ((srvcompress != COMP_NONE) && (srvcompress != COMP_CUST)) {
1510 if (runcompress(db->fd, &db->compresspid, srvcompress, "data compress") < 0) {
1516 security_stream_read(streams[DATAFD].fd, read_datafd, db);
1521 * Reset the timeout for future reads
1523 timeout(conf_dtimeout);
1527 * Callback for reads on the datafd stream
1535 struct databuf *db = cookie;
1540 * The read failed. Error out
1543 errstr = newvstrallocf(errstr, _("data read: %s"),
1544 security_stream_geterror(streams[DATAFD].fd));
1551 /* The header had better be written at this point */
1552 assert(ISSET(status, HEADER_DONE));
1555 * EOF. Stop and return.
1559 if (dumpbytes != (off_t)0) {
1560 dumpsize += (off_t)1;
1562 security_stream_close(streams[DATAFD].fd);
1563 streams[DATAFD].fd = NULL;
1566 * If the mesg fd and index fd has also shut down, then we're done.
1568 if (streams[MESGFD].fd == NULL && streams[INDEXFD].fd == NULL)
1574 * We read something. Add it to the databuf and reschedule for
1577 assert(buf != NULL);
1578 if (databuf_write(db, buf, (size_t)size) < 0) {
1579 int save_errno = errno;
1580 errstr = newvstrallocf(errstr, _("data write: %s"), strerror(save_errno));
1587 * Reset the timeout for future reads
1589 timeout(conf_dtimeout);
1591 security_stream_read(streams[DATAFD].fd, read_datafd, cookie);
1595 * Callback for reads on the index stream
1605 assert(cookie != NULL);
1606 fd = *(int *)cookie;
1609 errstr = newvstrallocf(errstr, _("index read: %s"),
1610 security_stream_geterror(streams[INDEXFD].fd));
1617 * EOF. Stop and return.
1620 security_stream_close(streams[INDEXFD].fd);
1621 streams[INDEXFD].fd = NULL;
1623 * If the mesg fd has also shut down, then we're done.
1625 if ((set_datafd == 0 || streams[DATAFD].fd == NULL) &&
1626 streams[MESGFD].fd == NULL)
1632 assert(buf != NULL);
1635 * We ignore error while writing to the index file.
1637 if (full_write(fd, buf, (size_t)size) < (size_t)size) {
1638 /* Ignore error, but schedule another read. */
1639 if(indexfderror == 0) {
1641 log_add(L_INFO, _("Index corrupted for %s:%s"), hostname, qdiskname);
1644 security_stream_read(streams[INDEXFD].fd, read_indexfd, cookie);
1648 handle_filter_stderr(
1651 filter_t *filter = cookie;
1656 event_release(filter->event);
1658 if (filter->buffer == NULL) {
1659 /* allocate initial buffer */
1660 filter->buffer = g_malloc(2048);
1663 filter->allocated_size = 2048;
1664 } else if (filter->first > 0) {
1665 if (filter->allocated_size - filter->size - filter->first < 1024) {
1666 memmove(filter->buffer, filter->buffer + filter->first,
1670 } else if (filter->allocated_size - filter->size < 1024) {
1671 /* double the size of the buffer */
1672 filter->allocated_size *= 2;
1673 filter->buffer = g_realloc(filter->buffer, filter->allocated_size);
1676 nread = read(filter->fd, filter->buffer + filter->first + filter->size,
1677 filter->allocated_size - filter->first - filter->size - 2);
1680 dump_result = max(dump_result, 2);
1685 if (filter->size > 0 && filter->buffer[filter->first + filter->size - 1] != '\n') {
1686 /* Add a '\n' at end of buffer */
1687 filter->buffer[filter->first + filter->size] = '\n';
1691 filter->size += nread;
1694 /* process all complete lines */
1695 b = filter->buffer + filter->first;
1696 filter->buffer[filter->first + filter->size] = '\0';
1697 while (b < filter->buffer + filter->first + filter->size &&
1698 (p = strchr(b, '\n')) != NULL) {
1700 g_fprintf(errf, _("? %s: %s\n"), filter->name, b);
1701 if (errstr == NULL) {
1702 errstr = stralloc(b);
1705 filter->first += len;
1706 filter->size -= len;
1711 g_free(filter->buffer);
1714 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1715 handle_filter_stderr, filter);
1720 * Startup a timeout in the event handler. If the arg is 0,
1721 * then remove the timeout.
1727 static event_handle_t *ev_timeout = NULL;
1730 * First, remove a timeout if one is active.
1732 if (ev_timeout != NULL) {
1733 event_release(ev_timeout);
1738 * Now, schedule a new one if 'seconds' is greater than 0
1741 ev_timeout = event_register((event_id_t)seconds, EV_TIME, timeout_callback, NULL);
1745 * This is the callback for timeout(). If this is reached, then we
1746 * have a data timeout.
1752 (void)unused; /* Quiet unused parameter warning */
1754 assert(unused == NULL);
1755 errstr = newstralloc(errstr, _("data timeout"));
1761 * This is called when everything needs to shut down so event_loop()
1768 struct cmdargs *cmdargs = NULL;
1770 /* Check if I have a pending ABORT command */
1771 cmdargs = get_pending_cmd();
1773 if (cmdargs->cmd != ABORT) {
1777 errstr = stralloc(cmdargs->argv[1]);
1778 free_cmdargs(cmdargs);
1781 for (i = 0; i < NSTREAMS; i++) {
1782 if (streams[i].fd != NULL) {
1783 security_stream_close(streams[i].fd);
1784 streams[i].fd = NULL;
1793 * Runs compress with the first arg as its stdout. Returns
1794 * 0 on success or negative if error, and it's pid via the second
1795 * argument. The outfd arg is dup2'd to the pipe to the compress
1805 int outpipe[2], rval;
1810 assert(pid != NULL);
1812 /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */
1813 if (pipe(outpipe) < 0) {
1814 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1818 /* errpipe[0] is pipe's output, outpipe[1] is input. */
1819 if (pipe(errpipe) < 0) {
1820 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1824 switch (*pid = fork()) {
1826 errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno));
1833 rval = dup2(outpipe[1], outfd);
1835 errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno));
1839 filter = g_new0(filter_t, 1);
1840 filter->fd = errpipe[0];
1841 filter->name = name;
1842 filter->buffer = NULL;
1844 filter->allocated_size = 0;
1845 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1846 handle_filter_stderr, filter);
1847 g_debug("event register %s %d", name, filter->fd);
1852 if (dup2(outpipe[0], 0) < 0) {
1853 error(_("err dup2 in: %s"), strerror(errno));
1856 if (dup2(outfd, 1) == -1) {
1857 error(_("err dup2 out: %s"), strerror(errno));
1860 if (dup2(errpipe[1], 2) == -1) {
1861 error(_("err dup2 err: %s"), strerror(errno));
1864 if (comptype != COMP_SERVER_CUST) {
1865 char *base = stralloc(COMPRESS_PATH);
1866 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1870 execlp(COMPRESS_PATH, COMPRESS_PATH, ( comptype == COMP_BEST ?
1871 COMPRESS_BEST_OPT : COMPRESS_FAST_OPT), (char *)NULL);
1872 error(_("error: couldn't exec %s: %s"), COMPRESS_PATH, strerror(errno));
1874 } else if (*srvcompprog) {
1875 char *base = stralloc(srvcompprog);
1876 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1880 execlp(srvcompprog, srvcompprog, (char *)0);
1881 error(_("error: couldn't exec server custom compression '%s'.\n"), srvcompprog);
1890 * Runs encrypt with the first arg as its stdout. Returns
1891 * 0 on success or negative if error, and it's pid via the second
1892 * argument. The outfd arg is dup2'd to the pipe to the encrypt
1899 encrypt_t encrypttype)
1901 int outpipe[2], rval;
1906 assert(pid != NULL);
1908 /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */
1909 if (pipe(outpipe) < 0) {
1910 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1914 /* errpipe[0] is pipe's output, outpipe[1] is input. */
1915 if (pipe(errpipe) < 0) {
1916 errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno));
1920 switch (*pid = fork()) {
1922 errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno));
1929 rval = dup2(outpipe[1], outfd);
1931 errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno));
1935 filter = g_new0(filter_t, 1);
1936 filter->fd = errpipe[0];
1937 filter->name = "encrypt";
1938 filter->buffer = NULL;
1940 filter->allocated_size = 0;
1941 filter->event = event_register((event_id_t)filter->fd, EV_READFD,
1942 handle_filter_stderr, filter);
1943 g_debug("event register %s %d", "encrypt data", filter->fd);
1947 if (dup2(outpipe[0], 0) < 0) {
1948 error(_("err dup2 in: %s"), strerror(errno));
1951 if (dup2(outfd, 1) < 0 ) {
1952 error(_("err dup2 out: %s"), strerror(errno));
1955 if (dup2(errpipe[1], 2) == -1) {
1956 error(_("err dup2 err: %s"), strerror(errno));
1960 base = stralloc(srv_encrypt);
1961 log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid());
1964 if ((encrypttype == ENCRYPT_SERV_CUST) && *srv_encrypt) {
1966 execlp(srv_encrypt, srv_encrypt, (char *)0);
1967 error(_("error: couldn't exec server custom encryption '%s'.\n"), srv_encrypt);
1977 /* -------------------- */
1980 sendbackup_response(
1983 security_handle_t * sech)
1985 int ports[NSTREAMS], *response_error = datap, i;
1990 assert(response_error != NULL);
1991 assert(sech != NULL);
1993 security_close_connection(sech, hostname);
1996 errstr = newvstrallocf(errstr, _("[request failed: %s]"),
1997 security_geterror(sech));
1998 *response_error = 1;
2003 memset(ports, 0, SIZEOF(ports));
2004 if (pkt->type == P_NAK) {
2005 #if defined(PACKET_DEBUG)
2006 g_fprintf(stderr, _("got nak response:\n----\n%s\n----\n\n"), pkt->body);
2009 tok = strtok(pkt->body, " ");
2010 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2013 tok = strtok(NULL, "\n");
2015 errstr = newvstrallocf(errstr, "NAK: %s", tok);
2016 *response_error = 1;
2019 errstr = newvstrallocf(errstr, "request NAK");
2020 *response_error = 2;
2025 if (pkt->type != P_REP) {
2026 errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"),
2027 pkt_type2str(pkt->type), pkt->body);
2028 *response_error = 1;
2032 dbprintf(_("got response:\n----\n%s\n----\n\n"), pkt->body);
2034 for(i = 0; i < NSTREAMS; i++) {
2036 streams[i].fd = NULL;
2040 while((tok = strtok(p, " \n")) != NULL) {
2044 * Error response packets have "ERROR" followed by the error message
2045 * followed by a newline.
2047 if (strcmp(tok, "ERROR") == 0) {
2048 tok = strtok(NULL, "\n");
2050 tok = _("[bogus error packet]");
2051 errstr = newvstrallocf(errstr, "%s", tok);
2052 *response_error = 2;
2057 * Regular packets have CONNECT followed by three streams
2059 if (strcmp(tok, "CONNECT") == 0) {
2062 * Parse the three stream specifiers out of the packet.
2064 for (i = 0; i < NSTREAMS; i++) {
2065 tok = strtok(NULL, " ");
2066 if (tok == NULL || strcmp(tok, streams[i].name) != 0) {
2068 _("CONNECT token is \"%s\": expected \"%s\""),
2069 tok ? tok : "(null)",
2073 tok = strtok(NULL, " \n");
2074 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2076 _("CONNECT %s token is \"%s\": expected a port number"),
2077 streams[i].name, tok ? tok : "(null)");
2085 * OPTIONS [options string] '\n'
2087 if (strcmp(tok, "OPTIONS") == 0) {
2088 tok = strtok(NULL, "\n");
2090 extra = vstrallocf(_("OPTIONS token is missing"));
2094 while((p = strchr(tok, ';')) != NULL) {
2097 if(strncmp_const_skip(tok, "features=", tok, ch) == 0) {
2098 char *u = strchr(tok, ';');
2101 am_release_feature_set(their_features);
2102 if((their_features = am_string_to_feature(tok)) == NULL) {
2103 errstr = newvstrallocf(errstr,
2104 _("OPTIONS: bad features value: %s"),
2116 extra = vstrallocf(_("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""),
2117 tok ? tok : "(null)");
2121 if (dumper_kencrypt == KENCRYPT_WILL_DO)
2122 dumper_kencrypt = KENCRYPT_YES;
2125 * Connect the streams to their remote ports
2127 for (i = 0; i < NSTREAMS; i++) {
2130 streams[i].fd = security_stream_client(sech, ports[i]);
2131 if (streams[i].fd == NULL) {
2132 errstr = newvstrallocf(errstr,
2133 _("[could not connect %s stream: %s]"),
2135 security_geterror(sech));
2141 * Authenticate the streams
2143 for (i = 0; i < NSTREAMS; i++) {
2144 if (streams[i].fd == NULL)
2146 if (security_stream_auth(streams[i].fd) < 0) {
2147 errstr = newvstrallocf(errstr,
2148 _("[could not authenticate %s stream: %s]"),
2150 security_stream_geterror(streams[i].fd));
2156 * The MESGFD and DATAFD streams are mandatory. If we didn't get
2159 if (streams[MESGFD].fd == NULL || streams[DATAFD].fd == NULL) {
2160 errstr = newvstrallocf(errstr, _("[couldn't open MESG or INDEX streams]"));
2164 /* everything worked */
2165 *response_error = 0;
2169 errstr = newvstrallocf(errstr,
2170 _("[parse of reply message failed: %s]"),
2171 extra ? extra : _("(no additional information)"));
2173 *response_error = 2;
2178 *response_error = 1;
2182 dumper_get_security_conf(
2186 (void)arg; /* Quiet unused parameter warning */
2188 if(!string || !*string)
2191 if(strcmp(string, "krb5principal")==0) {
2192 return(getconf_str(CNF_KRB5PRINCIPAL));
2193 } else if(strcmp(string, "krb5keytab")==0) {
2194 return(getconf_str(CNF_KRB5KEYTAB));
2195 } else if(strcmp(string, "amandad_path")==0) {
2196 return (amandad_path);
2197 } else if(strcmp(string, "client_username")==0) {
2198 return (client_username);
2199 } else if(strcmp(string, "client_port")==0) {
2200 return (client_port);
2201 } else if(strcmp(string, "ssh_keys")==0) {
2203 } else if(strcmp(string, "kencrypt")==0) {
2204 if (dumper_kencrypt == KENCRYPT_YES)
2214 const char *hostname,
2218 const char *dumpdate,
2219 const char *progname,
2220 const char *amandad_path,
2221 const char *client_username,
2222 const char *client_port,
2223 const char *ssh_keys,
2225 const char *options)
2227 char level_string[NUM_STR_SIZE];
2231 const security_driver_t *secdrv;
2232 char *application_api;
2238 (void)disk; /* Quiet unused parameter warning */
2239 (void)amandad_path; /* Quiet unused parameter warning */
2240 (void)client_username; /* Quiet unused parameter warning */
2241 (void)client_port; /* Quiet unused parameter warning */
2242 (void)ssh_keys; /* Quiet unused parameter warning */
2243 (void)auth; /* Quiet unused parameter warning */
2245 has_features = am_has_feature(their_features, fe_req_options_features);
2246 has_hostname = am_has_feature(their_features, fe_req_options_hostname);
2247 has_config = am_has_feature(their_features, fe_req_options_config);
2248 has_device = am_has_feature(their_features, fe_sendbackup_req_device);
2251 * Default to bsd authentication if none specified. This is gross.
2253 * Options really need to be pre-parsed into some sort of structure
2254 * much earlier, and then flattened out again before transmission.
2257 g_snprintf(level_string, SIZEOF(level_string), "%d", level);
2258 if(strcmp(progname, "DUMP") == 0
2259 || strcmp(progname, "GNUTAR") == 0) {
2260 application_api = "";
2262 application_api = "BACKUP ";
2264 req = vstralloc("SERVICE sendbackup\n",
2266 has_features ? "features=" : "",
2267 has_features ? our_feature_string : "",
2268 has_features ? ";" : "",
2269 has_hostname ? "hostname=" : "",
2270 has_hostname ? hostname : "",
2271 has_hostname ? ";" : "",
2272 has_config ? "config=" : "",
2273 has_config ? get_config_name() : "",
2274 has_config ? ";" : "",
2279 if (am_has_feature(their_features, fe_req_xml)) {
2282 vstrextend(&p, "<dle>\n", NULL);
2283 if (*application_api != '\0') {
2284 vstrextend(&p, " <program>APPLICATION</program>\n", NULL);
2286 vstrextend(&p, " <program>", progname, "</program>\n", NULL);
2288 vstrextend(&p, " ", b64disk, "\n", NULL);
2289 if (device && has_device) {
2290 vstrextend(&p, " ", b64device, "\n",
2293 vstrextend(&p, " <level>", level_string, "</level>\n", NULL);
2294 vstrextend(&p, options+1, "</dle>\n", NULL);
2295 pclean = clean_dle_str_for_client(p);
2296 vstrextend(&req, pclean, NULL);
2299 } else if (*application_api != '\0') {
2300 errstr = newvstrallocf(errstr,
2301 _("[does not support application-api]"));
2305 authopt = strstr(options, "auth=");
2312 " ", device && has_device ? device : "",
2315 " OPTIONS ", options,
2320 dbprintf(_("send request:\n----\n%s\n----\n\n"), req);
2321 secdrv = security_getdriver(auth);
2322 if (secdrv == NULL) {
2323 errstr = newvstrallocf(errstr,
2324 _("[could not find security driver '%s']"), auth);
2329 protocol_sendreq(hostname, secdrv, dumper_get_security_conf, req,
2330 STARTUP_TIMEOUT, sendbackup_response, &response_error);
2335 return (response_error);