updates.
[silc.git] / apps / silcd / silcd.c
1 /*
2
3   silcd.c
4   
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
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", 0, NULL, 'd' },
45   { "help", 0, NULL, 'h' },
46   { "version", 0, NULL,'V' },
47
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 },
53
54   { NULL, 0, NULL, 0 }
55 };
56
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;
63
64 /* Prints out the usage of silc client */
65
66 static void silc_usage()
67 {
68   printf("\
69 Usage: silcd [options]\n\
70 \n\
71   Generic 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\
76 \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\
82 \n\
83       The public key identifier may be of the following format:\n\
84 \n\
85       UN=<username>, HN=<hostname or IP>, RN=<real name>, E=<email>,\n\
86       O=<organization>, C=<country>\n\
87 \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\
91 \n\
92       Example identifier: \"UN=foobar, HN=foo.bar.com, RN=Foo T. Bar, \n\
93                            E=foo@bar.com, C=FI\"\n\
94 \n");
95   exit(0);
96 }
97
98 int main(int argc, char **argv)
99 {
100   int ret;
101   int opt, option_index;
102   char *config_file = NULL;
103   SilcServer silcd;
104
105   silc_debug = FALSE;
106
107   /* Parse command line arguments */
108   if (argc > 1) {
109     while ((opt = getopt_long(argc, argv, "cf:dhVC:",
110                               long_opts, &option_index)) != EOF) {
111       switch(opt) 
112         {
113         case 'h':
114           silc_usage();
115           break;
116         case 'V':
117           printf("SILCd Secure Internet Live Conferencing daemon, "
118                  "version %s (base: SILC Toolkit %s)\n",
119                  silc_dist_version, silc_version);
120           printf("(c) 1997 - 2001 Pekka Riikonen "
121                  "<priikone@poseidon.pspt.fi>\n");
122           exit(0);
123           break;
124         case 'd':
125           silc_debug = TRUE;
126           break;
127         case 'f':
128           config_file = strdup(optarg);
129           break;
130
131           /*
132            * Key management options
133            */
134         case 'C':
135           opt_create_keypair = TRUE;
136           if (optarg)
137             opt_keypath = strdup(optarg);
138           break;
139         case 10:
140           if (optarg)
141             opt_pkcs = strdup(optarg);
142           break;
143         case 11:
144           if (optarg)
145             opt_bits = atoi(optarg);
146           break;
147         case 12:
148           if (optarg)
149             opt_identifier = strdup(optarg);
150           break;
151
152         default:
153           silc_usage();
154           break;
155         }
156     }
157   }
158
159   if (opt_create_keypair == TRUE) {
160     /* Create new key pair and exit */
161     silc_cipher_register_default();
162     silc_pkcs_register_default();
163     silc_hash_register_default();
164     silc_hmac_register_default();
165     silc_server_create_key_pair(opt_pkcs, opt_bits, opt_keypath,
166                                 opt_identifier, NULL, NULL);
167     exit(0);
168   }
169
170   /* Default configuration file */
171   if (!config_file)
172     config_file = strdup(SILC_SERVER_CONFIG_FILE);
173
174   /* Create SILC Server object */
175   ret = silc_server_alloc(&silcd);
176   if (ret == FALSE)
177     goto fail;
178
179   /* Read configuration files */
180   silcd->config = silc_server_config_alloc(config_file);
181   if (silcd->config == NULL)
182     goto fail;
183
184   /* Initialize the server */
185   ret = silc_server_init(silcd);
186   if (ret == FALSE)
187     goto fail;
188
189   if (silc_debug == FALSE)
190     /* Before running the server, fork to background and set
191        both user and group no non-root */    
192     silc_server_daemonise(silcd);
193   
194   /* Run the server. When this returns the server has been stopped
195      and we will exit. */
196   silc_server_run(silcd);
197   
198   /* Stop the server. This probably has been done already but it
199      doesn't hurt to do it here again. */
200   silc_server_stop(silcd);
201   silc_server_free(silcd);
202   
203   exit(0);
204  fail:
205   exit(1);
206 }
207
208 /* Returns identifier string for public key generation. */
209
210 static char *silc_server_create_identifier()
211 {
212   char *username = NULL, *realname = NULL;
213   char hostname[256], email[256];
214   
215   /* Get realname */
216   realname = silc_get_real_name();
217
218   /* Get hostname */
219   memset(hostname, 0, sizeof(hostname));
220   gethostname(hostname, sizeof(hostname));
221
222   /* Get username (mandatory) */
223   username = silc_get_username();
224   if (!username)
225     return NULL;
226
227   /* Create default email address, whether it is right or not */
228   snprintf(email, sizeof(email), "%s@%s", username, hostname);
229
230   return silc_pkcs_encode_identifier(username, hostname, realname, email,
231                                      NULL, NULL);
232 }
233
234 /* Creates new public key and private key pair. This is used only
235    when user wants to create new key pair from command line. */
236
237 static int 
238 silc_server_create_key_pair(char *pkcs_name, int bits, char *path,
239                             char *identifier, 
240                             SilcPublicKey *ret_pub_key,
241                             SilcPrivateKey *ret_prv_key)
242 {
243   SilcPKCS pkcs;
244   SilcPublicKey pub_key;
245   SilcPrivateKey prv_key;
246   SilcRng rng;
247   unsigned char *key;
248   uint32 key_len;
249   char pkfile[256], prvfile[256];
250
251   if (!pkcs_name || !path)
252     return FALSE;
253
254   if (!silc_pkcs_is_supported(pkcs_name)) {
255     fprintf(stderr, "Unsupported PKCS `%s'", pkcs_name);
256     return FALSE;
257   }
258
259   if (!bits)
260     bits = 1024;
261
262   if (!identifier)
263     identifier = silc_server_create_identifier();
264
265   rng = silc_rng_alloc();
266   silc_rng_init(rng);
267   silc_rng_global_init(rng);
268
269   snprintf(pkfile, sizeof(pkfile) - 1, "%s%s", path,
270            SILC_SERVER_PUBLIC_KEY_NAME);
271   snprintf(prvfile, sizeof(prvfile) - 1, "%s%s", path,
272            SILC_SERVER_PRIVATE_KEY_NAME);
273
274   /* Generate keys */
275   silc_pkcs_alloc(pkcs_name, &pkcs);
276   pkcs->pkcs->init(pkcs->context, bits, rng);
277
278   /* Save public key into file */
279   key = silc_pkcs_get_public_key(pkcs, &key_len);
280   pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
281                                        key, key_len);
282   silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
283   if (ret_pub_key)
284     *ret_pub_key = pub_key;
285   else
286     silc_pkcs_public_key_free(pub_key);
287
288   memset(key, 0, sizeof(key_len));
289   silc_free(key);
290
291   /* Save private key into file */
292   key = silc_pkcs_get_private_key(pkcs, &key_len);
293   prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
294   silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
295   if (ret_prv_key)
296     *ret_prv_key = prv_key;
297   else
298     silc_pkcs_private_key_free(prv_key);
299
300   printf("Public key has been saved into `%s'\n", pkfile);
301   printf("Private key has been saved into `%s'\n", prvfile);
302
303   memset(key, 0, sizeof(key_len));
304   silc_free(key);
305
306   silc_rng_free(rng);
307   silc_pkcs_free(pkcs);
308
309   return TRUE;
310 }