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 "silcversion.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 bool opt_create_keypair = FALSE;
44 static char *opt_pkcs = NULL;
45 static int opt_bits = 0;
49 SilcClient silc_client = NULL;
50 extern SilcClientOperations ops;
51 extern bool silc_debug;
52 extern bool silc_debug_hexdump;
54 void silc_expandos_init(void);
55 void silc_expandos_deinit(void);
57 static int my_silc_scheduler(void)
59 silc_client_run_one(silc_client);
63 static CHATNET_REC *create_chatnet(void)
65 return g_malloc0(sizeof(CHATNET_REC));
68 static SERVER_SETUP_REC *create_server_setup(void)
70 return g_malloc0(sizeof(SERVER_SETUP_REC));
73 static CHANNEL_SETUP_REC *create_channel_setup(void)
75 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
78 static SERVER_CONNECT_REC *create_server_connect(void)
80 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
83 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
88 /* Checks user information and saves them to the config file it they
89 do not exist there already. */
91 static void silc_init_userinfo(void)
93 const char *set, *nick, *user_name;
96 /* check if nick/username/realname wasn't read from setup.. */
97 set = settings_get_str("real_name");
98 if (set == NULL || *set == '\0') {
99 str = g_getenv("SILCNAME");
101 str = g_getenv("IRCNAME");
102 settings_set_str("real_name",
103 str != NULL ? str : silc_get_real_name());
107 user_name = settings_get_str("user_name");
108 if (user_name == NULL || *user_name == '\0') {
109 str = g_getenv("SILCUSER");
111 str = g_getenv("IRCUSER");
112 settings_set_str("user_name",
113 str != NULL ? str : silc_get_username());
115 user_name = settings_get_str("user_name");
119 nick = settings_get_str("nick");
120 if (nick == NULL || *nick == '\0') {
121 str = g_getenv("SILCNICK");
123 str = g_getenv("IRCNICK");
124 settings_set_str("nick", str != NULL ? str : user_name);
126 nick = settings_get_str("nick");
130 set = settings_get_str("alternate_nick");
131 if (set == NULL || *set == '\0') {
132 str = g_strconcat(nick, "_", NULL);
133 settings_set_str("alternate_nick", str);
138 set = settings_get_str("hostname");
139 if (set == NULL || *set == '\0') {
140 str = g_getenv("SILCHOST");
142 str = g_getenv("IRCHOST");
144 settings_set_str("hostname", str);
150 static bool silc_irssi_debug_print(char *file, char *function, int line,
151 char *message, void *context)
153 printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
154 "DEBUG: %s:%d: %s", function, line, message);
158 static void sig_debug_setup_changed(void)
160 bool debug = settings_get_bool("debug");
162 const char *debug_string = settings_get_str("debug_string");
163 i_debug = silc_debug = TRUE;
164 if (strlen(debug_string))
165 silc_log_set_debug_string(debug_string);
166 silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
176 static bool silc_log_misc(SilcLogType type, char *message, void *context)
178 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", message);
182 static void silc_nickname_format_parse(const char *nickname,
185 silc_parse_userfqdn(nickname, ret_nickname, NULL);
188 static void silc_register_cipher(SilcClient client, const char *cipher)
193 for (i = 0; silc_default_ciphers[i].name; i++)
194 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
195 silc_cipher_register(&(silc_default_ciphers[i]));
199 if (!silc_cipher_is_supported(cipher)) {
200 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
205 /* Register other defaults */
206 silc_cipher_register_default();
209 static void silc_register_hash(SilcClient client, const char *hash)
214 for (i = 0; silc_default_hash[i].name; i++)
215 if (!strcmp(silc_default_hash[i].name, hash)) {
216 silc_hash_register(&(silc_default_hash[i]));
220 if (!silc_hash_is_supported(hash)) {
221 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
226 /* Register other defaults */
227 silc_hash_register_default();
230 static void silc_register_hmac(SilcClient client, const char *hmac)
235 for (i = 0; silc_default_hmacs[i].name; i++)
236 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
237 silc_hmac_register(&(silc_default_hmacs[i]));
241 if (!silc_hmac_is_supported(hmac)) {
242 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
247 /* Register other defaults */
248 silc_hmac_register_default();
251 /* Finalize init. Init finish signal calls this. */
253 void silc_opt_callback(poptContext con,
254 enum poptCallbackReason reason,
255 const struct poptOption *opt,
256 const char *arg, void *data)
258 if (strcmp(opt->longName, "show-key") == 0) {
260 silc_cipher_register_default();
261 silc_pkcs_register_default();
262 silc_hash_register_default();
263 silc_hmac_register_default();
264 silc_client_show_key((char *)arg);
268 if (strcmp(opt->longName, "list-ciphers") == 0) {
269 silc_cipher_register_default();
270 silc_client_list_ciphers();
274 if (strcmp(opt->longName, "list-hash-funcs") == 0) {
275 silc_hash_register_default();
276 silc_client_list_hash_funcs();
280 if (strcmp(opt->longName, "list-hmacs") == 0) {
281 silc_hmac_register_default();
282 silc_client_list_hmacs();
286 if (strcmp(opt->longName, "list-pkcs") == 0) {
287 silc_pkcs_register_default();
288 silc_client_list_pkcs();
292 if (strcmp(opt->longName, "debug") == 0) {
294 silc_debug_hexdump = TRUE;
295 silc_log_set_debug_string(arg);
296 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
297 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
298 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
299 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
302 "Run-time debugging is not enabled. To enable it recompile\n"
303 "the client with --enable-debug configuration option.\n");
309 static void sig_init_read_settings(void)
311 if (opt_create_keypair) {
312 /* Create new key pair and exit */
313 silc_cipher_register_default();
314 silc_pkcs_register_default();
315 silc_hash_register_default();
316 silc_hmac_register_default();
317 silc_client_create_key_pair(opt_pkcs, opt_bits,
318 NULL, NULL, NULL, NULL, NULL);
323 /* Init SILC. Called from src/fe-text/silc.c */
325 void silc_core_init(void)
327 static struct poptOption silc_options[] = {
328 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
329 { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
330 "Show the contents of the public key", "FILE" },
331 { "list-ciphers", 'c', POPT_ARG_NONE, NULL, 0,
332 "List supported ciphers", NULL },
333 { "list-hash-funcs", 'H', POPT_ARG_NONE, NULL, 0,
334 "List supported hash functions", NULL },
335 { "list-hmacs", 'M', POPT_ARG_NONE, NULL, 0,
336 "List supported HMACs", NULL },
337 { "list-pkcs", 'P', POPT_ARG_NONE, NULL, 0,
338 "List supported PKCSs", NULL },
339 { "debug", 'd', POPT_ARG_STRING, NULL, 0,
340 "Enable debugging", "STRING" },
341 { NULL, '\0', 0, NULL }
344 static struct poptOption options[] = {
345 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, silc_options, 0, NULL, NULL },
346 { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0,
347 "Create new public key pair", NULL },
348 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
349 "Set the PKCS of the public key pair", "PKCS" },
350 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
351 "Set the length of the public key pair", "VALUE" },
352 { NULL, '\0', 0, NULL }
355 CHAT_PROTOCOL_REC *rec;
356 SilcClientParams params;
357 const char *def_cipher, *def_hash, *def_hmac;
359 args_register(options);
360 signal_add("irssi init read settings", (SIGNAL_FUNC) sig_init_read_settings);
363 settings_add_bool("server", "skip_motd", FALSE);
364 settings_add_str("server", "alternate_nick", NULL);
365 settings_add_bool("server", "use_auto_addr", FALSE);
366 settings_add_str("server", "auto_bind_ip", "");
367 settings_add_str("server", "auto_public_ip", "");
368 settings_add_int("server", "auto_bind_port", 0);
369 settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
370 settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
371 settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
372 settings_add_int("server", "key_exchange_timeout_secs", 120);
373 settings_add_int("server", "key_exchange_rekey_secs", 3600);
374 settings_add_int("server", "connauth_request_secs", 2);
377 settings_add_bool("debug", "debug", FALSE);
378 settings_add_str("debug", "debug_string", "");
379 signal_add("setup changed", (SIGNAL_FUNC) sig_debug_setup_changed);
382 silc_init_userinfo();
384 /* Initialize client parameters */
385 memset(¶ms, 0, sizeof(params));
386 strcat(params.nickname_format, "%n@%h%a");
387 params.nickname_parse = silc_nickname_format_parse;
388 params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
389 params.connauth_request_secs = settings_get_int("connauth_request_secs");
391 /* Allocate SILC client */
392 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
394 /* Get the ciphers and stuff from config file */
395 def_cipher = settings_get_str("crypto_default_cipher");
396 def_hash = settings_get_str("crypto_default_hash");
397 def_hmac = settings_get_str("crypto_default_hmac");
398 silc_register_cipher(silc_client, def_cipher);
399 silc_register_hash(silc_client, def_hash);
400 silc_register_hmac(silc_client, def_hmac);
401 silc_pkcs_register_default();
403 /* Get user information */
404 silc_client->username = g_strdup(settings_get_str("user_name"));
405 silc_client->nickname = g_strdup(settings_get_str("nick"));
406 silc_client->hostname = silc_net_localhost();
407 silc_client->realname = g_strdup(settings_get_str("real_name"));
409 /* Check ~/.silc directory and public and private keys */
410 if (silc_client_check_silc_dir() == FALSE) {
415 /* Load public and private key */
416 if (silc_client_load_keys(silc_client) == FALSE) {
421 /* Initialize the SILC client */
422 if (!silc_client_init(silc_client)) {
427 /* Register SILC to the irssi */
428 rec = g_new0(CHAT_PROTOCOL_REC, 1);
430 rec->fullname = "Secure Internet Live Conferencing";
431 rec->chatnet = "silcnet";
432 rec->create_chatnet = create_chatnet;
433 rec->create_server_setup = create_server_setup;
434 rec->create_channel_setup = create_channel_setup;
435 rec->create_server_connect = create_server_connect;
436 rec->destroy_server_connect = destroy_server_connect;
437 rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
439 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
441 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
444 chat_protocol_register(rec);
448 silc_channels_init();
450 silc_expandos_init();
452 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
454 module_register("silc", "core");
457 /* Deinit SILC. Called from src/fe-text/silc.c */
459 void silc_core_deinit(void)
462 signal_emit("chat protocol deinit", 1,
463 chat_protocol_find("SILC"));
464 signal_remove("irssi init read settings",
465 (SIGNAL_FUNC) sig_init_read_settings);
467 signal_remove("setup changed", (SIGNAL_FUNC) sig_debug_setup_changed);
470 silc_server_deinit();
471 silc_channels_deinit();
472 silc_queries_deinit();
473 silc_expandos_deinit();
475 chat_protocol_unregister("SILC");
477 g_source_remove(idletag);
480 g_free(silc_client->username);
481 g_free(silc_client->realname);
482 silc_client_free(silc_client);