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)
108 merror("%s: -u needs an argument",ARGV0);
116 merror("%s: -u needs an argument",ARGV0);
124 merror("%s: -u needs an argument",ARGV0);
138 /* Getting the group name */
139 gid = Privsep_GetGroup(group);
140 uid = Privsep_GetUser(user);
143 ErrorExit(USER_ERROR, ARGV0, user, group);
147 /* Setting the group */
148 if(Privsep_SetGroup(gid) < 0)
150 ErrorExit(SETGID_ERROR,ARGV0, group);
154 /* Chrooting to the default directory */
155 if(Privsep_Chroot(dir) < 0)
157 ErrorExit(CHROOT_ERROR, ARGV0, dir);
161 /* Inside chroot now */
165 /* Setting the user */
166 if(Privsep_SetUser(uid) < 0)
168 ErrorExit(SETUID_ERROR, ARGV0, user);
173 /* Getting servers hostname */
174 memset(shost, '\0', 512);
175 if(gethostname(shost, 512 -1) != 0)
177 strncpy(shost, "localhost", 32);
183 /* Listing available agents. */
188 printf("\nOSSEC HIDS %s. List of available agents:",
190 printf("\n ID: 000, Name: %s (server), IP: 127.0.0.1, "
191 "Active/Local\n", shost);
195 printf("000,%s (server),127.0.0.1,Active/Local,\n", shost);
197 print_agents(1, active_only, csv_output);
204 /* Update syscheck database. */
207 /* Cleaning all agents (and server) db. */
208 if(strcmp(agent_id, "all") == 0)
211 struct dirent *entry;
213 sys_dir = opendir(SYSCHECK_DIR);
216 ErrorExit("%s: Unable to open: '%s'", ARGV0, SYSCHECK_DIR);
219 while((entry = readdir(sys_dir)) != NULL)
222 char full_path[OS_MAXSTR +1];
224 /* Do not even attempt to delete . and .. :) */
225 if((strcmp(entry->d_name,".") == 0)||
226 (strcmp(entry->d_name,"..") == 0))
231 snprintf(full_path, OS_MAXSTR,"%s/%s", SYSCHECK_DIR,
234 fp = fopen(full_path, "w");
239 if(entry->d_name[0] == '.')
246 printf("\n** Integrity check database updated.\n\n");
250 else if((strcmp(agent_id, "000") == 0) ||
251 (strcmp(agent_id, "local") == 0))
253 char final_dir[1024];
255 snprintf(final_dir, 1020, "/%s/syscheck", SYSCHECK_DIR);
257 fp = fopen(final_dir, "w");
265 /* Deleting cpt file */
266 snprintf(final_dir, 1020, "/%s/.syscheck.cpt", SYSCHECK_DIR);
268 fp = fopen(final_dir, "w");
275 printf("\n** Integrity check database updated.\n\n");
279 /* Database from remote agents. */
287 i = OS_IsAllowedID(&keys, agent_id);
290 printf("\n** Invalid agent id '%s'.\n", agent_id);
294 /* Deleting syscheck */
295 delete_syscheck(keys.keyentries[i]->name,
296 keys.keyentries[i]->ip->ip, 0);
298 printf("\n** Integrity check database updated.\n\n");
304 /* Printing information from an agent. */
308 char final_ip[128 +1];
309 char final_mask[128 +1];
313 if((strcmp(agent_id, "000") == 0) ||
314 (strcmp(agent_id, "local") == 0))
316 printf("\nIntegrity checking changes for local system '%s - %s':\n",
320 printf("Detailed information for entries matching: '%s'\n",
326 csv_output, zero_counter);
328 else if(strchr(agent_id, '@'))
332 printf("Detailed information for entries matching: '%s'\n",
335 print_syscheck(agent_id, NULL, fname, registry_only, 0,
336 csv_output, zero_counter);
343 i = OS_IsAllowedID(&keys, agent_id);
346 printf("\n** Invalid agent id '%s'.\n", agent_id);
350 /* Getting netmask from ip. */
351 final_ip[128] = '\0';
352 final_mask[128] = '\0';
353 getNetmask(keys.keyentries[i]->ip->netmask, final_mask, 128);
354 snprintf(final_ip, 128, "%s%s",keys.keyentries[i]->ip->ip,
359 printf("\nIntegrity changes for 'Windows Registry' of"
360 " agent '%s (%s) - %s':\n",
361 keys.keyentries[i]->name, keys.keyentries[i]->id,
366 printf("\nIntegrity changes for agent "
368 keys.keyentries[i]->name, keys.keyentries[i]->id,
374 printf("Detailed information for entries matching: '%s'\n",
377 print_syscheck(keys.keyentries[i]->name,
378 keys.keyentries[i]->ip->ip, fname,
379 registry_only, 0, csv_output, zero_counter);
388 printf("\n** Invalid argument combination.\n");