d60c24146fbc97ff65bc47a428691b4a31f68ea7
[platform/core/security/cynara.git] / src / helpers / creds-socket / creds-socket.cpp
1 /*
2  *  Copyright (c) 2014 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        src/helpers/creds-socket/creds-socket.cpp
18  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
19  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
20  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
21  * @version     1.0
22  * @brief       Implementation of external libcynara-creds-socket API
23  */
24
25 #include <sys/socket.h>
26 #include <sys/types.h>
27
28 #include <attributes/attributes.h>
29
30 #include <creds-socket-inner.h>
31
32 #include <cynara-client-error.h>
33 #include <cynara-creds-commons.h>
34 #include <cynara-creds-socket.h>
35
36 CYNARA_API
37 int cynara_creds_socket_get_client(int socket_fd, enum cynara_client_creds method, char **client) {
38     if (client == nullptr)
39         return CYNARA_API_INVALID_PARAM;
40
41     switch (method) {
42         case cynara_client_creds::CLIENT_METHOD_SMACK:
43             return getClientSmackLabel(socket_fd, client);
44         case cynara_client_creds::CLIENT_METHOD_PID:
45             return getClientPid(socket_fd, client);
46         default:
47             return CYNARA_API_METHOD_NOT_SUPPORTED;
48     }
49 }
50
51 CYNARA_API
52 int cynara_creds_socket_get_user(int socket_fd, enum cynara_user_creds method, char **user) {
53     if (user == nullptr)
54         return CYNARA_API_INVALID_PARAM;
55
56     switch (method) {
57         case cynara_user_creds::USER_METHOD_UID:
58             return getUserId(socket_fd, user);
59         case cynara_user_creds::USER_METHOD_GID:
60             return getUserGid(socket_fd, user);
61         default:
62             return CYNARA_API_METHOD_NOT_SUPPORTED;
63     }
64 }
65
66 CYNARA_API
67 int cynara_creds_socket_get_pid(int socket_fd, pid_t *pid) {
68     return getPid(socket_fd, pid);
69 }