xref: /freebsd/lib/libc/rpc/netname.c (revision 683544bd3e0dbd73c247fdf38e687548ed7f9df5)
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 
44e8636dfdSBill Paul #include <sys/param.h>
45e8636dfdSBill Paul #include <rpc/rpc.h>
46e8636dfdSBill Paul #include <rpc/rpc_com.h>
47e8636dfdSBill Paul #ifdef YP
48e8636dfdSBill Paul #include <rpcsvc/yp_prot.h>
49e8636dfdSBill Paul #include <rpcsvc/ypclnt.h>
50e8636dfdSBill Paul #endif
51e8636dfdSBill Paul #include <ctype.h>
52e8636dfdSBill Paul #include <stdlib.h>
53e8636dfdSBill Paul #include <unistd.h>
54e8636dfdSBill Paul #include <string.h>
55e8636dfdSBill Paul #include <stdio.h>
56e8636dfdSBill Paul 
57e8636dfdSBill Paul #ifndef MAXHOSTNAMELEN
58e8636dfdSBill Paul #define MAXHOSTNAMELEN 256
59e8636dfdSBill Paul #endif
60e8636dfdSBill Paul #ifndef NGROUPS
61e8636dfdSBill Paul #define NGROUPS 16
62e8636dfdSBill Paul #endif
63e8636dfdSBill Paul 
64e8636dfdSBill Paul static char *OPSYS = "unix";
65e8636dfdSBill Paul 
66e8636dfdSBill Paul /*
67e8636dfdSBill Paul  * Figure out my fully qualified network name
68e8636dfdSBill Paul  */
69e8636dfdSBill Paul int
70e8636dfdSBill Paul getnetname(name)
71e8636dfdSBill Paul 	char name[MAXNETNAMELEN+1];
72e8636dfdSBill Paul {
73e8636dfdSBill Paul 	uid_t uid;
74e8636dfdSBill Paul 
75e8636dfdSBill Paul 	uid = geteuid();
76e8636dfdSBill Paul 	if (uid == 0) {
77e8636dfdSBill Paul 		return (host2netname(name, (char *) NULL, (char *) NULL));
78e8636dfdSBill Paul 	} else {
79e8636dfdSBill Paul 		return (user2netname(name, uid, (char *) NULL));
80e8636dfdSBill Paul 	}
81e8636dfdSBill Paul }
82e8636dfdSBill Paul 
83e8636dfdSBill Paul 
84e8636dfdSBill Paul /*
85e8636dfdSBill Paul  * Convert unix cred to network-name
86e8636dfdSBill Paul  */
87e8636dfdSBill Paul int
88e8636dfdSBill Paul user2netname(netname, uid, domain)
89e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
90e8636dfdSBill Paul 	uid_t uid;
91e8636dfdSBill Paul 	char *domain;
92e8636dfdSBill Paul {
93e8636dfdSBill Paul 	char *dfltdom;
94e8636dfdSBill Paul 
95683544bdSKris Kennaway #define MAXIPRINT	(20)	/* max length of printed integer */
96e8636dfdSBill Paul 
97e8636dfdSBill Paul 	if (domain == NULL) {
98e8636dfdSBill Paul 		if (_rpc_get_default_domain(&dfltdom) != 0) {
99e8636dfdSBill Paul 			return (0);
100e8636dfdSBill Paul 		}
101e8636dfdSBill Paul 		domain = dfltdom;
102e8636dfdSBill Paul 	}
103683544bdSKris Kennaway 	if (strlen(domain) + 1 + MAXIPRINT + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
104e8636dfdSBill Paul 		return (0);
105e8636dfdSBill Paul 	}
106a7f8e530SBruce Evans 	(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
107e8636dfdSBill Paul 	return (1);
108e8636dfdSBill Paul }
109e8636dfdSBill Paul 
110e8636dfdSBill Paul 
111e8636dfdSBill Paul /*
112e8636dfdSBill Paul  * Convert host to network-name
113e8636dfdSBill Paul  */
114e8636dfdSBill Paul int
115e8636dfdSBill Paul host2netname(netname, host, domain)
116e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
117e8636dfdSBill Paul 	char *host;
118e8636dfdSBill Paul 	char *domain;
119e8636dfdSBill Paul {
120e8636dfdSBill Paul 	char *dfltdom;
121e8636dfdSBill Paul 	char hostname[MAXHOSTNAMELEN+1];
122e8636dfdSBill Paul 
123e8636dfdSBill Paul 	if (domain == NULL) {
124e8636dfdSBill Paul 		if (_rpc_get_default_domain(&dfltdom) != 0) {
125e8636dfdSBill Paul 			return (0);
126e8636dfdSBill Paul 		}
127e8636dfdSBill Paul 		domain = dfltdom;
128e8636dfdSBill Paul 	}
129e8636dfdSBill Paul 	if (host == NULL) {
130e8636dfdSBill Paul 		(void) gethostname(hostname, sizeof(hostname));
131e8636dfdSBill Paul 		host = hostname;
132e8636dfdSBill Paul 	}
133683544bdSKris Kennaway 	if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
134e8636dfdSBill Paul 		return (0);
135e8636dfdSBill Paul 	}
136e8636dfdSBill Paul 	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
137e8636dfdSBill Paul 	return (1);
138e8636dfdSBill Paul }
139