new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / remoted / remoted.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights 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 /* remote daemon
11  * Listen to remote packets and forward them to the analysis system
12  */
13
14 #include "shared.h"
15 #include "os_net/os_net.h"
16 #include "remoted.h"
17
18 /* Global variables */
19 keystore keys;
20 remoted logr;
21
22
23 /* Handle remote connections */
24 void HandleRemote(int position, int uid)
25 {
26     /* If syslog connection and allowips is not defined, exit */
27     if (logr.conn[position] == SYSLOG_CONN) {
28         if (logr.allowips == NULL) {
29             ErrorExit(NO_SYSLOG, ARGV0);
30         } else {
31             os_ip **tmp_ips;
32
33             tmp_ips = logr.allowips;
34             while (*tmp_ips) {
35                 verbose("%s: Remote syslog allowed from: '%s'",
36                         ARGV0, (*tmp_ips)->ip);
37                 tmp_ips++;
38             }
39         }
40     }
41
42     /* Bind TCP */
43     if (logr.proto[position] == IPPROTO_TCP) {
44         logr.sock    = 0;
45         logr.netinfo = OS_Bindporttcp(logr.port[position], logr.lip[position]);
46         if (logr.netinfo->status < 0) {
47             ErrorExit(BIND_ERROR, ARGV0, logr.port[position]);
48         }
49     } else {
50         /* Using UDP. Fast, unreliable... perfect */
51         logr.sock    = 0;
52         logr.netinfo = OS_Bindportudp(logr.port[position], logr.lip[position]);
53         if (logr.netinfo->status < 0) {
54             ErrorExit(BIND_ERROR, ARGV0, logr.port[position]);
55         }
56     }
57
58     /* Revoke privileges */
59     if (Privsep_SetUser(uid) < 0) {
60         ErrorExit(SETUID_ERROR, ARGV0, REMUSER, errno, strerror(errno));
61     }
62
63     /* Create PID */
64     if (CreatePID(ARGV0, getpid()) < 0) {
65         ErrorExit(PID_ERROR, ARGV0);
66     }
67
68     /* Start up message */
69     verbose(STARTUP_MSG, ARGV0, (int)getpid());
70
71     /* If secure connection, deal with it */
72     if (logr.conn[position] == SECURE_CONN) {
73         HandleSecure();
74     }
75
76     else if (logr.proto[position] == IPPROTO_TCP)
77     {
78         HandleSyslogTCP();
79     }
80
81     /* If not, deal with syslog */
82     else {
83         HandleSyslog();
84     }
85 }
86