1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "logcollector.h"
13 #define NMAPG_HOST "Host: "
14 #define NMAPG_PORT "Ports:"
15 #define NMAPG_OPEN "open/"
16 #define NMAPG_STAT "Status:"
19 static char *__go_after(char *x, const char *y);
20 static char *__get_port(char *str, char *proto, char *port, size_t msize);
23 /* Get port and protocol */
24 static char *__get_port(char *str, char *proto, char *port, size_t msize)
29 /* Remov whitespace */
43 strncpy(port, str, msize);
44 port[msize - 1] = '\0';
46 /* Check if the port is open */
47 q = __go_after(p, NMAPG_OPEN);
49 /* Port is not open */
53 /* Going to the start of protocol field */
72 strncpy(proto, str, msize);
73 proto[msize - 1] = '\0';
75 /* Set proto to null if port is not open */
95 /* Check if the string matches */
96 static char *__go_after(char *x, const char *y)
101 /* X and Y must be not null */
113 /* String does not match */
114 if (strncmp(x, y, y_s) != 0) {
123 /* Read Nmap grepable files */
124 void *read_nmapg(int pos, int *rc, int drop_it)
129 char str[OS_MAXSTR + 1];
130 char final_msg[OS_MAXSTR + 1];
131 char buffer[OS_MAXSTR + 1];
140 str[OS_MAXSTR] = '\0';
141 final_msg[OS_MAXSTR] = '\0';
142 buffer[OS_MAXSTR] = '\0';
147 while (fgets(str, OS_MAXSTR - OS_LOG_HEADER, logff[pos].fp) != NULL) {
148 /* If need clear is set, we need to clear the line */
150 if ((q = strchr(str, '\n')) != NULL) {
156 /* Remove \n at the end of the string */
157 if ((q = strchr(str, '\n')) != NULL) {
163 /* Do not get commented lines */
164 if ((str[0] == '#') || (str[0] == '\0')) {
169 q = __go_after(str, NMAPG_HOST);
174 /* Get ip/hostname */
180 /* Setting the valid ip */
190 /* Now fixing p, to have the closing parenthesis */
194 /* q now should point to the ports */
195 p = __go_after(q, NMAPG_PORT);
197 /* Check if no port is available */
198 p = __go_after(q, NMAPG_STAT);
206 /* Generate final msg */
207 snprintf(final_msg, OS_MAXSTR, "Host: %s, open ports:",
209 final_msg_s = OS_MAXSTR - ((strlen(final_msg) + 3));
211 /* Get port and protocol */
213 /* Avoid filling the buffer (3*port size) */
214 if (final_msg_s < 27) {
218 p = __get_port(p, proto, port, 9);
220 debug1("%s: Bad formatted nmap grepable file (port).", ARGV0);
225 if (proto[0] == '\0') {
230 snprintf(buffer, OS_MAXSTR, " %s(%s)", port, proto);
231 strncat(final_msg, buffer, final_msg_s);
232 final_msg_s -= (strlen(buffer) + 2);
234 } while (*p == ',' && (p++));
237 /* Send message to queue */
238 if (SendMSG(logr_queue, final_msg, logff[pos].file,
240 merror(QUEUE_SEND, ARGV0);
241 if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
242 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
253 merror("%s: Bad formatted nmap grepable file.", ARGV0);