[BZ #4381]
[glibc.git] / nis / nss_nisplus / nisplus-netgrp.c
blob24303b1474c25afb609cde3a25b753b033d9a6ee
1 /* Copyright (C) 1997, 2003, 2004, 2005, 2006 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 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
33 #define NISENTRYLEN(idx, col, res) \
34 (NIS_RES_OBJECT (res)[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 NISENTRYVAL (result->position, 2,
93 (nis_result *) result->data)[0] == '\0')
94 result->val.triple.host = NULL;
95 else
97 result->val.triple.host = cp;
98 cp = __stpncpy (cp, NISENTRYVAL (result->position, 2,
99 (nis_result *) result->data),
100 hostlen);
101 *cp++ = '\0';
104 if (userlen == 0 ||
105 NISENTRYVAL (result->position, 3,
106 (nis_result *) result->data)[0] == '\0')
107 result->val.triple.user = NULL;
108 else
110 result->val.triple.user = cp;
111 cp = __stpncpy (cp, NISENTRYVAL (result->position, 3,
112 (nis_result *) result->data),
113 userlen);
114 *cp++ = '\0';
117 if (domainlen == 0 ||
118 NISENTRYVAL (result->position, 4,
119 (nis_result *) result->data)[0] == '\0')
120 result->val.triple.domain = NULL;
121 else
123 result->val.triple.domain = cp;
124 cp = __stpncpy (cp, NISENTRYVAL (result->position, 4,
125 (nis_result *) result->data),
126 domainlen);
127 *cp = '\0';
130 status = NSS_STATUS_SUCCESS;
132 /* Remember where we stopped reading. */
133 ++result->position;
135 result->first = 0;
138 return status;
141 static void
142 internal_endnetgrent (struct __netgrent *netgrp)
144 nis_freeresult ((nis_result *) netgrp->data);
145 netgrp->data = NULL;
146 netgrp->data_size = 0;
147 netgrp->position = 0;
150 enum nss_status
151 _nss_nisplus_setnetgrent (const char *group, struct __netgrent *netgrp)
153 char buf[strlen (group) + 25];
155 if (group == NULL || group[0] == '\0')
156 return NSS_STATUS_UNAVAIL;
158 enum nss_status status = NSS_STATUS_SUCCESS;
160 snprintf (buf, sizeof (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;