Merge from vendor branch TCSH:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / irp_gr.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright(c) 1996, 1998 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: irp_gr.c,v 1.2.206.1 2004/03/09 08:33:36 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 /* extern */
23
24 #include "port_before.h"
25
26 #ifndef WANT_IRS_PW
27 static int __bind_irs_gr_unneeded;
28 #else
29
30 #include <syslog.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <grp.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <syslog.h>
42
43 #include <irs.h>
44 #include <irp.h>
45 #include <isc/memcluster.h>
46 #include <isc/irpmarshall.h>
47
48 #include "irs_p.h"
49 #include "lcl_p.h"
50 #include "irp_p.h"
51
52 #include "port_after.h"
53
54
55 /* Types. */
56
57 /*
58  * Module for the getnetgrent(3) family to use when connected to a
59  * remote irp daemon.
60  *
61  * See irpd.c for justification of caching done here.
62  *
63  */
64
65 struct pvt {
66         struct irp_p   *girpdata;       /* global IRP data */
67         int             warned;
68         struct group    group;
69 };
70
71 /* Forward. */
72
73 static void             gr_close(struct irs_gr *);
74 static struct group *   gr_next(struct irs_gr *);
75 static struct group *   gr_byname(struct irs_gr *, const char *);
76 static struct group *   gr_bygid(struct irs_gr *, gid_t);
77 static void             gr_rewind(struct irs_gr *);
78 static void             gr_minimize(struct irs_gr *);
79
80 /* Private */
81 static void             free_group(struct group *gr);
82
83
84 /* Public. */
85
86
87
88
89
90 /*
91  * struct irs_gr * irs_irp_gr(struct irs_acc *this)
92  *
93  * Notes:
94  *
95  *      Initialize the group sub-module.
96  *
97  * Notes:
98  *
99  *      Module data.
100  *
101  */
102
103 struct irs_gr *
104 irs_irp_gr(struct irs_acc *this) {
105         struct irs_gr *gr;
106         struct pvt *pvt;
107
108         if (!(gr = memget(sizeof *gr))) {
109                 errno = ENOMEM;
110                 return (NULL);
111         }
112         memset(gr, 0x0, sizeof *gr);
113
114         if (!(pvt = memget(sizeof *pvt))) {
115                 memput(gr, sizeof *gr);
116                 errno = ENOMEM;
117                 return (NULL);
118         }
119         memset(pvt, 0x0, sizeof *pvt);
120         pvt->girpdata = this->private;
121
122         gr->private = pvt;
123         gr->close = gr_close;
124         gr->next = gr_next;
125         gr->byname = gr_byname;
126         gr->bygid = gr_bygid;
127         gr->rewind = gr_rewind;
128         gr->list = make_group_list;
129         gr->minimize = gr_minimize;
130         return (gr);
131 }
132
133 /* Methods. */
134
135
136
137 /*
138  * void gr_close(struct irs_gr *this)
139  *
140  * Notes:
141  *
142  *      Close the sub-module.
143  *
144  */
145
146 static void
147 gr_close(struct irs_gr *this) {
148         struct pvt *pvt = (struct pvt *)this->private;
149
150         gr_minimize(this);
151
152         memput(pvt, sizeof *pvt);
153         memput(this, sizeof *this);
154 }
155
156
157
158
159 /*
160  * struct group * gr_next(struct irs_gr *this)
161  *
162  * Notes:
163  *
164  *      Gets the next group out of the cached data and returns it.
165  *
166  */
167
168 static struct group *
169 gr_next(struct irs_gr *this) {
170         struct pvt *pvt = (struct pvt *)this->private;
171         struct group *gr = &pvt->group;
172         char *body;
173         size_t bodylen;
174         int code;
175         char text[256];
176
177         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
178                 return (NULL);
179         }
180
181         if (irs_irp_send_command(pvt->girpdata, "getgrent") != 0) {
182                 return (NULL);
183         }
184
185         if (irs_irp_get_full_response(pvt->girpdata, &code,
186                                       text, sizeof text,
187                                       &body, &bodylen) != 0) {
188                 if (irp_log_errors) {
189                         syslog(LOG_WARNING, "getgrent failed: %s", text);
190                 }
191                 return (NULL);
192         }
193
194         if (code == IRPD_GETGROUP_OK) {
195                 free_group(gr);
196                 if (irp_unmarshall_gr(gr, body) != 0) {
197                         gr = NULL;
198                 }
199         } else {
200                 gr = NULL;
201         }
202
203         if (body != NULL) {
204                 memput(body, bodylen);
205         }
206
207         return (gr);
208 }
209
210
211
212
213
214 /*
215  * struct group * gr_byname(struct irs_gr *this, const char *name)
216  *
217  * Notes:
218  *
219  *      Gets a group by name from irpd and returns it.
220  *
221  */
222
223 static struct group *
224 gr_byname(struct irs_gr *this, const char *name) {
225         struct pvt *pvt = (struct pvt *)this->private;
226         struct group *gr = &pvt->group;
227         char *body;
228         size_t bodylen;
229         int code;
230         char text[256];
231
232
233         if (gr->gr_name != NULL && strcmp(name, gr->gr_name) == 0) {
234                 return (gr);
235         }
236
237         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
238                 return (NULL);
239         }
240
241         if (irs_irp_send_command(pvt->girpdata, "getgrnam %s", name) != 0)
242                 return (NULL);
243
244         if (irs_irp_get_full_response(pvt->girpdata, &code,
245                                       text, sizeof text,
246                                       &body, &bodylen) != 0) {
247                 return (NULL);
248         }
249
250         if (code == IRPD_GETGROUP_OK) {
251                 free_group(gr);
252                 if (irp_unmarshall_gr(gr, body) != 0) {
253                         gr = NULL;
254                 }
255         } else {
256                 gr = NULL;
257         }
258
259         if (body != NULL) {
260                 memput(body, bodylen);
261         }
262
263         return (gr);
264 }
265
266
267
268
269
270 /*
271  * struct group * gr_bygid(struct irs_gr *this, gid_t gid)
272  *
273  * Notes:
274  *
275  *      Gets a group by gid from irpd and returns it.
276  *
277  */
278
279 static struct group *
280 gr_bygid(struct irs_gr *this, gid_t gid) {
281         struct pvt *pvt = (struct pvt *)this->private;
282         struct group *gr = &pvt->group;
283         char *body;
284         size_t bodylen;
285         int code;
286         char text[256];
287
288         if (gr->gr_name != NULL && (gid_t)gr->gr_gid == gid) {
289                 return (gr);
290         }
291
292         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
293                 return (NULL);
294         }
295
296         if (irs_irp_send_command(pvt->girpdata, "getgrgid %d", gid) != 0)
297                 return (NULL);
298
299         if (irs_irp_get_full_response(pvt->girpdata, &code,
300                                       text, sizeof text,
301                                       &body, &bodylen) != 0) {
302                 return (NULL);
303         }
304
305         if (code == IRPD_GETGROUP_OK) {
306                 free_group(gr);
307                 if (irp_unmarshall_gr(gr, body) != 0) {
308                         gr = NULL;
309                 }
310         } else {
311                 gr = NULL;
312         }
313
314         if (body != NULL) {
315                 memput(body, bodylen);
316         }
317
318         return (gr);
319 }
320
321
322
323
324 /*
325  * void gr_rewind(struct irs_gr *this)
326  *
327  */
328
329 static void
330 gr_rewind(struct irs_gr *this) {
331         struct pvt *pvt = (struct pvt *)this->private;
332         char text[256];
333         int code;
334
335         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
336                 return;
337         }
338
339         if (irs_irp_send_command(pvt->girpdata, "setgrent") != 0) {
340                 return;
341         }
342
343         code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
344         if (code != IRPD_GETGROUP_SETOK) {
345                 if (irp_log_errors) {
346                         syslog(LOG_WARNING, "setgrent failed: %s", text);
347                 }
348         }
349
350         return;
351 }
352
353
354
355
356 /*
357  * void gr_minimize(struct irs_gr *this)
358  *
359  * Notes:
360  *
361  *      Frees up cached data and disconnects(if necessary) from the remote.
362  *
363  */
364
365 static void
366 gr_minimize(struct irs_gr *this) {
367         struct pvt *pvt = (struct pvt *)this->private;
368
369         free_group(&pvt->group);
370         irs_irp_disconnect(pvt->girpdata);
371 }
372
373 /* Private. */
374
375
376
377 /*
378  * static void free_group(struct group *gr);
379  *
380  *      Deallocate all the memory irp_unmarshall_gr allocated.
381  *
382  */
383
384 static void
385 free_group(struct group *gr) {
386         char **p;
387
388         if (gr == NULL)
389                 return;
390
391         if (gr->gr_name != NULL)
392                 free(gr->gr_name);
393
394         if (gr->gr_passwd != NULL)
395                 free(gr->gr_passwd);
396
397         for (p = gr->gr_mem ; p != NULL && *p != NULL ; p++)
398                 free(*p);
399
400         if (gr->gr_mem)
401                 free(gr->gr_mem);
402
403         if (p != NULL)
404                 free(p);
405 }
406
407
408 #endif /* WANT_IRS_GR */