From 8795d9bacdea7074b023c9f8836720002e39adc8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 19 Aug 2022 17:03:36 +0200 Subject: [PATCH] basic/user-util: rename output param to ret, shorten code --- src/basic/user-util.c | 78 +++++++++++++++---------------------------- src/basic/user-util.h | 2 +- 2 files changed, 27 insertions(+), 53 deletions(-) diff --git a/src/basic/user-util.c b/src/basic/user-util.c index e705f703ead..80f9ff144b1 100644 --- a/src/basic/user-util.c +++ b/src/basic/user-util.c @@ -569,43 +569,29 @@ int getgroups_alloc(gid_t** gids) { return ngroups; } -int get_home_dir(char **_h) { +int get_home_dir(char **ret) { struct passwd *p; const char *e; char *h; uid_t u; - assert(_h); + assert(ret); /* Take the user specified one */ e = secure_getenv("HOME"); - if (e && path_is_valid(e) && path_is_absolute(e)) { - h = strdup(e); - if (!h) - return -ENOMEM; - - *_h = path_simplify(h); - return 0; - } + if (e && path_is_valid(e) && path_is_absolute(e)) + goto found; /* Hardcode home directory for root and nobody to avoid NSS */ u = getuid(); if (u == 0) { - h = strdup("/root"); - if (!h) - return -ENOMEM; - - *_h = h; - return 0; + e = "/root"; + goto found; } if (u == UID_NOBODY && synthesize_nobody()) { - h = strdup("/"); - if (!h) - return -ENOMEM; - - *_h = h; - return 0; + e = "/"; + goto found; } /* Check the database... */ @@ -613,55 +599,42 @@ int get_home_dir(char **_h) { p = getpwuid(u); if (!p) return errno_or_else(ESRCH); + e = p->pw_dir; - if (!path_is_valid(p->pw_dir) || - !path_is_absolute(p->pw_dir)) + if (!path_is_valid(e) || !path_is_absolute(e)) return -EINVAL; - h = strdup(p->pw_dir); + found: + h = strdup(e); if (!h) return -ENOMEM; - *_h = path_simplify(h); + *ret = path_simplify(h); return 0; } -int get_shell(char **_s) { +int get_shell(char **ret) { struct passwd *p; const char *e; char *s; uid_t u; - assert(_s); + assert(ret); /* Take the user specified one */ e = secure_getenv("SHELL"); - if (e && path_is_valid(e) && path_is_absolute(e)) { - s = strdup(e); - if (!s) - return -ENOMEM; - - *_s = path_simplify(s); - return 0; - } + if (e && path_is_valid(e) && path_is_absolute(e)) + goto found; /* Hardcode shell for root and nobody to avoid NSS */ u = getuid(); if (u == 0) { - s = strdup(default_root_shell(NULL)); - if (!s) - return -ENOMEM; - - *_s = s; - return 0; + e = default_root_shell(NULL); + goto found; } if (u == UID_NOBODY && synthesize_nobody()) { - s = strdup(NOLOGIN); - if (!s) - return -ENOMEM; - - *_s = s; - return 0; + e = NOLOGIN; + goto found; } /* Check the database... */ @@ -669,16 +642,17 @@ int get_shell(char **_s) { p = getpwuid(u); if (!p) return errno_or_else(ESRCH); + e = p->pw_shell; - if (!path_is_valid(p->pw_shell) || - !path_is_absolute(p->pw_shell)) + if (!path_is_valid(e) || !path_is_absolute(e)) return -EINVAL; - s = strdup(p->pw_shell); + found: + s = strdup(e); if (!s) return -ENOMEM; - *_s = path_simplify(s); + *ret = path_simplify(s); return 0; } diff --git a/src/basic/user-util.h b/src/basic/user-util.h index c8f0758541f..614dec2fde8 100644 --- a/src/basic/user-util.h +++ b/src/basic/user-util.h @@ -55,7 +55,7 @@ int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t int getgroups_alloc(gid_t** gids); int get_home_dir(char **ret); -int get_shell(char **_ret); +int get_shell(char **ret); int reset_uid_gid(void); -- 2.47.3