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 running = 0;
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 gboolean my_silc_scheduler(gpointer data)
81 SILC_LOG_DEBUG(("Timeout"));
82 silc_client_run_one(silc_client);
86 static gboolean my_silc_scheduler_fd(GIOChannel *source,
87 GIOCondition condition,
90 SILC_LOG_DEBUG(("I/O event, %d", SILC_PTR_TO_32(data)));
91 silc_client_run_one(silc_client);
95 static void scheduler_notify_cb(SilcSchedule schedule,
96 SilcBool added, SilcTask task,
97 SilcBool fd_task, SilcUInt32 fd,
99 long seconds, long useconds,
108 SILC_LOG_DEBUG(("Add fd %d, events %d", fd, event));
109 g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
111 if (event & SILC_TASK_READ)
112 e |= (G_IO_IN | G_IO_PRI | G_IO_HUP | G_IO_ERR);
113 if (event & SILC_TASK_WRITE)
114 e |= (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL);
117 ch = g_io_channel_unix_new(fd);
118 g_io_add_watch(ch, e, my_silc_scheduler_fd, SILC_32_TO_PTR(fd));
119 g_io_channel_unref(ch);
125 t = (seconds * 1000) + (useconds / 1000);
126 SILC_LOG_DEBUG(("interval %d msec", t));
127 g_timeout_add(t, my_silc_scheduler, NULL);
132 g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
137 static CHATNET_REC *create_chatnet(void)
139 return g_malloc0(sizeof(CHATNET_REC));
142 static SERVER_SETUP_REC *create_server_setup(void)
144 return g_malloc0(sizeof(SERVER_SETUP_REC));
147 static CHANNEL_SETUP_REC *create_channel_setup(void)
149 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
152 static SERVER_CONNECT_REC *create_server_connect(void)
154 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
157 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
162 /* Checks user information and saves them to the config file it they
163 do not exist there already. */
165 static void silc_init_userinfo(void)
167 const char *set, *nick, *user_name, *str;
170 /* check if nick/username/realname wasn't read from setup.. */
171 set = settings_get_str("real_name");
172 if (set == NULL || *set == '\0') {
173 str = g_getenv("SILCNAME");
175 str = g_getenv("IRCNAME");
176 settings_set_str("real_name",
177 str != NULL ? str : silc_get_real_name());
180 /* Check that real name is UTF-8 encoded */
181 set = settings_get_str("real_name");
182 if (!silc_utf8_valid(set, strlen(set))) {
183 int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
184 tmp = silc_calloc(len, sizeof(*tmp));
186 silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
187 settings_set_str("real_name", tmp);
193 user_name = settings_get_str("user_name");
194 if (user_name == NULL || *user_name == '\0') {
195 str = g_getenv("SILCUSER");
197 str = g_getenv("IRCUSER");
198 settings_set_str("user_name",
199 str != NULL ? str : silc_get_username());
201 user_name = settings_get_str("user_name");
205 nick = settings_get_str("nick");
206 if (nick == NULL || *nick == '\0') {
207 str = g_getenv("SILCNICK");
209 str = g_getenv("IRCNICK");
210 settings_set_str("nick", str != NULL ? str : user_name);
212 nick = settings_get_str("nick");
216 set = settings_get_str("alternate_nick");
217 if (set == NULL || *set == '\0') {
218 tmp = g_strconcat(nick, "_", NULL);
219 settings_set_str("alternate_nick", tmp);
224 set = settings_get_str("hostname");
225 if (set == NULL || *set == '\0') {
226 str = g_getenv("SILCHOST");
228 str = g_getenv("IRCHOST");
230 settings_set_str("hostname", str);
234 #if defined(SILC_DEBUG) || defined(SILC_PLUGIN)
239 static bool silc_irssi_debug_print(char *file, char *function, int line,
240 char *message, void *context)
242 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
243 "DEBUG: %s:%d: %s", function, line, message);
248 static void sig_setup_changed(void)
251 bool debug = settings_get_bool("debug");
253 const char *debug_string = settings_get_str("debug_string");
255 silc_log_debug(TRUE);
256 if (strlen(debug_string))
257 silc_log_set_debug_string(debug_string);
258 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
262 silc_log_debug(FALSE);
268 static bool silc_log_misc(SilcLogType type, char *message, void *context)
270 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
271 (type == SILC_LOG_INFO ? "[Info]" :
272 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
276 static bool silc_log_stderr(SilcLogType type, char *message, void *context)
278 fprintf(stderr, "%s: %s\n",
279 (type == SILC_LOG_INFO ? "[Info]" :
280 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
284 static void silc_register_cipher(SilcClient client, const char *cipher)
289 for (i = 0; silc_default_ciphers[i].name; i++)
290 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
291 silc_cipher_register(&(silc_default_ciphers[i]));
295 if (!silc_cipher_is_supported(cipher)) {
296 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
307 static void silc_register_hash(SilcClient client, const char *hash)
312 for (i = 0; silc_default_hash[i].name; i++)
313 if (!strcmp(silc_default_hash[i].name, hash)) {
314 silc_hash_register(&(silc_default_hash[i]));
318 if (!silc_hash_is_supported(hash)) {
319 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
330 static void silc_register_hmac(SilcClient client, const char *hmac)
335 for (i = 0; silc_default_hmacs[i].name; i++)
336 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
337 silc_hmac_register(&(silc_default_hmacs[i]));
341 if (!silc_hmac_is_supported(hmac)) {
342 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
353 /* Finalize init. Init finish signal calls this. */
356 #define FUNCTION_EXIT goto out
357 void silc_opt_callback(const char *data, SERVER_REC *server,
360 #define FUNCTION_EXIT exit(0)
361 void silc_opt_callback(poptContext con,
362 enum poptCallbackReason reason,
363 const struct poptOption *opt,
364 const char *arg, void *data)
368 unsigned char **argv=NULL, *tmp;
369 SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
371 unsigned char privkey[128], pubkey[128];
373 memset(privkey, 0, sizeof(privkey));
374 memset(pubkey, 0, sizeof(pubkey));
375 snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
376 SILC_CLIENT_PUBLIC_KEY_NAME);
377 snprintf(privkey, sizeof(privkey) - 1, "%s/%s", get_irssi_dir(),
378 SILC_CLIENT_PRIVATE_KEY_NAME);
380 tmp = g_strconcat("SILC", " ", data, NULL);
381 silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
387 if (strcmp(opt->longName, "nick") == 0) {
388 g_free(opt_nickname);
389 opt_nickname = g_strdup(arg);
392 if (strcmp(opt->longName, "hostname") == 0) {
393 silc_free(opt_hostname);
394 opt_hostname = strdup(arg);
399 if ((argc == 2) && (strcasecmp(argv[1], "list-ciphers") == 0)) {
401 if (strcmp(opt->longName, "list-ciphers") == 0) {
403 silc_client_list_ciphers();
408 if ((argc == 2) && (strcasecmp(argv[1], "list-hash-funcs") == 0)) {
410 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
412 silc_client_list_hash_funcs();
417 if ((argc == 2) && (strcasecmp(argv[1], "list-hmacs") == 0)) {
419 if (strcmp(opt->longName, "list-hmacs") == 0) {
421 silc_client_list_hmacs();
426 if ((argc == 2) && (strcasecmp(argv[1], "list-pkcs") == 0)) {
428 if (strcmp(opt->longName, "list-pkcs") == 0) {
430 silc_client_list_pkcs();
435 if ((argc < 5) && (strcasecmp(argv[1], "debug") == 0)) {
437 if (strcmp(opt->longName, "debug") == 0) {
438 silc_log_debug(TRUE);
439 silc_log_debug_hexdump(TRUE);
440 silc_log_set_debug_string(arg);
444 printformat_module("fe-common/silc", NULL, NULL,
445 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
446 (i_debug == TRUE ? "enabled" : "disabled"));
452 printformat_module("fe-common/silc", NULL, NULL,
453 MSGLEVEL_CRAP, SILCTXT_CONFIG_NODEBUG);
456 "Run-time debugging is not enabled. To enable it recompile\n"
457 "the client with --enable-debug configuration option.\n");
462 if (strcasecmp(argv[2], "on") == 0) {
463 settings_set_bool("debug", TRUE);
465 settings_set_str("debug_string", argv[3]);
466 } else if ((argc == 3) && (strcasecmp(argv[2], "off") == 0)) {
467 settings_set_bool("debug", FALSE);
471 printformat_module("fe-common/silc", NULL, NULL,
472 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
473 (settings_get_bool("debug") == TRUE ?
474 "enabled" : "disabled"));
481 if (strcasecmp(argv[1], "create-key-pair") == 0) {
483 if (strcmp(opt->longName, "create-key-pair") == 0) {
485 /* Create new key pair and exit */
487 char *endptr, *pkcs=NULL;
492 if ((argc == 3) || (argc == 5))
495 for (i=2; i<argc-1; i+=2)
496 if (strcasecmp(argv[i], "-pkcs") == 0) {
501 } else if (strcasecmp(argv[i], "-bits") == 0) {
503 val = strtol(argv[i+1], &endptr, 10);
504 if ((*endptr != '\0') || (val <= 0) || (val >= INT_MAX))
512 rec = g_new0(CREATE_KEY_REC, 1);
513 rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
516 keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
517 format_get_text("fe-common/silc", NULL, NULL,
518 NULL, SILCTXT_CONFIG_PASS_ASK2),
519 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
520 printformat_module("fe-common/silc", NULL, NULL,
521 MSGLEVEL_CRAP, SILCTXT_CONFIG_NEXTTIME);
524 silc_crypto_init(NULL);
525 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
526 NULL, NULL, NULL, NULL, TRUE);
532 if ((argc < 4) && (strcasecmp(argv[1], "passphrase-change") == 0)) {
534 if (strcmp(opt->longName, "passphrase-change") == 0) {
536 /* Change the passphrase of the private key file */
540 rec = g_new0(CREATE_KEY_REC, 1);
541 rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
543 keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
544 format_get_text("fe-common/silc", NULL, NULL,
545 NULL, SILCTXT_CONFIG_PASS_ASK1),
546 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
549 silc_crypto_init(NULL);
550 silc_change_private_key_passphrase(arg, NULL, NULL);
556 if (strcmp(opt->longName, "show-key") == 0) {
558 silc_crypto_init(NULL);
559 silc_show_public_key_file((char *)arg);
566 printformat_module("fe-common/silc", NULL, NULL,
567 MSGLEVEL_CRAP, SILCTXT_CONFIG_UNKNOWN,
571 for (i=0; i<argc; i++)
575 silc_free(argv_lens);
576 silc_free(argv_types);
581 /* Called to indicate the client library has stopped. */
583 silc_stopped(SilcClient client, void *context)
585 SILC_LOG_DEBUG(("Client library has stopped"));
589 /* Called to indicate the client library is running. */
592 silc_running(SilcClient client, void *context)
595 SILC_LOG_DEBUG(("Client library is running"));
598 static void sig_init_finished(void)
600 /* Check ~/.silc directory and public and private keys */
601 if (!silc_client_check_silc_dir()) {
611 /* Load public and private key */
612 if (!silc_client_load_keys(silc_client)) {
622 /* Initialize the SILC client */
623 opt_hostname = (opt_hostname ? opt_hostname : silc_net_localhost());
624 if (!silc_client_init(silc_client, settings_get_str("user_name"),
625 opt_hostname, settings_get_str("real_name"),
626 silc_running, NULL)) {
636 silc_schedule_set_notify(silc_client->schedule, scheduler_notify_cb, NULL);
638 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
639 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
640 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
641 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
643 silc_hash_alloc("sha1", &sha1hash);
645 /* Run SILC scheduler */
646 my_silc_scheduler(NULL);
649 /* Init SILC. Called from src/fe-text/silc.c */
651 void silc_core_init(void)
654 static struct poptOption silc_options[] = {
655 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
656 { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
657 "List supported ciphers", NULL },
658 { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
659 "List supported hash functions", NULL },
660 { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
661 "List supported HMACs", NULL },
662 { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
663 "List supported PKCSs", NULL },
665 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
666 "Enable debugging", "STRING" },
667 #endif /* SILC_DEBUG */
668 { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
669 "Create new public key pair", NULL },
670 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
671 "Set the PKCS of the public key pair (-C)", "PKCS" },
672 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
673 "Set the length of the public key pair (-C)", "VALUE" },
674 { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
675 "Change the passphrase of private key file", "FILE" },
676 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
677 "Show the contents of the public key", "FILE" },
678 { NULL, '\0', 0, NULL }
682 CHAT_PROTOCOL_REC *rec;
683 SilcClientParams params;
684 const char *def_cipher, *def_hash, *def_hmac;
687 args_register(silc_options);
692 settings_add_bool("server", "skip_motd", FALSE);
693 settings_add_str("server", "alternate_nick", NULL);
695 settings_add_bool("server", "use_auto_addr", FALSE);
696 settings_add_str("server", "auto_bind_ip", "");
697 settings_add_str("server", "auto_public_ip", "");
698 settings_add_int("server", "auto_bind_port", 0);
699 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
700 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
701 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
702 settings_add_int("server", "key_exchange_timeout_secs", 120);
703 settings_add_int("server", "key_exchange_rekey_secs", 3600);
704 settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
705 settings_add_int("server", "heartbeat", 300);
706 settings_add_bool("server", "ignore_message_signatures", FALSE);
707 settings_add_str("server", "session_filename", "session.$chatnet");
708 settings_add_bool("server", "sign_channel_messages", FALSE);
709 settings_add_bool("server", "sign_private_messages", FALSE);
710 settings_add_str("silc", "nickname_format", "%n#%a");
712 /* Requested Attributes settings */
713 settings_add_bool("silc", "attr_allow", TRUE);
714 settings_add_str("silc", "attr_vcard", "");
715 settings_add_str("silc", "attr_services", "");
716 settings_add_str("silc", "attr_status_mood", "NORMAL");
717 settings_add_str("silc", "attr_status_text", "");
718 settings_add_str("silc", "attr_status_message", NULL);
719 settings_add_str("silc", "attr_preferred_language", "");
720 settings_add_str("silc", "attr_preferred_contact", "CHAT");
721 settings_add_bool("silc", "attr_timezone", TRUE);
722 settings_add_str("silc", "attr_geolocation", "");
723 settings_add_str("silc", "attr_device_info", NULL);
724 settings_add_str("silc", "attr_public_keys", "");
727 settings_add_bool("debug", "debug", FALSE);
728 settings_add_str("debug", "debug_string", "");
731 signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
733 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
736 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
737 if (settings_get_bool("debug") == TRUE)
741 silc_init_userinfo();
743 silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
744 silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
745 silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
746 silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
748 /* Initialize client parameters */
749 memset(¶ms, 0, sizeof(params));
750 strcat(params.nickname_format, settings_get_str("nickname_format"));
751 params.full_channel_names = TRUE;
753 /* Allocate SILC client */
754 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
756 /* Get the ciphers and stuff from config file */
757 def_cipher = settings_get_str("crypto_default_cipher");
758 def_hash = settings_get_str("crypto_default_hash");
759 def_hmac = settings_get_str("crypto_default_hmac");
760 silc_register_cipher(silc_client, def_cipher);
765 silc_register_hash(silc_client, def_hash);
770 silc_register_hmac(silc_client, def_hmac);
777 command_bind("silc", MODULE_NAME, (SIGNAL_FUNC) silc_opt_callback);
780 /* Register SILC to the irssi */
781 rec = g_new0(CHAT_PROTOCOL_REC, 1);
783 rec->fullname = "Secure Internet Live Conferencing";
784 rec->chatnet = "silcnet";
785 rec->create_chatnet = create_chatnet;
786 rec->create_server_setup = create_server_setup;
787 rec->create_channel_setup = create_channel_setup;
788 rec->create_server_connect = create_server_connect;
789 rec->destroy_server_connect = destroy_server_connect;
790 rec->server_init_connect = silc_server_init_connect;
791 rec->server_connect = silc_server_connect;
792 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
795 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
798 chat_protocol_register(rec);
803 silc_channels_init();
805 silc_expandos_init();
807 silc_chatnets_init();
817 module_register("silc", "core");
820 /* Deinit SILC. Called from src/fe-text/silc.c */
822 void silc_core_deinit(void)
825 volatile int stopped = 0;
826 silc_client_stop(silc_client, silc_stopped, (void *)&stopped);
828 silc_client_run_one(silc_client);
832 silc_free(opt_hostname);
834 g_free(opt_nickname);
836 signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
838 command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
840 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
843 signal_emit("chat protocol deinit", 1, chat_protocol_find("SILC"));
845 silc_hash_free(sha1hash);
848 silc_server_deinit();
849 silc_channels_deinit();
850 silc_queries_deinit();
851 silc_expandos_deinit();
853 silc_chatnets_deinit();
855 chat_protocol_unregister("SILC");
858 silc_pkcs_public_key_free(irssi_pubkey);
860 silc_pkcs_private_key_free(irssi_privkey);
861 silc_client_free(silc_client);