2 * Copyright (c) 2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
30 #endif /* STDC_HEADERS */
34 # include "compat/stdbool.h"
35 #endif /* HAVE_STDBOOL_H */
37 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
41 #endif /* HAVE_STRING_H */
44 #endif /* HAVE_STRINGS_H */
47 #endif /* HAVE_UNISTD_H */
51 # include "compat/dlfcn.h"
60 #include "sudo_plugin.h"
64 # define RTLD_DEFAULT NULL
68 * Sudoers group plugin that does group name-based lookups using the system
69 * group database functions, similar to how sudo behaved prior to 1.7.3.
70 * This can be used on systems where lookups by group ID are problematic.
73 static sudo_printf_t sudo_log;
75 typedef struct group * (*sysgroup_getgrnam_t)(const char *);
76 typedef struct group * (*sysgroup_getgrgid_t)(gid_t);
77 typedef void (*sysgroup_gr_delref_t)(struct group *);
79 static sysgroup_getgrnam_t sysgroup_getgrnam;
80 static sysgroup_getgrgid_t sysgroup_getgrgid;
81 static sysgroup_gr_delref_t sysgroup_gr_delref;
82 static bool need_setent;
85 sysgroup_init(int version, sudo_printf_t sudo_printf, char *const argv[])
89 sudo_log = sudo_printf;
91 if (GROUP_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
92 sudo_log(SUDO_CONV_ERROR_MSG,
93 "sysgroup_group: incompatible major version %d, expected %d\n",
94 GROUP_API_VERSION_GET_MAJOR(version),
95 GROUP_API_VERSION_MAJOR);
99 /* Share group cache with sudo if possible. */
100 handle = dlsym(RTLD_DEFAULT, "sudo_getgrnam");
101 if (handle != NULL) {
102 sysgroup_getgrnam = (sysgroup_getgrnam_t)handle;
104 sysgroup_getgrnam = (sysgroup_getgrnam_t)getgrnam;
108 handle = dlsym(RTLD_DEFAULT, "sudo_getgrgid");
109 if (handle != NULL) {
110 sysgroup_getgrgid = (sysgroup_getgrgid_t)handle;
112 sysgroup_getgrgid = (sysgroup_getgrgid_t)getgrgid;
116 handle = dlsym(RTLD_DEFAULT, "sudo_gr_delref");
118 sysgroup_gr_delref = (sysgroup_gr_delref_t)handle;
127 sysgroup_cleanup(void)
134 * Returns true if "user" is a member of "group", else false.
137 sysgroup_query(const char *user, const char *group, const struct passwd *pwd)
139 char **member, *ep = '\0';
142 grp = sysgroup_getgrnam(group);
143 if (grp == NULL && group[0] == '#' && group[1] != '\0') {
144 long lval = strtol(group + 1, &ep, 10);
146 if ((lval != LONG_MAX && lval != LONG_MIN) || errno != ERANGE)
147 grp = sysgroup_getgrgid((gid_t)lval);
151 for (member = grp->gr_mem; *member != NULL; member++) {
152 if (strcasecmp(user, *member) == 0) {
153 if (sysgroup_gr_delref)
154 sysgroup_gr_delref(grp);
158 if (sysgroup_gr_delref)
159 sysgroup_gr_delref(grp);
165 __dso_public struct sudoers_group_plugin group_plugin = {