Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/system_group/system_group.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2010-2014 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#ifdef HAVE_STDBOOL_H
24
# include <stdbool.h>
25
#else
26
# include <compat/stdbool.h>
27
#endif /* HAVE_STDBOOL_H */
28
#include <string.h>
29
#ifdef HAVE_STRINGS_H
30
# include <strings.h>
31
#endif /* HAVE_STRINGS_H */
32
#include <grp.h>
33
34
#include <sudo_compat.h>
35
#include <sudo_dso.h>
36
#include <sudo_plugin.h>
37
#include <sudo_util.h>
38
39
/*
40
* Sudoers group plugin that does group name-based lookups using the system
41
* group database functions, similar to how sudo behaved prior to 1.7.3.
42
* This can be used on systems where lookups by group ID are problematic.
43
*/
44
45
typedef struct group * (*sysgroup_getgrnam_t)(const char *);
46
typedef struct group * (*sysgroup_getgrgid_t)(gid_t);
47
typedef void (*sysgroup_gr_delref_t)(struct group *);
48
49
static sysgroup_getgrnam_t sysgroup_getgrnam;
50
static sysgroup_getgrgid_t sysgroup_getgrgid;
51
static sysgroup_gr_delref_t sysgroup_gr_delref;
52
static bool need_setent;
53
54
static int
55
sysgroup_init(int version, sudo_printf_t plugin_printf, char *const argv[])
56
{
57
void *handle;
58
59
if (SUDO_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
60
plugin_printf(SUDO_CONV_ERROR_MSG,
61
"sysgroup_group: incompatible major version %d, expected %d\n",
62
SUDO_API_VERSION_GET_MAJOR(version),
63
GROUP_API_VERSION_MAJOR);
64
return -1;
65
}
66
67
/* Share group cache with sudo if possible. */
68
handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_getgrnam");
69
if (handle != NULL) {
70
sysgroup_getgrnam = (sysgroup_getgrnam_t)handle;
71
} else {
72
sysgroup_getgrnam = (sysgroup_getgrnam_t)getgrnam;
73
need_setent = true;
74
}
75
76
handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_getgrgid");
77
if (handle != NULL) {
78
sysgroup_getgrgid = (sysgroup_getgrgid_t)handle;
79
} else {
80
sysgroup_getgrgid = (sysgroup_getgrgid_t)getgrgid;
81
need_setent = true;
82
}
83
84
handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_gr_delref");
85
if (handle != NULL)
86
sysgroup_gr_delref = (sysgroup_gr_delref_t)handle;
87
88
if (need_setent)
89
setgrent();
90
91
return true;
92
}
93
94
static void
95
sysgroup_cleanup(void)
96
{
97
if (need_setent)
98
endgrent();
99
}
100
101
/*
102
* Returns true if "user" is a member of "group", else false.
103
*/
104
static int
105
sysgroup_query(const char *user, const char *group, const struct passwd *pwd)
106
{
107
char **member;
108
struct group *grp;
109
110
grp = sysgroup_getgrnam(group);
111
if (grp == NULL && group[0] == '#' && group[1] != '\0') {
112
const char *errstr;
113
gid_t gid = sudo_strtoid(group + 1, &errstr);
114
if (errstr == NULL)
115
grp = sysgroup_getgrgid(gid);
116
}
117
if (grp != NULL) {
118
if (grp->gr_mem != NULL) {
119
for (member = grp->gr_mem; *member != NULL; member++) {
120
if (strcasecmp(user, *member) == 0) {
121
if (sysgroup_gr_delref)
122
sysgroup_gr_delref(grp);
123
return true;
124
}
125
}
126
}
127
if (sysgroup_gr_delref)
128
sysgroup_gr_delref(grp);
129
}
130
131
return false;
132
}
133
134
sudo_dso_public struct sudoers_group_plugin group_plugin = {
135
GROUP_API_VERSION,
136
sysgroup_init,
137
sysgroup_cleanup,
138
sysgroup_query
139
};
140
141