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