1 /*
2  * Copyright (c) 2023 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 "zip_helper.h"
17 
18 #include <algorithm>
19 #include <cerrno>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <ctime>
24 #include <fcntl.h>
25 #include <file_util.h>
26 #include <regex>
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <time_util.h>
31 #include <unistd.h>
32 
33 #include "bundle_mgr_client.h"
34 #include "climits"
35 #include "event_publish.h"
36 #include "hilog/log.h"
37 #include "hisysevent.h"
38 #include "json/json.h"
39 #include "securec.h"
40 #include "string_ex.h"
41 #include "parameters.h"
42 #include "parameter_ex.h"
43 
44 #undef LOG_DOMAIN
45 #define LOG_DOMAIN 0xD002D12
46 
47 #undef LOG_TAG
48 #define LOG_TAG "Sanitizer"
49 
50 namespace OHOS {
51 namespace HiviewDFX {
52 using namespace OHOS::AppExecFwk;
53 
IsNameValid(const std::string & name,const std::string & sep,bool canEmpty)54 bool IsNameValid(const std::string& name, const std::string& sep, bool canEmpty)
55 {
56     std::vector<std::string> nameVec;
57     SplitStr(name, sep, nameVec, canEmpty, false);
58     std::regex re("^[a-zA-Z0-9][a-zA-Z0-9_]{0,127}$");
59     for (auto const& splitName : nameVec) {
60         if (!std::regex_match(splitName, re)) {
61             HILOG_INFO(LOG_CORE, "Invalid splitName:%{public}s", splitName.c_str());
62             return false;
63         }
64     }
65     return true;
66 }
67 
IsModuleNameValid(const std::string & name)68 bool IsModuleNameValid(const std::string& name)
69 {
70     if (name.empty() || name.size() > MAX_NAME_LENGTH) {
71         HILOG_INFO(LOG_CORE, "invalid log name.");
72         return false;
73     }
74 
75     if (name.find("/") != std::string::npos || name.find(".") == std::string::npos) {
76         std::string path = name.substr(1); // may skip first .
77         path.erase(path.find_last_not_of(" \n\r\t") + 1);
78         HILOG_INFO(LOG_CORE, "module name:%{public}s", name.c_str());
79         return IsNameValid(path, "/", false);
80     }
81 
82     return IsNameValid(name, ".", true);
83 }
84 
GetApplicationNameById(int32_t uid)85 std::string GetApplicationNameById(int32_t uid)
86 {
87     std::string bundleName;
88     AppExecFwk::BundleMgrClient client;
89     if (client.GetNameForUid(uid, bundleName) != ERR_OK) {
90         HILOG_WARN(LOG_CORE, "Failed to query bundleName from bms, uid:%{public}d.", uid);
91     } else {
92         HILOG_INFO(LOG_CORE, "bundleName of uid:%{public}d is %{public}s", uid, bundleName.c_str());
93     }
94     return bundleName;
95 }
96 
GetDfxBundleInfo(const std::string & bundleName,DfxBundleInfo & bundleInfo)97 bool GetDfxBundleInfo(const std::string& bundleName, DfxBundleInfo& bundleInfo)
98 {
99     AppExecFwk::BundleInfo info;
100     AppExecFwk::BundleMgrClient client;
101     if (!client.GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
102                               info, Constants::ALL_USERID)) {
103         HILOG_WARN(LOG_CORE, "Failed to query BundleInfo from bms, bundle:%{public}s.", bundleName.c_str());
104         return false;
105     } else {
106         HILOG_INFO(LOG_CORE, "The version of %{public}s is %{public}s", bundleName.c_str(),
107             info.versionName.c_str());
108     }
109     bundleInfo.isPreInstalled = info.isPreInstallApp;
110     bundleInfo.versionName = info.versionName;
111     bundleInfo.versionCode = info.versionCode;
112     return true;
113 }
114 } // namespace HiviewDFX
115 } // namespace OHOS