1 /*
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2010-2014 Todd C. Miller <Todd.Miller@sudo.ws>
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 /*
20 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22 */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #ifdef HAVE_STDBOOL_H
29 # include <stdbool.h>
30 #else
31 # include "compat/stdbool.h"
32 #endif /* HAVE_STDBOOL_H */
33 #include <string.h>
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif /* HAVE_STRINGS_H */
37 #include <grp.h>
38
39 #include "sudo_compat.h"
40 #include "sudo_dso.h"
41 #include "sudo_plugin.h"
42 #include "sudo_util.h"
43
44 /*
45 * Sudoers group plugin that does group name-based lookups using the system
46 * group database functions, similar to how sudo behaved prior to 1.7.3.
47 * This can be used on systems where lookups by group ID are problematic.
48 */
49
50 typedef struct group * (*sysgroup_getgrnam_t)(const char *);
51 typedef struct group * (*sysgroup_getgrgid_t)(gid_t);
52 typedef void (*sysgroup_gr_delref_t)(struct group *);
53
54 static sysgroup_getgrnam_t sysgroup_getgrnam;
55 static sysgroup_getgrgid_t sysgroup_getgrgid;
56 static sysgroup_gr_delref_t sysgroup_gr_delref;
57 static bool need_setent;
58
59 static int
sysgroup_init(int version,sudo_printf_t plugin_printf,char * const argv[])60 sysgroup_init(int version, sudo_printf_t plugin_printf, char *const argv[])
61 {
62 void *handle;
63
64 if (SUDO_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
65 plugin_printf(SUDO_CONV_ERROR_MSG,
66 "sysgroup_group: incompatible major version %d, expected %d\n",
67 SUDO_API_VERSION_GET_MAJOR(version),
68 GROUP_API_VERSION_MAJOR);
69 return -1;
70 }
71
72 /* Share group cache with sudo if possible. */
73 handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_getgrnam");
74 if (handle != NULL) {
75 sysgroup_getgrnam = (sysgroup_getgrnam_t)handle;
76 } else {
77 sysgroup_getgrnam = (sysgroup_getgrnam_t)getgrnam;
78 need_setent = true;
79 }
80
81 handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_getgrgid");
82 if (handle != NULL) {
83 sysgroup_getgrgid = (sysgroup_getgrgid_t)handle;
84 } else {
85 sysgroup_getgrgid = (sysgroup_getgrgid_t)getgrgid;
86 need_setent = true;
87 }
88
89 handle = sudo_dso_findsym(SUDO_DSO_DEFAULT, "sudo_gr_delref");
90 if (handle != NULL)
91 sysgroup_gr_delref = (sysgroup_gr_delref_t)handle;
92
93 if (need_setent)
94 setgrent();
95
96 return true;
97 }
98
99 static void
sysgroup_cleanup(void)100 sysgroup_cleanup(void)
101 {
102 if (need_setent)
103 endgrent();
104 }
105
106 /*
107 * Returns true if "user" is a member of "group", else false.
108 */
109 static int
sysgroup_query(const char * user,const char * group,const struct passwd * pwd)110 sysgroup_query(const char *user, const char *group, const struct passwd *pwd)
111 {
112 char **member;
113 struct group *grp;
114
115 grp = sysgroup_getgrnam(group);
116 if (grp == NULL && group[0] == '#' && group[1] != '\0') {
117 const char *errstr;
118 gid_t gid = sudo_strtoid(group + 1, &errstr);
119 if (errstr == NULL)
120 grp = sysgroup_getgrgid(gid);
121 }
122 if (grp != NULL) {
123 if (grp->gr_mem != NULL) {
124 for (member = grp->gr_mem; *member != NULL; member++) {
125 if (strcasecmp(user, *member) == 0) {
126 if (sysgroup_gr_delref)
127 sysgroup_gr_delref(grp);
128 return true;
129 }
130 }
131 }
132 if (sysgroup_gr_delref)
133 sysgroup_gr_delref(grp);
134 }
135
136 return false;
137 }
138
139 sudo_dso_public struct sudoers_group_plugin group_plugin = {
140 GROUP_API_VERSION,
141 sysgroup_init,
142 sysgroup_cleanup,
143 sysgroup_query
144 };
145