1/* Copyright (C) 1997-2016 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, see
17 <http://www.gnu.org/licenses/>. */
18
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>
26
27#include "nss-nisplus.h"
28
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)
31
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)
34
35enum nss_status
36_nss_nisplus_getnetgrent_r (struct __netgrent *result, char *buffer,
37 size_t buflen, int *errnop)
38{
39 enum nss_status status;
40
41 /* Some sanity checks. */
42 if (result->data == NULL || result->data_size == 0)
43 return NSS_STATUS_NOTFOUND;
44
45 if (result->position == result->data_size)
46 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
47
48 unsigned int entrylen
49 = NISENTRYLEN (result->position, 1, (nis_result *) result->data);
50 if (entrylen > 0)
51 {
52 /* We have a list of other netgroups. */
53
54 result->type = group_val;
55 if (entrylen >= buflen)
56 {
57 *errnop = ERANGE;
58 return NSS_STATUS_TRYAGAIN;
59 }
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;
67
68 return NSS_STATUS_SUCCESS;
69 }
70
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)
80 {
81 *errnop = ERANGE;
82 status = NSS_STATUS_TRYAGAIN;
83 }
84 else
85 {
86 char *cp = buffer;
87
88 result->type = triple_val;
89
90 if (hostlen == 0 ||
91 NISENTRYVAL (result->position, 2,
92 (nis_result *) result->data)[0] == '\0')
93 result->val.triple.host = NULL;
94 else
95 {
96 result->val.triple.host = cp;
97 cp = __stpncpy (cp, NISENTRYVAL (result->position, 2,
98 (nis_result *) result->data),
99 hostlen);
100 *cp++ = '\0';
101 }
102
103 if (userlen == 0 ||
104 NISENTRYVAL (result->position, 3,
105 (nis_result *) result->data)[0] == '\0')
106 result->val.triple.user = NULL;
107 else
108 {
109 result->val.triple.user = cp;
110 cp = __stpncpy (cp, NISENTRYVAL (result->position, 3,
111 (nis_result *) result->data),
112 userlen);
113 *cp++ = '\0';
114 }
115
116 if (domainlen == 0 ||
117 NISENTRYVAL (result->position, 4,
118 (nis_result *) result->data)[0] == '\0')
119 result->val.triple.domain = NULL;
120 else
121 {
122 result->val.triple.domain = cp;
123 cp = __stpncpy (cp, NISENTRYVAL (result->position, 4,
124 (nis_result *) result->data),
125 domainlen);
126 *cp = '\0';
127 }
128
129 status = NSS_STATUS_SUCCESS;
130
131 /* Remember where we stopped reading. */
132 ++result->position;
133
134 result->first = 0;
135 }
136
137 return status;
138}
139
140static void
141internal_endnetgrent (struct __netgrent *netgrp)
142{
143 nis_freeresult ((nis_result *) netgrp->data);
144 netgrp->data = NULL;
145 netgrp->data_size = 0;
146 netgrp->position = 0;
147}
148
149enum nss_status
150_nss_nisplus_setnetgrent (const char *group, struct __netgrent *netgrp)
151{
152 char buf[strlen (group) + 25];
153
154 if (group == NULL || group[0] == '\0')
155 return NSS_STATUS_UNAVAIL;
156
157 enum nss_status status = NSS_STATUS_SUCCESS;
158
159 snprintf (buf, sizeof (buf), "[name=%s],netgroup.org_dir", group);
160
161 netgrp->data = (char *) nis_list (buf, EXPAND_NAME, NULL, NULL);
162
163 if (netgrp->data == NULL)
164 {
165 __set_errno (ENOMEM);
166 status = NSS_STATUS_TRYAGAIN;
167 }
168 else if (niserr2nss (((nis_result *) netgrp->data)->status)
169 != NSS_STATUS_SUCCESS)
170 {
171 status = niserr2nss (((nis_result *) netgrp->data)->status);
172
173 internal_endnetgrent (netgrp);
174 }
175 else
176 {
177 netgrp->data_size = ((nis_result *) netgrp->data)->objects.objects_len;
178 netgrp->position = 0;
179 netgrp->first = 1;
180 }
181
182 return status;
183}
184
185enum nss_status
186_nss_nisplus_endnetgrent (struct __netgrent *netgrp)
187{
188 internal_endnetgrent (netgrp);
189
190 return NSS_STATUS_SUCCESS;
191}
192