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