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