| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | Winbind ADS backend functions
|
|---|
| 5 |
|
|---|
| 6 | Copyright (C) Andrew Tridgell 2001
|
|---|
| 7 | Copyright (C) Andrew Bartlett <[email protected]> 2003
|
|---|
| 8 | Copyright (C) Gerald (Jerry) Carter 2004
|
|---|
| 9 |
|
|---|
| 10 | This program is free software; you can redistribute it and/or modify
|
|---|
| 11 | it under the terms of the GNU General Public License as published by
|
|---|
| 12 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 13 | (at your option) any later version.
|
|---|
| 14 |
|
|---|
| 15 | This program is distributed in the hope that it will be useful,
|
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 18 | GNU General Public License for more details.
|
|---|
| 19 |
|
|---|
| 20 | You should have received a copy of the GNU General Public License
|
|---|
| 21 | along with this program; if not, write to the Free Software
|
|---|
| 22 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include "includes.h"
|
|---|
| 26 | #include "winbindd.h"
|
|---|
| 27 |
|
|---|
| 28 | #ifdef HAVE_ADS
|
|---|
| 29 |
|
|---|
| 30 | #undef DBGC_CLASS
|
|---|
| 31 | #define DBGC_CLASS DBGC_WINBIND
|
|---|
| 32 |
|
|---|
| 33 | extern struct winbindd_methods reconnect_methods;
|
|---|
| 34 |
|
|---|
| 35 | /*
|
|---|
| 36 | return our ads connections structure for a domain. We keep the connection
|
|---|
| 37 | open to make things faster
|
|---|
| 38 | */
|
|---|
| 39 | static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
|
|---|
| 40 | {
|
|---|
| 41 | ADS_STRUCT *ads;
|
|---|
| 42 | ADS_STATUS status;
|
|---|
| 43 | fstring dc_name;
|
|---|
| 44 | struct in_addr dc_ip;
|
|---|
| 45 |
|
|---|
| 46 | DEBUG(10,("ads_cached_connection\n"));
|
|---|
| 47 |
|
|---|
| 48 | if (domain->private_data) {
|
|---|
| 49 |
|
|---|
| 50 | time_t expire;
|
|---|
| 51 | time_t now = time(NULL);
|
|---|
| 52 |
|
|---|
| 53 | /* check for a valid structure */
|
|---|
| 54 | ads = (ADS_STRUCT *)domain->private_data;
|
|---|
| 55 |
|
|---|
| 56 | expire = MIN(ads->auth.tgt_expire, ads->auth.tgs_expire);
|
|---|
| 57 |
|
|---|
| 58 | DEBUG(7, ("Current tickets expire in %d seconds (at %d, time is now %d)\n",
|
|---|
| 59 | (uint32)expire-(uint32)now, (uint32) expire, (uint32) now));
|
|---|
| 60 |
|
|---|
| 61 | if ( ads->config.realm && (expire > now)) {
|
|---|
| 62 | return ads;
|
|---|
| 63 | } else {
|
|---|
| 64 | /* we own this ADS_STRUCT so make sure it goes away */
|
|---|
| 65 | DEBUG(7,("Deleting expired krb5 credential cache\n"));
|
|---|
| 66 | ads->is_mine = True;
|
|---|
| 67 | ads_destroy( &ads );
|
|---|
| 68 | ads_kdestroy("MEMORY:winbind_ccache");
|
|---|
| 69 | domain->private_data = NULL;
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /* we don't want this to affect the users ccache */
|
|---|
| 74 | setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
|
|---|
| 75 |
|
|---|
| 76 | ads = ads_init(domain->alt_name, domain->name, NULL);
|
|---|
| 77 | if (!ads) {
|
|---|
| 78 | DEBUG(1,("ads_init for domain %s failed\n", domain->name));
|
|---|
| 79 | return NULL;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /* the machine acct password might have change - fetch it every time */
|
|---|
| 83 |
|
|---|
| 84 | SAFE_FREE(ads->auth.password);
|
|---|
| 85 | SAFE_FREE(ads->auth.realm);
|
|---|
| 86 |
|
|---|
| 87 | if ( IS_DC ) {
|
|---|
| 88 | DOM_SID sid;
|
|---|
| 89 | time_t last_set_time;
|
|---|
| 90 |
|
|---|
| 91 | if ( !secrets_fetch_trusted_domain_password( domain->name, &ads->auth.password, &sid, &last_set_time ) ) {
|
|---|
| 92 | ads_destroy( &ads );
|
|---|
| 93 | return NULL;
|
|---|
| 94 | }
|
|---|
| 95 | ads->auth.realm = SMB_STRDUP( ads->server.realm );
|
|---|
| 96 | strupper_m( ads->auth.realm );
|
|---|
| 97 | }
|
|---|
| 98 | else {
|
|---|
| 99 | struct winbindd_domain *our_domain = domain;
|
|---|
| 100 |
|
|---|
| 101 | ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
|
|---|
| 102 |
|
|---|
| 103 | /* always give preference to the alt_name in our
|
|---|
| 104 | primary domain if possible */
|
|---|
| 105 |
|
|---|
| 106 | if ( !domain->primary )
|
|---|
| 107 | our_domain = find_our_domain();
|
|---|
| 108 |
|
|---|
| 109 | if ( our_domain->alt_name[0] != '\0' ) {
|
|---|
| 110 | ads->auth.realm = SMB_STRDUP( our_domain->alt_name );
|
|---|
| 111 | strupper_m( ads->auth.realm );
|
|---|
| 112 | }
|
|---|
| 113 | else
|
|---|
| 114 | ads->auth.realm = SMB_STRDUP( lp_realm() );
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | ads->auth.renewable = WINBINDD_PAM_AUTH_KRB5_RENEW_TIME;
|
|---|
| 118 |
|
|---|
| 119 | /* Setup the server affinity cache. We don't reaally care
|
|---|
| 120 | about the name. Just setup affinity and the KRB5_CONFIG
|
|---|
| 121 | file. */
|
|---|
| 122 |
|
|---|
| 123 | get_dc_name( ads->server.workgroup, ads->server.realm, dc_name, &dc_ip );
|
|---|
| 124 |
|
|---|
| 125 | status = ads_connect(ads);
|
|---|
| 126 | if (!ADS_ERR_OK(status) || !ads->config.realm) {
|
|---|
| 127 | DEBUG(1,("ads_connect for domain %s failed: %s\n",
|
|---|
| 128 | domain->name, ads_errstr(status)));
|
|---|
| 129 | ads_destroy(&ads);
|
|---|
| 130 |
|
|---|
| 131 | /* if we get ECONNREFUSED then it might be a NT4
|
|---|
| 132 | server, fall back to MSRPC */
|
|---|
| 133 | if (status.error_type == ENUM_ADS_ERROR_SYSTEM &&
|
|---|
| 134 | status.err.rc == ECONNREFUSED) {
|
|---|
| 135 | /* 'reconnect_methods' is the MS-RPC backend. */
|
|---|
| 136 | DEBUG(1,("Trying MSRPC methods\n"));
|
|---|
| 137 | domain->backend = &reconnect_methods;
|
|---|
| 138 | }
|
|---|
| 139 | return NULL;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /* set the flag that says we don't own the memory even
|
|---|
| 143 | though we do so that ads_destroy() won't destroy the
|
|---|
| 144 | structure we pass back by reference */
|
|---|
| 145 |
|
|---|
| 146 | ads->is_mine = False;
|
|---|
| 147 |
|
|---|
| 148 | domain->private_data = (void *)ads;
|
|---|
| 149 | return ads;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | /* Query display info for a realm. This is the basic user list fn */
|
|---|
| 154 | static NTSTATUS query_user_list(struct winbindd_domain *domain,
|
|---|
| 155 | TALLOC_CTX *mem_ctx,
|
|---|
| 156 | uint32 *num_entries,
|
|---|
| 157 | WINBIND_USERINFO **info)
|
|---|
| 158 | {
|
|---|
| 159 | ADS_STRUCT *ads = NULL;
|
|---|
| 160 | const char *attrs[] = { "*", NULL };
|
|---|
| 161 | int i, count;
|
|---|
| 162 | ADS_STATUS rc;
|
|---|
| 163 | LDAPMessage *res = NULL;
|
|---|
| 164 | LDAPMessage *msg = NULL;
|
|---|
| 165 | NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 166 |
|
|---|
| 167 | *num_entries = 0;
|
|---|
| 168 |
|
|---|
| 169 | DEBUG(3,("ads: query_user_list\n"));
|
|---|
| 170 |
|
|---|
| 171 | ads = ads_cached_connection(domain);
|
|---|
| 172 |
|
|---|
| 173 | if (!ads) {
|
|---|
| 174 | domain->last_status = NT_STATUS_SERVER_DISABLED;
|
|---|
| 175 | goto done;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | rc = ads_search_retry(ads, &res, "(objectCategory=user)", attrs);
|
|---|
| 179 | if (!ADS_ERR_OK(rc) || !res) {
|
|---|
| 180 | DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
|
|---|
| 181 | goto done;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | count = ads_count_replies(ads, res);
|
|---|
| 185 | if (count == 0) {
|
|---|
| 186 | DEBUG(1,("query_user_list: No users found\n"));
|
|---|
| 187 | goto done;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | (*info) = TALLOC_ZERO_ARRAY(mem_ctx, WINBIND_USERINFO, count);
|
|---|
| 191 | if (!*info) {
|
|---|
| 192 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 193 | goto done;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | i = 0;
|
|---|
| 197 |
|
|---|
| 198 | for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
|
|---|
| 199 | char *name, *gecos = NULL;
|
|---|
| 200 | char *homedir = NULL;
|
|---|
| 201 | char *shell = NULL;
|
|---|
| 202 | uint32 group;
|
|---|
| 203 | uint32 atype;
|
|---|
| 204 | DOM_SID user_sid;
|
|---|
| 205 | gid_t primary_gid = (gid_t)-1;
|
|---|
| 206 |
|
|---|
| 207 | if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
|
|---|
| 208 | ads_atype_map(atype) != SID_NAME_USER) {
|
|---|
| 209 | DEBUG(1,("Not a user account? atype=0x%x\n", atype));
|
|---|
| 210 | continue;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | name = ads_pull_username(ads, mem_ctx, msg);
|
|---|
| 214 |
|
|---|
| 215 | if ( ads_pull_sid( ads, msg, "objectSid", &user_sid ) ) {
|
|---|
| 216 | status = nss_get_info( domain->name, &user_sid, mem_ctx,
|
|---|
| 217 | ads, msg, &homedir, &shell, &gecos,
|
|---|
| 218 | &primary_gid );
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if (gecos == NULL) {
|
|---|
| 222 | gecos = ads_pull_string(ads, mem_ctx, msg, "name");
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (!ads_pull_sid(ads, msg, "objectSid",
|
|---|
| 226 | &(*info)[i].user_sid)) {
|
|---|
| 227 | DEBUG(1,("No sid for %s !?\n", name));
|
|---|
| 228 | continue;
|
|---|
| 229 | }
|
|---|
| 230 | if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
|
|---|
| 231 | DEBUG(1,("No primary group for %s !?\n", name));
|
|---|
| 232 | continue;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | (*info)[i].acct_name = name;
|
|---|
| 236 | (*info)[i].full_name = gecos;
|
|---|
| 237 | (*info)[i].homedir = homedir;
|
|---|
| 238 | (*info)[i].shell = shell;
|
|---|
| 239 | (*info)[i].primary_gid = primary_gid;
|
|---|
| 240 | sid_compose(&(*info)[i].group_sid, &domain->sid, group);
|
|---|
| 241 | i++;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | (*num_entries) = i;
|
|---|
| 245 | status = NT_STATUS_OK;
|
|---|
| 246 |
|
|---|
| 247 | DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
|
|---|
| 248 |
|
|---|
| 249 | done:
|
|---|
| 250 | if (res)
|
|---|
| 251 | ads_msgfree(ads, res);
|
|---|
| 252 |
|
|---|
| 253 | return status;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /* list all domain groups */
|
|---|
| 257 | static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
|
|---|
| 258 | TALLOC_CTX *mem_ctx,
|
|---|
| 259 | uint32 *num_entries,
|
|---|
| 260 | struct acct_info **info)
|
|---|
| 261 | {
|
|---|
| 262 | ADS_STRUCT *ads = NULL;
|
|---|
| 263 | const char *attrs[] = {"userPrincipalName", "sAMAccountName",
|
|---|
| 264 | "name", "objectSid", NULL};
|
|---|
| 265 | int i, count;
|
|---|
| 266 | ADS_STATUS rc;
|
|---|
| 267 | LDAPMessage *res = NULL;
|
|---|
| 268 | LDAPMessage *msg = NULL;
|
|---|
| 269 | NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 270 | const char *filter;
|
|---|
| 271 | BOOL enum_dom_local_groups = False;
|
|---|
| 272 |
|
|---|
| 273 | *num_entries = 0;
|
|---|
| 274 |
|
|---|
| 275 | DEBUG(3,("ads: enum_dom_groups\n"));
|
|---|
| 276 |
|
|---|
| 277 | /* only grab domain local groups for our domain */
|
|---|
| 278 | if ( domain->native_mode && strequal(lp_realm(), domain->alt_name) ) {
|
|---|
| 279 | enum_dom_local_groups = True;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | /* Workaround ADS LDAP bug present in MS W2K3 SP0 and W2K SP4 w/o
|
|---|
| 283 | * rollup-fixes:
|
|---|
| 284 | *
|
|---|
| 285 | * According to Section 5.1(4) of RFC 2251 if a value of a type is it's
|
|---|
| 286 | * default value, it MUST be absent. In case of extensible matching the
|
|---|
| 287 | * "dnattr" boolean defaults to FALSE and so it must be only be present
|
|---|
| 288 | * when set to TRUE.
|
|---|
| 289 | *
|
|---|
| 290 | * When it is set to FALSE and the OpenLDAP lib (correctly) encodes a
|
|---|
| 291 | * filter using bitwise matching rule then the buggy AD fails to decode
|
|---|
| 292 | * the extensible match. As a workaround set it to TRUE and thereby add
|
|---|
| 293 | * the dnAttributes "dn" field to cope with those older AD versions.
|
|---|
| 294 | * It should not harm and won't put any additional load on the AD since
|
|---|
| 295 | * none of the dn components have a bitmask-attribute.
|
|---|
| 296 | *
|
|---|
| 297 | * Thanks to Ralf Haferkamp for input and testing - Guenther */
|
|---|
| 298 |
|
|---|
| 299 | filter = talloc_asprintf(mem_ctx, "(&(objectCategory=group)(&(groupType:dn:%s:=%d)(!(groupType:dn:%s:=%d))))",
|
|---|
| 300 | ADS_LDAP_MATCHING_RULE_BIT_AND, GROUP_TYPE_SECURITY_ENABLED,
|
|---|
| 301 | ADS_LDAP_MATCHING_RULE_BIT_AND,
|
|---|
| 302 | enum_dom_local_groups ? GROUP_TYPE_BUILTIN_LOCAL_GROUP : GROUP_TYPE_RESOURCE_GROUP);
|
|---|
| 303 |
|
|---|
| 304 | if (filter == NULL) {
|
|---|
| 305 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 306 | goto done;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | ads = ads_cached_connection(domain);
|
|---|
| 310 |
|
|---|
| 311 | if (!ads) {
|
|---|
| 312 | domain->last_status = NT_STATUS_SERVER_DISABLED;
|
|---|
| 313 | goto done;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | rc = ads_search_retry(ads, &res, filter, attrs);
|
|---|
| 317 | if (!ADS_ERR_OK(rc) || !res) {
|
|---|
| 318 | DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc)));
|
|---|
| 319 | goto done;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | count = ads_count_replies(ads, res);
|
|---|
| 323 | if (count == 0) {
|
|---|
| 324 | DEBUG(1,("enum_dom_groups: No groups found\n"));
|
|---|
| 325 | goto done;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | (*info) = TALLOC_ZERO_ARRAY(mem_ctx, struct acct_info, count);
|
|---|
| 329 | if (!*info) {
|
|---|
| 330 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 331 | goto done;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | i = 0;
|
|---|
| 335 |
|
|---|
| 336 | for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
|
|---|
| 337 | char *name, *gecos;
|
|---|
| 338 | DOM_SID sid;
|
|---|
| 339 | uint32 rid;
|
|---|
| 340 |
|
|---|
| 341 | name = ads_pull_username(ads, mem_ctx, msg);
|
|---|
| 342 | gecos = ads_pull_string(ads, mem_ctx, msg, "name");
|
|---|
| 343 | if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
|
|---|
| 344 | DEBUG(1,("No sid for %s !?\n", name));
|
|---|
| 345 | continue;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | if (!sid_peek_check_rid(&domain->sid, &sid, &rid)) {
|
|---|
| 349 | DEBUG(1,("No rid for %s !?\n", name));
|
|---|
| 350 | continue;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | fstrcpy((*info)[i].acct_name, name);
|
|---|
| 354 | fstrcpy((*info)[i].acct_desc, gecos);
|
|---|
| 355 | (*info)[i].rid = rid;
|
|---|
| 356 | i++;
|
|---|
|
|---|