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