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
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech 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 <verify_exec.h>
#include <actuator.h>
#include <promises.h>
#include <files_lib.h>
#include <files_interfaces.h>
#include <vars.h>
#include <conversion.h>
#include <instrumentation.h>
#include <attributes.h>
#include <pipes.h>
#include <locks.h>
#include <evalfunction.h>
#include <exec_tools.h>
#include <misc_lib.h>
#include <writer.h>
#include <policy.h>
#include <string_lib.h>
#include <scope.h>
#include <ornaments.h>
#include <eval_context.h>
#include <retcode.h>
#include <timeout.h>
typedef enum
{
ACTION_RESULT_OK,
ACTION_RESULT_TIMEOUT,
ACTION_RESULT_FAILED
} ActionResult;
static bool SyntaxCheckExec(const Attributes *attr, const Promise *pp);
static bool PromiseKeptExec(const Attributes *a, const Promise *pp);
static char *GetLockNameExec(const Attributes *a, const Promise *pp);
static ActionResult RepairExec(EvalContext *ctx, const Attributes *a, const Promise *pp, PromiseResult *result);
static void PreviewProtocolLine(char *line, char *comm);
char* BuildCommandLine(const Attributes *a, const Promise *pp)
{
assert(a != NULL);
Writer *w = StringWriter();
WriterWriteF(w, "%s", pp->promiser);
if (a->args)
{
WriterWrite(w, " ");
WriterWrite(w, a->args);
}
if (a->arglist)
{
for (const Rlist *rp = a->arglist; rp != NULL; rp = rp->next)
{
if (rp->val.type == RVAL_TYPE_SCALAR)
{
WriterWrite(w, " ");
WriterWrite(w, RlistScalarValue(rp));
}
else
{
Log(LOG_LEVEL_INFO, "GetLockNameExec: invalid rval (not a scalar) in arglist of commands promise '%s'", pp->promiser);
}
}
}
return StringWriterClose(w);
}
PromiseResult VerifyExecPromise(EvalContext *ctx, const Promise *pp)
{
Attributes a = GetExecAttributes(ctx, pp);
if (!SyntaxCheckExec(&a, pp))
{
return PROMISE_RESULT_FAIL;
}
if (PromiseKeptExec(&a, pp))
{
return PROMISE_RESULT_NOOP;
}
char *lock_name = GetLockNameExec(&a, pp);
CfLock thislock = AcquireLock(ctx, lock_name, VUQNAME, CFSTARTTIME, a.transaction.ifelapsed, a.transaction.expireafter, pp, false);
free(lock_name);
if (thislock.lock == NULL)
{
return PROMISE_RESULT_SKIPPED;
}
PromiseBanner(ctx,pp);
PromiseResult result = PROMISE_RESULT_NOOP;
/* See VerifyCommandRetcode for interpretation of return codes.
* Unless overridden by attributes in body classes, an exit code 0 means
* reparied (PROMISE_RESULT_CHANGE), an exit code != 0 means failure.
*/
switch (RepairExec(ctx, &a, pp, &result))
{
case ACTION_RESULT_OK:
result = PromiseResultUpdate(result, PROMISE_RESULT_NOOP);
break;
case ACTION_RESULT_TIMEOUT:
result = PromiseResultUpdate(result, PROMISE_RESULT_TIMEOUT);
break;
case ACTION_RESULT_FAILED:
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
break;
default:
ProgrammingError("Unexpected ActionResult value");
}
YieldCurrentLock(thislock);
return result;
}
/*****************************************************************************/
/* Level */
/*****************************************************************************/
static bool SyntaxCheckExec(const Attributes *attr, const Promise *pp)
{
assert(attr != NULL);
Attributes a = *attr; // TODO get rid of this, this function was probably
// intended to have side effects on the attr struct
if ((a.contain.nooutput) && (a.contain.preview))
{
Log(LOG_LEVEL_ERR, "no_output and preview are mutually exclusive (broken promise)");
PromiseRef(LOG_LEVEL_ERR, pp);
return false;
}
#ifdef __MINGW32__
if (a.contain.umask != (mode_t)CF_UNDEFINED)
{
Log(LOG_LEVEL_VERBOSE, "contain.umask is ignored on Windows");
}
if (a.contain.owner != CF_UNDEFINED)
{
Log(LOG_LEVEL_VERBOSE, "contain.exec_owner is ignored on Windows");
}
if (a.contain.group != CF_UNDEFINED)
{
Log(LOG_LEVEL_VERBOSE, "contain.exec_group is ignored on Windows");
}
if (a.contain.chroot != NULL)
{
Log(LOG_LEVEL_VERBOSE, "contain.chroot is ignored on Windows");
}
#else /* !__MINGW32__ */
if (a.contain.umask == (mode_t)CF_UNDEFINED)
{
a.contain.umask = 077; // FIXME: This has no effect!
}
#endif /* !__MINGW32__ */
return true;
}
static bool PromiseKeptExec(ARG_UNUSED const Attributes *a, ARG_UNUSED const Promise *pp)
{
return false;
}
static char *GetLockNameExec(const Attributes *a, const Promise *pp)
{
return BuildCommandLine(a, pp);
}
/*****************************************************************************/
static ActionResult RepairExec(EvalContext *ctx, const Attributes *a,
const Promise *pp, PromiseResult *result)
{
assert(a != NULL);
assert(pp != NULL);
char cmdline[CF_BUFSIZE];
char comm[20];
int count = 0;
#if !defined(__MINGW32__)
mode_t maskval = 0;
#endif
FILE *pfp;
char cmdOutBuf[CF_BUFSIZE];
size_t cmdOutBufPos = 0;
size_t lineOutLen;
char module_context[CF_BUFSIZE];
module_context[0] = '\0';
if (IsAbsoluteFileName(CommandArg0(pp->promiser)) || a->contain.shelltype == SHELL_TYPE_NONE)
{
if (!IsExecutable(CommandArg0(pp->promiser)))
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "'%s' promises to be executable but isn't", pp->promiser);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_FAIL);
if (strchr(pp->promiser, ' '))
{
Log(LOG_LEVEL_VERBOSE, "Paths with spaces must be inside escaped quoutes (e.g. \\\"%s\\\")", pp->promiser);
}
return ACTION_RESULT_FAILED;
}
else
{
Log(LOG_LEVEL_VERBOSE, "Promiser string contains a valid executable '%s' - ok", CommandArg0(pp->promiser));
}
}
char timeout_str[CF_BUFSIZE];
if (a->contain.timeout == CF_NOINT)
{
snprintf(timeout_str, CF_BUFSIZE, "no timeout");
}
else
{
snprintf(timeout_str, CF_BUFSIZE, "timeout=%ds", a->contain.timeout);
}
char owner_str[CF_BUFSIZE] = "";
if (a->contain.owner != (uid_t) -1)
{
snprintf(owner_str, CF_BUFSIZE, ",uid=%ju", (uintmax_t)a->contain.owner);
}
char group_str[CF_BUFSIZE] = "";
if (a->contain.group != (gid_t) -1)
{
snprintf(group_str, CF_BUFSIZE, ",gid=%ju", (uintmax_t)a->contain.group);
}
char* temp = BuildCommandLine(a, pp);
snprintf(cmdline, CF_BUFSIZE, "%s", temp); // TODO: remove CF_BUFSIZE limitation
free(temp);
const LogLevel info_or_verbose = a->inform ? LOG_LEVEL_INFO : LOG_LEVEL_VERBOSE;
Log(info_or_verbose, "Executing '%s%s%s' ... '%s'", timeout_str, owner_str, group_str, cmdline);
BeginMeasure();
if (DONTDO && (!a->contain.preview))
{
cfPS(ctx, LOG_LEVEL_WARNING, PROMISE_RESULT_WARN, pp, a, "Would execute script '%s'", cmdline);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_WARN);
return ACTION_RESULT_OK;
}
if (a->transaction.action != cfa_fix)
{
cfPS(ctx, LOG_LEVEL_WARNING, PROMISE_RESULT_WARN, pp, a, "Command '%s' needs to be executed, but only warning was promised", cmdline);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_WARN);
return ACTION_RESULT_OK;
}
CommandPrefix(cmdline, comm);
bool do_work_here = true;
#ifndef __MINGW32__
if (a->transaction.background)
{
Log(LOG_LEVEL_VERBOSE, "Backgrounding job '%s'", cmdline);
do_work_here = (fork() == 0); // true for child, false for parent
}
#endif
if (do_work_here) // work done here: either by child or non-background parent
{
if (a->contain.timeout != CF_NOINT)
{
SetTimeOut(a->contain.timeout);
}
#ifndef __MINGW32__
Log(LOG_LEVEL_VERBOSE, "Setting umask to %jo", (uintmax_t)a->contain.umask);
maskval = umask(a->contain.umask);
if (a->contain.umask == 0)
{
Log(LOG_LEVEL_VERBOSE, "Programming '%s' running with umask 0! Use umask= to set", cmdline);
}
#endif /* !__MINGW32__ */
const char *open_mode = a->module ? "rt" : "r";
if (a->contain.shelltype == SHELL_TYPE_POWERSHELL)
{
#ifdef __MINGW32__
pfp =
cf_popen_powershell_setuid(cmdline, open_mode, a->contain.owner, a->contain.group, a->contain.chdir, a->contain.chroot,
a->transaction.background);
#else // !__MINGW32__
Log(LOG_LEVEL_ERR, "Powershell is only supported on Windows");
return ACTION_RESULT_FAILED;
#endif // !__MINGW32__
}
else if (a->contain.shelltype == SHELL_TYPE_USE)
{
pfp =
cf_popen_shsetuid(cmdline, open_mode, a->contain.owner, a->contain.group, a->contain.chdir, a->contain.chroot,
a->transaction.background);
}
else
{
char *const command = (a->args == NULL)
? xstrdup(pp->promiser)
: StringFormat("%s %s", pp->promiser,
a->args);
Seq *arglist = NULL;
if (a->arglist != NULL)
{
arglist = SeqNew(8, NULL);
for (const Rlist *rp = a->arglist; rp != NULL; rp = rp->next)
{
if (rp->val.type == RVAL_TYPE_SCALAR)
{
SeqAppend(arglist, RlistScalarValue(rp));
}
else
{
Log(LOG_LEVEL_ERR,
"RepairExec: invalid rval (not a scalar) in arglist of commands promise '%s'",
pp->promiser);
}
}
}
pfp = cf_popensetuid(command, arglist, open_mode,
a->contain.owner, a->contain.group,
a->contain.chdir, a->contain.chroot,
a->transaction.background);
free(command);
SeqDestroy(arglist);
}
if (pfp == NULL)
{
Log(LOG_LEVEL_ERR, "Couldn't open pipe to command '%s'. (cf_popen: %s)", cmdline, GetErrorStr());
return ACTION_RESULT_FAILED;
}
StringSet *module_tags = StringSetNew();
long persistence = 0;
size_t line_size = CF_BUFSIZE;
char *line = xmalloc(line_size);
for (;;)
{
ssize_t res = CfReadLine(&line, &line_size, pfp);
if (res == -1)
{
if (!feof(pfp))
{
Log(LOG_LEVEL_ERR, "Unable to read output from command '%s'. (fread: %s)", cmdline, GetErrorStr());
cf_pclose(pfp);
free(line);
return ACTION_RESULT_FAILED;
}
else
{
break;
}
}
if (strstr(line, "cfengine-die"))
{
break;
}
if (a->contain.preview)
{
PreviewProtocolLine(line, cmdline);
}
if (a->module)
{
ModuleProtocol(ctx, cmdline, line, !a->contain.nooutput, module_context, sizeof(module_context), module_tags, &persistence);
}
if (!a->contain.nooutput && !EmptyString(line))
{
lineOutLen = strlen(comm) + strlen(line) + 12;
// if buffer is to small for this line, output it directly
if (lineOutLen > sizeof(cmdOutBuf))
{
Log(LOG_LEVEL_NOTICE, "Q: '%s': %s", comm, line);
}
else
{
if (cmdOutBufPos + lineOutLen > sizeof(cmdOutBuf))
{
Log(LOG_LEVEL_NOTICE, "%s", cmdOutBuf);
cmdOutBufPos = 0;
}
snprintf(cmdOutBuf + cmdOutBufPos,
sizeof(cmdOutBuf) - cmdOutBufPos,
"Q: \"...%s\": %s\n", comm, line);
cmdOutBufPos += (lineOutLen - 1);
}
count++;
}
}
StringSetDestroy(module_tags);
free(line);
#ifdef __MINGW32__
if (a->transaction.background) // only get return value if we waited for command execution
{
cf_pclose_nowait(pfp);
}
else
#endif /* __MINGW32__ */
{
int ret = cf_pclose(pfp);
if (ret == -1)
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Finished script '%s' - failed (abnormal termination)", pp->promiser);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_FAIL);
}
else
{
VerifyCommandRetcode(ctx, ret, a, pp, result);
}
}
}
if (count)
{
if (cmdOutBufPos)
{
Log(LOG_LEVEL_NOTICE, "%s", cmdOutBuf);
}
Log(LOG_LEVEL_INFO, "Last %d quoted lines were generated by promiser '%s'", count, cmdline);
}
if (a->contain.timeout != CF_NOINT)
{
alarm(0);
signal(SIGALRM, SIG_DFL);
}
Log(info_or_verbose, "Completed execution of '%s'", cmdline);
#ifndef __MINGW32__
umask(maskval);
#endif
#ifndef __MINGW32__
if ((a->transaction.background) && do_work_here) // Child process
{
Log(LOG_LEVEL_VERBOSE, "Backgrounded command '%s' is done - exiting", cmdline);
// _exit() since this is the child and we don't want to run cleanup
_exit(EXIT_SUCCESS);
}
#endif /* !__MINGW32__ */
return ACTION_RESULT_OK;
}
/*************************************************************/
/* Level */
/*************************************************************/
void PreviewProtocolLine(char *line, char *comm)
{
int i;
char *message = line;
/*
* Table matching cfoutputlevel enums to log prefixes.
*/
char *prefixes[] =
{
":silent:",
":inform:",
":verbose:",
":editverbose:",
":error:",
":logonly:",
};
int precount = sizeof(prefixes) / sizeof(char *);
if (line[0] == ':')
{
/*
* Line begins with colon - see if it matches a log prefix.
*/
for (i = 0; i < precount; i++)
{
int prelen = 0;
prelen = strlen(prefixes[i]);
if (strncmp(line, prefixes[i], prelen) == 0)
{
/*
* Found log prefix - set logging level, and remove the
* prefix from the log message.
*/
message += prelen;
break;
}
}
}
Log(LOG_LEVEL_VERBOSE, "'%s', preview of '%s'", message, comm);
}
|