1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
25*0Sstevel@tonic-gate static const char rcsid[] = "$Id: util.c,v 1.12 2001/05/29 05:49:21 marka Exp $";
26*0Sstevel@tonic-gate #endif
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include "port_before.h"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/socket.h>
32*0Sstevel@tonic-gate #include <netinet/in.h>
33*0Sstevel@tonic-gate #include <arpa/nameser.h>
34*0Sstevel@tonic-gate #include <resolv.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <ctype.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <irs.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include "port_after.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include "irs_p.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #ifdef SPRINTF_CHAR
49*0Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
50*0Sstevel@tonic-gate #else
51*0Sstevel@tonic-gate # define SPRINTF(x) sprintf x
52*0Sstevel@tonic-gate #endif
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate void
55*0Sstevel@tonic-gate map_v4v6_address(const char *src, char *dst) {
56*0Sstevel@tonic-gate 	u_char *p = (u_char *)dst;
57*0Sstevel@tonic-gate 	char tmp[NS_INADDRSZ];
58*0Sstevel@tonic-gate 	int i;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 	/* Stash a temporary copy so our caller can update in place. */
61*0Sstevel@tonic-gate 	memcpy(tmp, src, NS_INADDRSZ);
62*0Sstevel@tonic-gate 	/* Mark this ipv6 addr as a mapped ipv4. */
63*0Sstevel@tonic-gate 	for (i = 0; i < 10; i++)
64*0Sstevel@tonic-gate 		*p++ = 0x00;
65*0Sstevel@tonic-gate 	*p++ = 0xff;
66*0Sstevel@tonic-gate 	*p++ = 0xff;
67*0Sstevel@tonic-gate 	/* Retrieve the saved copy and we're done. */
68*0Sstevel@tonic-gate 	memcpy((void*)p, tmp, NS_INADDRSZ);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate int
72*0Sstevel@tonic-gate make_group_list(struct irs_gr *this, const char *name,
73*0Sstevel@tonic-gate 	gid_t basegid, gid_t *groups, int *ngroups)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	struct group *grp;
76*0Sstevel@tonic-gate 	int i, ng;
77*0Sstevel@tonic-gate 	int ret, maxgroups;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	ret = -1;
80*0Sstevel@tonic-gate 	ng = 0;
81*0Sstevel@tonic-gate 	maxgroups = *ngroups;
82*0Sstevel@tonic-gate 	/*
83*0Sstevel@tonic-gate 	 * When installing primary group, duplicate it;
84*0Sstevel@tonic-gate 	 * the first element of groups is the effective gid
85*0Sstevel@tonic-gate 	 * and will be overwritten when a setgid file is executed.
86*0Sstevel@tonic-gate 	 */
87*0Sstevel@tonic-gate 	if (ng >= maxgroups)
88*0Sstevel@tonic-gate 		goto done;
89*0Sstevel@tonic-gate 	groups[ng++] = basegid;
90*0Sstevel@tonic-gate 	if (ng >= maxgroups)
91*0Sstevel@tonic-gate 		goto done;
92*0Sstevel@tonic-gate 	groups[ng++] = basegid;
93*0Sstevel@tonic-gate 	/*
94*0Sstevel@tonic-gate 	 * Scan the group file to find additional groups.
95*0Sstevel@tonic-gate 	 */
96*0Sstevel@tonic-gate 	(*this->rewind)(this);
97*0Sstevel@tonic-gate 	while ((grp = (*this->next)(this)) != NULL) {
98*0Sstevel@tonic-gate 		if ((gid_t)grp->gr_gid == basegid)
99*0Sstevel@tonic-gate 			continue;
100*0Sstevel@tonic-gate 		for (i = 0; grp->gr_mem[i]; i++) {
101*0Sstevel@tonic-gate 			if (!strcmp(grp->gr_mem[i], name)) {
102*0Sstevel@tonic-gate 				if (ng >= maxgroups)
103*0Sstevel@tonic-gate 					goto done;
104*0Sstevel@tonic-gate 				groups[ng++] = grp->gr_gid;
105*0Sstevel@tonic-gate 				break;
106*0Sstevel@tonic-gate 			}
107*0Sstevel@tonic-gate 		}
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 	ret = 0;
110*0Sstevel@tonic-gate  done:
111*0Sstevel@tonic-gate 	*ngroups = ng;
112*0Sstevel@tonic-gate 	return (ret);
113*0Sstevel@tonic-gate }
114