1 /*
2 * Copyright (c) 2020 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 #include <ctype.h>
17 #include <fcntl.h>
18 #include <limits.h>
19 #include <securec.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include "ohos_errno.h"
24 #include "param_adaptor.h"
25
26 #ifndef __LITEOS_M__
27 #define DATA_PATH "/storage/data/system/param/"
28 #define SYS_UID_INDEX 1000
29 #else
30 #ifndef DATA_PATH
31 #define DATA_PATH ""
32 #endif
33 #endif
34
35 #define MAX_KEY_PATH 128
36
IsValidChar(const char ch)37 static boolean IsValidChar(const char ch)
38 {
39 if (islower(ch) || isdigit(ch) || (ch == '_') || (ch == '.')) {
40 return TRUE;
41 }
42 return FALSE;
43 }
44
IsValidValue(const char * value,unsigned int len)45 static boolean IsValidValue(const char* value, unsigned int len)
46 {
47 if ((value == NULL) || !strlen(value) || (strlen(value) >= len)) {
48 return FALSE;
49 }
50 return TRUE;
51 }
52
IsValidKey(const char * key)53 static boolean IsValidKey(const char* key)
54 {
55 if (!IsValidValue(key, MAX_KEY_LEN)) {
56 return FALSE;
57 }
58 int keyLen = strlen(key);
59 for (int i = 0; i < keyLen; i++) {
60 if (!IsValidChar(key[i])) {
61 return FALSE;
62 }
63 }
64 return TRUE;
65 }
66
GetSysParam(const char * key,char * value,unsigned int len)67 int GetSysParam(const char* key, char* value, unsigned int len)
68 {
69 if (!IsValidKey(key) || (value == NULL) || (len > MAX_GET_VALUE_LEN)) {
70 return EC_INVALID;
71 }
72 char* keyPath = (char *)malloc(MAX_KEY_PATH + 1);
73 if (keyPath == NULL) {
74 return EC_FAILURE;
75 }
76 if (sprintf_s(keyPath, MAX_KEY_PATH + 1, "%s%s", DATA_PATH, key) < 0) {
77 free(keyPath);
78 return EC_FAILURE;
79 }
80 struct stat info = {0};
81 if (stat(keyPath, &info) != F_OK) {
82 free(keyPath);
83 return EC_FAILURE;
84 }
85 if (info.st_size >= len) {
86 free(keyPath);
87 return EC_INVALID;
88 }
89 int fd = open(keyPath, O_RDONLY, S_IRUSR);
90 free(keyPath);
91 keyPath = NULL;
92 if (fd < 0) {
93 return EC_FAILURE;
94 }
95
96 int ret = read(fd, value, (size_t)info.st_size);
97 close(fd);
98 fd = -1;
99 if (ret < 0) {
100 return EC_FAILURE;
101 }
102 value[info.st_size] = '\0';
103 return info.st_size;
104 }
105
SetSysParam(const char * key,const char * value)106 int SetSysParam(const char* key, const char* value)
107 {
108 if (!IsValidKey(key) || !IsValidValue(value, MAX_VALUE_LEN)) {
109 return EC_INVALID;
110 }
111 char* keyPath = (char *)malloc(MAX_KEY_PATH + 1);
112 if (keyPath == NULL) {
113 return EC_FAILURE;
114 }
115 if (sprintf_s(keyPath, MAX_KEY_PATH + 1, "%s%s", DATA_PATH, key) < 0) {
116 free(keyPath);
117 return EC_FAILURE;
118 }
119 int fd = open(keyPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
120 free(keyPath);
121 keyPath = NULL;
122 if (fd < 0) {
123 return EC_FAILURE;
124 }
125
126 int ret = write(fd, value, strlen(value));
127 close(fd);
128 fd = -1;
129 return (ret < 0) ? EC_FAILURE : EC_SUCCESS;
130 }
131
CheckPermission(void)132 boolean CheckPermission(void)
133 {
134 #if (!defined(_WIN32) && !defined(_WIN64) && !defined(__LITEOS_M__))
135 uid_t uid = getuid();
136 if (uid <= SYS_UID_INDEX) {
137 return TRUE;
138 }
139 #endif
140 #if defined(__LITEOS_M__)
141 return TRUE;
142 #else
143 return FALSE;
144 #endif
145 }
146