Debian Codegraph @

database/sqlite/administration/firewalladmin.c

raw
1 /* firewalladmin.c -- Functions for managing firewall profiles
2 *
3 * GPLv2 only - Copyright (C) 2009 - 2012
4 * David Sommerseth <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
9 * of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
21
22 /**
23 * @file sqlite/administration/firewalladmin.c
24 * @author David Sommerseth <[email protected]>
25 * @date 2009-03-28
26 *
27 * @brief Functions for managing firewall profiles
28 *
29 */
30
31 #include <string.h>
32 #include <unistd.h>
33 #include <assert.h>
34
35 #include <libxml/tree.h>
36
37 #include <sqlite3.h>
38
39 #include <eurephia_nullsafe.h>
40 #include <eurephia_context.h>
41 #include <eurephia_log.h>
42 #include <eurephia_xml.h>
43 #include <eurephia_values.h>
44 #include <eurephiadb_session_struct.h>
45 #include <eurephiadb_mapping.h>
46 #include <passwd.h>
47
48 #include "../sqlite.h"
49
50 #define FMAP_OVPNACCESSES
51 #include "../fieldmapping.h"
52
53
54 /**
55 * Internal function. Queries the database for a list of user-certificate links
56 *
57 * @param ctx eurephiaCTX
58 * @param fmap eDBfieldMap containing the search criteria
59 *
60 * @return Returns a valid eurephia XML document on success, otherwise NULL
61 */
62 xmlDoc *fwadmin_search(eurephiaCTX *ctx, eDBfieldMap *fmap) {
63 dbresult *res = NULL;
64 xmlDoc *doc = NULL;
65 xmlNode *root_n = NULL, *rec_n = NULL, *acg_n = NULL, *acc_n = NULL, *tmp_n = NULL;
66 eDBfieldMap *fptr = NULL;
67 int last_acp = -1, i = 0;
68
69 // Add table alias on the certid, to avoid SQL error
70 for( fptr = fmap; fptr != NULL; fptr = fptr->next) {
71 switch( fptr->field_id ) {
72 case FIELD_CERTID:
73 fptr->table_alias = strdup("c");
74 default:
75 break;
76 }
77 }
78
79 // Query the database for accesses
80 res = sqlite_query_mapped(ctx, SQL_SELECT,
81 "SELECT access_descr, fw_profile, accessprofile, "
82 " uid, username, "
83 " uac.certid, common_name, organisation, "
84 " email, lower(digest), locdt(c.registered), uicid "
85 " FROM openvpn_accesses"
86 " LEFT JOIN openvpn_usercerts uac USING (accessprofile)"
87 " LEFT JOIN openvpn_users USING (uid)"
88 " LEFT JOIN openvpn_certificates c ON (uac.certid = c.certid)",
89 NULL, fmap, "accessprofile, uid, c.certid");
90 if( sqlite_query_status(res) != dbSUCCESS ) {
91 eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for firewall profiles");
92 sqlite_log_error(ctx, res);
93 sqlite_free_results(res);
94 return NULL;
95 }
96
97 eurephiaXML_CreateDoc(ctx, 1, "firewall_profiles", &doc, &root_n);
98 xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "profiles");
99
100 for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
101 xmlChar *tmp = NULL;
102 if( last_acp != atoi_nullsafe(sqlite_get_value(res, i, 2)) ) {
103 // Create a new block element when we get a new uid
104 rec_n = xmlNewChild(root_n, NULL, (xmlChar *) "profile", NULL);
105 sqlite_xml_value(rec_n, XML_ATTR, "accessprofile", res, i, 2);
106 sqlite_xml_value(rec_n, XML_NODE, "description", res, i, 0);
107 sqlite_xml_value(rec_n, XML_NODE, "firewall_destination", res, i, 1);
108 acg_n = xmlNewChild(rec_n, NULL, (xmlChar *) "granted_accesses", NULL);
109
110 last_acp = atoi_nullsafe(sqlite_get_value(res, i, 2));
111 }
112
113 // Only continue populating acc_n tags if we have some access info available
114 if( sqlite_get_value(res, i, 11) == NULL ) {
115 continue;
116 }
117
118 acc_n = xmlNewChild(acg_n, NULL, (xmlChar *) "access", NULL);
119 sqlite_xml_value(acc_n, XML_ATTR, "uicid", res, i, 11);
120 tmp_n = sqlite_xml_value(acc_n, XML_NODE, "username", res, i, 4);
121 sqlite_xml_value(tmp_n, XML_ATTR, "uid", res, i, 3);
122
123 tmp_n = xmlNewChild(acc_n, NULL, (xmlChar *) "certificate", NULL);
124 // Only populate tags with certificate info if we have certificate info available
125 if( sqlite_xml_value(tmp_n, XML_ATTR, "certid", res, i, 5) ) {
126 sqlite_xml_value(tmp_n, XML_ATTR, "registered", res, i, 10);
127
128 // OpenVPN uses underscore as default value for "unsafe" characters
129 // in X509 fields. Replace with space for better readability.
130 tmp = (xmlChar *)sqlite_get_value(res, i, 6);
131 xmlReplaceChars(tmp, '_', ' ');
132 xmlNewChild(tmp_n, NULL, (xmlChar *) "common_name", tmp);
133
134 tmp = (xmlChar *)sqlite_get_value(res, i, 7);
135 xmlReplaceChars(tmp, '_', ' ');
136 xmlNewChild(tmp_n, NULL, (xmlChar *) "organisation", tmp);
137
138 sqlite_xml_value(tmp_n, XML_NODE, "email", res, i, 8);
139 sqlite_xml_value(tmp_n, XML_NODE, "digest", res, i, 9);
140 }
141 }
142 sqlite_free_results(res);
143 return doc;
144 }
145
146
147 /**
148 * Internal function. Registers a new firewall profile
149 *
150 * @param ctx eurephiaCTX
151 * @param fmap eDBfieldMap containing information about the new profile
152 *
153 * @return Returns an eurephia ResultMsg XML document, with success message or an error message
154 */
155 xmlDoc *fwadmin_add(eurephiaCTX *ctx, eDBfieldMap *fmap) {
156 dbresult *res = NULL;
157 xmlDoc *ret = NULL;
158
159 // Check if we have the needed fields, and only the needed fields
160 if( eDBmappingFieldsPresent(fmap) != (FIELD_DESCR | FIELD_FWPROFILE) ) {
161 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
162 "Adding firewall profile only accepts "
163 "description and firewall profile fields");
164 }
165
166 res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_accesses", fmap, NULL, NULL);
167 if( sqlite_query_status(res) != dbSUCCESS ) {
168 xmlNode *err_n = NULL;
169
170 eurephia_log(ctx, LOG_FATAL, 0, "Could not register the new firewall profile");
171 err_n = sqlite_log_error_xml(ctx, res);
172 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n,
173 "Could not register the new firewall profile");
174 xmlFreeNode(err_n);
175 } else {
176 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
177 "Firewall profile registered with id %i",
178 res->last_insert_id);
179 }
180 sqlite_free_results(res);
181 return ret;
182 }
183
184
185 /**
186 * Internal function. Deletes firewall profile(s) from the database
187 *
188 * @param ctx eurephiaCTX
189 * @param fmap eDBfieldMap containing information about the profile(s) to be deleted
190 *
191 * @return Returns an eurephia ResultMsg XML document, with success message or an error message
192 */
193 xmlDoc *fwadmin_delete(eurephiaCTX *ctx, eDBfieldMap *fmap) {
194 dbresult *res = NULL;
195 xmlDoc *ret = NULL;
196 xmlNode *err_n = NULL;
197 long int fields;
198
199 // Check if we have the needed fields, and only the needed fields
200 fields = eDBmappingFieldsPresent(fmap);
201 if( !(fields & FIELD_FWPROFILE) && !(fields & FIELD_RECID) ) {
202 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
203 "Deleting firewall profile only accepts "
204 "firewall profile and destination fields");
205 }
206
207 // Find the accessprofile ID based on the fieldmap
208 res = sqlite_query_mapped(ctx, SQL_SELECT, "SELECT DISTINCT accessprofile FROM openvpn_accesses",
209 NULL, fmap, NULL);
210 if( sqlite_query_status(res) != dbSUCCESS ) {
211 eurephia_log(ctx, LOG_FATAL, 0, "Could not delete the firewall profile (1)");
212 err_n = sqlite_log_error_xml(ctx, res);
213 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Could not delete the firewall profile");
214 xmlFreeNode(err_n);
215 goto exit;
216 }
217
218 // Delete all references to this access profile in openvpn_usercerts
219 if( sqlite_get_numtuples(res) > 0 ) {
220 dbresult *dres = NULL;
221 int i = 0;
222
223 for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
224 dres = sqlite_query(ctx,
225 "DELETE FROM openvpn_usercerts "
226 " WHERE accessprofile = %q",
227 sqlite_get_value(res, i, 0));
228 if( sqlite_query_status(dres) != dbSUCCESS ) {
229 eurephia_log(ctx, LOG_FATAL, 0, "Could not delete the firewall profile (2)");
230 err_n = sqlite_log_error_xml(ctx, res);
231 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n,
232 "Could not delete the firewall profile");
233 sqlite_free_results(dres);
234 xmlFreeNode(err_n);
235 goto exit;
236 }
237 sqlite_free_results(dres);
238 }
239 }
240
241 // Delete requested access profiles from openvpn_accesses
242 res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_accesses", NULL, fmap, NULL);
243 if( sqlite_query_status(res) != dbSUCCESS ) {
244 eurephia_log(ctx, LOG_FATAL, 0, "Could not delete the firewall profile");
245 err_n = sqlite_log_error_xml(ctx, res);
246 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Could not delete the firewall profile");
247 xmlFreeNode(err_n);
248 } else {
249 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Firewall profile deleted");
250 }
251 exit:
252 sqlite_free_results(res);
253 return ret;
254 }
255
256
257 /**
258 * @copydoc eDBadminFirewallProfiles()
259 */
260 xmlDoc *eDBadminFirewallProfiles(eurephiaCTX *ctx, xmlDoc *xmlqry) {
261 eDBfieldMap *fmap = NULL;
262 char *mode = NULL;
263 xmlDoc *resxml = NULL;
264 xmlNode *root_n = NULL, *fieldmap_n = NULL;
265
266 DEBUG(ctx, 20, "Function call: eDBadminFirewallProfiles(ctx, {xmlDoc})");
267 assert( (ctx != NULL) && (xmlqry != NULL) );
268
269 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
270 eurephia_log(ctx, LOG_CRITICAL, 0,
271 "eurephia admin function call attempted with wrong context type");
272 return NULL;
273 }
274
275 root_n = eurephiaXML_getRoot(ctx, xmlqry, "firewall_profiles", 1);
276 if( root_n == NULL ) {
277 eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
278 return NULL;
279 }
280 mode = xmlGetAttrValue(root_n->properties, "mode");
281 if( mode == NULL ) {
282 eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute");
283 return NULL;
284 }
285
286 fieldmap_n = xmlFindNode(root_n, "fieldMapping");
287 if( fieldmap_n == NULL ) {
288 eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping");
289 }
290 fmap = eDBxmlMapping(ctx, tbl_sqlite_openvpnaccesses, NULL, fieldmap_n);
291
292 if( strcmp(mode, "search") == 0 ) {
293 resxml = fwadmin_search(ctx, fmap);
294 } else if( strcmp(mode, "add") == 0 ) {
295 resxml = fwadmin_add(ctx, fmap);
296 } else if( strcmp(mode, "delete") == 0 ) {
297 resxml = fwadmin_delete(ctx, fmap);
298 } else {
299 eurephia_log(ctx, LOG_ERROR, 0, "FirewallProfiles - Unknown mode: '%s'", mode);
300 resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode);
301 }
302 eDBfreeMapping(fmap);
303 return resxml;
304 }