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