e55ec75f6e4b7c80ec6790cfe87ded7e98bc1adf
[platform/core/test/security-tests.git] / tests / common / tests_common.cpp
1 /*
2  * Copyright (c) 2013 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 /*
18  * @file        tests_common.cpp
19  * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
20  * @version     1.0
21  * @brief       Common functions and macros used in security-tests package.
22  */
23
24 #include "tests_common.h"
25 #include <unistd.h>
26 #include <grp.h>
27 #include <errno.h>
28 #include <vector>
29
30 int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
31
32 const char *WGT_APP_ID = "QwCqJ0ttyS";
33
34 int smack_runtime_check(void)
35 {
36     static int smack_present = -1;
37     if (-1 == smack_present) {
38         if (smack_smackfs_path()) {
39             smack_present = 1;
40         } else {
41             smack_present = 0;
42         }
43     }
44     return smack_present;
45 }
46
47 int smack_check(void)
48 {
49 #ifndef WRT_SMACK_ENABLED
50     return 0;
51 #else
52     return smack_runtime_check();
53 #endif
54 }
55
56 /**
57  * Dropping root privileges
58  * returns 0 on success, 1 on error
59  */
60 int drop_root_privileges(void)
61 {
62     if (getuid() == 0) {
63         /* process is running as root, drop privileges */
64         if (setgid(APP_GID) != 0)
65             return 1;
66         if (setuid(APP_UID) != 0)
67             return 1;
68     }
69     uid_t uid = getuid();
70     if (uid == APP_UID)
71         return 0;
72
73     return 1;
74 }
75
76 void setLabelForSelf(const int line, const char *label)
77 {
78     int ret = smack_set_label_for_self(label);
79     RUNNER_ASSERT_MSG_BT(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
80 }
81
82 /*
83  * Add a new group to the current process groups.
84  */
85 void add_process_group(const char* group_name)
86 {
87     // get group ID by group name
88     group *gr = getgrnam(group_name);
89     RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
90     const gid_t new_group_id = gr->gr_gid;
91
92     // get number of groups that the current process belongs to
93     int ngroups = getgroups(0, NULL);
94
95     //allocate groups table + space for new group entry
96     std::vector<gid_t> groups(ngroups + 1);
97     getgroups(ngroups, groups.data());
98
99     // check if the process already belongs to the group
100     if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
101
102     // add new group & apply change
103     groups[ngroups] = new_group_id;
104     int ret = setgroups(groups.size(), groups.data());
105     int error = errno;
106     RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error));
107 }
108
109 /*
110  * Remove specific group from the current process groups.
111  */
112 void remove_process_group(const char* group_name)
113 {
114     // get group ID by group name
115     group *gr = getgrnam(group_name);
116     RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
117     const gid_t new_group_id = gr->gr_gid;
118
119     int ngroups = getgroups(0, NULL);
120     std::vector<gid_t> groups(ngroups);
121     getgroups(ngroups, groups.data());
122
123     // remove group from the list
124     groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
125
126     if (groups.size() != (size_t)ngroups) {
127         // apply change
128         int ret = setgroups(groups.size(), groups.data());
129         int error = errno;
130         RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error));
131     }
132 }
133
134 std::string formatCstr(const char *cstr)
135 {
136     if (!cstr)
137         return std::string("nullptr");
138     return std::string("\"") + cstr + "\"";
139 }