| 1 | /* 
 | 
|---|
| 2 |    Unix SMB/CIFS implementation.
 | 
|---|
| 3 | 
 | 
|---|
| 4 |    Wrapper around winbindd_rpc.c to centralize retry logic.
 | 
|---|
| 5 | 
 | 
|---|
| 6 |    Copyright (C) Volker Lendecke 2005
 | 
|---|
| 7 |    
 | 
|---|
| 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.
 | 
|---|
| 12 |    
 | 
|---|
| 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.
 | 
|---|
| 17 |    
 | 
|---|
| 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/>.
 | 
|---|
| 20 | */
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "includes.h"
 | 
|---|
| 23 | #include "winbindd.h"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #undef DBGC_CLASS
 | 
|---|
| 26 | #define DBGC_CLASS DBGC_WINBIND
 | 
|---|
| 27 | 
 | 
|---|
| 28 | extern struct winbindd_methods msrpc_methods;
 | 
|---|
| 29 | 
 | 
|---|
| 30 | static bool reconnect_need_retry(NTSTATUS status)
 | 
|---|
| 31 | {
 | 
|---|
| 32 |         if (NT_STATUS_IS_OK(status)) {
 | 
|---|
| 33 |                 return false;
 | 
|---|
| 34 |         }
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         if (!NT_STATUS_IS_ERR(status)) {
 | 
|---|
| 37 |                 return false;
 | 
|---|
| 38 |         }
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
 | 
|---|
| 41 |                 return false;
 | 
|---|
| 42 |         }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
 | 
|---|
| 45 |                 return false;
 | 
|---|
| 46 |         }
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_GROUP)) {
 | 
|---|
| 49 |                 return false;
 | 
|---|
| 50 |         }
 | 
|---|
| 51 | 
 | 
|---|
| 52 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_ALIAS)) {
 | 
|---|
| 53 |                 return false;
 | 
|---|
| 54 |         }
 | 
|---|
| 55 | 
 | 
|---|
| 56 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_MEMBER)) {
 | 
|---|
| 57 |                 return false;
 | 
|---|
| 58 |         }
 | 
|---|
| 59 | 
 | 
|---|
| 60 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_DOMAIN)) {
 | 
|---|
| 61 |                 return false;
 | 
|---|
| 62 |         }
 | 
|---|
| 63 | 
 | 
|---|
| 64 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_PRIVILEGE)) {
 | 
|---|
| 65 |                 return false;
 | 
|---|
| 66 |         }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
 | 
|---|
| 69 |                 return false;
 | 
|---|
| 70 |         }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         return true;
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | /* List all users */
 | 
|---|
| 76 | static NTSTATUS query_user_list(struct winbindd_domain *domain,
 | 
|---|
| 77 |                                 TALLOC_CTX *mem_ctx,
 | 
|---|
| 78 |                                 uint32 *num_entries, 
 | 
|---|
| 79 |                                 struct wbint_userinfo **info)
 | 
|---|
| 80 | {
 | 
|---|
| 81 |         NTSTATUS result;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |         result = msrpc_methods.query_user_list(domain, mem_ctx,
 | 
|---|
| 84 |                                                num_entries, info);
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         if (reconnect_need_retry(result))
 | 
|---|
| 87 |                 result = msrpc_methods.query_user_list(domain, mem_ctx,
 | 
|---|
| 88 |                                                        num_entries, info);
 | 
|---|
| 89 |         return result;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 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)
 | 
|---|
| 97 | {
 | 
|---|
| 98 |         NTSTATUS result;
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         result = msrpc_methods.enum_dom_groups(domain, mem_ctx,
 | 
|---|
| 101 |                                                num_entries, info);
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         if (reconnect_need_retry(result))
 | 
|---|
| 104 |                 result = msrpc_methods.enum_dom_groups(domain, mem_ctx,
 | 
|---|
| 105 |                                                        num_entries, info);
 | 
|---|
| 106 |         return result;
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | /* List all domain groups */
 | 
|---|
| 110 | 
 | 
|---|
| 111 | static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
 | 
|---|
| 112 |                                   TALLOC_CTX *mem_ctx,
 | 
|---|
| 113 |                                   uint32 *num_entries, 
 | 
|---|
| 114 |                                   struct acct_info **info)
 | 
