Skip to content

Commit

Permalink
feat: Delete of OSSECHIDS conditional directives.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdvendramini committed Aug 14, 2024
1 parent 604667d commit 9dace30
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 392 deletions.
100 changes: 0 additions & 100 deletions src/modules/rootcheck/src/check_open_ports.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,110 +13,10 @@
#include "headers/defs.h"
#include "rootcheck.h"

#ifndef OSSECHIDS

/* Prototypes */
static int connect_to_port(int proto, int port);
static void try_to_access_ports(void);

/* Global variables */
static int _ports_open;
static int open_ports_size;
static char open_ports_str[OS_SIZE_1024 + 1];


static int connect_to_port(int proto, int port)
{
int rc = 0;
int ossock;
struct sockaddr_in server;

if (proto == IPPROTO_UDP) {
if ((ossock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
return (0);
}
} else if (proto == IPPROTO_TCP) {
if ((ossock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
return (0);
}
} else {
return (0);
}

memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

if (connect(ossock, (struct sockaddr *)&server, sizeof(server)) == 0) {
rc = 1;
}

close(ossock);

return (rc);
}

static void try_to_access_ports()
{
int i;

for (i = 0; i <= 65535; i++) {
if (total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i)) {
char port_proto[64];

if (_ports_open == 0) {
snprintf(port_proto, 64, "\n %d (tcp),", i);
} else {
snprintf(port_proto, 64, "%d (tcp),", i);
}
strncat(open_ports_str, port_proto, open_ports_size);
open_ports_size -= strlen(port_proto) + 1;

_ports_open++;
}

if (total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i)) {
char port_proto[64];

if (_ports_open == 0) {
snprintf(port_proto, 64, "\n %d (udp),", i);
} else {
snprintf(port_proto, 64, "%d (udp),", i);
}

strncat(open_ports_str, port_proto, open_ports_size);
open_ports_size -= strlen(port_proto) + 1;

_ports_open++;
}

if (_ports_open >= 4) {
_ports_open = 0;
}
}

}
#endif

void check_open_ports()
{
#ifndef OSSECHIDS
memset(open_ports_str, '\0', OS_SIZE_1024 + 1);
open_ports_size = OS_SIZE_1024 - 1;
_ports_open = 0;

snprintf(open_ports_str, OS_SIZE_1024, "The following ports are open:");
open_ports_size -= strlen(open_ports_str) + 1;

/* Testing All ports */
try_to_access_ports();

open_ports_str[strlen(open_ports_str) - 1] = '\0';

notify_rk(ALERT_OK, open_ports_str);

#endif
return;
}

2 changes: 0 additions & 2 deletions src/modules/rootcheck/src/check_rc_pids.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,11 @@ static void loop_all_pids(const char *ps, pid_t max_pid, int *_errors, int *_tot
}

/* If we are run in the context of OSSEC-HIDS, sleep here (no rush) */
#ifdef OSSECHIDS
#ifdef WIN32
Sleep(rootcheck.tsleep);
#else
struct timeval timeout = {0, rootcheck.tsleep * 1000};
select(0, NULL, NULL, NULL, &timeout);
#endif
#endif

/* Everything fine, move on */
Expand Down
3 changes: 0 additions & 3 deletions src/modules/rootcheck/src/check_rc_ports.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,11 @@ static void test_ports(int proto, int *_errors, int *_total)
continue;
}

#ifdef OSSECHIDS
/* If we are in the context of OSSEC-HIDS, sleep here (no rush) */
#ifdef WIN32
Sleep(rootcheck.tsleep);
#else
struct timeval timeout = {0, rootcheck.tsleep * 1000};
select(0, NULL, NULL, NULL, &timeout);
#endif
#endif

