Esempio n. 1
0
static void
libbalsa_address_book_ldap_load_config(LibBalsaAddressBook * ab,
				       const gchar * prefix)
{
    LibBalsaAddressBookLdap *ldap;

    g_return_if_fail(LIBBALSA_IS_ADDRESS_BOOK_LDAP(ab));

    ldap = LIBBALSA_ADDRESS_BOOK_LDAP(ab);

    ldap->host = libbalsa_conf_get_string("Host");
    ldap->base_dn = libbalsa_conf_get_string("BaseDN");
    if(ldap->base_dn && *ldap->base_dn == 0) { 
	g_free(ldap->base_dn); ldap->base_dn = NULL; 
    }

    ldap->bind_dn = libbalsa_conf_private_get_string("BindDN");
    if(ldap->bind_dn && *ldap->bind_dn == 0) { 
	g_free(ldap->bind_dn); ldap->bind_dn = NULL; 
    }
    ldap->passwd = libbalsa_conf_private_get_string("Passwd");
    if(ldap->passwd && *ldap->passwd == 0) { 
	g_free(ldap->passwd); ldap->passwd = NULL; 
    }
    ldap->priv_book_dn = libbalsa_conf_get_string("BookDN");
    if(ldap->priv_book_dn && *ldap->priv_book_dn == 0) { 
	g_free(ldap->priv_book_dn); ldap->priv_book_dn = NULL; 
    }
    ldap->enable_tls = libbalsa_conf_get_bool("EnableTLS");

    if (LIBBALSA_ADDRESS_BOOK_CLASS(parent_class)->load_config)
	LIBBALSA_ADDRESS_BOOK_CLASS(parent_class)->load_config(ab, prefix);

    if (ab->is_expensive < 0)
        ab->is_expensive = TRUE;
}
Esempio n. 2
0
LibBalsaImapServer*
libbalsa_imap_server_new_from_config(void)
{
    LibBalsaServer tmp_server;
    LibBalsaImapServer *imap_server;
    LibBalsaServer *server;
    gboolean d, d1;
    gint tls_mode, conn_limit;

    tmp_server.host = libbalsa_conf_get_string("Server");
    if(strrchr(tmp_server.host, ':') == NULL) {
        gint port;
        port = libbalsa_conf_get_int_with_default("Port", &d);
        if (!d) {
            gchar *newhost = g_strdup_printf("%s:%d", tmp_server.host, port);
            g_free(tmp_server.host);
            tmp_server.host = newhost;
        }
    }       
    tmp_server.user = libbalsa_conf_private_get_string("Username");
    if (!tmp_server.user)
        tmp_server.user = g_strdup(getenv("USER"));

    imap_server = get_or_create(tmp_server.user, tmp_server.host);
    server = LIBBALSA_SERVER(imap_server);
    if (server->user) {
        g_free(tmp_server.user);
        g_free(tmp_server.host);
    } else {
        server->user = tmp_server.user;
        server->host = tmp_server.host;
    }
    d1 = libbalsa_conf_get_bool_with_default("Anonymous", &d);
    if(!d) server->try_anonymous = !!d1;
    server->use_ssl |= libbalsa_conf_get_bool("SSL=false");
    tls_mode = libbalsa_conf_get_int_with_default("TLSMode", &d);
    if(!d) server->tls_mode = tls_mode;
    conn_limit = libbalsa_conf_get_int_with_default("ConnectionLimit", &d);
    if(!d) imap_server->max_connections = conn_limit;
    d1 = libbalsa_conf_get_bool_with_default("PersistentCache", &d);
    if(!d) imap_server->persistent_cache = !!d1;
    d1 = libbalsa_conf_get_bool_with_default("HasFetchBug", &d);
    if(!d) imap_server->has_fetch_bug = !!d1;
    d1 = libbalsa_conf_get_bool_with_default("UseStatus", &d);
    if(!d) imap_server->use_status = !!d1;
    d1 = libbalsa_conf_get_bool_with_default("UseIdle", &d);
    if(!d) imap_server->use_idle = !!d1;
    if (!server->passwd) {
        server->remember_passwd = libbalsa_conf_get_bool("RememberPasswd=false");
        if(server->remember_passwd) {
#if defined(HAVE_LIBSECRET)
            GError *err = NULL;

            server->passwd =
                secret_password_lookup_sync(LIBBALSA_SERVER_SECRET_SCHEMA,
                                            NULL, &err,
                                            "protocol", server->protocol,
                                            "server",   server->host,
                                            "user",     server->user,
                                            NULL);
            if (err) {
                libbalsa_free_password(server->passwd);
                server->passwd = NULL;
                printf(_("Error looking up password for %s@%s: %s\n"),
                       server->user, server->host, err->message);
                printf(_("Falling back\n"));
                g_clear_error(&err);
                server->passwd =
                    libbalsa_conf_private_get_string("Password");
                if (server->passwd != NULL) {
                    gchar *buff = libbalsa_rot(server->passwd);
                    libbalsa_free_password(server->passwd);
                    server->passwd = buff;
                    secret_password_store_sync
                        (LIBBALSA_SERVER_SECRET_SCHEMA, NULL,
                         _("Balsa passwords"), server->passwd, NULL, &err,
                         "protocol", server->protocol,
                         "server",   server->host,
                         "user",     server->user,
                         NULL);
                    /* We could in principle clear the password in the
                     * config file here but we do not for the backward
                     * compatibility. */
                    if (err) {
                        printf(_("Error storing password for %s@%s: %s\n"),
                               server->user, server->host, err->message);
                        g_error_free(err);
                    }
                }
            }
#elif defined (HAVE_GNOME_KEYRING)
	    GnomeKeyringResult r;
	    server->passwd = NULL;
	    r = gnome_keyring_find_password_sync(LIBBALSA_SERVER_KEYRING_SCHEMA,
						 &server->passwd,
						 "protocol", server->protocol,
						 "server", server->host,
						 "user", server->user,
						 NULL);
	    if(r != GNOME_KEYRING_RESULT_OK) {
		gnome_keyring_free_password(server->passwd);
		server->passwd = NULL;
		printf("Keyring has no password for %s@%s\n",
		       server->user, server->host);
		server->passwd = libbalsa_conf_private_get_string("Password");
		if (server->passwd != NULL) {
		    gchar *buff = libbalsa_rot(server->passwd);
		    libbalsa_free_password(server->passwd);
		    server->passwd = buff;
	            gnome_keyring_store_password_sync
                        (LIBBALSA_SERVER_KEYRING_SCHEMA, NULL,
                         _("Balsa passwords"), server->passwd,
                         "protocol", server->protocol,
                         "server", server->host,
                         "user", server->user,
                         NULL);
		    /* We could in principle clear the password in the
		       config file here but we do not for the backward
		       compatibility. */
		}
	    }
#else
            server->passwd = libbalsa_conf_private_get_string("Password");
	    if (server->passwd != NULL) {
		gchar *buff = libbalsa_rot(server->passwd);
		libbalsa_free_password(server->passwd);
		server->passwd = buff;
	    }
#endif                          /* defined(HAVE_LIBSECRET) */
	}
        if(server->passwd && server->passwd[0] == '\0') {
            libbalsa_free_password(server->passwd);
            server->passwd = NULL;
        }
    }
    return imap_server;
}