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