| | |
| | | #include <string.h> |
| | | #include <stdbool.h> |
| | | #include "base/base64.h" |
| | | #ifdef __linux__ |
| | | #include <valgrind/memcheck.h> |
| | | #endif |
| | | |
| | | static FUNCTION_RETURN generate_default_pc_id(PcIdentifier * identifiers, |
| | | unsigned int * num_identifiers) { |
| | | size_t adapter_num, disk_num, plat_spec_id; |
| | | FUNCTION_RETURN result_adapterInfos, result_diskinfos, result_plat_spec; |
| | | unsigned int required_id_size, current_identifier, i, j, k; |
| | | size_t adapter_num, disk_num; |
| | | FUNCTION_RETURN result_adapterInfos, result_diskinfos; |
| | | unsigned int required_id_size, i, j, k; |
| | | DiskInfo * diskInfos; |
| | | AdapterInfo *adapterInfos; |
| | | required_id_size = 0; |
| | | OsAdapterInfo *adapterInfos; |
| | | |
| | | //just calculate the number of required identifiers |
| | | result_plat_spec = generate_platform_specific_pc_id(NULL, &plat_spec_id); |
| | | if (result_plat_spec == OK) { |
| | | required_id_size += 1; |
| | | } |
| | | result_adapterInfos = getAdapterInfos(NULL, &adapter_num); |
| | | if (result_adapterInfos != FUNC_RET_OK) { |
| | | //call generate_disk_pc_id; |
| | | return result_adapterInfos; |
| | | } |
| | | result_diskinfos = getDiskInfos(NULL, &disk_num); |
| | | if (result_diskinfos == OK && result_adapterInfos == OK) { |
| | | required_id_size += disk_num * adapter_num; |
| | | } else if (result_adapterInfos == OK) { |
| | | required_id_size += adapter_num; |
| | | } else if (result_diskinfos == OK) { |
| | | required_id_size += disk_num; |
| | | if (result_diskinfos == FUNC_RET_OK) { |
| | | required_id_size = disk_num * adapter_num; |
| | | } else { |
| | | required_id_size = disk_num; |
| | | } |
| | | int defined_identifiers = *num_identifiers; |
| | | *num_identifiers = required_id_size; |
| | | if (identifiers == NULL) { |
| | | *num_identifiers = required_id_size; |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } else if (required_id_size > defined_identifiers) { |
| | | return BUFFER_TOO_SMALL; |
| | | return FUNC_RET_BUFFER_TOO_SMALL; |
| | | } |
| | | |
| | | //calculate the identifiers |
| | | current_identifier = 0; |
| | | if (result_plat_spec == OK) { |
| | | generate_platform_specific_pc_id(identifiers, 1); |
| | | current_identifier += 1; |
| | | } |
| | | if (result_diskinfos == OK && result_adapterInfos == OK) { |
| | | diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo)); |
| | | result_diskinfos = getDiskInfos(diskInfos, &disk_num); |
| | | adapterInfos = (AdapterInfo*) malloc(adapter_num * sizeof(AdapterInfo)); |
| | | result_adapterInfos = getAdapterInfos(adapterInfos, &adapter_num); |
| | | for (i = 0; i < disk_num; i++) { |
| | | for (j = 0; j < adapter_num; j++) { |
| | | if (current_identifier > defined_identifiers) { |
| | | break; |
| | | } |
| | | for (k = 0; k < 6; k++) { |
| | | identifiers[current_identifier][k] = diskInfos[i].disk_sn[k |
| | | + 2] ^ adapterInfos[j].mac_address[k + 2]; |
| | | } |
| | | encodeStrategy(identifiers[current_identifier], DEFAULT); |
| | | current_identifier++; |
| | | } |
| | | diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo)); |
| | | result_diskinfos = getDiskInfos(diskInfos, &disk_num); |
| | | adapterInfos = (OsAdapterInfo*) malloc(adapter_num * sizeof(OsAdapterInfo)); |
| | | result_adapterInfos = getAdapterInfos(adapterInfos, &adapter_num); |
| | | for (i = 0; i < disk_num; i++) { |
| | | for (j = 0; j < adapter_num; j++) { |
| | | for (k = 0; k < 6; k++) |
| | | identifiers[i * adapter_num + j][k] = |
| | | diskInfos[i].disk_sn[k + 2] |
| | | ^ adapterInfos[j].mac_address[k + 2]; |
| | | } |
| | | free(diskInfos); |
| | | free(adapterInfos); |
| | | } else if (result_adapterInfos == OK) { |
| | | return generate_ethernet_pc_id(&identifiers[current_identifier], |
| | | defined_identifiers-current_identifier, true); |
| | | } else if (result_diskinfos == OK) { |
| | | return generate_disk_pc_id(&identifiers[current_identifier], |
| | | defined_identifiers-current_identifier, false); |
| | | } |
| | | |
| | | return OK; |
| | | } |
| | | |
| | | static void encodeStrategy(PcIdentifier * identifier, |
| | | IDENTIFICATION_STRATEGY strategy) { |
| | | unsigned char strategy_num = strategy << 5; |
| | | identifier[0] = (identifier[0] & 15) | strategy_num; |
| | | |
| | | } |
| | | |
| | | static FUNCTION_RETURN generate_platform_specific_pc_id( |
| | | PcIdentifier * identifiers, unsigned int * num_identifiers) { |
| | | |
| | | free(diskInfos); |
| | | free(adapterInfos); |
| | | return FUNC_RET_OK; |
| | | } |
| | | |
| | | static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier * identifiers, |
| | | unsigned int * num_identifiers, bool use_mac) { |
| | | size_t adapters; |
| | | FUNCTION_RETURN result_adapterInfos; |
| | | unsigned int j, k; |
| | | AdapterInfo *adapterInfos; |
| | | unsigned int i, j, k; |
| | | OsAdapterInfo *adapterInfos; |
| | | |
| | | result_adapterInfos = getAdapterInfos(NULL, &adapters); |
| | | if (result_adapterInfos != OK) { |
| | | if (result_adapterInfos != FUNC_RET_OK) { |
| | | return result_adapterInfos; |
| | | } |
| | | |
| | | int defined_adapters = *num_identifiers; |
| | | *num_identifiers = adapters; |
| | | if (identifiers == NULL) { |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } else if (adapters > defined_adapters) { |
| | | return BUFFER_TOO_SMALL; |
| | | return FUNC_RET_BUFFER_TOO_SMALL; |
| | | } |
| | | |
| | | adapterInfos = (AdapterInfo*) malloc(adapters * sizeof(AdapterInfo)); |
| | | adapterInfos = (OsAdapterInfo*)malloc(adapters * sizeof(OsAdapterInfo)); |
| | | result_adapterInfos = getAdapterInfos(adapterInfos, &adapters); |
| | | for (j = 0; j < adapters; j++) { |
| | | for (k = 0; k < 6; k++) |
| | |
| | | } |
| | | } |
| | | free(adapterInfos); |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } |
| | | |
| | | static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier * identifiers, |
| | | unsigned int * num_identifiers, bool use_label) { |
| | | size_t disk_num, available_disk_info = 0; |
| | | size_t disk_num, available_disk_info=0; |
| | | FUNCTION_RETURN result_diskinfos; |
| | | unsigned int i, j; |
| | | unsigned int i, k, j; |
| | | char firstChar; |
| | | DiskInfo * diskInfos; |
| | | |
| | | result_diskinfos = getDiskInfos(NULL, &disk_num); |
| | | if (result_diskinfos != OK) { |
| | | if (result_diskinfos != FUNC_RET_OK) { |
| | | return result_diskinfos; |
| | | } |
| | | diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo)); |
| | | //memset(diskInfos,0,disk_num * sizeof(DiskInfo)); |
| | | //memset(diskInfos,0,disk_num * sizeof(DiskInfo)); |
| | | result_diskinfos = getDiskInfos(diskInfos, &disk_num); |
| | | if (result_diskinfos != OK) { |
| | | if (result_diskinfos != FUNC_RET_OK) { |
| | | free(diskInfos); |
| | | return result_diskinfos; |
| | | } |
| | |
| | | *num_identifiers = available_disk_info; |
| | | if (identifiers == NULL) { |
| | | free(diskInfos); |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } else if (available_disk_info > defined_identifiers) { |
| | | free(diskInfos); |
| | | return BUFFER_TOO_SMALL; |
| | | return FUNC_RET_BUFFER_TOO_SMALL; |
| | | } |
| | | |
| | | j = 0; |
| | | j=0; |
| | | for (i = 0; i < disk_num; i++) { |
| | | if (use_label) { |
| | | if (diskInfos[i].label[0] != 0) { |
| | | memset(identifiers[j], 0, sizeof(PcIdentifier)); //!!!!!!! |
| | | strncpy(identifiers[j], diskInfos[i].label, |
| | | sizeof(PcIdentifier)); |
| | | if(use_label){ |
| | | if(diskInfos[i].label[0]!=0){ |
| | | memset(identifiers[j],0,sizeof(PcIdentifier)); //!!!!!!! |
| | | strncpy(identifiers[j],diskInfos[i].label,sizeof(PcIdentifier)); |
| | | j++; |
| | | } |
| | | } else { |
| | | if (diskInfos[i].disk_sn[0] != 0) { |
| | | memcpy(identifiers[j], &diskInfos[i].disk_sn[2], |
| | | sizeof(PcIdentifier)); |
| | | }else{ |
| | | if(diskInfos[i].disk_sn[0]!=0){ |
| | | memcpy(identifiers[j],&diskInfos[i].disk_sn[2],sizeof(PcIdentifier)); |
| | | j++; |
| | | } |
| | | } |
| | | } |
| | | free(diskInfos); |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } |
| | | |
| | | /** |
| | |
| | | case DISK_NUM: |
| | | result = generate_disk_pc_id(identifiers, array_size, false); |
| | | break; |
| | | case PLATFORM_SPECIFIC: |
| | | result = generate_platform_specific_pc_id(identifiers, array_size); |
| | | case DISK_LABEL: |
| | | result = generate_disk_pc_id(identifiers, array_size, true); |
| | | break; |
| | | default: |
| | | return ERROR; |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | |
| | | if (result == OK && identifiers != NULL) { |
| | | if (result == FUNC_RET_OK && identifiers != NULL) { |
| | | strategy_num = strategy << 5; |
| | | for (i = 0; i < *array_size; i++) { |
| | | //encode strategy in the first three bits of the pc_identifier |
| | | identifiers[i][0] = (identifiers[i][0] & 15) | strategy_num; |
| | | } |
| | | //fill array if larger |
| | | for (i = *array_size; i < original_array_size; i++) { |
| | | identifiers[i][0] = STRATEGY_UNKNOWN << 5; |
| | |
| | | char* b64_data = base64(concat_identifiers, concatIdentifiersSize, |
| | | &b64_size); |
| | | if (b64_size > sizeof(PcSignature)) { |
| | | return BUFFER_TOO_SMALL; |
| | | return FUNC_RET_BUFFER_TOO_SMALL; |
| | | } |
| | | sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0], |
| | | &b64_data[4], &b64_data[8], &b64_data[12]); |
| | | //free(concat_identifiers); |
| | | free(b64_data); |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } |
| | | |
| | | FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) { |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } |
| | | |
| | | FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out, |
| | |
| | | PcIdentifier* identifiers; |
| | | unsigned int req_buffer_size = 0; |
| | | result = generate_pc_id(NULL, &req_buffer_size, strategy); |
| | | if (result != OK) { |
| | | if (result != FUNC_RET_OK) { |
| | | return result; |
| | | } |
| | | if (req_buffer_size == 0) { |
| | | return ERROR; |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | req_buffer_size = req_buffer_size < 2 ? 2 : req_buffer_size; |
| | | identifiers = (PcIdentifier *) malloc( |
| | | sizeof(PcIdentifier) * req_buffer_size); |
| | | result = generate_pc_id(identifiers, &req_buffer_size, strategy); |
| | | if (result != OK) { |
| | | if (result != FUNC_RET_OK) { |
| | | free(identifiers); |
| | | return result; |
| | | } |
| | | #ifdef __linux__ |
| | | VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[0]); |
| | | VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[1]); |
| | | #endif |
| | | result = encode_pc_id(identifiers[0], identifiers[1], identifier_out); |
| | | #ifdef __linux__ |
| | | VALGRIND_CHECK_VALUE_IS_DEFINED(identifier_out); |
| | | #endif |
| | | free(identifiers); |
| | | return result; |
| | | } |
| | |
| | | &base64ids[8], &base64ids[12]); |
| | | concat_identifiers = unbase64(base64ids, 16, &identifiers_size); |
| | | if (identifiers_size > sizeof(PcIdentifier) * 2) { |
| | | return BUFFER_TOO_SMALL; |
| | | return FUNC_RET_BUFFER_TOO_SMALL; |
| | | } |
| | | memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier)); |
| | | memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier), |
| | | sizeof(PcIdentifier)); |
| | | free(concat_identifiers); |
| | | return OK; |
| | | return FUNC_RET_OK; |
| | | } |
| | | |
| | | static IDENTIFICATION_STRATEGY strategy_from_pc_id(PcIdentifier identifier) { |
| | |
| | | printf("Comparing pc identifiers: \n"); |
| | | #endif |
| | | result = decode_pc_id(user_identifiers[0], user_identifiers[1], str_code); |
| | | if (result != OK) { |
| | | if (result != FUNC_RET_OK) { |
| | | return result; |
| | | } |
| | | previous_strategy_id = STRATEGY_UNKNOWN; |