1 /* Copyright (C) 1997-2023 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 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
;
60 if (netname
[i
- 1] == '.')
61 netname
[i
- 1] = '\0';
64 libc_hidden_nolink_sunrpc (user2netname
, GLIBC_2_1
)
67 host2netname (char netname
[MAXNETNAMELEN
+ 1], const char *host
,
71 char hostname
[MAXHOSTNAMELEN
+ 1];
72 char domainname
[MAXHOSTNAMELEN
+ 1];
76 netname
[0] = '\0'; /* make null first (no need for memset) */
79 __gethostname (hostname
, MAXHOSTNAMELEN
);
82 strncpy (hostname
, host
, MAXHOSTNAMELEN
);
83 hostname
[MAXHOSTNAMELEN
] = '\0';
86 dot_in_host
= strchr (hostname
, '.');
93 strncpy (domainname
, p
, MAXHOSTNAMELEN
);
94 domainname
[MAXHOSTNAMELEN
] = '\0';
99 if (getdomainname (domainname
, MAXHOSTNAMELEN
))
105 strncpy (domainname
, domain
, MAXHOSTNAMELEN
);
106 domainname
[MAXHOSTNAMELEN
] = '\0';
109 i
= strlen (domainname
);
113 if (domainname
[i
- 1] == '.')
114 domainname
[i
- 1] = 0;
116 if (dot_in_host
) /* strip off rest of name */
119 if ((strlen (domainname
) + strlen (hostname
) + OPSYS_LEN
+ 3)
123 sprintf (netname
, "%s.%s@%s", OPSYS
, hostname
, domainname
);
126 #ifdef EXPORT_RPC_SYMBOLS
127 libc_hidden_def (host2netname
)
129 libc_hidden_nolink_sunrpc (host2netname
, GLIBC_2_1
)
133 getnetname (char name
[MAXNETNAMELEN
+ 1])
140 dummy
= host2netname (name
, NULL
, NULL
);
142 dummy
= user2netname (name
, uid
, NULL
);
145 libc_hidden_nolink_sunrpc (getnetname
, GLIBC_2_1
)
147 /* Type of the lookup function for netname2user. */
148 typedef int (*netname2user_function
) (const char netname
[MAXNETNAMELEN
+ 1],
149 uid_t
*, gid_t
*, int *, gid_t
*);
152 netname2user (const char *netname
, uid_t
* uidp
, gid_t
* gidp
,
153 int *gidlenp
, gid_t
* gidlist
)
158 netname2user_function f
;
161 enum nss_status status
= NSS_STATUS_UNAVAIL
;
164 no_more
= __nss_publickey_lookup2 (&nip
, "netname2user", NULL
, &fct
.ptr
);
168 status
= (*fct
.f
) (netname
, uidp
, gidp
, gidlenp
, gidlist
);
170 no_more
= __nss_next2 (&nip
, "netname2user", NULL
, &fct
.ptr
, status
, 0);
173 return status
== NSS_STATUS_SUCCESS
;
175 #ifdef EXPORT_RPC_SYMBOLS
176 libc_hidden_def (netname2user
)
178 libc_hidden_nolink_sunrpc (netname2user
, GLIBC_2_1
)
182 netname2host (const char *netname
, char *hostname
, const int hostlen
)
186 p1
= strchr (netname
, '.');
191 p2
= strchr (p1
, '@');
196 if (hostlen
> MAXNETNAMELEN
)
199 strncpy (hostname
, p1
, hostlen
);
200 hostname
[hostlen
] = '\0';
204 libc_hidden_nolink_sunrpc (netname2host
, GLIBC_2_1
)