Imported Upstream version 2.5.1
[ossec-hids.git] / src / client-agent / 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 /* Part of the OSSEC HIDS
13  * Available at http://www.ossec.net
14  */
15
16
17 /* agent daemon.
18  */
19
20
21 #include "shared.h"
22 #include "agentd.h"
23
24 #ifndef ARGV0
25    #define ARGV0 "ossec-agentd"
26 #endif
27
28
29
30
31
32 /* main, v0.2, 2005/11/09
33  */
34 int main(int argc, char **argv)
35 {
36     int c = 0;
37     int test_config = 0;
38     
39     char *dir = DEFAULTDIR;
40     char *user = USER;
41     char *group = GROUPGLOBAL;
42     
43     int uid = 0;
44     int gid = 0;
45
46     
47     /* Setting the name */
48     OS_SetName(ARGV0);
49
50
51     while((c = getopt(argc, argv, "Vtdhu:g:D:")) != -1){
52         switch(c){
53             case 'V':
54                 print_version();
55                 break;
56             case 'h':
57                 help(ARGV0);
58                 break;
59             case 'd':
60                 nowDebug();
61                 break;
62             case 'u':
63                 if(!optarg)
64                     ErrorExit("%s: -u needs an argument",ARGV0);
65                 user = optarg;
66                 break;
67             case 'g':
68                 if(!optarg)
69                     ErrorExit("%s: -g needs an argument",ARGV0);
70                 group = optarg;
71                 break;          
72             case 't':
73                 test_config = 1;    
74                 break;
75             case 'D':
76                 if(!optarg)
77                     ErrorExit("%s: -D needs an argument",ARGV0);
78                 dir = optarg;
79                 break;
80         }
81     }
82
83     debug1(STARTED_MSG, ARGV0);
84
85     logr = (agent *)calloc(1, sizeof(agent));
86     if(!logr)
87     {
88         ErrorExit(MEM_ERROR, ARGV0);
89     }
90
91     
92     /* Reading config */
93     if(ClientConf(DEFAULTCPATH) < 0)
94     {
95         ErrorExit(CLIENT_ERROR,ARGV0);
96     }
97
98     if(!logr->rip)
99     {
100         merror(AG_INV_IP, ARGV0);
101         ErrorExit(CLIENT_ERROR,ARGV0);        
102     }
103
104
105     /* Checking auth keys */
106     if(!OS_CheckKeys())
107     {
108         ErrorExit(AG_NOKEYS_EXIT, ARGV0);
109     }
110
111         
112     /* Check if the user/group given are valid */
113     uid = Privsep_GetUser(user);
114     gid = Privsep_GetGroup(group);
115     if((uid < 0)||(gid < 0))
116     {
117         ErrorExit(USER_ERROR,ARGV0,user,group);
118     }
119
120
121
122     /* Exit if test config */
123     if(test_config)
124         exit(0);
125
126
127     /* Starting the signal manipulation */
128     StartSIG(ARGV0);    
129
130
131     /* Agentd Start */
132     AgentdStart(dir, uid, gid, user, group);
133
134     
135     return(0);
136 }
137
138 /* EOF */