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