1 /* @(#) $Id: ./src/util/syscheck_control.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
14 #include "addagent/manage_agents.h"
19 #define ARGV0 "syscheck_control"
25 printf("\nOSSEC HIDS %s: Manages the integrity checking database.\n",
27 printf("Available options:\n");
28 printf("\t-h This help message.\n");
29 printf("\t-l List available (active or not) agents.\n");
30 printf("\t-lc List only active agents.\n");
31 printf("\t-u <id> Updates (clear) the database for the agent.\n");
32 printf("\t-u all Updates (clear) the database for all agents.\n");
33 printf("\t-i <id> List modified files for the agent.\n");
34 printf("\t-r -i <id> List modified registry entries for the agent "
36 printf("\t-f <file> Prints information about a modified file.\n");
37 printf("\t-z Used with the -f, zeroes the auto-ignore counter.\n");
38 printf("\t-d Used with the -f, ignores that file.\n");
39 printf("\t-s Changes the output to CSV (comma delimited).\n");
45 int main(int argc, char **argv)
47 char *dir = DEFAULTDIR;
48 char *group = GROUPGLOBAL;
50 char *agent_id = NULL;
55 int c = 0, info_agent = 0, update_syscheck = 0,
56 list_agents = 0, zero_counter = 0,
58 int active_only = 0, csv_output = 0;
64 /* Setting the name */
75 while((c = getopt(argc, argv, "VhzrDdlcsu:i:f:")) != -1)
109 merror("%s: -u needs an argument",ARGV0);
117 merror("%s: -u needs an argument",ARGV0);
125 merror("%s: -u needs an argument",ARGV0);
139 /* Getting the group name */
140 gid = Privsep_GetGroup(group);
141 uid = Privsep_GetUser(user);
144 ErrorExit(USER_ERROR, ARGV0, user, group);
148 /* Setting the group */
149 if(Privsep_SetGroup(gid) < 0)
151 ErrorExit(SETGID_ERROR,ARGV0, group);
155 /* Chrooting to the default directory */
156 if(Privsep_Chroot(dir) < 0)
158 ErrorExit(CHROOT_ERROR, ARGV0, dir);
162 /* Inside chroot now */
166 /* Setting the user */
167 if(Privsep_SetUser(uid) < 0)
169 ErrorExit(SETUID_ERROR, ARGV0, user);
174 /* Getting servers hostname */
175 memset(shost, '\0', 512);
176 if(gethostname(shost, 512 -1) != 0)
178 strncpy(shost, "localhost", 32);
184 /* Listing available agents. */
189 printf("\nOSSEC HIDS %s. List of available agents:",
191 printf("\n ID: 000, Name: %s (server), IP: 127.0.0.1, "
192 "Active/Local\n", shost);
196 printf("000,%s (server),127.0.0.1,Active/Local,\n", shost);
198 print_agents(1, active_only, csv_output);
205 /* Update syscheck database. */
208 /* Cleaning all agents (and server) db. */
209 if(strcmp(agent_id, "all") == 0)
212 struct dirent *entry;
214 sys_dir = opendir(SYSCHECK_DIR);
217 ErrorExit("%s: Unable to open: '%s'", ARGV0, SYSCHECK_DIR);
220 while((entry = readdir(sys_dir)) != NULL)
223 char full_path[OS_MAXSTR +1];
225 /* Do not even attempt to delete . and .. :) */
226 if((strcmp(entry->d_name,".") == 0)||
227 (strcmp(entry->d_name,"..") == 0))
232 snprintf(full_path, OS_MAXSTR,"%s/%s", SYSCHECK_DIR,
235 fp = fopen(full_path, "w");
240 if(entry->d_name[0] == '.')
247 printf("\n** Integrity check database updated.\n\n");
251 else if((strcmp(agent_id, "000") == 0) ||
252 (strcmp(agent_id, "local") == 0))
254 char final_dir[1024];
256 snprintf(final_dir, 1020, "/%s/syscheck", SYSCHECK_DIR);
258 fp = fopen(final_dir, "w");
266 /* Deleting cpt file */
267 snprintf(final_dir, 1020, "/%s/.syscheck.cpt", SYSCHECK_DIR);
269 fp = fopen(final_dir, "w");
276 printf("\n** Integrity check database updated.\n\n");
280 /* Database from remote agents. */
288 i = OS_IsAllowedID(&keys, agent_id);
291 printf("\n** Invalid agent id '%s'.\n", agent_id);
295 /* Deleting syscheck */
296 delete_syscheck(keys.keyentries[i]->name,
297 keys.keyentries[i]->ip->ip, 0);
299 printf("\n** Integrity check database updated.\n\n");
305 /* Printing information from an agent. */
309 char final_ip[128 +1];
310 char final_mask[128 +1];
314 if((strcmp(agent_id, "000") == 0) ||
315 (strcmp(agent_id, "local") == 0))
317 printf("\nIntegrity checking changes for local system '%s - %s':\n",
321 printf("Detailed information for entries matching: '%s'\n",
327 csv_output, zero_counter);
329 else if(strchr(agent_id, '@'))
333 printf("Detailed information for entries matching: '%s'\n",
336 print_syscheck(agent_id, NULL, fname, registry_only, 0,
337 csv_output, zero_counter);
344 i = OS_IsAllowedID(&keys, agent_id);
347 printf("\n** Invalid agent id '%s'.\n", agent_id);
351 /* Getting netmask from ip. */
352 final_ip[128] = '\0';
353 final_mask[128] = '\0';
354 getNetmask(keys.keyentries[i]->ip->netmask, final_mask, 128);
355 snprintf(final_ip, 128, "%s%s",keys.keyentries[i]->ip->ip,
360 printf("\nIntegrity changes for 'Windows Registry' of"
361 " agent '%s (%s) - %s':\n",
362 keys.keyentries[i]->name, keys.keyentries[i]->id,
367 printf("\nIntegrity changes for agent "
369 keys.keyentries[i]->name, keys.keyentries[i]->id,
375 printf("Detailed information for entries matching: '%s'\n",
378 print_syscheck(keys.keyentries[i]->name,
379 keys.keyentries[i]->ip->ip, fname,
380 registry_only, 0, csv_output, zero_counter);
389 printf("\n** Invalid argument combination.\n");