lastlog.c

Go to the documentation of this file.
00001 /* lastlog.c -- Function for Retrieving the eurephia lastlog records
00002  *
00003  *  GPLv2 only - Copyright (C) 2008 - 2010
00004  *               David Sommerseth <dazo@users.sourceforge.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; version 2
00009  *  of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *
00020  */
00021 
00031 #include <string.h>
00032 #include <unistd.h>
00033 #include <assert.h>
00034 
00035 #include <libxml/tree.h>
00036 #include <sqlite3.h>
00037 
00038 #include <eurephia_nullsafe.h>
00039 #include <eurephia_context.h>
00040 #include <eurephia_log.h>
00041 #include <eurephia_xml.h>
00042 #include <eurephia_values.h>
00043 #include <eurephiadb_session_struct.h>
00044 #include <eurephiadb_mapping.h>
00045 #include <passwd.h>
00046 
00047 #ifndef DRIVER_MODE
00048 #define DRIVER_MODE
00049 #endif
00050 #include <eurephiadb_driver.h>
00051 
00052 #include "../sqlite.h"
00053 
00054 #define FMAP_LASTLOG            
00055 #include "../fieldmapping.h"
00056 
00057 
00061 xmlDoc *eDBadminGetLastlog(eurephiaCTX *ctx, xmlDoc *srch_xml, const char *sortkeys)
00062 {
00063         dbresult *res = NULL;
00064         eDBfieldMap *fmap = NULL, *fptr = NULL;
00065         int i = 0;
00066 
00067         xmlDoc *doc = NULL;
00068         xmlNode *fieldmap_n = NULL, *lastl = NULL, *sess = NULL, *tmp1 = NULL, *tmp2 = NULL;
00069 
00070         DEBUG(ctx, 20, "Function call: eDBadminGetLastLog(ctx, {xmlDoc})");
00071         assert( (ctx != NULL) && (srch_xml != NULL) );
00072 
00073         if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
00074                 eurephia_log(ctx, LOG_CRITICAL, 0,
00075                              "eurephia admin function call attempted with wrong context type");
00076                 return NULL;
00077         }
00078 
00079         tmp1 = eurephiaXML_getRoot(ctx, srch_xml, "lastlog_query", 1);
00080         fieldmap_n = xmlFindNode(tmp1, "fieldMapping");
00081         fmap = eDBxmlMapping(ctx, tbl_sqlite_lastlog, "ll", fieldmap_n);
00082 
00083         // HACK: Remove table alias for some fields in the field mapping
00084         for( fptr = fmap; fptr != NULL; fptr = fptr->next) {
00085                 switch( fptr->field_id ) {
00086                 case FIELD_UNAME:
00087                         free_nullsafe(ctx, fptr->table_alias);
00088                 default:
00089                         break;
00090                 }
00091         }
00092 
00093         // Query the database, find the user defined in the user map
00094         res = sqlite_query_mapped(ctx, SQL_SELECT,
00095                                   "SELECT llid, ll.certid, protocol, remotehost, remoteport, macaddr,"
00096                                   "       vpnipaddr, vpnipmask, sessionstatus, sessionkey,"
00097                                   "       login, logout, session_duration, session_deleted,"
00098                                   "       bytes_sent, bytes_received, uicid, accessprofile,"
00099                                   "       access_descr, fw_profile, depth, lower(digest),"
00100                                   "       common_name, organisation, email, username, ll.uid"
00101                                   "  FROM openvpn_lastlog ll"
00102                                   "  LEFT JOIN openvpn_usercerts USING (uid, certid)"
00103                                   "  LEFT JOIN openvpn_accesses USING (accessprofile)"
00104                                   "  LEFT JOIN openvpn_users users ON( ll.uid = users.uid)"
00105                                   "  LEFT JOIN openvpn_certificates cert ON (ll.certid = cert.certid)",
00106                                   NULL, fmap, sortkeys);
00107         eDBfreeMapping(fmap);
00108         xmlFreeDoc(doc);
00109         if( res == NULL ) {
00110                 eurephia_log(ctx, LOG_ERROR, 0, "Quering the lastlog failed");
00111                 return NULL;
00112         }
00113         eurephiaXML_CreateDoc(ctx, 1, "lastlog", &doc, &lastl);
00114         assert( (doc != NULL) && (lastl != NULL) );
00115         for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
00116                 xmlChar *tmp = NULL;
00117                 sess = xmlNewChild(lastl, NULL, (xmlChar*) "session", NULL);
00118                 sqlite_xml_value(sess, XML_ATTR, "llid",                  res, i, 0);
00119                 xmlNewProp(sess, (xmlChar *) "session_status",
00120                            (xmlChar *)SESSION_STATUS[atoi_nullsafe(sqlite_get_value(res, i, 8))]);
00121                 sqlite_xml_value(sess, XML_ATTR, "session_duration",      res, i, 12);
00122                 sqlite_xml_value(sess, XML_NODE, "sessionkey",            res, i, 9);
00123                 sqlite_xml_value(sess, XML_NODE, "login",                 res, i, 10);
00124                 sqlite_xml_value(sess, XML_NODE, "logout",                res, i, 11);
00125                 sqlite_xml_value(sess, XML_NODE, "session_closed",        res, i, 13);
00126 
00127                 tmp1 = xmlNewChild(sess, NULL, (xmlChar *) "connection", NULL);
00128                 sqlite_xml_value(tmp1, XML_ATTR, "bytes_sent",            res, i, 14);
00129                 sqlite_xml_value(tmp1, XML_ATTR, "bytes_received",        res, i, 15);
00130                 sqlite_xml_value(tmp1, XML_NODE, "protocol",              res, i, 2);
00131                 sqlite_xml_value(tmp1, XML_NODE, "remote_host",           res, i, 3);
00132                 sqlite_xml_value(tmp1, XML_NODE, "remote_port",           res, i, 4);
00133                 sqlite_xml_value(tmp1, XML_NODE, "vpn_macaddr",           res, i, 5);
00134                 sqlite_xml_value(tmp1, XML_NODE, "vpn_ipaddr" ,           res, i, 6);
00135                 sqlite_xml_value(tmp1, XML_NODE, "vpn_netmask",           res, i, 7);
00136 
00137                 tmp1 = sqlite_xml_value(sess, XML_NODE, "username",       res, i, 25);
00138                 sqlite_xml_value(tmp1, XML_ATTR, "uid",                   res, i, 26);
00139 
00140                 tmp1 = xmlNewChild(sess, NULL, (xmlChar *) "certificate", NULL);
00141                 sqlite_xml_value(tmp1, XML_ATTR, "certid",                res, i, 1);
00142                 sqlite_xml_value(tmp1, XML_ATTR, "uicid",                 res, i, 16);
00143                 sqlite_xml_value(tmp1, XML_ATTR, "depth",                 res, i, 20);
00144                 sqlite_xml_value(tmp1, XML_NODE, "digest",                res, i, 21);
00145 
00146                 tmp = (xmlChar *)sqlite_get_value(res, i, 22);
00147                 xmlReplaceChars(tmp, '_', ' ');
00148                 xmlNewChild(tmp1, NULL, (xmlChar *) "common_name", tmp);
00149 
00150                 tmp = (xmlChar *)sqlite_get_value(res, i, 23);
00151                 xmlReplaceChars(tmp, '_', ' ');
00152                 xmlNewChild(tmp1, NULL, (xmlChar *) "organisation", tmp);
00153 
00154                 sqlite_xml_value(tmp1, XML_NODE, "email",                 res, i, 24);
00155 
00156                 tmp2 = sqlite_xml_value(tmp1, XML_NODE, "access_profile", res, i, 18);
00157                 sqlite_xml_value(tmp2, XML_ATTR, "accessprofile",         res, i, 17);
00158                 sqlite_xml_value(tmp2, XML_ATTR, "fwdestination",         res, i, 19);
00159         }
00160         sqlite_free_results(res);
00161         return doc;
00162 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines