f051dbdf1456a8506dd0740f6339d108e265b3f3
[gnucomo.git] / src / gcm_daemon / classes / gnucomo.process_log.php
1 <?PHP
2
3 /**********************************************************************************
4 **  (c) Copyright 2002, Brenno J.S.A.A.F. de Winter, De Winter Information Soltions
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***********************************************************************************/
8
9 function linux_log ()
10 {
11   /* The function linux_log will seperate the logline in several elements. This will
12    * ease the work of recognizing the type of logline. Once this has been detected
13    * the correct module will start using the data for a log_adv-table.
14    * INPUT    : NONE
15    * GLOBALS  : $dbms (database class containing the logline)
16    * OUTPUT   : Status of success ('TRUE' for success and 'FALSE' for failure
17    */
18    
19    global $dbms;
20    global $developrelease;
21    
22    $local_log_string = str_replace("  ", " ", $dbms->db_result_row[6]);
23    $local_logline_array = explode (" ", $local_log_string);
24
25    switch (strtolower($local_logline_array[4]))
26    {
27      case "kernel:":
28        //This is a kernel logline now discover which type kernel-record we have
29        
30        //Detect if this is a network-line
31        if (strtolower(substr($local_logline_array[5],0,3)) == "in=")
32        {
33           //this is a networkline call the processing the routines
34           $local_result = linux_kernel_network();
35           return $local_result;
36        }
37        else
38        {
39           if (strtolower($local_logline_array[4]) == 'device')
40           {
41              $local_result = linux_kernel_device();
42              return $local_result;
43           }
44           else
45           {
46              if ($developrelease == 'TRUE')
47              {
48                 $local_failing_string = "Failing string: ".$dbms->db_result_row[5];
49                 syslog (LOG_INFO, "Unrecognized kernelline:".$local_log_string);
50                 syslog (LOG_INFO, $local_failing_string);
51              }
52
53              return "FALSE";
54      
55           }
56        }     //  <===  We were missing this brace
57        break;
58
59      default:
60        break;
61    }
62 }
63
64 function linux_kernel_network() {
65
66    /* This function is able to deal with the output of kernel-network messages
67     * coming from iptables and other similar tools. When elements are found
68     * that cannot be identified a notification will be written to the logbook
69     * for easy expansion of this routine.
70     * INPUT    : NONE
71     * GLOBALS  : $dbms, $dbms_working;
72     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
73     */
74     global $dbms;
75     global $dbms_working;
76
77     $local_log_string = str_replace("  ", " ", $dbms->db_result_row[6]);
78     $local_logline_array = explode (" ", $local_log_string);
79     $local_sql_1 = "INSERT INTO log_adv_kernel_network"; //BASIC STATEMENT
80     $local_sql_2 = "logid, detailed_table"; //FIELDS
81     $local_sql_3 = "'".$dbms->db_result_row[0]."', 'kernel_network'"; //VALUES
82     $local_len   = 0;
83     $local_id    = 0;
84
85     for ($i = 4; $i <= ( count($local_logline_array) - 1); $i++) {
86         //Process each element by exploding this based on the sign: =
87         $local_element = explode("=", $local_logline_array[$i]);
88         switch (strtolower($local_element[0])) {
89
90           case "in":
91             $local_sql_2 .= ", device_in";
92             $local_sql_3 .= ", '".$local_element[1]."'";
93             break;
94
95           case "out":
96             $local_sql_2 .= ", device_out";
97             $local_sql_3 .= ", '".$local_element[1]."'";
98             break;
99
100           case "mac":
101             $local_sql_2 .= ", hw_address";
102             $local_sql_3 .= ", '".$local_element[1]."'";
103             break;
104    
105           case "src":
106             $local_sql_2 .= ", source_ip";
107             $local_sql_3 .= ", '".$local_element[1]."'";
108             break;
109
110          case "dst":
111             $local_sql_2 .= ", destination_ip";
112             $local_sql_3 .= ", '".$local_element[1]."'";
113             break;
114
115          case "len":
116             if ($local_len == 0) { 
117                $local_sql_2 .= ", packet_length";
118                $local_len++;
119             } else {
120                $local_sql_2 .= ", body_len";
121             }
122
123             $local_sql_3 .= ", '".$local_element[1]."'";
124             break;
125
126          case "tos":
127             $local_sql_2 .= ", tos_bit";
128             $local_sql_3 .= ", '".$local_element[1]."'";
129             break;
130
131          case "prec":
132             $local_sql_2 .= ", prec_bit";
133             $local_sql_3 .= ", '".$local_element[1]."'";
134             break;
135
136          case "ttl":
137             $local_sql_2 .= ", ttl";
138             $local_sql_3 .= ", '".$local_element[1]."'";
139             break;
140
141         case "id":
142             
143             if ($local_id == 0) {
144                $local_sql_2 .= ", header_id";
145                $local_sql_3 .= ", '".$local_element[1]."'";
146                $local_id = 1;
147             }
148             break;
149
150         case "proto":
151             $local_sql_2 .= ", protocol";
152             $local_sql_3 .= ", '".$local_element[1]."'";
153             if ($local_element[1] == 'ICMP') {
154                $local_icmp = true;
155             }
156             break;
157
158        case "spt":
159             $local_sql_2 .= ", destination_port";
160             $local_sql_3 .= ", '".$local_element[1]."'";
161             break;
162
163        case "dpt":
164             $local_sql_2 .= ", source_port";
165             $local_sql_3 .= ", '".$local_element[1]."'";
166             break;
167
168        case "window":
169             $local_sql_2 .= ", window";
170             $local_sql_3 .= ", '".$local_element[1]."'";
171             break;
172        
173        case "urgp":
174            $local_sql_2 .= ", urgp";
175            $local_sql_3 .= ", '".$local_element[1]."'";
176            break;
177
178        case "rst":
179            $local_sql_2 .= ", rst";
180            $local_sql_3 .= ", true";
181            break;
182
183        case "syn":
184            $local_sql_2 .= ", syn";
185            $local_sql_3 .= ", true";
186            break;
187
188        case "df":
189           $local_sql_2 .= ", df";
190           $local_sql_3 .= ", true";
191           break;
192
193        case "type":
194            $local_sql_2 .= ", type";
195            $local_sql_3 .= ", '".$local_element[1]."'";
196            break;
197
198        case "code":
199           $local_sql_2 .= ", code";
200           $local_sql_3 .= ", '".$local_element[1]."'";
201           break;
202
203        case "seq":
204           $local_sql_2 .= ", sequence_number";
205           $local_sql_3 .= ", '".$local_element[1]."'";
206           break;      
207
208        case "res":          
209           $local_sql_2 .= ", res";
210           $local_sql_3 .= ", '".$local_element[1]."'";
211           break;
212
213        case "[src":
214           /*This record is different. In ICMP information is sometimes returned on an original packet.
215            * When the brackets are used a second line will be added to the 
216            * log_adv_kernel_network-table. For that reason the processing into the database will be
217            * done here as well. After that a new insert-string will be created.
218            */
219
220           //Enter the data into the database
221           $local_sql = $local_sql_1." (".$local_sql_2.") VALUES (".$local_sql_3.")";  
222           $dbms_working->query($local_sql);
223
224           $local_sql_1 = "INSERT INTO log_adv_kernel_network"; //BASIC STATEMENT
225           $local_sql_2 = "logid, detailed_table"; //FIELDS
226           $local_sql_3 = "'".$dbms->db_result_row[0]."', 'kernel_network'"; //VALUES
227           $local_len   = 0;
228           $local_id    = 0;
229           break;   
230       default: 
231 /*           $local_element[0];
232            syslog(LOG_INFO, "Unrecognized kernel/network entry: ".$local_element[0]);
233
234 */
235      }
236
237  }
238
239
240  //Now that the data is complete create the SQL-statement
241  $local_sql = $local_sql_1." (".$local_sql_2.") VALUES (".$local_sql_3.")";
242  $dbms_working->query($local_sql);
243  RETURN "TRUE";
244
245  }
246  function linux_kernel_device() {
247    /* This function is able to deal with the output of kernel-network messages
248     * coming from device related processes. Typically networkcard and other
249     * hardware-related data will show-up here
250     * INPUT    : NONE
251     * GLOBALS  : $dbms, $dbms_working
252     * OUTPUT   : "TRUE" for success and "FALSE" for failure.
253     */
254  
255     global $dbms, $dbms_working;
256
257  }
258  
259 ?>