1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
12 #include "os_net/os_net.h"
15 /* Start the agent daemon */
16 void AgentdStart(const char *dir, int uid, int gid, const char *user, const char *group)
21 struct timeval fdtimeout;
25 /* Initial random numbers must happen before chroot */
29 if (!run_foreground) {
35 if (Privsep_SetGroup(gid) < 0) {
36 ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
40 if (Privsep_Chroot(dir) < 0) {
41 ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
45 if (Privsep_SetUser(uid) < 0) {
46 ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
49 /* Create the queue and read from it. Exit if fails. */
50 if ((agt->m_queue = StartMQ(DEFAULTQUEUE, READ)) < 0) {
51 ErrorExit(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
58 if (CreatePID(ARGV0, getpid()) < 0) {
59 merror(PID_ERROR, ARGV0);
62 /* Read private keys */
63 verbose(ENC_READ, ARGV0);
66 OS_StartCounter(&keys);
68 os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id,
71 /* Start up message */
72 verbose(STARTUP_MSG, ARGV0, (int)getpid());
78 while (rc < agt->rip_id) {
79 verbose("%s: INFO: Server %d: %s", ARGV0, rc+1, agt->rip[rc]);
83 /* Try to connect to the server */
84 if (!connect_server(0)) {
85 ErrorExit(UNABLE_CONN, ARGV0);
88 /* Set max fd for select */
89 if (agt->sock > maxfd) {
93 /* Connect to the execd queue */
94 if (agt->execdq == 0) {
95 if ((agt->execdq = StartMQ(EXECQUEUE, WRITE)) < 0) {
96 merror("%s: INFO: Unable to connect to the active response "
97 "queue (disabled).", ARGV0);
102 /* Try to connect to server */
109 /* Send integrity message for agent configs */
110 intcheck_file(OSSECCONF, dir);
111 intcheck_file(OSSEC_DEFINES, dir);
113 /* Send first notification */
116 /* Maxfd must be higher socket +1 */
121 /* Monitor all available sockets from here */
123 FD_SET(agt->sock, &fdset);
124 FD_SET(agt->m_queue, &fdset);
126 fdtimeout.tv_sec = 1;
127 fdtimeout.tv_usec = 0;
129 /* Continuously send notifications */
132 /* Wait with a timeout for any descriptor */
133 rc = select(maxfd, &fdset, NULL, NULL, &fdtimeout);
135 ErrorExit(SELECT_ERROR, ARGV0, errno, strerror(errno));
136 } else if (rc == 0) {
140 /* For the receiver */
141 if (FD_ISSET(agt->sock, &fdset)) {
145 /* For the forwarder */
146 if (FD_ISSET(agt->m_queue, &fdset)) {