xref: /freebsd/usr.sbin/nscd/agents/group.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 
31 #include <assert.h>
32 #include <grp.h>
33 #include <nsswitch.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "../debug.h"
38 #include "group.h"
39 
40 static int group_marshal_func(struct group *, char *, size_t *);
41 static int group_lookup_func(const char *, size_t, char **, size_t *);
42 static void *group_mp_init_func(void);
43 static int group_mp_lookup_func(char **, size_t *, void *);
44 static void group_mp_destroy_func(void *);
45 
46 static int
47 group_marshal_func(struct group *grp, char *buffer, size_t *buffer_size)
48 {
49 	struct group new_grp;
50 	size_t desired_size, size, mem_size;
51 	char *p, **mem;
52 
53 	TRACE_IN(group_marshal_func);
54 	desired_size = ALIGNBYTES + sizeof(struct group) + sizeof(char *);
55 
56 	if (grp->gr_name != NULL)
57 		desired_size += strlen(grp->gr_name) + 1;
58 	if (grp->gr_passwd != NULL)
59 		desired_size += strlen(grp->gr_passwd) + 1;
60 
61 	if (grp->gr_mem != NULL) {
62 		mem_size = 0;
63 		for (mem = grp->gr_mem; *mem; ++mem) {
64 			desired_size += strlen(*mem) + 1;
65 			++mem_size;
66 		}
67 
68 		desired_size += ALIGNBYTES + (mem_size + 1) * sizeof(char *);
69 	}
70 
71 	if ((desired_size > *buffer_size) || (buffer == NULL)) {
72 		*buffer_size = desired_size;
73 		TRACE_OUT(group_marshal_func);
74 		return (NS_RETURN);
75 	}
76 
77 	memcpy(&new_grp, grp, sizeof(struct group));
78 	memset(buffer, 0, desired_size);
79 
80 	*buffer_size = desired_size;
81 	p = buffer + sizeof(struct group) + sizeof(char *);
82 	memcpy(buffer + sizeof(struct group), &p, sizeof(char *));
83 	p = (char *)ALIGN(p);
84 
85 	if (new_grp.gr_name != NULL) {
86 		size = strlen(new_grp.gr_name);
87 		memcpy(p, new_grp.gr_name, size);
88 		new_grp.gr_name = p;
89 		p += size + 1;
90 	}
91 
92 	if (new_grp.gr_passwd != NULL) {
93 		size = strlen(new_grp.gr_passwd);
94 		memcpy(p, new_grp.gr_passwd, size);
95 		new_grp.gr_passwd = p;
96 		p += size + 1;
97 	}
98 
99 	if (new_grp.gr_mem != NULL) {
100 		p = (char *)ALIGN(p);
101 		memcpy(p, new_grp.gr_mem, sizeof(char *) * mem_size);
102 		new_grp.gr_mem = (char **)p;
103 		p += sizeof(char *) * (mem_size + 1);
104 
105 		for (mem = new_grp.gr_mem; *mem; ++mem) {
106 			size = strlen(*mem);
107 			memcpy(p, *mem, size);
108 			*mem = p;
109 			p += size + 1;
110 		}
111 	}
112 
113 	memcpy(buffer, &new_grp, sizeof(struct group));
114 	TRACE_OUT(group_marshal_func);
115 	return (NS_SUCCESS);
116 }
117 
118 static int
119 group_lookup_func(const char *key, size_t key_size, char **buffer,
120 	size_t *buffer_size)
121 {
122 	enum nss_lookup_type lookup_type;
123 	char	*name;
124 	size_t	size;
125 	gid_t	gid;
126 
127 	struct group *result;
128 
129 	TRACE_IN(group_lookup_func);
130 	assert(buffer != NULL);
131 	assert(buffer_size != NULL);
132 
133 	if (key_size < sizeof(enum nss_lookup_type)) {
134 		TRACE_OUT(group_lookup_func);
135 		return (NS_UNAVAIL);
136 	}
137 	memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
138 
139 	switch (lookup_type) {
140 	case nss_lt_name:
141 		size = key_size - sizeof(enum nss_lookup_type)	+ 1;
142 		name = calloc(1, size);
143 		assert(name != NULL);
144 		memcpy(name, key + sizeof(enum nss_lookup_type), size - 1);
145 		break;
146 	case nss_lt_id:
147 		if (key_size < sizeof(enum nss_lookup_type) +
148 			sizeof(gid_t)) {
149 			TRACE_OUT(passwd_lookup_func);
150 			return (NS_UNAVAIL);
151 		}
152 
153 		memcpy(&gid, key + sizeof(enum nss_lookup_type), sizeof(gid_t));
154 		break;
155 	default:
156 		TRACE_OUT(group_lookup_func);
157 		return (NS_UNAVAIL);
158 	}
159 
160 	switch (lookup_type) {
161 	case nss_lt_name:
162 		TRACE_STR(name);
163 		result = getgrnam(name);
164 		free(name);
165 		break;
166 	case nss_lt_id:
167 		result = getgrgid(gid);
168 		break;
169 	default:
170 		/* SHOULD NOT BE REACHED */
171 		break;
172 	}
173 
174 	if (result != NULL) {
175 		group_marshal_func(result, NULL, buffer_size);
176 		*buffer = malloc(*buffer_size);
177 		assert(*buffer != NULL);
178 		group_marshal_func(result, *buffer, buffer_size);
179 	}
180 
181 	TRACE_OUT(group_lookup_func);
182 	return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
183 }
184 
185 static void *
186 group_mp_init_func(void)
187 {
188 	TRACE_IN(group_mp_init_func);
189 	setgrent();
190 	TRACE_OUT(group_mp_init_func);
191 
192 	return (NULL);
193 }
194 
195 static int
196 group_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
197 {
198 	struct group *result;
199 
200 	TRACE_IN(group_mp_lookup_func);
201 	result = getgrent();
202 	if (result != NULL) {
203 		group_marshal_func(result, NULL, buffer_size);
204 		*buffer = malloc(*buffer_size);
205 		assert(*buffer != NULL);
206 		group_marshal_func(result, *buffer, buffer_size);
207 	}
208 
209 	TRACE_OUT(group_mp_lookup_func);
210 	return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
211 }
212 
213 static void
214 group_mp_destroy_func(void *mdata)
215 {
216 	TRACE_IN(group_mp_destroy_func);
217 	TRACE_OUT(group_mp_destroy_func);
218 }
219 
220 struct agent *
221 init_group_agent(void)
222 {
223 	struct common_agent	*retval;
224 
225 	TRACE_IN(init_group_agent);
226 	retval = calloc(1, sizeof(*retval));
227 	assert(retval != NULL);
228 
229 	retval->parent.name = strdup("group");
230 	assert(retval->parent.name != NULL);
231 
232 	retval->parent.type = COMMON_AGENT;
233 	retval->lookup_func = group_lookup_func;
234 
235 	TRACE_OUT(init_group_agent);
236 	return ((struct agent *)retval);
237 }
238 
239 struct agent *
240 init_group_mp_agent(void)
241 {
242 	struct multipart_agent	*retval;
243 
244 	TRACE_IN(init_group_mp_agent);
245 	retval = calloc(1,
246 		sizeof(*retval));
247 	assert(retval != NULL);
248 
249 	retval->parent.name = strdup("group");
250 	retval->parent.type = MULTIPART_AGENT;
251 	retval->mp_init_func = group_mp_init_func;
252 	retval->mp_lookup_func = group_mp_lookup_func;
253 	retval->mp_destroy_func = group_mp_destroy_func;
254 	assert(retval->parent.name != NULL);
255 
256 	TRACE_OUT(init_group_mp_agent);
257 	return ((struct agent *)retval);
258 }
259