void cleanup(struct hash_map_t *arguments) { /* allocates space for the possible argument to be executed from the arguments map */ void *value; /* prints a debug message */ V_DEBUG("Cleaning the process information\n"); /* in case no arguments map is provided must return immediately nothing left to be processed */ if(arguments == NULL) { return; } /* tries to retrieve the daemon argument from the arguments map in case the value is set daemonclean the current process so that no structures remaining from the daemon process are left */ get_value_string_hash_map(arguments, (unsigned char *) "daemon", &value); if(value != NULL) { daemonclean(); } }
int execute_arguments(char *program_name, struct hash_map_t *arguments) { /* allocates space for the possible argument to be executed from the arguments map */ void *value; /* allocates the value to be used to verify the exitence of error from the function */ ERROR_CODE return_value; /* sets space for the flag that will control if the service should be run or not, this is used for certain situations (mostyle test) where the service is not meant to be run */ char run_service = TRUE; /* tries to retrieve the help argument from the arguments map in case the value exists prints the help value and then exits the current system */ get_value_string_hash_map(arguments, (unsigned char *) "help", &value); if(value != NULL) { return help(); } /* tries to retrieve the version argument from the arguments map in case the value exists prints the version value and then exits the current system */ get_value_string_hash_map(arguments, (unsigned char *) "version", &value); if(value != NULL) { return version(); } /* retrieves the test argument value from the arguments map and in case it's set starts the test process runing a series of test functions in sequence */ get_value_string_hash_map(arguments, (unsigned char *) "test", &value); if(value != NULL) { return test(); } /* retrieves the speed argument value from the arguments map and in case it's set starts the speed measuring and disables the runnig of the service */ get_value_string_hash_map(arguments, (unsigned char *) "speed", &value); if(value != NULL) { return speed(); } /* tries to retrieve the daemon argument from the arguments map in case the value is set daemonizes the current process so that it remains in background and returns to the caller process immediately, otherwise prints the viriatum information into the standard output "file", the label should be standard */ get_value_string_hash_map(arguments, (unsigned char *) "daemon", &value); if(value != NULL) { daemonize(); } else { print_information(); } /* tries to retrieve the local argument from the arguments map in case the value exists localizes the current service so that any file read is read from the current directory */ get_value_string_hash_map(arguments, (unsigned char *) "local", &value); if(value != NULL) { localize(); } /* in cas the flag that control if the service must be run is unset the control flow must be returned immediately (avoids running service) */ if(run_service == FALSE) { RAISE_NO_ERROR; } /* runs the service, with the given arguments, this call should block the program control flow until an event stop the running of the main loop */ return_value = run_service_s(program_name, arguments); /* tests the error code for error in case it exists prints a message indicating the problem that occurred */ if(IS_ERROR_CODE(return_value)) { V_ERROR_F("Problem running service (%s)\n", (char *) GET_ERROR()); RAISE_AGAIN(return_value); } /* returns the normal result value as no problems has occured during the execution of the command */ RAISE_NO_ERROR; }
ERROR_CODE auth_file_http(char *auth_file, char *authorization, unsigned char *result) { /* allocates space for the error return value to be used in error checking for function calls */ ERROR_CODE return_value; /* allocates space for the pointer to the passwd key value structure to be created by parsing the auth file */ struct hash_map_t *passwd; /* allocates space to the various pointer values to be used for the separation and treatment of the auth value */ char *pointer; char *authorization_b64; char *authorization_d; char *password_pointer; /* allocates space for the buffers to be used for the username and password values extracted from the authorization token */ char username[128]; char password[128]; char *password_v; /* allocates the various size relates values for the buffer variables creation */ size_t authorization_size; size_t username_size; size_t password_size; /* tries to find the token that separates the authentication type from the authorization base 64 value in case the value is not found raises an error indicating the problem */ pointer = strchr(authorization, ' '); if(pointer == NULL) { RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "Authorization value not valid" ); } authorization_b64 = pointer + 1; /* tries to decode the authorization base 64 value into a plain text value in case the decoding fails, re-raises the error to the upper levels for caller information */ return_value = decode_base64( (unsigned char *) authorization_b64, strlen(authorization_b64), (unsigned char **) &authorization_d, &authorization_size ); if(IS_ERROR_CODE(return_value)) { RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "Problem decoding base 64 authorization" ); } /* tries to find the token that separates the username part of the authorization from the password part in case the value is not found raises an error to the upper levels */ pointer = memchr(authorization_d, ':', authorization_size); if(pointer == NULL) { FREE(authorization_d); RAISE_ERROR_M( RUNTIME_EXCEPTION_ERROR_CODE, (unsigned char *) "No password separator found in authorization" ); } password_pointer = pointer + 1; /* calculates the size of both the username and the password from the diference between the various pointers */ username_size = password_pointer - authorization_d - 1; password_size = authorization_d + authorization_size - password_pointer; /* copies both the username and the password values to the apropriate internal buffers (to be used in comparision) */ memcpy(username, authorization_d, username_size); username[username_size] = '\0'; memcpy(password, password_pointer, password_size); password[password_size] = '\0'; /* processes the passwd file using the provided file path for it, this is an expensive io driven operation, and must be used wth care */ process_passwd_file(auth_file, &passwd); /* retrieves the password verification value for the retrieved username and in case it's valid compares it and sets the result value accordingly */ get_value_string_hash_map( passwd, (unsigned char *) username, (void **) &password_v ); if(password_v != NULL && strcmp(password, password_v) == 0) { *result = TRUE; } else { *result = FALSE; } /* releases the memory associated with the complete set of values in the passwd structure and then releases the memory from the hash map structure itself, then releases the memory associated with the authorization decoded string */ delete_values_hash_map(passwd); delete_hash_map(passwd); FREE(authorization_d); /* raises no error, as everything has been done as possible with no problems created in the processing */ RAISE_NO_ERROR; }