new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / util / clear_stats.c
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
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
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             unlink(full_path);
115         }
116
117         closedir(daily);
118     }
119
120     /* Clear weekly averages */
121     if (clear_weekly) {
122         int i = 0;
123         while (i <= 6) {
124             const char *daily_dir = STATWQUEUE;
125             char dir_path[OS_MAXSTR + 1];
126             DIR *daily;
127             struct dirent *entry;
128
129             snprintf(dir_path, OS_MAXSTR, "%s/%d", daily_dir, i);
130             daily = opendir(dir_path);
131             if (!daily) {
132                 ErrorExit("%s: Unable to open: '%s' (no stats)",
133                           ARGV0, dir_path);
134             }
135
136             while ((entry = readdir(daily)) != NULL) {
137                 char full_path[OS_MAXSTR + 1];
138
139                 /* Do not even attempt to delete . and .. :) */
140                 if ((strcmp(entry->d_name, ".") == 0) ||
141                         (strcmp(entry->d_name, "..") == 0)) {
142                     continue;
143                 }
144
145                 /* Remove file */
146                 full_path[OS_MAXSTR] = '\0';
147                 snprintf(full_path, OS_MAXSTR, "%s/%s", dir_path,
148                          entry->d_name);
149                 unlink(full_path);
150             }
151
152             i++;
153             closedir(daily);
154         }
155     }
156
157     printf("\n** Internal stats clear.\n\n");
158     return (0);
159 }
160