5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2007 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 /* Command line option variables */
47 static char *opt_pkcs = NULL;
48 static int opt_bits = 0;
50 static int init_failed = 0;
53 static int idletag = -1;
56 SilcClient silc_client = NULL;
57 extern SilcClientOperations ops;
60 SilcPublicKey irssi_pubkey = NULL;
61 SilcPrivateKey irssi_privkey = NULL;
63 char *opt_nickname = NULL;
64 char *opt_hostname = NULL;
66 /* Default hash function */
67 SilcHash sha1hash = NULL;
69 void silc_expandos_init(void);
70 void silc_expandos_deinit(void);
72 void silc_lag_init(void);
73 void silc_lag_deinit(void);
76 void silc_core_deinit(void);
79 static int my_silc_scheduler(void)
81 silc_client_run_one(silc_client);
85 static CHATNET_REC *create_chatnet(void)
87 return g_malloc0(sizeof(CHATNET_REC));
90 static SERVER_SETUP_REC *create_server_setup(void)
92 return g_malloc0(sizeof(SERVER_SETUP_REC));
95 static CHANNEL_SETUP_REC *create_channel_setup(void)
97 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
100 static SERVER_CONNECT_REC *create_server_connect(void)
102 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
105 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
110 /* Checks user information and saves them to the config file it they
111 do not exist there already. */
113 static void silc_init_userinfo(void)
115 const char *set, *nick, *user_name, *str;
118 /* check if nick/username/realname wasn't read from setup.. */
119 set = settings_get_str("real_name");
120 if (set == NULL || *set == '\0') {
121 str = g_getenv("SILCNAME");
123 str = g_getenv("IRCNAME");
124 settings_set_str("real_name",
125 str != NULL ? str : silc_get_real_name());
128 /* Check that real name is UTF-8 encoded */
129 set = settings_get_str("real_name");
130 if (!silc_utf8_valid(set, strlen(set))) {
131 int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
132 tmp = silc_calloc(len, sizeof(*tmp));
134 silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
135 settings_set_str("real_name", tmp);
141 user_name = settings_get_str("user_name");
142 if (user_name == NULL || *user_name == '\0') {
143 str = g_getenv("SILCUSER");
145 str = g_getenv("IRCUSER");
146 settings_set_str("user_name",
147 str != NULL ? str : silc_get_username());
149 user_name = settings_get_str("user_name");
153 nick = settings_get_str("nick");
154 if (nick == NULL || *nick == '\0') {
155 str = g_getenv("SILCNICK");
157 str = g_getenv("IRCNICK");
158 settings_set_str("nick", str != NULL ? str : user_name);
160 nick = settings_get_str("nick");
164 set = settings_get_str("alternate_nick");
165 if (set == NULL || *set == '\0') {
166 tmp = g_strconcat(nick, "_", NULL);
167 settings_set_str("alternate_nick", tmp);
172 set = settings_get_str("hostname");
173 if (set == NULL || *set == '\0') {
174 str = g_getenv("SILCHOST");
176 str = g_getenv("IRCHOST");
178 settings_set_str("hostname", str);
182 #if defined(SILC_DEBUG) || defined(SILC_PLUGIN)
187 static bool silc_irssi_debug_print(char *file, char *function, int line,
188 char *message, void *context)
190 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
191 "DEBUG: %s:%d: %s", function, line, message);
196 static void sig_setup_changed(void)
199 bool debug = settings_get_bool("debug");
201 const char *debug_string = settings_get_str("debug_string");
203 silc_log_debug(TRUE);
204 if (strlen(debug_string))
205 silc_log_set_debug_string(debug_string);
206 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
210 silc_log_debug(FALSE);
216 static bool silc_log_misc(SilcLogType type, char *message, void *context)
218 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
219 (type == SILC_LOG_INFO ? "[Info]" :
220 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
224 static bool silc_log_stderr(SilcLogType type, char *message, void *context)
226 fprintf(stderr, "%s: %s\n",
227 (type == SILC_LOG_INFO ? "[Info]" :
228 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
232 static void silc_register_cipher(SilcClient client, const char *cipher)
237 for (i = 0; silc_default_ciphers[i].name; i++)
238 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
239 silc_cipher_register(&(silc_default_ciphers[i]));
243 if (!silc_cipher_is_supported(cipher)) {
244 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
254 /* Register other defaults */
255 silc_cipher_register_default();
258 static void silc_register_hash(SilcClient client, const char *hash)
263 for (i = 0; silc_default_hash[i].name; i++)
264 if (!strcmp(silc_default_hash[i].name, hash)) {
265 silc_hash_register(&(silc_default_hash[i]));
269 if (!silc_hash_is_supported(hash)) {
270 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
280 /* Register other defaults */
281 silc_hash_register_default();
284 static void silc_register_hmac(SilcClient client, const char *hmac)
289 for (i = 0; silc_default_hmacs[i].name; i++)
290 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
291 silc_hmac_register(&(silc_default_hmacs[i]));
295 if (!silc_hmac_is_supported(hmac)) {
296 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
306 /* Register other defaults */
307 silc_hmac_register_default();
310 /* Finalize init. Init finish signal calls this. */
313 #define FUNCTION_EXIT goto out
314 void silc_opt_callback(const char *data, SERVER_REC *server,
317 #define FUNCTION_EXIT exit(0)
318 void silc_opt_callback(poptContext con,
319 enum poptCallbackReason reason,
320 const struct poptOption *opt,
321 const char *arg, void *data)
325 unsigned char **argv=NULL, *tmp;
326 SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
328 unsigned char privkey[128], pubkey[128];
330 memset(privkey, 0, sizeof(privkey));
331 memset(pubkey, 0, sizeof(pubkey));
332 snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
333 SILC_CLIENT_PUBLIC_KEY_NAME);
334 snprintf(privkey, sizeof(privkey) - 1, "%s/%s", get_irssi_dir(),
335 SILC_CLIENT_PRIVATE_KEY_NAME);
337 tmp = g_strconcat("SILC", " ", data, NULL);
338 silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
344 if (strcmp(opt->longName, "nick") == 0) {
345 g_free(opt_nickname);
346 opt_nickname = g_strdup(arg);
349 if (strcmp(opt->longName, "hostname") == 0) {
350 silc_free(opt_hostname);
351 opt_hostname = strdup(arg);
356 if ((argc == 2) && (strcasecmp(argv[1], "list-ciphers") == 0)) {
358 if (strcmp(opt->longName, "list-ciphers") == 0) {
359 silc_cipher_register_default();
361 silc_client_list_ciphers();
366 if ((argc == 2) && (strcasecmp(argv[1], "list-hash-funcs") == 0)) {
368 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
369 silc_hash_register_default();
371 silc_client_list_hash_funcs();
376 if ((argc == 2) && (strcasecmp(argv[1], "list-hmacs") == 0)) {
378 if (strcmp(opt->longName, "list-hmacs") == 0) {
379 silc_hmac_register_default();
381 silc_client_list_hmacs();
386 if ((argc == 2) && (strcasecmp(argv[1], "list-pkcs") == 0)) {
388 if (strcmp(opt->longName, "list-pkcs") == 0) {
389 silc_pkcs_register_default();
391 silc_client_list_pkcs();
396 if ((argc < 5) && (strcasecmp(argv[1], "debug") == 0)) {
398 if (strcmp(opt->longName, "debug") == 0) {
399 silc_log_debug(TRUE);
400 silc_log_debug_hexdump(TRUE);
401 silc_log_set_debug_string(arg);
405 printformat_module("fe-common/silc", NULL, NULL,
406 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
407 (i_debug == TRUE ? "enabled" : "disabled"));
413 printformat_module("fe-common/silc", NULL, NULL,
414 MSGLEVEL_CRAP, SILCTXT_CONFIG_NODEBUG);
417 "Run-time debugging is not enabled. To enable it recompile\n"
418 "the client with --enable-debug configuration option.\n");
423 if (strcasecmp(argv[2], "on") == 0) {
424 settings_set_bool("debug", TRUE);
426 settings_set_str("debug_string", argv[3]);
427 } else if ((argc == 3) && (strcasecmp(argv[2], "off") == 0)) {
428 settings_set_bool("debug", FALSE);
432 printformat_module("fe-common/silc", NULL, NULL,
433 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
434 (settings_get_bool("debug") == TRUE ?
435 "enabled" : "disabled"));
442 if (strcasecmp(argv[1], "create-key-pair") == 0) {
444 if (strcmp(opt->longName, "create-key-pair") == 0) {
446 /* Create new key pair and exit */
448 char *endptr, *pkcs=NULL;
453 if ((argc == 3) || (argc == 5))
456 for (i=2; i<argc-1; i+=2)
457 if (strcasecmp(argv[i], "-pkcs") == 0) {
462 } else if (strcasecmp(argv[i], "-bits") == 0) {
464 val = strtol(argv[i+1], &endptr, 10);
465 if ((*endptr != '\0') || (val <= 0) || (val >= INT_MAX))
473 rec = g_new0(CREATE_KEY_REC, 1);
474 rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
477 keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
478 format_get_text("fe-common/silc", NULL, NULL,
479 NULL, SILCTXT_CONFIG_PASS_ASK2),
480 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
481 printformat_module("fe-common/silc", NULL, NULL,
482 MSGLEVEL_CRAP, SILCTXT_CONFIG_NEXTTIME);
485 silc_cipher_register_default();
486 silc_pkcs_register_default();
487 silc_hash_register_default();
488 silc_hmac_register_default();
489 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
490 NULL, NULL, NULL, NULL, TRUE);
496 if ((argc < 4) && (strcasecmp(argv[1], "passphrase-change") == 0)) {
498 if (strcmp(opt->longName, "passphrase-change") == 0) {
500 /* Change the passphrase of the private key file */
504 rec = g_new0(CREATE_KEY_REC, 1);
505 rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
507 keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
508 format_get_text("fe-common/silc", NULL, NULL,
509 NULL, SILCTXT_CONFIG_PASS_ASK1),
510 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
513 silc_cipher_register_default();
514 silc_pkcs_register_default();
515 silc_hash_register_default();
516 silc_hmac_register_default();
517 silc_change_private_key_passphrase(arg, NULL, NULL);
523 if (strcmp(opt->longName, "show-key") == 0) {
525 silc_cipher_register_default();
526 silc_pkcs_register_default();
527 silc_hash_register_default();
528 silc_hmac_register_default();
529 silc_show_public_key_file((char *)arg);
536 printformat_module("fe-common/silc", NULL, NULL,
537 MSGLEVEL_CRAP, SILCTXT_CONFIG_UNKNOWN,
541 for (i=0; i<argc; i++)
545 silc_free(argv_lens);
546 silc_free(argv_types);
551 /* Called to indicate the client library has stopped. */
554 silc_stopped(SilcClient client, void *context)
556 SILC_LOG_DEBUG(("Client library has stopped"));
558 g_source_remove(idletag);
559 signal_emit("chat protocol deinit", 1,
560 chat_protocol_find("SILC"));
563 static void sig_gui_quit(SILC_SERVER_REC *server, const char *msg)
565 silc_client_stop(silc_client, silc_stopped, NULL);
568 /* Called to indicate the client library is running. */
571 silc_running(SilcClient client, void *context)
573 SILC_LOG_DEBUG(("Client library is running"));
576 static void sig_init_finished(void)
578 /* Check ~/.silc directory and public and private keys */
579 if (!silc_client_check_silc_dir()) {
589 /* Load public and private key */
590 if (!silc_client_load_keys(silc_client)) {
600 /* Initialize the SILC client */
601 opt_hostname = (opt_hostname ? opt_hostname : silc_net_localhost());
602 if (!silc_client_init(silc_client, settings_get_str("user_name"),
603 opt_hostname, settings_get_str("real_name"),
604 silc_running, NULL)) {
614 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
615 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
616 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
617 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
619 silc_hash_alloc("sha1", &sha1hash);
621 /* register SILC scheduler */
622 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
625 /* Init SILC. Called from src/fe-text/silc.c */
627 void silc_core_init(void)
630 static struct poptOption silc_options[] = {
631 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
632 { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
633 "List supported ciphers", NULL },
634 { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
635 "List supported hash functions", NULL },
636 { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
637 "List supported HMACs", NULL },
638 { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
639 "List supported PKCSs", NULL },
641 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
642 "Enable debugging", "STRING" },
643 #endif /* SILC_DEBUG */
644 { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
645 "Create new public key pair", NULL },
646 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
647 "Set the PKCS of the public key pair (-C)", "PKCS" },
648 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
649 "Set the length of the public key pair (-C)", "VALUE" },
650 { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
651 "Change the passphrase of private key file", "FILE" },
652 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
653 "Show the contents of the public key", "FILE" },
654 { NULL, '\0', 0, NULL }
658 CHAT_PROTOCOL_REC *rec;
659 SilcClientParams params;
660 const char *def_cipher, *def_hash, *def_hmac;
663 args_register(silc_options);
668 settings_add_bool("server", "skip_motd", FALSE);
669 settings_add_str("server", "alternate_nick", NULL);
671 settings_add_bool("server", "use_auto_addr", FALSE);
672 settings_add_str("server", "auto_bind_ip", "");
673 settings_add_str("server", "auto_public_ip", "");
674 settings_add_int("server", "auto_bind_port", 0);
675 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
676 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
677 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
678 settings_add_int("server", "key_exchange_timeout_secs", 120);
679 settings_add_int("server", "key_exchange_rekey_secs", 3600);
680 settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
681 settings_add_int("server", "heartbeat", 300);
682 settings_add_bool("server", "ignore_message_signatures", FALSE);
683 settings_add_str("server", "session_filename", "session.$chatnet");
684 settings_add_bool("server", "sign_channel_messages", FALSE);
685 settings_add_bool("server", "sign_private_messages", FALSE);
686 settings_add_str("silc", "nickname_format", "%n#%a");
688 /* Requested Attributes settings */
689 settings_add_bool("silc", "attr_allow", TRUE);
690 settings_add_str("silc", "attr_vcard", "");
691 settings_add_str("silc", "attr_services", "");
692 settings_add_str("silc", "attr_status_mood", "NORMAL");
693 settings_add_str("silc", "attr_status_text", "");
694 settings_add_str("silc", "attr_status_message", NULL);
695 settings_add_str("silc", "attr_preferred_language", "");
696 settings_add_str("silc", "attr_preferred_contact", "CHAT");
697 settings_add_bool("silc", "attr_timezone", TRUE);
698 settings_add_str("silc", "attr_geolocation", "");
699 settings_add_str("silc", "attr_device_info", NULL);
700 settings_add_str("silc", "attr_public_keys", "");
703 settings_add_bool("debug", "debug", FALSE);
704 settings_add_str("debug", "debug_string", "");
707 signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
709 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
711 signal_add("gui exit", (SIGNAL_FUNC) sig_gui_quit);
713 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
714 if (settings_get_bool("debug") == TRUE)
718 silc_init_userinfo();
720 silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
721 silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
722 silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
723 silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
725 /* Initialize client parameters */
726 memset(¶ms, 0, sizeof(params));
727 strcat(params.nickname_format, settings_get_str("nickname_format"));
729 /* Allocate SILC client */
730 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
732 /* Get the ciphers and stuff from config file */
733 def_cipher = settings_get_str("crypto_default_cipher");
734 def_hash = settings_get_str("crypto_default_hash");
735 def_hmac = settings_get_str("crypto_default_hmac");
736 silc_register_cipher(silc_client, def_cipher);
741 silc_register_hash(silc_client, def_hash);
746 silc_register_hmac(silc_client, def_hmac);
751 silc_pkcs_register_default();
754 command_bind("silc", MODULE_NAME, (SIGNAL_FUNC) silc_opt_callback);
757 /* Register SILC to the irssi */
758 rec = g_new0(CHAT_PROTOCOL_REC, 1);
760 rec->fullname = "Secure Internet Live Conferencing";
761 rec->chatnet = "silcnet";
762 rec->create_chatnet = create_chatnet;
763 rec->create_server_setup = create_server_setup;
764 rec->create_channel_setup = create_channel_setup;
765 rec->create_server_connect = create_server_connect;
766 rec->destroy_server_connect = destroy_server_connect;
767 rec->server_init_connect = silc_server_init_connect;
768 rec->server_connect = silc_server_connect;
769 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
772 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
775 chat_protocol_register(rec);
780 silc_channels_init();
782 silc_expandos_init();
784 silc_chatnets_init();
794 module_register("silc", "core");
797 /* Deinit SILC. Called from src/fe-text/silc.c */
799 void silc_core_deinit(void)
802 g_source_remove(idletag);
805 silc_free(opt_hostname);
807 g_free(opt_nickname);
809 signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
810 signal_remove("gui exit", (SIGNAL_FUNC) sig_gui_quit);
812 command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
814 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
817 silc_hash_free(sha1hash);
820 silc_server_deinit();
821 silc_channels_deinit();
822 silc_queries_deinit();
823 silc_expandos_deinit();
825 silc_chatnets_deinit();
827 chat_protocol_unregister("SILC");
829 silc_pkcs_public_key_free(irssi_pubkey);
830 silc_pkcs_private_key_free(irssi_privkey);
831 silc_client_free(silc_client);