xref: /freebsd/lib/libc/rpc/netname.c (revision 683544bd3e0dbd73c247fdf38e687548ed7f9df5)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  *
30  * $FreeBSD$
31  */
32 #if !defined(lint) && defined(SCCSIDS)
33 static char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro";
34 #endif
35 
36 /*
37  * netname utility routines
38  * convert from unix names to network names and vice-versa
39  * This module is operating system dependent!
40  * What we define here will work with any unix system that has adopted
41  * the sun NIS domain architecture.
42  */
43 
44 #include <sys/param.h>
45 #include <rpc/rpc.h>
46 #include <rpc/rpc_com.h>
47 #ifdef YP
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
50 #endif
51 #include <ctype.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <string.h>
55 #include <stdio.h>
56 
57 #ifndef MAXHOSTNAMELEN
58 #define MAXHOSTNAMELEN 256
59 #endif
60 #ifndef NGROUPS
61 #define NGROUPS 16
62 #endif
63 
64 static char *OPSYS = "unix";
65 
66 /*
67  * Figure out my fully qualified network name
68  */
69 int
70 getnetname(name)
71 	char name[MAXNETNAMELEN+1];
72 {
73 	uid_t uid;
74 
75 	uid = geteuid();
76 	if (uid == 0) {
77 		return (host2netname(name, (char *) NULL, (char *) NULL));
78 	} else {
79 		return (user2netname(name, uid, (char *) NULL));
80 	}
81 }
82 
83 
84 /*
85  * Convert unix cred to network-name
86  */
87 int
88 user2netname(netname, uid, domain)
89 	char netname[MAXNETNAMELEN + 1];
90 	uid_t uid;
91 	char *domain;
92 {
93 	char *dfltdom;
94 
95 #define MAXIPRINT	(20)	/* max length of printed integer */
96 
97 	if (domain == NULL) {
98 		if (_rpc_get_default_domain(&dfltdom) != 0) {
99 			return (0);
100 		}
101 		domain = dfltdom;
102 	}
103 	if (strlen(domain) + 1 + MAXIPRINT + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
104 		return (0);
105 	}
106 	(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
107 	return (1);
108 }
109 
110 
111 /*
112  * Convert host to network-name
113  */
114 int
115 host2netname(netname, host, domain)
116 	char netname[MAXNETNAMELEN + 1];
117 	char *host;
118 	char *domain;
119 {
120 	char *dfltdom;
121 	char hostname[MAXHOSTNAMELEN+1];
122 
123 	if (domain == NULL) {
124 		if (_rpc_get_default_domain(&dfltdom) != 0) {
125 			return (0);
126 		}
127 		domain = dfltdom;
128 	}
129 	if (host == NULL) {
130 		(void) gethostname(hostname, sizeof(hostname));
131 		host = hostname;
132 	}
133 	if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
134 		return (0);
135 	}
136 	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
137 	return (1);
138 }
139