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)
113 if (event & SILC_TASK_WRITE)
117 ch = g_io_channel_unix_new(fd);
118 g_io_add_watch(ch, e, my_silc_scheduler_fd, SILC_32_TO_PTR(fd));
124 /* Zero timeouts are delievered always immediately, as per
125 SilcSchedule API documentation, no need to add them to glib. */
126 if (!seconds && !useconds)
129 t = (seconds * 1000) + (useconds / 1000),
130 SILC_LOG_DEBUG(("interval %d msec", t));
131 g_timeout_add(t, my_silc_scheduler, NULL);
136 g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
141 static CHATNET_REC *create_chatnet(void)
143 return g_malloc0(sizeof(CHATNET_REC));
146 static SERVER_SETUP_REC *create_server_setup(void)
148 return g_malloc0(sizeof(SERVER_SETUP_REC));
151 static CHANNEL_SETUP_REC *create_channel_setup(void)
153 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
156 static SERVER_CONNECT_REC *create_server_connect(void)
158 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
161 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
166 /* Checks user information and saves them to the config file it they
167 do not exist there already. */
169 static void silc_init_userinfo(void)
171 const char *set, *nick, *user_name, *str;
174 /* check if nick/username/realname wasn't read from setup.. */
175 set = settings_get_str("real_name");
176 if (set == NULL || *set == '\0') {
177 str = g_getenv("SILCNAME");
179 str = g_getenv("IRCNAME");
180 settings_set_str("real_name",
181 str != NULL ? str : silc_get_real_name());
184 /* Check that real name is UTF-8 encoded */
185 set = settings_get_str("real_name");
186 if (!silc_utf8_valid(set, strlen(set))) {
187 int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
188 tmp = silc_calloc(len, sizeof(*tmp));
190 silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
191 settings_set_str("real_name", tmp);
197 user_name = settings_get_str("user_name");
198 if (user_name == NULL || *user_name == '\0') {
199 str = g_getenv("SILCUSER");
201 str = g_getenv("IRCUSER");
202 settings_set_str("user_name",
203 str != NULL ? str : silc_get_username());
205 user_name = settings_get_str("user_name");
209 nick = settings_get_str("nick");
210 if (nick == NULL || *nick == '\0') {
211 str = g_getenv("SILCNICK");
213 str = g_getenv("IRCNICK");
214 settings_set_str("nick", str != NULL ? str : user_name);
216 nick = settings_get_str("nick");
220 set = settings_get_str("alternate_nick");
221 if (set == NULL || *set == '\0') {
222 tmp = g_strconcat(nick, "_", NULL);
223 settings_set_str("alternate_nick", tmp);
228 set = settings_get_str("hostname");
229 if (set == NULL || *set == '\0') {
230 str = g_getenv("SILCHOST");
232 str = g_getenv("IRCHOST");
234 settings_set_str("hostname", str);
238 #if defined(SILC_DEBUG) || defined(SILC_PLUGIN)
243 static bool silc_irssi_debug_print(char *file, char *function, int line,
244 char *message, void *context)
246 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
247 "DEBUG: %s:%d: %s", function, line, message);
252 static void sig_setup_changed(void)
255 bool debug = settings_get_bool("debug");
257 const char *debug_string = settings_get_str("debug_string");
259 silc_log_debug(TRUE);
260 if (strlen(debug_string))
261 silc_log_set_debug_string(debug_string);
262 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
266 silc_log_debug(FALSE);
272 static bool silc_log_misc(SilcLogType type, char *message, void *context)
274 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
275 (type == SILC_LOG_INFO ? "[Info]" :
276 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
280 static bool silc_log_stderr(SilcLogType type, char *message, void *context)
282 fprintf(stderr, "%s: %s\n",
283 (type == SILC_LOG_INFO ? "[Info]" :
284 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
288 static void silc_register_cipher(SilcClient client, const char *cipher)
293 for (i = 0; silc_default_ciphers[i].name; i++)
294 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
295 silc_cipher_register(&(silc_default_ciphers[i]));
299 if (!silc_cipher_is_supported(cipher)) {
300 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
310 /* Register other defaults */
311 silc_cipher_register_default();
314 static void silc_register_hash(SilcClient client, const char *hash)
319 for (i = 0; silc_default_hash[i].name; i++)
320 if (!strcmp(silc_default_hash[i].name, hash)) {
321 silc_hash_register(&(silc_default_hash[i]));
325 if (!silc_hash_is_supported(hash)) {
326 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
336 /* Register other defaults */
337 silc_hash_register_default();
340 static void silc_register_hmac(SilcClient client, const char *hmac)
345 for (i = 0; silc_default_hmacs[i].name; i++)
346 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
347 silc_hmac_register(&(silc_default_hmacs[i]));
351 if (!silc_hmac_is_supported(hmac)) {
352 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
362 /* Register other defaults */
363 silc_hmac_register_default();
366 /* Finalize init. Init finish signal calls this. */
369 #define FUNCTION_EXIT goto out
370 void silc_opt_callback(const char *data, SERVER_REC *server,
373 #define FUNCTION_EXIT exit(0)
374 void silc_opt_callback(poptContext con,
375 enum poptCallbackReason reason,
376 const struct poptOption *opt,
377 const char *arg, void *data)
381 unsigned char **argv=NULL, *tmp;
382 SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
384 unsigned char privkey[128], pubkey[128];
386 memset(privkey, 0, sizeof(privkey));
387 memset(pubkey, 0, sizeof(pubkey));
388 snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
389 SILC_CLIENT_PUBLIC_KEY_NAME);
390 snprintf(privkey, sizeof(privkey) - 1, "%s/%s", get_irssi_dir(),
391 SILC_CLIENT_PRIVATE_KEY_NAME);
393 tmp = g_strconcat("SILC", " ", data, NULL);
394 silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
400 if (strcmp(opt->longName, "nick") == 0) {
401 g_free(opt_nickname);
402 opt_nickname = g_strdup(arg);
405 if (strcmp(opt->longName, "hostname") == 0) {
406 silc_free(opt_hostname);
407 opt_hostname = strdup(arg);
412 if ((argc == 2) && (strcasecmp(argv[1], "list-ciphers") == 0)) {
414 if (strcmp(opt->longName, "list-ciphers") == 0) {
415 silc_cipher_register_default();
417 silc_client_list_ciphers();
422 if ((argc == 2) && (strcasecmp(argv[1], "list-hash-funcs") == 0)) {
424 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
425 silc_hash_register_default();
427 silc_client_list_hash_funcs();
432 if ((argc == 2) && (strcasecmp(argv[1], "list-hmacs") == 0)) {
434 if (strcmp(opt->longName, "list-hmacs") == 0) {
435 silc_hmac_register_default();
437 silc_client_list_hmacs();
442 if ((argc == 2) && (strcasecmp(argv[1], "list-pkcs") == 0)) {
444 if (strcmp(opt->longName, "list-pkcs") == 0) {
445 silc_pkcs_register_default();
447 silc_client_list_pkcs();
452 if ((argc < 5) && (strcasecmp(argv[1], "debug") == 0)) {
454 if (strcmp(opt->longName, "debug") == 0) {
455 silc_log_debug(TRUE);
456 silc_log_debug_hexdump(TRUE);
457 silc_log_set_debug_string(arg);
461 printformat_module("fe-common/silc", NULL, NULL,
462 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
463 (i_debug == TRUE ? "enabled" : "disabled"));
469 printformat_module("fe-common/silc", NULL, NULL,
470 MSGLEVEL_CRAP, SILCTXT_CONFIG_NODEBUG);
473 "Run-time debugging is not enabled. To enable it recompile\n"
474 "the client with --enable-debug configuration option.\n");
479 if (strcasecmp(argv[2], "on") == 0) {
480 settings_set_bool("debug", TRUE);
482 settings_set_str("debug_string", argv[3]);
483 } else if ((argc == 3) && (strcasecmp(argv[2], "off") == 0)) {
484 settings_set_bool("debug", FALSE);
488 printformat_module("fe-common/silc", NULL, NULL,
489 MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
490 (settings_get_bool("debug") == TRUE ?
491 "enabled" : "disabled"));
498 if (strcasecmp(argv[1], "create-key-pair") == 0) {
500 if (strcmp(opt->longName, "create-key-pair") == 0) {
502 /* Create new key pair and exit */
504 char *endptr, *pkcs=NULL;
509 if ((argc == 3) || (argc == 5))
512 for (i=2; i<argc-1; i+=2)
513 if (strcasecmp(argv[i], "-pkcs") == 0) {
518 } else if (strcasecmp(argv[i], "-bits") == 0) {
520 val = strtol(argv[i+1], &endptr, 10);
521 if ((*endptr != '\0') || (val <= 0) || (val >= INT_MAX))
529 rec = g_new0(CREATE_KEY_REC, 1);
530 rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
533 keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
534 format_get_text("fe-common/silc", NULL, NULL,
535 NULL, SILCTXT_CONFIG_PASS_ASK2),
536 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
537 printformat_module("fe-common/silc", NULL, NULL,
538 MSGLEVEL_CRAP, SILCTXT_CONFIG_NEXTTIME);
541 silc_cipher_register_default();
542 silc_pkcs_register_default();
543 silc_hash_register_default();
544 silc_hmac_register_default();
545 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
546 NULL, NULL, NULL, NULL, TRUE);
552 if ((argc < 4) && (strcasecmp(argv[1], "passphrase-change") == 0)) {
554 if (strcmp(opt->longName, "passphrase-change") == 0) {
556 /* Change the passphrase of the private key file */
560 rec = g_new0(CREATE_KEY_REC, 1);
561 rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
563 keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
564 format_get_text("fe-common/silc", NULL, NULL,
565 NULL, SILCTXT_CONFIG_PASS_ASK1),
566 ENTRY_REDIRECT_FLAG_HIDDEN, rec);
569 silc_cipher_register_default();
570 silc_pkcs_register_default();
571 silc_hash_register_default();
572 silc_hmac_register_default();
573 silc_change_private_key_passphrase(arg, NULL, NULL);
579 if (strcmp(opt->longName, "show-key") == 0) {
581 silc_cipher_register_default();
582 silc_pkcs_register_default();
583 silc_hash_register_default();
584 silc_hmac_register_default();
585 silc_show_public_key_file((char *)arg);
592 printformat_module("fe-common/silc", NULL, NULL,
593 MSGLEVEL_CRAP, SILCTXT_CONFIG_UNKNOWN,
597 for (i=0; i<argc; i++)
601 silc_free(argv_lens);
602 silc_free(argv_types);
607 /* Called to indicate the client library has stopped. */
609 silc_stopped(SilcClient client, void *context)
611 SILC_LOG_DEBUG(("Client library has stopped"));
615 /* Called to indicate the client library is running. */
618 silc_running(SilcClient client, void *context)
621 SILC_LOG_DEBUG(("Client library is running"));
624 static void sig_init_finished(void)
626 /* Check ~/.silc directory and public and private keys */
627 if (!silc_client_check_silc_dir()) {
637 /* Load public and private key */
638 if (!silc_client_load_keys(silc_client)) {
648 /* Initialize the SILC client */
649 opt_hostname = (opt_hostname ? opt_hostname : silc_net_localhost());
650 if (!silc_client_init(silc_client, settings_get_str("user_name"),
651 opt_hostname, settings_get_str("real_name"),
652 silc_running, NULL)) {
662 silc_schedule_set_notify(silc_client->schedule, scheduler_notify_cb, NULL);
664 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
665 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
666 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
667 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
669 silc_hash_alloc("sha1", &sha1hash);
671 /* Run SILC scheduler */
672 my_silc_scheduler(NULL);
675 /* Init SILC. Called from src/fe-text/silc.c */
677 void silc_core_init(void)
680 static struct poptOption silc_options[] = {
681 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
682 { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
683 "List supported ciphers", NULL },
684 { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
685 "List supported hash functions", NULL },
686 { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
687 "List supported HMACs", NULL },
688 { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
689 "List supported PKCSs", NULL },
691 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
692 "Enable debugging", "STRING" },
693 #endif /* SILC_DEBUG */
694 { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
695 "Create new public key pair", NULL },
696 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
697 "Set the PKCS of the public key pair (-C)", "PKCS" },
698 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
699 "Set the length of the public key pair (-C)", "VALUE" },
700 { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
701 "Change the passphrase of private key file", "FILE" },
702 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
703 "Show the contents of the public key", "FILE" },
704 { NULL, '\0', 0, NULL }
708 CHAT_PROTOCOL_REC *rec;
709 SilcClientParams params;
710 const char *def_cipher, *def_hash, *def_hmac;
713 args_register(silc_options);
718 settings_add_bool("server", "skip_motd", FALSE);
719 settings_add_str("server", "alternate_nick", NULL);
721 settings_add_bool("server", "use_auto_addr", FALSE);
722 settings_add_str("server", "auto_bind_ip", "");
723 settings_add_str("server", "auto_public_ip", "");
724 settings_add_int("server", "auto_bind_port", 0);
725 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
726 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
727 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
728 settings_add_int("server", "key_exchange_timeout_secs", 120);
729 settings_add_int("server", "key_exchange_rekey_secs", 3600);
730 settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
731 settings_add_int("server", "heartbeat", 300);
732 settings_add_bool("server", "ignore_message_signatures", FALSE);
733 settings_add_str("server", "session_filename", "session.$chatnet");
734 settings_add_bool("server", "sign_channel_messages", FALSE);
735 settings_add_bool("server", "sign_private_messages", FALSE);
736 settings_add_str("silc", "nickname_format", "%n#%a");
738 /* Requested Attributes settings */
739 settings_add_bool("silc", "attr_allow", TRUE);
740 settings_add_str("silc", "attr_vcard", "");
741 settings_add_str("silc", "attr_services", "");
742 settings_add_str("silc", "attr_status_mood", "NORMAL");
743 settings_add_str("silc", "attr_status_text", "");
744 settings_add_str("silc", "attr_status_message", NULL);
745 settings_add_str("silc", "attr_preferred_language", "");
746 settings_add_str("silc", "attr_preferred_contact", "CHAT");
747 settings_add_bool("silc", "attr_timezone", TRUE);
748 settings_add_str("silc", "attr_geolocation", "");
749 settings_add_str("silc", "attr_device_info", NULL);
750 settings_add_str("silc", "attr_public_keys", "");
753 settings_add_bool("debug", "debug", FALSE);
754 settings_add_str("debug", "debug_string", "");
757 signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
759 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
762 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
763 if (settings_get_bool("debug") == TRUE)
767 silc_init_userinfo();
769 silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
770 silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
771 silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
772 silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
774 /* Initialize client parameters */
775 memset(¶ms, 0, sizeof(params));
776 strcat(params.nickname_format, settings_get_str("nickname_format"));
778 /* Allocate SILC client */
779 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
781 /* Get the ciphers and stuff from config file */
782 def_cipher = settings_get_str("crypto_default_cipher");
783 def_hash = settings_get_str("crypto_default_hash");
784 def_hmac = settings_get_str("crypto_default_hmac");
785 silc_register_cipher(silc_client, def_cipher);
790 silc_register_hash(silc_client, def_hash);
795 silc_register_hmac(silc_client, def_hmac);
800 silc_pkcs_register_default();
803 command_bind("silc", MODULE_NAME, (SIGNAL_FUNC) silc_opt_callback);
806 /* Register SILC to the irssi */
807 rec = g_new0(CHAT_PROTOCOL_REC, 1);
809 rec->fullname = "Secure Internet Live Conferencing";
810 rec->chatnet = "silcnet";
811 rec->create_chatnet = create_chatnet;
812 rec->create_server_setup = create_server_setup;
813 rec->create_channel_setup = create_channel_setup;
814 rec->create_server_connect = create_server_connect;
815 rec->destroy_server_connect = destroy_server_connect;
816 rec->server_init_connect = silc_server_init_connect;
817 rec->server_connect = silc_server_connect;
818 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
821 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
824 chat_protocol_register(rec);
829 silc_channels_init();
831 silc_expandos_init();
833 silc_chatnets_init();
843 module_register("silc", "core");
846 /* Deinit SILC. Called from src/fe-text/silc.c */
848 void silc_core_deinit(void)
851 volatile int stopped = 0;
852 silc_client_stop(silc_client, silc_stopped, (void *)&stopped);
854 silc_client_run_one(silc_client);
858 silc_free(opt_hostname);
860 g_free(opt_nickname);
862 signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
864 command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
866 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
869 signal_emit("chat protocol deinit", 1, chat_protocol_find("SILC"));
871 silc_hash_free(sha1hash);
874 silc_server_deinit();
875 silc_channels_deinit();
876 silc_queries_deinit();
877 silc_expandos_deinit();
879 silc_chatnets_deinit();
881 chat_protocol_unregister("SILC");
884 silc_pkcs_public_key_free(irssi_pubkey);
886 silc_pkcs_private_key_free(irssi_privkey);
887 silc_client_free(silc_client);