void ecg_read_32bit (int fd, uint32_t *value) { int i; uint8_t c; uint8_t buf[4]; // Read up to start-of-header do { //fprintf (stderr,"."); read_n_bytes (fd,&c,1); //fprintf (stderr,"%x ",c); } while (c != STREAM_START); debug (9, "ecg_read_32bit: data frame found"); // Check frame type (expecting type 0x02) read_n_bytes (fd,&c,1); if (c != 0x02) { debug(1,"ecg_read_32bit: error: was expecting frame type 0x02, got 0x%x",c); return; } // Read 32 bits read_n_esc_bytes (fd,buf,4); *value = (uint32_t) ((buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]); }
/** * * @return The entire frame length (excluding cksum) if successful, -1 on error. */ void ecg_read_record (int fd, ecg_record_t *record) { int i; uint8_t c; uint8_t buf[8]; debug (9, "ecg_read_record: wait for header"); // Read up to start-of-header do { //fprintf (stderr,"."); read_n_bytes (fd,&c,1); //fprintf (stderr,"%x ",c); } while (c != STREAM_START); debug (9, "ecg_read_record: data frame found"); // Check frame type (expecting type 0x01) read_n_bytes (fd,&c,1); if (c != 0x01) { debug(1,"ecg_read_record: error: was expecting frame type 0x01, got 0x%x",c); return; } // Read ECG frame read_n_esc_bytes (fd,buf,7); record->status = buf[0]; record->ch1 = (((buf[1]<<16) | (buf[2]<<8) | buf[3])<<8)/256; record->ch2 = (((buf[4]<<16) | (buf[5]<<8) | buf[6])<<8)/256; }
/** * Read n data bytes from an escaped stream. */ void read_n_esc_bytes (int fd, uint8_t *buf, int length) { int i; for (i = 0; i < length; i++) { read_n_bytes(fd,buf+i,1); if (buf[i]==STREAM_ESCAPE) { read_n_bytes(fd,buf+i,1); buf[i] ^= 0x20; } } }
static bool reopen_input_file(int i,progress_info *proginfo) { unsigned char *header; if (!open_input_stream(files[i])) { prog_error(proginfo); st_warning("could not reopen input file: [%s]",files[i]->filename); return FALSE; } if (NULL == (header = malloc(files[i]->header_size * sizeof(unsigned char)))) { prog_error(proginfo); st_warning("could not allocate %d-byte WAVE header",files[i]->header_size); return FALSE; } if (read_n_bytes(files[i]->input,header,files[i]->header_size,NULL) != files[i]->header_size) { prog_error(proginfo); st_warning("error while reading %d-byte WAVE header",files[i]->header_size); st_free(header); return FALSE; } st_free(header); return TRUE; }
int read_until(int fd, size_t n, char terminator, char* buf) { size_t read = 0; while (read < n) { ssize_t tmp_read = read_n_bytes(fd, 1, buf + read); if (tmp_read < 0) { return -1; } else if (tmp_read == 0) { break; } if (memchr(buf + read, terminator, tmp_read) != NULL) { *((char* )memchr(buf + read, terminator, tmp_read)) = '\0'; return read + tmp_read; } else { read += tmp_read; } } return -1; }
int open_stobj(char *StObjFileName) { int fd; struct stat sb; off_t size; char *mapaddr; invt_sescounter_t cnt; char *name; errno=0; fd = open_and_lock( StObjFileName, FILE_WRITE, wait_for_locks ); if (fd < 0) { return fd; } name = strdup(StObjFileName); if(name == NULL) { fprintf(stderr, "%s: internal memory error: strdup stobj_name\n", g_programName); exit(1); } read_n_bytes(fd, &cnt, sizeof(invt_sescounter_t), StObjFileName); lseek( fd, 0, SEEK_SET ); errno = 0; if (fstat(fd, &sb) < 0) { fprintf(stderr, "Could not get stat info on %s\n", StObjFileName); perror("fstat"); return -1; } size = sb.st_size; mapaddr = mmap_n_bytes(fd, size, BOOL_FALSE, StObjFileName); return add_stobj(name, fd, size, mapaddr, (invt_sescounter_t *)mapaddr); }
static off_t read_off_t (int fd) { off_t out; if (!read_n_bytes (fd, (char *) &out, sizeof (off_t))) return 0; return out; }
static int read_int (int fd) { int out; if (!read_n_bytes (fd, (char *) &out, sizeof (int))) return 0; return out; }
static void handle_tcp(int tcp_sock, struct entry* entries, int *count) { int s; struct sockaddr_storage addr_him; socklen_t hislen; uint8_t inbuf[INBUF_SIZE]; uint16_t tcplen; struct handle_tcp_userdata userdata; /* accept */ hislen = (socklen_t)sizeof(addr_him); if((s = accept(tcp_sock, (struct sockaddr*)&addr_him, &hislen)) < 0) { log_msg("accept(): %s\n", strerror(errno)); return; } userdata.s = s; /* tcp recv */ read_n_bytes(s, (uint8_t*)&tcplen, sizeof(tcplen)); tcplen = ntohs(tcplen); if(tcplen >= INBUF_SIZE) { log_msg("query %d bytes too large, buffer %d bytes.\n", tcplen, INBUF_SIZE); #ifndef USE_WINSOCK close(s); #else closesocket(s); #endif return; } read_n_bytes(s, inbuf, tcplen); handle_query(inbuf, (ssize_t) tcplen, entries, count, transport_tcp, send_tcp, &userdata, do_verbose?logfile:0); #ifndef USE_WINSOCK close(s); #else closesocket(s); #endif }
void serial_test(HANDLE h) { unsigned char i; long read,written; long total=0,last_total=0; unsigned char rx_buffer[256]; unsigned char tx_buffer[256]; int result; long t1,t2; int iterations=0; int avg=0,sum=0,min=2000,max=0; int n; for(i=0;i<=SIZE-1;i++) tx_buffer[i]=i; skip_bytes_until(h,SIZE-1); printf("found 0x%02x\n",SIZE-1); memset(rx_buffer,0,SIZE); WriteFile(h,tx_buffer,32,&written,NULL); t1 = timeGetTime(); while(1) { n = read_n_bytes(h,rx_buffer,SIZE); if(n==SIZE) { t2=timeGetTime(); WriteFile(h,tx_buffer,32,&written,NULL); for(i=0;i<=SIZE-1;i++) { //printf("%02x",buffer[i]); if(rx_buffer[i] != i) { printf("error @ %02x: %02x ",i,rx_buffer[i]); for(i=0;i<=SIZE-1;i++) printf("%02x",rx_buffer[i]); printf("\n"); skip_bytes_until(h,SIZE-1); printf("found 0x7f\n"); i=SIZE; } } //printf("\n"); iterations++; sum=sum+(t2-t1); avg=sum/iterations; min=(t2-t1)<min ? (t2-t1) : min; max=(t2-t1)>max ? (t2-t1) : max; printf("%4d now=%4d min=%4d avg=%4d max=%4d\n",iterations, t2-t1, min,avg,max); t1=t2; memset(rx_buffer,0,SIZE); } } }
/* Unprivileged side. */ int setgid_io_stat (char *filename, struct stat *buffer) { int length; write_cmd (cmd_stat); length = strlen (filename) + 1; write_int (setgid_io_outfd, length); write_n_bytes (setgid_io_outfd, filename, length); read_n_bytes (setgid_io_infd, (char *) buffer, sizeof (struct stat)); return read_int (setgid_io_infd); }
/* Privileged side. */ static void setgid_io_stat_priv (int outfd, int infd) { int length; char *filename; int result; struct stat buffer; length = read_int (infd); filename = g_malloc (length); read_n_bytes (infd, filename, length); result = stat (filename, &buffer); write_n_bytes (outfd, (char *) &buffer, sizeof (struct stat)); write_int (outfd, result); }
/* Unprivileged side. */ int setgid_io_read (int fd, char *buffer, int n) { int result; write_cmd (cmd_read); write_int (setgid_io_outfd, fd); write_int (setgid_io_outfd, n); result = read_int (setgid_io_infd); if ((result >= 0) && (result <= n)) { read_n_bytes (setgid_io_infd, buffer, result); } return result; }
/* Privileged side. */ static void setgid_io_open_priv (int outfd, int infd) { int length; char *path; int flags; int newfd; length = read_int (infd); path = g_malloc (length); read_n_bytes (infd, path, length); flags = read_int (infd); newfd = open (path, flags); write_int (outfd, newfd); g_free (path); }
/* Privileged side. */ static void setgid_io_write_priv (int outfd, int infd) { int fd; int n; int result; char *buffer; fd = read_int (infd); n = read_int (infd); buffer = g_malloc (n); read_n_bytes (infd, buffer, n); result = write (fd, buffer, n); write_int (outfd, result); g_free (buffer); }
static bool do_join() { int i,bytes_to_skip,bytes_to_xfer; proc_info output_proc; char outfilename[FILENAME_SIZE]; wlong total=0; unsigned char header[CANONICAL_HEADER_SIZE]; FILE *output; wave_info *joined_info; bool success; progress_info proginfo; success = FALSE; create_output_filename("","",outfilename); for (i=0;i<numfiles;i++) total += files[i]->data_size; if (all_files_cd_quality && (total % CD_BLOCK_SIZE) != 0) { pad_bytes = CD_BLOCK_SIZE - (total % CD_BLOCK_SIZE); if (JOIN_NOPAD != pad_type) total += pad_bytes; } if (NULL == (joined_info = new_wave_info(NULL))) { st_error("could not allocate memory for joined file information"); } joined_info->chunk_size = total + CANONICAL_HEADER_SIZE - 8; joined_info->channels = files[0]->channels; joined_info->samples_per_sec = files[0]->samples_per_sec; joined_info->avg_bytes_per_sec = files[0]->avg_bytes_per_sec; joined_info->rate = files[0]->rate; joined_info->block_align = files[0]->block_align; joined_info->bits_per_sample = files[0]->bits_per_sample; joined_info->data_size = total; joined_info->wave_format = files[0]->wave_format; joined_info->problems = (files[0]->problems & PROBLEM_NOT_CD_QUALITY); if (PROB_ODD_SIZED_DATA(joined_info)) joined_info->chunk_size++; joined_info->total_size = joined_info->chunk_size + 8; joined_info->length = joined_info->data_size / joined_info->rate; joined_info->exact_length = (double)joined_info->data_size / (double)joined_info->rate; length_to_str(joined_info); proginfo.initialized = FALSE; proginfo.prefix = "Joining"; proginfo.clause = "-->"; proginfo.filename1 = files[0]->filename; proginfo.filedesc1 = files[0]->m_ss; proginfo.filename2 = outfilename; proginfo.filedesc2 = joined_info->m_ss; proginfo.bytes_total = files[0]->total_size; prog_update(&proginfo); if (NULL == (output = open_output_stream(outfilename,&output_proc))) { st_error("could not open output file"); } make_canonical_header(header,joined_info); if (write_n_bytes(output,header,CANONICAL_HEADER_SIZE,&proginfo) != CANONICAL_HEADER_SIZE) { prog_error(&proginfo); st_warning("error while writing %d-byte WAVE header",CANONICAL_HEADER_SIZE); goto cleanup; } if (all_files_cd_quality && (JOIN_PREPAD == pad_type) && pad_bytes) { if (pad_bytes != write_padding(output,pad_bytes,&proginfo)) { prog_error(&proginfo); st_warning("error while pre-padding with %d zero-bytes",pad_bytes); goto cleanup; } } for (i=0;i<numfiles;i++) { proginfo.bytes_total = files[i]->total_size; proginfo.filename1 = files[i]->filename; proginfo.filedesc1 = files[i]->m_ss; prog_update(&proginfo); if (!open_input_stream(files[i])) { prog_error(&proginfo); st_warning("could not reopen input file"); goto cleanup; } bytes_to_skip = files[i]->header_size; while (bytes_to_skip > 0) { bytes_to_xfer = min(bytes_to_skip,CANONICAL_HEADER_SIZE); if (read_n_bytes(files[i]->input,header,bytes_to_xfer,NULL) != bytes_to_xfer) { prog_error(&proginfo); st_warning("error while reading %d bytes of data",bytes_to_xfer); goto cleanup; } bytes_to_skip -= bytes_to_xfer; } if (transfer_n_bytes(files[i]->input,output,files[i]->data_size,&proginfo) != files[i]->data_size) { prog_error(&proginfo); st_warning("error while transferring %lu bytes of data",files[i]->data_size); goto cleanup; } prog_success(&proginfo); close_input_stream(files[i]); } if (all_files_cd_quality && JOIN_POSTPAD == pad_type && pad_bytes) { if (pad_bytes != write_padding(output,pad_bytes,NULL)) { prog_error(&proginfo); st_warning("error while post-padding with %d zero-bytes",pad_bytes); goto cleanup; } } if ((JOIN_NOPAD == pad_type) && PROB_ODD_SIZED_DATA(joined_info) && (1 != write_padding(output,1,NULL))) { prog_error(&proginfo); st_warning("error while NULL-padding odd-sized data chunk"); goto cleanup; } if (all_files_cd_quality) { if (JOIN_NOPAD != pad_type) { if (pad_bytes) st_info("%s-padded output file with %d zero-bytes.\n",((JOIN_PREPAD == pad_type)?"Pre":"Post"),pad_bytes); else st_info("No padding needed.\n"); } else { st_info("Output file was not padded, "); if (pad_bytes) st_info("though it needs %d bytes of padding.\n",pad_bytes); else st_info("nor was it needed.\n"); } } success = TRUE; cleanup: if ((CLOSE_CHILD_ERROR_OUTPUT == close_output(output,output_proc)) || !success) { success = FALSE; remove_file(outfilename); st_error("failed to join files"); } return success; }
static int tunnel_to(int sock, ip_type ip, unsigned short port, proxy_type pt, char *user, char *pass) { char *dns_name = NULL; char hostnamebuf[MSG_LEN_MAX]; size_t dns_len = 0; PFUNC(); // we use ip addresses with 224.* to lookup their dns name in our table, to allow remote DNS resolution // the range 224-255.* is reserved, and it won't go outside (unless the app does some other stuff with // the results returned from gethostbyname et al.) // the hardcoded number 224 can now be changed using the config option remote_dns_subnet to i.e. 127 if(ip.octet[0] == remote_dns_subnet) { dns_len = at_get_host_for_ip(ip, hostnamebuf); if(!dns_len) goto err; else dns_name = hostnamebuf; } PDEBUG("host dns %s\n", dns_name ? dns_name : "<NULL>"); size_t ulen = strlen(user); size_t passlen = strlen(pass); if(ulen > 0xFF || passlen > 0xFF || dns_len > 0xFF) { proxychains_write_log(LOG_PREFIX "error: maximum size of 255 for user/pass or domain name!\n"); goto err; } int len; unsigned char buff[BUFF_SIZE]; char ip_buf[16]; switch (pt) { case HTTP_TYPE:{ if(!dns_len) { pc_stringfromipv4(&ip.octet[0], ip_buf); dns_name = ip_buf; } #define HTTP_AUTH_MAX ((0xFF * 2) + 1 + 1) /* 2 * 0xff: username and pass, plus 1 for ':' and 1 for zero terminator. */ char src[HTTP_AUTH_MAX]; char dst[(4 * HTTP_AUTH_MAX)]; if(ulen) { snprintf(src, sizeof(src), "%s:%s", user, pass); encode_base_64(src, dst, sizeof(dst)); } else dst[0] = 0; len = snprintf((char *) buff, sizeof(buff), "CONNECT %s:%d HTTP/1.0\r\n%s%s%s\r\n", dns_name, ntohs(port), ulen ? "Proxy-Authorization: Basic " : dst, dst, ulen ? "\r\n" : dst); if(len != send(sock, buff, len, 0)) goto err; len = 0; // read header byte by byte. while(len < BUFF_SIZE) { if(1 == read_n_bytes(sock, (char *) (buff + len), 1)) len++; else goto err; if(len > 4 && buff[len - 1] == '\n' && buff[len - 2] == '\r' && buff[len - 3] == '\n' && buff[len - 4] == '\r') break; } // if not ok (200) or response greather than BUFF_SIZE return BLOCKED; if(len == BUFF_SIZE || !(buff[9] == '2' && buff[10] == '0' && buff[11] == '0')) { PDEBUG("HTTP proxy blocked: buff=\"%s\"\n", buff); return BLOCKED; } return SUCCESS; } break; case SOCKS4_TYPE:{ buff[0] = 4; // socks version buff[1] = 1; // connect command memcpy(&buff[2], &port, 2); // dest port if(dns_len) { ip.octet[0] = 0; ip.octet[1] = 0; ip.octet[2] = 0; ip.octet[3] = 1; } memcpy(&buff[4], &ip, 4); // dest host len = ulen + 1; // username if(len > 1) memcpy(&buff[8], user, len); else { buff[8] = 0; } // do socksv4a dns resolution on the server if(dns_len) { memcpy(&buff[8 + len], dns_name, dns_len + 1); len += dns_len + 1; } if((len + 8) != write_n_bytes(sock, (char *) buff, (8 + len))) goto err; if(8 != read_n_bytes(sock, (char *) buff, 8)) goto err; if(buff[0] != 0 || buff[1] != 90) return BLOCKED; return SUCCESS; } break; case SOCKS5_TYPE:{ int n_methods = ulen ? 2 : 1; buff[0] = 5; // version buff[1] = n_methods ; // number of methods buff[2] = 0; // no auth method if(ulen) buff[3] = 2; /// auth method -> username / password if(2+n_methods != write_n_bytes(sock, (char *) buff, 2+n_methods)) goto err; if(2 != read_n_bytes(sock, (char *) buff, 2)) goto err; if(buff[0] != 5 || (buff[1] != 0 && buff[1] != 2)) { if(buff[0] == 5 && buff[1] == 0xFF) return BLOCKED; else goto err; } if(buff[1] == 2) { // authentication char in[2]; char out[515]; char *cur = out; size_t c; *cur++ = 1; // version c = ulen & 0xFF; *cur++ = c; memcpy(cur, user, c); cur += c; c = passlen & 0xFF; *cur++ = c; memcpy(cur, pass, c); cur += c; if((cur - out) != write_n_bytes(sock, out, cur - out)) goto err; if(2 != read_n_bytes(sock, in, 2)) goto err; if(in[0] != 1 || in[1] != 0) { if(in[0] != 1) goto err; else return BLOCKED; } } int buff_iter = 0; buff[buff_iter++] = 5; // version buff[buff_iter++] = 1; // connect buff[buff_iter++] = 0; // reserved if(!dns_len) { buff[buff_iter++] = 1; // ip v4 memcpy(buff + buff_iter, &ip, 4); // dest host buff_iter += 4; } else { buff[buff_iter++] = 3; //dns buff[buff_iter++] = dns_len & 0xFF; memcpy(buff + buff_iter, dns_name, dns_len); buff_iter += dns_len; } memcpy(buff + buff_iter, &port, 2); // dest port buff_iter += 2; if(buff_iter != write_n_bytes(sock, (char *) buff, buff_iter)) goto err; if(4 != read_n_bytes(sock, (char *) buff, 4)) goto err; if(buff[0] != 5 || buff[1] != 0) goto err; switch (buff[3]) { case 1: len = 4; break; case 4: len = 16; break; case 3: len = 0; if(1 != read_n_bytes(sock, (char *) &len, 1)) goto err; break; default: goto err; } if(len + 2 != read_n_bytes(sock, (char *) buff, len + 2)) goto err; return SUCCESS; } break; } err: return SOCKET_ERROR; }
int32_t mail_responder(uint32_t socket) { #ifdef DEBUG printf("Entering MAIL subroutine\n"); #endif /*DEBUG*/ //Allocate general variables //Allocate primary buffers and counters char source_account_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t source_account_length = 0; char source_domain_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t source_domain_length = 0; char dest_account_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t dest_account_length = 0; char dest_domain_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t dest_domain_length = 0; uint32_t version = 0; uint32_t attachment_count = 0; uint64_t message_length = 0; uint64_t log_length = 0; //Generate unique file from current time and current FD //Get time with nanosecond resolution (or so they say) struct timespec time_for_file; clock_gettime(CLOCK_REALTIME, &time_for_file); //Mix time with FD and hash //We are hashing two ints and a long, so char meat_and_potatoes[24] = {0}; //memset(meat_and_potatoes, 0, sizeof(meat_and_potatoes)); memcpy(meat_and_potatoes, &time_for_file.tv_sec, sizeof(time_for_file.tv_sec)); memcpy(meat_and_potatoes+sizeof(time_for_file.tv_sec), &socket, sizeof(socket)); memcpy(meat_and_potatoes+sizeof(time_for_file.tv_sec)+sizeof(socket), &time_for_file.tv_nsec, sizeof(time_for_file.tv_nsec)); unsigned char hash[64]; //64 bytes because hash has a fixed size output crypto_generichash(hash, sizeof(hash), (const unsigned char *)meat_and_potatoes, sizeof(meat_and_potatoes),NULL, 0); //Get file ready to write //TODO needs to be /mail/user/unique_file_name char unique_file_name[129] = {0}; //char base64_username[341] = {0}; char unique_file_location[522] = {0}; //TODO Need to check if user is part of this domain. If not the file location should be some temporary storage. //unique_file_name_length is not currently used. Should be fine. uint32_t unique_file_name_length = base64_encode((char *)hash, sizeof(hash), unique_file_name, sizeof(unique_file_name), (char *)filesystem_safe_base64_string, 64); if (unique_file_name_length<=0) { perror("base64_encode. unique_file_name failed to be created"); print_to_log("base64_encode failed to create a unique_file_name.", LOG_ERR); return -1; } //uint32_t base64_username_length = base64_encode((char *)dest_account_buffer, strlen(dest_account_buffer), base64_username, strlen(base64_username), (char *)filesystem_safe_base64_string, 64); //Read primary routing and processing information //First read in fixed length fields if (read_n_bytes(socket, (unsigned char *)&version, 4)!=4) { perror("read_n_bytes version"); print_to_log("Read error while reading crypto type", LOG_ERR); return -1; } if (read_n_bytes(socket, (unsigned char *)&attachment_count, 4)!=4) { perror("read_n_bytes attachment_count"); print_to_log("Read error while reading attachment count", LOG_ERR); return -1; } if (read_n_bytes(socket, (unsigned char *)&log_length, 8)!=8) { perror("read_n_bytes log_length"); print_to_log("Read error while reading message length", LOG_ERR); return -1; } if (read_n_bytes(socket, (unsigned char *)&message_length, 8)!=8) { perror("read_n_bytes message_length"); print_to_log("Read error while reading message length", LOG_ERR); return -1; } //Read in account and domain info if ((dest_account_length=read_until(socket, (unsigned char *)dest_account_buffer, sizeof(dest_account_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading dest_account_buffer", LOG_ERR); return -1; } if (snprintf(unique_file_location, sizeof(unique_file_location), "%s%s%s%s", "/mail/", dest_account_buffer, "/" , unique_file_name)<0) { perror("snprintf"); print_to_log("snprintf failed to create a new file string. Cannot write message out",LOG_ERR); return -1; } write_to_file((char *)&version, 4, unique_file_location); version = be32toh(version); write_to_file((char *)&attachment_count, 4, unique_file_location); attachment_count = be32toh(attachment_count); write_to_file((char *)&log_length, 8, unique_file_location); log_length = be64toh(log_length); write_to_file((char *)&message_length, 8, unique_file_location); message_length = be64toh(message_length); #ifdef DEBUG printf("Writing mail to %s\n", unique_file_location); printf("Log length = %ld\n", log_length); printf("Version = %d\n", version); printf("Attachment count = %d\n", attachment_count); printf("Message length = %ld\n", message_length); #endif /*DEBUG*/ write_to_file(dest_account_buffer, dest_account_length, unique_file_location); #ifdef DEBUG printf("dest_account_length = %d\n", dest_account_length); #endif /*DEBUG*/ if ((dest_domain_length=read_until(socket, (unsigned char *)dest_domain_buffer, sizeof(dest_domain_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading dest_domain_buffer", LOG_ERR); return -1; } write_to_file(dest_domain_buffer, dest_domain_length, unique_file_location); #ifdef DEBUG printf("dest_domain_length = %d\n", dest_domain_length); #endif /*DEBUG*/ //Check if this is the destination if((strcmp(dest_domain_buffer, home_domain)!=0)&&(forward==0)) { //Send delivery failure, close connection, clean up and return. unsigned char signature_of_DELIVERYFAILURE[crypto_sign_BYTES] = {0}; unsigned char write_buffer[sizeof(network_crypto_version)+sizeof(server_public_key)+sizeof(signature_of_DELIVERYFAILURE)+2]; crypto_sign_detached(signature_of_DELIVERYFAILURE, NULL, signature_of_DELIVERYFAILURE, sizeof(signature_of_DELIVERYFAILURE), server_private_key); memcpy(write_buffer, &network_crypto_version, sizeof(network_crypto_version)); memcpy(write_buffer+sizeof(network_crypto_version), cmtp_reply_DELIVERYFAILURE, sizeof(cmtp_reply_DELIVERYFAILURE)); memcpy(write_buffer+sizeof(network_crypto_version)+sizeof(cmtp_reply_DELIVERYFAILURE), &signature_of_DELIVERYFAILURE, sizeof(signature_of_DELIVERYFAILURE)); memcpy(write_buffer+sizeof(network_crypto_version)+sizeof(cmtp_reply_DELIVERYFAILURE)+sizeof(signature_of_DELIVERYFAILURE), &termination_char, sizeof(termination_char)); write(socket, write_buffer, sizeof(write_buffer)); } if ((source_account_length=read_until(socket, (unsigned char *)source_account_buffer, sizeof(source_account_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading source_account_buffer", LOG_ERR); return -1; } write_to_file(source_account_buffer, source_account_length, unique_file_location); #ifdef DEBUG printf("source_account_length = %d\n", source_account_length); #endif /*DEBUG*/ if ((source_domain_length=read_until(socket, (unsigned char *)source_domain_buffer, sizeof(source_domain_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading source_domain_buffer", LOG_ERR); return -1; } write_to_file(source_domain_buffer, source_domain_length, unique_file_location); #ifdef DEBUG printf("source_domain_length = %d\n", source_domain_length); #endif /*DEBUG*/ //This completes the read of the header //uint64_t numeric_message_length = be64toh(*(uint64_t*)(&(message_length[0]))); //uint64_t numeric_log_length = be64toh(*(uint64_t*)(&(log_length[0]))); char temp_byte[1] = {0}; //Read log for (uint64_t i = 0; i<log_length; i++) { if (read(socket, temp_byte, 1)<1) { print_to_log("read error while reading message body", LOG_ERR); perror("read"); return -1; } write_to_file(temp_byte, 1, unique_file_location); } //Read message body for (uint64_t i = 0; i<message_length; i++) { if (read(socket, temp_byte, 1)<1) { print_to_log("read error while reading message body", LOG_ERR); perror("read"); return -1; } write_to_file(temp_byte, 1, unique_file_location); } #ifdef DEBUG printf("Message body finished. Moving to attachment handling.\n"); #endif /*DEBUG*/ //Read for attachment //uint32_t numeric_attachment_count = be32toh(*(uint32_t*)(&(attachment_count[0]))); temp_byte[0] = 0; for (uint64_t i = 0; i<attachment_count; i++) { if (read(socket, temp_byte, 1)<1) { print_to_log("read error while reading message body", LOG_ERR); perror("read"); return -1; } write_to_file(temp_byte, 1, unique_file_location); } #ifdef DEBUG printf("Mail destin for %s\n", dest_domain_buffer); printf("Server domain is %s\n", home_domain); #endif /*DEBUG*/ //Destination cases if ((memcmp(dest_domain_buffer, home_domain, dest_domain_length)==0)&&(memcmp(dest_account_buffer,"",1))==0) { #ifdef DEBUG printf("Devlivered mail is for server. Begin processing.\n"); #endif /*DEBUG*/ print_to_log("Mail has arrived for the server. Processing.",LOG_INFO); //Destination is this domain and for the server } else if ((memcmp(dest_domain_buffer, home_domain, dest_domain_length)==0)) { #ifdef DEBUG printf("Devlivered mail is for a user on this domain. Store.\n"); #endif /*DEBUG*/ print_to_log("Mail has arrived for a user on this domain. Storing.",LOG_INFO); //Destination is for a user at this domain } else { #ifdef DEBUG printf("Devlivered mail is not destined for this domain. Forward to %s\n", dest_domain_buffer); #endif /*DEBUG*/ print_to_log("Mail has arrived for another domain. Forwarding.",LOG_INFO); forwardMessage(unique_file_location, dest_domain_buffer); //Destination is on the web. Forward message. } #ifdef DEBUG printf("Mail section complete. Returning to mail loop.\n"); #endif /*DEBUG*/ return 0; }
static int tunnel_to(int sock, unsigned int ip, unsigned short port, proxy_type pt,char *user,char *pass) { int len; char buff[BUFF_SIZE]; bzero (buff,sizeof(buff)); switch(pt) { case HTTP_TYPE: { sprintf(buff,"CONNECT %s:%d HTTP/1.0\r\n", inet_ntoa( * (struct in_addr *) &ip), ntohs(port)); if (user[0]) { char src[256]; char dst[512]; strcpy(src,user); strcat(src,":"); strcat(src,pass); encode_base_64(src,dst,512); strcat(buff,"Proxy-Authorization: Basic "); strcat(buff,dst); strcat(buff,"\r\n\r\n"); } else strcat(buff,"\r\n"); len=strlen(buff); if(len!=send(sock,buff,len,0)) return SOCKET_ERROR; bzero(buff,sizeof(buff)); len=0 ; // read header byte by byte. while(len<BUFF_SIZE) { if(1==read_n_bytes(sock,buff+len,1)) len++; else return SOCKET_ERROR; if ( len > 4 && buff[len-1]=='\n' && buff[len-2]=='\r' && buff[len-3]=='\n' && buff[len-4]=='\r' ) break; } // if not ok (200) or response greather than BUFF_SIZE return BLOCKED; if ( (len==BUFF_SIZE) || ! ( buff[9] =='2' && buff[10]=='0' && buff[11]=='0' )) return BLOCKED; return SUCCESS; } break; case SOCKS4_TYPE: { memset(buff,0,sizeof(buff)); buff[0]=4; // socks version buff[1]=1; // connect command memcpy(&buff[2],&port,2); // dest port memcpy(&buff[4],&ip,4); // dest host len=strlen(user)+1; // username if(len>1) strcpy(&buff[8],user); if((len+8)!=write_n_bytes(sock,buff,(8+len))) return SOCKET_ERROR; if(8!=read_n_bytes(sock,buff,8)) return SOCKET_ERROR; if (buff[0]!=0||buff[1]!=90) return BLOCKED; return SUCCESS; } break; case SOCKS5_TYPE: { if(user) { buff[0]=5; //version buff[1]=2; //nomber of methods buff[2]=0; // no auth method buff[3]=2; /// auth method -> username / password if(4!=write_n_bytes(sock,buff,4)) return SOCKET_ERROR; } else { buff[0]=5; //version buff[1]=1; //nomber of methods buff[2]=0; // no auth method if(3!=write_n_bytes(sock,buff,3)) return SOCKET_ERROR; } memset(buff,0,sizeof(buff)); if(2!=read_n_bytes(sock,buff,2)) return SOCKET_ERROR; if (buff[0]!=5||(buff[1]!=0&&buff[1]!=2)) { if((buff[0]==0x05)&&(buff[1]==(char)0xFF)) return BLOCKED; else return SOCKET_ERROR; } if (buff[1]==2) { // authentication char in[2]; char out[515]; char* cur=out; int c; *cur++=1; // version c=strlen(user); *cur++=c; strncpy(cur,user,c); cur+=c; c=strlen(pass); *cur++=c; strncpy(cur,pass,c); cur+=c; if((cur-out)!=write_n_bytes(sock,out,cur-out)) return SOCKET_ERROR; if(2!=read_n_bytes(sock,in,2)) return SOCKET_ERROR; if(in[0]!=1||in[1]!=0) { if(in[0]!=1) return SOCKET_ERROR; else return BLOCKED; } } buff[0]=5; // version buff[1]=1; // connect buff[2]=0; // reserved buff[3]=1; // ip v4 memcpy(&buff[4],&ip,4); // dest host memcpy(&buff[8],&port,2); // dest port if(10!=write_n_bytes(sock,buff,10)) return SOCKET_ERROR; if(4!=read_n_bytes(sock,buff,4)) return SOCKET_ERROR; if (buff[0]!=5||buff[1]!=0) return SOCKET_ERROR; switch (buff[3]) { case 1: len=4; break; case 4: len=16; break; case 3: len=0; if(1!=read_n_bytes(sock,(char*)&len,1)) return SOCKET_ERROR; break; default: return SOCKET_ERROR; } if((len+2)!=read_n_bytes(sock,buff,(len+2))) return SOCKET_ERROR; return SUCCESS; } break; } return SOCKET_ERROR; }
static bool split_file(wave_info *info) { unsigned char header[CANONICAL_HEADER_SIZE]; char outfilename[FILENAME_SIZE],filenum[FILENAME_SIZE]; int current; wint discard,bytes; bool success; wlong leadin_bytes, leadout_bytes, bytes_to_xfer; progress_info proginfo; // uwe double start_time, end_time, last_end_time; success = FALSE; proginfo.initialized = FALSE; proginfo.prefix = "Splitting"; proginfo.clause = "-->"; proginfo.filename1 = info->filename; proginfo.filedesc1 = info->m_ss; proginfo.filename2 = NULL; proginfo.filedesc2 = NULL; proginfo.bytes_total = 0; if (!open_input_stream(info)) { prog_error(&proginfo); st_error("could not reopen input file: [%s]",info->filename); } discard = info->header_size; while (discard > 0) { bytes = min(discard,CANONICAL_HEADER_SIZE); if (read_n_bytes(info->input,header,bytes,NULL) != bytes) { prog_error(&proginfo); st_error("error while discarding %d-byte WAVE header",info->header_size); } discard -= bytes; } leadin_bytes = (leadin) ? smrt_parse((unsigned char *)leadin,info) : 0; leadout_bytes = (leadout) ? smrt_parse((unsigned char *)leadout,info) : 0; adjust_for_leadinout(leadin_bytes,leadout_bytes); // uwe transcription st_output("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); st_output("<Trans version=\"2\">\n\n"); start_time = end_time = last_end_time = 0; for (current=0;current<numfiles;current++) { if (SPLIT_INPUT_CUE == input_type && cueinfo.format) { create_output_filename(cueinfo.filenames[current],"",outfilename); } else { st_snprintf(filenum,8,num_format,current+offset); create_output_filename(filenum,"",outfilename); } files[current]->chunk_size = files[current]->data_size + CANONICAL_HEADER_SIZE - 8; files[current]->channels = info->channels; files[current]->samples_per_sec = info->samples_per_sec; files[current]->avg_bytes_per_sec = info->avg_bytes_per_sec; files[current]->block_align = info->block_align; files[current]->bits_per_sample = info->bits_per_sample; files[current]->wave_format = info->wave_format; files[current]->rate = info->rate; files[current]->length = files[current]->data_size / (wlong)info->rate; files[current]->exact_length = (double)files[current]->data_size / (double)info->rate; files[current]->total_size = files[current]->chunk_size + 8; length_to_str(files[current]); proginfo.filedesc2 = files[current]->m_ss; proginfo.bytes_total = files[current]->total_size; if (extract_track[current]) { proginfo.prefix = "Splitting"; proginfo.filename2 = outfilename; // uwe transcription start_time += last_end_time; end_time += files[current]->data_size / (wlong)info->rate; last_end_time = end_time; printf("<Turn startTime=\"%.2f\"", start_time); // %.2f st_output(" endTime=\"%.2f\"", end_time); st_output(" splitFilename=\"%s\"", outfilename); st_output(" speaker=\"spk1\">\n"); st_output("<Sync time=\"%.2f\"/>\n", start_time); st_output("***Include transcript words here***\n"); st_output("</Turn>\n"); if (NULL == (files[current]->output = open_output_stream(outfilename,&files[current]->output_proc))) { prog_error(&proginfo); st_error("could not open output file"); } } else { proginfo.prefix = "Skipping "; proginfo.filename2 = NULLDEVICE; if (NULL == (files[current]->output = open_output(NULLDEVICE))) { prog_error(&proginfo); st_error("while skipping track %d: could not open output file: [%s]",current+1,NULLDEVICE); } files[current]->output_proc.pid = NO_CHILD_PID; } if (PROB_ODD_SIZED_DATA(files[current])) files[current]->chunk_size++; make_canonical_header(header,files[current]); // uwe disable //prog_update(&proginfo); if (write_n_bytes(files[current]->output,header,CANONICAL_HEADER_SIZE,&proginfo) != CANONICAL_HEADER_SIZE) { prog_error(&proginfo); st_warning("error while writing %d-byte WAVE header",CANONICAL_HEADER_SIZE); goto cleanup; } /* if this is not the first file, finish up writing previous file, and simultaneously start writing to current file */ if (0 != current) { /* write overlapping lead-in/lead-out data to both previous and current files */ if (transfer_n_bytes2(info->input,files[current]->output,files[current-1]->output,leadin_bytes+leadout_bytes,&proginfo) != leadin_bytes+leadout_bytes) { prog_error(&proginfo); st_warning("error while transferring %ld bytes of lead-in/lead-out",leadin_bytes+leadout_bytes); goto cleanup; } /* pad and close previous file */ if (PROB_ODD_SIZED_DATA(files[current-1]) && (1 != write_padding(files[current-1]->output,1,&proginfo))) { prog_error(&proginfo); st_warning("error while NULL-padding odd-sized data chunk"); goto cleanup; } close_output(files[current-1]->output,files[current-1]->output_proc); } /* transfer unique non-overlapping data from input file to current file */ bytes_to_xfer = files[current]->new_data_size; if (0 != current) bytes_to_xfer -= leadout_bytes; if (numfiles - 1 != current) bytes_to_xfer -= leadin_bytes; if (transfer_n_bytes(info->input,files[current]->output,bytes_to_xfer,&proginfo) != bytes_to_xfer) { prog_error(&proginfo); st_warning("error while transferring %ld bytes of data",bytes_to_xfer); goto cleanup; } /* if this is the last file, close it */ if (numfiles - 1 == current) { /* pad and close current file */ if (PROB_ODD_SIZED_DATA(files[current]) && (1 != write_padding(files[current]->output,1,&proginfo))) { prog_error(&proginfo); st_warning("error while NULL-padding odd-sized data chunk"); goto cleanup; } close_output(files[current]->output,files[current]->output_proc); // uwe transcription st_output("</Trans>\n"); } // uwe disable // prog_success(&proginfo); } close_input_stream(info); success = TRUE; cleanup: if (!success) { close_output(files[current]->output,files[current]->output_proc); remove_file(outfilename); st_error("failed to split file"); } return success; }
static OP * read_action(int timeout) { #ifdef _WIN32 HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); DWORD available; PeekNamedPipe(hIn, NULL, 0, NULL, &available, NULL); if (available == 0) return NULL; #else struct pollfd pfd[] = {{0, POLLIN, 0}}; poll(pfd, 1, timeout); if ((pfd[0].revents & (POLLIN | POLLERR | POLLHUP)) == 0) return NULL; #endif char op; if (read(0, &op, 1) == 0) return &eof_op; if (op == 'r' || op == 's') { char input_buf[9]; size_t challenges_lengths[16]; size_t challenges_num, challenges_buf_len = 0, domain_len; int i; if (!read_n_bytes(input_buf, 8)) return &eof_op; input_buf[8] = '\0'; READ_LEN(input_buf + 4, challenges_num); input_buf[4] = '\0'; READ_LEN(input_buf, domain_len); if (challenges_num >= sizeof(challenges_lengths) / sizeof(challenges_lengths[0])) return &eof_op; for (i = 0; i < challenges_num; i++) { if (!read_n_bytes(input_buf, 4)) return &eof_op; READ_LEN(input_buf, challenges_lengths[i]); challenges_buf_len += challenges_lengths[i]; } OP *buf = malloc(domain_len + (sizeof(char *) * (challenges_num + 1)) + challenges_buf_len + challenges_num + 1 + sizeof(OP)); if (!buf) return &eof_op; buf->op = op; buf->challenges = (char **) (buf + 1); buf->domain = (char *) (buf->challenges + challenges_num + 1); buf->domain[domain_len] = '\0'; if (!read_n_bytes(buf->domain, domain_len)) { free(buf); return &eof_op; } char *challenge = buf->domain + domain_len + 1; for (i = 0; i < challenges_num; i++) { buf->challenges[i] = challenge; if (!read_n_bytes(buf->challenges[i], challenges_lengths[i])) { free(buf); return &eof_op; } challenge[challenges_lengths[i]] = '\0'; challenge += challenges_lengths[i]; } buf->challenges[challenges_num] = NULL; return buf; } else return &eof_op; }