static VOID CALLBACK DoBufferFlushTimerProc(HWND hwnd,UINT message,UINT_PTR idEvent,DWORD dwTime) { KillTimer(NULL,flushBuffersTimerId); log0("tflush1"); FlushFileBuffers(hDbFile); log0("tflush2"); }
void TC_ShowWindow::preprocessing() { log0("TITLE", "ShowOpenWindows"); log0("DESC", "Show current open windows classes and titles"); log0("PREP", ""); log0("TEST", "Show windows"); log0("POST", ""); }
/** * Initialize crypto library, generate keys */ void key_init() { unsigned char *prf_buf; time_t t; uint32_t t2; int explen, len; if (keytype == KEY_NONE) { return; } set_sys_keys(sys_keys); get_key_info(keytype, &keylen, &ivlen); hmaclen = get_hash_len(hashtype); memset(groupkey, 0, sizeof(groupkey)); memset(groupsalt, 0, sizeof(groupsalt)); memset(grouphmackey, 0, sizeof(grouphmackey)); if (!get_random_bytes(groupmaster, sizeof(groupmaster))) { log0(0, 0, "Failed to generate group master"); exit(1); } groupmaster[0] = UFTP_VER_NUM; if (!get_random_bytes(rand1, sizeof(rand1))) { log0(0, 0, "Failed to generate rand1"); exit(1); } // Sets the first 4 bytes of rand1 to the current time t = time(NULL); t2 = (uint32_t)(t & 0xFFFFFFFF); *(uint32_t *)rand1 = t2; explen = hmaclen + keylen + ivlen; prf_buf = calloc(explen + hmaclen, 1); if (prf_buf == NULL) { syserror(0, 0, "calloc failed!"); exit(1); } PRF(hashtype, explen, groupmaster, sizeof(groupmaster), "key expansion", rand1, sizeof(rand1), prf_buf, &len); memcpy(grouphmackey, prf_buf, hmaclen); memcpy(groupkey, prf_buf + hmaclen, keylen); memcpy(groupsalt, prf_buf + hmaclen + keylen, ivlen); free(prf_buf); if ((!strcmp(keyfile, "")) || (newkeylen != 0)) { privkey = gen_RSA_key(newkeylen, RSA_EXP, keyfile); } else { privkey = read_RSA_key(keyfile); } if (!privkey) { log0(0, 0, "Failed to read/generate private key"); exit(1); } rsalen = RSA_keylen(privkey); }
/** * Reads in the contents of the restart file. * Contains a server_restart_t header, followed by * one or more server_restart_host_t entries. */ void read_restart_file(const char *restart_name) { struct server_restart_t header; struct server_restart_host_t host; int fd, i, rval; if ((fd = open(restart_name, OPENREAD)) == -1) { syserror(0, 0, 0, "Failed to open restart file"); exit(ERR_PARAM); } if (file_read(fd, &header, sizeof(header), 0) == -1) { log0(0, 0, 0, "Failed to read header from restart file"); close(fd); exit(ERR_PARAM); } restart_groupid = header.group_id; restart_groupinst = header.group_inst; if (restart_groupinst == 0xff) { log0(0, 0, 0, "Maximum number of restarts reached"); close(fd); exit(ERR_PARAM); } if ((header.filecount > MAXFILES) || (header.filecount <= 0)) { log0(0, 0, 0, "Too many files listed in restart file"); close(fd); exit(ERR_PARAM); } for (i = 0; i < header.filecount; i++) { if (file_read(fd, filelist[i], sizeof(filelist[i]), 0) == -1) { log0(0, 0, 0, "Failed to read filename from restart file"); close(fd); exit(ERR_PARAM); } } filecount = header.filecount; while ((rval = file_read(fd, &host, sizeof(host), 1)) != 0) { if (rval == -1) { log0(0, 0, 0, "Failed to read host from restart file"); close(fd); exit(ERR_PARAM); } memcpy(destlist[destcount].name, host.name, sizeof(host.name)); destlist[destcount].id = host.id; destlist[destcount].proxyidx = -1; destlist[destcount].isproxy = host.is_proxy; destlist[destcount].has_fingerprint = host.has_fingerprint; if (host.has_fingerprint) { memcpy(destlist[destcount].keyfingerprint, host.keyfingerprint, sizeof(destlist[destcount].keyfingerprint)); } destcount++; } close(fd); }
/** * Save the state of a failed transfer so it can restarted later. */ void write_restart_file(int listidx) { struct file_t *fileinfo; struct client_restart_t restart; char restart_name[MAXPATHNAME]; int fd; // Don't bother if we're not using a temp directory. if (!strcmp(tempdir, "")) { return; } log1(group_list[listidx].group_id, 0, "Writing restart file"); memset(&restart, 0, sizeof(restart)); fileinfo = &group_list[listidx].fileinfo; if (group_list[listidx].phase != PHASE_MIDGROUP) { restart.blocks = fileinfo->blocks; restart.sections = fileinfo->sections; restart.size = fileinfo->size; strcpy(restart.name, fileinfo->name); } snprintf(restart_name, sizeof(restart_name), "%s%c_group_%08X_restart", tempdir, PATH_SEP, group_list[listidx].group_id); if ((fd = open(restart_name, OPENWRITE | O_CREAT | O_TRUNC, 0644)) == -1) { syserror(group_list[listidx].group_id, 0, "Failed to create restart file"); return; } if (file_write(fd, &restart, sizeof(restart)) == -1) { log0(group_list[listidx].group_id, 0, "Failed to write header for restart file"); goto errexit; } if (fileinfo->blocks && fileinfo->naklist) { if (file_write(fd, fileinfo->naklist, fileinfo->blocks) == -1) { log0(group_list[listidx].group_id, 0, "Failed to write NAK list for restart file"); goto errexit; } } if (fileinfo->sections && fileinfo->section_done) { if (file_write(fd, fileinfo->section_done, fileinfo->sections) == -1) { log0(group_list[listidx].group_id, 0, "Failed to write section_done list for restart file"); goto errexit; } } close(fd); return; errexit: close(fd); unlink(restart_name); }
static VOID CALLBACK DoBufferFlushTimerProc(HWND hwnd,UINT message,UINT idEvent,DWORD dwTime) { if (!pDbCache) return; KillTimer(NULL,flushBuffersTimerId); log0("tflush1"); if (FlushViewOfFile(pDbCache, 0) == 0) DatabaseCorruption(NULL); log0("tflush2"); }
/** * Print the final statistics for the given file */ void print_status(const struct finfo_t *finfo, struct timeval start_time) { struct timeval done_time; double elapsed_time; int i; if (sync_mode) { print_sync_status(finfo, start_time); return; } if (finfo->file_id == 0) { log0(0, 0, "Group complete"); return; } log0(0, 0, "Transfer status:"); for (done_time = start_time, i = 0; i < destcount; i++) { if (destlist[i].clientcnt >= 0) { continue; } clog0(0, 0, "Host: %-15s Status: ", destlist[i].name); switch (destlist[i].status) { case DEST_MUTE: slog0("Mute"); break; case DEST_LOST: slog0("Lost connection"); break; case DEST_ABORT: slog0("Aborted"); break; case DEST_DONE: if (destlist[i].comp_status == COMP_STAT_REJECTED) { slog0("Rejected"); break; } if (diff_usec(finfo->deststate[i].time, done_time) > 0) { done_time = finfo->deststate[i].time; } elapsed_time = (double)diff_usec(finfo->deststate[i].time, start_time) / 1000000; slog0("Completed time: %7.3f seconds NAKs: %d", elapsed_time, finfo->deststate[i].naks); break; default: slog0("Unknown code: %d", destlist[i].status); break; } } elapsed_time = (double)diff_usec(done_time, start_time) / 1000000; log1(0, 0, "Total elapsed time: %.3f seconds", elapsed_time); log1(0, 0, "Overall throughput: %.2f KB/s", (elapsed_time != 0) ? (finfo->size / elapsed_time / 1024) : 0); }
void DBFlush(int setting) { if(!setting) { log0("nflush1"); if(safetyMode) FlushFileBuffers(hDbFile); log0("nflush2"); return; } KillTimer(NULL,flushBuffersTimerId); flushBuffersTimerId=SetTimer(NULL,flushBuffersTimerId,50,DoBufferFlushTimerProc); }
void cls(uint8_t Port) { if(Port==1) { log1(ESC_ERASE_DISPLAY); log1(ESC_CURSOR_POS(0,0)); } else { log0(ESC_ERASE_DISPLAY); log0(ESC_CURSOR_POS(0,0)); } }
void DBFlush(int setting) { if(!setting) { log0("nflush1"); if(safetyMode && pDbCache) { if (FlushViewOfFile(pDbCache, 0) == 0) DatabaseCorruption(NULL); } log0("nflush2"); return; } KillTimer(NULL,flushBuffersTimerId); flushBuffersTimerId=SetTimer(NULL,flushBuffersTimerId,50,DoBufferFlushTimerProc); }
int verify_ECDSA_sig(EC_key_t ec, int hashtype, const unsigned char *mes, unsigned int meslen, const unsigned char *sig, unsigned int siglen) { log0(0, 0, 0, "ECDSA not supported"); return 0; }
/** * Returns the ALG_ID associated with a given keytype */ static ALG_ID get_cipher(int keytype) { switch (keytype) { case KEY_DES: return CALG_DES; case KEY_DES_EDE3: return CALG_3DES; case KEY_AES128_CBC: #ifdef CALG_AES_128 return CALG_AES_128; #else return 0; #endif case KEY_AES256_CBC: #ifdef CALG_AES_256 return CALG_AES_256; #else return 0; #endif case KEY_AES128_GCM: case KEY_AES256_GCM: case KEY_AES128_CCM: case KEY_AES256_CCM: // Not supported by CryptoAPI return 0; default: log0(0, 0, 0, "Unknown keytype: %d", keytype); return 0; } }
/** * Loads an RSA private key from the specified key container */ RSA_key_t read_RSA_key(const char *container) { int idx, found; // First find available private key slot for (idx = 0, found = 0; (idx < MAXLIST) && (!found); idx++) { if (private_key_list[idx].provider == 0) { found = 1; } } if (!found) { log0(0, 0, 0, "Couldn't find empty key slot for private key"); return 0; } idx--; if (!CryptAcquireContext(&private_key_list[idx].provider, container, NULL, prov_type, machine_keyset)) { mserror("CryptAcquireContext failed"); return 0; } if (!CryptGetUserKey(private_key_list[idx].provider, AT_KEYEXCHANGE, &private_key_list[idx].key)) { mserror("CryptGetUserKey failed"); return 0; } return private_key_list[idx].key; }
STDMETHODIMP_(MCONTACT) CDb3Mmap::AddContact() { DWORD ofsNew; log0("add contact"); DBContact dbc = { 0 }; dbc.signature = DBCONTACT_SIGNATURE; { mir_cslock lck(m_csDbAccess); ofsNew = CreateNewSpace(sizeof(DBContact)); dbc.ofsNext = m_dbHeader.ofsFirstContact; dbc.dwContactID = m_dwMaxContactId++; m_dbHeader.ofsFirstContact = ofsNew; m_dbHeader.contactCount++; DBWrite(ofsNew, &dbc, sizeof(DBContact)); DBWrite(0, &m_dbHeader, sizeof(m_dbHeader)); DBFlush(0); } DBCachedContact *cc = m_cache->AddContactToCache(dbc.dwContactID); cc->dwDriverData = ofsNew; NotifyEventHooks(hContactAddedEvent, dbc.dwContactID, 0); return dbc.dwContactID; }
static INT_PTR AddContact(WPARAM wParam,LPARAM lParam) { struct DBContact dbc; DWORD ofsNew; log0("add contact"); EnterCriticalSection(&csDbAccess); ofsNew=CreateNewSpace(sizeof(struct DBContact)); dbc.signature=DBCONTACT_SIGNATURE; dbc.eventCount=0; dbc.ofsFirstEvent=dbc.ofsLastEvent=0; dbc.ofsFirstSettings=0; dbc.ofsNext=dbHeader.ofsFirstContact; dbc.ofsFirstUnreadEvent=0; dbc.timestampFirstUnread=0; dbHeader.ofsFirstContact=ofsNew; dbHeader.contactCount++; DBWrite(ofsNew,&dbc,sizeof(struct DBContact)); DBWrite(0,&dbHeader,sizeof(dbHeader)); DBFlush(0); AddToCachedContactList((HANDLE)ofsNew, -1); LeaveCriticalSection(&csDbAccess); NotifyEventHooks(hContactAddedEvent,(WPARAM)ofsNew,0); return (INT_PTR)ofsNew; }
static VOID CALLBACK DoBufferFlushTimerProc(HWND hwnd, UINT message, UINT_PTR idEvent, DWORD dwTime) { if (!pDbCache) return; KillTimer(NULL,flushBuffersTimerId); log0("tflush1"); if (FlushViewOfFile(pDbCache, 0) == 0) { if (flushFailTick == 0) flushFailTick = GetTickCount(); else if (GetTickCount() - flushFailTick > 5000) DatabaseCorruption(NULL); } else flushFailTick = 0; log0("tflush2"); }
int create_ECDSA_sig(EC_key_t rsa, int hashtype, const unsigned char *mes, unsigned int meslen, unsigned char *sig, unsigned int *siglen) { log0(0, 0, 0, "ECDSA not supported"); return 0; }
/** * Sends out a data packet. All headers should be populated except blsize */ int send_data(const struct finfo_t *finfo, unsigned char *packet, int datalen, unsigned char *encpacket) { struct uftp_h *header; struct fileseg_h *fileseg; int payloadlen; unsigned char *outpacket; header = (struct uftp_h *)packet; fileseg = (struct fileseg_h *)(packet + sizeof(struct uftp_h)); payloadlen = sizeof(struct fileseg_h) + datalen; if (keytype != KEY_NONE) { if (!encrypt_and_sign(packet, &encpacket, payloadlen, mtu, keytype, groupkey, groupsalt, ivlen, hashtype, grouphmackey, hmaclen, sigtype, privkey, rsalen)) { log0(0, 0, "Error encrypting FILESEG"); return 0; } outpacket = encpacket; payloadlen = ntohs(((struct uftp_h *)outpacket)->blsize); } else { outpacket = packet; header->blsize = htons(payloadlen); } if (nb_sendto(sock, outpacket, payloadlen + sizeof(struct uftp_h), 0, (struct sockaddr *)&receive_dest, sizeof(receive_dest)) == SOCKET_ERROR) { sockerror(0, 0, "Error sending FILESEG"); return 0; } return 1; }
/** * Seeks to a particular block in a file * Returns 1 on success, 0 on error */ int seek_block(int file, int block, f_offset_t *offset, f_offset_t curr_offset) { if ((*offset = lseek_func(file, ((f_offset_t)block * blocksize) - curr_offset, SEEK_CUR)) == -1) { syserror(0, 0, "lseek failed for file"); return 0; } if (*offset != (f_offset_t)block * blocksize) { log0(0, 0, "block %d: offset is %s", block, printll(*offset)); log0(0, 0, " should be %s", printll((f_offset_t)block * blocksize)); if ((*offset = lseek_func(file, ((f_offset_t)block * blocksize) - (*offset), SEEK_CUR)) == -1) { syserror(0, 0, "lseek failed for file"); return 0; } } return 1; }
/** * Handles an ABORT message from a server */ void handle_abort(int listidx, const unsigned char *message, int meslen) { struct abort_h *abort; struct uftp2_h *v2_header; const char *v2_message; int found, i; if (group_list[listidx].version == UFTP_V2_VER) { v2_header = (struct uftp2_h *)message; v2_message = (const char *)message + sizeof(struct uftp2_h); log0(group_list[listidx].group_id, group_list[listidx].file_id, "Transfer aborted by server: %s", v2_message); file_cleanup(listidx, 1); return; } abort = (struct abort_h *)message; if (meslen != sizeof(struct abort_h)) { log1(group_list[listidx].group_id, group_list[listidx].file_id, "Rejecting ABORT from server: invalid message size"); } if (abort->host == 0) { if (((abort->flags & FLAG_CURRENT_FILE) != 0) && (group_list[listidx].phase == PHASE_MIDGROUP)) { found = 0; } else { found = 1; } } else { for (i = 0, found = 0; (i < interface_count) && !found; i++) { if (abort->host == m_interface[i].addr.s_addr) { found = 1; } } } if (found) { log0(group_list[listidx].group_id, group_list[listidx].file_id, "Transfer aborted by server: %s", abort->message); file_cleanup(listidx, 1); } }
void utils_vacuum_check() { time_t lastC = utils_private_setting_get_time(DBRW_VACUUM_KEY, 0); time_t now = time(NULL); if (lastC) { if (lastC>now) { log0("System time changed. Resetting LastCompact key."); utils_private_setting_set_time(DBRW_VACUUM_KEY, now); } else if ((now-lastC)>(60*60*24*DBRW_COMPACT_DAYS)) { log0("Compacting database"); DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_INFODLG), 0, utils_vacuum_proc,(LPARAM)0); hwndVacuum = NULL; utils_private_setting_set_time(DBRW_VACUUM_KEY, now); } } else { utils_private_setting_set_time(DBRW_VACUUM_KEY, now); } }
void DBFlush(int setting) { if(!setting) { log0("nflush1"); if(safetyMode && pDbCache) { if (FlushViewOfFile(pDbCache, 0) == 0) { if (flushFailTick == 0) flushFailTick = GetTickCount(); else if (GetTickCount() - flushFailTick > 5000) DatabaseCorruption(NULL); } else flushFailTick = 0; } log0("nflush2"); return; } KillTimer(NULL,flushBuffersTimerId); flushBuffersTimerId=SetTimer(NULL,flushBuffersTimerId,50,DoBufferFlushTimerProc); }
/* * language_breakdown_free * * frees the buffers allocated by a language_breakdown * */ void language_breakdown_free(LanguageBreakdown *lb) { #ifndef NDEBUG if (lb->code == NULL || lb->comment == NULL) { log0("freeing language_breakdown twice!"); } #endif free(lb->code); free(lb->comment); lb->code = NULL; lb->comment = NULL; }
/** * Windows event handler, sets user_abort flag */ BOOL WINAPI winsig(DWORD event) { switch (event) { case CTRL_C_EVENT: log0(0, 0, 0, "Got CTRL_C_EVENT"); break; case CTRL_BREAK_EVENT: log0(0, 0, 0, "Got CTRL_BREAK_EVENT"); break; case CTRL_CLOSE_EVENT: log0(0, 0, 0, "Got CTRL_CLOSE_EVENT"); break; case CTRL_LOGOFF_EVENT: log0(0, 0, 0, "Got CTRL_LOGOFF_EVENT"); break; case CTRL_SHUTDOWN_EVENT: log0(0, 0, 0, "Got CTRL_SHUTDOWN_EVENT"); break; default: log0(0, 0, 0, "GOT unknown event %d", event); break; } user_abort = 1; return TRUE; }
void Serial_Init(const uint32_t BaudRate0, const uint32_t BaudRate1) { if (BaudRate0<=0) USART_Init0(DefaultBaudRate); else USART_Init0(BaudRate0); if (BaudRate1<=0) USART_Init1(DefaultBaudRate); else USART_Init1(BaudRate1); cls(0); cls(1); log0("stdio initialised\n"); log0("compiled at %s on %s\n",__TIME__,__DATE__); log0("SerialPort0\n"); log1("SerialPort1\n"); }
/** * Sends an ABORT message to one or more clients */ void send_abort(const struct finfo_t *finfo, const char *message, const struct sockaddr_in *destaddr, const struct in_addr *dest, int encrypt) { unsigned char *buf, *encrypted, *outpacket; struct uftp_h *header; struct abort_h *abort; int payloadlen; buf = calloc(mtu, 1); if (buf == NULL) { syserror(0, 0, "calloc failed!"); exit(1); } header = (struct uftp_h *)buf; abort = (struct abort_h *)(buf + sizeof(struct uftp_h)); set_uftp_header(header, ABORT, finfo, destaddr); abort->func = ABORT; if (dest) { abort->host = dest->s_addr; } strncpy(abort->message, message, sizeof(abort->message) - 1); payloadlen = sizeof(struct abort_h); if (encrypt) { encrypted = NULL; if (!encrypt_and_sign(buf, &encrypted, payloadlen, mtu, keytype, groupkey, groupsalt, ivlen, hashtype, grouphmackey, hmaclen, sigtype, privkey, rsalen)) { log0(0, 0, "Error encrypting ABORT"); free(buf); return; } outpacket = encrypted; payloadlen = ntohs(((struct uftp_h *)outpacket)->blsize); } else { encrypted = NULL; outpacket = buf; header->blsize = htons(payloadlen); } if (nb_sendto(sock, outpacket, payloadlen + sizeof(struct uftp_h), 0, (struct sockaddr *)destaddr, sizeof(struct sockaddr_in)) == SOCKET_ERROR) { sockerror(0, 0, "Error sending ABORT"); } free(buf); free(encrypted); }
/** * Process an ABORT message */ void handle_abort(const unsigned char *message, int meslen, int idx, struct finfo_t *finfo, const struct in_addr *hostaddr) { struct abort_h *abort; int i; abort = (struct abort_h *)message; if (meslen != sizeof(struct abort_h)) { log1(0, 0, "Rejecting ABORT from %s: invalid message size", (idx == -1) ? inet_ntoa(*hostaddr) : destlist[idx].name); return; } if (idx == -1) { log1(0, 0, "Transfer aborted by %s: %s", inet_ntoa(*hostaddr), abort->message); return; } if (abort->host != 0) { idx = find_client(abort->host); } if (idx == -1) { log1(0, 0, "Transfer aborted by %s: %s", inet_ntoa(*hostaddr), abort->message); } else { destlist[idx].status = DEST_ABORT; log1(0, 0, "Transfer aborted by %s: %s", destlist[idx].name, abort->message); } if (quit_on_error) { log0(0, 0, "Aboring all clients"); send_abort(finfo, "A client aborted, aborting all", &receive_dest, NULL, 0); // If encryption enabled, send ABORT both encrypted and unencrypted // since we can't be sure what phase we're currently in. if (keytype != KEY_NONE) { send_abort(finfo, "A client aborted, aborting all", &receive_dest, NULL, 1); } for (i = 0; i < destcount; i++) { if ((destlist[i].status == DEST_ACTIVE) || (destlist[i].status == DEST_REGISTERED) || (destlist[i].status == DEST_STATUS)) { destlist[i].status = DEST_ABORT; } } } }
/** * Hashes a block of data and verifies it against an RSA signature. */ int verify_RSA_sig(RSA_key_t rsa, int hashtype, const unsigned char *mes, unsigned int meslen, unsigned char *sig, unsigned int siglen) { HCRYPTHASH hash; ALG_ID alg; unsigned hashlen, i; int rval; unsigned char *insig; hashlen = get_hash_len(hashtype); alg = get_hash(hashtype); if (alg == 0) { log0(0, 0, 0, "Invalid hashtype"); return 0; } if (!CryptCreateHash(base_prov, alg, 0, 0, &hash)) { mserror("CryptCreateHash failed"); return 0; } if (!CryptHashData(hash, mes, meslen, 0)) { mserror("CryptHashData failed"); rval = 0; goto end; } insig = safe_calloc(siglen, 1); // CryptoAPI expects signatures in little endian, so reverse the bytes for (i = 0; i < siglen; i++) { insig[i] = sig[siglen - i - 1]; } if (!CryptVerifySignature(hash, insig, siglen, rsa, NULL, 0)) { mserror("CryptVerifySignature failed"); free(insig); rval = 0; goto end; } free(insig); rval = 1; end: if (!CryptDestroyHash(hash)) { mserror("CryptDestroyHash failed"); } return rval; }
void DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes) { int x = 0; log3("move %d %08x->%08x",bytes,ofsSource,ofsDest); if (ofsDest+bytes>dwFileSize) ReMap(ofsDest+bytes-dwFileSize); if (ofsSource+bytes>dwFileSize) { x = ofsSource+bytes-dwFileSize; log0("buggy move!"); _ASSERT(0); } if (x > 0) ZeroMemory(pDbCache+ofsDest+bytes-x, x); if (ofsSource < dwFileSize) MoveMemory(pDbCache+ofsDest,pDbCache+ofsSource, bytes-x); logg(); }
/** * Returns the ALG_ID associated with a given hashtype */ static ALG_ID get_hash(int hashtype) { switch (hashtype) { case HASH_SHA256: #ifdef CALG_SHA_256 return CALG_SHA_256; #else return 0; #endif case HASH_SHA1: return CALG_SHA1; case HASH_MD5: return CALG_MD5; default: log0(0, 0, 0, "Unknown hashtype: %d", hashtype); return 0; } }