/
cf-runagent.c
769 lines (645 loc) · 22.2 KB
/
cf-runagent.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
Copyright (C) CFEngine AS
This file is part of CFEngine 3 - written and maintained by CFEngine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <generic_agent.h>
#include <known_dirs.h>
#include <unix.h>
#include <eval_context.h>
#include <lastseen.h>
#include <crypto.h>
#include <files_names.h>
#include <promises.h>
#include <conversion.h>
#include <vars.h>
#include <client_code.h>
#include <communication.h>
#include <net.h>
#include <string_lib.h>
#include <rlist.h>
#include <scope.h>
#include <policy.h>
#include <audit.h>
#include <man.h>
#include <connection_info.h>
#include <addr_lib.h>
#include <loading.h>
#include <expand.h> /* ProtocolVersionParse */
#include <files_hashes.h>
typedef enum
{
RUNAGENT_CONTROL_HOSTS,
RUNAGENT_CONTROL_PORT_NUMBER,
RUNAGENT_CONTROL_FORCE_IPV4,
RUNAGENT_CONTROL_TRUSTKEY,
RUNAGENT_CONTROL_ENCRYPT,
RUNAGENT_CONTROL_BACKGROUND,
RUNAGENT_CONTROL_MAX_CHILD,
RUNAGENT_CONTROL_OUTPUT_TO_FILE,
RUNAGENT_CONTROL_OUTPUT_DIRECTORY,
RUNAGENT_CONTROL_TIMEOUT,
RUNAGENT_CONTROL_NONE
} RunagentControl;
static void ThisAgentInit(void);
static GenericAgentConfig *CheckOpts(int argc, char **argv);
static void KeepControlPromises(EvalContext *ctx, const Policy *policy);
static int HailServer(EvalContext *ctx, char *host);
static void SendClassData(AgentConnection *conn);
static void HailExec(AgentConnection *conn, char *peer, char *recvbuffer, char *sendbuffer);
static FILE *NewStream(char *name);
/*******************************************************************/
/* Command line options */
/*******************************************************************/
static const char *const CF_RUNAGENT_SHORT_DESCRIPTION =
"activate cf-agent on a remote host";
static const char *const CF_RUNAGENT_MANPAGE_LONG_DESCRIPTION =
"cf-runagent connects to a list of running instances of "
"cf-serverd. It allows foregoing the usual cf-execd schedule "
"to activate cf-agent. Additionally, a user "
"may send classes to be defined on the remote\n"
"host. Two kinds of classes may be sent: classes to decide "
"on which hosts cf-agent will be started, and classes that "
"the user requests cf-agent should define on execution. "
"The latter type is regulated by cf-serverd's role based access control.";
static const struct option OPTIONS[] =
{
{"help", no_argument, 0, 'h'},
{"background", optional_argument, 0, 'b'},
{"debug", no_argument, 0, 'd'},
{"verbose", no_argument, 0, 'v'},
{"dry-run", no_argument, 0, 'n'},
{"version", no_argument, 0, 'V'},
{"file", required_argument, 0, 'f'},
{"define-class", required_argument, 0, 'D'},
{"select-class", required_argument, 0, 's'},
{"inform", no_argument, 0, 'I'},
{"remote-options", required_argument, 0, 'o'},
{"diagnostic", no_argument, 0, 'x'},
{"hail", required_argument, 0, 'H'},
{"interactive", no_argument, 0, 'i'},
{"timeout", required_argument, 0, 't'},
{"legacy-output", no_argument, 0, 'l'},
{"color", optional_argument, 0, 'C'},
{NULL, 0, 0, '\0'}
};
static const char *const HINTS[] =
{
"Print the help message",
"Parallelize connections (50 by default)",
"Enable debugging output",
"Output verbose information about the behaviour of the agent",
"All talk and no action mode - make no changes, only inform of promises not kept",
"Output the version of the software",
"Specify an alternative input file than the default",
"Define a list of comma separated classes to be sent to a remote agent",
"Define a list of comma separated classes to be used to select remote agents by constraint",
"Print basic information about changes made to the system, i.e. promises repaired",
"Pass options to a remote server process",
"Activate internal diagnostics (developers only)",
"Hail the following comma-separated lists of hosts, overriding default list",
"Enable interactive mode for key trust",
"Connection timeout, seconds",
"Use legacy output format",
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
NULL
};
extern const ConstraintSyntax CFR_CONTROLBODY[];
int INTERACTIVE = false; /* GLOBAL_A */
int OUTPUT_TO_FILE = false; /* GLOBAL_P */
char OUTPUT_DIRECTORY[CF_BUFSIZE] = ""; /* GLOBAL_P */
int BACKGROUND = false; /* GLOBAL_P GLOBAL_A */
int MAXCHILD = 50; /* GLOBAL_P GLOBAL_A */
char REMOTE_AGENT_OPTIONS[CF_MAXVARSIZE] = ""; /* GLOBAL_A */
const Rlist *HOSTLIST = NULL; /* GLOBAL_P GLOBAL_A */
char SENDCLASSES[CF_MAXVARSIZE] = ""; /* GLOBAL_A */
char DEFINECLASSES[CF_MAXVARSIZE] = ""; /* GLOBAL_A */
/*****************************************************************************/
int main(int argc, char *argv[])
{
#if !defined(__MINGW32__)
int count = 0;
int status;
int pid;
#endif
GenericAgentConfig *config = CheckOpts(argc, argv);
EvalContext *ctx = EvalContextNew();
GenericAgentConfigApply(ctx, config);
GenericAgentDiscoverContext(ctx, config);
Policy *policy = LoadPolicy(ctx, config);
ThisAgentInit();
KeepControlPromises(ctx, policy); // Set RUNATTR using copy
if (BACKGROUND && INTERACTIVE)
{
Log(LOG_LEVEL_ERR, "You cannot specify background mode and interactive mode together");
exit(EXIT_FAILURE);
}
/* HvB */
if (HOSTLIST)
{
const Rlist *rp = HOSTLIST;
while (rp != NULL)
{
#ifdef __MINGW32__
if (BACKGROUND)
{
Log(LOG_LEVEL_VERBOSE,
"Windows does not support starting processes in the background - starting in foreground");
BACKGROUND = false;
}
#else
if (BACKGROUND) /* parallel */
{
if (count <= MAXCHILD)
{
if (fork() == 0) /* child process */
{
HailServer(ctx, RlistScalarValue(rp));
exit(EXIT_SUCCESS);
}
else /* parent process */
{
rp = rp->next;
count++;
}
}
else
{
pid = wait(&status);
Log(LOG_LEVEL_DEBUG, "child = %d, child number = %d", pid, count);
count--;
}
}
else /* serial */
#endif /* __MINGW32__ */
{
HailServer(ctx, RlistScalarValue(rp));
rp = rp->next;
}
} /* end while */
} /* end if HOSTLIST */
#ifndef __MINGW32__
if (BACKGROUND)
{
Log(LOG_LEVEL_NOTICE, "Waiting for child processes to finish");
while (count > 1)
{
pid = wait(&status);
Log(LOG_LEVEL_VERBOSE, "Child %d ended, number %d", pid, count);
count--;
}
}
#endif
PolicyDestroy(policy);
GenericAgentFinalize(ctx, config);
return 0;
}
/*******************************************************************/
static GenericAgentConfig *CheckOpts(int argc, char **argv)
{
extern char *optarg;
int optindex = 0;
int c;
GenericAgentConfig *config = GenericAgentConfigNewDefault(AGENT_TYPE_RUNAGENT);
DEFINECLASSES[0] = '\0';
SENDCLASSES[0] = '\0';
while ((c = getopt_long(argc, argv, "t:q:db:vnKhIif:D:VSxo:s:MH:lC::", OPTIONS, &optindex)) != EOF)
{
switch ((char) c)
{
case 'l':
Log(LOG_LEVEL_VERBOSE, "Legacy output has been deprecated");
break;
case 'f':
GenericAgentConfigSetInputFile(config, GetInputDir(), optarg);
MINUSF = true;
break;
case 'b':
BACKGROUND = true;
if (optarg)
{
MAXCHILD = atoi(optarg);
}
break;
case 'd':
LogSetGlobalLevel(LOG_LEVEL_DEBUG);
break;
case 'K':
config->ignore_locks = true;
break;
case 's':
strlcpy(SENDCLASSES, optarg, CF_MAXVARSIZE);
if (strlen(optarg) > CF_MAXVARSIZE)
{
Log(LOG_LEVEL_ERR, "Argument too long (-s)");
exit(EXIT_FAILURE);
}
break;
case 'D':
strlcpy(DEFINECLASSES, optarg, CF_MAXVARSIZE);
if (strlen(optarg) > CF_MAXVARSIZE)
{
Log(LOG_LEVEL_ERR, "Argument too long (-D)");
exit(EXIT_FAILURE);
}
break;
case 'H':
HOSTLIST = RlistFromSplitString(optarg, ',');
break;
case 'o':
strlcpy(REMOTE_AGENT_OPTIONS, optarg, CF_MAXVARSIZE);
break;
case 'I':
LogSetGlobalLevel(LOG_LEVEL_INFO);
break;
case 'i':
INTERACTIVE = true;
break;
case 'v':
LogSetGlobalLevel(LOG_LEVEL_VERBOSE);
break;
case 'n':
DONTDO = true;
config->ignore_locks = true;
break;
case 't':
CONNTIMEOUT = atoi(optarg);
break;
case 'V':
{
Writer *w = FileWriter(stdout);
GenericAgentWriteVersion(w);
FileWriterDetach(w);
}
exit(EXIT_SUCCESS);
case 'h':
{
Writer *w = FileWriter(stdout);
GenericAgentWriteHelp(w, "cf-runagent", OPTIONS, HINTS, true);
FileWriterDetach(w);
}
exit(EXIT_SUCCESS);
case 'M':
{
Writer *out = FileWriter(stdout);
ManPageWrite(out, "cf-runagent", time(NULL),
CF_RUNAGENT_SHORT_DESCRIPTION,
CF_RUNAGENT_MANPAGE_LONG_DESCRIPTION,
OPTIONS, HINTS,
true);
FileWriterDetach(out);
exit(EXIT_SUCCESS);
}
case 'x':
Log(LOG_LEVEL_ERR, "Self-diagnostic functionality is retired.");
exit(EXIT_SUCCESS);
case 'C':
if (!GenericAgentConfigParseColor(config, optarg))
{
exit(EXIT_FAILURE);
}
break;
default:
{
Writer *w = FileWriter(stdout);
GenericAgentWriteHelp(w, "cf-runagent", OPTIONS, HINTS, true);
FileWriterDetach(w);
}
exit(EXIT_FAILURE);
}
}
if (!GenericAgentConfigParseArguments(config, argc - optind, argv + optind))
{
Log(LOG_LEVEL_ERR, "Too many arguments");
exit(EXIT_FAILURE);
}
return config;
}
/*******************************************************************/
static void ThisAgentInit(void)
{
umask(077);
if (strstr(REMOTE_AGENT_OPTIONS, "--file") || strstr(REMOTE_AGENT_OPTIONS, "-f"))
{
Log(LOG_LEVEL_ERR,
"The specified remote options include a useless --file option. The remote server has promised to ignore this, thus it is disallowed.");
exit(EXIT_FAILURE);
}
}
/********************************************************************/
static int HailServer(EvalContext *ctx, char *host)
{
assert(host != NULL);
AgentConnection *conn;
char sendbuffer[CF_BUFSIZE], recvbuffer[CF_BUFSIZE],
hostkey[CF_HOSTKEY_STRING_SIZE], user[CF_SMALLBUF];
bool gotkey;
char reply[8];
bool trustkey = false;
char *hostname, *port;
ParseHostPort(host, &hostname, &port);
if (hostname == NULL || strcmp(hostname, "localhost") == 0)
{
Log(LOG_LEVEL_INFO, "No remote hosts were specified to connect to");
return false;
}
if (port == NULL)
{
port = "5308";
}
char ipaddr[CF_MAX_IP_LEN];
if (Hostname2IPString(ipaddr, hostname, sizeof(ipaddr)) == -1)
{
Log(LOG_LEVEL_ERR,
"HailServer: ERROR, could not resolve '%s'", hostname);
return false;
}
Address2Hostkey(hostkey, sizeof(hostkey), ipaddr);
GetCurrentUserName(user, CF_SMALLBUF);
if (INTERACTIVE)
{
Log(LOG_LEVEL_VERBOSE, "Using interactive key trust...");
gotkey = HavePublicKey(user, ipaddr, hostkey) != NULL;
if (!gotkey)
{
printf("WARNING - You do not have a public key from host %s = %s\n",
hostname, ipaddr);
printf(" Do you want to accept one on trust? (yes/no)\n\n--> ");
while (true)
{
if (fgets(reply, sizeof(reply), stdin) == NULL)
{
FatalError(ctx, "EOF trying to read answer from terminal");
}
if (Chop(reply, CF_EXPANDSIZE) == -1)
{
Log(LOG_LEVEL_ERR, "Chop was called on a string that seemed to have no terminator");
}
if (strcmp(reply, "yes") == 0)
{
printf("Will trust the key...\n");
trustkey = true;
break;
}
else if (strcmp(reply, "no") == 0)
{
printf("Will not trust the key...\n");
trustkey = false;
break;
}
else
{
printf("Please reply yes or no...(%s)\n", reply);
}
}
}
}
#ifndef __MINGW32__
if (BACKGROUND)
{
Log(LOG_LEVEL_INFO, "Hailing '%s' : %s, with options '%s' (parallel)", hostname, port,
REMOTE_AGENT_OPTIONS);
}
else
#endif
{
Log(LOG_LEVEL_INFO, "...........................................................................");
Log(LOG_LEVEL_INFO, " * Hailing %s : %s, with options \"%s\" (serial)", hostname, port,
REMOTE_AGENT_OPTIONS);
Log(LOG_LEVEL_INFO, "...........................................................................");
}
const char *s =
EvalContextVariableControlCommonGet(ctx, COMMON_CONTROL_PROTOCOL_VERSION);
ConnectionFlags connflags = {
.protocol_version = ProtocolVersionParse(s),
.trust_server = trustkey
};
int err = 0;
conn = ServerConnection(hostname, port, CONNTIMEOUT, connflags, &err);
if (conn == NULL)
{
Log(LOG_LEVEL_VERBOSE, "No suitable server responded to hail");
return false;
}
/* Send EXEC command. */
HailExec(conn, hostname, recvbuffer, sendbuffer);
return true;
}
/********************************************************************/
/* Level 2 */
/********************************************************************/
static void KeepControlPromises(EvalContext *ctx, const Policy *policy)
{
Seq *constraints = ControlBodyConstraints(policy, AGENT_TYPE_RUNAGENT);
if (constraints)
{
for (size_t i = 0; i < SeqLength(constraints); i++)
{
Constraint *cp = SeqAt(constraints, i);
if (!IsDefinedClass(ctx, cp->classes))
{
continue;
}
VarRef *ref = VarRefParseFromScope(cp->lval, "control_runagent");
const void *value = EvalContextVariableGet(ctx, ref, NULL);
VarRefDestroy(ref);
if (!value)
{
Log(LOG_LEVEL_ERR, "Unknown lval '%s' in runagent control body", cp->lval);
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_FORCE_IPV4].lval) == 0)
{
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_TRUSTKEY].lval) == 0)
{
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_ENCRYPT].lval) == 0)
{
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_PORT_NUMBER].lval) == 0)
{
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_BACKGROUND].lval) == 0)
{
/*
* Only process this option if are is no -b or -i options specified on
* command line.
*/
if (BACKGROUND || INTERACTIVE)
{
Log(LOG_LEVEL_WARNING,
"'background_children' setting from 'body runagent control' is overridden by command-line option.");
}
else
{
BACKGROUND = BooleanFromString(value);
}
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_MAX_CHILD].lval) == 0)
{
MAXCHILD = (short) IntFromString(value);
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_OUTPUT_TO_FILE].lval) == 0)
{
OUTPUT_TO_FILE = BooleanFromString(value);
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_OUTPUT_DIRECTORY].lval) == 0)
{
if (IsAbsPath(value))
{
strlcpy(OUTPUT_DIRECTORY, value, CF_BUFSIZE);
Log(LOG_LEVEL_VERBOSE, "Setting output direcory to '%s'", OUTPUT_DIRECTORY);
}
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_TIMEOUT].lval) == 0)
{
continue;
}
if (strcmp(cp->lval, CFR_CONTROLBODY[RUNAGENT_CONTROL_HOSTS].lval) == 0)
{
if (HOSTLIST == NULL) // Don't override if command line setting
{
HOSTLIST = value;
}
continue;
}
}
}
const char *expire_after = EvalContextVariableControlCommonGet(ctx, COMMON_CONTROL_LASTSEEN_EXPIRE_AFTER);
if (expire_after)
{
LASTSEENEXPIREAFTER = IntFromString(expire_after) * 60;
}
}
static void SendClassData(AgentConnection *conn)
{
Rlist *classes, *rp;
char sendbuffer[CF_BUFSIZE];
classes = RlistFromSplitRegex(SENDCLASSES, "[,: ]", 99, false);
for (rp = classes; rp != NULL; rp = rp->next)
{
if (SendTransaction(conn->conn_info, RlistScalarValue(rp), 0, CF_DONE) == -1)
{
Log(LOG_LEVEL_ERR, "Transaction failed. (send: %s)", GetErrorStr());
return;
}
}
snprintf(sendbuffer, CF_MAXVARSIZE, "%s", CFD_TERMINATOR);
if (SendTransaction(conn->conn_info, sendbuffer, 0, CF_DONE) == -1)
{
Log(LOG_LEVEL_ERR, "Transaction failed. (send: %s)", GetErrorStr());
return;
}
}
/********************************************************************/
static void HailExec(AgentConnection *conn, char *peer, char *recvbuffer, char *sendbuffer)
{
FILE *fp = stdout;
char *sp;
int n_read;
if (strlen(DEFINECLASSES))
{
snprintf(sendbuffer, CF_BUFSIZE, "EXEC %s -D%s", REMOTE_AGENT_OPTIONS, DEFINECLASSES);
}
else
{
snprintf(sendbuffer, CF_BUFSIZE, "EXEC %s", REMOTE_AGENT_OPTIONS);
}
if (SendTransaction(conn->conn_info, sendbuffer, 0, CF_DONE) == -1)
{
Log(LOG_LEVEL_ERR, "Transmission rejected. (send: %s)", GetErrorStr());
DisconnectServer(conn);
return;
}
fp = NewStream(peer);
SendClassData(conn);
while (true)
{
memset(recvbuffer, 0, CF_BUFSIZE);
if ((n_read = ReceiveTransaction(conn->conn_info, recvbuffer, NULL)) == -1)
{
return;
}
if (n_read == 0)
{
break;
}
if (strlen(recvbuffer) == 0)
{
continue;
}
if ((sp = strstr(recvbuffer, CFD_TERMINATOR)) != NULL)
{
break;
}
if ((sp = strstr(recvbuffer, "BAD:")) != NULL)
{
fprintf(fp, "%s> !! %s\n", VPREFIX, recvbuffer + 4);
continue;
}
if (strstr(recvbuffer, "too soon"))
{
fprintf(fp, "%s> !! %s\n", VPREFIX, recvbuffer);
continue;
}
fprintf(fp, "%s> -> %s", VPREFIX, recvbuffer);
}
if (fp != stdout)
{
fclose(fp);
}
DisconnectServer(conn);
}
/********************************************************************/
/* Level */
/********************************************************************/
static FILE *NewStream(char *name)
{
FILE *fp;
char filename[CF_BUFSIZE];
if (OUTPUT_DIRECTORY[0] != '\0')
{
snprintf(filename, CF_BUFSIZE, "%s/%s_runagent.out", OUTPUT_DIRECTORY, name);
}
else
{
snprintf(filename, CF_BUFSIZE, "%s%coutputs%c%s_runagent.out",
GetWorkDir(), FILE_SEPARATOR, FILE_SEPARATOR, name);
}
if (OUTPUT_TO_FILE)
{
printf("Opening file...%s\n", filename);
if ((fp = fopen(filename, "w")) == NULL)
{
Log(LOG_LEVEL_ERR, "Unable to open file '%s'", filename);
fp = stdout;
}
}
else
{
fp = stdout;
}
return fp;
}