(gaih_inet): Little code cleanup. Move variable h into gethosts macro.
[glibc.git] / nis / nss_nisplus / nisplus-netgrp.c
blobc56a6854a4901f0bae9d21f4cc4a05e2ba4240f3
1 /* Copyright (C) 1997, 2003, 2004 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 <nss.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <netgroup.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
30 #define NISENTRYVAL(idx,col,res) \
31 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
33 #define NISENTRYLEN(idx,col,res) \
34 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
36 enum nss_status
37 _nss_nisplus_getnetgrent_r (struct __netgrent *result, char *buffer,
38 size_t buflen, int *errnop)
40 enum nss_status status;
42 /* Some sanity checks. */
43 if (result->data == NULL || result->data_size == 0)
44 return NSS_STATUS_NOTFOUND;
46 if (result->position == result->data_size)
47 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
49 unsigned int entrylen
50 = NISENTRYLEN (result->position, 1, (nis_result *) result->data);
51 if (entrylen > 0)
53 /* We have a list of other netgroups. */
55 result->type = group_val;
56 if (entrylen >= buflen)
58 *errnop = ERANGE;
59 return NSS_STATUS_TRYAGAIN;
61 strncpy (buffer, NISENTRYVAL (result->position, 1,
62 (nis_result *) result->data),
63 entrylen);
64 buffer[entrylen] = '\0';
65 result->val.group = buffer;
66 ++result->position;
67 result->first = 0;
69 return NSS_STATUS_SUCCESS;
72 /* Before we can copy the entry to the private buffer we have to make
73 sure it is big enough. */
74 unsigned int hostlen
75 = NISENTRYLEN (result->position, 2, (nis_result *) result->data);
76 unsigned int userlen
77 = NISENTRYLEN (result->position, 3, (nis_result *) result->data);
78 unsigned int domainlen
79 = NISENTRYLEN (result->position, 4, (nis_result *) result->data);
80 if (hostlen + userlen + domainlen + 6 > buflen)
82 *errnop = ERANGE;
83 status = NSS_STATUS_TRYAGAIN;
85 else
87 char *cp = buffer;
89 result->type = triple_val;
91 if (hostlen == 0)
92 result->val.triple.host = NULL;
93 else
95 result->val.triple.host = cp;
96 cp = __stpncpy (cp, NISENTRYVAL (result->position, 2,
97 (nis_result *) result->data),
98 hostlen);
99 *cp++ = '\0';
102 if (userlen == 0)
103 result->val.triple.user = NULL;
104 else
106 result->val.triple.user = cp;
107 cp = __stpncpy (cp, NISENTRYVAL (result->position, 3,
108 (nis_result *) result->data),
109 userlen);
110 *cp++ = '\0';
113 if (domainlen == 0)
114 result->val.triple.domain = NULL;
115 else
117 result->val.triple.domain = cp;
118 cp = __stpncpy (cp, NISENTRYVAL (result->position, 4,
119 (nis_result *) result->data),
120 domainlen);
121 *cp = '\0';
124 status = NSS_STATUS_SUCCESS;
126 /* Remember where we stopped reading. */
127 ++result->position;
129 result->first = 0;
132 return status;
135 static void
136 internal_endnetgrent (struct __netgrent *netgrp)
138 if (netgrp->data != NULL)
140 nis_freeresult ((nis_result *) netgrp->data);
141 netgrp->data = NULL;
142 netgrp->data_size = 0;
143 netgrp->position = 0;
147 enum nss_status
148 _nss_nisplus_setnetgrent (const char *group, struct __netgrent *netgrp)
150 enum nss_status status;
151 char buf[strlen (group) + 30];
153 if (group == NULL || group[0] == '\0')
154 return NSS_STATUS_UNAVAIL;
156 status = NSS_STATUS_SUCCESS;
158 internal_endnetgrent (netgrp);
160 sprintf (buf, "[name=%s],netgroup.org_dir", group);
162 netgrp->data = (char *) nis_list (buf, EXPAND_NAME, NULL, NULL);
164 if (netgrp->data == NULL)
166 __set_errno (ENOMEM);
167 status = NSS_STATUS_TRYAGAIN;
169 else if (niserr2nss (((nis_result *) netgrp->data)->status)
170 != NSS_STATUS_SUCCESS)
172 status = niserr2nss (((nis_result *) netgrp->data)->status);
174 internal_endnetgrent (netgrp);
176 else
178 netgrp->data_size = ((nis_result *) netgrp->data)->objects.objects_len;
179 netgrp->position = 0;
180 netgrp->first = 1;
183 return status;
186 enum nss_status
187 _nss_nisplus_endnetgrent (struct __netgrent *netgrp)
189 internal_endnetgrent (netgrp);
191 return NSS_STATUS_SUCCESS;