X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=server-src%2Fdumper.c;h=996c9ddf46c947b87a606b9d87d3e4880f7678f6;hb=b116e9366c7b2ea2c2eb53b0a13df4090e176235;hp=6afc7c052b0f6b10025c166aefce79b5e193572f;hpb=2df780bff19c457b0debb7adc29972a0bc2a5dc2;p=debian%2Famanda diff --git a/server-src/dumper.c b/server-src/dumper.c index 6afc7c0..996c9dd 100644 --- a/server-src/dumper.c +++ b/server-src/dumper.c @@ -1,6 +1,6 @@ /* * Amanda, The Advanced Maryland Automatic Network Disk Archiver - * Copyright (c) 1991-1998 University of Maryland at College Park + * Copyright (c) 1991-1999 University of Maryland at College Park * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its @@ -23,7 +23,7 @@ * Authors: the Amanda Development Team. Its members are listed in a * file named AUTHORS, in the root directory of this distribution. */ -/* $Id: dumper.c,v 1.75.2.14.2.7.2.17.2.4 2005/09/20 21:31:52 jrjackson Exp $ +/* $Id: dumper.c,v 1.190 2006/08/30 19:53:57 martinea Exp $ * * requests remote amandad processes to dump filesystems */ @@ -32,23 +32,24 @@ #include "arglist.h" #include "clock.h" #include "conffile.h" +#include "event.h" #include "logfile.h" +#include "packet.h" #include "protocol.h" +#include "security.h" #include "stream.h" -#include "token.h" -#include "version.h" #include "fileheader.h" #include "amfeatures.h" #include "server_util.h" -#include "holding.h" - -#ifdef KRB4_SECURITY -#include "dumper-krb4.c" -#else -#define NAUGHTY_BITS_INITIALIZE /* I'd tell you what these do */ -#define NAUGHTY_BITS /* but then I'd have to kill you */ -#endif +#include "util.h" +#include "timestamp.h" +#include "amxml.h" +#define dumper_debug(i,x) do { \ + if ((i) <= debug_dumper) { \ + dbprintf(x); \ + } \ +} while (0) #ifndef SEEK_SET #define SEEK_SET 0 @@ -59,450 +60,520 @@ #endif #define CONNECT_TIMEOUT 5*60 -#define MESGBUF_SIZE 4*1024 #define STARTUP_TIMEOUT 60 -int interactive; -char *handle = NULL; - -char databuf[DISK_BLOCK_BYTES]; -char mesgbuf[MESGBUF_SIZE+1]; -char *errstr = NULL; -char *datain; /* where to read in data */ -char *dataout; /* where to write out data */ -char *datalimit; /* end of the data area */ -int abort_pending; -long dumpsize; /* total size of dump */ -long dumpbytes; -long origsize; -long filesize; /* size of current holding disk file */ -int nb_header_block; -static enum { srvcomp_none, srvcomp_fast, srvcomp_best } srvcompress; +struct databuf { + int fd; /* file to flush to */ + char *buf; + char *datain; /* data buffer markers */ + char *dataout; + char *datalimit; + pid_t compresspid; /* valid if fd is pipe to compress */ + pid_t encryptpid; /* valid if fd is pipe to encrypt */ +}; + +typedef struct filter_s { + int fd; + char *name; + char *buffer; + gint64 first; /* first byte used */ + gint64 size; /* number of byte use in the buffer */ + gint64 allocated_size ; /* allocated size of the buffer */ + event_handle_t *event; +} filter_t; + +static char *handle = NULL; + +static char *errstr = NULL; +static off_t dumpbytes; +static off_t dumpsize, headersize, origsize; + +static comp_t srvcompress = COMP_NONE; +char *srvcompprog = NULL; +char *clntcompprog = NULL; + +static encrypt_t srvencrypt = ENCRYPT_NONE; +char *srv_encrypt = NULL; +char *clnt_encrypt = NULL; +char *srv_decrypt_opt = NULL; +char *clnt_decrypt_opt = NULL; +static kencrypt_type dumper_kencrypt; static FILE *errf = NULL; -char *filename = NULL; /* holding disk base file name */ -string_t cont_filename; -char *hostname = NULL; +static char *hostname = NULL; am_feature_t *their_features = NULL; -char *diskname = NULL; -char *device = NULL; -char *options = NULL; -char *progname = NULL; -int level; -char *dumpdate = NULL; -long chunksize; -long use; /* space remaining in this hold disk */ -char *datestamp; -char *backup_name = NULL; -char *recover_cmd = NULL; -char *compress_suffix = NULL; -int conf_dtimeout; - -dumpfile_t file; -int filename_seq; -long split_size; /* next dumpsize we will split at */ - -int datafd = -1; -int mesgfd = -1; -int indexfd = -1; -int amanda_port; +static char *diskname = NULL; +static char *qdiskname = NULL, *b64disk; +static char *device = NULL, *b64device; +static char *options = NULL; +static char *progname = NULL; +static char *amandad_path=NULL; +static char *client_username=NULL; +static char *client_port=NULL; +static char *ssh_keys=NULL; +static char *auth=NULL; +static data_path_t data_path=DATA_PATH_AMANDA; +static char *dataport_list = NULL; +static int level; +static char *dumpdate = NULL; +static char *dumper_timestamp = NULL; +static time_t conf_dtimeout; +static int indexfderror; +static int set_datafd; +static char *dle_str = NULL; + +static dumpfile_t file; + +static struct { + const char *name; + security_stream_t *fd; +} streams[] = { +#define DATAFD 0 + { "DATA", NULL }, +#define MESGFD 1 + { "MESG", NULL }, +#define INDEXFD 2 + { "INDEX", NULL }, +}; +#define NSTREAMS (int)(sizeof(streams) / sizeof(streams[0])) static am_feature_t *our_features = NULL; static char *our_feature_string = NULL; +/* buffer to keep partial line from the MESG stream */ +static struct { + char *buf; /* buffer holding msg data */ + size_t size; /* size of alloced buffer */ +} msg = { NULL, 0 }; + + /* local functions */ -int main P((int main_argc, char **main_argv)); -int do_dump P((int mesgfd, int datafd, int indexfd, int outfd)); -void check_options P((char *options)); -void service_ports_init P((void)); -int write_tapeheader P((int outfd, dumpfile_t *type)); -int write_dataptr P((int outf)); -int update_dataptr P((int *outf, int size)); -static void process_dumpeof P((void)); -static void process_dumpline P((char *str)); -static void add_msg_data P((char *str, int len)); -static void log_msgout P((logtype_t typ)); -void sendbackup_response P((proto_t *p, pkt_t *pkt)); -int startup_dump P((char *hostname, char *disk, char *device, int level, - char *dumpdate, char *progname, char *options)); - - -void check_options(options) -char *options; +int main(int, char **); +static int do_dump(struct databuf *); +static void check_options(char *); +static void xml_check_options(char *optionstr); +static void finish_tapeheader(dumpfile_t *); +static ssize_t write_tapeheader(int, dumpfile_t *); +static void databuf_init(struct databuf *, int); +static int databuf_write(struct databuf *, const void *, size_t); +static int databuf_flush(struct databuf *); +static void process_dumpeof(void); +static void process_dumpline(const char *); +static void add_msg_data(const char *, size_t); +static void parse_info_line(char *); +static void log_msgout(logtype_t); +static char * dumper_get_security_conf (char *, void *); + +static int runcompress(int, pid_t *, comp_t, char *); +static int runencrypt(int, pid_t *, encrypt_t); + +static void sendbackup_response(void *, pkt_t *, security_handle_t *); +static int startup_dump(const char *, const char *, const char *, int, + const char *, const char *, const char *, + const char *, const char *, const char *, + const char *, const char *); +static void stop_dump(void); + +static void read_indexfd(void *, void *, ssize_t); +static void read_datafd(void *, void *, ssize_t); +static void read_mesgfd(void *, void *, ssize_t); +static void timeout(time_t); +static void timeout_callback(void *); + +static void +check_options( + char *options) { -#ifdef KRB4_SECURITY - krb4_auth = strstr(options, "krb4-auth;") != NULL; - kencrypt = strstr(options, "kencrypt;") != NULL; -#endif - if (strstr(options, "srvcomp-best;") != NULL) - srvcompress = srvcomp_best; + char *compmode = NULL; + char *compend = NULL; + char *encryptmode = NULL; + char *encryptend = NULL; + char *decryptmode = NULL; + char *decryptend = NULL; + + /* parse the compression option */ + if (strstr(options, "srvcomp-best;") != NULL) + srvcompress = COMP_BEST; else if (strstr(options, "srvcomp-fast;") != NULL) - srvcompress = srvcomp_fast; - else - srvcompress = srvcomp_none; + srvcompress = COMP_FAST; + else if ((compmode = strstr(options, "srvcomp-cust=")) != NULL) { + compend = strchr(compmode, ';'); + if (compend ) { + srvcompress = COMP_SERVER_CUST; + *compend = '\0'; + srvcompprog = stralloc(compmode + strlen("srvcomp-cust=")); + *compend = ';'; + } + } else if ((compmode = strstr(options, "comp-cust=")) != NULL) { + compend = strchr(compmode, ';'); + if (compend) { + srvcompress = COMP_CUST; + *compend = '\0'; + clntcompprog = stralloc(compmode + strlen("comp-cust=")); + *compend = ';'; + } + } + else { + srvcompress = COMP_NONE; + } + + + /* now parse the encryption option */ + if ((encryptmode = strstr(options, "encrypt-serv-cust=")) != NULL) { + encryptend = strchr(encryptmode, ';'); + if (encryptend) { + srvencrypt = ENCRYPT_SERV_CUST; + *encryptend = '\0'; + srv_encrypt = stralloc(encryptmode + strlen("encrypt-serv-cust=")); + *encryptend = ';'; + } + } else if ((encryptmode = strstr(options, "encrypt-cust=")) != NULL) { + encryptend = strchr(encryptmode, ';'); + if (encryptend) { + srvencrypt = ENCRYPT_CUST; + *encryptend = '\0'; + clnt_encrypt = stralloc(encryptmode + strlen("encrypt-cust=")); + *encryptend = ';'; + } + } else { + srvencrypt = ENCRYPT_NONE; + } + /* get the decryption option parameter */ + if ((decryptmode = strstr(options, "server-decrypt-option=")) != NULL) { + decryptend = strchr(decryptmode, ';'); + if (decryptend) { + *decryptend = '\0'; + srv_decrypt_opt = stralloc(decryptmode + strlen("server-decrypt-option=")); + *decryptend = ';'; + } + } else if ((decryptmode = strstr(options, "client-decrypt-option=")) != NULL) { + decryptend = strchr(decryptmode, ';'); + if (decryptend) { + *decryptend = '\0'; + clnt_decrypt_opt = stralloc(decryptmode + strlen("client-decrypt-option=")); + *decryptend = ';'; + } + } + + if (strstr(options, "kencrypt;") != NULL) { + dumper_kencrypt = KENCRYPT_WILL_DO; + } else { + dumper_kencrypt = KENCRYPT_NONE; + } } -void service_ports_init() + +static void +xml_check_options( + char *optionstr) { - struct servent *amandad; + char *o, *oo; + char *errmsg = NULL; + dle_t *dle; - if((amandad = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL) { - amanda_port = AMANDA_SERVICE_DEFAULT; - log_add(L_WARNING, "no %s/udp service, using default port %d", - AMANDA_SERVICE_NAME, AMANDA_SERVICE_DEFAULT); + o = oo = vstralloc("", strchr(optionstr,'<'), "", NULL); + + dle = amxml_parse_node_CHAR(o, &errmsg); + if (dle == NULL) { + error("amxml_parse_node_CHAR failed: %s\n", errmsg); } - else - amanda_port = ntohs(amandad->s_port); -#ifdef KRB4_SECURITY - if((amandad = getservbyname(KAMANDA_SERVICE_NAME, "udp")) == NULL) { - kamanda_port = KAMANDA_SERVICE_DEFAULT; - log_add(L_WARNING, "no %s/udp service, using default port %d", - KAMANDA_SERVICE_NAME, KAMANDA_SERVICE_DEFAULT); + if (dle->compress == COMP_SERVER_FAST) { + srvcompress = COMP_FAST; + } else if (dle->compress == COMP_SERVER_BEST) { + srvcompress = COMP_BEST; + } else if (dle->compress == COMP_SERVER_CUST) { + srvcompress = COMP_SERVER_CUST; + srvcompprog = g_strdup(dle->compprog); + } else if (dle->compress == COMP_CUST) { + srvcompress = COMP_CUST; + clntcompprog = g_strdup(dle->compprog); + } else { + srvcompress = COMP_NONE; } - else - kamanda_port = ntohs(amandad->s_port); -#endif + + if (dle->encrypt == ENCRYPT_CUST) { + srvencrypt = ENCRYPT_CUST; + clnt_encrypt = g_strdup(dle->clnt_encrypt); + clnt_decrypt_opt = g_strdup(dle->clnt_decrypt_opt); + } else if (dle->encrypt == ENCRYPT_SERV_CUST) { + srvencrypt = ENCRYPT_SERV_CUST; + srv_encrypt = g_strdup(dle->srv_encrypt); + srv_decrypt_opt = g_strdup(dle->srv_decrypt_opt); + } else { + srvencrypt = ENCRYPT_NONE; + } + free_dle(dle); + amfree(o); } -int main(main_argc, main_argv) -int main_argc; -char **main_argv; +int +main( + int argc, + char ** argv) { - struct cmdargs cmdargs; - cmd_t cmd; - int outfd, protocol_port, taper_port, rc; - dgram_t *msg; - unsigned long malloc_hist_1, malloc_size_1; - unsigned long malloc_hist_2, malloc_size_2; - char *conffile; + static struct databuf db; + struct cmdargs *cmdargs = NULL; + int outfd = -1; + int rc; + in_port_t header_port; char *q = NULL; - char *tmp_filename = NULL, *pc; int a; + int res; + config_overrides_t *cfg_ovr = NULL; + char *cfg_opt = NULL; + int dumper_setuid; + + /* + * Configure program for internationalization: + * 1) Only set the message locale for now. + * 2) Set textdomain for all amanda related programs to "amanda" + * We don't want to be forced to support dozens of message catalogs. + */ + setlocale(LC_MESSAGES, "C"); + textdomain("amanda"); + + /* drop root privileges */ + dumper_setuid = set_root_privs(0); safe_fd(-1, 0); set_pname("dumper"); - malloc_size_1 = malloc_inuse(&malloc_hist_1); - - erroutput_type = (ERR_AMANDALOG|ERR_INTERACTIVE); - set_logerror(logerror); + dbopen(DBG_SUBDIR_SERVER); - if (main_argc > 1) { - config_name = stralloc(main_argv[1]); - config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL); - } else { - char my_cwd[STR_SIZE]; + /* Don't die when child closes pipe */ + signal(SIGPIPE, SIG_IGN); - if (getcwd(my_cwd, sizeof(my_cwd)) == NULL) { - error("cannot determine current working directory"); - } - config_dir = stralloc2(my_cwd, "/"); - if ((config_name = strrchr(my_cwd, '/')) != NULL) { - config_name = stralloc(config_name + 1); - } - } + add_amanda_log_handler(amanda_log_stderr); + add_amanda_log_handler(amanda_log_trace_log); - safe_cd(); + cfg_ovr = extract_commandline_config_overrides(&argc, &argv); + if (argc > 1) + cfg_opt = argv[1]; + set_config_overrides(cfg_ovr); + config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt); - our_features = am_init_feature_set(); - our_feature_string = am_feature_to_string(our_features); + if (!dumper_setuid) { + error(_("dumper must be run setuid root")); + } - conffile = stralloc2(config_dir, CONFFILE_NAME); - if(read_conffile(conffile)) { - error("errors processing config file \"%s\"", conffile); + if (config_errors(NULL) >= CFGERR_ERRORS) { + g_critical(_("errors processing config file")); } - amfree(conffile); - /* set up dgram port first thing */ + safe_cd(); /* do this *after* config_init() */ - msg = dgram_alloc(); - if(dgram_bind(msg, &protocol_port) == -1) - error("could not bind result datagram port: %s", strerror(errno)); + check_running_as(RUNNING_AS_ROOT | RUNNING_AS_UID_ONLY); - if(geteuid() == 0) { - /* set both real and effective uid's to real uid, likewise for gid */ - setgid(getgid()); - setuid(getuid()); - } -#ifdef BSD_SECURITY - else error("must be run setuid root to communicate correctly"); -#endif + dbrename(get_config_name(), DBG_SUBDIR_SERVER); + + our_features = am_init_feature_set(); + our_feature_string = am_feature_to_string(our_features); - fprintf(stderr, - "%s: pid %ld executable %s version %s, using port %d\n", + log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid()); + g_fprintf(stderr, + _("%s: pid %ld executable %s version %s\n"), get_pname(), (long) getpid(), - main_argv[0], version(), protocol_port); + argv[0], VERSION); fflush(stderr); /* now, make sure we are a valid user */ - if(getpwuid(getuid()) == NULL) - error("can't get login name for my uid %ld", (long)getuid()); - signal(SIGPIPE, SIG_IGN); - interactive = isatty(0); + conf_dtimeout = (time_t)getconf_int(CNF_DTIMEOUT); - amfree(datestamp); - datestamp = construct_datestamp(NULL); - conf_dtimeout = getconf_int(CNF_DTIMEOUT); - - service_ports_init(); - proto_init(msg->socket, time(0), 16); + protocol_init(); do { - cmd = getcmd(&cmdargs); + if (cmdargs) + free_cmdargs(cmdargs); + cmdargs = getcmd(); + + amfree(errstr); + switch(cmdargs->cmd) { + case START: + if(cmdargs->argc < 2) + error(_("error [dumper START: not enough args: timestamp]")); + dumper_timestamp = newstralloc(dumper_timestamp, cmdargs->argv[1]); + break; + + case ABORT: + break; - switch(cmd) { case QUIT: break; - case FILE_DUMP: + + case PORT_DUMP: /* - * FILE-DUMP + * PORT-DUMP * handle - * filename + * port * host * features * disk * device * level * dumpdate - * chunksize * progname - * use + * amandad_path + * client_username + * client_port + * ssh_keys + * security_driver + * data_path + * dataport_list * options */ - cmdargs.argc++; /* true count of args */ - a = 2; + a = 1; /* skip "PORT-DUMP" */ - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: handle]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: handle]")); + /*NOTREACHED*/ } - handle = newstralloc(handle, cmdargs.argv[a++]); + handle = newstralloc(handle, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: filename]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: port]")); + /*NOTREACHED*/ } - filename = newstralloc(filename, cmdargs.argv[a++]); + header_port = (in_port_t)atoi(cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: hostname]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: hostname]")); + /*NOTREACHED*/ } - hostname = newstralloc(hostname, cmdargs.argv[a++]); + hostname = newstralloc(hostname, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: features]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: features]")); + /*NOTREACHED*/ } am_release_feature_set(their_features); - their_features = am_string_to_feature(cmdargs.argv[a++]); - - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: diskname]"); - } - diskname = newstralloc(diskname, cmdargs.argv[a++]); - - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: device]"); - } - device = newstralloc(device, cmdargs.argv[a++]); - if(strcmp(device, "NODEVICE") == 0) amfree(device); - - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: level]"); - } - level = atoi(cmdargs.argv[a++]); - - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: dumpdate]"); - } - dumpdate = newstralloc(dumpdate, cmdargs.argv[a++]); - - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: chunksize]"); - } - chunksize = am_floor(atoi(cmdargs.argv[a++]), DISK_BLOCK_KB); - - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: progname]"); - } - progname = newstralloc(progname, cmdargs.argv[a++]); - - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: use]"); - } - use = am_floor(atoi(cmdargs.argv[a++]), DISK_BLOCK_KB); + their_features = am_string_to_feature(cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper FILE-DUMP: not enough args: options]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: diskname]")); + /*NOTREACHED*/ } - options = newstralloc(options, cmdargs.argv[a++]); - - if(a != cmdargs.argc) { - error("error [dumper FILE-DUMP: too many args: %d != %d]", - cmdargs.argc, a); - } - - cont_filename[0] = '\0'; - - tmp_filename = newvstralloc(tmp_filename, filename, ".tmp", NULL); - pc = strrchr(tmp_filename, '/'); - *pc = '\0'; - mkholdingdir(tmp_filename); - *pc = '/'; - outfd = open(tmp_filename, O_RDWR|O_CREAT|O_TRUNC, 0600); - if(outfd == -1) { - int save_errno = errno; - q = squotef("[main holding file \"%s\": %s]", - tmp_filename, strerror(errno)); - if(save_errno == ENOSPC) { - putresult(NO_ROOM, "%s %lu\n", handle, use); - putresult(TRYAGAIN, "%s %s\n", handle, q); - } - else { - putresult(FAILED, "%s %s\n", handle, q); - } - amfree(q); - break; + diskname = newstralloc(diskname, cmdargs->argv[a++]); + if (qdiskname != NULL) + amfree(qdiskname); + qdiskname = quote_string(diskname); + b64disk = amxml_format_tag("disk", diskname); + + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: device]")); + /*NOTREACHED*/ } - filename_seq = 1; - - check_options(options); - - rc = startup_dump(hostname, - diskname, - device, - level, - dumpdate, - progname, - options); - if(rc) { - q = squote(errstr); - putresult(rc == 2? FAILED : TRYAGAIN, "%s %s\n", - handle, q); - if(rc == 2) { - log_add(L_FAIL, "%s %s %s %d [%s]", hostname, diskname, - datestamp, level, errstr); - } - amfree(q); - /* do need to close if TRY-AGAIN, doesn't hurt otherwise */ - if (mesgfd != -1) - aclose(mesgfd); - if (datafd != -1) - aclose(datafd); - if (indexfd != -1) - aclose(indexfd); - if (outfd != -1) - aclose(outfd); - break; + device = newstralloc(device, cmdargs->argv[a++]); + b64device = amxml_format_tag("diskdevice", device); + if(strcmp(device,"NODEVICE") == 0) + amfree(device); + + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: level]")); + /*NOTREACHED*/ } + level = atoi(cmdargs->argv[a++]); - abort_pending = 0; - split_size = (chunksize>use)?use:chunksize; - use -= split_size; - if(do_dump(mesgfd, datafd, indexfd, outfd)) { + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: dumpdate]")); + /*NOTREACHED*/ } - aclose(mesgfd); - aclose(datafd); - if (indexfd != -1) - aclose(indexfd); - aclose(outfd); - if(abort_pending) putresult(ABORT_FINISHED, "%s\n", handle); - break; - - case PORT_DUMP: - /* - * PORT-DUMP - * handle - * port - * host - * features - * disk - * device - * level - * dumpdate - * progname - * options - */ - cmdargs.argc++; /* true count of args */ - a = 2; + dumpdate = newstralloc(dumpdate, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: handle]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: program]")); + /*NOTREACHED*/ } - handle = newstralloc(handle, cmdargs.argv[a++]); + progname = newstralloc(progname, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: port]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: amandad_path]")); + /*NOTREACHED*/ } - taper_port = atoi(cmdargs.argv[a++]); + amandad_path = newstralloc(amandad_path, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: hostname]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: client_username]")); } - hostname = newstralloc(hostname, cmdargs.argv[a++]); + client_username = newstralloc(client_username, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: features]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: client_port]")); } - am_release_feature_set(their_features); - their_features = am_string_to_feature(cmdargs.argv[a++]); + client_port = newstralloc(client_port, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: diskname]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: ssh_keys]")); } - diskname = newstralloc(diskname, cmdargs.argv[a++]); + ssh_keys = newstralloc(ssh_keys, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: device]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: auth]")); } - device = newstralloc(device, cmdargs.argv[a++]); - if(strcmp(device,"NODEVICE") == 0) amfree(device); + auth = newstralloc(auth, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: level]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: data_path]")); } - level = atoi(cmdargs.argv[a++]); + data_path = data_path_from_string(cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: dumpdate]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: dataport_list]")); } - dumpdate = newstralloc(dumpdate, cmdargs.argv[a++]); + dataport_list = newstralloc(dataport_list, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: progname]"); + if(a >= cmdargs->argc) { + error(_("error [dumper PORT-DUMP: not enough args: options]")); } - progname = newstralloc(progname, cmdargs.argv[a++]); + options = newstralloc(options, cmdargs->argv[a++]); - if(a >= cmdargs.argc) { - error("error [dumper PORT-DUMP: not enough args: options]"); + if(a != cmdargs->argc) { + error(_("error [dumper PORT-DUMP: too many args: %d != %d]"), + cmdargs->argc, a); + /*NOTREACHED*/ } - options = newstralloc(options, cmdargs.argv[a++]); - if(a != cmdargs.argc) { - error("error [dumper PORT-DUMP: too many args: %d != %d]", - cmdargs.argc, a); + /* Double-check that 'localhost' resolves properly */ + if ((res = resolve_hostname("localhost", 0, NULL, NULL) != 0)) { + errstr = newvstrallocf(errstr, + _("could not resolve localhost: %s"), + gai_strerror(res)); + q = quote_string(errstr); + putresult(FAILED, "%s %s\n", handle, q); + log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname, + dumper_timestamp, level, errstr); + amfree(q); + break; } - filename = newstralloc(filename, ""); - cont_filename[0] = '\0'; - - /* connect outf to taper port */ + /* connect outf to chunker/taper port */ - outfd = stream_client("localhost", taper_port, - STREAM_BUFSIZE, -1, NULL); - if(outfd == -1) { - q = squotef("[taper port open: %s]", strerror(errno)); + g_debug(_("Sending header to localhost:%d\n"), header_port); + outfd = stream_client("localhost", header_port, + STREAM_BUFSIZE, 0, NULL, 0); + if (outfd == -1) { + + errstr = newvstrallocf(errstr, _("port open: %s"), + strerror(errno)); + q = quote_string(errstr); putresult(FAILED, "%s %s\n", handle, q); + log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname, + dumper_timestamp, level, errstr); amfree(q); break; } - filename_seq = 1; + databuf_init(&db, outfd); - check_options(options); + if (am_has_feature(their_features, fe_req_xml)) + xml_check_options(options); /* note: modifies globals */ + else + check_options(options); /* note: modifies globals */ rc = startup_dump(hostname, diskname, @@ -510,416 +581,249 @@ char **main_argv; level, dumpdate, progname, + amandad_path, + client_username, + client_port, + ssh_keys, + auth, options); - if(rc) { - q = squote(errstr); + if (rc != 0) { + q = quote_string(errstr); putresult(rc == 2? FAILED : TRYAGAIN, "%s %s\n", - handle, q); - if(rc == 2) { - log_add(L_FAIL, "%s %s %s %d [%s]", hostname, diskname, - datestamp, level, errstr); - } + handle, q); + if (rc == 2) + log_add(L_FAIL, "%s %s %s %d [%s]", hostname, qdiskname, + dumper_timestamp, level, errstr); amfree(q); - /* do need to close if TRY-AGAIN, doesn't hurt otherwise */ - if (mesgfd != -1) - aclose(mesgfd); - if (datafd != -1) - aclose(datafd); - if (indexfd != -1) - aclose(indexfd); - if (outfd != -1) - aclose(outfd); - break; + } else { + do_dump(&db); + /* try to clean up any defunct processes, since Amanda doesn't + wait() for them explicitly */ + while(waitpid(-1, NULL, WNOHANG)> 0); } - abort_pending = 0; - split_size = -1; - if(do_dump(mesgfd, datafd, indexfd, outfd)) { - } - aclose(mesgfd); - aclose(datafd); - if (indexfd != -1) - aclose(indexfd); - aclose(outfd); - if(abort_pending) putresult(ABORT_FINISHED, "%s\n", handle); + amfree(amandad_path); + amfree(client_username); + amfree(client_port); + amfree(device); + amfree(b64device); + amfree(qdiskname); + amfree(b64disk); + break; default: - if(cmdargs.argc >= 1) { - q = squote(cmdargs.argv[1]); - } else if(cmdargs.argc >= 0) { - q = squote(cmdargs.argv[0]); + if(cmdargs->argc >= 1) { + q = quote_string(cmdargs->argv[0]); } else { - q = stralloc("(no input?)"); + q = stralloc(_("(no input?)")); } putresult(BAD_COMMAND, "%s\n", q); amfree(q); + break; } - while(wait(NULL) != -1); - } while(cmd != QUIT); - amfree(tmp_filename); + if (outfd != -1) + aclose(outfd); + } while(cmdargs->cmd != QUIT); + free_cmdargs(cmdargs); + + log_add(L_INFO, "pid-done %ld", (long)getpid()); + + am_release_feature_set(our_features); + amfree(our_feature_string); amfree(errstr); - amfree(msg); - amfree(datestamp); - amfree(backup_name); - amfree(recover_cmd); - amfree(compress_suffix); + amfree(dumper_timestamp); amfree(handle); - amfree(filename); amfree(hostname); + amfree(qdiskname); amfree(diskname); amfree(device); amfree(dumpdate); amfree(progname); + amfree(srvcompprog); + amfree(clntcompprog); + amfree(srv_encrypt); + amfree(clnt_encrypt); + amfree(srv_decrypt_opt); + amfree(clnt_decrypt_opt); amfree(options); - amfree(config_dir); - amfree(config_name); - amfree(our_feature_string); - am_release_feature_set(our_features); - our_features = NULL; - am_release_feature_set(their_features); - their_features = NULL; - - malloc_size_2 = malloc_inuse(&malloc_hist_2); - - if(malloc_size_1 != malloc_size_2) { - malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2); - } - return 0; + dbclose(); + return (0); /* exit */ } -int write_dataptr(outf) -int outf; +/* + * Initialize a databuf. Takes a writeable file descriptor. + */ +static void +databuf_init( + struct databuf * db, + int fd) { - int w; - int written; - written = w = 0; - while(dataout < datain) { - if((w = write(outf, dataout, datain - dataout)) < 0) { - break; - } - dataout += w; - written += w; - } - dumpbytes += written; - dumpsize += (dumpbytes / 1024); - filesize += (dumpbytes / 1024); - dumpbytes %= 1024; - if(w < 0) { - if(errno != ENOSPC) { - errstr = squotef("data write: %s", strerror(errno)); - return 1; - } - /* - * NO-ROOM is informational only. Later, RQ_MORE_DISK will be - * issued to use another holding disk. - */ - putresult(NO_ROOM, "%s %lu\n", handle, use+split_size-dumpsize); - use = 0; /* force RQ_MORE_DISK */ - split_size = dumpsize; - } - if(dataout == datain) { - /* - * We flushed the whole buffer so reset to use it all. - */ - dataout = datain = databuf; - } - return 0; + db->fd = fd; + db->datain = db->dataout = db->datalimit = NULL; + db->compresspid = -1; + db->encryptpid = -1; } -int update_dataptr(p_outfd, size) -int *p_outfd, size; + /* * Updates the buffer pointer for the input data buffer. The buffer is - * written if it is full or we are at EOF. + * written regardless of how much data is present, since we know we + * are writing to a socket (to chunker) and there is no need to maintain + * any boundaries. */ +static int +databuf_write( + struct databuf * db, + const void * buf, + size_t size) { - int outfd = *p_outfd; - int rc = 0; - char *arg_filename = NULL; - char *new_filename = NULL; - char *tmp_filename = NULL; - char *pc; - char sequence[NUM_STR_SIZE]; - int new_outfd = -1; - struct cmdargs cmdargs; - cmd_t cmd; - filetype_t save_type; - long left_in_chunk; - int a; - char *q; - - datain += size; - - while(rc == 0 && ((size == 0 && dataout < datain) || datain >= datalimit)) { - - NAUGHTY_BITS; - - /* We open a new chunkfile if */ - /* We have something to write (dataout < datain) */ - /* We have a split_size defined (split_size > 0) */ - /* The current file is already filled (dumpsize >= split_size) */ - - while(dataout < datain && split_size > 0 && dumpsize >= split_size) { - amfree(new_filename); - if(use == 0) { - /* - * Probably no more space on this disk. Request more. - */ - putresult(RQ_MORE_DISK, "%s\n", handle); - cmd = getcmd(&cmdargs); - if(cmd == CONTINUE) { - /* - * CONTINUE - * serial - * filename - * chunksize - * use - */ - cmdargs.argc++; /* true count of args */ - a = 3; - - if(a >= cmdargs.argc) { - error("error [dumper CONTINUE: not enough args: filename]"); - } - arg_filename = newstralloc(arg_filename, cmdargs.argv[a++]); - - if(a >= cmdargs.argc) { - error("error [dumper CONTINUE: not enough args: chunksize]"); - } - chunksize = atoi(cmdargs.argv[a++]); - chunksize = am_floor(chunksize, DISK_BLOCK_KB); - - if(a >= cmdargs.argc) { - error("error [dumper CONTINUE: not enough args: use]"); - } - use = atoi(cmdargs.argv[a++]); - - if(a != cmdargs.argc) { - error("error [dumper CONTINUE: too many args: %d != %d]", - cmdargs.argc, a); - } - - if(strcmp(filename, arg_filename) == 0) { - /* - * Same disk, so use what room is left up to the - * next chunk boundary or the amount we were given, - * whichever is less. - */ - left_in_chunk = chunksize - filesize; - if(left_in_chunk > use) { - split_size += use; - use = 0; - } else { - split_size += left_in_chunk; - use -= left_in_chunk; - } - if(left_in_chunk > 0) { - /* - * We still have space in this chunk. - */ - break; - } - } else { - /* - * Different disk, so use new file. - */ - filename = newstralloc(filename, arg_filename); - } - } else if(cmd == ABORT) { - abort_pending = 1; - errstr = newstralloc(errstr, "ERROR"); - rc = 1; - goto common_exit; - } else { - if(cmdargs.argc >= 1) { - q = squote(cmdargs.argv[1]); - } else if(cmdargs.argc >= 0) { - q = squote(cmdargs.argv[0]); - } else { - q = stralloc("(no input?)"); - } - error("error [bad command after RQ-MORE-DISK: \"%s\"]", q); - } - } - - ap_snprintf(sequence, sizeof(sequence), "%d", filename_seq); - new_filename = newvstralloc(new_filename, - filename, - ".", - sequence, - NULL); - tmp_filename = newvstralloc(tmp_filename, - new_filename, - ".tmp", - NULL); - pc = strrchr(tmp_filename, '/'); - *pc = '\0'; - mkholdingdir(tmp_filename); - *pc = '/'; - new_outfd = open(tmp_filename, O_RDWR|O_CREAT|O_TRUNC, 0600); - if(new_outfd == -1) { - int save_errno = errno; - - errstr = squotef("creating chunk holding file \"%s\": %s", - tmp_filename, - strerror(save_errno)); - if(save_errno == ENOSPC) { - putresult(NO_ROOM, "%s %lu\n", - handle, - use + split_size - dumpsize); - use = 0; /* force RQ_MORE_DISK */ - split_size = dumpsize; - continue; - } - aclose(outfd); - rc = 1; - goto common_exit; - } - save_type = file.type; - file.type = F_CONT_DUMPFILE; - file.cont_filename[0] = '\0'; - if(write_tapeheader(new_outfd, &file)) { - int save_errno = errno; - - aclose(new_outfd); - unlink(tmp_filename); - if(save_errno == ENOSPC) { - putresult(NO_ROOM, "%s %lu\n", - handle, - use + split_size - dumpsize); - use = 0; /* force RQ_MORE_DISK */ - split_size = dumpsize; - continue; - } - errstr = squotef("write_tapeheader file \"%s\": %s", - tmp_filename, strerror(errno)); - rc = 1; - goto common_exit; - } - if(lseek(outfd, (off_t)0, SEEK_SET) != 0) { - errstr = squotef("cannot lseek: %s", strerror(errno)); - aclose(new_outfd); - unlink(tmp_filename); - rc = 1; - goto common_exit; - } - strncpy(file.cont_filename, new_filename, - sizeof(file.cont_filename)); - file.cont_filename[sizeof(file.cont_filename)-1] = '\0'; - file.type = save_type; - if(write_tapeheader(outfd, &file)) { - errstr = squotef("write_tapeheader file linked to \"%s\": %s", - tmp_filename, strerror(errno)); - aclose(new_outfd); - unlink(tmp_filename); - rc = 1; - goto common_exit; - } - file.type = F_CONT_DUMPFILE; - strncpy(cont_filename, new_filename, sizeof(cont_filename)); - cont_filename[sizeof(cont_filename)-1] = '\0'; + db->buf = (char *)buf; + db->datain = db->datalimit = db->buf + size; + db->dataout = db->buf; + return databuf_flush(db); +} - aclose(outfd); - *p_outfd = outfd = new_outfd; - new_outfd = -1; - - dumpsize += DISK_BLOCK_KB; - filesize = DISK_BLOCK_KB; - split_size += (chunksize>use)?use:chunksize; - use = (chunksize>use)?0:use-chunksize; - nb_header_block++; - filename_seq++; - } - rc = write_dataptr(outfd); +/* + * Write out the buffer to chunker. + */ +static int +databuf_flush( + struct databuf * db) +{ + size_t written; + char *m; + + /* + * If there's no data, do nothing. + */ + if (db->dataout >= db->datain) { + return 0; } -common_exit: - - amfree(new_filename); - amfree(tmp_filename); - amfree(arg_filename); - return rc; + /* + * Write out the buffer + */ + written = full_write(db->fd, db->dataout, + (size_t)(db->datain - db->dataout)); + if (written > 0) { + db->dataout += written; + dumpbytes += (off_t)written; + } + if (dumpbytes >= (off_t)1024) { + dumpsize += (dumpbytes / (off_t)1024); + dumpbytes %= (off_t)1024; + } + if (written == 0) { + int save_errno = errno; + m = vstrallocf(_("data write: %s"), strerror(save_errno)); + amfree(errstr); + errstr = quote_string(m); + amfree(m); + errno = save_errno; + return -1; + } + db->datain = db->dataout = db->buf; + return 0; } +static int dump_result; +static int status; +#define GOT_INFO_ENDLINE (1 << 0) +#define GOT_SIZELINE (1 << 1) +#define GOT_ENDLINE (1 << 2) +#define HEADER_DONE (1 << 3) -static char *msgbuf = NULL; -int got_info_endline; -int got_sizeline; -int got_endline; -int dump_result; -static void process_dumpeof() +static void +process_dumpeof(void) { /* process any partial line in msgbuf? !!! */ - if(msgbuf != NULL) { - fprintf(errf,"? %s: error [partial line in msgbuf: %ld bytes]\n", - get_pname(), (long) strlen(msgbuf)); - fprintf(errf,"? %s: error [partial line in msgbuf: \"%s\"]\n", - get_pname(), msgbuf); - } - if(!got_sizeline && dump_result < 2) { + add_msg_data(NULL, 0); + if(!ISSET(status, GOT_SIZELINE) && dump_result < 2) { /* make a note if there isn't already a failure */ - fprintf(errf,"? %s: strange [missing size line from sendbackup]\n", + g_fprintf(errf, + _("? %s: strange [missing size line from sendbackup]\n"), get_pname()); + if(errstr == NULL) { + errstr = stralloc(_("missing size line from sendbackup")); + } dump_result = max(dump_result, 2); } - if(!got_endline && dump_result < 2) { - fprintf(errf,"? %s: strange [missing end line from sendbackup]\n", + if(!ISSET(status, GOT_ENDLINE) && dump_result < 2) { + g_fprintf(errf, + _("? %s: strange [missing end line from sendbackup]\n"), get_pname()); + if(errstr == NULL) { + errstr = stralloc(_("missing end line from sendbackup")); + } dump_result = max(dump_result, 2); } } -/* Parse an information line from the client. -** We ignore unknown parameters and only remember the last -** of any duplicates. -*/ -static void parse_info_line(str) -char *str; +/* + * Parse an information line from the client. + * We ignore unknown parameters and only remember the last + * of any duplicates. + */ +static void +parse_info_line( + char *str) { - if(strcmp(str, "end") == 0) { - got_info_endline = 1; + static const struct { + const char *name; + char *value; + size_t len; + } fields[] = { + { "BACKUP", file.program, SIZEOF(file.program) }, + { "APPLICATION", file.application, SIZEOF(file.application) }, + { "RECOVER_CMD", file.recover_cmd, SIZEOF(file.recover_cmd) }, + { "COMPRESS_SUFFIX", file.comp_suffix, SIZEOF(file.comp_suffix) }, + { "SERVER_CUSTOM_COMPRESS", file.srvcompprog, SIZEOF(file.srvcompprog) }, + { "CLIENT_CUSTOM_COMPRESS", file.clntcompprog, SIZEOF(file.clntcompprog) }, + { "SERVER_ENCRYPT", file.srv_encrypt, SIZEOF(file.srv_encrypt) }, + { "CLIENT_ENCRYPT", file.clnt_encrypt, SIZEOF(file.clnt_encrypt) }, + { "SERVER_DECRYPT_OPTION", file.srv_decrypt_opt, SIZEOF(file.srv_decrypt_opt) }, + { "CLIENT_DECRYPT_OPTION", file.clnt_decrypt_opt, SIZEOF(file.clnt_decrypt_opt) } + }; + char *name, *value; + size_t i; + + if (strcmp(str, "end") == 0) { + SET(status, GOT_INFO_ENDLINE); return; } -#define sc "BACKUP=" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { - backup_name = newstralloc(backup_name, str + sizeof(sc)-1); + name = strtok(str, "="); + if (name == NULL) return; - } -#undef sc - -#define sc "RECOVER_CMD=" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { - recover_cmd = newstralloc(recover_cmd, str + sizeof(sc)-1); + value = strtok(NULL, ""); + if (value == NULL) return; - } -#undef sc -#define sc "COMPRESS_SUFFIX=" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { - compress_suffix = newstralloc(compress_suffix, str + sizeof(sc)-1); - return; + for (i = 0; i < SIZEOF(fields) / SIZEOF(fields[0]); i++) { + if (strcmp(name, fields[i].name) == 0) { + strncpy(fields[i].value, value, fields[i].len - 1); + fields[i].value[fields[i].len - 1] = '\0'; + break; + } } -#undef sc } -static void process_dumpline(str) -char *str; +static void +process_dumpline( + const char * str) { - char *s, *fp; - int ch; + char *buf, *tok; - s = str; - ch = *s++; + buf = stralloc(str); - switch(ch) { + switch (*buf) { case '|': /* normal backup output line */ break; @@ -929,241 +833,347 @@ char *str; break; case 's': /* a sendbackup line, just check them all since there are only 5 */ -#define sc "sendbackup: start" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { + tok = strtok(buf, " "); + if (tok == NULL || strcmp(tok, "sendbackup:") != 0) + goto bad_line; + + tok = strtok(NULL, " "); + if (tok == NULL) + goto bad_line; + + if (strcmp(tok, "start") == 0) { break; } -#undef sc -#define sc "sendbackup: size" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { - s += sizeof(sc)-1; - ch = s[-1]; - skip_whitespace(s, ch); - if(ch) { - origsize = (long)atof(str + sizeof(sc)-1); - got_sizeline = 1; - break; + + if (strcmp(tok, "size") == 0) { + tok = strtok(NULL, ""); + if (tok != NULL) { + origsize = OFF_T_ATOI(tok); + SET(status, GOT_SIZELINE); } + break; + } + + if (strcmp(tok, "no-op") == 0) { + amfree(buf); + return; } -#undef sc -#define sc "sendbackup: end" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { - got_endline = 1; + + if (strcmp(tok, "end") == 0) { + SET(status, GOT_ENDLINE); break; } -#undef sc -#define sc "sendbackup: warning" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { + + if (strcmp(tok, "warning") == 0) { dump_result = max(dump_result, 1); break; } -#undef sc -#define sc "sendbackup: error" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { - s += sizeof(sc)-1; - ch = s[-1]; -#undef sc - got_endline = 1; + + if (strcmp(tok, "error") == 0) { + SET(status, GOT_ENDLINE); dump_result = max(dump_result, 2); - skip_whitespace(s, ch); - if(ch == '\0' || ch != '[') { - errstr = newvstralloc(errstr, - "bad remote error: ", str, - NULL); - } else { - ch = *s++; - fp = s - 1; - while(ch && ch != ']') ch = *s++; - s[-1] = '\0'; - errstr = newstralloc(errstr, fp); - s[-1] = ch; + + tok = strtok(NULL, ""); + if (!errstr) { /* report first error line */ + if (tok == NULL || *tok != '[') { + errstr = newvstrallocf(errstr, _("bad remote error: %s"), + str); + } else { + char *enderr; + + tok++; /* skip over '[' */ + if ((enderr = strchr(tok, ']')) != NULL) + *enderr = '\0'; + errstr = newstralloc(errstr, tok); + } } break; } -#define sc "sendbackup: info" - if(strncmp(str, sc, sizeof(sc)-1) == 0) { - s += sizeof(sc)-1; - ch = s[-1]; - skip_whitespace(s, ch); - parse_info_line(s - 1); + + if (strcmp(tok, "info") == 0) { + tok = strtok(NULL, ""); + if (tok != NULL) + parse_info_line(tok); break; } -#undef sc /* else we fall through to bad line */ default: - fprintf(errf, "??%s", str); +bad_line: + /* prefix with ?? */ + g_fprintf(errf, "??"); dump_result = max(dump_result, 1); - return; + break; } - fprintf(errf, "%s\n", str); + g_fprintf(errf, "%s\n", str); + amfree(buf); } -static void add_msg_data(str, len) -char *str; -int len; +static void +add_msg_data( + const char * str, + size_t len) { - char *t; - char *nl; + char *line, *ch; + size_t buflen; - while(len > 0) { - if((nl = strchr(str, '\n')) != NULL) { - *nl = '\0'; - } - if(msgbuf) { - t = stralloc2(msgbuf, str); - amfree(msgbuf); - msgbuf = t; - } else if(nl == NULL) { - msgbuf = stralloc(str); - } else { - msgbuf = str; + if (msg.buf != NULL) + buflen = strlen(msg.buf); + else + buflen = 0; + + /* + * If our argument is NULL, then we need to flush out any remaining + * bits and return. + */ + if (str == NULL) { + if (buflen == 0) + return; + g_fprintf(errf,_("? %s: error [partial line in msgbuf: %zu bytes]\n"), + get_pname(), buflen); + g_fprintf(errf,_("? %s: error [partial line in msgbuf: \"%s\"]\n"), + get_pname(), msg.buf); + msg.buf[0] = '\0'; + return; + } + + /* + * Expand the buffer if it can't hold the new contents. + */ + if ((buflen + len + 1) > msg.size) { + char *newbuf; + size_t newsize; + +/* round up to next y, where y is a power of 2 */ +#define ROUND(x, y) (((x) + (y) - 1) & ~((y) - 1)) + + newsize = ROUND(buflen + (ssize_t)len + 1, 256); + newbuf = alloc(newsize); + + if (msg.buf != NULL) { + strncpy(newbuf, msg.buf, newsize); + amfree(msg.buf); + } else + newbuf[0] = '\0'; + msg.buf = newbuf; + msg.size = newsize; + } + + /* + * If there was a partial line from the last call, then + * append the new data to the end. + */ + strncat(msg.buf, str, len); + + /* + * Process all lines in the buffer + * scanning line for unqouted newline. + */ + for (ch = line = msg.buf; *ch != '\0'; ch++) { + if (*ch == '\n') { + /* + * Found a newline. Terminate and process line. + */ + *ch = '\0'; + process_dumpline(line); + line = ch + 1; } - if(nl == NULL) break; - process_dumpline(msgbuf); - if(msgbuf != str) free(msgbuf); - msgbuf = NULL; - len -= nl + 1 - str; - str = nl + 1; + } + + /* + * If we did not process all of the data, move it to the front + * of the buffer so it is there next time. + */ + if (*line != '\0') { + buflen = strlen(line); + memmove(msg.buf, line, (size_t)buflen + 1); + } else { + msg.buf[0] = '\0'; } } -static void log_msgout(typ) -logtype_t typ; +static void +log_msgout( + logtype_t typ) { - char *line = NULL; + char *line; fflush(errf); - (void) fseek(errf, 0L, SEEK_SET); - for(; (line = agets(errf)) != NULL; free(line)) { - log_add(typ, "%s", line); + if (fseeko(errf, 0L, SEEK_SET) < 0) { + dbprintf(_("log_msgout: warning - seek failed: %s\n"), strerror(errno)); } + while ((line = agets(errf)) != NULL) { + if (line[0] != '\0') { + log_add(typ, "%s", line); + } + amfree(line); + } + afclose(errf); } /* ------------- */ -void make_tapeheader(file, type) -dumpfile_t *file; -filetype_t type; +/* + * Fill in the rest of the tape header + */ +static void +finish_tapeheader( + dumpfile_t *file) { - fh_init(file); - file->type = type; - strncpy(file->datestamp , datestamp , sizeof(file->datestamp)-1); - file->datestamp[sizeof(file->datestamp)-1] = '\0'; - strncpy(file->name , hostname , sizeof(file->name)-1); - file->name[sizeof(file->name)-1] = '\0'; - strncpy(file->disk , diskname , sizeof(file->disk)-1); - file->disk[sizeof(file->disk)-1] = '\0'; + + assert(ISSET(status, HEADER_DONE)); + + file->type = F_DUMPFILE; + strncpy(file->datestamp, dumper_timestamp, sizeof(file->datestamp) - 1); + strncpy(file->name, hostname, SIZEOF(file->name) - 1); + strncpy(file->disk, diskname, SIZEOF(file->disk) - 1); file->dumplevel = level; - strncpy(file->program , backup_name, sizeof(file->program)-1); - file->program[sizeof(file->program)-1] = '\0'; - strncpy(file->recover_cmd, recover_cmd, sizeof(file->recover_cmd)-1); - file->recover_cmd[sizeof(file->recover_cmd)-1] = '\0'; file->blocksize = DISK_BLOCK_BYTES; - if (srvcompress) { - file->compressed=1; - ap_snprintf(file->uncompress_cmd, sizeof(file->uncompress_cmd), - " %s %s |", UNCOMPRESS_PATH, -#ifdef UNCOMPRESS_OPT - UNCOMPRESS_OPT -#else - "" + /* + * If we're doing the compression here, we need to override what + * sendbackup told us the compression was. + */ + if (srvcompress != COMP_NONE) { + file->compressed = 1; +#ifndef UNCOMPRESS_OPT +#define UNCOMPRESS_OPT "" #endif - ); - strncpy(file->comp_suffix, COMPRESS_SUFFIX,sizeof(file->comp_suffix)-1); - file->comp_suffix[sizeof(file->comp_suffix)-1] = '\0'; + if (srvcompress == COMP_SERVER_CUST) { + g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd), + " %s %s |", srvcompprog, "-d"); + strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1); + file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0'; + strncpy(file->srvcompprog, srvcompprog, SIZEOF(file->srvcompprog) - 1); + file->srvcompprog[SIZEOF(file->srvcompprog) - 1] = '\0'; + } else if ( srvcompress == COMP_CUST ) { + g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd), + " %s %s |", clntcompprog, "-d"); + strncpy(file->comp_suffix, "cust", SIZEOF(file->comp_suffix) - 1); + file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0'; + strncpy(file->clntcompprog, clntcompprog, SIZEOF(file->clntcompprog)); + file->clntcompprog[SIZEOF(file->clntcompprog) - 1] = '\0'; + } else { + g_snprintf(file->uncompress_cmd, SIZEOF(file->uncompress_cmd), + " %s %s |", UNCOMPRESS_PATH, UNCOMPRESS_OPT); + strncpy(file->comp_suffix, COMPRESS_SUFFIX,SIZEOF(file->comp_suffix) - 1); + file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0'; + } + } else { + if (file->comp_suffix[0] == '\0') { + file->compressed = 0; + assert(SIZEOF(file->comp_suffix) >= 2); + strncpy(file->comp_suffix, "N", SIZEOF(file->comp_suffix) - 1); + file->comp_suffix[SIZEOF(file->comp_suffix) - 1] = '\0'; + } else { + file->compressed = 1; + } } - else { - file->uncompress_cmd[0] = '\0'; - file->compressed=compress_suffix!=NULL; - if(compress_suffix) { - strncpy(file->comp_suffix, compress_suffix, - sizeof(file->comp_suffix)-1); - file->comp_suffix[sizeof(file->comp_suffix)-1] = '\0'; + /* take care of the encryption header here */ + if (srvencrypt != ENCRYPT_NONE) { + file->encrypted= 1; + if (srvencrypt == ENCRYPT_SERV_CUST) { + if (srv_decrypt_opt) { + g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd), + " %s %s |", srv_encrypt, srv_decrypt_opt); + strncpy(file->srv_decrypt_opt, srv_decrypt_opt, SIZEOF(file->srv_decrypt_opt) - 1); + file->srv_decrypt_opt[SIZEOF(file->srv_decrypt_opt) - 1] = '\0'; } else { - strncpy(file->comp_suffix, "N", sizeof(file->comp_suffix)-1); - file->comp_suffix[sizeof(file->comp_suffix)-1] = '\0'; + g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd), + " %s |", srv_encrypt); + file->srv_decrypt_opt[0] = '\0'; } + strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1); + file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0'; + strncpy(file->srv_encrypt, srv_encrypt, SIZEOF(file->srv_encrypt) - 1); + file->srv_encrypt[SIZEOF(file->srv_encrypt) - 1] = '\0'; + } else if ( srvencrypt == ENCRYPT_CUST ) { + if (clnt_decrypt_opt) { + g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd), + " %s %s |", clnt_encrypt, clnt_decrypt_opt); + strncpy(file->clnt_decrypt_opt, clnt_decrypt_opt, + SIZEOF(file->clnt_decrypt_opt)); + file->clnt_decrypt_opt[SIZEOF(file->clnt_decrypt_opt) - 1] = '\0'; + } else { + g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd), + " %s |", clnt_encrypt); + file->clnt_decrypt_opt[0] = '\0'; + } + g_snprintf(file->decrypt_cmd, SIZEOF(file->decrypt_cmd), + " %s %s |", clnt_encrypt, clnt_decrypt_opt); + strncpy(file->encrypt_suffix, "enc", SIZEOF(file->encrypt_suffix) - 1); + file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0'; + strncpy(file->clnt_encrypt, clnt_encrypt, SIZEOF(file->clnt_encrypt) - 1); + file->clnt_encrypt[SIZEOF(file->clnt_encrypt) - 1] = '\0'; + } + } else { + if (file->encrypt_suffix[0] == '\0') { + file->encrypted = 0; + assert(SIZEOF(file->encrypt_suffix) >= 2); + strncpy(file->encrypt_suffix, "N", SIZEOF(file->encrypt_suffix) - 1); + file->encrypt_suffix[SIZEOF(file->encrypt_suffix) - 1] = '\0'; + } else { + file->encrypted= 1; + } } - strncpy(file->cont_filename, cont_filename, sizeof(file->cont_filename)-1); - file->cont_filename[sizeof(file->cont_filename)-1] = '\0'; + if (dle_str) + file->dle_str = stralloc(dle_str); + else + file->dle_str = NULL; } -/* Send an Amanda dump header to the output file. - * returns true if an error occured, false on success +/* + * Send an Amanda dump header to the output file. */ - -int write_tapeheader(outfd, file) -int outfd; -dumpfile_t *file; +static ssize_t +write_tapeheader( + int outfd, + dumpfile_t *file) { - char buffer[DISK_BLOCK_BYTES]; - int written; + char * buffer; + size_t written; - build_header(buffer, file, sizeof(buffer)); + if (debug_dumper > 1) + dump_dumpfile_t(file); + buffer = build_header(file, NULL, DISK_BLOCK_BYTES); + if (!buffer) /* this shouldn't happen */ + error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES); + + written = full_write(outfd, buffer, DISK_BLOCK_BYTES); + amfree(buffer); + if(written == DISK_BLOCK_BYTES) + return 0; - written = fullwrite(outfd, buffer, sizeof(buffer)); - if(written == sizeof(buffer)) return 0; - if(written < 0) return written; - errno = ENOSPC; return -1; } +int indexout = -1; -int do_dump(mesgfd, datafd, indexfd, outfd) -int mesgfd, datafd, indexfd, outfd; +static int +do_dump( + struct databuf *db) { - int maxfd, nfound, size1, size2, eof1, eof2; - int rc; - fd_set readset, selectset; - struct timeval timeout; - int outpipe[2]; - int header_done; /* flag - header has been written */ char *indexfile_tmp = NULL; char *indexfile_real = NULL; char level_str[NUM_STR_SIZE]; - char kb_str[NUM_STR_SIZE]; - char kps_str[NUM_STR_SIZE]; - char orig_kb_str[NUM_STR_SIZE]; char *fn; char *q; times_t runtime; double dumptime; /* Time dump took in secs */ - int compresspid = -1, indexpid = -1, killerr; char *errfname = NULL; - -#ifndef DUMPER_SOCKET_BUFFERING -#define DUMPER_SOCKET_BUFFERING 0 -#endif - -#if !defined(SO_RCVBUF) || !defined(SO_RCVLOWAT) -#undef DUMPER_SOCKET_BUFFERING -#define DUMPER_SOCKET_BUFFERING 0 -#endif - -#if DUMPER_SOCKET_BUFFERING - int lowat = NETWORK_BLOCK_BYTES; - int recbuf = 0; - int sizeof_recbuf = sizeof(recbuf); - int lowwatset = 0; - int lowwatset_count = 0; -#endif + pid_t indexpid = -1; + char *m; startclock(); - datain = dataout = databuf; - datalimit = databuf + sizeof(databuf); - dumpsize = dumpbytes = origsize = filesize = dump_result = 0; - nb_header_block = 0; - got_info_endline = got_sizeline = got_endline = 0; - header_done = 0; - amfree(backup_name); - amfree(recover_cmd); - amfree(compress_suffix); - - ap_snprintf(level_str, sizeof(level_str), "%d", level); + if (msg.buf) msg.buf[0] = '\0'; /* reset msg buffer */ + status = 0; + dump_result = 0; + dumpbytes = dumpsize = headersize = origsize = (off_t)0; + fh_init(&file); + + g_snprintf(level_str, SIZEOF(level_str), "%d", level); fn = sanitise_filename(diskname); errfname = newvstralloc(errfname, AMANDA_TMPDIR, @@ -1174,621 +1184,1083 @@ int mesgfd, datafd, indexfd, outfd; NULL); amfree(fn); if((errf = fopen(errfname, "w+")) == NULL) { - errstr = newvstralloc(errstr, - "errfile open \"", errfname, "\": ", - strerror(errno), - NULL); + errstr = newvstrallocf(errstr, "errfile open \"%s\": %s", + errfname, strerror(errno)); amfree(errfname); - rc = 2; goto failed; } unlink(errfname); /* so it goes away on close */ amfree(errfname); - /* insert pipe in the *READ* side, if server-side compression is desired */ - compresspid = -1; - if (srvcompress) { - int tmpfd; - - tmpfd = datafd; - pipe(outpipe); /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */ - datafd = outpipe[0]; - if(datafd < 0 || datafd >= FD_SETSIZE) { - aclose(outpipe[0]); - aclose(outpipe[1]); - errstr = newstralloc(errstr, "descriptor out of range"); - errno = EMFILE; - rc = 2; - goto failed; - } - switch(compresspid=fork()) { - case -1: - errstr = newstralloc2(errstr, "couldn't fork: ", strerror(errno)); - rc = 2; - goto failed; - default: - aclose(outpipe[1]); - aclose(tmpfd); - break; - case 0: - aclose(outpipe[0]); - /* child acts on stdin/stdout */ - if (dup2(outpipe[1],1) == -1) - fprintf(stderr, "err dup2 out: %s\n", strerror(errno)); - if (dup2(tmpfd, 0) == -1) - fprintf(stderr, "err dup2 in: %s\n", strerror(errno)); - safe_fd(-1, 0); - /* now spawn gzip -1 to take care of the rest */ - execlp(COMPRESS_PATH, COMPRESS_PATH, - (srvcompress == srvcomp_best ? COMPRESS_BEST_OPT - : COMPRESS_FAST_OPT), - (char *)0); - error("error: couldn't exec %s.\n", COMPRESS_PATH); - } - /* Now the pipe has been inserted. */ - } - - indexpid = -1; - if (indexfd != -1) { - indexfile_real = getindexfname(hostname, diskname, datestamp, level), + if (streams[INDEXFD].fd != NULL) { + indexfile_real = getindexfname(hostname, diskname, dumper_timestamp, level); indexfile_tmp = stralloc2(indexfile_real, ".tmp"); - if (mkpdir(indexfile_tmp, 02755, (uid_t)-1, (gid_t)-1) == -1) { - errstr = newvstralloc(errstr, - "err create ", + if (mkpdir(indexfile_tmp, 0755, (uid_t)-1, (gid_t)-1) == -1) { + errstr = newvstrallocf(errstr, + _("err create %s: %s"), indexfile_tmp, - ": ", - strerror(errno), - NULL); + strerror(errno)); amfree(indexfile_real); amfree(indexfile_tmp); - rc = 2; goto failed; } - - switch(indexpid=fork()) { - case -1: - errstr = newstralloc2(errstr, "couldn't fork: ", strerror(errno)); - rc = 2; + indexout = open(indexfile_tmp, O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (indexout == -1) { + errstr = newvstrallocf(errstr, _("err open %s: %s"), + indexfile_tmp, strerror(errno)); goto failed; - default: - aclose(indexfd); - indexfd = -1; /* redundant */ - break; - case 0: - if (dup2(indexfd, 0) == -1) { - error("err dup2 in: %s", strerror(errno)); - } - indexfd = open(indexfile_tmp, O_WRONLY | O_CREAT | O_TRUNC, 0600); - if (indexfd == -1) - error("err open %s: %s", indexfile_tmp, strerror(errno)); - if (dup2(indexfd,1) == -1) - error("err dup2 out: %s", strerror(errno)); - safe_fd(-1, 0); - execlp(COMPRESS_PATH, COMPRESS_PATH, COMPRESS_BEST_OPT, (char *)0); - error("error: couldn't exec %s.", COMPRESS_PATH); + } else { + if (runcompress(indexout, &indexpid, COMP_BEST, "index compress") < 0) { + aclose(indexout); + goto failed; + } } + indexfderror = 0; + /* + * Schedule the indexfd for relaying to the index file + */ + security_stream_read(streams[INDEXFD].fd, read_indexfd, &indexout); } - NAUGHTY_BITS_INITIALIZE; + /* + * We only need to process messages initially. Once we have done + * the header, we will start processing data too. + */ + security_stream_read(streams[MESGFD].fd, read_mesgfd, db); + set_datafd = 0; + + /* + * Setup a read timeout + */ + timeout(conf_dtimeout); + + /* + * Start the event loop. This will exit when all three events + * (read the mesgfd, read the datafd, and timeout) are removed. + */ + event_loop(0); + + if (!ISSET(status, HEADER_DONE)) { + dump_result = max(dump_result, 2); + if (!errstr) errstr = stralloc(_("got no header information")); + } - maxfd = max(mesgfd, datafd) + 1; - eof1 = eof2 = 0; + dumpsize -= headersize; /* don't count the header */ + if (dumpsize <= (off_t)0 && data_path == DATA_PATH_AMANDA) { + dumpsize = (off_t)0; + dump_result = max(dump_result, 2); + if (!errstr) errstr = stralloc(_("got no data")); + } - FD_ZERO(&readset); + if (data_path == DATA_PATH_DIRECTTCP) { + dumpsize = origsize; + } - /* Just process messages for now. Once we have done the header - ** we will start processing data too. - */ - FD_SET(mesgfd, &readset); + if (!ISSET(status, HEADER_DONE)) { + dump_result = max(dump_result, 2); + if (!errstr) errstr = stralloc(_("got no header information")); + } - if(datafd == -1) eof1 = 1; /* fake eof on data */ + if (dumpsize == 0 && data_path == DATA_PATH_AMANDA) { + dump_result = max(dump_result, 2); + if (!errstr) errstr = stralloc(_("got no data")); + } -#if DUMPER_SOCKET_BUFFERING + if (dump_result > 1) + goto failed; -#ifndef EST_PACKET_SIZE -#define EST_PACKET_SIZE 512 -#endif -#ifndef EST_MIN_WINDOW -#define EST_MIN_WINDOW EST_PACKET_SIZE*4 /* leave room for 2k in transit */ -#endif + runtime = stopclock(); + dumptime = g_timeval_to_double(runtime); - else { - recbuf = STREAM_BUFSIZE; - if (setsockopt(datafd, SOL_SOCKET, SO_RCVBUF, - (void *) &recbuf, sizeof_recbuf)) { - const int errornumber = errno; - fprintf(stderr, "%s: pid %ld setsockopt(SO_RCVBUF): %s\n", - get_pname(), (long) getpid(), strerror(errornumber)); - } - if (getsockopt(datafd, SOL_SOCKET, SO_RCVBUF, - (void *) &recbuf, (void *)&sizeof_recbuf)) { - const int errornumber = errno; - fprintf(stderr, "%s: pid %ld getsockopt(SO_RCVBUF): %s\n", - get_pname(), (long) getpid(), strerror(errornumber)); - recbuf = 0; - } + amfree(errstr); + errstr = alloc(128); + g_snprintf(errstr, 128, _("sec %s kb %lld kps %3.1lf orig-kb %lld"), + walltime_str(runtime), + (long long)dumpsize, + (isnormal(dumptime) ? ((double)dumpsize / (double)dumptime) : 0.0), + (long long)origsize); + m = vstrallocf("[%s]", errstr); + q = quote_string(m); + amfree(m); + putresult(DONE, _("%s %lld %lld %lu %s\n"), handle, + (long long)origsize, + (long long)dumpsize, + (unsigned long)((double)dumptime+0.5), q); + amfree(q); - /* leave at least EST_MIN_WINDOW between lowwat and recbuf */ - if (recbuf-lowat < EST_MIN_WINDOW) - lowat = recbuf-EST_MIN_WINDOW; + switch(dump_result) { + case 0: + log_add(L_SUCCESS, "%s %s %s %d [%s]", hostname, qdiskname, dumper_timestamp, level, errstr); - /* if lowwat < ~512, don't bother */ - if (lowat < EST_PACKET_SIZE) - recbuf = 0; - fprintf(stderr, "%s: pid %ld receive size is %d, low water is %d\n", - get_pname(), (long) getpid(), recbuf, lowat); - } -#endif + break; - while(!(eof1 && eof2)) { - -#if DUMPER_SOCKET_BUFFERING - /* Set socket buffering */ - if (recbuf>0 && !lowwatset) { - if (setsockopt(datafd, SOL_SOCKET, SO_RCVLOWAT, - (void *) &lowat, sizeof(lowat))) { - const int errornumber = errno; - fprintf(stderr, - "%s: pid %ld setsockopt(SO_RCVLOWAT): %s\n", - get_pname(), (long) getpid(), strerror(errornumber)); - } - lowwatset = 1; - lowwatset_count++; - } -#endif + case 1: + log_start_multiline(); + log_add(L_STRANGE, "%s %s %d [%s]", hostname, qdiskname, level, errstr); + log_msgout(L_STRANGE); + log_end_multiline(); - timeout.tv_sec = conf_dtimeout; - timeout.tv_usec = 0; - memcpy(&selectset, &readset, sizeof(fd_set)); + break; + } - nfound = select(maxfd, (SELECT_ARG_TYPE *)(&selectset), NULL, NULL, &timeout); + if (errf) afclose(errf); - /* check for errors or timeout */ + if (data_path == DATA_PATH_AMANDA) + aclose(db->fd); -#if DUMPER_SOCKET_BUFFERING - if (nfound==0 && lowwatset) { - const int zero = 0; - /* Disable socket buffering and ... */ - if (setsockopt(datafd, SOL_SOCKET, SO_RCVLOWAT, - (void *) &zero, sizeof(zero))) { - const int errornumber = errno; - fprintf(stderr, - "%s: pid %ld setsockopt(SO_RCVLOWAT): %s\n", - get_pname(), (long) getpid(), strerror(errornumber)); - } - lowwatset = 0; + if (indexfile_tmp) { + amwait_t index_status; - /* ... try once more */ - timeout.tv_sec = conf_dtimeout; - timeout.tv_usec = 0; - memcpy(&selectset, &readset, sizeof(fd_set)); - nfound = select(maxfd, (SELECT_ARG_TYPE *)(&selectset), NULL, NULL, &timeout); + /*@i@*/ aclose(indexout); + waitpid(indexpid,&index_status,0); + log_add(L_INFO, "pid-done %ld", (long)indexpid); + if (rename(indexfile_tmp, indexfile_real) != 0) { + log_add(L_WARNING, _("could not rename \"%s\" to \"%s\": %s"), + indexfile_tmp, indexfile_real, strerror(errno)); } -#endif + amfree(indexfile_tmp); + amfree(indexfile_real); + } - if(nfound == 0) { - errstr = newstralloc(errstr, "data timeout"); - rc = 2; - goto failed; - } - if(nfound == -1) { - errstr = newstralloc2(errstr, "select: ", strerror(errno)); - rc = 2; - goto failed; - } + if(db->compresspid != -1) { + waitpid(db->compresspid,NULL,0); + log_add(L_INFO, "pid-done %ld", (long)db->compresspid); + } + if(db->encryptpid != -1) { + waitpid(db->encryptpid,NULL,0); + log_add(L_INFO, "pid-done %ld", (long)db->encryptpid); + } - /* read/write any data */ + amfree(errstr); + dumpfile_free_data(&file); - if(datafd >= 0 && FD_ISSET(datafd, &selectset)) { - size1 = read(datafd, datain, datalimit - datain); - if(size1 < 0) { - errstr = newstralloc2(errstr, "data read: ", strerror(errno)); - rc = 2; - goto failed; - } - if(update_dataptr(&outfd, size1)) { - rc = 2; - goto failed; - } - if(size1 == 0) { - eof1 = 1; - FD_CLR(datafd, &readset); - aclose(datafd); + return 1; + +failed: + m = vstrallocf("[%s]", errstr); + q = quote_string(m); + putresult(FAILED, "%s %s\n", handle, q); + amfree(q); + amfree(m); + + aclose(db->fd); + /* kill all child process */ + if (db->compresspid != -1) { + g_fprintf(stderr,_("%s: kill compress command\n"),get_pname()); + if (kill(db->compresspid, SIGTERM) < 0) { + if (errno != ESRCH) { + g_fprintf(stderr,_("%s: can't kill compress command: %s\n"), + get_pname(), strerror(errno)); + } else { + log_add(L_INFO, "pid-done %ld", (long)db->compresspid); } } + else { + waitpid(db->compresspid,NULL,0); + log_add(L_INFO, "pid-done %ld", (long)db->compresspid); + } + } - if(mesgfd >= 0 && FD_ISSET(mesgfd, &selectset)) { - size2 = read(mesgfd, mesgbuf, sizeof(mesgbuf)-1); - switch(size2) { - case -1: - errstr = newstralloc2(errstr, "mesg read: ", strerror(errno)); - rc = 2; - goto failed; - case 0: - eof2 = 1; - process_dumpeof(); - FD_CLR(mesgfd, &readset); - aclose(mesgfd); - break; - default: - mesgbuf[size2] = '\0'; - add_msg_data(mesgbuf, size2); + if (db->encryptpid != -1) { + g_fprintf(stderr,_("%s: kill encrypt command\n"),get_pname()); + if (kill(db->encryptpid, SIGTERM) < 0) { + if (errno != ESRCH) { + g_fprintf(stderr,_("%s: can't kill encrypt command: %s\n"), + get_pname(), strerror(errno)); + } else { + log_add(L_INFO, "pid-done %ld", (long)db->encryptpid); } + } + else { + waitpid(db->encryptpid,NULL,0); + log_add(L_INFO, "pid-done %ld", (long)db->encryptpid); + } + } - if (got_info_endline && !header_done) { /* time to do the header */ - make_tapeheader(&file, F_DUMPFILE); - if (write_tapeheader(outfd, &file)) { - int save_errno = errno; - errstr = newstralloc2(errstr, "write_tapeheader: ", - strerror(errno)); - if(save_errno == ENOSPC) { - putresult(NO_ROOM, "%s %lu\n", handle, - use+split_size-dumpsize); - use = 0; /* force RQ_MORE_DISK */ - split_size = dumpsize; - rc = 1; - } - else { - rc = 2; - } - goto failed; - } - dumpsize += DISK_BLOCK_KB; - filesize += DISK_BLOCK_KB; - nb_header_block++; - header_done = 1; - strncat(cont_filename,filename,sizeof(cont_filename)); - cont_filename[sizeof(cont_filename)-1] = '\0'; - - if (datafd != -1) - FD_SET(datafd, &readset); /* now we can read the data */ + if (indexpid != -1) { + g_fprintf(stderr,_("%s: kill index command\n"),get_pname()); + if (kill(indexpid, SIGTERM) < 0) { + if (errno != ESRCH) { + g_fprintf(stderr,_("%s: can't kill index command: %s\n"), + get_pname(),strerror(errno)); + } else { + log_add(L_INFO, "pid-done %ld", (long)indexpid); } } - } /* end while */ + else { + waitpid(indexpid,NULL,0); + log_add(L_INFO, "pid-done %ld", (long)indexpid); + } + } -#if DUMPER_SOCKET_BUFFERING - if(lowwatset_count > 1) { - fprintf(stderr, "%s: pid %ld low water set %d times\n", - get_pname(), (long) getpid(), lowwatset_count); + log_start_multiline(); + log_add(L_FAIL, _("%s %s %s %d [%s]"), hostname, qdiskname, dumper_timestamp, + level, errstr); + if (errf) { + log_msgout(L_FAIL); } -#endif + log_end_multiline(); - if(dump_result > 1) { - rc = 2; - goto failed; + if (errf) afclose(errf); + + if (indexfile_tmp) { + unlink(indexfile_tmp); + amfree(indexfile_tmp); + amfree(indexfile_real); } - runtime = stopclock(); - dumptime = runtime.r.tv_sec + runtime.r.tv_usec/1000000.0; - - dumpsize -= (nb_header_block * DISK_BLOCK_KB);/* don't count the header */ - if (dumpsize < 0) dumpsize = 0; /* XXX - maybe this should be fatal? */ - - ap_snprintf(kb_str, sizeof(kb_str), "%ld", dumpsize); - ap_snprintf(kps_str, sizeof(kps_str), - "%3.1f", - dumptime ? dumpsize / dumptime : 0.0); - ap_snprintf(orig_kb_str, sizeof(orig_kb_str), "%ld", origsize); - errstr = newvstralloc(errstr, - "sec ", walltime_str(runtime), - " ", "kb ", kb_str, - " ", "kps ", kps_str, - " ", "orig-kb ", orig_kb_str, - NULL); - q = squotef("[%s]", errstr); - putresult(DONE, "%s %ld %ld %ld %s\n", handle, origsize, dumpsize, - (long)(dumptime+0.5), q); - amfree(q); + return 0; +} + +/* + * Callback for reads on the mesgfd stream + */ +static void +read_mesgfd( + void * cookie, + void * buf, + ssize_t size) +{ + struct databuf *db = cookie; + + assert(db != NULL); + + switch (size) { + case -1: + errstr = newvstrallocf(errstr, _("mesg read: %s"), + security_stream_geterror(streams[MESGFD].fd)); + dump_result = 2; + stop_dump(); + return; - switch(dump_result) { case 0: - log_add(L_SUCCESS, "%s %s %s %d [%s]", hostname, diskname, datestamp, level, errstr); + /* + * EOF. Just shut down the mesg stream. + */ + process_dumpeof(); + security_stream_close(streams[MESGFD].fd); + streams[MESGFD].fd = NULL; + /* + * If the data fd and index fd has also shut down, then we're done. + */ + if ((set_datafd == 0 || streams[DATAFD].fd == NULL) && + streams[INDEXFD].fd == NULL) + stop_dump(); + return; + default: + assert(buf != NULL); + add_msg_data(buf, (size_t)size); + security_stream_read(streams[MESGFD].fd, read_mesgfd, cookie); break; + } - case 1: - log_start_multiline(); - log_add(L_STRANGE, "%s %s %d [%s]", hostname, diskname, level, errstr); - log_msgout(L_STRANGE); - log_end_multiline(); + if (ISSET(status, GOT_INFO_ENDLINE) && !ISSET(status, HEADER_DONE)) { + /* Use the first in the dataport_list */ + in_port_t data_port; + char *data_host = dataport_list; + char *s= strchr(dataport_list, ':'); + *s = '\0'; + s++; + data_port = atoi(s); + + SET(status, HEADER_DONE); + /* time to do the header */ + finish_tapeheader(&file); + if (write_tapeheader(db->fd, &file)) { + errstr = newvstrallocf(errstr, _("write_tapeheader: %s"), + strerror(errno)); + dump_result = 2; + stop_dump(); + dumpfile_free_data(&file); + return; + } + dumpfile_free_data(&file); + aclose(db->fd); + if (data_path == DATA_PATH_AMANDA) { + g_debug(_("Sending data to %s:%d\n"), data_host, data_port); + db->fd = stream_client(data_host, data_port, + STREAM_BUFSIZE, 0, NULL, 0); + if (db->fd == -1) { + errstr = newvstrallocf(errstr, + _("Can't opendata output stream: %s"), + strerror(errno)); + dump_result = 2; + stop_dump(); + return; + } + } - break; + dumpsize += (off_t)DISK_BLOCK_KB; + headersize += (off_t)DISK_BLOCK_KB; + + if (srvencrypt == ENCRYPT_SERV_CUST) { + if (runencrypt(db->fd, &db->encryptpid, srvencrypt) < 0) { + dump_result = 2; + stop_dump(); + return; + } + } + /* + * Now, setup the compress for the data output, and start + * reading the datafd. + */ + if ((srvcompress != COMP_NONE) && (srvcompress != COMP_CUST)) { + if (runcompress(db->fd, &db->compresspid, srvcompress, "data compress") < 0) { + dump_result = 2; + stop_dump(); + return; + } + } + security_stream_read(streams[DATAFD].fd, read_datafd, db); + set_datafd = 1; } - if(errf) afclose(errf); + /* + * Reset the timeout for future reads + */ + timeout(conf_dtimeout); +} - if (indexfile_tmp) { - waitpid(indexpid,NULL,0); - if(rename(indexfile_tmp, indexfile_real) != 0) { - log_add(L_WARNING, "could not rename \"%s\" to \"%s\": %s", - indexfile_tmp, indexfile_real, strerror(errno)); +/* + * Callback for reads on the datafd stream + */ +static void +read_datafd( + void * cookie, + void * buf, + ssize_t size) +{ + struct databuf *db = cookie; + + assert(db != NULL); + + /* + * The read failed. Error out + */ + if (size < 0) { + errstr = newvstrallocf(errstr, _("data read: %s"), + security_stream_geterror(streams[DATAFD].fd)); + dump_result = 2; + aclose(db->fd); + stop_dump(); + return; + } + + /* The header had better be written at this point */ + assert(ISSET(status, HEADER_DONE)); + + /* + * EOF. Stop and return. + */ + if (size == 0) { + databuf_flush(db); + if (dumpbytes != (off_t)0) { + dumpsize += (off_t)1; } - amfree(indexfile_tmp); - amfree(indexfile_real); + security_stream_close(streams[DATAFD].fd); + streams[DATAFD].fd = NULL; + aclose(db->fd); + /* + * If the mesg fd and index fd has also shut down, then we're done. + */ + if (streams[MESGFD].fd == NULL && streams[INDEXFD].fd == NULL) + stop_dump(); + return; } - return 0; + /* + * We read something. Add it to the databuf and reschedule for + * more data. + */ + assert(buf != NULL); + if (databuf_write(db, buf, (size_t)size) < 0) { + int save_errno = errno; + errstr = newvstrallocf(errstr, _("data write: %s"), strerror(save_errno)); + dump_result = 2; + stop_dump(); + return; + } + + /* + * Reset the timeout for future reads + */ + timeout(conf_dtimeout); + + security_stream_read(streams[DATAFD].fd, read_datafd, cookie); +} - failed: +/* + * Callback for reads on the index stream + */ +static void +read_indexfd( + void * cookie, + void * buf, + ssize_t size) +{ + int fd; -#if DUMPER_SOCKET_BUFFERING - if(lowwatset_count > 1) { - fprintf(stderr, "%s: pid %ld low water set %d times\n", - get_pname(), (long) getpid(), lowwatset_count); + assert(cookie != NULL); + fd = *(int *)cookie; + + if (size < 0) { + errstr = newvstrallocf(errstr, _("index read: %s"), + security_stream_geterror(streams[INDEXFD].fd)); + dump_result = 2; + stop_dump(); + return; } -#endif - if(!abort_pending) { - q = squotef("[%s]", errstr); - if(rc==2) - putresult(FAILED, "%s %s\n", handle, q); - else - putresult(TRYAGAIN, "%s %s\n", handle, q); - amfree(q); + /* + * EOF. Stop and return. + */ + if (size == 0) { + security_stream_close(streams[INDEXFD].fd); + streams[INDEXFD].fd = NULL; + /* + * If the mesg fd has also shut down, then we're done. + */ + if ((set_datafd == 0 || streams[DATAFD].fd == NULL) && + streams[MESGFD].fd == NULL) + stop_dump(); + aclose(indexout); + return; } - /* kill all child process */ - if(compresspid != -1) { - killerr = kill(compresspid,SIGTERM); - if(killerr == 0) { - fprintf(stderr,"%s: kill compress command\n",get_pname()); - } - else if ( killerr == -1 ) { - if(errno != ESRCH) - fprintf(stderr,"%s: can't kill compress command: %s\n", - get_pname(), strerror(errno)); + assert(buf != NULL); + + /* + * We ignore error while writing to the index file. + */ + if (full_write(fd, buf, (size_t)size) < (size_t)size) { + /* Ignore error, but schedule another read. */ + if(indexfderror == 0) { + indexfderror = 1; + log_add(L_INFO, _("Index corrupted for %s:%s"), hostname, qdiskname); } } + security_stream_read(streams[INDEXFD].fd, read_indexfd, cookie); +} - if(indexpid != -1) { - killerr = kill(indexpid,SIGTERM); - if(killerr == 0) { - fprintf(stderr,"%s: kill index command\n",get_pname()); +static void +handle_filter_stderr( + void *cookie) +{ + filter_t *filter = cookie; + ssize_t nread; + char *b, *p; + gint64 len; + + event_release(filter->event); + + if (filter->buffer == NULL) { + /* allocate initial buffer */ + filter->buffer = g_malloc(2048); + filter->first = 0; + filter->size = 0; + filter->allocated_size = 2048; + } else if (filter->first > 0) { + if (filter->allocated_size - filter->size - filter->first < 1024) { + memmove(filter->buffer, filter->buffer + filter->first, + filter->size); + filter->first = 0; } - else if ( killerr == -1 ) { - if(errno != ESRCH) - fprintf(stderr,"%s: can't kill index command: %s\n", - get_pname(),strerror(errno)); + } else if (filter->allocated_size - filter->size < 1024) { + /* double the size of the buffer */ + filter->allocated_size *= 2; + filter->buffer = g_realloc(filter->buffer, filter->allocated_size); + } + + nread = read(filter->fd, filter->buffer + filter->first + filter->size, + filter->allocated_size - filter->first - filter->size - 2); + + if (nread != 0) { + dump_result = max(dump_result, 2); + } + + if (nread <= 0) { + aclose(filter->fd); + if (filter->size > 0 && filter->buffer[filter->first + filter->size - 1] != '\n') { + /* Add a '\n' at end of buffer */ + filter->buffer[filter->first + filter->size] = '\n'; + filter->size++; } + } else { + filter->size += nread; } - if(!abort_pending) { - log_start_multiline(); - log_add(L_FAIL, "%s %s %s %d [%s]", hostname, diskname, - datestamp, level, errstr); - if (errf) { - log_msgout(L_FAIL); + /* process all complete lines */ + b = filter->buffer + filter->first; + filter->buffer[filter->first + filter->size] = '\0'; + while (b < filter->buffer + filter->first + filter->size && + (p = strchr(b, '\n')) != NULL) { + *p = '\0'; + g_fprintf(errf, _("? %s: %s\n"), filter->name, b); + if (errstr == NULL) { + errstr = stralloc(b); } - log_end_multiline(); + len = p - b + 1; + filter->first += len; + filter->size -= len; + b = p + 1; } - if(errf) afclose(errf); + if (nread <= 0) { + g_free(filter->buffer); + g_free(filter); + } else { + filter->event = event_register((event_id_t)filter->fd, EV_READFD, + handle_filter_stderr, filter); + } +} - if (indexfile_tmp) { - unlink(indexfile_tmp); - amfree(indexfile_tmp); - amfree(indexfile_real); +/* + * Startup a timeout in the event handler. If the arg is 0, + * then remove the timeout. + */ +static void +timeout( + time_t seconds) +{ + static event_handle_t *ev_timeout = NULL; + + /* + * First, remove a timeout if one is active. + */ + if (ev_timeout != NULL) { + event_release(ev_timeout); + ev_timeout = NULL; } - return rc; + /* + * Now, schedule a new one if 'seconds' is greater than 0 + */ + if (seconds > 0) + ev_timeout = event_register((event_id_t)seconds, EV_TIME, timeout_callback, NULL); } -/* -------------------- */ +/* + * This is the callback for timeout(). If this is reached, then we + * have a data timeout. + */ +static void +timeout_callback( + void * unused) +{ + (void)unused; /* Quiet unused parameter warning */ -char *hostname, *disk; -int response_error; + assert(unused == NULL); + errstr = newstralloc(errstr, _("data timeout")); + dump_result = 2; + stop_dump(); +} -void sendbackup_response(p, pkt) -proto_t *p; -pkt_t *pkt; +/* + * This is called when everything needs to shut down so event_loop() + * will exit. + */ +static void +stop_dump(void) { - int data_port = -1; - int mesg_port = -1; - int index_port = -1; - char *line; - char *fp; - char *s; - char *t; - int ch; - int tch; - - am_release_feature_set(their_features); - their_features = NULL; - - if(p->state == S_FAILED) { - if(pkt == NULL) { - if(p->prevstate == S_REPWAIT) { - errstr = newstralloc(errstr, "[reply timeout]"); - } - else { - errstr = newstralloc(errstr, "[request timeout]"); - } - response_error = 1; - return; + int i; + struct cmdargs *cmdargs = NULL; + + /* Check if I have a pending ABORT command */ + cmdargs = get_pending_cmd(); + if (cmdargs) { + if (cmdargs->cmd != ABORT) { + error(_("beurk")); } + amfree(errstr); + errstr = stralloc(cmdargs->argv[1]); + free_cmdargs(cmdargs); } -#if 0 - fprintf(stderr, "got %sresponse:\n----\n%s----\n\n", - (p->state == S_FAILED) ? "NAK " : "", pkt->body); -#endif + for (i = 0; i < NSTREAMS; i++) { + if (streams[i].fd != NULL) { + security_stream_close(streams[i].fd); + streams[i].fd = NULL; + } + } + aclose(indexout); + timeout(0); +} + + +/* + * Runs compress with the first arg as its stdout. Returns + * 0 on success or negative if error, and it's pid via the second + * argument. The outfd arg is dup2'd to the pipe to the compress + * process. + */ +static int +runcompress( + int outfd, + pid_t * pid, + comp_t comptype, + char *name) +{ + int outpipe[2], rval; + int errpipe[2]; + filter_t *filter; + + assert(outfd >= 0); + assert(pid != NULL); + + /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */ + if (pipe(outpipe) < 0) { + errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno)); + return (-1); + } + + /* errpipe[0] is pipe's output, outpipe[1] is input. */ + if (pipe(errpipe) < 0) { + errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno)); + return (-1); + } + + switch (*pid = fork()) { + case -1: + errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno)); + aclose(outpipe[0]); + aclose(outpipe[1]); + aclose(errpipe[0]); + aclose(errpipe[1]); + return (-1); + default: + rval = dup2(outpipe[1], outfd); + if (rval < 0) + errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno)); + aclose(outpipe[1]); + aclose(outpipe[0]); + aclose(errpipe[1]); + filter = g_new0(filter_t, 1); + filter->fd = errpipe[0]; + filter->name = name; + filter->buffer = NULL; + filter->size = 0; + filter->allocated_size = 0; + filter->event = event_register((event_id_t)filter->fd, EV_READFD, + handle_filter_stderr, filter); +g_debug("event register %s %d", name, filter->fd); + return (rval); + case 0: + close(outpipe[1]); + close(errpipe[0]); + if (dup2(outpipe[0], 0) < 0) { + error(_("err dup2 in: %s"), strerror(errno)); + /*NOTREACHED*/ + } + if (dup2(outfd, 1) == -1) { + error(_("err dup2 out: %s"), strerror(errno)); + /*NOTREACHED*/ + } + if (dup2(errpipe[1], 2) == -1) { + error(_("err dup2 err: %s"), strerror(errno)); + /*NOTREACHED*/ + } + if (comptype != COMP_SERVER_CUST) { + char *base = stralloc(COMPRESS_PATH); + log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid()); + amfree(base); + safe_fd(-1, 0); + set_root_privs(-1); + execlp(COMPRESS_PATH, COMPRESS_PATH, ( comptype == COMP_BEST ? + COMPRESS_BEST_OPT : COMPRESS_FAST_OPT), (char *)NULL); + error(_("error: couldn't exec %s: %s"), COMPRESS_PATH, strerror(errno)); + /*NOTREACHED*/ + } else if (*srvcompprog) { + char *base = stralloc(srvcompprog); + log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid()); + amfree(base); + safe_fd(-1, 0); + set_root_privs(-1); + execlp(srvcompprog, srvcompprog, (char *)0); + error(_("error: couldn't exec server custom compression '%s'.\n"), srvcompprog); + /*NOTREACHED*/ + } + } + /*NOTREACHED*/ + return (-1); +} + +/* + * Runs encrypt with the first arg as its stdout. Returns + * 0 on success or negative if error, and it's pid via the second + * argument. The outfd arg is dup2'd to the pipe to the encrypt + * process. + */ +static int +runencrypt( + int outfd, + pid_t * pid, + encrypt_t encrypttype) +{ + int outpipe[2], rval; + int errpipe[2]; + filter_t *filter; + + assert(outfd >= 0); + assert(pid != NULL); + + /* outpipe[0] is pipe's stdin, outpipe[1] is stdout. */ + if (pipe(outpipe) < 0) { + errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno)); + return (-1); + } + + /* errpipe[0] is pipe's output, outpipe[1] is input. */ + if (pipe(errpipe) < 0) { + errstr = newvstrallocf(errstr, _("pipe: %s"), strerror(errno)); + return (-1); + } + + switch (*pid = fork()) { + case -1: + errstr = newvstrallocf(errstr, _("couldn't fork: %s"), strerror(errno)); + aclose(outpipe[0]); + aclose(outpipe[1]); + aclose(errpipe[0]); + aclose(errpipe[1]); + return (-1); + default: + rval = dup2(outpipe[1], outfd); + if (rval < 0) + errstr = newvstrallocf(errstr, _("couldn't dup2: %s"), strerror(errno)); + aclose(outpipe[1]); + aclose(outpipe[0]); + aclose(errpipe[1]); + filter = g_new0(filter_t, 1); + filter->fd = errpipe[0]; + filter->name = "encrypt"; + filter->buffer = NULL; + filter->size = 0; + filter->allocated_size = 0; + filter->event = event_register((event_id_t)filter->fd, EV_READFD, + handle_filter_stderr, filter); +g_debug("event register %s %d", "encrypt data", filter->fd); + return (rval); + case 0: { + char *base; + if (dup2(outpipe[0], 0) < 0) { + error(_("err dup2 in: %s"), strerror(errno)); + /*NOTREACHED*/ + } + if (dup2(outfd, 1) < 0 ) { + error(_("err dup2 out: %s"), strerror(errno)); + /*NOTREACHED*/ + } + if (dup2(errpipe[1], 2) == -1) { + error(_("err dup2 err: %s"), strerror(errno)); + /*NOTREACHED*/ + } + close(errpipe[0]); + base = stralloc(srv_encrypt); + log_add(L_INFO, "%s pid %ld", basename(base), (long)getpid()); + amfree(base); + safe_fd(-1, 0); + if ((encrypttype == ENCRYPT_SERV_CUST) && *srv_encrypt) { + set_root_privs(-1); + execlp(srv_encrypt, srv_encrypt, (char *)0); + error(_("error: couldn't exec server custom encryption '%s'.\n"), srv_encrypt); + /*NOTREACHED*/ + } + } + } + /*NOTREACHED*/ + return (-1); +} + + +/* -------------------- */ -#ifdef KRB4_SECURITY - if(krb4_auth && !check_mutual_authenticator(&cred.session, pkt, p)) { - errstr = newstralloc(errstr, "mutual-authentication failed"); - response_error = 2; +static void +sendbackup_response( + void * datap, + pkt_t * pkt, + security_handle_t * sech) +{ + int ports[NSTREAMS], *response_error = datap, i; + char *p; + char *tok; + char *extra; + + assert(response_error != NULL); + assert(sech != NULL); + + security_close_connection(sech, hostname); + + if (pkt == NULL) { + errstr = newvstrallocf(errstr, _("[request failed: %s]"), + security_geterror(sech)); + *response_error = 1; return; } + + extra = NULL; + memset(ports, 0, SIZEOF(ports)); + if (pkt->type == P_NAK) { +#if defined(PACKET_DEBUG) + g_fprintf(stderr, _("got nak response:\n----\n%s\n----\n\n"), pkt->body); #endif - s = pkt->body; - ch = *s++; - while(ch) { - line = s - 1; - skip_line(s, ch); - if (s[-2] == '\n') { - s[-2] = '\0'; + tok = strtok(pkt->body, " "); + if (tok == NULL || strcmp(tok, "ERROR") != 0) + goto bad_nak; + + tok = strtok(NULL, "\n"); + if (tok != NULL) { + errstr = newvstrallocf(errstr, "NAK: %s", tok); + *response_error = 1; + } else { +bad_nak: + errstr = newvstrallocf(errstr, "request NAK"); + *response_error = 2; } + return; + } -#define sc "OPTIONS " - if(strncmp(line, sc, sizeof(sc)-1) == 0) { -#undef sc - -#define sc "features=" - t = strstr(line, sc); - if(t != NULL && (isspace((int)t[-1]) || t[-1] == ';')) { - t += sizeof(sc)-1; -#undef sc - am_release_feature_set(their_features); - if((their_features = am_string_to_feature(t)) == NULL) { - errstr = newvstralloc(errstr, - "bad features value: ", - line, - NULL); - } - } + if (pkt->type != P_REP) { + errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"), + pkt_type2str(pkt->type), pkt->body); + *response_error = 1; + return; + } - continue; - } + dbprintf(_("got response:\n----\n%s\n----\n\n"), pkt->body); -#define sc "ERROR " - if(strncmp(line, sc, sizeof(sc)-1) == 0) { - t = line + sizeof(sc)-1; - tch = t[-1]; -#undef sc - - fp = t - 1; - skip_whitespace(t, tch); - errstr = newvstralloc(errstr, - (p->state == S_FAILED) ? "nak error: " : "", - fp, - NULL); - response_error = ((p->state == S_FAILED) ? 1 : 2); + for(i = 0; i < NSTREAMS; i++) { + ports[i] = -1; + streams[i].fd = NULL; + } + + p = pkt->body; + while((tok = strtok(p, " \n")) != NULL) { + p = NULL; + + /* + * Error response packets have "ERROR" followed by the error message + * followed by a newline. + */ + if (strcmp(tok, "ERROR") == 0) { + tok = strtok(NULL, "\n"); + if (tok == NULL) + tok = _("[bogus error packet]"); + errstr = newvstrallocf(errstr, "%s", tok); + *response_error = 2; return; } -#define sc "CONNECT " - if(strncmp(line, sc, sizeof(sc)-1) == 0) { -#undef sc + /* + * Regular packets have CONNECT followed by three streams + */ + if (strcmp(tok, "CONNECT") == 0) { -#define sc "DATA " - t = strstr(line, sc); - if(t != NULL && isspace((int)t[-1])) { - t += sizeof(sc)-1; -#undef sc - data_port = atoi(t); + /* + * Parse the three stream specifiers out of the packet. + */ + for (i = 0; i < NSTREAMS; i++) { + tok = strtok(NULL, " "); + if (tok == NULL || strcmp(tok, streams[i].name) != 0) { + extra = vstrallocf( + _("CONNECT token is \"%s\": expected \"%s\""), + tok ? tok : "(null)", + streams[i].name); + goto parse_error; + } + tok = strtok(NULL, " \n"); + if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) { + extra = vstrallocf( + _("CONNECT %s token is \"%s\": expected a port number"), + streams[i].name, tok ? tok : "(null)"); + goto parse_error; + } } + continue; + } -#define sc "MESG " - t = strstr(line, sc); - if(t != NULL && isspace((int)t[-1])) { - t += sizeof(sc)-1; -#undef sc - mesg_port = atoi(t); + /* + * OPTIONS [options string] '\n' + */ + if (strcmp(tok, "OPTIONS") == 0) { + tok = strtok(NULL, "\n"); + if (tok == NULL) { + extra = vstrallocf(_("OPTIONS token is missing")); + goto parse_error; } -#define sc "INDEX " - t = strstr(line, sc); - if(t != NULL && isspace((int)t[-1])) { - t += sizeof(sc)-1; -#undef sc - index_port = atoi(t); + while((p = strchr(tok, ';')) != NULL) { + char ch; + *p++ = '\0'; + if(strncmp_const_skip(tok, "features=", tok, ch) == 0) { + char *u = strchr(tok, ';'); + if (u) + *u = '\0'; + am_release_feature_set(their_features); + if((their_features = am_string_to_feature(tok)) == NULL) { + errstr = newvstrallocf(errstr, + _("OPTIONS: bad features value: %s"), + tok); + goto parse_error; + } + if (u) + *u = ';'; + } + tok = p; } continue; } - errstr = newvstralloc(errstr, - "unknown response: ", - line, - NULL); - response_error = 2; - return; + extra = vstrallocf(_("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""), + tok ? tok : "(null)"); + goto parse_error; } - if (data_port == -1 || mesg_port == -1) { - errstr = newvstralloc(errstr, "bad CONNECT response", NULL); - response_error = 2; - return; - } + if (dumper_kencrypt == KENCRYPT_WILL_DO) + dumper_kencrypt = KENCRYPT_YES; - datafd = stream_client(hostname, data_port, -1, -1, NULL); - if(datafd == -1) { - errstr = newvstralloc(errstr, - "could not connect to data port: ", - strerror(errno), - NULL); - response_error = 1; - return; - } - mesgfd = stream_client(hostname, mesg_port, -1, -1, NULL); - if(mesgfd == -1) { - errstr = newvstralloc(errstr, - "could not connect to mesg port: ", - strerror(errno), - NULL); - aclose(datafd); - datafd = -1; /* redundant */ - response_error = 1; - return; + /* + * Connect the streams to their remote ports + */ + for (i = 0; i < NSTREAMS; i++) { + if (ports[i] == -1) + continue; + streams[i].fd = security_stream_client(sech, ports[i]); + if (streams[i].fd == NULL) { + errstr = newvstrallocf(errstr, + _("[could not connect %s stream: %s]"), + streams[i].name, + security_geterror(sech)); + goto connect_error; + } } - if (index_port != -1) { - indexfd = stream_client(hostname, index_port, -1, -1, NULL); - if (indexfd == -1) { - errstr = newvstralloc(errstr, - "could not connect to index port: ", - strerror(errno), - NULL); - aclose(datafd); - aclose(mesgfd); - datafd = mesgfd = -1; /* redundant */ - response_error = 1; - return; + /* + * Authenticate the streams + */ + for (i = 0; i < NSTREAMS; i++) { + if (streams[i].fd == NULL) + continue; + if (security_stream_auth(streams[i].fd) < 0) { + errstr = newvstrallocf(errstr, + _("[could not authenticate %s stream: %s]"), + streams[i].name, + security_stream_geterror(streams[i].fd)); + goto connect_error; } } + /* + * The MESGFD and DATAFD streams are mandatory. If we didn't get + * them, complain. + */ + if (streams[MESGFD].fd == NULL || streams[DATAFD].fd == NULL) { + errstr = newvstrallocf(errstr, _("[couldn't open MESG or INDEX streams]")); + goto connect_error; + } + /* everything worked */ + *response_error = 0; + return; -#ifdef KRB4_SECURITY - if(krb4_auth && kerberos_handshake(datafd, cred.session) == 0) { - errstr = newstralloc(errstr, - "mutual authentication in data stream failed"); - aclose(datafd); - aclose(mesgfd); - if (indexfd != -1) - aclose(indexfd); - response_error = 1; - return; - } - if(krb4_auth && kerberos_handshake(mesgfd, cred.session) == 0) { - errstr = newstralloc(errstr, - "mutual authentication in mesg stream failed"); - aclose(datafd); - if (indexfd != -1) - aclose(indexfd); - aclose(mesgfd); - response_error = 1; - return; - } -#endif - response_error = 0; +parse_error: + errstr = newvstrallocf(errstr, + _("[parse of reply message failed: %s]"), + extra ? extra : _("(no additional information)")); + amfree(extra); + *response_error = 2; return; + +connect_error: + stop_dump(); + *response_error = 1; } -int startup_dump(hostname, disk, device, level, dumpdate, progname, options) -char *hostname, *disk, *device, *dumpdate, *progname, *options; -int level; +static char * +dumper_get_security_conf( + char * string, + void * arg) +{ + (void)arg; /* Quiet unused parameter warning */ + + if(!string || !*string) + return(NULL); + + if(strcmp(string, "krb5principal")==0) { + return(getconf_str(CNF_KRB5PRINCIPAL)); + } else if(strcmp(string, "krb5keytab")==0) { + return(getconf_str(CNF_KRB5KEYTAB)); + } else if(strcmp(string, "amandad_path")==0) { + return (amandad_path); + } else if(strcmp(string, "client_username")==0) { + return (client_username); + } else if(strcmp(string, "client_port")==0) { + return (client_port); + } else if(strcmp(string, "ssh_keys")==0) { + return (ssh_keys); + } else if(strcmp(string, "kencrypt")==0) { + if (dumper_kencrypt == KENCRYPT_YES) + return ("yes"); + else + return (NULL); + } + return(NULL); +} + +static int +startup_dump( + const char *hostname, + const char *disk, + const char *device, + int level, + const char *dumpdate, + const char *progname, + const char *amandad_path, + const char *client_username, + const char *client_port, + const char *ssh_keys, + const char *auth, + const char *options) { char level_string[NUM_STR_SIZE]; char *req = NULL; - int rc; - - int has_features = am_has_feature(their_features, fe_req_options_features); - int has_hostname = am_has_feature(their_features, fe_req_options_hostname); - int has_device = am_has_feature(their_features, fe_sendbackup_req_device); - - ap_snprintf(level_string, sizeof(level_string), "%d", level); + char *authopt; + int response_error; + const security_driver_t *secdrv; + char *application_api; + int has_features; + int has_hostname; + int has_device; + int has_config; + + (void)disk; /* Quiet unused parameter warning */ + (void)amandad_path; /* Quiet unused parameter warning */ + (void)client_username; /* Quiet unused parameter warning */ + (void)client_port; /* Quiet unused parameter warning */ + (void)ssh_keys; /* Quiet unused parameter warning */ + (void)auth; /* Quiet unused parameter warning */ + + has_features = am_has_feature(their_features, fe_req_options_features); + has_hostname = am_has_feature(their_features, fe_req_options_hostname); + has_config = am_has_feature(their_features, fe_req_options_config); + has_device = am_has_feature(their_features, fe_sendbackup_req_device); + + /* + * Default to bsd authentication if none specified. This is gross. + * + * Options really need to be pre-parsed into some sort of structure + * much earlier, and then flattened out again before transmission. + */ + + g_snprintf(level_string, SIZEOF(level_string), "%d", level); + if(strcmp(progname, "DUMP") == 0 + || strcmp(progname, "GNUTAR") == 0) { + application_api = ""; + } else { + application_api = "BACKUP "; + } req = vstralloc("SERVICE sendbackup\n", "OPTIONS ", has_features ? "features=" : "", @@ -1797,75 +2269,68 @@ int level; has_hostname ? "hostname=" : "", has_hostname ? hostname : "", has_hostname ? ";" : "", - "\n", - progname, - " ", disk, - " ", device && has_device ? device : "", - " ", level_string, - " ", dumpdate, - " ", "OPTIONS ", options, + has_config ? "config=" : "", + has_config ? get_config_name() : "", + has_config ? ";" : "", "\n", NULL); - datafd = mesgfd = indexfd = -1; - -#ifdef KRB4_SECURITY - if(krb4_auth) { - char rc_str[NUM_STR_SIZE]; - - rc = make_krb_request(hostname, kamanda_port, req, NULL, - STARTUP_TIMEOUT, sendbackup_response); - if(!rc) { - char inst[256], realm[256]; -#define HOSTNAME_INSTANCE inst - /* - * This repeats a lot of work with make_krb_request, but it's - * ultimately the kerberos library's fault: krb_mk_req calls - * krb_get_cred, but doesn't make the session key available! - * XXX: But admittedly, we could restructure a bit here and - * at least eliminate the duplicate gethostbyname(). - */ - if(host2krbname(hostname, inst, realm) == 0) - rc = -1; - else - rc = krb_get_cred(CLIENT_HOST_PRINCIPLE, CLIENT_HOST_INSTANCE, - realm, &cred); - if(rc > 0 ) { - ap_snprintf(rc_str, sizeof(rc_str), "%d", rc); - errstr = newvstralloc(errstr, - "[host ", hostname, - ": krb4 error (krb_get_cred) ", - rc_str, - ": ", krb_err_txt[rc], - NULL); - amfree(req); - return 2; - } + amfree(dle_str); + if (am_has_feature(their_features, fe_req_xml)) { + char *p = NULL; + char *pclean; + vstrextend(&p, "\n", NULL); + if (*application_api != '\0') { + vstrextend(&p, " APPLICATION\n", NULL); + } else { + vstrextend(&p, " ", progname, "\n", NULL); } - if(rc > 0) { - ap_snprintf(rc_str, sizeof(rc_str), "%d", rc); - errstr = newvstralloc(errstr, - "[host ", hostname, - ": krb4 error (make_krb_req) ", - rc_str, - ": ", krb_err_txt[rc], - NULL); - amfree(req); - return 2; + vstrextend(&p, " ", b64disk, "\n", NULL); + if (device && has_device) { + vstrextend(&p, " ", b64device, "\n", + NULL); } - } else -#endif - rc = make_request(hostname, amanda_port, req, NULL, - STARTUP_TIMEOUT, sendbackup_response); - - req = NULL; /* do not own this any more */ + vstrextend(&p, " ", level_string, "\n", NULL); + vstrextend(&p, options+1, "\n", NULL); + pclean = clean_dle_str_for_client(p); + vstrextend(&req, pclean, NULL); + amfree(pclean); + dle_str = p; + } else if (*application_api != '\0') { + errstr = newvstrallocf(errstr, + _("[does not support application-api]")); + amfree(req); + return 2; + } else { + authopt = strstr(options, "auth="); + if (auth == NULL) { + auth = "BSD"; + } + vstrextend(&req, + progname, + " ", qdiskname, + " ", device && has_device ? device : "", + " ", level_string, + " ", dumpdate, + " OPTIONS ", options, + "\n", + NULL); + } - if(rc) { - errstr = newvstralloc(errstr, - "[could not resolve name \"", hostname, "\"]", - NULL); + dbprintf(_("send request:\n----\n%s\n----\n\n"), req); + secdrv = security_getdriver(auth); + if (secdrv == NULL) { + errstr = newvstrallocf(errstr, + _("[could not find security driver '%s']"), auth); + amfree(req); return 2; } - run_protocol(); - return response_error; + + protocol_sendreq(hostname, secdrv, dumper_get_security_conf, req, + STARTUP_TIMEOUT, sendbackup_response, &response_error); + + amfree(req); + + protocol_run(); + return (response_error); }