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 #if defined(LIBC_SCCS) && !defined(lint) 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: util.c,v 1.3 2005/04/27 04:56:34 sra Exp $"; 207c478bd9Sstevel@tonic-gate #endif 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate #include "port_before.h" 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate #include <sys/types.h> 257c478bd9Sstevel@tonic-gate #include <sys/socket.h> 267c478bd9Sstevel@tonic-gate #include <netinet/in.h> 277c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 287c478bd9Sstevel@tonic-gate #include <resolv.h> 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <ctype.h> 317c478bd9Sstevel@tonic-gate #include <errno.h> 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <irs.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include "port_after.h" 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include "irs_p.h" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR 437c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x) 447c478bd9Sstevel@tonic-gate #else 457c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x 467c478bd9Sstevel@tonic-gate #endif 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate void 497c478bd9Sstevel@tonic-gate map_v4v6_address(const char *src, char *dst) { 507c478bd9Sstevel@tonic-gate u_char *p = (u_char *)dst; 517c478bd9Sstevel@tonic-gate char tmp[NS_INADDRSZ]; 527c478bd9Sstevel@tonic-gate int i; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* Stash a temporary copy so our caller can update in place. */ 557c478bd9Sstevel@tonic-gate memcpy(tmp, src, NS_INADDRSZ); 567c478bd9Sstevel@tonic-gate /* Mark this ipv6 addr as a mapped ipv4. */ 577c478bd9Sstevel@tonic-gate for (i = 0; i < 10; i++) 587c478bd9Sstevel@tonic-gate *p++ = 0x00; 597c478bd9Sstevel@tonic-gate *p++ = 0xff; 607c478bd9Sstevel@tonic-gate *p++ = 0xff; 617c478bd9Sstevel@tonic-gate /* Retrieve the saved copy and we're done. */ 627c478bd9Sstevel@tonic-gate memcpy((void*)p, tmp, NS_INADDRSZ); 637c478bd9Sstevel@tonic-gate } 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate int 667c478bd9Sstevel@tonic-gate make_group_list(struct irs_gr *this, const char *name, 677c478bd9Sstevel@tonic-gate gid_t basegid, gid_t *groups, int *ngroups) 687c478bd9Sstevel@tonic-gate { 697c478bd9Sstevel@tonic-gate struct group *grp; 707c478bd9Sstevel@tonic-gate int i, ng; 717c478bd9Sstevel@tonic-gate int ret, maxgroups; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate ret = -1; 747c478bd9Sstevel@tonic-gate ng = 0; 757c478bd9Sstevel@tonic-gate maxgroups = *ngroups; 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * When installing primary group, duplicate it; 787c478bd9Sstevel@tonic-gate * the first element of groups is the effective gid 797c478bd9Sstevel@tonic-gate * and will be overwritten when a setgid file is executed. 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate if (ng >= maxgroups) 827c478bd9Sstevel@tonic-gate goto done; 837c478bd9Sstevel@tonic-gate groups[ng++] = basegid; 847c478bd9Sstevel@tonic-gate if (ng >= maxgroups) 857c478bd9Sstevel@tonic-gate goto done; 867c478bd9Sstevel@tonic-gate groups[ng++] = basegid; 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * Scan the group file to find additional groups. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate (*this->rewind)(this); 917c478bd9Sstevel@tonic-gate while ((grp = (*this->next)(this)) != NULL) { 927c478bd9Sstevel@tonic-gate if ((gid_t)grp->gr_gid == basegid) 937c478bd9Sstevel@tonic-gate continue; 947c478bd9Sstevel@tonic-gate for (i = 0; grp->gr_mem[i]; i++) { 957c478bd9Sstevel@tonic-gate if (!strcmp(grp->gr_mem[i], name)) { 967c478bd9Sstevel@tonic-gate if (ng >= maxgroups) 977c478bd9Sstevel@tonic-gate goto done; 987c478bd9Sstevel@tonic-gate groups[ng++] = grp->gr_gid; 997c478bd9Sstevel@tonic-gate break; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate ret = 0; 1047c478bd9Sstevel@tonic-gate done: 1057c478bd9Sstevel@tonic-gate *ngroups = ng; 1067c478bd9Sstevel@tonic-gate return (ret); 1077c478bd9Sstevel@tonic-gate } 108*9525b14bSRao Shoaib 109*9525b14bSRao Shoaib /*! \file */ 110