File: server.c

package info (click to toggle)
balsa 2.6.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,576 kB
  • sloc: ansic: 99,871; xml: 4,934; makefile: 769; sh: 185; awk: 60; python: 34
file content (881 lines) | stat: -rw-r--r-- 26,711 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 *
 * Copyright (C) 1997-2016 Stuart Parmenter and others,
 *                         See the file AUTHORS for a list.
 *
 * 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
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif                          /* HAVE_CONFIG_H */
#include "server.h"

#include <string.h>
#include <stdlib.h>

#if defined(HAVE_LIBSECRET)
#include <libsecret/secret.h>
#endif                          /* defined(HAVE_LIBSECRET) */

#include "libbalsa.h"
#include "libbalsa_private.h"
#include "libbalsa-conf.h"
#include "net-client-utils.h"
#include <glib/gi18n.h>


#ifdef G_LOG_DOMAIN
#  undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "libbalsa-server"


#if defined(HAVE_LIBSECRET)
static const SecretSchema server_schema = {
    "org.gnome.Balsa.NetworkPassword", SECRET_SCHEMA_NONE,
    {
	{ "protocol", SECRET_SCHEMA_ATTRIBUTE_STRING },
	{ "server",   SECRET_SCHEMA_ATTRIBUTE_STRING },
	{ "user",     SECRET_SCHEMA_ATTRIBUTE_STRING },
	{ NULL, 0 }
    }
};

static const SecretSchema cert_pass_schema = {
    "org.gnome.Balsa.CertificatePassword", SECRET_SCHEMA_NONE,
    {
	{ "protocol", SECRET_SCHEMA_ATTRIBUTE_STRING },
	{ "server",   SECRET_SCHEMA_ATTRIBUTE_STRING },
	{ "user",     SECRET_SCHEMA_ATTRIBUTE_STRING },
	{ NULL, 0 }
    }
};
#endif                          /* defined(HAVE_LIBSECRET) */

typedef struct _LibBalsaServerPrivate LibBalsaServerPrivate;

struct _LibBalsaServerPrivate {
    const gchar *protocol; /**< type of the server: imap, pop3, or smtp. */
    gchar *host;
    gchar *user;
    gchar *passwd;
    NetClientCryptMode security;
    gboolean client_cert;
    gchar *cert_file;
    gchar *cert_passphrase;
    gboolean remember_passwd;
    gboolean remember_cert_passphrase;
    NetClientAuthMode auth_mode;
};

static void libbalsa_server_finalize(GObject * object);

static void libbalsa_server_real_set_username(LibBalsaServer *server,
					      const gchar    *username);
static void libbalsa_server_real_set_host(LibBalsaServer    *server,
                                          const gchar       *host,
                                          NetClientCryptMode security);
static gchar *lbs_get_password(LibBalsaServer *server,
                                           gchar          *cert_subject);
static gchar *libbalsa_free_password(gchar *password);

G_DEFINE_TYPE_WITH_PRIVATE(LibBalsaServer, libbalsa_server, G_TYPE_OBJECT)

enum {
    SET_USERNAME,
    SET_HOST,
    CONFIG_CHANGED,
    GET_PASSWORD,
    LAST_SIGNAL
};

static guint libbalsa_server_signals[LAST_SIGNAL];

