1 /* Copyright (C) 1997-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #include <shlib-compat.h>
23 #include <libc-diag.h>
28 #define MAXIPRINT (11) /* max length of printed integer */
29 static const char OPSYS
[] = "unix";
32 user2netname (char netname
[MAXNETNAMELEN
+ 1], const uid_t uid
,
35 char dfltdom
[MAXNETNAMELEN
+ 1];
40 if (getdomainname (dfltdom
, sizeof (dfltdom
)) < 0)
45 strncpy (dfltdom
, domain
, MAXNETNAMELEN
);
46 dfltdom
[MAXNETNAMELEN
] = '\0';
49 if ((strlen (dfltdom
) + OPSYS_LEN
+ 3 + MAXIPRINT
) > (size_t) MAXNETNAMELEN
)
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
55 #if __GNUC_PREREQ (8, 0)
56 DIAG_PUSH_NEEDS_COMMENT
;
57 DIAG_IGNORE_NEEDS_COMMENT (8, "-Wformat-overflow");
59 sprintf (netname
, "%s.%d@%s", OPSYS
, uid
, dfltdom
);
60 #if __GNUC_PREREQ (8, 0)
61 DIAG_POP_NEEDS_COMMENT
;
64 if (netname
[i
- 1] == '.')
65 netname
[i
- 1] = '\0';
68 libc_hidden_nolink_sunrpc (user2netname
, GLIBC_2_1
)
71 host2netname (char netname
[MAXNETNAMELEN
+ 1], const char *host
,
75 char hostname
[MAXHOSTNAMELEN
+ 1];
76 char domainname
[MAXHOSTNAMELEN
+ 1];
80 netname
[0] = '\0'; /* make null first (no need for memset) */
83 __gethostname (hostname
, MAXHOSTNAMELEN
);
86 strncpy (hostname
, host
, MAXHOSTNAMELEN
);
87 hostname
[MAXHOSTNAMELEN
] = '\0';
90 dot_in_host
= strchr (hostname
, '.');
97 strncpy (domainname
, p
, MAXHOSTNAMELEN
);
98 domainname
[MAXHOSTNAMELEN
] = '\0';
103 if (getdomainname (domainname
, MAXHOSTNAMELEN
))
109 strncpy (domainname
, domain
, MAXHOSTNAMELEN
);
110 domainname
[MAXHOSTNAMELEN
] = '\0';
113 i
= strlen (domainname
);
117 if (domainname
[i
- 1] == '.')
118 domainname
[i
- 1] = 0;
120 if (dot_in_host
) /* strip off rest of name */
123 if ((strlen (domainname
) + strlen (hostname
) + OPSYS_LEN
+ 3)
127 sprintf (netname
, "%s.%s@%s", OPSYS
, hostname
, domainname
);
130 #ifdef EXPORT_RPC_SYMBOLS
131 libc_hidden_def (host2netname
)
133 libc_hidden_nolink_sunrpc (host2netname
, GLIBC_2_1
)
137 getnetname (char name
[MAXNETNAMELEN
+ 1])
144 dummy
= host2netname (name
, NULL
, NULL
);
146 dummy
= user2netname (name
, uid
, NULL
);
149 libc_hidden_nolink_sunrpc (getnetname
, GLIBC_2_1
)
151 /* Type of the lookup function for netname2user. */
152 typedef int (*netname2user_function
) (const char netname
[MAXNETNAMELEN
+ 1],
153 uid_t
*, gid_t
*, int *, gid_t
*);
156 netname2user (const char *netname
, uid_t
* uidp
, gid_t
* gidp
,
157 int *gidlenp
, gid_t
* gidlist
)
162 netname2user_function f
;
165 enum nss_status status
= NSS_STATUS_UNAVAIL
;
168 no_more
= __nss_publickey_lookup2 (&nip
, "netname2user", NULL
, &fct
.ptr
);
172 status
= (*fct
.f
) (netname
, uidp
, gidp
, gidlenp
, gidlist
);
174 no_more
= __nss_next2 (&nip
, "netname2user", NULL
, &fct
.ptr
, status
, 0);
177 return status
== NSS_STATUS_SUCCESS
;
179 #ifdef EXPORT_RPC_SYMBOLS
180 libc_hidden_def (netname2user
)
182 libc_hidden_nolink_sunrpc (netname2user
, GLIBC_2_1
)
186 netname2host (const char *netname
, char *hostname
, const int hostlen
)
190 p1
= strchr (netname
, '.');
195 p2
= strchr (p1
, '@');
200 if (hostlen
> MAXNETNAMELEN
)
203 strncpy (hostname
, p1
, hostlen
);
204 hostname
[hostlen
] = '\0';
208 libc_hidden_nolink_sunrpc (netname2host
, GLIBC_2_1
)