From 168609eb2521cdccfc5ee6f9316be6054c917b32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20K=C3=A1cha?= <ph@cesnet.cz>
Date: Wed, 25 Mar 2015 14:25:01 +0100
Subject: [PATCH]  * Added loadmaps command and ditched data from
 categories/tags of sqldump  * Changed charset to utf8 (not mb4, to support
 older MySQLs) in sqldump  * Ditched data from clients (no predefined clients
 for new installations) in sqldump  * Identity is renamed to name everywhere
 ii code and db to be consistent with Idea terminology  * Better exception
 handling in insertLastReceivedId

---
 warden3/warden_server/warden3.0-alpha.sql | 245 ----------------------
 warden3/warden_server/warden_3.0.sql      | 129 ++++++++++++
 warden3/warden_server/warden_server.py    |  99 ++++++---
 3 files changed, 197 insertions(+), 276 deletions(-)
 delete mode 100644 warden3/warden_server/warden3.0-alpha.sql
 create mode 100644 warden3/warden_server/warden_3.0.sql

diff --git a/warden3/warden_server/warden3.0-alpha.sql b/warden3/warden_server/warden3.0-alpha.sql
deleted file mode 100644
index cfd494b..0000000
--- a/warden3/warden_server/warden3.0-alpha.sql
+++ /dev/null
@@ -1,245 +0,0 @@
--- phpMyAdmin SQL Dump
--- version 3.4.11.1deb2+deb7u1
--- http://www.phpmyadmin.net
---
--- Host: localhost
--- Generation Time: Dec 04, 2014 at 02:54 PM
--- Server version: 5.5.38
--- PHP Version: 5.4.4-14+deb7u14
-
-SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
-SET time_zone = "+00:00";
-
-
-/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
-/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
-/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
-/*!40101 SET NAMES utf8 */;
-
---
--- Database: `warden3`
---
-
--- --------------------------------------------------------
-
---
--- Table structure for table `categories`
---
-
-CREATE TABLE IF NOT EXISTS `categories` (
-  `id` int(11) NOT NULL,
-  `category` varchar(64) NOT NULL,
-  `subcategory` varchar(64) DEFAULT NULL,
-  `cat_subcat` varchar(129) NOT NULL,
-  KEY `cat_sub` (`cat_subcat`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;
-
---
--- Dumping data for table `categories`
---
-
-INSERT INTO `categories` (`id`, `category`, `subcategory`, `cat_subcat`) VALUES
-(100, 'Abusive', NULL, 'Abusive'),
-(101, 'Abusive', 'Spam', 'Abusive.Spam'),
-(102, 'Abusive', 'Harassment', 'Abusive.Harassment'),
-(103, 'Abusive', 'Child', 'Abusive.Child'),
-(104, 'Abusive', 'Sexual', 'Abusive.Sexual'),
-(105, 'Abusive', 'Violence', 'Abusive.Violence'),
-(200, 'Malware', NULL, 'Malware'),
-(201, 'Malware', 'Virus', 'Malware.Virus'),
-(202, 'Malware', 'Worm', 'Malware.Worm'),
-(203, 'Malware', 'Trojan', 'Malware.Trojan'),
-(204, 'Malware', 'Spyware', 'Malware.Spyware'),
-(205, 'Malware', 'Dialer', 'Malware.Dialer'),
-(206, 'Malware', 'Rootkit', 'Malware.Rootkit'),
-(300, 'Recon', NULL, 'Recon'),
-(301, 'Recon', 'Scanning', 'Recon.Scanning'),
-(302, 'Recon', 'Sniffing', 'Recon.Sniffing'),
-(303, 'Recon', 'SocialEngineering', 'Recon.SocialEngineering'),
-(304, 'Recon', 'Searching', 'Recon.Searching'),
-(400, 'Attempt', NULL, 'Attempt'),
-(401, 'Attempt', 'Exploit', 'Attempt.Exploit'),
-(402, 'Attempt', 'Login', 'Attempt.Login'),
-(403, 'Attempt', 'NewSignature', 'Attempt.NewSignature'),
-(500, 'Intrusion', NULL, 'Intrusion'),
-(501, 'Intrusion', 'AdminCompromise', 'Intrusion.AdminCompromise'),
-(502, 'Intrusion', 'UserCompromise', 'Intrusion.UserCompromise'),
-(503, 'Intrusion', 'AppCompromise', 'Intrusion.AppCompromise'),
-(504, 'Intrusion', 'Botnet', 'Intrusion.Botnet'),
-(600, 'Availability', NULL, 'Availability'),
-(601, 'Availability', 'DoS', 'Availability.DoS'),
-(602, 'Availability', 'DDoS', 'Availability.DDoS'),
-(603, 'Availability', 'Sabotage', 'Availability.Sabotage'),
-(604, 'Availability', 'Outage', 'Availability.Outage'),
-(700, 'Information', NULL, 'Information'),
-(701, 'Information', 'UnauthorizedAccess', 'Information.UnauthorizedAccess'),
-(702, 'Information', 'UnauthorizedModification', 'Information.UnauthorizedModification'),
-(800, 'Fraud', NULL, 'Fraud'),
-(801, 'Fraud', 'UnauthorizedUsage', 'Fraud.UnauthorizedUsage'),
-(802, 'Fraud', 'Copyright', 'Fraud.Copyright'),
-(803, 'Fraud', 'Masquerade', 'Fraud.Masquerade'),
-(804, 'Fraud', 'Phishing', 'Fraud.Phishing'),
-(805, 'Fraud', 'Scam', 'Fraud.Scam'),
-(900, 'Vulnerable', NULL, 'Vulnerable'),
-(901, 'Vulnerable', 'Open', 'Vulnerable.Open'),
-(1000, 'Anomaly', NULL, 'Anomaly'),
-(1001, 'Anomaly', 'Traffic', 'Anomaly.Traffic'),
-(1002, 'Anomaly', 'Connection', 'Anomaly.Connection'),
-(1003, 'Anomaly', 'Protocol', 'Anomaly.Protocol'),
-(1004, 'Anomaly', 'System', 'Anomaly.System'),
-(1005, 'Anomaly', 'Application', 'Anomaly.Application'),
-(1006, 'Anomaly', 'Behaviour', 'Anomaly.Behaviour'),
-(9998, 'Other', '', 'Other'),
-(9999, 'Test', '', 'Test');
-
--- --------------------------------------------------------
-
---
--- Table structure for table `clients`
---
-
-CREATE TABLE IF NOT EXISTS `clients` (
-  `id` int(11) NOT NULL AUTO_INCREMENT,
-  `registered` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
-  `requestor` varchar(256) NOT NULL,
-  `hostname` varchar(256) NOT NULL,
-  `service` varchar(256) NOT NULL,
-  `note` text NOT NULL,
-  `valid` tinyint(1) NOT NULL DEFAULT '1',
-  `identity` varchar(64) NOT NULL,
-  `secret` varchar(16) NULL,
-  `read` tinyint(1) NOT NULL DEFAULT '1',
-  `debug` tinyint(1) NOT NULL DEFAULT '0',
-  `write` tinyint(1) NOT NULL DEFAULT '0',
-  `test` int(11) NOT NULL DEFAULT '0',
-  PRIMARY KEY (`id`)
-) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci AUTO_INCREMENT=31 ;
-
---
--- Dumping data for table `clients`
---
-
-INSERT INTO `clients` (`id`, `registered`, `requestor`, `hostname`, `service`, `note`, `valid`, `identity`, `secret`, `read`, `debug`, `write`, `test`) VALUES
-(1, '0000-00-00 00:00:00', '', 'afrodita.civ.zcu.cz', 'hihat', '', 1, 'cz.zcu.civ.afrodita.hihat', NULL, 1, 0, 1, 0),
-(2, '0000-00-00 00:00:00', '', 'afrodita.civ.zcu.cz', 'labrea', '', 1, 'cz.zcu.civ.afrodita.labrea', NULL, 1, 0, 1, 0),
-(3, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'cesnet_ids', '', 1, 'cz.cesnet.au1.cesnet_ids', NULL, 1, 0, 1, 0),
-(4, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'cesnet_sserv', '', 1, 'cz.cesnet.au1.cesnet_sserv', NULL, 1, 0, 1, 0),
-(5, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'n6d-dorkbot', '', 1, 'cz.cesnet.au1.n6d-dorkbot', NULL, 1, 0, 1, 0),
-(6, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'n6e-certplsinkhole', '', 1, 'cz.cesnet.au1.n6e-certplsinkhole', NULL, 1, 0, 1, 0),
-(7, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'n6i-citadelsinkhole', '', 1, 'cz.cesnet.au1.n6i-citadelsinkhole', NULL, 1, 0, 1, 0),
-(8, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'n6i-citadelsinkholeqd', '', 1, 'cz.cesnet.au1.n6i-citadelsinkholeqd', NULL, 1, 0, 1, 0),
-(9, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'n6n-openntp', '', 1, 'cz.cesnet.au1.n6n-openntp', NULL, 1, 0, 1, 0),
-(10, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'n6o-botszeroaccess', '', 1, 'cz.cesnet.au1.n6o-botszeroaccess', NULL, 1, 0, 1, 0),
-(11, '0000-00-00 00:00:00', '', 'au1.cesnet.cz', 'report_n6v-virut', '', 1, 'cz.cesnet.au1.report_n6v-virut', NULL, 1, 0, 1, 0),
-(12, '0000-00-00 00:00:00', '', 'au2.cesnet.cz', 'ids-cz', '', 1, 'cz.cesnet.au2.ids-cz', NULL, 1, 0, 1, 0),
-(13, '0000-00-00 00:00:00', '', 'bee.net.vutbr.cz', 'hpscan', '', 1, 'cz.vutbr.net.bee.hpscan', NULL, 1, 0, 1, 0),
-(14, '0000-00-00 00:00:00', '', 'buldog.vsb.cz', 'kippo', '', 1, 'cz.vsb.buldog.kippo', NULL, 1, 0, 1, 0),
-(15, '0000-00-00 00:00:00', '', 'collector-nemea.liberouter.org', 'nemea', '', 1, 'org.liberouter.collector-nemea.nemea', NULL, 1, 0, 1, 0),
-(16, '0000-00-00 00:00:00', '', 'collector.liberouter.org', 'hoststats', '', 1, 'org.liberouter.collector.hoststats', NULL, 1, 0, 1, 0),
-(17, '0000-00-00 00:00:00', '', 'collector.liberouter.org', 'synscandetector_1_0', '', 1, 'org.liberouter.collector.synscandetector_1_0', NULL, 1, 0, 1, 0),
-(18, '0000-00-00 00:00:00', '', 'holly.cesnet.cz', 'kippohoneypot', '', 1, 'cz.cesnet.holly.kippohoneypot', NULL, 1, 0, 1, 0),
-(19, '0000-00-00 00:00:00', '', 'kryten.cesnet.cz', 'dionaeahoneypot', '', 1, 'cz.cesnet.kryten.dionaeahoneypot', NULL, 1, 0, 1, 0),
-(20, '0000-00-00 00:00:00', '', 'mentat.cesnet.cz', 'mentat', '', 1, 'cz.cesnet.mentat.mentat', NULL, 1, 0, 1, 0),
-(21, '0000-00-00 00:00:00', '', 'miel.opf.slu.cz', 'kippo', '', 1, 'cz.slu.opf.miel.kippo', NULL, 1, 0, 1, 0),
-(22, '0000-00-00 00:00:00', '', 'nfsen.ics.muni.cz', 'honeyscan', '', 1, 'cz.muni.ics.nfsen.honeyscan', NULL, 1, 0, 1, 0),
-(23, '0000-00-00 00:00:00', '', 'nfsen.ics.muni.cz', 'scandetector_1_0', '', 1, 'cz.muni.ics.nfsen.scandetector_1_0', NULL, 1, 0, 1, 0),
-(24, '0000-00-00 00:00:00', '', 'nfsen.ics.muni.cz', 'sshbruteforce-1_n', '', 1, 'cz.muni.ics.nfsen.sshbruteforce-1_n', NULL, 1, 0, 1, 0),
-(25, '0000-00-00 00:00:00', '', 'vinovago.cesnet.cz', 'fail2ban', '', 1, 'cz.cesnet.vinovago.fail2ban', NULL, 1, 0, 1, 0),
-(26, '0000-00-00 00:00:00', '', 'ward.tul.cz', 'dionaeatul', '', 1, 'cz.tul.ward.dionaeatul', NULL, 1, 0, 1, 0),
-(27, '0000-00-00 00:00:00', '', 'ward.tul.cz', 'kippo', '', 1, 'cz.tul.ward.kippo', NULL, 1, 0, 1, 0),
-(28, '0000-00-00 00:00:00', 'kostenec@civ.zcu.cz', 'kostik.zcu.cz', 'com.example.test-node', '', 1, 'com.example.test-node', "Phaipe5ush7p", 1, 0, 0, 0),
-(29, '0000-00-00 00:00:00', 'kostenec@civ.zcu.cz', 'kostik.zcu.cz', 'com.example.test-node2', '', 1, 'com.example.test-node2', NULL, 1, 0, 0, 0),
-(30, '0000-00-00 00:00:00', 'kostenec@civ.zcu.cz', 'kostik.zcu.cz', 'Test', '', 1, 'com.example.test-node3', NULL, 1, 0, 0, 0),
-(31, '2014-12-11 13:51:18', 'ph@cesnet.cz', 'grey.cesnet.cz', 'Test', '', 1, 'cz.cesnet.grey.test', "co3kaero5Ruv", 1, 1, 1, 0);
-
--- --------------------------------------------------------
-
---
--- Table structure for table `events`
---
-
-CREATE TABLE IF NOT EXISTS `events` (
-  `id` int(11) NOT NULL AUTO_INCREMENT,
-  `received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-  `client_id` int(11) NOT NULL,
-  `data` text NOT NULL,
-  `valid` tinyint(1) NOT NULL DEFAULT '1',
-  PRIMARY KEY (`id`),
-  KEY `id` (`id`,`client_id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4  DEFAULT COLLATE utf8mb4_unicode_ci AUTO_INCREMENT=1 ;
-
--- --------------------------------------------------------
-
---
--- Table structure for table `event_category_mapping`
---
-
-CREATE TABLE IF NOT EXISTS `event_category_mapping` (
-  `event_id` int(11) NOT NULL,
-  `category_id` int(11) NOT NULL,
-  KEY `event_id_2` (`event_id`,`category_id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;
-
--- --------------------------------------------------------
-
---
--- Table structure for table `event_tag_mapping`
---
-
-CREATE TABLE IF NOT EXISTS `event_tag_mapping` (
-  `event_id` int(11) NOT NULL,
-  `tag_id` int(11) NOT NULL,
-  KEY `event_id_2` (`event_id`,`tag_id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;
-
--- --------------------------------------------------------
-
---
--- Table structure for table `last_events`
---
-
-CREATE TABLE IF NOT EXISTS `last_events` (
-  `id` int(11) NOT NULL AUTO_INCREMENT,
-  `client_id` int(11) NOT NULL,
-  `event_id` int(11) NOT NULL,
-  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-  PRIMARY KEY (`id`),
-  KEY `client_id` (`client_id`,`event_id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci AUTO_INCREMENT=1 ;
-
--- --------------------------------------------------------
-
---
--- Table structure for table `tags`
---
-
-CREATE TABLE IF NOT EXISTS `tags` (
-  `id` int(11) NOT NULL,
-  `tag` varchar(64) NOT NULL,
-  KEY `id_tag_name` (`id`,`tag`),
-  KEY `tag_name` (`tag`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;
-
---
--- Dumping data for table `tags`
---
-
-INSERT INTO `tags` (`id`, `tag`) VALUES
-(1, 'Connection'),
-(2, 'Datagram'),
-(3, 'Content'),
-(4, 'Data'),
-(5, 'File'),
-(6, 'Flow'),
-(7, 'Log'),
-(8, 'Protocol'),
-(9, 'Host'),
-(10, 'Network'),
-(11, 'Correlation'),
-(12, 'External'),
-(13, 'Reporting'),
-(99, 'Other');
-
-/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
-/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
-/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
diff --git a/warden3/warden_server/warden_3.0.sql b/warden3/warden_server/warden_3.0.sql
new file mode 100644
index 0000000..84cce8e
--- /dev/null
+++ b/warden3/warden_server/warden_3.0.sql
@@ -0,0 +1,129 @@
+-- phpMyAdmin SQL Dump
+-- version 3.4.11.1deb2+deb7u1
+-- http://www.phpmyadmin.net
+--
+-- Host: localhost
+-- Generation Time: Dec 04, 2014 at 02:54 PM
+-- Server version: 5.5.38
+-- PHP Version: 5.4.4-14+deb7u14
+
+SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
+SET time_zone = "+00:00";
+
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+
+--
+-- Database: `warden3`
+--
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `categories`
+--
+
+CREATE TABLE IF NOT EXISTS `categories` (
+  `id` int(11) NOT NULL,
+  `category` varchar(64) NOT NULL,
+  `subcategory` varchar(64) DEFAULT NULL,
+  `cat_subcat` varchar(129) NOT NULL,
+  KEY `cat_sub` (`cat_subcat`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `clients`
+--
+
+CREATE TABLE IF NOT EXISTS `clients` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `registered` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
+  `requestor` varchar(256) NOT NULL,
+  `hostname` varchar(256) NOT NULL,
+  `note` text NULL,
+  `valid` tinyint(1) NOT NULL DEFAULT '1',
+  `name` varchar(64) NOT NULL,
+  `secret` varchar(16) NULL,
+  `read` tinyint(1) NOT NULL DEFAULT '1',
+  `debug` tinyint(1) NOT NULL DEFAULT '0',
+  `write` tinyint(1) NOT NULL DEFAULT '0',
+  `test` int(11) NOT NULL DEFAULT '0',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci AUTO_INCREMENT=31 ;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `events`
+--
+
+CREATE TABLE IF NOT EXISTS `events` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `client_id` int(11) NOT NULL,
+  `data` text NOT NULL,
+  `valid` tinyint(1) NOT NULL DEFAULT '1',
+  PRIMARY KEY (`id`),
+  KEY `id` (`id`,`client_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8  DEFAULT COLLATE utf8_unicode_ci AUTO_INCREMENT=1 ;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `event_category_mapping`
+--
+
+CREATE TABLE IF NOT EXISTS `event_category_mapping` (
+  `event_id` int(11) NOT NULL,
+  `category_id` int(11) NOT NULL,
+  KEY `event_id_2` (`event_id`,`category_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `event_tag_mapping`
+--
+
+CREATE TABLE IF NOT EXISTS `event_tag_mapping` (
+  `event_id` int(11) NOT NULL,
+  `tag_id` int(11) NOT NULL,
+  KEY `event_id_2` (`event_id`,`tag_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `last_events`
+--
+
+CREATE TABLE IF NOT EXISTS `last_events` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `client_id` int(11) NOT NULL,
+  `event_id` int(11) NOT NULL,
+  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  PRIMARY KEY (`id`),
+  KEY `client_id` (`client_id`,`event_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci AUTO_INCREMENT=1 ;
+
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `tags`
+--
+
+CREATE TABLE IF NOT EXISTS `tags` (
+  `id` int(11) NOT NULL,
+  `tag` varchar(64) NOT NULL,
+  KEY `id_tag_name` (`id`,`tag`),
+  KEY `tag_name` (`tag`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_unicode_ci;
+
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
diff --git a/warden3/warden_server/warden_server.py b/warden3/warden_server/warden_server.py
index 3b48297..e940e2d 100755
--- a/warden3/warden_server/warden_server.py
+++ b/warden3/warden_server/warden_server.py
@@ -211,17 +211,17 @@ def SysLogger(req, socket="/dev/log", facility=logging.handlers.SysLogHandler.LO
 
 
 class Client(namedtuple("ClientTuple",
-    ["id", "registered", "requestor", "hostname", "service", "note",
-    "valid", "identity", "secret", "read", "debug", "write", "test"])):
+    ["id", "registered", "requestor", "hostname", "note",
+    "valid", "name", "secret", "read", "debug", "write", "test"])):
 
     def __str__(self):
         return (
             "%s(id=%i, registered=%s, requestor=\"%s\", hostname=\"%s\", "
-            "service=\"%s\", note=\"%s\", identity=\"%s\", secret=%s, "
+            "note=\"%s\", name=\"%s\", secret=%s, "
             "valid=%i read=%i, debug=%i, write=%i, test=%i)") % (
             type(self).__name__, self.id, self.registered,
-            self.requestor, self.hostname, self.service, self.note,
-            self.identity, "..." if self.secret is not None else "None",
+            self.requestor, self.hostname, self.note,
+            self.name, "..." if self.secret is not None else "None",
             self.valid, self.read, self.debug, self.write, self.test)
 
 
@@ -335,14 +335,14 @@ class X509Authenticator(NoAuthenticator):
             logging.info("authenticate: cannot get or parse certificate from env")
             return None
 
-        identity = args.get("client", [None])[0]
+        name = args.get("client", [None])[0]
         secret =  args.get("secret", [None])[0]
 
-        client = self.db.get_client_by_name(cert_names, identity, secret)
+        client = self.db.get_client_by_name(cert_names, name, secret)
 
         if not client:
-            logging.info("authenticate: client not found by identity: \"%s\", secret: %s, cert_names: %s" % (
-                identity, "..." if secret else "None", str(cert_names)))
+            logging.info("authenticate: client not found by name: \"%s\", secret: %s, cert_names: %s" % (
+                name, "..." if secret else "None", str(cert_names)))
             return None
         
         # Clients with 'secret' set muset get authorized by it.
@@ -515,12 +515,12 @@ class MySQL(ObjectReq):
         return "" if b else "NOT"
 
 
-    def get_client_by_name(self, cert_names, identity=None, secret=None):
-        query = ["SELECT id, registered, requestor, hostname, service, note, valid, identity, secret, `read`, debug, `write`, test FROM clients WHERE valid = 1"]
+    def get_client_by_name(self, cert_names, name=None, secret=None):
+        query = ["SELECT id, registered, requestor, hostname, note, valid, name, secret, `read`, debug, `write`, test FROM clients WHERE valid = 1"]
         params = []
-        if identity:
-            query.append(" AND identity = %s")
-            params.append(identity)
+        if name:
+            query.append(" AND name = %s")
+            params.append(name)
         if secret:
             query.append(" AND secret = %s")
             params.append(secret)
@@ -537,7 +537,7 @@ class MySQL(ObjectReq):
 
 
     def get_clients(self, id=None):
-        query = ["SELECT id, registered, requestor, hostname, service, note, valid, identity, secret, `read`, debug, `write`, test FROM clients"]
+        query = ["SELECT id, registered, requestor, hostname, note, valid, name, secret, `read`, debug, `write`, test FROM clients"]
         params = []
         if id:
             query.append("WHERE id = %s")
@@ -556,7 +556,7 @@ class MySQL(ObjectReq):
             uquery.append("registered = now()")
         else:
             query.append("UPDATE clients SET")
-        for attr in ["identity", "hostname", "requestor", "secret", "note",
+        for attr in ["name", "hostname", "requestor", "secret", "note",
                       "valid", "read", "write", "debug", "test"]:
             val = kwargs.get(attr, None)
             if val is not None:
@@ -630,11 +630,11 @@ class MySQL(ObjectReq):
 
         if group or nogroup:
             subquery = []
-            for identity in (group or nogroup):
-                subquery.append("c.identity = %s")      # exact client
-                params.append(identity)
-                subquery.append("c.identity LIKE %s")   # whole subtree
-                params.append(identity + ".%")
+            for name in (group or nogroup):
+                subquery.append("c.name = %s")      # exact client
+                params.append(name)
+                subquery.append("c.name LIKE %s")   # whole subtree
+                params.append(name + ".%")
 
             query.append(" AND %s (%s)" % (self._get_not(group), " OR ".join(subquery)))
 
@@ -688,8 +688,12 @@ class MySQL(ObjectReq):
 
     def insertLastReceivedId(self, client, id):
         logging.debug("insertLastReceivedId: id %i for client %i(%s)" % (id, client.id, client.hostname))
-        self.query("INSERT INTO last_events(client_id, event_id, timestamp) VALUES(%s, %s, NOW())", (client.id, id), dml=True)
-        self.con.commit()
+        try:
+            self.query("INSERT INTO last_events(client_id, event_id, timestamp) VALUES(%s, %s, NOW())", (client.id, id), dml=True)
+            self.con.commit()
+        except Exception as e:
+            self.con.rollback()
+            raise
 
 
     def getLastEventId(self):
@@ -707,6 +711,24 @@ class MySQL(ObjectReq):
         return id
 
 
+    def load_maps(self):
+        try:
+            self.query("DELETE FROM tags")
+            for tag, num in self.tagmap.iteritems():
+                self.query("INSERT INTO tags(id, tag) VALUES (%s, %s)", (num, tag))
+            self.query("DELETE FROM categories")
+            for cat_subcat, num in self.catmap.iteritems():
+                catsplit = cat_subcat.split(".", 1)
+                category = catsplit[0]
+                subcategory = catsplit[1] if len(catsplit)>1 else None
+                self.query("INSERT INTO categories(id, category, subcategory, cat_subcat) VALUES (%s, %s, %s, %s)",
+                    (num, category, subcategory, cat_subcat))
+            self.con.commit()
+        except Exception as e:
+            self.con.rollback()
+            raise
+
+
 
 def expose(read=1, write=0, debug=0):
 
@@ -788,7 +810,7 @@ class Server(ObjectReq):
 
             auth = self.auth.authorize(self.req.env, self.req.client, self.req.path, method)
             if not auth:
-                raise self.req.error(message="I'm watching. Not authorized.", error=403, client=client.identity)
+                raise self.req.error(message="I'm watching. Not authorized.", error=403, client=client.name)
 
             # These args are not for handler
             args.pop("client", None)
@@ -933,13 +955,13 @@ class WardenHandler(ObjectReq):
         return res
 
 
-    def check_node(self, event, identity):
+    def check_node(self, event, name):
         try:
             ev_id = event['Node'][0]['Name'].lower()
         except (KeyError, TypeError):
             # Event does not bear valid Node attribute
             return [{"error": 422, "message": "Event does not bear valid Node attribute"}]
-        if ev_id != identity:
+        if ev_id != name:
             return [{"error": 422, "message": "Node does not correspond with saving client"}]
         return []
 
@@ -974,7 +996,7 @@ class WardenHandler(ObjectReq):
                 errs.extend(self.add_event_nums([i], events, v_errs))
                 continue
 
-            node_errs = self.check_node(event, self.req.client.identity)
+            node_errs = self.check_node(event, self.req.client.name)
             if node_errs:
                 errs.extend(self.add_event_nums([i], events, node_errs))
                 continue
@@ -1219,7 +1241,7 @@ def check_config():
 
 def list_clients(id=None):
     clients = server.handler.db.get_clients(id)
-    order = ["id", "registered", "requestor", "hostname", "service", "identity",
+    order = ["id", "registered", "requestor", "hostname", "name",
              "secret", "valid", "read", "debug", "write", "test", "note"]
     lines = [[str(getattr(client, col)) for col in order] for client in clients]
     col_width = [max(len(val) for val in col) for col in zip(*(lines+[order]))]
@@ -1283,19 +1305,23 @@ def modify_client(id, name, hostname, requestor, secret, note, valid, read, writ
         print >>sys.stderr, "Invalid id \"%s\"." % id
         return 254
 
-    existing_clients = server.handler.db.get_client_by_name([hostname], identity=name, secret=secret)
+    existing_clients = server.handler.db.get_client_by_name([hostname], name=name, secret=secret)
     if existing_clients:
-        print >>sys.stderr, "Clash with existing hostname/identity/secret: %s" % str(existing_clients)
+        print >>sys.stderr, "Clash with existing hostname/name/secret: %s" % str(existing_clients)
         return 254
 
     newid = server.handler.db.add_modify_client(
-        id=id, identity=name, hostname=hostname,
+        id=id, name=name, hostname=hostname,
         requestor=requestor, secret=secret, note=note, valid=valid,
         read=read, write=write, debug=debug, test=test)
 
     list_clients(id=newid)
 
 
+def load_maps():
+    server.handler.db.load_maps()
+
+
 def add_client_args(subargp, mod=False):
     subargp.add_argument("--help", action="help", help="show this help message and exit")
     if mod:
@@ -1376,6 +1402,17 @@ def get_args():
     subargp_list.add_argument("--id", action="store", type=int,
         help="client id", default=None)
 
+    subargp_loadmaps = subargp.add_parser("loadmaps", add_help=False,
+        description=
+            "Load 'categories' and 'tags' table from 'catmap_mysql.json' and 'tagmap_mysql.json'."
+            " Note that this is NOT needed for server at all, load them into db at will,"
+            " should you need to run your own specific SQL queries on data directly."
+            " Note also that previous content of both tables will be lost.",
+        help="load catmap and tagmap into db")
+    subargp_loadmaps.set_defaults(command=load_maps)
+    subargp_loadmaps.add_argument("--help", action="help",
+        help="show this help message and exit")
+
     return argp.parse_args()
 
 
-- 
GitLab