1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
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 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
32 | static char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro"; |
---|
33 | #endif |
---|
34 | #include <sys/cdefs.h> |
---|
35 | __FBSDID("$FreeBSD$"); |
---|
36 | |
---|
37 | /* |
---|
38 | * netname utility routines |
---|
39 | * convert from unix names to network names and vice-versa |
---|
40 | * This module is operating system dependent! |
---|
41 | * What we define here will work with any unix system that has adopted |
---|
42 | * the sun NIS domain architecture. |
---|
43 | */ |
---|
44 | |
---|
45 | #include "namespace.h" |
---|
46 | #include <rtems/bsd/sys/param.h> |
---|
47 | #include <rpc/rpc.h> |
---|
48 | #include <rpc/rpc_com.h> |
---|
49 | #ifdef YP |
---|
50 | #include <rpcsvc/yp_prot.h> |
---|
51 | #include <rpcsvc/ypclnt.h> |
---|
52 | #endif |
---|
53 | #include <ctype.h> |
---|
54 | #include <limits.h> |
---|
55 | #include <stdio.h> |
---|
56 | #include <stdlib.h> |
---|
57 | #include <string.h> |
---|
58 | #include <unistd.h> |
---|
59 | #include "un-namespace.h" |
---|
60 | |
---|
61 | #ifndef MAXHOSTNAMELEN |
---|
62 | #define MAXHOSTNAMELEN 256 |
---|
63 | #endif |
---|
64 | |
---|
65 | #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT) |
---|
66 | |
---|
67 | #define TYPE_SIGNED(type) (((type) -1) < 0) |
---|
68 | |
---|
69 | /* |
---|
70 | ** 302 / 1000 is log10(2.0) rounded up. |
---|
71 | ** Subtract one for the sign bit if the type is signed; |
---|
72 | ** add one for integer division truncation; |
---|
73 | ** add one more for a minus sign if the type is signed. |
---|
74 | */ |
---|
75 | #define INT_STRLEN_MAXIMUM(type) \ |
---|
76 | ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type)) |
---|
77 | |
---|
78 | static char *OPSYS = "unix"; |
---|
79 | |
---|
80 | /* |
---|
81 | * Figure out my fully qualified network name |
---|
82 | */ |
---|
83 | int |
---|
84 | getnetname(name) |
---|
85 | char name[MAXNETNAMELEN+1]; |
---|
86 | { |
---|
87 | uid_t uid; |
---|
88 | |
---|
89 | uid = geteuid(); |
---|
90 | if (uid == 0) { |
---|
91 | return (host2netname(name, (char *) NULL, (char *) NULL)); |
---|
92 | } else { |
---|
93 | return (user2netname(name, uid, (char *) NULL)); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /* |
---|
99 | * Convert unix cred to network-name |
---|
100 | */ |
---|
101 | int |
---|
102 | user2netname(netname, uid, domain) |
---|
103 | char netname[MAXNETNAMELEN + 1]; |
---|
104 | const uid_t uid; |
---|
105 | const char *domain; |
---|
106 | { |
---|
107 | char *dfltdom; |
---|
108 | |
---|
109 | if (domain == NULL) { |
---|
110 | if (__rpc_get_default_domain(&dfltdom) != 0) { |
---|
111 | return (0); |
---|
112 | } |
---|
113 | domain = dfltdom; |
---|
114 | } |
---|
115 | if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) { |
---|
116 | return (0); |
---|
117 | } |
---|
118 | (void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain); |
---|
119 | return (1); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | /* |
---|
124 | * Convert host to network-name |
---|
125 | */ |
---|
126 | int |
---|
127 | host2netname(netname, host, domain) |
---|
128 | char netname[MAXNETNAMELEN + 1]; |
---|
129 | const char *host; |
---|
130 | const char *domain; |
---|
131 | { |
---|
132 | char *dfltdom; |
---|
133 | char hostname[MAXHOSTNAMELEN+1]; |
---|
134 | |
---|
135 | if (domain == NULL) { |
---|
136 | if (__rpc_get_default_domain(&dfltdom) != 0) { |
---|
137 | return (0); |
---|
138 | } |
---|
139 | domain = dfltdom; |
---|
140 | } |
---|
141 | if (host == NULL) { |
---|
142 | (void) gethostname(hostname, sizeof(hostname)); |
---|
143 | host = hostname; |
---|
144 | } |
---|
145 | if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) { |
---|
146 | return (0); |
---|
147 | } |
---|
148 | (void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain); |
---|
149 | return (1); |
---|
150 | } |
---|