5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
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 },
56 /* Command line option variables */
57 static bool opt_create_keypair = FALSE;
58 static char *opt_keypath = NULL;
59 static char *opt_pkcs = "rsa";
60 static int opt_bits = 1024;
62 /* Prints out the usage of silc client */
64 static void silc_usage()
67 Usage: silcd [options]\n\
70 -f --config-file=FILE Alternate configuration file\n\
71 -d --debug Enable debugging (no daemon)\n\
72 -h --help Display this message\n\
73 -V --version Display version\n\
75 Key Management Options:\n\
76 -C, --create-key-pair=PATH Create new public key pair\n\
77 --pkcs=PKCS Set the PKCS of the public key pair\n\
78 --bits=VALUE Set length of the public key pair\n\
83 int main(int argc, char **argv)
86 int opt, option_index;
87 char *config_file = NULL;
92 /* Parse command line arguments */
94 while ((opt = getopt_long(argc, argv, "cf:dhVC:",
95 long_opts, &option_index)) != EOF) {
102 printf("SILCd Secure Internet Live Conferencing daemon, "
103 "version %s\n", silc_version);
104 printf("(c) 1997 - 2001 Pekka Riikonen "
105 "<priikone@poseidon.pspt.fi>\n");
112 config_file = strdup(optarg);
116 * Key management options
119 opt_create_keypair = TRUE;
121 opt_keypath = strdup(optarg);
125 opt_pkcs = strdup(optarg);
129 opt_bits = atoi(optarg);
139 if (opt_create_keypair == TRUE) {
140 /* Create new key pair and exit */
141 silc_server_create_key_pair(opt_pkcs, opt_bits, opt_keypath,
146 /* Default configuration file */
148 config_file = strdup(SILC_SERVER_CONFIG_FILE);
150 /* Create SILC Server object */
151 ret = silc_server_alloc(&silcd);
155 /* Read configuration files */
156 silcd->config = silc_server_config_alloc(config_file);
157 if (silcd->config == NULL)
160 /* Initialize the server */
161 ret = silc_server_init(silcd);
165 if (silc_debug == FALSE)
166 /* Before running the server, fork to background and set
167 both user and group no non-root */
168 silc_server_daemonise(silcd);
170 /* Run the server. When this returns the server has been stopped
172 silc_server_run(silcd);
174 /* Stop the server. This probably has been done already but it
175 doesn't hurt to do it here again. */
176 silc_server_stop(silcd);
177 silc_server_free(silcd);
184 /* Returns identifier string for public key generation. */
186 static char *silc_server_create_identifier()
188 char *username = NULL, *realname = NULL;
189 char hostname[256], email[256];
192 realname = silc_get_real_name();
195 memset(hostname, 0, sizeof(hostname));
196 gethostname(hostname, sizeof(hostname));
198 /* Get username (mandatory) */
199 username = silc_get_username();
203 /* Create default email address, whether it is right or not */
204 snprintf(email, sizeof(email), "%s@%s", username, hostname);
206 return silc_pkcs_encode_identifier(username, hostname, realname, email,
210 /* Creates new public key and private key pair. This is used only
211 when user wants to create new key pair from command line. */
214 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
216 SilcPublicKey *ret_pub_key,
217 SilcPrivateKey *ret_prv_key)
220 SilcPublicKey pub_key;
221 SilcPrivateKey prv_key;
225 char pkfile[256], prvfile[256];
227 if (!pkcs_name || !path)
230 if (!silc_pkcs_is_supported(pkcs_name)) {
231 fprintf(stderr, "Unsupported PKCS `%s'", pkcs_name);
239 identifier = silc_server_create_identifier();
241 rng = silc_rng_alloc();
243 silc_rng_global_init(rng);
245 snprintf(pkfile, sizeof(pkfile) - 1, "%s%s", path,
246 SILC_SERVER_PUBLIC_KEY_NAME);
247 snprintf(prvfile, sizeof(prvfile) - 1, "%s%s", path,
248 SILC_SERVER_PRIVATE_KEY_NAME);
251 silc_pkcs_alloc(pkcs_name, &pkcs);
252 pkcs->pkcs->init(pkcs->context, bits, rng);
254 /* Save public key into file */
255 key = silc_pkcs_get_public_key(pkcs, &key_len);
256 pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
258 silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
260 *ret_pub_key = pub_key;
262 silc_pkcs_public_key_free(pub_key);
264 memset(key, 0, sizeof(key_len));
267 /* Save private key into file */
268 key = silc_pkcs_get_private_key(pkcs, &key_len);
269 prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
270 silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
272 *ret_prv_key = prv_key;
274 silc_pkcs_private_key_free(prv_key);
276 printf("Public key has been saved into `%s'\n", pkfile);
277 printf("Private key has been saved into `%s'\n", prvfile);
279 memset(key, 0, sizeof(key_len));
283 silc_pkcs_free(pkcs);