-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathagentd.c
More file actions
152 lines (118 loc) · 3.57 KB
/
Copy pathagentd.c
File metadata and controls
152 lines (118 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright (C) 2009 Trend Micro Inc.
* All right reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation
*/
#include "shared.h"
#include "agentd.h"
#include "os_net/os_net.h"
/* Start the agent daemon */
void AgentdStart(const char *dir, int uid, int gid, const char *user, const char *group)
{
int rc = 0;
int maxfd = 0;
fd_set fdset;
struct timeval fdtimeout;
available_server = 0;
/* Initial random numbers must happen before chroot */
srandom_init();
/* Going Daemon */
if (!run_foreground) {
nowDaemon();
goDaemon();
}
/* Set group ID */
if (Privsep_SetGroup(gid) < 0) {
ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
}
/* chroot */
if (Privsep_Chroot(dir) < 0) {
ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
}
nowChroot();
if (Privsep_SetUser(uid) < 0) {
ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
}
/* Create the queue and read from it. Exit if fails. */
if ((agt->m_queue = StartMQ(DEFAULTQUEUE, READ)) < 0) {
ErrorExit(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
}
maxfd = agt->m_queue;
agt->sock = -1;
agt->sock_r = -1;
/* Create PID file */
if (CreatePID(ARGV0, getpid()) < 0) {
merror(PID_ERROR, ARGV0);
}
/* Read private keys */
verbose(ENC_READ, ARGV0);
OS_ReadKeys(&keys);
OS_StartCounter(&keys);
os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id,
agt->profile);
/* Start up message */
verbose(STARTUP_MSG, ARGV0, (int)getpid());
random();
/* Connect UDP */
rc = 0;
while (rc < agt->rip_id) {
verbose("%s: INFO: Server %d: %s", ARGV0, rc+1, agt->rip[rc]);
rc++;
}
/* Try to connect to the server */
if (!connect_server(0)) {
ErrorExit(UNABLE_CONN, ARGV0);
}
/* Set max fd for select */
if (agt->sock > maxfd) {
maxfd = agt->sock;
}
/* Connect to the execd queue */
if (agt->execdq == 0) {
if ((agt->execdq = StartMQ(EXECQUEUE, WRITE)) < 0) {
merror("%s: INFO: Unable to connect to the active response "
"queue (disabled).", ARGV0);
agt->execdq = -1;
}
}
/* Try to connect to server */
os_setwait();
start_agent(1);
os_delwait();
/* Send integrity message for agent configs */
intcheck_file(OSSECCONF, dir);
intcheck_file(OSSEC_DEFINES, dir);
/* Send first notification */
run_notify();
/* Maxfd must be higher socket +1 */
maxfd++;
/* Monitor loop */
while (1) {
/* Monitor all available sockets from here */
FD_ZERO(&fdset);
FD_SET(agt->sock, &fdset);
FD_SET(agt->m_queue, &fdset);
fdtimeout.tv_sec = 1;
fdtimeout.tv_usec = 0;
/* Continuously send notifications */
run_notify();
/* Wait with a timeout for any descriptor */
rc = select(maxfd, &fdset, NULL, NULL, &fdtimeout);
if (rc == -1) {
ErrorExit(SELECT_ERROR, ARGV0, errno, strerror(errno));
} else if (rc == 0) {
continue;
}
/* For the receiver */
if (FD_ISSET(agt->sock, &fdset)) {
receive_msg();
}
/* For the forwarder */
if (FD_ISSET(agt->m_queue, &fdset)) {
EventForward();
}
}
}