static void
libbalsa_server_class_init(LibBalsaServerClass * klass)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS(klass);

    object_class->finalize = libbalsa_server_finalize;

    libbalsa_server_signals[SET_USERNAME] =
	g_signal_new("set-username",
                     G_TYPE_FROM_CLASS(object_class),
                     G_SIGNAL_RUN_FIRST,
		     G_STRUCT_OFFSET(LibBalsaServerClass,
				     set_username),
                     NULL, NULL, NULL,
                     G_TYPE_NONE, 1,
		     G_TYPE_STRING);
    libbalsa_server_signals[SET_HOST] =
	g_signal_new("set-host",
                     G_TYPE_FROM_CLASS(object_class),
                     G_SIGNAL_RUN_FIRST,
		     G_STRUCT_OFFSET(LibBalsaServerClass,
                                     set_host),
                     NULL, NULL, NULL,
                     G_TYPE_NONE, 2,
                     G_TYPE_POINTER, G_TYPE_INT
		     );
    libbalsa_server_signals[CONFIG_CHANGED] =
	g_signal_new("config-changed",
                     G_TYPE_FROM_CLASS(object_class),
                     G_SIGNAL_RUN_FIRST,
		     G_STRUCT_OFFSET(LibBalsaServerClass,
				     config_changed),
                     NULL, NULL, NULL,
                     G_TYPE_NONE, 0);

    libbalsa_server_signals[GET_PASSWORD] =
	g_signal_new("get-password",
                     G_TYPE_FROM_CLASS(object_class),
                     G_SIGNAL_RUN_LAST,
					 0U,
                     NULL, NULL, NULL,
                     G_TYPE_STRING, 1,
					 G_TYPE_BYTE_ARRAY);

    klass->set_username = libbalsa_server_real_set_username;
    klass->set_host = libbalsa_server_real_set_host;
}

static void
libbalsa_server_init(LibBalsaServer * server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    priv->protocol = NULL;
    priv->host = NULL;
    priv->user = NULL;
    priv->passwd = NULL;
    priv->remember_passwd = TRUE;
    priv->security		= NET_CLIENT_CRYPT_STARTTLS;
}

/* leave object in sane state (NULLified fields) */
static void
libbalsa_server_finalize(GObject * object)
{
    LibBalsaServer *server = LIBBALSA_SERVER(object);
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_free(priv->host);
    g_free(priv->user);
    g_free(priv->cert_file);
    priv->passwd = libbalsa_free_password(priv->passwd);
    priv->cert_passphrase = libbalsa_free_password(priv->cert_passphrase);

    G_OBJECT_CLASS(libbalsa_server_parent_class)->finalize(object);
}

LibBalsaServer *
libbalsa_server_new(void)
{
    LibBalsaServer *server;

    server = g_object_new(LIBBALSA_TYPE_SERVER, NULL);

    return server;
}

void
libbalsa_server_set_username(LibBalsaServer * server,
			     const gchar * username)
{
    g_return_if_fail(server != NULL);
    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    g_signal_emit(server,
		  libbalsa_server_signals[SET_USERNAME], 0, username);
}

void
libbalsa_server_set_password(LibBalsaServer *server,
                             const gchar    *passwd,
							 gboolean        for_cert)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
	gchar **target;
    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    if (for_cert) {
        target = &priv->cert_passphrase;
    } else {
        target = &priv->passwd;
    }

    *target = libbalsa_free_password(*target);
    if ((passwd != NULL) && (passwd[0] != '\0')) {
        *target = g_strdup(passwd);
    }
}

void
libbalsa_server_set_host(LibBalsaServer * server, const gchar * host,
                         NetClientCryptMode security)
{
    g_return_if_fail(server != NULL);
    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    g_signal_emit(server, libbalsa_server_signals[SET_HOST],
		  0, host, security);

}

void
libbalsa_server_config_changed(LibBalsaServer * server)
{
    g_return_if_fail(server != NULL);
    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    g_signal_emit(server, libbalsa_server_signals[CONFIG_CHANGED],
                  0);
}

static gchar *
lbs_get_password(LibBalsaServer *server,
			     	 	 	 gchar          *cert_subject)
{
    gchar *retval = NULL;

    g_return_val_if_fail(server != NULL, NULL);
    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), NULL);

    g_signal_emit(server, libbalsa_server_signals[GET_PASSWORD], 0, cert_subject, &retval);
    return retval;
}

static void
libbalsa_server_real_set_username(LibBalsaServer * server,
				  const gchar * username)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_free(priv->user);
    priv->user = g_strdup(username);
}

static void
libbalsa_server_real_set_host(LibBalsaServer * server, const gchar * host,
	NetClientCryptMode  security)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_free(priv->host);
    priv->host = g_strdup(host);
    priv->security = security;
}

