Annotation of embedaddon/sudo/plugins/sample_group/sample_group.c, revision 1.1.1.2
1.1 misho 1: /*
2: * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
3: *
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.
7: *
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.
15: */
16:
17: #include <config.h>
18:
19: #include <sys/types.h>
20: #include <sys/param.h>
21: #include <sys/stat.h>
22:
23: #include <stdio.h>
24: #ifdef STDC_HEADERS
25: # include <stdlib.h>
26: # include <stddef.h>
27: #else
28: # ifdef HAVE_STDLIB_H
29: # include <stdlib.h>
30: # endif
31: #endif /* STDC_HEADERS */
1.1.1.2 ! misho 32: #ifdef HAVE_STDBOOL_H
! 33: # include <stdbool.h>
! 34: #else
! 35: # include "compat/stdbool.h"
! 36: #endif /* HAVE_STDBOOL_H */
1.1 misho 37: #ifdef HAVE_STRING_H
38: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
39: # include <memory.h>
40: # endif
41: # include <string.h>
42: #endif /* HAVE_STRING_H */
43: #ifdef HAVE_STRINGS_H
44: # include <strings.h>
45: #endif /* HAVE_STRINGS_H */
46: #ifdef HAVE_UNISTD_H
47: # include <unistd.h>
48: #endif /* HAVE_UNISTD_H */
49: #include <ctype.h>
50: #include <errno.h>
51: #include <fcntl.h>
52: #include <limits.h>
53: #include <grp.h>
54: #include <pwd.h>
55:
56: #include "sudo_plugin.h"
57: #include "missing.h"
58:
59: /*
60: * Sample sudoers group plugin that uses an extra group file with the
61: * same format as /etc/group.
62: */
63:
64: static sudo_printf_t sudo_log;
65:
66: extern void mysetgrfile(const char *);
67: extern void mysetgrent(void);
68: extern void myendgrent(void);
69: extern struct group *mygetgrnam(const char *);
70:
71: static int
72: sample_init(int version, sudo_printf_t sudo_printf, char *const argv[])
73: {
74: struct stat sb;
75:
76: sudo_log = sudo_printf;
77:
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);
1.1.1.2 ! misho 83: return -1;
1.1 misho 84: }
85:
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");
1.1.1.2 ! misho 90: return -1;
1.1 misho 91: }
92: if (stat(argv[0], &sb) != 0) {
93: sudo_log(SUDO_CONV_ERROR_MSG,
94: "sample_group: %s: %s\n", argv[0], strerror(errno));
1.1.1.2 ! misho 95: return -1;
1.1 misho 96: }
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]);
1.1.1.2 ! misho 100: return -1;
1.1 misho 101: }
102:
103: mysetgrfile(argv[0]);
104: mysetgrent();
105:
1.1.1.2 ! misho 106: return true;
1.1 misho 107: }
108:
109: static void
110: sample_cleanup(void)
111: {
112: myendgrent();
113: }
114:
115: /*
1.1.1.2 ! misho 116: * Returns true if "user" is a member of "group", else false.
1.1 misho 117: */
118: static int
119: sample_query(const char *user, const char *group, const struct passwd *pwd)
120: {
121: struct group *grp;
122: char **member;
123:
124: grp = mygetgrnam(group);
125: if (grp != NULL) {
126: for (member = grp->gr_mem; *member != NULL; member++) {
127: if (strcasecmp(user, *member) == 0)
1.1.1.2 ! misho 128: return true;
1.1 misho 129: }
130: }
131:
1.1.1.2 ! misho 132: return false;
1.1 misho 133: }
134:
135: struct sudoers_group_plugin group_plugin = {
136: GROUP_API_VERSION,
137: sample_init,
138: sample_cleanup,
139: sample_query
140: };
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>