0.9.3claws10
authorChristoph Hohmann <reboot@gmx.ch>
Wed, 16 Jul 2003 10:48:31 +0000 (10:48 +0000)
committerChristoph Hohmann <reboot@gmx.ch>
Wed, 16 Jul 2003 10:48:31 +0000 (10:48 +0000)
* src/imap.[ch]
* src/prefs_account.[ch]
        sync IMAP CRAM-MD5 authentication with sylpheed-main

ChangeLog.claws
configure.ac
src/imap.c
src/imap.h
src/prefs_account.c
src/prefs_account.h

index 7d71743..e0a4f21 100644 (file)
@@ -1,3 +1,9 @@
+2003-07-16 [christoph] 0.9.3claws10
+
+       * src/imap.[ch]
+       * src/prefs_account.[ch]
+               sync IMAP CRAM-MD5 authentication with sylpheed-main
+
 2003-07-15 [alfons]    0.9.3claws9
 
        * src/main.c
index 5607ef8..d58db4f 100644 (file)
@@ -11,7 +11,7 @@ MINOR_VERSION=9
 MICRO_VERSION=3
 INTERFACE_AGE=0
 BINARY_AGE=0
-EXTRA_VERSION=claws9
+EXTRA_VERSION=claws10
 VERSION=$MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION$EXTRA_VERSION
 
 dnl set $target
index 562c906..fd890dd 100644 (file)
@@ -49,6 +49,8 @@
 #include "procheader.h"
 #include "prefs_account.h"
 #include "codeconv.h"
+#include "md5.h"
+#include "base64.h"
 #include "utils.h"
 #include "inputdialog.h"
 #include "log.h"
@@ -194,6 +196,11 @@ static void imap_folder_item_destroy       (Folder         *folder,
 
 static IMAPSession *imap_session_get   (Folder         *folder);
 
+static gint imap_auth                  (IMAPSession    *session,
+                                        const gchar    *user,
+                                        const gchar    *pass,
+                                        IMAPAuthType    type);
+
 static gint imap_scan_tree_recursive   (IMAPSession    *session,
                                         FolderItem     *item);
 static GSList *imap_parse_list         (IMAPFolder     *folder,
@@ -286,6 +293,11 @@ static const IMAPSet numberlist_to_imapset
                                        (MsgNumberList *list);
 
 /* low-level IMAP4rev1 commands */
+static gint imap_cmd_authenticate
+                               (IMAPSession    *session,
+                                const gchar    *user,
+                                const gchar    *pass,
+                                IMAPAuthType    type);
 static gint imap_cmd_login     (IMAPSession    *sock,
                                 const gchar    *user,
                                 const gchar    *pass);
@@ -503,25 +515,24 @@ static void imap_reset_uid_lists(Folder *folder)
        g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL); 
 }
 
