s3:winbindd_reconnect: don't only reconnect on NT_STATUS_UNSUCCESSFUL
[Samba.git] / source3 / winbindd / winbindd_reconnect.c
blobf76a93aa94fbf3f01246089559d9b294c711186d
1 /*
2 Unix SMB/CIFS implementation.
4 Wrapper around winbindd_rpc.c to centralize retry logic.
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "winbindd.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_WINBIND
28 extern struct winbindd_methods msrpc_methods;
30 static bool reconnect_need_retry(NTSTATUS status)
32 if (NT_STATUS_IS_OK(status)) {
33 return false;
36 if (!NT_STATUS_IS_ERR(status)) {
37 return false;
40 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
41 return false;
44 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
45 return false;
48 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_GROUP)) {
49 return false;
52 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_ALIAS)) {
53 return false;
56 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_MEMBER)) {
57 return false;
60 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_DOMAIN)) {
61 return false;
64 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_PRIVILEGE)) {
65 return false;
68 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
69 return false;
72 return true;
75 /* List all users */
76 static NTSTATUS query_user_list(struct winbindd_domain *domain,
77 TALLOC_CTX *mem_ctx,
78 uint32 *num_entries,
79 WINBIND_USERINFO **info)
81 NTSTATUS result;
83 result = msrpc_methods.query_user_list(domain, mem_ctx,
84 num_entries, info);
86 if (reconnect_need_retry(result))
87 result = msrpc_methods.query_user_list(domain, mem_ctx,
88 num_entries, info);
89 return result;
92 /* list all domain groups */
93 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
94 TALLOC_CTX *mem_ctx,
95 uint32 *num_entries,
96 struct acct_info **info)
98 NTSTATUS result;
100 result = msrpc_methods.enum_dom_groups(domain, mem_ctx,
101 num_entries, info);
103 if (reconnect_need_retry(result))
104 result = msrpc_methods.enum_dom_groups(domain, mem_ctx,
105 num_entries, info);
106 return result;
109 /* List all domain groups */
111 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
112 TALLOC_CTX *mem_ctx,
113 uint32 *num_entries,
114 struct acct_info **info)
116 NTSTATUS result;
118 result = msrpc_methods.enum_local_groups(domain, mem_ctx,
119 num_entries, info);
121 if (reconnect_need_retry(result))
122 result = msrpc_methods.enum_local_groups(domain, mem_ctx,
123 num_entries, info);
125 return result;
128 /* convert a single name to a sid in a domain */
129 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
130 TALLOC_CTX *mem_ctx,
131 enum winbindd_cmd orig_cmd,
132 const char *domain_name,
133 const char *name,
134 DOM_SID *sid,
135 enum lsa_SidType *type)
137 NTSTATUS result;
139 result = msrpc_methods.name_to_sid(domain, mem_ctx, orig_cmd,
140 domain_name, name,
141 sid, type);
143 if (reconnect_need_retry(result))
144 result = msrpc_methods.name_to_sid(domain, mem_ctx, orig_cmd,
145 domain_name, name,
146 sid, type);
148 return result;
152 convert a domain SID to a user or group name
154 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
155 TALLOC_CTX *mem_ctx,
156 const DOM_SID *sid,
157 char **domain_name,
158 char **name,
159 enum lsa_SidType *type)
161 NTSTATUS result;
163 result = msrpc_methods.sid_to_name(domain, mem_ctx, sid,
164 domain_name, name, type);
166 if (reconnect_need_retry(result))
167 result = msrpc_methods.sid_to_name(domain, mem_ctx, sid,
168 domain_name, name, type);
170 return result;
173 static NTSTATUS rids_to_names(struct winbindd_domain *domain,
174 TALLOC_CTX *mem_ctx,
175 const DOM_SID *sid,
176 uint32 *rids,
177 size_t num_rids,
178 char **domain_name,
179 char ***names,
180 enum lsa_SidType **types)
182 NTSTATUS result;
184 result = msrpc_methods.rids_to_names(domain, mem_ctx, sid,
185 rids, num_rids,
186 domain_name, names, types);
187 if (reconnect_need_retry(result)) {
188 result = msrpc_methods.rids_to_names(domain, mem_ctx, sid,
189 rids, num_rids,
190 domain_name, names,
191 types);
194 return result;
197 /* Lookup user information from a rid or username. */
198 static NTSTATUS query_user(struct winbindd_domain *domain,
199 TALLOC_CTX *mem_ctx,
200 const DOM_SID *user_sid,
201 WINBIND_USERINFO *user_info)
203 NTSTATUS result;
205 result = msrpc_methods.query_user(domain, mem_ctx, user_sid,
206 user_info);
208 if (reconnect_need_retry(result))
209 result = msrpc_methods.query_user(domain, mem_ctx, user_sid,
210 user_info);
212 return result;
215 /* Lookup groups a user is a member of. I wish Unix had a call like this! */
216 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
217 TALLOC_CTX *mem_ctx,
218 const DOM_SID *user_sid,
219 uint32 *num_groups, DOM_SID **user_gids)
221 NTSTATUS result;
223 result = msrpc_methods.lookup_usergroups(domain, mem_ctx,
224 user_sid, num_groups,
225 user_gids);
227 if (reconnect_need_retry(result))
228 result = msrpc_methods.lookup_usergroups(domain, mem_ctx,
229 user_sid, num_groups,
230 user_gids);
232 return result;
235 static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
236 TALLOC_CTX *mem_ctx,
237 uint32 num_sids, const DOM_SID *sids,
238 uint32 *num_aliases, uint32 **alias_rids)
240 NTSTATUS result;
242 result = msrpc_methods.lookup_useraliases(domain, mem_ctx,
243 num_sids, sids,
244 num_aliases,
245 alias_rids);
247 if (reconnect_need_retry(result))
248 result = msrpc_methods.lookup_useraliases(domain, mem_ctx,
249 num_sids, sids,
250 num_aliases,
251 alias_rids);
253 return result;
256 /* Lookup group membership given a rid. */
257 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
258 TALLOC_CTX *mem_ctx,
259 const DOM_SID *group_sid, uint32 *num_names,
260 DOM_SID **sid_mem, char ***names,
261 uint32 **name_types)
263 NTSTATUS result;
265 result = msrpc_methods.lookup_groupmem(domain, mem_ctx,
266 group_sid, num_names,
267 sid_mem, names,
268 name_types);
270 if (reconnect_need_retry(result))
271 result = msrpc_methods.lookup_groupmem(domain, mem_ctx,
272 group_sid, num_names,
273 sid_mem, names,
274 name_types);
276 return result;
279 /* find the sequence number for a domain */
280 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
282 NTSTATUS result;
284 result = msrpc_methods.sequence_number(domain, seq);
286 if (reconnect_need_retry(result))
287 result = msrpc_methods.sequence_number(domain, seq);
289 return result;
292 /* find the lockout policy of a domain */
293 static NTSTATUS lockout_policy(struct winbindd_domain *domain,
294 TALLOC_CTX *mem_ctx,
295 struct samr_DomInfo12 *policy)
297 NTSTATUS result;
299 result = msrpc_methods.lockout_policy(domain, mem_ctx, policy);
301 if (reconnect_need_retry(result))
302 result = msrpc_methods.lockout_policy(domain, mem_ctx, policy);
304 return result;
307 /* find the password policy of a domain */
308 static NTSTATUS password_policy(struct winbindd_domain *domain,
309 TALLOC_CTX *mem_ctx,
310 struct samr_DomInfo1 *policy)
312 NTSTATUS result;
314 result = msrpc_methods.password_policy(domain, mem_ctx, policy);
316 if (reconnect_need_retry(result))
317 result = msrpc_methods.password_policy(domain, mem_ctx, policy);
319 return result;
322 /* get a list of trusted domains */
323 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
324 TALLOC_CTX *mem_ctx,
325 uint32 *num_domains,
326 char ***names,
327 char ***alt_names,
328 DOM_SID **dom_sids)
330 NTSTATUS result;
332 result = msrpc_methods.trusted_domains(domain, mem_ctx,
333 num_domains, names,
334 alt_names, dom_sids);
336 if (reconnect_need_retry(result))
337 result = msrpc_methods.trusted_domains(domain, mem_ctx,
338 num_domains, names,
339 alt_names, dom_sids);
341 return result;
344 /* the rpc backend methods are exposed via this structure */
345 struct winbindd_methods reconnect_methods = {
346 False,
347 query_user_list,
348 enum_dom_groups,
349 enum_local_groups,
350 name_to_sid,
351 sid_to_name,
352 rids_to_names,
353 query_user,
354 lookup_usergroups,
355 lookup_useraliases,
356 lookup_groupmem,
357 sequence_number,
358 lockout_policy,
359 password_policy,
360 trusted_domains,