1 /* Copyright (C) 2009 Trend Micro Inc.
2 * All right reserved.
3 *
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
7 * Foundation
8 */
9
10 /* This tool will clear the event statistics */
11
12 #include "shared.h"
13
14 #undef ARGV0
15 #define ARGV0 "clear_stats"
16
17 /* Prototypes */
18 static void helpmsg(void) __attribute__((noreturn));
19
20
helpmsg()21 static void helpmsg()
22 {
23 printf("\nOSSEC HIDS %s: Clear the events stats (averages).\n", ARGV0);
24 printf("Available options:\n");
25 printf("\t-h This help message.\n");
26 printf("\t-a Clear all the stats (averages).\n");
27 printf("\t-d Clear the daily averages.\n");
28 printf("\t-w Clear the weekly averages.\n\n");
29 exit(1);
30 }
31
main(int argc,char ** argv)32 int main(int argc, char **argv)
33 {
34 int clear_daily = 0;
35 int clear_weekly = 0;
36
37 const char *dir = DEFAULTDIR;
38 const char *group = GROUPGLOBAL;
39 const char *user = USER;
40 gid_t gid;
41 uid_t uid;
42
43 /* Set the name */
44 OS_SetName(ARGV0);
45
46 /* user arguments */
47 if (argc != 2) {
48 helpmsg();
49 }
50
51 /* Get the group name */
52 gid = Privsep_GetGroup(group);
53 uid = Privsep_GetUser(user);
54 if (uid == (uid_t) - 1 || gid == (gid_t) - 1) {
55 ErrorExit(USER_ERROR, ARGV0, user, group);
56 }
57
58 /* Set the group */
59 if (Privsep_SetGroup(gid) < 0) {
60 ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
61 }
62
63 /* Chroot to the default directory */
64 if (Privsep_Chroot(dir) < 0) {
65 ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
66 }
67
68 /* Inside chroot now */
69 nowChroot();
70
71 /* Set the user */
72 if (Privsep_SetUser(uid) < 0) {
73 ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
74 }
75
76 /* User options */
77 if (strcmp(argv[1], "-h") == 0) {
78 helpmsg();
79 } else if (strcmp(argv[1], "-a") == 0) {
80 clear_daily = 1;
81 clear_weekly = 1;
82 } else if (strcmp(argv[1], "-d") == 0) {
83 clear_daily = 1;
84 } else if (strcmp(argv[1], "-w") == 0) {
85 clear_weekly = 1;
86 } else {
87 printf("\n** Invalid option '%s'.\n", argv[1]);
88 helpmsg();
89 }
90
91 /* Clear daily files */
92 if (clear_daily) {
93 const char *daily_dir = STATQUEUE;
94 DIR *daily;
95 struct dirent *entry;
96
97 daily = opendir(daily_dir);
98 if (!daily) {
99 ErrorExit("%s: Unable to open: '%s'", ARGV0, daily_dir);
100 }
101
102 while ((entry = readdir(daily)) != NULL) {
103 char full_path[OS_MAXSTR + 1];
104
105 /* Do not even attempt to delete . and .. :) */
106 if ((strcmp(entry->d_name, ".") == 0) ||
107 (strcmp(entry->d_name, "..") == 0)) {
108 continue;
109 }
110
111 /* Remove file */
112 full_path[OS_MAXSTR] = '\0';
113 snprintf(full_path, OS_MAXSTR, "%s/%s", daily_dir, entry->d_name);
114 if ((unlink(full_path)) != 0) {
115 ErrorExit("%s: ERROR: Cannot delete file %s: %s", ARGV0, full_path, strerror(errno));
116 }
117 }
118
119 closedir(daily);
120 }
121
122 /* Clear weekly averages */
123 if (clear_weekly) {
124 int i = 0;
125 while (i <= 6) {
126 const char *daily_dir = STATWQUEUE;
127 char dir_path[OS_MAXSTR + 1];
128 DIR *daily;
129 struct dirent *entry;
130
131 snprintf(dir_path, OS_MAXSTR, "%s/%d", daily_dir, i);
132 daily = opendir(dir_path);
133 if (!daily) {
134 ErrorExit("%s: Unable to open: '%s' (no stats)",
135 ARGV0, dir_path);
136 }
137
138 while ((entry = readdir(daily)) != NULL) {
139 char full_path[OS_MAXSTR + 1];
140
141 /* Do not even attempt to delete . and .. :) */
142 if ((strcmp(entry->d_name, ".") == 0) ||
143 (strcmp(entry->d_name, "..") == 0)) {
144 continue;
145 }
146
147 /* Remove file */
148 full_path[OS_MAXSTR] = '\0';
149 snprintf(full_path, OS_MAXSTR, "%s/%s", dir_path,
150 entry->d_name);
151 if ((unlink(full_path)) != 0) {
152 ErrorExit("%s: ERROR: Cannot delete file %s: %s", ARGV0, full_path, strerror(errno));
153 }
154 }
155
156 i++;
157 closedir(daily);
158 }
159 }
160
161 printf("\n** Internal stats clear.\n\n");
162 return (0);
163 }
164
165