Update copyright notices with scripts/update-copyrights
[glibc.git] / nis / nss_nisplus / nisplus-netgrp.c
blob1027be1db6d9d355a7e8cb781dda39862e351888
1 /* Copyright (C) 1997-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <nss.h>
20 #include <errno.h>
21 #include <ctype.h>
22 #include <netdb.h>
23 #include <string.h>
24 #include <netgroup.h>
25 #include <rpcsvc/nis.h>
27 #include "nss-nisplus.h"
29 #define NISENTRYVAL(idx, col, res) \
30 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
32 #define NISENTRYLEN(idx, col, res) \
33 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
35 enum nss_status
36 _nss_nisplus_getnetgrent_r (struct __netgrent *result, char *buffer,
37 size_t buflen, int *errnop)
39 enum nss_status status;
41 /* Some sanity checks. */
42 if (result->data == NULL || result->data_size == 0)
43 return NSS_STATUS_NOTFOUND;
45 if (result->position == result->data_size)
46 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
48 unsigned int entrylen
49 = NISENTRYLEN (result->position, 1, (nis_result *) result->data);
50 if (entrylen > 0)
52 /* We have a list of other netgroups. */
54 result->type = group_val;
55 if (entrylen >= buflen)
57 *errnop = ERANGE;
58 return NSS_STATUS_TRYAGAIN;
60 strncpy (buffer, NISENTRYVAL (result->position, 1,
61 (nis_result *) result->data),
62 entrylen);
63 buffer[entrylen] = '\0';
64 result->val.group = buffer;
65 ++result->position;
66 result->first = 0;
68 return NSS_STATUS_SUCCESS;
71 /* Before we can copy the entry to the private buffer we have to make
72 sure it is big enough. */
73 unsigned int hostlen
74 = NISENTRYLEN (result->position, 2, (nis_result *) result->data);
75 unsigned int userlen
76 = NISENTRYLEN (result->position, 3, (nis_result *) result->data);
77 unsigned int domainlen
78 = NISENTRYLEN (result->position, 4, (nis_result *) result->data);
79 if (hostlen + userlen + domainlen + 6 > buflen)
81 *errnop = ERANGE;
82 status = NSS_STATUS_TRYAGAIN;
84 else
86 char *cp = buffer;
88 result->type = triple_val;
90 if (hostlen == 0 ||
91 NISENTRYVAL (result->position, 2,
92 (nis_result *) result->data)[0] == '\0')
93 result->val.triple.host = NULL;
94 else
96 result->val.triple.host = cp;
97 cp = __stpncpy (cp, NISENTRYVAL (result->position, 2,
98 (nis_result *) result->data),
99 hostlen);
100 *cp++ = '\0';
103 if (userlen == 0 ||
104 NISENTRYVAL (result->position, 3,
105 (nis_result *) result->data)[0] == '\0')
106 result->val.triple.user = NULL;
107 else
109 result->val.triple.user = cp;
110 cp = __stpncpy (cp, NISENTRYVAL (result->position, 3,
111 (nis_result *) result->data),
112 userlen);
113 *cp++ = '\0';
116 if (domainlen == 0 ||
117 NISENTRYVAL (result->position, 4,
118 (nis_result *) result->data)[0] == '\0')
119 result->val.triple.domain = NULL;
120 else
122 result->val.triple.domain = cp;
123 cp = __stpncpy (cp, NISENTRYVAL (result->position, 4,
124 (nis_result *) result->data),
125 domainlen);
126 *cp = '\0';
129 status = NSS_STATUS_SUCCESS;
131 /* Remember where we stopped reading. */
132 ++result->position;
134 result->first = 0;
137 return status;
140 static void
141 internal_endnetgrent (struct __netgrent *netgrp)
143 nis_freeresult ((nis_result *) netgrp->data);
144 netgrp->data = NULL;
145 netgrp->data_size = 0;
146 netgrp->position = 0;
149 enum nss_status
150 _nss_nisplus_setnetgrent (const char *group, struct __netgrent *netgrp)
152 char buf[strlen (group) + 25];
154 if (group == NULL || group[0] == '\0')
155 return NSS_STATUS_UNAVAIL;
157 enum nss_status status = NSS_STATUS_SUCCESS;
159 snprintf (buf, sizeof (buf), "[name=%s],netgroup.org_dir", group);
161 netgrp->data = (char *) nis_list (buf, EXPAND_NAME, NULL, NULL);
163 if (netgrp->data == NULL)
165 __set_errno (ENOMEM);
166 status = NSS_STATUS_TRYAGAIN;
168 else if (niserr2nss (((nis_result *) netgrp->data)->status)
169 != NSS_STATUS_SUCCESS)
171 status = niserr2nss (((nis_result *) netgrp->data)->status);
173 internal_endnetgrent (netgrp);
175 else
177 netgrp->data_size = ((nis_result *) netgrp->data)->objects.objects_len;
178 netgrp->position = 0;
179 netgrp->first = 1;
182 return status;
185 enum nss_status
186 _nss_nisplus_endnetgrent (struct __netgrent *netgrp)
188 internal_endnetgrent (netgrp);
190 return NSS_STATUS_SUCCESS;