crypt: Remove manul entry for --enable-crypt
[glibc.git] / sunrpc / netname.c
blob9e2f78329e48a55d2ae996022b701c2d9bf4391f
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/>. */
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>
25 #include "nsswitch.h"
27 #define OPSYS_LEN 4
28 #define MAXIPRINT (11) /* max length of printed integer */
29 static const char OPSYS[] = "unix";
31 int
32 user2netname (char netname[MAXNETNAMELEN + 1], const uid_t uid,
33 const char *domain)
35 char dfltdom[MAXNETNAMELEN + 1];
36 size_t i;
38 if (domain == NULL)
40 if (getdomainname (dfltdom, sizeof (dfltdom)) < 0)
41 return 0;
43 else
45 strncpy (dfltdom, domain, MAXNETNAMELEN);
46 dfltdom[MAXNETNAMELEN] = '\0';
49 if ((strlen (dfltdom) + OPSYS_LEN + 3 + MAXIPRINT) > (size_t) MAXNETNAMELEN)
50 return 0;
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;
68 libc_hidden_nolink_sunrpc (user2netname, GLIBC_2_1)
70 int
71 host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
72 const char *domain)
74 char *p;
75 char hostname[MAXHOSTNAMELEN + 1];
76 char domainname[MAXHOSTNAMELEN + 1];
77 char *dot_in_host;
78 size_t i;
80 netname[0] = '\0'; /* make null first (no need for memset) */
82 if (host == NULL)
83 __gethostname (hostname, MAXHOSTNAMELEN);
84 else
86 strncpy (hostname, host, MAXHOSTNAMELEN);
87 hostname[MAXHOSTNAMELEN] = '\0';
90 dot_in_host = strchr (hostname, '.');
91 if (domain == NULL)
93 p = dot_in_host;
94 if (p)
96 ++p;
97 strncpy (domainname, p, MAXHOSTNAMELEN);
98 domainname[MAXHOSTNAMELEN] = '\0';
100 else
102 domainname[0] = 0;
103 if (getdomainname (domainname, MAXHOSTNAMELEN))
104 return 0;
107 else
109 strncpy (domainname, domain, MAXHOSTNAMELEN);
110 domainname[MAXHOSTNAMELEN] = '\0';
113 i = strlen (domainname);
114 if (i == 0)
115 /* No domainname */
116 return 0;
117 if (domainname[i - 1] == '.')
118 domainname[i - 1] = 0;
120 if (dot_in_host) /* strip off rest of name */
121 *dot_in_host = '\0';
123 if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
124 > MAXNETNAMELEN)
125 return 0;
127 sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
128 return 1;
130 #ifdef EXPORT_RPC_SYMBOLS
131 libc_hidden_def (host2netname)
132 #else
133 libc_hidden_nolink_sunrpc (host2netname, GLIBC_2_1)
134 #endif
137 getnetname (char name[MAXNETNAMELEN + 1])
139 uid_t uid;
140 int dummy;
142 uid = __geteuid ();
143 if (uid == 0)
144 dummy = host2netname (name, NULL, NULL);
145 else
146 dummy = user2netname (name, uid, NULL);
147 return (dummy);
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)
159 nss_action_list nip;
160 union
162 netname2user_function f;
163 void *ptr;
164 } fct;
165 enum nss_status status = NSS_STATUS_UNAVAIL;
166 int no_more;
168 no_more = __nss_publickey_lookup2 (&nip, "netname2user", NULL, &fct.ptr);
170 while (!no_more)
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)
181 #else
182 libc_hidden_nolink_sunrpc (netname2user, GLIBC_2_1)
183 #endif
186 netname2host (const char *netname, char *hostname, const int hostlen)
188 char *p1, *p2;
190 p1 = strchr (netname, '.');
191 if (p1 == NULL)
192 return 0;
193 p1++;
195 p2 = strchr (p1, '@');
196 if (p2 == NULL)
197 return 0;
198 *p2 = '\0';
200 if (hostlen > MAXNETNAMELEN)
201 return 0;
203 strncpy (hostname, p1, hostlen);
204 hostname[hostlen] = '\0';
206 return 1;
208 libc_hidden_nolink_sunrpc (netname2host, GLIBC_2_1)