From f3c59c897808985578e692bb0a162a46c560f516 Mon Sep 17 00:00:00 2001 From: obarun <eric@obarun.org> Date: Sat, 15 Oct 2022 20:20:59 +1100 Subject: [PATCH] add name_isvalid(), set_ownersysdir_stack(), set_ownerhome_stack() functions --- src/lib66/utils/name_isvalid.c | 34 ++++++++++++++++++++++++++++++ src/lib66/utils/set_ownerhome.c | 28 +++++++++++++++++++++++++ src/lib66/utils/set_ownersysdir.c | 35 +++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 src/lib66/utils/name_isvalid.c diff --git a/src/lib66/utils/name_isvalid.c b/src/lib66/utils/name_isvalid.c new file mode 100644 index 00000000..457ad0c5 --- /dev/null +++ b/src/lib66/utils/name_isvalid.c @@ -0,0 +1,34 @@ +/* + * name_isvalid.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <string.h> + +#include <oblibs/log.h> + +#include <66/constants.h> +#include <66/utils.h> + +void name_isvalid(char const *name) +{ + log_flow() ; + + if (!memcmp(name, SS_MASTER + 1, 6)) + log_die(LOG_EXIT_USER, "service name: ", name, ": starts with reserved prefix Master") ; + + if (!strcmp(name, SS_SERVICE)) + log_die(LOG_EXIT_USER, "service as service name is a reserved name") ; + + if (!strcmp(name, SS_SERVICE "@")) + log_die(LOG_EXIT_USER, "service@ as service name is a reserved name") ; +} diff --git a/src/lib66/utils/set_ownerhome.c b/src/lib66/utils/set_ownerhome.c index 9d948cfd..a69b0cbd 100644 --- a/src/lib66/utils/set_ownerhome.c +++ b/src/lib66/utils/set_ownerhome.c @@ -22,6 +22,7 @@ #include <skalibs/stralloc.h> #include <66/utils.h> +#include <66/constants.h> int set_ownerhome(stralloc *base,uid_t owner) { @@ -45,3 +46,30 @@ int set_ownerhome(stralloc *base,uid_t owner) return 1 ; } + +int set_ownerhome_stack(char *store) +{ + log_flow() ; + + char const *user_home = 0 ; + int e = errno ; + struct passwd *st = getpwuid(getuid()) ; + errno = 0 ; + if (!st) { + if (!errno) errno = ESRCH ; + return 0 ; + } + user_home = st->pw_dir ; + errno = e ; + if (!user_home) + return 0 ; + + if (strlen(user_home) + 1 > SS_MAX_PATH_LEN) { + errno = ENAMETOOLONG ; + return 0 ; + } + + auto_strings(store, user_home, "/") ; + + return 1 ; +} diff --git a/src/lib66/utils/set_ownersysdir.c b/src/lib66/utils/set_ownersysdir.c index ceb0eb6c..494254db 100644 --- a/src/lib66/utils/set_ownersysdir.c +++ b/src/lib66/utils/set_ownersysdir.c @@ -52,3 +52,38 @@ int set_ownersysdir(stralloc *base, uid_t owner) return 1 ; } + +int set_ownersysdir_stack(char *base, uid_t owner) +{ + log_flow() ; + + char const *user_home = NULL ; + int e = errno ; + struct passwd *st = getpwuid(owner) ; + errno = 0 ; + + if (!st) { + + if (!errno) errno = ESRCH ; + return 0 ; + } + + user_home = st->pw_dir ; + + errno = e ; + + if (user_home == NULL) + return 0 ; + + if (strlen(user_home) + 1 + strlen(SS_USER_DIR) > SS_MAX_PATH_LEN) { + errno = ENAMETOOLONG ; + return 0 ; + } + + if(owner > 0) + auto_strings(base, user_home, "/", SS_USER_DIR) ; + else + auto_strings(base, SS_SYSTEM_DIR) ; + + return 1 ; +} -- GitLab