NEWS update
[paraslash.git] / client_common.c
1 /*
2 * Copyright (C) 1997-2010 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 int client_recv_buffer(struct client_task *ct, fd_set *rfds,
111 char *buf, size_t sz, size_t *n)
112 {
113 int ret;
114
115 if (ct->status < CL_SENT_CH_RESPONSE)
116 return read_nonblock(ct->rc4c.fd, buf, sz, rfds, n);
117
118 *n = 0;
119 ret = rc4_recv_buffer(&ct->rc4c, buf, sz);
120 /*
121 * rc4_recv_buffer is used with blocking fds elsewhere, so it
122 * does not use the nonblock-API. Therefore we need to
123 * check for EOF and EAGAIN.
124 */
125 if (ret == 0)
126 return -E_SERVER_EOF;
127 if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
128 return 0;
129 if (ret < 0)
130 return ret;
131 *n = ret;
132 return 0;
133 }
134
135 /**
136 * The post select hook for client commands.
137 *
138 * \param s Pointer to the scheduler.
139 * \param t Pointer to the task struct for this command.
140 *
141 * Depending on the current state of the connection and the status of the read
142 * and write fd sets of \a s, this function performs the necessary steps to
143 * authenticate the connection, to send the command given by \a t->private_data
144 * and to receive para_server's output, if any.
145 *
146 * \sa struct sched, struct task.
147 */
148 static void client_post_select(struct sched *s, struct task *t)
149 {
150 struct client_task *ct = container_of(t, struct client_task, task);
151 struct btr_node *btrn = ct->btrn;
152 int ret = 0;
153 size_t n;
154 char buf[CLIENT_BUFSIZE];
155
156 t->error = 0;
157 if (ct->rc4c.fd < 0)
158 return;
159 switch (ct->status) {
160 case CL_CONNECTED: /* receive welcome message */
161 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
162 if (ret < 0 || n == 0)
163 goto out;
164 ct->status = CL_RECEIVED_WELCOME;
165 return;
166 case CL_RECEIVED_WELCOME: /* send auth command */
167 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
168 PARA_INFO_LOG("--> %s\n", buf);
169 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
170 return;
171 ret = send_buffer(ct->rc4c.fd, buf);
172 if (ret < 0)
173 goto err;
174 ct->status = CL_SENT_AUTH;
175 return;
176 case CL_SENT_AUTH:
177 /*
178 * Receive challenge and rc4 keys, decrypt the challenge and
179 * send back the hash of the decrypted challenge.
180 */
181 {
182 /* decrypted challenge/rc4 buffer */
183 unsigned char crypt_buf[1024];
184 /* the SHA1 of the decrypted challenge */
185 unsigned char challenge_sha1[HASH_SIZE];
186
187 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
188 if (ret < 0 || n == 0)
189 goto out;
190 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
191 ret = para_decrypt_buffer(ct->key_file, crypt_buf,
192 (unsigned char *)buf, n);
193 if (ret < 0)
194 goto err;
195 sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
196 RC4_set_key(&ct->rc4c.send_key, RC4_KEY_LEN,
197 crypt_buf + CHALLENGE_SIZE);
198 RC4_set_key(&ct->rc4c.recv_key, RC4_KEY_LEN,
199 crypt_buf + CHALLENGE_SIZE + RC4_KEY_LEN);
200 hash_to_asc(challenge_sha1, buf);
201 PARA_INFO_LOG("--> %s\n", buf);
202 ret = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
203 HASH_SIZE);
204 if (ret < 0)
205 goto err;
206 ct->status = CL_SENT_CH_RESPONSE;
207 return;
208 }
209 case CL_SENT_CH_RESPONSE: /* read server response */
210 {
211 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
212 if (ret < 0 || n == 0)
213 goto out;
214 /* check if server has sent "Proceed" message */
215 ret = -E_CLIENT_AUTH;
216 if (n < PROCEED_MSG_LEN)
217 goto out;
218 if (!strstr(buf, PROCEED_MSG))
219 goto out;
220 ct->status = CL_RECEIVED_PROCEED;
221 return;
222 }
223 case CL_RECEIVED_PROCEED: /* concat args and send command */
224 {
225 int i;
226 char *command = NULL;
227 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
228 return;
229 for (i = 0; i < ct->conf.inputs_num; i++) {
230 char *tmp = command;
231 command = make_message("%s\n%s", command?
232 command : "", ct->conf.inputs[i]);
233 free(tmp);
234 }
235 command = para_strcat(command, EOC_MSG "\n");
236 PARA_DEBUG_LOG("--> %s\n", command);
237 ret = rc4_send_buffer(&ct->rc4c, command);
238 free(command);
239 if (ret < 0)
240 goto err;
241 ct->status = CL_SENT_COMMAND;
242 return;
243 }
244 case CL_SENT_COMMAND:
245 {
246 char *buf2;
247 /* can not use "buf" here because we need a malloced buffer */
248 buf2 = para_malloc(CLIENT_BUFSIZE);
249 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
250 if (n > 0) {
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, n, btrn);
258 } else
259 free(buf2);
260 goto out;
261 }
262 case CL_SENDING:
263 {
264 char *buf2;
265 size_t sz;
266 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
267 if (ret < 0)
268 goto err;
269 if (ret == 0)
270 return;
271 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
272 return;
273 sz = btr_next_buffer(btrn, &buf2);
274 ret = rc4_send_bin_buffer(&ct->rc4c, buf2, sz);
275 if (ret < 0)
276 goto err;
277 btr_consume(btrn, sz);
278 return;
279 }
280 case CL_RECEIVING:
281 {
282 char *buf2;
283 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
284 if (ret < 0)
285 goto err;
286 if (ret == 0)
287 return;
288 /*
289 * The FD_ISSET() is not strictly necessary, but is allows us
290 * to skip the malloc below if there is nothing to read anyway.
291 */
292 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
293 return;
294 buf2 = para_malloc(CLIENT_BUFSIZE);
295 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
296 if (n > 0) {
297 buf2 = para_realloc(buf2, n);
298 btr_add_output(buf2, n, btrn);
299 } else
300 free(buf2);
301 goto out;
302 }
303 }
304 err:
305 out:
306 t->error = ret;
307 if (ret < 0) {
308 if (ret != -E_SERVER_EOF && ret != -E_BTR_EOF)
309 PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
310 btr_remove_node(btrn);
311 }
312 }
313
314 /* connect to para_server and register the client task */
315 static int client_connect(struct client_task *ct)
316 {
317 int ret;
318
319 ct->rc4c.fd = -1;
320 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
321 ct->conf.server_port_arg);
322 if (ret < 0)
323 return ret;
324 ct->rc4c.fd = ret;
325 ct->status = CL_CONNECTED;
326 ret = mark_fd_nonblocking(ct->rc4c.fd);
327 if (ret < 0)
328 goto err_out;
329 ct->task.pre_select = client_pre_select;
330 ct->task.post_select = client_post_select;
331 sprintf(ct->task.status, "client");
332 register_task(&ct->task);
333 return 1;
334 err_out:
335 close(ct->rc4c.fd);
336 ct->rc4c.fd = -1;
337 return ret;
338 }
339
340 /**
341 * Open connection to para_server.
342 *
343 * \param argc Usual argument count.
344 * \param argv Usual argument vector.
345 * \param ct_ptr Points to dynamically allocated and initialized client task
346 * struct upon successful return.
347 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
348 * \param parent Add the new buffer tree node as a child of this node.
349 * \param child Add the new buffer tree node as a parent of this node.
350 *
351 * Check the command line options given by \a argc and argv, set default values
352 * for user name and rsa key file, read further option from the config file.
353 * Finally, establish a connection to para_server.
354 *
355 * \return Standard.
356 */
357 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
358 int *loglevel, struct btr_node *parent, struct btr_node *child)
359 {
360 char *home = para_homedir();
361 int ret;
362 struct client_task *ct = para_calloc(sizeof(struct client_task));
363
364 ct->btrn = btr_new_node(&(struct btr_node_description)
365 EMBRACE(.name = "client", .parent = parent, .child = child));
366 *ct_ptr = ct;
367 ct->rc4c.fd = -1;
368 ret = -E_CLIENT_SYNTAX;
369 if (client_cmdline_parser(argc, argv, &ct->conf))
370 goto out;
371 HANDLE_VERSION_FLAG("client", ct->conf);
372 ret = -E_CLIENT_SYNTAX;
373 if (!ct->conf.inputs_num)
374 goto out;
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 ct->user = ct->conf.user_given?
398 para_strdup(ct->conf.user_arg) : para_logname();
399
400 ct->key_file = ct->conf.key_file_given?
401 para_strdup(ct->conf.key_file_arg) :
402 make_message("%s/.paraslash/key.%s", home, ct->user);
403
404 if (loglevel)
405 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
406 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
407 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
408 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
409 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
410 ct->conf.server_port_arg);
411 ret = client_connect(ct);
412 out:
413 free(home);
414 if (ret < 0) {
415 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
416 btr_remove_node(ct->btrn);
417 btr_free_node(ct->btrn);
418 client_close(ct);
419 *ct_ptr = NULL;
420 }
421 return ret;
422 }