17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <sys/types.h>
217c478bd9Sstevel@tonic-gate #include <sys/socket.h>
227c478bd9Sstevel@tonic-gate #include <netinet/in.h>
237c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
247c478bd9Sstevel@tonic-gate #include <resolv.h>
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <ctype.h>
277c478bd9Sstevel@tonic-gate #include <errno.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <irs.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "port_after.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "irs_p.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
397c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
407c478bd9Sstevel@tonic-gate #else
417c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate void
map_v4v6_address(const char * src,char * dst)457c478bd9Sstevel@tonic-gate map_v4v6_address(const char *src, char *dst) {
467c478bd9Sstevel@tonic-gate 	u_char *p = (u_char *)dst;
477c478bd9Sstevel@tonic-gate 	char tmp[NS_INADDRSZ];
487c478bd9Sstevel@tonic-gate 	int i;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	/* Stash a temporary copy so our caller can update in place. */
517c478bd9Sstevel@tonic-gate 	memcpy(tmp, src, NS_INADDRSZ);
527c478bd9Sstevel@tonic-gate 	/* Mark this ipv6 addr as a mapped ipv4. */
537c478bd9Sstevel@tonic-gate 	for (i = 0; i < 10; i++)
547c478bd9Sstevel@tonic-gate 		*p++ = 0x00;
557c478bd9Sstevel@tonic-gate 	*p++ = 0xff;
567c478bd9Sstevel@tonic-gate 	*p++ = 0xff;
577c478bd9Sstevel@tonic-gate 	/* Retrieve the saved copy and we're done. */
587c478bd9Sstevel@tonic-gate 	memcpy((void*)p, tmp, NS_INADDRSZ);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int
make_group_list(struct irs_gr * this,const char * name,gid_t basegid,gid_t * groups,int * ngroups)627c478bd9Sstevel@tonic-gate make_group_list(struct irs_gr *this, const char *name,
637c478bd9Sstevel@tonic-gate 	gid_t basegid, gid_t *groups, int *ngroups)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	struct group *grp;
667c478bd9Sstevel@tonic-gate 	int i, ng;
677c478bd9Sstevel@tonic-gate 	int ret, maxgroups;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	ret = -1;
707c478bd9Sstevel@tonic-gate 	ng = 0;
717c478bd9Sstevel@tonic-gate 	maxgroups = *ngroups;
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * When installing primary group, duplicate it;
747c478bd9Sstevel@tonic-gate 	 * the first element of groups is the effective gid
757c478bd9Sstevel@tonic-gate 	 * and will be overwritten when a setgid file is executed.
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	if (ng >= maxgroups)
787c478bd9Sstevel@tonic-gate 		goto done;
797c478bd9Sstevel@tonic-gate 	groups[ng++] = basegid;
807c478bd9Sstevel@tonic-gate 	if (ng >= maxgroups)
817c478bd9Sstevel@tonic-gate 		goto done;
827c478bd9Sstevel@tonic-gate 	groups[ng++] = basegid;
837c478bd9Sstevel@tonic-gate 	/*
847c478bd9Sstevel@tonic-gate 	 * Scan the group file to find additional groups.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	(*this->rewind)(this);
877c478bd9Sstevel@tonic-gate 	while ((grp = (*this->next)(this)) != NULL) {
887c478bd9Sstevel@tonic-gate 		if ((gid_t)grp->gr_gid == basegid)
897c478bd9Sstevel@tonic-gate 			continue;
907c478bd9Sstevel@tonic-gate 		for (i = 0; grp->gr_mem[i]; i++) {
917c478bd9Sstevel@tonic-gate 			if (!strcmp(grp->gr_mem[i], name)) {
927c478bd9Sstevel@tonic-gate 				if (ng >= maxgroups)
937c478bd9Sstevel@tonic-gate 					goto done;
947c478bd9Sstevel@tonic-gate 				groups[ng++] = grp->gr_gid;
957c478bd9Sstevel@tonic-gate 				break;
967c478bd9Sstevel@tonic-gate 			}
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	ret = 0;
1007c478bd9Sstevel@tonic-gate  done:
1017c478bd9Sstevel@tonic-gate 	*ngroups = ng;
1027c478bd9Sstevel@tonic-gate 	return (ret);
1037c478bd9Sstevel@tonic-gate }
104*9525b14bSRao Shoaib 
105*9525b14bSRao Shoaib /*! \file */
106