cfb3661259832d87567ab01688fa39c9f7ff60d3
[ossec-hids.git] / src / client-agent / main.c
1 /* @(#) $Id: ./src/client-agent/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 /* Part of the OSSEC HIDS
14  * Available at http://www.ossec.net
15  */
16
17
18 /* agent daemon.
19  */
20
21
22 #include "shared.h"
23 #include "agentd.h"
24
25 #ifndef ARGV0
26    #define ARGV0 "ossec-agentd"
27 #endif
28
29
30
31
32
33 /* main, v0.2, 2005/11/09
34  */
35 int main(int argc, char **argv)
36 {
37     int c = 0;
38     int test_config = 0;
39     int debug_level = 0;
40
41     char *dir = DEFAULTDIR;
42     char *user = USER;
43     char *group = GROUPGLOBAL;
44
45     int uid = 0;
46     int gid = 0;
47
48     run_foreground = 0;
49
50     /* Setting the name */
51     OS_SetName(ARGV0);
52
53
54     while((c = getopt(argc, argv, "Vtdfhu:g:D:")) != -1){
55         switch(c){
56             case 'V':
57                 print_version();
58                 break;
59             case 'h':
60                 help(ARGV0);
61                 break;
62             case 'd':
63                 nowDebug();
64                 debug_level = 1;
65                 break;
66             case 'f':
67                 run_foreground = 1;
68                 break;
69             case 'u':
70                 if(!optarg)
71                     ErrorExit("%s: -u needs an argument",ARGV0);
72                 user = optarg;
73                 break;
74             case 'g':
75                 if(!optarg)
76                     ErrorExit("%s: -g needs an argument",ARGV0);
77                 group = optarg;
78                 break;
79             case 't':
80                 test_config = 1;
81                 break;
82             case 'D':
83                 if(!optarg)
84                     ErrorExit("%s: -D needs an argument",ARGV0);
85                 dir = optarg;
86                 break;
87         }
88     }
89
90
91     debug1(STARTED_MSG, ARGV0);
92
93     agt = (agent *)calloc(1, sizeof(agent));
94     if(!agt)
95     {
96         ErrorExit(MEM_ERROR, ARGV0);
97     }
98
99
100     /* Check current debug_level
101      * Command line setting takes precedence
102      */
103     if (debug_level == 0)
104     {
105         /* Getting debug level */
106         debug_level = getDefine_Int("agent","debug", 0, 2);
107         while(debug_level != 0)
108         {
109             nowDebug();
110             debug_level--;
111         }
112     }
113
114
115     /* Reading config */
116     if(ClientConf(DEFAULTCPATH) < 0)
117     {
118         ErrorExit(CLIENT_ERROR,ARGV0);
119     }
120
121     if(!agt->rip)
122     {
123         merror(AG_INV_IP, ARGV0);
124         ErrorExit(CLIENT_ERROR,ARGV0);
125     }
126
127     if(agt->notify_time == 0)
128     {
129         agt->notify_time = NOTIFY_TIME;
130     }
131     if(agt->max_time_reconnect_try == 0 )
132     {
133         agt->max_time_reconnect_try = NOTIFY_TIME * 3;
134     }
135     if(agt->max_time_reconnect_try <= agt->notify_time)
136     {
137         agt->max_time_reconnect_try = (agt->notify_time * 3);
138         verbose("%s: INFO: Max time to reconnect can't be less than notify_time(%d), using notify_time*3 (%d)",ARGV0,agt->notify_time,agt->max_time_reconnect_try);
139     }
140     verbose("%s: INFO: Using notify time: %d and max time to reconnect: %d",ARGV0,agt->notify_time,agt->max_time_reconnect_try);
141
142
143     /* Checking auth keys */
144     if(!OS_CheckKeys())
145     {
146         ErrorExit(AG_NOKEYS_EXIT, ARGV0);
147     }
148
149
150     /* Check if the user/group given are valid */
151     uid = Privsep_GetUser(user);
152     gid = Privsep_GetGroup(group);
153     if((uid < 0)||(gid < 0))
154     {
155         ErrorExit(USER_ERROR,ARGV0,user,group);
156     }
157
158
159
160     /* Exit if test config */
161     if(test_config)
162         exit(0);
163
164
165     /* Starting the signal manipulation */
166     StartSIG(ARGV0);
167
168
169     /* Agentd Start */
170     AgentdStart(dir, uid, gid, user, group);
171
172
173     return(0);
174 }
175
176 /* EOF */