]> sourceware.org Git - glibc.git/blame - nis/nss_nisplus/nisplus-netgrp.c
Update.
[glibc.git] / nis / nss_nisplus / nisplus-netgrp.c
CommitLineData
e61abf83
UD
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.
4
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.
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 Library General Public License for more details.
14
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. */
19
20#include <nss.h>
21#include <errno.h>
22#include <ctype.h>
23#include <netdb.h>
24#include <string.h>
25#include <netgroup.h>
5107cf1d 26#include <bits/libc-lock.h>
e61abf83 27#include <rpcsvc/nis.h>
e61abf83
UD
28
29#include "nss-nisplus.h"
30
31__libc_lock_define_initialized (static, lock)
32
26dee9c4
UD
33static nis_result *data = NULL;
34static unsigned long data_size = 0;
35static unsigned long position = 0;
e61abf83
UD
36
37#define NISENTRYVAL(idx,col,res) \
2d7da676 38 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
e61abf83
UD
39
40#define NISENTRYLEN(idx,col,res) \
2d7da676 41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
e61abf83 42
26dee9c4 43static enum nss_status
5107cf1d 44_nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
26dee9c4
UD
45 size_t buflen)
46{
47 enum nss_status status;
48
49 /* Some sanity checks. */
50 if (data == NULL || data_size == 0)
2d7da676 51 return NSS_STATUS_NOTFOUND;
5107cf1d 52
26dee9c4
UD
53 if (position == data_size)
54 return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
5107cf1d 55
26dee9c4
UD
56 if (NISENTRYLEN (position, 1, data) > 0)
57 {
58 /* We have a list of other netgroups. */
5107cf1d 59
26dee9c4
UD
60 result->type = group_val;
61 if (NISENTRYLEN (position, 1, data) >= buflen)
62 {
63 __set_errno (ERANGE);
64 return NSS_STATUS_TRYAGAIN;
65 }
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;
5107cf1d 72
26dee9c4
UD
73 return NSS_STATUS_SUCCESS;
74 }
75
5107cf1d 76 /* Before we can copy the entry to the private buffer we have to make
26dee9c4 77 sure it is big enough. */
5107cf1d 78 if (NISENTRYLEN (position, 2, data) + NISENTRYLEN (position, 3, data) +
26dee9c4
UD
79 NISENTRYLEN (position, 4, data) + 6 > buflen)
80 {
81 __set_errno (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
26dee9c4
UD
90 if (NISENTRYLEN (position, 2, data) == 0)
91 result->val.triple.host = NULL;
92 else
93 {
94 result->val.triple.host = cp;
95 cp = stpncpy (cp, NISENTRYVAL (position, 2, data),
96 NISENTRYLEN (position, 2, data));
97 *cp = '\0';
98 ++cp;
99 }
100
101 if (NISENTRYLEN (position, 3, data) == 0)
102 result->val.triple.user = NULL;
103 else
104 {
105 result->val.triple.user = cp;
106 cp = stpncpy (cp, NISENTRYVAL (position, 3, data),
107 NISENTRYLEN (position, 3, data));
108 *cp = '\0';
109 ++cp;
110 }
111
112 if (NISENTRYLEN (position, 4, data) == 0)
113 result->val.triple.domain = NULL;
114 else
115 {
116 result->val.triple.domain = cp;
117 cp = stpncpy (cp, NISENTRYVAL (position, 4, data),
118 NISENTRYLEN (position, 4, data));
119 *cp = '\0';
120 }
121
122 status = NSS_STATUS_SUCCESS;
123
124 /* Remember where we stopped reading. */
125 ++position;
126
127 result->first = 0;
128 }
129
130 return status;
131}
132
e61abf83
UD
133enum nss_status
134_nss_nisplus_setnetgrent (char *group)
135
136{
137 enum nss_status status;
e61abf83 138 char buf[strlen (group) + 30];
e61abf83
UD
139
140 if (group == NULL || group[0] == '\0')
141 return NSS_STATUS_UNAVAIL;
142
143 status = NSS_STATUS_SUCCESS;
144
145 __libc_lock_lock (lock);
146
147 if (data != NULL)
148 {
26dee9c4 149 nis_freeresult (data);
e61abf83
UD
150 data = NULL;
151 data_size = 0;
26dee9c4 152 position = 0;
e61abf83
UD
153 }
154
2d7da676 155 sprintf (buf, "[name=%s],netgroup.org_dir", group);
e61abf83 156
2d7da676 157 data = nis_list (buf, EXPAND_NAME, NULL, NULL);
e61abf83 158
26dee9c4 159 if (niserr2nss (data->status) != NSS_STATUS_SUCCESS)
e61abf83 160 {
26dee9c4
UD
161 status = niserr2nss (data->status);
162 nis_freeresult (data);
163 data = NULL;
e61abf83 164 }
26dee9c4
UD
165 else
166 data_size = data->objects.objects_len;
5107cf1d 167
e61abf83 168 __libc_lock_unlock (lock);
5107cf1d 169
e61abf83
UD
170 return status;
171}
172
173enum nss_status
174_nss_nisplus_endnetgrent (void)
175{
176 __libc_lock_lock (lock);
177
178 if (data != NULL)
179 {
26dee9c4 180 nis_freeresult (data);
e61abf83
UD
181 data = NULL;
182 data_size = 0;
26dee9c4 183 position = 0;
e61abf83
UD
184 }
185
186 __libc_lock_unlock (lock);
187
188 return NSS_STATUS_SUCCESS;
189}
190
191enum nss_status
192_nss_nisplus_getnetgrent_r (struct __netgrent *result,
193 char *buffer, size_t buflen)
194{
195 enum nss_status status;
196
e61abf83
UD
197 __libc_lock_lock (lock);
198
26dee9c4 199 status = _nss_nisplus_parse_netgroup (result, buffer, buflen);
e61abf83
UD
200
201 __libc_lock_unlock (lock);
202
203 return status;
204}
This page took 0.05842 seconds and 5 git commands to generate.