xref: /freebsd/lib/libc/rpc/netname.c (revision a986ef5788bd3338d2a970eb96104824e4a0e3bb)
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
29e8636dfdSBill Paul  */
30a986ef57SDavid E. O'Brien 
31a986ef57SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint)
32e8636dfdSBill Paul static char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro";
33e8636dfdSBill Paul #endif
34d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
35d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
36e8636dfdSBill Paul 
37e8636dfdSBill Paul /*
38e8636dfdSBill Paul  * netname utility routines
39e8636dfdSBill Paul  * convert from unix names to network names and vice-versa
40e8636dfdSBill Paul  * This module is operating system dependent!
41e8636dfdSBill Paul  * What we define here will work with any unix system that has adopted
42e8636dfdSBill Paul  * the sun NIS domain architecture.
43e8636dfdSBill Paul  */
44e8636dfdSBill Paul 
458360efbdSAlfred Perlstein #include "namespace.h"
46e8636dfdSBill Paul #include <sys/param.h>
47e8636dfdSBill Paul #include <rpc/rpc.h>
48e8636dfdSBill Paul #include <rpc/rpc_com.h>
49e8636dfdSBill Paul #ifdef YP
50e8636dfdSBill Paul #include <rpcsvc/yp_prot.h>
51e8636dfdSBill Paul #include <rpcsvc/ypclnt.h>
52e8636dfdSBill Paul #endif
53e8636dfdSBill Paul #include <ctype.h>
5463c21920SKris Kennaway #include <limits.h>
55e8636dfdSBill Paul #include <stdio.h>
5663c21920SKris Kennaway #include <stdlib.h>
5763c21920SKris Kennaway #include <string.h>
5863c21920SKris Kennaway #include <unistd.h>
598360efbdSAlfred Perlstein #include "un-namespace.h"
60e8636dfdSBill Paul 
61e8636dfdSBill Paul #ifndef MAXHOSTNAMELEN
62e8636dfdSBill Paul #define MAXHOSTNAMELEN 256
63e8636dfdSBill Paul #endif
64e8636dfdSBill Paul #ifndef NGROUPS
65e8636dfdSBill Paul #define NGROUPS 16
66e8636dfdSBill Paul #endif
67e8636dfdSBill Paul 
6863c21920SKris Kennaway #define TYPE_BIT(type)  (sizeof (type) * CHAR_BIT)
6963c21920SKris Kennaway 
7063c21920SKris Kennaway #define TYPE_SIGNED(type) (((type) -1) < 0)
7163c21920SKris Kennaway 
7263c21920SKris Kennaway /*
7363c21920SKris Kennaway ** 302 / 1000 is log10(2.0) rounded up.
7463c21920SKris Kennaway ** Subtract one for the sign bit if the type is signed;
7563c21920SKris Kennaway ** add one for integer division truncation;
7663c21920SKris Kennaway ** add one more for a minus sign if the type is signed.
7763c21920SKris Kennaway */
7863c21920SKris Kennaway #define INT_STRLEN_MAXIMUM(type) \
7963c21920SKris Kennaway     ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
8063c21920SKris Kennaway 
81e8636dfdSBill Paul static char *OPSYS = "unix";
82e8636dfdSBill Paul 
83e8636dfdSBill Paul /*
84e8636dfdSBill Paul  * Figure out my fully qualified network name
85e8636dfdSBill Paul  */
86e8636dfdSBill Paul int
87e8636dfdSBill Paul getnetname(name)
88e8636dfdSBill Paul 	char name[MAXNETNAMELEN+1];
89e8636dfdSBill Paul {
90e8636dfdSBill Paul 	uid_t uid;
91e8636dfdSBill Paul 
92e8636dfdSBill Paul 	uid = geteuid();
93e8636dfdSBill Paul 	if (uid == 0) {
94e8636dfdSBill Paul 		return (host2netname(name, (char *) NULL, (char *) NULL));
95e8636dfdSBill Paul 	} else {
96e8636dfdSBill Paul 		return (user2netname(name, uid, (char *) NULL));
97e8636dfdSBill Paul 	}
98e8636dfdSBill Paul }
99e8636dfdSBill Paul 
100e8636dfdSBill Paul 
101e8636dfdSBill Paul /*
102e8636dfdSBill Paul  * Convert unix cred to network-name
103e8636dfdSBill Paul  */
104e8636dfdSBill Paul int
105e8636dfdSBill Paul user2netname(netname, uid, domain)
106e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
1078360efbdSAlfred Perlstein 	const uid_t uid;
1088360efbdSAlfred Perlstein 	const char *domain;
109e8636dfdSBill Paul {
110e8636dfdSBill Paul 	char *dfltdom;
111e8636dfdSBill Paul 
112e8636dfdSBill Paul 	if (domain == NULL) {
1138d630135SAlfred Perlstein 		if (__rpc_get_default_domain(&dfltdom) != 0) {
114e8636dfdSBill Paul 			return (0);
115e8636dfdSBill Paul 		}
116e8636dfdSBill Paul 		domain = dfltdom;
117e8636dfdSBill Paul 	}
11863c21920SKris Kennaway 	if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
119e8636dfdSBill Paul 		return (0);
120e8636dfdSBill Paul 	}
121a7f8e530SBruce Evans 	(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
122e8636dfdSBill Paul 	return (1);
123e8636dfdSBill Paul }
124e8636dfdSBill Paul 
125e8636dfdSBill Paul 
126e8636dfdSBill Paul /*
127e8636dfdSBill Paul  * Convert host to network-name
128e8636dfdSBill Paul  */
129e8636dfdSBill Paul int
130e8636dfdSBill Paul host2netname(netname, host, domain)
131e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
1328360efbdSAlfred Perlstein 	const char *host;
1338360efbdSAlfred Perlstein 	const char *domain;
134e8636dfdSBill Paul {
135e8636dfdSBill Paul 	char *dfltdom;
136e8636dfdSBill Paul 	char hostname[MAXHOSTNAMELEN+1];
137e8636dfdSBill Paul 
138e8636dfdSBill Paul 	if (domain == NULL) {
1398d630135SAlfred Perlstein 		if (__rpc_get_default_domain(&dfltdom) != 0) {
140e8636dfdSBill Paul 			return (0);
141e8636dfdSBill Paul 		}
142e8636dfdSBill Paul 		domain = dfltdom;
143e8636dfdSBill Paul 	}
144e8636dfdSBill Paul 	if (host == NULL) {
145e8636dfdSBill Paul 		(void) gethostname(hostname, sizeof(hostname));
146e8636dfdSBill Paul 		host = hostname;
147e8636dfdSBill Paul 	}
148683544bdSKris Kennaway 	if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
149e8636dfdSBill Paul 		return (0);
150e8636dfdSBill Paul 	}
151e8636dfdSBill Paul 	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
152e8636dfdSBill Paul 	return (1);
153e8636dfdSBill Paul }
154