1536bb2e965ec94007fc81def6edbfac1d16b444
[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 <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <openssl/rc4.h>
13
14 #include "para.h"
15 #include "error.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "client.cmdline.h"
19 #include "crypt.h"
20 #include "rc4.h"
21 #include "net.h"
22 #include "fd.h"
23 #include "string.h"
24 #include "client.cmdline.h"
25 #include "client.h"
26 #include "hash.h"
27 #include "buffer_tree.h"
28
29 /** The size of the receiving buffer. */
30 #define CLIENT_BUFSIZE 4000
31
32 /**
33  * Close the connection to para_server and free all resources.
34  *
35  * \param ct Pointer to the client data.
36  *
37  * \sa client_open.
38  */
39 void client_close(struct client_task *ct)
40 {
41         if (!ct)
42                 return;
43         if (ct->rc4c.fd >= 0)
44                 close(ct->rc4c.fd);
45         free(ct->user);
46         free(ct->config_file);
47         free(ct->key_file);
48         client_cmdline_parser_free(&ct->conf);
49         free(ct);
50 }
51
52 /**
53  * The preselect hook for server commands.
54  *
55  * \param s Pointer to the scheduler.
56  * \param t Pointer to the task struct for this command.
57  *
58  * The task pointer must contain a pointer to the initialized client data
59  * structure as it is returned by client_open().
60  *
61  * This function checks the state of the connection and adds the file descriptor
62  * of the connection to the read or write fd set of \a s accordingly.
63  *
64  * \sa register_task() client_open(), struct sched, struct task.
65  */
66 static void client_pre_select(struct sched *s, struct task *t)
67 {
68         int ret;
69         struct client_task *ct = container_of(t, struct client_task, task);
70         struct btr_node *btrn = ct->btrn;
71
72         if (ct->rc4c.fd < 0)
73                 return;
74         switch (ct->status) {
75         case CL_CONNECTED:
76         case CL_SENT_AUTH:
77         case CL_SENT_CH_RESPONSE:
78         case CL_SENT_COMMAND:
79                 para_fd_set(ct->rc4c.fd, &s->rfds, &s->max_fileno);
80                 return;
81
82         case CL_RECEIVED_WELCOME:
83         case CL_RECEIVED_PROCEED:
84                 para_fd_set(ct->rc4c.fd, &s->wfds, &s->max_fileno);
85                 return;
86
87         case CL_RECEIVING:
88                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
89                 if (ret != 0) {
90                         if (ret < 0)
91                                 sched_min_delay(s);
92                         else
93                                 para_fd_set(ct->rc4c.fd, &s->rfds,
94                                         &s->max_fileno);
95                 }
96                 return;
97         case CL_SENDING:
98                 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
99                 if (ret != 0) {
100                         if (ret < 0)
101                                 sched_min_delay(s);
102                         else
103                                 para_fd_set(ct->rc4c.fd, &s->wfds,
104                                         &s->max_fileno);
105                 }
106                 return;
107         }
108 }
109
110 static ssize_t client_recv_buffer(struct client_task *ct, char *buf, size_t len)
111 {
112         ssize_t ret;
113
114         if (ct->status < CL_SENT_CH_RESPONSE)
115                 ret = recv_buffer(ct->rc4c.fd, buf, len);
116         else
117                 ret = rc4_recv_buffer(&ct->rc4c, buf, len);
118         if (ret == 0)
119                 return -E_SERVER_EOF;
120         return ret;
121 }
122
123 /**
124  * The post select hook for client commands.
125  *
126  * \param s Pointer to the scheduler.
127  * \param t Pointer to the task struct for this command.
128  *
129  * Depending on the current state of the connection and the status of the read
130  * and write fd sets of \a s, this function performs the necessary steps to
131  * authenticate the connection, to send the command given by \a t->private_data
132  * and to receive para_server's output, if any.
133  *
134  * \sa struct sched, struct task.
135  */
136 static void client_post_select(struct sched *s, struct task *t)
137 {
138         struct client_task *ct = container_of(t, struct client_task, task);
139         struct btr_node *btrn = ct->btrn;
140         int ret = 0;
141         char buf[CLIENT_BUFSIZE];
142
143         t->error = 0;
144         if (ct->rc4c.fd < 0)
145                 return;
146         switch (ct->status) {
147         case CL_CONNECTED: /* receive welcome message */
148                 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
149                         return;
150                 ret = client_recv_buffer(ct, buf, sizeof(buf));
151                 if (ret < 0)
152                         goto err;
153                 ct->status = CL_RECEIVED_WELCOME;
154                 return;
155         case CL_RECEIVED_WELCOME: /* send auth command */
156                 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
157                 PARA_INFO_LOG("--> %s\n", buf);
158                 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
159                         return;
160                 ret = send_buffer(ct->rc4c.fd, buf);
161                 if (ret < 0)
162                         goto err;
163                 ct->status = CL_SENT_AUTH;
164                 return;
165         case CL_SENT_AUTH:
166                 /*
167                  * Receive challenge and rc4 keys, decrypt the challenge and
168                  * send back the hash of the decrypted challenge.
169                  */
170                 {
171                 /* decrypted challenge/rc4 buffer */
172                 unsigned char crypt_buf[1024];
173                 /* the SHA1 of the decrypted challenge */
174                 unsigned char challenge_sha1[HASH_SIZE];
175
176                 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
177                         return;
178                 ret = client_recv_buffer(ct, buf, sizeof(buf));
179                 if (ret < 0)
180                         goto err;
181                 PARA_INFO_LOG("<-- [challenge] (%d bytes)\n", ret);
182                 ret = para_decrypt_buffer(ct->key_file, crypt_buf,
183                         (unsigned char *)buf, ret);
184                 if (ret < 0)
185                         goto err;
186                 sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
187                 RC4_set_key(&ct->rc4c.send_key, RC4_KEY_LEN,
188                         crypt_buf + CHALLENGE_SIZE);
189                 RC4_set_key(&ct->rc4c.recv_key, RC4_KEY_LEN,
190                         crypt_buf + CHALLENGE_SIZE + RC4_KEY_LEN);
191                 hash_to_asc(challenge_sha1, buf);
192                 PARA_INFO_LOG("--> %s\n", buf);
193                 ret = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
194                         HASH_SIZE);
195                 if (ret < 0)
196                         goto err;
197                 ct->status = CL_SENT_CH_RESPONSE;
198                 return;
199                 }
200         case CL_SENT_CH_RESPONSE: /* read server response */
201                 {
202                 size_t bytes_received;
203                 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
204                         return;
205                 ret = client_recv_buffer(ct, buf, sizeof(buf));
206                 if (ret < 0)
207                         goto err;
208                 bytes_received = ret;
209                 /* check if server has sent "Proceed" message */
210                 ret = -E_CLIENT_AUTH;
211                 if (bytes_received < PROCEED_MSG_LEN)
212                         goto err;
213                 if (!strstr(buf, PROCEED_MSG))
214                         goto err;
215                 ct->status = CL_RECEIVED_PROCEED;
216                 return;
217                 }
218         case CL_RECEIVED_PROCEED: /* concat args and send command */
219                 {
220                 int i;
221                 char *command = NULL;
222                 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
223                         return;
224                 for (i = 0; i < ct->conf.inputs_num; i++) {
225                         char *tmp = command;
226                         command = make_message("%s\n%s", command?
227                                 command : "", ct->conf.inputs[i]);
228                         free(tmp);
229                 }
230                 command = para_strcat(command, EOC_MSG "\n");
231                 PARA_DEBUG_LOG("--> %s\n", command);
232                 ret = rc4_send_buffer(&ct->rc4c, command);
233                 free(command);
234                 if (ret < 0)
235                         goto err;
236                 ct->status = CL_SENT_COMMAND;
237                 return;
238                 }
239         case CL_SENT_COMMAND:
240                 {
241                 char *buf2;
242                 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
243                         return;
244                 /* can not use "buf" here because we need a malloced buffer */
245                 buf2 = para_malloc(CLIENT_BUFSIZE);
246                 ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
247                 if (ret < 0) {
248                         free(buf2);
249                         goto err;
250                 }
251                 if (strstr(buf2, AWAITING_DATA_MSG)) {
252                         free(buf2);
253                         ct->status = CL_SENDING;
254                         return;
255                 }
256                 ct->status = CL_RECEIVING;
257                 btr_add_output(buf2, ret, btrn);
258                 return;
259                 }
260         case CL_SENDING:
261                 {
262                 char *buf2;
263                 size_t sz;
264                 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
265                 if (ret < 0)
266                         goto err;
267                 if (ret == 0)
268                         return;
269                 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
270                         return;
271                 sz = btr_next_buffer(btrn, &buf2);
272                 ret = rc4_send_bin_buffer(&ct->rc4c, buf2, sz);
273                 if (ret < 0)
274                         goto err;
275                 btr_consume(btrn, sz);
276                 return;
277                 }
278         case CL_RECEIVING:
279                 {
280                 char *buf2;
281                 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
282                 if (ret < 0)
283                         goto err;
284                 if (ret == 0)
285                         return;
286                 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
287                         return;
288                 buf2 = para_malloc(CLIENT_BUFSIZE);
289                 ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
290                 if (ret < 0) {
291                         free(buf2);
292                         goto err;
293                 }
294                 buf2 = para_realloc(buf2, ret);
295                 btr_add_output(buf2, ret, btrn);
296                 return;
297                 }
298         }
299 err:
300         t->error = ret;
301         if (ret < 0) {
302                 if (ret != -E_SERVER_EOF && ret != -E_BTR_EOF)
303                         PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
304                 btr_remove_node(btrn);
305         }
306 }
307
308 /* connect to para_server and register the client task */
309 static int client_connect(struct client_task *ct)
310 {
311         int ret;
312
313         ct->rc4c.fd = -1;
314         ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
315                 ct->conf.server_port_arg);
316         if (ret < 0)
317                 return ret;
318         ct->rc4c.fd = ret;
319         ct->status = CL_CONNECTED;
320         ret = mark_fd_nonblocking(ct->rc4c.fd);
321         if (ret < 0)
322                 goto err_out;
323         ct->task.pre_select = client_pre_select;
324         ct->task.post_select = client_post_select;
325         sprintf(ct->task.status, "client");
326         register_task(&ct->task);
327         return 1;
328 err_out:
329         close(ct->rc4c.fd);
330         ct->rc4c.fd = -1;
331         return ret;
332 }
333
334 /**
335  * Open connection to para_server.
336  *
337  * \param argc Usual argument count.
338  * \param argv Usual argument vector.
339  * \param ct_ptr Points to dynamically allocated and initialized client task
340  * struct upon successful return.
341  * \param loglevel If not \p NULL, the number of the loglevel is stored here.
342  * \param parent Add the new buffer tree node as a child of this node.
343  * \param child Add the new buffer tree node as a parent of this node.
344  *
345  * Check the command line options given by \a argc and argv, set default values
346  * for user name and rsa key file, read further option from the config file.
347  * Finally, establish a connection to para_server.
348  *
349  * \return Standard.
350  */
351 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
352                 int *loglevel, struct btr_node *parent, struct btr_node *child)
353 {
354         char *home = para_homedir();
355         int ret;
356         struct client_task *ct = para_calloc(sizeof(struct client_task));
357
358         ct->btrn = btr_new_node(&(struct btr_node_description)
359                 EMBRACE(.name = "client", .parent = parent, .child = child));
360         *ct_ptr = ct;
361         ct->rc4c.fd = -1;
362         ret = -E_CLIENT_SYNTAX;
363         if (client_cmdline_parser(argc, argv, &ct->conf))
364                 goto out;
365         HANDLE_VERSION_FLAG("client", ct->conf);
366         ret = -E_CLIENT_SYNTAX;
367         if (!ct->conf.inputs_num)
368                 goto out;
369         ct->user = ct->conf.user_given?
370                 para_strdup(ct->conf.user_arg) : para_logname();
371
372         ct->key_file = ct->conf.key_file_given?
373                 para_strdup(ct->conf.key_file_arg) :
374                 make_message("%s/.paraslash/key.%s", home, ct->user);
375
376         ct->config_file = ct->conf.config_file_given?
377                 para_strdup(ct->conf.config_file_arg) :
378                 make_message("%s/.paraslash/client.conf", home);
379         ret = file_exists(ct->config_file);
380         if (!ret && ct->conf.config_file_given) {
381                 ret = -E_NO_CONFIG;
382                 goto out;
383         }
384         if (ret) {
385                 struct client_cmdline_parser_params params = {
386                         .override = 0,
387                         .initialize = 0,
388                         .check_required = 0,
389                         .check_ambiguity = 0,
390                         .print_errors = 0
391                 };
392                 ret = -E_BAD_CONFIG;
393                 if (client_cmdline_parser_config_file(ct->config_file,
394                         &ct->conf, &params))
395                         goto out;
396         }
397         if (loglevel)
398                 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
399         PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
400         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
401         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
402         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
403                 ct->conf.server_port_arg);
404         ret = client_connect(ct);
405 out:
406         free(home);
407         if (ret < 0) {
408                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
409                 btr_remove_node(ct->btrn);
410                 btr_free_node(ct->btrn);
411                 client_close(ct);
412                 *ct_ptr = NULL;
413         }
414         return ret;
415 }