xref: /reactos/dll/3rdparty/libtirpc/src/netname.c (revision 0c2cdcae)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * Copyright (c) 2009, Sun Microsystems, Inc.
3*c2c66affSColin Finck  * All rights reserved.
4*c2c66affSColin Finck  *
5*c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
6*c2c66affSColin Finck  * modification, are permitted provided that the following conditions are met:
7*c2c66affSColin Finck  * - Redistributions of source code must retain the above copyright notice,
8*c2c66affSColin Finck  *   this list of conditions and the following disclaimer.
9*c2c66affSColin Finck  * - Redistributions in binary form must reproduce the above copyright notice,
10*c2c66affSColin Finck  *   this list of conditions and the following disclaimer in the documentation
11*c2c66affSColin Finck  *   and/or other materials provided with the distribution.
12*c2c66affSColin Finck  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*c2c66affSColin Finck  *   contributors may be used to endorse or promote products derived
14*c2c66affSColin Finck  *   from this software without specific prior written permission.
15*c2c66affSColin Finck  *
16*c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*c2c66affSColin Finck  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*c2c66affSColin Finck  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*c2c66affSColin Finck  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*c2c66affSColin Finck  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*c2c66affSColin Finck  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*c2c66affSColin Finck  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*c2c66affSColin Finck  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*c2c66affSColin Finck  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*c2c66affSColin Finck  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGE.
27*c2c66affSColin Finck  */
28*c2c66affSColin Finck 
29*c2c66affSColin Finck /*
30*c2c66affSColin Finck  * netname utility routines
31*c2c66affSColin Finck  * convert from unix names to network names and vice-versa
32*c2c66affSColin Finck  * This module is operating system dependent!
33*c2c66affSColin Finck  * What we define here will work with any unix system that has adopted
34*c2c66affSColin Finck  * the sun NIS domain architecture.
35*c2c66affSColin Finck  */
36*c2c66affSColin Finck 
37*c2c66affSColin Finck #include <wintirpc.h>
38*c2c66affSColin Finck //#include <sys/param.h>
39*c2c66affSColin Finck #include <rpc/rpc.h>
40*c2c66affSColin Finck #include "rpc_com.h"
41*c2c66affSColin Finck #ifdef YP
42*c2c66affSColin Finck #include <rpcsvc/yp_prot.h>
43*c2c66affSColin Finck #include <rpcsvc/ypclnt.h>
44*c2c66affSColin Finck #endif
45*c2c66affSColin Finck #include <ctype.h>
46*c2c66affSColin Finck #include <limits.h>
47*c2c66affSColin Finck #include <stdio.h>
48*c2c66affSColin Finck #include <stdlib.h>
49*c2c66affSColin Finck #include <string.h>
50*c2c66affSColin Finck //#include <unistd.h>
51*c2c66affSColin Finck 
52*c2c66affSColin Finck #ifndef MAXHOSTNAMELEN
53*c2c66affSColin Finck #define MAXHOSTNAMELEN 256
54*c2c66affSColin Finck #endif
55*c2c66affSColin Finck #ifndef NGROUPS
56*c2c66affSColin Finck #define NGROUPS 16
57*c2c66affSColin Finck #endif
58*c2c66affSColin Finck 
59*c2c66affSColin Finck #define TYPE_BIT(type)  (sizeof (type) * CHAR_BIT)
60*c2c66affSColin Finck 
61*c2c66affSColin Finck #define TYPE_SIGNED(type) (((type) -1) < 0)
62*c2c66affSColin Finck 
63*c2c66affSColin Finck /*
64*c2c66affSColin Finck ** 302 / 1000 is log10(2.0) rounded up.
65*c2c66affSColin Finck ** Subtract one for the sign bit if the type is signed;
66*c2c66affSColin Finck ** add one for integer division truncation;
67*c2c66affSColin Finck ** add one more for a minus sign if the type is signed.
68*c2c66affSColin Finck */
69*c2c66affSColin Finck #define INT_STRLEN_MAXIMUM(type) \
70*c2c66affSColin Finck     ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
71*c2c66affSColin Finck 
72*c2c66affSColin Finck static char *OPSYS = "unix";
73*c2c66affSColin Finck 
74*c2c66affSColin Finck /*
75*c2c66affSColin Finck  * Figure out my fully qualified network name
76*c2c66affSColin Finck  */
77*c2c66affSColin Finck int
getnetname(name)78*c2c66affSColin Finck getnetname(name)
79*c2c66affSColin Finck 	char name[MAXNETNAMELEN+1];
80*c2c66affSColin Finck {
81*c2c66affSColin Finck #ifdef _WIN32
82*c2c66affSColin Finck 	/* I don't understand what these underlying routines are accomplishing??? */
83*c2c66affSColin Finck 	return 1;
84*c2c66affSColin Finck #else
85*c2c66affSColin Finck 	uid_t uid;
86*c2c66affSColin Finck 
87*c2c66affSColin Finck 	uid = geteuid();
88*c2c66affSColin Finck 	if (uid == 0) {
89*c2c66affSColin Finck 		return (host2netname(name, (char *) NULL, (char *) NULL));
90*c2c66affSColin Finck 	} else {
91*c2c66affSColin Finck 		return (user2netname(name, uid, (char *) NULL));
92*c2c66affSColin Finck 	}
93*c2c66affSColin Finck #endif
94*c2c66affSColin Finck }
95*c2c66affSColin Finck 
96*c2c66affSColin Finck #ifndef _WIN32
97*c2c66affSColin Finck /*
98*c2c66affSColin Finck  * Convert unix cred to network-name
99*c2c66affSColin Finck  */
100*c2c66affSColin Finck int
user2netname(netname,uid,domain)101*c2c66affSColin Finck user2netname(netname, uid, domain)
102*c2c66affSColin Finck 	char netname[MAXNETNAMELEN + 1];
103*c2c66affSColin Finck 	const uid_t uid;
104*c2c66affSColin Finck 	const char *domain;
105*c2c66affSColin Finck {
106*c2c66affSColin Finck 	char *dfltdom;
107*c2c66affSColin Finck 
108*c2c66affSColin Finck 	if (domain == NULL) {
109*c2c66affSColin Finck 		if (__rpc_get_default_domain(&dfltdom) != 0) {
110*c2c66affSColin Finck 			return (0);
111*c2c66affSColin Finck 		}
112*c2c66affSColin Finck 		domain = dfltdom;
113*c2c66affSColin Finck 	}
114*c2c66affSColin Finck 	if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
115*c2c66affSColin Finck 		return (0);
116*c2c66affSColin Finck 	}
117*c2c66affSColin Finck 	(void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
118*c2c66affSColin Finck 	return (1);
119*c2c66affSColin Finck }
120*c2c66affSColin Finck 
121*c2c66affSColin Finck 
122*c2c66affSColin Finck /*
123*c2c66affSColin Finck  * Convert host to network-name
124*c2c66affSColin Finck  */
125*c2c66affSColin Finck int
host2netname(netname,host,domain)126*c2c66affSColin Finck host2netname(netname, host, domain)
127*c2c66affSColin Finck 	char netname[MAXNETNAMELEN + 1];
128*c2c66affSColin Finck 	const char *host;
129*c2c66affSColin Finck 	const char *domain;
130*c2c66affSColin Finck {
131*c2c66affSColin Finck 	char *dfltdom;
132*c2c66affSColin Finck 	char hostname[MAXHOSTNAMELEN+1];
133*c2c66affSColin Finck 
134*c2c66affSColin Finck 	if (domain == NULL) {
135*c2c66affSColin Finck 		if (__rpc_get_default_domain(&dfltdom) != 0) {
136*c2c66affSColin Finck 			return (0);
137*c2c66affSColin Finck 		}
138*c2c66affSColin Finck 		domain = dfltdom;
139*c2c66affSColin Finck 	}
140*c2c66affSColin Finck 	if (host == NULL) {
141*c2c66affSColin Finck 		(void) gethostname(hostname, sizeof(hostname));
142*c2c66affSColin Finck 		host = hostname;
143*c2c66affSColin Finck 	}
144*c2c66affSColin Finck 	if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
145*c2c66affSColin Finck 		return (0);
146*c2c66affSColin Finck 	}
147*c2c66affSColin Finck 	(void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
148*c2c66affSColin Finck 	return (1);
149*c2c66affSColin Finck }
150*c2c66affSColin Finck #endif
151