|
1 | 1 | /** |
2 | | - * (C) Copyright 2025 Hewlett Packard Enterprise Development LP |
| 2 | + * (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: BSD-2-Clause-Patent |
5 | 5 | */ |
|
8 | 8 | #include <stdlib.h> |
9 | 9 | #include <stdio.h> |
10 | 10 | #include <sys/types.h> |
11 | | -#include <pwd.h> |
| 11 | +#include <grp.h> |
12 | 12 | #include <abt.h> |
13 | 13 |
|
14 | 14 | #include <daos_errno.h> |
|
22 | 22 | #include "dlck_cmds.h" |
23 | 23 |
|
24 | 24 | #define EFFECTIVE_USER_STR "Effective user: " |
| 25 | +#define USER_BELONGS_TO_GRP_FMT "User %sbelong%s to group: %s (gid=%" PRIuMAX ")\n" |
25 | 26 | #define UNEXPECTED_USER_WARNING_MSG \ |
26 | | - "WARNING: It is recommended to run this program as root or user '" DAOS_DEFAULT_SYS_NAME \ |
27 | | - "'.\n" \ |
28 | | - "These accounts are expected to have the necessary privileges.\n" \ |
29 | | - "Running under other users may cause the program to stop due to insufficient " \ |
| 27 | + "\nWARNING: It is recommended to run this program as root or as a user who belongs to " \ |
| 28 | + "the '" DAOS_DEFAULT_SYS_NAME "' group.\n" \ |
| 29 | + "Running it under any other account may cause the program to stop due to insufficient " \ |
30 | 30 | "privileges.\n\n" |
31 | 31 |
|
32 | | -static void |
33 | | -check_user(struct checker *ck) |
| 32 | +static bool |
| 33 | +user_is_root(struct checker *ck) |
34 | 34 | { |
35 | | - uid_t euid = geteuid(); |
36 | | - struct passwd *pw = NULL; |
37 | | - int ret; |
| 35 | + uid_t euid = geteuid(); |
| 36 | + |
| 37 | + if (DAOS_FAIL_CHECK(DLCK_MOCK_ROOT)) { /** fault injection */ |
| 38 | + /** it does not have ANY effect on the actual privileges of the user */ |
| 39 | + euid = 0; |
| 40 | + } |
38 | 41 |
|
39 | | - /** The root user is not always named "root" but its uid is always 0. */ |
40 | 42 | if (euid == 0) { |
41 | | - /** the root user have all the privileges */ |
| 43 | + /** The root user is not always named "root" but its uid is always 0. */ |
42 | 44 | CK_PRINT(ck, EFFECTIVE_USER_STR "root\n"); |
43 | | - return; |
| 45 | + return true; |
44 | 46 | } |
45 | 47 |
|
46 | | - if (DAOS_FAIL_CHECK(DLCK_FAULT_GETPWUID)) { /** fault injection */ |
| 48 | + CK_PRINTF(ck, EFFECTIVE_USER_STR "uid=%" PRIuMAX "\n", (uintmax_t)euid); |
| 49 | + return false; |
| 50 | +} |
| 51 | + |
| 52 | +#define MAX_GROUPS 128 |
| 53 | + |
| 54 | +static bool |
| 55 | +user_belongs_to_group(const char *group_name, struct checker *ck) |
| 56 | +{ |
| 57 | + struct group *group = NULL; |
| 58 | + gid_t group_id; |
| 59 | + gid_t groups[MAX_GROUPS]; |
| 60 | + int rc; |
| 61 | + |
| 62 | + /** get GID of the requested group */ |
| 63 | + if (DAOS_FAIL_CHECK(DLCK_FAULT_GETGRNAM)) { /** fault injection */ |
47 | 64 | errno = daos_fail_value_get(); |
| 65 | + } else if (DAOS_FAIL_CHECK(DLCK_MOCK_NO_DAOS_SERVER_GROUP)) { /** fault injection */ |
| 66 | + errno = 0; |
48 | 67 | } else { |
49 | | - pw = getpwuid(euid); |
| 68 | + errno = 0; |
| 69 | + group = getgrnam(group_name); |
| 70 | + } |
| 71 | + if (group == NULL) { |
| 72 | + if (errno != 0) { |
| 73 | + rc = daos_errno2der(errno); |
| 74 | + CK_PRINTFL_RC(ck, rc, "getgrnam(%s) failed", group_name); |
| 75 | + } else { |
| 76 | + CK_PRINTF(ck, "The %s group does not exist.\n", group_name); |
| 77 | + } |
| 78 | + return false; |
50 | 79 | } |
51 | | - if (pw == NULL || pw->pw_name == NULL) { |
52 | | - ret = d_errno2der(errno); |
53 | | - CK_PRINTFL_RC(ck, ret, "Cannot get the name of a user for uid=%" PRIuMAX, |
54 | | - (uintmax_t)euid); |
55 | | - CK_PRINT(ck, UNEXPECTED_USER_WARNING_MSG); |
| 80 | + group_id = group->gr_gid; |
| 81 | + |
| 82 | + /** check primary group */ |
| 83 | + if (getgid() == group_id) { |
| 84 | + CK_PRINTF(ck, USER_BELONGS_TO_GRP_FMT, "", "s", group_name, (uintmax_t)group_id); |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + /** get supplementary groups */ |
| 89 | + if (DAOS_FAIL_CHECK(DLCK_FAULT_GETGROUPS)) { /** fault injection */ |
| 90 | + rc = -1; |
| 91 | + errno = daos_fail_value_get(); |
| 92 | + } else { |
| 93 | + rc = getgroups(MAX_GROUPS, groups); |
| 94 | + } |
| 95 | + if (rc < 0) { |
| 96 | + rc = daos_errno2der(errno); |
| 97 | + CK_PRINTFL_RC(ck, rc, "getgroups() failed", group_name); |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + /** check supplementary groups */ |
| 102 | + if (!DAOS_FAIL_CHECK(DLCK_MOCK_NOT_IN_DAOS_SERVER_GROUP)) { /** fault injection */ |
| 103 | + for (int i = 0; i < rc; i++) { |
| 104 | + if (groups[i] == group_id) { |
| 105 | + CK_PRINTF(ck, USER_BELONGS_TO_GRP_FMT, "", "s", group_name, |
| 106 | + (uintmax_t)group_id); |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + CK_PRINTF(ck, USER_BELONGS_TO_GRP_FMT, "DOES NOT ", "", group_name, (uintmax_t)group_id); |
| 113 | + |
| 114 | + return false; |
| 115 | +} |
| 116 | + |
| 117 | +static void |
| 118 | +check_user_privileges(struct checker *ck) |
| 119 | +{ |
| 120 | + if (user_is_root(ck)) { |
| 121 | + /** the root user is assumed to have all required privileges */ |
56 | 122 | return; |
57 | 123 | } |
58 | 124 |
|
59 | | - if (strncmp(pw->pw_name, DAOS_DEFAULT_SYS_NAME, DAOS_SYS_NAME_MAX) == 0) { |
60 | | - /** the daos_server user ought to have all the necessary privileges */ |
61 | | - CK_PRINT(ck, EFFECTIVE_USER_STR DAOS_DEFAULT_SYS_NAME "\n"); |
| 125 | + if (user_belongs_to_group(DAOS_DEFAULT_SYS_NAME, ck)) { |
62 | 126 | return; |
63 | 127 | } |
64 | 128 |
|
65 | | - CK_PRINTF(ck, EFFECTIVE_USER_STR "%s (uid=%" PRIuMAX ")\n", pw->pw_name, (uintmax_t)euid); |
66 | 129 | CK_PRINT(ck, UNEXPECTED_USER_WARNING_MSG); |
67 | 130 | } |
68 | 131 |
|
@@ -103,7 +166,7 @@ main(int argc, char *argv[]) |
103 | 166 | goto err_abt_fini; |
104 | 167 | } |
105 | 168 |
|
106 | | - check_user(&ctrl.checker); |
| 169 | + check_user_privileges(&ctrl.checker); |
107 | 170 |
|
108 | 171 | rc = dlck_cmd_check(&ctrl); |
109 | 172 | if (rc != DER_SUCCESS) { |
|
0 commit comments