Code auditing weekend results and fixes committing.
[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 /* Long command line options */
33 static struct option long_opts[] = 
34 {
35   { "config-file", 1, NULL, 'f' },
36   { "generate-config-file", 0, NULL, 'c' },
37   { "help", 0, NULL, 'h' },
38   { "version", 0, NULL,'V' },
39   { NULL, 0, NULL, 0 }
40 };
41
42 /* Prints out the usage of silc client */
43
44 void silc_usage()
45 {
46   printf("Usage: silcd [options]\n");
47   printf("Options:\n");
48   printf("  -f  --config-file=FILE        Alternate configuration file\n");
49   printf("  -c  --generate-config-file    Generate example configuration "
50          "file\n");
51   printf("  -h  --help                    Display this message\n");
52   printf("  -V  --version                 Display version\n");
53   exit(0);
54 }
55
56 int main(int argc, char **argv)
57 {
58   int ret;
59   int opt, option_index;
60   char *config_file = NULL;
61   SilcServer silcd;
62
63   silc_debug = TRUE;
64
65   /* Parse command line arguments */
66   if (argc > 1) {
67     while ((opt = getopt_long(argc, argv, "cf:hV",
68                               long_opts, &option_index)) != EOF) {
69       switch(opt) 
70         {
71         case 'h':
72           silc_usage();
73           break;
74         case 'V':
75           printf("SILCd Secure Internet Live Conferencing daemon, "
76                  "version %s\n", silc_version);
77           printf("(c) 1997 - 2000 Pekka Riikonen "
78                  "<priikone@poseidon.pspt.fi>\n");
79           exit(0);
80           break;
81         case 'c':
82           /* Print out example configuration file */
83           silc_config_server_print();
84           exit(0);
85           break;
86         case 'f':
87           config_file = strdup(optarg);
88           break;
89         default:
90           silc_usage();
91           break;
92         }
93     }
94   }
95
96   /* Default configuration file */
97   if (!config_file)
98     config_file = strdup(SILC_SERVER_CONFIG_FILE);
99
100   /* Create SILC Server object */
101   ret = silc_server_alloc(&silcd);
102   if (ret == FALSE)
103     goto fail;
104
105   /* Read configuration files */
106   silcd->config = silc_config_server_alloc(config_file);
107   if (silcd->config == NULL)
108     goto fail;
109
110   /* Initialize the server */
111   ret = silc_server_init(silcd);
112   if (ret == FALSE)
113     goto fail;
114   
115   /* Run the server. When this returns the server has been stopped
116      and we will exit. */
117   silc_server_run(silcd);
118   
119   /* Stop the server. This probably has been done already but it
120      doesn't hurt to do it here again. */
121   silc_server_stop(silcd);
122   silc_server_free(silcd);
123   
124   exit(0);
125  fail:
126   exit(1);
127 }