Imported Upstream version 2.7
[ossec-hids.git] / src / client-agent / agentd.c
1 /* @(#) $Id: ./src/client-agent/agentd.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/hids/
15  */
16
17
18 #include "shared.h"
19 #include "agentd.h"
20
21 #include "os_net/os_net.h"
22
23
24
25 /* AgentdStart v0.2, 2005/11/09
26  * Starts the agent daemon.
27  */
28 void AgentdStart(char *dir, int uid, int gid, char *user, char *group)
29 {
30     int rc = 0;
31     int pid = 0;
32     int maxfd = 0;
33
34     fd_set fdset;
35
36     struct timeval fdtimeout;
37
38
39     /* Going daemon */
40     pid = getpid();
41     available_server = 0;
42     nowDaemon();
43     goDaemon();
44
45
46     /* Setting group ID */
47     if(Privsep_SetGroup(gid) < 0)
48         ErrorExit(SETGID_ERROR, ARGV0, group);
49
50
51     /* chrooting */
52     if(Privsep_Chroot(dir) < 0)
53         ErrorExit(CHROOT_ERROR, ARGV0, dir);
54
55
56     nowChroot();
57
58
59     if(Privsep_SetUser(uid) < 0)
60         ErrorExit(SETUID_ERROR, ARGV0, user);
61
62
63     /* Create the queue. In this case we are going to create
64      * and read from it
65      * Exit if fails.
66      */
67     if((logr->m_queue = StartMQ(DEFAULTQUEUE, READ)) < 0)
68         ErrorExit(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
69
70     maxfd = logr->m_queue;
71     logr->sock = -1;
72
73
74
75     /* Creating PID file */     
76     if(CreatePID(ARGV0, getpid()) < 0)
77         merror(PID_ERROR,ARGV0);
78
79
80     /* Reading the private keys  */
81     verbose(ENC_READ, ARGV0);
82
83     OS_ReadKeys(&keys);
84     OS_StartCounter(&keys);
85
86     /* cmoraes : changed the following call to
87     os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id);
88     */
89     os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id,
90                         logr->profile);
91
92
93     /* Start up message */
94     verbose(STARTUP_MSG, ARGV0, (int)getpid());
95
96
97     /* Initial random numbers */
98     #ifdef __OpenBSD__
99     srandomdev();
100     #else
101     srandom( time(0) + getpid()+ pid + getppid());
102     #endif
103
104     random();
105
106
107     /* Connecting UDP */
108     rc = 0;
109     while(rc < logr->rip_id)
110     {
111         verbose("%s: INFO: Server IP Address: %s", ARGV0, logr->rip[rc]);
112         rc++;
113     }
114
115
116     /* Trying to connect to the server */
117     if(!connect_server(0))
118     {
119         ErrorExit(UNABLE_CONN, ARGV0);
120     }
121
122
123     /* Setting max fd for select */
124     if(logr->sock > maxfd)
125     {
126         maxfd = logr->sock;
127     }
128
129
130     /* Connecting to the execd queue */
131     if(logr->execdq == 0)
132     {
133         if((logr->execdq = StartMQ(EXECQUEUE, WRITE)) < 0)
134         {
135             merror("%s: INFO: Unable to connect to the active response "
136                    "queue (disabled).", ARGV0);
137             logr->execdq = -1;
138         }
139     }
140
141
142
143     /* Trying to connect to server */
144     os_setwait();
145
146     start_agent(1);
147
148     os_delwait();
149
150
151     /* Sending integrity message for agent configs */
152     intcheck_file(OSSECCONF, dir);
153     intcheck_file(OSSEC_DEFINES, dir);
154
155
156     /* Sending first notification */
157     run_notify();
158
159
160     /* Maxfd must be higher socket +1 */
161     maxfd++;
162
163
164     /* monitor loop */
165     while(1)
166     {
167         /* Monitoring all available sockets from here */
168         FD_ZERO(&fdset);
169         FD_SET(logr->sock, &fdset);
170         FD_SET(logr->m_queue, &fdset);
171
172         fdtimeout.tv_sec = 120;
173         fdtimeout.tv_usec = 0;
174
175
176         /* Wait for 120 seconds at a maximum for any descriptor */
177         rc = select(maxfd, &fdset, NULL, NULL, &fdtimeout);
178         if(rc == -1)
179         {
180             ErrorExit(SELECT_ERROR, ARGV0);
181         }
182
183
184         else if(rc == 0)
185         {
186             continue;
187         }
188
189
190         /* For the receiver */
191         if(FD_ISSET(logr->sock, &fdset))
192         {
193             receive_msg();
194         }
195
196
197         /* For the forwarder */
198         if(FD_ISSET(logr->m_queue, &fdset))
199         {
200             EventForward();
201         }
202     }
203 }
204
205
206
207 /* EOF */