提交 d618f02c 编写于 作者: C cheng_jinsong 提交者: linrj

fixed a3cde860 from https://gitee.com/linrj2022/startup_init_lite/pulls/1590

使用统一的接口操作uid,gid
Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 1c8f3ad6
......@@ -30,8 +30,11 @@ declare_args() {
# boot_kernel_extended_cmdline for extend cmdline
startup_init_feature_decode_group_file = false
startup_init_with_param_base = false
if (!use_musl) {
startup_init_with_param_base = true
startup_init_feature_decode_group_file = true
}
}
......@@ -33,27 +33,26 @@
#define OCT_BASE 8
#define INVALID_UID(uid) ((uid) == (uid_t)-1)
static void GetUserIdByName(uid_t *uid, const char *name, uint32_t nameLen)
static void GetUserIdByName(uid_t *uid, const char *name)
{
*uid = -1;
struct passwd *data = NULL;
while ((data = getpwent()) != NULL) {
if ((data->pw_name != NULL) && (strlen(data->pw_name) == nameLen) &&
(strncmp(data->pw_name, name, nameLen) == 0)) {
*uid = data->pw_uid;
break;
}
struct passwd *data = getpwnam(name);
if (data == NULL) {
*uid = -1;
return ;
}
endpwent();
*uid = data->pw_uid;
}
static void GetGroupIdByName(gid_t *gid, const char *name, uint32_t nameLen)
static void GetGroupIdByName(gid_t *gid, const char *name)
{
*gid = -1;
struct group *data = NULL;
struct group *data = getgrnam(name);
if (data != NULL) {
*gid = data->gr_gid;
return;
}
while ((data = getgrent()) != NULL) {
if ((data->gr_name != NULL) && (strlen(data->gr_name) == nameLen) &&
(strncmp(data->gr_name, name, nameLen) == 0)) {
if ((data->gr_name != NULL) && (strcmp(data->gr_name, name) == 0)) {
*gid = data->gr_gid;
break;
}
......@@ -73,9 +72,6 @@ static int GetParamDacData(ParamDacData *dacData, const char *value)
{ "bool", PARAM_TYPE_BOOL },
};
if (dacData == NULL) {
return -1;
}
char *groupName = strstr(value, ":");
if (groupName == NULL) {
return -1;
......@@ -84,8 +80,15 @@ static int GetParamDacData(ParamDacData *dacData, const char *value)
if (mode == NULL) {
return -1;
}
GetUserIdByName(&dacData->uid, value, groupName - value);
GetGroupIdByName(&dacData->gid, groupName + 1, mode - groupName - 1);
uint32_t nameLen = groupName - value;
char *name = (char *)value;
name[nameLen] = '\0';
GetUserIdByName(&dacData->uid, name);
nameLen = mode - groupName - 1;
name = (char *)groupName + 1;
name[nameLen] = '\0';
GetGroupIdByName(&dacData->gid, name);
dacData->paramType = PARAM_TYPE_STRING;
char *type = strstr(mode + 1, ":");
......@@ -122,6 +125,8 @@ static int FreeLocalSecurityLabel(ParamSecurityLabel *srcLabel)
static int LoadOneParam_(const uint32_t *context, const char *name, const char *value)
{
ParamAuditData auditData = {0};
auditData.dacData.gid = -1;
auditData.dacData.uid = -1;
auditData.name = name;
int ret = GetParamDacData(&auditData.dacData, value);
PARAM_CHECK(ret == 0, return -1, "Failed to get param info %d %s", ret, name);
......@@ -295,7 +300,7 @@ static void AddGroupUser(const char *userName, gid_t gid)
return;
}
uid_t uid = 0;
GetUserIdByName(&uid, userName, strlen(userName));
GetUserIdByName(&uid, userName);
PARAM_LOGV("Add group user '%s' gid %d uid %d", userName, gid, uid);
if (INVALID_UID(gid) || INVALID_UID(uid)) {
PARAM_LOGW("Invalid user for '%s' gid %d uid %d", userName, gid, uid);
......@@ -357,17 +362,12 @@ static void LoadGroupUser_(void)
char *groupName = strtok(buffer, ":");
groupName = UserNameTrim(groupName);
PARAM_CHECK(groupName != NULL, continue, "Invalid group name %s", buff);
gid_t gid = -1;
GetGroupIdByName(&gid, groupName);
// skip x
(void)strtok(NULL, ":");
char *strGid = strtok(NULL, ":");
strGid = UserNameTrim(strGid);
PARAM_CHECK(strGid != NULL, continue, "Invalid gid %s", buff);
errno = 0;
gid_t gid = (gid_t)strtoul(strGid, 0, 10); // 10 base
PARAM_CHECK(errno == 0, continue, "Invalid gid %s", strGid);
char *userName = strGid + strlen(strGid) + 1;
userName = UserNameTrim(userName);
PARAM_LOGV("LoadGroupUser_ %s userName '%s'", groupName, userName);
......
......@@ -106,12 +106,12 @@ if (defined(ohos_lite)) {
cflags = [ "-fPIC" ]
include_dirs = base_include_dirs
public_configs = [ ":exported_header_files" ]
defines = [
"_GNU_SOURCE",
"PARAM_DECODE_GROUPID_FROM_FILE",
]
defines = [ "_GNU_SOURCE" ]
deps = []
if (startup_init_feature_decode_group_file) {
defines += [ "PARAM_DECODE_GROUPID_FROM_FILE" ]
}
if (use_musl) {
defines += [ "__MUSL__" ]
}
......
......@@ -39,12 +39,10 @@ __attribute__((constructor)) static void ParameterInit(void)
if (getpid() == 1) {
return;
}
EnableInitLog(INIT_INFO);
EnableInitLog(INIT_ERROR);
PARAM_WORKSPACE_OPS ops = {0};
ops.updaterMode = 0;
#ifdef PARAM_BASE_LOG
ops.logFunc = InitLog;
#endif
#ifdef PARAM_SUPPORT_SELINUX
ops.setfilecon = NULL;
#endif
......
......@@ -107,7 +107,10 @@ gid_t DecodeGid(const char *name)
return -1;
}
}
struct group *data = NULL;
struct group *data = getgrnam(name);
if (data != NULL) {
return data->gr_gid;
}
while ((data = getgrent()) != NULL) {
if ((data->gr_name != NULL) && (strcmp(data->gr_name, name) == 0)) {
gid = data->gr_gid;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册