Update.
[platform/upstream/linaro-glibc.git] / nis / nss_nisplus / nisplus-netgrp.c
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>
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
28
29 #include "nss-nisplus.h"
30
31 __libc_lock_define_initialized (static, lock)
32
33 static nis_result *data = NULL;
34 static unsigned long data_size = 0;
35 static unsigned long position = 0;
36
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)
39
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)
42
43 static enum nss_status
44 _nss_nisplus_parse_netgroup (struct __netgrent *result, char *buffer,
45                              size_t buflen, int *errnop)
46 {
47   enum nss_status status;
48
49   /* Some sanity checks.  */
50   if (data == NULL || data_size == 0)
51     return NSS_STATUS_NOTFOUND;
52
53   if (position == data_size)
54     return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN;
55
56   if (NISENTRYLEN (position, 1, data) > 0)
57     {
58       /* We have a list of other netgroups.  */
59
60       result->type = group_val;
61       if (NISENTRYLEN (position, 1, data) >= buflen)
62         {
63           *errnop = 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;
72
73       return NSS_STATUS_SUCCESS;
74     }
75
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)
80     {
81       *errnop = ERANGE;
82       status = NSS_STATUS_TRYAGAIN;
83     }
84   else
85     {
86       char *cp = buffer;
87
88       result->type = triple_val;
89
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         }
99
100       if (NISENTRYLEN (position, 3, data) == 0)
101         result->val.triple.user = NULL;
102       else
103         {
104           result->val.triple.user = cp;
105           cp = __stpncpy (cp, NISENTRYVAL (position, 3, data),
106                           NISENTRYLEN (position, 3, data));
107           *cp++ = '\0';
108         }
109
110       if (NISENTRYLEN (position, 4, data) == 0)
111         result->val.triple.domain = NULL;
112       else
113         {
114           result->val.triple.domain = cp;
115           cp = __stpncpy (cp, NISENTRYVAL (position, 4, data),
116                           NISENTRYLEN (position, 4, data));
117           *cp = '\0';
118         }
119
120       status = NSS_STATUS_SUCCESS;
121
122       /* Remember where we stopped reading.  */
123       ++position;
124
125       result->first = 0;
126     }
127
128   return status;
129 }
130
131 enum nss_status
132 _nss_nisplus_setnetgrent (char *group)
133
134 {
135   enum nss_status status;
136   char buf[strlen (group) + 30];
137
138   if (group == NULL || group[0] == '\0')
139     return NSS_STATUS_UNAVAIL;
140
141   status = NSS_STATUS_SUCCESS;
142
143   __libc_lock_lock (lock);
144
145   if (data != NULL)
146     {
147       nis_freeresult (data);
148       data = NULL;
149       data_size = 0;
150       position = 0;
151     }
152
153   sprintf (buf, "[name=%s],netgroup.org_dir", group);
154
155   data = nis_list (buf, EXPAND_NAME, NULL, NULL);
156
157   if (niserr2nss (data->status) != NSS_STATUS_SUCCESS)
158     {
159       status = niserr2nss (data->status);
160       nis_freeresult (data);
161       data = NULL;
162     }
163   else
164     data_size = data->objects.objects_len;
165
166   __libc_lock_unlock (lock);
167
168   return status;
169 }
170
171 enum nss_status
172 _nss_nisplus_endnetgrent (void)
173 {
174   __libc_lock_lock (lock);
175
176   if (data != NULL)
177     {
178       nis_freeresult (data);
179       data = NULL;
180       data_size = 0;
181       position = 0;
182     }
183
184   __libc_lock_unlock (lock);
185
186   return NSS_STATUS_SUCCESS;
187 }
188
189 enum nss_status
190 _nss_nisplus_getnetgrent_r (struct __netgrent *result,
191                             char *buffer, size_t buflen, int *errnop)
192 {
193   enum nss_status status;
194
195   __libc_lock_lock (lock);
196
197   status = _nss_nisplus_parse_netgroup (result, buffer, buflen, errnop);
198
199   __libc_lock_unlock (lock);
200
201   return status;
202 }