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 34extern "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 71typedef 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 86typedef struct { 87 ParamTaskPtr serverTask; 88 ParamTaskPtr timer; 89 ParamTaskPtr watcherTask; 90} ParamService; 91 92typedef struct { 93 uint32_t flags; 94 long long commitId; 95 ParamTaskPtr saveTimer; 96 struct timespec lastSaveTimer; 97 PersistParamOps persistParamOps; 98} ParamPersistWorkSpace; 99 100typedef 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 107typedef void (*TraversalParamPtr)(ParamHandle handle, void *context); 108typedef struct { 109 TraversalParamPtr traversalParamPtr; 110 void *context; 111 char *prefix; 112} ParamTraversalContext; 113 114typedef struct { 115 uint8_t type; 116 uint8_t mode; 117 const char *name; 118 const char *value; 119} ParamInfos; 120 121#define PARAM_HANDLE(workSpace, index) (ParamHandle)((workSpace)->spaceIndex << 24 | (index)) 122#define PARAM_GET_HANDLE_INFO(handle, label, index) \ 123 do { \ 124 (label) = (((handle) >> 24) & 0x000000ff); \ 125 (index) = (handle) & 0x00ffffff; \ 126 if (((index) & 0x03) != 0) { \ 127 (index) = 0; \ 128 } \ 129 } while (0) 130 131INIT_LOCAL_API int AddWorkSpace(const char *name, uint32_t labelIndex, int onlyRead, uint32_t spacesize); 132INIT_LOCAL_API int OpenWorkSpace(uint32_t index, int readOnly); 133 134INIT_LOCAL_API WorkSpace *GetNextWorkSpace(WorkSpace *curr); 135INIT_LOCAL_API WorkSpace *GetWorkSpace(uint32_t labelIndex); 136INIT_LOCAL_API WorkSpace *GetWorkSpaceByName(const char *name); 137 138INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType); 139INIT_LOCAL_API int CheckParamName(const char *name, int paramInfo); 140INIT_LOCAL_API uint8_t GetParamValueType(const char *name); 141 142INIT_LOCAL_API ParamNode *SystemCheckMatchParamWait(const char *name, const char *value); 143INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dataIndex, int onlyAdd); 144INIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData); 145INIT_LOCAL_API ParamSecurityLabel *GetParamSecurityLabel(void); 146 147INIT_LOCAL_API void LoadParamFromBuild(void); 148INIT_LOCAL_API int LoadParamFromCmdLine(void); 149INIT_LOCAL_API void LoadParamAreaSize(void); 150INIT_LOCAL_API int InitPersistParamWorkSpace(void); 151INIT_LOCAL_API void ClosePersistParamWorkSpace(void); 152INIT_LOCAL_API int WritePersistParam(const char *name, const char *value); 153 154INIT_LOCAL_API int CheckParameterSet(const char *name, const char *value, 155 const ParamSecurityLabel *srcLabel, int *ctrlService); 156 157INIT_LOCAL_API int CheckParamPermission(const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode); 158 159INIT_LOCAL_API int SysCheckParamExist(const char *name); 160INIT_LOCAL_API int GenerateKeyHasCode(const char *buff, size_t len); 161 162INIT_INNER_API ParamWorkSpace *GetParamWorkSpace(void); 163INIT_INNER_API int GetParamSecurityAuditData(const char *name, int type, ParamAuditData *auditData); 164INIT_LOCAL_API int GetServiceCtrlInfo(const char *name, const char *value, ServiceCtrlInfo **ctrlInfo); 165 166INIT_INNER_API int InitParamWorkSpace(int onlyRead, const PARAM_WORKSPACE_OPS *ops); 167INIT_LOCAL_API void CloseParamWorkSpace(void); 168INIT_LOCAL_API int CheckIfUidInGroup(const gid_t groupId, const char *groupCheckName); 169 170#ifdef STARTUP_INIT_TEST 171ParamService *GetParamService(); 172#endif 173#ifdef __cplusplus 174#if __cplusplus 175} 176#endif 177#endif 178#endif