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