|---|
| 115 | {
 | 
|---|
| 116 |         NTSTATUS result;
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         result = msrpc_methods.enum_local_groups(domain, mem_ctx,
 | 
|---|
| 119 |                                                  num_entries, info);
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         if (reconnect_need_retry(result))
 | 
|---|
| 122 |                 result = msrpc_methods.enum_local_groups(domain, mem_ctx,
 | 
|---|
| 123 |                                                          num_entries, info);
 | 
|---|
| 124 | 
 | 
|---|
| 125 |         return result;
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 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 |                             const char *domain_name,
 | 
|---|
| 132 |                             const char *name,
 | 
|---|
| 133 |                             uint32_t flags,
 | 
|---|
| 134 |                             DOM_SID *sid,
 | 
|---|
| 135 |                             enum lsa_SidType *type)
 | 
|---|
| 136 | {
 | 
|---|
| 137 |         NTSTATUS result;
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         result = msrpc_methods.name_to_sid(domain, mem_ctx, domain_name, name,
 | 
|---|
| 140 |                                            flags, sid, type);
 | 
|---|
| 141 | 
 | 
|---|
| 142 |         if (reconnect_need_retry(result))
 | 
|---|
| 143 |                 result = msrpc_methods.name_to_sid(domain, mem_ctx,
 | 
|---|
| 144 |                                                    domain_name, name, flags,
 | 
|---|
| 145 |                                                    sid, type);
 | 
|---|
| 146 | 
 | 
|---|
| 147 |         return result;
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | /*
 | 
|---|
| 151 |   convert a domain SID to a user or group name
 | 
|---|
| 152 | */
 | 
|---|
| 153 | static NTSTATUS sid_to_name(struct winbindd_domain *domain,
 | 
|---|
| 154 |                             TALLOC_CTX *mem_ctx,
 | 
|---|
| 155 |                             const DOM_SID *sid,
 | 
|---|
| 156 |                             char **domain_name,
 | 
|---|
| 157 |                             char **name,
 | 
|---|
| 158 |                             enum lsa_SidType *type)
 | 
|---|
| 159 | {
 | 
|---|
| 160 |         NTSTATUS result;
 | 
|---|
| 161 | 
 | 
|---|
| 162 |         result = msrpc_methods.sid_to_name(domain, mem_ctx, sid,
 | 
|---|
| 163 |                                            domain_name, name, type);
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         if (reconnect_need_retry(result))
 | 
|---|
| 166 |                 result = msrpc_methods.sid_to_name(domain, mem_ctx, sid,
 | 
|---|
| 167 |                                                    domain_name, name, type);
 | 
|---|
| 168 | 
 | 
|---|
| 169 |         return result;
 | 
|---|
| 170 | }
 | 
|---|
| 171 | 
 | 
|---|
| 172 | static NTSTATUS rids_to_names(struct winbindd_domain *domain,
 | 
|---|
| 173 |                               TALLOC_CTX *mem_ctx,
 | 
|---|
| 174 |                               const DOM_SID *sid,
 | 
|---|
| 175 |                               uint32 *rids,
 | 
|---|
| 176 |                               size_t num_rids,
 | 
|---|
| 177 |                               char **domain_name,
 | 
|---|
| 178 |                               char ***names,
 | 
|---|
| 179 |                               enum lsa_SidType **types)
 | 
|---|
| 180 | {
 | 
|---|
| 181 |         NTSTATUS result;
 | 
|---|
| 182 | 
 | 
|---|
| 183 |         result = msrpc_methods.rids_to_names(domain, mem_ctx, sid,
 | 
|---|
| 184 |                                              rids, num_rids,
 | 
|---|
| 185 |                                              domain_name, names, types);
 | 
|---|
| 186 |         if (reconnect_need_retry(result)) {
 | 
|---|
| 187 |                 result = msrpc_methods.rids_to_names(domain, mem_ctx, sid,
 | 
|---|
| 188 |                                                      rids, num_rids,
 | 
|---|
| 189 |                                                      domain_name, names,
 | 
|---|
| 190 |                                                      types);
 | 
|---|
| 191 |         }
 | 
|---|
| 192 | 
 | 
|---|
| 193 |         return result;
 | 
|---|
| 194 | }
 | 
|---|
| 195 | 
 | 
|---|
| 196 | /* Lookup user information from a rid or username. */
 | 
|---|
| 197 | static NTSTATUS query_user(struct winbindd_domain *domain, 
 | 
|---|
| 198 |                            TALLOC_CTX *mem_ctx, 
 | 
|---|
| 199 |                            const DOM_SID *user_sid,
 | 
|---|
| 200 |                            struct wbint_userinfo *user_info)
 | 
|---|
| 201 | {
 | 
|---|
| 202 |         NTSTATUS result;
 | 
|---|
| 203 | 
 | 
|---|
| 204 |         result = msrpc_methods.query_user(domain, mem_ctx, user_sid,
 | 
|---|
| 205 |                                           user_info);
 | 
|---|
| 206 | 
 | 
|---|
| 207 |         if (reconnect_need_retry(result))
 | 
|---|
| 208 |                 result = msrpc_methods.query_user(domain, mem_ctx, user_sid,
 | 
|---|
| 209 |                                                   user_info);
 | 
|---|
| 210 | 
 | 
|---|
| 211 |         return result;
 | 
|---|
| 212 | }
 | 
|---|
| 213 | 
 | 
|---|
| 214 | /* Lookup groups a user is a member of.  I wish Unix had a call like this! */
 | 
|---|
| 215 | static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
 | 
|---|
| 216 |                                   TALLOC_CTX *mem_ctx,
 | 
|---|
| 217 |                                   const DOM_SID *user_sid,
 | 
|---|
| 218 |                                   uint32 *num_groups, DOM_SID **user_gids)
 | 
