| 1 |
/* usercerts.c -- Admin functions - user-certificate table |
| 2 |
* |
| 3 |
* GPLv2 only - Copyright (C) 2008 - 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/usercerts.c |
| 24 |
* @author David Sommerseth <[email protected]> |
| 25 |
* @date 2009-08-29 |
| 26 |
* |
| 27 |
* @brief Functions for working with the openvpn_usercerts table |
| 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 |
|
| 47 |
#include "../sqlite.h" |
| 48 |
|
| 49 |
#define FMAP_USERCERTS |
| 50 |
#include "../fieldmapping.h" |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Internal function. Queries the database for a list of user-certificate links |
| 55 |
* |
| 56 |
* @param ctx eurephiaCTX |
| 57 |
* @param where_m eDBfieldMap containing the search criteria |
| 58 |
* @param sortkeys String containing the sort order of the data |
| 59 |
* |
| 60 |
* @return Returns a valid eurephia XML document on success, otherwise NULL |
| 61 |
*/ |
| 62 |
xmlDoc *usercerts_search(eurephiaCTX *ctx, eDBfieldMap *where_m, const char *sortkeys) { |
| 63 |
xmlDoc *list_xml = NULL; |
| 64 |
xmlNode *link_root_n = NULL, *link_n = NULL, *tmp_n = NULL; |
| 65 |
dbresult *res = NULL; |
| 66 |
xmlChar tmp[2050]; |
| 67 |
const char *dbsort = NULL; |
| 68 |
int i; |
| 69 |
|
| 70 |
DEBUG(ctx, 21, "Function call: usercerts_search(ctx, eDBfieldMap, '%s')", sortkeys); |
| 71 |
assert( ctx != NULL ); |
| 72 |
|
| 73 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 74 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 75 |
"eurephia admin function call attempted with wrong context type"); |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
if( sortkeys != NULL ) { |
| 80 |
dbsort = eDBmkSortKeyString(where_m, sortkeys); |
| 81 |
} |
| 82 |
|
| 83 |
res = sqlite_query_mapped(ctx, SQL_SELECT, |
| 84 |
"SELECT uicid, ucs.uid AS uid, certid, locdt(ucs.registered) AS registered," |
| 85 |
" ucs.accessprofile AS accessprofile, access_descr," |
| 86 |
" username, " |
| 87 |
" common_name, organisation, email, lower(digest), depth " |
| 88 |
" FROM openvpn_usercerts ucs" |
| 89 |
" LEFT JOIN openvpn_certificates USING(certid)" |
| 90 |
" LEFT JOIN openvpn_accesses acc ON(ucs.accessprofile = acc.accessprofile)" |
| 91 |
" LEFT JOIN openvpn_users u ON(u.uid = ucs.uid)", |
| 92 |
NULL, // values (not used for SELECT) |
| 93 |
where_m, // fields and values for the WHERE clause |
| 94 |
dbsort); |
| 95 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 96 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not query the usercerts table"); |
| 97 |
sqlite_log_error(ctx, res); |
| 98 |
sqlite_free_results(res); |
| 99 |
return NULL; |
| 100 |
} |
| 101 |
|
| 102 |
memset(&tmp, 0, 2050); |
| 103 |
eurephiaXML_CreateDoc(ctx, 1, "usercerts", &list_xml, &link_root_n); |
| 104 |
xmlStrPrintf(tmp, 64, "%ld", sqlite_get_numtuples(res)); |
| 105 |
xmlNewProp(link_root_n, (xmlChar *) "link_count", (xmlChar *) tmp); |
| 106 |
|
| 107 |
for( i = 0; i < sqlite_get_numtuples(res); i++ ) { |
| 108 |
link_n = xmlNewChild(link_root_n, NULL, (xmlChar *) "usercert_link", NULL); |
| 109 |
|
| 110 |
sqlite_xml_value(link_n, XML_ATTR, "uicid", res, i, 0); |
| 111 |
sqlite_xml_value(link_n, XML_ATTR, "registered", res, i, 3); |
| 112 |
|
| 113 |
tmp_n = sqlite_xml_value(link_n, XML_NODE, "username", res, i, 6); |
| 114 |
sqlite_xml_value(tmp_n, XML_ATTR, "uid", res, i, 1); |
| 115 |
|
| 116 |
tmp_n = xmlNewChild(link_n, NULL, (xmlChar *) "certificate", NULL); |
| 117 |
sqlite_xml_value(tmp_n, XML_ATTR, "certid", res, i, 2); |
| 118 |
sqlite_xml_value(tmp_n, XML_ATTR, "depth", res, i, 11); |
| 119 |
|
| 120 |
xmlStrPrintf(tmp, 2048, "%.2048s", sqlite_get_value(res, i, 7)); |
| 121 |
xmlReplaceChars(tmp, '_', ' '); |
| 122 |
xmlNewChild(tmp_n, NULL, (xmlChar *) "common_name", tmp); |
| 123 |
|
| 124 |
xmlStrPrintf(tmp, 2048, "%.2048s", sqlite_get_value(res, i, 8)); |
| 125 |
xmlReplaceChars(tmp, '_', ' '); |
| 126 |
xmlNewChild(tmp_n, NULL, (xmlChar *) "organisation", tmp); |
| 127 |
|
| 128 |
sqlite_xml_value(tmp_n, XML_NODE, "email", res, i, 9); |
| 129 |
sqlite_xml_value(tmp_n, XML_NODE, "digest", res, i, 10); |
| 130 |
|
| 131 |
tmp_n = sqlite_xml_value(link_n, XML_NODE, "access_profile", res, i, 5); |
| 132 |
sqlite_xml_value(tmp_n, XML_ATTR, "accessprofile", res, i, 4); |
| 133 |
} |
| 134 |
sqlite_free_results(res); |
| 135 |
|
| 136 |
return list_xml; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Internal function. Adds or deletes a user-certificate link |
| 142 |
* |
| 143 |
* @param ctx eurephiaCTX |
| 144 |
* @param mode String containing the operation mode. Must be "register" or "remove" |
| 145 |
* @param usrcrt_m eDBfieldMap containing information about the link to add or remove |
| 146 |
* |
| 147 |
* @return Returns an eurephia ResultMsg XML document, with success message or an error message |
| 148 |
*/ |
| 149 |
xmlDoc *usercerts_add_del(eurephiaCTX *ctx, const char *mode, eDBfieldMap *usrcrt_m) { |
| 150 |
xmlDoc *res = NULL; |
| 151 |
dbresult *dbres = NULL; |
| 152 |
|
| 153 |
DEBUG(ctx, 21, "Function call: usercerts_add_del(ctx, xmlDoc)"); |
| 154 |
assert( (ctx != NULL) && (usrcrt_m != NULL) ); |
| 155 |
|
| 156 |
if( strcmp(mode, "register") == 0 ) { |
| 157 |
dbres = sqlite_query_mapped(ctx, SQL_INSERT, |
| 158 |
"INSERT INTO openvpn_usercerts", usrcrt_m, NULL, NULL); |
| 159 |
if( sqlite_query_status(dbres) == dbSUCCESS ) { |
| 160 |
res = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, |
| 161 |
"Registered new user-cert link with id %i", |
| 162 |
dbres->last_insert_id); |
| 163 |
} |
| 164 |
} else if( strcmp(mode, "remove") == 0 ) { |
| 165 |
dbres = sqlite_query_mapped(ctx, SQL_DELETE, |
| 166 |
"DELETE FROM openvpn_usercerts", NULL, usrcrt_m, NULL); |
| 167 |
if( sqlite_query_status(dbres) == dbSUCCESS ) { |
| 168 |
int num_rows = sqlite_get_affected_rows(dbres); |
| 169 |
if( num_rows > 0 ) { |
| 170 |
res = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, |
| 171 |
"Removed %i user-cert %s successfully", |
| 172 |
num_rows, (num_rows == 1 ? "link" : "links")); |
| 173 |
} else { |
| 174 |
res = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 175 |
"No user-cert links where removed"); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if( sqlite_query_status(dbres) != dbSUCCESS ) { |
| 181 |
xmlNode *tmp_n = NULL; |
| 182 |
|
| 183 |
eurephia_log(ctx, LOG_ERROR, 0, "Failed to %s user-cert link.", mode); |
| 184 |
tmp_n = sqlite_log_error_xml(ctx, dbres); |
| 185 |
res = eurephiaXML_ResultMsg(ctx, exmlERROR, tmp_n, "Failed to %s user-cert link", mode); |
| 186 |
xmlFreeNode(tmp_n); |
| 187 |
} |
| 188 |
sqlite_free_results(dbres); |
| 189 |
|
| 190 |
return res; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Internal function. Updates a user-certs link in the database |
| 196 |
* |
| 197 |
* @param ctx eurephiaCTX |
| 198 |
* @param uicid String containing the numeric record ID (uicid field) of the record to be updated |
| 199 |
* @param usrcrt_m eDBfieldMap containing the new information for the record. Only the fields being |
| 200 |
* changed needs to be set. |
| 201 |
* |
| 202 |
* @return Returns an eurephia ResultMsg XML document, with success message or an error message |
| 203 |
*/ |
| 204 |
xmlDoc *usercerts_update(eurephiaCTX *ctx, const char *uicid, eDBfieldMap *usrcrt_m) { |
| 205 |
xmlNode *where_n = NULL; |
| 206 |
eDBfieldMap *where_m = NULL; |
| 207 |
dbresult *dbres = NULL; |
| 208 |
xmlDoc *where_d = NULL, *res = NULL; |
| 209 |
|
| 210 |
DEBUG(ctx, 21, "Function call: usercerts_update(ctx, '%s', eDBfieldMap)", uicid); |
| 211 |
assert( ctx != NULL && uicid != NULL && usrcrt_m != NULL ); |
| 212 |
|
| 213 |
// Create a eDBfieldMap which will contain the uicid value |
| 214 |
eurephiaXML_CreateDoc(ctx, 1, "usercerts", &where_d, &where_n); |
| 215 |
assert( (where_d != NULL) && (where_n != NULL) ); |
| 216 |
|
| 217 |
where_n = xmlNewChild(where_n, NULL, (xmlChar *) "fieldMapping", NULL); |
| 218 |
xmlNewProp(where_n, (xmlChar *) "table", (xmlChar *) "usercerts"); |
| 219 |
xmlNewChild(where_n, NULL, (xmlChar *) "uicid", (xmlChar *) uicid); |
| 220 |
|
| 221 |
// Convert xmlNode with fieldMapping into a eDBfieldMap |
| 222 |
where_m = eDBxmlMapping(ctx, tbl_sqlite_usercerts, NULL, where_n); |
| 223 |
assert( where_m != NULL ); |
| 224 |
|
| 225 |
// Send update query to the database |
| 226 |
dbres = sqlite_query_mapped(ctx, SQL_UPDATE, "UPDATE openvpn_usercerts", |
| 227 |
usrcrt_m, where_m, NULL); |
| 228 |
if( sqlite_query_status(dbres) == dbSUCCESS ) { |
| 229 |
int num_rows = sqlite_get_affected_rows(dbres); |
| 230 |
if( num_rows > 0 ) { |
| 231 |
res = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, |
| 232 |
"Updated firewall access profile on %i user-cert %s.", |
| 233 |
num_rows, (num_rows == 1 ? "link" : "links")); |
| 234 |
} else { |
| 235 |
res = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 236 |
"No user-cert links where updated"); |
| 237 |
} |
| 238 |
} else { |
| 239 |
xmlNode *err_n = NULL; |
| 240 |
|
| 241 |
eurephia_log(ctx, LOG_ERROR, 0, "Failed to update user-cert link.(uicid: %s)", uicid); |
| 242 |
err_n = sqlite_log_error_xml(ctx, dbres); |
| 243 |
res = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, |
| 244 |
"Failed to update user-cert link for uicid %s", uicid); |
| 245 |
xmlFreeNode(err_n); |
| 246 |
} |
| 247 |
sqlite_free_results(dbres); |
| 248 |
eDBfreeMapping(where_m); |
| 249 |
xmlFreeDoc(where_d); |
| 250 |
|
| 251 |
return res; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
/** |
| 256 |
* @copydoc eDBadminUserCertsLink() |
| 257 |
*/ |
| 258 |
xmlDoc *eDBadminUserCertsLink(eurephiaCTX *ctx, xmlDoc *usrcrt_xml) { |
| 259 |
xmlNode *usrcrt_n = NULL, *tmp_n = NULL; |
| 260 |
xmlDoc *resxml = NULL; |
| 261 |
eDBfieldMap *usrcrt_m = NULL; |
| 262 |
const char *mode = NULL, *sortfields = NULL, *uicid = NULL; |
| 263 |
|
| 264 |
DEBUG(ctx, 20, "Function call: eDBadminUserCertsLink(ctx, xmlDoc)"); |
| 265 |
assert( (ctx != NULL) && (usrcrt_xml != NULL) ); |
| 266 |
|
| 267 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 268 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 269 |
"eurephia admin function call attempted with wrong context type"); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
usrcrt_n = eurephiaXML_getRoot(ctx, usrcrt_xml, "usercerts", 1); |
| 274 |
if( usrcrt_n == NULL ) { |
| 275 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the user-certs link request"); |
| 276 |
return 0; |
| 277 |
} |
| 278 |
mode = xmlGetAttrValue(usrcrt_n->properties, "mode"); |
| 279 |
if( mode == NULL ) { |
| 280 |
eurephia_log(ctx, LOG_ERROR, 0, "Invalid user-cert link request (1)."); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
tmp_n = xmlFindNode(usrcrt_n, "sortfields"); |
| 285 |
if( tmp_n ) { |
| 286 |
sortfields = xmlExtractContent(tmp_n); |
| 287 |
} |
| 288 |
|
| 289 |
tmp_n = xmlFindNode(usrcrt_n, "fieldMapping"); |
| 290 |
if( tmp_n == NULL ) { |
| 291 |
eurephia_log(ctx, LOG_ERROR, 0, "Invalid user-cert link request (2)."); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
usrcrt_m = eDBxmlMapping(ctx, tbl_sqlite_usercerts, NULL, tmp_n); |
| 295 |
assert(usrcrt_m != NULL); |
| 296 |
|
| 297 |
|
| 298 |
if( strcmp(mode, "search") == 0 ) { |
| 299 |
resxml = usercerts_search(ctx, usrcrt_m, sortfields); |
| 300 |
} else if( strcmp(mode, "register") == 0 ) { |
| 301 |
resxml = usercerts_add_del(ctx, mode, usrcrt_m); |
| 302 |
} else if( strcmp(mode, "remove") == 0 ) { |
| 303 |
resxml = usercerts_add_del(ctx, mode, usrcrt_m); |
| 304 |
} else if( strcmp(mode, "update") == 0 ) { |
| 305 |
uicid = xmlGetAttrValue(usrcrt_n->properties, "uicid"); |
| 306 |
if( uicid == NULL ) { |
| 307 |
eurephia_log(ctx, LOG_ERROR, 0, "Missing required attribute, uicid, for updates"); |
| 308 |
resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 309 |
"Can not set firewall access profile without uicid"); |
| 310 |
goto exit; |
| 311 |
} |
| 312 |
resxml = usercerts_update(ctx, uicid, usrcrt_m); |
| 313 |
} |
| 314 |
|
| 315 |
exit: |
| 316 |
eDBfreeMapping(usrcrt_m); |
| 317 |
return resxml; |
| 318 |
} |