]>
Commit | Line | Data |
---|---|---|
fc9f33e3 | 1 | /* Copyright (C) 1997, 2001 Free Software Foundation, Inc. |
e61abf83 UD |
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 | |
41bdb6e2 AJ |
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. | |
e61abf83 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
e61abf83 | 14 | |
41bdb6e2 AJ |
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 | |
18 | 02111-1307 USA. */ | |
e61abf83 UD |
19 | |
20 | #include <nss.h> | |
21 | #include <errno.h> | |
22 | #include <shadow.h> | |
23 | #include <string.h> | |
5107cf1d | 24 | #include <bits/libc-lock.h> |
e61abf83 | 25 | #include <rpcsvc/nis.h> |
e61abf83 UD |
26 | |
27 | #include "nss-nisplus.h" | |
2d7da676 | 28 | #include "nisplus-parser.h" |
e61abf83 UD |
29 | |
30 | __libc_lock_define_initialized (static, lock) | |
31 | ||
fc9f33e3 UD |
32 | static nis_result *result; |
33 | static nis_name tablename_val; | |
34 | static u_long tablename_len; | |
e61abf83 | 35 | |
2d7da676 | 36 | static enum nss_status |
d71b808a | 37 | _nss_create_tablename (int *errnop) |
e61abf83 | 38 | { |
2d7da676 | 39 | if (tablename_val == NULL) |
714a562f | 40 | { |
2d7da676 UD |
41 | char buf [40 + strlen (nis_local_directory ())]; |
42 | char *p; | |
43 | ||
9a0a462c UD |
44 | p = __stpcpy (buf, "passwd.org_dir."); |
45 | p = __stpcpy (p, nis_local_directory ()); | |
46 | tablename_val = __strdup (buf); | |
2d7da676 | 47 | if (tablename_val == NULL) |
d71b808a UD |
48 | { |
49 | *errnop = errno; | |
50 | return NSS_STATUS_TRYAGAIN; | |
51 | } | |
2d7da676 | 52 | tablename_len = strlen (tablename_val); |
714a562f | 53 | } |
2d7da676 | 54 | return NSS_STATUS_SUCCESS; |
e61abf83 UD |
55 | } |
56 | ||
57 | enum nss_status | |
51eecc4a | 58 | _nss_nisplus_setspent (int stayopen) |
e61abf83 | 59 | { |
2d7da676 | 60 | enum nss_status status = NSS_STATUS_SUCCESS; |
d71b808a | 61 | int err; |
2d7da676 | 62 | |
e61abf83 UD |
63 | __libc_lock_lock (lock); |
64 | ||
65 | if (result) | |
66 | nis_freeresult (result); | |
67 | result = NULL; | |
2d7da676 UD |
68 | |
69 | if (tablename_val == NULL) | |
d71b808a | 70 | status = _nss_create_tablename (&err); |
e61abf83 UD |
71 | |
72 | __libc_lock_unlock (lock); | |
73 | ||
74 | return NSS_STATUS_SUCCESS; | |
75 | } | |
76 | ||
77 | enum nss_status | |
78 | _nss_nisplus_endspent (void) | |
79 | { | |
80 | __libc_lock_lock (lock); | |
81 | ||
82 | if (result) | |
83 | nis_freeresult (result); | |
84 | result = NULL; | |
e61abf83 UD |
85 | |
86 | __libc_lock_unlock (lock); | |
87 | ||
88 | return NSS_STATUS_SUCCESS; | |
89 | } | |
90 | ||
91 | static enum nss_status | |
d71b808a UD |
92 | internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen, |
93 | int *errnop) | |
e61abf83 UD |
94 | { |
95 | int parse_res; | |
96 | ||
97 | /* Get the next entry until we found a correct one. */ | |
98 | do | |
99 | { | |
60c96635 UD |
100 | nis_result *saved_res; |
101 | ||
e61abf83 UD |
102 | if (result == NULL) |
103 | { | |
60c96635 UD |
104 | saved_res = NULL; |
105 | ||
2d7da676 | 106 | if (tablename_val == NULL) |
d71b808a UD |
107 | { |
108 | enum nss_status status = _nss_create_tablename (errnop); | |
109 | ||
110 | if (status != NSS_STATUS_SUCCESS) | |
111 | return status; | |
112 | } | |
e61abf83 | 113 | |
2d7da676 | 114 | result = nis_first_entry (tablename_val); |
e61abf83 UD |
115 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
116 | return niserr2nss (result->status); | |
117 | } | |
118 | else | |
119 | { | |
120 | nis_result *res; | |
121 | ||
60c96635 | 122 | saved_res = result; |
2d7da676 | 123 | res = nis_next_entry (tablename_val, &result->cookie); |
e61abf83 UD |
124 | result = res; |
125 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
60c96635 UD |
126 | { |
127 | nis_freeresult (saved_res); | |
128 | return niserr2nss (result->status); | |
129 | } | |
e61abf83 UD |
130 | } |
131 | ||
d71b808a UD |
132 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, |
133 | buflen, errnop); | |
134 | if (parse_res == -1) | |
60c96635 UD |
135 | { |
136 | nis_freeresult (result); | |
137 | result = saved_res; | |
d71b808a | 138 | *errnop = ERANGE; |
60c96635 UD |
139 | return NSS_STATUS_TRYAGAIN; |
140 | } | |
141 | else | |
142 | { | |
143 | if (saved_res) | |
144 | nis_freeresult (saved_res); | |
145 | } | |
e61abf83 UD |
146 | } while (!parse_res); |
147 | ||
148 | return NSS_STATUS_SUCCESS; | |
149 | } | |
150 | ||
151 | enum nss_status | |
d71b808a UD |
152 | _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen, |
153 | int *errnop) | |
e61abf83 UD |
154 | { |
155 | int status; | |
156 | ||
157 | __libc_lock_lock (lock); | |
158 | ||
d71b808a | 159 | status = internal_nisplus_getspent_r (result, buffer, buflen, errnop); |
e61abf83 UD |
160 | |
161 | __libc_lock_unlock (lock); | |
162 | ||
163 | return status; | |
164 | } | |
165 | ||
166 | enum nss_status | |
167 | _nss_nisplus_getspnam_r (const char *name, struct spwd *sp, | |
d71b808a | 168 | char *buffer, size_t buflen, int *errnop) |
e61abf83 UD |
169 | { |
170 | int parse_res; | |
171 | ||
2d7da676 | 172 | if (tablename_val == NULL) |
d71b808a UD |
173 | { |
174 | enum nss_status status = _nss_create_tablename (errnop); | |
175 | ||
176 | if (status != NSS_STATUS_SUCCESS) | |
177 | return status; | |
178 | } | |
2d7da676 | 179 | |
ac9f45cf UD |
180 | if (name == NULL) |
181 | { | |
182 | *errnop = EINVAL; | |
183 | return NSS_STATUS_NOTFOUND; | |
184 | } | |
e61abf83 UD |
185 | else |
186 | { | |
187 | nis_result *result; | |
2d7da676 | 188 | char buf[strlen (name) + 24 + tablename_len]; |
e61abf83 | 189 | |
2d7da676 | 190 | sprintf (buf, "[name=%s],%s", name, tablename_val); |
e61abf83 | 191 | |
2d7da676 | 192 | result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL); |
e61abf83 UD |
193 | |
194 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
195 | { | |
196 | enum nss_status status = niserr2nss (result->status); | |
197 | ||
198 | nis_freeresult (result); | |
199 | return status; | |
200 | } | |
201 | ||
d71b808a UD |
202 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen, |
203 | errnop); | |
e61abf83 UD |
204 | nis_freeresult (result); |
205 | ||
ac9f45cf | 206 | if (parse_res < 1) |
d71b808a | 207 | { |
ac9f45cf UD |
208 | if (parse_res == -1) |
209 | { | |
210 | *errnop = ERANGE; | |
211 | return NSS_STATUS_TRYAGAIN; | |
212 | } | |
213 | else | |
214 | return NSS_STATUS_NOTFOUND; | |
d71b808a | 215 | } |
ac9f45cf | 216 | return NSS_STATUS_SUCCESS; |
e61abf83 UD |
217 | } |
218 | } |