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