]>
Commit | Line | Data |
---|---|---|
e61abf83 UD |
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 <pwd.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" | |
28 | ||
29 | __libc_lock_define_initialized (static, lock) | |
30 | ||
31 | static nis_result *result = NULL; | |
2d7da676 UD |
32 | static nis_name tablename_val = NULL; |
33 | static u_long tablename_len = 0; | |
e61abf83 | 34 | |
2d7da676 UD |
35 | extern int _nss_nisplus_parse_pwent (nis_result *res, struct passwd *pw, |
36 | char *buffer, size_t buflen); | |
e61abf83 | 37 | |
2d7da676 UD |
38 | static enum nss_status |
39 | _nss_create_tablename (void) | |
e61abf83 | 40 | { |
2d7da676 | 41 | if (tablename_val == NULL) |
e61abf83 | 42 | { |
2d7da676 UD |
43 | char buf [40 + strlen (nis_local_directory ())]; |
44 | char *p; | |
45 | ||
46 | p = stpcpy (buf, "passwd.org_dir."); | |
47 | p = stpcpy (p, nis_local_directory ()); | |
48 | tablename_val = strdup (buf); | |
49 | if (tablename_val == NULL) | |
50 | return NSS_STATUS_TRYAGAIN; | |
51 | tablename_len = strlen (tablename_val); | |
e61abf83 | 52 | } |
2d7da676 | 53 | return NSS_STATUS_SUCCESS; |
e61abf83 UD |
54 | } |
55 | ||
2d7da676 | 56 | |
e61abf83 UD |
57 | enum nss_status |
58 | _nss_nisplus_setpwent (void) | |
59 | { | |
2d7da676 UD |
60 | enum nss_status status = NSS_STATUS_SUCCESS; |
61 | ||
e61abf83 UD |
62 | __libc_lock_lock (lock); |
63 | ||
64 | if (result) | |
65 | nis_freeresult (result); | |
66 | result = NULL; | |
2d7da676 UD |
67 | |
68 | if (tablename_val == NULL) | |
69 | status = _nss_create_tablename (); | |
e61abf83 UD |
70 | |
71 | __libc_lock_unlock (lock); | |
72 | ||
2d7da676 | 73 | return status; |
e61abf83 UD |
74 | } |
75 | ||
76 | enum nss_status | |
77 | _nss_nisplus_endpwent (void) | |
78 | { | |
79 | __libc_lock_lock (lock); | |
80 | ||
81 | if (result) | |
82 | nis_freeresult (result); | |
83 | result = NULL; | |
e61abf83 UD |
84 | |
85 | __libc_lock_unlock (lock); | |
86 | ||
87 | return NSS_STATUS_SUCCESS; | |
88 | } | |
89 | ||
90 | static enum nss_status | |
91 | internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen) | |
92 | { | |
93 | int parse_res; | |
94 | ||
95 | /* Get the next entry until we found a correct one. */ | |
96 | do | |
97 | { | |
60c96635 | 98 | nis_result *saved_res; |
8f2ece69 | 99 | |
e61abf83 UD |
100 | if (result == NULL) |
101 | { | |
60c96635 | 102 | saved_res = NULL; |
2d7da676 UD |
103 | if (tablename_val == NULL) |
104 | if (_nss_create_tablename () != NSS_STATUS_SUCCESS) | |
105 | return NSS_STATUS_UNAVAIL; | |
e61abf83 | 106 | |
2d7da676 | 107 | result = nis_first_entry(tablename_val); |
e61abf83 UD |
108 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
109 | return niserr2nss (result->status); | |
110 | } | |
111 | else | |
112 | { | |
113 | nis_result *res; | |
114 | ||
60c96635 | 115 | saved_res = result; |
2d7da676 | 116 | res = nis_next_entry(tablename_val, &result->cookie); |
e61abf83 UD |
117 | result = res; |
118 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
60c96635 UD |
119 | { |
120 | nis_freeresult (saved_res); | |
121 | return niserr2nss (result->status); | |
122 | } | |
e61abf83 UD |
123 | } |
124 | ||
8f2ece69 | 125 | if ((parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, |
60c96635 UD |
126 | buflen)) == -1) |
127 | { | |
128 | nis_freeresult (result); | |
129 | result = saved_res; | |
130 | return NSS_STATUS_TRYAGAIN; | |
131 | } | |
132 | else | |
133 | { | |
134 | if (saved_res) | |
135 | nis_freeresult (saved_res); | |
136 | } | |
e61abf83 UD |
137 | } while (!parse_res); |
138 | ||
139 | return NSS_STATUS_SUCCESS; | |
140 | } | |
141 | ||
142 | enum nss_status | |
143 | _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen) | |
144 | { | |
145 | int status; | |
146 | ||
147 | __libc_lock_lock (lock); | |
148 | ||
149 | status = internal_nisplus_getpwent_r (result, buffer, buflen); | |
150 | ||
151 | __libc_lock_unlock (lock); | |
152 | ||
153 | return status; | |
154 | } | |
155 | ||
156 | enum nss_status | |
157 | _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw, | |
158 | char *buffer, size_t buflen) | |
159 | { | |
160 | int parse_res; | |
161 | ||
2d7da676 UD |
162 | if (tablename_val == NULL) |
163 | if (_nss_create_tablename () != NSS_STATUS_SUCCESS) | |
164 | return NSS_STATUS_UNAVAIL; | |
165 | ||
e61abf83 UD |
166 | if (name == NULL || strlen (name) > 8) |
167 | return NSS_STATUS_NOTFOUND; | |
168 | else | |
169 | { | |
170 | nis_result *result; | |
2d7da676 | 171 | char buf[strlen (name) + 24 + tablename_len]; |
e61abf83 | 172 | |
2d7da676 | 173 | sprintf(buf, "[name=%s],%s", name, tablename_val); |
e61abf83 | 174 | |
2d7da676 | 175 | result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL); |
e61abf83 UD |
176 | |
177 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
178 | { | |
179 | enum nss_status status = niserr2nss (result->status); | |
180 | ||
181 | nis_freeresult (result); | |
182 | return status; | |
183 | } | |
184 | ||
185 | parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen); | |
186 | ||
187 | nis_freeresult (result); | |
188 | ||
60c96635 UD |
189 | if (parse_res == -1) |
190 | return NSS_STATUS_TRYAGAIN; | |
191 | ||
e61abf83 UD |
192 | if (parse_res) |
193 | return NSS_STATUS_SUCCESS; | |
8f2ece69 | 194 | |
60c96635 | 195 | return NSS_STATUS_NOTFOUND; |
e61abf83 UD |
196 | } |
197 | } | |
198 | ||
199 | enum nss_status | |
200 | _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw, | |
201 | char *buffer, size_t buflen) | |
202 | { | |
2d7da676 UD |
203 | if (tablename_val == NULL) |
204 | if (_nss_create_tablename () != NSS_STATUS_SUCCESS) | |
205 | return NSS_STATUS_UNAVAIL; | |
206 | { | |
207 | int parse_res; | |
208 | nis_result *result; | |
209 | char buf[100 + tablename_len]; | |
e61abf83 | 210 | |
2d7da676 | 211 | sprintf(buf, "[uid=%d],%s", uid, tablename_val); |
e61abf83 | 212 | |
2d7da676 | 213 | result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL); |
e61abf83 | 214 | |
2d7da676 UD |
215 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
216 | { | |
217 | enum nss_status status = niserr2nss (result->status); | |
e61abf83 | 218 | |
2d7da676 UD |
219 | nis_freeresult (result); |
220 | return status; | |
221 | } | |
e61abf83 | 222 | |
2d7da676 | 223 | parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen); |
e61abf83 | 224 | |
2d7da676 | 225 | nis_freeresult (result); |
60c96635 UD |
226 | |
227 | if (parse_res == -1) | |
228 | return NSS_STATUS_TRYAGAIN; | |
8f2ece69 | 229 | |
2d7da676 UD |
230 | if (parse_res) |
231 | return NSS_STATUS_SUCCESS; | |
232 | ||
60c96635 | 233 | return NSS_STATUS_NOTFOUND; |
2d7da676 | 234 | } |
e61abf83 | 235 | } |