2023-06-07 15:23:01 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-06-07 15:23:01 +08:00
|
|
|
#include "detection/users/users.h"
|
|
|
|
|
#include "modules/users/users.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-06-07 15:23:01 +08:00
|
|
|
|
2023-09-08 00:07:14 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
|
2023-06-07 15:23:01 +08:00
|
|
|
#define FF_USERS_NUM_FORMAT_ARGS 1
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintUsers(FFUsersOptions* options)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
2023-09-08 00:07:14 +08:00
|
|
|
FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFUserResult));
|
2023-06-07 15:23:01 +08:00
|
|
|
|
2023-09-08 00:07:14 +08:00
|
|
|
const char* error = ffDetectUsers(&users);
|
2023-06-07 15:23:01 +08:00
|
|
|
|
2023-09-08 00:07:14 +08:00
|
|
|
if(error)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
2023-09-08 00:07:14 +08:00
|
|
|
ffPrintError(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
2023-06-07 15:23:01 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 00:07:14 +08:00
|
|
|
if(users.length == 0)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
2023-09-08 00:07:14 +08:00
|
|
|
ffPrintError(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, "%s", "Unable to detect any users");
|
|
|
|
|
return;
|
2023-06-07 15:23:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-09-08 00:07:14 +08:00
|
|
|
if(options->compact)
|
|
|
|
|
{
|
|
|
|
|
ffPrintLogoAndKey(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
|
|
|
|
for(uint32_t i = 0; i < users.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(i > 0)
|
|
|
|
|
ffStrbufAppendS(&result, ", ");
|
|
|
|
|
FFUserResult* user = (FFUserResult*)ffListGet(&users, i);
|
|
|
|
|
ffStrbufAppend(&result, &user->name);
|
|
|
|
|
}
|
|
|
|
|
ffStrbufPutTo(&result, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for(uint32_t i = 0; i < users.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
FFUserResult* user = (FFUserResult*)ffListGet(&users, i);
|
|
|
|
|
|
|
|
|
|
ffPrintLogoAndKey(FF_USERS_MODULE_NAME, users.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateCopy(&user->name);
|
|
|
|
|
if(user->hostname.length)
|
|
|
|
|
ffStrbufAppendF(&result, "@%s", user->hostname.chars);
|
|
|
|
|
|
|
|
|
|
if(user->loginTime)
|
|
|
|
|
{
|
|
|
|
|
char buf[64];
|
|
|
|
|
time_t time = (time_t) (user->loginTime / 1000);
|
|
|
|
|
strftime(buf, sizeof(buf), "%FT%T%z", localtime(&time));
|
|
|
|
|
ffStrbufAppendF(&result, " - login time %s", buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufPutTo(&result, stdout);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 15:23:01 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-09-08 00:07:14 +08:00
|
|
|
for(uint32_t i = 0; i < users.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
FFUserResult* user = (FFUserResult*)ffListGet(&users, i);
|
|
|
|
|
|
|
|
|
|
ffPrintFormat(FF_USERS_MODULE_NAME, users.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_USERS_NUM_FORMAT_ARGS, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &user->name},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &user->hostname},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &user->tty},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &user->clientIp},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_UINT64, &user->loginTime},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFUserResult, user, users)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&user->clientIp);
|
|
|
|
|
ffStrbufDestroy(&user->hostname);
|
|
|
|
|
ffStrbufDestroy(&user->tty);
|
|
|
|
|
ffStrbufDestroy(&user->name);
|
2023-06-07 15:23:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitUsersOptions(FFUsersOptions* options)
|
|
|
|
|
{
|
2023-08-29 11:13:55 +08:00
|
|
|
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_USERS_MODULE_NAME, ffParseUsersCommandOptions, ffParseUsersJsonObject, ffPrintUsers, NULL);
|
2023-06-07 15:23:01 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
2023-09-08 00:07:14 +08:00
|
|
|
|
|
|
|
|
options->compact = true;
|
2023-06-07 15:23:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseUsersCommandOptions(FFUsersOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_USERS_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-09-08 00:07:14 +08:00
|
|
|
if(ffStrEqualsIgnCase(subKey, "compact"))
|
|
|
|
|
{
|
|
|
|
|
options->compact = ffOptionParseBoolean(value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 15:23:01 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyUsersOptions(FFUsersOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseUsersJsonObject(FFUsersOptions* options, yyjson_val* module)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
2023-06-10 15:01:54 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-06-07 15:23:01 +08:00
|
|
|
|
2023-09-08 00:07:14 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "compact"))
|
|
|
|
|
{
|
|
|
|
|
options->compact = yyjson_get_bool(val);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
ffPrintError(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
|
2023-06-07 15:23:01 +08:00
|
|
|
}
|
|
|
|
|
}
|