1 /* @(#) $Id: agentd.c,v 1.44 2009/06/24 17:06:24 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
12 /* Part of the OSSEC HIDS
13 * Available at http://www.ossec.net/hids/
20 #include "os_net/os_net.h"
24 /* AgentdStart v0.2, 2005/11/09
25 * Starts the agent daemon.
27 void AgentdStart(char *dir, int uid, int gid, char *user, char *group)
35 struct timeval fdtimeout;
45 /* Setting group ID */
46 if(Privsep_SetGroup(gid) < 0)
47 ErrorExit(SETGID_ERROR, ARGV0, group);
51 if(Privsep_Chroot(dir) < 0)
52 ErrorExit(CHROOT_ERROR, ARGV0, dir);
58 if(Privsep_SetUser(uid) < 0)
59 ErrorExit(SETUID_ERROR, ARGV0, user);
62 /* Create the queue. In this case we are going to create
66 if((logr->m_queue = StartMQ(DEFAULTQUEUE, READ)) < 0)
67 ErrorExit(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
69 maxfd = logr->m_queue;
74 /* Creating PID file */
75 if(CreatePID(ARGV0, getpid()) < 0)
76 merror(PID_ERROR,ARGV0);
79 /* Reading the private keys */
80 verbose(ENC_READ, ARGV0);
83 OS_StartCounter(&keys);
84 os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id);
87 /* Start up message */
88 verbose(STARTUP_MSG, ARGV0, (int)getpid());
91 /* Initial random numbers */
95 srandom( time(0) + getpid()+ pid + getppid());
103 while(rc < logr->rip_id)
105 verbose("%s: INFO: Server IP Address: %s", ARGV0, logr->rip[rc]);
110 /* Trying to connect to the server */
111 if(!connect_server(0))
113 ErrorExit(UNABLE_CONN, ARGV0);
117 /* Setting max fd for select */
118 if(logr->sock > maxfd)
124 /* Connecting to the execd queue */
125 if(logr->execdq == 0)
127 if((logr->execdq = StartMQ(EXECQUEUE, WRITE)) < 0)
129 merror("%s: INFO: Unable to connect to the active response "
130 "queue (disabled).", ARGV0);
137 /* Trying to connect to server */
145 /* Sending integrity message for agent configs */
146 intcheck_file(OSSECCONF, dir);
147 intcheck_file(OSSEC_DEFINES, dir);
150 /* Sending first notification */
154 /* Maxfd must be higher socket +1 */
161 /* Monitoring all available sockets from here */
163 FD_SET(logr->sock, &fdset);
164 FD_SET(logr->m_queue, &fdset);
166 fdtimeout.tv_sec = 120;
167 fdtimeout.tv_usec = 0;
170 /* Wait for 120 seconds at a maximum for any descriptor */
171 rc = select(maxfd, &fdset, NULL, NULL, &fdtimeout);
174 ErrorExit(SELECT_ERROR, ARGV0);
184 /* For the receiver */
185 if(FD_ISSET(logr->sock, &fdset))
191 /* For the forwarder */
192 if(FD_ISSET(logr->m_queue, &fdset))