1 /* Copyright (C) 2005 Red Hat, Inc. */
2 
3 struct semanage_iface;
4 struct semanage_iface_key;
5 typedef struct semanage_iface record_t;
6 typedef struct semanage_iface_key record_key_t;
7 #define DBASE_RECORD_DEFINED
8 
9 struct dbase_file;
10 typedef struct dbase_file dbase_t;
11 #define DBASE_DEFINED
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <semanage/handle.h>
16 #include "iface_internal.h"
17 #include "context_internal.h"
18 #include "database_file.h"
19 #include "parse_utils.h"
20 #include "debug.h"
21 
iface_print(semanage_handle_t * handle,semanage_iface_t * iface,FILE * str)22 static int iface_print(semanage_handle_t * handle,
23 		       semanage_iface_t * iface, FILE * str)
24 {
25 
26 	char *con_str = NULL;
27 
28 	const char *name = semanage_iface_get_name(iface);
29 	semanage_context_t *ifcon = semanage_iface_get_ifcon(iface);
30 	semanage_context_t *msgcon = semanage_iface_get_msgcon(iface);
31 
32 	if (fprintf(str, "netifcon %s ", name) < 0)
33 		goto err;
34 
35 	if (semanage_context_to_string(handle, ifcon, &con_str) < 0)
36 		goto err;
37 	if (fprintf(str, "%s ", con_str) < 0)
38 		goto err;
39 	free(con_str);
40 	con_str = NULL;
41 
42 	if (semanage_context_to_string(handle, msgcon, &con_str) < 0)
43 		goto err;
44 	if (fprintf(str, "%s\n", con_str) < 0)
45 		goto err;
46 	free(con_str);
47 	con_str = NULL;
48 
49 	return STATUS_SUCCESS;
50 
51       err:
52 	ERR(handle, "could not print interface %s to stream", name);
53 	free(con_str);
54 	return STATUS_ERR;
55 }
56 
iface_parse(semanage_handle_t * handle,parse_info_t * info,semanage_iface_t * iface)57 static int iface_parse(semanage_handle_t * handle,
58 		       parse_info_t * info, semanage_iface_t * iface)
59 {
60 
61 	char *str = NULL;
62 	semanage_context_t *con = NULL;
63 
64 	if (parse_skip_space(handle, info) < 0)
65 		goto err;
66 	if (!info->ptr)
67 		goto last;
68 
69 	/* Header */
70 	if (parse_assert_str(handle, info, "netifcon") < 0)
71 		goto err;
72 	if (parse_assert_space(handle, info) < 0)
73 		goto err;
74 
75 	/* Name */
76 	if (parse_fetch_string(handle, info, &str, ' ') < 0)
77 		goto err;
78 	if (semanage_iface_set_name(handle, iface, str) < 0)
79 		goto err;
80 	free(str);
81 	str = NULL;
82 
83 	/* Interface context */
84 	if (parse_assert_space(handle, info) < 0)
85 		goto err;
86 	if (parse_fetch_string(handle, info, &str, ' ') < 0)
87 		goto err;
88 	if (semanage_context_from_string(handle, str, &con) < 0) {
89 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
90 		    str, info->filename, info->lineno, info->orig_line);
91 		goto err;
92 	}
93 	if (con == NULL) {
94 		ERR(handle, "<<none>> context is not valid for "
95 		    "interfaces (%s: %u)\n%s", info->filename,
96 		    info->lineno, info->orig_line);
97 		goto err;
98 	}
99 	free(str);
100 	str = NULL;
101 
102 	if (semanage_iface_set_ifcon(handle, iface, con) < 0)
103 		goto err;
104 	semanage_context_free(con);
105 	con = NULL;
106 
107 	/* Message context */
108 	if (parse_assert_space(handle, info) < 0)
109 		goto err;
110 	if (parse_fetch_string(handle, info, &str, ' ') < 0)
111 		goto err;
112 	if (semanage_context_from_string(handle, str, &con) < 0) {
113 		ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
114 		    str, info->filename, info->lineno, info->orig_line);
115 		goto err;
116 	}
117 	if (con == NULL) {
118 		ERR(handle, "<<none>> context is not valid for "
119 		    "interfaces (%s: %u)\n%s", info->filename,
120 		    info->lineno, info->orig_line);
121 		goto err;
122 	}
123 	free(str);
124 	str = NULL;
125 
126 	if (semanage_iface_set_msgcon(handle, iface, con) < 0)
127 		goto err;
128 	semanage_context_free(con);
129 	con = NULL;
130 
131 	if (parse_assert_space(handle, info) < 0)
132 		goto err;
133 
134 	return STATUS_SUCCESS;
135 
136       last:
137 	parse_dispose_line(info);
138 	return STATUS_NODATA;
139 
140       err:
141 	ERR(handle, "could not parse interface record");
142 	free(str);
143 	semanage_context_free(con);
144 	parse_dispose_line(info);
145 	return STATUS_ERR;
146 }
147 
148 /* IFACE RECORD: FILE extension: method table */
149 record_file_table_t SEMANAGE_IFACE_FILE_RTABLE = {
150 	.parse = iface_parse,
151 	.print = iface_print,
152 };
153 
iface_file_dbase_init(semanage_handle_t * handle,const char * path_ro,const char * path_rw,dbase_config_t * dconfig)154 int iface_file_dbase_init(semanage_handle_t * handle,
155 			  const char *path_ro,
156 			  const char *path_rw,
157 			  dbase_config_t * dconfig)
158 {
159 
160 	if (dbase_file_init(handle,
161 			    path_ro,
162 			    path_rw,
163 			    &SEMANAGE_IFACE_RTABLE,
164 			    &SEMANAGE_IFACE_FILE_RTABLE, &dconfig->dbase) < 0)
165 		return STATUS_ERR;
166 
167 	dconfig->dtable = &SEMANAGE_FILE_DTABLE;
168 	return STATUS_SUCCESS;
169 }
170 
iface_file_dbase_release(dbase_config_t * dconfig)171 void iface_file_dbase_release(dbase_config_t * dconfig)
172 {
173 
174 	dbase_file_release(dconfig->dbase);
175 }
176