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 
16 #include "trans_channel_limit.h"
17 
18 #include <securec.h>
19 
20 #include "anonymizer.h"
21 #include "permission_entry.h"
22 #include "regex.h"
23 #include "softbus_def.h"
24 #include "softbus_errcode.h"
25 #include "trans_log.h"
26 
27 typedef struct {
28     const char *sessionName;
29     bool regexp;
30 } SessionWhiteList;
31 
32 static const SessionWhiteList g_sessionWhiteList[] = {
33     {
34         .sessionName = "ohos.distributedhardware.devicemanager.resident",
35         .regexp = false,
36     },
37     {
38         .sessionName = "com.huawei.devicegroupmanage",
39         .regexp = false,
40     },
41     {
42         .sessionName = "IShareAuthSession",
43         .regexp = false,
44     },
45     {
46         .sessionName = "com.huawei.devicemanager.resident",
47         .regexp = false,
48     },
49     {
50         .sessionName = "com.huawei.plrdtest.dsoftbus",
51         .regexp = false,
52     },
53     {
54         .sessionName = "com.huawei.*CastPlusDiscoveryModule",
55         .regexp = true,
56     },
57     {
58         .sessionName = "com.huawei.dmsdp+dmsdp",
59         .regexp = false,
60     },
61     {
62         .sessionName = "com.huawei.devicemanager.dynamic",
63         .regexp = false,
64     },
65     {
66         .sessionName = "ohos.distributedhardware.devicemanager.pinholder",
67         .regexp = false,
68     }
69 };
70 
71 #define NO_PKG_NAME_SESSION_WHITE_LIST_NUM (1)
72 static char g_noPkgNameSessionWhiteList[NO_PKG_NAME_SESSION_WHITE_LIST_NUM][SESSION_NAME_SIZE_MAX] = {
73     "com.huawei.devicemanager.resident",
74 };
75 
CheckSessionNameValidOnAuthChannel(const char * sessionName)76 bool CheckSessionNameValidOnAuthChannel(const char *sessionName)
77 {
78     if (sessionName == NULL) {
79         return false;
80     }
81 
82     uint32_t count = sizeof(g_sessionWhiteList) / sizeof(g_sessionWhiteList[0]);
83     for (uint32_t index = 0; index < count; ++index) {
84         if (CompareString(g_sessionWhiteList[index].sessionName, sessionName,
85                 g_sessionWhiteList[index].regexp) == SOFTBUS_OK) {
86             return true;
87         }
88     }
89     char *tmpName = NULL;
90     Anonymize(sessionName, &tmpName);
91     TRANS_LOGE(TRANS_CTRL,
92         "auth channel sessionName invalid. sessionName=%{public}s", tmpName);
93     AnonymizeFree(tmpName);
94     return false;
95 }
96 
IsNoPkgNameSession(const char * sessionName)97 bool IsNoPkgNameSession(const char *sessionName)
98 {
99     if (sessionName == NULL) {
100         return false;
101     }
102 
103     uint16_t index = 0;
104     size_t len = 0;
105     for (; index < NO_PKG_NAME_SESSION_WHITE_LIST_NUM; ++index) {
106         len = strnlen(g_noPkgNameSessionWhiteList[index], SESSION_NAME_SIZE_MAX);
107         if (strncmp(sessionName, g_noPkgNameSessionWhiteList[index], len) == 0) {
108             return true;
109         }
110     }
111 
112     return false;
113 }
114