xref: /freebsd/lib/libc/rpc/netname.c (revision 8360efbd6c932013ffdb2f83d2f2de4278febb5e)
1e8636dfdSBill Paul /*
2e8636dfdSBill Paul  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3e8636dfdSBill Paul  * unrestricted use provided that this legend is included on all tape
4e8636dfdSBill Paul  * media and as a part of the software program in whole or part.  Users
5e8636dfdSBill Paul  * may copy or modify Sun RPC without charge, but are not authorized
6e8636dfdSBill Paul  * to license or distribute it to anyone else except as part of a product or
7e8636dfdSBill Paul  * program developed by the user or with the express written consent of
8e8636dfdSBill Paul  * Sun Microsystems, Inc.
9e8636dfdSBill Paul  *
10e8636dfdSBill Paul  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11e8636dfdSBill Paul  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12e8636dfdSBill Paul  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13e8636dfdSBill Paul  *
14e8636dfdSBill Paul  * Sun RPC is provided with no support and without any obligation on the
15e8636dfdSBill Paul  * part of Sun Microsystems, Inc. to assist in its use, correction,
16e8636dfdSBill Paul  * modification or enhancement.
17e8636dfdSBill Paul  *
18e8636dfdSBill Paul  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19e8636dfdSBill Paul  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20e8636dfdSBill Paul  * OR ANY PART THEREOF.
21e8636dfdSBill Paul  *
22e8636dfdSBill Paul  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23e8636dfdSBill Paul  * or profits or other special, indirect and consequential damages, even if
24e8636dfdSBill Paul  * Sun has been advised of the possibility of such damages.
25e8636dfdSBill Paul  *
26e8636dfdSBill Paul  * Sun Microsystems, Inc.
27e8636dfdSBill Paul  * 2550 Garcia Avenue
28e8636dfdSBill Paul  * Mountain View, California  94043
29683544bdSKris Kennaway  *
30683544bdSKris Kennaway  * $FreeBSD$
31e8636dfdSBill Paul  */
32e8636dfdSBill Paul #if !defined(lint) && defined(SCCSIDS)
33e8636dfdSBill Paul static char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro";
34e8636dfdSBill Paul #endif
35e8636dfdSBill Paul 
36e8636dfdSBill Paul /*
37e8636dfdSBill Paul  * netname utility routines
38e8636dfdSBill Paul  * convert from unix names to network names and vice-versa
39e8636dfdSBill Paul  * This module is operating system dependent!
40e8636dfdSBill Paul  * What we define here will work with any unix system that has adopted
41e8636dfdSBill Paul  * the sun NIS domain architecture.
42e8636dfdSBill Paul  */
43e8636dfdSBill Paul 
448360efbdSAlfred Perlstein #include "namespace.h"
45e8636dfdSBill Paul #include <sys/param.h>
46e8636dfdSBill Paul #include <rpc/rpc.h>
47e8636dfdSBill Paul #include <rpc/rpc_com.h>
48e8636dfdSBill Paul #ifdef YP
49e8636dfdSBill Paul #include <rpcsvc/yp_prot.h>
50e8636dfdSBill Paul #include <rpcsvc/ypclnt.h>
51e8636dfdSBill Paul #endif
52e8636dfdSBill Paul #include <ctype.h>
5363c21920SKris Kennaway #include <limits.h>
54e8636dfdSBill Paul #include <stdio.h>
5563c21920SKris Kennaway #include <stdlib.h>
5663c21920SKris Kennaway #include <string.h>
5763c21920SKris Kennaway #include <unistd.h>
588360efbdSAlfred Perlstein #include "un-namespace.h"
59e8636dfdSBill Paul 
60e8636dfdSBill Paul #ifndef MAXHOSTNAMELEN
61e8636dfdSBill Paul #define MAXHOSTNAMELEN 256
62e8636dfdSBill Paul #endif
63e8636dfdSBill Paul #ifndef NGROUPS
64e8636dfdSBill Paul #define NGROUPS 16
65e8636dfdSBill Paul #endif
66e8636dfdSBill Paul 
6763c21920SKris Kennaway #define TYPE_BIT(type)  (sizeof (type) * CHAR_BIT)
6863c21920SKris Kennaway 
6963c21920SKris Kennaway #define TYPE_SIGNED(type) (((type) -1) < 0)
7063c21920SKris Kennaway 
7163c21920SKris Kennaway /*
7263c21920SKris Kennaway ** 302 / 1000 is log10(2.0) rounded up.
7363c21920SKris Kennaway ** Subtract one for the sign bit if the type is signed;
7463c21920SKris Kennaway ** add one for integer division truncation;
7563c21920SKris Kennaway ** add one more for a minus sign if the type is signed.
7663c21920SKris Kennaway */
7763c21920SKris Kennaway #define INT_STRLEN_MAXIMUM(type) \
7863c21920SKris Kennaway     ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
7963c21920SKris Kennaway 
80e8636dfdSBill Paul static char *OPSYS = "unix";
81e8636dfdSBill Paul 
82e8636dfdSBill Paul /*
83e8636dfdSBill Paul  * Figure out my fully qualified network name
84e8636dfdSBill Paul  */
85e8636dfdSBill Paul int
86e8636dfdSBill Paul getnetname(name)
87e8636dfdSBill Paul 	char name[MAXNETNAMELEN+1];
88e8636dfdSBill Paul {
89e8636dfdSBill Paul 	uid_t uid;
90e8636dfdSBill Paul 
91e8636dfdSBill Paul 	uid = geteuid();
92e8636dfdSBill Paul 	if (uid == 0) {
93e8636dfdSBill Paul 		return (host2netname(name, (char *) NULL, (char *) NULL));
94e8636dfdSBill Paul 	} else {
95e8636dfdSBill Paul 		return (user2netname(name, uid, (char *) NULL));
96e8636dfdSBill Paul 	}
97e8636dfdSBill Paul }
98e8636dfdSBill Paul 
99e8636dfdSBill Paul 
100e8636dfdSBill Paul /*
101e8636dfdSBill Paul  * Convert unix cred to network-name
102e8636dfdSBill Paul  */
103e8636dfdSBill Paul int
104e8636dfdSBill Paul user2netname(netname, uid, domain)
105e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
1068360efbdSAlfred Perlstein 	const uid_t uid;
1078360efbdSAlfred Perlstein 	const char *domain;
108e8636dfdSBill Paul {
109e8636dfdSBill Paul 	char *dfltdom;
110e8636dfdSBill Paul 
111e8636dfdSBill Paul 	if (domain == NULL) {
112e8636dfdSBill Paul 		if (_rpc_get_default_domain(&dfltdom) != 0) {
113e8636dfdSBill Paul 			return (0);
114e8636dfdSBill Paul 		}
115e8636dfdSBill Paul 		domain = dfltdom;
116e8636dfdSBill Paul 	}
11763c21920SKris Kennaway 	if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
118e8636dfdSBill Paul 		return (0);
119e8636dfdSBill Paul 	}
120a7f8e530SBruce Evans 	(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
121e8636dfdSBill Paul 	return (1);
122e8636dfdSBill Paul }
123e8636dfdSBill Paul 
124e8636dfdSBill Paul 
125e8636dfdSBill Paul /*
126e8636dfdSBill Paul  * Convert host to network-name
127e8636dfdSBill Paul  */
128e8636dfdSBill Paul int
129e8636dfdSBill Paul host2netname(netname, host, domain)
130e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
1318360efbdSAlfred Perlstein 	const char *host;
1328360efbdSAlfred Perlstein 	const char *domain;
133e8636dfdSBill Paul {
134e8636dfdSBill Paul 	char *dfltdom;
135e8636dfdSBill Paul 	char hostname[MAXHOSTNAMELEN+1];
136e8636dfdSBill Paul 
137e8636dfdSBill Paul 	if (domain == NULL) {
138e8636dfdSBill Paul 		if (_rpc_get_default_domain(&dfltdom) != 0) {
139e8636dfdSBill Paul 			return (0);
140e8636dfdSBill Paul 		}
141e8636dfdSBill Paul 		domain = dfltdom;
142e8636dfdSBill Paul 	}
143e8636dfdSBill Paul 	if (host == NULL) {
144e8636dfdSBill Paul 		(void) gethostname(hostname, sizeof(hostname));
145e8636dfdSBill Paul 		host = hostname;
146e8636dfdSBill Paul 	}
147683544bdSKris Kennaway 	if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
148e8636dfdSBill Paul 		return (0);
149e8636dfdSBill Paul 	}
150e8636dfdSBill Paul 	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
151e8636dfdSBill Paul 	return (1);
152e8636dfdSBill Paul }
153