1/* Copyright (C) 1997-2024 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#if __GNUC_PREREQ (8, 0)
56 DIAG_PUSH_NEEDS_COMMENT;
57 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wformat-overflow");
58#endif
59 sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom);
60#if __GNUC_PREREQ (8, 0)
61 DIAG_POP_NEEDS_COMMENT;
62#endif
63 i = strlen (netname);
64 if (netname[i - 1] == '.')
65 netname[i - 1] = '\0';
66 return 1;
67}
68libc_hidden_nolink_sunrpc (user2netname, GLIBC_2_1)
69
70int
71host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
72 const char *domain)
73{
74 char *p;
75 char hostname[MAXHOSTNAMELEN + 1];
76 char domainname[MAXHOSTNAMELEN + 1];
77 char *dot_in_host;
78 size_t i;
79
80 netname[0] = '\0'; /* make null first (no need for memset) */
81
82 if (host == NULL)
83 __gethostname (name: hostname, MAXHOSTNAMELEN);
84 else
85 {
86 strncpy (hostname, host, MAXHOSTNAMELEN);
87 hostname[MAXHOSTNAMELEN] = '\0';
88 }
89
90 dot_in_host = strchr (hostname, '.');
91 if (domain == NULL)
92 {
93 p = dot_in_host;
94 if (p)
95 {
96 ++p;
97 strncpy (domainname, p, MAXHOSTNAMELEN);
98 domainname[MAXHOSTNAMELEN] = '\0';
99 }
100 else
101 {
102 domainname[0] = 0;
103 if (getdomainname (domainname, MAXHOSTNAMELEN))
104 return 0;
105 }
106 }
107 else
108 {
109 strncpy (domainname, domain, MAXHOSTNAMELEN);
110 domainname[MAXHOSTNAMELEN] = '\0';
111 }
112
113 i = strlen (domainname);
114 if (i == 0)
115 /* No domainname */
116 return 0;
117 if (domainname[i - 1] == '.')
118 domainname[i - 1] = 0;
119
120 if (dot_in_host) /* strip off rest of name */
121 *dot_in_host = '\0';
122
123 if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
124 > MAXNETNAMELEN)
125 return 0;
126
127 sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
128 return 1;
129}
130#ifdef EXPORT_RPC_SYMBOLS
131libc_hidden_def (host2netname)
132#else
133libc_hidden_nolink_sunrpc (host2netname, GLIBC_2_1)
134#endif
135
136int
137getnetname (char name[MAXNETNAMELEN + 1])
138{
139 uid_t uid;
140 int dummy;
141
142 uid = __geteuid ();
143 if (uid == 0)
144 dummy = host2netname (netname: name, NULL, NULL);
145 else
146 dummy = user2netname (netname: name, uid, NULL);
147 return (dummy);
148}
149libc_hidden_nolink_sunrpc (getnetname, GLIBC_2_1)
150
151/* Type of the lookup function for netname2user. */
152typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
153 uid_t *, gid_t *, int *, gid_t *);
154
155int
156netname2user (const char *netname, uid_t * uidp, gid_t * gidp,
157 int *gidlenp, gid_t * gidlist)
158{
159 nss_action_list nip;
160 union
161 {
162 netname2user_function f;
163 void *ptr;
164 } fct;
165 enum nss_status status = NSS_STATUS_UNAVAIL;
166 int no_more;
167
168 no_more = __nss_publickey_lookup2 (&nip, "netname2user", NULL, &fct.ptr);
169
170 while (!no_more)
171 {
172 status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
173
174 no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
175 }
176
177 return status == NSS_STATUS_SUCCESS;
178}
179#ifdef EXPORT_RPC_SYMBOLS
180libc_hidden_def (netname2user)
181#else
182libc_hidden_nolink_sunrpc (netname2user, GLIBC_2_1)
183#endif
184
185int
186netname2host (const char *netname, char *hostname, const int hostlen)
187{
188 char *p1, *p2;
189
190 p1 = strchr (netname, '.');
191 if (p1 == NULL)
192 return 0;
193 p1++;
194
195 p2 = strchr (p1, '@');
196 if (p2 == NULL)
197 return 0;
198 *p2 = '\0';
199
200 if (hostlen > MAXNETNAMELEN)
201 return 0;
202
203 strncpy (hostname, p1, hostlen);
204 hostname[hostlen] = '\0';
205
206 return 1;
207}
208libc_hidden_nolink_sunrpc (netname2host, GLIBC_2_1)
209

source code of glibc/sunrpc/netname.c