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