summaryrefslogtreecommitdiff
path: root/usr/src/lib/nsswitch/nisplus/common/getgrent.c
blob: 9f1e78a89c0d7b12e37e3cc87fff3bd2d5a85537 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * nisplus/getgrent.c -- NIS+ backend for nsswitch "group" database
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <grp.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include "nisplus_common.h"
#include "nisplus_tables.h"

struct memdata {
	nss_status_t	nss_err;
	const	char *uname;
	size_t	unamelen;
	gid_t	*gid_array;
	int	numgids;
	int	maxgids;
};

extern uint_t __nis_force_hard_lookups;

extern nis_result *__nis_list_localcb(nis_name, uint_t, int (*)(), void *);
static int gr_cback();

static nss_status_t netid_lookup(struct memdata *, nisplus_backend_ptr_t);
static int netid_cback(nis_name, nis_object*, struct memdata *);

static nss_status_t
getbynam(be, a)
	nisplus_backend_ptr_t	be;
	void			*a;
{
	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;

	return (_nss_nisplus_lookup(be, argp, GR_TAG_NAME, argp->key.name));
}

static nss_status_t
getbygid(be, a)
	nisplus_backend_ptr_t	be;
	void			*a;
{
	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
	char			gidstr[12];	/* More than enough */

	(void) snprintf(gidstr, 12, "%u", argp->key.gid);
	return (_nss_nisplus_lookup(be, argp, GR_TAG_GID, gidstr));
}

static nss_status_t
getbymember(be, a)
	nisplus_backend_ptr_t	be;
	void			*a;
{
	struct	nss_groupsbymem  *argp = (struct nss_groupsbymem *)a;
	struct	memdata grdata;
	nis_result	*r;
	char buf[NIS_MAXNAMELEN];

	if (strcmp(argp->username, "") == 0 ||
	    strcmp(argp->username, "root") == 0) {
		/*
		 * Assume that "root" can only sensibly be in /etc/group,
		 *   not in NIS or NIS+
		 * If we don't do this, a hung name-service may cause
		 *   a root login or su to hang.
		 * 1251680: Also, we should not allow empty strings to be
		 *   passed to NIS+ backend or application will dump core.
		 */
		return (NSS_NOTFOUND);
	}

	grdata.uname	= argp->username;
	grdata.unamelen	= strlen(argp->username);
	grdata.gid_array = argp->gid_array;
	grdata.maxgids	= argp->maxgids;
	grdata.numgids	= argp->numgids;

	/*
	 * If force_slow_way is not set then we can use the local creds
	 * for the group information.  This assumes that there are admin
	 * procedures in place to keep the local creds and group table
	 * in sync with each other.
	 */
	if (argp->force_slow_way != 1) {
		switch (netid_lookup(&grdata, be)) {
		case NSS_SUCCESS:
			/*
			 * Return SUCESS only if array is full.
			 * Explained in <nss_dbdefs.h>.
			 */
			argp->numgids = grdata.numgids;
			return ((argp->numgids == argp->maxgids)
				? NSS_SUCCESS
				: NSS_NOTFOUND);
		case NSS_NOTFOUND:
		case NSS_UNAVAIL:
			/*
			 * Failover to group map seach if no luck with local
			 * creds (netid).
			 */
			break;
		case NSS_TRYAGAIN:
			return (NSS_TRYAGAIN);
		}
	}

	/*
	 *  Newer versions of rpc.nisd will let us do a "multivalue" search
	 *  on the members column of the group table.  The server will return
	 *  any entries that have the member name listed somewhere in the
	 *  list of members.  Since that is fairly efficient, we try that
	 *  first.  If the server returns NIS_BADATTRIBUTE, then it doesn't
	 *  support this feature and we fail over to the slow way of
	 *  dumping all of the group entries and searching from the member
	 *  name on our own.
	 */
	(void) snprintf(buf, NIS_MAXNAMELEN, "[members=%s],%s",
			argp->username, be->table_name);
	r = __nis_list_localcb(buf, NIS_LIST_COMMON | ALL_RESULTS |
			__nis_force_hard_lookups, gr_cback, &grdata);
	if (r && r->status != NIS_BADATTRIBUTE) {
		if (r)
			nis_freeresult(r);
		argp->numgids = grdata.numgids;
		return (NSS_SUCCESS);
	}
	r = __nis_list_localcb(be->table_name, NIS_LIST_COMMON | ALL_RESULTS |
				__nis_force_hard_lookups, gr_cback, &grdata);
	if (r == 0)
		return (NSS_NOTFOUND);

	nis_freeresult(r);
	argp->numgids = grdata.numgids;
	return (NSS_SUCCESS);
}


