printing: add nt_printer_guid_retrieve() helper
authorDavid Disseldorp <ddiss@samba.org>
Thu, 18 Dec 2014 17:23:11 +0000 (18:23 +0100)
committerGünther Deschner <gd@samba.org>
Wed, 18 Feb 2015 09:14:09 +0000 (10:14 +0100)
This function connects to the domain controller and retrieves the
GUID for the corresponding printer DN.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=11018

Pair-programmed-with: Andreas Schneider <asn@samba.org>
Signed-off-by: David Disseldorp <ddiss@samba.org>
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Guenther Deschner <gd@samba.org>
source3/include/nt_printing.h
source3/printing/nt_printing_ads.c

index 4af44d75d9115245deb0e083f9c51a7c7cd8a584..3d74b655c4a8cfeeb018b147169628ef7e4b1dca 100644 (file)
@@ -132,6 +132,9 @@ WERROR print_access_check(const struct auth_session_info *server_info,
                          struct messaging_context *msg_ctx, int snum,
                          int access_type);
 
+WERROR nt_printer_guid_retrieve(TALLOC_CTX *mem_ctx, const char *printer,
+                               struct GUID *pguid);
+
 WERROR nt_printer_guid_get(TALLOC_CTX *mem_ctx,
                           const struct auth_session_info *session_info,
                           struct messaging_context *msg_ctx,
index c136a70c034d38c98fecc4d10d4ff58504d05ce5..6f14e2d400ee671608c1636a5e2c62a76a39283c 100644 (file)
@@ -209,6 +209,58 @@ static WERROR nt_printer_guid_retrieve_internal(ADS_STRUCT *ads,
        return WERR_OK;
 }
 
+WERROR nt_printer_guid_retrieve(TALLOC_CTX *mem_ctx, const char *printer,
+                               struct GUID *pguid)
+{
+       ADS_STRUCT *ads = NULL;
+       char *old_krb5ccname = NULL;
+       char *printer_dn;
+       WERROR result;
+       ADS_STATUS ads_status;
+       TALLOC_CTX *tmp_ctx;
+
+       tmp_ctx = talloc_new(mem_ctx);
+       if (tmp_ctx == NULL) {
+               return WERR_NOMEM;
+       }
+
+       ads = ads_init(lp_realm(), lp_workgroup(), NULL);
+       if (ads == NULL) {
+               result = WERR_SERVER_UNAVAILABLE;
+               goto out;
+       }
+
+       old_krb5ccname = getenv(KRB5_ENV_CCNAME);
+       setenv(KRB5_ENV_CCNAME, "MEMORY:prtpub_cache", 1);
+       SAFE_FREE(ads->auth.password);
+       ads->auth.password = secrets_fetch_machine_password(lp_workgroup(),
+                                                           NULL, NULL);
+
+       ads_status = ads_connect(ads);
+       if (!ADS_ERR_OK(ads_status)) {
+               DEBUG(3, ("ads_connect failed: %s\n", ads_errstr(ads_status)));
+               result = WERR_ACCESS_DENIED;
+               goto out;
+       }
+
+       result = nt_printer_dn_lookup(tmp_ctx, ads, printer, &printer_dn);
+       if (!W_ERROR_IS_OK(result)) {
+               goto out;
+       }
+
+       result = nt_printer_guid_retrieve_internal(ads, printer_dn, pguid);
+out:
+       TALLOC_FREE(tmp_ctx);
+       ads_destroy(&ads);
+       ads_kdestroy("MEMORY:prtpub_cache");
+       unsetenv(KRB5_ENV_CCNAME);
+       if (old_krb5ccname != NULL) {
+               setenv(KRB5_ENV_CCNAME, old_krb5ccname, 0);
+       }
+
+       return result;
+}
+
 WERROR nt_printer_guid_get(TALLOC_CTX *mem_ctx,
                           const struct auth_session_info *session_info,
                           struct messaging_context *msg_ctx,
@@ -665,6 +717,12 @@ bool is_printer_published(TALLOC_CTX *mem_ctx,
        return true;
 }
 #else
+WERROR nt_printer_guid_retrieve(TALLOC_CTX *mem_ctx, const char *printer,
+                               struct GUID *pguid)
+{
+       return WERR_NOT_SUPPORTED;
+}
+
 WERROR nt_printer_guid_get(TALLOC_CTX *mem_ctx,
                           const struct auth_session_info *session_info,
                           struct messaging_context *msg_ctx,