Implement a password get hooklist, allowing plugins to
authorColin Leroy <colin@colino.net>
Thu, 30 May 2013 13:26:00 +0000 (15:26 +0200)
committerColin Leroy <colin@colino.net>
Fri, 14 Jun 2013 08:26:05 +0000 (10:26 +0200)
provide passwords for various accounts. Patch by Michal
Gorki, bug #2252 "API for external password storage"

src/account.c
src/account.h
src/imap.c
src/inc.c
src/news.c
src/send_message.c

index 303d9ec..3a898da 100644 (file)
@@ -53,6 +53,7 @@
 #include "manual.h"
 #include "filtering.h"
 #include "prefs_actions.h"
+#include "hooks.h"
 
 enum {
        ACCOUNT_IS_DEFAULT,
@@ -1849,3 +1850,29 @@ PrefsAccount *account_get_cur_account (void)
 {
        return cur_account;
 }
+
+gboolean password_get(const gchar *user,
+                   const gchar *server,
+                   const gchar *protocol,
+                   guint16 port,
+                   gchar **password)
+{
+       PasswordRequest req;
+
+       /* all have to be set */
+       cm_return_val_if_fail(user != NULL, FALSE);
+       cm_return_val_if_fail(server != NULL, FALSE);
+       cm_return_val_if_fail(protocol != NULL, FALSE);
+       cm_return_val_if_fail(port != NULL, FALSE);
+
+       req.user = user;
+       req.server = server;
+       req.protocol = protocol;
+       req.port = port;
+
+       if (hooks_invoke(PASSWORD_GET_HOOKLIST, &req)) {
+               *password = req.password;
+               return TRUE;
+       }
+       return FALSE;
+}
index 4bc44be..e7c7760 100644 (file)
 typedef gint   (*AccountFunc)  (PrefsAccount   *ac_prefs,
                                 gpointer        user_data);
 
+typedef struct {
+       const gchar *user;
+       const gchar *server;
+       const gchar *protocol;
+       guint16 port;
+
+       gchar *password;
+} PasswordRequest;
+
+#define PASSWORD_GET_HOOKLIST "password_get_hooklist"
+
 extern PrefsAccount *cur_account;
 
 PrefsAccount *account_get_cur_account   (void);
@@ -63,4 +74,10 @@ void               account_rename_path       (const gchar    *old_id,
                                         const gchar    *new_id);
 gchar *account_get_signature_str(PrefsAccount *account);
 
+gboolean      password_get(const gchar *user,
+                          const gchar *server,
+                          const gchar *protocol,
+                          guint16 port,
+                          gchar **password);
+
 #endif /* __ACCOUNT_H__ */
index 94de747..ba4a480 100644 (file)
@@ -1213,7 +1213,14 @@ static gint imap_session_authenticate(IMAPSession *session,
        gboolean failed = FALSE;
        gint ok = MAILIMAP_NO_ERROR;
        g_return_val_if_fail(account->userid != NULL, MAILIMAP_ERROR_BAD_STATE);
-       acc_pass = account->passwd;
+
+       if (password_get(account->userid, account->recv_server, "imap",
+                        SESSION(session)->port, &pass)) {
+               Xstrdup_a(acc_pass, pass, {g_free(pass); return MAILIMAP_NO_ERROR;});
+               g_free(pass);
+       } else {
+               acc_pass = account->passwd;
+       }
 try_again:
        pass = acc_pass;
        if (!pass && account->imap_auth_type != IMAP_AUTH_ANON && account->imap_auth_type != IMAP_AUTH_GSSAPI) {
index 80ad90d..c9ce084 100644 (file)
--- a/src/inc.c
+++ b/src/inc.c
@@ -552,7 +552,12 @@ static gint inc_start(IncProgressDialog *inc_dialog)
                session = qlist->data;
                pop3_session = POP3_SESSION(session->session); 
                pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
-               if (pop3_session->ac_prefs->passwd)
+               if (password_get(pop3_session->user,
+                                       pop3_session->ac_prefs->recv_server,
+                                       "pop3", SESSION(session)->port,
+                                       &(pop3_session->pass))) {
+                       /* NOP */;
+               } else if (pop3_session->ac_prefs->passwd)
                        pop3_session->pass =
                                g_strdup(pop3_session->ac_prefs->passwd);
                else {
index 9c9080c..9df3090 100644 (file)
@@ -376,15 +376,6 @@ static Session *news_session_new_for_folder(Folder *folder)
        cm_return_val_if_fail(folder->account != NULL, NULL);
 
        ac = folder->account;
-       if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
-               userid = ac->userid;
-               if (ac->passwd && ac->passwd[0])
-                       passwd = g_strdup(ac->passwd);
-               else
-                       passwd = input_dialog_query_password_keep(ac->nntp_server,
-                                                                 userid,
-                                                                 &(ac->session_passwd));
-       }
 
 #ifdef USE_GNUTLS
        port = ac->set_nntpport ? ac->nntpport
@@ -409,6 +400,18 @@ static Session *news_session_new_for_folder(Folder *folder)
        session = news_session_new(folder, ac->nntp_server, port, userid, passwd);
 #endif
 
+       if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
+               userid = ac->userid;
+               if (password_get(userid, ac->nntp_server, "nntp", port, &passwd)) {
+                       /* NOP */;
+               } else if (ac->passwd && ac->passwd[0])
+                       passwd = g_strdup(ac->passwd);
+               else
+                       passwd = input_dialog_query_password_keep(ac->nntp_server,
+                                                                 userid,
+                                                                 &(ac->session_passwd));
+       }
+
        if (session != NULL)
                r = nntp_threaded_mode_reader(folder);
        else
index 4e5f6e6..3cf8204 100644 (file)
@@ -287,7 +287,11 @@ gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, g
                        smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
                        if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
                                smtp_session->user = g_strdup(ac_prefs->smtp_userid);
-                               if (ac_prefs->smtp_passwd)
+                               if (password_get(smtp_session->user,
+                                                       ac_prefs->smtp_server, "smtp", port,
+                                                       &(smtp_session->pass))) {
+                                       /* NOP */;
+                               } else if (ac_prefs->smtp_passwd)
                                        smtp_session->pass =
                                                g_strdup(ac_prefs->smtp_passwd);
                                else {
@@ -303,7 +307,11 @@ gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, g
                                }
                        } else {
                                smtp_session->user = g_strdup(ac_prefs->userid);
-                               if (ac_prefs->passwd)
+                               if (password_get(smtp_session->user,
+                                                       ac_prefs->smtp_server, "smtp", port,
+                                                       &(smtp_session->pass))) {
+                                       /* NOP */;
+                               } else if (ac_prefs->passwd)
                                        smtp_session->pass = g_strdup(ac_prefs->passwd);
                                else {
                                        smtp_session->pass =