Skip to content

Latest commit

 

History

History
132 lines (109 loc) · 3.65 KB

sailfishaccesscontrol.c

File metadata and controls

132 lines (109 loc) · 3.65 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* Copyright (c) 2019 Open Mobile Platform LLC.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "sailfishaccesscontrol.h"
#include <grp.h>
#include <pwd.h>
#include <stdlib.h>
#include <glib.h>
Apr 9, 2020
Apr 9, 2020
25
#include <sys/stat.h>
Apr 9, 2020
Apr 9, 2020
27
#define GROUPS_DATABASE "/etc/group"
28
#define NO_GROUPS GINT_TO_POINTER(1)
Dec 18, 2019
Dec 18, 2019
29
30
#define SAILFISH_SYSTEM_GROUP "sailfish-system"
#define SAILFISH_SYSTEM_PREFIX "sailfish-"
31
32
33
GHashTable *s_groups = NULL;
Mar 11, 2022
Mar 11, 2022
34
static void ensure_groups_validity(void)
Apr 9, 2020
Apr 9, 2020
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
struct stat buf;
time_t new_mtime = 0;
static time_t old_mtime = 0;
if (stat(GROUPS_DATABASE, &buf) == 0)
new_mtime = buf.st_mtime;
if (old_mtime != new_mtime) {
old_mtime = new_mtime;
if (s_groups)
g_hash_table_remove_all(s_groups);
}
}
static void destroy_group_list(GSList *group_list)
{
if (group_list == NO_GROUPS)
return;
g_slist_free_full(group_list, g_free);
}
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
static GSList *init_group_list(uid_t uid)
{
GSList *group_list = NULL;
struct passwd *passwd_entry = getpwuid(uid);
if (passwd_entry) {
int ngroups = 32;
const char *user_name = passwd_entry->pw_name;
gid_t group_id = passwd_entry->pw_gid;
gid_t *groups = g_malloc0(ngroups * sizeof(gid_t));
if (getgrouplist(user_name, group_id, groups, &ngroups) == -1) {
g_free(groups);
// When failing ngroups will contain number of groups.
groups = g_malloc0(ngroups * sizeof(gid_t));
if (getgrouplist(user_name, group_id, groups, &ngroups) == -1)
ngroups = 0;
}
for (int i = 0; i < ngroups; i++) {
struct group *gr = getgrgid(groups[i]);
if (gr)
group_list = g_slist_prepend(group_list, g_strdup(gr->gr_name));
}
g_free(groups);
}
g_hash_table_insert(s_groups, GINT_TO_POINTER(uid), (group_list ? group_list : NO_GROUPS));
return group_list;
}
bool sailfish_access_control_hasgroup(uid_t uid, const char *group_name)
{
GSList *groups = NULL;
Apr 9, 2020
Apr 9, 2020
91
92
93
ensure_groups_validity();
Apr 9, 2020
Apr 9, 2020
95
s_groups = g_hash_table_new_full(NULL, NULL, NULL, destroy_group_list);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
groups = g_hash_table_lookup(s_groups, GINT_TO_POINTER(uid));
if (!groups)
groups = init_group_list(uid);
if (groups == NO_GROUPS)
return false;
GSList *item = g_slist_find_custom(groups, group_name, (GCompareFunc)g_strcmp0);
if (item)
return true;
return false;
}
Dec 18, 2019
Dec 18, 2019
110
Mar 11, 2022
Mar 11, 2022
111
uid_t sailfish_access_control_systemuser_uid(void)
Dec 18, 2019
Dec 18, 2019
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
struct group *grp = getgrnam(SAILFISH_SYSTEM_GROUP);
struct passwd *pw;
int i;
uid_t uid = SAILFISH_UNDEFINED_UID;
// Get system user UID using sailfish-system group
if (grp) {
for (i = 0; grp->gr_mem[i]; i++) {
if (strncmp(grp->gr_mem[i], SAILFISH_SYSTEM_PREFIX, strlen(SAILFISH_SYSTEM_PREFIX))) {
pw = getpwnam(grp->gr_mem[i]);
if (pw) {
uid = pw->pw_uid;
break;
}
}
}
}
return uid;
}