1 /*
2 * Copyright (c) 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 #include <ctype.h>
16 #include <limits.h>
17
18 #include "param_manager.h"
19 #include "param_trie.h"
20 #include "param_base.h"
21
GetWorkSpaceByName(const char * name)22 INIT_LOCAL_API WorkSpace *GetWorkSpaceByName(const char *name)
23 {
24 ParamWorkSpace *paramSpace = GetParamWorkSpace();
25 PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
26 #ifdef PARAM_SUPPORT_SELINUX
27 if (paramSpace->selinuxSpace.getParamLabelIndex == NULL) {
28 return NULL;
29 }
30 uint32_t labelIndex = (uint32_t)paramSpace->selinuxSpace.getParamLabelIndex(name) + WORKSPACE_INDEX_BASE;
31 if (labelIndex < paramSpace->maxLabelIndex) {
32 return paramSpace->workSpace[labelIndex];
33 }
34 return NULL;
35 #else
36 return paramSpace->workSpace[WORKSPACE_INDEX_DAC];
37 #endif
38 }
39
GetWorkSpace(uint32_t labelIndex)40 INIT_LOCAL_API WorkSpace *GetWorkSpace(uint32_t labelIndex)
41 {
42 ParamWorkSpace *paramSpace = GetParamWorkSpace();
43 PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
44 PARAM_CHECK(paramSpace->workSpace != NULL, return NULL, "Invalid paramSpace->workSpace");
45 PARAM_WORKSPACE_CHECK(paramSpace, return NULL, "Invalid space");
46 #ifdef PARAM_SUPPORT_SELINUX
47 if (labelIndex == 0) {
48 return paramSpace->workSpace[0];
49 }
50 WorkSpace *workSpace = NULL;
51 if (labelIndex < paramSpace->maxLabelIndex) {
52 workSpace = paramSpace->workSpace[labelIndex];
53 }
54 if (workSpace == NULL) {
55 return NULL;
56 }
57 uint32_t rwSpaceLock = ATOMIC_LOAD_EXPLICIT(&workSpace->rwSpaceLock, MEMORY_ORDER_ACQUIRE);
58 if (rwSpaceLock & WORKSPACE_STATUS_IN_PROCESS) {
59 return NULL;
60 }
61 if (workSpace->area != NULL) {
62 return workSpace;
63 }
64 return NULL;
65 #else
66 return paramSpace->workSpace[0];
67 #endif
68 }
69
GetParamSecurityOps(int type)70 INIT_LOCAL_API ParamSecurityOps *GetParamSecurityOps(int type)
71 {
72 PARAM_CHECK(type < PARAM_SECURITY_MAX, return NULL, "Invalid type");
73 ParamWorkSpace *paramSpace = GetParamWorkSpace();
74 PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
75 return ¶mSpace->paramSecurityOps[type];
76 }
77
GetParamSecurityLabel()78 INIT_LOCAL_API ParamSecurityLabel *GetParamSecurityLabel()
79 {
80 ParamWorkSpace *paramSpace = GetParamWorkSpace();
81 PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
82 return ¶mSpace->securityLabel;
83 }
84
SplitParamString(char * line,const char * exclude[],uint32_t count,int (* result)(const uint32_t * context,const char * name,const char * value),const uint32_t * context)85 INIT_LOCAL_API int SplitParamString(char *line, const char *exclude[], uint32_t count,
86 int (*result)(const uint32_t *context, const char *name, const char *value), const uint32_t *context)
87 {
88 // Skip spaces
89 char *name = line;
90 while (isspace(*name) && (*name != '\0')) {
91 name++;
92 }
93 // Empty line or Comment line
94 if (*name == '\0' || *name == '#') {
95 return 0;
96 }
97
98 char *value = name;
99 // find the first delimiter '='
100 while (*value != '\0') {
101 if (*value == '=') {
102 (*value) = '\0';
103 value = value + 1;
104 break;
105 }
106 value++;
107 }
108
109 // Skip spaces
110 char *tmp = name;
111 while ((tmp < value) && (*tmp != '\0')) {
112 if (isspace(*tmp)) {
113 (*tmp) = '\0';
114 break;
115 }
116 tmp++;
117 }
118
119 // empty name, just ignore this line
120 if (*value == '\0') {
121 return 0;
122 }
123
124 // Filter excluded parameters
125 for (uint32_t i = 0; i < count; i++) {
126 if (strncmp(name, exclude[i], strlen(exclude[i])) == 0) {
127 return 0;
128 }
129 }
130
131 // Skip spaces for value
132 while (isspace(*value) && (*value != '\0')) {
133 value++;
134 }
135
136 // Trim the ending spaces of value
137 char *pos = value + strlen(value);
138 pos--;
139 while (isspace(*pos) && pos > value) {
140 (*pos) = '\0';
141 pos--;
142 }
143
144 // Strip starting and ending " for value
145 if ((*value == '"') && (pos > value) && (*pos == '"')) {
146 value = value + 1;
147 *pos = '\0';
148 }
149 return result(context, name, value);
150 }
151