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