new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / util / syscheck_update.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 #include "addagent/manage_agents.h"
11 #include "sec.h"
12
13 #undef ARGV0
14 #define ARGV0 "syscheck_update"
15
16 /* Prototypes */
17 static void helpmsg(void) __attribute__((noreturn));
18
19
20 static void helpmsg()
21 {
22     printf("\nOSSEC HIDS %s: Updates (clears) the integrity check database.\n", ARGV0);
23     printf("Available options:\n");
24     printf("\t-h       This help message.\n");
25     printf("\t-l       List available agents.\n");
26     printf("\t-a       Update (clear) syscheck database for all agents.\n");
27     printf("\t-u <id>  Update (clear) syscheck database for a specific agent.\n");
28     printf("\t-u local Update (clear) syscheck database locally.\n\n");
29     exit(1);
30 }
31
32 int main(int argc, char **argv)
33 {
34     const char *dir = DEFAULTDIR;
35     const char *group = GROUPGLOBAL;
36     const char *user = USER;
37     gid_t gid;
38     uid_t uid;
39
40     /* Set the name */
41     OS_SetName(ARGV0);
42
43     /* User arguments */
44     if (argc < 2) {
45         helpmsg();
46     }
47
48     /* Get the group name */
49     gid = Privsep_GetGroup(group);
50     uid = Privsep_GetUser(user);
51     if (uid == (uid_t) - 1 || gid == (gid_t) - 1) {
52         ErrorExit(USER_ERROR, ARGV0, user, group);
53     }
54
55     /* Set the group */
56     if (Privsep_SetGroup(gid) < 0) {
57         ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
58     }
59
60     /* Chroot to the default directory */
61     if (Privsep_Chroot(dir) < 0) {
62         ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
63     }
64
65     /* Inside chroot now */
66     nowChroot();
67
68     /* Set the user */
69     if (Privsep_SetUser(uid) < 0) {
70         ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
71     }
72
73     /* User options */
74     if (strcmp(argv[1], "-h") == 0) {
75         helpmsg();
76     } else if (strcmp(argv[1], "-l") == 0) {
77         printf("\nOSSEC HIDS %s: Updates the integrity check database.",
78                ARGV0);
79         print_agents(0, 0, 0, 0);
80         printf("\n");
81         exit(0);
82     } else if (strcmp(argv[1], "-u") == 0) {
83         if (argc != 3) {
84             printf("\n** Option -u requires an extra argument\n");
85             helpmsg();
86         }
87     } else if (strcmp(argv[1], "-a") == 0) {
88         DIR *sys_dir;
89         struct dirent *entry;
90
91         sys_dir = opendir(SYSCHECK_DIR);
92         if (!sys_dir) {
93             ErrorExit("%s: Unable to open: '%s'", ARGV0, SYSCHECK_DIR);
94         }
95
96         while ((entry = readdir(sys_dir)) != NULL) {
97             FILE *fp;
98             char full_path[OS_MAXSTR + 1];
99
100             /* Do not even attempt to delete . and .. :) */
101             if ((strcmp(entry->d_name, ".") == 0) ||
102                     (strcmp(entry->d_name, "..") == 0)) {
103                 continue;
104             }
105
106             snprintf(full_path, OS_MAXSTR, "%s/%s", SYSCHECK_DIR, entry->d_name);
107
108             fp = fopen(full_path, "w");
109             if (fp) {
110                 fclose(fp);
111             }
112             if (entry->d_name[0] == '.') {
113                 unlink(full_path);
114             }
115         }
116
117         closedir(sys_dir);
118         printf("\n** Integrity check database updated.\n\n");
119         exit(0);
120     } else {
121         printf("\n** Invalid option '%s'.\n", argv[1]);
122         helpmsg();
123     }
124
125     /* Local */
126     if (strcmp(argv[2], "local") == 0) {
127         char final_dir[1024];
128         FILE *fp;
129         snprintf(final_dir, 1020, "/%s/syscheck", SYSCHECK_DIR);
130
131         fp = fopen(final_dir, "w");
132         if (fp) {
133             fclose(fp);
134         }
135         unlink(final_dir);
136
137         /* Delete cpt file */
138         snprintf(final_dir, 1020, "/%s/.syscheck.cpt", SYSCHECK_DIR);
139
140         fp = fopen(final_dir, "w");
141         if (fp) {
142             fclose(fp);
143         }
144         /* unlink(final_dir); */
145     }
146
147     /* External agents */
148     else {
149         int i;
150         keystore keys;
151
152         OS_ReadKeys(&keys);
153
154         i = OS_IsAllowedID(&keys, argv[2]);
155         if (i < 0) {
156             printf("\n** Invalid agent id '%s'.\n", argv[2]);
157             helpmsg();
158         }
159
160         /* Delete syscheck */
161         delete_syscheck(keys.keyentries[i]->name, keys.keyentries[i]->ip->ip, 0);
162     }
163
164     printf("\n** Integrity check database updated.\n\n");
165     return (0);
166 }
167