Imported Upstream version 2.7
[ossec-hids.git] / src / remoted / main.c
1 /* @(#) $Id: ./src/remoted/main.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
15 #include "shared.h"
16 #include "remoted.h"
17
18
19 int main(int argc, char **argv)
20 {
21     int i = 0,c = 0;
22     int uid = 0, gid = 0;
23     int test_config = 0,run_foreground = 0;
24
25     char *cfg = DEFAULTCPATH;
26     char *dir = DEFAULTDIR;
27     char *user = REMUSER;
28     char *group = GROUPGLOBAL;
29
30
31     /* Setting the name -- must be done ASAP */
32     OS_SetName(ARGV0);
33
34
35     while((c = getopt(argc, argv, "Vdthfu:g:c:D:")) != -1){
36         switch(c){
37             case 'V':
38                 print_version();
39                 break;
40             case 'h':
41                 help(ARGV0);
42                 break;
43             case 'd':
44                 nowDebug();
45                 break;
46             case 'f':
47                 run_foreground = 1;
48                 break;
49             case 'u':
50                 if(!optarg)
51                     ErrorExit("%s: -u needs an argument",ARGV0);
52                 user = optarg;
53                 break;
54             case 'g':
55                 if(!optarg)
56                     ErrorExit("%s: -g needs an argument",ARGV0);
57                 group = optarg;
58                 break;          
59             case 't':
60                 test_config = 1;
61                 break;
62             case 'c':
63                 if (!optarg)
64                     ErrorExit("%s: -c need an argument", ARGV0);
65                 cfg = optarg;
66                 break;
67             case 'D':
68                 if(!optarg)
69                     ErrorExit("%s: -D needs an argument",ARGV0);
70                 dir = optarg;
71         }
72     }
73
74     debug1(STARTED_MSG,ARGV0);
75
76
77     /* Return 0 if not configured */
78     if(RemotedConfig(cfg, &logr) < 0)
79     {
80         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
81     }
82
83
84     /* Exit if test_config is set */
85     if(test_config)
86         exit(0);
87
88     if(logr.conn == NULL)
89     {
90         /* Not configured. */
91         exit(0);
92     }
93
94     /* Check if the user and group given are valid */
95     uid = Privsep_GetUser(user);
96     gid = Privsep_GetGroup(group);
97     if((uid < 0)||(gid < 0))
98         ErrorExit(USER_ERROR, ARGV0, user, group);
99
100
101     /* pid before going daemon */
102     i = getpid();
103
104
105     if(!run_foreground)
106     {
107         nowDaemon();
108         goDaemon();
109     }
110
111
112     /* Setting new group */
113     if(Privsep_SetGroup(gid) < 0)
114             ErrorExit(SETGID_ERROR, ARGV0, group);
115
116     /* Going on chroot */
117     if(Privsep_Chroot(dir) < 0)
118                 ErrorExit(CHROOT_ERROR,ARGV0,dir);
119
120
121     nowChroot();
122
123
124     /* Starting the signal manipulation */
125     StartSIG(ARGV0);    
126
127
128     /* Creating some randoness  */
129     #ifdef __OpenBSD__
130     srandomdev();
131     #else
132     srandom( time(0) + getpid()+ i);
133     #endif
134
135     random();
136
137
138     /* Start up message */
139     verbose(STARTUP_MSG, ARGV0, (int)getpid());
140
141
142     /* Really starting the program. */
143     i = 0;
144     while(logr.conn[i] != 0)
145     {
146         /* Forking for each connection handler */
147         if(fork() == 0)
148         {
149             /* On the child */
150             debug1("%s: DEBUG: Forking remoted: '%d'.",ARGV0, i);
151             HandleRemote(i, uid);
152         }
153         else
154         {
155             i++;
156             continue;
157         }
158     }
159
160
161     /* Done over here */
162     return(0);
163 }
164
165
166 /* EOF */