ADS_STATUS ads_find_printers(ADS_STRUCT *ads, LDAPMessage **res) { const char *ldap_expr; const char *attrs[] = { "objectClass", "printerName", "location", "driverName", "serverName", "description", NULL }; /* For the moment only display all printers */ ldap_expr = "(&(!(showInAdvancedViewOnly=TRUE))(uncName=*)" "(objectCategory=printQueue))"; return ads_search(ads, res, ldap_expr, attrs); }
static int ads_user_info(int argc, const char **argv) { ADS_STRUCT *ads; ADS_STATUS rc; void *res; const char *attrs[] = {"memberOf", NULL}; char *searchstring=NULL; char **grouplist; char *escaped_user = escape_ldap_string_alloc(argv[0]); if (argc < 1) { return net_ads_user_usage(argc, argv); } if (!(ads = ads_startup())) { return -1; } if (!escaped_user) { d_printf("ads_user_info: failed to escape user %s\n", argv[0]); ads_destroy(&ads); return -1; } asprintf(&searchstring, "(sAMAccountName=%s)", escaped_user); rc = ads_search(ads, &res, searchstring, attrs); safe_free(searchstring); if (!ADS_ERR_OK(rc)) { d_printf("ads_search: %s\n", ads_errstr(rc)); ads_destroy(&ads); return -1; } grouplist = ldap_get_values(ads->ld, res, "memberOf"); if (grouplist) { int i; char **groupname; for (i=0;grouplist[i];i++) { groupname = ldap_explode_dn(grouplist[i], 1); d_printf("%s\n", groupname[0]); ldap_value_free(groupname); } ldap_value_free(grouplist); } ads_msgfree(ads, res); ads_destroy(&ads); return 0; }
/* find a printer given the name and the hostname Note that results "res" may be allocated on return so that the results can be used. It should be freed using ads_msgfree. */ ADS_STATUS ads_find_printer_on_server(ADS_STRUCT *ads, LDAPMessage **res, const char *printer, const char *servername) { ADS_STATUS status; char *srv_dn, **srv_cn, *s = NULL; const char *attrs[] = {"*", "nTSecurityDescriptor", NULL}; status = ads_find_machine_acct(ads, res, servername); if (!ADS_ERR_OK(status)) { DEBUG(1, ("ads_find_printer_on_server: cannot find host %s in ads\n", servername)); return status; } if (ads_count_replies(ads, *res) != 1) { ads_msgfree(ads, *res); *res = NULL; return ADS_ERROR(LDAP_NO_SUCH_OBJECT); } srv_dn = ldap_get_dn(ads->ldap.ld, *res); if (srv_dn == NULL) { ads_msgfree(ads, *res); *res = NULL; return ADS_ERROR(LDAP_NO_MEMORY); } srv_cn = ldap_explode_dn(srv_dn, 1); if (srv_cn == NULL) { ldap_memfree(srv_dn); ads_msgfree(ads, *res); *res = NULL; return ADS_ERROR(LDAP_INVALID_DN_SYNTAX); } ads_msgfree(ads, *res); *res = NULL; if (asprintf(&s, "(cn=%s-%s)", srv_cn[0], printer) == -1) { ldap_memfree(srv_dn); return ADS_ERROR(LDAP_NO_MEMORY); } status = ads_search(ads, res, s, attrs); ldap_memfree(srv_dn); ldap_value_free(srv_cn); SAFE_FREE(s); return status; }
/* find a user account */ ADS_STATUS ads_find_user_acct(ADS_STRUCT *ads, LDAPMessage **res, const char *user) { ADS_STATUS status; char *ldap_exp; const char *attrs[] = {"*", NULL}; char *escaped_user = escape_ldap_string_alloc(user); if (!escaped_user) { return ADS_ERROR(LDAP_NO_MEMORY); } if (asprintf(&ldap_exp, "(samAccountName=%s)", escaped_user) == -1) { SAFE_FREE(escaped_user); return ADS_ERROR(LDAP_NO_MEMORY); } status = ads_search(ads, res, ldap_exp, attrs); SAFE_FREE(ldap_exp); SAFE_FREE(escaped_user); return status; }