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
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
)
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
;
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
)
82 status
= NSS_STATUS_TRYAGAIN
;
88 result
->type
= triple_val
;
90 if (NISENTRYLEN (position
, 2, data
) == 0)
91 result
->val
.triple
.host
= NULL
;
94 result
->val
.triple
.host
= cp
;
95 cp
= __stpncpy (cp
, NISENTRYVAL (position
, 2, data
),
96 NISENTRYLEN (position
, 2, data
));
100 if (NISENTRYLEN (position
, 3, data
) == 0)
101 result
->val
.triple
.user
= NULL
;
104 result
->val
.triple
.user
= cp
;
105 cp
= __stpncpy (cp
, NISENTRYVAL (position
, 3, data
),
106 NISENTRYLEN (position
, 3, data
));
110 if (NISENTRYLEN (position
, 4, data
) == 0)
111 result
->val
.triple
.domain
= NULL
;
114 result
->val
.triple
.domain
= cp
;
115 cp
= __stpncpy (cp
, NISENTRYVAL (position
, 4, data
),
116 NISENTRYLEN (position
, 4, data
));
120 status
= NSS_STATUS_SUCCESS
;
122 /* Remember where we stopped reading. */
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
);
146 nis_freeresult (data
);
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
);
163 data_size
= data
->objects
.objects_len
;
165 __libc_lock_unlock (lock
);
171 _nss_nisplus_endnetgrent (struct __netgrent
*dummy
)
173 __libc_lock_lock (lock
);
177 nis_freeresult (data
);
183 __libc_lock_unlock (lock
);
185 return NSS_STATUS_SUCCESS
;
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
);