/*
 * convert the nisplus object into files format
 * Returns NSS_STR_PARSE_{SUCCESS, ERANGE, PARSE}
 *
 * This routine does not tolerate non-numeric gr_gid. It
 * will immediately flag a PARSE error and return.
 */
/*ARGSUSED*/
static int
nis_object2str(nobj, obj, be, argp)
	int			nobj;
	nis_object		*obj;
	nisplus_backend_ptr_t	be;
	nss_XbyY_args_t		*argp;
{
	char			*buffer, *name, *passwd, *gid, *members;
	int			buflen, namelen, passwdlen, gidlen, memberslen;
	char			*endnum;
	struct entry_col	*ecol;

	/*
	 * If we got more than one nis_object, we just ignore it.
	 * Although it should never have happened.
	 *
	 * ASSUMPTION: All the columns in the NIS+ tables are
	 * null terminated.
	 */

	if (obj->zo_data.zo_type != NIS_ENTRY_OBJ ||
		obj->EN_data.en_cols.en_cols_len < GR_COL) {
		/* namespace/table/object is curdled */
		return (NSS_STR_PARSE_PARSE);
	}
	ecol = obj->EN_data.en_cols.en_cols_val;

	/* name: group name */
	__NISPLUS_GETCOL_OR_RETURN(ecol, GR_NDX_NAME, namelen, name);

	/*
	 * passwd: group passwd
	 * empty password ("") is gladly accepted.
	 */
	__NISPLUS_GETCOL_OR_EMPTY(ecol, GR_NDX_PASSWD, passwdlen, passwd);

	/* gid: group id */
	__NISPLUS_GETCOL_OR_RETURN(ecol, GR_NDX_GID, gidlen, gid);
	(void) strtol(gid, &endnum, 10);
	if (*endnum != 0 || gid == endnum)
		return (NSS_STR_PARSE_PARSE);

	/* members: gid list */
	__NISPLUS_GETCOL_OR_EMPTY(ecol, GR_NDX_MEM, memberslen, members);

	buflen = namelen + passwdlen + gidlen + memberslen + 4;
	if (argp->buf.result != NULL) {
		if ((be->buffer = calloc(1, buflen)) == NULL)
			return (NSS_STR_PARSE_PARSE);
		/* exclude the trailing null from length */
		be->buflen = buflen - 1;
		buffer = be->buffer;
	} else {
		if (buflen > argp->buf.buflen)
			return (NSS_STR_PARSE_ERANGE);
		buflen = argp->buf.buflen;
		buffer = argp->buf.buffer;
		(void) memset(buffer, 0, buflen);
	}
	(void) snprintf(buffer, buflen, "%s:%s:%s:%s",
			name, passwd, gid, members);
#ifdef DEBUG
	(void) fprintf(stdout, "group [%s]\n", buffer);
	(void) fflush(stdout);
#endif  /* DEBUG */
	return (NSS_STR_PARSE_SUCCESS);
}

static nisplus_backend_op_t gr_ops[] = {
	_nss_nisplus_destr,
	_nss_nisplus_endent,
	_nss_nisplus_setent,
	_nss_nisplus_getent,
	getbynam,
	getbygid,
	getbymember
};

/*ARGSUSED*/
nss_backend_t *
_nss_nisplus_group_constr(dummy1, dummy2, dummy3)
	const char	*dummy1, *dummy2, *dummy3;
{
	return (_nss_nisplus_constr(gr_ops,
				    sizeof (gr_ops) / sizeof (gr_ops[0]),
				    GR_TBLNAME, nis_object2str));
}

#define	NEXT	0
#define	DONE	1	/* Stop callback iteration */

/*ARGSUSED*/
static int
gr_cback(objname, obj, g)
	nis_name	objname;
	nis_object	*obj;
	struct memdata	*g;
{
	struct entry_col *ecol;
	char		*val;
	int		len;
	char		*p;
	char		*endp;
	int		i;
	gid_t		gid;

	if (obj->zo_data.zo_type != NIS_ENTRY_OBJ ||
	    /* ==== should check entry type? */
	    obj->EN_data.en_cols.en_cols_len < GR_COL) {
		/*
		 * We've found one bad entry, so throw up our hands
		 *   and say that we don't like any of the entries (?!)
		 */
		g->nss_err = NSS_NOTFOUND;
		return (DONE);
	}

