Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions libenv/sysinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3530,6 +3530,43 @@ JsonElement* GetUserInfo(const void *passwd)

/*****************************************************************************/

JsonElement* GetGroupInfo(const void *group)
{
#ifdef __MINGW32__
return NULL;

#else /* !__MINGW32__ */

const struct group *gr = (struct group*) group;

if (gr == NULL)
{
gr = getgrgid(getgid());
}

if (gr == NULL)
{
return NULL;
}

JsonElement *result = JsonObjectCreate(3);
JsonObjectAppendString(result, "name", gr->gr_name);
JsonObjectAppendInteger(result, "gid", gr->gr_gid);

JsonElement *mem = JsonArrayCreate(10);
for (int i = 0; gr->gr_mem[i] != NULL; i++)
{
JsonArrayAppendString(mem, gr->gr_mem[i]);
}

JsonObjectAppendArray(result, "members", mem);

return result;
#endif /* !__MINGW32__ */
}

/*****************************************************************************/

void GetSysVars(EvalContext *ctx)
{
/* Get info for current user. */
Expand Down
1 change: 1 addition & 0 deletions libenv/sysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ void GetNetworkingInfo(EvalContext *ctx);
JsonElement* GetNetworkingConnections(EvalContext *ctx);

JsonElement* GetUserInfo(const void *passwd);
JsonElement* GetGroupInfo(const void *group);

#endif
60 changes: 60 additions & 0 deletions libpromises/evalfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,57 @@ static FnCallResult FnCallGetUserInfo(ARG_UNUSED EvalContext *ctx, ARG_UNUSED co

/*********************************************************************/


static FnCallResult FnCallGetGroupInfo(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Policy *policy, ARG_UNUSED const FnCall *fp, const Rlist *finalargs)
{
#ifdef __MINGW32__
return FnFailure();

#else /* !__MINGW32__ */

struct group *gr = NULL;

if (finalargs == NULL)
{
gr = getgrgid(getgid());
}
else
{
char *arg = RlistScalarValue(finalargs);
if (StringIsNumeric(arg))
{
gid_t gid = Str2Gid(arg, NULL, NULL);
if (gid == CF_SAME_GROUP) // user "*"
{
gid = getgid();
}
else if (gid == CF_UNKNOWN_GROUP)
{
Log(LOG_LEVEL_ERR, "The specified group '%s' doesn't exist", arg);
return FnFailure();
Comment thread
victormlg marked this conversation as resolved.
}

gr = getgrgid(gid);
}
else
{
gr = getgrnam(arg);
}
}

JsonElement *result = GetGroupInfo(gr);

if (result == NULL)
{
return FnFailure();
}

return FnReturnContainerNoCopy(result);
#endif
}

/*********************************************************************/

static FnCallResult FnCallGetUid(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Policy *policy, ARG_UNUSED const FnCall *fp, const Rlist *finalargs)
{
#ifdef __MINGW32__
Expand Down Expand Up @@ -10426,6 +10477,13 @@ static const FnCallArg GETUSERINFO_ARGS[] =
{NULL, CF_DATA_TYPE_NONE, NULL}
};

static const FnCallArg GETGROUPINFO_ARGS[] =
{
{CF_ANYSTRING, CF_DATA_TYPE_STRING, "Group name or group ID as string"},
{NULL, CF_DATA_TYPE_NONE, NULL}
};


static const FnCallArg GREP_ARGS[] =
{
{CF_ANYSTRING, CF_DATA_TYPE_STRING, "Regular expression"},
Expand Down Expand Up @@ -11367,6 +11425,8 @@ const FnCallType CF_FNCALL_TYPES[] =
FNCALL_OPTION_NONE, FNCALL_CATEGORY_SYSTEM, SYNTAX_STATUS_NORMAL),
FnCallTypeNew("getuserinfo", CF_DATA_TYPE_CONTAINER, GETUSERINFO_ARGS, &FnCallGetUserInfo, "Get a data container describing user arg1, defaulting to current user",
FNCALL_OPTION_VARARG, FNCALL_CATEGORY_SYSTEM, SYNTAX_STATUS_NORMAL),
FnCallTypeNew("getgroupinfo", CF_DATA_TYPE_CONTAINER, GETGROUPINFO_ARGS, &FnCallGetGroupInfo, "Get a data container describing specified group, or current group if not specified",
FNCALL_OPTION_VARARG, FNCALL_CATEGORY_SYSTEM, SYNTAX_STATUS_NORMAL),
FnCallTypeNew("getvalues", CF_DATA_TYPE_STRING_LIST, GETINDICES_ARGS, &FnCallGetValues, "Get a list of values in the list or array or data container arg1",
FNCALL_OPTION_COLLECTING, FNCALL_CATEGORY_DATA, SYNTAX_STATUS_NORMAL),
FnCallTypeNew("getvariablemetatags", CF_DATA_TYPE_STRING_LIST, GETVARIABLEMETATAGS_ARGS, &FnCallGetMetaTags, "Collect the variable arg1's meta tags into an slist, optionally collecting only tag key arg2",
Expand Down
31 changes: 31 additions & 0 deletions tests/acceptance/01_vars/02_functions/getgroupinfo.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#######################################################
#
# Test 'getgroupinfo' function
#
#######################################################

body common control
{
inputs => { "../../default.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
version => "1.0";
}

bundle agent test
{
meta:
"test_soft_fail" string => "windows",
meta => { "ENT-10217" };
vars:
# this is pretty much all we can test across platforms
"info_root" string => nth(getgroupinfo("root"), "name");
Comment thread
victormlg marked this conversation as resolved.
"info_0" string => nth(getgroupinfo(0), "gid");
}

bundle agent check
{
methods:
"check" usebundle => dcs_check_state(test,
"$(this.promise_filename).expected.json",
"$(this.promise_filename)");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"info_0": "0",
"info_root": "root"
}
Loading