[btr] Only print debug message if we're really increasing the wrap buffer.
[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] (%zu 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 *
343 * Check the command line options given by \a argc and argv, set default values
344 * for user name and rsa key file, read further option from the config file.
345 * Finally, establish a connection to para_server.
346 *
347 * \return Standard.
348 */
349 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
350 int *loglevel, struct btr_node *parent, struct btr_node *child)
351 {
352 char *home = para_homedir();
353 int ret;
354 struct client_task *ct = para_calloc(sizeof(struct client_task));
355
356 ct->btrn = btr_new_node(&(struct btr_node_description)
357 EMBRACE(.name = "client", .parent = parent, .child = child));
358 *ct_ptr = ct;
359 ct->rc4c.fd = -1;
360 ret = -E_CLIENT_SYNTAX;
361 if (client_cmdline_parser(argc, argv, &ct->conf))
362 goto out;
363 HANDLE_VERSION_FLAG("client", ct->conf);
364 ret = -E_CLIENT_SYNTAX;
365 if (!ct->conf.inputs_num)
366 goto out;
367 ct->user = ct->conf.user_given?
368 para_strdup(ct->conf.user_arg) : para_logname();
369
370 ct->key_file = ct->conf.key_file_given?
371 para_strdup(ct->conf.key_file_arg) :
372 make_message("%s/.paraslash/key.%s", home, ct->user);
373
374 ct->config_file = ct->conf.config_file_given?
375 para_strdup(ct->conf.config_file_arg) :
376 make_message("%s/.paraslash/client.conf", home);
377 ret = file_exists(ct->config_file);
378 if (!ret && ct->conf.config_file_given) {
379 ret = -E_NO_CONFIG;
380 goto out;
381 }
382 if (ret) {
383 struct client_cmdline_parser_params params = {
384 .override = 0,
385 .initialize = 0,
386 .check_required = 0,
387 .check_ambiguity = 0,
388 .print_errors = 0
389 };
390 ret = -E_BAD_CONFIG;
391 if (client_cmdline_parser_config_file(ct->config_file,
392 &ct->conf, &params))
393 goto out;
394 }
395 if (loglevel)
396 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
397 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
398 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
399 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
400 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
401 ct->conf.server_port_arg);
402 ret = client_connect(ct);
403 out:
404 free(home);
405 if (ret < 0) {
406 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
407 btr_free_node(ct->btrn);
408 client_close(ct);
409 *ct_ptr = NULL;
410 }
411 return ret;
412 }