5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2001 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"
37 #include "fe-common/core/printtext.h"
38 #include "fe-common/core/fe-channels.h"
39 #include "fe-common/core/keyboard.h"
40 #include "fe-common/silc/module-formats.h"
42 /* Command line option variables */
43 static char *opt_pkcs = NULL;
44 static int opt_bits = 0;
46 static int idletag = -1;
48 SilcClient silc_client = NULL;
49 extern SilcClientOperations ops;
50 extern bool silc_debug;
51 extern bool silc_debug_hexdump;
53 void silc_expandos_init(void);
54 void silc_expandos_deinit(void);
56 void silc_lag_init(void);
57 void silc_lag_deinit(void);
59 static int my_silc_scheduler(void)
61 silc_client_run_one(silc_client);
65 static CHATNET_REC *create_chatnet(void)
67 return g_malloc0(sizeof(CHATNET_REC));
70 static SERVER_SETUP_REC *create_server_setup(void)
72 return g_malloc0(sizeof(SERVER_SETUP_REC));
75 static CHANNEL_SETUP_REC *create_channel_setup(void)
77 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
80 static SERVER_CONNECT_REC *create_server_connect(void)
82 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
85 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
90 /* Checks user information and saves them to the config file it they
91 do not exist there already. */
93 static void silc_init_userinfo(void)
95 const char *set, *nick, *user_name;
98 /* check if nick/username/realname wasn't read from setup.. */
99 set = settings_get_str("real_name");
100 if (set == NULL || *set == '\0') {
101 str = g_getenv("SILCNAME");
103 str = g_getenv("IRCNAME");
104 settings_set_str("real_name",
105 str != NULL ? str : silc_get_real_name());
109 user_name = settings_get_str("user_name");
110 if (user_name == NULL || *user_name == '\0') {
111 str = g_getenv("SILCUSER");
113 str = g_getenv("IRCUSER");
114 settings_set_str("user_name",
115 str != NULL ? str : silc_get_username());
117 user_name = settings_get_str("user_name");
121 nick = settings_get_str("nick");
122 if (nick == NULL || *nick == '\0') {
123 str = g_getenv("SILCNICK");
125 str = g_getenv("IRCNICK");
126 settings_set_str("nick", str != NULL ? str : user_name);
128 nick = settings_get_str("nick");
132 set = settings_get_str("alternate_nick");
133 if (set == NULL || *set == '\0') {
134 str = g_strconcat(nick, "_", NULL);
135 settings_set_str("alternate_nick", str);
140 set = settings_get_str("hostname");
141 if (set == NULL || *set == '\0') {
142 str = g_getenv("SILCHOST");
144 str = g_getenv("IRCHOST");
146 settings_set_str("hostname", str);
152 static bool silc_irssi_debug_print(char *file, char *function, int line,
153 char *message, void *context)
155 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
156 "DEBUG: %s:%d: %s", function, line, message);
161 static void sig_setup_changed(void)
164 bool debug = settings_get_bool("debug");
166 const char *debug_string = settings_get_str("debug_string");
167 i_debug = silc_debug = TRUE;
168 if (strlen(debug_string))
169 silc_log_set_debug_string(debug_string);
170 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
180 static bool silc_log_misc(SilcLogType type, char *message, void *context)
182 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
183 (type == SILC_LOG_INFO ? "[Info]" :
184 type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
188 static void silc_nickname_format_parse(const char *nickname,
191 silc_parse_userfqdn(nickname, ret_nickname, NULL);
194 static void silc_register_cipher(SilcClient client, const char *cipher)
199 for (i = 0; silc_default_ciphers[i].name; i++)
200 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
201 silc_cipher_register(&(silc_default_ciphers[i]));
205 if (!silc_cipher_is_supported(cipher)) {
206 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
211 /* Register other defaults */
212 silc_cipher_register_default();
215 static void silc_register_hash(SilcClient client, const char *hash)
220 for (i = 0; silc_default_hash[i].name; i++)
221 if (!strcmp(silc_default_hash[i].name, hash)) {
222 silc_hash_register(&(silc_default_hash[i]));
226 if (!silc_hash_is_supported(hash)) {
227 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
232 /* Register other defaults */
233 silc_hash_register_default();
236 static void silc_register_hmac(SilcClient client, const char *hmac)
241 for (i = 0; silc_default_hmacs[i].name; i++)
242 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
243 silc_hmac_register(&(silc_default_hmacs[i]));
247 if (!silc_hmac_is_supported(hmac)) {
248 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
253 /* Register other defaults */
254 silc_hmac_register_default();
257 /* Finalize init. Init finish signal calls this. */
259 void silc_opt_callback(poptContext con,
260 enum poptCallbackReason reason,
261 const struct poptOption *opt,
262 const char *arg, void *data)
264 if (strcmp(opt->longName, "list-ciphers") == 0) {
265 silc_cipher_register_default();
266 silc_client_list_ciphers();
270 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
271 silc_hash_register_default();
272 silc_client_list_hash_funcs();
276 if (strcmp(opt->longName, "list-hmacs") == 0) {
277 silc_hmac_register_default();
278 silc_client_list_hmacs();
282 if (strcmp(opt->longName, "list-pkcs") == 0) {
283 silc_pkcs_register_default();
284 silc_client_list_pkcs();
288 if (strcmp(opt->longName, "debug") == 0) {
290 silc_debug_hexdump = TRUE;
291 silc_log_set_debug_string(arg);
294 "Run-time debugging is not enabled. To enable it recompile\n"
295 "the client with --enable-debug configuration option.\n");
300 if (strcmp(opt->longName, "create-key-pair") == 0) {
301 /* Create new key pair and exit */
302 silc_cipher_register_default();
303 silc_pkcs_register_default();
304 silc_hash_register_default();
305 silc_hmac_register_default();
306 silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL, NULL,
307 NULL, NULL, NULL, NULL, TRUE);
311 if (strcmp(opt->longName, "passphrase-change") == 0) {
312 /* Change the passphrase of the private key file */
313 silc_cipher_register_default();
314 silc_pkcs_register_default();
315 silc_hash_register_default();
316 silc_hmac_register_default();
317 silc_change_private_key_passphrase(arg, NULL, NULL);
321 if (strcmp(opt->longName, "show-key") == 0) {
323 silc_cipher_register_default();
324 silc_pkcs_register_default();
325 silc_hash_register_default();
326 silc_hmac_register_default();
327 silc_show_public_key((char *)arg);
332 static void sig_init_finished(void)
334 /* Check ~/.silc directory and public and private keys */
335 if (!silc_client_check_silc_dir())
338 /* Load public and private key */
339 if (!silc_client_load_keys(silc_client))
342 /* Initialize the SILC client */
343 if (!silc_client_init(silc_client))
346 /* register SILC scheduler */
347 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
350 /* Init SILC. Called from src/fe-text/silc.c */
352 void silc_core_init(void)
354 static struct poptOption silc_options[] = {
355 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
356 { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
357 "List supported ciphers", NULL },
358 { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
359 "List supported hash functions", NULL },
360 { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
361 "List supported HMACs", NULL },
362 { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
363 "List supported PKCSs", NULL },
365 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
366 "Enable debugging", "STRING" },
367 #endif /* SILC_DEBUG */
368 { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
369 "Create new public key pair", NULL },
370 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
371 "Set the PKCS of the public key pair (-C)", "PKCS" },
372 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
373 "Set the length of the public key pair (-C)", "VALUE" },
374 { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
375 "Change the passphrase of private key file", "FILE" },
376 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
377 "Show the contents of the public key", "FILE" },
378 { NULL, '\0', 0, NULL }
381 CHAT_PROTOCOL_REC *rec;
382 SilcClientParams params;
383 const char *def_cipher, *def_hash, *def_hmac;
385 args_register(silc_options);
388 settings_add_bool("server", "skip_motd", FALSE);
389 settings_add_str("server", "alternate_nick", NULL);
390 settings_add_bool("server", "use_auto_addr", FALSE);
391 settings_add_str("server", "auto_bind_ip", "");
392 settings_add_str("server", "auto_public_ip", "");
393 settings_add_int("server", "auto_bind_port", 0);
394 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
395 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
396 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
397 settings_add_int("server", "key_exchange_timeout_secs", 120);
398 settings_add_int("server", "key_exchange_rekey_secs", 3600);
399 settings_add_int("server", "connauth_request_secs", 2);
400 settings_add_int("server", "heartbeat", 300);
401 settings_add_bool("server", "ignore_message_signatures", FALSE);
402 settings_add_str("server", "session_filename", "session.$chatnet");
404 /* Requested Attributes settings */
405 settings_add_bool("silc", "attr_allow", TRUE);
406 settings_add_str("silc", "attr_vcard", "");
407 settings_add_str("silc", "attr_services", "");
408 settings_add_str("silc", "attr_status_mood", "NORMAL");
409 settings_add_str("silc", "attr_status_text", "");
410 settings_add_str("silc", "attr_status_message", NULL);
411 settings_add_str("silc", "attr_preferred_language", "");
412 settings_add_str("silc", "attr_preferred_contact", "CHAT");
413 settings_add_bool("silc", "attr_timezone", TRUE);
414 settings_add_str("silc", "attr_geolocation", "");
415 settings_add_str("silc", "attr_device_info", NULL);
416 settings_add_str("silc", "attr_public_keys", "");
419 settings_add_bool("debug", "debug", FALSE);
420 settings_add_str("debug", "debug_string", "");
423 signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
424 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
426 silc_init_userinfo();
428 /* Initialize client parameters */
429 memset(¶ms, 0, sizeof(params));
430 strcat(params.nickname_format, "%n@%h%a");
431 params.nickname_parse = silc_nickname_format_parse;
432 params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
433 params.connauth_request_secs = settings_get_int("connauth_request_secs");
435 /* Allocate SILC client */
436 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
438 /* Get the ciphers and stuff from config file */
439 def_cipher = settings_get_str("crypto_default_cipher");
440 def_hash = settings_get_str("crypto_default_hash");
441 def_hmac = settings_get_str("crypto_default_hmac");
442 silc_register_cipher(silc_client, def_cipher);
443 silc_register_hash(silc_client, def_hash);
444 silc_register_hmac(silc_client, def_hmac);
445 silc_pkcs_register_default();
447 /* Get user information */
448 silc_client->username = g_strdup(settings_get_str("user_name"));
449 silc_client->nickname = g_strdup(settings_get_str("nick"));
450 silc_client->hostname = silc_net_localhost();
451 silc_client->realname = g_strdup(settings_get_str("real_name"));
453 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
454 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
455 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
456 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
458 /* Register SILC to the irssi */
459 rec = g_new0(CHAT_PROTOCOL_REC, 1);
461 rec->fullname = "Secure Internet Live Conferencing";
462 rec->chatnet = "silcnet";
463 rec->create_chatnet = create_chatnet;
464 rec->create_server_setup = create_server_setup;
465 rec->create_channel_setup = create_channel_setup;
466 rec->create_server_connect = create_server_connect;
467 rec->destroy_server_connect = destroy_server_connect;
468 rec->server_init_connect = silc_server_init_connect;
469 rec->server_connect = silc_server_connect;
470 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
473 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
476 chat_protocol_register(rec);
480 silc_channels_init();
482 silc_expandos_init();
484 silc_chatnets_init();
486 module_register("silc", "core");
489 /* Deinit SILC. Called from src/fe-text/silc.c */
491 void silc_core_deinit(void)
494 g_source_remove(idletag);
496 signal_emit("chat protocol deinit", 1,
497 chat_protocol_find("SILC"));
498 signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
499 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
501 silc_server_deinit();
502 silc_channels_deinit();
503 silc_queries_deinit();
504 silc_expandos_deinit();
506 silc_chatnets_deinit();
508 chat_protocol_unregister("SILC");
510 g_free(silc_client->username);
511 g_free(silc_client->realname);
512 silc_free(silc_client->hostname);
513 silc_pkcs_free(silc_client->pkcs);
514 silc_pkcs_private_key_free(silc_client->private_key);
515 silc_pkcs_public_key_free(silc_client->public_key);
516 silc_client_free(silc_client);