void checkNick(int sd, char *buff) { //TODO: add uniquenick support char nick[GP_NICK_LEN],email[GP_EMAIL_LEN],pass[GP_PASSWORD_LEN]; char sendbuff[512]; int userid,profileid; if(!find_param("nick", buff, nick, sizeof(nick))) { sendError(sd,"Error recieving request"); return; } mysql_real_escape_string(conn,nick,nick,strlen(nick)); if(!find_param("email", buff, email, sizeof(email))) { sendError(sd,"Error recieving request"); return; } mysql_real_escape_string(conn,email,email,strlen(email)); if(!find_param("pass", buff, pass, sizeof(pass))) { if(!find_param("passenc",buff,pass,sizeof(pass))) { sendError(sd,"Error recieving request"); return; } else { char *dpass; int passlen = strlen(pass); dpass = (char *)base64_decode((uint8_t *)pass, &passlen); passlen = gspassenc((uint8_t *)dpass); strcpy(pass,dpass); free(dpass); } } mysql_real_escape_string(conn,pass,pass,strlen(pass)); userid = getUserIDFromEmail(conn,email); if(userid == 0) { formatSend(sd,true,0,"\\cur\\%d",GP_CHECK_BAD_EMAIL); return; } if(!tryPassword(conn,userid,pass)) { formatSend(sd,true,0,"\\cur\\%d",GP_CHECK_BAD_PASSWORD); return; } profileid = getProfileIDFromNickEmail(conn, nick, email); if(profileid == 0) { formatSend(sd,true,0,"\\cur\\%d",GP_CHECK_BAD_NICK); return; } formatSend(sd,true,0,"\\cur\\0\\pid\\%d",profileid); return; }
int tryPassword(char *guess, int sock, struct addrinfo *servAddr, int i, int j) { int position = 0; while(position < strlen(options)) { guess[i] = options[position]; struct sockaddr_storage fromAddr; // Source address of IPaddress // Set length of from address structure (in-out parameter) socklen_t fromAddrLen = sizeof(fromAddr); guesses += 1; guess[j + 1] = '\0'; ssize_t numBytes = sendto(sock, guess, j+1, 0, servAddr->ai_addr, servAddr->ai_addrlen); if (numBytes < 0) dieWithMessage("sendto() failed"); else if (numBytes != j+1) dieWithMessage("sendto() error, sent unexpected number of bytes"); char message[2]; //starting timeout check struct pollfd fd; int res; fd.fd = sock; fd.events = POLLIN; res = poll(&fd, 1, 1000); // 1000 ms timeout if(res == 0) { printf("Connection timeout, continuing to next iteration.\n"); int next_length = i + 1; if(next_length <= j) { if(tryPassword(guess, sock, servAddr, next_length, j) == 0) { position++; } else { return 1; } } else { position++; } } else if(res == -1) { dieWithMessage("connection Error, client closing"); } else { //recieving from server numBytes = recv(sock, message, 1, 0); //returning from server, 0 = success, 1 = failure, 2 = timeout if (numBytes < 0) dieWithMessage("recvfrom() failed"); else if (numBytes != 1) dieWithMessage("recvfrom() error, received unexpected number of bytes"); //printf("message is: %s\n", message); message[1] = '\0'; if(strcmp(message, "0") == 0) { printf("Password guessed! The password was %s\n", guess); return 0; } if(strcmp(message, "1") == 0) { int next_length = i + 1; if(next_length <= j) { if(tryPassword(guess, sock, servAddr, next_length, j) == 0) { position++; } else { return 1; } } else { position++; } } } } return 0; }
int main(int argc, char *argv[]) { signal(SIGINT, ExitHandler); //signal handler and a start time to track programs runtime start_time = clock(); if (argc != 4) // Test for correct number of arguments { dieWithMessage("3 arguments required, Server Name(IP address), Server Port(between 5k and 10k), and a password length of 1-8\n"); } char *IPaddress = argv[1]; // First arg: Server's IP address //Checking for legal port char *originalport = argv[2]; // 2nd argument: Port int port = atoi(originalport); while(port < 5000 || port > 10000) { printf("port invalid, please choose a number between 5000 and 10000: "); scanf("%d", &port); } char servPort[6]; sprintf(servPort, "%d", port); int length = atoi(argv[3]);// 3rd argument: password Length while(length < 1 || length > 8) { printf("Length invalid, please enter a number between 1 and 8: "); scanf("%d", &length); } int found = 1; char *guess = malloc(sizeof(char)*(length+1)); int i = 0; int j = length - 1; // Tell the system what kind(s) of address info we want struct addrinfo addrCriteria; // Criteria for address match memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure addrCriteria.ai_family = AF_UNSPEC; // Any address family addrCriteria.ai_socktype = SOCK_DGRAM; // Only datagram sockets addrCriteria.ai_protocol = IPPROTO_UDP; // Only UDP protocol // Get address(es) struct addrinfo *servAddr; // List of IPaddress addresses int rtnVal = getaddrinfo(IPaddress, servPort, &addrCriteria, &servAddr); if (rtnVal != 0) dieWithMessage("getaddrinfo() failed"); // Create a datagram/UDP socket int sock = socket(servAddr->ai_family, servAddr->ai_socktype, servAddr->ai_protocol); // Socket descriptor for client if (sock < 0) dieWithMessage("socket() failed"); int thing = tryPassword(guess, sock, servAddr, i, j); if(thing == 0) { printf("The password was found in %lu guesses!\n", guesses); stop_time = clock(); printf("Elapsed time: %lf microseconds\n", (double)(stop_time - start_time)); } else { printf("The password was not guessed after %lu guesses!\n", guesses); stop_time = clock(); printf("Elapsed time: %lf microseconds\n", (double)(stop_time - start_time)); freeaddrinfo(servAddr); } close(sock); exit(0); }
void newUser(int sd, char *buff) { //TODO: add uniquenick support char sendbuff[512]; char nick[GP_NICK_LEN],email[GP_EMAIL_LEN],pass[GP_PASSWORD_LEN],uniquenick[GP_NICK_LEN]; int userid,profileid; find_param("uniquenick",buff,uniquenick,sizeof(uniquenick)); if(!find_param("nick", buff, nick, sizeof(nick))) { sendError(sd,"Error recieving request"); return; } mysql_real_escape_string(conn,nick,nick,strlen(nick)); if(!find_param("email", buff, email, sizeof(email))) { sendError(sd,"Error recieving request"); return; } mysql_real_escape_string(conn,email,email,strlen(email)); if(!find_param("pass", buff, pass, sizeof(pass))) { if(!find_param("passenc",buff,pass,sizeof(pass))) { sendError(sd,"Error recieving request"); return; } else { char *dpass; int passlen = strlen(pass); dpass = (char *)base64_decode((uint8_t *)pass, &passlen); passlen = gspassenc((uint8_t *)dpass); strcpy(pass,dpass); free(dpass); } } mysql_real_escape_string(conn,pass,pass,strlen(pass)); userid = getUserIDFromEmail(conn,email); if(userid == 0 || !tryPassword(conn,userid,pass)) { formatSend(sd,true,0,"\\nur\\%d",GP_NEWUSER_BAD_PASSWORD); return; } profileid = getProfileIDFromNickEmail(conn, nick, email); if(profileid != 0) { formatSend(sd,true,0,"\\nur\\%d\\pid\\%d",GP_NEWUSER_BAD_NICK,profileid); return; } if(!nameValid(nick,false)) { formatSend(sd,true,0,"\\nur\\%d",GP_NEWUSER_BAD_NICK); return; } if(uniquenick[0] != 0) { profileid = getProfileIDFromUniquenick(conn,uniquenick); if(profileid != 0) { formatSend(sd,true,0,"\\nur\\%d",GP_NEWUSER_UNIQUENICK_INUSE); return; } if(!nameValid(uniquenick,false)) { formatSend(sd,true,0,"\\nur\\%d",GP_NEWUSER_UNIQUENICK_INVALID); return; } } if(uniquenick[0] != 0) { profileid = makeNewProfileWithUniquenick(conn,nick,uniquenick,userid); } else { profileid = makeNewProfile(conn,nick,userid); } if(profileid != 0) { formatSend(sd,true,0,"\\nur\\0\\pid\\%d",profileid); return; } }