Debian Codegraph @

database/sqlite/administration/attempts.c

raw
1 /* attempts.c -- Functions for processing openvpn_attempts records
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/attempts.c
24 * @author David Sommerseth <[email protected]>
25 * @date 2009-04-09
26 *
27 * @brief Functions for processing attempts entries in the database
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_OVPNATTEMPTS /**< fieldmapping.h: Include declaration of tbl_sqlite_attempts */
51 #include "../fieldmapping.h"
52
53 /**
54 * Internal function. Retrieves a list of all registered entries in the attempts table
55 *
56 * @param ctx eurephiaCTX
57 * @param fmap eDBfieldMap containing the search query
58 *
59 * @return Returns a valid eurephia XML document on success, otherwise NULL
60 */
61 xmlDoc *attempts_list(eurephiaCTX *ctx, eDBfieldMap *fmap) {
62 dbresult *res = NULL;
63 xmlDoc *doc = NULL;
64 xmlNode *root_n = NULL, *uname_n = NULL, *cert_n = NULL, *remip_n = NULL;
65 int i = 0;
66
67 // Query the database for registered attempts
68 res = sqlite_query_mapped(ctx, SQL_SELECT,
69 "SELECT username, lower(digest), remoteip, attempts,"
70 " locdt(registered), locdt(last_attempt), atpid"
71 " FROM openvpn_attempts",
72 NULL, fmap, "atpid");
73 if( sqlite_query_status(res) != dbSUCCESS ) {
74 eurephia_log(ctx, LOG_ERROR, 0, "Error querying the attempts log");
75 sqlite_log_error(ctx, res);
76 sqlite_free_results(res);
77 return NULL;
78 }
79
80 eurephiaXML_CreateDoc(ctx, 1, "attemptslog", &doc, &root_n);
81 xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "list");
82
83 for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
84 xmlNode *atmpt_n = NULL;
85
86 if( sqlite_get_value(res, i, 0) != NULL ) { // Username
87 if( uname_n == NULL ) {
88 uname_n = xmlNewChild(root_n, NULL, (xmlChar *) "username", NULL);
89 assert( uname_n != NULL );
90 }
91 atmpt_n = xmlNewChild(uname_n, NULL, (xmlChar *) "attempt", NULL);
92 sqlite_xml_value(atmpt_n, XML_NODE, "username", res, i, 0);
93 } else if( sqlite_get_value(res, i, 1) != NULL ) { // Digest
94 if( cert_n == NULL ) {
95 cert_n = xmlNewChild(root_n, NULL, (xmlChar *) "certificate", NULL);
96 assert( cert_n != NULL );
97 }
98 atmpt_n = xmlNewChild(cert_n, NULL, (xmlChar *) "attempt", NULL);
99 sqlite_xml_value(atmpt_n, XML_NODE, "certificate", res, i, 1);
100 } else if( sqlite_get_value(res, i, 2) != NULL ) { // IP address
101 if( remip_n == NULL ) {
102 remip_n = xmlNewChild(root_n, NULL, (xmlChar *) "ipaddress", NULL);
103 assert( remip_n != NULL );
104 }
105 atmpt_n = xmlNewChild(remip_n, NULL, (xmlChar *) "attempt", NULL);
106 sqlite_xml_value(atmpt_n, XML_NODE, "ipaddress", res, i, 2);
107 } else {
108 continue;
109 }
110
111 sqlite_xml_value(atmpt_n, XML_ATTR, "atpid", res, i, 6);
112 sqlite_xml_value(atmpt_n, XML_ATTR, "attempts", res, i, 3);
113 sqlite_xml_value(atmpt_n, XML_NODE, "registered", res, i, 4);
114 sqlite_xml_value(atmpt_n, XML_NODE, "last_attempt", res, i, 5);
115 }
116 sqlite_free_results(res);
117 return doc;
118 }
119
120
121
122 /**
123 * Internal function. Resets the attempts counter for one or more entries in the database
124 *
125 * @param ctx eurephiaCTX
126 * @param fmap eDBfieldMap defining which records to reset the attempts counter on
127 *
128 * @return Returns a valid eurephia XML document on success, otherwise NULL
129 */
130 xmlDoc *attempts_reset(eurephiaCTX *ctx, eDBfieldMap *fmap) {
131 dbresult *res = NULL;
132 xmlDoc *ret = NULL;
133 int fields = 0;
134 eDBfieldMap update_vals[] = {
135 {TABLE_ATTEMPTS, NULL, FIELD_ATTEMPTS, ft_INT, flt_NOTSET, "attempts", "0", NULL},
136 {0, NULL, 0, ft_UNDEF, flt_NOTSET, NULL, NULL, NULL}
137 };
138
139 fields = eDBmappingFieldsPresent(fmap);
140 if( (fields & (FIELD_UNAME | FIELD_CERTDIGEST | FIELD_REMOTEIP | FIELD_RECID)) == 0 ) {
141 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
142 "Missing username, IP address, certificate digest or atpid");
143 }
144
145 res = sqlite_query_mapped(ctx, SQL_UPDATE, "UPDATE openvpn_attempts", update_vals, fmap, NULL);
146 if( sqlite_query_status(res) == dbSUCCESS ) {
147 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Attempts count reset");
148 } else {
149 xmlNode *err_n = NULL;
150
151 eurephia_log(ctx, LOG_FATAL, 0, "Could not reset the attempts count");
152 err_n = sqlite_log_error_xml(ctx, res);
153 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Could not reset the attempts count");
154 xmlFreeNode(err_n);
155 }
156 sqlite_free_results(res);
157 return ret;
158 }
159
160
161 /**
162 * Internal function. Deletes an entry from the attempts log
163 *
164 * @param ctx eurephiaCTX
165 * @param fmap eDBfieldMap containing the entry/entries to delete
166 *
167 * @return Returns a valid eurephia XML document on success, otherwise NULL
168 */
169 xmlDoc *attempts_delete(eurephiaCTX *ctx, eDBfieldMap *fmap) {
170 dbresult *res = NULL;
171 xmlDoc *ret = NULL;
172 int fields;
173
174 fields = eDBmappingFieldsPresent(fmap);
175 if( (fields & (FIELD_UNAME | FIELD_CERTDIGEST | FIELD_REMOTEIP | FIELD_RECID)) == 0 ) {
176 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
177 "Missing username, IP address, certificate digest or atpid");
178 }
179
180 res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_attempts", NULL, fmap, NULL);
181 if( sqlite_query_status(res) == dbSUCCESS ) {
182 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Attempts record removed");
183 } else {
184 xmlNode *err_n = NULL;
185
186 eurephia_log(ctx, LOG_FATAL, 0, "Could not remove attempts record");
187 err_n = sqlite_log_error_xml(ctx, res);
188 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Could not delete the attempts record");
189 xmlFreeNode(err_n);
190 }
191 sqlite_free_results(res);
192 return ret;
193 }
194
195
196 /**
197 * @copydoc eDBadminAttemptsLog()
198 */
199 xmlDoc *eDBadminAttemptsLog(eurephiaCTX *ctx, xmlDoc *qryxml) {
200 eDBfieldMap *fmap = NULL;
201 char *mode = NULL;
202 xmlDoc *resxml = NULL;
203 xmlNode *root_n = NULL, *fieldmap_n = NULL;
204
205 DEBUG(ctx, 20, "Function call: eDBadminAttemptsLog(ctx, {xmlDoc})");
206 assert( (ctx != NULL) && (qryxml != NULL) );
207
208 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
209 eurephia_log(ctx, LOG_CRITICAL, 0,
210 "eurephia admin function call attempted with wrong context type");
211 return NULL;
212 }
213
214 root_n = eurephiaXML_getRoot(ctx, qryxml, "attemptslog", 1);
215 if( root_n == NULL ) {
216 eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
217 return NULL;
218 }
219 mode = xmlGetAttrValue(root_n->properties, "mode");
220 if( mode == NULL ) {
221 eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute");
222 return NULL;
223 }
224
225 fieldmap_n = xmlFindNode(root_n, "fieldMapping");
226 if( fieldmap_n == NULL ) {
227 eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping");
228 }
229 fmap = eDBxmlMapping(ctx, tbl_sqlite_attempts, NULL, fieldmap_n);
230
231 if( strcmp(mode, "list") == 0 ) {
232 resxml = attempts_list(ctx, fmap);
233 } else if( strcmp(mode, "reset") == 0 ) {
234 resxml = attempts_reset(ctx, fmap);
235 } else if( strcmp(mode, "delete") == 0 ) {
236 resxml = attempts_delete(ctx, fmap);
237 } else {
238 eurephia_log(ctx, LOG_ERROR, 0, "Attempts - Unknown mode: '%s'", mode);
239 resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode);
240 }
241 eDBfreeMapping(fmap);
242 return resxml;
243 }