/*
- Unix SMB/Netbios implementation.
+ Unix SMB/CIFS implementation.
Winbind ADS backend functions
#ifdef HAVE_ADS
+/* the realm of our primary LDAP server */
+static char *primary_realm;
+
/*
a wrapper around ldap_search_s that retries depending on the error code
this is supposed to catch dropped connections and auto-reconnect
*/
-int ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path, int scope,
- const char *exp,
- const char **attrs, void **res)
+ADS_STATUS ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path, int scope,
+ const char *exp,
+ const char **attrs, void **res)
{
- int rc = -1, rc2;
+ ADS_STATUS status;
int count = 3;
+ char *bp;
if (!ads->ld &&
time(NULL) - ads->last_attempt < ADS_RECONNECT_TIME) {
- return LDAP_SERVER_DOWN;
+ return ADS_ERROR(LDAP_SERVER_DOWN);
}
+ bp = strdup(bind_path);
+
while (count--) {
- rc = ads_do_search(ads, bind_path, scope, exp, attrs, res);
- if (rc == 0) {
+ status = ads_do_search_all(ads, bp, scope, exp, attrs, res);
+ if (ADS_ERR_OK(status)) {
DEBUG(5,("Search for %s gave %d replies\n",
exp, ads_count_replies(ads, *res)));
- return rc;
+ free(bp);
+ return status;
}
if (*res) ads_msgfree(ads, *res);
*res = NULL;
- DEBUG(1,("Reopening ads connection after error %s\n", ads_errstr(rc)));
+ DEBUG(1,("Reopening ads connection to %s after error %s\n",
+ ads->ldap_server, ads_errstr(status)));
if (ads->ld) {
- /* we should unbind here, but that seems to trigger openldap bugs :(
- ldap_unbind(ads->ld);
- */
+ ldap_unbind(ads->ld);
}
ads->ld = NULL;
- rc2 = ads_connect(ads);
- if (rc2) {
- DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n", ads_errstr(rc)));
- return rc2;
+ status = ads_connect(ads);
+ if (!ADS_ERR_OK(status)) {
+ DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
+ ads_errstr(status)));
+ ads_destroy(&ads);
+ free(bp);
+ return status;
}
}
- DEBUG(1,("ads reopen failed after error %s\n", ads_errstr(rc)));
- return rc;
+ free(bp);
+
+ DEBUG(1,("ads reopen failed after error %s\n", ads_errstr(status)));
+ return status;
}
-int ads_search_retry(ADS_STRUCT *ads, void **res,
- const char *exp,
- const char **attrs)
+ADS_STATUS ads_search_retry(ADS_STRUCT *ads, void **res,
+ const char *exp,
+ const char **attrs)
{
return ads_do_search_retry(ads, ads->bind_path, LDAP_SCOPE_SUBTREE,
exp, attrs, res);
}
-int ads_search_retry_dn(ADS_STRUCT *ads, void **res,
- const char *dn,
- const char **attrs)
+ADS_STATUS ads_search_retry_dn(ADS_STRUCT *ads, void **res,
+ const char *dn,
+ const char **attrs)
{
return ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
"(objectclass=*)", attrs, res);
static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
{
ADS_STRUCT *ads;
- int rc;
+ ADS_STATUS status;
char *ccache;
+ struct in_addr server_ip;
+ char *sname;
if (domain->private) {
return (ADS_STRUCT *)domain->private;
/* we don't want this to affect the users ccache */
ccache = lock_path("winbindd_ccache");
- setenv("KRB5CCNAME", ccache, 1);
+ SETENV("KRB5CCNAME", ccache, 1);
unlink(ccache);
- ads = ads_init(NULL, NULL, NULL, NULL);
+ if (resolve_name(domain->name, &server_ip, 0x1b)) {
+ sname = inet_ntoa(server_ip);
+ } else {
+ if (strcasecmp(domain->name, lp_workgroup()) != 0) {
+ DEBUG(1,("can't find domain controller for %s\n", domain->name));
+ return NULL;
+ }
+ sname = NULL;
+ }
+
+ ads = ads_init(primary_realm, sname, NULL, NULL);
if (!ads) {
DEBUG(1,("ads_init for domain %s failed\n", domain->name));
return NULL;
SAFE_FREE(ads->password);
ads->password = secrets_fetch_machine_password();
- rc = ads_connect(ads);
- if (rc) {
- DEBUG(1,("ads_connect for domain %s failed: %s\n", domain->name, ads_errstr(rc)));
+ status = ads_connect(ads);
+ if (!ADS_ERR_OK(status)) {
+ extern struct winbindd_methods msrpc_methods;
+ DEBUG(1,("ads_connect for domain %s failed: %s\n",
+ domain->name, ads_errstr(status)));
ads_destroy(&ads);
+
+ /* if we get ECONNREFUSED then it might be a NT4
+ server, fall back to MSRPC */
+ if (status.error_type == ADS_ERROR_SYSTEM &&
+ status.rc == ECONNREFUSED) {
+ DEBUG(1,("Trying MSRPC methods\n"));
+ domain->methods = &msrpc_methods;
+ }
return NULL;
}
+ /* remember our primary realm for trusted domain support */
+ if (!primary_realm) {
+ primary_realm = strdup(ads->realm);
+ }
+
+ fstrcpy(domain->full_name, ads->server_realm);
+
domain->private = (void *)ads;
return ads;
}
/* Query display info for a realm. This is the basic user list fn */
static NTSTATUS query_user_list(struct winbindd_domain *domain,
TALLOC_CTX *mem_ctx,
- uint32 *start_ndx, uint32 *num_entries,
+ uint32 *num_entries,
WINBIND_USERINFO **info)
{
ADS_STRUCT *ads = NULL;
const char *attrs[] = {"sAMAccountName", "name", "objectSid", "primaryGroupID",
"sAMAccountType", NULL};
- int rc, i, count;
+ int i, count;
+ ADS_STATUS rc;
void *res = NULL;
void *msg = NULL;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
DEBUG(3,("ads: query_user_list\n"));
- if ((*start_ndx) != 0) {
- DEBUG(1,("ads backend start_ndx not implemented!\n"));
- status = NT_STATUS_NOT_IMPLEMENTED;
- goto done;
- }
-
ads = ads_cached_connection(domain);
if (!ads) goto done;
rc = ads_search_retry(ads, &res, "(objectCategory=user)", attrs);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
goto done;
}
goto done;
}
- (*info) = talloc(mem_ctx, count * sizeof(**info));
+ (*info) = talloc_zero(mem_ctx, count * sizeof(**info));
if (!*info) {
status = NT_STATUS_NO_MEMORY;
goto done;
/* list all domain groups */
static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
TALLOC_CTX *mem_ctx,
- uint32 *start_ndx, uint32 *num_entries,
+ uint32 *num_entries,
struct acct_info **info)
{
ADS_STRUCT *ads = NULL;
const char *attrs[] = {"sAMAccountName", "name", "objectSid",
"sAMAccountType", NULL};
- int rc, i, count;
+ int i, count;
+ ADS_STATUS rc;
void *res = NULL;
void *msg = NULL;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
DEBUG(3,("ads: enum_dom_groups\n"));
- if ((*start_ndx) != 0) {
- DEBUG(1,("ads backend start_ndx not implemented\n"));
- status = NT_STATUS_NOT_IMPLEMENTED;
- goto done;
- }
-
ads = ads_cached_connection(domain);
if (!ads) goto done;
rc = ads_search_retry(ads, &res, "(objectCategory=group)", attrs);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
goto done;
}
goto done;
}
- (*info) = talloc(mem_ctx, count * sizeof(**info));
+ (*info) = talloc_zero(mem_ctx, count * sizeof(**info));
if (!*info) {
status = NT_STATUS_NO_MEMORY;
goto done;
{
ADS_STRUCT *ads = NULL;
const char *attrs[] = {"objectSid", "sAMAccountType", NULL};
- int rc, count;
+ int count;
+ ADS_STATUS rc;
void *res = NULL;
char *exp;
uint32 t;
- fstring name2, dom2;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
- /* sigh. Need to fix interface to give us a raw name */
- if (!parse_domain_user(name, dom2, name2)) {
- goto done;
- }
-
DEBUG(3,("ads: name_to_sid\n"));
ads = ads_cached_connection(domain);
if (!ads) goto done;
- asprintf(&exp, "(sAMAccountName=%s)", name2);
+ asprintf(&exp, "(sAMAccountName=%s)", name);
rc = ads_search_retry(ads, &res, exp, attrs);
free(exp);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("name_to_sid ads_search: %s\n", ads_errstr(rc)));
goto done;
}
{
ADS_STRUCT *ads = NULL;
const char *attrs[] = {"sAMAccountName", "sAMAccountType", NULL};
- int rc;
+ ADS_STATUS rc;
void *msg = NULL;
char *exp;
char *sidstr;
uint32 atype;
- char *s;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
DEBUG(3,("ads: sid_to_name\n"));
rc = ads_search_retry(ads, &msg, exp, attrs);
free(exp);
free(sidstr);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("sid_to_name ads_search: %s\n", ads_errstr(rc)));
goto done;
}
goto done;
}
- s = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
- *name = talloc_asprintf(mem_ctx, "%s%s%s", domain->name, lp_winbind_separator(), s);
+ *name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
*type = ads_atype_map(atype);
status = NT_STATUS_OK;
}
+/* convert a sid to a distnguished name */
+static NTSTATUS sid_to_distinguished_name(struct winbindd_domain *domain,
+ TALLOC_CTX *mem_ctx,
+ DOM_SID *sid,
+ char **dn)
+{
+ ADS_STRUCT *ads = NULL;
+ const char *attrs[] = {"distinguishedName", NULL};
+ ADS_STATUS rc;
+ void *msg = NULL;
+ char *exp;
+ char *sidstr;
+ NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+
+ DEBUG(3,("ads: sid_to_distinguished_name\n"));
+
+ ads = ads_cached_connection(domain);
+ if (!ads) goto done;
+
+ sidstr = sid_binstring(sid);
+ asprintf(&exp, "(objectSid=%s)", sidstr);
+ rc = ads_search_retry(ads, &msg, exp, attrs);
+ free(exp);
+ free(sidstr);
+ if (!ADS_ERR_OK(rc)) {
+ DEBUG(1,("sid_to_distinguished_name ads_search: %s\n", ads_errstr(rc)));
+ goto done;
+ }
+
+ *dn = ads_pull_string(ads, mem_ctx, msg, "distinguishedName");
+
+ status = NT_STATUS_OK;
+
+ DEBUG(3,("ads sid_to_distinguished_name mapped %s\n", *dn));
+
+done:
+ if (msg) ads_msgfree(ads, msg);
+
+ return status;
+}
+
+
/* Lookup user information from a rid */
static NTSTATUS query_user(struct winbindd_domain *domain,
TALLOC_CTX *mem_ctx,
ADS_STRUCT *ads = NULL;
const char *attrs[] = {"sAMAccountName", "name", "objectSid",
"primaryGroupID", NULL};
- int rc, count;
+ ADS_STATUS rc;
+ int count;
void *msg = NULL;
char *exp;
DOM_SID sid;
rc = ads_search_retry(ads, &msg, exp, attrs);
free(exp);
free(sidstr);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("query_user(rid=%d) ads_search: %s\n", user_rid, ads_errstr(rc)));
goto done;
}
ADS_STRUCT *ads = NULL;
const char *attrs[] = {"distinguishedName", NULL};
const char *attrs2[] = {"tokenGroups", "primaryGroupID", NULL};
- int rc, count;
+ ADS_STATUS rc;
+ int count;
void *msg = NULL;
char *exp;
char *user_dn;
rc = ads_search_retry(ads, &msg, exp, attrs);
free(exp);
free(sidstr);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("lookup_usergroups(rid=%d) ads_search: %s\n", user_rid, ads_errstr(rc)));
goto done;
}
if (msg) ads_msgfree(ads, msg);
rc = ads_search_retry_dn(ads, &msg, user_dn, attrs2);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("lookup_usergroups(rid=%d) ads_search tokenGroups: %s\n", user_rid, ads_errstr(rc)));
goto done;
}
if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group)) {
- DEBUG(1,("No primary group for rid=%d !?\n", user_rid));
+ DEBUG(1,("%s: No primary group for rid=%d !?\n", domain->name, user_rid));
goto done;
}
count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids) + 1;
- (*user_gids) = (uint32 *)talloc(mem_ctx, sizeof(uint32) * count);
+ (*user_gids) = (uint32 *)talloc_zero(mem_ctx, sizeof(uint32) * count);
(*user_gids)[(*num_groups)++] = primary_group;
for (i=1;i<count;i++) {
uint32 **name_types)
{
DOM_SID group_sid;
- char *sidstr;
const char *attrs[] = {"sAMAccountName", "objectSid", "sAMAccountType", NULL};
- int rc, count;
+ ADS_STATUS rc;
+ int count;
void *res=NULL, *msg=NULL;
ADS_STRUCT *ads = NULL;
- char *exp;
+ char *exp, *dn = NULL;
NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
*num_names = 0;
if (!ads) goto done;
sid_from_rid(domain, group_rid, &group_sid);
- sidstr = sid_binstring(&group_sid);
+ status = sid_to_distinguished_name(domain, mem_ctx, &group_sid, &dn);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(3,("Failed to find distinguishedName for %s\n", sid_string_static(&group_sid)));
+ return status;
+ }
+
/* search for all users who have that group sid as primary group or as member */
asprintf(&exp, "(&(objectCategory=user)(|(primaryGroupID=%d)(memberOf=%s)))",
- group_rid, sidstr);
+ group_rid, dn);
rc = ads_search_retry(ads, &res, exp, attrs);
free(exp);
- free(sidstr);
- if (rc) {
+ if (!ADS_ERR_OK(rc)) {
DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
goto done;
}
goto done;
}
- (*rid_mem) = talloc(mem_ctx, sizeof(uint32) * count);
- (*name_types) = talloc(mem_ctx, sizeof(uint32) * count);
- (*names) = talloc(mem_ctx, sizeof(char *) * count);
+ (*rid_mem) = talloc_zero(mem_ctx, sizeof(uint32) * count);
+ (*name_types) = talloc_zero(mem_ctx, sizeof(uint32) * count);
+ (*names) = talloc_zero(mem_ctx, sizeof(char *) * count);
for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
uint32 atype, rid;
static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
{
ADS_STRUCT *ads = NULL;
+ ADS_STATUS rc;
*seq = DOM_SEQUENCE_NONE;
ads = ads_cached_connection(domain);
if (!ads) return NT_STATUS_UNSUCCESSFUL;
- if (!ads_USN(ads, seq)) {
- return NT_STATUS_UNSUCCESSFUL;
+ rc = ads_USN(ads, seq);
+ if (!ADS_ERR_OK(rc)) {
+ /* its a dead connection */
+ ads_destroy(&ads);
+ domain->private = NULL;
}
-
- return NT_STATUS_OK;
+ return ads_ntstatus(rc);
}
/* get a list of trusted domains */
char ***names,
DOM_SID **dom_sids)
{
+ ADS_STRUCT *ads;
+ ADS_STATUS rc;
+
*num_domains = 0;
- return NT_STATUS_NOT_IMPLEMENTED;
+ *names = NULL;
+
+ ads = ads_cached_connection(domain);
+ if (!ads) return NT_STATUS_UNSUCCESSFUL;
+
+ rc = ads_trusted_domains(ads, mem_ctx, num_domains, names, dom_sids);
+
+ return ads_ntstatus(rc);
}
/* find the domain sid for a domain */
static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
{
- NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
- const char *attrs[] = {"objectSid", NULL};
- ADS_STRUCT *ads = NULL;
- void *res;
- int rc;
+ ADS_STRUCT *ads;
+ ADS_STATUS rc;
ads = ads_cached_connection(domain);
- if (!ads) goto done;
+ if (!ads) return NT_STATUS_UNSUCCESSFUL;
- rc = ads_do_search(ads, ads->bind_path, LDAP_SCOPE_BASE, "(objectclass=*)",
- attrs, &res);
- if (rc) goto done;
- if (ads_pull_sid(ads, res, "objectSid", sid)) {
- status = NT_STATUS_OK;
+ rc = ads_domain_sid(ads, sid);
+
+ if (!ADS_ERR_OK(rc)) {
+ /* its a dead connection */
+ ads_destroy(&ads);
+ domain->private = NULL;
}
- ads_msgfree(ads, res);
-done:
- return status;
+ return ads_ntstatus(rc);
}
/* the ADS backend methods are exposed via this structure */