filter.h: Kill some unused fields of struct filter_node.
[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
145         t->error = 0;
146         if (ct->rc4c.fd < 0)
147                 return;
148         if (!ct->check_r && !ct->check_w)
149                 return;
150         if (ct->check_r && !FD_ISSET(ct->rc4c.fd, &s->rfds))
151                 return;
152         if (ct->check_w && !FD_ISSET(ct->rc4c.fd, &s->wfds))
153                 return;
154         switch (ct->status) {
155         case CL_CONNECTED: /* receive welcome message */
156                 t->error = client_recv_buffer(ct);
157                 if (t->error < 0)
158                         goto err;
159                 ct->status = CL_RECEIVED_WELCOME;
160                 return;
161         case CL_RECEIVED_WELCOME: /* send auth command */
162                 sprintf(ct->buf, AUTH_REQUEST_MSG "%s", ct->user);
163                 PARA_INFO_LOG("--> %s\n", ct->buf);
164                 t->error = send_buffer(ct->rc4c.fd, ct->buf);
165                 if (t->error < 0)
166                         goto err;
167                 ct->status = CL_SENT_AUTH;
168                 return;
169         case CL_SENT_AUTH: /* receive challenge and rc4 keys */
170                 ct->loaded = 0;
171                 t->error = client_recv_buffer(ct);
172                 if (t->error < 0)
173                         goto err;
174                 ct->loaded = t->error;
175                 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", ct->loaded);
176                 ct->status = CL_RECEIVED_CHALLENGE;
177                 return;
178         case CL_RECEIVED_CHALLENGE:
179                 {
180                 /* decrypted challenge/rc4 buffer */
181                 unsigned char crypt_buf[1024];
182                 /* the SHA1 of the decrypted challenge */
183                 unsigned char challenge_sha1[HASH_SIZE];
184
185                 t->error = para_decrypt_buffer(ct->key_file, crypt_buf,
186                         (unsigned char *)ct->buf, ct->loaded);
187                 if (t->error < 0)
188                         goto err;
189                 sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
190                 RC4_set_key(&ct->rc4c.send_key, RC4_KEY_LEN,
191                         crypt_buf + CHALLENGE_SIZE);
192                 RC4_set_key(&ct->rc4c.recv_key, RC4_KEY_LEN,
193                         crypt_buf + CHALLENGE_SIZE + RC4_KEY_LEN);
194                 hash_to_asc(challenge_sha1, ct->buf);
195                 PARA_INFO_LOG("--> %s\n", ct->buf);
196                 t->error = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
197                         HASH_SIZE);
198                 if (t->error < 0)
199                         goto err;
200                 ct->status = CL_SENT_CH_RESPONSE;
201                 return;
202                 }
203         case CL_SENT_CH_RESPONSE: /* read server response */
204                 {
205                 size_t bytes_received;
206                 ct->loaded = 0;
207                 t->error = client_recv_buffer(ct);
208                 if (t->error < 0)
209                         goto err;
210                 bytes_received = t->error;
211                 /* check if server has sent "Proceed" message */
212                 t->error = -E_CLIENT_AUTH;
213                 if (bytes_received < PROCEED_MSG_LEN)
214                         goto err;
215                 if (!strstr(ct->buf, PROCEED_MSG))
216                         goto err;
217                 ct->status = CL_RECEIVED_PROCEED;
218                 t->error = 0;
219                 return;
220                 }
221         case CL_RECEIVED_PROCEED: /* concat args and send command */
222                 {
223                 int i;
224                 char *command = NULL;
225                 for (i = 0; i < ct->conf.inputs_num; i++) {
226                         char *tmp = command;
227                         command = make_message("%s\n%s", command?
228                                 command : "", ct->conf.inputs[i]);
229                         free(tmp);
230                 }
231                 command = para_strcat(command, EOC_MSG "\n");
232                 PARA_DEBUG_LOG("--> %s\n", command);
233                 t->error = rc4_send_buffer(&ct->rc4c, command);
234                 free(command);
235                 if (t->error < 0)
236                         goto err;
237                 ct->status = CL_SENT_COMMAND;
238                 return;
239                 }
240         case CL_SENT_COMMAND:
241                 ct->loaded = 0;
242                 t->error = client_recv_buffer(ct);
243                 if (t->error < 0)
244                         goto err;
245                 if (strstr(ct->buf, AWAITING_DATA_MSG))
246                         ct->status = CL_SENDING;
247                 else
248                         ct->status = CL_RECEIVING;
249                 return;
250         case CL_SENDING:
251                 PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
252                 t->error = rc4_send_bin_buffer(&ct->rc4c, ct->inbuf,
253                         *ct->in_loaded);
254                 if (t->error < 0)
255                         goto err;
256                 *ct->in_loaded = 0;
257                 return;
258         case CL_RECEIVING:
259                 t->error = client_recv_buffer(ct);
260                 if (t->error < 0)
261                         goto err;
262                 return;
263         }
264 err:
265         if (t->error != -E_SERVER_EOF)
266                 PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
267 }
268
269 /* connect to para_server and register the client task */
270 static int client_connect(struct client_task *ct)
271 {
272         int ret;
273
274         ct->rc4c.fd = -1;
275         ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
276                 ct->conf.server_port_arg);
277         if (ret < 0)
278                 return ret;
279         ct->rc4c.fd = ret;
280         ct->status = CL_CONNECTED;
281         ret = mark_fd_nonblocking(ct->rc4c.fd);
282         if (ret < 0)
283                 goto err_out;
284         ct->task.pre_select = client_pre_select;
285         ct->task.post_select = client_post_select;
286         sprintf(ct->task.status, "client");
287         register_task(&ct->task);
288         return 1;
289 err_out:
290         close(ct->rc4c.fd);
291         ct->rc4c.fd = -1;
292         return ret;
293 }
294
295 /**
296  * Open connection to para_server.
297  *
298  * \param argc Usual argument count.
299  * \param argv Usual argument vector.
300  * \param ct_ptr Points to dynamically allocated and initialized client task
301  * struct upon successful return.
302  * \param loglevel If not \p NULL, the number of the loglevel is stored here.
303  *
304  * Check the command line options given by \a argc and argv, set default values
305  * for user name and rsa key file, read further option from the config file.
306  * Finally, establish a connection to para_server.
307  *
308  * \return Standard.
309  */
310 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
311                 int *loglevel)
312 {
313         char *home = para_homedir();
314         int ret;
315         struct client_task *ct = para_calloc(sizeof(struct client_task));
316
317         ct->buf = para_malloc(CLIENT_BUFSIZE);
318         *ct_ptr = ct;
319         ct->rc4c.fd = -1;
320         ret = -E_CLIENT_SYNTAX;
321         if (client_cmdline_parser(argc, argv, &ct->conf))
322                 goto out;
323         HANDLE_VERSION_FLAG("client", ct->conf);
324         ret = -E_CLIENT_SYNTAX;
325         if (!ct->conf.inputs_num)
326                 goto out;
327         ct->user = ct->conf.user_given?
328                 para_strdup(ct->conf.user_arg) : para_logname();
329
330         ct->key_file = ct->conf.key_file_given?
331                 para_strdup(ct->conf.key_file_arg) :
332                 make_message("%s/.paraslash/key.%s", home, ct->user);
333
334         ct->config_file = ct->conf.config_file_given?
335                 para_strdup(ct->conf.config_file_arg) :
336                 make_message("%s/.paraslash/client.conf", home);
337         ret = file_exists(ct->config_file);
338         if (!ret && ct->conf.config_file_given) {
339                 ret = -E_NO_CONFIG;
340                 goto out;
341         }
342         if (ret) {
343                 struct client_cmdline_parser_params params = {
344                         .override = 0,
345                         .initialize = 0,
346                         .check_required = 0,
347                         .check_ambiguity = 0,
348                         .print_errors = 0
349                 };
350                 ret = -E_BAD_CONFIG;
351                 if (client_cmdline_parser_config_file(ct->config_file,
352                         &ct->conf, &params))
353                         goto out;
354         }
355         if (loglevel)
356                 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
357         PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
358         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
359         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
360         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
361                 ct->conf.server_port_arg);
362         ret = client_connect(ct);
363 out:
364         free(home);
365         if (ret < 0) {
366                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
367                 client_close(ct);
368                 *ct_ptr = NULL;
369         }
370         return ret;
371 }