/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2009, Sun Microsystems, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8* - Redistributions of source code must retain the above copyright notice,9* this list of conditions and the following disclaimer.10* - Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13* - Neither the name of Sun Microsystems, Inc. nor the names of its14* contributors may be used to endorse or promote products derived15* from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*/2930/*31* netname utility routines32* convert from unix names to network names and vice-versa33* This module is operating system dependent!34* What we define here will work with any unix system that has adopted35* the sun NIS domain architecture.36*/3738#include "namespace.h"39#include <sys/param.h>40#include <rpc/rpc.h>41#include <rpc/rpc_com.h>42#ifdef YP43#include <rpcsvc/yp_prot.h>44#include <rpcsvc/ypclnt.h>45#endif46#include <ctype.h>47#include <limits.h>48#include <stdio.h>49#include <stdlib.h>50#include <string.h>51#include <unistd.h>52#include "un-namespace.h"5354#ifndef MAXHOSTNAMELEN55#define MAXHOSTNAMELEN 25656#endif5758#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)5960#define TYPE_SIGNED(type) (((type) -1) < 0)6162/*63** 302 / 1000 is log10(2.0) rounded up.64** Subtract one for the sign bit if the type is signed;65** add one for integer division truncation;66** add one more for a minus sign if the type is signed.67*/68#define INT_STRLEN_MAXIMUM(type) \69((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))7071static char *OPSYS = "unix";7273/*74* Figure out my fully qualified network name75*/76int77getnetname(char name[MAXNETNAMELEN+1])78{79uid_t uid;8081uid = geteuid();82if (uid == 0) {83return (host2netname(name, (char *) NULL, (char *) NULL));84} else {85return (user2netname(name, uid, (char *) NULL));86}87}888990/*91* Convert unix cred to network-name92*/93int94user2netname(char netname[MAXNETNAMELEN + 1], const uid_t uid, const char *domain)95{96char *dfltdom;9798if (domain == NULL) {99if (__rpc_get_default_domain(&dfltdom) != 0) {100return (0);101}102domain = dfltdom;103}104if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {105return (0);106}107(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);108return (1);109}110111112/*113* Convert host to network-name114*/115int116host2netname(char netname[MAXNETNAMELEN + 1], const char *host, const char *domain)117{118char *dfltdom;119char hostname[MAXHOSTNAMELEN+1];120121if (domain == NULL) {122if (__rpc_get_default_domain(&dfltdom) != 0) {123return (0);124}125domain = dfltdom;126}127if (host == NULL) {128(void) gethostname(hostname, sizeof(hostname));129host = hostname;130}131if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {132return (0);133}134(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);135return (1);136}137138139