1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

added "net ads user" and "net ads group" commands

This commit is contained in:
Andrew Tridgell 0001-01-01 00:00:00 +00:00
parent ae0eabd04c
commit f482583139
2 changed files with 83 additions and 32 deletions

View File

@ -177,6 +177,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
*/
@ -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)) {

View File

@ -76,6 +76,69 @@ int net_ads_usage(void)
return -1;
}
static ADS_STRUCT *ads_startup(void)
{
ADS_STRUCT *ads;
int rc;
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)
{
@ -84,13 +147,7 @@ static int net_ads_status(int argc, const char **argv)
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 -1;
}
if (!(ads = ads_startup())) return -1;
rc = ads_find_machine_acct(ads, &res, global_myname);
if (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}
};