Update.
[glibc.git] / nis / nss_nis / nis-netgrp.c
blobd170b56f5056e077463609221223ac28cceebb18
1 /* Copyright (C) 1996, 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>, 1996.
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. */
20 #include <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <bits/libc-lock.h>
24 #include <netdb.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <netgroup.h>
29 #include <rpcsvc/yp.h>
30 #include <rpcsvc/ypclnt.h>
32 #include "nss-nis.h"
34 /* Locks the static variables in this file. */
35 __libc_lock_define_initialized (static, lock)
37 static char *data = NULL;
38 static size_t data_size = 0;
39 static char *cursor = NULL;;
41 extern enum nss_status
42 _nss_netgroup_parseline (char **cursor, struct __netgrent *result,
43 char *buffer, size_t buflen, int *errnop);
45 enum nss_status
46 _nss_nis_setnetgrent (char *group)
48 char *domain;
49 char *result;
50 int len, group_len;
51 enum nss_status status;
53 status = NSS_STATUS_SUCCESS;
55 if (group == NULL || group[0] == '\0')
56 return NSS_STATUS_UNAVAIL;
58 if (yp_get_default_domain (&domain))
59 return NSS_STATUS_UNAVAIL;
61 __libc_lock_lock (lock);
63 if (data != NULL)
65 free (data);
66 data = NULL;
67 data_size = 0;
68 cursor = NULL;
71 group_len = strlen (group);
73 status = yperr2nss (yp_match (domain, "netgroup", group, group_len,
74 &result, &len));
75 if (status == NSS_STATUS_SUCCESS)
77 if (len > 0)
79 data = malloc (len + 1);
80 data_size = len;
81 cursor = strncpy (data, result, len + 1);
82 data[len] = '\0';
83 free (result);
85 else
86 status = NSS_STATUS_NOTFOUND;
89 __libc_lock_unlock (lock);
91 return status;
95 enum nss_status
96 _nss_nis_endnetgrent (void)
98 __libc_lock_lock (lock);
100 if (data != NULL)
102 free (data);
103 data = NULL;
104 data_size = 0;
105 cursor = NULL;
108 __libc_lock_unlock (lock);
110 return NSS_STATUS_SUCCESS;
113 enum nss_status
114 _nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
115 int *errnop)
117 enum nss_status status;
119 if (cursor == NULL)
120 return NSS_STATUS_NOTFOUND;
122 __libc_lock_lock (lock);
124 status = _nss_netgroup_parseline (&cursor, result, buffer, buflen, errnop);
126 __libc_lock_unlock (lock);
128 return status;