static size_t hash_perm_state_size_class(const qpol_iterator_t * iter) { perm_hash_state_t *hs = NULL; uint32_t tmp_bucket = 0; size_t count = 0; hashtab_node_t *tmp_node; sepol_policydb_t sp; qpol_policy_t qp; qpol_iterator_t *internal_perms; class_datum_t *internal_class; char *tmp = NULL; if (iter == NULL || qpol_iterator_state(iter) == NULL) { errno = EINVAL; return STATUS_ERR; } hs = (perm_hash_state_t *) qpol_iterator_state(iter); if (hs == NULL) { errno = EINVAL; return STATUS_ERR; } /* shallow copy ok here as only internal values are used */ sp.p = *qpol_iterator_policy(iter); qp.p = &sp; qp.fn = NULL; if (&sp.p == NULL) { errno = EINVAL; return STATUS_ERR; } for (tmp_bucket = 0; tmp_bucket < (*(hs->table))->size; tmp_bucket++) { for (tmp_node = (*(hs->table))->htable[tmp_bucket]; tmp_node; tmp_node = tmp_node->next) { internal_class = tmp_node ? ((class_datum_t *) tmp_node->datum) : NULL; qpol_class_get_perm_iter(&qp, (qpol_class_t *) internal_class, &internal_perms); for (; !qpol_iterator_end(internal_perms); qpol_iterator_next(internal_perms)) { qpol_iterator_get_item(internal_perms, (void **)&tmp); if (!strcmp(tmp, hs->perm_name)) { count++; break; } } qpol_iterator_destroy(&internal_perms); } } return count; }
static int hash_state_next_class_w_perm(qpol_iterator_t * iter) { class_datum_t *internal_class = NULL; qpol_iterator_t *internal_perms = NULL; unsigned char has_perm = 0; perm_hash_state_t *hs = NULL; sepol_policydb_t sp; qpol_policy_t qp; char *tmp = NULL; hs = (perm_hash_state_t *) qpol_iterator_state(iter); if (hs == NULL) { errno = EINVAL; return STATUS_ERR; } if (hs->bucket >= (*(hs->table))->size) { errno = ERANGE; return STATUS_ERR; } /* shallow copy ok here as only internal values are used */ sp.p = *qpol_iterator_policy(iter); qp.p = &sp; qp.fn = NULL; do { hash_state_next(iter); if (hash_state_end(iter)) break; internal_class = hs->node ? (class_datum_t *) hs->node->datum : NULL; qpol_class_get_perm_iter(&qp, (qpol_class_t *) internal_class, &internal_perms); for (; !qpol_iterator_end(internal_perms); qpol_iterator_next(internal_perms)) { qpol_iterator_get_item(internal_perms, (void **)&tmp); if (!strcmp(tmp, hs->perm_name)) { has_perm = 1; break; } } qpol_iterator_destroy(&internal_perms); } while (!has_perm && !hash_state_end(iter)); return STATUS_SUCCESS; }
/** * Prints a textual representation of an object class and possibly * all of that object class' permissions. * * @param fp Reference to a file to which to print object class information * @param type_datum Reference to sepol type_datum * @param policydb Reference to a policy * @param expand Flag indicating whether to print each object class' * permissions */ static PyObject* get_class(const qpol_class_t * class_datum, const apol_policy_t * policydb) { const char *class_name = NULL, *perm_name = NULL; qpol_iterator_t *iter = NULL; const qpol_common_t *common_datum = NULL; qpol_policy_t *q = apol_policy_get_qpol(policydb); int error = 0; int rt; PyObject *list = NULL; PyObject *dict = PyDict_New(); if (!dict) goto err; if (!class_datum) goto err; if (qpol_class_get_name(q, class_datum, &class_name)) goto err; if (py_insert_string(dict, "name", class_name)) goto err; /* get commons for this class */ if (qpol_class_get_common(q, class_datum, &common_datum)) goto err; list = PyList_New(0); if (!list) goto err; if (common_datum) { if (qpol_common_get_perm_iter(q, common_datum, &iter)) goto err; /* print perms for the common */ for (; !qpol_iterator_end(iter); qpol_iterator_next(iter)) { if (qpol_iterator_get_item(iter, (void **)&perm_name)) goto err; if (py_append_string(list, perm_name)) goto err; } } /* print unique perms for this class */ if (qpol_class_get_perm_iter(q, class_datum, &iter)) goto err; for (; !qpol_iterator_end(iter); qpol_iterator_next(iter)) { if (qpol_iterator_get_item(iter, (void **)&perm_name)) goto err; if (py_append_string(list, perm_name)) goto err; } rt = py_insert_obj(dict, "permlist", list); Py_DECREF(list); list = NULL; if (rt) goto err; qpol_iterator_destroy(&iter); goto cleanup; err: error = errno; PyErr_SetString(PyExc_RuntimeError,strerror(errno)); py_decref(list); list=NULL; py_decref(dict); dict=NULL; cleanup: errno = error; qpol_iterator_destroy(&iter); return dict; }