]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1997-2015 Free Software Foundation, Inc. |
e61abf83 UD |
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 | |
41bdb6e2 AJ |
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. | |
e61abf83 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
e61abf83 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
e61abf83 UD |
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> | |
e61abf83 | 25 | #include <rpcsvc/nis.h> |
e61abf83 UD |
26 | |
27 | #include "nss-nisplus.h" | |
28 | ||
23948bda UD |
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) | |
e61abf83 | 31 | |
23948bda UD |
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) | |
e61abf83 | 34 | |
8500b0ca UD |
35 | enum nss_status |
36 | _nss_nisplus_getnetgrent_r (struct __netgrent *result, char *buffer, | |
37 | size_t buflen, int *errnop) | |
26dee9c4 UD |
38 | { |
39 | enum nss_status status; | |
40 | ||
41 | /* Some sanity checks. */ | |
8500b0ca | 42 | if (result->data == NULL || result->data_size == 0) |
2d7da676 | 43 | return NSS_STATUS_NOTFOUND; |
5107cf1d | 44 | |
8500b0ca | 45 | if (result->position == result->data_size) |
26dee9c4 | 46 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
5107cf1d | 47 | |
8500b0ca UD |
48 | unsigned int entrylen |
49 | = NISENTRYLEN (result->position, 1, (nis_result *) result->data); | |
50 | if (entrylen > 0) | |
26dee9c4 UD |
51 | { |
52 | /* We have a list of other netgroups. */ | |
5107cf1d | 53 | |
26dee9c4 | 54 | result->type = group_val; |
8500b0ca | 55 | if (entrylen >= buflen) |
26dee9c4 | 56 | { |
d71b808a | 57 | *errnop = ERANGE; |
26dee9c4 UD |
58 | return NSS_STATUS_TRYAGAIN; |
59 | } | |
8500b0ca UD |
60 | strncpy (buffer, NISENTRYVAL (result->position, 1, |
61 | (nis_result *) result->data), | |
62 | entrylen); | |
63 | buffer[entrylen] = '\0'; | |
26dee9c4 | 64 | result->val.group = buffer; |
8500b0ca | 65 | ++result->position; |
26dee9c4 | 66 | result->first = 0; |
5107cf1d | 67 | |
26dee9c4 UD |
68 | return NSS_STATUS_SUCCESS; |
69 | } | |
70 | ||
5107cf1d | 71 | /* Before we can copy the entry to the private buffer we have to make |
26dee9c4 | 72 | sure it is big enough. */ |
8500b0ca UD |
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) | |
26dee9c4 | 80 | { |
d71b808a | 81 | *errnop = ERANGE; |
60c96635 | 82 | status = NSS_STATUS_TRYAGAIN; |
26dee9c4 UD |
83 | } |
84 | else | |
85 | { | |
86 | char *cp = buffer; | |
5107cf1d | 87 | |
26dee9c4 | 88 | result->type = triple_val; |
5107cf1d | 89 | |
cd8ea5d2 UD |
90 | if (hostlen == 0 || |
91 | NISENTRYVAL (result->position, 2, | |
92 | (nis_result *) result->data)[0] == '\0') | |
26dee9c4 UD |
93 | result->val.triple.host = NULL; |
94 | else | |
95 | { | |
96 | result->val.triple.host = cp; | |
8500b0ca UD |
97 | cp = __stpncpy (cp, NISENTRYVAL (result->position, 2, |
98 | (nis_result *) result->data), | |
99 | hostlen); | |
d71b808a | 100 | *cp++ = '\0'; |
26dee9c4 UD |
101 | } |
102 | ||
cd8ea5d2 UD |
103 | if (userlen == 0 || |
104 | NISENTRYVAL (result->position, 3, | |
105 | (nis_result *) result->data)[0] == '\0') | |
26dee9c4 UD |
106 | result->val.triple.user = NULL; |
107 | else | |
108 | { | |
109 | result->val.triple.user = cp; | |
8500b0ca UD |
110 | cp = __stpncpy (cp, NISENTRYVAL (result->position, 3, |
111 | (nis_result *) result->data), | |
112 | userlen); | |
d71b808a | 113 | *cp++ = '\0'; |
26dee9c4 UD |
114 | } |
115 | ||
cd8ea5d2 UD |
116 | if (domainlen == 0 || |
117 | NISENTRYVAL (result->position, 4, | |
118 | (nis_result *) result->data)[0] == '\0') | |
26dee9c4 UD |
119 | result->val.triple.domain = NULL; |
120 | else | |
121 | { | |
122 | result->val.triple.domain = cp; | |
8500b0ca UD |
123 | cp = __stpncpy (cp, NISENTRYVAL (result->position, 4, |
124 | (nis_result *) result->data), | |
125 | domainlen); | |
26dee9c4 UD |
126 | *cp = '\0'; |
127 | } | |
128 | ||
129 | status = NSS_STATUS_SUCCESS; | |
130 | ||
131 | /* Remember where we stopped reading. */ | |
8500b0ca | 132 | ++result->position; |
26dee9c4 UD |
133 | |
134 | result->first = 0; | |
135 | } | |
136 | ||
137 | return status; | |
138 | } | |
139 | ||
8500b0ca UD |
140 | static void |
141 | internal_endnetgrent (struct __netgrent *netgrp) | |
142 | { | |
ff3cacc5 UD |
143 | nis_freeresult ((nis_result *) netgrp->data); |
144 | netgrp->data = NULL; | |
145 | netgrp->data_size = 0; | |
146 | netgrp->position = 0; | |
8500b0ca UD |
147 | } |
148 | ||
e61abf83 | 149 | enum nss_status |
8500b0ca | 150 | _nss_nisplus_setnetgrent (const char *group, struct __netgrent *netgrp) |
e61abf83 | 151 | { |
8e64faef | 152 | char buf[strlen (group) + 25]; |
e61abf83 UD |
153 | |
154 | if (group == NULL || group[0] == '\0') | |
155 | return NSS_STATUS_UNAVAIL; | |
156 | ||
8e64faef | 157 | enum nss_status status = NSS_STATUS_SUCCESS; |
a334319f | 158 | |
8e64faef | 159 | snprintf (buf, sizeof (buf), "[name=%s],netgroup.org_dir", group); |
e61abf83 | 160 | |
8500b0ca | 161 | netgrp->data = (char *) nis_list (buf, EXPAND_NAME, NULL, NULL); |
e61abf83 | 162 | |
8500b0ca | 163 | if (netgrp->data == NULL) |
901956a5 UD |
164 | { |
165 | __set_errno (ENOMEM); | |
166 | status = NSS_STATUS_TRYAGAIN; | |
167 | } | |
8500b0ca UD |
168 | else if (niserr2nss (((nis_result *) netgrp->data)->status) |
169 | != NSS_STATUS_SUCCESS) | |
e61abf83 | 170 | { |
8500b0ca UD |
171 | status = niserr2nss (((nis_result *) netgrp->data)->status); |
172 | ||
173 | internal_endnetgrent (netgrp); | |
e61abf83 | 174 | } |
26dee9c4 | 175 | else |
e61abf83 | 176 | { |
8500b0ca UD |
177 | netgrp->data_size = ((nis_result *) netgrp->data)->objects.objects_len; |
178 | netgrp->position = 0; | |
179 | netgrp->first = 1; | |
e61abf83 UD |
180 | } |
181 | ||
8500b0ca | 182 | return status; |
e61abf83 UD |
183 | } |
184 | ||
185 | enum nss_status | |
8500b0ca | 186 | _nss_nisplus_endnetgrent (struct __netgrent *netgrp) |
e61abf83 | 187 | { |
8500b0ca | 188 | internal_endnetgrent (netgrp); |
e61abf83 | 189 | |
8500b0ca | 190 | return NSS_STATUS_SUCCESS; |
e61abf83 | 191 | } |