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_remove_node(&ct->btrn);
55 * Close the connection to para_server and free all resources.
57 * \param ct Pointer to the client data.
59 * \sa \ref client_open(), \ref client_disconnect().
61 void client_close(struct client_task *ct)
65 client_disconnect(ct);
67 free(ct->config_file);
69 client_cmdline_parser_free(&ct->conf);
70 free(ct->challenge_hash);
76 * The preselect hook for server commands.
78 * \param s Pointer to the scheduler.
79 * \param t Pointer to the task struct for this command.
81 * The task pointer must contain a pointer to the initialized client data
82 * structure as it is returned by client_open().
84 * This function checks the state of the connection and adds the file descriptor
85 * of the connection to the read or write fd set of \a s accordingly.
87 * \sa register_task() client_open(), struct sched, struct task.
89 static void client_pre_select(struct sched *s, struct task *t)
92 struct client_task *ct = container_of(t, struct client_task, task);
93 struct btr_node *btrn = ct->btrn;
100 case CL_SENT_CH_RESPONSE:
101 case CL_SENT_COMMAND:
102 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
105 case CL_RECEIVED_WELCOME:
106 case CL_RECEIVED_PROCEED:
107 case CL_RECEIVED_CHALLENGE:
108 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
112 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
117 para_fd_set(ct->scc.fd, &s->rfds,
122 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
127 para_fd_set(ct->scc.fd, &s->wfds,
134 static int client_recv_buffer(struct client_task *ct, fd_set *rfds,
135 char *buf, size_t sz, size_t *n)
139 if (ct->status < CL_SENT_CH_RESPONSE)
140 return read_nonblock(ct->scc.fd, buf, sz, rfds, n);
143 ret = sc_recv_buffer(&ct->scc, buf, sz);
145 * sc_recv_buffer is used with blocking fds elsewhere, so it
146 * does not use the nonblock-API. Therefore we need to
147 * check for EOF and EAGAIN.
150 return -E_SERVER_EOF;
151 if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
159 static int send_sb(struct client_task *ct, void *buf, size_t numbytes,
160 enum sb_designator band, bool dont_free)
162 int ret, fd = ct->scc.fd;
166 struct sb_buffer sbb;
167 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
169 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
170 ct->sbc = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
172 ret = sb_get_send_buffers(ct->sbc, iov);
173 ret = xwritev(fd, iov, ret);
179 if (sb_sent(ct->sbc, ret)) {
186 static int recv_sb(struct client_task *ct, fd_set *rfds,
187 struct sb_buffer *result)
191 sb_transformation trafo;
195 if (!FD_ISSET(ct->scc.fd, rfds))
197 if (ct->status < CL_SENT_CH_RESPONSE)
198 trafo = trafo_context = NULL;
201 trafo_context = ct->scc.recv;
204 ct->sbc = sb_new_recv(0, trafo, trafo_context);
206 sb_get_recv_buffer(ct->sbc, &iov);
207 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
215 if (!sb_received(ct->sbc, n, result))
222 static char **parse_features(char *buf)
225 const char id[] = "\nFeatures: ";
226 char *p, *q, **features;
236 create_argv(p, ",", &features);
237 for (i = 0; features[i]; i++)
238 PARA_INFO_LOG("server feature: %s\n", features[i]);
242 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
245 const char *designator[] = {SB_DESIGNATORS_ARRAY};
249 if (sbb->band < NUM_SB_DESIGNATORS)
250 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
254 if (iov_valid(&sbb->iov))
255 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
262 case SBD_WARNING_LOG:
266 if (iov_valid(&sbb->iov)) {
267 int ll = sbb->band - SBD_DEBUG_LOG;
268 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
272 case SBD_EXIT__SUCCESS:
273 ret = -E_SERVER_CMD_SUCCESS;
275 case SBD_EXIT__FAILURE:
276 ret = -E_SERVER_CMD_FAILURE;
279 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
284 free(sbb->iov.iov_base);
286 sbb->iov.iov_base = NULL;
290 static bool has_feature(const char *feature, struct client_task *ct)
292 return find_arg(feature, ct->features) >= 0? true : false;
295 static int send_sb_command(struct client_task *ct)
302 return send_sb(ct, NULL, 0, 0, false);
304 for (i = 0; i < ct->conf.inputs_num; i++)
305 len += strlen(ct->conf.inputs[i]) + 1;
306 p = command = para_malloc(len);
307 for (i = 0; i < ct->conf.inputs_num; i++) {
308 strcpy(p, ct->conf.inputs[i]);
309 p += strlen(ct->conf.inputs[i]) + 1;
311 PARA_DEBUG_LOG("--> %s\n", command);
312 return send_sb(ct, command, len, SBD_COMMAND, false);
316 * The post select hook for client commands.
318 * \param s Pointer to the scheduler.
319 * \param t Pointer to the task struct for this command.
321 * Depending on the current state of the connection and the status of the read
322 * and write fd sets of \a s, this function performs the necessary steps to
323 * authenticate the connection, to send the command given by \a t->private_data
324 * and to receive para_server's output, if any.
326 * \sa struct sched, struct task.
328 static void client_post_select(struct sched *s, struct task *t)
330 struct client_task *ct = container_of(t, struct client_task, task);
331 struct btr_node *btrn = ct->btrn;
334 char buf[CLIENT_BUFSIZE];
339 switch (ct->status) {
340 case CL_CONNECTED: /* receive welcome message */
341 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
342 if (ret < 0 || n == 0)
344 ct->features = parse_features(buf);
345 ct->status = CL_RECEIVED_WELCOME;
347 case CL_RECEIVED_WELCOME: /* send auth command */
348 if (!FD_ISSET(ct->scc.fd, &s->wfds))
350 if (has_feature("sideband", ct)) {
351 ct->use_sideband = true;
352 sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user);
354 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
355 PARA_INFO_LOG("--> %s\n", buf);
356 ret = write_buffer(ct->scc.fd, buf);
359 ct->status = CL_SENT_AUTH;
363 * Receive challenge and session keys, decrypt the challenge and
364 * send back the hash of the decrypted challenge.
367 /* decrypted challenge/session key buffer */
368 unsigned char crypt_buf[1024];
369 /* the SHA1 of the decrypted challenge */
371 if (ct->use_sideband) {
372 struct sb_buffer sbb;
373 ret = recv_sb(ct, &s->rfds, &sbb);
376 if (sbb.band != SBD_CHALLENGE) {
378 free(sbb.iov.iov_base);
382 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
383 ret = priv_decrypt(ct->key_file, crypt_buf,
384 sbb.iov.iov_base, n);
385 free(sbb.iov.iov_base);
389 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
390 if (ret < 0 || n == 0)
392 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
393 ret = priv_decrypt(ct->key_file, crypt_buf,
394 (unsigned char *)buf, n);
398 ct->challenge_hash = para_malloc(HASH_SIZE);
399 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
400 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
401 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
403 hash_to_asc(ct->challenge_hash, buf);
404 PARA_INFO_LOG("--> %s\n", buf);
405 ct->status = CL_RECEIVED_CHALLENGE;
408 case CL_RECEIVED_CHALLENGE:
409 if (ct->use_sideband) {
410 ret = send_sb(ct, ct->challenge_hash, HASH_SIZE,
411 SBD_CHALLENGE_RESPONSE, false);
413 ct->challenge_hash = NULL;
417 ret = write_all(ct->scc.fd, (char *)ct->challenge_hash, HASH_SIZE);
421 ct->status = CL_SENT_CH_RESPONSE;
423 case CL_SENT_CH_RESPONSE: /* read server response */
425 if (ct->use_sideband) {
426 struct sb_buffer sbb;
427 ret = recv_sb(ct, &s->rfds, &sbb);
430 free(sbb.iov.iov_base);
431 if (sbb.band != SBD_PROCEED)
434 ct->status = CL_RECEIVED_PROCEED;
437 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
438 if (ret < 0 || n == 0)
440 /* check if server has sent "Proceed" message */
441 ret = -E_CLIENT_AUTH;
442 if (n < PROCEED_MSG_LEN)
444 if (!strstr(buf, PROCEED_MSG))
446 ct->status = CL_RECEIVED_PROCEED;
449 case CL_RECEIVED_PROCEED: /* concat args and send command */
452 char *command = NULL;
453 if (!FD_ISSET(ct->scc.fd, &s->wfds))
455 if (ct->use_sideband) {
456 ret = send_sb_command(ct);
459 ct->status = CL_SENT_COMMAND;
462 for (i = 0; i < ct->conf.inputs_num; i++) {
464 command = make_message("%s\n%s", command?
465 command : "", ct->conf.inputs[i]);
468 command = para_strcat(command, EOC_MSG "\n");
469 PARA_DEBUG_LOG("--> %s\n", command);
470 ret = sc_send_buffer(&ct->scc, command);
474 ct->status = CL_SENT_COMMAND;
477 case CL_SENT_COMMAND:
480 if (ct->use_sideband) {
481 struct sb_buffer sbb;
482 ret = recv_sb(ct, &s->rfds, &sbb);
485 if (sbb.band == SBD_AWAITING_DATA) {
486 ct->status = CL_SENDING;
487 free(sbb.iov.iov_base);
490 ct->status = CL_RECEIVING;
491 ret = dispatch_sbb(ct, &sbb);
494 /* can not use "buf" here because we need a malloced buffer */
495 buf2 = para_malloc(CLIENT_BUFSIZE);
496 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
498 if (strstr(buf2, AWAITING_DATA_MSG)) {
500 ct->status = CL_SENDING;
503 ct->status = CL_RECEIVING;
504 btr_add_output(buf2, n, btrn);
513 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
518 if (!FD_ISSET(ct->scc.fd, &s->wfds))
520 sz = btr_next_buffer(btrn, &buf2);
521 ret = sc_send_bin_buffer(&ct->scc, buf2, sz);
524 btr_consume(btrn, sz);
530 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
536 * The FD_ISSET() is not strictly necessary, but is allows us
537 * to skip the malloc below if there is nothing to read anyway.
539 if (!FD_ISSET(ct->scc.fd, &s->rfds))
541 if (ct->use_sideband) {
542 struct sb_buffer sbb;
543 ret = recv_sb(ct, &s->rfds, &sbb);
545 ret = dispatch_sbb(ct, &sbb);
548 buf2 = para_malloc(CLIENT_BUFSIZE);
549 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
551 buf2 = para_realloc(buf2, n);
552 btr_add_output(buf2, n, btrn);
561 if (!ct->use_sideband && ret != -E_SERVER_EOF &&
562 ret != -E_BTR_EOF && ret != -E_EOF)
563 PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
564 btr_remove_node(&ct->btrn);
569 * Connect to para_server and register the client task.
571 * \param ct The initialized client task structure.
572 * \param s The scheduler instance to register the client task to.
573 * \param parent The parent node of the client btr node.
574 * \param child The child node of the client node.
576 * The client task structure given by \a ct must be allocated and initialized
577 * by \ref client_parse_config() before this function is called.
581 int client_connect(struct client_task *ct, struct sched *s,
582 struct btr_node *parent, struct btr_node *child)
586 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
587 ct->conf.server_port_arg);
589 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
590 ct->conf.server_port_arg);
594 ret = mark_fd_nonblocking(ct->scc.fd);
597 ct->status = CL_CONNECTED;
598 ct->btrn = btr_new_node(&(struct btr_node_description)
599 EMBRACE(.name = "client", .parent = parent, .child = child));
600 ct->task.pre_select = client_pre_select;
601 ct->task.post_select = client_post_select;
603 sprintf(ct->task.status, "client");
604 register_task(s, &ct->task);
613 * Parse a client configuration.
615 * \param argc Usual argument count.
616 * \param argv Usual argument vector.
617 * \param ct_ptr Filled in by this function.
618 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
620 * This checks the command line options given by \a argc and \a argv, sets
621 * default values for the user name and the name of the rsa key file and reads
622 * further options from the config file.
624 * Upon successful return, \a ct_ptr points to a dynamically allocated and
625 * initialized client task struct.
627 * \return The number of non-option arguments in \a argc/argv on success,
628 * negative on errors.
630 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
633 char *home = para_homedir();
635 struct client_task *ct = para_calloc(sizeof(struct client_task));
639 ret = -E_CLIENT_SYNTAX;
640 if (client_cmdline_parser(argc, argv, &ct->conf))
642 HANDLE_VERSION_FLAG("client", ct->conf);
644 ct->config_file = ct->conf.config_file_given?
645 para_strdup(ct->conf.config_file_arg) :
646 make_message("%s/.paraslash/client.conf", home);
647 ret = file_exists(ct->config_file);
648 if (!ret && ct->conf.config_file_given) {
653 struct client_cmdline_parser_params params = {
657 .check_ambiguity = 0,
661 if (client_cmdline_parser_config_file(ct->config_file,
665 ct->user = ct->conf.user_given?
666 para_strdup(ct->conf.user_arg) : para_logname();
668 if (ct->conf.key_file_given)
669 ct->key_file = para_strdup(ct->conf.key_file_arg);
671 ct->key_file = make_message("%s/.paraslash/key.%s",
673 if (!file_exists(ct->key_file)) {
675 ct->key_file = make_message("%s/.ssh/id_rsa", home);
680 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
681 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
682 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
683 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
684 ret = ct->conf.inputs_num;
688 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
696 * Parse the client configuration and open a connection to para_server.
698 * \param argc See \ref client_parse_config.
699 * \param argv See \ref client_parse_config.
700 * \param ct_ptr See \ref client_parse_config.
701 * \param loglevel See \ref client_parse_config.
702 * \param parent See \ref client_connect().
703 * \param child See \ref client_connect().
704 * \param sched See \ref client_connect().
706 * This function combines client_parse_config() and client_connect(). It is
707 * considered a syntax error if no command was given, i.e. if the number
708 * of non-option arguments is zero.
712 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
713 int *loglevel, struct btr_node *parent, struct btr_node *child,
716 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
721 ret = -E_CLIENT_SYNTAX;
724 ret = client_connect(*ct_ptr, sched, parent, child);
729 client_close(*ct_ptr);