	ecol = obj->EN_data.en_cols.en_cols_val;
	EC_SET(ecol, GR_NDX_MEM, len, val);
	if (len == 0) {
		return (NEXT);
	}
	for (p = val;  (p = strstr(p, g->uname)) != 0;  p++) {
		if ((p == val || p[-1] == ',') &&
		    (p[g->unamelen] == ',' || p[g->unamelen] == '\0')) {
			/* Found uname */
			break;
		}
	}
	if (p == 0) {
		return (NEXT);
	}

	EC_SET(ecol, GR_NDX_GID, len, val);
	if (len == 0) {
		return (NEXT);	/* Actually pretty curdled, but not enough */
				/*   for us to abort the callback sequence */
	}
	val[len - 1] = '\0';
	gid = strtol(val, &endp, 10);
	if (*endp != '\0') {
		return (NEXT);	/* Again, somewhat curdled */
	}
	i = g->numgids;
	while (i-- > 0) {
		if (g->gid_array[i] == gid) {
			return (NEXT);	/* We've already got it, presumably */
					/*   from another name service (!?) */
		}
	}
	if (g->numgids < g->maxgids) {
		g->gid_array[g->numgids++] = gid;
		return (NEXT);
	} else {
		return (DONE);	/* Filled up gid_array, so quit iterating */
	}
}

static nss_status_t
netid_lookup(struct memdata *grdata, nisplus_backend_ptr_t be)
{
	struct passwd	pw;
	char		pwbuf[NSS_BUFLEN_PASSWD];
	nis_result	*r;
	char		buf[NIS_MAXNAMELEN];

	/*
	 * We need to get the users uid given their name as we can't
	 * assume that their NIS+ principle name is username.`domainname`
	 *
	 * We also need to workout which cred.org_dir table to use but the
	 * backend pointer (be) we have is to a group.org_dir table.
	 */

	if ((getpwnam_r(grdata->uname, &pw, pwbuf, sizeof (pwbuf)) == NULL) ||
	    (pw.pw_uid == 0)) {
		return (NSS_NOTFOUND);
	}

	(void) snprintf(buf, NIS_MAXNAMELEN,
	    "[auth_name=%d,auth_type=LOCAL],cred.%s", pw.pw_uid,
	    nis_domain_of(be->table_name));

	/*
	 * NOTE: We are explicitly NOT saying ALL_RESULTS here because
	 * the cred table must NEVER be linked or pathed this is documented
	 * in the Answerbook.
	 */
	r = __nis_list_localcb(buf,
			NIS_LIST_COMMON | __nis_force_hard_lookups,
			netid_cback, grdata);

	if (r == 0)
		return (NSS_NOTFOUND);

	switch (r->status) {
	case NIS_SUCCESS:
	case NIS_CBRESULTS:
		nis_freeresult(r);
		return (NSS_SUCCESS);
	case NIS_TRYAGAIN:
		return (NSS_TRYAGAIN);
	case NIS_UNAVAIL:
		return (NSS_UNAVAIL);
	case NIS_NOTFOUND:
		return (NSS_NOTFOUND);
	default:
		return (NSS_NOTFOUND);
	}
}

/*ARGSUSED*/
static int
netid_cback(nis_name objname, nis_object *obj, struct memdata *g)
{
	struct entry_col *ecol;
	char		*val;
	char		*val_next;
	int		vallen;
	int		i;
	gid_t		gid;
	long		value;

	if (obj->zo_data.zo_type != NIS_ENTRY_OBJ) {
		/*
		 * Badly formed LOCAL cred entry
		 */
		g->nss_err = NSS_NOTFOUND;
		return (DONE);
	}

	ecol = obj->EN_data.en_cols.en_cols_val;
	EC_SET(ecol, CRED_NDX_GROUPLIST, vallen, val);
	if (vallen == 0) {
		return (NEXT);
	}

	/* val should now point to a comma-separated list of gids */

	while (*val != '\0') {
		errno = 0;
		value = strtol(val, &val_next, 10);

		if (val == val_next) {
			return (NSS_STR_PARSE_PARSE);
		} else if ((value == LONG_MAX && errno == ERANGE) ||
		    (ulong_t)value > UINT_MAX) {
			return (NSS_STR_PARSE_ERANGE);
		}

		gid = (gid_t)value;
		if (g->numgids < g->maxgids) {
			i = g->numgids;
			while (i-- > 0)	 {
				if (g->gid_array[i] == gid)
					goto do_next_entry;
			}
			g->gid_array[g->numgids++] = gid;
		} else {
			return (DONE);
		}
do_next_entry:
		val = val_next;
		if (*val == ',') {
			val++;
		}
	}
	if (g->numgids < g->maxgids) {
		return (NEXT);
	} else {
		return (DONE);
	}
}