• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
60 #define PARAM_WORKSPACE_CHECK(space, exper, ...) \
61     if (((*space).flags & WORKSPACE_FLAGS_INIT) != WORKSPACE_FLAGS_INIT) { \
62         PARAM_LOGE(__VA_ARGS__);     \
63         exper;                       \
64     }
65 
66 typedef struct {
67     uint32_t flags;
68     ParamSecurityLabel securityLabel;
69     ParamSecurityOps paramSecurityOps[PARAM_SECURITY_MAX];
70     PARAM_WORKSPACE_OPS ops;
71 #ifdef PARAM_SUPPORT_SELINUX
72     SelinuxSpace selinuxSpace;
73 #endif
74     int (*checkParamPermission)(const ParamLabelIndex *labelIndex,
75         const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode);
76     uint32_t maxSpaceCount;
77     uint32_t maxLabelIndex;
78     WorkSpace **workSpace;
79 } ParamWorkSpace;
80 
81 typedef struct {
82     ParamTaskPtr serverTask;
83     ParamTaskPtr timer;
84     ParamTaskPtr watcherTask;
85 } ParamService;
86 
87 typedef struct {
88     uint32_t flags;
89     long long commitId;
90     ParamTaskPtr saveTimer;
91     struct timespec lastSaveTimer;
92     PersistParamOps persistParamOps;
93 } ParamPersistWorkSpace;
94 
95 typedef struct {
96     char realKey[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 1];
97     char cmdName[32];
98     uint32_t valueOffset;
99     uint8_t ctrlParam;
100 } ServiceCtrlInfo;
101 
102 typedef void (*TraversalParamPtr)(ParamHandle handle, void *context);
103 typedef struct {
104     TraversalParamPtr traversalParamPtr;
105     void *context;
106     char *prefix;
107 } ParamTraversalContext;
108 
109 #define  PARAM_HANDLE(workSpace, index) (ParamHandle)((workSpace)->spaceIndex << 24 | (index))
110 #define  PARAM_GET_HANDLE_INFO(handle, label, index) \
111     do { \
112         (label) = (((handle) >> 24) & 0x000000ff);  \
113         (index) = (handle) & 0x00ffffff; \
114         if (((index) & 0x03) != 0) { \
115             (index) = 0; \
116         } \
117     } while (0)
118 
119 INIT_LOCAL_API int AddWorkSpace(const char *name, uint32_t labelIndex, int onlyRead, uint32_t spacesize);
120 INIT_LOCAL_API int OpenWorkSpace(uint32_t index, int readOnly);
121 
122 INIT_LOCAL_API WorkSpace *GetNextWorkSpace(WorkSpace *curr);
123 INIT_LOCAL_API WorkSpace *GetWorkSpace(uint32_t labelIndex);
124 INIT_LOCAL_API WorkSpace *GetWorkSpaceByName(const char *name);
125 
126 INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType);
127 INIT_LOCAL_API int CheckParamName(const char *name, int paramInfo);
128 INIT_LOCAL_API uint8_t GetParamValueType(const char *name);
129 
130 INIT_LOCAL_API ParamNode *SystemCheckMatchParamWait(const char *name, const char *value);
131 INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dataIndex, int onlyAdd);
132 INIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData);
133 INIT_LOCAL_API ParamSecurityLabel *GetParamSecurityLabel(void);
134 
135 INIT_LOCAL_API void LoadParamFromBuild(void);
136 INIT_LOCAL_API int LoadParamFromCmdLine(void);
137 INIT_LOCAL_API void LoadParamAreaSize(void);
138 INIT_LOCAL_API int InitPersistParamWorkSpace(void);
139 INIT_LOCAL_API void ClosePersistParamWorkSpace(void);
140 INIT_LOCAL_API int WritePersistParam(const char *name, const char *value);
141 
142 INIT_LOCAL_API int CheckParameterSet(const char *name, const char *value,
143     const ParamSecurityLabel *srcLabel, int *ctrlService);
144 
145 INIT_LOCAL_API int CheckParamPermission(const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode);
146 
147 INIT_LOCAL_API int SysCheckParamExist(const char *name);
148 INIT_LOCAL_API int GenerateKeyHasCode(const char *buff, size_t len);
149 
150 INIT_INNER_API ParamWorkSpace *GetParamWorkSpace(void);
151 INIT_INNER_API int GetParamSecurityAuditData(const char *name, int type, ParamAuditData *auditData);
152 INIT_LOCAL_API int GetServiceCtrlInfo(const char *name, const char *value, ServiceCtrlInfo **ctrlInfo);
153 
154 INIT_INNER_API int InitParamWorkSpace(int onlyRead, const PARAM_WORKSPACE_OPS *ops);
155 INIT_LOCAL_API void CloseParamWorkSpace(void);
156 INIT_LOCAL_API int CheckIfUidInGroup(const gid_t groupId, const char *groupCheckName);
157 
158 #ifdef STARTUP_INIT_TEST
159 ParamService *GetParamService();
160 #endif
161 #ifdef __cplusplus
162 #if __cplusplus
163 }
164 #endif
165 #endif
166 #endif