5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 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 static void silc_usage();
33 static char *silc_server_create_identifier();
35 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
37 SilcPublicKey *ret_pub_key,
38 SilcPrivateKey *ret_prv_key);
40 /* Long command line options */
41 static struct option long_opts[] =
43 { "config-file", 1, NULL, 'f' },
44 { "debug", 0, NULL, 'd' },
45 { "help", 0, NULL, 'h' },
46 { "version", 0, NULL,'V' },
48 /* Key management options */
49 { "create-key-pair", 1, NULL, 'C' },
50 { "pkcs", 1, NULL, 10 },
51 { "bits", 1, NULL, 11 },
52 { "identifier", 1, NULL, 12 },
57 /* Command line option variables */
58 static bool opt_create_keypair = FALSE;
59 static char *opt_keypath = NULL;
60 static char *opt_pkcs = "rsa";
61 static char *opt_identifier = NULL;
62 static int opt_bits = 1024;
64 /* Prints out the usage of silc client */
66 static void silc_usage()
69 Usage: silcd [options]\n\
72 -f --config-file=FILE Alternate configuration file\n\
73 -d --debug Enable debugging (no daemon)\n\
74 -h --help Display this message\n\
75 -V --version Display version\n\
77 Key Management Options:\n\
78 -C, --create-key-pair=PATH Create new public key pair\n\
79 --pkcs=PKCS Set the PKCS of the public key pair\n\
80 --bits=VALUE Set length of the public key pair\n\
81 --identifier=IDENTIFIER Public key identifier\n\
83 The public key identifier may be of the following format:\n\
85 UN=<username>, HN=<hostname or IP>, RN=<real name>, E=<email>,\n\
86 O=<organization>, C=<country>\n\
88 The UN and HN must be provided, the others are optional. If the\n\
89 --identifier option is not used an identifier will be created for\n\
90 the public key automatically.\n\
92 Example identifier: \"UN=foobar, HN=foo.bar.com, RN=Foo T. Bar, \n\
93 E=foo@bar.com, C=FI\"\n\
98 int main(int argc, char **argv)
101 int opt, option_index;
102 char *config_file = NULL;
108 /* Parse command line arguments */
110 while ((opt = getopt_long(argc, argv, "cf:dhVC:",
111 long_opts, &option_index)) != EOF) {
118 printf("SILCd Secure Internet Live Conferencing daemon, "
119 "version %s (base: SILC Toolkit %s)\n",
120 silc_dist_version, silc_version);
121 printf("(c) 1997 - 2001 Pekka Riikonen "
122 "<priikone@silcnet.org>\n");
129 config_file = strdup(optarg);
133 * Key management options
136 opt_create_keypair = TRUE;
138 opt_keypath = strdup(optarg);
142 opt_pkcs = strdup(optarg);
146 opt_bits = atoi(optarg);
150 opt_identifier = strdup(optarg);
160 if (opt_create_keypair == TRUE) {
161 /* Create new key pair and exit */
162 silc_cipher_register_default();
163 silc_pkcs_register_default();
164 silc_hash_register_default();
165 silc_hmac_register_default();
166 silc_server_create_key_pair(opt_pkcs, opt_bits, opt_keypath,
167 opt_identifier, NULL, NULL);
171 /* Default configuration file */
173 config_file = strdup(SILC_SERVER_CONFIG_FILE);
175 /* Create SILC Server object */
176 ret = silc_server_alloc(&silcd);
180 /* Read configuration files */
181 silcd->config = silc_server_config_alloc(config_file);
182 if (silcd->config == NULL)
185 /* Initialize the server */
186 ret = silc_server_init(silcd);
191 sa.sa_handler = SIG_IGN;
193 sigemptyset(&sa.sa_mask);
194 sigaction(SIGPIPE, &sa, NULL);
196 if (silc_debug == FALSE)
197 /* Before running the server, fork to background and set
198 both user and group no non-root */
199 silc_server_daemonise(silcd);
201 /* Run the server. When this returns the server has been stopped
203 silc_server_run(silcd);
205 /* Stop the server. This probably has been done already but it
206 doesn't hurt to do it here again. */
207 silc_server_stop(silcd);
208 silc_server_free(silcd);
215 /* Returns identifier string for public key generation. */
217 static char *silc_server_create_identifier()
219 char *username = NULL, *realname = NULL;
220 char hostname[256], email[256];
223 realname = silc_get_real_name();
226 memset(hostname, 0, sizeof(hostname));
227 gethostname(hostname, sizeof(hostname));
229 /* Get username (mandatory) */
230 username = silc_get_username();
234 /* Create default email address, whether it is right or not */
235 snprintf(email, sizeof(email), "%s@%s", username, hostname);
237 return silc_pkcs_encode_identifier(username, hostname, realname, email,
241 /* Creates new public key and private key pair. This is used only
242 when user wants to create new key pair from command line. */
245 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
247 SilcPublicKey *ret_pub_key,
248 SilcPrivateKey *ret_prv_key)
251 SilcPublicKey pub_key;
252 SilcPrivateKey prv_key;
256 char pkfile[256], prvfile[256];
258 if (!pkcs_name || !path)
261 if (!silc_pkcs_is_supported(pkcs_name)) {
262 fprintf(stderr, "Unsupported PKCS `%s'", pkcs_name);
270 identifier = silc_server_create_identifier();
272 rng = silc_rng_alloc();
274 silc_rng_global_init(rng);
276 snprintf(pkfile, sizeof(pkfile) - 1, "%s%s", path,
277 SILC_SERVER_PUBLIC_KEY_NAME);
278 snprintf(prvfile, sizeof(prvfile) - 1, "%s%s", path,
279 SILC_SERVER_PRIVATE_KEY_NAME);
282 silc_pkcs_alloc(pkcs_name, &pkcs);
283 pkcs->pkcs->init(pkcs->context, bits, rng);
285 /* Save public key into file */
286 key = silc_pkcs_get_public_key(pkcs, &key_len);
287 pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
289 silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
291 *ret_pub_key = pub_key;
293 silc_pkcs_public_key_free(pub_key);
295 memset(key, 0, sizeof(key_len));
298 /* Save private key into file */
299 key = silc_pkcs_get_private_key(pkcs, &key_len);
300 prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
301 silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
303 *ret_prv_key = prv_key;
305 silc_pkcs_private_key_free(prv_key);
307 printf("Public key has been saved into `%s'\n", pkfile);
308 printf("Private key has been saved into `%s'\n", prvfile);
310 memset(key, 0, sizeof(key_len));
314 silc_pkcs_free(pkcs);