|---|
| 219 | {
 | 
|---|
| 220 |         NTSTATUS result;
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         result = msrpc_methods.lookup_usergroups(domain, mem_ctx,
 | 
|---|
| 223 |                                                  user_sid, num_groups,
 | 
|---|
| 224 |                                                  user_gids);
 | 
|---|
| 225 | 
 | 
|---|
| 226 |         if (reconnect_need_retry(result))
 | 
|---|
| 227 |                 result = msrpc_methods.lookup_usergroups(domain, mem_ctx,
 | 
|---|
| 228 |                                                          user_sid, num_groups,
 | 
|---|
| 229 |                                                          user_gids);
 | 
|---|
| 230 | 
 | 
|---|
| 231 |         return result;
 | 
|---|
| 232 | }
 | 
|---|
| 233 | 
 | 
|---|
| 234 | static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
 | 
|---|
| 235 |                                    TALLOC_CTX *mem_ctx,
 | 
|---|
| 236 |                                    uint32 num_sids, const DOM_SID *sids,
 | 
|---|
| 237 |                                    uint32 *num_aliases, uint32 **alias_rids)
 | 
|---|
| 238 | {
 | 
|---|
| 239 |         NTSTATUS result;
 | 
|---|
| 240 | 
 | 
|---|
| 241 |         result = msrpc_methods.lookup_useraliases(domain, mem_ctx,
 | 
|---|
| 242 |                                                   num_sids, sids,
 | 
|---|
| 243 |                                                   num_aliases,
 | 
|---|
| 244 |                                                   alias_rids);
 | 
|---|
| 245 | 
 | 
|---|
| 246 |         if (reconnect_need_retry(result))
 | 
|---|
| 247 |                 result = msrpc_methods.lookup_useraliases(domain, mem_ctx,
 | 
|---|
| 248 |                                                           num_sids, sids,
 | 
|---|
| 249 |                                                           num_aliases,
 | 
|---|
| 250 |                                                           alias_rids);
 | 
|---|
| 251 | 
 | 
|---|
| 252 |         return result;
 | 
|---|
| 253 | }
 | 
|---|
| 254 | 
 | 
|---|
| 255 | /* Lookup group membership given a rid.   */
 | 
|---|
| 256 | static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
 | 
|---|
| 257 |                                 TALLOC_CTX *mem_ctx,
 | 
|---|
| 258 |                                 const DOM_SID *group_sid,
 | 
|---|
| 259 |                                 enum lsa_SidType type,
 | 
|---|
| 260 |                                 uint32 *num_names,
 | 
|---|
| 261 |                                 DOM_SID **sid_mem, char ***names, 
 | 
|---|
| 262 |                                 uint32 **name_types)
 | 
|---|
| 263 | {
 | 
|---|
| 264 |         NTSTATUS result;
 | 
|---|
| 265 | 
 | 
|---|
| 266 |         result = msrpc_methods.lookup_groupmem(domain, mem_ctx,
 | 
|---|
| 267 |                                                group_sid, type, num_names,
 | 
|---|
| 268 |                                                sid_mem, names,
 | 
|---|
| 269 |                                                name_types);
 | 
|---|
| 270 | 
 | 
|---|
| 271 |         if (reconnect_need_retry(result))
 | 
|---|
| 272 |                 result = msrpc_methods.lookup_groupmem(domain, mem_ctx,
 | 
|---|
| 273 |                                                        group_sid, type,
 | 
|---|
| 274 |                                                        num_names,
 | 
|---|
| 275 |                                                        sid_mem, names,
 | 
|---|
| 276 |                                                        name_types);
 | 
|---|
| 277 | 
 | 
|---|
| 278 |         return result;
 | 
|---|
| 279 | }
 | 
|---|
| 280 | 
 | 
|---|
| 281 | /* find the sequence number for a domain */
 | 
