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