2 * Copyright (C) 1997-2013 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>
21 #include "client.cmdline.h"
23 #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 resources.
33 * \param ct The client task.
35 * This frees all resources 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);
48 sc_free(ct->scc.recv);
50 sc_free(ct->scc.send);
52 btr_remove_node(&ct->btrn[0]);
53 btr_remove_node(&ct->btrn[1]);
57 * Close the connection to para_server and free all resources.
59 * \param ct Pointer to the client data.
61 * \sa \ref client_open(), \ref client_disconnect().
63 void client_close(struct client_task *ct)
67 client_disconnect(ct);
69 free(ct->config_file);
71 client_cmdline_parser_free(&ct->conf);
72 free(ct->challenge_hash);
79 * The preselect hook for server commands.
81 * \param s Pointer to the scheduler.
82 * \param t Pointer to the task struct for this command.
84 * The task pointer must contain a pointer to the initialized client data
85 * structure as it is returned by client_open().
87 * This function checks the state of the connection and adds the file descriptor
88 * of the connection to the read or write fd set of \a s accordingly.
90 * \sa register_task() client_open(), struct sched, struct task.
92 static void client_pre_select(struct sched *s, struct task *t)
95 struct client_task *ct = container_of(t, struct client_task, task);
102 case CL_SENT_CH_RESPONSE:
103 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
106 case CL_RECEIVED_WELCOME:
107 case CL_RECEIVED_PROCEED:
108 case CL_RECEIVED_CHALLENGE:
109 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
114 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
118 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
123 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
127 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
133 static int send_sb(struct client_task *ct, int channel, void *buf, size_t numbytes,
134 enum sb_designator band, bool dont_free)
136 int ret, fd = ct->scc.fd;
139 if (!ct->sbc[channel]) {
140 struct sb_buffer sbb;
141 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
143 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
144 ct->sbc[channel] = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
146 ret = sb_get_send_buffers(ct->sbc[channel], iov);
147 ret = xwritev(fd, iov, ret);
149 sb_free(ct->sbc[channel]);
150 ct->sbc[channel] = NULL;
153 if (sb_sent(ct->sbc[channel], ret)) {
154 ct->sbc[channel] = NULL;
160 static int recv_sb(struct client_task *ct, fd_set *rfds,
161 struct sb_buffer *result)
165 sb_transformation trafo;
169 if (!FD_ISSET(ct->scc.fd, rfds))
171 if (ct->status < CL_SENT_CH_RESPONSE)
172 trafo = trafo_context = NULL;
175 trafo_context = ct->scc.recv;
178 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
180 sb_get_recv_buffer(ct->sbc[0], &iov);
181 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
189 if (!sb_received(ct->sbc[0], n, result))
196 static char **parse_features(char *buf)
199 const char id[] = "\nFeatures: ";
200 char *p, *q, **features;
210 create_argv(p, ",", &features);
211 for (i = 0; features[i]; i++)
212 PARA_INFO_LOG("server feature: %s\n", features[i]);
216 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
219 const char *designator[] = {SB_DESIGNATORS_ARRAY};
223 if (sbb->band < NUM_SB_DESIGNATORS)
224 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
227 case SBD_AWAITING_DATA:
228 ct->status = CL_SENDING;
232 if (iov_valid(&sbb->iov))
233 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
240 case SBD_WARNING_LOG:
244 if (iov_valid(&sbb->iov)) {
245 int ll = sbb->band - SBD_DEBUG_LOG;
246 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
250 case SBD_EXIT__SUCCESS:
251 ret = -E_SERVER_CMD_SUCCESS;
253 case SBD_EXIT__FAILURE:
254 ret = -E_SERVER_CMD_FAILURE;
257 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
262 free(sbb->iov.iov_base);
264 sbb->iov.iov_base = NULL;
268 static bool has_feature(const char *feature, struct client_task *ct)
270 return find_arg(feature, ct->features) >= 0? true : false;
273 static int send_sb_command(struct client_task *ct)
280 return send_sb(ct, 0, NULL, 0, 0, false);
282 for (i = 0; i < ct->conf.inputs_num; i++)
283 len += strlen(ct->conf.inputs[i]) + 1;
284 p = command = para_malloc(len);
285 for (i = 0; i < ct->conf.inputs_num; i++) {
286 strcpy(p, ct->conf.inputs[i]);
287 p += strlen(ct->conf.inputs[i]) + 1;
289 PARA_DEBUG_LOG("--> %s\n", command);
290 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
294 * The post select hook for client commands.
296 * \param s Pointer to the scheduler.
297 * \param t Pointer to the task struct for this command.
299 * Depending on the current state of the connection and the status of the read
300 * and write fd sets of \a s, this function performs the necessary steps to
301 * authenticate the connection, to send the command given by \a t->private_data
302 * and to receive para_server's output, if any.
304 * \sa struct sched, struct task.
306 static int client_post_select(struct sched *s, struct task *t)
308 struct client_task *ct = container_of(t, struct client_task, task);
311 char buf[CLIENT_BUFSIZE];
313 ret = task_get_notification(t);
318 switch (ct->status) {
319 case CL_CONNECTED: /* receive welcome message */
320 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
321 if (ret < 0 || n == 0)
323 ct->features = parse_features(buf);
324 if (!has_feature("sideband", ct)) {
325 PARA_ERROR_LOG("server has no sideband support\n");
326 ret = -E_INCOMPAT_FEAT;
329 ct->status = CL_RECEIVED_WELCOME;
331 case CL_RECEIVED_WELCOME: /* send auth command */
332 if (!FD_ISSET(ct->scc.fd, &s->wfds))
334 sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user);
335 PARA_INFO_LOG("--> %s\n", buf);
336 ret = write_buffer(ct->scc.fd, buf);
339 ct->status = CL_SENT_AUTH;
343 * Receive challenge and session keys, decrypt the challenge and
344 * send back the hash of the decrypted challenge.
347 /* decrypted challenge/session key buffer */
348 unsigned char crypt_buf[1024];
349 struct sb_buffer sbb;
351 ret = recv_sb(ct, &s->rfds, &sbb);
354 if (sbb.band != SBD_CHALLENGE) {
356 free(sbb.iov.iov_base);
360 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
361 ret = priv_decrypt(ct->key_file, crypt_buf,
362 sbb.iov.iov_base, n);
363 free(sbb.iov.iov_base);
366 ct->challenge_hash = para_malloc(HASH_SIZE);
367 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
368 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
369 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
371 hash_to_asc(ct->challenge_hash, buf);
372 PARA_INFO_LOG("--> %s\n", buf);
373 ct->status = CL_RECEIVED_CHALLENGE;
376 case CL_RECEIVED_CHALLENGE:
377 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
378 SBD_CHALLENGE_RESPONSE, false);
380 ct->challenge_hash = NULL;
383 ct->status = CL_SENT_CH_RESPONSE;
385 case CL_SENT_CH_RESPONSE: /* read server response */
387 struct sb_buffer sbb;
388 ret = recv_sb(ct, &s->rfds, &sbb);
391 free(sbb.iov.iov_base);
392 if (sbb.band != SBD_PROCEED)
395 ct->status = CL_RECEIVED_PROCEED;
398 case CL_RECEIVED_PROCEED: /* concat args and send command */
400 if (!FD_ISSET(ct->scc.fd, &s->wfds))
402 ret = send_sb_command(ct);
405 ct->status = CL_EXECUTING;
412 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
413 if (ret == -E_BTR_EOF) {
414 /* empty blob data packet indicates EOF */
415 PARA_INFO_LOG("blob sent\n");
416 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
422 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
423 sz = btr_next_buffer(ct->btrn[1], &buf2);
425 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
429 btr_consume(ct->btrn[1], sz);
435 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
438 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
439 struct sb_buffer sbb;
440 ret = recv_sb(ct, &s->rfds, &sbb);
444 ret = dispatch_sbb(ct, &sbb);
454 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
455 btr_remove_node(&ct->btrn[1]);
460 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
461 btr_remove_node(&ct->btrn[0]);
462 if (ct->btrn[1] && ct->status == CL_SENDING)
467 btr_remove_node(&ct->btrn[0]);
468 btr_remove_node(&ct->btrn[1]);
469 if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
470 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
475 * Connect to para_server and register the client task.
477 * \param ct The initialized client task structure.
478 * \param s The scheduler instance to register the client task to.
479 * \param parent The parent node of the client btr node.
480 * \param child The child node of the client node.
482 * The client task structure given by \a ct must be allocated and initialized
483 * by \ref client_parse_config() before this function is called.
487 int client_connect(struct client_task *ct, struct sched *s,
488 struct btr_node *parent, struct btr_node *child)
492 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
493 ct->conf.server_port_arg);
495 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
496 ct->conf.server_port_arg);
500 ret = mark_fd_nonblocking(ct->scc.fd);
503 ct->status = CL_CONNECTED;
504 ct->btrn[0] = btr_new_node(&(struct btr_node_description)
505 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
506 ct->btrn[1] = btr_new_node(&(struct btr_node_description)
507 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
508 ct->task.pre_select = client_pre_select;
509 ct->task.post_select = client_post_select;
511 sprintf(ct->task.status, "client");
512 register_task(s, &ct->task);
520 __noreturn static void print_help_and_die(struct client_task *ct)
522 struct ggo_help h = DEFINE_GGO_HELP(client);
523 bool d = ct->conf.detailed_help_given;
525 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
530 * Parse a client configuration.
532 * \param argc Usual argument count.
533 * \param argv Usual argument vector.
534 * \param ct_ptr Filled in by this function.
535 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
537 * This checks the command line options given by \a argc and \a argv, sets
538 * default values for the user name and the name of the rsa key file and reads
539 * further options from the config file.
541 * Upon successful return, \a ct_ptr points to a dynamically allocated and
542 * initialized client task struct.
544 * \return The number of non-option arguments in \a argc/argv on success,
545 * negative on errors.
547 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
550 char *home = para_homedir();
552 struct client_task *ct = para_calloc(sizeof(struct client_task));
556 ret = -E_CLIENT_SYNTAX;
557 if (client_cmdline_parser(argc, argv, &ct->conf))
559 version_handle_flag("client", ct->conf.version_given);
560 if (ct->conf.help_given || ct->conf.detailed_help_given)
561 print_help_and_die(ct);
563 ct->config_file = ct->conf.config_file_given?
564 para_strdup(ct->conf.config_file_arg) :
565 make_message("%s/.paraslash/client.conf", home);
566 ret = file_exists(ct->config_file);
567 if (!ret && ct->conf.config_file_given) {
572 struct client_cmdline_parser_params params = {
576 .check_ambiguity = 0,
580 if (client_cmdline_parser_config_file(ct->config_file,
584 ct->user = ct->conf.user_given?
585 para_strdup(ct->conf.user_arg) : para_logname();
587 if (ct->conf.key_file_given)
588 ct->key_file = para_strdup(ct->conf.key_file_arg);
590 ct->key_file = make_message("%s/.paraslash/key.%s",
592 if (!file_exists(ct->key_file)) {
594 ct->key_file = make_message("%s/.ssh/id_rsa", home);
599 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
600 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
601 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
602 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
603 ret = ct->conf.inputs_num;
607 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
615 * Parse the client configuration and open a connection to para_server.
617 * \param argc See \ref client_parse_config.
618 * \param argv See \ref client_parse_config.
619 * \param ct_ptr See \ref client_parse_config.
620 * \param loglevel See \ref client_parse_config.
621 * \param parent See \ref client_connect().
622 * \param child See \ref client_connect().
623 * \param sched See \ref client_connect().
625 * This function combines client_parse_config() and client_connect(). It is
626 * considered a syntax error if no command was given, i.e. if the number
627 * of non-option arguments is zero.
631 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
632 int *loglevel, struct btr_node *parent, struct btr_node *child,
635 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
640 ret = -E_CLIENT_SYNTAX;
643 ret = client_connect(*ct_ptr, sched, parent, child);
648 client_close(*ct_ptr);