1 /* Copyright (C) 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
28 #include <rpcsvc/nislib.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock)
34 static nis_result *data = NULL;
35 static unsigned long data_size = 0;
36 static unsigned long position = 0;
38 #define NISENTRYVAL(idx,col,res) \
39 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
41 #define NISENTRYLEN(idx,col,res) \
42 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
44 static enum nss_status
45 _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
48 enum nss_status status;
50 /* Some sanity checks. */
51 if (data == NULL || data_size == 0)
52 return NSS_STATUS_NOTFOUND;
54 if (position == data_size)
55 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
57 if (NISENTRYLEN (position, 1, data) > 0)
59 /* We have a list of other netgroups. */
61 result->type = group_val;
62 if (NISENTRYLEN (position, 1, data) >= buflen)
65 return NSS_STATUS_TRYAGAIN;
67 strncpy (buffer, NISENTRYVAL (position, 1, data),
68 NISENTRYLEN (position, 1, data));
69 buffer[NISENTRYLEN (position, 1, data)] = '\0';
70 result->val.group = buffer;
74 return NSS_STATUS_SUCCESS;
77 /* Before we can copy the entry to the private buffer we have to make
78 sure it is big enough. */
79 if (NISENTRYLEN (position, 2, data) + NISENTRYLEN (position, 3, data) +
80 NISENTRYLEN (position, 4, data) + 6 > buflen)
83 status = NSS_STATUS_TRYAGAIN;
89 result->type = triple_val;
91 if (NISENTRYLEN (position, 2, data) == 0)
92 result->val.triple.host = NULL;
95 result->val.triple.host = cp;
96 cp = stpncpy (cp, NISENTRYVAL (position, 2, data),
97 NISENTRYLEN (position, 2, data));
102 if (NISENTRYLEN (position, 3, data) == 0)
103 result->val.triple.user = NULL;
106 result->val.triple.user = cp;
107 cp = stpncpy (cp, NISENTRYVAL (position, 3, data),
108 NISENTRYLEN (position, 3, data));
113 if (NISENTRYLEN (position, 4, data) == 0)
114 result->val.triple.domain = NULL;
117 result->val.triple.domain = cp;
118 cp = stpncpy (cp, NISENTRYVAL (position, 4, data),
119 NISENTRYLEN (position, 4, data));
123 status = NSS_STATUS_SUCCESS;
125 /* Remember where we stopped reading. */
135 _nss_nisplus_setnetgrent (char *group)
138 enum nss_status status;
139 char buf[strlen (group) + 30];
141 if (group == NULL || group[0] == '\0')
142 return NSS_STATUS_UNAVAIL;
144 status = NSS_STATUS_SUCCESS;
146 __libc_lock_lock (lock);
150 nis_freeresult (data);
156 sprintf (buf, "[name=%s],netgroup.org_dir", group);
158 data = nis_list (buf, EXPAND_NAME, NULL, NULL);
160 if (niserr2nss (data->status) != NSS_STATUS_SUCCESS)
162 status = niserr2nss (data->status);
163 nis_freeresult (data);
167 data_size = data->objects.objects_len;
169 __libc_lock_unlock (lock);
175 _nss_nisplus_endnetgrent (void)
177 __libc_lock_lock (lock);
181 nis_freeresult (data);
187 __libc_lock_unlock (lock);
189 return NSS_STATUS_SUCCESS;
193 _nss_nisplus_getnetgrent_r (struct __netgrent *result,
194 char *buffer, size_t buflen)
196 enum nss_status status;
198 __libc_lock_lock (lock);
200 status = _nss_nisplus_parse_netgroup (result, buffer, buflen);
202 __libc_lock_unlock (lock);