]> sourceware.org Git - glibc.git/blob - nis/nss_nisplus/nisplus-grp.c
Update.
[glibc.git] / nis / nss_nisplus / nisplus-grp.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 <grp.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
27 #include <rpcsvc/nislib.h>
28
29 #include "nss-nisplus.h"
30 #include "nisplus-parser.h"
31
32 __libc_lock_define_initialized (static, lock);
33
34 static nis_result *result = NULL;
35 static unsigned long next_entry = 0;
36 static nis_name tablename_val = NULL;
37 static u_long tablename_len = 0;
38
39 static enum nss_status
40 _nss_create_tablename (void)
41 {
42 if (tablename_val == NULL)
43 {
44 char buf [40 + strlen (nis_local_directory ())];
45 char *p;
46
47 p = stpcpy (buf, "group.org_dir.");
48 p = stpcpy (p, nis_local_directory ());
49 tablename_val = strdup (buf);
50 if (tablename_val == NULL)
51 return NSS_STATUS_TRYAGAIN;
52 tablename_len = strlen (tablename_val);
53 }
54 return NSS_STATUS_SUCCESS;
55 }
56
57 static enum nss_status
58 internal_setgrent (void)
59 {
60 if (result)
61 nis_freeresult (result);
62 result = NULL;
63 next_entry = 0;
64
65 if (tablename_val == NULL)
66 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
67 return NSS_STATUS_UNAVAIL;
68
69 result = nis_list (tablename_val, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
70 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
71 {
72 nis_freeresult (result);
73 result = NULL;
74 }
75 return niserr2nss (result->status);
76 }
77
78 enum nss_status
79 _nss_nisplus_setgrent (void)
80 {
81 enum nss_status status;
82
83 __libc_lock_lock (lock);
84
85 status = internal_setgrent ();
86
87 __libc_lock_unlock (lock);
88
89 return status;
90 }
91
92 enum nss_status
93 _nss_nisplus_endgrent (void)
94 {
95 __libc_lock_lock (lock);
96
97 if (result)
98 nis_freeresult (result);
99 result = NULL;
100
101 __libc_lock_unlock (lock);
102
103 return NSS_STATUS_SUCCESS;
104 }
105
106 static enum nss_status
107 internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen)
108 {
109 int parse_res;
110
111 if (result == NULL)
112 internal_setgrent ();
113
114 /* Get the next entry until we found a correct one. */
115 do
116 {
117 if (next_entry >= result->objects.objects_len)
118 return NSS_STATUS_NOTFOUND;
119
120 if ((parse_res = _nss_nisplus_parse_grent (result, next_entry, gr,
121 buffer, buflen)) == -1)
122 return NSS_STATUS_TRYAGAIN;
123
124 ++next_entry;
125 }
126 while (!parse_res);
127
128 return NSS_STATUS_SUCCESS;
129 }
130
131 enum nss_status
132 _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen)
133 {
134 int status;
135
136 __libc_lock_lock (lock);
137
138 status = internal_nisplus_getgrent_r (result, buffer, buflen);
139
140 __libc_lock_unlock (lock);
141
142 return status;
143 }
144
145 enum nss_status
146 _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
147 char *buffer, size_t buflen)
148 {
149 int parse_res;
150
151 if (tablename_val == NULL)
152 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
153 return NSS_STATUS_UNAVAIL;
154
155 if (name == NULL || strlen (name) > 8)
156 return NSS_STATUS_NOTFOUND;
157 else
158 {
159 nis_result *result;
160 char buf[strlen (name) + 24 + tablename_len];
161
162 sprintf (buf, "[name=%s],%s", name, tablename_val);
163
164 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
165
166 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
167 {
168 enum nss_status status = niserr2nss (result->status);
169
170 nis_freeresult (result);
171 return status;
172 }
173
174 parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen);
175 nis_freeresult (result);
176
177 if (parse_res == -1)
178 return NSS_STATUS_TRYAGAIN;
179 if (parse_res)
180 return NSS_STATUS_SUCCESS;
181
182 return NSS_STATUS_NOTFOUND;
183 }
184 }
185
186 enum nss_status
187 _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
188 char *buffer, size_t buflen)
189 {
190 if (tablename_val == NULL)
191 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
192 return NSS_STATUS_UNAVAIL;
193
194 {
195 int parse_res;
196 nis_result *result;
197 char buf[36 + tablename_len];
198
199 sprintf (buf, "[gid=%d],%s", gid, tablename_val);
200
201 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
202
203 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
204 {
205 enum nss_status status = niserr2nss (result->status);
206
207 nis_freeresult (result);
208 return status;
209 }
210
211 parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen);
212
213 nis_freeresult (result);
214
215 if (parse_res == -1)
216 return NSS_STATUS_TRYAGAIN;
217
218 if (parse_res)
219 return NSS_STATUS_SUCCESS;
220
221 return NSS_STATUS_NOTFOUND;
222 }
223 }
This page took 0.045876 seconds and 5 git commands to generate.