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