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;
54 static int running = 0;
57 SilcClient silc_client = NULL;
58 extern SilcClientOperations ops;
61 SilcPublicKey irssi_pubkey = NULL;
62 SilcPrivateKey irssi_privkey = NULL;
64 char *opt_nickname = NULL;
65 char *opt_hostname = NULL;
67 /* Default hash function */
68 SilcHash sha1hash = NULL;
70 void silc_expandos_init(void);
71 void silc_expandos_deinit(void);
73 void silc_lag_init(void);
74 void silc_lag_deinit(void);
77 void silc_core_deinit(void);
80 static int my_silc_scheduler(void)
82 silc_client_run_one(silc_client);
86 static CHATNET_REC *create_chatnet(void)
88 return g_malloc0(sizeof(CHATNET_REC));
91 static SERVER_SETUP_REC *create_server_setup(void)
93 return g_malloc0(sizeof(SERVER_SETUP_REC));
96 static CHANNEL_SETUP_REC *create_channel_setup(void)
98 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
101 static SERVER_CONNECT_REC *create_server_connect(void)
103 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
106 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
111 /* Checks user information and saves them to the config file it they
112 do not exist there already. */
114 static void silc_init_userinfo(void)
116 const char *set, *nick, *user_name, *str;
119 /* check if nick/username/realname wasn't read from setup.. */
120 set = settings_get_str("real_name");
121 if (set == NULL || *set == '\0') {
122 str = g_getenv("SILCNAME");
124 str = g_getenv("IRCNAME");
125 settings_set_str("real_name",
126 str != NULL ? str : silc_get_real_name());
129 /* Check that real name is UTF-8 encoded */
130 set = settings_get_str("real_name");
131 if (!silc_utf8_valid(set, strlen(set))) {
132 int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
133 tmp = silc_calloc(len, sizeof(*tmp));
135 silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
136 settings_set_str("real_name", tmp);
142 user_name = settings_get_str("user_name");
143 if (user_name == NULL || *user_name == '\0') {
144 str = g_getenv("SILCUSER");
146 str = g_getenv("IRCUSER");
147 settings_set_str("user_name",
148 str != NULL ? str : silc_get_username());
150 user_name = settings_get_str("user_name");
154 nick = settings_get_str("nick");
155 if (nick == NULL || *nick == '\0') {
156 str = g_getenv("SILCNICK");
158 str = g_getenv("IRCNICK");
159 settings_set_str("nick", str != NULL ? str : user_name);
161 nick = settings_get_str("nick");
165 set = settings_get_str("alternate_nick");
166 if (set == NULL || *set == '\0') {
167 tmp = g_strconcat(nick, "_", NULL);
168 settings_set_str("alternate_nick", tmp);
173 set = settings_get_str("hostname");
174 if (set == NULL || *set == '\0') {
175 str = g_getenv("SILCHOST");
177 str = g_getenv("IRCHOST");
179 settings_set_str("hostname", str);
183 #if defined(SILC_DEBUG) || defined(SILC_PLUGIN)
188 static bool silc_irssi_debug_print(char *file, char *function, int line,
189 char *message, void *context)
191 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
192 "DEBUG: %s:%d: %s", function, line, message);
197 static void sig_setup_changed(void)
200 bool debug = settings_get_bool("debug");
202 const char *debug_string = settings_get_str("debug_string");
204 silc_log_debug(TRUE);
205 if (strlen(debug_string))
206 silc_log_set_debug_string(debug_string);
207 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
211 silc_log_debug(FALSE);
217 static bool silc_log_misc(SilcLogType type, char *message, void *context)
219 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
220 (type == SILC_LOG_INFO ? "[Info]" :
221 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
225 static bool silc_log_stderr(SilcLogType type, char *message, void *context)
227 fprintf(stderr, "%s: %s\n",
228 (type == SILC_LOG_INFO ? "[Info]" :
229 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
233 static void silc_register_cipher(SilcClient client, const char *cipher)
238 for (i = 0; silc_default_ciphers[i].name; i++)
239 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
240 silc_cipher_register(&(silc_default_ciphers[i]));
244 if (!silc_cipher_is_supported(cipher)) {
245 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
255 /* Register other defaults */
256 silc_cipher_register_default();
259 static void silc_register_hash(SilcClient client, const char *hash)
264 for (i = 0; silc_default_hash[i].name; i++)
265 if (!strcmp(silc_default_hash[i].name, hash)) {
266 silc_hash_register(&(silc_default_hash[i]));
270 if (!silc_hash_is_supported(hash)) {
271 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
281 /* Register other defaults */
282 silc_hash_register_default();
285 static void silc_register_hmac(SilcClient client, const char *hmac)
290 for (i = 0; silc_default_hmacs[i].name; i++)
291 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
292 silc_hmac_register(&(silc_default_hmacs[i]));
296 if (!silc_hmac_is_supported(hmac)) {
297 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
307 /* Register other defaults */
308 silc_hmac_register_default();
311 /* Finalize init. Init finish signal calls this. */
314 #define FUNCTION_EXIT goto out
315 void silc_opt_callback(const char *data, SERVER_REC *server,
318 #define FUNCTION_EXIT exit(0)
319 void silc_opt_callback(poptContext con,
320 enum poptCallbackReason reason,
321 const struct poptOption *opt,
322 const char *arg, void *data)
326 unsigned char **argv=NULL, *tmp;
327 SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
329 unsigned char privkey[128], pubkey[128];
331 memset(privkey, 0, sizeof(privkey));
332 memset(pubkey, 0, sizeof(pubkey));
333 snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
334 SILC_CLIENT_PUBLIC_KEY_NAME);
335 snprintf(privkey, sizeof(privkey) - 1, "%s/%s", get_irssi_dir(),
336 SILC_CLIENT_PRIVATE_KEY_NAME);
338 tmp = g_strconcat("SILC", " ", data, NULL);
339 silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
345 if (strcmp(opt->longName, "nick") == 0) {
346 g_free(opt_nickname);
347 opt_nickname = g_strdup(arg);
350 if (strcmp(opt->longName, "hostname") == 0) {
351 silc_free(opt_hostname);
352 opt_hostname = strdup(arg);
357 if ((argc == 2) && (strcasecmp(argv[1], "list-ciphers") == 0)) {
359 if (strcmp(opt->longName, "list-ciphers") == 0) {
360 silc_cipher_register_default();
362 silc_client_list_ciphers();
367 if ((argc == 2) && (strcasecmp(argv[1], "list-hash-funcs") == 0)) {
369 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
370 silc_hash_register_default();
372 silc_client_list_hash_funcs();
377 if ((argc == 2) && (strcasecmp(argv[1], "list-hmacs") == 0)) {
379 if (strcmp(opt->longName, "list-hmacs") == 0) {
380 silc_hmac_register_default();
382 silc_client_list_hmacs();
387 if ((argc == 2) && (strcasecmp(argv[1], "list-pkcs") == 0)) {
389 if (strcmp(opt->longName, "list-pkcs") == 0) {
390 silc_pkcs_register_default();
392 silc_client_list_pkcs();
397 if ((argc < 5) && (strcasecmp(argv[1], "debug") == 0)) {
399 if (strcmp(opt->longName, "debug") == 0) {
400 silc_log_debug(TRUE);
401 silc_log_debug_hexdump(TRUE);
402 silc_log_set_debug_string(arg);
406 printformat_module("fe-common/silc", NULL, NULL,
407 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
408 (i_debug == TRUE ? "enabled" : "disabled"));
414 printformat_module("fe-common/silc", NULL, NULL,
415 MSGLEVEL_CRAP, SILCTXT_CONFIG_NODEBUG);
418 "Run-time debugging is not enabled. To enable it recompile\n"
419 "the client with --enable-debug configuration option.\n");
424 if (strcasecmp(argv[2], "on") == 0) {
425 settings_set_bool("debug", TRUE);
427 settings_set_str("debug_string", argv[3]);
428 } else if ((argc == 3) && (strcasecmp(argv[2], "off") == 0)) {
429 settings_set_bool("debug", FALSE);
433 printformat_module("fe-common/silc", NULL, NULL,
434 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
435 (settings_get_bool("debug") == TRUE ?
436 "enabled" : "disabled"));
443 if (strcasecmp(argv[1], "create-key-pair") == 0) {
445 if (strcmp(opt->longName, "create-key-pair") == 0) {
447 /* Create new key pair and exit */
449 char *endptr, *pkcs=NULL;
454 if ((argc == 3) || (argc == 5))
457 for (i=2; i<argc-1; i+=2)
458 if (strcasecmp(argv[i], "-pkcs") == 0) {
463 } else if (strcasecmp(argv[i], "-bits") == 0) {
465 val = strtol(argv[i+1], &endptr, 10);
466 if ((*endptr != '\0') || (val <= 0) || (val >= INT_MAX))
474 rec = g_new0(CREATE_KEY_REC, 1);
475 rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
478 keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
479 format_get_text("fe-common/silc", NULL, NULL,
480 NULL, SILCTXT_CONFIG_PASS_ASK2),
481 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
482 printformat_module("fe-common/silc", NULL, NULL,
483 MSGLEVEL_CRAP, SILCTXT_CONFIG_NEXTTIME);
486 silc_cipher_register_default();
487 silc_pkcs_register_default();
488 silc_hash_register_default();
489 silc_hmac_register_default();
490 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
491 NULL, NULL, NULL, NULL, TRUE);
497 if ((argc < 4) && (strcasecmp(argv[1], "passphrase-change") == 0)) {
499 if (strcmp(opt->longName, "passphrase-change") == 0) {
501 /* Change the passphrase of the private key file */
505 rec = g_new0(CREATE_KEY_REC, 1);
506 rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
508 keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
509 format_get_text("fe-common/silc", NULL, NULL,
510 NULL, SILCTXT_CONFIG_PASS_ASK1),
511 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
514 silc_cipher_register_default();
515 silc_pkcs_register_default();
516 silc_hash_register_default();
517 silc_hmac_register_default();
518 silc_change_private_key_passphrase(arg, NULL, NULL);
524 if (strcmp(opt->longName, "show-key") == 0) {
526 silc_cipher_register_default();
527 silc_pkcs_register_default();
528 silc_hash_register_default();
529 silc_hmac_register_default();
530 silc_show_public_key_file((char *)arg);
537 printformat_module("fe-common/silc", NULL, NULL,
538 MSGLEVEL_CRAP, SILCTXT_CONFIG_UNKNOWN,
542 for (i=0; i<argc; i++)
546 silc_free(argv_lens);
547 silc_free(argv_types);
552 /* Called to indicate the client library has stopped. */
554 silc_stopped(SilcClient client, void *context)
556 SILC_LOG_DEBUG(("Client library has stopped"));
560 /* Called to indicate the client library is running. */
563 silc_running(SilcClient client, void *context)
566 SILC_LOG_DEBUG(("Client library is running"));
569 static void sig_init_finished(void)
571 /* Check ~/.silc directory and public and private keys */
572 if (!silc_client_check_silc_dir()) {
582 /* Load public and private key */
583 if (!silc_client_load_keys(silc_client)) {
593 /* Initialize the SILC client */
594 opt_hostname = (opt_hostname ? opt_hostname : silc_net_localhost());
595 if (!silc_client_init(silc_client, settings_get_str("user_name"),
596 opt_hostname, settings_get_str("real_name"),
597 silc_running, NULL)) {
607 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
608 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
609 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
610 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
612 silc_hash_alloc("sha1", &sha1hash);
614 /* register SILC scheduler */
615 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
618 /* Init SILC. Called from src/fe-text/silc.c */
620 void silc_core_init(void)
623 static struct poptOption silc_options[] = {
624 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
625 { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
626 "List supported ciphers", NULL },
627 { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
628 "List supported hash functions", NULL },
629 { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
630 "List supported HMACs", NULL },
631 { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
632 "List supported PKCSs", NULL },
634 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
635 "Enable debugging", "STRING" },
636 #endif /* SILC_DEBUG */
637 { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
638 "Create new public key pair", NULL },
639 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
640 "Set the PKCS of the public key pair (-C)", "PKCS" },
641 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
642 "Set the length of the public key pair (-C)", "VALUE" },
643 { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
644 "Change the passphrase of private key file", "FILE" },
645 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
646 "Show the contents of the public key", "FILE" },
647 { NULL, '\0', 0, NULL }
651 CHAT_PROTOCOL_REC *rec;
652 SilcClientParams params;
653 const char *def_cipher, *def_hash, *def_hmac;
656 args_register(silc_options);
661 settings_add_bool("server", "skip_motd", FALSE);
662 settings_add_str("server", "alternate_nick", NULL);
664 settings_add_bool("server", "use_auto_addr", FALSE);
665 settings_add_str("server", "auto_bind_ip", "");
666 settings_add_str("server", "auto_public_ip", "");
667 settings_add_int("server", "auto_bind_port", 0);
668 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
669 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
670 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
671 settings_add_int("server", "key_exchange_timeout_secs", 120);
672 settings_add_int("server", "key_exchange_rekey_secs", 3600);
673 settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
674 settings_add_int("server", "heartbeat", 300);
675 settings_add_bool("server", "ignore_message_signatures", FALSE);
676 settings_add_str("server", "session_filename", "session.$chatnet");
677 settings_add_bool("server", "sign_channel_messages", FALSE);
678 settings_add_bool("server", "sign_private_messages", FALSE);
679 settings_add_str("silc", "nickname_format", "%n#%a");
681 /* Requested Attributes settings */
682 settings_add_bool("silc", "attr_allow", TRUE);
683 settings_add_str("silc", "attr_vcard", "");
684 settings_add_str("silc", "attr_services", "");
685 settings_add_str("silc", "attr_status_mood", "NORMAL");
686 settings_add_str("silc", "attr_status_text", "");
687 settings_add_str("silc", "attr_status_message", NULL);
688 settings_add_str("silc", "attr_preferred_language", "");
689 settings_add_str("silc", "attr_preferred_contact", "CHAT");
690 settings_add_bool("silc", "attr_timezone", TRUE);
691 settings_add_str("silc", "attr_geolocation", "");
692 settings_add_str("silc", "attr_device_info", NULL);
693 settings_add_str("silc", "attr_public_keys", "");
696 settings_add_bool("debug", "debug", FALSE);
697 settings_add_str("debug", "debug_string", "");
700 signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
702 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
705 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
706 if (settings_get_bool("debug") == TRUE)
710 silc_init_userinfo();
712 silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
713 silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
714 silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
715 silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
717 /* Initialize client parameters */
718 memset(¶ms, 0, sizeof(params));
719 strcat(params.nickname_format, settings_get_str("nickname_format"));
721 /* Allocate SILC client */
722 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
724 /* Get the ciphers and stuff from config file */
725 def_cipher = settings_get_str("crypto_default_cipher");
726 def_hash = settings_get_str("crypto_default_hash");
727 def_hmac = settings_get_str("crypto_default_hmac");
728 silc_register_cipher(silc_client, def_cipher);
733 silc_register_hash(silc_client, def_hash);
738 silc_register_hmac(silc_client, def_hmac);
743 silc_pkcs_register_default();
746 command_bind("silc", MODULE_NAME, (SIGNAL_FUNC) silc_opt_callback);
749 /* Register SILC to the irssi */
750 rec = g_new0(CHAT_PROTOCOL_REC, 1);
752 rec->fullname = "Secure Internet Live Conferencing";
753 rec->chatnet = "silcnet";
754 rec->create_chatnet = create_chatnet;
755 rec->create_server_setup = create_server_setup;
756 rec->create_channel_setup = create_channel_setup;
757 rec->create_server_connect = create_server_connect;
758 rec->destroy_server_connect = destroy_server_connect;
759 rec->server_init_connect = silc_server_init_connect;
760 rec->server_connect = silc_server_connect;
761 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
764 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
767 chat_protocol_register(rec);
772 silc_channels_init();
774 silc_expandos_init();
776 silc_chatnets_init();
786 module_register("silc", "core");
789 /* Deinit SILC. Called from src/fe-text/silc.c */
791 void silc_core_deinit(void)
794 g_source_remove(idletag);
798 silc_client_stop(silc_client, silc_stopped, &stopped);
800 silc_client_run_one(silc_client);
804 silc_free(opt_hostname);
806 g_free(opt_nickname);
808 signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
810 command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
812 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
815 signal_emit("chat protocol deinit", 1, chat_protocol_find("SILC"));
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);