Skip to content
44 changes: 44 additions & 0 deletions src/utils/dlck/dlck_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,59 @@

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <abt.h>

#include <daos_errno.h>
#include <daos/debug.h>
#include <daos/rpc.h>
#include <daos_srv/daos_engine.h>
#include <gurt/common.h>

#include "dlck_args.h"
#include "dlck_checker.h"
#include "dlck_cmds.h"

#define EFFECTIVE_USER_STR "Effective user: "
#define UNEXPECTED_USER_WARNING_MSG \
"WARNING: It is recommended to run this program as root or user '" DAOS_DEFAULT_SYS_NAME \
"'.\n" \
"These accounts are expected to have the necessary privileges.\n" \
"Running under other users may cause the program to stop due to insufficient " \
"privileges.\n\n"

static void
check_user(struct checker *ck)
{
uid_t euid = geteuid();
struct passwd *pw;
int ret;

/** The root user is not always named "root" but its uid is always 0. */
if (euid == 0) {
/** the root user have all the privileges */
CK_PRINT(ck, EFFECTIVE_USER_STR "root\n");
return;
}

pw = getpwuid(euid);
if (pw == NULL || pw->pw_name == NULL) {
ret = d_errno2der(errno);
CK_PRINTFL_RC(ck, ret, "Cannot get the name of a user for uid=%" PRIuMAX,
(uintmax_t)euid);
}

if (strncmp(pw->pw_name, DAOS_DEFAULT_SYS_NAME, DAOS_SYS_NAME_MAX) == 0) {
/** the daos_server user ought to have all the necessary privileges */
CK_PRINT(ck, EFFECTIVE_USER_STR DAOS_DEFAULT_SYS_NAME "\n");
return;
}

CK_PRINTF(ck, EFFECTIVE_USER_STR "%s (uid=%" PRIuMAX ")\n", pw->pw_name, (uintmax_t)euid);
CK_PRINT(ck, UNEXPECTED_USER_WARNING_MSG);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar as above merge suggestion.

}

int
main(int argc, char *argv[])
{
Expand Down Expand Up @@ -48,6 +90,8 @@ main(int argc, char *argv[])
goto err_abt_fini;
}

check_user(&ctrl.checker);

rc = dlck_cmd_check(&ctrl);
if (rc != DER_SUCCESS) {
goto err_print_main_fini;
Expand Down
Loading