3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 2) as published by the FSF - Free Software
14 #include "logcollector.h"
17 #define NMAPG_HOST "Host: "
18 #define NMAPG_PORT "Ports:"
19 #define NMAPG_OPEN "open/"
20 #define NMAPG_STAT "Status:"
24 /** Function Prototypes **/
25 static char *__go_after(char *x, char *y);
26 static char *__get_port(char *str, char *proto, char *port, int msize);
30 /* Get port and protocol.
32 static char *__get_port(char *str, char *proto, char *port, int msize)
38 /* Removing white spaces */
54 strncpy(port, str, msize);
55 port[msize -1] = '\0';
59 /* Checking if the port is open */
60 q = __go_after(p, NMAPG_OPEN);
63 /* Port is not open */
68 /* Going to the start of protocol field */
81 /* Getting protocol */
92 strncpy(proto, str, msize);
93 proto[msize -1] = '\0';
96 /* Setting proto to null if port is not open */
101 /* Removing slashes */
120 /* Check if the string matches.
122 static char *__go_after(char *x, char *y)
127 /* X and Y must be not null */
139 /* String does not match */
140 if(strncmp(x,y,y_s) != 0)
151 /* Read Nmap grepable files */
152 void *read_nmapg(int pos, int *rc, int drop_it)
157 char str[OS_MAXSTR + 1];
158 char final_msg[OS_MAXSTR + 1];
159 char buffer[OS_MAXSTR + 1];
168 str[OS_MAXSTR] = '\0';
169 final_msg[OS_MAXSTR] = '\0';
170 buffer[OS_MAXSTR] = '\0';
175 while(fgets(str, OS_MAXSTR -OS_LOG_HEADER, logff[pos].fp) != NULL)
177 /* If need clear is set, we need to clear the line */
180 if((q = strchr(str, '\n')) != NULL)
187 /* Removing \n at the end of the string */
188 if ((q = strchr(str, '\n')) != NULL)
198 /* Do not get commented lines */
199 if((str[0] == '#') || (str[0] == '\0'))
206 q = __go_after(str, NMAPG_HOST);
213 /* Getting ip/hostname */
221 /* Setting the valid ip */
226 /* Getting the ports */
235 /* Now fixing p, to have the closing parenthesis */
240 /* q now should point to the ports */
241 p = __go_after(q, NMAPG_PORT);
244 /* Checking if no port is available */
245 p = __go_after(q, NMAPG_STAT);
255 /* Generating final msg */
256 snprintf(final_msg, OS_MAXSTR, "Host: %s, open ports:",
258 final_msg_s = OS_MAXSTR - ((strlen(final_msg) +3));
261 /* Getting port and protocol */
264 /* Avoid filling the buffer (3*port size). */
270 p = __get_port(p, proto, port, 9);
273 debug1("%s: Bad formated nmap grepable file (port).", ARGV0);
286 snprintf(buffer, OS_MAXSTR, " %s(%s)", port, proto);
287 strncat(final_msg, buffer, final_msg_s);
288 final_msg_s-=(strlen(buffer) +2);
290 }while(*p == ',' && (p++));
295 /* Sending message to queue */
296 if(SendMSG(logr_queue, final_msg, logff[pos].file,
299 merror(QUEUE_SEND, ARGV0);
300 if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
302 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
312 /* Handling errors */
315 merror("%s: Bad formated nmap grepable file.", ARGV0);