Imported Upstream version 2.7
[ossec-hids.git] / src / remoted / remoted.c
1 /* @(#) $Id: ./src/remoted/remoted.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights 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  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17
18 /* remote daemon.
19  * Listen to remote packets and forward them to the analysis
20  * system
21  */
22
23
24 #include "shared.h"
25 #include "os_net/os_net.h"
26
27 #include "remoted.h"
28
29
30
31 /** void HandleRemote(int position, int uid) v0.2 2005/11/09
32  * Handle remote connections
33  * v0.2, 2005/11/09
34  * v0.1, 2004/7/30
35  */
36 void HandleRemote(int position, int uid)
37 {
38     /* If syslog connection and allowips is not defined, exit */
39     if(logr.conn[position] == SYSLOG_CONN)
40     {
41         if(logr.allowips == NULL)
42         {
43             ErrorExit(NO_SYSLOG, ARGV0);
44         }
45         else
46         {
47             os_ip **tmp_ips;
48
49             tmp_ips = logr.allowips;
50             while(*tmp_ips)
51             {
52                 verbose("%s: Remote syslog allowed from: '%s'",
53                         ARGV0, (*tmp_ips)->ip);
54                 tmp_ips++;
55             }
56         }
57     }
58
59
60     /* Bind TCP */
61     if(logr.proto[position] == TCP_PROTO)
62     {
63         if((logr.sock =
64             OS_Bindporttcp(logr.port[position],logr.lip[position], logr.ipv6[position])) < 0)
65         {
66             ErrorExit(BIND_ERROR, ARGV0, logr.port[position]);
67         }
68     }
69     else
70     {
71         /* Using UDP. Fast, unreliable.. perfect */
72         if((logr.sock =
73             OS_Bindportudp(logr.port[position], logr.lip[position], logr.ipv6[position])) < 0)
74         {
75             ErrorExit(BIND_ERROR, ARGV0, logr.port[position]);
76         }
77     }
78
79
80
81     /* Revoking the privileges */
82     if(Privsep_SetUser(uid) < 0)
83     {
84         ErrorExit(SETUID_ERROR,ARGV0, REMUSER);
85     }
86
87
88     /* Creating PID */
89     if(CreatePID(ARGV0, getpid()) < 0)
90     {
91         ErrorExit(PID_ERROR,ARGV0);
92     }
93
94
95     /* Start up message */
96     verbose(STARTUP_MSG, ARGV0, (int)getpid());
97
98
99     /* If Secure connection, deal with it */
100     if(logr.conn[position] == SECURE_CONN)
101     {
102         HandleSecure();
103     }
104
105     else if(logr.proto[position] == TCP_PROTO)
106     {
107         HandleSyslogTCP();
108     }
109
110     /* If not, deal with syslog */
111     else
112     {
113         HandleSyslog();
114     }
115
116     return;
117 }
118
119
120 /* EOF */