Merged silc_1_0_branch to trunk.
[silc.git] / apps / irssi / src / silc / core / silc-core.c
1 /*
2
3   silc-core.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2001, 2005 Pekka Riikonen
8
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.
13
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.
18
19 */
20
21 #include "module.h"
22 #include "chat-protocols.h"
23 #include "args.h"
24
25 #include "chatnets.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"
34
35 #include "signals.h"
36 #include "levels.h"
37 #include "settings.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"
42
43 /* Command line option variables */
44 static char *opt_pkcs = NULL;
45 static int opt_bits = 0;
46
47 static int idletag = -1;
48
49 SilcClient silc_client = NULL;
50 extern SilcClientOperations ops;
51
52 void silc_expandos_init(void);
53 void silc_expandos_deinit(void);
54
55 void silc_lag_init(void);
56 void silc_lag_deinit(void);
57
58 static int my_silc_scheduler(void)
59 {
60   silc_client_run_one(silc_client);
61   return 1;
62 }
63
64 static CHATNET_REC *create_chatnet(void)
65 {
66   return g_malloc0(sizeof(CHATNET_REC));
67 }
68
69 static SERVER_SETUP_REC *create_server_setup(void)
70 {
71   return g_malloc0(sizeof(SERVER_SETUP_REC));
72 }
73
74 static CHANNEL_SETUP_REC *create_channel_setup(void)
75 {
76   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
77 }
78
79 static SERVER_CONNECT_REC *create_server_connect(void)
80 {
81   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
82 }
83
84 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
85 {
86
87 }
88
89 /* Checks user information and saves them to the config file it they
90    do not exist there already. */
91
92 static void silc_init_userinfo(void)
93 {
94   const char *set, *nick, *user_name;
95   char *str;
96
97   /* check if nick/username/realname wasn't read from setup.. */
98   set = settings_get_str("real_name");
99   if (set == NULL || *set == '\0') {
100     str = g_getenv("SILCNAME");
101     if (!str)
102       str = g_getenv("IRCNAME");
103     settings_set_str("real_name",
104                      str != NULL ? str : silc_get_real_name());
105   }
106
107   /* username */
108   user_name = settings_get_str("user_name");
109   if (user_name == NULL || *user_name == '\0') {
110     str = g_getenv("SILCUSER");
111     if (!str)
112       str = g_getenv("IRCUSER");
113     settings_set_str("user_name",
114                      str != NULL ? str : silc_get_username());
115
116     user_name = settings_get_str("user_name");
117   }
118
119   /* nick */
120   nick = settings_get_str("nick");
121   if (nick == NULL || *nick == '\0') {
122     str = g_getenv("SILCNICK");
123     if (!str)
124       str = g_getenv("IRCNICK");
125     settings_set_str("nick", str != NULL ? str : user_name);
126
127     nick = settings_get_str("nick");
128   }
129
130   /* alternate nick */
131   set = settings_get_str("alternate_nick");
132   if (set == NULL || *set == '\0') {
133     str = g_strconcat(nick, "_", NULL);
134     settings_set_str("alternate_nick", str);
135     g_free(str);
136   }
137
138   /* host name */
139   set = settings_get_str("hostname");
140   if (set == NULL || *set == '\0') {
141     str = g_getenv("SILCHOST");
142     if (!str)
143       str = g_getenv("IRCHOST");
144     if (str != NULL)
145       settings_set_str("hostname", str);
146   }
147 }
148
149 #ifdef SILC_DEBUG
150 static bool i_debug;
151 static bool silc_irssi_debug_print(char *file, char *function, int line,
152                                    char *message, void *context)
153 {
154   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
155             "DEBUG: %s:%d: %s", function, line, message);
156   return TRUE;
157 }
158 #endif
159
160 static void sig_setup_changed(void)
161 {
162 #ifdef SILC_DEBUG
163   bool debug = settings_get_bool("debug");
164   if (debug) {
165     const char *debug_string = settings_get_str("debug_string");
166     i_debug = TRUE;
167     silc_log_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);
171     return;
172   }
173   if (i_debug)
174     silc_log_debug(FALSE);
175 #endif
176 }
177
178 /* Log callbacks */
179
180 static bool silc_log_misc(SilcLogType type, char *message, void *context)
181 {
182   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
183             (type == SILC_LOG_INFO ? "[Info]" :
184              type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
185   return TRUE;
186 }
187
188 static void silc_nickname_format_parse(const char *nickname,
189                                        char **ret_nickname)
190 {
191   silc_parse_userfqdn(nickname, ret_nickname, NULL);
192 }
193
194 static void silc_register_cipher(SilcClient client, const char *cipher)
195 {
196   int i;
197
198   if (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]));
202         break;
203       }
204
205     if (!silc_cipher_is_supported(cipher)) {
206       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
207       exit(1);
208     }
209   }
210
211   /* Register other defaults */
212   silc_cipher_register_default();
213 }
214
215 static void silc_register_hash(SilcClient client, const char *hash)
216 {
217   int i;
218
219   if (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]));
223         break;
224       }
225
226     if (!silc_hash_is_supported(hash)) {
227       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
228       exit(1);
229     }
230   }
231
232   /* Register other defaults */
233   silc_hash_register_default();
234 }
235
236 static void silc_register_hmac(SilcClient client, const char *hmac)
237 {
238   int i;
239
240   if (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]));
244         break;
245       }
246
247     if (!silc_hmac_is_supported(hmac)) {
248       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
249       exit(1);
250     }
251   }
252
253   /* Register other defaults */
254   silc_hmac_register_default();
255 }
256
257 /* Finalize init. Init finish signal calls this. */
258
259 void silc_opt_callback(poptContext con,
260                        enum poptCallbackReason reason,
261                        const struct poptOption *opt,
262                        const char *arg, void *data)
263 {
264   if (strcmp(opt->longName, "nick") == 0) {
265     g_free(silc_client->nickname);
266     silc_client->nickname = g_strdup(arg);
267   }
268
269   if (strcmp(opt->longName, "hostname") == 0) {
270     silc_free(silc_client->hostname);
271     silc_client->hostname = g_strdup(arg);
272   }
273
274   if (strcmp(opt->longName, "list-ciphers") == 0) {
275     silc_cipher_register_default();
276     silc_client_list_ciphers();
277     exit(0);
278   }
279
280   if (strcmp(opt->longName, "list-hash-funcs") == 0) {
281     silc_hash_register_default();
282     silc_client_list_hash_funcs();
283     exit(0);
284   }
285
286   if (strcmp(opt->longName, "list-hmacs") == 0) {
287     silc_hmac_register_default();
288     silc_client_list_hmacs();
289     exit(0);
290   }
291
292   if (strcmp(opt->longName, "list-pkcs") == 0) {
293     silc_pkcs_register_default();
294     silc_client_list_pkcs();
295     exit(0);
296   }
297
298   if (strcmp(opt->longName, "debug") == 0) {
299     silc_log_debug(TRUE);
300     silc_log_debug_hexdump(TRUE);
301     silc_log_set_debug_string(arg);
302 #ifndef SILC_DEBUG
303     fprintf(stdout,
304             "Run-time debugging is not enabled. To enable it recompile\n"
305             "the client with --enable-debug configuration option.\n");
306     sleep(1);
307 #endif
308   }
309
310   if (strcmp(opt->longName, "create-key-pair") == 0) {
311     /* Create new key pair and exit */
312     silc_cipher_register_default();
313     silc_pkcs_register_default();
314     silc_hash_register_default();
315     silc_hmac_register_default();
316     silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL, NULL,
317                          NULL, NULL, NULL, NULL, TRUE);
318     exit(0);
319   }
320
321   if (strcmp(opt->longName, "passphrase-change") == 0) {
322     /* Change the passphrase of the private key file */
323     silc_cipher_register_default();
324     silc_pkcs_register_default();
325     silc_hash_register_default();
326     silc_hmac_register_default();
327     silc_change_private_key_passphrase(arg, NULL, NULL);
328     exit(0);
329   }
330
331   if (strcmp(opt->longName, "show-key") == 0) {
332     /* Dump the key */
333     silc_cipher_register_default();
334     silc_pkcs_register_default();
335     silc_hash_register_default();
336     silc_hmac_register_default();
337     silc_show_public_key((char *)arg);
338     exit(0);
339   }
340 }
341
342 static void sig_init_finished(void)
343 {
344   /* Check ~/.silc directory and public and private keys */
345   if (!silc_client_check_silc_dir())
346     exit(1);
347
348   /* Load public and private key */
349   if (!silc_client_load_keys(silc_client))
350     exit(1);
351
352   /* Initialize the SILC client */
353   if (!silc_client_init(silc_client))
354     exit(1);
355
356   /* register SILC scheduler */
357   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
358 }
359
360 /* Init SILC. Called from src/fe-text/silc.c */
361
362 void silc_core_init(void)
363 {
364   static struct poptOption silc_options[] = {
365     { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
366     { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
367       "List supported ciphers", NULL },
368     { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
369       "List supported hash functions", NULL },
370     { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
371       "List supported HMACs", NULL },
372     { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
373       "List supported PKCSs", NULL },
374 #ifdef SILC_DEBUG
375     { "debug", 'd', POPT_ARG_STRING, NULL, 0,
376       "Enable debugging", "STRING" },
377 #endif /* SILC_DEBUG */
378     { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
379       "Create new public key pair", NULL },
380     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
381       "Set the PKCS of the public key pair (-C)", "PKCS" },
382     { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
383       "Set the length of the public key pair (-C)", "VALUE" },
384     { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
385       "Change the passphrase of private key file", "FILE" },
386     { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
387       "Show the contents of the public key", "FILE" },
388     { NULL, '\0', 0, NULL }
389   };
390
391   CHAT_PROTOCOL_REC *rec;
392   SilcClientParams params;
393   const char *def_cipher, *def_hash, *def_hmac;
394
395   args_register(silc_options);
396
397   /* Settings */
398   settings_add_bool("server", "skip_motd", FALSE);
399   settings_add_str("server", "alternate_nick", NULL);
400   settings_add_bool("server", "use_auto_addr", FALSE);
401   settings_add_str("server", "auto_bind_ip", "");
402   settings_add_str("server", "auto_public_ip", "");
403   settings_add_int("server", "auto_bind_port", 0);
404   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
405   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
406   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
407   settings_add_int("server", "key_exchange_timeout_secs", 120);
408   settings_add_int("server", "key_exchange_rekey_secs", 3600);
409   settings_add_int("server", "connauth_request_secs", 2);
410   settings_add_int("server", "heartbeat", 300);
411   settings_add_bool("server", "ignore_message_signatures", FALSE);
412   settings_add_str("server", "session_filename", "session.$chatnet");
413
414   /* Requested Attributes settings */
415   settings_add_bool("silc", "attr_allow", TRUE);
416   settings_add_str("silc", "attr_vcard", "");
417   settings_add_str("silc", "attr_services", "");
418   settings_add_str("silc", "attr_status_mood", "NORMAL");
419   settings_add_str("silc", "attr_status_text", "");
420   settings_add_str("silc", "attr_status_message", NULL);
421   settings_add_str("silc", "attr_preferred_language", "");
422   settings_add_str("silc", "attr_preferred_contact", "CHAT");
423   settings_add_bool("silc", "attr_timezone", TRUE);
424   settings_add_str("silc", "attr_geolocation", "");
425   settings_add_str("silc", "attr_device_info", NULL);
426   settings_add_str("silc", "attr_public_keys", "");
427
428 #ifdef SILC_DEBUG
429   settings_add_bool("debug", "debug", FALSE);
430   settings_add_str("debug", "debug_string", "");
431 #endif
432
433   signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
434   signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
435
436   silc_init_userinfo();
437
438   /* Initialize client parameters */
439   memset(&params, 0, sizeof(params));
440   strcat(params.nickname_format, "%n@%h%a");
441   params.nickname_parse = silc_nickname_format_parse;
442   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
443   params.connauth_request_secs = settings_get_int("connauth_request_secs");
444
445   /* Allocate SILC client */
446   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
447
448   /* Get the ciphers and stuff from config file */
449   def_cipher = settings_get_str("crypto_default_cipher");
450   def_hash = settings_get_str("crypto_default_hash");
451   def_hmac = settings_get_str("crypto_default_hmac");
452   silc_register_cipher(silc_client, def_cipher);
453   silc_register_hash(silc_client, def_hash);
454   silc_register_hmac(silc_client, def_hmac);
455   silc_pkcs_register_default();
456
457   /* Get user information */
458   silc_client->username = g_strdup(settings_get_str("user_name"));
459   silc_client->nickname = g_strdup(settings_get_str("nick"));
460   silc_client->hostname = silc_net_localhost();
461   silc_client->realname = g_strdup(settings_get_str("real_name"));
462
463   silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
464   silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
465   silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
466   silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
467
468   /* Register SILC to the irssi */
469   rec = g_new0(CHAT_PROTOCOL_REC, 1);
470   rec->name = "SILC";
471   rec->fullname = "Secure Internet Live Conferencing";
472   rec->chatnet = "silcnet";
473   rec->create_chatnet = create_chatnet;
474   rec->create_server_setup = create_server_setup;
475   rec->create_channel_setup = create_channel_setup;
476   rec->create_server_connect = create_server_connect;
477   rec->destroy_server_connect = destroy_server_connect;
478   rec->server_init_connect = silc_server_init_connect;
479   rec->server_connect = silc_server_connect;
480   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
481                                            const char *, int))
482     silc_channel_create;
483   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
484     silc_query_create;
485
486   chat_protocol_register(rec);
487   g_free(rec);
488
489   silc_queue_init();
490   silc_server_init();
491   silc_channels_init();
492   silc_queries_init();
493   silc_expandos_init();
494   silc_lag_init();
495   silc_chatnets_init();
496
497   module_register("silc", "core");
498 }
499
500 /* Deinit SILC. Called from src/fe-text/silc.c */
501
502 void silc_core_deinit(void)
503 {
504   if (idletag != -1)
505     g_source_remove(idletag);
506
507   signal_emit("chat protocol deinit", 1,
508         chat_protocol_find("SILC"));
509   signal_remove("setup changed", (SIGNAL_FUNC) sig_setup_changed);
510   signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
511
512   silc_queue_deinit();
513   silc_server_deinit();
514   silc_channels_deinit();
515   silc_queries_deinit();
516   silc_expandos_deinit();
517   silc_lag_deinit();
518   silc_chatnets_deinit();
519
520   chat_protocol_unregister("SILC");
521
522   g_free(silc_client->username);
523   g_free(silc_client->realname);
524   silc_free(silc_client->hostname);
525   silc_pkcs_free(silc_client->pkcs);
526   silc_pkcs_private_key_free(silc_client->private_key);
527   silc_pkcs_public_key_free(silc_client->public_key);
528   silc_client_free(silc_client);
529 }