]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/netname.c
* include/rpc/svc.h: Use libc_hidden_proto for xprt_register,
[thirdparty/glibc.git] / sunrpc / netname.c
CommitLineData
f7ddf3d3 1/* Copyright (C) 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
800d775e
UD
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
800d775e
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
800d775e 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
800d775e
UD
19
20#include <stdio.h>
21#include <unistd.h>
22#include <string.h>
23#include <rpc/rpc.h>
24
25#include "nsswitch.h"
26
27#define OPSYS_LEN 4
28#define MAXIPRINT (11) /* max length of printed integer */
c4563d2d 29static const char OPSYS[] = "unix";
800d775e
UD
30
31int
32user2netname (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 sprintf (netname, "%s.%d@%s", OPSYS, uid, dfltdom);
53 i = strlen (netname);
54 if (netname[i - 1] == '.')
55 netname[i - 1] = '\0';
56 return 1;
57}
58
59int
60host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
61 const char *domain)
62{
63 char *p;
64 char hostname[MAXHOSTNAMELEN + 1];
65 char domainname[MAXHOSTNAMELEN + 1];
66 char *dot_in_host;
67 size_t i;
68
69 netname[0] = '\0'; /* make null first (no need for memset) */
70
71 if (host == NULL)
50304ef0 72 __gethostname (hostname, MAXHOSTNAMELEN);
800d775e
UD
73 else
74 {
75 strncpy (hostname, host, MAXHOSTNAMELEN);
76 hostname[MAXHOSTNAMELEN] = '\0';
77 }
78
79 dot_in_host = strchr (hostname, '.');
80 if (domain == NULL)
81 {
82 p = dot_in_host;
83 if (p)
84 {
26a60f90 85 ++p;
800d775e
UD
86 strncpy (domainname, p, MAXHOSTNAMELEN);
87 domainname[MAXHOSTNAMELEN] = '\0';
88 }
89 else
90 {
91 domainname[0] = 0;
92 getdomainname (domainname, MAXHOSTNAMELEN);
93 }
94 }
95 else
96 {
97 strncpy (domainname, domain, MAXHOSTNAMELEN);
98 domainname[MAXHOSTNAMELEN] = '\0';
99 }
100
101 i = strlen (domainname);
102 if (i == 0)
103 /* No domainname */
104 return 0;
105 if (domainname[i - 1] == '.')
106 domainname[i - 1] = 0;
107
108 if (dot_in_host) /* strip off rest of name */
109 *dot_in_host = '\0';
110
111 if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
112 > MAXNETNAMELEN)
113 return 0;
114
115 sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
116 return 1;
117}
118
119int
120getnetname (char name[MAXNETNAMELEN + 1])
121{
122 uid_t uid;
123 int dummy;
124
50304ef0 125 uid = __geteuid ();
800d775e
UD
126 if (uid == 0)
127 dummy = host2netname (name, NULL, NULL);
128 else
129 dummy = user2netname (name, uid, NULL);
130 return (dummy);
131}
132
133/* Type of the lookup function for netname2user. */
134typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
135 uid_t *, gid_t *, int *, gid_t *);
136/* The lookup function for the first entry of this service. */
137extern int __nss_publickey_lookup (service_user ** nip, const char *name,
f7ddf3d3 138 void **fctp) internal_function;
800d775e
UD
139
140int
141netname2user (const char netname[MAXNETNAMELEN + 1], uid_t * uidp, gid_t * gidp,
142 int *gidlenp, gid_t * gidlist)
143{
c4563d2d 144 static service_user *startp;
800d775e
UD
145 static netname2user_function start_fct;
146 service_user *nip;
147 netname2user_function fct;
148 enum nss_status status = NSS_STATUS_UNAVAIL;
149 int no_more;
150
151 if (startp == NULL)
152 {
153 no_more = __nss_publickey_lookup (&nip, "netname2user", (void **) &fct);
154 if (no_more)
155 startp = (service_user *) - 1;
156 else
157 {
158 startp = nip;
159 start_fct = fct;
160 }
161 }
162 else
163 {
164 fct = start_fct;
165 no_more = (nip = startp) == (service_user *) - 1;
166 }
167
168 while (!no_more)
169 {
170 status = (*fct) (netname, uidp, gidp, gidlenp, gidlist);
171
172 no_more = __nss_next (&nip, "netname2user", (void **) &fct, status, 0);
173 }
174
175 return status == NSS_STATUS_SUCCESS;
176}
177
178int
179netname2host (const char netname[MAXNETNAMELEN + 1], char *hostname,
180 const int hostlen)
181{
182 char *p1, *p2;
183 char buffer[MAXNETNAMELEN + 1];
184
185 p1 = strchr (buffer, '.');
186 if (p1 == NULL)
187 return 0;
188 p1++;
189
190 p2 = strchr (p1, '@');
191 if (p2 == NULL)
192 return 0;
193 *p2 = '\0';
194
195 if (hostlen > MAXNETNAMELEN)
196 return 0;
197
198 strncpy (hostname, p1, hostlen);
199 hostname[hostlen] = '\0';
200
201 return 1;
202}