2 * Copyright (C) 1997-2012 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>
16 #include "client.cmdline.h"
22 #include "client.cmdline.h"
24 #include "buffer_tree.h"
27 /** The size of the receiving buffer. */
28 #define CLIENT_BUFSIZE 4000
31 * Close the connection to para_server and deallocate per-command ressources.
33 * \param ct The client task.
35 * This frees all ressources of the current command but keeps the configuration
38 * \sa \ref client_close().
40 void client_disconnect(struct client_task *ct)
46 free_argv(ct->features);
47 sc_free(ct->scc.recv);
49 sc_free(ct->scc.send);
51 btr_free_node(ct->btrn);
56 * Close the connection to para_server and free all resources.
58 * \param ct Pointer to the client data.
60 * \sa \ref client_open(), \ref client_disconnect().
62 void client_close(struct client_task *ct)
66 client_disconnect(ct);
68 free(ct->config_file);
70 client_cmdline_parser_free(&ct->conf);
75 * The preselect hook for server commands.
77 * \param s Pointer to the scheduler.
78 * \param t Pointer to the task struct for this command.
80 * The task pointer must contain a pointer to the initialized client data
81 * structure as it is returned by client_open().
83 * This function checks the state of the connection and adds the file descriptor
84 * of the connection to the read or write fd set of \a s accordingly.
86 * \sa register_task() client_open(), struct sched, struct task.
88 static void client_pre_select(struct sched *s, struct task *t)
91 struct client_task *ct = container_of(t, struct client_task, task);
92 struct btr_node *btrn = ct->btrn;
99 case CL_SENT_CH_RESPONSE:
100 case CL_SENT_COMMAND:
101 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
104 case CL_RECEIVED_WELCOME:
105 case CL_RECEIVED_PROCEED:
106 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
110 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
115 para_fd_set(ct->scc.fd, &s->rfds,
120 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
125 para_fd_set(ct->scc.fd, &s->wfds,
132 static int client_recv_buffer(struct client_task *ct, fd_set *rfds,
133 char *buf, size_t sz, size_t *n)
137 if (ct->status < CL_SENT_CH_RESPONSE)
138 return read_nonblock(ct->scc.fd, buf, sz, rfds, n);
141 ret = sc_recv_buffer(&ct->scc, buf, sz);
143 * sc_recv_buffer is used with blocking fds elsewhere, so it
144 * does not use the nonblock-API. Therefore we need to
145 * check for EOF and EAGAIN.
148 return -E_SERVER_EOF;
149 if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
157 static int send_sb(struct client_task *ct, void *buf, size_t numbytes,
158 enum sb_designator band, bool dont_free)
160 int ret, fd = ct->scc.fd;
164 struct sb_buffer sbb;
165 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
167 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
168 ct->sbc = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
170 ret = sb_get_send_buffers(ct->sbc, iov);
171 ret = xwritev(fd, iov, ret);
177 if (sb_sent(ct->sbc, ret)) {
184 static int recv_sb(struct client_task *ct, fd_set *rfds,
185 struct sb_buffer *result)
189 sb_transformation trafo;
193 if (!FD_ISSET(ct->scc.fd, rfds))
195 if (ct->status < CL_SENT_CH_RESPONSE)
196 trafo = trafo_context = NULL;
199 trafo_context = ct->scc.recv;
202 ct->sbc = sb_new_recv(0, trafo, trafo_context);
204 sb_get_recv_buffer(ct->sbc, &iov);
205 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
213 if (!sb_received(ct->sbc, n, result))
220 static char **parse_features(char *buf)
223 const char id[] = "\nFeatures: ";
224 char *p, *q, **features;
234 create_argv(p, ",", &features);
235 for (i = 0; features[i]; i++)
236 PARA_INFO_LOG("server feature: %s\n", features[i]);
240 static bool has_feature(const char *feature, struct client_task *ct)
242 return find_arg(feature, ct->features) >= 0? true : false;
246 * The post select hook for client commands.
248 * \param s Pointer to the scheduler.
249 * \param t Pointer to the task struct for this command.
251 * Depending on the current state of the connection and the status of the read
252 * and write fd sets of \a s, this function performs the necessary steps to
253 * authenticate the connection, to send the command given by \a t->private_data
254 * and to receive para_server's output, if any.
256 * \sa struct sched, struct task.
258 static void client_post_select(struct sched *s, struct task *t)
260 struct client_task *ct = container_of(t, struct client_task, task);
261 struct btr_node *btrn = ct->btrn;
264 char buf[CLIENT_BUFSIZE];
269 switch (ct->status) {
270 case CL_CONNECTED: /* receive welcome message */
271 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
272 if (ret < 0 || n == 0)
274 ct->features = parse_features(buf);
275 ct->status = CL_RECEIVED_WELCOME;
277 case CL_RECEIVED_WELCOME: /* send auth command */
278 if (!FD_ISSET(ct->scc.fd, &s->wfds))
280 if (has_feature("sideband", ct)) {
281 ct->use_sideband = true;
282 sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user);
284 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
285 PARA_INFO_LOG("--> %s\n", buf);
286 ret = write_buffer(ct->scc.fd, buf);
289 ct->status = CL_SENT_AUTH;
293 * Receive challenge and session keys, decrypt the challenge and
294 * send back the hash of the decrypted challenge.
297 /* decrypted challenge/session key buffer */
298 unsigned char crypt_buf[1024];
299 /* the SHA1 of the decrypted challenge */
300 unsigned char challenge_hash[HASH_SIZE];
302 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
303 if (ret < 0 || n == 0)
305 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
306 ret = priv_decrypt(ct->key_file, crypt_buf,
307 (unsigned char *)buf, n);
310 hash_function((char *)crypt_buf, CHALLENGE_SIZE, challenge_hash);
311 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
312 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
314 hash_to_asc(challenge_hash, buf);
315 PARA_INFO_LOG("--> %s\n", buf);
316 ret = write_all(ct->scc.fd, (char *)challenge_hash, HASH_SIZE);
319 ct->status = CL_SENT_CH_RESPONSE;
322 case CL_SENT_CH_RESPONSE: /* read server response */
324 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
325 if (ret < 0 || n == 0)
327 /* check if server has sent "Proceed" message */
328 ret = -E_CLIENT_AUTH;
329 if (n < PROCEED_MSG_LEN)
331 if (!strstr(buf, PROCEED_MSG))
333 ct->status = CL_RECEIVED_PROCEED;
336 case CL_RECEIVED_PROCEED: /* concat args and send command */
339 char *command = NULL;
340 if (!FD_ISSET(ct->scc.fd, &s->wfds))
342 for (i = 0; i < ct->conf.inputs_num; i++) {
344 command = make_message("%s\n%s", command?
345 command : "", ct->conf.inputs[i]);
348 command = para_strcat(command, EOC_MSG "\n");
349 PARA_DEBUG_LOG("--> %s\n", command);
350 ret = sc_send_buffer(&ct->scc, command);
354 ct->status = CL_SENT_COMMAND;
357 case CL_SENT_COMMAND:
360 /* can not use "buf" here because we need a malloced buffer */
361 buf2 = para_malloc(CLIENT_BUFSIZE);
362 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
364 if (strstr(buf2, AWAITING_DATA_MSG)) {
366 ct->status = CL_SENDING;
369 ct->status = CL_RECEIVING;
370 btr_add_output(buf2, n, btrn);
379 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
384 if (!FD_ISSET(ct->scc.fd, &s->wfds))
386 sz = btr_next_buffer(btrn, &buf2);
387 ret = sc_send_bin_buffer(&ct->scc, buf2, sz);
390 btr_consume(btrn, sz);
396 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
402 * The FD_ISSET() is not strictly necessary, but is allows us
403 * to skip the malloc below if there is nothing to read anyway.
405 if (!FD_ISSET(ct->scc.fd, &s->rfds))
407 buf2 = para_malloc(CLIENT_BUFSIZE);
408 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
410 buf2 = para_realloc(buf2, n);
411 btr_add_output(buf2, n, btrn);
420 if (ret != -E_SERVER_EOF && ret != -E_BTR_EOF)
421 PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
422 btr_remove_node(btrn);
427 * Connect to para_server and register the client task.
429 * \param ct The initialized client task structure.
430 * \param s The scheduler instance to register the client task to.
431 * \param parent The parent node of the client btr node.
432 * \param child The child node of the client node.
434 * The client task structure given by \a ct must be allocated and initialized
435 * by \ref client_parse_config() before this function is called.
439 int client_connect(struct client_task *ct, struct sched *s,
440 struct btr_node *parent, struct btr_node *child)
444 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
445 ct->conf.server_port_arg);
447 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
448 ct->conf.server_port_arg);
452 ret = mark_fd_nonblocking(ct->scc.fd);
455 ct->status = CL_CONNECTED;
456 ct->btrn = btr_new_node(&(struct btr_node_description)
457 EMBRACE(.name = "client", .parent = parent, .child = child));
458 ct->task.pre_select = client_pre_select;
459 ct->task.post_select = client_post_select;
461 sprintf(ct->task.status, "client");
462 register_task(s, &ct->task);
471 * Parse a client configuration.
473 * \param argc Usual argument count.
474 * \param argv Usual argument vector.
475 * \param ct_ptr Filled in by this function.
476 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
478 * This checks the command line options given by \a argc and \a argv, sets
479 * default values for the user name and the name of the rsa key file and reads
480 * further options from the config file.
482 * Upon successful return, \a ct_ptr points to a dynamically allocated and
483 * initialized client task struct.
485 * \return The number of non-option arguments in \a argc/argv on success,
486 * negative on errors.
488 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
491 char *home = para_homedir();
493 struct client_task *ct = para_calloc(sizeof(struct client_task));
497 ret = -E_CLIENT_SYNTAX;
498 if (client_cmdline_parser(argc, argv, &ct->conf))
500 HANDLE_VERSION_FLAG("client", ct->conf);
502 ct->config_file = ct->conf.config_file_given?
503 para_strdup(ct->conf.config_file_arg) :
504 make_message("%s/.paraslash/client.conf", home);
505 ret = file_exists(ct->config_file);
506 if (!ret && ct->conf.config_file_given) {
511 struct client_cmdline_parser_params params = {
515 .check_ambiguity = 0,
519 if (client_cmdline_parser_config_file(ct->config_file,
523 ct->user = ct->conf.user_given?
524 para_strdup(ct->conf.user_arg) : para_logname();
526 if (ct->conf.key_file_given)
527 ct->key_file = para_strdup(ct->conf.key_file_arg);
529 ct->key_file = make_message("%s/.paraslash/key.%s",
531 if (!file_exists(ct->key_file)) {
533 ct->key_file = make_message("%s/.ssh/id_rsa", home);
538 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
539 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
540 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
541 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
542 ret = ct->conf.inputs_num;
546 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
554 * Parse the client configuration and open a connection to para_server.
556 * \param argc See \ref client_parse_config.
557 * \param argv See \ref client_parse_config.
558 * \param ct_ptr See \ref client_parse_config.
559 * \param loglevel See \ref client_parse_config.
560 * \param parent See \ref client_connect().
561 * \param child See \ref client_connect().
562 * \param sched See \ref client_connect().
564 * This function combines client_parse_config() and client_connect(). It is
565 * considered a syntax error if no command was given, i.e. if the number
566 * of non-option arguments is zero.
570 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
571 int *loglevel, struct btr_node *parent, struct btr_node *child,
574 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
579 ret = -E_CLIENT_SYNTAX;
582 ret = client_connect(*ct_ptr, sched, parent, child);
587 client_close(*ct_ptr);