1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci
16d9f0492fSopenharmony_ci#ifndef BASE_STARTUP_PARAM_MANAGER_H
17d9f0492fSopenharmony_ci#define BASE_STARTUP_PARAM_MANAGER_H
18d9f0492fSopenharmony_ci#include <pthread.h>
19d9f0492fSopenharmony_ci#include <stdio.h>
20d9f0492fSopenharmony_ci#include <string.h>
21d9f0492fSopenharmony_ci#include <grp.h>
22d9f0492fSopenharmony_ci
23d9f0492fSopenharmony_ci#include "init_param.h"
24d9f0492fSopenharmony_ci#include "list.h"
25d9f0492fSopenharmony_ci
26d9f0492fSopenharmony_ci#include "param_osadp.h"
27d9f0492fSopenharmony_ci#include "param_persist.h"
28d9f0492fSopenharmony_ci#include "param_security.h"
29d9f0492fSopenharmony_ci#include "param_trie.h"
30d9f0492fSopenharmony_ci#include "param_utils.h"
31d9f0492fSopenharmony_ci
32d9f0492fSopenharmony_ci#ifdef __cplusplus
33d9f0492fSopenharmony_ci#if __cplusplus
34d9f0492fSopenharmony_ciextern "C" {
35d9f0492fSopenharmony_ci#endif
36d9f0492fSopenharmony_ci#endif
37d9f0492fSopenharmony_ci
38d9f0492fSopenharmony_ci#define PARAM_MAX_SELINUX_LABEL 256
39d9f0492fSopenharmony_ci#ifdef PARAM_SUPPORT_SELINUX
40d9f0492fSopenharmony_ci#define PARAM_DEF_SELINUX_LABEL 64
41d9f0492fSopenharmony_ci#else
42d9f0492fSopenharmony_ci#define PARAM_DEF_SELINUX_LABEL 1
43d9f0492fSopenharmony_ci#endif
44d9f0492fSopenharmony_ci
45d9f0492fSopenharmony_ci#define WORKSPACE_INDEX_DAC 0
46d9f0492fSopenharmony_ci#define WORKSPACE_INDEX_BASE 1
47d9f0492fSopenharmony_ci#define WORKSPACE_INDEX_SIZE WORKSPACE_INDEX_DAC
48d9f0492fSopenharmony_ci
49d9f0492fSopenharmony_ci#define WORKSPACE_NAME_DAC "param_sec_dac"
50d9f0492fSopenharmony_ci#define WORKSPACE_NAME_DEF_SELINUX "u:object_r:default_param:s0"
51d9f0492fSopenharmony_ci#ifndef PARAM_SUPPORT_SELINUX
52d9f0492fSopenharmony_ci#define WORKSPACE_NAME_NORMAL "param_storage"
53d9f0492fSopenharmony_ci#else
54d9f0492fSopenharmony_ci#define WORKSPACE_NAME_NORMAL WORKSPACE_NAME_DEF_SELINUX
55d9f0492fSopenharmony_ci#endif
56d9f0492fSopenharmony_ci
57d9f0492fSopenharmony_ci#define PARAM_NEED_CHECK_IN_SERVICE 0x2
58d9f0492fSopenharmony_ci#define PARAM_CTRL_SERVICE 0x1
59d9f0492fSopenharmony_ci#ifndef OHOS_LITE
60d9f0492fSopenharmony_ci#define PERSIST_PARAM_FIXED_FLAGS "/data/service/el1/startup/persist_param_fixed"
61d9f0492fSopenharmony_ci#else
62d9f0492fSopenharmony_ci#define PERSIST_PARAM_FIXED_FLAGS "/storage/data/system/param/persist_param_fixed"
63d9f0492fSopenharmony_ci#endif
64d9f0492fSopenharmony_ci
65d9f0492fSopenharmony_ci#define PARAM_WORKSPACE_CHECK(space, exper, ...) \
66d9f0492fSopenharmony_ci    if (((*space).flags & WORKSPACE_FLAGS_INIT) != WORKSPACE_FLAGS_INIT) { \
67d9f0492fSopenharmony_ci        PARAM_LOGE(__VA_ARGS__);     \
68d9f0492fSopenharmony_ci        exper;                       \
69d9f0492fSopenharmony_ci    }
70d9f0492fSopenharmony_ci
71d9f0492fSopenharmony_citypedef struct {
72d9f0492fSopenharmony_ci    uint32_t flags;
73d9f0492fSopenharmony_ci    ParamSecurityLabel securityLabel;
74d9f0492fSopenharmony_ci    ParamSecurityOps paramSecurityOps[PARAM_SECURITY_MAX];
75d9f0492fSopenharmony_ci    PARAM_WORKSPACE_OPS ops;
76d9f0492fSopenharmony_ci#ifdef PARAM_SUPPORT_SELINUX
77d9f0492fSopenharmony_ci    SelinuxSpace selinuxSpace;
78d9f0492fSopenharmony_ci#endif
79d9f0492fSopenharmony_ci    int (*checkParamPermission)(const ParamLabelIndex *labelIndex,
80d9f0492fSopenharmony_ci        const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode);
81d9f0492fSopenharmony_ci    uint32_t maxSpaceCount;
82d9f0492fSopenharmony_ci    uint32_t maxLabelIndex;
83d9f0492fSopenharmony_ci    WorkSpace **workSpace;
84d9f0492fSopenharmony_ci} ParamWorkSpace;
85d9f0492fSopenharmony_ci
86d9f0492fSopenharmony_citypedef struct {
87d9f0492fSopenharmony_ci    ParamTaskPtr serverTask;
88d9f0492fSopenharmony_ci    ParamTaskPtr timer;
89d9f0492fSopenharmony_ci    ParamTaskPtr watcherTask;
90d9f0492fSopenharmony_ci} ParamService;
91d9f0492fSopenharmony_ci
92d9f0492fSopenharmony_citypedef struct {
93d9f0492fSopenharmony_ci    uint32_t flags;
94d9f0492fSopenharmony_ci    long long commitId;
95d9f0492fSopenharmony_ci    ParamTaskPtr saveTimer;
96d9f0492fSopenharmony_ci    struct timespec lastSaveTimer;
97d9f0492fSopenharmony_ci    PersistParamOps persistParamOps;
98d9f0492fSopenharmony_ci} ParamPersistWorkSpace;
99d9f0492fSopenharmony_ci
100d9f0492fSopenharmony_citypedef struct {
101d9f0492fSopenharmony_ci    char realKey[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 1];
102d9f0492fSopenharmony_ci    char cmdName[32];
103d9f0492fSopenharmony_ci    uint32_t valueOffset;
104d9f0492fSopenharmony_ci    uint8_t ctrlParam;
105d9f0492fSopenharmony_ci} ServiceCtrlInfo;
106d9f0492fSopenharmony_ci
107d9f0492fSopenharmony_citypedef void (*TraversalParamPtr)(ParamHandle handle, void *context);
108d9f0492fSopenharmony_citypedef struct {
109d9f0492fSopenharmony_ci    TraversalParamPtr traversalParamPtr;
110d9f0492fSopenharmony_ci    void *context;
111d9f0492fSopenharmony_ci    char *prefix;
112d9f0492fSopenharmony_ci} ParamTraversalContext;
113d9f0492fSopenharmony_ci
114d9f0492fSopenharmony_citypedef struct {
115d9f0492fSopenharmony_ci    uint8_t type;
116d9f0492fSopenharmony_ci    uint8_t mode;
117d9f0492fSopenharmony_ci    const char *name;
118d9f0492fSopenharmony_ci    const char *value;
119d9f0492fSopenharmony_ci} ParamInfos;
120d9f0492fSopenharmony_ci
121d9f0492fSopenharmony_ci#define  PARAM_HANDLE(workSpace, index) (ParamHandle)((workSpace)->spaceIndex << 24 | (index))
122d9f0492fSopenharmony_ci#define  PARAM_GET_HANDLE_INFO(handle, label, index) \
123d9f0492fSopenharmony_ci    do { \
124d9f0492fSopenharmony_ci        (label) = (((handle) >> 24) & 0x000000ff);  \
125d9f0492fSopenharmony_ci        (index) = (handle) & 0x00ffffff; \
126d9f0492fSopenharmony_ci        if (((index) & 0x03) != 0) { \
127d9f0492fSopenharmony_ci            (index) = 0; \
128d9f0492fSopenharmony_ci        } \
129d9f0492fSopenharmony_ci    } while (0)
130d9f0492fSopenharmony_ci
131d9f0492fSopenharmony_ciINIT_LOCAL_API int AddWorkSpace(const char *name, uint32_t labelIndex, int onlyRead, uint32_t spacesize);
132d9f0492fSopenharmony_ciINIT_LOCAL_API int OpenWorkSpace(uint32_t index, int readOnly);
133d9f0492fSopenharmony_ci
134d9f0492fSopenharmony_ciINIT_LOCAL_API WorkSpace *GetNextWorkSpace(WorkSpace *curr);
135d9f0492fSopenharmony_ciINIT_LOCAL_API WorkSpace *GetWorkSpace(uint32_t labelIndex);
136d9f0492fSopenharmony_ciINIT_LOCAL_API WorkSpace *GetWorkSpaceByName(const char *name);
137d9f0492fSopenharmony_ci
138d9f0492fSopenharmony_ciINIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType);
139d9f0492fSopenharmony_ciINIT_LOCAL_API int CheckParamName(const char *name, int paramInfo);
140d9f0492fSopenharmony_ciINIT_LOCAL_API uint8_t GetParamValueType(const char *name);
141d9f0492fSopenharmony_ci
142d9f0492fSopenharmony_ciINIT_LOCAL_API ParamNode *SystemCheckMatchParamWait(const char *name, const char *value);
143d9f0492fSopenharmony_ciINIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dataIndex, int onlyAdd);
144d9f0492fSopenharmony_ciINIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData);
145d9f0492fSopenharmony_ciINIT_LOCAL_API ParamSecurityLabel *GetParamSecurityLabel(void);
146d9f0492fSopenharmony_ci
147d9f0492fSopenharmony_ciINIT_LOCAL_API void LoadParamFromBuild(void);
148d9f0492fSopenharmony_ciINIT_LOCAL_API int LoadParamFromCmdLine(void);
149d9f0492fSopenharmony_ciINIT_LOCAL_API void LoadParamAreaSize(void);
150d9f0492fSopenharmony_ciINIT_LOCAL_API int InitPersistParamWorkSpace(void);
151d9f0492fSopenharmony_ciINIT_LOCAL_API void ClosePersistParamWorkSpace(void);
152d9f0492fSopenharmony_ciINIT_LOCAL_API int WritePersistParam(const char *name, const char *value);
153d9f0492fSopenharmony_ci
154d9f0492fSopenharmony_ciINIT_LOCAL_API int CheckParameterSet(const char *name, const char *value,
155d9f0492fSopenharmony_ci    const ParamSecurityLabel *srcLabel, int *ctrlService);
156d9f0492fSopenharmony_ci
157d9f0492fSopenharmony_ciINIT_LOCAL_API int CheckParamPermission(const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode);
158d9f0492fSopenharmony_ci
159d9f0492fSopenharmony_ciINIT_LOCAL_API int SysCheckParamExist(const char *name);
160d9f0492fSopenharmony_ciINIT_LOCAL_API int GenerateKeyHasCode(const char *buff, size_t len);
161d9f0492fSopenharmony_ci
162d9f0492fSopenharmony_ciINIT_INNER_API ParamWorkSpace *GetParamWorkSpace(void);
163d9f0492fSopenharmony_ciINIT_INNER_API int GetParamSecurityAuditData(const char *name, int type, ParamAuditData *auditData);
164d9f0492fSopenharmony_ciINIT_LOCAL_API int GetServiceCtrlInfo(const char *name, const char *value, ServiceCtrlInfo **ctrlInfo);
165d9f0492fSopenharmony_ci
166d9f0492fSopenharmony_ciINIT_INNER_API int InitParamWorkSpace(int onlyRead, const PARAM_WORKSPACE_OPS *ops);
167d9f0492fSopenharmony_ciINIT_LOCAL_API void CloseParamWorkSpace(void);
168d9f0492fSopenharmony_ciINIT_LOCAL_API int CheckIfUidInGroup(const gid_t groupId, const char *groupCheckName);
169d9f0492fSopenharmony_ci
170d9f0492fSopenharmony_ci#ifdef STARTUP_INIT_TEST
171d9f0492fSopenharmony_ciParamService *GetParamService();
172d9f0492fSopenharmony_ci#endif
173d9f0492fSopenharmony_ci#ifdef __cplusplus
174d9f0492fSopenharmony_ci#if __cplusplus
175d9f0492fSopenharmony_ci}
176d9f0492fSopenharmony_ci#endif
177d9f0492fSopenharmony_ci#endif
178d9f0492fSopenharmony_ci#endif