diff --git a/gtk/gtkprintbackend.c b/gtk/gtkprintbackend.c index 66f5b41a9..86921174b 100644 --- a/gtk/gtkprintbackend.c +++ b/gtk/gtkprintbackend.c @@ -48,8 +48,18 @@ struct _GtkPrintBackendPrivate GtkPrintBackendStatus status; char **auth_info_required; char **auth_info; + GtkWidget *remember; /* "Remember password", if offered */ }; +/* A backend that can keep passwords in the user's keyring puts these into + * the first two reserved slots of its class: whether it can do so for the + * password asked for now, and set_password() with the user's choice. */ +typedef gboolean (*CanStorePasswordFunc) (GtkPrintBackend *backend); +typedef void (*SetPasswordFullFunc) (GtkPrintBackend *backend, + gchar **auth_info_required, + gchar **auth_info, + gboolean store_auth_info); + enum { PRINTER_LIST_CHANGED, PRINTER_LIST_DONE, @@ -698,10 +708,15 @@ password_dialog_response (GtkWidget *dialog, GtkPrintBackendPrivate *priv = backend->priv; gint i; - if (response_id == GTK_RESPONSE_OK) + if (response_id == GTK_RESPONSE_OK && priv->remember != NULL && + gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->remember))) + ((SetPasswordFullFunc) GTK_PRINT_BACKEND_GET_CLASS (backend)->_gtk_reserved2) + (backend, priv->auth_info_required, priv->auth_info, TRUE); + else if (response_id == GTK_RESPONSE_OK) gtk_print_backend_set_password (backend, priv->auth_info_required, priv->auth_info); else gtk_print_backend_set_password (backend, priv->auth_info_required, NULL); + priv->remember = NULL; for (i = 0; i < g_strv_length (priv->auth_info_required); i++) if (priv->auth_info[i] != NULL) @@ -810,6 +825,15 @@ request_password (GtkPrintBackend *backend, } } + priv->remember = NULL; + if (GTK_PRINT_BACKEND_GET_CLASS (backend)->_gtk_reserved1 != NULL && + GTK_PRINT_BACKEND_GET_CLASS (backend)->_gtk_reserved2 != NULL && + ((CanStorePasswordFunc) GTK_PRINT_BACKEND_GET_CLASS (backend)->_gtk_reserved1) (backend)) + { + priv->remember = gtk_check_button_new_with_mnemonic (_("_Remember password")); + gtk_box_pack_start (GTK_BOX (vbox), priv->remember, FALSE, FALSE, 6); + } + if (focus != NULL) { gtk_widget_grab_focus (focus); diff --git a/modules/printbackends/cups/gtkprintbackendcups.c b/modules/printbackends/cups/gtkprintbackendcups.c index 45aca43d7..838a73056 100644 --- a/modules/printbackends/cups/gtkprintbackendcups.c +++ b/modules/printbackends/cups/gtkprintbackendcups.c @@ -144,6 +144,8 @@ struct _GtkPrintBackendCups GHashTable *auth; gchar *username; gboolean authentication_lock; + guint secrets_available : 1; /* a keyring answered */ + guint secrets_unavailable : 1; /* there is none */ #ifdef HAVE_CUPS_API_1_6 GDBusConnection *dbus_connection; gchar *avahi_default_printer; @@ -213,6 +215,14 @@ static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter static void gtk_print_backend_cups_set_password (GtkPrintBackend *backend, gchar **auth_info_required, gchar **auth_info); +static void gtk_print_backend_cups_set_password_full (GtkPrintBackend *backend, + gchar **auth_info_required, + gchar **auth_info, + gboolean store_auth_info); +static gboolean gtk_print_backend_cups_can_store_password (GtkPrintBackend *backend); +static void cups_secret_store (gchar **auth_info_required, + gchar **auth_info, + const gchar *printer_uri); void overwrite_and_free (gpointer data); static gboolean is_address_local (const gchar *address); @@ -341,6 +351,9 @@ gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class) backend_class->printer_get_hard_margins = cups_printer_get_hard_margins; backend_class->printer_get_capabilities = cups_printer_get_capabilities; backend_class->set_password = gtk_print_backend_cups_set_password; + /* see gtk/gtkprintbackend.c */ + backend_class->_gtk_reserved1 = (void (*) (void)) gtk_print_backend_cups_can_store_password; + backend_class->_gtk_reserved2 = (void (*) (void)) gtk_print_backend_cups_set_password_full; } static cairo_status_t @@ -973,9 +986,10 @@ httpGetHostname(http_t *http, #endif static void -gtk_print_backend_cups_set_password (GtkPrintBackend *backend, - gchar **auth_info_required, - gchar **auth_info) +gtk_print_backend_cups_set_password_full (GtkPrintBackend *backend, + gchar **auth_info_required, + gchar **auth_info, + gboolean store_auth_info) { GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend); GList *l; @@ -1026,6 +1040,11 @@ gtk_print_backend_cups_set_password (GtkPrintBackend *backend, dispatch->request->auth_info = g_new0 (gchar *, length + 1); for (i = 0; i < length; i++) dispatch->request->auth_info[i] = g_strdup (auth_info[i]); + + if (store_auth_info && password != NULL) + cups_secret_store (auth_info_required, auth_info, + gtk_cups_request_ipp_get_string (dispatch->request, + IPP_TAG_URI, "printer-uri")); } dispatch->backend->authentication_lock = FALSE; dispatch->request->need_auth_info = FALSE; @@ -1042,6 +1061,31 @@ gtk_print_backend_cups_set_password (GtkPrintBackend *backend, } } +static void +gtk_print_backend_cups_set_password (GtkPrintBackend *backend, + gchar **auth_info_required, + gchar **auth_info) +{ + gtk_print_backend_cups_set_password_full (backend, auth_info_required, auth_info, FALSE); +} + +/* Offers to remember the password asked for a job (not for the server). */ +static gboolean +gtk_print_backend_cups_can_store_password (GtkPrintBackend *backend) +{ + GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend); + GList *l; + + if (!cups_backend->secrets_available) + return FALSE; + + for (l = cups_backend->requests; l; l = l->next) + if (((GtkPrintCupsDispatchWatch *) l->data)->request->need_auth_info) + return TRUE; + + return FALSE; +} + static gboolean request_password (gpointer data) { @@ -1357,6 +1401,399 @@ request_auth_info (gpointer user_data) return FALSE; } +/* Passwords that printers need for each job (auth-info-required, as for + * printers shared by Windows) can be kept in the user's keyring through the + * freedesktop.org Secret Service API. The entries are the same as GTK+ 3 + * makes, so both find each other's: label and "uri" attribute are the + * printer URI, "user", "server" and "domain" attributes hold the other + * fields and the secret is the password as text/plain. + * The session bus is used only when a printer asks for a password. + */ + +#define SECRETS_BUS "org.freedesktop.secrets" +#define SECRETS_PATH "/org/freedesktop/secrets" +#define SECRETS_COLLECTION "/org/freedesktop/secrets/aliases/default" +#define SECRETS_IFACE(x) "org.freedesktop.Secret." x + +typedef enum +{ + SECRET_SEARCH, + SECRET_UNLOCK, + SECRET_PROMPT, + SECRET_ATTRIBUTES, + SECRET_SESSION, + SECRET_GET, + SECRET_CREATE +} SecretStep; + +typedef struct +{ + GtkPrintCupsDispatchWatch *dispatch; /* looking up for it; NULL when storing */ + GDBusConnection *bus; + SecretStep step; + gchar *uri; + gchar **required; /* names of the auth_info fields */ + gchar **auth_info; + gchar *item; + gchar *session; + guint prompt_signal; + guint prompt_returned : 1; /* Prompt() and Completed can come in any order */ + guint prompt_completed : 1; + guint dismissed : 1; +} CupsSecret; + +static void cups_secret_reply (GObject *source, GAsyncResult *res, gpointer data); + +static void +cups_secret_call (CupsSecret *secret, + const gchar *path, + const gchar *interface, + const gchar *method, + GVariant *parameters, + const GVariantType *reply_type, + SecretStep step) +{ + secret->step = step; + g_dbus_connection_call (secret->bus, SECRETS_BUS, path, interface, method, + parameters, reply_type, G_DBUS_CALL_FLAGS_NONE, + -1, NULL, cups_secret_reply, secret); +} + +static void +cups_secret_open_session (CupsSecret *secret) +{ + cups_secret_call (secret, SECRETS_PATH, SECRETS_IFACE ("Service"), "OpenSession", + g_variant_new ("(sv)", "plain", g_variant_new_string ("")), + G_VARIANT_TYPE ("(vo)"), SECRET_SESSION); +} + +static void +cups_secret_finish (CupsSecret *secret, + gboolean found, + GError *error) +{ + GtkPrintCupsDispatchWatch *dispatch = secret->dispatch; + guint i; + + if (error != NULL) + GTK_NOTE (PRINTING, g_print ("CUPS backend: keyring: %s\n", error->message)); + + if (dispatch != NULL) + { + if (error == NULL) + dispatch->backend->secrets_available = TRUE; + else if (secret->bus == NULL || + g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN) || + g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER)) + dispatch->backend->secrets_unavailable = TRUE; + + if (found) + gtk_print_backend_cups_set_password (GTK_PRINT_BACKEND (dispatch->backend), + secret->required, secret->auth_info); + else + dispatch->backend->authentication_lock = FALSE; + } + + g_clear_error (&error); + + if (secret->prompt_signal != 0) + g_dbus_connection_signal_unsubscribe (secret->bus, secret->prompt_signal); + if (secret->session != NULL) + g_dbus_connection_call (secret->bus, SECRETS_BUS, secret->session, + SECRETS_IFACE ("Session"), "Close", NULL, NULL, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); + if (secret->auth_info != NULL) + for (i = 0; secret->required[i] != NULL; i++) + overwrite_and_free (secret->auth_info[i]); + g_free (secret->auth_info); + g_strfreev (secret->required); + g_free (secret->uri); + g_free (secret->item); + g_free (secret->session); + if (secret->bus != NULL) + g_object_unref (secret->bus); + g_free (secret); + + if (dispatch != NULL) + { + if (found) + check_auth_info (dispatch); + else + request_auth_info (dispatch); + } +} + +/* Goes on once the keyring is unlocked. */ +static void +cups_secret_unlocked (CupsSecret *secret) +{ + if (secret->dismissed) + cups_secret_finish (secret, FALSE, NULL); + else if (secret->session != NULL) + cups_secret_finish (secret, TRUE, NULL); /* stored after a prompt */ + else if (secret->dispatch != NULL) + cups_secret_call (secret, secret->item, "org.freedesktop.DBus.Properties", "Get", + g_variant_new ("(ss)", SECRETS_IFACE ("Item"), "Attributes"), + G_VARIANT_TYPE ("(v)"), SECRET_ATTRIBUTES); + else + cups_secret_open_session (secret); +} + +static void +cups_secret_prompt_completed (GDBusConnection *bus, + const gchar *sender, + const gchar *path, + const gchar *interface, + const gchar *signal, + GVariant *parameters, + gpointer data) +{ + CupsSecret *secret = data; + gboolean dismissed = TRUE; + + g_dbus_connection_signal_unsubscribe (bus, secret->prompt_signal); + secret->prompt_signal = 0; + + if (g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(bv)"))) + g_variant_get (parameters, "(bv)", &dismissed, NULL); + secret->dismissed = dismissed; + secret->prompt_completed = TRUE; + + if (secret->prompt_returned) + cups_secret_unlocked (secret); +} + +/* Lets the keyring ask the user, if @prompt is not "/". */ +static void +cups_secret_prompt (CupsSecret *secret, + const gchar *prompt) +{ + if (strcmp (prompt, "/") == 0) + { + cups_secret_unlocked (secret); + return; + } + + secret->prompt_returned = secret->prompt_completed = FALSE; + secret->prompt_signal = + g_dbus_connection_signal_subscribe (secret->bus, SECRETS_BUS, SECRETS_IFACE ("Prompt"), + "Completed", prompt, NULL, G_DBUS_SIGNAL_FLAGS_NONE, + cups_secret_prompt_completed, secret, NULL); + cups_secret_call (secret, prompt, SECRETS_IFACE ("Prompt"), "Prompt", + g_variant_new ("(s)", ""), NULL, SECRET_PROMPT); +} + +static void +cups_secret_reply (GObject *source, + GAsyncResult *res, + gpointer data) +{ + CupsSecret *secret = data; + GError *error = NULL; + GVariant *reply, *value; + const gchar **unlocked, **locked; + const gchar *prompt; + gconstpointer password; + gsize length; + guint i; + + reply = g_dbus_connection_call_finish (secret->bus, res, &error); + if (reply == NULL) + { + cups_secret_finish (secret, FALSE, error); + return; + } + + switch (secret->step) + { + case SECRET_SEARCH: + g_variant_get (reply, "(^a&o^a&o)", &unlocked, &locked); + secret->item = g_strdup (unlocked[0] != NULL ? unlocked[0] : locked[0]); + i = unlocked[0] != NULL; + g_free (unlocked); + g_free (locked); + if (secret->item == NULL) + cups_secret_finish (secret, FALSE, NULL); + else if (i) + cups_secret_unlocked (secret); + else + cups_secret_call (secret, SECRETS_PATH, SECRETS_IFACE ("Service"), "Unlock", + g_variant_new ("(^ao)", (gchar *[]) { secret->item, NULL }), + G_VARIANT_TYPE ("(aoo)"), SECRET_UNLOCK); + break; + + case SECRET_UNLOCK: + case SECRET_CREATE: + g_variant_get (reply, secret->step == SECRET_UNLOCK ? "(@ao&o)" : "(&o&o)", NULL, &prompt); + cups_secret_prompt (secret, prompt); + break; + + case SECRET_PROMPT: + secret->prompt_returned = TRUE; + if (secret->prompt_completed) + cups_secret_unlocked (secret); + break; + + case SECRET_ATTRIBUTES: + g_variant_get (reply, "(v)", &value); + secret->auth_info = g_new0 (gchar *, g_strv_length (secret->required) + 1); + for (i = 0; secret->required[i] != NULL; i++) + { + const gchar *name = secret->required[i]; + const gchar *found = NULL; + + if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("a{ss}"))) + break; + if (strcmp (name, "password") == 0) + continue; + if (strcmp (name, "username") == 0 && + !g_variant_lookup (value, "user", "&s", &found)) + g_variant_lookup (value, "username", "&s", &found); + else if (strcmp (name, "hostname") == 0 && + !g_variant_lookup (value, "server", "&s", &found)) + g_variant_lookup (value, "hostname", "&s", &found); + else if (strcmp (name, "domain") == 0) + g_variant_lookup (value, "domain", "&s", &found); + + if (found == NULL) + break; + secret->auth_info[i] = g_strdup (found); + } + g_variant_unref (value); + if (secret->required[i] != NULL) + cups_secret_finish (secret, FALSE, NULL); + else + cups_secret_open_session (secret); + break; + + case SECRET_SESSION: + g_variant_get (reply, "(vo)", NULL, &secret->session); + if (secret->dispatch != NULL) + cups_secret_call (secret, secret->item, SECRETS_IFACE ("Item"), "GetSecret", + g_variant_new ("(o)", secret->session), + G_VARIANT_TYPE ("((oayays))"), SECRET_GET); + else + { + GVariantBuilder attributes, properties; + const gchar *password = NULL; + + g_variant_builder_init (&attributes, G_VARIANT_TYPE ("a{ss}")); + g_variant_builder_add (&attributes, "{ss}", "uri", secret->uri); + for (i = 0; secret->required[i] != NULL; i++) + { + const gchar *name = secret->required[i]; + + if (strcmp (name, "password") == 0) + password = secret->auth_info[i]; + else if (secret->auth_info[i] != NULL) + g_variant_builder_add (&attributes, "{ss}", + strcmp (name, "username") == 0 ? "user" : + strcmp (name, "hostname") == 0 ? "server" : name, + secret->auth_info[i]); + } + + g_variant_builder_init (&properties, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&properties, "{sv}", SECRETS_IFACE ("Item.Label"), + g_variant_new_string (secret->uri)); + g_variant_builder_add (&properties, "{sv}", SECRETS_IFACE ("Item.Attributes"), + g_variant_builder_end (&attributes)); + + cups_secret_call (secret, SECRETS_COLLECTION, SECRETS_IFACE ("Collection"), "CreateItem", + g_variant_new ("(a{sv}(o@ay@ays)b)", &properties, secret->session, + g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, "", 0, 1), + g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, password, + strlen (password), 1), + "text/plain", TRUE), + G_VARIANT_TYPE ("(oo)"), SECRET_CREATE); + } + break; + + case SECRET_GET: + g_variant_get (reply, "((o@ay@ays))", NULL, NULL, &value, NULL); + password = g_variant_get_fixed_array (value, &length, 1); + for (i = 0; strcmp (secret->required[i], "password") != 0; i++) + ; + secret->auth_info[i] = g_strndup (password, length); + g_variant_unref (value); + cups_secret_finish (secret, TRUE, NULL); + break; + } + + g_variant_unref (reply); +} + +static void +cups_secret_got_bus (GObject *source, + GAsyncResult *res, + gpointer data) +{ + CupsSecret *secret = data; + GError *error = NULL; + + secret->bus = g_bus_get_finish (res, &error); + if (secret->bus == NULL) + cups_secret_finish (secret, FALSE, error); + else if (secret->dispatch != NULL) + cups_secret_call (secret, SECRETS_PATH, SECRETS_IFACE ("Service"), "SearchItems", + g_variant_new_parsed ("({'uri': %s},)", secret->uri), + G_VARIANT_TYPE ("(aoao)"), SECRET_SEARCH); + else + cups_secret_call (secret, SECRETS_PATH, SECRETS_IFACE ("Service"), "Unlock", + g_variant_new ("(^ao)", (gchar *[]) { SECRETS_COLLECTION, NULL }), + G_VARIANT_TYPE ("(aoo)"), SECRET_UNLOCK); +} + +/* Asks for the password in the dialog unless the keyring has it. */ +static void +lookup_auth_info (GtkPrintCupsDispatchWatch *dispatch) +{ + CupsSecret *secret; + const gchar *uri; + guint i; + + if (dispatch->backend->authentication_lock) + return; + + uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri"); + for (i = 0; dispatch->request->auth_info_required[i] != NULL; i++) + if (strcmp (dispatch->request->auth_info_required[i], "password") == 0) + break; + if (dispatch->backend->secrets_unavailable || uri == NULL || + dispatch->request->auth_info_required[i] == NULL) + { + request_auth_info (dispatch); + return; + } + + dispatch->backend->authentication_lock = TRUE; + + secret = g_new0 (CupsSecret, 1); + secret->dispatch = dispatch; + secret->uri = g_strdup (uri); + secret->required = g_strdupv (dispatch->request->auth_info_required); + g_bus_get (G_BUS_TYPE_SESSION, NULL, cups_secret_got_bus, secret); +} + +static void +cups_secret_store (gchar **auth_info_required, + gchar **auth_info, + const gchar *printer_uri) +{ + CupsSecret *secret; + guint i, length = g_strv_length (auth_info_required); + + if (printer_uri == NULL) + return; + + secret = g_new0 (CupsSecret, 1); + secret->uri = g_strdup (printer_uri); + secret->required = g_strdupv (auth_info_required); + secret->auth_info = g_new0 (gchar *, length + 1); + for (i = 0; i < length; i++) + secret->auth_info[i] = g_strdup (auth_info[i]); + g_bus_get (G_BUS_TYPE_SESSION, NULL, cups_secret_got_bus, secret); +} + static gboolean cups_dispatch_watch_check (GSource *source) { @@ -1554,7 +1991,7 @@ cups_request_execute (GtkPrintBackendCups *print_backend, { dispatch->callback = callback; dispatch->callback_data = user_data; - request_auth_info (dispatch); + lookup_auth_info (dispatch); } else {