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 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
33 #include "signals.h"
34 #include "levels.h"
35 #include "settings.h"
36 #include "fe-common/core/printtext.h"
37 #include "fe-common/core/fe-channels.h"
38 #include "fe-common/core/keyboard.h"
39 #include "fe-common/silc/module-formats.h"
40
41 /* Command line option variables */
42 static char *opt_pkcs = NULL;
43 static int opt_bits = 0;
44
45 static int idletag;
46
47 SilcClient silc_client = NULL;
48 extern SilcClientOperations ops;
49 extern bool silc_debug;
50 extern bool silc_debug_hexdump;
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 = silc_debug = TRUE;
167     if (strlen(debug_string))
168       silc_log_set_debug_string(debug_string);
169     silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
170     return;
171   }
172   if (i_debug)
173     silc_debug = FALSE;
174 #endif
175 }
176
177 /* Log callbacks */
178
179 static bool silc_log_misc(SilcLogType type, char *message, void *context)
180 {
181   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s", 
182             (type == SILC_LOG_INFO ? "[Info]" :
183              type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
184   return TRUE;
185 }
186
187 static void silc_nickname_format_parse(const char *nickname,
188                                        char **ret_nickname)
189 {
190   silc_parse_userfqdn(nickname, ret_nickname, NULL);
191 }
192
193 static void silc_register_cipher(SilcClient client, const char *cipher)
194 {
195   int i;
196
197   if (cipher) {
198     for (i = 0; silc_default_ciphers[i].name; i++)
199       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
200         silc_cipher_register(&(silc_default_ciphers[i]));
201         break;
202       }
203     
204     if (!silc_cipher_is_supported(cipher)) {
205       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
206       exit(1);
207     }
208   }
209
210   /* Register other defaults */
211   silc_cipher_register_default();
212 }
213
214 static void silc_register_hash(SilcClient client, const char *hash)
215 {
216   int i;
217
218   if (hash) {
219     for (i = 0; silc_default_hash[i].name; i++)
220       if (!strcmp(silc_default_hash[i].name, hash)) {
221         silc_hash_register(&(silc_default_hash[i]));
222         break;
223       }
224     
225     if (!silc_hash_is_supported(hash)) {
226       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
227       exit(1);
228     }
229   }
230
231   /* Register other defaults */
232   silc_hash_register_default();
233 }
234
235 static void silc_register_hmac(SilcClient client, const char *hmac)
236 {
237   int i;
238
239   if (hmac) {
240     for (i = 0; silc_default_hmacs[i].name; i++)
241       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
242         silc_hmac_register(&(silc_default_hmacs[i]));
243         break;
244       }
245     
246     if (!silc_hmac_is_supported(hmac)) {
247       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
248       exit(1);
249     }
250   }
251
252   /* Register other defaults */
253   silc_hmac_register_default();
254 }
255
256 /* Finalize init. Init finish signal calls this. */
257
258 void silc_opt_callback(poptContext con, 
259                        enum poptCallbackReason reason,
260                        const struct poptOption *opt,
261                        const char *arg, void *data)
262 {
263   if (strcmp(opt->longName, "list-ciphers") == 0) {
264     silc_cipher_register_default();
265     silc_client_list_ciphers();
266     exit(0);
267   }
268
269   if (strcmp(opt->longName, "list-hash-funcs") == 0) {
270     silc_hash_register_default();
271     silc_client_list_hash_funcs();
272     exit(0);
273   }
274
275   if (strcmp(opt->longName, "list-hmacs") == 0) {
276     silc_hmac_register_default();
277     silc_client_list_hmacs();
278     exit(0);
279   }
280
281   if (strcmp(opt->longName, "list-pkcs") == 0) {
282     silc_pkcs_register_default();
283     silc_client_list_pkcs();
284     exit(0);
285   }
286
287   if (strcmp(opt->longName, "debug") == 0) {
288     silc_debug = TRUE;
289     silc_debug_hexdump = TRUE;
290     silc_log_set_debug_string(arg);
291 #ifndef SILC_DEBUG
292     fprintf(stdout, 
293             "Run-time debugging is not enabled. To enable it recompile\n"
294             "the client with --enable-debug configuration option.\n");
295     sleep(1);
296 #endif
297   }
298
299   if (strcmp(opt->longName, "create-key-pair") == 0) {
300     /* Create new key pair and exit */
301     silc_cipher_register_default();
302     silc_pkcs_register_default();
303     silc_hash_register_default();
304     silc_hmac_register_default();
305     silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL, NULL,
306                          NULL, NULL, NULL, NULL, TRUE);
307     exit(0);
308   }
309
310   if (strcmp(opt->longName, "passphrase-change") == 0) {
311     /* Change the passphrase of the private key file */
312     silc_cipher_register_default();
313     silc_pkcs_register_default();
314     silc_hash_register_default();
315     silc_hmac_register_default();
316     silc_change_private_key_passphrase(arg, NULL, NULL);
317     exit(0);
318   }
319
320   if (strcmp(opt->longName, "show-key") == 0) {
321     /* Dump the key */
322     silc_cipher_register_default();
323     silc_pkcs_register_default();
324     silc_hash_register_default();
325     silc_hmac_register_default();
326     silc_show_public_key((char *)arg);
327     exit(0);
328   }
329 }
330
331 static void sig_init_finished(void)
332 {
333   /* Check ~/.silc directory and public and private keys */
334   if (!silc_client_check_silc_dir()) {
335     idletag = -1;
336     exit(1);
337   }
338
339   /* Load public and private key */
340   if (!silc_client_load_keys(silc_client)) {
341     idletag = -1;
342     exit(1);
343   }
344
345   /* Initialize the SILC client */
346   if (!silc_client_init(silc_client)) {
347     idletag = -1;
348     exit(1);
349   }
350 }
351
352 /* Init SILC. Called from src/fe-text/silc.c */
353
354 void silc_core_init(void)
355 {
356   static struct poptOption silc_options[] = {
357     { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
358     { "list-ciphers", 0, POPT_ARG_NONE, NULL, 0,
359       "List supported ciphers", NULL },
360     { "list-hash-funcs", 0, POPT_ARG_NONE, NULL, 0,
361       "List supported hash functions", NULL },
362     { "list-hmacs", 0, POPT_ARG_NONE, NULL, 0,
363       "List supported HMACs", NULL },
364     { "list-pkcs", 0, POPT_ARG_NONE, NULL, 0,
365       "List supported PKCSs", NULL },
366 #ifdef SILC_DEBUG
367     { "debug", 'd', POPT_ARG_STRING, NULL, 0,
368       "Enable debugging", "STRING" },
369 #endif /* SILC_DEBUG */
370     { "create-key-pair", 'C', POPT_ARG_NONE, NULL, 0,
371       "Create new public key pair", NULL },
372     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
373       "Set the PKCS of the public key pair (-C)", "PKCS" },
374     { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
375       "Set the length of the public key pair (-C)", "VALUE" },
376     { "passphrase-change", 'P', POPT_ARG_STRING, NULL, 0,
377       "Change the passphrase of private key file", "FILE" },
378     { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
379       "Show the contents of the public key", "FILE" },
380     { NULL, '\0', 0, NULL }
381   };
382
383   CHAT_PROTOCOL_REC *rec;
384   SilcClientParams params;
385   const char *def_cipher, *def_hash, *def_hmac;
386
387   args_register(silc_options);
388
389   /* Settings */
390   settings_add_bool("server", "skip_motd", FALSE);
391   settings_add_str("server", "alternate_nick", NULL);
392   settings_add_bool("server", "use_auto_addr", FALSE);
393   settings_add_str("server", "auto_bind_ip", "");
394   settings_add_str("server", "auto_public_ip", "");
395   settings_add_int("server", "auto_bind_port", 0);
396   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
397   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
398   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
399   settings_add_int("server", "key_exchange_timeout_secs", 120);
400   settings_add_int("server", "key_exchange_rekey_secs", 3600);
401   settings_add_int("server", "connauth_request_secs", 2);
402   settings_add_int("server", "heartbeat", 300);
403   settings_add_bool("server", "ignore_message_signatures", FALSE);
404
405   /* Requested Attributes settings */
406   settings_add_bool("silc", "attr_allow", TRUE);
407   settings_add_str("silc", "attr_vcard", "");
408   settings_add_str("silc", "attr_services", "");
409   settings_add_str("silc", "attr_status_mood", "NORMAL");
410   settings_add_str("silc", "attr_status_text", "");
411   settings_add_str("silc", "attr_status_message", NULL);
412   settings_add_str("silc", "attr_preferred_language", "");
413   settings_add_str("silc", "attr_preferred_contact", "CHAT");
414   settings_add_bool("silc", "attr_timezone", TRUE);
415   settings_add_str("silc", "attr_geolocation", "");
416   settings_add_str("silc", "attr_device_info", NULL);
417   settings_add_str("silc", "attr_public_keys", "");
418
419 #ifdef SILC_DEBUG
420   settings_add_bool("debug", "debug", FALSE);
421   settings_add_str("debug", "debug_string", "");
422 #endif
423
424   signal_add("setup changed", (SIGNAL_FUNC) sig_setup_changed);
425   signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
426
427   silc_init_userinfo();
428
429   /* Initialize client parameters */
430   memset(&params, 0, sizeof(params));
431   strcat(params.nickname_format, "%n@%h%a");
432   params.nickname_parse = silc_nickname_format_parse;
433   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
434   params.connauth_request_secs = settings_get_int("connauth_request_secs");
435
436   /* Allocate SILC client */
437   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
438
439   /* Get the ciphers and stuff from config file */
440   def_cipher = settings_get_str("crypto_default_cipher");
441   def_hash = settings_get_str("crypto_default_hash");
442   def_hmac = settings_get_str("crypto_default_hmac");
443   silc_register_cipher(silc_client, def_cipher);
444   silc_register_hash(silc_client, def_hash);
445   silc_register_hmac(silc_client, def_hmac);
446   silc_pkcs_register_default();
447
448   /* Get user information */
449   silc_client->username = g_strdup(settings_get_str("user_name"));
450   silc_client->nickname = g_strdup(settings_get_str("nick"));
451   silc_client->hostname = silc_net_localhost();
452   silc_client->realname = g_strdup(settings_get_str("real_name"));
453
454   silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
455   silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
456   silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
457   silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
458
459   /* Register SILC to the irssi */
460   rec = g_new0(CHAT_PROTOCOL_REC, 1);
461   rec->name = "SILC";
462   rec->fullname = "Secure Internet Live Conferencing";
463   rec->chatnet = "silcnet";
464   rec->create_chatnet = create_chatnet;
465   rec->create_server_setup = create_server_setup;
466   rec->create_channel_setup = create_channel_setup;
467   rec->create_server_connect = create_server_connect;
468   rec->destroy_server_connect = destroy_server_connect;
469   rec->server_init_connect = silc_server_init_connect; 
470   rec->server_connect = silc_server_connect;
471   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, 
472                                            const char *, int))
473     silc_channel_create;
474   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
475     silc_query_create;
476   
477   chat_protocol_register(rec);
478   g_free(rec);
479
480   silc_server_init();
481   silc_channels_init();
482   silc_queries_init();
483   silc_expandos_init();
484   silc_lag_init();
485
486   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
487
488   module_register("silc", "core");
489 }
490
491 /* Deinit SILC. Called from src/fe-text/silc.c */
492
493 void silc_core_deinit(void)
494 {
495   if (idletag != -1) {
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);
500
501     silc_server_deinit();
502     silc_channels_deinit();
503     silc_queries_deinit();
504     silc_expandos_deinit();
505     silc_lag_deinit();
506     
507     chat_protocol_unregister("SILC");
508     
509     g_source_remove(idletag);
510   }
511   
512   g_free(silc_client->username);
513   g_free(silc_client->realname);
514   silc_free(silc_client->hostname);
515   silc_pkcs_free(silc_client->pkcs);
516   silc_pkcs_private_key_free(silc_client->private_key);
517   silc_pkcs_public_key_free(silc_client->public_key);
518   silc_client_free(silc_client);
519 }