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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* Copyright (c) 1999-2002 Rinet Corp., Novosibirsk, Russia
* partial (c) vfom@narod.ru
*
* Redistribution and use in source forms, with and without modification,
* are permitted provided that this entire comment appears intact.
*
* THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
#ifdef HAVE_DMALLOC_H
#include <dmalloc.h>
#else
#include "malloc.h"
#endif
#include "assert.h"
#include "netmon.h"
#include "regex.h"
extern int errno;
extern CONFIG *cf; /* current configuration */
static int pipe_execfile __P ((char *, char *,pid_t *, OBJECT *));
void
pipe_start(method)
METHOD *method;
{
SESSION *sd = method->sd;
OBJECT *target = sd->owner;
int tmpval;
/* sanity check */
if (!sd) return;
tcp_stop(method); /* stop the method if still running */
if ((method->sd)->pid > 0) { kill((method->sd)->pid, SIGTERM); }
sd->pid = -1;
/* open stream socket near before TCP session */
if ((sd->sock = pipe_execfile(method->when, method->argument, &(sd->pid), target)) < 0) {
tcp_close(errno, sd, 0);
return;
}
/* turn on non-blocking I/O before connecting */
if (set_socket_async(sd->sock, TRUE) < 0) {
tcp_close(errno, sd, 0);
if ((method->sd)->pid > 0) { kill((method->sd)->pid, SIGTERM); }
return;
}
/* timeout and retries for connection */
sd->timeout = method->timeout * 1000000L; /* make miscroseconds */
sd->retries = method->retries;
sd->connect = tcp_connect; /* `awaiting' connection */
if ((tmpval = session_start(sd, NULL)) != 0) {
dprintf(("pipe_start(%s/%s): reqid=%d\n",
sd->owner->name, method->name, tmpval));
//report(LOG_INFO, "pipe_start(%s/%s): reqid=%d\n",
// sd->owner->name, method->name, tmpval);
}
}
int
pipe_init(target, method)
OBJECT *target;
METHOD *method;
{
SESSION template;
dprintf(("pipe_init(%s/%s)\n", target->name, method->name));
// report(LOG_INFO,"pipe_init(%s/%s)\n", target->name, method->name);
if (method->sd)
session_free(method->sd);
memset(&template, 0, sizeof(template));
template.owner = target;
template.method = method;
template.sock = -1; /* not yet opened */
template.pid = -1;
template.timeout = method->timeout * 1000000L; /* make microseconds */
template.retries = method->retries;
template.connect = tcp_connect;
template.send = tcp_send;
template.recv = tcp_recv;
template.read = tcp_close;
if ((method->sd = session_create(&template)) == NULL) {
errno = ENOMEM;
return -1;
}
return 0;
}
#define MAX_ARGS 50
int
pipe_execfile(file, args, p_pid, target)
char *file, *args;
pid_t *p_pid;
OBJECT *target;
{
sigset_t sigmask;
int ac = 0, fildes[2];
char *av[MAX_ARGS+2];
extern char **environ;
pid_t pid;
dprintf(("pipe_execfile: %s %s\n", file, args ? args : "null"));
// report(LOG_INFO,"pipe_execfile: %s %s\n", file, args ? args : "null");
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fildes) < 0) {
report(LOG_ERR, "socketpair: %m");
return(-1);
}
if (*file != '/') {
av[ac++] = "sh";
av[ac++] = "-c";
av[ac++] = file;
#ifdef _PATH_BSHELL
file = _PATH_BSHELL;
#else
file = "/bin/sh";
#endif
} else av[ac++] = strippath(file);
ac += make_argv(args, (char ***) &av[ac], MAX_ARGS);
av[ac] = NULL;
switch (pid=vfork()) {
case -1:
report(LOG_ERR, "fork: %m");
close(fildes[0]);
close(fildes[1]);
return(-1);
case 0: /* child */
if (fcntl(fildes[0], F_SETFD, 0) < 0) {
report(LOG_ERR," fcntl (F_SETFD, 0): %m");
_exit(127);
}
if (fildes[0] != 0) {
dup2(fildes[0], 0);
close(fildes[0]);
}
dup2(0, 1);
dup2(0, 2);
/* child would be terminated by signals */
sigprocmask(SIG_SETMASK, NULL, &sigmask);
sigprocmask(SIG_UNBLOCK, &sigmask, NULL);
close(netstate_sock);
/* make session leader to be able killpg() latter */
setsid();
if ( cf->chrootdir) {
if ( chroot( cf->chrootdir ) < 0 ) {
report(LOG_ERR, "chroot %s: %s", cf->chrootdir,strerror(*(__error())) );
_exit(127);
}
}
if ( setgid(cf->gid) < 0 ) {
report(LOG_ERR, "setgid %s[%d]: %s", cf->groupname, cf->gid, strerror(*(__error())) );
_exit(127);
}
if ( (cf->uid != 0) & (setuid(cf->uid) < 0) ) {
report(LOG_ERR, "setuid %s[%d]: %s", cf->username, cf->uid, strerror(*(__error())) );
_exit(127);
}
setenv ("OBJECT_NAME",target->name,1 );
setenv ("OBJECT_ADDRESS",target->address,1 );
if (target->srcaddress)
setenv ("OBJECT_SRC_ADDRESS",target->srcaddress,1 );
if (target->datadir)
setenv ("OBJECT_DATADIR",target->datadir,1 );
execve(file, av, environ);
report(LOG_ERR, "execve %s: %m", file);
_exit(127);
}
/* parent */
if ( p_pid != NULL ) { *p_pid = pid; };
close(fildes[0]);
return(fildes[1]);
}
void
when_stop(method)
METHOD *method;
{
// session_stop(method->sd, 1);
}
void
when_start(method)
METHOD *method;
{
SESSION *sd = method->sd;
OBJECT *target = sd->owner;
TIMEVAL tv_now;
char *diag, buf[4096];
int tmpval, matched;
/* sanity check */
if (!sd) return;
sd->sock=-1;
/* calculate condition */
insert_variables(buf, sizeof(buf), method->when, TYPE_OBJECT, target);
matched = (calculate(buf) ? 1 : 0);
// report(LOG_WARNING, "when_start EVAL: '%s' %d %s %d",buf,matched,target->name,sd->tv_sum.tv_sec);
diag = "OK" ;
if ( matched ) {
gettimeofday(&tv_now,NULL);
if ( (u_int64_t)sd->tv_sum.tv_sec != 0 ) {
if ( ( (u_int64_t)(tv_now.tv_sec) - (u_int64_t)(sd->tv_sum.tv_sec) ) > method->timeout ) {
insert_variables(buf, sizeof(buf), method->when_fmt, TYPE_OBJECT, target);
diag = buf;
}
} else {
sd->tv_sum.tv_sec = tv_now.tv_sec;
matched = 0;
}
} else {
sd->tv_sum.tv_sec=0;
}
// report(LOG_WARNING, "when_start DIAG: '%s' %d %d",diag,matched,sd->tv_sum.tv_sec);
dump_var_list(target->var_list);
method_finished(target, method, diag, !matched);
}
int
when_init(target, method)
OBJECT *target;
METHOD *method;
{
SESSION template;
dprintf(("when_init(%s/%s)\n", target->name, method->name));
if (method->sd)
session_free(method->sd);
memset(&template, 0, sizeof(template));
template.owner = target;
template.method = method;
template.sock = -1; /* never used socket */
if ((method->sd = session_create(&template)) == NULL) {
errno = ENOMEM;
return -1;
}
return 0;
}
|