1/* Copyright (C) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <stdio.h>
19#include <unistd.h>
20#include <string.h>
21#include <rpc/rpc.h>
22#include <shlib-compat.h>
23#include <libc-diag.h>
24
25#include "nsswitch.h"
26
27#define OPSYS_LEN 4
28#define MAXIPRINT (11) /* max length of printed integer */
29static const char OPSYS[] = "unix";
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 /* GCC with -Os or -O1 warns that sprint might overflow while handling
53 dfltdom, however the above test does check if an overflow would
54 happen. */
55 DIAG_PUSH_NEEDS_COMMENT;
56 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wformat-overflow");
57 sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom);
58 DIAG_POP_NEEDS_COMMENT;
59 i = strlen (netname);
60 if (netname[i - 1] == '.')
61 netname[i - 1] = '\0';
62 return 1;
63}
64libc_hidden_nolink_sunrpc (user2netname, GLIBC_2_1)
65
66int
67host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
68 const char *domain)
69{
70 char *p;
71 char hostname[MAXHOSTNAMELEN + 1];
72 char domainname[MAXHOSTNAMELEN + 1];
73 char *dot_in_host;
74 size_t i;
75
76 netname[0] = '\0'; /* make null first (no need for memset) */
77
78 if (host == NULL)
79 __gethostname (hostname, MAXHOSTNAMELEN);
80 else
81 {
82 strncpy (hostname, host, MAXHOSTNAMELEN);
83 hostname[MAXHOSTNAMELEN] = '\0';
84 }
85
86 dot_in_host = strchr (hostname, '.');
87 if (domain == NULL)
88 {
89 p = dot_in_host;
90 if (p)
91 {
92 ++p;
93 strncpy (domainname, p, MAXHOSTNAMELEN);
94 domainname[MAXHOSTNAMELEN] = '\0';
95 }
96 else
97 {
98 domainname[0] = 0;
99 getdomainname (domainname, MAXHOSTNAMELEN);
100 }
101 }
102 else
103 {
104 strncpy (domainname, domain, MAXHOSTNAMELEN);
105 domainname[MAXHOSTNAMELEN] = '\0';
106 }
107
108 i = strlen (domainname);
109 if (i == 0)
110 /* No domainname */
111 return 0;
112 if (domainname[i - 1] == '.')
113 domainname[i - 1] = 0;
114
115 if (dot_in_host) /* strip off rest of name */
116 *dot_in_host = '\0';
117
118 if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
119 > MAXNETNAMELEN)
120 return 0;
121
122 sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
123 return 1;
124}
125#ifdef EXPORT_RPC_SYMBOLS
126libc_hidden_def (host2netname)
127#else
128libc_hidden_nolink_sunrpc (host2netname, GLIBC_2_1)
129#endif
130
131int
132getnetname (char name[MAXNETNAMELEN + 1])
133{
134 uid_t uid;
135 int dummy;
136
137 uid = __geteuid ();
138 if (uid == 0)
139 dummy = host2netname (name, NULL, NULL);
140 else
141 dummy = user2netname (name, uid, NULL);
142 return (dummy);
143}
144libc_hidden_nolink_sunrpc (getnetname, GLIBC_2_1)
145
146/* Type of the lookup function for netname2user. */
147typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
148 uid_t *, gid_t *, int *, gid_t *);
149
150int
151netname2user (const char *netname, uid_t * uidp, gid_t * gidp,
152 int *gidlenp, gid_t * gidlist)
153{
154 nss_action_list nip;
155 union
156 {
157 netname2user_function f;
158 void *ptr;
159 } fct;
160 enum nss_status status = NSS_STATUS_UNAVAIL;
161 int no_more;
162
163 no_more = __nss_publickey_lookup2 (&nip, "netname2user", NULL, &fct.ptr);
164
165 while (!no_more)
166 {
167 status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
168
169 no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
170 }
171
172 return status == NSS_STATUS_SUCCESS;
173}
174#ifdef EXPORT_RPC_SYMBOLS
175libc_hidden_def (netname2user)
176#else
177libc_hidden_nolink_sunrpc (netname2user, GLIBC_2_1)
178#endif
179
180int
181netname2host (const char *netname, char *hostname, const int hostlen)
182{
183 char *p1, *p2;
184
185 p1 = strchr (netname, '.');
186 if (p1 == NULL)
187 return 0;
188 p1++;
189
190 p2 = strchr (p1, '@');
191 if (p2 == NULL)
192 return 0;
193 *p2 = '\0';
194
195 if (hostlen > MAXNETNAMELEN)
196 return 0;
197
198 strncpy (hostname, p1, hostlen);
199 hostname[hostlen] = '\0';
200
201 return 1;
202}
203libc_hidden_nolink_sunrpc (netname2host, GLIBC_2_1)
204