5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2014 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "chat-protocols.h"
26 #include "servers-setup.h"
27 #include "channels-setup.h"
28 #include "silc-servers.h"
29 #include "silc-channels.h"
30 #include "silc-queries.h"
31 #include "silc-nicklist.h"
32 #include "silc-chatnets.h"
33 #include "silc-cmdqueue.h"
34 #include "silc-commands.h"
40 #include "fe-common/core/printtext.h"
41 #include "fe-common/core/fe-channels.h"
42 #include "fe-common/core/keyboard.h"
43 #include "fe-common/silc/module-formats.h"
46 static int init_failed = 0;
48 static char *opt_pkcs = NULL;
49 static int opt_bits = 0;
52 static int running = 0;
55 SilcClient silc_client = NULL;
56 extern SilcClientOperations ops;
59 SilcPublicKey irssi_pubkey = NULL;
60 SilcPrivateKey irssi_privkey = NULL;
62 char *opt_hostname = NULL;
64 /* Default hash function */
65 SilcHash sha1hash = NULL;
67 void silc_expandos_init(void);
68 void silc_expandos_deinit(void);
70 void silc_lag_init(void);
71 void silc_lag_deinit(void);
74 void silc_core_deinit(void);
77 static gboolean my_silc_scheduler(gpointer data)
79 SILC_LOG_DEBUG(("Timeout"));
80 silc_client_run_one(silc_client);
84 static gboolean my_silc_scheduler_fd(GIOChannel *source,
85 GIOCondition condition,
88 SILC_LOG_DEBUG(("I/O event, %d", SILC_PTR_TO_32(data)));
89 silc_client_run_one(silc_client);
93 static void scheduler_notify_cb(SilcSchedule schedule,
94 SilcBool added, SilcTask task,
95 SilcBool fd_task, SilcUInt32 fd,
97 long seconds, long useconds,
106 SILC_LOG_DEBUG(("Add fd %d, events %d", fd, event));
107 g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
109 if (event & SILC_TASK_READ)
110 e |= (G_IO_IN | G_IO_PRI | G_IO_HUP | G_IO_ERR);
111 if (event & SILC_TASK_WRITE)
112 e |= (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL);
115 ch = g_io_channel_unix_new(fd);
116 g_io_add_watch(ch, e, my_silc_scheduler_fd, SILC_32_TO_PTR(fd));
117 g_io_channel_unref(ch);
123 t = (seconds * 1000) + (useconds / 1000);
124 SILC_LOG_DEBUG(("interval %d msec", t));
125 g_timeout_add(t, my_silc_scheduler, NULL);
130 g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
135 static CHATNET_REC *create_chatnet(void)
137 return g_malloc0(sizeof(CHATNET_REC));
140 static SERVER_SETUP_REC *create_server_setup(void)
142 return g_malloc0(sizeof(SERVER_SETUP_REC));
145 static CHANNEL_SETUP_REC *create_channel_setup(void)
147 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
150 static SERVER_CONNECT_REC *create_server_connect(void)
152 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
155 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
160 /* Checks user information and saves them to the config file it they
161 do not exist there already. */
163 static void silc_init_userinfo(void)
165 const char *set, *nick, *user_name, *str;
168 /* check if nick/username/realname wasn't read from setup.. */
169 set = settings_get_str("real_name");
170 if (set == NULL || *set == '\0') {
171 str = g_getenv("SILCNAME");
173 str = g_getenv("IRCNAME");
174 settings_set_str("real_name",
175 str != NULL ? str : silc_get_real_name());
178 /* Check that real name is UTF-8 encoded */
179 set = settings_get_str("real_name");
180 if (!silc_utf8_valid(set, strlen(set))) {
181 int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
182 tmp = silc_calloc(len, sizeof(*tmp));
184 silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
185 settings_set_str("real_name", tmp);
191 user_name = settings_get_str("user_name");
192 if (user_name == NULL || *user_name == '\0') {
193 str = g_getenv("SILCUSER");
195 str = g_getenv("IRCUSER");
196 settings_set_str("user_name",
197 str != NULL ? str : silc_get_username());
199 user_name = settings_get_str("user_name");
203 nick = settings_get_str("nick");
204 if (nick == NULL || *nick == '\0') {
205 str = g_getenv("SILCNICK");
207 str = g_getenv("IRCNICK");
208 settings_set_str("nick", str != NULL ? str : user_name);
210 nick = settings_get_str("nick");
214 set = settings_get_str("alternate_nick");
215 if (set == NULL || *set == '\0') {
216 tmp = g_strconcat(nick, "_", NULL);
217 settings_set_str("alternate_nick", tmp);
222 set = settings_get_str("hostname");
223 if (set == NULL || *set == '\0') {
224 str = g_getenv("SILCHOST");
226 str = g_getenv("IRCHOST");
228 settings_set_str("hostname", str);
232 #if defined(SILC_DEBUG) || defined(SILC_PLUGIN)
237 static SilcBool silc_irssi_debug_print(char *file, char *function, int line,
238 char *message, void *context)
240 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
241 "DEBUG: %s:%d: %s", function, line, message);
246 static void silc_sig_setup_changed(void)
249 bool debug = settings_get_bool("debug");
251 const char *debug_string = settings_get_str("debug_string");
253 silc_log_debug(TRUE);
254 if (strlen(debug_string))
255 silc_log_set_debug_string(debug_string);
256 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
260 silc_log_debug(FALSE);
266 static SilcBool silc_log_misc(SilcLogType type, char *message, void *context)
268 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
269 (type == SILC_LOG_INFO ? "[Info]" :
270 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
274 static SilcBool silc_log_stderr(SilcLogType type, char *message, void *context)
276 fprintf(stderr, "%s: %s\n",
277 (type == SILC_LOG_INFO ? "[Info]" :
278 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
282 static void silc_register_cipher(SilcClient client, const char *cipher)
287 for (i = 0; silc_default_ciphers[i].name; i++)
288 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
289 silc_cipher_register(&(silc_default_ciphers[i]));
293 if (!silc_cipher_is_supported(cipher)) {
294 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
304 /* Register other defaults */
305 silc_cipher_register_default();
308 static void silc_register_hash(SilcClient client, const char *hash)
313 for (i = 0; silc_default_hash[i].name; i++)
314 if (!strcmp(silc_default_hash[i].name, hash)) {
315 silc_hash_register(&(silc_default_hash[i]));
319 if (!silc_hash_is_supported(hash)) {
320 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
330 /* Register other defaults */
331 silc_hash_register_default();
334 static void silc_register_hmac(SilcClient client, const char *hmac)
339 for (i = 0; silc_default_hmacs[i].name; i++)
340 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
341 silc_hmac_register(&(silc_default_hmacs[i]));
345 if (!silc_hmac_is_supported(hmac)) {
346 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
356 /* Register other defaults */
357 silc_hmac_register_default();
360 /* Finalize init. Init finish signal calls this. */
363 void silc_opt_callback(const char *data, SERVER_REC *server,
366 unsigned char **argv=NULL, *tmp;
367 SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
369 unsigned char privkey[128], pubkey[128];
371 memset(privkey, 0, sizeof(privkey));
372 memset(pubkey, 0, sizeof(pubkey));
373 snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
374 SILC_CLIENT_PUBLIC_KEY_NAME);
375 snprintf(privkey, sizeof(privkey) - 1, "%s/%s", get_irssi_dir(),
376 SILC_CLIENT_PRIVATE_KEY_NAME);
378 tmp = g_strconcat("SILC", " ", data, NULL);
379 silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
385 if ((argc == 2) && (strcasecmp(argv[1], "list-ciphers") == 0)) {
386 silc_client_list_ciphers();
390 if ((argc == 2) && (strcasecmp(argv[1], "list-hash-funcs") == 0)) {
391 silc_client_list_hash_funcs();
395 if ((argc == 2) && (strcasecmp(argv[1], "list-hmacs") == 0)) {
396 silc_client_list_hmacs();
400 if ((argc == 2) && (strcasecmp(argv[1], "list-pkcs") == 0)) {
401 silc_client_list_pkcs();
405 if ((argc < 5) && (strcasecmp(argv[1], "debug") == 0)) {
407 printformat_module("fe-common/silc", NULL, NULL,
408 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
409 (i_debug == TRUE ? "enabled" : "disabled"));
413 printformat_module("fe-common/silc", NULL, NULL,
414 MSGLEVEL_CRAP, SILCTXT_CONFIG_NODEBUG);
416 if (strcasecmp(argv[2], "on") == 0) {
417 settings_set_bool("debug", TRUE);
419 settings_set_str("debug_string", argv[3]);
420 } else if ((argc == 3) && (strcasecmp(argv[2], "off") == 0)) {
421 settings_set_bool("debug", FALSE);
424 silc_sig_setup_changed();
425 printformat_module("fe-common/silc", NULL, NULL,
426 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
427 (settings_get_bool("debug") == TRUE ?
428 "enabled" : "disabled"));
433 if (strcasecmp(argv[1], "create-key-pair") == 0) {
434 /* Create new key pair and exit */
435 char *endptr, *pkcs=NULL;
440 if ((argc == 3) || (argc == 5))
443 for (i=2; i<argc-1; i+=2)
444 if (strcasecmp(argv[i], "-pkcs") == 0) {
449 } else if (strcasecmp(argv[i], "-bits") == 0) {
451 val = strtol(argv[i+1], &endptr, 10);
452 if ((*endptr != '\0') || (val <= 0) || (val >= INT_MAX))
460 rec = g_new0(CREATE_KEY_REC, 1);
461 rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
464 keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
465 format_get_text("fe-common/silc", NULL, NULL,
466 NULL, SILCTXT_CONFIG_PASS_ASK2),
467 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
468 printformat_module("fe-common/silc", NULL, NULL,
469 MSGLEVEL_CRAP, SILCTXT_CONFIG_NEXTTIME);
473 if ((argc < 4) && (strcasecmp(argv[1], "passphrase-change") == 0)) {
474 /* Change the passphrase of the private key file */
477 rec = g_new0(CREATE_KEY_REC, 1);
478 rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
480 keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
481 format_get_text("fe-common/silc", NULL, NULL,
482 NULL, SILCTXT_CONFIG_PASS_ASK1),
483 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
488 printformat_module("fe-common/silc", NULL, NULL,
489 MSGLEVEL_CRAP, SILCTXT_CONFIG_UNKNOWN,
493 for (i=0; i<argc; i++)
497 silc_free(argv_lens);
498 silc_free(argv_types);
500 #endif /* SILC_PLUGIN */
502 /* Called to indicate the client library has stopped. */
504 silc_stopped(SilcClient client, void *context)
506 SILC_LOG_DEBUG(("Client library has stopped"));
510 /* Called to indicate the client library is running. */
513 silc_running(SilcClient client, void *context)
516 SILC_LOG_DEBUG(("Client library is running"));
519 static void sig_init_finished(void)
521 /* Check ~/.silc directory and public and private keys */
522 if (!silc_client_check_silc_dir()) {
532 /* Load public and private key */
533 if (!silc_client_load_keys(silc_client)) {
543 /* Initialize the SILC client */
544 opt_hostname = (char *)settings_get_str("hostname");
545 if (!opt_hostname || *opt_hostname == '\0')
546 opt_hostname = silc_net_localhost();
547 if (!silc_client_init(silc_client, settings_get_str("user_name"),
548 opt_hostname, settings_get_str("real_name"),
549 silc_running, NULL)) {
559 silc_schedule_set_notify(silc_client->schedule, scheduler_notify_cb, NULL);
561 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
562 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
563 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
564 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
566 silc_hash_alloc("sha1", &sha1hash);
568 /* Run SILC scheduler */
569 my_silc_scheduler(NULL);
573 static gboolean list_ciphers(const gchar *option_name, const gchar *value,
574 gpointer data, GError **error)
576 silc_cipher_register_default();
577 silc_client_list_ciphers();
582 static gboolean list_hash_funcs(const gchar *option_name, const gchar *value,
583 gpointer data, GError **error)
585 silc_hash_register_default();
586 silc_client_list_hash_funcs();
591 static gboolean list_hmacs(const gchar *option_name, const gchar *value,
592 gpointer data, GError **error)
594 silc_hmac_register_default();
595 silc_client_list_hmacs();
600 static gboolean list_pkcs(const gchar *option_name, const gchar *value,
601 gpointer data, GError **error)
603 silc_pkcs_register_default();
604 silc_client_list_pkcs();
610 static gboolean enable_debug(const gchar *option_name, const gchar *value,
611 gpointer data, GError **error)
613 /* Set debug string setting but don't enable it. User must be
614 /set enable on to get the debugs. */
615 settings_set_str("debug_string", (char *)value);
617 /* Default debugging when -d was given in command line */
618 silc_log_debug(TRUE);
619 silc_log_debug_hexdump(TRUE);
620 silc_log_set_debug_string((char *)value);
624 static gboolean enable_debug(const gchar *option_name, const gchar *value,
625 gpointer data, GError **error)
628 "Run-time debugging is not enabled. To enable it recompile\n"
629 "the client with --enable-debug configuration option.\n");
633 #endif /* SILC_DEBUG */
635 static gboolean create_keypair(const gchar *option_name, const gchar *value,
636 gpointer data, GError **error)
638 silc_cipher_register_default();
639 silc_pkcs_register_default();
640 silc_hash_register_default();
641 silc_hmac_register_default();
642 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL, NULL, NULL,
648 static gboolean change_passphrase(const gchar *option_name, const gchar *value,
649 gpointer data, GError **error)
651 silc_cipher_register_default();
652 silc_pkcs_register_default();
653 silc_hash_register_default();
654 silc_hmac_register_default();
655 silc_change_private_key_passphrase((char *)value, NULL, NULL);
660 static gboolean show_key(const gchar *option_name, const gchar *value,
661 gpointer data, GError **error)
663 silc_cipher_register_default();
664 silc_pkcs_register_default();
665 silc_hash_register_default();
666 silc_hmac_register_default();
667 silc_show_public_key_file((char *)value);
671 #endif /* !SILC_PLUGIN */
673 /* Init SILC. Called from src/fe-text/silc.c */
675 void silc_core_init(void)
678 static GOptionEntry silc_options[] = {
679 { "list-ciphers", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
680 list_ciphers, "List supported ciphers", NULL },
681 { "list-hash-funcs", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
682 list_hash_funcs, "List supported hash functions", NULL },
683 { "list-hmacs", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, list_hmacs,
684 "List supported HMACs", NULL },
685 { "list-pkcs", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, list_pkcs,
686 "List supported PKCSs", NULL },
687 { "debug", 'd', 0, G_OPTION_ARG_CALLBACK, enable_debug,
688 "Enable debugging", NULL },
689 { "create-key-pair", 'C', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
690 create_keypair, "Create new public key pair", NULL },
691 { "pkcs", 0, 0, G_OPTION_ARG_STRING, &opt_pkcs,
692 "Set the PKCS of the public key pair (-C)", "PKCS" },
693 { "bits", 0, 0, G_OPTION_ARG_INT, &opt_bits,
694 "Set the length of the public key pair (-C)", "VALUE" },
695 { "passphrase-change", 'P', 0, G_OPTION_ARG_CALLBACK, change_passphrase,
696 "Change the passphrase of private key file", "FILE" },
697 { "show-key", 'S', 0, G_OPTION_ARG_CALLBACK, show_key,
698 "Show the contents of the public key", "FILE" },
703 CHAT_PROTOCOL_REC *rec;
704 SilcClientParams params;
705 const char *def_cipher, *def_hash, *def_hmac;
708 args_register(silc_options);
713 settings_add_bool("server", "skip_motd", FALSE);
714 settings_add_str("server", "alternate_nick", NULL);
716 settings_add_bool("server", "use_auto_addr", FALSE);
717 settings_add_str("server", "auto_bind_ip", "");
718 settings_add_str("server", "auto_public_ip", "");
719 settings_add_int("server", "auto_bind_port", 0);
720 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
721 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
722 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
723 settings_add_int("server", "key_exchange_timeout_secs", 120);
724 settings_add_int("server", "key_exchange_rekey_secs", 3600);
725 settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
726 settings_add_int("server", "heartbeat", 300);
727 settings_add_bool("server", "ignore_message_signatures", FALSE);
728 settings_add_str("server", "session_filename", "session.$chatnet");
729 settings_add_bool("server", "sign_channel_messages", FALSE);
730 settings_add_bool("server", "sign_private_messages", FALSE);
731 settings_add_str("silc", "nickname_format", "%n#%a");
733 /* Requested Attributes settings */
734 settings_add_bool("silc", "attr_allow", TRUE);
735 settings_add_str("silc", "attr_vcard", "");
736 settings_add_str("silc", "attr_services", "");
737 settings_add_str("silc", "attr_status_mood", "NORMAL");
738 settings_add_str("silc", "attr_status_text", "");
739 settings_add_str("silc", "attr_status_message", NULL);
740 settings_add_str("silc", "attr_preferred_language", "");
741 settings_add_str("silc", "attr_preferred_contact", "CHAT");
742 settings_add_bool("silc", "attr_timezone", TRUE);
743 settings_add_str("silc", "attr_geolocation", "");
744 settings_add_str("silc", "attr_device_info", NULL);
745 settings_add_str("silc", "attr_public_keys", "");
748 settings_add_bool("debug", "debug", FALSE);
749 settings_add_str("debug", "debug_string", "");
752 signal_add("setup changed", (SIGNAL_FUNC) silc_sig_setup_changed);
754 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
755 #endif /* !SILC_PLUGIN */
757 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
758 if (settings_get_bool("debug") == TRUE)
759 silc_sig_setup_changed();
762 silc_init_userinfo();
764 silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
765 silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
766 silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
767 silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
769 /* Initialize client parameters */
770 memset(¶ms, 0, sizeof(params));
771 strcat(params.nickname_format, settings_get_str("nickname_format"));
772 params.full_channel_names = TRUE;
774 /* Allocate SILC client */
775 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
777 /* Get the ciphers and stuff from config file */
778 def_cipher = settings_get_str("crypto_default_cipher");
779 def_hash = settings_get_str("crypto_default_hash");
780 def_hmac = settings_get_str("crypto_default_hmac");
781 silc_register_cipher(silc_client, def_cipher);
786 silc_register_hash(silc_client, def_hash);
791 silc_register_hmac(silc_client, def_hmac);
796 silc_pkcs_register_default();
799 command_bind("silc", MODULE_NAME, (SIGNAL_FUNC) silc_opt_callback);
802 /* Register SILC to the irssi */
803 rec = g_new0(CHAT_PROTOCOL_REC, 1);
805 rec->fullname = "Secure Internet Live Conferencing";
806 rec->chatnet = "silcnet";
807 rec->create_chatnet = create_chatnet;
808 rec->create_server_setup = create_server_setup;
809 rec->create_channel_setup = create_channel_setup;
810 rec->create_server_connect = create_server_connect;
811 rec->destroy_server_connect = destroy_server_connect;
812 rec->server_init_connect = silc_server_init_connect;
813 rec->server_connect = silc_server_connect;
814 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
817 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
820 chat_protocol_register(rec);
825 silc_channels_init();
827 silc_expandos_init();
829 silc_chatnets_init();
839 module_register("silc", "core");
842 /* Deinit SILC. Called from src/fe-text/silc.c */
844 void silc_core_deinit(void)
847 volatile int stopped = 0;
848 silc_client_stop(silc_client, silc_stopped, (void *)&stopped);
850 silc_client_run_one(silc_client);
853 signal_remove("setup changed", (SIGNAL_FUNC) silc_sig_setup_changed);
855 command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
857 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
860 signal_emit("chat protocol deinit", 1, chat_protocol_find("SILC"));
862 silc_hash_free(sha1hash);
865 silc_server_deinit();
866 silc_channels_deinit();
867 silc_queries_deinit();
868 silc_expandos_deinit();
870 silc_chatnets_deinit();
872 chat_protocol_unregister("SILC");
875 silc_pkcs_public_key_free(irssi_pubkey);
877 silc_pkcs_private_key_free(irssi_privkey);
878 silc_client_free(silc_client);