xref: /freebsd/lib/libc/rpc/netname.c (revision e8636dfd57b1bc6a19328606214e847c0d9eb1aa)
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  */
30e8636dfdSBill Paul #if !defined(lint) && defined(SCCSIDS)
31e8636dfdSBill Paul static char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro";
32e8636dfdSBill Paul #endif
33e8636dfdSBill Paul 
34e8636dfdSBill Paul /*
35e8636dfdSBill Paul  * netname utility routines
36e8636dfdSBill Paul  * convert from unix names to network names and vice-versa
37e8636dfdSBill Paul  * This module is operating system dependent!
38e8636dfdSBill Paul  * What we define here will work with any unix system that has adopted
39e8636dfdSBill Paul  * the sun NIS domain architecture.
40e8636dfdSBill Paul  */
41e8636dfdSBill Paul 
42e8636dfdSBill Paul #include <sys/param.h>
43e8636dfdSBill Paul #include <rpc/rpc.h>
44e8636dfdSBill Paul #include <rpc/rpc_com.h>
45e8636dfdSBill Paul #ifdef YP
46e8636dfdSBill Paul #include <rpcsvc/yp_prot.h>
47e8636dfdSBill Paul #include <rpcsvc/ypclnt.h>
48e8636dfdSBill Paul #endif
49e8636dfdSBill Paul #include <ctype.h>
50e8636dfdSBill Paul #include <stdlib.h>
51e8636dfdSBill Paul #include <unistd.h>
52e8636dfdSBill Paul #include <string.h>
53e8636dfdSBill Paul #include <stdio.h>
54e8636dfdSBill Paul 
55e8636dfdSBill Paul #ifndef MAXHOSTNAMELEN
56e8636dfdSBill Paul #define MAXHOSTNAMELEN 256
57e8636dfdSBill Paul #endif
58e8636dfdSBill Paul #ifndef NGROUPS
59e8636dfdSBill Paul #define NGROUPS 16
60e8636dfdSBill Paul #endif
61e8636dfdSBill Paul 
62e8636dfdSBill Paul static char *OPSYS = "unix";
63e8636dfdSBill Paul 
64e8636dfdSBill Paul /*
65e8636dfdSBill Paul  * Figure out my fully qualified network name
66e8636dfdSBill Paul  */
67e8636dfdSBill Paul int
68e8636dfdSBill Paul getnetname(name)
69e8636dfdSBill Paul 	char name[MAXNETNAMELEN+1];
70e8636dfdSBill Paul {
71e8636dfdSBill Paul 	uid_t uid;
72e8636dfdSBill Paul 
73e8636dfdSBill Paul 	uid = geteuid();
74e8636dfdSBill Paul 	if (uid == 0) {
75e8636dfdSBill Paul 		return (host2netname(name, (char *) NULL, (char *) NULL));
76e8636dfdSBill Paul 	} else {
77e8636dfdSBill Paul 		return (user2netname(name, uid, (char *) NULL));
78e8636dfdSBill Paul 	}
79e8636dfdSBill Paul }
80e8636dfdSBill Paul 
81e8636dfdSBill Paul 
82e8636dfdSBill Paul /*
83e8636dfdSBill Paul  * Convert unix cred to network-name
84e8636dfdSBill Paul  */
85e8636dfdSBill Paul int
86e8636dfdSBill Paul user2netname(netname, uid, domain)
87e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
88e8636dfdSBill Paul 	uid_t uid;
89e8636dfdSBill Paul 	char *domain;
90e8636dfdSBill Paul {
91e8636dfdSBill Paul 	char *dfltdom;
92e8636dfdSBill Paul 
93e8636dfdSBill Paul #define MAXIPRINT	(11)	/* max length of printed integer */
94e8636dfdSBill Paul 
95e8636dfdSBill Paul 	if (domain == NULL) {
96e8636dfdSBill Paul 		if (_rpc_get_default_domain(&dfltdom) != 0) {
97e8636dfdSBill Paul 			return (0);
98e8636dfdSBill Paul 		}
99e8636dfdSBill Paul 		domain = dfltdom;
100e8636dfdSBill Paul 	}
101e8636dfdSBill Paul 	if (strlen(domain) + 1 + MAXIPRINT > MAXNETNAMELEN) {
102e8636dfdSBill Paul 		return (0);
103e8636dfdSBill Paul 	}
104e8636dfdSBill Paul 	(void) sprintf(netname, "%s.%ld@%s", OPSYS, uid, domain);
105e8636dfdSBill Paul 	return (1);
106e8636dfdSBill Paul }
107e8636dfdSBill Paul 
108e8636dfdSBill Paul 
109e8636dfdSBill Paul /*
110e8636dfdSBill Paul  * Convert host to network-name
111e8636dfdSBill Paul  */
112e8636dfdSBill Paul int
113e8636dfdSBill Paul host2netname(netname, host, domain)
114e8636dfdSBill Paul 	char netname[MAXNETNAMELEN + 1];
115e8636dfdSBill Paul 	char *host;
116e8636dfdSBill Paul 	char *domain;
117e8636dfdSBill Paul {
118e8636dfdSBill Paul 	char *dfltdom;
119e8636dfdSBill Paul 	char hostname[MAXHOSTNAMELEN+1];
120e8636dfdSBill Paul 
121e8636dfdSBill Paul 	if (domain == NULL) {
122e8636dfdSBill Paul 		if (_rpc_get_default_domain(&dfltdom) != 0) {
123e8636dfdSBill Paul 			return (0);
124e8636dfdSBill Paul 		}
125e8636dfdSBill Paul 		domain = dfltdom;
126e8636dfdSBill Paul 	}
127e8636dfdSBill Paul 	if (host == NULL) {
128e8636dfdSBill Paul 		(void) gethostname(hostname, sizeof(hostname));
129e8636dfdSBill Paul 		host = hostname;
130e8636dfdSBill Paul 	}
131e8636dfdSBill Paul 	if (strlen(domain) + 1 + strlen(host) > MAXNETNAMELEN) {
132e8636dfdSBill Paul 		return (0);
133e8636dfdSBill Paul 	}
134e8636dfdSBill Paul 	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
135e8636dfdSBill Paul 	return (1);
136e8636dfdSBill Paul }
137