Imported Upstream version 2.7
[ossec-hids.git] / src / util / syscheck_update.c
1 /* @(#) $Id: ./src/util/syscheck_update.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
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
10  * Foundation
11  */
12
13
14 #include "addagent/manage_agents.h"
15 #include "sec.h"
16
17 #undef ARGV0
18 #define ARGV0 "syscheck_update"
19
20
21 /** help **/
22 void helpmsg()
23 {
24     printf("\nOSSEC HIDS %s: Updates (clears) the integrity check database.\n", ARGV0);
25     printf("Available options:\n");
26     printf("\t-h       This help message.\n");
27     printf("\t-l       List available agents.\n");
28     printf("\t-a       Update (clear) syscheck database for all agents.\n");
29     printf("\t-u <id>  Update (clear) syscheck database for a specific agent.\n");
30     printf("\t-u local Update (clear) syscheck database locally.\n\n");
31     exit(1);
32 }
33
34 /** main **/
35 int main(int argc, char **argv)
36 {
37     char *dir = DEFAULTDIR;
38     char *group = GROUPGLOBAL;
39     char *user = USER;
40     int gid;
41     int uid;
42
43
44     /* Setting the name */
45     OS_SetName(ARGV0);
46
47
48     /* user arguments */
49     if(argc < 2)
50     {
51         helpmsg();
52     }
53
54     /* Getting the group name */
55     gid = Privsep_GetGroup(group);
56     uid = Privsep_GetUser(user);
57     if(gid < 0)
58     {
59             ErrorExit(USER_ERROR, ARGV0, user, group);
60     }
61         
62
63     /* Setting the group */
64     if(Privsep_SetGroup(gid) < 0)
65     {
66             ErrorExit(SETGID_ERROR,ARGV0, group);
67     }
68
69
70     /* Chrooting to the default directory */
71     if(Privsep_Chroot(dir) < 0)
72     {
73         ErrorExit(CHROOT_ERROR, ARGV0, dir);
74     }
75
76
77     /* Inside chroot now */
78     nowChroot();
79
80
81     /* Setting the user */
82     if(Privsep_SetUser(uid) < 0)
83     {
84         ErrorExit(SETUID_ERROR, ARGV0, user);
85     }
86
87     /* User options */
88     if(strcmp(argv[1], "-h") == 0)
89     {
90         helpmsg();
91     }
92     else if(strcmp(argv[1], "-l") == 0)
93     {
94         printf("\nOSSEC HIDS %s: Updates the integrity check database.",
95                                  ARGV0);
96         print_agents(0, 0, 0);
97         printf("\n");
98         exit(0);
99     }
100     else if(strcmp(argv[1], "-u") == 0)
101     {
102         if(argc != 3)
103         {
104             printf("\n** Option -u requires an extra argument\n");
105             helpmsg();
106         }
107     }
108     else if(strcmp(argv[1], "-a") == 0)
109     {
110         DIR *sys_dir;
111         struct dirent *entry;
112
113         sys_dir = opendir(SYSCHECK_DIR);
114         if(!sys_dir)
115         {
116             ErrorExit("%s: Unable to open: '%s'", ARGV0, SYSCHECK_DIR);
117         }
118
119         while((entry = readdir(sys_dir)) != NULL)
120         {
121             FILE *fp;
122             char full_path[OS_MAXSTR +1];
123
124             /* Do not even attempt to delete . and .. :) */
125             if((strcmp(entry->d_name,".") == 0)||
126                (strcmp(entry->d_name,"..") == 0))
127             {
128                 continue;
129             }
130
131             snprintf(full_path, OS_MAXSTR,"%s/%s", SYSCHECK_DIR, entry->d_name);
132
133             fp = fopen(full_path, "w");
134             if(fp)
135             {
136                 fclose(fp);
137             }
138             if(entry->d_name[0] == '.')
139             {
140                 unlink(full_path);
141             }
142         }
143
144         closedir(sys_dir);
145         printf("\n** Integrity check database updated.\n\n");
146         exit(0);
147     }
148     else
149     {
150         printf("\n** Invalid option '%s'.\n", argv[1]);
151         helpmsg();
152     }
153
154
155     /* local */
156     if(strcmp(argv[2],"local") == 0)
157     {
158         char final_dir[1024];
159         FILE *fp;
160         snprintf(final_dir, 1020, "/%s/syscheck", SYSCHECK_DIR);
161
162         fp = fopen(final_dir, "w");
163         if(fp)
164         {
165             fclose(fp);
166         }
167         unlink(final_dir);
168
169
170         /* Deleting cpt file */
171         snprintf(final_dir, 1020, "/%s/.syscheck.cpt", SYSCHECK_DIR);
172
173         fp = fopen(final_dir, "w");
174         if(fp)
175         {
176             fclose(fp);
177         }
178         /* unlink(final_dir); */
179     }
180
181     /* external agents */
182     else
183     {
184         int i;
185         keystore keys;
186
187         OS_ReadKeys(&keys);
188
189         i = OS_IsAllowedID(&keys, argv[2]);
190         if(i < 0)
191         {
192             printf("\n** Invalid agent id '%s'.\n", argv[2]);
193             helpmsg();
194         }
195
196         /* Deleting syscheck */
197         delete_syscheck(keys.keyentries[i]->name,keys.keyentries[i]->ip->ip,0);
198     }
199
200     printf("\n** Integrity check database updated.\n\n");
201     return(0);
202 }
203
204
205 /* EOF */