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;
46 /* Setting group ID */
47 if(Privsep_SetGroup(gid) < 0)
48 ErrorExit(SETGID_ERROR, ARGV0, group);
52 if(Privsep_Chroot(dir) < 0)
53 ErrorExit(CHROOT_ERROR, ARGV0, dir);
59 if(Privsep_SetUser(uid) < 0)
60 ErrorExit(SETUID_ERROR, ARGV0, user);
63 /* Create the queue. In this case we are going to create
67 if((logr->m_queue = StartMQ(DEFAULTQUEUE, READ)) < 0)
68 ErrorExit(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
70 maxfd = logr->m_queue;
75 /* Creating PID file */
76 if(CreatePID(ARGV0, getpid()) < 0)
77 merror(PID_ERROR,ARGV0);
80 /* Reading the private keys */
81 verbose(ENC_READ, ARGV0);
84 OS_StartCounter(&keys);
86 /* cmoraes : changed the following call to
87 os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id);
89 os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id,
93 /* Start up message */
94 verbose(STARTUP_MSG, ARGV0, (int)getpid());
97 /* Initial random numbers */
101 srandom( time(0) + getpid()+ pid + getppid());
109 while(rc < logr->rip_id)
111 verbose("%s: INFO: Server IP Address: %s", ARGV0, logr->rip[rc]);
116 /* Trying to connect to the server */
117 if(!connect_server(0))
119 ErrorExit(UNABLE_CONN, ARGV0);
123 /* Setting max fd for select */
124 if(logr->sock > maxfd)
130 /* Connecting to the execd queue */
131 if(logr->execdq == 0)
133 if((logr->execdq = StartMQ(EXECQUEUE, WRITE)) < 0)
135 merror("%s: INFO: Unable to connect to the active response "
136 "queue (disabled).", ARGV0);
143 /* Trying to connect to server */
151 /* Sending integrity message for agent configs */
152 intcheck_file(OSSECCONF, dir);
153 intcheck_file(OSSEC_DEFINES, dir);
156 /* Sending first notification */
160 /* Maxfd must be higher socket +1 */
167 /* Monitoring all available sockets from here */
169 FD_SET(logr->sock, &fdset);
170 FD_SET(logr->m_queue, &fdset);
172 fdtimeout.tv_sec = 120;
173 fdtimeout.tv_usec = 0;
176 /* Wait for 120 seconds at a maximum for any descriptor */
177 rc = select(maxfd, &fdset, NULL, NULL, &fdtimeout);
180 ErrorExit(SELECT_ERROR, ARGV0);
190 /* For the receiver */
191 if(FD_ISSET(logr->sock, &fdset))
197 /* For the forwarder */
198 if(FD_ISSET(logr->m_queue, &fdset))