2 * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file client_common.c Common functions of para_client and para_audiod. */
10 #include <sys/types.h>
17 #include "client.cmdline.h"
23 #include "client.cmdline.h"
26 #include "buffer_tree.h"
28 /** The size of the receiving buffer. */
29 #define CLIENT_BUFSIZE 4000
32 * Close the connection to para_server and free all resources.
34 * \param ct Pointer to the client data.
38 void client_close(struct client_task *ct)
44 stream_cipher_free(ct->rc4c.recv);
45 stream_cipher_free(ct->rc4c.send);
47 free(ct->config_file);
49 client_cmdline_parser_free(&ct->conf);
54 * The preselect hook for server commands.
56 * \param s Pointer to the scheduler.
57 * \param t Pointer to the task struct for this command.
59 * The task pointer must contain a pointer to the initialized client data
60 * structure as it is returned by client_open().
62 * This function checks the state of the connection and adds the file descriptor
63 * of the connection to the read or write fd set of \a s accordingly.
65 * \sa register_task() client_open(), struct sched, struct task.
67 static void client_pre_select(struct sched *s, struct task *t)
70 struct client_task *ct = container_of(t, struct client_task, task);
71 struct btr_node *btrn = ct->btrn;
78 case CL_SENT_CH_RESPONSE:
80 para_fd_set(ct->rc4c.fd, &s->rfds, &s->max_fileno);
83 case CL_RECEIVED_WELCOME:
84 case CL_RECEIVED_PROCEED:
85 para_fd_set(ct->rc4c.fd, &s->wfds, &s->max_fileno);
89 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
94 para_fd_set(ct->rc4c.fd, &s->rfds,
99 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
104 para_fd_set(ct->rc4c.fd, &s->wfds,
111 static int client_recv_buffer(struct client_task *ct, fd_set *rfds,
112 char *buf, size_t sz, size_t *n)
116 if (ct->status < CL_SENT_CH_RESPONSE)
117 return read_nonblock(ct->rc4c.fd, buf, sz, rfds, n);
120 ret = rc4_recv_buffer(&ct->rc4c, buf, sz);
122 * rc4_recv_buffer is used with blocking fds elsewhere, so it
123 * does not use the nonblock-API. Therefore we need to
124 * check for EOF and EAGAIN.
127 return -E_SERVER_EOF;
128 if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
137 * The post select hook for client commands.
139 * \param s Pointer to the scheduler.
140 * \param t Pointer to the task struct for this command.
142 * Depending on the current state of the connection and the status of the read
143 * and write fd sets of \a s, this function performs the necessary steps to
144 * authenticate the connection, to send the command given by \a t->private_data
145 * and to receive para_server's output, if any.
147 * \sa struct sched, struct task.
149 static void client_post_select(struct sched *s, struct task *t)
151 struct client_task *ct = container_of(t, struct client_task, task);
152 struct btr_node *btrn = ct->btrn;
155 char buf[CLIENT_BUFSIZE];
160 switch (ct->status) {
161 case CL_CONNECTED: /* receive welcome message */
162 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
163 if (ret < 0 || n == 0)
165 ct->status = CL_RECEIVED_WELCOME;
167 case CL_RECEIVED_WELCOME: /* send auth command */
168 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
169 PARA_INFO_LOG("--> %s\n", buf);
170 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
172 ret = send_buffer(ct->rc4c.fd, buf);
175 ct->status = CL_SENT_AUTH;
179 * Receive challenge and rc4 keys, decrypt the challenge and
180 * send back the hash of the decrypted challenge.
183 /* decrypted challenge/rc4 buffer */
184 unsigned char crypt_buf[1024];
185 /* the SHA1 of the decrypted challenge */
186 unsigned char challenge_sha1[HASH_SIZE];
188 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
189 if (ret < 0 || n == 0)
191 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
192 ret = priv_decrypt(ct->key_file, crypt_buf,
193 (unsigned char *)buf, n);
196 sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
197 ct->rc4c.send = stream_cipher_new(crypt_buf + CHALLENGE_SIZE,
199 ct->rc4c.recv = stream_cipher_new(crypt_buf + CHALLENGE_SIZE
200 + RC4_KEY_LEN, RC4_KEY_LEN);
201 hash_to_asc(challenge_sha1, buf);
202 PARA_INFO_LOG("--> %s\n", buf);
203 ret = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
207 ct->status = CL_SENT_CH_RESPONSE;
210 case CL_SENT_CH_RESPONSE: /* read server response */
212 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
213 if (ret < 0 || n == 0)
215 /* check if server has sent "Proceed" message */
216 ret = -E_CLIENT_AUTH;
217 if (n < PROCEED_MSG_LEN)
219 if (!strstr(buf, PROCEED_MSG))
221 ct->status = CL_RECEIVED_PROCEED;
224 case CL_RECEIVED_PROCEED: /* concat args and send command */
227 char *command = NULL;
228 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
230 for (i = 0; i < ct->conf.inputs_num; i++) {
232 command = make_message("%s\n%s", command?
233 command : "", ct->conf.inputs[i]);
236 command = para_strcat(command, EOC_MSG "\n");
237 PARA_DEBUG_LOG("--> %s\n", command);
238 ret = rc4_send_buffer(&ct->rc4c, command);
242 ct->status = CL_SENT_COMMAND;
245 case CL_SENT_COMMAND:
248 /* can not use "buf" here because we need a malloced buffer */
249 buf2 = para_malloc(CLIENT_BUFSIZE);
250 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
252 if (strstr(buf2, AWAITING_DATA_MSG)) {
254 ct->status = CL_SENDING;
257 ct->status = CL_RECEIVING;
258 btr_add_output(buf2, n, btrn);
267 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
272 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
274 sz = btr_next_buffer(btrn, &buf2);
275 ret = rc4_send_bin_buffer(&ct->rc4c, buf2, sz);
278 btr_consume(btrn, sz);
284 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
290 * The FD_ISSET() is not strictly necessary, but is allows us
291 * to skip the malloc below if there is nothing to read anyway.
293 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
295 buf2 = para_malloc(CLIENT_BUFSIZE);
296 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
298 buf2 = para_realloc(buf2, n);
299 btr_add_output(buf2, n, btrn);
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);
314 /* connect to para_server and register the client task */
315 static int client_connect(struct client_task *ct)
320 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
321 ct->conf.server_port_arg);
325 ct->status = CL_CONNECTED;
326 ret = mark_fd_nonblocking(ct->rc4c.fd);
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);
341 * Open connection to para_server.
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.
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.
357 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
358 int *loglevel, struct btr_node *parent, struct btr_node *child)
360 char *home = para_homedir();
362 struct client_task *ct = para_calloc(sizeof(struct client_task));
364 ct->btrn = btr_new_node(&(struct btr_node_description)
365 EMBRACE(.name = "client", .parent = parent, .child = child));
368 ret = -E_CLIENT_SYNTAX;
369 if (client_cmdline_parser(argc, argv, &ct->conf))
371 HANDLE_VERSION_FLAG("client", ct->conf);
372 ret = -E_CLIENT_SYNTAX;
373 if (!ct->conf.inputs_num)
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) {
385 struct client_cmdline_parser_params params = {
389 .check_ambiguity = 0,
393 if (client_cmdline_parser_config_file(ct->config_file,
397 ct->user = ct->conf.user_given?
398 para_strdup(ct->conf.user_arg) : para_logname();
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);
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);
415 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
416 btr_remove_node(ct->btrn);
417 btr_free_node(ct->btrn);