1 /* @(#) $Id: ./src/logcollector/read_nmapg.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
15 #include "logcollector.h"
18 #define NMAPG_HOST "Host: "
19 #define NMAPG_PORT "Ports:"
20 #define NMAPG_OPEN "open/"
21 #define NMAPG_STAT "Status:"
25 /** Function Prototypes **/
26 static char *__go_after(char *x, char *y);
27 static char *__get_port(char *str, char *proto, char *port, int msize);
31 /* Get port and protocol.
33 static char *__get_port(char *str, char *proto, char *port, int msize)
39 /* Removing white spaces */
55 strncpy(port, str, msize);
56 port[msize -1] = '\0';
60 /* Checking if the port is open */
61 q = __go_after(p, NMAPG_OPEN);
64 /* Port is not open */
69 /* Going to the start of protocol field */
82 /* Getting protocol */
93 strncpy(proto, str, msize);
94 proto[msize -1] = '\0';
97 /* Setting proto to null if port is not open */
102 /* Removing slashes */
121 /* Check if the string matches.
123 static char *__go_after(char *x, char *y)
128 /* X and Y must be not null */
140 /* String does not match */
141 if(strncmp(x,y,y_s) != 0)
152 /* Read Nmap grepable files */
153 void *read_nmapg(int pos, int *rc, int drop_it)
158 char str[OS_MAXSTR + 1];
159 char final_msg[OS_MAXSTR + 1];
160 char buffer[OS_MAXSTR + 1];
169 str[OS_MAXSTR] = '\0';
170 final_msg[OS_MAXSTR] = '\0';
171 buffer[OS_MAXSTR] = '\0';
176 while(fgets(str, OS_MAXSTR -OS_LOG_HEADER, logff[pos].fp) != NULL)
178 /* If need clear is set, we need to clear the line */
181 if((q = strchr(str, '\n')) != NULL)
188 /* Removing \n at the end of the string */
189 if ((q = strchr(str, '\n')) != NULL)
199 /* Do not get commented lines */
200 if((str[0] == '#') || (str[0] == '\0'))
207 q = __go_after(str, NMAPG_HOST);
214 /* Getting ip/hostname */
222 /* Setting the valid ip */
227 /* Getting the ports */
236 /* Now fixing p, to have the closing parenthesis */
241 /* q now should point to the ports */
242 p = __go_after(q, NMAPG_PORT);
245 /* Checking if no port is available */
246 p = __go_after(q, NMAPG_STAT);
256 /* Generating final msg */
257 snprintf(final_msg, OS_MAXSTR, "Host: %s, open ports:",
259 final_msg_s = OS_MAXSTR - ((strlen(final_msg) +3));
262 /* Getting port and protocol */
265 /* Avoid filling the buffer (3*port size). */
271 p = __get_port(p, proto, port, 9);
274 debug1("%s: Bad formated nmap grepable file (port).", ARGV0);
287 snprintf(buffer, OS_MAXSTR, " %s(%s)", port, proto);
288 strncat(final_msg, buffer, final_msg_s);
289 final_msg_s-=(strlen(buffer) +2);
291 }while(*p == ',' && (p++));
296 /* Sending message to queue */
297 if(SendMSG(logr_queue, final_msg, logff[pos].file,
300 merror(QUEUE_SEND, ARGV0);
301 if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
303 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
313 /* Handling errors */
316 merror("%s: Bad formated nmap grepable file.", ARGV0);