struct oval_iterator *oval_collection_iterator(struct oval_collection *collection) { __attribute__nonnull__(collection); struct oval_iterator *iterator = (struct oval_iterator *)oscap_alloc(sizeof(oval_iterator_t)); if (iterator == NULL) return NULL; if ((iterator_count++) < 0) { _debugStack[iterator_count - 1] = iterator; oscap_dlprintf(DBG_W, "iterator_count: %d.\n", iterator_count); } iterator->item_iterator_frame = NULL; struct _oval_collection_item_frame *collection_frame = collection->item_collection_frame; while (collection_frame != NULL) { struct _oval_collection_item_frame *iterator_frame = (struct _oval_collection_item_frame *)oscap_alloc(sizeof(_oval_collection_item_frame_t)); if (iterator_frame == NULL) return NULL; iterator_frame->next = iterator->item_iterator_frame; iterator_frame->item = collection_frame->item; iterator->item_iterator_frame = iterator_frame; collection_frame = collection_frame->next; } return iterator; }
static int get_substrings(char *str, int *ofs, pcre *re, int want_substrs, char ***substrings) { int i, ret, rc; int ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]); char **substrs; // todo: max match count check for (i = 0; i < ovector_len; ++i) ovector[i] = -1; #if defined(__SVR4) && defined(__sun) rc = pcre_exec(re, NULL, str, strlen(str), *ofs, PCRE_NO_UTF8_CHECK, ovector, ovector_len); #else rc = pcre_exec(re, NULL, str, strlen(str), *ofs, 0, ovector, ovector_len); #endif if (rc < -1) { dE("Function pcre_exec() failed to match a regular expression with return code %d on string '%s'.", rc, str); return rc; } else if (rc == -1) { /* no match */ return 0; } *ofs = (*ofs == ovector[1]) ? ovector[1] + 1 : ovector[1]; if (!want_substrs) { /* just report successful match */ return 1; } ret = 0; if (rc == 0) { /* vector too small */ // todo: report partial results rc = ovector_len / 3; } substrs = oscap_alloc(rc * sizeof (char *)); for (i = 0; i < rc; ++i) { int len; char *buf; if (ovector[2 * i] == -1) continue; len = ovector[2 * i + 1] - ovector[2 * i]; buf = oscap_alloc(len + 1); memcpy(buf, str + ovector[2 * i], len); buf[len] = '\0'; substrs[ret] = buf; ++ret; } *substrings = substrs; return ret; }
static int get_substrings(char *str, pcre *re, int want_substrs, char ***substrings) { int i, ret, rc; int ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]); // todo: max match count check for (i = 0; i < ovector_len; ++i) ovector[i] = -1; rc = pcre_exec(re, NULL, str, strlen(str), 0, 0, ovector, ovector_len); if (rc < -1) { return -1; } else if (rc == -1) { /* no match */ return 0; } else if(!want_substrs) { /* just report successful match */ return 1; } char **substrs; ret = 0; if (rc == 0) { /* vector too small */ rc = ovector_len / 3; } substrs = oscap_alloc(rc * sizeof (char *)); for (i = 0; i < rc; ++i) { int len; char *buf; if (ovector[2 * i] == -1) continue; len = ovector[2 * i + 1] - ovector[2 * i]; buf = oscap_alloc(len + 1); memcpy(buf, str + ovector[2 * i], len); buf[len] = '\0'; substrs[ret] = buf; ++ret; } /* if (ret < rc) substrs = realloc(substrs, ret * sizeof (char *)); */ *substrings = substrs; return ret; }
void oval_string_map_put(struct oval_string_map *map, const char *key, void *item) { __attribute__nonnull__(map); char *temp = (char *)oscap_alloc((strlen(key) + 1) * sizeof(char) + 1); char *usekey = strcpy(temp, key); /* SEARCH FOR INSERTION POINT */ struct _oval_string_map_entry *insert_before = map->entries, *insert_after = NULL, *insertion; if (insert_before == NULL) { map->entries = insertion = _oval_string_map_entry_new(NULL, NULL); } else { int compare; while (insert_before != NULL && ((compare = strcmp(usekey, insert_before->key)) < 0)) { insert_after = insert_before; insert_before = insert_after->next; } if (insert_before == NULL) { insertion = _oval_string_map_entry_new(insert_after, NULL); } else if (compare == 0) { insertion = insert_before; } else { insertion = _oval_string_map_entry_new(insert_after, insert_before); if (insert_after == NULL) map->entries = insertion; } } if (insertion == NULL) { free(temp); return; } insertion->key = usekey; insertion->item = item; }
struct oval_test *oval_test_new(struct oval_definition_model *model, const char *id) { __attribute__nonnull__(model); oval_test_t *test; test = (oval_test_t *) oscap_alloc(sizeof(oval_test_t)); if (test == NULL) return NULL; test->deprecated = 0; test->version = 0; test->check = OVAL_CHECK_UNKNOWN; test->existence = OVAL_EXISTENCE_UNKNOWN; test->state_operator = OVAL_OPERATOR_AND; test->subtype = OVAL_SUBTYPE_UNKNOWN; test->comment = NULL; test->id = oscap_strdup(id); test->object = NULL; test->states = oval_collection_new(); test->notes = oval_collection_new(); test->model = model; oval_definition_model_add_test(model, test); return test; }
const char *oval_schema_version_to_cstr(oval_schema_version_t version) { size_t buf_len = 32; char *buf = oscap_alloc(buf_len); const char *format; if (version.component[OVAL_SCHEMA_VERSION_PLATFORM_MAJOR]) { if (version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE] && version.component[OVAL_SCHEMA_VERSION_PLATFORM_UPDATE]) { format = "%1$d.%2$d.%3$d:%4$d.%5$d.%6$d"; } else if (version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE]) { format = "%1$d.%2$d.%3$d:%4$d.%5$d"; } else if (version.component[OVAL_SCHEMA_VERSION_PLATFORM_UPDATE]) { format = "%1$d.%2$d:%4$d.%5$d.%6$d"; } else { format = "%1$d.%2$d:%4$d.%5$d"; } } else if (version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE]) { format = "%1$d.%2$d.%3$d"; } else { format = "%1$d.%2$d"; } snprintf(buf, buf_len, format, version.component[OVAL_SCHEMA_VERSION_CORE_MAJOR], version.component[OVAL_SCHEMA_VERSION_CORE_MINOR], version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE], version.component[OVAL_SCHEMA_VERSION_PLATFORM_MAJOR], version.component[OVAL_SCHEMA_VERSION_PLATFORM_MINOR], version.component[OVAL_SCHEMA_VERSION_PLATFORM_UPDATE]); return buf; }
// takes given relative filepath and mangles it so that it's acceptable // as a component id static char* ds_sds_mangle_filepath(const char* filepath) { if (filepath == NULL) return NULL; // the string will grow 2x the size in the worst case (every char is /) // TODO: We can do better than this by counting the slashes char* ret = oscap_alloc(strlen(filepath) * sizeof(char) * 2); const char* src_it = filepath; char* dst_it = ret; while (*src_it) { if (*src_it == '/') { *dst_it++ = '-'; *dst_it++ = '-'; } else { *dst_it++ = *src_it; } src_it++; } *dst_it = '\0'; return ret; }
static inline int ds_sds_compose_component_add_script_content(xmlNode *component, const char *filepath) { FILE* f = fopen(filepath, "r"); if (!f) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "Can't read plain text from file '%s'.", filepath); return -1; } fseek(f, 0, SEEK_END); long int length = ftell(f); fseek(f, 0, SEEK_SET); if (length >= 0) { char* buffer = oscap_alloc((length + 1) * sizeof(char)); if (fread(buffer, length, 1, f) != 1) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "Error while reading from file '%s'.", filepath); fclose(f); oscap_free(buffer); return -1; } fclose(f); buffer[length] = '\0'; xmlNsPtr local_ns = xmlNewNs(component, BAD_CAST sce_xccdf_ns_uri, BAD_CAST "oscap-sce-xccdf-stream"); xmlNewTextChild(component, local_ns, BAD_CAST "script", BAD_CAST buffer); oscap_free(buffer); return 0; } else { oscap_seterr(OSCAP_EFAMILY_GLIBC, "No data read from file '%s'.", filepath); fclose(f); return -1; } }
struct oval_record_field *oval_record_field_clone(struct oval_record_field *old_rf) { struct oval_record_field *new_rf; switch (old_rf->record_field_type) { case OVAL_RECORD_FIELD_STATE: { struct oval_record_field_STATE *new_rfs, *old_rfs; new_rfs = oscap_alloc(sizeof(*new_rfs)); if (new_rfs == NULL) return NULL; old_rfs = (struct oval_record_field_STATE *) old_rf; new_rfs->operation = old_rfs->operation; new_rfs->variable = old_rfs->variable; new_rfs->var_check = old_rfs->var_check; new_rfs->ent_check = old_rfs->ent_check; new_rf = (struct oval_record_field *) new_rfs; break; } case OVAL_RECORD_FIELD_ITEM: { struct oval_record_field_ITEM *new_rfi, *old_rfi; new_rfi = oscap_alloc(sizeof(*new_rfi)); if (new_rfi == NULL) return NULL; old_rfi = (struct oval_record_field_ITEM *) old_rf; new_rfi->status = old_rfi->status; new_rf = (struct oval_record_field *) new_rfi; break; } default: dE("Unsupported record field type: %d.\n", old_rf->record_field_type); return NULL; } new_rf->record_field_type = old_rf->record_field_type; new_rf->name = oscap_strdup(old_rf->name); new_rf->value = oscap_strdup(old_rf->value); new_rf->datatype = old_rf->datatype; new_rf->mask = old_rf->mask; return new_rf; }
struct oval_record_field *oval_record_field_new(oval_record_field_type_t type) { struct oval_record_field *rf; switch (type) { case OVAL_RECORD_FIELD_STATE: { struct oval_record_field_STATE *rfs; rfs = oscap_alloc(sizeof(*rfs)); if (rfs == NULL) return NULL; rfs->operation = OVAL_OPERATION_UNKNOWN; rfs->variable = NULL; rfs->var_check = OVAL_CHECK_UNKNOWN; rfs->ent_check = OVAL_CHECK_UNKNOWN; rf = (struct oval_record_field *) rfs; break; } case OVAL_RECORD_FIELD_ITEM: { struct oval_record_field_ITEM *rfi; rfi = oscap_alloc(sizeof(*rfi)); if (rfi == NULL) return NULL; rfi->status = SYSCHAR_STATUS_UNKNOWN; rf = (struct oval_record_field *) rfi; break; } default: dE("Unsupported record field type: %d.\n", type); return NULL; } rf->record_field_type = type; rf->name = NULL; rf->value = NULL; rf->datatype = OVAL_DATATYPE_UNKNOWN; rf->mask = 0; return rf; }
/* failed - NULL * success - oval_definition_model * */ struct oval_string_map *oval_string_map_new() { struct oval_string_map *map = (struct oval_string_map *)oscap_alloc(sizeof(oval_string_map_t)); if (map == NULL) return NULL; map->entries = NULL; return map; }
struct oval_collection *oval_collection_new() { struct oval_collection *collection = (struct oval_collection *)oscap_alloc(sizeof(oval_collection_t)); if (collection == NULL) return NULL; collection->item_collection_frame = NULL; return collection; }
struct oval_message *oval_message_new() { oval_message_t *message = (oval_message_t *) oscap_alloc(sizeof(oval_message_t)); if (message == NULL) return NULL; message->text = NULL; message->level = OVAL_MESSAGE_LEVEL_NONE; return message; }
struct oval_value *oval_value_new(oval_datatype_t datatype, char *text_value) { oval_value_t *value = (oval_value_t *) oscap_alloc(sizeof(oval_value_t)); if (value == NULL) return NULL; value->datatype = datatype; value->text = oscap_strdup(text_value); return value; }
struct oval_behavior *oval_behavior_new(struct oval_definition_model *model) { oval_behavior_t *behavior = (oval_behavior_t *) oscap_alloc(sizeof(oval_behavior_t)); if (behavior == NULL) return NULL; behavior->model = model; behavior->value = NULL; behavior->key = NULL; return behavior; }
struct oval_affected *oval_affected_new(struct oval_definition_model *model) { struct oval_affected *affected = (struct oval_affected *)oscap_alloc(sizeof(oval_affected_t)); if (affected == NULL) return NULL; affected->model = model; affected->family = OVAL_AFCFML_UNKNOWN; affected->platforms = oval_collection_new(); affected->products = oval_collection_new(); return affected; }
void oval_collection_add(struct oval_collection *collection, void *item) { __attribute__nonnull__(collection); struct _oval_collection_item_frame *next = oscap_alloc(sizeof(_oval_collection_item_frame_t)); if (next == NULL) return; next->next = collection->item_collection_frame; collection->item_collection_frame = next; next->item = item; }
static int get_substrings(char *str, int *ofs, regex_t *re, int want_substrs, char ***substrings) { int i, ret, rc; regmatch_t pmatch[40]; int pmatch_len = sizeof (pmatch) / sizeof (pmatch[0]); char **substrs; rc = regexec(re, str + *ofs, pmatch_len, pmatch, 0); if (rc == REG_NOMATCH) { /* no match */ return 0; } *ofs += (0 == pmatch[0].rm_eo) ? 1 : pmatch[0].rm_eo; if (!want_substrs) { /* just report successful match */ return 1; } ret = 0; substrs = oscap_alloc(pmatch_len * sizeof (char *)); for (i = 0; i < pmatch_len; ++i) { int len; char *buf; if (pmatch[i].rm_so == -1) continue; len = pmatch[i].rm_eo - pmatch[i].rm_so; buf = oscap_alloc(len + 1); memcpy(buf, str + pmatch[i].rm_so, len); buf[len] = '\0'; substrs[ret] = buf; ++ret; } *substrings = substrs; return ret; }
void oval_collection_iterator_add(struct oval_iterator *iterator, void *item) { __attribute__nonnull__(iterator); struct _oval_collection_item_frame *newframe = (struct _oval_collection_item_frame *)oscap_alloc(sizeof(_oval_collection_item_frame_t)); if (newframe == NULL) /* We don't have any information that error occured ! */ return; newframe->next = iterator->item_iterator_frame; newframe->item = item; iterator->item_iterator_frame = newframe; }
struct oval_iterator *oval_collection_iterator_new() { struct oval_iterator *iterator = (struct oval_iterator *)oscap_alloc(sizeof(oval_iterator_t)); if (iterator == NULL) return NULL; if ((iterator_count++) < 0) { _debugStack[iterator_count - 1] = iterator; oscap_dlprintf(DBG_W, "iterator_count: %d.\n", iterator_count); } iterator->item_iterator_frame = NULL; return iterator; }
struct oval_state_content *oval_state_content_new(struct oval_definition_model *model) { oval_state_content_t *content = (oval_state_content_t *) oscap_alloc(sizeof(oval_state_content_t)); if (content == NULL) return NULL; content->entity = NULL; content->record_fields = oval_collection_new(); content->ent_check = OVAL_CHECK_UNKNOWN; content->var_check = OVAL_CHECK_UNKNOWN; content->check_existence = OVAL_EXISTENCE_UNKNOWN; content->model = model; return content; }
struct cpe_lang_model *cpe_lang_model_new() { struct cpe_lang_model *ret; ret = oscap_alloc(sizeof(struct cpe_lang_model)); if (ret == NULL) return NULL; ret->platforms = oscap_list_new(); ret->item = oscap_htable_new(); ret->origin_file = NULL; return ret; }
struct oval_sysent *oval_sysent_new(struct oval_syschar_model *model) { oval_sysent_t *sysent = (oval_sysent_t *) oscap_alloc(sizeof(oval_sysent_t)); if (sysent == NULL) return NULL; sysent->name = NULL; sysent->value = NULL; sysent->record_fields = NULL; sysent->status = SYSCHAR_STATUS_UNKNOWN; sysent->datatype = OVAL_DATATYPE_UNKNOWN; sysent->mask = 0; sysent->model = model; return sysent; }
static struct _oval_string_map_entry *_oval_string_map_entry_new(struct _oval_string_map_entry *after, struct _oval_string_map_entry *before) { struct _oval_string_map_entry *entry = (struct _oval_string_map_entry *)oscap_alloc(sizeof(_oval_string_map_entry_t)); if (entry == NULL) return NULL; entry->next = before; if (after != NULL) after->next = entry; return entry; }
void oscap_text_consumer(char *text, void *user) { char *platform = *(char **)user; if (platform == NULL) platform = oscap_strdup(text); else { int size = strlen(platform) + strlen(text) + 1; char *newtext = (char *) oscap_alloc(size * sizeof(char)); *newtext = 0; strcat(newtext, platform); strcat(newtext, text); oscap_free(platform); platform = newtext; } *(char **)user = platform; }
struct cpe_platform *cpe_platform_new() { struct cpe_platform *ret; ret = oscap_alloc(sizeof(struct cpe_platform)); if (ret == NULL) return NULL; ret->titles = oscap_list_new(); ret->expr = cpe_testexpr_new(); ret->id = NULL; ret->remark = NULL; return ret; }
struct oval_entity *oval_entity_new(struct oval_definition_model *model) { struct oval_entity *entity = (struct oval_entity *)oscap_alloc(sizeof(struct oval_entity)); if (entity == NULL) return NULL; entity->datatype = OVAL_DATATYPE_UNKNOWN; entity->mask = 0; entity->xsi_nil = 0; entity->operation = OVAL_OPERATION_UNKNOWN; entity->type = OVAL_ENTITY_TYPE_UNKNOWN; entity->name = NULL; entity->value = NULL; entity->variable = NULL; entity->model = model; return entity; }
struct oval_result_definition *oval_result_definition_new(struct oval_result_system *sys, char *definition_id) { oval_result_definition_t *definition = (oval_result_definition_t *) oscap_alloc(sizeof(oval_result_definition_t)); if (definition == NULL) return NULL; definition->system = sys; struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model); definition->definition = oval_definition_model_get_new_definition(definition_model, definition_id); definition->result = OVAL_RESULT_NOT_EVALUATED; definition->criteria = NULL; definition->messages = oval_collection_new(); definition->variable_instance_hint = 1; definition->instance = 1; return definition; }
/* failed - NULL * success - oval_definition_model * */ struct oval_definition_model *oval_definition_model_new() { oval_definition_model_t *newmodel = (oval_definition_model_t *) oscap_alloc(sizeof(oval_definition_model_t)); if (newmodel == NULL) return NULL; newmodel->generator = oval_generator_new(); newmodel->definition_map = oval_string_map_new(); newmodel->object_map = oval_string_map_new(); newmodel->state_map = oval_string_map_new(); newmodel->test_map = oval_string_map_new(); newmodel->variable_map = oval_string_map_new(); newmodel->bound_variable_models = NULL; newmodel->schema = strdup(OVAL_DEF_SCHEMA_LOCATION); newmodel->vardef_map = NULL; return newmodel; }
struct oval_result_test *oval_result_test_new(struct oval_result_system *sys, char *tstid) { oval_result_test_t *test = (oval_result_test_t *) oscap_alloc(sizeof(oval_result_test_t)); if (test == NULL) return NULL; struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model); test->system = sys; test->test = oval_definition_model_get_new_test(definition_model, tstid); test->messages = oval_collection_new(); test->result = OVAL_RESULT_NOT_EVALUATED; test->instance = 1; test->items = oval_collection_new(); test->bindings = oval_collection_new(); test->bindings_initialized = false; return test; }