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 22 #include "init_param.h" 23 #include "list.h" 24 25 #include "param_osadp.h" 26 #include "param_persist.h" 27 #include "param_security.h" 28 #include "param_trie.h" 29 #include "param_utils.h" 30 31 #ifdef __cplusplus 32 #if __cplusplus 33 extern "C" { 34 #endif 35 #endif 36 37 #define PARAM_MAX_SELINUX_LABEL 256 38 #ifdef PARAM_SUPPORT_SELINUX 39 #define PARAM_DEF_SELINUX_LABEL 64 40 #else 41 #define PARAM_DEF_SELINUX_LABEL 1 42 #endif 43 44 #define WORKSPACE_INDEX_DAC 0 45 #define WORKSPACE_INDEX_BASE 1 46 47 #define WORKSPACE_NAME_DAC "param_sec_dac" 48 #define WORKSPACE_NAME_DEF_SELINUX "u:object_r:default_param:s0" 49 #ifndef PARAM_SUPPORT_SELINUX 50 #define WORKSPACE_NAME_NORMAL "param_storage" 51 #else 52 #define WORKSPACE_NAME_NORMAL WORKSPACE_NAME_DEF_SELINUX 53 #endif 54 55 #define PARAM_NEED_CHECK_IN_SERVICE 0x2 56 #define PARAM_CTRL_SERVICE 0x1 57 58 #define PARAM_WORKSPACE_CHECK(space, exper, ...) \ 59 if (((*space).flags & WORKSPACE_FLAGS_INIT) != WORKSPACE_FLAGS_INIT) { \ 60 PARAM_LOGE(__VA_ARGS__); \ 61 exper; \ 62 } 63 64 typedef struct { 65 uint32_t flags; 66 ParamSecurityLabel securityLabel; 67 ParamSecurityOps paramSecurityOps[PARAM_SECURITY_MAX]; 68 PARAM_WORKSPACE_OPS ops; 69 #ifdef PARAM_SUPPORT_SELINUX 70 SelinuxSpace selinuxSpace; 71 #endif 72 int (*checkParamPermission)(const ParamLabelIndex *labelIndex, 73 const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode); 74 uint32_t maxLabelIndex; 75 WorkSpace **workSpace; 76 } ParamWorkSpace; 77 78 typedef struct { 79 ParamTaskPtr serverTask; 80 ParamTaskPtr timer; 81 ParamTaskPtr watcherTask; 82 } ParamService; 83 84 typedef struct { 85 uint32_t flags; 86 long long commitId; 87 ParamTaskPtr saveTimer; 88 struct timespec lastSaveTimer; 89 PersistParamOps persistParamOps; 90 } ParamPersistWorkSpace; 91 92 typedef struct { 93 char realKey[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 1]; 94 char cmdName[32]; 95 uint32_t valueOffset; 96 uint8_t ctrlParam; 97 } ServiceCtrlInfo; 98 99 typedef void (*TraversalParamPtr)(ParamHandle handle, void *context); 100 typedef struct { 101 TraversalParamPtr traversalParamPtr; 102 void *context; 103 char *prefix; 104 } ParamTraversalContext; 105 106 #define PARAM_HANDLE(workSpace, index) (ParamHandle)((workSpace)->spaceIndex << 24 | (index)) 107 #define PARAM_GET_HANDLE_INFO(handle, label, index) \ 108 do { \ 109 (label) = (((handle) >> 24) & 0x000000ff); \ 110 (index) = (handle) & 0x00ffffff; \ 111 if (((index) & 0x03) != 0) { \ 112 (index) = 0; \ 113 } \ 114 } while (0) 115 116 INIT_LOCAL_API int AddWorkSpace(const char *name, uint32_t labelIndex, int onlyRead, uint32_t spacesize); 117 INIT_LOCAL_API int OpenWorkSpace(uint32_t index, int readOnly); 118 119 INIT_LOCAL_API WorkSpace *GetNextWorkSpace(WorkSpace *curr); 120 INIT_LOCAL_API WorkSpace *GetWorkSpace(uint32_t labelIndex); 121 INIT_LOCAL_API WorkSpace *GetWorkSpaceByName(const char *name); 122 123 INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType); 124 INIT_LOCAL_API int CheckParamName(const char *name, int paramInfo); 125 INIT_LOCAL_API uint8_t GetParamValueType(const char *name); 126 127 INIT_LOCAL_API ParamNode *SystemCheckMatchParamWait(const char *name, const char *value); 128 INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dataIndex, int onlyAdd); 129 INIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData); 130 INIT_LOCAL_API ParamSecurityLabel *GetParamSecurityLabel(void); 131 132 INIT_LOCAL_API void LoadParamFromBuild(void); 133 INIT_LOCAL_API int LoadParamFromCmdLine(void); 134 INIT_LOCAL_API void LoadParamAreaSize(void); 135 INIT_LOCAL_API int InitPersistParamWorkSpace(void); 136 INIT_LOCAL_API void ClosePersistParamWorkSpace(void); 137 INIT_LOCAL_API int WritePersistParam(const char *name, const char *value); 138 139 INIT_LOCAL_API int CheckParameterSet(const char *name, const char *value, 140 const ParamSecurityLabel *srcLabel, int *ctrlService); 141 142 INIT_LOCAL_API int CheckParamPermission(const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode); 143 144 INIT_LOCAL_API int SysCheckParamExist(const char *name); 145 INIT_LOCAL_API int GenerateKeyHasCode(const char *buff, size_t len); 146 147 INIT_INNER_API ParamWorkSpace *GetParamWorkSpace(void); 148 INIT_INNER_API int GetParamSecurityAuditData(const char *name, int type, ParamAuditData *auditData); 149 INIT_LOCAL_API int GetServiceCtrlInfo(const char *name, const char *value, ServiceCtrlInfo **ctrlInfo); 150 151 INIT_INNER_API int InitParamWorkSpace(int onlyRead, const PARAM_WORKSPACE_OPS *ops); 152 INIT_LOCAL_API void CloseParamWorkSpace(void); 153 #ifdef STARTUP_INIT_TEST 154 ParamService *GetParamService(); 155 #endif 156 #ifdef __cplusplus 157 #if __cplusplus 158 } 159 #endif 160 #endif 161 #endif