1 /*
2  * Copyright (c) 2022-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 "remote_uri.h"
17 #include <string>
18 #include <fcntl.h>
19 #include <tuple>
20 #include <unistd.h>
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "hap_token_info.h"
24 
25 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleRemoteUri {
28 
29 using namespace std;
30 
IsMediaUri(const string & uriString)31 bool RemoteUri::IsMediaUri(const string &uriString)
32 {
33     if (uriString.find(MEDIALIBRARY_FILE_URI_PREFIX) != string::npos) {
34         return true;
35     }
36     RemoteUri remoteUri = RemoteUri(uriString);
37     string scheme = remoteUri.GetScheme();
38     string path = remoteUri.GetPath();
39     std::size_t len = MEDIA.length();
40     if (path.length() > len) {
41         string media = path.substr(0, len);
42         return scheme == SCHEME && media == MEDIA;
43     }
44     return false;
45 }
46 
IsFileUri(const string & uriString)47 bool RemoteUri::IsFileUri(const string &uriString)
48 {
49     return RemoteUri(uriString).GetScheme() == SCHEME_FILE;
50 }
51 
IsAllDigits(string fdStr)52 static bool IsAllDigits(string fdStr)
53 {
54     for (size_t i = 0; i < fdStr.size(); i++) {
55         if (!isdigit(fdStr[i])) {
56             return false;
57         }
58     }
59     return true;
60 }
61 
GetCallingPkgName()62 static string GetCallingPkgName()
63 {
64     uint32_t pid = IPCSkeleton::GetCallingTokenID();
65     Security::AccessToken::HapTokenInfo tokenInfo = Security::AccessToken::HapTokenInfo();
66     Security::AccessToken::AccessTokenKit::GetHapTokenInfo(pid, tokenInfo);
67     return tokenInfo.bundleName;
68 }
69 
RemoveFd(int fd)70 void RemoteUri::RemoveFd(int fd)
71 {
72     auto iter = fdFromBinder.find(fd);
73     if (iter != fdFromBinder.end()) {
74         fdFromBinder.erase(iter);
75     }
76 }
77 
IsRemoteUri(const string & path,int & fd,const int & flags)78 bool RemoteUri::IsRemoteUri(const string& path, int &fd, const int& flags)
79 {
80     string::size_type posDatashare = path.find(SCHEME_TAG);
81     string::size_type posFragment = path.find(FRAGMENT_TAG);
82     string::size_type posFd = path.find(FD_TAG);
83     if (posDatashare == string::npos || posFragment == string::npos ||
84         posFd == string::npos) {
85         return false;
86     }
87 
88     string scheme = path.substr(0, posDatashare);
89     if (scheme != SCHEME) {
90         return false;
91     }
92 
93     string fragment = path.substr(posFragment + 1, REMOTE_URI_TAG.size());
94     if (fragment == REMOTE_URI_TAG) {
95         string fdStr = path.substr(posFd + 1);
96         if (IsAllDigits(fdStr)) {
97             fd = stoi(fdStr.c_str());
98             if (fd < 0 || flags != O_RDONLY) {
99                 fd = -1;
100             }
101             RemoveFd(fd);
102             return true;
103         }
104         fd = -1;
105         return true;
106     }
107     return false;
108 }
109 
ConvertUri(const int & fd,string & remoteUri)110 int RemoteUri::ConvertUri(const int &fd, string &remoteUri)
111 {
112     if (fd < 0) {
113         return -EINVAL;
114     }
115 
116     if (fdFromBinder.size() == MAX_URI_SIZE) {
117         close(*fdFromBinder.begin());
118         fdFromBinder.erase(fdFromBinder.begin());
119     }
120     fdFromBinder.emplace(fd);
121 
122     string pkgName = GetCallingPkgName();
123     remoteUri = SCHEME + ":///" + pkgName + "/" + FRAGMENT_TAG +
124                             REMOTE_URI_TAG + FD_TAG + to_string(fd);
125     return 0;
126 }
127 
OpenRemoteUri(const string & remoteUri)128 int RemoteUri::OpenRemoteUri(const string &remoteUri)
129 {
130     int fd = -1;
131     (void)IsRemoteUri(remoteUri, fd);
132 
133     return fd;
134 }
135 } // namespace ModuleRemoteUri
136 } // namespace DistributedFS
137 } // namespace OHOS
138