virtual void tryAgain() { if (user.isEmpty() || (accessToken.isEmpty() && (clientId.isEmpty() || clientSecretKey.isEmpty() || requestUrl.isEmpty() || refreshToken.isEmpty()))) { result_ = QCA::SASLContext::Params; QMetaObject::invokeMethod(this, "resultsReady", Qt::QueuedConnection); return; } if (accessToken.isEmpty()) { requestAccessToken(); return; } sendAuth(); }
void MySQLClient::handleHandshake() { readData(); if( ! (( m_len > 1 ) && ( memchr( m_buf + 1, 0, m_len ) != NULL )) ) { DBG("Connected but could not find pcsz...\n"); onResult(MYSQL_PRTCL); return; } DBG("Connected to server: %d bytes read ; Protocol version %d, mysql-%s.\n", m_len, m_buf[0], &m_buf[1]); m_pPos = (byte*) memchr( (char*)(m_buf + 1), 0, m_len ) + 1; sendAuth(); }
int main(int argc, char *argv[]) { if (argc < 3) { // Check that there is an appropriate amount of arguements for usage fprintf(stderr,"usage %s plaintextfile keytextfile portno\n", argv[0]); exit(0); } FILE *cipherTextFP,*keyTextFP; // File pointers /* Open files */ cipherTextFP= fopen(argv[1],"r"); keyTextFP = fopen(argv[2],"r"); /* ERROR out if files don't open */ if(cipherTextFP==NULL)error("ERROR opening plaintext file for input",2); if(keyTextFP==NULL)error("ERROR opening keytext file for input",2); /* This if statement checks that the key file is at least as big as the ptext file */ if(fSize(cipherTextFP)>fSize(keyTextFP))error("ERROR cipherText is larger than keyText",2); /* These if statements employ checkContents to check file for bad chars */ if(checkContents(cipherTextFP))error("ERROR cipherText contains bad char data",2); if(checkContents(keyTextFP))error("ERROR cipherText contains bad char data",2); /* Setup socket */ int sockfd = sockSetup(atoi(argv[3])); /* Authorize server by listening for one time code */ sendAuth(sockfd,"decAck"); // exit if not correct server /* Transmit key and ptext files */ sendFile(cipherTextFP,sockfd); sendFile(keyTextFP,sockfd); /* close open files */ fclose(cipherTextFP); fclose(keyTextFP); /* Receive decoded text */ char pText[290000]; receive(pText,sockfd); close(sockfd); printf("%s\n", pText); return (0); }
void put(int* sock, char* filename, conf_t config, node_t* headptr) { FILE* fp = NULL, *f1 = NULL, *f2 = NULL, *f3 = NULL, *f4 = NULL; int i, rc; int invalid = 0; int hash; int size, f1size, f2size, f3size;//, f4size; pieces_t dfs[4]; // Denotes which piece we are putting in which file char buf; fp = fopen(filename, "rb"); if (!fp) { fprintf(stderr, "Error opening file\n"); return; } hash = getHash(fp); putHash(hash, filename, headptr); fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); f1size = size/4; f2size = size/4; f3size = size/4; //f4size = size/4 + size%4; // Give the extra bytes to the last file chunk /* Divide the file */ f1 = tmpfile(); f2 = tmpfile(); f3 = tmpfile(); f4 = tmpfile(); char c; int k = 0; while((c = fgetc(fp)) != EOF) { if (k < f1size){ fputc(c, f1); } else if (k < f1size+f2size) { fputc(c, f2); } else if (k < f1size+f2size+f3size) { fputc(c, f3); } else { fputc(c, f4); } k++; } /* Set up the hasing values */ switch (hash) { case 0: dfs[0].p1 = 1; dfs[0].p2 = 2; dfs[1].p1 = 2; dfs[1].p2 = 3; dfs[2].p1 = 3; dfs[2].p2 = 4; dfs[3].p1 = 4; dfs[3].p2 = 1; break; case 1: dfs[0].p1 = 4; dfs[0].p2 = 1; dfs[1].p1 = 1; dfs[1].p2 = 2; dfs[2].p1 = 2; dfs[2].p2 = 3; dfs[3].p1 = 3; dfs[3].p2 = 4; break; case 2: dfs[0].p1 = 3; dfs[0].p2 = 4; dfs[1].p1 = 4; dfs[1].p2 = 1; dfs[2].p1 = 1; dfs[2].p2 = 2; dfs[3].p1 = 2; dfs[3].p2 = 3; break; case 3: dfs[0].p1 = 2; dfs[0].p2 = 3; dfs[1].p1 = 3; dfs[1].p2 = 4; dfs[2].p1 = 4; dfs[2].p2 = 1; dfs[3].p1 = 1; dfs[3].p2 = 2; break; default: printf("Oh no!"); return; } sendAuth(sock, config); // Send authorization to servers for (i = 0; i < 4; i++) { while((rc = read(sock[i], &buf, sizeof(buf))) > 0) { if (buf == 'I') invalid = 1; if (buf == '\x3') break; } if (invalid) { write(sock[i], "PUT", strlen("PUT")); printf("Invalid Username/Password. Please try again.\n"); return; } if(rc > 0) { char request[BUF_SIZE], piece[BUF_SIZE], ch; strncpy(request, "PUT ", sizeof("PUT ")); strcat(request, filename); strcat(request, " "); sprintf(piece, "%d %d", dfs[i].p1, dfs[i].p2); strcat(request, piece); write(sock[i], request, strlen(request)); fseek(f1, 0, SEEK_SET); fseek(f2, 0, SEEK_SET); fseek(f3, 0, SEEK_SET); fseek(f4, 0, SEEK_SET); if(dfs[i].p1 == 1) { while((ch = fgetc(f1)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } else if (dfs[i].p1 == 2) { while((ch = fgetc(f2)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } else if (dfs[i].p1 == 3) { while((ch = fgetc(f3)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } else if (dfs[i].p1 == 4) { while((ch = fgetc(f4)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } fseek(f1, 0, SEEK_SET); fseek(f2, 0, SEEK_SET); fseek(f3, 0, SEEK_SET); fseek(f4, 0, SEEK_SET); if(dfs[i].p2 == 1) { while((ch = fgetc(f1)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } else if (dfs[i].p2 == 2) { while((ch = fgetc(f2)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } else if (dfs[i].p2 == 3) { while((ch = fgetc(f3)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } else if (dfs[i].p2 == 4) { while((ch = fgetc(f4)) != EOF) write(sock[i], &ch, 1); char delim = '\x3'; write(sock[i], &delim, 1); } // while((rc = read(sock[i], &r, sizeof(r))) > 0) { // if (r == '\x3') // break; // printf("%c", r); // } } else { sock[i] = -1; } } if (fclose(fp)) { fprintf(stderr, "Error closing file\n"); } if (fclose(f1)) { fprintf(stderr, "Error closing file\n"); } if (fclose(f2)) { fprintf(stderr, "Error closing file\n"); } if (fclose(f3)) { fprintf(stderr, "Error closing file\n"); } if (fclose(f4)) { fprintf(stderr, "Error closing file\n"); } unlink(filename); }
void get(int* sock, char* filename, conf_t config, node_t* headptr) { int i, rc, hash; int invalid = 0; char buf; char r; FILE** f1 = malloc(sizeof(FILE*) * 4); FILE** f2 = malloc(sizeof(FILE*) * 4); FILE* fp = fopen(filename, "wb"); for (i = 0; i < 4; i++) { f1[i] = tmpfile(); f2[i] = tmpfile(); } sendAuth(sock, config); // Send authorization to servers for (i = 0; i < 4; i++) { while((rc = read(sock[i], &buf, sizeof(buf))) > 0) { if (buf == 'I') invalid = 1; if (buf == '\x3') break; } if (invalid) { write(sock[i], "GET ", strlen("GET ")); printf("Invalid Username/Password. Please try again.\n"); return; } if(rc > 0) { char request[BUF_SIZE]; strncpy(request, "GET ", sizeof("GET ")); strcat(request, filename); write(sock[i], request, strlen(request)); while((rc = read(sock[i], &r, sizeof(r))) > 0) { if (r == '\x3') break; fputc(r, f1[i]); } while((rc = read(sock[i], &r, sizeof(r))) > 0) { if (r == '\x3') break; fputc(r, f2[i]); } } else { fclose(f1[i]); f1[i] = NULL; fclose(f2[i]); f2[i] = NULL; sock[i] = -1; } } hash = retrieveHash(filename, headptr); for(i = 0; i < 4; i++) { if(f1[i] != NULL) { fseek(f1[i], 0, SEEK_SET); fseek(f2[i], 0, SEEK_SET); } } int missing_count = 0; if(((sock[0] == -1) && (sock[1] == -1)) || ((sock[1] == -1) && (sock[2] == -1)) || ((sock[2] == -1) && (sock[3] == -1)) || ((sock[3] == -1) && (sock[0] == -1))) { missing_count = 2; } if (missing_count > 1) { printf("File is incomplete.\n"); } else { int sock_num; // Number of the socket to get the file from char c; // Reads the character switch (hash) { case 0: /* File piece 1 */ sock_num = 0; if (sock[0] == -1) sock_num = 3; if(sock_num == 0) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 3) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 2 */ sock_num = 0; if (sock[0] == -1) { sock_num = 1; } if(sock_num == 0) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 1) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 3 */ sock_num = 1; if (sock[1] == -1) { sock_num = 2; } if(sock_num == 1) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 2) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 4 */ sock_num = 2; if (sock[0] == -1) { sock_num = 3; } if(sock_num == 2) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 3) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } break; case 1: /* File piece 1 */ sock_num = 0; if (sock[0] == -1) sock_num = 1; if(sock_num == 0) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 1) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 2 */ sock_num = 1; if (sock[0] == -1) { sock_num = 2; } if(sock_num == 1) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 2) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 3 */ sock_num = 2; if (sock[1] == -1) { sock_num = 3; } if(sock_num == 2) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 3) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 4 */ sock_num = 3; if (sock[0] == -1) { sock_num = 0; } if(sock_num == 3) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 0) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } break; case 2: /* File piece 1 */ sock_num = 1; if (sock[0] == -1) sock_num = 2; if(sock_num == 1) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 2) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 2 */ sock_num = 2; if (sock[0] == -1) { sock_num = 3; } if(sock_num == 2) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 3) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 3 */ sock_num = 3; if (sock[1] == -1) { sock_num = 0; } if(sock_num == 3) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 0) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 4 */ sock_num = 0; if (sock[0] == -1) { sock_num = 1; } if(sock_num == 0) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 1) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } break; case 3: /* File piece 1 */ sock_num = 2; if (sock[0] == -1) sock_num = 3; if(sock_num == 2) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 3) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 2 */ sock_num = 3; if (sock[0] == -1) { sock_num = 0; } if(sock_num == 3) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 0) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 3 */ sock_num = 0; if (sock[1] == -1) { sock_num = 1; } if(sock_num == 0) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 1) { while((c = fgetc(f1[sock_num])) != EOF) { fputc(c, fp); } } /* File piece 4 */ sock_num = 1; if (sock[0] == -1) { sock_num = 2; } if(sock_num == 1) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } else if(sock_num == 2) { while((c = fgetc(f2[sock_num])) != EOF) { fputc(c, fp); } } break; default: printf("File not found.\n"); break; } } for (i = 0; i < 4; i++) { if (f1[i] != NULL) { if (fclose(f1[i])) { fprintf(stderr, "Error closing file\n"); } if (fclose(f2[i])) { fprintf(stderr, "Error closing file\n"); } } } free(f1); free(f2); if (fclose(fp)) { fprintf(stderr, "Error closing file\n"); } for (i = 0; i < 4; i++) { char userdir[BUF_SIZE], fpath[BUF_SIZE]; char hiddenpath[BUF_SIZE]; char num[2]; int j; strncpy(userdir, "./DFS", sizeof("./DFS")); sprintf(num, "%d", i+1); strcat(userdir, num); strncat(userdir, "/", sizeof("/")); strcat(userdir, config.username); strncat(userdir, "/", sizeof("/")); for(j = 0; j < 4; j++) { strncpy(fpath, userdir, sizeof(userdir)); strcpy(hiddenpath, fpath); strcat(hiddenpath, "."); sprintf(num, "%d", j+1); strcat(hiddenpath, num); strcat(hiddenpath, "."); strcat(hiddenpath, filename); remove(hiddenpath); //unlink(hiddenpath); } } }
void list(int* sock, conf_t config) { int i, rc; int invalid = 0; char buf; int resCount = 0; char r; char response[BUF_SIZE]; char incomp[BUF_SIZE]; sendAuth(sock, config); // Send authorization to servers for (i = 0; i < 4; i++) { while((rc = read(sock[i], &buf, sizeof(buf))) > 0) { if (buf == 'I') invalid = 1; if (buf == '\x3') break; } if (invalid) { write(sock[i], "LIST", strlen("LIST")); printf("Invalid Username/Password. Please try again.\n"); return; } if(rc > 0) { write(sock[i], "LIST", strlen("LIST")); while((rc = read(sock[i], &r, sizeof(r))) > 0) { if (r == '\n') resCount++; if (r == '\x3') break; strncat(response, &r, 1); } } else { sock[i] = -1; } } int offsock = 0; for(i = 0; i < 4; i++) { if (sock[i] == -1) offsock++; } int missing_count = 0; if(((sock[0] == -1) && (sock[1] == -1)) || ((sock[1] == -1) && (sock[2] == -1)) || ((sock[2] == -1) && (sock[3] == -1)) || ((sock[3] == -1) && (sock[0] == -1))) { missing_count = 2; } if (missing_count > 1) { strcpy(incomp, "[incomplete]"); } else { strcpy(incomp, ""); } char strip_leading_dot[BUF_SIZE]; i = 1; int j = 0; while(response[i] != '\0') { if(!((response[i-1] == '\n') && (response[i] == '.'))) { strip_leading_dot[j] = response[i]; j++; } i++; } char strip_num[BUF_SIZE]; i = 1; j = 0; while(strip_leading_dot[i+1] != '\0') { if(!((strip_leading_dot[i-1] == '\n') && (strip_leading_dot[i+1] == '.'))) { strip_num[j] = strip_leading_dot[i]; j++; } i++; } char strip_second_dot[BUF_SIZE]; i = 1; j = 0; while(strip_num[i] != '\0') { if(!((strip_num[i-1] == '\n') && (strip_num[i] == '.'))) { strip_second_dot[j] = strip_num[i]; j++; } i++; } char strip_extra_newlines[BUF_SIZE]; i = 1; j = 0; while(strip_second_dot[i] != '\0') { if(!((strip_second_dot[i-1] == '\n') && (strip_second_dot[i] == '\n'))) { strip_extra_newlines[j] = strip_second_dot[i]; j++; } i++; } const char* current = strip_extra_newlines; int line_num = 1; int okay_print = 0; int count = 0; resCount /= (4-offsock); // Divide by the number of working sockets resCount /= 2; // Divide by 2 for 2 res from each working socket resCount -= 1; // Subtract 1 for returning .. okay_print = resCount * 2; // Number of lines we care about while(current) { if(count == okay_print) break; char* next = strchr(current, '\n'); if (next != NULL) { *next = '\0'; } if(strcmp(current, "") != 0 && (current[0] != '/')) { if(count%2 == 0) { printf("%d. %s %s\n", line_num, current, incomp); line_num++; } } if (next != NULL) { current = next + 1; } else { current = NULL; } count ++; } memset(&strip_leading_dot[0], 0, sizeof(strip_leading_dot)); memset(&strip_num[0], 0, sizeof(strip_num)); memset(&strip_second_dot[0], 0, sizeof(strip_second_dot)); memset(&strip_extra_newlines[0], 0, sizeof(strip_extra_newlines)); //memset(¤t[0], 0, sizeof(current)); }