1d8a11f54eb402a523415e98c72d4319b344e7aa
[ossec-hids.git] / src / os_csyslogd / 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  * License details at the LICENSE file included with OSSEC or 
12  * online at: http://www.ossec.net/en/licensing.html
13  */
14
15
16 #include "csyslogd.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
25     /* Using MAILUSER (read only) */
26     char *dir  = DEFAULTDIR;
27     char *user = MAILUSER;
28     char *group = GROUPGLOBAL;
29     char *cfg = DEFAULTCPATH;
30
31
32     /* Database Structure */
33     SyslogConfig **syslog_config = NULL;
34
35
36     /* Setting the name */
37     OS_SetName(ARGV0);
38         
39
40     while((c = getopt(argc, argv, "vVdhtfu:g:D:c:")) != -1){
41         switch(c){
42             case 'V':
43                 print_version();
44                 break;
45             case 'v':
46                 print_version();
47                 break;    
48             case 'h':
49                 help(ARGV0);
50                 break;
51             case 'd':
52                 nowDebug();
53                 break;
54             case 'f':
55                 run_foreground = 1;
56                 break;
57             case 'u':
58                 if(!optarg)
59                     ErrorExit("%s: -u needs an argument",ARGV0);
60                 user=optarg;
61                 break;
62             case 'g':
63                 if(!optarg)
64                     ErrorExit("%s: -g needs an argument",ARGV0);
65                 group=optarg;
66                 break;
67             case 'D':
68                 if(!optarg)
69                     ErrorExit("%s: -D needs an argument",ARGV0);
70                 dir=optarg;
71             case 'c':
72                 if(!optarg)
73                     ErrorExit("%s: -c needs an argument",ARGV0);
74                 cfg = optarg;
75                 break;
76             case 't':
77                 test_config = 1;    
78                 break;
79             default:
80                 help(ARGV0);
81                 break;
82         }
83
84     }
85
86
87     /* Starting daemon */
88     debug1(STARTED_MSG, ARGV0);
89
90
91     /* Check if the user/group given are valid */
92     uid = Privsep_GetUser(user);
93     gid = Privsep_GetGroup(group);
94     if((uid < 0)||(gid < 0))
95     {
96         ErrorExit(USER_ERROR, ARGV0, user, group);
97     }
98
99
100     /* Reading configuration */
101     syslog_config = OS_ReadSyslogConf(test_config, cfg, syslog_config);
102
103
104     /* Getting servers hostname */
105     memset(__shost, '\0', 512);
106     if(gethostname(__shost, 512 -1) != 0)
107     {
108         ErrorExit("%s: ERROR: gethostname() failed", ARGV0);
109     }
110     else
111     {
112         char *ltmp;
113
114         /* Remove domain part if available */
115         ltmp = strchr(__shost, '.');
116         if(ltmp)
117             *ltmp = '\0';
118     }
119                                         
120
121     /* Exit here if test config is set */
122     if(test_config)
123         exit(0);
124         
125         
126     if (!run_foreground) 
127     {
128         /* Going on daemon mode */
129         nowDaemon();
130         goDaemon();
131     }
132
133
134     
135     /* Not configured */
136     if(!syslog_config || !syslog_config[0])
137     {
138         verbose("%s: INFO: Remote syslog server not configured. "
139                 "Clean exit.", ARGV0);
140         exit(0);
141     }
142
143     
144
145     /* Privilege separation */  
146     if(Privsep_SetGroup(gid) < 0)
147         ErrorExit(SETGID_ERROR,ARGV0,group);
148
149     
150     /* chrooting */
151     if(Privsep_Chroot(dir) < 0)
152         ErrorExit(CHROOT_ERROR,ARGV0,dir);
153
154
155     /* Now on chroot */
156     nowChroot();
157
158
159     
160     /* Changing user */        
161     if(Privsep_SetUser(uid) < 0)
162         ErrorExit(SETUID_ERROR,ARGV0,user);
163
164
165     /* Basic start up completed. */
166     debug1(PRIVSEP_MSG,ARGV0,dir,user);
167
168
169     /* Signal manipulation */
170     StartSIG(ARGV0);
171
172     
173     /* Creating PID files */
174     if(CreatePID(ARGV0, getpid()) < 0)
175         ErrorExit(PID_ERROR, ARGV0);
176
177     
178     /* Start up message */
179     verbose(STARTUP_MSG, ARGV0, (int)getpid());
180     
181
182     /* the real daemon now */   
183     OS_CSyslogD(syslog_config);
184     exit(0);
185 }
186
187
188 /* EOF */