summaryrefslogtreecommitdiff
path: root/src/tmpfiles/offline-passwd.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-07-18 14:03:51 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-07-18 14:14:19 +0200
commit3e5d2264b584504769eda3a248b45c3367829e7a (patch)
tree66eca9cbd92a2ff51d19733c630d262c3ef7b832 /src/tmpfiles/offline-passwd.c
parent7f16ef9fba3ba16c66452ca1d96c6c68cc8ebe3e (diff)
downloadsystemd-3e5d2264b584504769eda3a248b45c3367829e7a.tar.gz
Move offline-password.[ch] to shared and add test-offline-passwd
The test binary has two modes: in the default argument-less mode, it just checks that "root" can be resolved. When invoked manually, a root prefix and user/group names can be specified.
Diffstat (limited to 'src/tmpfiles/offline-passwd.c')
-rw-r--r--src/tmpfiles/offline-passwd.c122
1 files changed, 0 insertions, 122 deletions
diff --git a/src/tmpfiles/offline-passwd.c b/src/tmpfiles/offline-passwd.c
deleted file mode 100644
index 8ac5431c66..0000000000
--- a/src/tmpfiles/offline-passwd.c
+++ /dev/null
@@ -1,122 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
-
-#include "fd-util.h"
-#include "offline-passwd.h"
-#include "path-util.h"
-#include "user-util.h"
-
-DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(uid_gid_hash_ops, char, string_hash_func, string_compare_func, free);
-
-int name_to_uid_offline(
- const char *root,
- const char *user,
- uid_t *ret_uid,
- Hashmap **cache) {
-
- void *found;
- int r;
-
- assert(user);
- assert(ret_uid);
- assert(cache);
-
- if (!*cache) {
- _cleanup_(hashmap_freep) Hashmap *uid_by_name = NULL;
- _cleanup_fclose_ FILE *f = NULL;
- struct passwd *pw;
- const char *passwd_path;
-
- passwd_path = prefix_roota(root, "/etc/passwd");
- f = fopen(passwd_path, "re");
- if (!f)
- return errno == ENOENT ? -ESRCH : -errno;
-
- uid_by_name = hashmap_new(&uid_gid_hash_ops);
- if (!uid_by_name)
- return -ENOMEM;
-
- while ((r = fgetpwent_sane(f, &pw)) > 0) {
- _cleanup_free_ char *n = NULL;
-
- n = strdup(pw->pw_name);
- if (!n)
- return -ENOMEM;
-
- r = hashmap_put(uid_by_name, n, UID_TO_PTR(pw->pw_uid));
- if (r == -EEXIST) {
- log_warning_errno(r, "Duplicate entry in %s for %s: %m", passwd_path, pw->pw_name);
- continue;
- }
- if (r < 0)
- return r;
-
- TAKE_PTR(n);
- }
-
- *cache = TAKE_PTR(uid_by_name);
- }
-
- found = hashmap_get(*cache, user);
- if (!found)
- return -ESRCH;
-
- *ret_uid = PTR_TO_UID(found);
- return 0;
-}
-
-int name_to_gid_offline(
- const char *root,
- const char *group,
- gid_t *ret_gid,
- Hashmap **cache) {
-
- void *found;
- int r;
-
- assert(group);
- assert(ret_gid);
- assert(cache);
-
- if (!*cache) {
- _cleanup_(hashmap_freep) Hashmap *gid_by_name = NULL;
- _cleanup_fclose_ FILE *f = NULL;
- struct group *gr;
- const char *group_path;
-
- group_path = prefix_roota(root, "/etc/group");
- f = fopen(group_path, "re");
- if (!f)
- return errno == ENOENT ? -ESRCH : -errno;
-
- gid_by_name = hashmap_new(&uid_gid_hash_ops);
- if (!gid_by_name)
- return -ENOMEM;
-
- while ((r = fgetgrent_sane(f, &gr)) > 0) {
- _cleanup_free_ char *n = NULL;
-
- n = strdup(gr->gr_name);
- if (!n)
- return -ENOMEM;
-
- r = hashmap_put(gid_by_name, n, GID_TO_PTR(gr->gr_gid));
- if (r == -EEXIST) {
- log_warning_errno(r, "Duplicate entry in %s for %s: %m", group_path, gr->gr_name);
- continue;
- }
- if (r < 0)
- return r;
-
- TAKE_PTR(n);
- }
-
- *cache = TAKE_PTR(gid_by_name);
- }
-
- found = hashmap_get(*cache, group);
- if (!found)
- return -ESRCH;
-
- *ret_gid = PTR_TO_GID(found);
- return 0;
-}