Add the missing "; \".
[glibc.git] / sunrpc / netname.c
blob07ff768e6ab6a987ce73424b89094da515158f2d
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.
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.
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.
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. */
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <rpc/rpc.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 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;
58 libc_hidden_def (user2netname)
60 int
61 host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
62 const char *domain)
64 char *p;
65 char hostname[MAXHOSTNAMELEN + 1];
66 char domainname[MAXHOSTNAMELEN + 1];
67 char *dot_in_host;
68 size_t i;
70 netname[0] = '\0'; /* make null first (no need for memset) */
72 if (host == NULL)
73 __gethostname (hostname, MAXHOSTNAMELEN);
74 else
76 strncpy (hostname, host, MAXHOSTNAMELEN);
77 hostname[MAXHOSTNAMELEN] = '\0';
80 dot_in_host = strchr (hostname, '.');
81 if (domain == NULL)
83 p = dot_in_host;
84 if (p)
86 ++p;
87 strncpy (domainname, p, MAXHOSTNAMELEN);
88 domainname[MAXHOSTNAMELEN] = '\0';
90 else
92 domainname[0] = 0;
93 getdomainname (domainname, MAXHOSTNAMELEN);
96 else
98 strncpy (domainname, domain, MAXHOSTNAMELEN);
99 domainname[MAXHOSTNAMELEN] = '\0';
102 i = strlen (domainname);
103 if (i == 0)
104 /* No domainname */
105 return 0;
106 if (domainname[i - 1] == '.')
107 domainname[i - 1] = 0;
109 if (dot_in_host) /* strip off rest of name */
110 *dot_in_host = '\0';
112 if ((strlen (domainname) + strlen (hostname) + OPSYS_LEN + 3)
113 > MAXNETNAMELEN)
114 return 0;
116 sprintf (netname, "%s.%s@%s", OPSYS, hostname, domainname);
117 return 1;
119 libc_hidden_def (host2netname)
122 getnetname (char name[MAXNETNAMELEN + 1])
124 uid_t uid;
125 int dummy;
127 uid = __geteuid ();
128 if (uid == 0)
129 dummy = host2netname (name, NULL, NULL);
130 else
131 dummy = user2netname (name, uid, NULL);
132 return (dummy);
134 libc_hidden_def (getnetname)
136 /* Type of the lookup function for netname2user. */
137 typedef int (*netname2user_function) (const char netname[MAXNETNAMELEN + 1],
138 uid_t *, gid_t *, int *, gid_t *);
139 /* The lookup function for the first entry of this service. */
140 extern int __nss_publickey_lookup (service_user ** nip, const char *name,
141 void **fctp) internal_function;
144 netname2user (const char netname[MAXNETNAMELEN + 1], uid_t * uidp, gid_t * gidp,
145 int *gidlenp, gid_t * gidlist)
147 static service_user *startp;
148 static netname2user_function start_fct;
149 service_user *nip;
150 union
152 netname2user_function f;
153 void *ptr;
154 } fct;
155 enum nss_status status = NSS_STATUS_UNAVAIL;
156 int no_more;
158 if (startp == NULL)
160 no_more = __nss_publickey_lookup (&nip, "netname2user", &fct.ptr);
161 if (no_more)
162 startp = (service_user *) - 1;
163 else
165 startp = nip;
166 start_fct = fct.f;
169 else
171 fct.f = start_fct;
172 no_more = (nip = startp) == (service_user *) - 1;
175 while (!no_more)
177 status = (*fct.f) (netname, uidp, gidp, gidlenp, gidlist);
179 no_more = __nss_next2 (&nip, "netname2user", NULL, &fct.ptr, status, 0);
182 return status == NSS_STATUS_SUCCESS;
184 libc_hidden_def (netname2user)
187 netname2host (const char netname[MAXNETNAMELEN + 1], char *hostname,
188 const int hostlen)
190 char *p1, *p2;
191 char buffer[MAXNETNAMELEN + 1];
193 p1 = strchr (buffer, '.');
194 if (p1 == NULL)
195 return 0;
196 p1++;
198 p2 = strchr (p1, '@');
199 if (p2 == NULL)
200 return 0;
201 *p2 = '\0';
203 if (hostlen > MAXNETNAMELEN)
204 return 0;
206 strncpy (hostname, p1, hostlen);
207 hostname[hostlen] = '\0';
209 return 1;