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