1 /* Copyright (C) 2005 Red Hat, Inc. */
2
3 struct semanage_node;
4 struct semanage_node_key;
5 typedef struct semanage_node record_t;
6 typedef struct semanage_node_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 <strings.h>
16 #include <semanage/handle.h>
17 #include "node_internal.h"
18 #include "database_file.h"
19 #include "parse_utils.h"
20 #include "debug.h"
21
node_print(semanage_handle_t * handle,semanage_node_t * node,FILE * str)22 static int node_print(semanage_handle_t * handle,
23 semanage_node_t * node, FILE * str)
24 {
25
26 char *con_str = NULL;
27 char *addr = NULL;
28 char *mask = NULL;
29
30 int proto = semanage_node_get_proto(node);
31 const char *proto_str = semanage_node_get_proto_str(proto);
32 semanage_context_t *con = semanage_node_get_con(node);
33
34 if (semanage_node_get_addr(handle, node, &addr) < 0)
35 goto err;
36
37 if (semanage_node_get_mask(handle, node, &mask) < 0)
38 goto err;
39
40 if (semanage_context_to_string(handle, con, &con_str) < 0)
41 goto err;
42
43 if (fprintf
44 (str, "nodecon %s %s %s %s\n", proto_str, addr, mask, con_str) < 0)
45 goto err;
46
47 free(addr);
48 free(mask);
49 free(con_str);
50 return STATUS_SUCCESS;
51
52 err:
53 free(addr);
54 free(mask);
55 free(con_str);
56 ERR(handle, "could not print node to stream");
57 return STATUS_ERR;
58 }
59
node_parse(semanage_handle_t * handle,parse_info_t * info,semanage_node_t * node)60 static int node_parse(semanage_handle_t * handle,
61 parse_info_t * info, semanage_node_t * node)
62 {
63
64 int proto;
65 char *str = NULL;
66 semanage_context_t *con = NULL;
67
68 if (parse_skip_space(handle, info) < 0)
69 goto err;
70 if (!info->ptr)
71 goto last;
72
73 /* Header */
74 if (parse_assert_str(handle, info, "nodecon") < 0)
75 goto err;
76 if (parse_assert_space(handle, info) < 0)
77 goto err;
78
79 /* Protocol */
80 if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
81 goto err;
82 if (!strcasecmp(str, "ipv4"))
83 proto = SEMANAGE_PROTO_IP4;
84 else if (!strcasecmp(str, "ipv6"))
85 proto = SEMANAGE_PROTO_IP6;
86 else {
87 ERR(handle, "invalid protocol \"%s\" (%s: %u):\n%s", str,
88 info->filename, info->lineno, info->orig_line);
89 goto err;
90 }
91 free(str);
92 str = NULL;
93
94 semanage_node_set_proto(node, proto);
95
96 /* Address */
97 if (parse_assert_space(handle, info) < 0)
98 goto err;
99 if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
100 goto err;
101 if (semanage_node_set_addr(handle, node, proto, str) < 0)
102 goto err;
103 if (parse_assert_space(handle, info) < 0)
104 goto err;
105 free(str);
106 str = NULL;
107
108 /* Netmask */
109 if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
110 goto err;
111 if (semanage_node_set_mask(handle, node, proto, str) < 0)
112 goto err;
113 if (parse_assert_space(handle, info) < 0)
114 goto err;
115 free(str);
116 str = NULL;
117
118 /* Port context */
119 if (parse_fetch_string(handle, info, &str, ' ', 0) < 0)
120 goto err;
121 if (semanage_context_from_string(handle, str, &con) < 0) {
122 ERR(handle, "invalid security context \"%s\" (%s: %u)\n%s",
123 str, info->filename, info->lineno, info->orig_line);
124 goto err;
125 }
126 if (con == NULL) {
127 ERR(handle, "<<none>> context is not valid "
128 "for nodes (%s: %u):\n%s", info->filename,
129 info->lineno, info->orig_line);
130 goto err;
131 }
132 free(str);
133 str = NULL;
134
135 if (semanage_node_set_con(handle, node, con) < 0)
136 goto err;
137
138 if (parse_assert_space(handle, info) < 0)
139 goto err;
140
141 semanage_context_free(con);
142 return STATUS_SUCCESS;
143
144 last:
145 parse_dispose_line(info);
146 return STATUS_NODATA;
147
148 err:
149 ERR(handle, "could not parse node record");
150 free(str);
151 semanage_context_free(con);
152 parse_dispose_line(info);
153 return STATUS_ERR;
154 }
155
156 /* NODE RECORD: FILE extension: method table */
157 record_file_table_t SEMANAGE_NODE_FILE_RTABLE = {
158 .parse = node_parse,
159 .print = node_print,
160 };
161
node_file_dbase_init(semanage_handle_t * handle,const char * path_ro,const char * path_rw,dbase_config_t * dconfig)162 int node_file_dbase_init(semanage_handle_t * handle,
163 const char *path_ro,
164 const char *path_rw,
165 dbase_config_t * dconfig)
166 {
167
168 if (dbase_file_init(handle,
169 path_ro,
170 path_rw,
171 &SEMANAGE_NODE_RTABLE,
172 &SEMANAGE_NODE_FILE_RTABLE, &dconfig->dbase) < 0)
173 return STATUS_ERR;
174
175 dconfig->dtable = &SEMANAGE_FILE_DTABLE;
176 return STATUS_SUCCESS;
177 }
178
node_file_dbase_release(dbase_config_t * dconfig)179 void node_file_dbase_release(dbase_config_t * dconfig)
180 {
181
182 dbase_file_release(dconfig->dbase);
183 }
184