* sysdeps/unix/sysv/linux/m68k/sysdep-cancel.h: Support cancellation
[glibc.git] / nis / nss_nisplus / nisplus-netgrp.c
blob831a13d891a69de67b2b926aac01bc7c3f4be655
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 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 <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock)
33 static nis_result *data = NULL;
34 static unsigned long data_size = 0;
35 static unsigned long position = 0;
37 #define NISENTRYVAL(idx,col,res) \
38 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 static enum nss_status
44 _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
45 size_t buflen, int *errnop)
47 enum nss_status status;
49 /* Some sanity checks. */
50 if (data == NULL || data_size == 0)
51 return NSS_STATUS_NOTFOUND;
53 if (position == data_size)
54 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
56 if (NISENTRYLEN (position, 1, data) > 0)
58 /* We have a list of other netgroups. */
60 result->type = group_val;
61 if (NISENTRYLEN (position, 1, data) >= buflen)
63 *errnop = ERANGE;
64 return NSS_STATUS_TRYAGAIN;
66 strncpy (buffer, NISENTRYVAL (position, 1, data),
67 NISENTRYLEN (position, 1, data));
68 buffer[NISENTRYLEN (position, 1, data)] = '\0';
69 result->val.group = buffer;
70 ++position;
71 result->first = 0;
73 return NSS_STATUS_SUCCESS;
76 /* Before we can copy the entry to the private buffer we have to make
77 sure it is big enough. */
78 if (NISENTRYLEN (position, 2, data) + NISENTRYLEN (position, 3, data) +
79 NISENTRYLEN (position, 4, data) + 6 > buflen)
81 *errnop = ERANGE;
82 status = NSS_STATUS_TRYAGAIN;
84 else
86 char *cp = buffer;
88 result->type = triple_val;
90 if (NISENTRYLEN (position, 2, data) == 0)
91 result->val.triple.host = NULL;
92 else
94 result->val.triple.host = cp;
95 cp = __stpncpy (cp, NISENTRYVAL (position, 2, data),
96 NISENTRYLEN (position, 2, data));
97 *cp++ = '\0';
100 if (NISENTRYLEN (position, 3, data) == 0)
101 result->val.triple.user = NULL;
102 else
104 result->val.triple.user = cp;
105 cp = __stpncpy (cp, NISENTRYVAL (position, 3, data),
106 NISENTRYLEN (position, 3, data));
107 *cp++ = '\0';
110 if (NISENTRYLEN (position, 4, data) == 0)
111 result->val.triple.domain = NULL;
112 else
114 result->val.triple.domain = cp;
115 cp = __stpncpy (cp, NISENTRYVAL (position, 4, data),
116 NISENTRYLEN (position, 4, data));
117 *cp = '\0';
120 status = NSS_STATUS_SUCCESS;
122 /* Remember where we stopped reading. */
123 ++position;
125 result->first = 0;
128 return status;
131 enum nss_status
132 _nss_nisplus_setnetgrent (const char *group, struct __netgrent *dummy)
134 enum nss_status status;
135 char buf[strlen (group) + 30];
137 if (group == NULL || group[0] == '\0')
138 return NSS_STATUS_UNAVAIL;
140 status = NSS_STATUS_SUCCESS;
142 __libc_lock_lock (lock);
144 if (data != NULL)
146 nis_freeresult (data);
147 data = NULL;
148 data_size = 0;
149 position = 0;
152 sprintf (buf, "[name=%s],netgroup.org_dir", group);
154 data = nis_list (buf, EXPAND_NAME, NULL, NULL);
156 if (niserr2nss (data->status) != NSS_STATUS_SUCCESS)
158 status = niserr2nss (data->status);
159 nis_freeresult (data);
160 data = NULL;
162 else
163 data_size = data->objects.objects_len;
165 __libc_lock_unlock (lock);
167 return status;
170 enum nss_status
171 _nss_nisplus_endnetgrent (struct __netgrent *dummy)
173 __libc_lock_lock (lock);
175 if (data != NULL)
177 nis_freeresult (data);
178 data = NULL;
179 data_size = 0;
180 position = 0;
183 __libc_lock_unlock (lock);
185 return NSS_STATUS_SUCCESS;
188 enum nss_status
189 _nss_nisplus_getnetgrent_r (struct __netgrent *result,
190 char *buffer, size_t buflen, int *errnop)
192 enum nss_status status;
194 __libc_lock_lock (lock);
196 status = _nss_nisplus_parse_netgroup (result, buffer, buflen, errnop);
198 __libc_lock_unlock (lock);
200 return status;