2 * Copyright (c) 2010 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>
20 #include <sys/param.h>
31 #endif /* STDC_HEADERS */
35 # include "compat/stdbool.h"
36 #endif /* HAVE_STDBOOL_H */
38 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
42 #endif /* HAVE_STRING_H */
45 #endif /* HAVE_STRINGS_H */
48 #endif /* HAVE_UNISTD_H */
56 #include "sudo_plugin.h"
60 * Sample sudoers group plugin that uses an extra group file with the
61 * same format as /etc/group.
64 static sudo_printf_t sudo_log;
66 extern void mysetgrfile(const char *);
67 extern void mysetgrent(void);
68 extern void myendgrent(void);
69 extern struct group *mygetgrnam(const char *);
72 sample_init(int version, sudo_printf_t sudo_printf, char *const argv[])
76 sudo_log = sudo_printf;
78 if (GROUP_API_VERSION_GET_MAJOR(version) != GROUP_API_VERSION_MAJOR) {
79 sudo_log(SUDO_CONV_ERROR_MSG,
80 "sample_group: incompatible major version %d, expected %d\n",
81 GROUP_API_VERSION_GET_MAJOR(version),
82 GROUP_API_VERSION_MAJOR);
86 /* Sanity check the specified group file. */
87 if (argv == NULL || argv[0] == NULL) {
88 sudo_log(SUDO_CONV_ERROR_MSG,
89 "sample_group: path to group file not specified\n");
92 if (stat(argv[0], &sb) != 0) {
93 sudo_log(SUDO_CONV_ERROR_MSG,
94 "sample_group: %s: %s\n", argv[0], strerror(errno));
97 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
98 sudo_log(SUDO_CONV_ERROR_MSG,
99 "%s must be only be writable by owner\n", argv[0]);
103 mysetgrfile(argv[0]);
116 * Returns true if "user" is a member of "group", else false.
119 sample_query(const char *user, const char *group, const struct passwd *pwd)
124 grp = mygetgrnam(group);
126 for (member = grp->gr_mem; *member != NULL; member++) {
127 if (strcasecmp(user, *member) == 0)
135 struct sudoers_group_plugin group_plugin = {