summaryrefslogtreecommitdiffstats
path: root/libsefs/src/entry.cc
blob: cf9a5e612a127b95250e7bbc4879b397f6a766dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 *  @file
 *  Implementation of the sefs_entry class.
 *
 *  @author Jeremy A. Mowery jmowery@tresys.com
 *  @author Jason Tang jtang@tresys.com
 *
 *  Copyright (C) 2007 Tresys Technology, LLC
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include "sefs_internal.hh"

#include <sefs/entry.hh>
#include <apol/util.h>
#include <qpol/genfscon_query.h>

#include <assert.h>
#include <errno.h>

/******************** public functions below ********************/

sefs_entry::sefs_entry(const sefs_entry * e)
{
	_fclist = e->_fclist;
	_context = e->_context;
	_inode = e->_inode;
	_dev = e->_dev;
	_objectClass = e->_objectClass;
	_path = e->_path;
	_origin = e->_origin;
}

sefs_entry::~sefs_entry()
{
	// do nothing
}

const apol_context_t *sefs_entry::context() const
{
	return _context->context;
}

ino64_t sefs_entry::inode() const
{
	return _inode;
}

const char *sefs_entry::dev() const
{
	return _dev;
}

uint32_t sefs_entry::objectClass() const
{
	return _objectClass;
}

const char *sefs_entry::path() const
{
	return _path;
}

const char *sefs_entry::origin() const
{
	return _origin;
}

char *sefs_entry::toString() const throw(std::bad_alloc)
{
	char *class_str;

	switch (_objectClass)
	{
	case QPOL_CLASS_ALL:
		class_str = "  ";
		break;
	case QPOL_CLASS_BLK_FILE:
		class_str = "-b";
		break;
	case QPOL_CLASS_CHR_FILE:
		class_str = "-c";
		break;
	case QPOL_CLASS_DIR:
		class_str = "-d";
		break;
	case QPOL_CLASS_FIFO_FILE:
		class_str = "-p";
		break;
	case QPOL_CLASS_FILE:
		class_str = "--";
		break;
	case QPOL_CLASS_LNK_FILE:
		class_str = "-l";
		break;
	case QPOL_CLASS_SOCK_FILE:
		class_str = "-s";
		break;
	default:
		// should never get here
		assert(0);
		class_str = "-?";
	}

	char *s = NULL;
	if (asprintf(&s, "%s\t%s\t%s", _path, class_str, _context->context_str) < 0)
	{
		SEFS_ERR(_fclist, "%s", strerror(errno));
		throw std::bad_alloc();
	}
	return s;
}

/******************** private functions below ********************/

sefs_entry::sefs_entry(class sefs_fclist * fclist, const struct sefs_context_node * new_context, uint32_t new_objectClass,
		       const char *new_path, const char *new_origin)
{
	_fclist = fclist;
	_context = new_context;
	_objectClass = new_objectClass;
	_inode = 0;
	_dev = NULL;
	_path = new_path;
	_origin = new_origin;
}

/******************** C functions below ********************/

const apol_context_t *sefs_entry_get_context(const sefs_entry_t * ent)
{
	if (ent == NULL)
	{
		errno = EINVAL;
		return NULL;
	}
	return ent->context();
}

ino64_t sefs_entry_get_inode(const sefs_entry_t * ent)
{
	if (ent == NULL)
	{
		errno = EINVAL;
		return 0;
	}
	return ent->inode();
}

const char *sefs_entry_get_dev(const sefs_entry_t * ent)
{
	if (ent == NULL)
	{
		errno = EINVAL;
		return 0;
	}
	return ent->dev();
}

uint32_t sefs_entry_get_object_class(const sefs_entry_t * ent)
{
	if (ent == NULL)
	{
		errno = EINVAL;
		return QPOL_CLASS_ALL;
	}
	return ent->objectClass();
}

const char *sefs_entry_get_path(const sefs_entry_t * ent)
{
	if (ent == NULL)
	{
		errno = EINVAL;
		return NULL;
	}
	return ent->path();
}

const char *sefs_entry_get_origin(const sefs_entry_t * ent)
{
	if (ent == NULL)
	{
		errno = EINVAL;
		return NULL;
	}
	return ent->origin();
}

char *sefs_entry_to_string(const sefs_entry_t * ent)
{
	if (ent == NULL)
	{
		errno = EINVAL;
		return NULL;
	}
	return ent->toString();
}