mlsrule_query.c

Go to the documentation of this file.
00001 /**
00002  *  @file
00003  *  Implementation for the public interface for searching and iterating over
00004  *  range transition rules.
00005  *
00006  *  @author Kevin Carr kcarr@tresys.com
00007  *  @author Jeremy A. Mowery jmowery@tresys.com
00008  *  @author Jason Tang jtang@tresys.com
00009  *
00010  *  Copyright (C) 2006-2007 Tresys Technology, LLC
00011  *
00012  *  This library is free software; you can redistribute it and/or
00013  *  modify it under the terms of the GNU Lesser General Public
00014  *  License as published by the Free Software Foundation; either
00015  *  version 2.1 of the License, or (at your option) any later version.
00016  *
00017  *  This library is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  *  Lesser General Public License for more details.
00021  *
00022  *  You should have received a copy of the GNU Lesser General Public
00023  *  License along with this library; if not, write to the Free Software
00024  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00025  */
00026 
00027 #include "iterator_internal.h"
00028 #include <qpol/iterator.h>
00029 #include <qpol/policy.h>
00030 #include <qpol/mlsrule_query.h>
00031 #include <sepol/policydb/policydb.h>
00032 #include <sepol/policydb/avtab.h>
00033 #include <sepol/policydb/util.h>
00034 #include <stdlib.h>
00035 #include "qpol_internal.h"
00036 
00037 typedef struct range_trans_state
00038 {
00039         range_trans_t *head;
00040         range_trans_t *cur;
00041 } range_trans_state_t;
00042 
00043 static int range_trans_state_end(const qpol_iterator_t * iter)
00044 {
00045         range_trans_state_t *rs = NULL;
00046 
00047         if (!iter || !(rs = qpol_iterator_state(iter))) {
00048                 errno = EINVAL;
00049                 return STATUS_ERR;
00050         }
00051 
00052         return rs->cur ? 0 : 1;
00053 }
00054 
00055 static void *range_trans_state_get_cur(const qpol_iterator_t * iter)
00056 {
00057         range_trans_state_t *rs = NULL;
00058 
00059         if (!iter || !(rs = qpol_iterator_state(iter))) {
00060                 errno = EINVAL;
00061                 return NULL;
00062         }
00063 
00064         return rs->cur;
00065 }
00066 
00067 static int range_trans_state_next(qpol_iterator_t * iter)
00068 {
00069         range_trans_state_t *rs = NULL;
00070 
00071         if (!iter || !(rs = qpol_iterator_state(iter))) {
00072                 errno = EINVAL;
00073                 return STATUS_ERR;
00074         }
00075 
00076         if (range_trans_state_end(iter)) {
00077                 errno = EINVAL;
00078                 return STATUS_ERR;
00079         }
00080 
00081         rs->cur = rs->cur->next;
00082 
00083         return STATUS_SUCCESS;
00084 }
00085 
00086 static size_t range_trans_state_size(const qpol_iterator_t * iter)
00087 {
00088         range_trans_state_t *rs = NULL;
00089         size_t count = 0;
00090         range_trans_t *tmp = NULL;
00091 
00092         if (!iter || !(rs = qpol_iterator_state(iter))) {
00093                 errno = EINVAL;
00094                 return 0;
00095         }
00096 
00097         for (tmp = rs->head; tmp; tmp = tmp->next)
00098                 count++;
00099 
00100         return count;
00101 }
00102 
00103 int qpol_policy_get_range_trans_iter(const qpol_policy_t * policy, qpol_iterator_t ** iter)
00104 {
00105         policydb_t *db = NULL;
00106         range_trans_state_t *rs = NULL;
00107         int error = 0;
00108 
00109         if (iter)
00110                 *iter = NULL;
00111 
00112         if (!policy || !iter) {
00113                 ERR(policy, "%s", strerror(EINVAL));
00114                 errno = EINVAL;
00115                 return STATUS_ERR;
00116         }
00117 
00118         db = &policy->p->p;
00119 
00120         rs = calloc(1, sizeof(range_trans_state_t));
00121         if (!rs) {
00122                 error = errno;
00123                 ERR(policy, "%s", strerror(error));
00124                 errno = error;
00125                 return STATUS_ERR;
00126         }
00127 
00128         if (qpol_iterator_create(policy, (void *)rs, range_trans_state_get_cur,
00129                                  range_trans_state_next, range_trans_state_end, range_trans_state_size, free, iter)) {
00130                 error = errno;
00131                 free(rs);
00132                 errno = error;
00133                 return STATUS_ERR;
00134         }
00135 
00136         rs->head = rs->cur = db->range_tr;
00137         return STATUS_SUCCESS;
00138 }
00139 
00140 int qpol_range_trans_get_source_type(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_type_t ** source)
00141 {
00142         policydb_t *db = NULL;
00143         range_trans_t *rt = NULL;
00144 
00145         if (source) {
00146                 *source = NULL;
00147         }
00148 
00149         if (!policy || !rule || !source) {
00150                 errno = EINVAL;
00151                 ERR(policy, "%s", strerror(EINVAL));
00152                 return STATUS_ERR;
00153         }
00154 
00155         db = &policy->p->p;
00156         rt = (range_trans_t *) rule;
00157 
00158         *source = (qpol_type_t *) db->type_val_to_struct[rt->source_type - 1];
00159 
00160         return STATUS_SUCCESS;
00161 }
00162 
00163 int qpol_range_trans_get_target_type(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_type_t ** target)
00164 {
00165         policydb_t *db = NULL;
00166         range_trans_t *rt = NULL;
00167 
00168         if (target) {
00169                 *target = NULL;
00170         }
00171 
00172         if (!policy || !rule || !target) {
00173                 ERR(policy, "%s", strerror(EINVAL));
00174                 errno = EINVAL;
00175                 return STATUS_ERR;
00176         }
00177 
00178         db = &policy->p->p;
00179         rt = (range_trans_t *) rule;
00180 
00181         *target = (qpol_type_t *) db->type_val_to_struct[rt->target_type - 1];
00182 
00183         return STATUS_SUCCESS;
00184 }
00185 
00186 int qpol_range_trans_get_target_class(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_class_t ** target)
00187 {
00188         policydb_t *db = NULL;
00189         range_trans_t *rt = NULL;
00190 
00191         if (target) {
00192                 *target = NULL;
00193         }
00194 
00195         if (!policy || !rule || !target) {
00196                 ERR(policy, "%s", strerror(EINVAL));
00197                 errno = EINVAL;
00198                 return STATUS_ERR;
00199         }
00200 
00201         db = &policy->p->p;
00202         rt = (range_trans_t *) rule;
00203 
00204         *target = (qpol_class_t *) db->class_val_to_struct[rt->target_class - 1];
00205 
00206         return STATUS_SUCCESS;
00207 }
00208 
00209 int qpol_range_trans_get_range(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_mls_range_t ** range)
00210 {
00211         policydb_t *db = NULL;
00212         range_trans_t *rt = NULL;
00213 
00214         if (range) {
00215                 *range = NULL;
00216         }
00217 
00218         if (!policy || !rule || !range) {
00219                 ERR(policy, "%s", strerror(EINVAL));
00220                 errno = EINVAL;
00221                 return STATUS_ERR;
00222         }
00223 
00224         db = &policy->p->p;
00225         rt = (range_trans_t *) rule;
00226 
00227         *range = (qpol_mls_range_t *) & rt->target_range;
00228 
00229         return STATUS_SUCCESS;
00230 }