diff options
-rw-r--r-- | source3/libads/ldap.c | 24 | ||||
-rw-r--r-- | source3/utils/net_ads.c | 85 |
2 files changed, 80 insertions, 29 deletions
diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c index 812c44e7d7..604a11aa5b 100644 --- a/source3/libads/ldap.c +++ b/source3/libads/ldap.c @@ -178,6 +178,19 @@ int ads_connect(ADS_STRUCT *ads) /* + do a general ADS search +*/ +int ads_search(ADS_STRUCT *ads, void **res, + const char *exp, + const char **attrs) +{ + *res = NULL; + return ldap_search_s(ads->ld, ads->bind_path, + LDAP_SCOPE_SUBTREE, exp, (char **)attrs, 0, (LDAPMessage **)res); +} + + +/* find a machine account given a hostname */ int ads_find_machine_acct(ADS_STRUCT *ads, void **res, const char *host) @@ -188,9 +201,7 @@ int ads_find_machine_acct(ADS_STRUCT *ads, void **res, const char *host) /* the easiest way to find a machine account anywhere in the tree is to look for hostname$ */ asprintf(&exp, "(samAccountName=%s$)", host); - *res = NULL; - ret = ldap_search_s(ads->ld, ads->bind_path, - LDAP_SCOPE_SUBTREE, exp, NULL, 0, (LDAPMessage **)res); + ret = ads_search(ads, res, exp, NULL); free(exp); return ret; } @@ -320,7 +331,6 @@ void ads_dump(ADS_STRUCT *ads, void *res) char *field; LDAPMessage *msg; BerElement *b; - char *this_dn; struct { char *name; void (*handler)(const char *, struct berval **); @@ -332,12 +342,6 @@ void ads_dump(ADS_STRUCT *ads, void *res) for (msg = ldap_first_entry(ads->ld, (LDAPMessage *)res); msg; msg = ldap_next_entry(ads->ld, msg)) { - this_dn = ldap_get_dn(ads->ld, (LDAPMessage *)res); - if (this_dn) { - printf("Dumping: %s\n", this_dn); - } - ldap_memfree(this_dn); - for (field = ldap_first_attribute(ads->ld, msg, &b); field; field = ldap_next_attribute(ads->ld, msg, b)) { diff --git a/source3/utils/net_ads.c b/source3/utils/net_ads.c index d7b508bf89..87db4ada34 100644 --- a/source3/utils/net_ads.c +++ b/source3/utils/net_ads.c @@ -76,22 +76,79 @@ int net_ads_usage(void) return -1; } + -static int net_ads_status(int argc, const char **argv) +static ADS_STRUCT *ads_startup(void) { ADS_STRUCT *ads; int rc; - extern pstring global_myname; - void *res; - ads = ads_init(NULL, NULL, NULL); rc = ads_connect(ads); if (rc) { d_printf("ads_connect: %s\n", ads_errstr(rc)); + return NULL; + } + return ads; +} + + + +static int net_ads_user(int argc, const char **argv) +{ + ADS_STRUCT *ads; + int rc; + void *res; + const char *attrs[] = {"sAMAccountName", "name", "objectSid", NULL}; + + if (!(ads = ads_startup())) return -1; + rc = ads_search(ads, &res, "(objectclass=user)", attrs); + if (rc) { + d_printf("ads_search: %s\n", ads_errstr(rc)); + return -1; + } + + if (ads_count_replies(ads, res) == 0) { + d_printf("No users found\n"); + return -1; + } + + ads_dump(ads, res); + return 0; +} + +static int net_ads_group(int argc, const char **argv) +{ + ADS_STRUCT *ads; + int rc; + void *res; + const char *attrs[] = {"sAMAccountName", "name", "objectSid", NULL}; + + if (!(ads = ads_startup())) return -1; + rc = ads_search(ads, &res, "(objectclass=group)", attrs); + if (rc) { + d_printf("ads_search: %s\n", ads_errstr(rc)); + return -1; + } + + if (ads_count_replies(ads, res) == 0) { + d_printf("No groups found\n"); return -1; } + ads_dump(ads, res); + return 0; +} + +static int net_ads_status(int argc, const char **argv) +{ + ADS_STRUCT *ads; + int rc; + extern pstring global_myname; + void *res; + + if (!(ads = ads_startup())) return -1; + rc = ads_find_machine_acct(ads, &res, global_myname); if (rc) { d_printf("ads_find_machine_acct: %s\n", ads_errstr(rc)); @@ -114,19 +171,13 @@ static int net_ads_leave(int argc, const char **argv) int rc; extern pstring global_myname; + if (!(ads = ads_startup())) return -1; + if (!secrets_init()) { DEBUG(1,("Failed to initialise secrets database\n")); return -1; } - ads = ads_init(NULL, NULL, NULL); - - rc = ads_connect(ads); - if (rc) { - d_printf("ads_connect: %s\n", ads_errstr(rc)); - return -1; - } - rc = ads_leave_realm(ads, global_myname); if (rc) { d_printf("Failed to delete host '%s' from the '%s' realm.\n", @@ -154,13 +205,7 @@ static int net_ads_join(int argc, const char **argv) password = generate_random_password(15); - ads = ads_init(NULL, NULL, NULL); - - rc = ads_connect(ads); - if (rc) { - d_printf("ads_connect: %s\n", ads_errstr(rc)); - return -1; - } + if (!(ads = ads_startup())) return -1; rc = ads_join_realm(ads, global_myname); if (rc) { @@ -190,6 +235,8 @@ int net_ads(int argc, const char **argv) {"JOIN", net_ads_join}, {"LEAVE", net_ads_leave}, {"STATUS", net_ads_status}, + {"USER", net_ads_user}, + {"GROUP", net_ads_group}, {NULL, NULL} }; |