void
libbalsa_server_load_security_config(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
	gboolean not_found;

    priv->security = libbalsa_conf_get_int_with_default("Security", &not_found);
    if (not_found) {
        gboolean want_ssl;

        g_debug("server %s@%s: no security config, try to read old settings", priv->protocol, priv->host);
        want_ssl = libbalsa_conf_get_bool_with_default("SSL", &not_found);
        if (want_ssl && !not_found) {
        	priv->security = NET_CLIENT_CRYPT_ENCRYPTED;
        	libbalsa_conf_clean_key("SSL");
        } else {
        	int want_tls;

        	want_tls = libbalsa_conf_get_int_with_default("TLSMode", &not_found);
        	if (not_found) {
        		priv->security = NET_CLIENT_CRYPT_STARTTLS;
        	} else {
        		switch (want_tls) {
        		case 0:
        			priv->security = NET_CLIENT_CRYPT_NONE;
        			break;
        		case 1:
        			priv->security = NET_CLIENT_CRYPT_STARTTLS_OPT;
        			break;
        		default:
        			priv->security = NET_CLIENT_CRYPT_STARTTLS;
        		}
            	libbalsa_conf_clean_key("SSL");
        	}
        }
    }
}


#if defined(HAVE_LIBSECRET)

static gchar *
libbalsa_free_password(gchar *password)
{
	secret_password_free(password);
	return NULL;
}

static void
store_password_libsecret(LibBalsaServer     *server,
						 const gchar        *password,
						 const gchar        *cfg_path,
						 const SecretSchema *schema)
{
	if (libbalsa_conf_use_libsecret()) {
		LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
		GError *err = NULL;

		secret_password_store_sync(schema, NULL, _("Balsa passwords"), password, NULL, &err,
			"protocol", priv->protocol,
			"server",   priv->host,
			"user",     priv->user,
			NULL);
		if (err != NULL) {
			libbalsa_information(LIBBALSA_INFORMATION_ERROR,
				/* Translators: #1 user name; #2 protocol (imap, etc); #3 server name; #4 error message */
				_("Error saving credentials for user “%s”, protocol “%s”, server “%s” in Secret Service: %s"),
				priv->user, priv->protocol, priv->host, err->message);
			g_error_free(err);
		} else {
			g_debug("stored password for %s@%s:%s in secret service", priv->user, priv->protocol, priv->host);
			libbalsa_conf_clean_key(cfg_path);
		}
	} else {
		libbalsa_conf_private_set_string(cfg_path, password, TRUE);
	}
}

static gchar *
load_password_libsecret(LibBalsaServer     *server,
						const gchar        *cfg_path,
						const SecretSchema *schema)
{
	gchar *password;

	if (libbalsa_conf_use_libsecret()) {
		LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
		GError *err = NULL;

		password = secret_password_lookup_sync(schema, NULL, &err,
			"protocol", priv->protocol,
			"server",   priv->host,
			"user",     priv->user,
			NULL);
		if (err != NULL) {
			libbalsa_information(LIBBALSA_INFORMATION_ERROR,
				/* Translators: #1 user name; #2 protocol (imap, etc); #3 server name; #4 error message */
				_("Error loading credentials for user %s, protocol “%s”, server “%s” from Secret Service: %s"),
				priv->user, priv->protocol, priv->host, err->message);
			g_error_free(err);
		}

		/* check the config file if the returned password is NULL, make sure to remove it from the config file otherwise */
		if (password == NULL) {
			password = libbalsa_conf_private_get_string(cfg_path, TRUE);
			if (password != NULL) {
				store_password_libsecret(server, password, cfg_path, schema);
			}
		} else {
			g_debug("loaded password for %s@%s:%s from secret service", priv->user, priv->protocol, priv->host);
			libbalsa_conf_clean_key(cfg_path);
		}
	} else {
		password = libbalsa_conf_private_get_string(cfg_path, TRUE);
	}

	return password;
}

static void
erase_password_libsecret(LibBalsaServer *server,
						 const SecretSchema   *schema)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
    GError *err = NULL;
    gboolean removed;

    removed = secret_password_clear_sync(schema, NULL, &err,
        "protocol", priv->protocol,
		"server",   priv->host,
		"user",     priv->user,
		NULL);
    if (err != NULL) {
        g_info("error erasing password for %s@%s:%s from secret service: %s", priv->user, priv->protocol, priv->host,
        	err->message);
        g_error_free(err);
    } else if (removed) {
        g_debug("erased password for %s@%s:%s from secret service", priv->user, priv->protocol, priv->host);
    } else {
        g_debug("no password erased for %s@%s:%s from secret service", priv->user, priv->protocol, priv->host);
    }
}

