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 "version_internal.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 bool opt_create_keypair = FALSE;
45 static bool opt_list_ciphers = FALSE;
46 static bool opt_list_hash = FALSE;
47 static bool opt_list_hmac = FALSE;
48 static bool opt_list_pkcs = FALSE;
49 static bool opt_version = FALSE;
50 static char *opt_debug = FALSE;
51 static char *opt_pkcs = NULL;
52 static char *opt_keyfile = NULL;
53 static int opt_bits = 0;
57 SilcClient silc_client = NULL;
58 extern SilcClientOperations ops;
59 extern bool silc_debug;
60 extern bool silc_debug_hexdump;
62 /* SIM (SILC Module) table */
63 SilcSimContext **sims = NULL;
64 uint32 sims_count = 0;
67 static int my_silc_scheduler(void)
69 silc_client_run_one(silc_client);
73 static CHATNET_REC *create_chatnet(void)
75 return g_malloc0(sizeof(CHATNET_REC));
78 static SERVER_SETUP_REC *create_server_setup(void)
80 return g_malloc0(sizeof(SERVER_SETUP_REC));
83 static CHANNEL_SETUP_REC *create_channel_setup(void)
85 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
88 static SERVER_CONNECT_REC *create_server_connect(void)
90 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
93 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
98 /* Checks user information and saves them to the config file it they
99 do not exist there already. */
101 static void silc_init_userinfo(void)
103 const char *set, *nick, *user_name;
106 /* check if nick/username/realname wasn't read from setup.. */
107 set = settings_get_str("real_name");
108 if (set == NULL || *set == '\0') {
109 str = g_getenv("SILCNAME");
111 str = g_getenv("IRCNAME");
112 settings_set_str("real_name",
113 str != NULL ? str : g_get_real_name());
117 user_name = settings_get_str("user_name");
118 if (user_name == NULL || *user_name == '\0') {
119 str = g_getenv("SILCUSER");
121 str = g_getenv("IRCUSER");
122 settings_set_str("user_name",
123 str != NULL ? str : g_get_user_name());
125 user_name = settings_get_str("user_name");
129 nick = settings_get_str("nick");
130 if (nick == NULL || *nick == '\0') {
131 str = g_getenv("SILCNICK");
133 str = g_getenv("IRCNICK");
134 settings_set_str("nick", str != NULL ? str : user_name);
136 nick = settings_get_str("nick");
140 set = settings_get_str("alternate_nick");
141 if (set == NULL || *set == '\0') {
142 if (strlen(nick) < 9)
143 str = g_strconcat(nick, "_", NULL);
145 str = g_strdup(nick);
146 str[strlen(str)-1] = '_';
148 settings_set_str("alternate_nick", str);
153 set = settings_get_str("hostname");
154 if (set == NULL || *set == '\0') {
155 str = g_getenv("SILCHOST");
157 str = g_getenv("IRCHOST");
159 settings_set_str("hostname", str);
165 static bool silc_log_misc(SilcLogType type, char *message, void *context)
167 fprintf(stderr, "%s\n", message);
171 static void silc_nickname_format_parse(const char *nickname,
174 silc_parse_userfqdn(nickname, ret_nickname, NULL);
177 static void silc_register_cipher(SilcClient client, const char *cipher)
181 for (i = 0; silc_default_ciphers[i].name; i++)
182 if (!strcmp(silc_default_ciphers[i].name, cipher)) {
183 silc_cipher_register(&silc_default_ciphers[i]);
187 if (!silc_cipher_is_supported(cipher)) {
188 SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
192 /* Register other defaults */
193 silc_cipher_register_default();
196 static void silc_register_hash(SilcClient client, const char *hash)
200 for (i = 0; silc_default_hash[i].name; i++)
201 if (!strcmp(silc_default_hash[i].name, hash)) {
202 silc_hash_register(&silc_default_hash[i]);
206 if (!silc_hash_is_supported(hash)) {
207 SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
211 /* Register other defaults */
212 silc_hash_register_default();
215 static void silc_register_hmac(SilcClient client, const char *hmac)
219 for (i = 0; silc_default_hmacs[i].name; i++)
220 if (!strcmp(silc_default_hmacs[i].name, hmac)) {
221 silc_hmac_register(&silc_default_hmacs[i]);
225 if (!silc_hmac_is_supported(hmac)) {
226 SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
230 /* Register other defaults */
231 silc_hmac_register_default();
234 /* Finalize init. Init finish signal calls this. */
236 void silc_core_init_finish(SERVER_REC *server)
238 CHAT_PROTOCOL_REC *rec;
239 SilcClientParams params;
240 const char *def_cipher, *def_hash, *def_hmac;
242 if (opt_create_keypair == TRUE) {
243 /* Create new key pair and exit */
244 silc_cipher_register_default();
245 silc_pkcs_register_default();
246 silc_hash_register_default();
247 silc_hmac_register_default();
248 silc_client_create_key_pair(opt_pkcs, opt_bits,
249 NULL, NULL, NULL, NULL, NULL);
255 silc_cipher_register_default();
256 silc_pkcs_register_default();
257 silc_hash_register_default();
258 silc_hmac_register_default();
259 silc_client_show_key(opt_keyfile);
263 if (opt_list_ciphers) {
264 silc_cipher_register_default();
265 silc_client_list_ciphers();
270 silc_hash_register_default();
271 silc_client_list_hash_funcs();
276 silc_hmac_register_default();
277 silc_client_list_hmacs();
282 silc_pkcs_register_default();
283 silc_client_list_pkcs();
288 printf("SILC Secure Internet Live Conferencing, version %s "
289 "(base: SILC Toolkit %s)\n", silc_dist_version, silc_version);
290 printf("(c) 1997 - 2001 Pekka Riikonen <priikone@silcnet.org>\n");
296 silc_debug_hexdump = TRUE;
297 silc_log_set_debug_string(opt_debug);
298 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
299 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
300 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
301 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
304 "Run-time debugging is not enabled. To enable it recompile\n"
305 "the client with --enable-debug configuration option.\n");
309 /* Do some irssi initializing */
310 settings_add_bool("server", "skip_motd", FALSE);
311 settings_add_str("server", "alternate_nick", NULL);
313 /* Initialize the auto_addr variables Is "server" the best choice for
314 * this? No existing category seems to apply.
316 settings_add_bool("server", "use_auto_addr", FALSE);
317 settings_add_str("server", "auto_bind_ip", "");
318 settings_add_str("server", "auto_public_ip", "");
319 settings_add_int("server", "auto_bind_port", 0);
321 silc_init_userinfo();
323 /* Initialize client parameters */
324 memset(¶ms, 0, sizeof(params));
325 strcat(params.nickname_format, "%n@%h%a");
326 params.nickname_parse = silc_nickname_format_parse;
328 /* Allocate SILC client */
329 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
331 /* Crypto settings */
332 settings_add_str("server", "crypto_default_cipher", "aes-256-cbc");
333 settings_add_str("server", "crypto_default_hash", "sha1");
334 settings_add_str("server", "crypto_default_hmac", "hmac-sha1-96");
336 /* Get the ciphers and stuff from config file */
337 def_cipher = settings_get_str("crypto_default_cipher");
338 def_hash = settings_get_str("crypto_default_hash");
339 def_hmac = settings_get_str("crypto_default_hmac");
340 silc_register_cipher(silc_client, def_cipher);
341 silc_register_hash(silc_client, def_hash);
342 silc_register_hmac(silc_client, def_hmac);
343 silc_pkcs_register_default();
345 /* Get user information */
346 silc_client->username = g_strdup(settings_get_str("user_name"));
347 silc_client->nickname = g_strdup(settings_get_str("nick"));
348 silc_client->hostname = silc_net_localhost();
349 silc_client->realname = g_strdup(settings_get_str("real_name"));
351 /* Check ~/.silc directory and public and private keys */
352 if (silc_client_check_silc_dir() == FALSE) {
357 /* Load public and private key */
358 if (silc_client_load_keys(silc_client) == FALSE) {
363 /* Initialize the SILC client */
364 if (!silc_client_init(silc_client)) {
369 /* Register SILC to the irssi */
370 rec = g_new0(CHAT_PROTOCOL_REC, 1);
372 rec->fullname = "Secure Internet Live Conferencing";
373 rec->chatnet = "silcnet";
374 rec->create_chatnet = create_chatnet;
375 rec->create_server_setup = create_server_setup;
376 rec->create_channel_setup = create_channel_setup;
377 rec->create_server_connect = create_server_connect;
378 rec->destroy_server_connect = destroy_server_connect;
379 rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
381 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
383 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
386 chat_protocol_register(rec);
390 silc_channels_init();
393 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
396 /* Init SILC. Called from src/fe-text/silc.c */
398 void silc_core_init(void)
400 static struct poptOption options[] = {
401 { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0,
402 "Create new public key pair", NULL },
403 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
404 "Set the PKCS of the public key pair", "PKCS" },
405 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
406 "Set the length of the public key pair", "VALUE" },
407 { "show-key", 'S', POPT_ARG_STRING, &opt_keyfile, 0,
408 "Show the contents of the public key", "FILE" },
409 { "list-ciphers", 'c', POPT_ARG_NONE, &opt_list_ciphers, 0,
410 "List supported ciphers", NULL },
411 { "list-hash-funcs", 'H', POPT_ARG_NONE, &opt_list_hash, 0,
412 "List supported hash functions", NULL },
413 { "list-hmacs", 'M', POPT_ARG_NONE, &opt_list_hmac, 0,
414 "List supported HMACs", NULL },
415 { "list-pkcs", 'P', POPT_ARG_NONE, &opt_list_pkcs, 0,
416 "List supported PKCSs", NULL },
417 { "debug", 'd', POPT_ARG_STRING, &opt_debug, 0,
418 "Enable debugging", "STRING" },
419 { "version", 'V', POPT_ARG_NONE, &opt_version, 0,
420 "Show version", NULL },
421 { NULL, '\0', 0, NULL }
424 signal_add("irssi init finished", (SIGNAL_FUNC) silc_core_init_finish);
426 args_register(options);
429 /* Deinit SILC. Called from src/fe-text/silc.c */
431 void silc_core_deinit(void)
434 signal_emit("chat protocol deinit", 1,
435 chat_protocol_find("SILC"));
437 silc_server_deinit();
438 silc_channels_deinit();
439 silc_queries_deinit();
441 chat_protocol_unregister("SILC");
443 g_source_remove(idletag);
446 g_free(silc_client->username);
447 g_free(silc_client->realname);
448 silc_client_free(silc_client);