Add mockup for security-manager.
[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 <sys/smack.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24
25 #include <security-manager.h>
26
27 #include <dpl/log/log.h>
28 #include <protocols.h>
29 #include <socket-2-id.h>
30
31 namespace CKM {
32 namespace {
33
34 int getCredentialsFromSocket(int sock, std::string &res)  {
35     std::vector<char> result(1);
36     socklen_t length = 1;
37
38     if ((0 > getsockopt(sock, SOL_SOCKET, SO_PEERSEC, result.data(), &length))
39       && errno != ERANGE)
40     {
41         LogError("getsockopt failed");
42         return -1;
43     }
44
45     result.resize(length);
46
47     if (0 > getsockopt(sock, SOL_SOCKET, SO_PEERSEC, result.data(), &length)) {
48         LogError("getsockopt failed");
49         return -1;
50     }
51
52     result.push_back('\0');
53     res = result.data();
54     return 0;
55 }
56
57 int getPkgIdFromSmack(const std::string &smack, std::string &pkgId) {
58     pkgId = smack;
59     return 0;
60 }
61
62 } // namespace anonymous
63
64
65 int Socket2Id::translate(int sock, std::string &result) {
66     std::string smack;
67     std::string pkgId;
68
69     if (0 > getCredentialsFromSocket(sock, smack)) {
70         return -1;
71     }
72
73     if (0 > getPkgIdFromSmack(smack, pkgId)) {
74         return -1;
75     }
76
77     result = pkgId;
78     return 0;
79 }
80
81 void Socket2Id::resetCache() {
82     m_stringMap.clear();
83 }
84
85 } // namespace CKM
86