889e8ead09428487d18ca1ef876b54e7899729b5
[platform/core/security/key-manager.git] / src / manager / main / socket-2-id-mockup.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       socket-2-id-mockup.cpp
18  * @author     Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21 #include <string>
22
23 #include <dpl/log/log.h>
24 #include <protocols.h>
25 #include <socket-2-id.h>
26
27 namespace CKM {
28
29 int Socket2Id::getPkgIdFromSmack(const std::string &smack, std::string &pkgId) {
30     static const std::string SMACK_PREFIX_APPID  = "User::App::";
31
32     if (smack.empty()) {
33         LogError("Smack is empty. Connection will be rejected");
34         return -1;
35     }
36
37     if (smack.compare(0, SMACK_PREFIX_APPID.size(), SMACK_PREFIX_APPID)) {
38         pkgId = "/" + smack;
39         LogDebug("Smack: " << smack << " Was translated to owner id: " << pkgId);
40         return 0;
41     }
42
43     std::string appId = smack.substr(SMACK_PREFIX_APPID.size(), std::string::npos);
44
45     if (appId.empty()) {
46         LogError("After conversion (smack->pkgId) pkgId is empty. Label: " << appId);
47         return -1;
48     }
49
50     pkgId = std::move(appId);
51     LogDebug("Smack: " << smack << " Was translated to owner id: " << pkgId);
52     return 0;
53 }
54
55 int Socket2Id::translate(int sock, std::string &result) {
56     std::string smack;
57     std::string pkgId;
58
59     if (0 > getCredentialsFromSocket(sock, smack)) {
60         return -1;
61     }
62
63     if (0 > getPkgIdFromSmack(smack, pkgId)) {
64         return -1;
65     }
66
67     result = std::move(pkgId);
68     return 0;
69 }
70
71 } // namespace CKM
72