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