Fix memory leaks in xxx_recv_init().
[paraslash.git] / client_common.c
1 /*
2  * Copyright (C) 1997-2009 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->buf);
68         free(ct->user);
69         free(ct->config_file);
70         free(ct->key_file);
71         client_cmdline_parser_free(&ct->conf);
72         free(ct);
73 }
74
75 /**
76  * The preselect hook for server commands.
77  *
78  * \param s Pointer to the scheduler.
79  * \param t Pointer to the task struct for this command.
80  *
81  * The task pointer must contain a pointer to the initialized client data
82  * structure as it is returned by client_open().
83  *
84  * This function checks the state of the connection and adds the file descriptor
85  * of the connection to the read or write fd set of \a s accordingly.
86  *
87  * \sa register_task() client_open(), struct sched, struct task.
88  */
89 static void client_pre_select(struct sched *s, struct task *t)
90 {
91         struct client_task *ct = container_of(t, struct client_task, task);
92
93         ct->check_r = 0;
94         ct->check_w = 0;
95         if (ct->fd < 0)
96                 return;
97         switch (ct->status) {
98         case CL_CONNECTED:
99         case CL_SENT_AUTH:
100         case CL_SENT_CH_RESPONSE:
101         case CL_SENT_COMMAND:
102                 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
103                 ct->check_r = 1;
104                 return;
105
106         case CL_RECEIVED_WELCOME:
107         case CL_RECEIVED_CHALLENGE:
108         case CL_RECEIVED_PROCEED:
109                 para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
110                 ct->check_w = 1;
111                 return;
112
113         case CL_RECEIVING:
114                 if (ct->loaded < CLIENT_BUFSIZE - 1) {
115                         para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
116                         ct->check_r = 1;
117                 }
118                 return;
119         case CL_SENDING:
120                 if (!ct->in_loaded) /* stdin task not yet started */
121                         return;
122                 if (*ct->in_loaded) {
123                         PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
124                         para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
125                         ct->check_w = 1;
126                 } else {
127                         if (*ct->in_error) {
128                                 t->error = *ct->in_error;
129                                 s->timeout.tv_sec = 0;
130                                 s->timeout.tv_usec = 1;
131                         }
132                 }
133                 return;
134         }
135 }
136
137 static ssize_t client_recv_buffer(struct client_task *ct)
138 {
139         ssize_t ret = recv_buffer(ct->fd, ct->buf + ct->loaded,
140                 CLIENT_BUFSIZE - ct->loaded);
141         if (!ret)
142                 return -E_SERVER_EOF;
143         if (ret > 0)
144                 ct->loaded += ret;
145         return ret;
146
147 }
148
149 /**
150  * The post select hook for client commands.
151  *
152  * \param s Pointer to the scheduler.
153  * \param t Pointer to the task struct for this command.
154  *
155  * Depending on the current state of the connection and the status of the read
156  * and write fd sets of \a s, this function performs the necessary steps to
157  * authenticate the connection, to send the command given by \a t->private_data
158  * and to receive para_server's output, if any.
159  *
160  * \sa struct sched, struct task.
161  */
162 static void client_post_select(struct sched *s, struct task *t)
163 {
164         struct client_task *ct = container_of(t, struct client_task, task);
165
166         t->error = 0;
167         if (ct->fd < 0)
168                 return;
169         if (!ct->check_r && !ct->check_w)
170                 return;
171         if (ct->check_r && !FD_ISSET(ct->fd, &s->rfds))
172                 return;
173         if (ct->check_w && !FD_ISSET(ct->fd, &s->wfds))
174                 return;
175         switch (ct->status) {
176         case CL_CONNECTED: /* receive welcome message */
177                 t->error = client_recv_buffer(ct);
178                 if (t->error > 0)
179                         ct->status = CL_RECEIVED_WELCOME;
180                 return;
181         case CL_RECEIVED_WELCOME: /* send auth command */
182                 sprintf(ct->buf, "auth %s%s", ct->conf.plain_given?
183                         "" : "rc4 ", ct->user);
184                 PARA_INFO_LOG("--> %s\n", ct->buf);
185                 t->error = send_buffer(ct->fd, ct->buf);
186                 if (t->error >= 0)
187                         ct->status = CL_SENT_AUTH;
188                 return;
189         case CL_SENT_AUTH: /* receive challenge number */
190                 ct->loaded = 0;
191                 t->error = client_recv_buffer(ct);
192                 if (t->error < 0)
193                         return;
194                 if (t->error < 64) {
195                         t->error = -E_INVALID_CHALLENGE;
196                         PARA_ERROR_LOG("received the following: %s\n", ct->buf);
197                         return;
198                 }
199                 PARA_INFO_LOG("<-- [challenge] (%d bytes)\n", t->error);
200                 /* decrypt challenge number */
201                 t->error = para_decrypt_challenge(ct->key_file, &ct->challenge_nr,
202                         (unsigned char *) ct->buf, t->error);
203                 if (t->error > 0)
204                         ct->status = CL_RECEIVED_CHALLENGE;
205                 return;
206         case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
207                 PARA_INFO_LOG("--> %lu\n", ct->challenge_nr);
208                 t->error = send_va_buffer(ct->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
209                         ct->challenge_nr);
210                 if (t->error > 0)
211                         ct->status = CL_SENT_CH_RESPONSE;
212                 return;
213         case CL_SENT_CH_RESPONSE: /* read server response */
214                 {
215                 size_t bytes_received;
216                 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
217                 ct->loaded = 0;
218                 t->error = client_recv_buffer(ct);
219                 if (t->error < 0)
220                         return;
221                 bytes_received = t->error;
222                 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
223                         "info ++++\n", ct->buf);
224                 /* check if server has sent "Proceed" message */
225                 t->error = -E_CLIENT_AUTH;
226                 if (!strstr(ct->buf, PROCEED_MSG))
227                         return;
228                 t->error = 0;
229                 ct->status = CL_RECEIVED_PROCEED;
230                 if (bytes_received < PROCEED_MSG_LEN + 32)
231                         return;
232                 PARA_INFO_LOG("decrypting session key\n");
233                 t->error = para_decrypt_buffer(ct->key_file, rc4_buf,
234                         (unsigned char *)ct->buf + PROCEED_MSG_LEN + 1,
235                         bytes_received - PROCEED_MSG_LEN - 1);
236                 if (t->error < 0)
237                         return;
238                 RC4_set_key(&ct->rc4_send_key, RC4_KEY_LEN, rc4_buf);
239                 RC4_set_key(&ct->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
240                 enable_crypt(ct->fd, rc4_recv, rc4_send, ct);
241                 }
242         case CL_RECEIVED_PROCEED: /* concat args and send command */
243                 {
244                 int i;
245                 char *command = NULL;
246                 for (i = 0; i < ct->conf.inputs_num; i++) {
247                         char *tmp = command;
248                         command = make_message("%s\n%s", command?
249                                 command : "", ct->conf.inputs[i]);
250                         free(tmp);
251                 }
252                 command = para_strcat(command, EOC_MSG "\n");
253                 PARA_DEBUG_LOG("--> %s\n", command);
254                 t->error = send_buffer(ct->fd, command);
255                 free(command);
256                 if (t->error > 0)
257                         ct->status = CL_SENT_COMMAND;
258                 return;
259                 }
260         case CL_SENT_COMMAND:
261                 ct->loaded = 0;
262                 t->error = client_recv_buffer(ct);
263                 if (t->error < 0)
264                         return;
265                 if (strstr(ct->buf, AWAITING_DATA_MSG))
266                         ct->status = CL_SENDING;
267                 else
268                         ct->status = CL_RECEIVING;
269                 return;
270         case CL_SENDING: /* FIXME: might block */
271                 PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
272                 t->error = send_bin_buffer(ct->fd, ct->inbuf, *ct->in_loaded);
273                 if (t->error < 0)
274                         return;
275                 *ct->in_loaded = 0;
276                 return;
277         case CL_RECEIVING:
278                 t->error = client_recv_buffer(ct);
279                 return;
280         }
281 }
282
283 /* connect to para_server and register the client task */
284 static int client_connect(struct client_task *ct)
285 {
286         int ret;
287
288         ct->fd = -1;
289         ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
290                 ct->conf.server_port_arg);
291         if (ret < 0)
292                 return ret;
293         ct->fd = ret;
294         ct->status = CL_CONNECTED;
295         ret = mark_fd_nonblocking(ct->fd);
296         if (ret < 0)
297                 goto err_out;
298         ct->task.pre_select = client_pre_select;
299         ct->task.post_select = client_post_select;
300         sprintf(ct->task.status, "client");
301         register_task(&ct->task);
302         return 1;
303 err_out:
304         close(ct->fd);
305         ct->fd = -1;
306         return ret;
307 }
308
309 /**
310  * Open connection to para_server.
311  *
312  * \param argc Usual argument count.
313  * \param argv Usual argument vector.
314  * \param ct_ptr Points to dynamically allocated and initialized client task
315  * struct upon successful return.
316  * \param loglevel If not \p NULL, the number of the loglevel is stored here.
317  *
318  * Check the command line options given by \a argc and argv, set default values
319  * for user name and rsa key file, read further option from the config file.
320  * Finally, establish a connection to para_server.
321  *
322  * \return Standard.
323  */
324 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
325                 int *loglevel)
326 {
327         char *home = para_homedir();
328         int ret;
329         struct client_task *ct = para_calloc(sizeof(struct client_task));
330
331         ct->buf = para_malloc(CLIENT_BUFSIZE);
332         *ct_ptr = ct;
333         ct->fd = -1;
334         ret = -E_CLIENT_SYNTAX;
335         if (client_cmdline_parser(argc, argv, &ct->conf))
336                 goto out;
337         HANDLE_VERSION_FLAG("client", ct->conf);
338         ret = -E_CLIENT_SYNTAX;
339         if (!ct->conf.inputs_num)
340                 goto out;
341         ct->user = ct->conf.user_given?
342                 para_strdup(ct->conf.user_arg) : para_logname();
343
344         ct->key_file = ct->conf.key_file_given?
345                 para_strdup(ct->conf.key_file_arg) :
346                 make_message("%s/.paraslash/key.%s", home, ct->user);
347
348         ct->config_file = ct->conf.config_file_given?
349                 para_strdup(ct->conf.config_file_arg) :
350                 make_message("%s/.paraslash/client.conf", home);
351         ret = file_exists(ct->config_file);
352         if (!ret && ct->conf.config_file_given) {
353                 ret = -E_NO_CONFIG;
354                 goto out;
355         }
356         if (ret) {
357                 struct client_cmdline_parser_params params = {
358                         .override = 0,
359                         .initialize = 0,
360                         .check_required = 0,
361                         .check_ambiguity = 0,
362                         .print_errors = 0
363                 };
364                 ret = -E_BAD_CONFIG;
365                 if (client_cmdline_parser_config_file(ct->config_file,
366                         &ct->conf, &params))
367                         goto out;
368         }
369         if (loglevel)
370                 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
371         PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
372         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
373         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
374         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
375                 ct->conf.server_port_arg);
376         ret = client_connect(ct);
377 out:
378         free(home);
379         if (ret < 0) {
380                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
381                 client_close(ct);
382                 *ct_ptr = NULL;
383         }
384         return ret;
385 }