]>
Commit | Line | Data |
---|---|---|
f9efbf3a | 1 | /* Copyright (C) 1997-1999, 2002, 2007, 2011 Free Software Foundation, Inc. |
800d775e UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
800d775e UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
800d775e | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
800d775e UD |
18 | |
19 | #include <stdio.h> | |
20 | #include <unistd.h> | |
21 | #include <string.h> | |
22 | #include <rpc/rpc.h> | |
23 | ||
24 | #include "nsswitch.h" | |
25 | ||
26 | #define OPSYS_LEN 4 | |
27 | #define MAXIPRINT (11) /* max length of printed integer */ | |
c4563d2d | 28 | static const char OPSYS[] = "unix"; |
800d775e UD |
29 | |
30 | int | |
31 | user2netname (char netname[MAXNETNAMELEN + 1], const uid_t uid, | |
32 | const char *domain) | |
33 | { | |
34 | char dfltdom[MAXNETNAMELEN + 1]; | |
35 | size_t i; | |
36 | ||
37 | if (domain == NULL) | |
38 | { | |
39 | if (getdomainname (dfltdom, sizeof (dfltdom)) < 0) | |
40 | return 0; | |
41 | } | |
42 | else | |
43 | { | |
44 | strncpy (dfltdom, domain, MAXNETNAMELEN); | |
45 | dfltdom[MAXNETNAMELEN] = '\0'; | |
46 | } | |
47 | ||
48 | if ((strlen (dfltdom) + OPSYS_LEN + 3 + MAXIPRINT) > (size_t) MAXNETNAMELEN) | |
49 | return 0; | |
50 | ||
51 | sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom); | |
52 | i = strlen (netname); | |
53 | if (netname[i - 1] == '.') | |
54 | netname[i - 1] = '\0'; | |
55 | return 1; | |
56 | } | |
7b57bfe5 | 57 | libc_hidden_nolink (user2netname, GLIBC_2_1) |
800d775e UD |
58 | |
59 | int | |
60 | host2netname (char netname[MAXNETNAMELEN + 1], const char *host, | |
61 | const char *domain) | |
62 | { | |
63 | char *p; | |
64 | char hostname[MAXHOSTNAMELEN + 1]; | |
65 | char domainname[MAXHOSTNAMELEN + 1]; | |
66 | char *dot_in_host; | |
67 | size_t i; | |
68 | ||
69 | netname[0] = '\0'; /* make null first (no need for memset) */ | |
70 | ||
71 | if (host == NULL) | |
50304ef0 | 72 | __gethostname (hostname, MAXHOSTNAMELEN); |
800d775e UD |
73 | else |
74 | { | |
75 | strncpy (hostname, host, MAXHOSTNAMELEN); | |
76 | hostname[MAXHOSTNAMELEN] = '\0'; | |
77 | } | |
78 | ||
79 | dot_in_host = strchr (hostname, '.'); | |
80 | if (domain == NULL) | |
81 | { | |
82 | p = dot_in_host; | |
83 | if (p) | |
84 | { | |
26a60f90 | 85 | ++p; |
800d775e UD |
86 | strncpy (domainname, p, MAXHOSTNAMELEN); |
87 | domainname[MAXHOSTNAMELEN] = '\0'; | |
88 | } | |
89 | else | |
90 | { | |
91 | domainname[0] = 0; | |
92 | getdomainname (domainname, MAXHOSTNAMELEN); | |
93 | } | |
94 | } | |
95 | else | |
96 | { | |
97 | strncpy (domainname, domain, MAXHOSTNAMELEN); | |
98 | domainname[MAXHOSTNAMELEN] = '\0'; | |
99 | } | |
100 | ||
101 | i = strlen (domainname); | |
102 | if (i == 0) | |
103 | /* No domainname */ | |
104 | return 0; | |
105 | if (domainname[i - 1] == '.') | |
106 | domainname[i - 1] = 0; | |
107 | ||
108 | if (dot_in_host) /* strip off rest of name */ | |
109 | *dot_in_host = '\0'; | |
110 | ||
111 | if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3) | |
112 | > MAXNETNAMELEN) | |
113 | return 0; | |
114 | ||
115 | sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname); | |
116 | return 1; | |
117 | } | |
7b57bfe5 | 118 | #ifdef EXPORT_RPC_SYMBOLS |
a585ba22 | 119 | libc_hidden_def (host2netname) |
7b57bfe5 UD |
120 | #else |
121 | libc_hidden_nolink (host2netname, GLIBC_2_1) | |
122 | #endif | |
800d775e UD |
123 | |
124 | int | |
125 | getnetname (char name[MAXNETNAMELEN + 1]) | |
126 | { | |
127 | uid_t uid; | |
128 | int dummy; | |
129 | ||
50304ef0 | 130 | uid = __geteuid (); |
800d775e UD |
131 | if (uid == 0) |
132 | dummy = host2netname (name, NULL, NULL); | |
133 | else | |
134 | dummy = user2netname (name, uid, NULL); | |
135 | return (dummy); | |
136 | } | |
7b57bfe5 | 137 | libc_hidden_nolink (getnetname, GLIBC_2_1) |
800d775e UD |
138 | |
139 | /* Type of the lookup function for netname2user. */ | |
140 | typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1], | |
141 | uid_t *, gid_t *, int *, gid_t *); | |
142 | /* The lookup function for the first entry of this service. */ | |
143 | extern int __nss_publickey_lookup (service_user ** nip, const char *name, | |
f7ddf3d3 | 144 | void **fctp) internal_function; |
800d775e UD |
145 | |
146 | int | |
147 | netname2user (const char netname[MAXNETNAMELEN + 1], uid_t * uidp, gid_t * gidp, | |
148 | int *gidlenp, gid_t * gidlist) | |
149 | { | |
c4563d2d | 150 | static service_user *startp; |
800d775e UD |
151 | static netname2user_function start_fct; |
152 | service_user *nip; | |
fb776f3e AJ |
153 | union |
154 | { | |
155 | netname2user_function f; | |
156 | void *ptr; | |
157 | } fct; | |
800d775e UD |
158 | enum nss_status status = NSS_STATUS_UNAVAIL; |
159 | int no_more; | |
160 | ||
161 | if (startp == NULL) | |
162 | { | |
fb776f3e | 163 | no_more = __nss_publickey_lookup (&nip, "netname2user", &fct.ptr); |
800d775e UD |
164 | if (no_more) |
165 | startp = (service_user *) - 1; | |
166 | else | |
167 | { | |
168 | startp = nip; | |
fb776f3e | 169 | start_fct = fct.f; |
800d775e UD |
170 | } |
171 | } | |
172 | else | |
173 | { | |
fb776f3e | 174 | fct.f = start_fct; |
800d775e UD |
175 | no_more = (nip = startp) == (service_user *) - 1; |
176 | } | |
177 | ||
178 | while (!no_more) | |
179 | { | |
fb776f3e | 180 | status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist); |
800d775e | 181 | |
384ca551 | 182 | no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0); |
800d775e UD |
183 | } |
184 | ||
185 | return status == NSS_STATUS_SUCCESS; | |
186 | } | |
7b57bfe5 | 187 | #ifdef EXPORT_RPC_SYMBOLS |
a585ba22 | 188 | libc_hidden_def (netname2user) |
7b57bfe5 UD |
189 | #else |
190 | libc_hidden_nolink (netname2user, GLIBC_2_1) | |
191 | #endif | |
800d775e UD |
192 | |
193 | int | |
194 | netname2host (const char netname[MAXNETNAMELEN + 1], char *hostname, | |
195 | const int hostlen) | |
196 | { | |
197 | char *p1, *p2; | |
800d775e | 198 | |
f9efbf3a | 199 | p1 = strchr (netname, '.'); |
800d775e UD |
200 | if (p1 == NULL) |
201 | return 0; | |
202 | p1++; | |
203 | ||
204 | p2 = strchr (p1, '@'); | |
205 | if (p2 == NULL) | |
206 | return 0; | |
207 | *p2 = '\0'; | |
208 | ||
209 | if (hostlen > MAXNETNAMELEN) | |
210 | return 0; | |
211 | ||
212 | strncpy (hostname, p1, hostlen); | |
213 | hostname[hostlen] = '\0'; | |
214 | ||
215 | return 1; | |
216 | } | |
7b57bfe5 | 217 | libc_hidden_nolink (netname2host, GLIBC_2_1) |