1 /* @(#) $Id: ./src/client-agent/agentd.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
13 /* Part of the OSSEC HIDS
14 * Available at http://www.ossec.net/hids/
21 #include "os_net/os_net.h"
25 /* AgentdStart v0.2, 2005/11/09
26 * Starts the agent daemon.
28 void AgentdStart(char *dir, int uid, int gid, char *user, char *group)
36 struct timeval fdtimeout;
51 /* Setting group ID */
52 if(Privsep_SetGroup(gid) < 0)
53 ErrorExit(SETGID_ERROR, ARGV0, group);
57 if(Privsep_Chroot(dir) < 0)
58 ErrorExit(CHROOT_ERROR, ARGV0, dir);
64 if(Privsep_SetUser(uid) < 0)
65 ErrorExit(SETUID_ERROR, ARGV0, user);
68 /* Create the queue. In this case we are going to create
72 if((agt->m_queue = StartMQ(DEFAULTQUEUE, READ)) < 0)
73 ErrorExit(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
80 /* Creating PID file */
81 if(CreatePID(ARGV0, getpid()) < 0)
82 merror(PID_ERROR,ARGV0);
85 /* Reading the private keys */
86 verbose(ENC_READ, ARGV0);
89 OS_StartCounter(&keys);
91 /* cmoraes : changed the following call to
92 os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id);
94 os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id,
98 /* Start up message */
99 verbose(STARTUP_MSG, ARGV0, (int)getpid());
102 /* Initial random numbers */
106 srandom( time(0) + getpid()+ pid + getppid());
114 while(rc < agt->rip_id)
116 verbose("%s: INFO: Server IP Address: %s", ARGV0, agt->rip[rc]);
121 /* Trying to connect to the server */
122 if(!connect_server(0))
124 ErrorExit(UNABLE_CONN, ARGV0);
128 /* Setting max fd for select */
129 if(agt->sock > maxfd)
135 /* Connecting to the execd queue */
138 if((agt->execdq = StartMQ(EXECQUEUE, WRITE)) < 0)
140 merror("%s: INFO: Unable to connect to the active response "
141 "queue (disabled).", ARGV0);
148 /* Trying to connect to server */
156 /* Sending integrity message for agent configs */
157 intcheck_file(OSSECCONF, dir);
158 intcheck_file(OSSEC_DEFINES, dir);
161 /* Sending first notification */
165 /* Maxfd must be higher socket +1 */
172 /* Monitoring all available sockets from here */
174 FD_SET(agt->sock, &fdset);
175 FD_SET(agt->m_queue, &fdset);
177 fdtimeout.tv_sec = 1;
178 fdtimeout.tv_usec = 0;
180 /* Continuously send notifications */
183 /* Wait with a timeout for any descriptor */
184 rc = select(maxfd, &fdset, NULL, NULL, &fdtimeout);
187 ErrorExit(SELECT_ERROR, ARGV0);
197 /* For the receiver */
198 if(FD_ISSET(agt->sock, &fdset))
204 /* For the forwarder */
205 if(FD_ISSET(agt->m_queue, &fdset))