]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/netname.c
Add glibc.malloc.mxfast tunable
[thirdparty/glibc.git] / sunrpc / netname.c
CommitLineData
688903eb 1/* Copyright (C) 1997-2018 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>
82f43dd2 23#include <shlib-compat.h>
800d775e
UD
24
25#include "nsswitch.h"
26
27#define OPSYS_LEN 4
28#define MAXIPRINT (11) /* max length of printed integer */
c4563d2d 29static const char OPSYS[] = "unix";
800d775e
UD
30
31int
32user2netname (char netname[MAXNETNAMELEN + 1], const uid_t uid,
33 const char *domain)
34{
35 char dfltdom[MAXNETNAMELEN + 1];
36 size_t i;
37
38 if (domain == NULL)
39 {
40 if (getdomainname (dfltdom, sizeof (dfltdom)) < 0)
41 return 0;
42 }
43 else
44 {
45 strncpy (dfltdom, domain, MAXNETNAMELEN);
46 dfltdom[MAXNETNAMELEN] = '\0';
47 }
48
49 if ((strlen (dfltdom) + OPSYS_LEN + 3 + MAXIPRINT) > (size_t) MAXNETNAMELEN)
50 return 0;
51
52 sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom);
53 i = strlen (netname);
54 if (netname[i - 1] == '.')
55 netname[i - 1] = '\0';
56 return 1;
57}
021db4be 58libc_hidden_nolink_sunrpc (user2netname, GLIBC_2_1)
800d775e
UD
59
60int
61host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
62 const char *domain)
63{
64 char *p;
65 char hostname[MAXHOSTNAMELEN + 1];
66 char domainname[MAXHOSTNAMELEN + 1];
67 char *dot_in_host;
68 size_t i;
69
70 netname[0] = '\0'; /* make null first (no need for memset) */
71
72 if (host == NULL)
50304ef0 73 __gethostname (hostname, MAXHOSTNAMELEN);
800d775e
UD
74 else
75 {
76 strncpy (hostname, host, MAXHOSTNAMELEN);
77 hostname[MAXHOSTNAMELEN] = '\0';
78 }
79
80 dot_in_host = strchr (hostname, '.');
81 if (domain == NULL)
82 {
83 p = dot_in_host;
84 if (p)
85 {
26a60f90 86 ++p;
800d775e
UD
87 strncpy (domainname, p, MAXHOSTNAMELEN);
88 domainname[MAXHOSTNAMELEN] = '\0';
89 }
90 else
91 {
92 domainname[0] = 0;
93 getdomainname (domainname, MAXHOSTNAMELEN);
94 }
95 }
96 else
97 {
98 strncpy (domainname, domain, MAXHOSTNAMELEN);
99 domainname[MAXHOSTNAMELEN] = '\0';
100 }
101
102 i = strlen (domainname);
103 if (i == 0)
104 /* No domainname */
105 return 0;
106 if (domainname[i - 1] == '.')
107 domainname[i - 1] = 0;
108
109 if (dot_in_host) /* strip off rest of name */
110 *dot_in_host = '\0';
111
112 if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
113 > MAXNETNAMELEN)
114 return 0;
115
116 sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
117 return 1;
118}
7b57bfe5 119#ifdef EXPORT_RPC_SYMBOLS
a585ba22 120libc_hidden_def (host2netname)
7b57bfe5 121#else
021db4be 122libc_hidden_nolink_sunrpc (host2netname, GLIBC_2_1)
7b57bfe5 123#endif
800d775e
UD
124
125int
126getnetname (char name[MAXNETNAMELEN + 1])
127{
128 uid_t uid;
129 int dummy;
130
50304ef0 131 uid = __geteuid ();
800d775e
UD
132 if (uid == 0)
133 dummy = host2netname (name, NULL, NULL);
134 else
135 dummy = user2netname (name, uid, NULL);
136 return (dummy);
137}
021db4be 138libc_hidden_nolink_sunrpc (getnetname, GLIBC_2_1)
800d775e
UD
139
140/* Type of the lookup function for netname2user. */
141typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
142 uid_t *, gid_t *, int *, gid_t *);
800d775e
UD
143
144int
145netname2user (const char netname[MAXNETNAMELEN + 1], uid_t * uidp, gid_t * gidp,
146 int *gidlenp, gid_t * gidlist)
147{
c4563d2d 148 static service_user *startp;
800d775e
UD
149 static netname2user_function start_fct;
150 service_user *nip;
fb776f3e
AJ
151 union
152 {
153 netname2user_function f;
154 void *ptr;
155 } fct;
800d775e
UD
156 enum nss_status status = NSS_STATUS_UNAVAIL;
157 int no_more;
158
159 if (startp == NULL)
160 {
c88ffc23 161 no_more = __nss_publickey_lookup2 (&nip, "netname2user", NULL, &fct.ptr);
800d775e
UD
162 if (no_more)
163 startp = (service_user *) - 1;
164 else
165 {
166 startp = nip;
fb776f3e 167 start_fct = fct.f;
800d775e
UD
168 }
169 }
170 else
171 {
fb776f3e 172 fct.f = start_fct;
800d775e
UD
173 no_more = (nip = startp) == (service_user *) - 1;
174 }
175
176 while (!no_more)
177 {
fb776f3e 178 status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
800d775e 179
384ca551 180 no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
800d775e
UD
181 }
182
183 return status == NSS_STATUS_SUCCESS;
184}
7b57bfe5 185#ifdef EXPORT_RPC_SYMBOLS
a585ba22 186libc_hidden_def (netname2user)
7b57bfe5 187#else
021db4be 188libc_hidden_nolink_sunrpc (netname2user, GLIBC_2_1)
7b57bfe5 189#endif
800d775e
UD
190
191int
192netname2host (const char netname[MAXNETNAMELEN + 1], char *hostname,
193 const int hostlen)
194{
195 char *p1, *p2;
800d775e 196
f9efbf3a 197 p1 = strchr (netname, '.');
800d775e
UD
198 if (p1 == NULL)
199 return 0;
200 p1++;
201
202 p2 = strchr (p1, '@');
203 if (p2 == NULL)
204 return 0;
205 *p2 = '\0';
206
207 if (hostlen > MAXNETNAMELEN)
208 return 0;
209
210 strncpy (hostname, p1, hostlen);
211 hostname[hostlen] = '\0';
212
213 return 1;
214}
021db4be 215libc_hidden_nolink_sunrpc (netname2host, GLIBC_2_1)