summaryrefslogtreecommitdiffstats
path: root/contrib/bind/lib/irs/irp_gr.c
blob: c235e19020a92ad7e3d5c319c44368eb81e4ca30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Portions Copyright(c) 1996, 1998 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$Id: irp_gr.c,v 8.3 2001/05/29 05:48:57 marka Exp $";
#endif /* LIBC_SCCS and not lint */

/* extern */

#include "port_before.h"

#ifndef WANT_IRS_PW
static int __bind_irs_gr_unneeded;
#else

#include <syslog.h>
#include <sys/param.h>
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>

#include <irs.h>
#include <irp.h>
#include <isc/memcluster.h>
#include <isc/irpmarshall.h>

#include "irs_p.h"
#include "lcl_p.h"
#include "irp_p.h"

#include "port_after.h"


/* Types. */

/*
 * Module for the getnetgrent(3) family to use when connected to a
 * remote irp daemon.
 *
 * See irpd.c for justification of caching done here.
 *
 */

struct pvt {
	struct irp_p   *girpdata;	/* global IRP data */
	int		warned;
	struct group	group;
};

/* Forward. */

static void		gr_close(struct irs_gr *);
static struct group *	gr_next(struct irs_gr *);
static struct group *	gr_byname(struct irs_gr *, const char *);
static struct group *	gr_bygid(struct irs_gr *, gid_t);
static void		gr_rewind(struct irs_gr *);
static void		gr_minimize(struct irs_gr *);

/* Private */
static void		free_group(struct group *gr);


/* Public. */





/*
 * struct irs_gr * irs_irp_gr(struct irs_acc *this)
 *
 * Notes:
 *
 *	Initialize the group sub-module.
 *
 * Notes:
 *
 *	Module data.
 *
 */

struct irs_gr *
irs_irp_gr(struct irs_acc *this) {
	struct irs_gr *gr;
	struct pvt *pvt;

	if (!(gr = memget(sizeof *gr))) {
		errno = ENOMEM;
		return (NULL);
	}
	memset(gr, 0x0, sizeof *gr);

	if (!(pvt = memget(sizeof *pvt))) {
		memput(gr, sizeof *gr);
		errno = ENOMEM;
		return (NULL);
	}
	memset(pvt, 0x0, sizeof *pvt);
	pvt->girpdata = this->private;

	gr->private = pvt;
	gr->close = gr_close;
	gr->next = gr_next;
	gr->byname = gr_byname;
	gr->bygid = gr_bygid;
	gr->rewind = gr_rewind;
	gr->list = make_group_list;
	gr->minimize = gr_minimize;
	return (gr);
}

/* Methods. */



/*
 * void gr_close(struct irs_gr *this)
 *
 * Notes:
 *
 *	Close the sub-module.
 *
 */

static void
gr_close(struct irs_gr *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	gr_minimize(this);

	memput(pvt, sizeof *pvt);
	memput(this, sizeof *this);
}




/*
 * struct group * gr_next(struct irs_gr *this)
 *
 * Notes:
 *
 *	Gets the next group out of the cached data and returns it.
 *
 */

static struct group *
gr_next(struct irs_gr *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct group *gr = &pvt->group;
	char *body;
	size_t bodylen;
	int code;
	char text[256];

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return (NULL);
	}

	if (irs_irp_send_command(pvt->girpdata, "getgrent") != 0) {
		return (NULL);
	}

	if (irs_irp_get_full_response(pvt->girpdata, &code,
				      text, sizeof text,
				      &body, &bodylen) != 0) {
		if (irp_log_errors) {
			syslog(LOG_WARNING, "getgrent failed: %s", text);
		}
		return (NULL);
	}

	if (code == IRPD_GETGROUP_OK) {
		free_group(gr);
		if (irp_unmarshall_gr(gr, body) != 0) {
			gr = NULL;
		}
	} else {
		gr = NULL;
	}

	if (body != NULL) {
		memput(body, bodylen);
	}

	return (gr);
}





/*
 * struct group * gr_byname(struct irs_gr *this, const char *name)
 *
 * Notes:
 *
 *	Gets a group by name from irpd and returns it.
 *
 */

static struct group *
gr_byname(struct irs_gr *this, const char *name) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct group *gr = &pvt->group;
	char *body;
	size_t bodylen;
	int code;
	char text[256];


	if (gr->gr_name != NULL && strcmp(name, gr->gr_name) == 0) {
		return (gr);
	}

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return (NULL);
	}

	if (irs_irp_send_command(pvt->girpdata, "getgrnam %s", name) != 0)
		return (NULL);

	if (irs_irp_get_full_response(pvt->girpdata, &code,
				      text, sizeof text,
				      &body, &bodylen) != 0) {
		return (NULL);
	}

	if (code == IRPD_GETGROUP_OK) {
		free_group(gr);
		if (irp_unmarshall_gr(gr, body) != 0) {
			gr = NULL;
		}
	} else {
		gr = NULL;
	}

	if (body != NULL) {
		memput(body, bodylen);
	}

	return (gr);
}





/*
 * struct group * gr_bygid(struct irs_gr *this, gid_t gid)
 *
 * Notes:
 *
 *	Gets a group by gid from irpd and returns it.
 *
 */

static struct group *
gr_bygid(struct irs_gr *this, gid_t gid) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct group *gr = &pvt->group;
	char *body;
	size_t bodylen;
	int code;
	char text[256];

	if (gr->gr_name != NULL && (gid_t)gr->gr_gid == gid) {
		return (gr);
	}

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return (NULL);
	}

	if (irs_irp_send_command(pvt->girpdata, "getgrgid %d", gid) != 0)
		return (NULL);

	if (irs_irp_get_full_response(pvt->girpdata, &code,
				      text, sizeof text,
				      &body, &bodylen) != 0) {
		return (NULL);
	}

	if (code == IRPD_GETGROUP_OK) {
		free_group(gr);
		if (irp_unmarshall_gr(gr, body) != 0) {
			gr = NULL;
		}
	} else {
		gr = NULL;
	}

	if (body != NULL) {
		memput(body, bodylen);
	}

	return (gr);
}




/*
 * void gr_rewind(struct irs_gr *this)
 *
 */

static void
gr_rewind(struct irs_gr *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	char text[256];
	int code;

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return;
	}

	if (irs_irp_send_command(pvt->girpdata, "setgrent") != 0) {
		return;
	}

	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
	if (code != IRPD_GETGROUP_SETOK) {
		if (irp_log_errors) {
			syslog(LOG_WARNING, "setgrent failed: %s", text);
		}
	}

	return;
}




/*
 * void gr_minimize(struct irs_gr *this)
 *
 * Notes:
 *
 *	Frees up cached data and disconnects(if necessary) from the remote.
 *
 */

static void
gr_minimize(struct irs_gr *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	free_group(&pvt->group);
	irs_irp_disconnect(pvt->girpdata);
}

/* Private. */



/*
 * static void free_group(struct group *gr);
 *
 *	Deallocate all the memory irp_unmarshall_gr allocated.
 *
 */

static void
free_group(struct group *gr) {
	char **p;

	if (gr == NULL)
		return;

	if (gr->gr_name != NULL)
		free(gr->gr_name);

	if (gr->gr_passwd != NULL)
		free(gr->gr_passwd);

	for (p = gr->gr_mem ; p != NULL && *p != NULL ; p++)
		free(*p);

	if (gr->gr_mem)
		free(gr->gr_mem);

	if (p != NULL)
		free(p);
}


#endif /* WANT_IRS_GR */
OpenPOWER on IntegriCloud