struct async * getaddrinfo_async(const char *hostname, const char *servname, const struct addrinfo *hints, struct asr *asr) { struct asr_ctx *ac; struct async *as; ac = asr_use_resolver(asr); if ((as = async_new(ac, ASR_GETADDRINFO)) == NULL) goto abort; /* errno set */ as->as_run = getaddrinfo_async_run; if (hostname && (as->as.ai.hostname = strdup(hostname)) == NULL) goto abort; /* errno set */ if (servname && (as->as.ai.servname = strdup(servname)) == NULL) goto abort; /* errno set */ if (hints) memmove(&as->as.ai.hints, hints, sizeof *hints); else { memset(&as->as.ai.hints, 0, sizeof as->as.ai.hints); as->as.ai.hints.ai_family = PF_UNSPEC; } asr_ctx_unref(ac); return (as); abort: if (as) async_free(as); asr_ctx_unref(ac); return (NULL); }
struct async * gethostbyname2_async(const char *name, int af, struct asr *asr) { struct asr_ctx *ac; struct async *as; /* the original segfaults */ if (name == NULL) { errno = EINVAL; return (NULL); } ac = asr_use_resolver(asr); if ((as = async_new(ac, ASR_GETHOSTBYNAME)) == NULL) goto abort; /* errno set */ as->as_run = gethostnamadr_async_run; as->as.hostnamadr.family = af; if (af == AF_INET) as->as.hostnamadr.addrlen = INADDRSZ; else if (af == AF_INET6) as->as.hostnamadr.addrlen = IN6ADDRSZ; as->as.hostnamadr.name = strdup(name); if (as->as.hostnamadr.name == NULL) goto abort; /* errno set */ asr_ctx_unref(ac); return (as); abort: if (as) async_free(as); asr_ctx_unref(ac); return (NULL); }
static void tear_down(struct async **as, struct sdirs **sdirs, struct conf ***confs) { async_free(as); sdirs_free(sdirs); confs_free(confs); fail_unless(!recursive_delete(BASE)); //printf("%d %d\n", alloc_count, free_count); alloc_check(); }
static void tear_down(struct async **as, struct asfd **asfd, struct sdirs **sdirs, struct conf ***confs) { async_free(as); asfd_free(asfd); sdirs_free(sdirs); asfd_mock_teardown(&reads, &writes); confs_free(confs); //printf("%d %d\n", alloc_count, free_count); alloc_check(); }
void async_asfd_free_all(struct async **as) { struct asfd *a=NULL; struct asfd *asfd=NULL; if(!as || !*as) return; for(asfd=(*as)->asfd; asfd; asfd=a) { a=asfd->next; asfd_free(&asfd); } async_free(as); }
static void do_snapshot_test( const char *orig_client, int expected_ret, void setup_callback( struct asfd *csin, struct asfd *csout, struct asfd *so_asfd ) ) { struct asfd *csin; struct asfd *csout; struct asfd *so_asfd; struct async *as; struct sel *sel; as=setup_async(); csin=asfd_mock_setup(&reads_csin, &writes_csin); csout=asfd_mock_setup(&reads_csout, &writes_csout); so_asfd=asfd_mock_setup(&reads_so, &writes_so); fail_unless((csin->desc=strdup_w("csin", __func__))!=NULL); fail_unless((csout->desc=strdup_w("csout", __func__))!=NULL); fail_unless((so_asfd->desc=strdup_w("so_asfd", __func__))!=NULL); as->asfd_add(as, csin); as->asfd_add(as, csout); as->asfd_add(as, so_asfd); as->read_write=async_rw_simple; as->write=async_write_simple; setup_callback(csin, csout, so_asfd); fail_unless((sel=sel_alloc())!=NULL); fail_unless(!status_client_ncurses_init(ACTION_STATUS_SNAPSHOT)); fail_unless(status_client_ncurses_main_loop( as, so_asfd, sel, orig_client )==expected_ret); sel_free(&sel); asfd_free(&csin); asfd_free(&csout); asfd_free(&so_asfd); asfd_mock_teardown(&reads_csin, &writes_csin); asfd_mock_teardown(&reads_csout, &writes_csout); asfd_mock_teardown(&reads_so, &writes_so); async_free(&as); alloc_check(); }
static void async_complete(void *opaque) { USBHostDevice *s = opaque; AsyncURB *aurb; while (1) { USBPacket *p; int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb); if (r < 0) { if (errno == EAGAIN) return; if (errno == ENODEV && !s->closing) { printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr); usb_device_del_addr(0, s->dev.addr); return; } dprintf("husb: async. reap urb failed errno %d\n", errno); return; } p = aurb->packet; dprintf("husb: async completed. aurb %p status %d alen %d\n", aurb, aurb->urb.status, aurb->urb.actual_length); if (p) { switch (aurb->urb.status) { case 0: p->len = aurb->urb.actual_length; if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) async_complete_ctrl(s, p); break; case -EPIPE: set_halt(s, p->devep); /* fall through */ default: p->len = USB_RET_NAK; break; } usb_packet_complete(p); } async_free(aurb); } }
struct async * gethostbyaddr_async_ctx(const void *addr, socklen_t len, int af, struct asr_ctx *ac) { struct async *as; if ((as = async_new(ac, ASR_GETHOSTBYADDR)) == NULL) goto abort; /* errno set */ as->as_run = gethostnamadr_async_run; as->as.hostnamadr.family = af; as->as.hostnamadr.addrlen = len; if (len > 0) memmove(as->as.hostnamadr.addr, addr, (len > 16) ? 16 : len); return (as); abort: if (as) async_free(as); return (NULL); }
static void do_simple_test( const char *orig_client, int expected_ret, void setup_callback(struct asfd *asfd), void setup_stdout_callback(struct asfd *asfd) ) { struct asfd *asfd; struct asfd *so_asfd; struct async *as; as=setup_async(); asfd=asfd_mock_setup(&reads, &writes); so_asfd=asfd_mock_setup(&reads_so, &writes_so); as->asfd_add(as, asfd); as->asfd_add(as, so_asfd); asfd->as=as; as->read_write=async_rw_simple; as->write=async_write_simple; setup_callback(asfd); if(setup_stdout_callback) setup_stdout_callback(so_asfd); fail_unless(!status_client_ncurses_init(ACTION_STATUS_SNAPSHOT)); fail_unless(status_client_ncurses_main_loop( as, so_asfd, orig_client )==expected_ret); asfd_free(&asfd); asfd_free(&so_asfd); asfd_mock_teardown(&reads, &writes); asfd_mock_teardown(&reads_so, &writes_so); async_free(&as); alloc_check(); }
END_TEST static void do_status_test( const char *orig_client, int expected_ret, void setup_callback( struct asfd *csin, struct asfd *csout, struct asfd *nin, struct asfd *so_asfd ), void check_sel_callback( struct sel *sel ) ) { struct asfd *csin; struct asfd *csout; struct asfd *nin; struct asfd *so_asfd; struct async *as; struct sel *sel; as=setup_async(); csin=asfd_mock_setup(&reads_csin, &writes_csin); csout=asfd_mock_setup(&reads_csout, &writes_csout); nin=asfd_mock_setup(&reads_nin, &writes_nin); so_asfd=asfd_mock_setup(&reads_so, &writes_so); fail_unless((csin->desc=strdup_w("csin", __func__))!=NULL); fail_unless((csout->desc=strdup_w("csout", __func__))!=NULL); fail_unless((nin->desc=strdup_w("nin", __func__))!=NULL); fail_unless((so_asfd->desc=strdup_w("so_asfd", __func__))!=NULL); nin->streamtype=ASFD_STREAM_NCURSES_STDIN; as->asfd_add(as, csin); as->asfd_add(as, csout); as->asfd_add(as, nin); as->asfd_add(as, so_asfd); as->read_write=async_rw_both; as->write=async_write_simple; setup_callback(csin, csout, nin, so_asfd); fail_unless((sel=sel_alloc())!=NULL); fail_unless(!status_client_ncurses_init(ACTION_STATUS)); fail_unless(status_client_ncurses_main_loop( as, so_asfd, sel, orig_client )==expected_ret); if(check_sel_callback) check_sel_callback(sel); sel_free(&sel); asfd_free(&csin); asfd_free(&csout); asfd_free(&nin); asfd_free(&so_asfd); asfd_mock_teardown(&reads_csin, &writes_csin); asfd_mock_teardown(&reads_csout, &writes_csout); asfd_mock_teardown(&reads_nin, &writes_nin); asfd_mock_teardown(&reads_so, &writes_so); async_free(&as); alloc_check(); }
int champ_chooser_server(struct sdirs *sdirs, struct conf **confs, int resume) { int s; int ret=-1; int len; struct asfd *asfd=NULL; struct sockaddr_un local; struct lock *lock=NULL; struct async *as=NULL; int started=0; struct scores *scores=NULL; const char *directory=get_string(confs[OPT_DIRECTORY]); if(!(lock=lock_alloc_and_init(sdirs->champlock)) || build_path_w(sdirs->champlock)) goto end; lock_get(lock); switch(lock->status) { case GET_LOCK_GOT: log_fzp_set(sdirs->champlog, confs); logp("Got champ lock for dedup_group: %s\n", get_string(confs[OPT_DEDUP_GROUP])); break; case GET_LOCK_NOT_GOT: case GET_LOCK_ERROR: default: //logp("Did not get champ lock\n"); goto end; } if((s=socket(AF_UNIX, SOCK_STREAM, 0))<0) { logp("socket error in %s: %s\n", __func__, strerror(errno)); goto end; } memset(&local, 0, sizeof(struct sockaddr_un)); local.sun_family=AF_UNIX; snprintf(local.sun_path, sizeof(local.sun_path), "%s", sdirs->champsock); len=strlen(local.sun_path)+sizeof(local.sun_family)+1; unlink(sdirs->champsock); if(bind(s, (struct sockaddr *)&local, len)<0) { logp("bind error in %s: %s\n", __func__, strerror(errno)); goto end; } if(listen(s, 5)<0) { logp("listen error in %s: %s\n", __func__, strerror(errno)); goto end; } if(!(as=async_alloc()) || as->init(as, 0) || !(asfd=setup_asfd(as, "champ chooser main socket", &s, /*listen*/""))) goto end; asfd->fdtype=ASFD_FD_SERVER_LISTEN_MAIN; // I think that this is probably the best point at which to run a // cleanup job to delete unused data files, because no other process // can fiddle with the dedup_group at this point. // Cannot do it on a resume, or it will delete files that are // referenced in the backup we are resuming. if(delete_unused_data_files(sdirs, resume)) goto end; // Load the sparse indexes for this dedup group. if(!(scores=champ_chooser_init(sdirs->data))) goto end; while(1) { for(asfd=as->asfd->next; asfd; asfd=asfd->next) { if(!asfd->blist->head || asfd->blist->head->got==BLK_INCOMING) continue; if(results_to_fd(asfd)) goto end; } int removed; switch(as->read_write(as)) { case 0: // Check the main socket last, as it might add // a new client to the list. for(asfd=as->asfd->next; asfd; asfd=asfd->next) { while(asfd->rbuf->buf) { if(deal_with_client_rbuf(asfd, directory, scores)) goto end; // Get as much out of the // readbuf as possible. if(asfd->parse_readbuf(asfd)) goto end; } } if(as->asfd->new_client) { // Incoming client. as->asfd->new_client=0; if(champ_chooser_new_client(as, confs)) goto end; started=1; } break; default: removed=0; // Maybe one of the fds had a problem. // Find and remove it and carry on if possible. for(asfd=as->asfd->next; asfd; ) { struct asfd *a; if(!asfd->want_to_remove) { asfd=asfd->next; continue; } as->asfd_remove(as, asfd); logp("%s: disconnected fd %d\n", asfd->desc, asfd->fd); a=asfd->next; asfd_free(&asfd); asfd=a; removed++; } if(removed) break; // If we got here, there was no fd to remove. // It is a fatal error. goto end; } if(started && !as->asfd->next) { logp("All clients disconnected.\n"); ret=0; break; } } end: logp("champ chooser exiting: %d\n", ret); champ_chooser_free(&scores); log_fzp_set(NULL, confs); async_free(&as); asfd_free(&asfd); // This closes s for us. close_fd(&s); unlink(sdirs->champsock); // FIX THIS: free asfds. lock_release(lock); lock_free(&lock); return ret; }
static enum cliret do_client(struct conf **confs, enum action action, int vss_restore) { enum cliret ret=CLIENT_OK; int rfd=-1; SSL *ssl=NULL; SSL_CTX *ctx=NULL; struct cntr *cntr=NULL; char *incexc=NULL; enum action act=action; struct async *as=NULL; struct asfd *asfd=NULL; // as->settimers(0, 100); // logp("begin client\n"); // logp("action %d\n", action); // Status monitor forks a child process instead of connecting to // the server directly. if(action==ACTION_STATUS || action==ACTION_STATUS_SNAPSHOT) { #ifdef HAVE_WIN32 logp("Status mode not implemented on Windows.\n"); goto error; #endif if(status_client_ncurses_init(act) || status_client_ncurses(confs)) ret=CLIENT_ERROR; goto end; } if(!(cntr=cntr_alloc()) || cntr_init(cntr, get_string(confs[OPT_CNAME]))) goto error; set_cntr(confs[OPT_CNTR], cntr); if(act!=ACTION_ESTIMATE && ssl_setup(&rfd, &ssl, &ctx, action, confs)) goto could_not_connect; if(!(as=async_alloc()) || !(asfd=asfd_alloc()) || as->init(as, act==ACTION_ESTIMATE) || asfd->init(asfd, "main socket", as, rfd, ssl, ASFD_STREAM_STANDARD, confs)) goto end; as->asfd_add(as, asfd); // Set quality of service bits on backup packets. if(act==ACTION_BACKUP || act==ACTION_BACKUP_TIMED || act==ACTION_TIMER_CHECK) as->asfd->set_bulk_packets(as->asfd); if(act!=ACTION_ESTIMATE) { if((ret=initial_comms(as, &act, &incexc, confs))) goto end; } rfd=-1; switch(act) { case ACTION_BACKUP: ret=backup_wrapper(asfd, act, "backupphase1", incexc, confs); break; case ACTION_BACKUP_TIMED: ret=backup_wrapper(asfd, act, "backupphase1timed", incexc, confs); break; case ACTION_TIMER_CHECK: ret=backup_wrapper(asfd, act, "backupphase1timedcheck", incexc, confs); break; case ACTION_RESTORE: case ACTION_VERIFY: ret=restore_wrapper(asfd, act, vss_restore, confs); break; case ACTION_ESTIMATE: if(do_backup_client(asfd, confs, act, 0)) goto error; break; case ACTION_DELETE: if(do_delete_client(asfd, confs)) goto error; break; case ACTION_MONITOR: if(do_monitor_client(asfd, confs)) goto error; break; case ACTION_DIFF: case ACTION_DIFF_LONG: /* if(!strcmp(get_string(confs[OPT_BACKUP2]), "n")) // Do a phase1 scan and diff that. ret=backup_wrapper(asfd, act, "backupphase1diff", incexc, confs); else */ // Diff two backups that already exist. // Fall through, the list code is all we need // for simple diffs on the client side. case ACTION_LIST: case ACTION_LIST_LONG: default: if(do_list_client(asfd, act, confs)) goto error; break; } if(asfd_flush_asio(asfd)) ret=CLIENT_ERROR; goto end; error: ret=CLIENT_ERROR; goto end; could_not_connect: ret=CLIENT_COULD_NOT_CONNECT; end: close_fd(&rfd); async_free(&as); asfd_free(&asfd); if(ctx) ssl_destroy_ctx(ctx); free_w(&incexc); set_cntr(confs[OPT_CNTR], NULL); cntr_free(&cntr); //logp("end client\n"); return ret; }
static void tear_down(struct async **as) { async_free(as); alloc_check(); }
// Return -1 on error or success, 0 to continue normally. int autoupgrade_server(long ser_ver, long cli_ver, const char *os, struct config *conf, struct cntr *p1cntr) { int ret=-1; char *path=NULL; char *base_path=NULL; char *script_path=NULL; char *package_path=NULL; char *script_path_top=NULL; char *script_path_specific=NULL; struct stat stats; struct stat statp; if(!conf->autoupgrade_dir) { // Autoupgrades not turned on on the server. ret=0; async_write_str(CMD_GEN, "do not autoupgrade"); goto end; } if(cli_ver>=ser_ver) { // No need to upgrade - client is same version as server, // or newer. ret=0; async_write_str(CMD_GEN, "do not autoupgrade"); goto end; } if(!(base_path=prepend_s(conf->autoupgrade_dir, os, strlen(os))) || !(path=prepend_s(base_path, VERSION, strlen(VERSION))) || !(script_path_top=prepend_s(base_path, "script", strlen("script"))) || !(script_path_specific=prepend_s(path, "script", strlen("script"))) || !(package_path=prepend_s(path, "package", strlen("package")))) { async_write_str(CMD_GEN, "do not autoupgrade"); goto end; } if(!stat(script_path_specific, &stats)) script_path=script_path_specific; else if(!stat(script_path_top, &stats)) script_path=script_path_top; else { logp("Want to autoupgrade client, but no file at:\n"); logp("%s\n", script_path_top); logp("or:\n"); logp("%s\n", script_path_specific); ret=0; // this is probably OK async_write_str(CMD_GEN, "do not autoupgrade"); goto end; } if(stat(package_path, &statp)) { logp("Want to autoupgrade client, but no file available at:\n"); logp("%s\n", package_path); ret=0; // this is probably OK async_write_str(CMD_GEN, "do not autoupgrade"); goto end; } if(!S_ISREG(stats.st_mode)) { logp("%s is not a regular file\n", script_path); async_write_str(CMD_GEN, "do not autoupgrade"); goto end; } if(!S_ISREG(statp.st_mode)) { logp("%s is not a regular file\n", package_path); async_write_str(CMD_GEN, "do not autoupgrade"); goto end; } if(async_write_str(CMD_GEN, "autoupgrade ok")) goto end; if(send_a_file(script_path, p1cntr)) { logp("Problem sending %s\n", script_path); goto end; } if(send_a_file(package_path, p1cntr)) { logp("Problem sending %s\n", package_path); goto end; } ret=0; /* Clients currently exit after forking, so exit ourselves. */ logp("Expecting client to upgrade - now exiting\n"); async_free(); exit(0); end: if(path) free(path); if(base_path) free(base_path); if(script_path_specific) free(script_path_specific); if(script_path_top) free(script_path_top); if(package_path) free(package_path); return ret; }
static int usb_host_handle_control(USBHostDevice *s, USBPacket *p) { struct usbdevfs_urb *urb; AsyncURB *aurb; int ret, value, index; /* * Process certain standard device requests. * These are infrequent and are processed synchronously. */ value = le16_to_cpu(s->ctrl.req.wValue); index = le16_to_cpu(s->ctrl.req.wIndex); dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n", s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index, s->ctrl.len); if (s->ctrl.req.bRequestType == 0) { switch (s->ctrl.req.bRequest) { case USB_REQ_SET_ADDRESS: return usb_host_set_address(s, value); case USB_REQ_SET_CONFIGURATION: return usb_host_set_config(s, value & 0xff); } } if (s->ctrl.req.bRequestType == 1 && s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE) return usb_host_set_interface(s, index, value); /* The rest are asynchronous */ aurb = async_alloc(); if (!aurb) { dprintf("husb: async malloc failed\n"); return USB_RET_NAK; } aurb->hdev = s; aurb->packet = p; /* * Setup ctrl transfer. * * s->ctrl is layed out such that data buffer immediately follows * 'req' struct which is exactly what usbdevfs expects. */ urb = &aurb->urb; urb->type = USBDEVFS_URB_TYPE_CONTROL; urb->endpoint = p->devep; urb->buffer = &s->ctrl.req; urb->buffer_length = 8 + s->ctrl.len; urb->usercontext = s; ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb); if (ret < 0) { dprintf("husb: submit failed. errno %d\n", errno); async_free(aurb); switch(errno) { case ETIMEDOUT: return USB_RET_NAK; case EPIPE: default: return USB_RET_STALL; } } usb_defer_packet(p, async_cancel, aurb); return USB_RET_ASYNC; }
static int usb_host_handle_data(USBHostDevice *s, USBPacket *p) { struct usbdevfs_urb *urb; AsyncURB *aurb; int ret; aurb = async_alloc(); if (!aurb) { dprintf("husb: async malloc failed\n"); return USB_RET_NAK; } aurb->hdev = s; aurb->packet = p; urb = &aurb->urb; if (p->pid == USB_TOKEN_IN) urb->endpoint = p->devep | 0x80; else urb->endpoint = p->devep; if (is_halted(s, p->devep)) { ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint); if (ret < 0) { dprintf("husb: failed to clear halt. ep 0x%x errno %d\n", urb->endpoint, errno); return USB_RET_NAK; } clear_halt(s, p->devep); } urb->buffer = p->data; urb->buffer_length = p->len; if (is_isoc(s, p->devep)) { /* Setup ISOC transfer */ urb->type = USBDEVFS_URB_TYPE_ISO; urb->flags = USBDEVFS_URB_ISO_ASAP; urb->number_of_packets = 1; urb->iso_frame_desc[0].length = p->len; } else { /* Setup bulk transfer */ urb->type = USBDEVFS_URB_TYPE_BULK; } urb->usercontext = s; ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb); if (ret < 0) { dprintf("husb: submit failed. errno %d\n", errno); async_free(aurb); switch(errno) { case ETIMEDOUT: return USB_RET_NAK; case EPIPE: default: return USB_RET_STALL; } } usb_defer_packet(p, async_cancel, aurb); return USB_RET_ASYNC; }
static enum cliret do_client(struct conf *conf, enum action action, int vss_restore, int json) { enum cliret ret=CLIENT_OK; int rfd=-1; int resume=0; SSL *ssl=NULL; SSL_CTX *ctx=NULL; struct cntr *cntr=NULL; char *incexc=NULL; long name_max=0; enum action act=action; struct async *as=NULL; struct asfd *asfd=NULL; // as->settimers(0, 100); logp("begin client\n"); if(!(cntr=cntr_alloc()) || cntr_init(cntr, conf->cname)) goto error; conf->cntr=cntr; if(act!=ACTION_ESTIMATE && ssl_setup(&rfd, &ssl, &ctx, conf)) goto error; if(!(as=async_alloc()) || !(asfd=asfd_alloc()) || as->init(as, act==ACTION_ESTIMATE) || asfd->init(asfd, as, rfd, ssl, conf)) goto end; as->add_asfd(as, asfd); // Set quality of service bits on backup packets. if(act==ACTION_BACKUP || act==ACTION_BACKUP_TIMED || act==ACTION_TIMER_CHECK) as->asfd->set_bulk_packets(as->asfd); if(act!=ACTION_ESTIMATE) { if((ret=initial_comms(as, &act, &incexc, &name_max, conf))) goto end; } rfd=-1; switch(act) { case ACTION_BACKUP: ret=backup_wrapper(asfd, act, "backupphase1", incexc, resume, name_max, conf); break; case ACTION_BACKUP_TIMED: ret=backup_wrapper(asfd, act, "backupphase1timed", incexc, resume, name_max, conf); break; case ACTION_TIMER_CHECK: ret=backup_wrapper(asfd, act, "backupphase1timedcheck", incexc, resume, name_max, conf); break; case ACTION_RESTORE: case ACTION_VERIFY: ret=restore_wrapper(asfd, act, vss_restore, conf); break; case ACTION_ESTIMATE: if(do_backup_client(asfd, conf, act, name_max, 0)) goto error; break; case ACTION_DELETE: if(do_delete_client(asfd, conf)) goto error; break; case ACTION_LIST: case ACTION_LONG_LIST: default: if(do_list_client(asfd, conf, act, json)) goto error; break; } goto end; error: ret=CLIENT_ERROR; end: close_fd(&rfd); async_free(&as); asfd_free(&asfd); if(ctx) ssl_destroy_ctx(ctx); if(incexc) free(incexc); conf->cntr=NULL; if(cntr) cntr_free(&cntr); //logp("end client\n"); return ret; }
/* May return 1 to mean try again. This happens after a successful certificate signing request so that it connects again straight away with the new key/certificate. Returns 2 if there were restore/verify warnings. Returns 3 if timer conditions were not met. */ static int do_client(struct config *conf, enum action act, int vss_restore, int json) { int ret=0; int rfd=-1; int resume=0; SSL *ssl=NULL; BIO *sbio=NULL; char buf[256]=""; SSL_CTX *ctx=NULL; struct cntr cntr; struct cntr p1cntr; char *incexc=NULL; char *server_version=NULL; const char *phase1str="backupphase1"; reset_filecounter(&p1cntr, time(NULL)); reset_filecounter(&cntr, time(NULL)); setup_signals_client(); // settimers(0, 100); logp("begin client\n"); if(act!=ACTION_ESTIMATE) { ssl_load_globals(); if(!(ctx=ssl_initialise_ctx(conf))) { logp("error initialising ssl ctx\n"); ret=-1; goto end; } SSL_CTX_set_session_id_context(ctx, (const unsigned char *)&s_server_session_id_context, sizeof(s_server_session_id_context)); if((rfd=init_client_socket(conf->server, conf->port))<0) { ret=-1; goto end; } if(!(ssl=SSL_new(ctx)) || !(sbio=BIO_new_socket(rfd, BIO_NOCLOSE))) { ERR_error_string_n(ERR_get_error(), buf, sizeof(buf)); logp("Problem joining SSL to the socket: %s\n", buf); ret=-1; goto end; } SSL_set_bio(ssl, sbio, sbio); if(SSL_connect(ssl)<=0) { ERR_error_string_n(ERR_get_error(), buf, sizeof(buf)); logp("SSL connect error: %s\n", buf); ret=-1; goto end; } } if((ret=async_init(rfd, ssl, conf, act==ACTION_ESTIMATE))) goto end; // Set quality of service bits on backup packets. if(act==ACTION_BACKUP || act==ACTION_BACKUP_TIMED) set_bulk_packets(); if(act!=ACTION_ESTIMATE) { char cmd=0; size_t len=0; char *feat=NULL; int ca_ret=0; if((ret=authorise_client(conf, &server_version, &p1cntr))) goto end; if(server_version) { logp("Server version: %s\n", server_version); // Servers before 1.3.2 did not tell us their versions. // 1.3.2 and above can do the automatic CA stuff that // follows. if((ca_ret=ca_client_setup(conf, &p1cntr))<0) { // Error logp("Error with certificate signing request\n"); ret=-1; goto end; } else if(ca_ret>0) { // Certificate signed successfully. // Everything is OK, but we will reconnect now, in // order to use the new keys/certificates. ret=1; goto end; } } set_non_blocking(rfd); if((ret=ssl_check_cert(ssl, conf))) { logp("check cert failed\n"); goto end; } if((ret=async_write_str(CMD_GEN, "extra_comms_begin"))) { logp("Problem requesting extra_comms_begin\n"); goto end; } // Servers greater than 1.3.0 will list the extra_comms // features they support. else if((ret=async_read(&cmd, &feat, &len))) { logp("Problem reading response to extra_comms_begin\n"); goto end; } else if(cmd!=CMD_GEN) { logp("Unexpected command from server when reading response to extra_comms_begin: %c:%s\n", cmd, feat); ret=-1; goto end; } else if(strncmp(feat, "extra_comms_begin ok", strlen("extra_comms_begin ok"))) { logp("Unexpected response from server when reading response to extra_comms_begin: %c:%s\n", cmd, feat); ret=-1; goto end; } // Can add extra bits here. The first extra bit is the // autoupgrade stuff. if(server_supports_autoupgrade(feat)) { if(conf->autoupgrade_dir && conf->autoupgrade_os && (ret=autoupgrade_client(conf, &p1cntr))) goto end; } // :srestore: means that the server wants to do a restore. if(server_supports(feat, ":srestore:")) { if(conf->server_can_restore) { logp("Server is initiating a restore\n"); if(incexc) { free(incexc); incexc=NULL; } if((ret=incexc_recv_client_restore(&incexc, conf, &p1cntr))) goto end; if(incexc) { if((ret=parse_incexcs_buf(conf, incexc))) goto end; act=ACTION_RESTORE; log_restore_settings(conf, 1); } } else { logp("Server wants to initiate a restore\n"); logp("Client configuration says no\n"); if(async_write_str(CMD_GEN, "srestore not ok")) { ret=-1; goto end; } } } if(conf->orig_client) { char str[512]=""; snprintf(str, sizeof(str), "orig_client=%s", conf->orig_client); if(!server_supports(feat, ":orig_client:")) { logp("Server does not support switching client.\n"); ret=-1; goto end; } if((ret=async_write_str(CMD_GEN, str)) || (ret=async_read_expect(CMD_GEN, "orig_client ok"))) { logp("Problem requesting %s\n", str); ret=-1; goto end; } logp("Switched to client %s\n", conf->orig_client); } // :sincexc: is for the server giving the client the // incexc config. if(act==ACTION_BACKUP || act==ACTION_BACKUP_TIMED) { if(!incexc && server_supports(feat, ":sincexc:")) { logp("Server is setting includes/excludes.\n"); if(incexc) { free(incexc); incexc=NULL; } if((ret=incexc_recv_client(&incexc, conf, &p1cntr))) goto end; if(incexc && (ret=parse_incexcs_buf(conf, incexc))) goto end; } } if(server_supports(feat, ":counters:")) { if(async_write_str(CMD_GEN, "countersok")) goto end; conf->send_client_counters=1; } // :incexc: is for the client sending the server the // incexc config so that it better knows what to do on // resume. if(server_supports(feat, ":incexc:") && (ret=incexc_send_client(conf, &p1cntr))) goto end; if((ret=async_write_str(CMD_GEN, "extra_comms_end")) || (ret=async_read_expect(CMD_GEN, "extra_comms_end ok"))) { logp("Problem requesting extra_comms_end\n"); goto end; } if(feat) free(feat); } rfd=-1; switch(act) { case ACTION_BACKUP_TIMED: phase1str="backupphase1timed"; case ACTION_BACKUP: { // Set bulk packets quality of service flags on backup. if(incexc) { logp("Server is overriding the configuration\n"); logp("with the following settings:\n"); if(log_incexcs_buf(incexc)) { ret=-1; goto end; } } if(!conf->sdcount) { logp("Found no include paths!\n"); ret=-1; goto end; } if(!(ret=maybe_check_timer(phase1str, conf, &resume))) { if(conf->backup_script_pre) { int a=0; const char *args[12]; args[a++]=conf->backup_script_pre; args[a++]="pre"; args[a++]="reserved2"; args[a++]="reserved3"; args[a++]="reserved4"; args[a++]="reserved5"; args[a++]=NULL; if(run_script(args, conf->backup_script_pre_arg, conf->bprecount, &p1cntr, 1, 1)) ret=-1; } if(!ret && do_backup_client(conf, resume, 0, &p1cntr, &cntr)) ret=-1; if((conf->backup_script_post_run_on_fail || !ret) && conf->backup_script_post) { int a=0; const char *args[12]; args[a++]=conf->backup_script_post; args[a++]="post"; // Tell post script whether the restore // failed. args[a++]=ret?"1":"0"; args[a++]="reserved3"; args[a++]="reserved4"; args[a++]="reserved5"; args[a++]=NULL; if(run_script(args, conf->backup_script_post_arg, conf->bpostcount, &cntr, 1, 1)) ret=-1; } } if(ret<0) logp("error in backup\n"); else if(ret>0) { // Timer script said no. // Have a distinct return value to // differentiate between other cases // (ssl reconnection and restore/verify // warnings). ret=3; } else logp("backup finished ok\n"); break; } case ACTION_RESTORE: case ACTION_VERIFY: { if(conf->restore_script_pre) { int a=0; const char *args[12]; args[a++]=conf->restore_script_pre; args[a++]="pre"; args[a++]="reserved2"; args[a++]="reserved3"; args[a++]="reserved4"; args[a++]="reserved5"; args[a++]=NULL; if(run_script(args, conf->restore_script_pre_arg, conf->rprecount, &cntr, 1, 1)) ret=-1; } if(!ret && do_restore_client(conf, act, vss_restore, &p1cntr, &cntr)) ret=-1; if((conf->restore_script_post_run_on_fail || !ret) && conf->restore_script_post) { int a=0; const char *args[12]; args[a++]=conf->restore_script_pre; args[a++]="post"; // Tell post script whether the restore // failed. args[a++]=ret?"1":"0"; args[a++]="reserved3"; args[a++]="reserved4"; args[a++]="reserved5"; args[a++]=NULL; if(run_script(args, conf->restore_script_post_arg, conf->rpostcount, &cntr, 1, 1)) ret=-1; } // Return non-zero if there were warnings, // so that the test script can easily check. if(p1cntr.warning+cntr.warning) ret=2; break; } case ACTION_ESTIMATE: if(!ret) ret=do_backup_client(conf, 0, 1, &p1cntr, &cntr); break; case ACTION_DELETE: if(!ret) ret=do_delete_client(conf); break; case ACTION_LIST: case ACTION_LONG_LIST: default: ret=do_list_client(conf, act, json); break; } end: close_fd(&rfd); async_free(); if(act!=ACTION_ESTIMATE) ssl_destroy_ctx(ctx); if(incexc) free(incexc); if(server_version) free(server_version); //logp("end client\n"); return ret; }
static int run_child(int *rfd, int *cfd, SSL_CTX *ctx, const char *conffile, int forking) { int ret=-1; int ca_ret=0; SSL *ssl=NULL; BIO *sbio=NULL; struct conf *conf=NULL; struct conf *cconf=NULL; struct cntr *cntr=NULL; struct async *as=NULL; struct asfd *asfd=NULL; if(!(conf=conf_alloc()) || !(cconf=conf_alloc())) goto end; if(forking) close_fd(rfd); // Reload global config, in case things have changed. This means that // the server does not need to be restarted for most conf changes. conf_init(conf); conf_init(cconf); if(conf_load(conffile, conf, 1)) goto end; // Hack to keep forking turned off if it was specified as off on the // command line. if(!forking) conf->forking=0; if(!(sbio=BIO_new_socket(*cfd, BIO_NOCLOSE)) || !(ssl=SSL_new(ctx))) { logp("There was a problem joining ssl to the socket\n"); goto end; } SSL_set_bio(ssl, sbio, sbio); /* Do not try to check peer certificate straight away. Clients can send a certificate signing request when they have no certificate. */ SSL_set_verify(ssl, SSL_VERIFY_PEER /* | SSL_VERIFY_FAIL_IF_NO_PEER_CERT */, 0); if(SSL_accept(ssl)<=0) { char buf[256]=""; ERR_error_string_n(ERR_get_error(), buf, sizeof(buf)); logp("SSL_accept: %s\n", buf); goto end; } if(!(as=async_alloc()) || !(asfd=asfd_alloc()) || as->init(as, 0) || asfd->init(asfd, "main socket", as, *cfd, ssl, conf)) goto end; as->asfd_add(as, asfd); if(authorise_server(asfd, conf, cconf) || !cconf->cname || !*(cconf->cname)) { // Add an annoying delay in case they are tempted to // try repeatedly. log_and_send(asfd, "unable to authorise on server"); sleep(1); goto end; } // Set up counters. Have to wait until here to get cname. if(!(cntr=cntr_alloc()) || cntr_init(cntr, cconf->cname)) goto end; conf->cntr=cntr; cconf->cntr=cntr; /* At this point, the client might want to get a new certificate signed. Clients on 1.3.2 or newer can do this. */ if((ca_ret=ca_server_maybe_sign_client_cert(asfd, conf, cconf))<0) { logp("Error signing client certificate request for %s\n", cconf->cname); goto end; } else if(ca_ret>0) { // Certificate signed and sent back. // Everything is OK, but we will close this instance // so that the client can start again with a new // connection and its new certificates. logp("Signed and returned client certificate request for %s\n", cconf->cname); ret=0; goto end; } /* Now it is time to check the certificate. */ if(ssl_check_cert(ssl, cconf)) { log_and_send(asfd, "check cert failed on server"); goto end; } set_non_blocking(*cfd); ret=child(as, conf, cconf); end: *cfd=-1; async_free(&as); asfd_free(&asfd); // This closes cfd for us. logp("exit child\n"); if(cntr) cntr_free(&cntr); if(conf) conf_free(conf); if(cconf) conf_free(cconf); return ret; }