|---|
| 282 | static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
 | 
|---|
| 283 | {
 | 
|---|
| 284 |         NTSTATUS result;
 | 
|---|
| 285 | 
 | 
|---|
| 286 |         result = msrpc_methods.sequence_number(domain, seq);
 | 
|---|
| 287 | 
 | 
|---|
| 288 |         if (reconnect_need_retry(result))
 | 
|---|
| 289 |                 result = msrpc_methods.sequence_number(domain, seq);
 | 
|---|
| 290 | 
 | 
|---|
| 291 |         return result;
 | 
|---|
| 292 | }
 | 
|---|
| 293 | 
 | 
|---|
| 294 | /* find the lockout policy of a domain */
 | 
|---|
| 295 | static NTSTATUS lockout_policy(struct winbindd_domain *domain, 
 | 
|---|
| 296 |                                TALLOC_CTX *mem_ctx,
 | 
|---|
| 297 |                                struct samr_DomInfo12 *policy)
 | 
|---|
| 298 | {
 | 
|---|
| 299 |         NTSTATUS result;
 | 
|---|
| 300 | 
 | 
|---|
| 301 |         result = msrpc_methods.lockout_policy(domain, mem_ctx, policy);
 | 
|---|
| 302 | 
 | 
|---|
| 303 |         if (reconnect_need_retry(result))
 | 
|---|
| 304 |                 result = msrpc_methods.lockout_policy(domain, mem_ctx, policy);
 | 
|---|
| 305 | 
 | 
|---|
| 306 |         return result;
 | 
|---|
| 307 | }
 | 
|---|
| 308 | 
 | 
|---|
| 309 | /* find the password policy of a domain */
 | 
|---|
| 310 | static NTSTATUS password_policy(struct winbindd_domain *domain, 
 | 
|---|
| 311 |                                 TALLOC_CTX *mem_ctx,
 | 
|---|
| 312 |                                 struct samr_DomInfo1 *policy)
 | 
|---|
| 313 | {
 | 
|---|
| 314 |         NTSTATUS result;
 | 
|---|
| 315 |  
 | 
|---|
| 316 |         result = msrpc_methods.password_policy(domain, mem_ctx, policy);
 | 
|---|
| 317 | 
 | 
|---|
| 318 |         if (reconnect_need_retry(result))
 | 
|---|
| 319 |                 result = msrpc_methods.password_policy(domain, mem_ctx, policy);
 | 
|---|
| 320 |         
 | 
|---|
| 321 |         return result;
 | 
|---|
| 322 | }
 | 
|---|
| 323 | 
 | 
|---|
| 324 | /* get a list of trusted domains */
 | 
|---|
| 325 | static NTSTATUS trusted_domains(struct winbindd_domain *domain,
 | 
|---|
| 326 |                                 TALLOC_CTX *mem_ctx,
 | 
|---|
| 327 |                                 struct netr_DomainTrustList *trusts)
 | 
|---|
| 328 | {
 | 
|---|
| 329 |         NTSTATUS result;
 | 
|---|
| 330 | 
 | 
|---|
| 331 |         result = msrpc_methods.trusted_domains(domain, mem_ctx, trusts);
 | 
|---|
| 332 | 
 | 
|---|
| 333 |         if (reconnect_need_retry(result))
 | 
|---|
| 334 |                 result = msrpc_methods.trusted_domains(domain, mem_ctx,
 | 
|---|
| 335 |                                                        trusts);
 | 
|---|
| 336 | 
 | 
|---|
| 337 |         return result;
 | 
|---|
| 338 | }
 | 
|---|
| 339 | 
 | 
|---|
| 340 | /* the rpc backend methods are exposed via this structure */
 | 
|---|
| 341 | struct winbindd_methods reconnect_methods = {
 | 
|---|
| 342 |         False,
 | 
|---|
| 343 |         query_user_list,
 | 
|---|
| 344 |         enum_dom_groups,
 | 
|---|
| 345 |         enum_local_groups,
 | 
|---|
| 346 |         name_to_sid,
 | 
|---|
| 347 |         sid_to_name,
 | 
|---|
| 348 |         rids_to_names,
 | 
|---|
| 349 |         query_user,
 | 
|---|
| 350 |         lookup_usergroups,
 | 
|---|
| 351 |         lookup_useraliases,
 | 
|---|
| 352 |         lookup_groupmem,
 | 
|---|
| 353 |         sequence_number,
 | 
|---|
| 354 |         lockout_policy,
 | 
|---|
| 355 |         password_policy,
 | 
|---|
| 356 |         trusted_domains,
 | 
|---|
| 357 | };
 | 
|---|