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