2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
42 static int group_marshal_func(struct group *, char *, size_t *);
43 static int group_lookup_func(const char *, size_t, char **, size_t *);
44 static void *group_mp_init_func(void);
45 static int group_mp_lookup_func(char **, size_t *, void *);
46 static void group_mp_destroy_func(void *);
49 group_marshal_func(struct group *grp, char *buffer, size_t *buffer_size)
52 size_t desired_size, size, mem_size;
55 TRACE_IN(group_marshal_func);
56 desired_size = ALIGNBYTES + sizeof(struct group) + sizeof(char *);
58 if (grp->gr_name != NULL)
59 desired_size += strlen(grp->gr_name) + 1;
60 if (grp->gr_passwd != NULL)
61 desired_size += strlen(grp->gr_passwd) + 1;
63 if (grp->gr_mem != NULL) {
65 for (mem = grp->gr_mem; *mem; ++mem) {
66 desired_size += strlen(*mem) + 1;
70 desired_size += ALIGNBYTES + (mem_size + 1) * sizeof(char *);
73 if ((desired_size > *buffer_size) || (buffer == NULL)) {
74 *buffer_size = desired_size;
75 TRACE_OUT(group_marshal_func);
79 memcpy(&new_grp, grp, sizeof(struct group));
80 memset(buffer, 0, desired_size);
82 *buffer_size = desired_size;
83 p = buffer + sizeof(struct group) + sizeof(char *);
84 memcpy(buffer + sizeof(struct group), &p, sizeof(char *));
87 if (new_grp.gr_name != NULL) {
88 size = strlen(new_grp.gr_name);
89 memcpy(p, new_grp.gr_name, size);
94 if (new_grp.gr_passwd != NULL) {
95 size = strlen(new_grp.gr_passwd);
96 memcpy(p, new_grp.gr_passwd, size);
97 new_grp.gr_passwd = p;
101 if (new_grp.gr_mem != NULL) {
102 p = (char *)ALIGN(p);
103 memcpy(p, new_grp.gr_mem, sizeof(char *) * mem_size);
104 new_grp.gr_mem = (char **)p;
105 p += sizeof(char *) * (mem_size + 1);
107 for (mem = new_grp.gr_mem; *mem; ++mem) {
109 memcpy(p, *mem, size);
115 memcpy(buffer, &new_grp, sizeof(struct group));
116 TRACE_OUT(group_marshal_func);
121 group_lookup_func(const char *key, size_t key_size, char **buffer,
124 enum nss_lookup_type lookup_type;
129 struct group *result;
131 TRACE_IN(group_lookup_func);
132 assert(buffer != NULL);
133 assert(buffer_size != NULL);
135 if (key_size < sizeof(enum nss_lookup_type)) {
136 TRACE_OUT(group_lookup_func);
139 memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
141 switch (lookup_type) {
143 size = key_size - sizeof(enum nss_lookup_type) + 1;
144 name = calloc(1, size);
145 assert(name != NULL);
146 memcpy(name, key + sizeof(enum nss_lookup_type), size - 1);
149 if (key_size < sizeof(enum nss_lookup_type) +
151 TRACE_OUT(passwd_lookup_func);
155 memcpy(&gid, key + sizeof(enum nss_lookup_type), sizeof(gid_t));
158 TRACE_OUT(group_lookup_func);
162 switch (lookup_type) {
165 result = getgrnam(name);
169 result = getgrgid(gid);
172 /* SHOULD NOT BE REACHED */
176 if (result != NULL) {
177 group_marshal_func(result, NULL, buffer_size);
178 *buffer = malloc(*buffer_size);
179 assert(*buffer != NULL);
180 group_marshal_func(result, *buffer, buffer_size);
183 TRACE_OUT(group_lookup_func);
184 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
188 group_mp_init_func(void)
190 TRACE_IN(group_mp_init_func);
192 TRACE_OUT(group_mp_init_func);
198 group_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
200 struct group *result;
202 TRACE_IN(group_mp_lookup_func);
204 if (result != NULL) {
205 group_marshal_func(result, NULL, buffer_size);
206 *buffer = malloc(*buffer_size);
207 assert(*buffer != NULL);
208 group_marshal_func(result, *buffer, buffer_size);
211 TRACE_OUT(group_mp_lookup_func);
212 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
216 group_mp_destroy_func(void *mdata)
218 TRACE_IN(group_mp_destroy_func);
219 TRACE_OUT(group_mp_destroy_func);
223 init_group_agent(void)
225 struct common_agent *retval;
227 TRACE_IN(init_group_agent);
228 retval = calloc(1, sizeof(*retval));
229 assert(retval != NULL);
231 retval->parent.name = strdup("group");
232 assert(retval->parent.name != NULL);
234 retval->parent.type = COMMON_AGENT;
235 retval->lookup_func = group_lookup_func;
237 TRACE_OUT(init_group_agent);
238 return ((struct agent *)retval);
242 init_group_mp_agent(void)
244 struct multipart_agent *retval;
246 TRACE_IN(init_group_mp_agent);
249 assert(retval != NULL);
251 retval->parent.name = strdup("group");
252 retval->parent.type = MULTIPART_AGENT;
253 retval->mp_init_func = group_mp_init_func;
254 retval->mp_lookup_func = group_mp_lookup_func;
255 retval->mp_destroy_func = group_mp_destroy_func;
256 assert(retval->parent.name != NULL);
258 TRACE_OUT(init_group_mp_agent);
259 return ((struct agent *)retval);