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