Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/group_file/group_file.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 <sys/stat.h>
22
23
#include <stdio.h>
24
#include <stdlib.h>
25
#ifdef HAVE_STDBOOL_H
26
# include <stdbool.h>
27
#else
28
# include <compat/stdbool.h>
29
#endif /* HAVE_STDBOOL_H */
30
#include <string.h>
31
#ifdef HAVE_STRINGS_H
32
# include <strings.h>
33
#endif /* HAVE_STRINGS_H */
34
#include <unistd.h>
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <grp.h>
38
39
#include <sudo_plugin.h>
40
#include <sudo_compat.h>
41
42
/*
43
* Sample sudoers group plugin that uses an extra group file with the
44
* same format as /etc/group.
45
*/
46
47
static sudo_printf_t sudo_log;
48
49
extern void mysetgrfile(const char *);
50
extern int mysetgroupent(int);
51
extern void myendgrent(void);
52
extern struct group *mygetgrnam(const char *);
53
54
static int
55
sample_init(int version, sudo_printf_t sudo_printf, char *const argv[])
56
{
57
struct stat sb;
58
59
sudo_log = sudo_printf;
60
61
if (SUDO_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
62
sudo_log(SUDO_CONV_ERROR_MSG,
63
"group_file: incompatible major version %d, expected %d\n",
64
SUDO_API_VERSION_GET_MAJOR(version),
65
GROUP_API_VERSION_MAJOR);
66
return -1;
67
}
68
69
/* Check that the group file exists and has a safe mode. */
70
if (argv == NULL || argv[0] == NULL) {
71
sudo_log(SUDO_CONV_ERROR_MSG,
72
"group_file: path to group file not specified\n");
73
return -1;
74
}
75
if (stat(argv[0], &sb) != 0) {
76
sudo_log(SUDO_CONV_ERROR_MSG,
77
"group_file: %s: %s\n", argv[0], strerror(errno));
78
return -1;
79
}
80
if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
81
sudo_log(SUDO_CONV_ERROR_MSG,
82
"%s must be only be writable by owner\n", argv[0]);
83
return -1;
84
}
85
86
mysetgrfile(argv[0]);
87
if (!mysetgroupent(1))
88
return false;
89
90
return true;
91
}
92
93
static void
94
sample_cleanup(void)
95
{
96
myendgrent();
97
}
98
99
/*
100
* Returns true if "user" is a member of "group", else false.
101
*/
102
static int
103
sample_query(const char *user, const char *group, const struct passwd *pwd)
104
{
105
struct group *grp;
106
char **member;
107
108
grp = mygetgrnam(group);
109
if (grp != NULL && grp->gr_mem != NULL) {
110
for (member = grp->gr_mem; *member != NULL; member++) {
111
if (strcasecmp(user, *member) == 0)
112
return true;
113
}
114
}
115
116
return false;
117
}
118
119
sudo_dso_public struct sudoers_group_plugin group_plugin = {
120
GROUP_API_VERSION,
121
sample_init,
122
sample_cleanup,
123
sample_query
124
};
125
126