Update translations.
[glibc.git] / sunrpc / netname.c
blobbf7f0b81c435f7dfb4d648e4f02ae2b0a74f082c
1 /* Copyright (C) 1997-2022 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>
24 #include "nsswitch.h"
26 #define OPSYS_LEN 4
27 #define MAXIPRINT (11) /* max length of printed integer */
28 static const char OPSYS[] = "unix";
30 int
31 user2netname (char netname[MAXNETNAMELEN + 1], const uid_t uid,
32 const char *domain)
34 char dfltdom[MAXNETNAMELEN + 1];
35 size_t i;
37 if (domain == NULL)
39 if (getdomainname (dfltdom, sizeof (dfltdom)) < 0)
40 return 0;
42 else
44 strncpy (dfltdom, domain, MAXNETNAMELEN);
45 dfltdom[MAXNETNAMELEN] = '\0';
48 if ((strlen (dfltdom) + OPSYS_LEN + 3 + MAXIPRINT) > (size_t) MAXNETNAMELEN)
49 return 0;
51 sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom);
52 i = strlen (netname);
53 if (netname[i - 1] == '.')
54 netname[i - 1] = '\0';
55 return 1;
57 libc_hidden_nolink_sunrpc (user2netname, GLIBC_2_1)
59 int
60 host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
61 const char *domain)
63 char *p;
64 char hostname[MAXHOSTNAMELEN + 1];
65 char domainname[MAXHOSTNAMELEN + 1];
66 char *dot_in_host;
67 size_t i;
69 netname[0] = '\0'; /* make null first (no need for memset) */
71 if (host == NULL)
72 __gethostname (hostname, MAXHOSTNAMELEN);
73 else
75 strncpy (hostname, host, MAXHOSTNAMELEN);
76 hostname[MAXHOSTNAMELEN] = '\0';
79 dot_in_host = strchr (hostname, '.');
80 if (domain == NULL)
82 p = dot_in_host;
83 if (p)
85 ++p;
86 strncpy (domainname, p, MAXHOSTNAMELEN);
87 domainname[MAXHOSTNAMELEN] = '\0';
89 else
91 domainname[0] = 0;
92 getdomainname (domainname, MAXHOSTNAMELEN);
95 else
97 strncpy (domainname, domain, MAXHOSTNAMELEN);
98 domainname[MAXHOSTNAMELEN] = '\0';
101 i = strlen (domainname);
102 if (i == 0)
103 /* No domainname */
104 return 0;
105 if (domainname[i - 1] == '.')
106 domainname[i - 1] = 0;
108 if (dot_in_host) /* strip off rest of name */
109 *dot_in_host = '\0';
111 if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
112 > MAXNETNAMELEN)
113 return 0;
115 sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
116 return 1;
118 #ifdef EXPORT_RPC_SYMBOLS
119 libc_hidden_def (host2netname)
120 #else
121 libc_hidden_nolink_sunrpc (host2netname, GLIBC_2_1)
122 #endif
125 getnetname (char name[MAXNETNAMELEN + 1])
127 uid_t uid;
128 int dummy;
130 uid = __geteuid ();
131 if (uid == 0)
132 dummy = host2netname (name, NULL, NULL);
133 else
134 dummy = user2netname (name, uid, NULL);
135 return (dummy);
137 libc_hidden_nolink_sunrpc (getnetname, GLIBC_2_1)
139 /* Type of the lookup function for netname2user. */
140 typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
141 uid_t *, gid_t *, int *, gid_t *);
144 netname2user (const char *netname, uid_t * uidp, gid_t * gidp,
145 int *gidlenp, gid_t * gidlist)
147 nss_action_list nip;
148 union
150 netname2user_function f;
151 void *ptr;
152 } fct;
153 enum nss_status status = NSS_STATUS_UNAVAIL;
154 int no_more;
156 no_more = __nss_publickey_lookup2 (&nip, "netname2user", NULL, &fct.ptr);
158 while (!no_more)
160 status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
162 no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
165 return status == NSS_STATUS_SUCCESS;
167 #ifdef EXPORT_RPC_SYMBOLS
168 libc_hidden_def (netname2user)
169 #else
170 libc_hidden_nolink_sunrpc (netname2user, GLIBC_2_1)
171 #endif
174 netname2host (const char *netname, char *hostname, const int hostlen)
176 char *p1, *p2;
178 p1 = strchr (netname, '.');
179 if (p1 == NULL)
180 return 0;
181 p1++;
183 p2 = strchr (p1, '@');
184 if (p2 == NULL)
185 return 0;
186 *p2 = '\0';
188 if (hostlen > MAXNETNAMELEN)
189 return 0;
191 strncpy (hostname, p1, hostlen);
192 hostname[hostlen] = '\0';
194 return 1;
196 libc_hidden_nolink_sunrpc (netname2host, GLIBC_2_1)