Initial revision
[silc.git] / apps / silc / silc.c
1 /*
2
3   silc.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 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  * $Id$
22  * $Log$
23  * Revision 1.1  2000/06/27 11:36:56  priikone
24  * Initial revision
25  *
26  *
27  */
28
29 #include "clientincludes.h"
30 #include "version.h"
31
32 /* Long command line options */
33 static struct option long_opts[] = 
34 {
35   /* Generic options */
36   { "server", 1, NULL, 's' },
37   { "port", 1, NULL, 'p' },
38   { "nickname", 1, NULL, 'n' },
39   { "channel", 1, NULL, 'c' },
40   { "cipher", 1, NULL, 'r' },
41   { "public-key", 1, NULL, 'b' },
42   { "private-key", 1, NULL, 'k' },
43   { "config-file", 1, NULL, 'f' },
44   { "no-silcrc", 0, NULL, 'q' },
45   { "help", 0, NULL, 'h' },
46   { "version", 0, NULL, 'V' },
47   { "list-ciphers", 0, NULL, 1 },
48   { "list-hash-funcs", 0, NULL, 2 },
49   { "list-pkcs", 0, NULL, 3 },
50
51   /* Key management options */
52   { "create-key-pair", 0, NULL, 'C' },
53   { "pkcs", 1, NULL, 10 },
54   { "bits", 1, NULL, 11 },
55
56   { NULL, 0, NULL, 0 }
57 };
58
59 /* Command line option variables */
60 static char *opt_server = NULL;
61 static int opt_port = 0;
62 static char *opt_nickname = NULL;
63 static char *opt_channel = NULL;
64 static char *opt_cipher = NULL;
65 static char *opt_public_key = NULL;
66 static char *opt_private_key = NULL;
67 static char *opt_config_file = NULL;
68 static int opt_no_silcrc = FALSE;
69
70 static int opt_create_keypair = FALSE;
71 static char *opt_pkcs = NULL;
72 static int opt_bits = 0;
73
74 /* Prints out the usage of silc client */
75
76 void usage()
77 {
78   printf("\
79 Usage: silc [options]\n\
80 \n\
81   Generic Options:\n\
82   -s, --server=HOST            Open connection to server HOST\n\
83   -p, --port=PORT              Set PORT as default port to connect\n\
84   -n, --nickname=STRING        Set default nickname on startup\n\
85   -c, --channel=STRING         Join channel on startup\n\
86   -r, --cipher=CIPHER          Use CIPHER as default cipher in SILC\n\
87   -b, --public-key=FILE        Public key used in SILC\n\
88   -k, --private-key=FILE       Private key used in SILC\n\
89   -f, --config-file=FILE       Alternate configuration file\n\
90   -q, --no-silcrc              Don't load ~/.silcrc on startup\n\
91   -h, --help                   Display this help message\n\
92   -V, --version                Display version\n\
93       --list-ciphers           List supported ciphers\n\
94       --list-hash-funcs        List supported hash functions\n\
95       --list-pkcs              List supported PKCS's\n\
96 \n\
97   Key Management Options:\n\
98   -C, --create-key-pair        Create new public key pair\n\
99       --pkcs=PKCS              Set the PKCS of the public key pair\n\
100       --bits=VALUE             Set length of the public key pair\n\
101 \n");
102 }
103
104 int main(int argc, char **argv)
105 {
106   int opt, option_index = 1;
107   int ret;
108   SilcClient silc = NULL;
109   SilcClientConfig config = NULL;
110   
111   if (argc > 1) 
112     {
113       while ((opt = 
114               getopt_long(argc, argv,
115                           "s:p:n:c:b:k:f:qhVC",
116                           long_opts, &option_index)) != EOF)
117         {
118           switch(opt) 
119             {
120               /* 
121                * Generic options
122                */
123             case 's':
124               if (optarg)
125                 opt_server = strdup(optarg);
126               break;
127             case 'p':
128               if (optarg)
129                 opt_port = atoi(optarg);
130               break;
131             case 'n':
132               if (optarg)
133                 opt_nickname = strdup(optarg);
134               break;
135             case 'c':
136               if (optarg)
137                 opt_channel = strdup(optarg);
138               break;
139             case 'r':
140               if (optarg)
141                 opt_cipher = strdup(optarg);
142               break;
143             case 'b':
144               if (optarg)
145                 opt_public_key = strdup(optarg);
146               break;
147             case 'k':
148               if (optarg)
149                 opt_private_key = strdup(optarg);
150               break;
151             case 'f':
152               if (optarg)
153                 opt_config_file = strdup(optarg);
154               break;
155             case 'q':
156               opt_no_silcrc = TRUE;
157               break;
158             case 'h':
159               usage();
160               exit(0);
161               break;
162             case 'V':
163               printf("\
164 SILC Secure Internet Live Conferencing, version %s\n", 
165                      silc_version);
166               printf("\
167 (c) 1997 - 2000 Pekka Riikonen <priikone@poseidon.pspt.fi>\n");
168               exit(0);
169               break;
170             case 1:
171               silc_client_list_ciphers();
172               exit(0);
173               break;
174             case 2:
175               silc_client_list_hash_funcs();
176               exit(0);
177               break;
178             case 3:
179               silc_client_list_pkcs();
180               exit(0);
181               break;
182
183               /*
184                * Key management options
185                */
186             case 'C':
187               opt_create_keypair = TRUE;
188               break;
189             case 10:
190               if (optarg)
191                 opt_pkcs = strdup(optarg);
192               break;
193             case 11:
194               if (optarg)
195                 opt_bits = atoi(optarg);
196               break;
197
198             default:
199               exit(0);
200               break;
201             }
202         }
203     }
204
205   /* Init signals */
206   signal(SIGHUP, SIG_DFL);
207   signal(SIGTERM, SIG_DFL);
208   signal(SIGPIPE, SIG_IGN);
209   signal(SIGCHLD, SIG_DFL);
210   signal(SIGALRM, SIG_IGN);
211   signal(SIGQUIT, SIG_IGN);
212   signal(SIGSEGV, SIG_DFL);
213   signal(SIGBUS, SIG_DFL);
214   signal(SIGFPE, SIG_DFL);
215   //  signal(SIGINT, SIG_IGN);
216   
217   /* Default configuration file */
218   if (!opt_config_file)
219     opt_config_file = strdup(SILC_CLIENT_CONFIG_FILE);
220
221   /* Read global configuration file. */
222   config = silc_client_config_alloc(opt_config_file);
223   if (config == NULL)
224     goto fail;
225
226   if (opt_create_keypair == TRUE) {
227     /* Create new key pair and exit */
228     silc_client_create_key_pair(opt_pkcs, opt_bits);
229     exit(0);
230   }
231
232   /* Read local configuration file */
233
234
235   /* Allocate new client */
236   ret = silc_client_alloc(&silc);
237   if (ret == FALSE)
238     goto fail;
239
240   /* Initialize the client */
241   silc->config = config;
242   ret = silc_client_init(silc);
243   if (ret == FALSE)
244     goto fail;
245
246   /* Run the client */
247   silc_client_run(silc);
248
249   /* Stop the client. This probably has been done already but it
250      doesn't hurt to do it here again. */
251   silc_client_stop(silc);
252   silc_client_free(silc);
253   
254   exit(0);
255
256  fail:
257   if (config)
258     silc_client_config_free(config);
259   if (silc)
260     silc_client_free(silc);
261   exit(1);
262 }