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
|
/*
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 <execd-config.h>
#include <generic_agent.h>
#include <eval_context.h>
#include <conversion.h>
#include <string_lib.h>
#include <mod_common.h> /* CFS_CONTROLBODY, SERVER_CONTROL_CFRUNCOMMAND */
#include <expand.h> /* ExpandScalar() */
/* Get a number in the interval [0, 1) for a calculation of a pseudo-random delay */
static double GetSplay(void)
{
char splay[CF_BUFSIZE];
snprintf(splay, CF_BUFSIZE, "%s+%s+%ju", VFQNAME, VIPADDRESS, (uintmax_t)getuid());
return ((double) MIN(StringHash(splay, 0), UINT_MAX - 1)) / UINT_MAX;
}
ExecdConfig *ExecdConfigNew(const EvalContext *ctx, const Policy *policy)
{
ExecdConfig *execd_config = xcalloc(1, sizeof(ExecdConfig));
execd_config->schedule = StringSetNew();
StringSetAdd(execd_config->schedule, xstrdup("Min00"));
StringSetAdd(execd_config->schedule, xstrdup("Min05"));
StringSetAdd(execd_config->schedule, xstrdup("Min10"));
StringSetAdd(execd_config->schedule, xstrdup("Min15"));
StringSetAdd(execd_config->schedule, xstrdup("Min20"));
StringSetAdd(execd_config->schedule, xstrdup("Min25"));
StringSetAdd(execd_config->schedule, xstrdup("Min30"));
StringSetAdd(execd_config->schedule, xstrdup("Min35"));
StringSetAdd(execd_config->schedule, xstrdup("Min40"));
StringSetAdd(execd_config->schedule, xstrdup("Min45"));
StringSetAdd(execd_config->schedule, xstrdup("Min50"));
StringSetAdd(execd_config->schedule, xstrdup("Min55"));
execd_config->splay_time = 0;
execd_config->log_facility = xstrdup("LOG_USER");
execd_config->runagent_allow_users = StringSetNew();
Seq *constraints = ControlBodyConstraints(policy, AGENT_TYPE_EXECUTOR);
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_executor");
DataType t;
const void *value = EvalContextVariableGet(ctx, ref, &t);
VarRefDestroy(ref);
if (t == CF_DATA_TYPE_NONE)
{
ProgrammingError("Unknown attribute '%s' in control body,"
" should have already been stopped by the parser",
cp->lval);
}
if (StringEqual(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_EXECUTORFACILITY].lval))
{
free(execd_config->log_facility);
execd_config->log_facility = xstrdup(value);
Log(LOG_LEVEL_DEBUG, "executorfacility '%s'", execd_config->log_facility);
}
else if (StringEqual(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_SPLAYTIME].lval))
{
int time = IntFromString(value);
execd_config->splay_time = (int) (time * SECONDS_PER_MINUTE * GetSplay());
}
else if (StringEqual(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_SCHEDULE].lval))
{
Log(LOG_LEVEL_DEBUG, "Loading user-defined schedule...");
StringSetClear(execd_config->schedule);
for (const Rlist *rp = value; rp; rp = rp->next)
{
StringSetAdd(execd_config->schedule, xstrdup(RlistScalarValue(rp)));
Log(LOG_LEVEL_DEBUG, "Adding '%s'", RlistScalarValue(rp));
}
}
else if (StringEqual(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_RUNAGENT_ALLOW_USERS].lval))
{
assert(t == CF_DATA_TYPE_STRING_LIST); /* should be enforced by parser */
for (const Rlist *rp = value; rp; rp = rp->next)
{
Log(LOG_LEVEL_DEBUG, "User '%s' allowed to use the runagent.socket", RlistScalarValue(rp));
StringSetAdd(execd_config->runagent_allow_users, xstrdup(RlistScalarValue(rp)));
}
}
}
}
/* We need to pull the 'cfruncommand' value from 'body control server' for
* local run-agent requests. */
constraints = ControlBodyConstraints(policy, AGENT_TYPE_SERVER);
if (constraints != NULL)
{
const size_t length = SeqLength(constraints);
for (size_t i = 0; i < length; i++)
{
Constraint *cp = SeqAt(constraints, i);
if (!IsDefinedClass(ctx, cp->classes))
{
continue;
}
if (StringEqual(cp->lval, CFS_CONTROLBODY[SERVER_CONTROL_CFRUNCOMMAND].lval))
{
assert(cp->rval.type == RVAL_TYPE_SCALAR);
execd_config->local_run_command = ExpandScalar(ctx, NULL, NULL,
RvalScalarValue(cp->rval),
NULL);
break;
}
}
}
return execd_config;
}
void ExecdConfigDestroy(ExecdConfig *execd_config)
{
if (execd_config != NULL)
{
free(execd_config->log_facility);
StringSetDestroy(execd_config->schedule);
StringSetDestroy(execd_config->runagent_allow_users);
free(execd_config->local_run_command);
}
free(execd_config);
}
|