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