Imported Upstream version 2.7
[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
40     char *dir = DEFAULTDIR;
41     char *user = USER;
42     char *group = GROUPGLOBAL;
43
44     int uid = 0;
45     int gid = 0;
46
47
48     /* Setting the name */
49     OS_SetName(ARGV0);
50
51
52     while((c = getopt(argc, argv, "Vtdhu:g:D:")) != -1){
53         switch(c){
54             case 'V':
55                 print_version();
56                 break;
57             case 'h':
58                 help(ARGV0);
59                 break;
60             case 'd':
61                 nowDebug();
62                 break;
63             case 'u':
64                 if(!optarg)
65                     ErrorExit("%s: -u needs an argument",ARGV0);
66                 user = optarg;
67                 break;
68             case 'g':
69                 if(!optarg)
70                     ErrorExit("%s: -g needs an argument",ARGV0);
71                 group = optarg;
72                 break;          
73             case 't':
74                 test_config = 1;
75                 break;
76             case 'D':
77                 if(!optarg)
78                     ErrorExit("%s: -D needs an argument",ARGV0);
79                 dir = optarg;
80                 break;
81         }
82     }
83
84     debug1(STARTED_MSG, ARGV0);
85
86     logr = (agent *)calloc(1, sizeof(agent));
87     if(!logr)
88     {
89         ErrorExit(MEM_ERROR, ARGV0);
90     }
91
92
93     /* Reading config */
94     if(ClientConf(DEFAULTCPATH) < 0)
95     {
96         ErrorExit(CLIENT_ERROR,ARGV0);
97     }
98
99     if(!logr->rip)
100     {
101         merror(AG_INV_IP, ARGV0);
102         ErrorExit(CLIENT_ERROR,ARGV0);
103     }
104
105
106     /* Checking auth keys */
107     if(!OS_CheckKeys())
108     {
109         ErrorExit(AG_NOKEYS_EXIT, ARGV0);
110     }
111
112
113     /* Check if the user/group given are valid */
114     uid = Privsep_GetUser(user);
115     gid = Privsep_GetGroup(group);
116     if((uid < 0)||(gid < 0))
117     {
118         ErrorExit(USER_ERROR,ARGV0,user,group);
119     }
120
121
122
123     /* Exit if test config */
124     if(test_config)
125         exit(0);
126
127
128     /* Starting the signal manipulation */
129     StartSIG(ARGV0);    
130
131
132     /* Agentd Start */
133     AgentdStart(dir, uid, gid, user, group);
134
135
136     return(0);
137 }
138
139 /* EOF */