+static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
+                     IMAPAuthType type)
+{
+       if (type == 0 || type == IMAP_AUTH_LOGIN)
+               return imap_cmd_login(session, user, pass);
+       else
+               return imap_cmd_authenticate(session, user, pass, type);
+}
+
 static IMAPSession *imap_session_get(Folder *folder)
 {
        RemoteFolder *rfolder = REMOTE_FOLDER(folder);
        IMAPSession *session = NULL;
-       gushort port;
 
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
 
-#if USE_OPENSSL
-       port = folder->account->set_imapport ? folder->account->imapport
-               : folder->account->ssl_imap == SSL_TUNNEL
-               ? IMAPS_PORT : IMAP4_PORT;
-#else
-       port = folder->account->set_imapport ? folder->account->imapport
-               : IMAP4_PORT;
-#endif
-
        /* Make sure we have a session */
        if (rfolder->session != NULL) {
                session = IMAP_SESSION(rfolder->session);
@@ -566,14 +577,14 @@ static IMAPSession *imap_session_get(Folder *folder)
                        /* Check if this is the first try to establish a
                           connection, if yes we don't try to reconnect */
                        if (rfolder->session == NULL) {
-                               log_warning(_("Connecting %s:%d failed"),
-                                           folder->account->recv_server, port);
+                               log_warning(_("Connecting %s failed"),
+                                           folder->account->recv_server);
                                session_destroy(SESSION(session));
                                session = NULL;
                        } else {
-                               log_warning(_("IMAP4 connection to %s:%d has been"
+                               log_warning(_("IMAP4 connection to %s has been"
                                              " disconnected. Reconnecting...\n"),
-                                           folder->account->recv_server, port);
+                                           folder->account->recv_server);
                                session_destroy(SESSION(session));
                                /* Clear folders session to make imap_session_get create
                                   a new session, because of rfolder->session == NULL
@@ -703,7 +714,7 @@ void imap_session_authenticate(IMAPSession *session, const PrefsAccount *account
                g_free(tmp_pass);
        }
 
-       if (imap_cmd_login(session, account->userid, pass) != IMAP_SUCCESS) {
+       if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
                imap_cmd_logout(session);
                return;
        }
@@ -2232,6 +2243,51 @@ catch:
 
 /* low-level IMAP4rev1 commands */
 
+static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
+                                 const gchar *pass, IMAPAuthType type)
+{
+       gchar *auth_type;
+       gint ok;
+       gchar *buf;
+       gchar *challenge;
+       gint challenge_len;
+       gchar hexdigest[33];
+       gchar *response;
+       gchar *response64;
+
+       auth_type = "CRAM-MD5";
+
+       imap_gen_send(session, "AUTHENTICATE %s", auth_type);
+       ok = imap_gen_recv(session, &buf);
+       if (ok != IMAP_SUCCESS || buf[0] != '+' || buf[1] != ' ') {
+               g_free(buf);
+               return IMAP_ERROR;
+       }
+
+       challenge = g_malloc(strlen(buf + 2) + 1);
+       challenge_len = base64_decode(challenge, buf + 2, -1);
+       challenge[challenge_len] = '\0';
+       log_print("IMAP< [Decoded: %s]\n", challenge);
+       g_free(buf);
+
+       md5_hex_hmac(hexdigest, challenge, challenge_len, pass, strlen(pass));
+       g_free(challenge);
+
+       response = g_strdup_printf("%s %s", user, hexdigest);
+       log_print("IMAP> [Encoded: %s]\n", response);
+       response64 = g_malloc((strlen(response) + 3) * 2 + 1);
+       base64_encode(response64, response, strlen(response));
+       g_free(response);
+
+       log_print("IMAP> %s\n", response64);
+       sock_puts(SESSION(session)->sock, response64);
+       ok = imap_cmd_ok(session, NULL);
+       if (ok != IMAP_SUCCESS)
+               log_warning(_("IMAP4 authentication failed.\n"));
+
+       return ok;
+}
+
 static gint imap_cmd_login(IMAPSession *session,
                           const gchar *user, const gchar *pass)
 {
index 6f6c29e..3a287de 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #include "folder.h"
 
+typedef enum
+{
+       IMAP_AUTH_LOGIN         = 1 << 0,
+       IMAP_AUTH_CRAM_MD5      = 1 << 1,
+} IMAPAuthType;
+
 FolderClass *imap_get_class            (void);
 
 #endif /* __IMAP_H__ */
index 549828e..556c609 100644 (file)
@@ -47,6 +47,8 @@
 #include "utils.h"
 #include "alertpanel.h"
 #include "colorlabel.h"
+#include "smtp.h"
+#include "imap.h"
 
 static gboolean cancelled;
 
@@ -97,6 +99,9 @@ static struct Receive {
 
        GtkWidget *filter_on_recv_chkbtn;
        GtkWidget *recvatgetall_chkbtn;
+       
+       GtkWidget *imap_frame;
+       GtkWidget *imap_auth_type_optmenu;
 
        GtkWidget *frame_maxarticle;
        GtkWidget *label_maxarticle;
@@ -201,6 +206,9 @@ static void prefs_account_protocol_set_data_from_optmenu(PrefParam *pparam);
 static void prefs_account_protocol_set_optmenu         (PrefParam *pparam);
 static void prefs_account_protocol_activated           (GtkMenuItem *menuitem);
 
+static void prefs_account_imap_auth_type_set_data_from_optmenu
+                                                       (PrefParam *pparam);
+static void prefs_account_imap_auth_type_set_optmenu   (PrefParam *pparam);
 static void prefs_account_smtp_auth_type_set_data_from_optmenu
                                                        (PrefParam *pparam);
 static void prefs_account_smtp_auth_type_set_optmenu   (PrefParam *pparam);
@@ -302,6 +310,11 @@ static PrefParam param[] = {
         &receive.filter_on_recv_chkbtn,
         prefs_set_data_from_toggle, prefs_set_toggle},
 
+       {"imap_auth_method", "0", &tmp_ac_prefs.imap_auth_type, P_ENUM,
+        &receive.imap_auth_type_optmenu,
+        prefs_account_imap_auth_type_set_data_from_optmenu,
+        prefs_account_imap_auth_type_set_optmenu},
+
        {"receive_at_get_all", "TRUE", &tmp_ac_prefs.recv_at_getall, P_BOOL,
         &receive.recvatgetall_chkbtn,
         prefs_set_data_from_toggle, prefs_set_toggle},
@@ -1103,6 +1116,10 @@ static void prefs_account_receive_create(void)
        GtkWidget *inbox_label;
        GtkWidget *inbox_entry;
        GtkWidget *inbox_btn;
+       GtkWidget *imap_frame;
+       GtkWidget *optmenu;
+       GtkWidget *optmenu_menu;
+       GtkWidget *menuitem;
        GtkWidget *recvatgetall_chkbtn;
 
        GtkWidget *hbox2;
@@ -1245,6 +1262,33 @@ static void prefs_account_receive_create(void)
        gtk_box_pack_start (GTK_BOX (hbox2), label_maxarticle, FALSE, FALSE, 0);
        gtk_label_set_justify (GTK_LABEL (label_maxarticle), GTK_JUSTIFY_LEFT);
 
+       PACK_FRAME (vbox1, imap_frame, _("IMAP4"));
+
+       vbox2 = gtk_vbox_new (FALSE, 0);
+       gtk_widget_show (vbox2);
+       gtk_container_add (GTK_CONTAINER (imap_frame), vbox2);
+       gtk_container_set_border_width (GTK_CONTAINER (vbox2), 8);
+
+       hbox1 = gtk_hbox_new (FALSE, 8);
+       gtk_widget_show (hbox1);
+       gtk_box_pack_start (GTK_BOX (vbox2), hbox1, FALSE, FALSE, 0);
+
+       label = gtk_label_new (_("Authentication method"));
+       gtk_widget_show (label);
+       gtk_box_pack_start (GTK_BOX (hbox1), label, FALSE, FALSE, 0);
+
+       optmenu = gtk_option_menu_new ();
+       gtk_widget_show (optmenu);
+       gtk_box_pack_start (GTK_BOX (hbox1), optmenu, FALSE, FALSE, 0);
+
+       optmenu_menu = gtk_menu_new ();
+
+       MENUITEM_ADD (optmenu_menu, menuitem, _("Automatic"), 0);
+       MENUITEM_ADD (optmenu_menu, menuitem, "LOGIN", IMAP_AUTH_LOGIN);
+       MENUITEM_ADD (optmenu_menu, menuitem, "CRAM-MD5", IMAP_AUTH_CRAM_MD5);
+
+       gtk_option_menu_set_menu (GTK_OPTION_MENU (optmenu), optmenu_menu);
+
        PACK_CHECK_BUTTON (vbox1, filter_on_recv_chkbtn,
                           _("Filter messages on receiving"));
 
@@ -1263,6 +1307,9 @@ static void prefs_account_receive_create(void)
        receive.inbox_entry              = inbox_entry;
        receive.inbox_btn                = inbox_btn;
 
+       receive.imap_frame               = imap_frame;
+       receive.imap_auth_type_optmenu   = optmenu;
+
        receive.recvatgetall_chkbtn      = recvatgetall_chkbtn;
 
        receive.frame_maxarticle        = frame2;
@@ -2336,6 +2383,41 @@ static void prefs_account_protocol_set_optmenu(PrefParam *pparam)
        gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
 }
 
+static void prefs_account_imap_auth_type_set_data_from_optmenu(PrefParam *pparam)
+{
+       GtkWidget *menu;
+       GtkWidget *menuitem;
+
+       menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(*pparam->widget));
+       menuitem = gtk_menu_get_active(GTK_MENU(menu));
+       *((RecvProtocol *)pparam->data) = GPOINTER_TO_INT
+               (gtk_object_get_user_data(GTK_OBJECT(menuitem)));
+}
+
+static void prefs_account_imap_auth_type_set_optmenu(PrefParam *pparam)
+{
+       IMAPAuthType type = *((IMAPAuthType *)pparam->data);
+       GtkOptionMenu *optmenu = GTK_OPTION_MENU(*pparam->widget);
+       GtkWidget *menu;
+       GtkWidget *menuitem;
+
+       switch (type) {
+       case IMAP_AUTH_LOGIN:
+               gtk_option_menu_set_history(optmenu, 1);
+               break;
+       case IMAP_AUTH_CRAM_MD5:
+               gtk_option_menu_set_history(optmenu, 2);
+               break;
+       case 0:
+       default:
+               gtk_option_menu_set_history(optmenu, 0);
+       }
+
+       menu = gtk_option_menu_get_menu(optmenu);
+       menuitem = gtk_menu_get_active(GTK_MENU(menu));
+       gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
+}
+
 static void prefs_account_smtp_auth_type_set_data_from_optmenu(PrefParam *pparam)
 {
        GtkWidget *menu;
@@ -2432,6 +2514,7 @@ static void prefs_account_protocol_activated(GtkMenuItem *menuitem)
                prefs_account_nntpauth_toggled
                        (GTK_TOGGLE_BUTTON(basic.nntpauth_chkbtn), NULL);
                gtk_widget_hide(receive.pop3_frame);
+               gtk_widget_hide(receive.imap_frame);
                gtk_widget_show(receive.frame_maxarticle);
                gtk_widget_set_sensitive(receive.recvatgetall_chkbtn, TRUE);
                /* update pop_before_smtp sensitivity */
@@ -2505,6 +2588,7 @@ static void prefs_account_protocol_activated(GtkMenuItem *menuitem)
                gtk_widget_set_sensitive(basic.uid_entry,  TRUE);
                gtk_widget_set_sensitive(basic.pass_entry, TRUE);
                gtk_widget_hide(receive.pop3_frame);
+               gtk_widget_hide(receive.imap_frame);
                gtk_widget_hide(receive.frame_maxarticle);
                gtk_widget_set_sensitive(receive.recvatgetall_chkbtn, TRUE);
                prefs_account_mailcmd_toggled
@@ -2584,6 +2668,7 @@ static void prefs_account_protocol_activated(GtkMenuItem *menuitem)
                gtk_widget_set_sensitive(basic.uid_entry,  TRUE);
                gtk_widget_set_sensitive(basic.pass_entry, TRUE);
                gtk_widget_hide(receive.pop3_frame);
+               gtk_widget_show(receive.imap_frame);
                gtk_widget_hide(receive.frame_maxarticle);
                gtk_widget_set_sensitive(receive.recvatgetall_chkbtn, TRUE);
                gtk_widget_set_sensitive(basic.smtpserv_entry, TRUE);
@@ -2665,6 +2750,7 @@ static void prefs_account_protocol_activated(GtkMenuItem *menuitem)
                gtk_widget_set_sensitive(basic.pass_entry, TRUE);
                gtk_widget_set_sensitive(receive.pop3_frame, TRUE);
                gtk_widget_show(receive.pop3_frame);
+               gtk_widget_hide(receive.imap_frame);
                gtk_widget_hide(receive.frame_maxarticle);
                gtk_widget_set_sensitive(receive.recvatgetall_chkbtn, TRUE);
 
index d130c92..ddeb1be 100644 (file)
@@ -110,6 +110,8 @@ struct _PrefsAccount
        gchar *inbox;
        gint max_articles;
 
+       gint imap_auth_type;
+
        /* Send */
        gboolean add_date;
        gboolean gen_msgid;