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;
55 SilcPrivateKey irssi_privkey;
57 /* Default hash function */
60 void silc_expandos_init(void);
61 void silc_expandos_deinit(void);
63 void silc_lag_init(void);
64 void silc_lag_deinit(void);
66 static int my_silc_scheduler(void)
68 silc_client_run_one(silc_client);
72 static CHATNET_REC *create_chatnet(void)
74 return g_malloc0(sizeof(CHATNET_REC));
77 static SERVER_SETUP_REC *create_server_setup(void)
79 return g_malloc0(sizeof(SERVER_SETUP_REC));
82 static CHANNEL_SETUP_REC *create_channel_setup(void)
84 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
87 static SERVER_CONNECT_REC *create_server_connect(void)
89 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
92 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
97 /* Checks user information and saves them to the config file it they
98 do not exist there already. */
100 static void silc_init_userinfo(void)
102 const char *set, *nick, *user_name, *str;
105 /* check if nick/username/realname wasn't read from setup.. */
106 set = settings_get_str("real_name");
107 if (set == NULL || *set == '\0') {
108 str = g_getenv("SILCNAME");
110 str = g_getenv("IRCNAME");
111 settings_set_str("real_name",
112 str != NULL ? str : silc_get_real_name());
116 user_name = settings_get_str("user_name");
117 if (user_name == NULL || *user_name == '\0') {
118 str = g_getenv("SILCUSER");
120 str = g_getenv("IRCUSER");
121 settings_set_str("user_name",
122 str != NULL ? str : silc_get_username());
124 user_name = settings_get_str("user_name");
128 nick = settings_get_str("nick");
129 if (nick == NULL || *nick == '\0') {
130 str = g_getenv("SILCNICK");
132 str = g_getenv("IRCNICK");
133 settings_set_str("nick", str != NULL ? str : user_name);
135 nick = settings_get_str("nick");
139 set = settings_get_str("alternate_nick");
140 if (set == NULL || *set == '\0') {
141 tmp = g_strconcat(nick, "_", NULL);
142 settings_set_str("alternate_nick", tmp);
147 set = settings_get_str("hostname");
148 if (set == NULL || *set == '\0') {
149 str = g_getenv("SILCHOST");
151 str = g_getenv("IRCHOST");
153 settings_set_str("hostname", str);
159 static bool silc_irssi_debug_print(char *file, char *function, int line,
160 char *message, void *context)
162 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
163 "DEBUG: %s:%d: %s", function, line, message);
168 static void sig_setup_changed(void)
171 bool debug = settings_get_bool("debug");
173 const char *debug_string = settings_get_str("debug_string");
175 silc_log_debug(TRUE);
176 if (strlen(debug_string))
177 silc_log_set_debug_string(debug_string);
178 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
182 silc_log_debug(FALSE);
188 static bool silc_log_misc(SilcLogType type, char *message, void *context)
190 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
191 (type == SILC_LOG_INFO ? "[Info]" :
192 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
196 static void silc_nickname_format_parse(const char *nickname,
199 silc_parse_userfqdn(nickname, *ret_nickname, 128 + 1, NULL, 0);
202 static void silc_register_cipher(SilcClient client, const char *cipher)
207 for (i = 0; silc_default_ciphers[i].name; i++)
208 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
209 silc_cipher_register(&(silc_default_ciphers[i]));
213 if (!silc_cipher_is_supported(cipher)) {
214 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
219 /* Register other defaults */
220 silc_cipher_register_default();
223 static void silc_register_hash(SilcClient client, const char *hash)
228 for (i = 0; silc_default_hash[i].name; i++)
229 if (!strcmp(silc_default_hash[i].name, hash)) {
230 silc_hash_register(&(silc_default_hash[i]));
234 if (!silc_hash_is_supported(hash)) {
235 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
240 /* Register other defaults */
241 silc_hash_register_default();
244 static void silc_register_hmac(SilcClient client, const char *hmac)
249 for (i = 0; silc_default_hmacs[i].name; i++)
250 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
251 silc_hmac_register(&(silc_default_hmacs[i]));
255 if (!silc_hmac_is_supported(hmac)) {
256 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
261 /* Register other defaults */
262 silc_hmac_register_default();
265 /* Finalize init. Init finish signal calls this. */
267 void silc_opt_callback(poptContext con,
268 enum poptCallbackReason reason,
269 const struct poptOption *opt,
270 const char *arg, void *data)
273 if (strcmp(opt->longName, "nick") == 0) {
274 g_free(silc_client->nickname);
275 silc_client->nickname = g_strdup(arg);
278 if (strcmp(opt->longName, "hostname") == 0) {
279 silc_free(silc_client->hostname);
280 silc_client->hostname = g_strdup(arg);
284 if (strcmp(opt->longName, "list-ciphers") == 0) {
285 silc_cipher_register_default();
286 silc_client_list_ciphers();
290 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
291 silc_hash_register_default();
292 silc_client_list_hash_funcs();
296 if (strcmp(opt->longName, "list-hmacs") == 0) {
297 silc_hmac_register_default();
298 silc_client_list_hmacs();
302 if (strcmp(opt->longName, "list-pkcs") == 0) {
303 silc_pkcs_register_default();
304 silc_client_list_pkcs();
308 if (strcmp(opt->longName, "debug") == 0) {
309 silc_log_debug(TRUE);
310 silc_log_debug_hexdump(TRUE);
311 silc_log_set_debug_string(arg);
314 "Run-time debugging is not enabled. To enable it recompile\n"
315 "the client with --enable-debug configuration option.\n");
320 if (strcmp(opt->longName, "create-key-pair") == 0) {
321 /* Create new key pair and exit */
322 silc_cipher_register_default();
323 silc_pkcs_register_default();
324 silc_hash_register_default();
325 silc_hmac_register_default();
326 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL,
327 NULL, NULL, NULL, NULL, TRUE);
331 if (strcmp(opt->longName, "passphrase-change") == 0) {
332 /* Change the passphrase of the private key file */
333 silc_cipher_register_default();
334 silc_pkcs_register_default();
335 silc_hash_register_default();
336 silc_hmac_register_default();
337 silc_change_private_key_passphrase(arg, NULL, NULL);
341 if (strcmp(opt->longName, "show-key") == 0) {
343 silc_cipher_register_default();
344 silc_pkcs_register_default();
345 silc_hash_register_default();
346 silc_hmac_register_default();
347 silc_show_public_key_file((char *)arg);
352 static void sig_init_finished(void)
354 /* Check ~/.silc directory and public and private keys */
355 if (!silc_client_check_silc_dir())
358 /* Load public and private key */
359 if (!silc_client_load_keys(silc_client))
362 /* Initialize the SILC client */
363 if (!silc_client_init(silc_client, settings_get_str("user_name"),
364 silc_net_localhost(), settings_get_str("real_name")))
367 /* register SILC scheduler */
368 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
371 /* Init SILC. Called from src/fe-text/silc.c */
373 void silc_core_init(void)
375 static struct poptOption silc_options[] = {
376 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
377 { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
378 "List supported ciphers", NULL },
379 { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
380 "List supported hash functions", NULL },
381 { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
382 "List supported HMACs", NULL },
383 { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
384 "List supported PKCSs", NULL },
386 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
387 "Enable debugging", "STRING" },
388 #endif /* SILC_DEBUG */
389 { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
390 "Create new public key pair", NULL },
391 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
392 "Set the PKCS of the public key pair (-C)", "PKCS" },
393 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
394 "Set the length of the public key pair (-C)", "VALUE" },
395 { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
396 "Change the passphrase of private key file", "FILE" },
397 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
398 "Show the contents of the public key", "FILE" },
399 { NULL, '\0', 0, NULL }
402 CHAT_PROTOCOL_REC *rec;
403 SilcClientParams params;
404 const char *def_cipher, *def_hash, *def_hmac;
406 args_register(silc_options);
409 settings_add_bool("server", "skip_motd", FALSE);
410 settings_add_str("server", "alternate_nick", NULL);
411 settings_add_bool("server", "use_auto_addr", FALSE);
412 settings_add_str("server", "auto_bind_ip", "");
413 settings_add_str("server", "auto_public_ip", "");
414 settings_add_int("server", "auto_bind_port", 0);
415 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
416 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
417 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
418 settings_add_int("server", "key_exchange_timeout_secs", 120);
419 settings_add_int("server", "key_exchange_rekey_secs", 3600);
420 settings_add_int("server", "connauth_request_secs", 2);
421 settings_add_int("server", "heartbeat", 300);
422 settings_add_bool("server", "ignore_message_signatures", FALSE);
423 settings_add_str("server", "session_filename", "session.$chatnet");
425 /* Requested Attributes settings */
426 settings_add_bool("silc", "attr_allow", TRUE);
427 settings_add_str("silc", "attr_vcard", "");
428 settings_add_str("silc", "attr_services", "");
429 settings_add_str("silc", "attr_status_mood", "NORMAL");
430 settings_add_str("silc", "attr_status_text", "");
431 settings_add_str("silc", "attr_status_message", NULL);
432 settings_add_str("silc", "attr_preferred_language", "");
433 settings_add_str("silc", "attr_preferred_contact", "CHAT");
434 settings_add_bool("silc", "attr_timezone", TRUE);
435 settings_add_str("silc", "attr_geolocation", "");
436 settings_add_str("silc", "attr_device_info", NULL);
437 settings_add_str("silc", "attr_public_keys", "");
440 settings_add_bool("debug", "debug", FALSE);
441 settings_add_str("debug", "debug_string", "");
444 signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
445 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
447 silc_init_userinfo();
449 /* Initialize client parameters */
450 memset(¶ms, 0, sizeof(params));
451 strcat(params.nickname_format, "%n@%h%a");
452 params.nickname_parse = silc_nickname_format_parse;
453 params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
454 params.connauth_request_secs = settings_get_int("connauth_request_secs");
456 /* Allocate SILC client */
457 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
459 /* Get the ciphers and stuff from config file */
460 def_cipher = settings_get_str("crypto_default_cipher");
461 def_hash = settings_get_str("crypto_default_hash");
462 def_hmac = settings_get_str("crypto_default_hmac");
463 silc_register_cipher(silc_client, def_cipher);
464 silc_register_hash(silc_client, def_hash);
465 silc_register_hmac(silc_client, def_hmac);
466 silc_pkcs_register_default();
468 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
469 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
470 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
471 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
473 /* Register SILC to the irssi */
474 rec = g_new0(CHAT_PROTOCOL_REC, 1);
476 rec->fullname = "Secure Internet Live Conferencing";
477 rec->chatnet = "silcnet";
478 rec->create_chatnet = create_chatnet;
479 rec->create_server_setup = create_server_setup;
480 rec->create_channel_setup = create_channel_setup;
481 rec->create_server_connect = create_server_connect;
482 rec->destroy_server_connect = destroy_server_connect;
483 rec->server_init_connect = silc_server_init_connect;
484 rec->server_connect = silc_server_connect;
485 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
488 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
491 chat_protocol_register(rec);
496 silc_channels_init();
498 silc_expandos_init();
500 silc_chatnets_init();
502 module_register("silc", "core");
505 /* Deinit SILC. Called from src/fe-text/silc.c */
507 void silc_core_deinit(void)
510 g_source_remove(idletag);
512 signal_emit("chat protocol deinit", 1,
513 chat_protocol_find("SILC"));
514 signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
515 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
518 silc_server_deinit();
519 silc_channels_deinit();
520 silc_queries_deinit();
521 silc_expandos_deinit();
523 silc_chatnets_deinit();
525 chat_protocol_unregister("SILC");
527 silc_pkcs_private_key_free(irssi_privkey);
528 silc_pkcs_public_key_free(irssi_pubkey);
529 silc_client_free(silc_client);