#else

static gchar *
libbalsa_free_password(gchar *password)
{
	net_client_free_authstr(password);
	return NULL;
}

#endif


/* libbalsa_server_load_config:
   load the server configuration using gnome-config.
   Try to use sensible defaults.
   FIXME: Port field is kept here only for compatibility, drop after 1.4.x
   release.
*/
void
libbalsa_server_load_config(LibBalsaServer * server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    priv->host = libbalsa_conf_get_string("Server");
    if ((priv->host != NULL) && (strrchr(priv->host, ':') == NULL)) {
        gint port;
        gboolean d;

        port = libbalsa_conf_get_int_with_default("Port", &d);
        if (!d) {
            gchar *newhost = g_strdup_printf("%s:%d", priv->host, port);

            g_free(priv->host);
            priv->host = newhost;
        }
    }
    libbalsa_server_load_security_config(server);
    priv->user = libbalsa_conf_private_get_string("Username", FALSE);
    if (priv->user == NULL) {
        priv->user = g_strdup(g_get_user_name());
    }

	priv->auth_mode = libbalsa_conf_get_int("AuthMode=2");  /* default NET_CLIENT_AUTH_USER_PASS */
    if (libbalsa_conf_has_key("Anonymous")) {
    	if (libbalsa_conf_get_bool("Anonymous")) {
    		priv->auth_mode = NET_CLIENT_AUTH_NONE_ANON;
    	}
    	libbalsa_conf_clean_key("Anonymous");
    }
    priv->remember_passwd = libbalsa_conf_get_bool("RememberPasswd=false");

    priv->passwd = libbalsa_free_password(priv->passwd);
    if (priv->remember_passwd) {
#if defined(HAVE_LIBSECRET)
        priv->passwd = load_password_libsecret(server, "Password", &server_schema);
#else
        priv->passwd = libbalsa_conf_private_get_string("Password", TRUE);
#endif		/* HAVE_LIBSECRET */
        if ((priv->passwd != NULL) && (priv->passwd[0] == '\0')) {
        	priv->passwd = libbalsa_free_password(priv->passwd);
        }
    }

    /* client certificate stuff */
    priv->client_cert = libbalsa_conf_get_bool("NeedClientCert=false");
    priv->cert_file = libbalsa_conf_get_string("UserCertificateFile");
    priv->remember_cert_passphrase = libbalsa_conf_get_bool("RememberCertPasswd=false");

    priv->cert_passphrase = libbalsa_free_password(priv->cert_passphrase);
    if (priv->client_cert && priv->remember_cert_passphrase) {
#if defined(HAVE_LIBSECRET)
        priv->cert_passphrase = load_password_libsecret(server, "CertificatePassphrase", &cert_pass_schema);
#else
        priv->cert_passphrase = libbalsa_conf_private_get_string("CertificatePassphrase", TRUE);
#endif		/* HAVE_LIBSECRET */
        if ((priv->cert_passphrase != NULL) && (priv->cert_passphrase[0] == '\0')) {
        	priv->cert_passphrase = libbalsa_free_password(priv->cert_passphrase);
        }
    }
}

