updates.
[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 #include "version_internal.h"
33 #include "version.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 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;
54
55 static int idletag;
56
57 SilcClient silc_client = NULL;
58 extern SilcClientOperations ops;
59 extern bool silc_debug;
60 extern bool silc_debug_hexdump;
61 #ifdef SILC_SIM
62 /* SIM (SILC Module) table */
63 SilcSimContext **sims = NULL;
64 uint32 sims_count = 0;
65 #endif
66
67 static int my_silc_scheduler(void)
68 {
69   silc_client_run_one(silc_client);
70   return 1;
71 }
72
73 static CHATNET_REC *create_chatnet(void)
74 {
75   return g_malloc0(sizeof(CHATNET_REC));
76 }
77
78 static SERVER_SETUP_REC *create_server_setup(void)
79 {
80   return g_malloc0(sizeof(SERVER_SETUP_REC));
81 }
82
83 static CHANNEL_SETUP_REC *create_channel_setup(void)
84 {
85   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
86 }
87
88 static SERVER_CONNECT_REC *create_server_connect(void)
89 {
90   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
91 }
92
93 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
94 {
95
96 }
97
98 /* Checks user information and saves them to the config file it they
99    do not exist there already. */
100
101 static void silc_init_userinfo(void)
102 {
103   const char *set, *nick, *user_name;
104   char *str;   
105         
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");
110     if (!str)
111       str = g_getenv("IRCNAME");
112     settings_set_str("real_name",
113                      str != NULL ? str : g_get_real_name());
114   }
115  
116   /* username */
117   user_name = settings_get_str("user_name");
118   if (user_name == NULL || *user_name == '\0') {
119     str = g_getenv("SILCUSER");
120     if (!str)
121       str = g_getenv("IRCUSER");
122     settings_set_str("user_name",
123                      str != NULL ? str : g_get_user_name());
124     
125     user_name = settings_get_str("user_name");
126   }
127
128   /* nick */
129   nick = settings_get_str("nick");
130   if (nick == NULL || *nick == '\0') {
131     str = g_getenv("SILCNICK");
132     if (!str)
133       str = g_getenv("IRCNICK");
134     settings_set_str("nick", str != NULL ? str : user_name);
135     
136     nick = settings_get_str("nick");
137   }
138                 
139   /* alternate 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);
144     else { 
145       str = g_strdup(nick);
146       str[strlen(str)-1] = '_';
147     }
148     settings_set_str("alternate_nick", str);
149     g_free(str);
150   }
151
152   /* host name */
153   set = settings_get_str("hostname");
154   if (set == NULL || *set == '\0') {
155     str = g_getenv("SILCHOST");
156     if (!str)
157       str = g_getenv("IRCHOST");
158     if (str != NULL)
159       settings_set_str("hostname", str);
160   }
161 }
162
163 /* Log callbacks */
164
165 static bool silc_log_misc(SilcLogType type, char *message, void *context)
166 {
167   fprintf(stderr, "%s\n", message);
168   return TRUE;
169 }
170
171 static void silc_nickname_format_parse(const char *nickname,
172                                        char **ret_nickname)
173 {
174   silc_parse_userfqdn(nickname, ret_nickname, NULL);
175 }
176
177 static void silc_register_cipher(SilcClient client, const char *cipher)
178 {
179   int i;
180
181   if (cipher) {
182     for (i = 0; silc_default_ciphers[i].name; i++)
183       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
184         silc_cipher_register(&silc_default_ciphers[i]);
185         break;
186       }
187     
188     if (!silc_cipher_is_supported(cipher)) {
189       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
190       exit(1);
191     }
192   }
193
194   /* Register other defaults */
195   silc_cipher_register_default();
196 }
197
198 static void silc_register_hash(SilcClient client, const char *hash)
199 {
200   int i;
201
202   if (hash) {
203     for (i = 0; silc_default_hash[i].name; i++)
204       if (!strcmp(silc_default_hash[i].name, hash)) {
205         silc_hash_register(&silc_default_hash[i]);
206         break;
207       }
208     
209     if (!silc_hash_is_supported(hash)) {
210       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
211       exit(1);
212     }
213   }
214
215   /* Register other defaults */
216   silc_hash_register_default();
217 }
218
219 static void silc_register_hmac(SilcClient client, const char *hmac)
220 {
221   int i;
222
223   if (hmac) {
224     for (i = 0; silc_default_hmacs[i].name; i++)
225       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
226         silc_hmac_register(&silc_default_hmacs[i]);
227         break;
228       }
229     
230     if (!silc_hmac_is_supported(hmac)) {
231       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
232       exit(1);
233     }
234   }
235
236   /* Register other defaults */
237   silc_hmac_register_default();
238 }
239
240 /* Finalize init. Init finish signal calls this. */
241
242 void silc_core_init_finish(SERVER_REC *server)
243 {
244   CHAT_PROTOCOL_REC *rec;
245   SilcClientParams params;
246   const char *def_cipher, *def_hash, *def_hmac;
247
248   if (opt_create_keypair == TRUE) {
249     /* Create new key pair and exit */
250     silc_cipher_register_default();
251     silc_pkcs_register_default();
252     silc_hash_register_default();
253     silc_hmac_register_default();
254     silc_client_create_key_pair(opt_pkcs, opt_bits, 
255                                 NULL, NULL, NULL, NULL, NULL);
256     exit(0);
257   }
258
259   if (opt_keyfile) {
260     /* Dump the key */
261     silc_cipher_register_default();
262     silc_pkcs_register_default();
263     silc_hash_register_default();
264     silc_hmac_register_default();
265     silc_client_show_key(opt_keyfile);
266     exit(0);
267   }
268
269   if (opt_list_ciphers) {
270     silc_cipher_register_default();
271     silc_client_list_ciphers();
272     exit(0);
273   }
274
275   if (opt_list_hash) {
276     silc_hash_register_default();
277     silc_client_list_hash_funcs();
278     exit(0);
279   }
280
281   if (opt_list_hmac) {
282     silc_hmac_register_default();
283     silc_client_list_hmacs();
284     exit(0);
285   }
286
287   if (opt_list_pkcs) {
288     silc_pkcs_register_default();
289     silc_client_list_pkcs();
290     exit(0);
291   }
292
293   if (opt_version) {
294     printf("SILC Secure Internet Live Conferencing, version %s "
295            "(base: SILC Toolkit %s)\n", silc_dist_version, silc_version);
296     printf("(c) 1997 - 2001 Pekka Riikonen <priikone@silcnet.org>\n");
297     exit(0); 
298   }
299
300   if (opt_debug) {
301     silc_debug = TRUE;
302     silc_debug_hexdump = TRUE;
303     silc_log_set_debug_string(opt_debug);
304     silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
305     silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
306     silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
307     silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
308 #ifndef SILC_DEBUG
309     fprintf(stdout, 
310             "Run-time debugging is not enabled. To enable it recompile\n"
311             "the client with --enable-debug configuration option.\n");
312 #endif
313   }
314
315   /* Settings */
316   settings_add_bool("server", "skip_motd", FALSE);
317   settings_add_str("server", "alternate_nick", NULL);
318   settings_add_bool("server", "use_auto_addr", FALSE);
319   settings_add_str("server", "auto_bind_ip", "");
320   settings_add_str("server", "auto_public_ip", "");
321   settings_add_int("server", "auto_bind_port", 0);
322   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
323   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
324   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
325   settings_add_int("server", "key_exchange_timeout_secs", 120);
326   settings_add_int("server", "key_exchange_rekey_secs", 3600);
327   settings_add_int("server", "connauth_request_secs", 2);
328
329   silc_init_userinfo();
330
331   /* Initialize client parameters */
332   memset(&params, 0, sizeof(params));
333   strcat(params.nickname_format, "%n@%h%a");
334   params.nickname_parse = silc_nickname_format_parse;
335   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
336   params.connauth_request_secs = settings_get_int("connauth_request_secs");
337
338   /* Allocate SILC client */
339   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
340
341   /* Get the ciphers and stuff from config file */
342   def_cipher = settings_get_str("crypto_default_cipher");
343   def_hash = settings_get_str("crypto_default_hash");
344   def_hmac = settings_get_str("crypto_default_hmac");
345   silc_register_cipher(silc_client, def_cipher);
346   silc_register_hash(silc_client, def_hash);
347   silc_register_hmac(silc_client, def_hmac);
348   silc_pkcs_register_default();
349
350   /* Get user information */
351   silc_client->username = g_strdup(settings_get_str("user_name"));
352   silc_client->nickname = g_strdup(settings_get_str("nick"));
353   silc_client->hostname = silc_net_localhost();
354   silc_client->realname = g_strdup(settings_get_str("real_name"));
355
356   /* Check ~/.silc directory and public and private keys */
357   if (silc_client_check_silc_dir() == FALSE) {
358     idletag = -1;
359     return;
360   }
361
362   /* Load public and private key */
363   if (silc_client_load_keys(silc_client) == FALSE) {
364     idletag = -1;
365     return;
366   }
367
368   /* Initialize the SILC client */
369   if (!silc_client_init(silc_client)) {
370     idletag = -1;
371     return;
372   }
373
374   /* Register SILC to the irssi */
375   rec = g_new0(CHAT_PROTOCOL_REC, 1);
376   rec->name = "SILC";
377   rec->fullname = "Secure Internet Live Conferencing";
378   rec->chatnet = "silcnet";
379   rec->create_chatnet = create_chatnet;
380   rec->create_server_setup = create_server_setup;
381   rec->create_channel_setup = create_channel_setup;
382   rec->create_server_connect = create_server_connect;
383   rec->destroy_server_connect = destroy_server_connect;
384   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
385     silc_server_connect; 
386   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
387     silc_channel_create;
388   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
389     silc_query_create;
390   
391   chat_protocol_register(rec);
392   g_free(rec);
393
394   silc_server_init();
395   silc_channels_init();
396   silc_queries_init();
397
398   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
399 }
400
401 /* Init SILC. Called from src/fe-text/silc.c */
402
403 void silc_core_init(void)
404 {
405   static struct poptOption options[] = {
406     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0, 
407       "Create new public key pair", NULL },
408     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0, 
409       "Set the PKCS of the public key pair", "PKCS" },
410     { "bits", 0, POPT_ARG_INT, &opt_bits, 0, 
411       "Set the length of the public key pair", "VALUE" },
412     { "show-key", 'S', POPT_ARG_STRING, &opt_keyfile, 0, 
413       "Show the contents of the public key", "FILE" },
414     { "list-ciphers", 'c', POPT_ARG_NONE, &opt_list_ciphers, 0,
415       "List supported ciphers", NULL },
416     { "list-hash-funcs", 'H', POPT_ARG_NONE, &opt_list_hash, 0,
417       "List supported hash functions", NULL },
418     { "list-hmacs", 'M', POPT_ARG_NONE, &opt_list_hmac, 0,
419       "List supported HMACs", NULL },
420     { "list-pkcs", 'P', POPT_ARG_NONE, &opt_list_pkcs, 0,
421       "List supported PKCSs", NULL },
422     { "debug", 'd', POPT_ARG_STRING, &opt_debug, 0,
423       "Enable debugging", "STRING" },
424     { "version", 'V', POPT_ARG_NONE, &opt_version, 0,
425       "Show version", NULL },
426     { NULL, '\0', 0, NULL }
427   };
428
429   signal_add("irssi init finished", (SIGNAL_FUNC) silc_core_init_finish);
430
431   args_register(options);
432 }
433
434 /* Deinit SILC. Called from src/fe-text/silc.c */
435
436 void silc_core_deinit(void)
437 {
438   if (idletag != -1) {
439     signal_emit("chat protocol deinit", 1,
440                 chat_protocol_find("SILC"));
441     
442     silc_server_deinit();
443     silc_channels_deinit();
444     silc_queries_deinit();
445     
446     chat_protocol_unregister("SILC");
447     
448     g_source_remove(idletag);
449   }
450   
451   g_free(silc_client->username);
452   g_free(silc_client->realname);
453   silc_client_free(silc_client);
454 }