From ab0b3090b08654409ea371788bab7131aded5db5 Mon Sep 17 00:00:00 2001 From: Mupceet Date: Wed, 22 Jun 2022 16:16:11 +0800 Subject: [PATCH] decodeuidgid0622 Signed-off-by: Mupceet --- services/include/init_utils.h | 1 + services/utils/init_utils.c | 78 ++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/services/include/init_utils.h b/services/include/init_utils.h index ba29ecef..97865013 100644 --- a/services/include/init_utils.h +++ b/services/include/init_utils.h @@ -60,6 +60,7 @@ void WaitForFile(const char *source, unsigned int maxSecond); size_t WriteAll(int fd, const char *buffer, size_t size); char *GetRealPath(const char *source); int StringToInt(const char *str, int defaultValue); +int StringToUint(const char *name, unsigned int *value); int MakeDirRecursive(const char *dir, mode_t mode); void CheckAndCreateDir(const char *fileName); int CheckAndCreatFile(const char *file, mode_t mode); diff --git a/services/utils/init_utils.c b/services/utils/init_utils.c index 48508681..67c88844 100644 --- a/services/utils/init_utils.c +++ b/services/utils/init_utils.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -48,43 +49,74 @@ float ConvertMicrosecondToSecond(int x) return ((x / THOUSAND_UNIT_INT) / THOUSAND_UNIT_FLOAT); } -static uid_t DecodeId(const char *name, bool isUid) +static bool CheckDigit(const char *name) { -#ifndef __LITEOS_M__ - INIT_CHECK_RETURN_VALUE(name != NULL, -1); - int digitFlag = 1; size_t nameLen = strlen(name); - for (unsigned int i = 0; i < nameLen; ++i) { - if (isalpha(name[i])) { - digitFlag = 0; - break; + for (size_t i = 0; i < nameLen; ++i) { + if (!isdigit(name[i])) { + return false; } } - if (digitFlag) { - errno = 0; - uid_t result = strtoul(name, 0, DECIMAL_BASE); - INIT_CHECK_RETURN_VALUE(errno == 0, -1); - return result; - } else { - struct passwd *userInf = getpwnam(name); - if (userInf == NULL) { + return true; +} + +int StringToUint(const char *name, unsigned int *value) +{ + errno = 0; + *value = (unsigned int)strtoul(name, 0, DECIMAL_BASE); + INIT_CHECK_RETURN_VALUE(errno == 0, -1); + return 0; +} + +uid_t DecodeUid(const char *name) +{ +#ifndef __LITEOS_M__ + INIT_CHECK_RETURN_VALUE(name != NULL, -1); + uid_t uid = -1; + if (CheckDigit(name)) { + if (!StringToUint(name, &uid)) { + return uid; + } else { + INIT_LOGE("Failed to decode uid"); return -1; } - return isUid ? userInf->pw_uid : userInf->pw_gid; } + struct passwd *p = getpwnam(name); + if (p == NULL) { + INIT_LOGE("Failed to decode uid"); + return -1; + } + return p->pw_uid; #else return -1; #endif } -uid_t DecodeUid(const char *name) -{ - return DecodeId(name, true); -} - gid_t DecodeGid(const char *name) { - return DecodeId(name, false); +#ifndef __LITEOS_M__ + INIT_CHECK_RETURN_VALUE(name != NULL, -1); + gid_t gid = -1; + if (CheckDigit(name)) { + if (!StringToUint(name, &gid)) { + return gid; + } else { + INIT_LOGE("Failed to decode gid"); + return -1; + } + } + struct group *data = NULL; + while ((data = getgrent()) != NULL) { + if ((data->gr_name != NULL) && (strcmp(data->gr_name, name) == 0)) { + gid = data->gr_gid; + break; + } + } + endgrent(); + return gid; +#else + return -1; +#endif } char *ReadFileToBuf(const char *configFile) -- GitLab