Author: biurrun Date: Thu Nov 26 15:11:09 2009 New Revision: 1807 Log: Move functions around to avoid forward declarations. Modified: trunk/pairing/packet_handler_accept.c Modified: trunk/pairing/packet_handler_accept.c ============================================================================== --- trunk/pairing/packet_handler_accept.c Thu Nov 26 14:56:59 2009 (r1806) +++ trunk/pairing/packet_handler_accept.c Thu Nov 26 15:11:09 2009 (r1807) @@ -110,6 +110,50 @@ return gen_hdr; } + +/** This function authenticates the password. + * + * @todo Read from config file instead of sha.txt? + * + * @param password A pointer to string containing the received password. + * @return 1 if the password matches, 0 if the password does not match + */ +int check_password(char *password) +{ + FILE *file; + char file_contents[2*SHA_DIGEST_LENGTH+1]; + int result; + char pwd_hash_hex[2*SHA_DIGEST_LENGTH+1]; + + assert(password != NULL); + DEBUG_MED("Checking password: %s", password); + + // Open file + if ((file = fopen(FILE_PWD, "r")) == NULL) + { + DEBUG("Cannot open password file.\n"); + return 0; + } + + // Read line + if (!fgets(file_contents, 2*SHA_DIGEST_LENGTH+1, file)) + return 0; + file_contents[2*SHA_DIGEST_LENGTH] = 0; // Null-terminate the line + DEBUG_MED("File contents:\t\'%s\'", file_contents); + + get_sha(password, pwd_hash_hex); // Get the SHA1 hash of /password/ + DEBUG_MED("Hash is:\t\t\'%s\'", pwd_hash_hex); + result = strcmp(pwd_hash_hex, file_contents); // Compare the password with the file contents + + // Close file + if (fclose(file)) + DEBUG("File close error.\n"); + + return (result == 0) ? 1 : 0 ; + +} + + /** This function handles a password packet sent from the user to the relay. * * @param socket_addr Pointer to sockaddr_in6 structure connected to peer. @@ -169,104 +213,6 @@ } -/** Handles a password request packet. - * - * @param socket_addr Pointer to sockaddr_in6 structure connected to peer. - * @param hdr_pwd_request Pointer to the header_pwd_request structure. - * - * @return 1 if password generated; 0 otherwise - */ -int handle_packet_pwd_request(struct sockaddr_in6 *socket_addr, header_pwd_request *hdr_pwd_request) -{ - pisa_hitlist *allowed_hosts; - pisa_hitlist_entry *con; - - assert(socket_addr != NULL); - assert(hdr_pwd_request != NULL); - - header_general *gen_hdr; - char password[LENGTH_PASSWORD]; - - // Check if HIT is in allowed_hosts list - allowed_hosts = pisa_hitlist_build("allowed_hosts"); - con = pisa_hitlist_find(allowed_hosts,&socket_addr->sin6_addr); - - if(!con) - { - gen_hdr = create_error_struct(ERROR_NOT_CONNECTED, "Sorry, but that HIT is not authenticated on this relay."); - CHECK_FOR_NULL_HDR(gen_hdr); - - // Transmit message - SENDTO(global_accept.socket_desc, socket_addr, gen_hdr); - - free(gen_hdr); - - return 0; - } - - if (!store_pwd((char*)hdr_pwd_request->nickname, hdr_pwd_request->expiration1, hdr_pwd_request->expiration2, password)) - return 0; - - // Create password ack structure - gen_hdr = create_ack_2_struct(password); - CHECK_FOR_NULL_HDR(gen_hdr); - - // Send password ack structure - SENDTO(global_accept.socket_desc, socket_addr, gen_hdr); - - pisa_hitlist_destroy(allowed_hosts); - free(gen_hdr); - - - return 1; -} - - - -/** This function authenticates the password. - * - * @todo Read from config file instead of sha.txt? - * - * @param password A pointer to string containing the received password. - * @return 1 if the password matches, 0 if the password does not match - */ -int check_password(char *password) -{ - FILE *file; - char file_contents[2*SHA_DIGEST_LENGTH+1]; - int result; - char pwd_hash_hex[2*SHA_DIGEST_LENGTH+1]; - - assert(password != NULL); - DEBUG_MED("Checking password: %s", password); - - // Open file - if ((file = fopen(FILE_PWD, "r")) == NULL) - { - DEBUG("Cannot open password file.\n"); - return 0; - } - - // Read line - if (!fgets(file_contents, 2*SHA_DIGEST_LENGTH+1, file)) - return 0; - file_contents[2*SHA_DIGEST_LENGTH] = 0; // Null-terminate the line - DEBUG_MED("File contents:\t\'%s\'", file_contents); - - get_sha(password, pwd_hash_hex); // Get the SHA1 hash of /password/ - DEBUG_MED("Hash is:\t\t\'%s\'", pwd_hash_hex); - result = strcmp(pwd_hash_hex, file_contents); // Compare the password with the file contents - - // Close file - if (fclose(file)) - DEBUG("File close error.\n"); - - return (result == 0) ? 1 : 0 ; - -} - - - /** Creates a new password with the associated information and writes it all * in FILE_AUTHORIZED_HOSTS. * @@ -387,3 +333,55 @@ return 1; } + +/** Handles a password request packet. + * + * @param socket_addr Pointer to sockaddr_in6 structure connected to peer. + * @param hdr_pwd_request Pointer to the header_pwd_request structure. + * + * @return 1 if password generated; 0 otherwise + */ +int handle_packet_pwd_request(struct sockaddr_in6 *socket_addr, header_pwd_request *hdr_pwd_request) +{ + pisa_hitlist *allowed_hosts; + pisa_hitlist_entry *con; + + assert(socket_addr != NULL); + assert(hdr_pwd_request != NULL); + + header_general *gen_hdr; + char password[LENGTH_PASSWORD]; + + // Check if HIT is in allowed_hosts list + allowed_hosts = pisa_hitlist_build("allowed_hosts"); + con = pisa_hitlist_find(allowed_hosts,&socket_addr->sin6_addr); + + if(!con) + { + gen_hdr = create_error_struct(ERROR_NOT_CONNECTED, "Sorry, but that HIT is not authenticated on this relay."); + CHECK_FOR_NULL_HDR(gen_hdr); + + // Transmit message + SENDTO(global_accept.socket_desc, socket_addr, gen_hdr); + + free(gen_hdr); + + return 0; + } + + if (!store_pwd((char*)hdr_pwd_request->nickname, hdr_pwd_request->expiration1, hdr_pwd_request->expiration2, password)) + return 0; + + // Create password ack structure + gen_hdr = create_ack_2_struct(password); + CHECK_FOR_NULL_HDR(gen_hdr); + + // Send password ack structure + SENDTO(global_accept.socket_desc, socket_addr, gen_hdr); + + pisa_hitlist_destroy(allowed_hosts); + free(gen_hdr); + + + return 1; +}