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
|
/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1996,1999 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 ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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: nis_ng.c,v 1.3.18.1 2005/04/27 05:01:03 sra Exp $";
#endif
/* Imports */
#include "port_before.h"
#ifndef WANT_IRS_NIS
static int __bind_irs_nis_unneeded;
#else
#include <sys/types.h>
#include <netinet/in.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>
#include <isc/assertions.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#ifdef T_NULL
#undef T_NULL /* Silence re-definition warning of T_NULL. */
#endif
#include <arpa/nameser.h>
#include <resolv.h>
#include <isc/memcluster.h>
#include <irs.h>
#include "port_after.h"
#include "irs_p.h"
#include "nis_p.h"
/* Definitions */
struct tmpgrp {
const char * name;
const char * host;
const char * user;
const char * domain;
struct tmpgrp * next;
};
struct pvt {
char * nis_domain;
struct tmpgrp * tmp;
struct tmpgrp * cur;
char * tmpgroup;
};
enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
static /*const*/ char netgroup_map[] = "netgroup";
/* Forward */
static void ng_close(struct irs_ng *);
static int ng_next(struct irs_ng *, const char **,
const char **, const char **);
static int ng_test(struct irs_ng *,
const char *, const char *,
const char *, const char *);
static void ng_rewind(struct irs_ng *, const char *);
static void ng_minimize(struct irs_ng *);
static void add_group_to_list(struct pvt *, const char *, int);
static void add_tuple_to_list(struct pvt *, const char *, char *);
static void tmpfree(struct pvt *);
/* Public */
struct irs_ng *
irs_nis_ng(struct irs_acc *this) {
struct irs_ng *ng;
struct pvt *pvt;
if (!(ng = memget(sizeof *ng))) {
errno = ENOMEM;
return (NULL);
}
memset(ng, 0x5e, sizeof *ng);
if (!(pvt = memget(sizeof *pvt))) {
memput(ng, sizeof *ng);
errno = ENOMEM;
return (NULL);
}
memset(pvt, 0, sizeof *pvt);
pvt->nis_domain = ((struct nis_p *)this->private)->domain;
ng->private = pvt;
ng->close = ng_close;
ng->next = ng_next;
ng->test = ng_test;
ng->rewind = ng_rewind;
ng->minimize = ng_minimize;
return (ng);
}
/* Methods */
static void
ng_close(struct irs_ng *this) {
struct pvt *pvt = (struct pvt *)this->private;
tmpfree(pvt);
memput(pvt, sizeof *pvt);
memput(this, sizeof *this);
}
static int
ng_next(struct irs_ng *this, const char **host, const char **user, const char **domain) {
struct pvt *pvt = (struct pvt *)this->private;
if (!pvt->cur)
return (0);
*host = pvt->cur->host;
*user = pvt->cur->user;
*domain = pvt->cur->domain;
pvt->cur = pvt->cur->next;
return (1);
}
static int
ng_test(struct irs_ng *this, const char *name,
const char *host, const char *user, const char *domain)
{
struct pvt *pvt = (struct pvt *)this->private;
struct tmpgrp *cur;
tmpfree(pvt);
add_group_to_list(pvt, name, strlen(name));
for (cur = pvt->tmp; cur; cur = cur->next) {
if ((!host || !cur->host || !strcmp(host, cur->host)) &&
(!user || !cur->user || !strcmp(user, cur->user)) &&
(!domain || !cur->domain || !strcmp(domain, cur->domain)))
break;
}
tmpfree(pvt);
return ((cur == NULL) ? 0 : 1);
}
static void
ng_rewind(struct irs_ng *this, const char *name) {
struct pvt *pvt = (struct pvt *)this->private;
/* Either hand back or free the existing list. */
if (pvt->tmpgroup) {
if (pvt->tmp && !strcmp(pvt->tmpgroup, name))
goto reset;
tmpfree(pvt);
}
pvt->tmpgroup = strdup(name);
add_group_to_list(pvt, name, strlen(name));
reset:
pvt->cur = pvt->tmp;
}
static void
ng_minimize(struct irs_ng *this) {
UNUSED(this);
/* NOOP */
}
/* Private */
static void
add_group_to_list(struct pvt *pvt, const char *name, int len) {
char *vdata, *cp, *np;
struct tmpgrp *tmp;
int vlen, r;
char *nametmp;
/* Don't add the same group to the list more than once. */
for (tmp = pvt->tmp; tmp; tmp = tmp->next)
if (!strcmp(tmp->name, name))
return;
DE_CONST(name, nametmp);
r = yp_match(pvt->nis_domain, netgroup_map, nametmp, len,
&vdata, &vlen);
if (r == 0) {
cp = vdata;
if (*cp && cp[strlen(cp)-1] == '\n')
cp[strlen(cp)-1] = '\0';
for ( ; cp; cp = np) {
np = strchr(cp, ' ');
if (np)
*np++ = '\0';
if (*cp == '(')
add_tuple_to_list(pvt, name, cp);
else
add_group_to_list(pvt, cp, strlen(cp));
}
free(vdata);
}
}
static void
add_tuple_to_list(struct pvt *pvt, const char *name, char *cp) {
struct tmpgrp *tmp;
char *tp, *np;
INSIST(*cp++ == '(');
tmp = malloc(sizeof *tmp + strlen(name) + sizeof '\0' +
strlen(cp) - sizeof ')');
if (!tmp)
return;
memset(tmp, 0, sizeof *tmp);
tp = ((char *)tmp) + sizeof *tmp;
/* Name */
strcpy(tp, name);
tmp->name = tp;
tp += strlen(tp) + 1;
/* Host */
if (!(np = strchr(cp, ',')))
goto cleanup;
*np++ = '\0';
strcpy(tp, cp);
tmp->host = tp;
tp += strlen(tp) + 1;
cp = np;
/* User */
if (!(np = strchr(cp, ',')))
goto cleanup;
*np++ = '\0';
strcpy(tp, cp);
tmp->user = tp;
tp += strlen(tp) + 1;
cp = np;
/* Domain */
if (!(np = strchr(cp, ')')))
goto cleanup;
*np++ = '\0';
strcpy(tp, cp);
tmp->domain = tp;
/*
* Empty string in file means wildcard, but
* NULL string in return value means wildcard.
*/
if (!*tmp->host)
tmp->host = NULL;
if (!*tmp->user)
tmp->user = NULL;
if (!*tmp->domain)
tmp->domain = NULL;
/* Add to list (LIFO). */
tmp->next = pvt->tmp;
pvt->tmp = tmp;
return;
cleanup:
free(tmp);
}
static void
tmpfree(struct pvt *pvt) {
struct tmpgrp *cur, *next;
if (pvt->tmpgroup) {
free(pvt->tmpgroup);
pvt->tmpgroup = NULL;
}
for (cur = pvt->tmp; cur; cur = next) {
next = cur->next;
free(cur);
}
pvt->tmp = NULL;
}
#endif /*WANT_IRS_NIS*/
/*! \file */
|