Fix a bug in stdout.c.
[paraslash.git] / client_common.c
1 /*
2  * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file client_common.c Common functions of para_client and para_audiod. */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "client.cmdline.h"
17 #include "crypt.h"
18 #include "rc4.h"
19 #include "net.h"
20 #include "fd.h"
21 #include "string.h"
22 #include "client.cmdline.h"
23 #include "client.h"
24
25 /*
26  * Rc4-encrypt data before sending.
27  *
28  * \param len The number of bytes to encrypt.
29  * \param indata Pointer to the input data of length \a len to be encrypted.
30  * \param outdata Result-pointer that holds the encrypted data.
31  * \param private_data Contains the rc4 key.
32  */
33 static void rc4_send(unsigned long len, const unsigned char *indata,
34                 unsigned char *outdata, void *private_data)
35 {
36         struct client_task *ct = private_data;
37         RC4(&ct->rc4_send_key, len, indata, outdata);
38 }
39
40 /*
41  * Rc4-decrypt received data.
42  *
43  * Parameters are identical to those of rc4_send.
44  */
45 static void rc4_recv(unsigned long len, const unsigned char *indata,
46                 unsigned char *outdata, void *private_data)
47 {
48         struct client_task *ct = private_data;
49         RC4(&ct->rc4_recv_key, len, indata, outdata);
50 }
51
52 /**
53  * Close the connection to para_server and free all resources.
54  *
55  * \param ct Pointer to the client data.
56  *
57  * \sa client_open.
58  */
59 void client_close(struct client_task *ct)
60 {
61         if (!ct)
62                 return;
63         if (ct->fd >= 0) {
64                 disable_crypt(ct->fd);
65                 close(ct->fd);
66         }
67         free(ct->user);
68         free(ct->config_file);
69         free(ct->key_file);
70         client_cmdline_parser_free(&ct->conf);
71         free(ct);
72 }
73
74 /**
75  * The preselect hook for server commands.
76  *
77  * \param s Pointer to the scheduler.
78  * \param t Pointer to the task struct for this command.
79  *
80  * The task pointer must contain a pointer to the initialized client data
81  * structure as it is returned by client_open().
82  *
83  * This function checks the state of the connection and adds the file descriptor
84  * of the connection to the read or write fd set of \a s accordingly.
85  *
86  * \sa register_task() client_open(), struct sched, struct task.
87  */
88 static void client_pre_select(struct sched *s, struct task *t)
89 {
90         struct client_task *ct = container_of(t, struct client_task, task);
91
92         ct->check_r = 0;
93         ct->check_w = 0;
94         if (ct->fd < 0)
95                 return;
96         switch (ct->status) {
97         case CL_CONNECTED:
98         case CL_SENT_AUTH:
99         case CL_SENT_CH_RESPONSE:
100         case CL_SENT_COMMAND:
101                 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
102                 ct->check_r = 1;
103                 return;
104
105         case CL_RECEIVED_WELCOME:
106         case CL_RECEIVED_CHALLENGE:
107         case CL_RECEIVED_PROCEED:
108                 para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
109                 ct->check_w = 1;
110                 return;
111
112         case CL_RECEIVING:
113                 if (ct->loaded < CLIENT_BUFSIZE - 1) {
114                         para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
115                         ct->check_r = 1;
116                 }
117                 return;
118         case CL_SENDING:
119                 if (*ct->in_loaded) {
120                         PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
121                         para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
122                         ct->check_w = 1;
123                 } else {
124                         if (*ct->in_error) {
125                                 t->error = *ct->in_error;
126                                 s->timeout.tv_sec = 0;
127                                 s->timeout.tv_usec = 1;
128                         }
129                 }
130                 return;
131         }
132 }
133
134 static ssize_t client_recv_buffer(struct client_task *ct)
135 {
136         ssize_t ret = recv_buffer(ct->fd, ct->buf + ct->loaded,
137                 CLIENT_BUFSIZE - ct->loaded);
138         if (!ret)
139                 return -E_SERVER_EOF;
140         if (ret > 0)
141                 ct->loaded += ret;
142         return ret;
143
144 }
145
146 /**
147  * The post select hook for client commands.
148  *
149  * \param s Pointer to the scheduler.
150  * \param t Pointer to the task struct for this command.
151  *
152  * Depending on the current state of the connection and the status of the read
153  * and write fd sets of \a s, this function performs the necessary steps to
154  * authenticate the connection, to send the command given by \a t->private_data
155  * and to receive para_server's output, if any.
156  *
157  * \sa struct sched, struct task.
158  */
159 static void client_post_select(struct sched *s, struct task *t)
160 {
161         struct client_task *ct = container_of(t, struct client_task, task);
162
163         t->error = 0;
164         if (ct->fd < 0)
165                 return;
166         if (!ct->check_r && !ct->check_w)
167                 return;
168         if (ct->check_r && !FD_ISSET(ct->fd, &s->rfds))
169                 return;
170         if (ct->check_w && !FD_ISSET(ct->fd, &s->wfds))
171                 return;
172         switch (ct->status) {
173         case CL_CONNECTED: /* receive welcome message */
174                 t->error = client_recv_buffer(ct);
175                 if (t->error > 0)
176                         ct->status = CL_RECEIVED_WELCOME;
177                 return;
178         case CL_RECEIVED_WELCOME: /* send auth command */
179                 sprintf(ct->buf, "auth %s%s", ct->conf.plain_given?
180                         "" : "rc4 ", ct->user);
181                 PARA_INFO_LOG("--> %s\n", ct->buf);
182                 t->error = send_buffer(ct->fd, ct->buf);
183                 if (t->error >= 0)
184                         ct->status = CL_SENT_AUTH;
185                 return;
186         case CL_SENT_AUTH: /* receive challenge number */
187                 ct->loaded = 0;
188                 t->error = client_recv_buffer(ct);
189                 if (t->error < 0)
190                         return;
191                 if (t->error != 64) {
192                         t->error = -E_INVALID_CHALLENGE;
193                         PARA_ERROR_LOG("received the following: %s\n", ct->buf);
194                         return;
195                 }
196                 PARA_INFO_LOG("<-- [challenge]\n");
197                 /* decrypt challenge number */
198                 t->error = para_decrypt_challenge(ct->key_file, &ct->challenge_nr,
199                         (unsigned char *) ct->buf, 64);
200                 if (t->error > 0)
201                         ct->status = CL_RECEIVED_CHALLENGE;
202                 return;
203         case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
204                 PARA_INFO_LOG("--> %lu\n", ct->challenge_nr);
205                 t->error = send_va_buffer(ct->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
206                         ct->challenge_nr);
207                 if (t->error > 0)
208                         ct->status = CL_SENT_CH_RESPONSE;
209                 return;
210         case CL_SENT_CH_RESPONSE: /* read server response */
211                 {
212                 size_t bytes_received;
213                 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
214                 ct->loaded = 0;
215                 t->error = client_recv_buffer(ct);
216                 if (t->error < 0)
217                         return;
218                 bytes_received = t->error;
219                 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
220                         "info ++++\n", ct->buf);
221                 /* check if server has sent "Proceed" message */
222                 t->error = -E_CLIENT_AUTH;
223                 if (!strstr(ct->buf, PROCEED_MSG))
224                         return;
225                 t->error = 0;
226                 ct->status = CL_RECEIVED_PROCEED;
227                 if (bytes_received < PROCEED_MSG_LEN + 32)
228                         return;
229                 PARA_INFO_LOG("decrypting session key\n");
230                 t->error = para_decrypt_buffer(ct->key_file, rc4_buf,
231                         (unsigned char *)ct->buf + PROCEED_MSG_LEN + 1,
232                         bytes_received - PROCEED_MSG_LEN - 1);
233                 if (t->error < 0)
234                         return;
235                 RC4_set_key(&ct->rc4_send_key, RC4_KEY_LEN, rc4_buf);
236                 RC4_set_key(&ct->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
237                 enable_crypt(ct->fd, rc4_recv, rc4_send, ct);
238                 }
239         case CL_RECEIVED_PROCEED: /* concat args and send command */
240                 {
241                 int i;
242                 char *command = NULL;
243                 for (i = 0; i < ct->conf.inputs_num; i++) {
244                         char *tmp = command;
245                         command = make_message("%s\n%s", command?
246                                 command : "", ct->conf.inputs[i]);
247                         free(tmp);
248                 }
249                 command = para_strcat(command, EOC_MSG "\n");
250                 PARA_DEBUG_LOG("--> %s\n", command);
251                 t->error = send_buffer(ct->fd, command);
252                 free(command);
253                 if (t->error > 0)
254                         ct->status = CL_SENT_COMMAND;
255                 return;
256                 }
257         case CL_SENT_COMMAND:
258                 ct->loaded = 0;
259                 t->error = client_recv_buffer(ct);
260                 if (t->error < 0)
261                         return;
262                 if (strstr(ct->buf, AWAITING_DATA_MSG))
263                         ct->status = CL_SENDING;
264                 else
265                         ct->status = CL_RECEIVING;
266                 return;
267         case CL_SENDING: /* FIXME: might block */
268                 PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
269                 t->error = send_bin_buffer(ct->fd, ct->inbuf, *ct->in_loaded);
270                 if (t->error < 0)
271                         return;
272                 *ct->in_loaded = 0;
273                 return;
274         case CL_RECEIVING:
275                 t->error = client_recv_buffer(ct);
276                 return;
277         }
278 }
279
280 /* connect to para_server and register the client task */
281 static int client_connect(struct client_task *ct)
282 {
283         int ret;
284
285         ct->fd = -1;
286         ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
287                 ct->conf.server_port_arg);
288         if (ret < 0)
289                 return ret;
290         ct->fd = ret;
291         ct->status = CL_CONNECTED;
292         ret = mark_fd_nonblocking(ct->fd);
293         if (ret < 0)
294                 goto err_out;
295         ct->task.pre_select = client_pre_select;
296         ct->task.post_select = client_post_select;
297         sprintf(ct->task.status, "client");
298         register_task(&ct->task);
299         return 1;
300 err_out:
301         close(ct->fd);
302         ct->fd = -1;
303         return ret;
304 }
305
306 /**
307  * Open connection to para_server.
308  *
309  * \param argc Usual argument count.
310  * \param argv Usual argument vector.
311  * \param ct_ptr Points to dynamically allocated and initialized client task
312  * struct upon successful return.
313  *
314  * Check the command line options given by \a argc and argv, set default values
315  * for user name and rsa key file, read further option from the config file.
316  * Finally, establish a connection to para_server.
317  *
318  * \return Standard.
319  */
320 int client_open(int argc, char *argv[], struct client_task **ct_ptr)
321 {
322         char *home = para_homedir();
323         struct stat statbuf;
324         int ret;
325         struct client_task *ct = para_calloc(sizeof(struct client_task));
326
327         *ct_ptr = ct;
328         ct->fd = -1;
329         ret = client_cmdline_parser(argc, argv, &ct->conf);
330         HANDLE_VERSION_FLAG("client", ct->conf);
331         ret = -E_CLIENT_SYNTAX;
332         if (!ct->conf.inputs_num)
333                 goto out;
334         ct->user = ct->conf.user_given?
335                 para_strdup(ct->conf.user_arg) : para_logname();
336
337         ct->key_file = ct->conf.key_file_given?
338                 para_strdup(ct->conf.key_file_arg) :
339                 make_message("%s/.paraslash/key.%s", home, ct->user);
340
341         ct->config_file = ct->conf.config_file_given?
342                 para_strdup(ct->conf.config_file_arg) :
343                 make_message("%s/.paraslash/client.conf", home);
344         ret = stat(ct->config_file, &statbuf);
345         if (ret && ct->conf.config_file_given) {
346                 ret = -E_NO_CONFIG;
347                 goto out;
348         }
349         if (!ret) {
350                 struct client_cmdline_parser_params params = {
351                         .override = 0,
352                         .initialize = 0,
353                         .check_required = 0,
354                         .check_ambiguity = 0
355                 };
356                 client_cmdline_parser_config_file(ct->config_file,
357                         &ct->conf, &params);
358         }
359         ret = 1;
360         PARA_INFO_LOG("loglevel: %d\n", ct->conf.loglevel_arg);
361         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
362         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
363         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
364                 ct->conf.server_port_arg);
365         ret = client_connect(ct);
366 out:
367         free(home);
368         if (ret < 0) {
369                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
370                 client_close(ct);
371                 *ct_ptr = NULL;
372         }
373         return ret;
374 }
375