126219Swpaul/*
226219Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
326219Swpaul * unrestricted use provided that this legend is included on all tape
426219Swpaul * media and as a part of the software program in whole or part.  Users
526219Swpaul * may copy or modify Sun RPC without charge, but are not authorized
626219Swpaul * to license or distribute it to anyone else except as part of a product or
726219Swpaul * program developed by the user or with the express written consent of
826219Swpaul * Sun Microsystems, Inc.
926219Swpaul *
1026219Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1126219Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1226219Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1326219Swpaul *
1426219Swpaul * Sun RPC is provided with no support and without any obligation on the
1526219Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction,
1626219Swpaul * modification or enhancement.
1726219Swpaul *
1826219Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1926219Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2026219Swpaul * OR ANY PART THEREOF.
2126219Swpaul *
2226219Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2326219Swpaul * or profits or other special, indirect and consequential damages, even if
2426219Swpaul * Sun has been advised of the possibility of such damages.
2526219Swpaul *
2626219Swpaul * Sun Microsystems, Inc.
2726219Swpaul * 2550 Garcia Avenue
2826219Swpaul * Mountain View, California  94043
2926219Swpaul */
30136581Sobrien
31136581Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3226219Swpaulstatic char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro";
3326219Swpaul#endif
3492990Sobrien#include <sys/cdefs.h>
3592990Sobrien__FBSDID("$FreeBSD$");
3626219Swpaul
3726219Swpaul/*
3826219Swpaul * netname utility routines
3926219Swpaul * convert from unix names to network names and vice-versa
4026219Swpaul * This module is operating system dependent!
4126219Swpaul * What we define here will work with any unix system that has adopted
4226219Swpaul * the sun NIS domain architecture.
4326219Swpaul */
4426219Swpaul
4574462Salfred#include "namespace.h"
4626219Swpaul#include <sys/param.h>
4726219Swpaul#include <rpc/rpc.h>
4826219Swpaul#include <rpc/rpc_com.h>
4926219Swpaul#ifdef YP
5026219Swpaul#include <rpcsvc/yp_prot.h>
5126219Swpaul#include <rpcsvc/ypclnt.h>
5226219Swpaul#endif
5326219Swpaul#include <ctype.h>
5464242Skris#include <limits.h>
5564242Skris#include <stdio.h>
5626219Swpaul#include <stdlib.h>
5764242Skris#include <string.h>
5826219Swpaul#include <unistd.h>
5974462Salfred#include "un-namespace.h"
6026219Swpaul
6126219Swpaul#ifndef MAXHOSTNAMELEN
6226219Swpaul#define MAXHOSTNAMELEN 256
6326219Swpaul#endif
6426219Swpaul
6564242Skris#define TYPE_BIT(type)  (sizeof (type) * CHAR_BIT)
6664242Skris
6764242Skris#define TYPE_SIGNED(type) (((type) -1) < 0)
6864242Skris
6964242Skris/*
7064242Skris** 302 / 1000 is log10(2.0) rounded up.
7164242Skris** Subtract one for the sign bit if the type is signed;
7264242Skris** add one for integer division truncation;
7364242Skris** add one more for a minus sign if the type is signed.
7464242Skris*/
7564242Skris#define INT_STRLEN_MAXIMUM(type) \
7664242Skris    ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
7764242Skris
7826219Swpaulstatic char *OPSYS = "unix";
7926219Swpaul
8026219Swpaul/*
8126219Swpaul * Figure out my fully qualified network name
8226219Swpaul */
8326219Swpaulint
8426219Swpaulgetnetname(name)
8526219Swpaul	char name[MAXNETNAMELEN+1];
8626219Swpaul{
8726219Swpaul	uid_t uid;
8826219Swpaul
8926219Swpaul	uid = geteuid();
9026219Swpaul	if (uid == 0) {
9126219Swpaul		return (host2netname(name, (char *) NULL, (char *) NULL));
9226219Swpaul	} else {
9326219Swpaul		return (user2netname(name, uid, (char *) NULL));
9426219Swpaul	}
9526219Swpaul}
9626219Swpaul
9726219Swpaul
9826219Swpaul/*
9926219Swpaul * Convert unix cred to network-name
10026219Swpaul */
10126219Swpaulint
10226219Swpauluser2netname(netname, uid, domain)
10326219Swpaul	char netname[MAXNETNAMELEN + 1];
10474462Salfred	const uid_t uid;
10574462Salfred	const char *domain;
10626219Swpaul{
10726219Swpaul	char *dfltdom;
10826219Swpaul
10926219Swpaul	if (domain == NULL) {
11090271Salfred		if (__rpc_get_default_domain(&dfltdom) != 0) {
11126219Swpaul			return (0);
11226219Swpaul		}
11326219Swpaul		domain = dfltdom;
11426219Swpaul	}
11564242Skris	if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
11626219Swpaul		return (0);
11726219Swpaul	}
11837301Sbde	(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
11926219Swpaul	return (1);
12026219Swpaul}
12126219Swpaul
12226219Swpaul
12326219Swpaul/*
12426219Swpaul * Convert host to network-name
12526219Swpaul */
12626219Swpaulint
12726219Swpaulhost2netname(netname, host, domain)
12826219Swpaul	char netname[MAXNETNAMELEN + 1];
12974462Salfred	const char *host;
13074462Salfred	const char *domain;
13126219Swpaul{
13226219Swpaul	char *dfltdom;
13326219Swpaul	char hostname[MAXHOSTNAMELEN+1];
13426219Swpaul
13526219Swpaul	if (domain == NULL) {
13690271Salfred		if (__rpc_get_default_domain(&dfltdom) != 0) {
13726219Swpaul			return (0);
13826219Swpaul		}
13926219Swpaul		domain = dfltdom;
14026219Swpaul	}
14126219Swpaul	if (host == NULL) {
14226219Swpaul		(void) gethostname(hostname, sizeof(hostname));
14326219Swpaul		host = hostname;
14426219Swpaul	}
14564240Skris	if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
14626219Swpaul		return (0);
14726219Swpaul	}
14826219Swpaul	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
14926219Swpaul	return (1);
15026219Swpaul}
151