if (!run_netstat(proto, i) && conn_port(proto, i)) {
Expand Down
22 changes: 0 additions & 22 deletions src/modules/rootcheck/src/check_rc_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ static int read_sys_file(const char *file_name, int do_read)

if (statbuf.st_uid == 0) {
char op_msg[OS_SIZE_1024 + 1];
#ifdef OSSECHIDS
const char op_msg_fmt[] = "File '%*s' is owned by root and has written permissions to anyone.";

const int size = snprintf(NULL, 0, op_msg_fmt, (int)strlen(file_name), file_name);
Expand All @@ -161,27 +160,6 @@ static int read_sys_file(const char *file_name, int do_read)
}

_sys_errors++;

#else
const char op_msg_fmt[] = "File '%*s' is: \n - owned by root,\n - has write permissions to anyone.";

const int size = snprintf(NULL, 0, op_msg_fmt, (int)strlen(file_name), file_name);

if (size >= 0) {
if ((size_t)size < sizeof(op_msg)) {
snprintf(op_msg, sizeof(op_msg), op_msg_fmt, (int)strlen(file_name), file_name);
} else {
const unsigned int surplus = size - sizeof(op_msg) + 1;
snprintf(op_msg, sizeof(op_msg), op_msg_fmt, (int)(strlen(file_name) - surplus), file_name);
}

notify_rk(ALERT_SYSTEM_CRIT, op_msg);
} else {
mtdebug2(ARGV0, "Error %d (%s) with snprintf with file %s", errno, strerror(errno), file_name);
}

_sys_errors++;
#endif
}
} else if ((statbuf.st_mode & S_ISUID) == S_ISUID) {
if (_suid) {
Expand Down
3 changes: 0 additions & 3 deletions src/modules/rootcheck/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* Foundation
*/

#ifdef OSSECHIDS
#include "shared.h"
#include "rootcheck.h"
#include "config/config.h"
Expand Down Expand Up @@ -104,5 +103,3 @@ cJSON *getRootcheckConfig(void) {

return root;
}

#endif /* OSSECHIDS */
103 changes: 0 additions & 103 deletions src/modules/rootcheck/src/rootcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,12 @@ char total_ports_tcp[65535 + 1];
#define ARGV0 "rootcheck"
#endif

#ifndef OSSECHIDS

/* Print help statement */
void help_rootcheck(char * home_path)
{
print_header();
print_out(" %s: -[Vhdtsr] [-c config] [-D dir]", ARGV0);
print_out(" -V Version and license message");
print_out(" -h Print this help message");
print_out(" -d Execute in debug mode. This parameter");
print_out(" can be specified multiple times");
print_out(" to increase the debug level.");
print_out(" -t Test configuration");
print_out(" -s Scan the whole system");
print_out(" -r Read all the files for kernel-based detection");
print_out(" -c <config> Configuration file to use");
print_out(" -D <dir> Directory to chroot into (default: %s)", home_path);
print_out(" ");
os_free(home_path);
exit(1);
}

int main(int argc, char **argv)
{
int test_config = 0;
const char *cfg = "./rootcheck.conf";
char * home_path = w_homedir(argv[0]);

#else

int rootcheck_init(int test_config)
{
const char *cfg = OSSECCONF;

#endif /* OSSECHIDS */

int c;

#ifndef OSSECHIDS
if (chdir(home_path) == -1) {
merror_exit(CHDIR_ERROR, home_path, errno, strerror(errno));
}
#endif /* OSSECHIDS */

/* Zero the structure, initialize default values */
rootcheck.workdir = NULL;
rootcheck.basedir = NULL;
Expand Down Expand Up @@ -115,46 +77,6 @@ int rootcheck_init(int test_config)
c++;
}

#ifndef OSSECHIDS
rootcheck.notify = SYSLOG_RK;
rootcheck.daemon = 0;
while ((c = getopt(argc, argv, "VstrdhD:c:")) != -1) {
switch (c) {
case 'V':
print_version();
break;
case 'h':
help_rootcheck(home_path);
break;
case 'd':
nowDebug();
break;
case 'D':
if (!optarg) {
mterror_exit(ARGV0, "-D needs an argument");
}
rootcheck.workdir = optarg;
break;
case 'c':
if (!optarg) {
mterror_exit(ARGV0, "-c needs an argument");
}
cfg = optarg;
break;
case 's':
rootcheck.scanall = 1;
break;
case 't':
test_config = 1;
break;
case 'r':
rootcheck.readall = 1;
break;
default:
help_rootcheck(home_path);
break;
}
}
#ifdef WIN32
/* Start Winsock */
{
Expand All @@ -165,8 +87,6 @@ int rootcheck_init(int test_config)
}
#endif /* WIN32 */

#endif /* OSSECHIDS */

/* Check if the configuration is present */
if (File_DateofChange(cfg) < 0) {
mterror(ARGV0, "Configuration file '%s' not found", cfg);
Expand Down Expand Up @@ -211,22 +131,11 @@ int rootcheck_init(int test_config)
}
#endif

/* Set default values */
#ifndef OSSECHIDS
mdebug1(WAZUH_HOMEDIR, home_path);
if (rootcheck.workdir == NULL) {
rootcheck.workdir = home_path;
}
#endif

#ifdef OSSECHIDS
/* Start up message */
#ifdef WIN32
mtinfo(ARGV0, STARTUP_MSG, getpid());
#endif /* WIN32 */

#endif /* OSSECHIDS */

/* Initialize rk list */
rk_sys_name = (char **) calloc(MAX_RK_SYS + 2, sizeof(char *));
rk_sys_file = (char **) calloc(MAX_RK_SYS + 2, sizeof(char *));
Expand All @@ -236,18 +145,6 @@ int rootcheck_init(int test_config)
rk_sys_name[0] = NULL;
rk_sys_file[0] = NULL;

#ifndef OSSECHIDS
#ifndef WIN32
/* Start signal handling */
StartSIG(ARGV0);
rootcheck_connect();
#endif
mtdebug1(ARGV0, "Running run_rk_check");
run_rk_check();

mtdebug1(ARGV0, "Leaving...");
os_free(home_path);
#endif /* OSSECHIDS */
return (0);
}

Expand Down
Loading

0 comments on commit 9dace30

Please sign in to comment.