/***************************************************************************** 初期化 *****************************************************************************/ bool CGainer::Init(int portNum, int mode) { if ( m_inited ) { return true; } m_led = false; m_config = mode; m_port = portNum; m_endFlag = false; m_hwnd_sstp = NULL; m_analog_period = 0; m_analog_last = 0; m_digital_period = 0; m_digital_last = 0; ZeroMemory(m_LEDMatrixOutputs,sizeof(m_LEDMatrixOutputs)); m_receive_buffer.assign(RECV_BUFFER,0); InitializeCriticalSection(&m_receive_queue_lock); m_receive_queue_semaphore = ::CreateSemaphore(NULL,0,9999,NULL); // COM port open if ( ! m_serial.Open(m_port, RECV_BUFFER, SEND_BUFFER) ) { return false; } // setting of COM port if ( ! m_serial.SetConfig(COMLIBFLAG_38400|COMLIBFLAG_NO|COMLIBFLAG_1|COMLIBFLAG_8|COMLIBFLAG_NO_FLOW_CONTROL) ) { m_serial.Close(); return false; } // setting receiving timeout m_serial.SetTimeout(RECV_TIMEOUT_COMM, RECV_TIMEOUT_INTERVAL, SEND_TIMEOUT_COMM); // software reset Reboot(true); m_thread_handle = (HANDLE)_beginthreadex(NULL, 0, receiver, this, 0, NULL); wait_recv(); // ver. m_version_string = command_send("?*"); m_version_string.erase(0,1); m_version_string.erase(m_version_string.size()-1,1); // set configulation mode if ( m_config ) { SetConfiguration(m_config); } m_inited = true; g_gainer_open_ports.push_back(m_port); return true; }
/***************************************************************************** コマンド送信 *****************************************************************************/ std::string CGainer::command_send(const std::string &cmd,bool nowait) { #ifdef _DEBUG std::cout << "command_send : " << cmd.c_str() << std::endl << std::flush; #endif m_serial.Send(cmd.c_str()); if ( nowait ) { return ""; } else { return wait_recv(); } }
/* * TODO: * - strptime() is most likely not portable enough */ int submitstats(const char *clamdcfg, const struct optstruct *opts) { int fd, sd, bread, lread = 0, cnt, ret; char post[SUBMIT_MIN_ENTRIES * 256 + 512]; char query[SUBMIT_MIN_ENTRIES * 256]; char buff[512], statsdat[512], newstatsdat[512], uastr[128]; char logfile[256], fbuff[FILEBUFF]; char *pt, *pt2, *auth = NULL; const char *line, *country = NULL, *user, *proxy = NULL; struct optstruct *clamdopt; const struct optstruct *opt; struct stat sb; struct tm tms; time_t epoch; unsigned int qcnt, entries, submitted = 0, permfail = 0, port = 0; if((opt = optget(opts, "DetectionStatsCountry"))->enabled) { if(strlen(opt->strarg) != 2 || !isalpha(opt->strarg[0]) || !isalpha(opt->strarg[1])) { logg("!SubmitDetectionStats: DetectionStatsCountry requires a two-letter country code\n"); return 56; } country = opt->strarg; } if(!(clamdopt = optparse(clamdcfg, 0, NULL, 1, OPT_CLAMD, 0, NULL))) { logg("!SubmitDetectionStats: Can't open or parse configuration file %s\n", clamdcfg); return 56; } if(!(opt = optget(clamdopt, "LogFile"))->enabled) { logg("!SubmitDetectionStats: LogFile needs to be enabled in %s\n", clamdcfg); optfree(clamdopt); return 56; } strncpy(logfile, opt->strarg, sizeof(logfile)); logfile[sizeof(logfile) - 1] = 0; if(!optget(clamdopt, "LogTime")->enabled) { logg("!SubmitDetectionStats: LogTime needs to be enabled in %s\n", clamdcfg); optfree(clamdopt); return 56; } optfree(clamdopt); if((fd = open("stats.dat", O_RDONLY)) != -1) { if((bread = read(fd, statsdat, sizeof(statsdat) - 1)) == -1) { logg("^SubmitDetectionStats: Can't read stats.dat\n"); bread = 0; } statsdat[bread] = 0; close(fd); } else { *statsdat = 0; } if((fd = open(logfile, O_RDONLY)) == -1) { logg("!SubmitDetectionStats: Can't open %s for reading\n", logfile); return 56; } if(fstat(fd, &sb) == -1) { logg("!SubmitDetectionStats: fstat() failed\n"); close(fd); return 56; } while((line = readbline(fd, fbuff, FILEBUFF, sb.st_size, &lread))) if(strlen(line) >= 32 && !strcmp(&line[strlen(line) - 6], " FOUND")) break; if(!line) { logg("SubmitDetectionStats: No detection records found\n"); close(fd); return 1; } if(*statsdat && !strcmp(line, statsdat)) { logg("SubmitDetectionStats: No new detection records found\n"); close(fd); return 1; } else { strncpy(newstatsdat, line, sizeof(newstatsdat)); } if((opt = optget(opts, "HTTPUserAgent"))->enabled) strncpy(uastr, opt->strarg, sizeof(uastr)); else snprintf(uastr, sizeof(uastr), PACKAGE"/%s (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")%s%s", get_version(), country ? ":" : "", country ? country : ""); uastr[sizeof(uastr) - 1] = 0; if((opt = optget(opts, "HTTPProxyServer"))->enabled) { proxy = opt->strarg; if(!strncasecmp(proxy, "http://", 7)) proxy += 7; if((opt = optget(opts, "HTTPProxyUsername"))->enabled) { user = opt->strarg; if(!(opt = optget(opts, "HTTPProxyPassword"))->enabled) { logg("!SubmitDetectionStats: HTTPProxyUsername requires HTTPProxyPassword\n"); close(fd); return 56; } auth = proxyauth(user, opt->strarg); if(!auth) { close(fd); return 56; } } if((opt = optget(opts, "HTTPProxyPort"))->enabled) port = opt->numarg; logg("*Connecting via %s\n", proxy); } ret = 0; memset(query, 0, sizeof(query)); qcnt = 0; entries = 0; do { if(strlen(line) < 32 || strcmp(&line[strlen(line) - 6], " FOUND")) continue; if(*statsdat && !strcmp(line, statsdat)) break; strncpy(buff, line, sizeof(buff)); buff[sizeof(buff) - 1] = 0; if(!(pt = strstr(buff, " -> "))) { logg("*SubmitDetectionStats: Skipping detection entry logged without time\b"); continue; } *pt = 0; pt += 4; if(!strptime(buff, "%a %b %d %H:%M:%S %Y", &tms) || (epoch = mktime(&tms)) == -1) { logg("!SubmitDetectionStats: Failed to convert date string\n"); ret = 1; break; } pt2 = &pt[strlen(pt) - 6]; *pt2 = 0; if(!(pt2 = strrchr(pt, ':'))) { logg("!SubmitDetectionStats: Incorrect format of the log file (1)\n"); ret = 1; break; } *pt2 = 0; pt2 += 2; #ifdef C_WINDOWS if((pt = strrchr(pt, '\\'))) #else if((pt = strrchr(pt, '/'))) #endif *pt++ = 0; if(!pt) pt = (char*) "NOFNAME"; qcnt += snprintf(&query[qcnt], sizeof(query) - qcnt, "ts[]=%u&fname[]=%s&virus[]=%s&", (unsigned int) epoch, pt, pt2); entries++; if(entries == SUBMIT_MIN_ENTRIES) { sd = wwwconnect("stats.clamav.net", proxy, port, NULL, optget(opts, "LocalIPAddress")->strarg, optget(opts, "ConnectTimeout")->numarg, NULL, 0, 0); if(sd == -1) { logg("!SubmitDetectionStats: Can't connect to server\n"); ret = 52; break; } query[sizeof(query) - 1] = 0; snprintf(post, sizeof(post), "POST http://stats.clamav.net/submit.php HTTP/1.0\r\n" "Host: stats.clamav.net\r\n%s" "Content-Type: application/x-www-form-urlencoded\r\n" "User-Agent: %s\r\n" "Content-Length: %u\r\n\n" "%s", auth ? auth : "", uastr, (unsigned int) strlen(query), query); if(send(sd, post, strlen(post), 0) < 0) { logg("!SubmitDetectionStats: Can't write to socket\n"); ret = 52; closesocket(sd); break; } pt = post; cnt = sizeof(post) - 1; #ifdef SO_ERROR while((bread = wait_recv(sd, pt, cnt, 0, optget(opts, "ReceiveTimeout")->numarg)) > 0) { #else while((bread = recv(sd, pt, cnt, 0)) > 0) { #endif pt += bread; cnt -= bread; if(cnt <= 0) break; } *pt = 0; closesocket(sd); if(bread < 0) { logg("!SubmitDetectionStats: Can't read from socket\n"); ret = 52; break; } if(strstr(post, "SUBMIT_OK")) { submitted += entries; if(submitted + SUBMIT_MIN_ENTRIES > SUBMIT_MAX_ENTRIES) break; qcnt = 0; entries = 0; memset(query, 0, sizeof(query)); continue; } ret = 52; if((pt = strstr(post, "SUBMIT_PERMANENT_FAILURE"))) { if(!submitted) { permfail = 1; if((pt + 32 <= post + sizeof(post)) && pt[24] == ':') logg("!SubmitDetectionStats: Remote server reported permanent failure: %s\n", &pt[25]); else logg("!SubmitDetectionStats: Remote server reported permanent failure\n"); } } else if((pt = strstr(post, "SUBMIT_TEMPORARY_FAILURE"))) { if(!submitted) { if((pt + 32 <= post + sizeof(post)) && pt[24] == ':') logg("!SubmitDetectionStats: Remote server reported temporary failure: %s\n", &pt[25]); else logg("!SubmitDetectionStats: Remote server reported temporary failure\n"); } } else { if(!submitted) logg("!SubmitDetectionStats: Incorrect answer from server\n"); } break; } } while((line = readbline(fd, fbuff, FILEBUFF, sb.st_size, &lread))); close(fd); if(auth) free(auth); if(submitted || permfail) { if((fd = open("stats.dat", O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1) { logg("^SubmitDetectionStats: Can't open stats.dat for writing\n"); } else { if((bread = write(fd, newstatsdat, sizeof(newstatsdat))) != sizeof(newstatsdat)) logg("^SubmitDetectionStats: Can't write to stats.dat\n"); close(fd); } } if(ret == 0) { if(!submitted) logg("SubmitDetectionStats: Not enough recent data for submission\n"); else logg("SubmitDetectionStats: Submitted %u records\n", submitted); } return ret; } static int Rfc2822DateTime(char *buf, time_t mtime) { struct tm *gmt; gmt = gmtime(&mtime); return strftime(buf, 36, "%a, %d %b %Y %X GMT", gmt); } static struct cl_cvd *remote_cvdhead(const char *cvdfile, const char *localfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int *ims, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist) { char cmd[512], head[513], buffer[FILEBUFF], ipaddr[46], *ch, *tmp; int bread, cnt, sd; unsigned int i, j; char *remotename = NULL, *authorization = NULL; struct cl_cvd *cvd; char last_modified[36], uastr[128]; if(proxy) { remotename = malloc(strlen(hostname) + 8); if(!remotename) { logg("!remote_cvdhead: Can't allocate memory for 'remotename'\n"); return NULL; } sprintf(remotename, "http://%s", hostname); if(user) { authorization = proxyauth(user, pass); if(!authorization) { free(remotename); return NULL; } } } if(!access(localfile, R_OK)) { cvd = cl_cvdhead(localfile); if(!cvd) { logg("!remote_cvdhead: Can't parse file %s\n", localfile); free(remotename); free(authorization); return NULL; } Rfc2822DateTime(last_modified, (time_t) cvd->stime); cl_cvdfree(cvd); } else { time_t mtime = 1104119530; Rfc2822DateTime(last_modified, mtime); logg("*Assuming modification time in the past\n"); } logg("*If-Modified-Since: %s\n", last_modified); logg("Reading CVD header (%s): ", cvdfile); if(uas) strncpy(uastr, uas, sizeof(uastr)); else snprintf(uastr, sizeof(uastr), PACKAGE"/%s (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")", get_version()); uastr[sizeof(uastr) - 1] = 0; snprintf(cmd, sizeof(cmd), "GET %s/%s HTTP/1.0\r\n" "Host: %s\r\n%s" "User-Agent: %s\r\n" "Connection: close\r\n" "Range: bytes=0-511\r\n" "If-Modified-Since: %s\r\n" "\r\n", (remotename != NULL) ? remotename : "", cvdfile, hostname, (authorization != NULL) ? authorization : "", uastr, last_modified); free(remotename); free(authorization); memset(ipaddr, 0, sizeof(ipaddr)); if(ip[0]) /* use ip to connect */ sd = wwwconnect(ip, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist); else sd = wwwconnect(hostname, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist); if(sd < 0) { return NULL; } else { logg("*Connected to %s (IP: %s).\n", hostname, ipaddr); logg("*Trying to retrieve CVD header of http://%s/%s\n", hostname, cvdfile); } if(!ip[0]) strcpy(ip, ipaddr); if(send(sd, cmd, strlen(cmd), 0) < 0) { logg("%cremote_cvdhead: write failed\n", logerr ? '!' : '^'); closesocket(sd); return NULL; } tmp = buffer; cnt = FILEBUFF; #ifdef SO_ERROR while((bread = wait_recv(sd, tmp, cnt, 0, rtimeout)) > 0) { #else while((bread = recv(sd, tmp, cnt, 0)) > 0) { #endif tmp += bread; cnt -= bread; if(cnt <= 0) break; } closesocket(sd); if(bread == -1) { logg("%cremote_cvdhead: Error while reading CVD header from %s\n", logerr ? '!' : '^', hostname); mirman_update(mdat->currip, mdat->af, mdat, 1); return NULL; } if((strstr(buffer, "HTTP/1.1 404")) != NULL || (strstr(buffer, "HTTP/1.0 404")) != NULL) { logg("%cCVD file not found on remote server\n", logerr ? '!' : '^'); mirman_update(mdat->currip, mdat->af, mdat, 2); return NULL; } /* check whether the resource is up-to-date */ if((strstr(buffer, "HTTP/1.1 304")) != NULL || (strstr(buffer, "HTTP/1.0 304")) != NULL) { *ims = 0; logg("OK (IMS)\n"); mirman_update(mdat->currip, mdat->af, mdat, 0); return NULL; } else { *ims = 1; } if(!strstr(buffer, "HTTP/1.1 200") && !strstr(buffer, "HTTP/1.0 200") && !strstr(buffer, "HTTP/1.1 206") && !strstr(buffer, "HTTP/1.0 206")) { logg("%cUnknown response from remote server\n", logerr ? '!' : '^'); mirman_update(mdat->currip, mdat->af, mdat, 1); return NULL; } i = 3; ch = buffer + i; while(i < sizeof(buffer)) { if (*ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n' && *(ch - 3) == '\r') { ch++; i++; break; } ch++; i++; } if(sizeof(buffer) - i < 512) { logg("%cremote_cvdhead: Malformed CVD header (too short)\n", logerr ? '!' : '^'); mirman_update(mdat->currip, mdat->af, mdat, 1); return NULL; } memset(head, 0, sizeof(head)); for(j = 0; j < 512; j++) { if(!ch || (ch && !*ch) || (ch && !isprint(ch[j]))) { logg("%cremote_cvdhead: Malformed CVD header (bad chars)\n", logerr ? '!' : '^'); mirman_update(mdat->currip, mdat->af, mdat, 1); return NULL; } head[j] = ch[j]; } if(!(cvd = cl_cvdparse(head))) { logg("%cremote_cvdhead: Malformed CVD header (can't parse)\n", logerr ? '!' : '^'); mirman_update(mdat->currip, mdat->af, mdat, 1); } else { logg("OK\n"); mirman_update(mdat->currip, mdat->af, mdat, 0); } return cvd; } static int getfile(const char *srcfile, const char *destfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist) { char cmd[512], uastr[128], buffer[FILEBUFF], *ch; int bread, fd, totalsize = 0, rot = 0, totaldownloaded = 0, percentage = 0, sd; unsigned int i; char *remotename = NULL, *authorization = NULL, *headerline, ipaddr[46]; const char *rotation = "|/-\\"; #ifdef FILTER_RULES pid_t pid = -1; #endif if(proxy) { remotename = malloc(strlen(hostname) + 8); if(!remotename) { logg("!getfile: Can't allocate memory for 'remotename'\n"); return 75; /* FIXME */ } sprintf(remotename, "http://%s", hostname); if(user) { authorization = proxyauth(user, pass); if(!authorization) { free(remotename); return 75; /* FIXME */ } } } if(uas) strncpy(uastr, uas, sizeof(uastr)); else snprintf(uastr, sizeof(uastr), PACKAGE"/%s (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")", get_version()); uastr[sizeof(uastr) - 1] = 0; snprintf(cmd, sizeof(cmd), "GET %s/%s HTTP/1.0\r\n" "Host: %s\r\n%s" "User-Agent: %s\r\n" #ifdef FRESHCLAM_NO_CACHE "Cache-Control: no-cache\r\n" #endif "Connection: close\r\n" "\r\n", (remotename != NULL) ? remotename : "", srcfile, hostname, (authorization != NULL) ? authorization : "", uastr); if(remotename) free(remotename); if(authorization) free(authorization); memset(ipaddr, 0, sizeof(ipaddr)); if(ip[0]) /* use ip to connect */ sd = wwwconnect(ip, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist); else sd = wwwconnect(hostname, proxy, port, ipaddr, localip, ctimeout, mdat, logerr, can_whitelist); if(sd < 0) { return 52; } else { logg("*Trying to download http://%s/%s (IP: %s)\n", hostname, srcfile, ipaddr); } if(!ip[0]) strcpy(ip, ipaddr); if(send(sd, cmd, strlen(cmd), 0) < 0) { logg("%cgetfile: Can't write to socket\n", logerr ? '!' : '^'); closesocket(sd); return 52; } /* read http headers */ ch = buffer; i = 0; while(1) { /* recv one byte at a time, until we reach \r\n\r\n */ #ifdef SO_ERROR if((i >= sizeof(buffer) - 1) || wait_recv(sd, buffer + i, 1, 0, rtimeout) == -1) { #else if((i >= sizeof(buffer) - 1) || recv(sd, buffer + i, 1, 0) == -1) { #endif logg("%cgetfile: Error while reading database from %s (IP: %s)\n", logerr ? '!' : '^', hostname, ipaddr); mirman_update(mdat->currip, mdat->af, mdat, 1); closesocket(sd); return 52; } if(i > 2 && *ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n' && *(ch - 3) == '\r') { i++; break; } ch++; i++; } buffer[i] = 0; /* check whether the resource actually existed or not */ if((strstr(buffer, "HTTP/1.1 404")) != NULL || (strstr(buffer, "HTTP/1.0 404")) != NULL) { logg("^getfile: %s not found on remote server (IP: %s)\n", srcfile, ipaddr); mirman_update(mdat->currip, mdat->af, mdat, 2); closesocket(sd); return 58; } if(!strstr(buffer, "HTTP/1.1 200") && !strstr(buffer, "HTTP/1.0 200") && !strstr(buffer, "HTTP/1.1 206") && !strstr(buffer, "HTTP/1.0 206")) { logg("%cgetfile: Unknown response from remote server (IP: %s)\n", logerr ? '!' : '^', ipaddr); mirman_update(mdat->currip, mdat->af, mdat, 1); closesocket(sd); return 58; } /* get size of resource */ for(i = 0; (headerline = cli_strtok(buffer, i, "\n")); i++){ if(strstr(headerline, "Content-Length:")) { if((ch = cli_strtok(headerline, 1, ": "))) { totalsize = atoi(ch); free(ch); } else { totalsize = 0; } } free(headerline); } #ifdef FILTER_RULES { int pfd[2]; if (pipe(pfd) == -1) { logg("!getfile: Can't create pipe to filter\n"); closesocket(sd); return 57; } pid = fork(); if (pid == -1) { logg("!getfile: Can't filter fork failed\n"); closesocket(sd); return 57; } if (pid == 0) { /* Child */ close(pfd[1]); dup2(pfd[0], 0); close(pfd[0]); execl("/bin/clamfilter", "clamfilter", destfile, NULL); _exit(1); } /* Parent */ close(pfd[0]); fd = pfd[1]; destfile = "clam-filter"; } #else if((fd = open(destfile, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644)) == -1) { char currdir[512]; if(getcwd(currdir, sizeof(currdir))) logg("!getfile: Can't create new file %s in %s\n", destfile, currdir); else logg("!getfile: Can't create new file %s in the current directory\n", destfile); logg("Hint: The database directory must be writable for UID %d or GID %d\n", getuid(), getgid()); closesocket(sd); return 57; } #endif #ifdef SO_ERROR while((bread = wait_recv(sd, buffer, FILEBUFF, 0, rtimeout)) > 0) { #else while((bread = recv(sd, buffer, FILEBUFF, 0)) > 0) { #endif if(write(fd, buffer, bread) != bread) { logg("getfile: Can't write %d bytes to %s\n", bread, destfile); #ifdef FILTER_RULES // if (pid == -1) // unlink(destfile); #else unlink(destfile); #endif close(fd); closesocket(sd); return 57; /* FIXME */ } if(totalsize > 0) { totaldownloaded += bread; percentage = (int) (100 * (float) totaldownloaded / totalsize); } if(!mprintf_quiet) { if(totalsize > 0) { mprintf("Downloading %s [%3i%%]\r", srcfile, percentage); } else { mprintf("Downloading %s [%c]\r", srcfile, rotation[rot]); rot++; rot %= 4; } fflush(stdout); } } closesocket(sd); close(fd); #ifdef FILTER_RULES /*if (pid != -1)*/ { int status; waitpid(pid, &status, 0); if (! WIFEXITED(status) || WEXITSTATUS(status) != 0) { logg("!getfile: Filter failed\n"); return 57; } } #endif if(totalsize > 0) logg("Downloading %s [%i%%]\n", srcfile, percentage); else logg("Downloading %s [*]\n", srcfile); mirman_update(mdat->currip, mdat->af, mdat, 0); return 0; } static int getcvd(const char *cvdfile, const char *newfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, unsigned int newver, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist) { struct cl_cvd *cvd; int ret; logg("*Retrieving http://%s/%s\n", hostname, cvdfile); if((ret = getfile(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat, logerr, can_whitelist))) { logg("%cCan't download %s from %s\n", logerr ? '!' : '^', cvdfile, hostname); unlink(newfile); return ret; } if((ret = cl_cvdverify(newfile))) { logg("!Verification: %s\n", cl_strerror(ret)); unlink(newfile); return 54; } if(!(cvd = cl_cvdhead(newfile))) { logg("!Can't read CVD header of new %s database.\n", cvdfile); unlink(newfile); return 54; } if(cvd->version < newver) { logg("^Mirror %s is not synchronized.\n", ip); mirman_update(mdat->currip, mdat->af, mdat, 2); cl_cvdfree(cvd); unlink(newfile); return 59; } cl_cvdfree(cvd); return 0; } static int chdir_tmp(const char *dbname, const char *tmpdir) { char cvdfile[32]; if(access(tmpdir, R_OK|W_OK) == -1) { sprintf(cvdfile, "%s.cvd", dbname); if(access(cvdfile, R_OK) == -1) { sprintf(cvdfile, "%s.cld", dbname); if(access(cvdfile, R_OK) == -1) { logg("!chdir_tmp: Can't access local %s database\n", dbname); return -1; } } if(mkdir(tmpdir, 0755) == -1) { logg("!chdir_tmp: Can't create directory %s\n", tmpdir); return -1; } if(cli_cvdunpack(cvdfile, tmpdir) == -1) { logg("!chdir_tmp: Can't unpack %s into %s\n", cvdfile, tmpdir); cli_rmdirs(tmpdir); return -1; } } if(chdir(tmpdir) == -1) { logg("!chdir_tmp: Can't change directory to %s\n", tmpdir); return -1; } return 0; } static int getpatch(const char *dbname, const char *tmpdir, int version, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist) { char *tempname, patch[32], olddir[512]; int ret, fd; if(!getcwd(olddir, sizeof(olddir))) { logg("!getpatch: Can't get path of current working directory\n"); return 50; /* FIXME */ } if(chdir_tmp(dbname, tmpdir) == -1) return 50; tempname = cli_gentemp("."); snprintf(patch, sizeof(patch), "%s-%d.cdiff", dbname, version); logg("*Retrieving http://%s/%s\n", hostname, patch); if((ret = getfile(patch, tempname, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat, logerr, can_whitelist))) { logg("%cgetpatch: Can't download %s from %s\n", logerr ? '!' : '^', patch, hostname); unlink(tempname); free(tempname); CHDIR_ERR(olddir); return ret; } if((fd = open(tempname, O_RDONLY|O_BINARY)) == -1) { logg("!getpatch: Can't open %s for reading\n", tempname); unlink(tempname); free(tempname); CHDIR_ERR(olddir); return 55; } if(cdiff_apply(fd, 1) == -1) { logg("!getpatch: Can't apply patch\n"); close(fd); unlink(tempname); free(tempname); CHDIR_ERR(olddir); return 70; /* FIXME */ } close(fd); unlink(tempname); free(tempname); if(chdir(olddir) == -1) { logg("!getpatch: Can't chdir to %s\n", olddir); return 50; /* FIXME */ } return 0; }