fix cut-and paste typo.
[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 "list.h"
23 #include "sched.h"
24 #include "config.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 "fd.h"
31 #include "string.h"
32 #include "stdin.h"
33 #include "stdout.h"
34 #include "error.h"
35
36 enum {
37 CL_CONNECTED,
38 CL_RECEIVED_WELCOME,
39 CL_SENT_AUTH,
40 CL_RECEIVED_CHALLENGE,
41 CL_SENT_CH_RESPONSE,
42 CL_RECEIVED_PROCEED,
43 CL_SENT_COMMAND,
44 CL_SENDING_STDIN,
45 CL_RECEIVING_SERVER_OUTPUT
46 };
47
48 #define CLIENT_BUFSIZE 8192
49
50 struct private_client_data {
51 int status;
52 int fd;
53 struct client_args_info conf;
54 char *config_file;
55 char *key_file;
56 char *user;
57 RC4_KEY rc4_recv_key;
58 RC4_KEY rc4_send_key;
59 struct task task;
60 int eof;
61 char buf[CLIENT_BUFSIZE];
62 size_t loaded;
63 int check_r;
64 int check_w;
65 long unsigned challenge_nr;
66 /* only used if stdin gets sent to para_server */
67 char *inbuf;
68 size_t *in_loaded;
69 int *in_eof;
70 };
71
72 INIT_CLIENT_ERRLISTS;
73
74 static struct private_client_data *pcd;
75 static struct stdin_task sit;
76 static struct stdout_task sot;
77
78
79 static void rc4_send(unsigned long len, const unsigned char *indata,
80 unsigned char *outdata)
81 {
82 RC4(&pcd->rc4_send_key, len, indata, outdata);
83 }
84
85 static void rc4_recv(unsigned long len, const unsigned char *indata,
86 unsigned char *outdata)
87 {
88 RC4(&pcd->rc4_recv_key, len, indata, outdata);
89 }
90
91
92 /*
93 * client log function
94 */
95 void para_log(int ll, const char* fmt,...)
96 {
97 va_list argp;
98
99 /* ignore log message if loglevel is not high enough */
100 if (pcd && ll < pcd->conf.loglevel_arg)
101 return;
102 va_start(argp, fmt);
103 vfprintf(stderr, fmt, argp);
104 va_end(argp);
105 }
106
107 static void client_close(struct private_client_data *pcd)
108 {
109 if (pcd)
110 return;
111 if (pcd->fd >= 0)
112 close(pcd->fd);
113 free(pcd->user);
114 free(pcd->config_file);
115 free(pcd->key_file);
116 free(pcd);
117 }
118
119 static int client_parse_config(int argc, char *argv[],
120 struct private_client_data **pcd_ptr)
121 {
122 char *home = para_homedir();
123 struct stat statbuf;
124 int ret;
125 struct private_client_data *p =
126 para_calloc(sizeof(struct private_client_data));
127
128 p->fd = -1;
129 cmdline_parser(argc, argv, &p->conf);
130 ret = - E_CLIENT_SYNTAX;
131 if (!p->conf.inputs_num)
132 goto out;
133 p->user = p->conf.user_given?
134 para_strdup(p->conf.user_arg) : para_logname();
135
136 p->key_file = p->conf.key_file_given?
137 para_strdup(p->conf.key_file_arg) :
138 make_message("%s/.paraslash/key.%s", home, p->user);
139
140 p->config_file = p->conf.config_file_given?
141 para_strdup(p->conf.config_file_arg) :
142 make_message("%s/.paraslash/client.conf", home);
143 ret = stat(p->config_file, &statbuf);
144 if (ret && p->conf.config_file_given) {
145 ret = -E_NO_CONFIG;
146 goto out;
147 }
148 if (!ret)
149 cmdline_parser_configfile(p->config_file, &p->conf, 0, 0, 0);
150 ret = 1;
151 *pcd_ptr = p;
152 PARA_INFO_LOG(
153 "current loglevel: %d\n"
154 "using config_file: %s\n"
155 "using key_file: %s\n"
156 "connecting to %s:%d\n" ,
157 p->conf.loglevel_arg,
158 p->config_file,
159 p->key_file,
160 p->conf.hostname_arg, p->conf.server_port_arg
161 );
162 out:
163 free(home);
164 if (ret < 0)
165 client_close(p);
166 return ret;
167 }
168
169 static void client_pre_select(struct sched *s, struct task *t)
170 {
171 struct private_client_data *p = t->private_data;
172
173 PARA_INFO_LOG("status %d\n", p->status);
174 t->ret = 1;
175 pcd->check_r = 0;
176 pcd->check_w = 0;
177 if (p->fd < 0)
178 return;
179 switch (pcd->status) {
180 case CL_CONNECTED:
181 case CL_SENT_AUTH:
182 case CL_SENT_CH_RESPONSE:
183 case CL_SENT_COMMAND:
184 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
185 pcd->check_r = 1;
186 return;
187
188 case CL_RECEIVED_WELCOME:
189 case CL_RECEIVED_CHALLENGE:
190 case CL_RECEIVED_PROCEED:
191 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
192 pcd->check_w = 1;
193 return;
194
195 case CL_RECEIVING_SERVER_OUTPUT:
196 if (pcd->loaded < CLIENT_BUFSIZE - 1) {
197 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
198 p->check_r = 1;
199 }
200 return;
201 case CL_SENDING_STDIN:
202 if (*p->in_loaded) {
203 PARA_INFO_LOG("loaded: %d\n", *p->in_loaded);
204 para_fd_set(p->fd, &s->wfds, &s->max_fileno);
205 p->check_w = 1;
206 } else {
207 if (*p->in_eof) {
208 t->ret = -E_INPUT_EOF;
209 s->timeout.tv_sec = 0;
210 s->timeout.tv_usec = 1;
211 }
212 }
213 return;
214 }
215 }
216
217 static ssize_t client_recv_buffer(struct private_client_data *p)
218 {
219 ssize_t ret = recv_buffer(p->fd, p->buf + p->loaded,
220 CLIENT_BUFSIZE - p->loaded);
221 if (!ret)
222 return -E_SERVER_EOF;
223 if (ret > 0)
224 p->loaded += ret;
225 return ret;
226
227 }
228
229 static void client_post_select(struct sched *s, struct task *t)
230 {
231 struct private_client_data *p = t->private_data;
232
233 PARA_INFO_LOG("status %d\n", p->status);
234 t->ret = 1;
235 if (p->fd < 0)
236 return;
237 if (!p->check_r && !p->check_w)
238 return;
239 if (p->check_r && !FD_ISSET(p->fd, &s->rfds))
240 return;
241 if (p->check_w && !FD_ISSET(p->fd, &s->wfds))
242 return;
243 switch (p->status) {
244 case CL_CONNECTED: /* receive welcome message */
245 t->ret = client_recv_buffer(p);
246 if (t->ret > 0)
247 p->status = CL_RECEIVED_WELCOME;
248 return;
249 case CL_RECEIVED_WELCOME: /* send auth command */
250 sprintf(p->buf, "auth %s%s", p->conf.plain_given?
251 "" : "rc4 ", p->user);
252 PARA_INFO_LOG("--> %s\n", p->buf);
253 t->ret = send_buffer(p->fd, p->buf);
254 if (t->ret >= 0)
255 p->status = CL_SENT_AUTH;
256 return;
257 case CL_SENT_AUTH: /* receive challenge number */
258 p->loaded = 0;
259 t->ret = client_recv_buffer(p);
260 if (t->ret < 0)
261 return;
262 if (t->ret != 64) {
263 t->ret = -E_INVALID_CHALLENGE;
264 PARA_ERROR_LOG("received the following: %s\n", p->buf);
265 return;
266 }
267 PARA_INFO_LOG("%s", "<-- [challenge]\n");
268 /* decrypt challenge number */
269 t->ret = para_decrypt_challenge(p->key_file, &p->challenge_nr,
270 (unsigned char *) p->buf, 64);
271 if (t->ret > 0)
272 p->status = CL_RECEIVED_CHALLENGE;
273 return;
274 case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
275 PARA_INFO_LOG("--> %lu\n", p->challenge_nr);
276 t->ret = send_va_buffer(p->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
277 p->challenge_nr);
278 if (t->ret > 0)
279 p->status = CL_SENT_CH_RESPONSE;
280 return;
281 case CL_SENT_CH_RESPONSE: /* read server response */
282 {
283 size_t bytes_received;
284 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
285 p->loaded = 0;
286 t->ret = client_recv_buffer(p);
287 if (t->ret < 0)
288 return;
289 bytes_received = t->ret;
290 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
291 "info ++++\n", p->buf);
292 /* check if server has sent "Proceed" message */
293 t->ret = -E_CLIENT_AUTH;
294 if (!strstr(p->buf, PROCEED_MSG))
295 return;
296 t->ret = 1;
297 p->status = CL_RECEIVED_PROCEED;
298 if (bytes_received < PROCEED_MSG_LEN + 32)
299 return;
300 PARA_INFO_LOG("%s", "decrypting session key\n");
301 t->ret = para_decrypt_buffer(p->key_file, rc4_buf,
302 (unsigned char *)p->buf + PROCEED_MSG_LEN + 1,
303 bytes_received - PROCEED_MSG_LEN - 1);
304 if (t->ret < 0)
305 return;
306 RC4_set_key(&p->rc4_send_key, RC4_KEY_LEN, rc4_buf);
307 RC4_set_key(&p->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
308 enable_crypt(p->fd, rc4_recv, rc4_send);
309 }
310 case CL_RECEIVED_PROCEED: /* concat args and send command */
311 {
312 int i;
313 char *command = NULL;
314 for (i = 0; i < p->conf.inputs_num; i++) {
315 char *tmp = command;
316 command = make_message("%s\n%s", command?
317 command : "", p->conf.inputs[i]);
318 free(tmp);
319 }
320 command = para_strcat(command, EOC_MSG "\n");
321 PARA_INFO_LOG("--> %s\n", command);
322 t->ret = send_buffer(p->fd, command);
323 free(command);
324 if (t->ret > 0)
325 p->status = CL_SENT_COMMAND;
326 return;
327 }
328 case CL_SENT_COMMAND:
329 p->loaded = 0;
330 t->ret = client_recv_buffer(p);
331 if (t->ret < 0)
332 return;
333 t->ret = -E_HANDSHAKE_COMPLETE;
334 if (strstr(p->buf, AWAITING_DATA_MSG))
335 p->status = CL_SENDING_STDIN;
336 else
337 p->status = CL_RECEIVING_SERVER_OUTPUT;
338 return;
339 case CL_SENDING_STDIN: /* FIXME: might block */
340 PARA_INFO_LOG("loaded: %d\n", *p->in_loaded);
341 t->ret = send_bin_buffer(p->fd, p->inbuf, *p->in_loaded);
342 if (t->ret <= 0) {
343 if (!t->ret)
344 t->ret = 1;
345 return;
346 }
347 *p->in_loaded = 0; /* FIXME: short writes */
348 return;
349 case CL_RECEIVING_SERVER_OUTPUT:
350 t->ret = client_recv_buffer(p);
351 return;
352 }
353
354 }
355 static int client_open(struct private_client_data *pcd)
356 {
357 int ret;
358 struct hostent *he;
359 struct sockaddr_in their_addr;
360
361 /* get the host info */
362 PARA_NOTICE_LOG("getting host info of %s\n",
363 pcd->conf.hostname_arg);
364 ret = get_host_info(pcd->conf.hostname_arg, &he);
365 if (ret < 0)
366 goto out;
367 /* get new socket */
368 ret = get_socket();
369 if (ret < 0)
370 goto out;
371 pcd->fd = ret;
372 /* init their_addr */
373 init_sockaddr(&their_addr, pcd->conf.server_port_arg, he);
374 /* connect */
375 PARA_NOTICE_LOG("connecting to %s\n", pcd->conf.hostname_arg);
376 ret = para_connect(pcd->fd, &their_addr);
377 if (ret < 0)
378 goto out;
379 pcd->status = CL_CONNECTED;
380 pcd->task.pre_select = client_pre_select;
381 pcd->task.post_select = client_post_select;
382 pcd->task.private_data = pcd;
383 sprintf(pcd->task.status, "client");
384 register_task(&pcd->task);
385 ret = 1;
386 out:
387 return ret;
388 }
389
390 static void client_event_handler(struct task *t)
391 {
392 struct private_client_data *p = t->private_data;
393
394 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
395 if (t->ret != -E_HANDSHAKE_COMPLETE) {
396 unregister_task(t);
397 p->eof = 1;
398 return;
399 }
400 if (p->status == CL_SENDING_STDIN) {
401 stdin_set_defaults(&sit);
402 sit.buf = para_malloc(sit.bufsize),
403 register_task(&sit.task);
404 p->inbuf = sit.buf;
405 p->in_loaded = &sit.loaded;
406 p->in_eof = &sit.eof;
407 return;
408 }
409 stdout_set_defaults(&sot);
410 sot.buf = p->buf;
411 sot.loaded = &p->loaded;
412 sot.input_eof = &p->eof;
413 register_task(&sot.task);
414 }
415
416 /*
417 * MAIN
418 */
419 int main(int argc, char *argv[])
420 {
421
422 int ret;
423 struct sched s;
424
425 init_sched();
426 s.default_timeout.tv_sec = 1;
427 s.default_timeout.tv_usec = 0;
428 ret = client_parse_config(argc, argv, &pcd);
429 if (ret < 0)
430 goto out;
431 pcd->task.event_handler = client_event_handler;
432 ret = client_open(pcd);
433 if (ret < 0)
434 goto out;
435 ret = sched(&s);
436 client_close(pcd);
437 out:
438 if (ret < 0)
439 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
440 return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
441 }