/* libbalsa_server_save_config:
   save config.
   It is bit tricky to decide the value of the remember_passwd field.
   Should empty values be remembered? Even if they are, balsa will 
   still ask for the password if it is empty.
*/
void
libbalsa_server_save_config(LibBalsaServer * server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    libbalsa_conf_set_string("Server", priv->host);
    libbalsa_conf_private_set_string("Username", priv->user, FALSE);
    libbalsa_conf_set_int("AuthMode", priv->auth_mode);

    if (priv->remember_passwd && (priv->passwd != NULL)) {
        libbalsa_conf_set_bool("RememberPasswd", TRUE);
#if defined(HAVE_LIBSECRET)
        store_password_libsecret(server, priv->passwd, "Password", &server_schema);
#else
        libbalsa_conf_private_set_string("Password", priv->passwd, TRUE);
#endif
    } else {
        libbalsa_conf_set_bool("RememberPasswd", FALSE);
#if defined(HAVE_LIBSECRET)
        erase_password_libsecret(server, &server_schema);
#endif		/* HAVE_LIBSECRET */
        libbalsa_conf_private_set_string("Password", NULL, TRUE);
    }

    libbalsa_conf_set_bool("NeedClientCert", priv->client_cert);
    if (priv->cert_file != NULL) {
        libbalsa_conf_set_string("UserCertificateFile", priv->cert_file);
        if (priv->remember_cert_passphrase && (priv->cert_passphrase != NULL)) {
        	libbalsa_conf_set_bool("RememberCertPasswd", TRUE);
#if defined(HAVE_LIBSECRET)
        	store_password_libsecret(server, priv->cert_passphrase, "CertificatePassphrase", &cert_pass_schema);
#else
        	libbalsa_conf_private_set_string("CertificatePassphrase", priv->cert_passphrase, TRUE);
#endif

        } else {
        	libbalsa_conf_set_bool("RememberCertPasswd", FALSE);
#if defined(HAVE_LIBSECRET)
        	erase_password_libsecret(server, &cert_pass_schema);
#endif		/* HAVE_LIBSECRET */
        	libbalsa_conf_private_set_string("CertificatePassphrase", NULL, TRUE);
        }
    }

    libbalsa_conf_set_int("Security", priv->security);
}

/* Connect the server's "get-password" signal to the callback; if the
 * ask-password callback is connected more than once, the dialog is
 * popped up the corresponding number of times, so we'll ignore the
 * request if a callback is already connected. */
void
libbalsa_server_connect_signals(LibBalsaServer * server, GCallback cb,
                                gpointer cb_data)
{
    if (!g_signal_has_handler_pending(server,
                                      libbalsa_server_signals
                                      [GET_PASSWORD], 0, TRUE))
        g_signal_connect(server, "get-password", cb, cb_data);
}


gchar **
libbalsa_server_get_auth(NetClient         *client,
						 NetClientAuthMode  mode,
						 gpointer           user_data)
{
    LibBalsaServer *server = LIBBALSA_SERVER(user_data);
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
    gchar **result = NULL;

    g_debug("%s: %p %d %p: encrypted = %d", __func__, client, mode, user_data, net_client_is_encrypted(client));
    if (priv->auth_mode != NET_CLIENT_AUTH_NONE_ANON) {
        result = g_new0(gchar *, 3U);
        result[0] = g_strdup(priv->user);
        switch (mode) {
        case NET_CLIENT_AUTH_USER_PASS:
            if ((priv->passwd != NULL) && (priv->passwd[0] != '\0')) {
            	result[1] = g_strdup(priv->passwd);
            } else {
            	result[1] = lbs_get_password(server, NULL);
            }
            break;
        case NET_CLIENT_AUTH_KERBEROS:
        	break;			/* only user name required */
        default:
        	g_assert_not_reached();
        }
    }
    return result;
}


gboolean
libbalsa_server_check_cert(NetClient           *client,
           	   	   	   GTlsCertificate     *peer_cert,
						   GTlsCertificateFlags errors,
						   gpointer             user_data)
{
	return libbalsa_is_cert_known(peer_cert, errors);
}


gchar *
libbalsa_server_get_cert_pass(NetClient *client,
			  	  	  	  	  gchar     *cert_subject,
							  gpointer   user_data)
{
    LibBalsaServer *server = LIBBALSA_SERVER(user_data);
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
    gchar *result;

    if ((priv->cert_passphrase != NULL) && (priv->cert_passphrase[0] != '\0')) {
        result = g_strdup(priv->cert_passphrase);
    } else {
        result = lbs_get_password(server, cert_subject);
    }
	return result;
}

/* Test whether a server is reachable */

typedef struct {
    LibBalsaCanReachCallback * cb;
    gpointer                   cb_data;
    GObject                  * source_object;
} CanReachInfo;

