2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/lib/libc/rpc/netname.c,v 1.8 2004/10/16 06:11:35 obrien Exp $
29 * $DragonFly: src/lib/libc/rpc/netname.c,v 1.3 2005/11/13 12:27:04 swildner Exp $
31 * @(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro
35 * netname utility routines
36 * convert from unix names to network names and vice-versa
37 * This module is operating system dependent!
38 * What we define here will work with any unix system that has adopted
39 * the sun NIS domain architecture.
42 #include "namespace.h"
43 #include <sys/param.h>
45 #include <rpc/rpc_com.h>
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
56 #include "un-namespace.h"
58 #ifndef MAXHOSTNAMELEN
59 #define MAXHOSTNAMELEN 256
65 #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
67 #define TYPE_SIGNED(type) (((type) -1) < 0)
70 ** 302 / 1000 is log10(2.0) rounded up.
71 ** Subtract one for the sign bit if the type is signed;
72 ** add one for integer division truncation;
73 ** add one more for a minus sign if the type is signed.
75 #define INT_STRLEN_MAXIMUM(type) \
76 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
78 static char *OPSYS = "unix";
81 * Figure out my fully qualified network name
84 getnetname(char *name)
90 return (host2netname(name, NULL, NULL));
92 return (user2netname(name, uid, NULL));
98 * Convert unix cred to network-name
101 user2netname(char *netname, const uid_t uid, const char *domain)
105 if (domain == NULL) {
106 if (__rpc_get_default_domain(&dfltdom) != 0) {
111 if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
114 sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
120 * Convert host to network-name
123 host2netname(char *netname, const char *host, const char *domain)
126 char hostname[MAXHOSTNAMELEN+1];
128 if (domain == NULL) {
129 if (__rpc_get_default_domain(&dfltdom) != 0) {
135 gethostname(hostname, sizeof(hostname));
138 if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
141 sprintf(netname, "%s.%s@%s", OPSYS, host, domain);