updates.
[silc.git] / apps / silcd / silcd.c
1 /*
2
3   silcd.c
4   
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 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  * Created: Wed Mar 19 00:17:12 1997
22  *
23  * This is the main program for the SILC daemon. This parses command
24  * line arguments and creates the server object.
25  */
26 /* $Id$ */
27
28 #include "serverincludes.h"
29 #include "server_internal.h"
30 #include "version.h"
31
32 static void silc_usage();
33 static char *silc_server_create_identifier();
34 static int 
35 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
36                             char *identifier, 
37                             SilcPublicKey *ret_pub_key,
38                             SilcPrivateKey *ret_prv_key);
39
40 /* Long command line options */
41 static struct option long_opts[] = 
42 {
43   { "config-file", 1, NULL, 'f' },
44   { "debug", 1, NULL, 'd' },
45   { "help", 0, NULL, 'h' },
46   { "foreground", 0, NULL, 'F' },
47   { "version", 0, NULL,'V' },
48
49   /* Key management options */
50   { "create-key-pair", 1, NULL, 'C' },
51   { "pkcs", 1, NULL, 10 },
52   { "bits", 1, NULL, 11 },
53   { "identifier", 1, NULL, 12 },
54
55   { NULL, 0, NULL, 0 }
56 };
57
58 /* Command line option variables */
59 static bool opt_create_keypair = FALSE;
60 static char *opt_keypath = NULL;
61 static char *opt_pkcs = "rsa";
62 static char *opt_identifier = NULL;
63 static int opt_bits = 1024;
64
65 /* Prints out the usage of silc client */
66
67 static void silc_usage()
68 {
69   printf("\
70 Usage: silcd [options]\n\
71 \n\
72   Generic Options:\n\
73   -f  --config-file=FILE        Alternate configuration file\n\
74   -d  --debug=string            Enable debugging (Implies --foreground)\n\
75   -h  --help                    Display this message\n\
76   -F  --foreground              Dont fork\n\
77   -V  --version                 Display version\n\
78 \n\
79   Key Management Options:\n\
80   -C, --create-key-pair=PATH    Create new public key pair\n\
81       --pkcs=PKCS               Set the PKCS of the public key pair\n\
82       --bits=VALUE              Set length of the public key pair\n\
83       --identifier=IDENTIFIER   Public key identifier\n\
84 \n\
85       The public key identifier may be of the following format:\n\
86 \n\
87       UN=<username>, HN=<hostname or IP>, RN=<real name>, E=<email>,\n\
88       O=<organization>, C=<country>\n\
89 \n\
90       The UN and HN must be provided, the others are optional.  If the\n\
91       --identifier option is not used an identifier will be created for\n\
92       the public key automatically.\n\
93 \n\
94       Example identifier: \"UN=foobar, HN=foo.bar.com, RN=Foo T. Bar, \n\
95                            E=foo@bar.com, C=FI\"\n\
96 \n");
97   exit(0);
98 }
99
100 int main(int argc, char **argv)
101 {
102   int ret;
103   int opt, option_index;
104   int foreground = FALSE;
105   char *config_file = NULL;
106   SilcServer silcd;
107   struct sigaction sa;
108   char pid[10];
109
110   silc_debug = FALSE;
111
112   /* Parse command line arguments */
113   if (argc > 1) {
114     while ((opt = getopt_long(argc, argv, "cf:d:hFVC:",
115                               long_opts, &option_index)) != EOF) {
116       switch(opt) 
117         {
118         case 'h':
119           silc_usage();
120           break;
121         case 'V':
122           printf("SILCd Secure Internet Live Conferencing daemon, "
123                  "version %s (base: SILC Toolkit %s)\n",
124                  silc_dist_version, silc_version);
125           printf("(c) 1997 - 2001 Pekka Riikonen "
126                  "<priikone@silcnet.org>\n");
127           exit(0);
128           break;
129         case 'd':
130           silc_debug = TRUE;
131           silc_debug_hexdump = TRUE;
132           silc_log_set_debug_string(optarg);
133 #ifndef SILC_DEBUG
134           fprintf(stdout, 
135                   "Run-time debugging is not enabled. To enable it recompile\n"
136                   "the server with --enable-debug configuration option.\n");
137 #endif
138           break;
139         case 'f':
140           config_file = strdup(optarg);
141           break;
142         case 'F':
143           foreground = TRUE;
144           break;
145
146           /*
147            * Key management options
148            */
149         case 'C':
150           opt_create_keypair = TRUE;
151           if (optarg)
152             opt_keypath = strdup(optarg);
153           break;
154         case 10:
155           if (optarg)
156             opt_pkcs = strdup(optarg);
157           break;
158         case 11:
159           if (optarg)
160             opt_bits = atoi(optarg);
161           break;
162         case 12:
163           if (optarg)
164             opt_identifier = strdup(optarg);
165           break;
166
167         default:
168           silc_usage();
169           break;
170         }
171     }
172   }
173
174   if (opt_create_keypair == TRUE) {
175     /* Create new key pair and exit */
176     silc_cipher_register_default();
177     silc_pkcs_register_default();
178     silc_hash_register_default();
179     silc_hmac_register_default();
180     silc_server_create_key_pair(opt_pkcs, opt_bits, opt_keypath,
181                                 opt_identifier, NULL, NULL);
182     exit(0);
183   }
184
185   /* Default configuration file */
186   if (!config_file)
187     config_file = strdup(SILC_SERVER_CONFIG_FILE);
188
189   /* Create SILC Server object */
190   ret = silc_server_alloc(&silcd);
191   if (ret == FALSE)
192     goto fail;
193
194   /* Read configuration files */
195   silcd->config = silc_server_config_alloc(config_file);
196   if (silcd->config == NULL)
197     goto fail;
198
199   /* Initialize the server */
200   ret = silc_server_init(silcd);
201   if (ret == FALSE)
202     goto fail;
203
204   /* Ignore SIGPIPE */
205   sa.sa_handler = SIG_IGN;
206   sa.sa_flags = 0;
207   sigemptyset(&sa.sa_mask);
208   sigaction(SIGPIPE, &sa, NULL);
209
210   if ((silc_debug == FALSE) && (foreground == FALSE))
211     /* Before running the server, fork to background. */    
212     silc_server_daemonise(silcd);
213
214   /* Set /var/run/silcd.pid */
215   unlink(SILC_SERVER_PID_FILE);
216   memset(pid, 0, sizeof(pid));
217   snprintf(pid, sizeof(pid) - 1, "%d\n", getpid());
218   if (silcd->config->pidfile && silcd->config->pidfile->pid_file) {
219     silc_file_writefile(silcd->config->pidfile->pid_file, pid, strlen(pid));
220   } else {
221     silc_file_writefile(SILC_SERVER_PID_FILE, pid, strlen(pid));
222   }
223   
224   /* Drop root. */
225   silc_server_drop(silcd);
226
227   /* Run the server. When this returns the server has been stopped
228      and we will exit. */
229   silc_server_run(silcd);
230   
231   /* Stop the server. This probably has been done already but it
232      doesn't hurt to do it here again. */
233   silc_server_stop(silcd);
234   silc_server_free(silcd);
235   
236   exit(0);
237  fail:
238   exit(1);
239 }
240
241 /* Returns identifier string for public key generation. */
242
243 static char *silc_server_create_identifier()
244 {
245   char *username = NULL, *realname = NULL;
246   char hostname[256], email[256];
247   
248   /* Get realname */
249   realname = silc_get_real_name();
250
251   /* Get hostname */
252   memset(hostname, 0, sizeof(hostname));
253   gethostname(hostname, sizeof(hostname));
254
255   /* Get username (mandatory) */
256   username = silc_get_username();
257   if (!username)
258     return NULL;
259
260   /* Create default email address, whether it is right or not */
261   snprintf(email, sizeof(email), "%s@%s", username, hostname);
262
263   return silc_pkcs_encode_identifier(username, hostname, realname, email,
264                                      NULL, NULL);
265 }
266
267 /* Creates new public key and private key pair. This is used only
268    when user wants to create new key pair from command line. */
269
270 static int 
271 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
272                             char *identifier, 
273                             SilcPublicKey *ret_pub_key,
274                             SilcPrivateKey *ret_prv_key)
275 {
276   SilcPKCS pkcs;
277   SilcPublicKey pub_key;
278   SilcPrivateKey prv_key;
279   SilcRng rng;
280   unsigned char *key;
281   uint32 key_len;
282   char pkfile[256], prvfile[256];
283
284   if (!pkcs_name || !path)
285     return FALSE;
286
287   if (!silc_pkcs_is_supported(pkcs_name)) {
288     fprintf(stderr, "Unsupported PKCS `%s'", pkcs_name);
289     return FALSE;
290   }
291
292   if (!bits)
293     bits = 1024;
294
295   if (!identifier)
296     identifier = silc_server_create_identifier();
297
298   rng = silc_rng_alloc();
299   silc_rng_init(rng);
300   silc_rng_global_init(rng);
301
302   snprintf(pkfile, sizeof(pkfile) - 1, "%s%s", path,
303            SILC_SERVER_PUBLIC_KEY_NAME);
304   snprintf(prvfile, sizeof(prvfile) - 1, "%s%s", path,
305            SILC_SERVER_PRIVATE_KEY_NAME);
306
307   /* Generate keys */
308   silc_pkcs_alloc(pkcs_name, &pkcs);
309   pkcs->pkcs->init(pkcs->context, bits, rng);
310
311   /* Save public key into file */
312   key = silc_pkcs_get_public_key(pkcs, &key_len);
313   pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
314                                        key, key_len);
315   silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
316   if (ret_pub_key)
317     *ret_pub_key = pub_key;
318   else
319     silc_pkcs_public_key_free(pub_key);
320
321   memset(key, 0, sizeof(key_len));
322   silc_free(key);
323
324   /* Save private key into file */
325   key = silc_pkcs_get_private_key(pkcs, &key_len);
326   prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
327   silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
328   if (ret_prv_key)
329     *ret_prv_key = prv_key;
330   else
331     silc_pkcs_private_key_free(prv_key);
332
333   printf("Public key has been saved into `%s'\n", pkfile);
334   printf("Private key has been saved into `%s'\n", prvfile);
335
336   memset(key, 0, sizeof(key_len));
337   silc_free(key);
338
339   silc_rng_free(rng);
340   silc_pkcs_free(pkcs);
341
342   return TRUE;
343 }