c9f15ff4f4425849afd8c91de455abf2e32c5e49
[ossec-hids.git] / src / InstallServer.sh
1 #!/bin/sh
2
3
4 # Checking if it is executed from the right place
5 LOCATION=./LOCATION
6 ls ${LOCATION} > /dev/null 2>&1
7 if [ $? != 0 ]; then
8     echo "Cannot execute. Wrong directory"
9     exit 1;
10 fi
11
12 # Getting any argument
13 if [ "X$1" = "Xlocal" ]; then
14     # Setting local install
15     LOCAL="local"
16 fi
17     
18 UNAME=`uname`;
19
20 # Getting default variables
21 DIR=`grep DIR ${LOCATION} | cut -f2 -d\"`
22 GROUP="ossec"
23 USER="ossec"
24 USER_MAIL="ossecm"
25 USER_REM="ossecr"
26 subdirs="logs logs/archives logs/alerts logs/firewall bin stats rules queue queue/alerts queue/ossec queue/fts queue/syscheck queue/rootcheck queue/diff queue/agent-info queue/agentless queue/rids tmp var var/run etc etc/shared active-response active-response/bin agentless .ssh"
27
28 # ${DIR} must be set 
29 if [ "X${DIR}" = "X" ]; then
30     echo "Error building OSSEC HIDS."
31     exit 1;
32 fi    
33
34     
35 # Creating root directory
36 ls ${DIR} > /dev/null 2>&1    
37 if [ $? != 0 ]; then mkdir -m 700 -p ${DIR}; fi
38 ls ${DIR} > /dev/null 2>&1    
39 if [ $? != 0 ]; then 
40     echo "You do not have permissions to create ${DIR}. Exiting..."
41     exit 1;
42 fi
43
44
45 # Creating groups/users
46 if [ "$UNAME" = "FreeBSD" -o "$UNAME" = "DragonFly" ]; then
47     grep "^${USER_REM}" /etc/passwd > /dev/null 2>&1
48     if [ ! $? = 0 ]; then
49     /usr/sbin/pw groupadd ${GROUP}
50         /usr/sbin/pw useradd ${USER} -d ${DIR} -s /sbin/nologin -g ${GROUP}
51         /usr/sbin/pw useradd ${USER_MAIL} -d ${DIR} -s /sbin/nologin -g ${GROUP}
52         /usr/sbin/pw useradd ${USER_REM} -d ${DIR} -s /sbin/nologin -g ${GROUP}
53     fi
54
55 elif [ "$UNAME" = "SunOS" ]; then
56     grep "^${USER_REM}" /etc/passwd > /dev/null 2>&1
57     if [ ! $? = 0 ]; then
58     /usr/sbin/groupadd ${GROUP}
59     /usr/sbin/useradd -d ${DIR} -s /bin/false -g ${GROUP} ${USER}
60     /usr/sbin/useradd -d ${DIR} -s /bin/false -g ${GROUP} ${USER_MAIL}
61     /usr/sbin/useradd -d ${DIR} -s /bin/false -g ${GROUP} ${USER_REM}
62     fi
63
64 elif [ "$UNAME" = "AIX" ]; then
65     AIXSH=""
66     ls -la /bin/false > /dev/null 2>&1
67     if [ $? = 0 ]; then
68         AIXSH="-s /bin/false"
69     fi
70
71     grep "^${USER_REM}" /etc/passwd > /dev/null 2>&1
72     if [ ! $? = 0 ]; then
73     /usr/bin/mkgroup ${GROUP}
74     /usr/sbin/useradd -d ${DIR} ${AIXSH} -g ${GROUP} ${USER}
75     /usr/sbin/useradd -d ${DIR} ${AIXSH} -g ${GROUP} ${USER_MAIL}
76     /usr/sbin/useradd -d ${DIR} ${AIXSH} -g ${GROUP} ${USER_REM}
77     fi
78
79 # Thanks Chuck L. for the mac addusers    
80 elif [ "$UNAME" = "Darwin" ]; then
81     id -u ${USER} > /dev/null 2>&1
82     if [ ! $? = 0 ]; then
83
84         # Creating for 10.5 and 10.6
85         /usr/bin/sw_vers 2>/dev/null| grep "ProductVersion" | grep -E "10.5.|10.6" > /dev/null 2>&1
86         if [ $? = 0 ]; then
87             chmod +x ./init/osx105-addusers.sh
88             ./init/osx105-addusers.sh
89         else
90             chmod +x ./init/darwin-addusers.pl
91             ./init/darwin-addusers.pl    
92         fi        
93     fi    
94 else
95     grep "^${USER_REM}" /etc/passwd > /dev/null 2>&1
96     if [ ! $? = 0 ]; then
97         /usr/sbin/groupadd ${GROUP}
98
99     # We first check if /sbin/nologin is present. If it is not,
100     # we look for bin/false. If none of them is present, we
101     # just stick with nologin (no need to fail the install for that).
102     OSMYSHELL="/sbin/nologin"
103     ls -la ${OSMYSHELL} > /dev/null 2>&1
104     if [ ! $? = 0 ]; then
105         ls -la /bin/false > /dev/null 2>&1
106         if [ $? = 0 ]; then
107             OSMYSHELL="/bin/false"
108         fi    
109     fi    
110         /usr/sbin/useradd -d ${DIR} -s ${OSMYSHELL} -g ${GROUP} ${USER}
111         /usr/sbin/useradd -d ${DIR} -s ${OSMYSHELL} -g ${GROUP} ${USER_MAIL}
112         /usr/sbin/useradd -d ${DIR} -s ${OSMYSHELL} -g ${GROUP} ${USER_REM}
113     fi
114 fi
115
116
117 # Creating sub directories
118 for i in ${subdirs}; do
119     ls ${DIR}/${i} > /dev/null 2>&1
120     if [ $? != 0 ]; then mkdir -m 700 ${DIR}/${i}; fi
121 done
122
123 # Default for all directories
124 chmod -R 550 ${DIR}
125 chown -R root:${GROUP} ${DIR}
126
127 # AnalysisD needs to write to alerts: log, mail and cmds
128 chown -R ${USER}:${GROUP} ${DIR}/queue/alerts
129 chmod -R 770 ${DIR}/queue/alerts
130
131 # To the ossec queue (default for analysisd to read)
132 chown -R ${USER}:${GROUP} ${DIR}/queue/ossec
133 chmod -R 770 ${DIR}/queue/ossec
134
135 # To the ossec fts queue
136 chown -R ${USER}:${GROUP} ${DIR}/queue/fts
137 chmod -R 750 ${DIR}/queue/fts
138 chmod 740 ${DIR}/queue/fts/* > /dev/null 2>&1
139
140 # To the ossec syscheck/rootcheck queue
141 chown -R ${USER}:${GROUP} ${DIR}/queue/syscheck
142 chmod -R 750 ${DIR}/queue/syscheck
143 chmod 740 ${DIR}/queue/syscheck/* > /dev/null 2>&1
144
145 chown -R ${USER}:${GROUP} ${DIR}/queue/rootcheck
146 chmod -R 750 ${DIR}/queue/rootcheck
147 chmod 740 ${DIR}/queue/rootcheck/* > /dev/null 2>&1
148
149 chown -R ${USER}:${GROUP} ${DIR}/queue/diff
150 chmod -R 750 ${DIR}/queue/diff
151 chmod 740 ${DIR}/queue/diff/* > /dev/null 2>&1
152
153 chown -R ${USER_REM}:${GROUP} ${DIR}/queue/agent-info
154 chmod -R 755 ${DIR}/queue/agent-info
155 chmod 744 ${DIR}/queue/agent-info/* > /dev/null 2>&1
156 chown -R ${USER_REM}:${GROUP} ${DIR}/queue/rids
157 chmod -R 755 ${DIR}/queue/rids
158 chmod 744 ${DIR}/queue/rids/* > /dev/null 2>&1
159
160 chown -R ${USER}:${GROUP} ${DIR}/queue/agentless
161 chmod -R 755 ${DIR}/queue/agentless
162 chmod 744 ${DIR}/queue/agentless/* > /dev/null 2>&1
163
164
165 # For the stats directory
166 chown -R ${USER}:${GROUP} ${DIR}/stats
167 chmod -R 750 ${DIR}/stats
168
169 # For the logging user
170 chown -R ${USER}:${GROUP} ${DIR}/logs
171 chmod -R 750 ${DIR}/logs
172 touch ${DIR}/logs/ossec.log
173 chown ${USER}:${GROUP} ${DIR}/logs/ossec.log
174 chmod 664 ${DIR}/logs/ossec.log
175
176 # For the rules directory
177 ls ${DIR}/rules/*.xml > /dev/null 2>&1
178
179 # Backup previous rules
180 if [ $? = 0 ]; then
181     mkdir ${DIR}/rules/backup-rules.$$
182     cp -pr ${DIR}/rules/*.xml ${DIR}/rules/backup-rules.$$/
183     
184     # Checking for the local rules
185     ls ${DIR}/rules/local_rules.xml > /dev/null 2>&1
186     if [ $? = 0 ]; then
187         cp -pr ${DIR}/rules/local_rules.xml ${DIR}/rules/saved_local_rules.xml.$$
188     fi    
189 fi
190     
191 cp -pr ../etc/rules/* ${DIR}/rules/
192
193 # If the local_rules is saved, moved it back
194 ls ${DIR}/rules/saved_local_rules.xml.$$ > /dev/null 2>&1
195 if [ $? = 0 ]; then
196     mv ${DIR}/rules/saved_local_rules.xml.$$ ${DIR}/rules/local_rules.xml
197 fi    
198
199 chown -R root:${GROUP} ${DIR}/rules
200 chmod -R 550 ${DIR}/rules
201
202
203 # For the etc dir
204 chmod 550 ${DIR}/etc
205 chown -R root:${GROUP} ${DIR}/etc
206 ls /etc/localtime > /dev/null 2>&1
207 if [ $? = 0 ]; then
208     cp -pL /etc/localtime ${DIR}/etc/;
209     chmod 555 ${DIR}/etc/localtime
210     chown root:${GROUP} ${DIR}/etc/localtime 
211 fi
212
213 # Solaris Needs some extra files
214 if [ "$UNAME" = "SunOS" ]; then
215     mkdir -p ${DIR}/usr/share/lib/zoneinfo/
216     chmod -R 555 ${DIR}/usr/
217     cp -pr /usr/share/lib/zoneinfo/* ${DIR}/usr/share/lib/zoneinfo/
218 fi
219
220 ls /etc/TIMEZONE > /dev/null 2>&1
221 if [ $? = 0 ]; then
222     cp -p /etc/TIMEZONE ${DIR}/etc/;
223     chmod 555 ${DIR}/etc/TIMEZONE
224 fi
225                         
226
227 # For the /var/run
228 chmod 770 ${DIR}/var/run
229 chown root:${GROUP} ${DIR}/var/run
230
231 # Moving the binary files
232 cp -pr ../bin/ossec* ${DIR}/bin/
233 cp -pr ../bin/manage_agents ${DIR}/bin/
234 cp -pr ../bin/syscheck_update ${DIR}/bin/
235 cp -pr ../bin/verify-agent-conf ${DIR}/bin/
236 cp -pr ../bin/clear_stats ${DIR}/bin/
237 cp -pr ../bin/list_agents ${DIR}/bin/
238 cp -pr ../bin/agent_control ${DIR}/bin/
239 cp -pr ../bin/syscheck_control ${DIR}/bin/
240 cp -pr ../bin/rootcheck_control ${DIR}/bin/
241
242 # Local install chosen
243 if [ "X$LOCAL" = "Xlocal" ]; then
244     cp -pr ./init/ossec-local.sh ${DIR}/bin/ossec-control
245 else    
246     cp -pr ./init/ossec-server.sh ${DIR}/bin/ossec-control
247 fi
248
249 # Moving the decoders/internal_conf file.
250 cp -pr ../etc/decoder.xml ${DIR}/etc/
251
252 # Copying local files.
253 cp -pr ../etc/local_decoder.xml ${DIR}/etc/ > /dev/null 2>&1
254 cp -pr ../etc/local_internal_options.conf ${DIR}/etc/ > /dev/null 2>&1
255 cp -pr ../etc/client.keys ${DIR}/etc/ > /dev/null 2>&1
256
257 # Copying agentless files.
258 cp -pr agentlessd/scripts/* ${DIR}/agentless/
259
260
261 # Backup currently internal_options file.
262 ls ${DIR}/etc/internal_options.conf > /dev/null 2>&1
263 if [ $? = 0 ]; then
264   cp -pr ${DIR}/etc/internal_options.conf ${DIR}/etc/backup-internal_options.$$
265 fi
266   
267 cp -pr ../etc/internal_options.conf ${DIR}/etc/
268 cp -pr rootcheck/db/*.txt ${DIR}/etc/shared/
269 chown root:${GROUP} ${DIR}/etc/decoder.xml
270 chown root:${GROUP} ${DIR}/etc/local_decoder.xml >/dev/null 2>&1
271 chown root:${GROUP} ${DIR}/etc/internal_options.conf
272 chown root:${GROUP} ${DIR}/etc/local_internal_options.conf >/dev/null 2>&1
273 chown root:${GROUP} ${DIR}/etc/client.keys >/dev/null 2>&1
274 chown root:${GROUP} ${DIR}/etc/shared/*
275 chown root:${GROUP} ${DIR}/agentless/*
276 chown ${USER}:${GROUP} ${DIR}/.ssh
277 chmod 440 ${DIR}/etc/decoder.xml
278 chmod 440 ${DIR}/etc/local_decoder.xml >/dev/null 2>&1
279 chmod 440 ${DIR}/etc/internal_options.conf
280 chmod 440 ${DIR}/etc/local_internal_options.conf >/dev/null 2>&1
281 chmod 440 ${DIR}/etc/client.keys >/dev/null 2>&1
282 chmod 550 ${DIR}/etc
283 chmod 770 ${DIR}/etc/shared
284 chmod 440 ${DIR}/etc/shared/*
285 chmod 550 ${DIR}/agentless/*
286 rm ${DIR}/etc/shared/merged.mg >/dev/null 2>&1
287 chmod 700 ${DIR}/.ssh
288
289
290 # Copying active response modules
291 sh ./init/fw-check.sh execute > /dev/null
292 cp -p ../active-response/*.sh ${DIR}/active-response/bin/
293 cp -p ../active-response/firewalls/*.sh ${DIR}/active-response/bin/
294
295 chmod 755 ${DIR}/active-response/bin/*
296 chown root:${GROUP} ${DIR}/active-response/bin/*
297
298 chown root:${GROUP} ${DIR}/bin/*
299 chmod 550 ${DIR}/bin/*
300
301
302 # Moving the config file
303 ls ${DIR}/etc/ossec.conf > /dev/null 2>&1
304 if [ $? = 0 ]; then
305     exit 0;
306 fi
307
308 ls ../etc/ossec.mc > /dev/null 2>&1
309 if [ $? = 0 ]; then
310     cp -pr ../etc/ossec.mc ${DIR}/etc/ossec.conf
311 else    
312     cp -pr ../etc/ossec-server.conf ${DIR}/etc/ossec.conf
313 fi
314 chown root:${GROUP} ${DIR}/etc/ossec.conf
315 chmod 440 ${DIR}/etc/ossec.conf
316
317
318
319 exit 0;
320
321 #EOF