e39c282b8b88aecf1181ceb2676809a2a4e51208
[platform/upstream/glibc.git] / nis / nss_nis / nis-netgrp.c
1 /* Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
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 <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>
31
32 #include "nss-nis.h"
33
34 /* Locks the static variables in this file.  */
35 __libc_lock_define_initialized (static, lock)
36
37 static char *data = NULL;
38 static size_t data_size = 0;
39 static char *cursor = NULL;;
40
41 extern enum nss_status
42 _nss_netgroup_parseline (char **cursor, struct __netgrent *result,
43                          char *buffer, size_t buflen, int *errnop);
44
45 enum nss_status
46 _nss_nis_setnetgrent (const char *group, struct __netgrent *dummy)
47 {
48   char *domain;
49   char *result;
50   int len, group_len;
51   enum nss_status status;
52
53   status = NSS_STATUS_SUCCESS;
54
55   if (group == NULL || group[0] == '\0')
56     return NSS_STATUS_UNAVAIL;
57
58   if (yp_get_default_domain (&domain))
59     return NSS_STATUS_UNAVAIL;
60
61   __libc_lock_lock (lock);
62
63   if (data != NULL)
64     {
65       free (data);
66       data = NULL;
67       data_size = 0;
68       cursor = NULL;
69     }
70
71   group_len = strlen (group);
72
73   status = yperr2nss (yp_match (domain, "netgroup", group, group_len,
74                                 &result, &len));
75   if (status == NSS_STATUS_SUCCESS)
76     {
77       if (len > 0 && (data = malloc (len + 1)) != NULL)
78         {
79           data_size = len;
80           cursor = strncpy (data, result, len + 1);
81           data[len] = '\0';
82           free (result);
83         }
84       else
85         status = NSS_STATUS_NOTFOUND;
86     }
87
88   __libc_lock_unlock (lock);
89
90   return status;
91 }
92
93
94 enum nss_status
95 _nss_nis_endnetgrent (struct __netgrent *dummy)
96 {
97   __libc_lock_lock (lock);
98
99   if (data != NULL)
100     {
101       free (data);
102       data = NULL;
103       data_size = 0;
104       cursor = NULL;
105     }
106
107   __libc_lock_unlock (lock);
108
109   return NSS_STATUS_SUCCESS;
110 }
111
112 enum nss_status
113 _nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
114                         int *errnop)
115 {
116   enum nss_status status;
117
118   if (cursor == NULL)
119     {
120       *errnop = ENOENT;
121       return NSS_STATUS_NOTFOUND;
122     }
123
124   __libc_lock_lock (lock);
125
126   status = _nss_netgroup_parseline (&cursor, result, buffer, buflen, errnop);
127
128   __libc_lock_unlock (lock);
129
130   return status;
131 }