5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
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.
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.
21 * Created: Wed Mar 19 00:17:12 1997
23 * This is the main program for the SILC daemon. This parses command
24 * line arguments and creates the server object.
28 #include "serverincludes.h"
29 #include "server_internal.h"
32 /* For now, we'll have this one server context global for this module. */
33 static SilcServer silcd;
35 static void silc_usage();
36 static char *silc_server_create_identifier();
38 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
40 SilcPublicKey *ret_pub_key,
41 SilcPrivateKey *ret_prv_key);
43 /* Long command line options */
44 static struct option long_opts[] =
46 { "config-file", 1, NULL, 'f' },
47 { "debug", 1, NULL, 'd' },
48 { "help", 0, NULL, 'h' },
49 { "foreground", 0, NULL, 'F' },
50 { "version", 0, NULL,'V' },
52 /* Key management options */
53 { "create-key-pair", 1, NULL, 'C' },
54 { "pkcs", 1, NULL, 10 },
55 { "bits", 1, NULL, 11 },
56 { "identifier", 1, NULL, 12 },
61 /* Command line option variables */
62 static bool opt_create_keypair = FALSE;
63 static char *opt_keypath = NULL;
64 static char *opt_pkcs = "rsa";
65 static char *opt_identifier = NULL;
66 static int opt_bits = 1024;
68 /* Prints out the usage of silc client */
70 static void silc_usage()
73 Usage: silcd [options]\n\
76 -f --config-file=FILE Alternate configuration file\n\
77 -d --debug=string Enable debugging (Implies --foreground)\n\
78 -h --help Display this message\n\
79 -F --foreground Dont fork\n\
80 -V --version Display version\n\
82 Key Management Options:\n\
83 -C, --create-key-pair=PATH Create new public key pair\n\
84 --pkcs=PKCS Set the PKCS of the public key pair\n\
85 --bits=VALUE Set length of the public key pair\n\
86 --identifier=IDENTIFIER Public key identifier\n\
88 The public key identifier may be of the following format:\n\
90 UN=<username>, HN=<hostname or IP>, RN=<real name>, E=<email>,\n\
91 O=<organization>, C=<country>\n\
93 The UN and HN must be provided, the others are optional. If the\n\
94 --identifier option is not used an identifier will be created for\n\
95 the public key automatically.\n\
97 Example identifier: \"UN=foobar, HN=foo.bar.com, RN=Foo T. Bar, \n\
98 E=foo@bar.com, C=FI\"\n\
103 /* Dies if a *valid* pid file exists already */
105 static void silc_checkpid(SilcServer silcd)
107 if (silcd->config->pidfile && silcd->config->pidfile->pid_file) {
112 SILC_LOG_DEBUG(("Checking for another silcd running"));
113 buf = silc_file_readfile(silcd->config->pidfile->pid_file, &buf_len);
120 kill(oldpid, SIGCHLD); /* this signal does nothing, check if alive */
121 if (errno != ESRCH) {
122 fprintf(stderr, "\nI detected another daemon running with the same pid file.\n");
123 fprintf(stderr, "Please change the config file, or erase the %s\n",
124 silcd->config->pidfile->pid_file);
130 static void got_hup(int z)
132 /* First, reset all log files (they might have been deleted) */
133 silc_log_reset_all();
134 silc_log_flush_all();
137 static void stop_server(int z)
139 SILC_LOG_DEBUG(("Start"));
141 /* Flush log files */
142 silc_log_flush_all();
144 /* Gracefully stop the server */
145 /* silc_server_stop(silcd); */
146 /* XXX do this for now since doing graceful exit now can remove
147 the scheduler underneath the server too early and crash it. */
151 int main(int argc, char **argv)
153 int ret, opt, option_index;
154 char *config_file = NULL;
155 bool foreground = FALSE;
158 /* Parse command line arguments */
160 while ((opt = getopt_long(argc, argv, "cf:d:hFVC:",
161 long_opts, &option_index)) != EOF) {
168 printf("SILCd Secure Internet Live Conferencing daemon, "
169 "version %s (base: SILC Toolkit %s)\n",
170 silc_dist_version, silc_version);
171 printf("(c) 1997 - 2001 Pekka Riikonen "
172 "<priikone@silcnet.org>\n");
178 silc_debug_hexdump = TRUE;
179 silc_log_set_debug_string(optarg);
181 silc_log_quick = TRUE;
184 "Run-time debugging is not enabled. To enable it recompile\n"
185 "the server with --enable-debug configuration option.\n");
189 config_file = strdup(optarg);
196 * Key management options
199 opt_create_keypair = TRUE;
201 opt_keypath = strdup(optarg);
205 opt_pkcs = strdup(optarg);
209 opt_bits = atoi(optarg);
213 opt_identifier = strdup(optarg);
223 if (opt_create_keypair == TRUE) {
224 /* Create new key pair and exit */
225 silc_cipher_register_default();
226 silc_pkcs_register_default();
227 silc_hash_register_default();
228 silc_hmac_register_default();
229 silc_server_create_key_pair(opt_pkcs, opt_bits, opt_keypath,
230 opt_identifier, NULL, NULL);
234 /* Default configuration file */
236 config_file = strdup(SILC_SERVER_CONFIG_FILE);
238 /* Create SILC Server object */
239 ret = silc_server_alloc(&silcd);
243 /* Read configuration files */
244 silcd->config = silc_server_config_alloc(config_file);
245 if (silcd->config == NULL)
248 /* Check for another silcd running */
249 silc_checkpid(silcd);
251 /* Initialize the server */
252 ret = silc_server_init(silcd);
257 sa.sa_handler = SIG_IGN;
259 sigemptyset(&sa.sa_mask);
260 sigaction(SIGPIPE, &sa, NULL);
261 sa.sa_handler = got_hup;
262 sigaction(SIGHUP, &sa, NULL);
263 sa.sa_handler = stop_server;
264 sigaction(SIGTERM, &sa, NULL);
265 sa.sa_handler = stop_server;
266 sigaction(SIGINT, &sa, NULL);
268 /* Before running the server, fork to background. */
270 silc_server_daemonise(silcd);
272 /* If set, write pid to file */
273 if (silcd->config->pidfile && silcd->config->pidfile->pid_file) {
275 unlink(silcd->config->pidfile->pid_file);
276 snprintf(buf, sizeof(buf) - 1, "%d\n", getpid());
277 silc_file_writefile(silcd->config->pidfile->pid_file, buf, strlen(buf));
281 silc_server_drop(silcd);
283 /* Run the server. When this returns the server has been stopped
285 silc_server_run(silcd);
287 /* The server was stopped, free it now */
288 silc_server_free(silcd);
290 /* Flush the logging system */
291 silc_log_flush_all();
298 /* Returns identifier string for public key generation. */
300 static char *silc_server_create_identifier()
302 char *username = NULL, *realname = NULL;
303 char hostname[256], email[256];
306 realname = silc_get_real_name();
309 memset(hostname, 0, sizeof(hostname));
310 gethostname(hostname, sizeof(hostname));
312 /* Get username (mandatory) */
313 username = silc_get_username();
317 /* Create default email address, whether it is right or not */
318 snprintf(email, sizeof(email), "%s@%s", username, hostname);
320 return silc_pkcs_encode_identifier(username, hostname, realname, email,
324 /* Creates new public key and private key pair. This is used only
325 when user wants to create new key pair from command line. */
328 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
330 SilcPublicKey *ret_pub_key,
331 SilcPrivateKey *ret_prv_key)
334 SilcPublicKey pub_key;
335 SilcPrivateKey prv_key;
339 char pkfile[256], prvfile[256];
341 if (!pkcs_name || !path)
344 if (!silc_pkcs_is_supported(pkcs_name)) {
345 fprintf(stderr, "Unsupported PKCS `%s'", pkcs_name);
353 identifier = silc_server_create_identifier();
355 rng = silc_rng_alloc();
357 silc_rng_global_init(rng);
359 snprintf(pkfile, sizeof(pkfile) - 1, "%s%s", path,
360 SILC_SERVER_PUBLIC_KEY_NAME);
361 snprintf(prvfile, sizeof(prvfile) - 1, "%s%s", path,
362 SILC_SERVER_PRIVATE_KEY_NAME);
365 silc_pkcs_alloc(pkcs_name, &pkcs);
366 pkcs->pkcs->init(pkcs->context, bits, rng);
368 /* Save public key into file */
369 key = silc_pkcs_get_public_key(pkcs, &key_len);
370 pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
372 silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
374 *ret_pub_key = pub_key;
376 silc_pkcs_public_key_free(pub_key);
378 memset(key, 0, sizeof(key_len));
381 /* Save private key into file */
382 key = silc_pkcs_get_private_key(pkcs, &key_len);
383 prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
384 silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
386 *ret_prv_key = prv_key;
388 silc_pkcs_private_key_free(prv_key);
390 printf("Public key has been saved into `%s'\n", pkfile);
391 printf("Private key has been saved into `%s'\n", prvfile);
393 memset(key, 0, sizeof(key_len));
397 silc_pkcs_free(pkcs);