Imported Upstream version 2.5.1
[ossec-hids.git] / src / agentlessd / main.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights 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 #include "shared.h"
14 #include "agentlessd.h"
15 #include "config/config.h"
16
17
18
19 int main(int argc, char **argv)
20 {
21     int c, test_config = 0, run_foreground = 0;
22     int uid=0,gid=0;
23     char *dir  = DEFAULTDIR;
24     char *user = USER;
25     char *group = GROUPGLOBAL;
26     char *cfg = DEFAULTCPATH;
27
28
29     /* Setting the name */
30     OS_SetName(ARGV0);
31         
32
33     while((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1){
34         switch(c){
35             case 'V':
36                 print_version();
37                 break;
38             case 'h':
39                 help(ARGV0);
40                 break;
41             case 'd':
42                 nowDebug();
43                 break;
44             case 'f':
45                 run_foreground = 1;
46             break;
47             case 'u':
48                 if(!optarg)
49                     ErrorExit("%s: -u needs an argument",ARGV0);
50                 user=optarg;
51                 break;
52             case 'g':
53                 if(!optarg)
54                     ErrorExit("%s: -g needs an argument",ARGV0);
55                 group=optarg;
56                 break;
57             case 'D':
58                 if(!optarg)
59                     ErrorExit("%s: -D needs an argument",ARGV0);
60                 dir=optarg;
61             case 'c':
62                 if(!optarg)
63                     ErrorExit("%s: -c needs an argument",ARGV0);
64                 cfg = optarg;
65                 break;
66             case 't':
67                 test_config = 1;    
68                 break;
69             default:
70                 help(ARGV0);
71                 break;
72         }
73
74     }
75
76
77     /* Starting daemon */
78     debug1(STARTED_MSG,ARGV0);
79
80
81     /* Check if the user/group given are valid */
82     uid = Privsep_GetUser(user);
83     gid = Privsep_GetGroup(group);
84     if((uid < 0)||(gid < 0))
85         ErrorExit(USER_ERROR,ARGV0,user,group);
86
87
88     /* Reading config. */
89     c = 0;
90     c|= CAGENTLESS;
91     lessdc.entries = NULL;
92     lessdc.queue = 0;
93
94     if(ReadConfig(c, cfg, &lessdc, NULL) < 0)
95     {
96         ErrorExit(XML_INV_AGENTLESS, ARGV0);
97     }
98
99
100     /* Exit here if test config is set */
101     if(test_config)
102         exit(0);
103
104         
105     /* Going on daemon mode */
106     if(!run_foreground) 
107     {
108         nowDaemon();
109         goDaemonLight();
110     }
111     chdir(dir);
112
113     
114     /* Exiting if not configured. */
115     if(!lessdc.entries)
116     {
117         verbose("%s: INFO: Not configured. Exiting.", ARGV0);
118         exit(0);
119     }
120     
121     
122     /* Privilege separation */  
123     if(Privsep_SetGroup(gid) < 0)
124         ErrorExit(SETGID_ERROR,ARGV0,group);
125
126     
127     /* Changing user */        
128     if(Privsep_SetUser(uid) < 0)
129         ErrorExit(SETUID_ERROR,ARGV0,user);
130
131
132     debug1(PRIVSEP_MSG,ARGV0,dir,user);
133
134
135
136     /* Signal manipulation */
137     StartSIG(ARGV0);
138
139     
140
141     /* Creating PID files */
142     if(CreatePID(ARGV0, getpid()) < 0)
143         ErrorExit(PID_ERROR,ARGV0);
144
145     
146     /* Start up message */
147     verbose(STARTUP_MSG, ARGV0, (int)getpid());
148     
149
150     /* the real daemon now */   
151     Agentlessd();
152     exit(0);
153 }
154
155
156 /* EOF */