Fix return value of dccp_recv pre_select()
[paraslash.git] / client.c
1 /*
2  * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file client.c the client program used to connect to para_server */
20
21 #include "para.h"
22 #include "config.h"
23 #include <readline/readline.h>
24 #include <readline/history.h>
25 #include "client.cmdline.h"
26 #include "crypt.h"
27 #include "rc4.h"
28 #include <openssl/rc4.h>
29 #include "net.h"
30 #include "string.h"
31
32 /* A static variable for holding the line. */
33 static char *line_read;
34
35 struct gengetopt_args_info args_info;
36
37 /*
38  * client log function
39  */
40 void para_log(int ll, char* fmt,...)
41 {
42         va_list argp;
43         FILE *outfd;
44
45         /* ignore log message if loglevel is not high enough */
46         if (ll < args_info.loglevel_arg)
47                 return;
48         if (ll < WARNING)
49                 outfd = stdout;
50         else
51                 outfd = stderr;
52         va_start(argp, fmt);
53         vfprintf(stdout, fmt, argp);
54         va_end(argp);
55 }
56
57 /*
58  * Read a string, and return a pointer to it. Returns NULL on EOF.
59  */
60 static char *rl_gets(void)
61 {
62         free(line_read);
63         /* Get a line from the user. */
64         line_read = readline("para_client> ");
65         /* If the line has any text in it, save it on the history. */
66         if (line_read && *line_read)
67                 add_history(line_read);
68         return line_read;
69 }
70
71 /*
72  * do several cleanups on sigint
73  */
74 static void sigint_handler(__unused int i)
75 {
76         rl_cleanup_after_signal();
77         rl_reset_after_signal();
78 }
79
80 void get_options(int argc, char *argv[],
81         char **config_file, char **key_file)
82 {
83         char *home;
84         static char default_key_file[_POSIX_PATH_MAX] = "";
85         static char default_config_file[_POSIX_PATH_MAX] = "";
86         struct stat statbuf;
87         int ret;
88
89         cmdline_parser(argc, argv, &args_info);
90         if (!args_info.user_given)
91                 args_info.user_arg = para_logname();
92         if (!args_info.key_file_given) {
93                 home = para_homedir();
94                 sprintf(default_key_file, "%s/.paraslash/key.%s", home,
95                         args_info.user_arg);
96                 free(home);
97         }
98         if (!args_info.config_file_given) {
99                 home = para_homedir();
100                 sprintf(default_config_file, "%s/.paraslash/client.conf",
101                         home);
102                 free(home);
103         }
104         if (!args_info.config_file_given)
105                 *config_file = default_config_file;
106         else
107                 *config_file = args_info.config_file_arg;
108         ret = stat(*config_file, &statbuf);
109         if (ret && args_info.config_file_given) {
110                 fprintf(stderr, "can not stat config file %s\n",
111                         args_info.config_file_arg);
112                 exit(EXIT_FAILURE);
113         }
114         if (!ret)
115                 cmdline_parser_configfile(*config_file, &args_info, 0, 0, 0);
116         if (!args_info.key_file_given)
117                 *key_file = default_key_file;
118         else
119                 *key_file = args_info.key_file_arg;
120 }
121
122 static RC4_KEY rc4_recv_key;
123 static RC4_KEY rc4_send_key;
124 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
125
126 static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
127 {
128         RC4(&rc4_send_key, len, indata, outdata);
129 }
130
131 static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
132 {
133         RC4(&rc4_recv_key, len, indata, outdata);
134 }
135 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
136 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
137
138
139 static void append_str(char **data, const char* append)
140 {
141         if (*data) {
142                 char *tmp = make_message("%s\n%s", *data, append);
143                 free(*data);
144                 *data = tmp;
145         } else
146                 *data = para_strdup(append);
147 }
148
149 /*
150  * MAIN
151  */
152 int main(int argc, char *argv[])
153 {
154
155         int sockfd, numbytes, i, interactive, received, ret;
156         struct hostent *he;
157         struct sockaddr_in their_addr;
158         char *command = NULL;
159         char buf[8192];
160         char *auth_str;
161         char *key_file, *config_file;
162         long unsigned challenge_nr;
163         char *line;
164
165         get_options(argc, argv, &config_file, &key_file);
166         if (args_info.loglevel_arg <= NOTICE)
167                 cmdline_parser_print_version();
168         PARA_INFO_LOG(
169                 "current loglevel: %d\n"
170                 "using config_file: %s\n"
171                 "using key_file: %s\n"
172                 "connecting to %s:%d\n",
173                 args_info.loglevel_arg,
174                 config_file,
175                 key_file,
176                 args_info.hostname_arg,
177                 args_info.server_port_arg
178         );
179         interactive = args_info.inputs_num == 0? 1 : 0;
180         if (interactive) {
181                 PARA_NOTICE_LOG("%s", "no command, entering interactive mode\n");
182                 signal(SIGINT, sigint_handler);
183         } else {
184                 /* not interactive, concat args */
185                 for (i = 0; i < args_info.inputs_num; i++)
186                         append_str(&command, args_info.inputs[i]);
187         }
188 interactive_loop:
189         crypt_function_recv = NULL;
190         crypt_function_send = NULL;
191         if (interactive) {
192                 int i = 0;
193                 char *p;
194
195                 rl_save_prompt();
196                 rl_message("\n");
197                 rl_kill_full_line(0, 0);
198                 rl_free_line_state();
199                 /* read a line via readline */
200                 line = rl_gets();
201                 if (!line)
202                         return 0;
203                 if (!line[0])
204                         goto interactive_loop;
205                 p = line;
206                 while (sscanf(p, "%200s%n", buf, &i) == 1) {
207                         append_str(&command, buf);
208                         p += i;
209                 }
210         }
211         /* get the host info */
212         PARA_NOTICE_LOG("getting host info of %s\n",
213                 args_info.hostname_arg);
214         if (!(he = get_host_info(args_info.hostname_arg)))
215                 exit(EXIT_FAILURE);
216         /* get new socket */
217         if ((sockfd = get_socket()) < 0)
218                 exit(EXIT_FAILURE);
219         /* init their_addr */
220         init_sockaddr(&their_addr, args_info.server_port_arg, he);
221         /* Connect */
222         PARA_NOTICE_LOG("connecting to %s...\n",
223                 args_info.hostname_arg);
224         if (para_connect(sockfd, &their_addr) < 0)
225                 exit(EXIT_FAILURE);
226         /* Receive Welcome message */
227         if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
228                 exit(EXIT_FAILURE);
229         /* send auth command */
230         auth_str = make_message("auth %s%s", args_info.plain_given?  "" : "rc4 ",
231                 args_info.user_arg);
232         PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
233         if (send_buffer(sockfd, auth_str) < 0)
234                 exit(EXIT_FAILURE);
235         /* receive challenge number */
236         if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
237                 exit(EXIT_FAILURE);
238         if (numbytes != 64) {
239                 PARA_EMERG_LOG("did not receive valid challenge (got %i bytes)\n",
240                         numbytes);
241                 buf[numbytes] = '\0';
242                 PARA_ERROR_LOG("received the following instead: %s\n", buf);
243                 goto write_out;
244         }
245         PARA_INFO_LOG("<-- [challenge (%i bytes)]\n", numbytes);
246         /* decrypt challenge number */
247         ret = para_decrypt_challenge(key_file, &challenge_nr, (unsigned char *) buf,
248                 numbytes);
249         if (ret < 0) {
250                 PARA_EMERG_LOG("decrypt error (%d). Bad secret key?\n", ret);
251                 exit(EXIT_FAILURE);
252         }
253         /* send decrypted challenge */
254         PARA_INFO_LOG("--> %lu\n", challenge_nr);
255         if (send_va_buffer(sockfd, "%s%lu", CHALLENGE_RESPONSE_MSG, challenge_nr) < 0)
256                 exit(EXIT_FAILURE);
257         /* Wait for approval */
258         PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
259         if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
260                 exit(EXIT_FAILURE);
261         PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
262                 "info ++++\n", buf);
263         /* Check if server has sent "Proceed" message */
264         if (!strstr(buf, PROCEED_MSG)) {
265                 PARA_EMERG_LOG("%s", "authentication failed\n");
266                 exit(EXIT_FAILURE);
267         }
268         if (numbytes >= PROCEED_MSG_LEN + 32) {
269                 PARA_INFO_LOG("%s", "decrypting session key\n");
270                 if (para_decrypt_buffer(key_file, rc4_buf,
271                                 (unsigned char *)buf + PROCEED_MSG_LEN + 1,
272                                 numbytes - PROCEED_MSG_LEN - 1) < 0) {
273                         PARA_EMERG_LOG("%s", "error receiving rc4 key\n");
274                         exit(EXIT_FAILURE);
275                 }
276                 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf);
277                 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
278                 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
279                         rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]);
280                 crypt_function_recv = rc4_recv;
281                 crypt_function_send = rc4_send;
282         }
283         /* send command */
284         PARA_INFO_LOG("--> %s\n", command);
285         if (send_buffer(sockfd, command) < 0)
286                 exit(EXIT_FAILURE);
287         free(command);
288         command = NULL;
289         if (send_buffer(sockfd, EOC_MSG "\n") < 0)
290                 exit(EXIT_FAILURE);
291         PARA_NOTICE_LOG("%s", "command sent.\n");
292 write_out:
293         received = 0;
294         /* write server output to stdout */
295         while ((numbytes = recv_bin_buffer(sockfd, buf, sizeof(buf))) > 0) {
296                 int ret;
297
298                 if (!received && strstr(buf, AWAITING_DATA_MSG)) {
299                         PARA_NOTICE_LOG("%s", "<-- awaiting data\n");
300                         PARA_NOTICE_LOG("%s", "--> sending stdin\n");
301                         while ((ret = read(STDIN_FILENO, buf,
302                                         sizeof(buf))) > 0)
303                                 send_bin_buffer(sockfd, buf, ret);
304                         PARA_NOTICE_LOG("%s", "closing connection\n");
305                         numbytes = 1;
306                         break;
307                 }
308                 received = 1;
309                 if (write(STDOUT_FILENO, buf, numbytes) != numbytes)
310                         break;
311         }
312         if (!numbytes)
313                 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
314         close(sockfd);
315         if (interactive)
316                 goto interactive_loop;
317         return 0;
318 }