xref: /freebsd/lib/libc/rpc/netname.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
12e322d37SHiroki Sato /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
42e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
52e322d37SHiroki Sato  * All rights reserved.
6e8636dfdSBill Paul  *
72e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
82e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
92e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
102e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
112e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
132e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
142e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
152e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
162e322d37SHiroki Sato  *   from this software without specific prior written permission.
17e8636dfdSBill Paul  *
182e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
192e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
222e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
232e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
242e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
262e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
272e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
29e8636dfdSBill Paul  */
30a986ef57SDavid E. O'Brien 
31e8636dfdSBill Paul /*
32e8636dfdSBill Paul  * netname utility routines
33e8636dfdSBill Paul  * convert from unix names to network names and vice-versa
34e8636dfdSBill Paul  * This module is operating system dependent!
35e8636dfdSBill Paul  * What we define here will work with any unix system that has adopted
36e8636dfdSBill Paul  * the sun NIS domain architecture.
37e8636dfdSBill Paul  */
38e8636dfdSBill Paul 
398360efbdSAlfred Perlstein #include "namespace.h"
40e8636dfdSBill Paul #include <sys/param.h>
41e8636dfdSBill Paul #include <rpc/rpc.h>
42e8636dfdSBill Paul #include <rpc/rpc_com.h>
43e8636dfdSBill Paul #ifdef YP
44e8636dfdSBill Paul #include <rpcsvc/yp_prot.h>
45e8636dfdSBill Paul #include <rpcsvc/ypclnt.h>
46e8636dfdSBill Paul #endif
47e8636dfdSBill Paul #include <ctype.h>
4863c21920SKris Kennaway #include <limits.h>
49e8636dfdSBill Paul #include <stdio.h>
5063c21920SKris Kennaway #include <stdlib.h>
5163c21920SKris Kennaway #include <string.h>
5263c21920SKris Kennaway #include <unistd.h>
538360efbdSAlfred Perlstein #include "un-namespace.h"
54e8636dfdSBill Paul 
55e8636dfdSBill Paul #ifndef MAXHOSTNAMELEN
56e8636dfdSBill Paul #define MAXHOSTNAMELEN 256
57e8636dfdSBill Paul #endif
58e8636dfdSBill Paul 
5963c21920SKris Kennaway #define TYPE_BIT(type)  (sizeof (type) * CHAR_BIT)
6063c21920SKris Kennaway 
6163c21920SKris Kennaway #define TYPE_SIGNED(type) (((type) -1) < 0)
6263c21920SKris Kennaway 
6363c21920SKris Kennaway /*
6463c21920SKris Kennaway ** 302 / 1000 is log10(2.0) rounded up.
6563c21920SKris Kennaway ** Subtract one for the sign bit if the type is signed;
6663c21920SKris Kennaway ** add one for integer division truncation;
6763c21920SKris Kennaway ** add one more for a minus sign if the type is signed.
6863c21920SKris Kennaway */
6963c21920SKris Kennaway #define INT_STRLEN_MAXIMUM(type) \
7063c21920SKris Kennaway     ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
7163c21920SKris Kennaway 
72e8636dfdSBill Paul static char *OPSYS = "unix";
73e8636dfdSBill Paul 
74e8636dfdSBill Paul /*
75e8636dfdSBill Paul  * Figure out my fully qualified network name
76e8636dfdSBill Paul  */
77e8636dfdSBill Paul int
getnetname(char name[MAXNETNAMELEN+1])78587cf682SCraig Rodrigues getnetname(char name[MAXNETNAMELEN+1])
79e8636dfdSBill Paul {
80e8636dfdSBill Paul 	uid_t uid;
81e8636dfdSBill Paul 
82e8636dfdSBill Paul 	uid = geteuid();
83e8636dfdSBill Paul 	if (uid == 0) {
84e8636dfdSBill Paul 		return (host2netname(name, (char *) NULL, (char *) NULL));
85e8636dfdSBill Paul 	} else {
86e8636dfdSBill Paul 		return (user2netname(name, uid, (char *) NULL));
87e8636dfdSBill Paul 	}
88e8636dfdSBill Paul }
89e8636dfdSBill Paul 
90e8636dfdSBill Paul 
91e8636dfdSBill Paul /*
92e8636dfdSBill Paul  * Convert unix cred to network-name
93e8636dfdSBill Paul  */
94e8636dfdSBill Paul int
user2netname(char netname[MAXNETNAMELEN+1],const uid_t uid,const char * domain)95587cf682SCraig Rodrigues user2netname(char netname[MAXNETNAMELEN + 1], const uid_t uid, const char *domain)
96e8636dfdSBill Paul {
97e8636dfdSBill Paul 	char *dfltdom;
98e8636dfdSBill Paul 
99e8636dfdSBill Paul 	if (domain == NULL) {
1008d630135SAlfred Perlstein 		if (__rpc_get_default_domain(&dfltdom) != 0) {
101e8636dfdSBill Paul 			return (0);
102e8636dfdSBill Paul 		}
103e8636dfdSBill Paul 		domain = dfltdom;
104e8636dfdSBill Paul 	}
10563c21920SKris Kennaway 	if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
106e8636dfdSBill Paul 		return (0);
107e8636dfdSBill Paul 	}
108a7f8e530SBruce Evans 	(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
109e8636dfdSBill Paul 	return (1);
110e8636dfdSBill Paul }
111e8636dfdSBill Paul 
112e8636dfdSBill Paul 
113e8636dfdSBill Paul /*
114e8636dfdSBill Paul  * Convert host to network-name
115e8636dfdSBill Paul  */
116e8636dfdSBill Paul int
host2netname(char netname[MAXNETNAMELEN+1],const char * host,const char * domain)117587cf682SCraig Rodrigues host2netname(char netname[MAXNETNAMELEN + 1], const char *host, const char *domain)
118e8636dfdSBill Paul {
119e8636dfdSBill Paul 	char *dfltdom;
120e8636dfdSBill Paul 	char hostname[MAXHOSTNAMELEN+1];
121e8636dfdSBill Paul 
122e8636dfdSBill Paul 	if (domain == NULL) {
1238d630135SAlfred Perlstein 		if (__rpc_get_default_domain(&dfltdom) != 0) {
124e8636dfdSBill Paul 			return (0);
125e8636dfdSBill Paul 		}
126e8636dfdSBill Paul 		domain = dfltdom;
127e8636dfdSBill Paul 	}
128e8636dfdSBill Paul 	if (host == NULL) {
129e8636dfdSBill Paul 		(void) gethostname(hostname, sizeof(hostname));
130e8636dfdSBill Paul 		host = hostname;
131e8636dfdSBill Paul 	}
132683544bdSKris Kennaway 	if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
133e8636dfdSBill Paul 		return (0);
134e8636dfdSBill Paul 	}
135e8636dfdSBill Paul 	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
136e8636dfdSBill Paul 	return (1);
137e8636dfdSBill Paul }
138