static void
libbalsa_server_can_reach_cb(GObject      * monitor,
                             GAsyncResult * res,
                             gpointer       user_data)
{
    CanReachInfo *info = user_data;
    gboolean can_reach;

    can_reach = g_network_monitor_can_reach_finish((GNetworkMonitor *) monitor, res, NULL);
    info->cb(info->source_object, can_reach, info->cb_data);

    g_object_unref(info->source_object);
    g_free(info);
}

void
libbalsa_server_test_can_reach_full(LibBalsaServer           * server,
                                    LibBalsaCanReachCallback * cb,
                                    gpointer                   cb_data,
                                    GObject                  * source_object)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);
    CanReachInfo *info;
    gchar *host;
    GNetworkMonitor *monitor;
    GSocketConnectable *address;

    info = g_new(CanReachInfo, 1);
    info->cb              = cb;
    info->cb_data         = cb_data;
    info->source_object = g_object_ref(source_object);

    monitor = g_network_monitor_get_default();

    host = net_client_host_only(priv->host);
    address = g_network_address_new(host, 0);
    g_free(host);
    g_network_monitor_can_reach_async(monitor, address, NULL,
                                      libbalsa_server_can_reach_cb, info);
    g_object_unref(address);
}

void
libbalsa_server_test_can_reach(LibBalsaServer           * server,
                               LibBalsaCanReachCallback * cb,
                               gpointer                   cb_data)
{
    libbalsa_server_test_can_reach_full(server, cb, cb_data, (GObject *) server);
}

/*
 * Getters
 */

const gchar *
libbalsa_server_get_host(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), NULL);

    return priv->host;
}

const gchar *
libbalsa_server_get_user(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), NULL);

    return priv->user;
}

const gchar *
libbalsa_server_get_cert_file(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), NULL);

    return priv->cert_file;
}

const gchar *
libbalsa_server_get_protocol(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), NULL);

    return priv->protocol;
}

const gchar *
libbalsa_server_get_password(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), NULL);

    return priv->passwd;
}

const gchar *
libbalsa_server_get_cert_passphrase(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), NULL);

    return priv->cert_passphrase;
}

NetClientCryptMode
libbalsa_server_get_security(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), (NetClientCryptMode) 0);

    return priv->security;
}

NetClientAuthMode
libbalsa_server_get_auth_mode(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), (NetClientAuthMode) 0);

    return priv->auth_mode;
}

gboolean
libbalsa_server_get_client_cert(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), FALSE);

    return priv->client_cert;
}

gboolean
libbalsa_server_get_remember_password(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), FALSE);

    return priv->remember_passwd;
}

gboolean
libbalsa_server_get_remember_cert_passphrase(LibBalsaServer *server)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_val_if_fail(LIBBALSA_IS_SERVER(server), FALSE);

    return priv->remember_cert_passphrase;
}

/*
 * Setters
 */

void
libbalsa_server_set_protocol(LibBalsaServer *server, const gchar *protocol)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_if_fail(LIBBALSA_IS_SERVER(server));
    g_return_if_fail(priv->protocol == NULL);

    /* We do not allocate a string for the protocol: */
    priv->protocol = protocol;
}

void
libbalsa_server_set_cert_file(LibBalsaServer *server, const gchar *cert_file)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    g_free(priv->cert_file);
    priv->cert_file = g_strdup(cert_file);
}

void
libbalsa_server_set_security(LibBalsaServer *server, NetClientCryptMode security)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    priv->security = security;
}

void
libbalsa_server_set_auth_mode(LibBalsaServer *server, NetClientAuthMode auth_mode)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    priv->auth_mode = auth_mode;
}

void
libbalsa_server_set_remember_password(LibBalsaServer *server, gboolean remember_password)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    priv->remember_passwd = remember_password;
}

void
libbalsa_server_set_client_cert(LibBalsaServer *server, gboolean client_cert)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    priv->client_cert = client_cert;
}

void
libbalsa_server_set_remember_cert_passphrase(LibBalsaServer *server, gboolean remember_cert_passphrase)
{
    LibBalsaServerPrivate *priv = libbalsa_server_get_instance_private(server);

    g_return_if_fail(LIBBALSA_IS_SERVER(server));

    priv->remember_cert_passphrase = remember_cert_passphrase;
}