Fix "make xcheck" in sunrpc.
[platform/upstream/glibc.git] / sunrpc / netname.c
1 /* Copyright (C) 1997, 1998, 1999, 2002, 2007 Free Software Foundation, Inc.
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
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.
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
13    Lesser General Public License for more details.
14
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.  */
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 */
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   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 libc_hidden_nolink (user2netname, GLIBC_2_1)
59
60 int
61 host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
62               const char *domain)
63 {
64   char *p;
65   char hostname[MAXHOSTNAMELEN + 1];
66   char domainname[MAXHOSTNAMELEN + 1];
67   char *dot_in_host;
68   size_t i;
69
70   netname[0] = '\0';            /* make null first (no need for memset) */
71
72   if (host == NULL)
73     __gethostname (hostname, MAXHOSTNAMELEN);
74   else
75     {
76       strncpy (hostname, host, MAXHOSTNAMELEN);
77       hostname[MAXHOSTNAMELEN] = '\0';
78     }
79
80   dot_in_host = strchr (hostname, '.');
81   if (domain == NULL)
82     {
83       p = dot_in_host;
84       if (p)
85         {
86           ++p;
87           strncpy (domainname, p, MAXHOSTNAMELEN);
88           domainname[MAXHOSTNAMELEN] = '\0';
89         }
90       else
91         {
92           domainname[0] = 0;
93           getdomainname (domainname, MAXHOSTNAMELEN);
94         }
95     }
96   else
97     {
98       strncpy (domainname, domain, MAXHOSTNAMELEN);
99       domainname[MAXHOSTNAMELEN] = '\0';
100     }
101
102   i = strlen (domainname);
103   if (i == 0)
104     /* No domainname */
105     return 0;
106   if (domainname[i - 1] == '.')
107     domainname[i - 1] = 0;
108
109   if (dot_in_host)              /* strip off rest of name */
110     *dot_in_host = '\0';
111
112   if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
113       > MAXNETNAMELEN)
114     return 0;
115
116   sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
117   return 1;
118 }
119 #ifdef EXPORT_RPC_SYMBOLS
120 libc_hidden_def (host2netname)
121 #else
122 libc_hidden_nolink (host2netname, GLIBC_2_1)
123 #endif
124
125 int
126 getnetname (char name[MAXNETNAMELEN + 1])
127 {
128   uid_t uid;
129   int dummy;
130
131   uid = __geteuid ();
132   if (uid == 0)
133     dummy = host2netname (name, NULL, NULL);
134   else
135     dummy = user2netname (name, uid, NULL);
136   return (dummy);
137 }
138 libc_hidden_nolink (getnetname, GLIBC_2_1)
139
140 /* Type of the lookup function for netname2user.  */
141 typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
142                                       uid_t *, gid_t *, int *, gid_t *);
143 /* The lookup function for the first entry of this service.  */
144 extern int __nss_publickey_lookup (service_user ** nip, const char *name,
145                                    void **fctp) internal_function;
146
147 int
148 netname2user (const char netname[MAXNETNAMELEN + 1], uid_t * uidp, gid_t * gidp,
149               int *gidlenp, gid_t * gidlist)
150 {
151   static service_user *startp;
152   static netname2user_function start_fct;
153   service_user *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   if (startp == NULL)
163     {
164       no_more = __nss_publickey_lookup (&nip, "netname2user", &fct.ptr);
165       if (no_more)
166         startp = (service_user *) - 1;
167       else
168         {
169           startp = nip;
170           start_fct = fct.f;
171         }
172     }
173   else
174     {
175       fct.f = start_fct;
176       no_more = (nip = startp) == (service_user *) - 1;
177     }
178
179   while (!no_more)
180     {
181       status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
182
183       no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
184     }
185
186   return status == NSS_STATUS_SUCCESS;
187 }
188 #ifdef EXPORT_RPC_SYMBOLS
189 libc_hidden_def (netname2user)
190 #else
191 libc_hidden_nolink (netname2user, GLIBC_2_1)
192 #endif
193
194 int
195 netname2host (const char netname[MAXNETNAMELEN + 1], char *hostname,
196               const int hostlen)
197 {
198   char *p1, *p2;
199   char buffer[MAXNETNAMELEN + 1];
200
201   p1 = strchr (buffer, '.');
202   if (p1 == NULL)
203     return 0;
204   p1++;
205
206   p2 = strchr (p1, '@');
207   if (p2 == NULL)
208     return 0;
209   *p2 = '\0';
210
211   if (hostlen > MAXNETNAMELEN)
212     return 0;
213
214   strncpy (hostname, p1, hostlen);
215   hostname[hostlen] = '\0';
216
217   return 1;
218 }
219 libc_hidden_nolink (netname2host, GLIBC_2_1)