1261046Smav/*- 2261046Smav * Copyright (c) 2009, Sun Microsystems, Inc. 3261046Smav * All rights reserved. 426219Swpaul * 5261046Smav * Redistribution and use in source and binary forms, with or without 6261046Smav * modification, are permitted provided that the following conditions are met: 7261046Smav * - Redistributions of source code must retain the above copyright notice, 8261046Smav * this list of conditions and the following disclaimer. 9261046Smav * - Redistributions in binary form must reproduce the above copyright notice, 10261046Smav * this list of conditions and the following disclaimer in the documentation 11261046Smav * and/or other materials provided with the distribution. 12261046Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its 13261046Smav * contributors may be used to endorse or promote products derived 14261046Smav * from this software without specific prior written permission. 15261046Smav * 16261046Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17261046Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18261046Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19261046Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20261046Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21261046Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22261046Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23261046Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24261046Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25261046Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26261046Smav * POSSIBILITY OF SUCH DAMAGE. 2726219Swpaul */ 28136581Sobrien 29136581Sobrien#if defined(LIBC_SCCS) && !defined(lint) 3026219Swpaulstatic char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro"; 3126219Swpaul#endif 3292990Sobrien#include <sys/cdefs.h> 3392990Sobrien__FBSDID("$FreeBSD: releng/10.2/lib/libc/rpc/netname.c 261046 2014-01-22 23:45:27Z mav $"); 3426219Swpaul 3526219Swpaul/* 3626219Swpaul * netname utility routines 3726219Swpaul * convert from unix names to network names and vice-versa 3826219Swpaul * This module is operating system dependent! 3926219Swpaul * What we define here will work with any unix system that has adopted 4026219Swpaul * the sun NIS domain architecture. 4126219Swpaul */ 4226219Swpaul 4374462Salfred#include "namespace.h" 4426219Swpaul#include <sys/param.h> 4526219Swpaul#include <rpc/rpc.h> 4626219Swpaul#include <rpc/rpc_com.h> 4726219Swpaul#ifdef YP 4826219Swpaul#include <rpcsvc/yp_prot.h> 4926219Swpaul#include <rpcsvc/ypclnt.h> 5026219Swpaul#endif 5126219Swpaul#include <ctype.h> 5264242Skris#include <limits.h> 5364242Skris#include <stdio.h> 5426219Swpaul#include <stdlib.h> 5564242Skris#include <string.h> 5626219Swpaul#include <unistd.h> 5774462Salfred#include "un-namespace.h" 5826219Swpaul 5926219Swpaul#ifndef MAXHOSTNAMELEN 6026219Swpaul#define MAXHOSTNAMELEN 256 6126219Swpaul#endif 6226219Swpaul 6364242Skris#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT) 6464242Skris 6564242Skris#define TYPE_SIGNED(type) (((type) -1) < 0) 6664242Skris 6764242Skris/* 6864242Skris** 302 / 1000 is log10(2.0) rounded up. 6964242Skris** Subtract one for the sign bit if the type is signed; 7064242Skris** add one for integer division truncation; 7164242Skris** add one more for a minus sign if the type is signed. 7264242Skris*/ 7364242Skris#define INT_STRLEN_MAXIMUM(type) \ 7464242Skris ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type)) 7564242Skris 7626219Swpaulstatic char *OPSYS = "unix"; 7726219Swpaul 7826219Swpaul/* 7926219Swpaul * Figure out my fully qualified network name 8026219Swpaul */ 8126219Swpaulint 8226219Swpaulgetnetname(name) 8326219Swpaul char name[MAXNETNAMELEN+1]; 8426219Swpaul{ 8526219Swpaul uid_t uid; 8626219Swpaul 8726219Swpaul uid = geteuid(); 8826219Swpaul if (uid == 0) { 8926219Swpaul return (host2netname(name, (char *) NULL, (char *) NULL)); 9026219Swpaul } else { 9126219Swpaul return (user2netname(name, uid, (char *) NULL)); 9226219Swpaul } 9326219Swpaul} 9426219Swpaul 9526219Swpaul 9626219Swpaul/* 9726219Swpaul * Convert unix cred to network-name 9826219Swpaul */ 9926219Swpaulint 10026219Swpauluser2netname(netname, uid, domain) 10126219Swpaul char netname[MAXNETNAMELEN + 1]; 10274462Salfred const uid_t uid; 10374462Salfred const char *domain; 10426219Swpaul{ 10526219Swpaul char *dfltdom; 10626219Swpaul 10726219Swpaul if (domain == NULL) { 10890271Salfred if (__rpc_get_default_domain(&dfltdom) != 0) { 10926219Swpaul return (0); 11026219Swpaul } 11126219Swpaul domain = dfltdom; 11226219Swpaul } 11364242Skris if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) { 11426219Swpaul return (0); 11526219Swpaul } 11637301Sbde (void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain); 11726219Swpaul return (1); 11826219Swpaul} 11926219Swpaul 12026219Swpaul 12126219Swpaul/* 12226219Swpaul * Convert host to network-name 12326219Swpaul */ 12426219Swpaulint 12526219Swpaulhost2netname(netname, host, domain) 12626219Swpaul char netname[MAXNETNAMELEN + 1]; 12774462Salfred const char *host; 12874462Salfred const char *domain; 12926219Swpaul{ 13026219Swpaul char *dfltdom; 13126219Swpaul char hostname[MAXHOSTNAMELEN+1]; 13226219Swpaul 13326219Swpaul if (domain == NULL) { 13490271Salfred if (__rpc_get_default_domain(&dfltdom) != 0) { 13526219Swpaul return (0); 13626219Swpaul } 13726219Swpaul domain = dfltdom; 13826219Swpaul } 13926219Swpaul if (host == NULL) { 14026219Swpaul (void) gethostname(hostname, sizeof(hostname)); 14126219Swpaul host = hostname; 14226219Swpaul } 14364240Skris if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) { 14426219Swpaul return (0); 14526219Swpaul } 14626219Swpaul (void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain); 14726219Swpaul return (1); 14826219Swpaul} 149