5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2006 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"
38 #include "fe-common/core/printtext.h"
39 #include "fe-common/core/fe-channels.h"
40 #include "fe-common/core/keyboard.h"
41 #include "fe-common/silc/module-formats.h"
43 /* Command line option variables */
44 static char *opt_pkcs = NULL;
45 static int opt_bits = 0;
47 static int idletag = -1;
50 SilcClient silc_client = NULL;
51 extern SilcClientOperations ops;
54 SilcPublicKey irssi_pubkey = NULL;
55 SilcPrivateKey irssi_privkey = NULL;
57 char *opt_nickname = NULL;
58 char *opt_hostname = NULL;
60 /* Default hash function */
61 SilcHash sha1hash = NULL;
63 void silc_expandos_init(void);
64 void silc_expandos_deinit(void);
66 void silc_lag_init(void);
67 void silc_lag_deinit(void);
69 static int my_silc_scheduler(void)
71 silc_client_run_one(silc_client);
75 static CHATNET_REC *create_chatnet(void)
77 return g_malloc0(sizeof(CHATNET_REC));
80 static SERVER_SETUP_REC *create_server_setup(void)
82 return g_malloc0(sizeof(SERVER_SETUP_REC));
85 static CHANNEL_SETUP_REC *create_channel_setup(void)
87 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
90 static SERVER_CONNECT_REC *create_server_connect(void)
92 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
95 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
100 /* Checks user information and saves them to the config file it they
101 do not exist there already. */
103 static void silc_init_userinfo(void)
105 const char *set, *nick, *user_name, *str;
108 /* check if nick/username/realname wasn't read from setup.. */
109 set = settings_get_str("real_name");
110 if (set == NULL || *set == '\0') {
111 str = g_getenv("SILCNAME");
113 str = g_getenv("IRCNAME");
114 settings_set_str("real_name",
115 str != NULL ? str : silc_get_real_name());
118 /* Check that real name is UTF-8 encoded */
119 set = settings_get_str("real_name");
120 if (!silc_utf8_valid(set, strlen(set))) {
121 int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
122 tmp = silc_calloc(len, sizeof(*tmp));
124 silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
125 settings_set_str("real_name", tmp);
131 user_name = settings_get_str("user_name");
132 if (user_name == NULL || *user_name == '\0') {
133 str = g_getenv("SILCUSER");
135 str = g_getenv("IRCUSER");
136 settings_set_str("user_name",
137 str != NULL ? str : silc_get_username());
139 user_name = settings_get_str("user_name");
143 nick = settings_get_str("nick");
144 if (nick == NULL || *nick == '\0') {
145 str = g_getenv("SILCNICK");
147 str = g_getenv("IRCNICK");
148 settings_set_str("nick", str != NULL ? str : user_name);
150 nick = settings_get_str("nick");
154 set = settings_get_str("alternate_nick");
155 if (set == NULL || *set == '\0') {
156 tmp = g_strconcat(nick, "_", NULL);
157 settings_set_str("alternate_nick", tmp);
162 set = settings_get_str("hostname");
163 if (set == NULL || *set == '\0') {
164 str = g_getenv("SILCHOST");
166 str = g_getenv("IRCHOST");
168 settings_set_str("hostname", str);
174 static bool silc_irssi_debug_print(char *file, char *function, int line,
175 char *message, void *context)
177 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
178 "DEBUG: %s:%d: %s", function, line, message);
183 static void sig_setup_changed(void)
186 bool debug = settings_get_bool("debug");
188 const char *debug_string = settings_get_str("debug_string");
190 silc_log_debug(TRUE);
191 if (strlen(debug_string))
192 silc_log_set_debug_string(debug_string);
193 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
197 silc_log_debug(FALSE);
203 static bool silc_log_misc(SilcLogType type, char *message, void *context)
205 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
206 (type == SILC_LOG_INFO ? "[Info]" :
207 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
211 static bool silc_log_stderr(SilcLogType type, char *message, void *context)
213 fprintf(stderr, "%s: %s\n",
214 (type == SILC_LOG_INFO ? "[Info]" :
215 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
219 static void silc_nickname_format_parse(const char *nickname,
223 silc_parse_userfqdn(nickname, nick, sizeof(nick), NULL, 0);
224 *ret_nickname = strdup(nick);
227 static void silc_register_cipher(SilcClient client, const char *cipher)
232 for (i = 0; silc_default_ciphers[i].name; i++)
233 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
234 silc_cipher_register(&(silc_default_ciphers[i]));
238 if (!silc_cipher_is_supported(cipher)) {
239 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
244 /* Register other defaults */
245 silc_cipher_register_default();
248 static void silc_register_hash(SilcClient client, const char *hash)
253 for (i = 0; silc_default_hash[i].name; i++)
254 if (!strcmp(silc_default_hash[i].name, hash)) {
255 silc_hash_register(&(silc_default_hash[i]));
259 if (!silc_hash_is_supported(hash)) {
260 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
265 /* Register other defaults */
266 silc_hash_register_default();
269 static void silc_register_hmac(SilcClient client, const char *hmac)
274 for (i = 0; silc_default_hmacs[i].name; i++)
275 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
276 silc_hmac_register(&(silc_default_hmacs[i]));
280 if (!silc_hmac_is_supported(hmac)) {
281 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
286 /* Register other defaults */
287 silc_hmac_register_default();
290 /* Finalize init. Init finish signal calls this. */
292 void silc_opt_callback(poptContext con,
293 enum poptCallbackReason reason,
294 const struct poptOption *opt,
295 const char *arg, void *data)
297 if (strcmp(opt->longName, "nick") == 0) {
298 g_free(opt_nickname);
299 opt_nickname = g_strdup(arg);
302 if (strcmp(opt->longName, "hostname") == 0) {
303 g_free(opt_hostname);
304 opt_hostname = g_strdup(arg);
307 if (strcmp(opt->longName, "list-ciphers") == 0) {
308 silc_cipher_register_default();
309 silc_client_list_ciphers();
313 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
314 silc_hash_register_default();
315 silc_client_list_hash_funcs();
319 if (strcmp(opt->longName, "list-hmacs") == 0) {
320 silc_hmac_register_default();
321 silc_client_list_hmacs();
325 if (strcmp(opt->longName, "list-pkcs") == 0) {
326 silc_pkcs_register_default();
327 silc_client_list_pkcs();
331 if (strcmp(opt->longName, "debug") == 0) {
332 silc_log_debug(TRUE);
333 silc_log_debug_hexdump(TRUE);
334 silc_log_set_debug_string(arg);
337 "Run-time debugging is not enabled. To enable it recompile\n"
338 "the client with --enable-debug configuration option.\n");
343 if (strcmp(opt->longName, "create-key-pair") == 0) {
344 /* Create new key pair and exit */
345 silc_cipher_register_default();
346 silc_pkcs_register_default();
347 silc_hash_register_default();
348 silc_hmac_register_default();
349 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
350 NULL, NULL, NULL, NULL, TRUE);
354 if (strcmp(opt->longName, "passphrase-change") == 0) {
355 /* Change the passphrase of the private key file */
356 silc_cipher_register_default();
357 silc_pkcs_register_default();
358 silc_hash_register_default();
359 silc_hmac_register_default();
360 silc_change_private_key_passphrase(arg, NULL, NULL);
364 if (strcmp(opt->longName, "show-key") == 0) {
366 silc_cipher_register_default();
367 silc_pkcs_register_default();
368 silc_hash_register_default();
369 silc_hmac_register_default();
370 silc_show_public_key_file((char *)arg);
375 static void sig_init_finished(void)
377 /* Check ~/.silc directory and public and private keys */
378 if (!silc_client_check_silc_dir()) {
380 signal_emit("gui exit", 0);
384 /* Load public and private key */
385 if (!silc_client_load_keys(silc_client)) {
387 signal_emit("gui exit", 0);
391 /* Initialize the SILC client */
392 if (!silc_client_init(silc_client, settings_get_str("user_name"),
393 opt_hostname ? opt_hostname : silc_net_localhost(),
394 settings_get_str("real_name"))) {
396 signal_emit("gui exit", 0);
400 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
401 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
402 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
403 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
405 silc_hash_alloc("sha1", &sha1hash);
407 /* register SILC scheduler */
408 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
411 /* Init SILC. Called from src/fe-text/silc.c */
413 void silc_core_init(void)
415 static struct poptOption silc_options[] = {
416 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
417 { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
418 "List supported ciphers", NULL },
419 { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
420 "List supported hash functions", NULL },
421 { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
422 "List supported HMACs", NULL },
423 { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
424 "List supported PKCSs", NULL },
426 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
427 "Enable debugging", "STRING" },
428 #endif /* SILC_DEBUG */
429 { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
430 "Create new public key pair", NULL },
431 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
432 "Set the PKCS of the public key pair (-C)", "PKCS" },
433 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
434 "Set the length of the public key pair (-C)", "VALUE" },
435 { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
436 "Change the passphrase of private key file", "FILE" },
437 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
438 "Show the contents of the public key", "FILE" },
439 { NULL, '\0', 0, NULL }
442 CHAT_PROTOCOL_REC *rec;
443 SilcClientParams params;
444 const char *def_cipher, *def_hash, *def_hmac;
446 args_register(silc_options);
449 settings_add_bool("server", "skip_motd", FALSE);
450 settings_add_str("server", "alternate_nick", NULL);
451 settings_add_bool("server", "use_auto_addr", FALSE);
452 settings_add_str("server", "auto_bind_ip", "");
453 settings_add_str("server", "auto_public_ip", "");
454 settings_add_int("server", "auto_bind_port", 0);
455 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
456 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
457 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
458 settings_add_int("server", "key_exchange_timeout_secs", 120);
459 settings_add_int("server", "key_exchange_rekey_secs", 3600);
460 settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
461 settings_add_int("server", "connauth_request_secs", 2);
462 settings_add_int("server", "heartbeat", 300);
463 settings_add_bool("server", "ignore_message_signatures", FALSE);
464 settings_add_str("server", "session_filename", "session.$chatnet");
465 settings_add_bool("server", "sign_channel_messages", FALSE);
466 settings_add_bool("server", "sign_private_messages", FALSE);
468 /* Requested Attributes settings */
469 settings_add_bool("silc", "attr_allow", TRUE);
470 settings_add_str("silc", "attr_vcard", "");
471 settings_add_str("silc", "attr_services", "");
472 settings_add_str("silc", "attr_status_mood", "NORMAL");
473 settings_add_str("silc", "attr_status_text", "");
474 settings_add_str("silc", "attr_status_message", NULL);
475 settings_add_str("silc", "attr_preferred_language", "");
476 settings_add_str("silc", "attr_preferred_contact", "CHAT");
477 settings_add_bool("silc", "attr_timezone", TRUE);
478 settings_add_str("silc", "attr_geolocation", "");
479 settings_add_str("silc", "attr_device_info", NULL);
480 settings_add_str("silc", "attr_public_keys", "");
483 settings_add_bool("debug", "debug", FALSE);
484 settings_add_str("debug", "debug_string", "");
487 signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
488 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
490 silc_init_userinfo();
492 silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
493 silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
494 silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
495 silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
497 /* Initialize client parameters */
498 memset(¶ms, 0, sizeof(params));
499 strcat(params.nickname_format, "%n@%h%a");
500 params.nickname_parse = silc_nickname_format_parse;
501 params.connauth_request_secs = settings_get_int("connauth_request_secs");
503 /* Allocate SILC client */
504 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
506 /* Get the ciphers and stuff from config file */
507 def_cipher = settings_get_str("crypto_default_cipher");
508 def_hash = settings_get_str("crypto_default_hash");
509 def_hmac = settings_get_str("crypto_default_hmac");
510 silc_register_cipher(silc_client, def_cipher);
511 silc_register_hash(silc_client, def_hash);
512 silc_register_hmac(silc_client, def_hmac);
513 silc_pkcs_register_default();
515 /* Register SILC to the irssi */
516 rec = g_new0(CHAT_PROTOCOL_REC, 1);
518 rec->fullname = "Secure Internet Live Conferencing";
519 rec->chatnet = "silcnet";
520 rec->create_chatnet = create_chatnet;
521 rec->create_server_setup = create_server_setup;
522 rec->create_channel_setup = create_channel_setup;
523 rec->create_server_connect = create_server_connect;
524 rec->destroy_server_connect = destroy_server_connect;
525 rec->server_init_connect = silc_server_init_connect;
526 rec->server_connect = silc_server_connect;
527 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
530 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
533 chat_protocol_register(rec);
538 silc_channels_init();
540 silc_expandos_init();
542 silc_chatnets_init();
544 module_register("silc", "core");
547 /* Deinit SILC. Called from src/fe-text/silc.c */
549 void silc_core_deinit(void)
552 g_source_remove(idletag);
554 signal_emit("chat protocol deinit", 1,
555 chat_protocol_find("SILC"));
556 signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
557 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
560 silc_server_deinit();
561 silc_channels_deinit();
562 silc_queries_deinit();
563 silc_expandos_deinit();
565 silc_chatnets_deinit();
567 chat_protocol_unregister("SILC");
569 silc_pkcs_private_key_free(irssi_privkey);
570 silc_pkcs_public_key_free(irssi_pubkey);
571 silc_client_free(silc_client);