2 * Copyright (C) 1997-2014 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. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
26 #include "client.cmdline.h"
28 #include "buffer_tree.h"
32 /** The size of the receiving buffer. */
33 #define CLIENT_BUFSIZE 4000
36 * Close the connection to para_server and deallocate per-command resources.
38 * \param ct The client task.
40 * This frees all resources of the current command but keeps the configuration
43 * \sa \ref client_close().
45 void client_disconnect(struct client_task *ct)
51 free_argv(ct->features);
53 sc_free(ct->scc.recv);
55 sc_free(ct->scc.send);
57 btr_remove_node(&ct->btrn[0]);
58 btr_remove_node(&ct->btrn[1]);
62 * Close the connection to para_server and free all resources.
64 * \param ct Pointer to the client data.
66 * \sa \ref client_open(), \ref client_disconnect().
68 void client_close(struct client_task *ct)
72 client_disconnect(ct);
74 free(ct->config_file);
76 client_cmdline_parser_free(&ct->conf);
77 free(ct->challenge_hash);
84 * The preselect hook for server commands.
86 * \param s Pointer to the scheduler.
87 * \param t Pointer to the task struct for this command.
89 * The task pointer must contain a pointer to the initialized client data
90 * structure as it is returned by client_open().
92 * This function checks the state of the connection and adds the file descriptor
93 * of the connection to the read or write fd set of \a s accordingly.
95 * \sa register_task() client_open(), struct sched, struct task.
97 static void client_pre_select(struct sched *s, struct task *t)
100 struct client_task *ct = container_of(t, struct client_task, task);
104 switch (ct->status) {
107 case CL_SENT_CH_RESPONSE:
108 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
111 case CL_RECEIVED_WELCOME:
112 case CL_RECEIVED_PROCEED:
113 case CL_RECEIVED_CHALLENGE:
114 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
119 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
123 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
128 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
132 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
138 static int send_sb(struct client_task *ct, int channel, void *buf, size_t numbytes,
139 enum sb_designator band, bool dont_free)
141 int ret, fd = ct->scc.fd;
144 if (!ct->sbc[channel]) {
145 struct sb_buffer sbb;
146 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
148 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
149 ct->sbc[channel] = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
151 ret = sb_get_send_buffers(ct->sbc[channel], iov);
152 ret = xwritev(fd, iov, ret);
154 sb_free(ct->sbc[channel]);
155 ct->sbc[channel] = NULL;
158 if (sb_sent(ct->sbc[channel], ret)) {
159 ct->sbc[channel] = NULL;
165 static int recv_sb(struct client_task *ct, fd_set *rfds,
166 struct sb_buffer *result)
170 sb_transformation trafo;
174 if (!FD_ISSET(ct->scc.fd, rfds))
176 if (ct->status < CL_SENT_CH_RESPONSE)
177 trafo = trafo_context = NULL;
180 trafo_context = ct->scc.recv;
183 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
185 sb_get_recv_buffer(ct->sbc[0], &iov);
186 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
194 if (!sb_received(ct->sbc[0], n, result))
201 static char **parse_features(char *buf)
204 const char id[] = "\nFeatures: ";
205 char *p, *q, **features;
215 create_argv(p, ",", &features);
216 for (i = 0; features[i]; i++)
217 PARA_INFO_LOG("server feature: %s\n", features[i]);
221 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
224 const char *designator[] = {SB_DESIGNATORS_ARRAY};
228 if (sbb->band < NUM_SB_DESIGNATORS)
229 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
232 case SBD_AWAITING_DATA:
233 ct->status = CL_SENDING;
237 if (iov_valid(&sbb->iov))
238 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
245 case SBD_WARNING_LOG:
249 if (iov_valid(&sbb->iov)) {
250 int ll = sbb->band - SBD_DEBUG_LOG;
251 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
255 case SBD_EXIT__SUCCESS:
256 ret = -E_SERVER_CMD_SUCCESS;
258 case SBD_EXIT__FAILURE:
259 ret = -E_SERVER_CMD_FAILURE;
262 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
267 free(sbb->iov.iov_base);
269 sbb->iov.iov_base = NULL;
273 static bool has_feature(const char *feature, struct client_task *ct)
275 return find_arg(feature, ct->features) >= 0? true : false;
278 static int send_sb_command(struct client_task *ct)
285 return send_sb(ct, 0, NULL, 0, 0, false);
287 for (i = 0; i < ct->conf.inputs_num; i++)
288 len += strlen(ct->conf.inputs[i]) + 1;
289 p = command = para_malloc(len);
290 for (i = 0; i < ct->conf.inputs_num; i++) {
291 strcpy(p, ct->conf.inputs[i]);
292 p += strlen(ct->conf.inputs[i]) + 1;
294 PARA_DEBUG_LOG("--> %s\n", command);
295 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
299 * The post select hook for client commands.
301 * \param s Pointer to the scheduler.
302 * \param t Pointer to the task struct for this command.
304 * Depending on the current state of the connection and the status of the read
305 * and write fd sets of \a s, this function performs the necessary steps to
306 * authenticate the connection, to send the command given by \a t->private_data
307 * and to receive para_server's output, if any.
309 * \sa struct sched, struct task.
311 static int client_post_select(struct sched *s, struct task *t)
313 struct client_task *ct = container_of(t, struct client_task, task);
316 char buf[CLIENT_BUFSIZE];
318 ret = task_get_notification(t);
323 switch (ct->status) {
324 case CL_CONNECTED: /* receive welcome message */
325 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
326 if (ret < 0 || n == 0)
328 ct->features = parse_features(buf);
329 if (!has_feature("sideband", ct)) {
330 PARA_ERROR_LOG("server has no sideband support\n");
331 ret = -E_INCOMPAT_FEAT;
334 ct->status = CL_RECEIVED_WELCOME;
336 case CL_RECEIVED_WELCOME: /* send auth command */
337 if (!FD_ISSET(ct->scc.fd, &s->wfds))
339 sprintf(buf, AUTH_REQUEST_MSG "%s sideband%s", ct->user,
340 has_feature("aes_ctr128", ct)? ",aes_ctr128" : "");
341 PARA_INFO_LOG("--> %s\n", buf);
342 ret = write_buffer(ct->scc.fd, buf);
345 ct->status = CL_SENT_AUTH;
349 * Receive challenge and session keys, decrypt the challenge and
350 * send back the hash of the decrypted challenge.
353 /* decrypted challenge/session key buffer */
354 unsigned char crypt_buf[1024];
355 struct sb_buffer sbb;
358 ret = recv_sb(ct, &s->rfds, &sbb);
361 if (sbb.band != SBD_CHALLENGE) {
363 free(sbb.iov.iov_base);
367 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
368 ret = priv_decrypt(ct->key_file, crypt_buf,
369 sbb.iov.iov_base, n);
370 free(sbb.iov.iov_base);
373 ct->challenge_hash = para_malloc(HASH_SIZE);
374 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
375 use_aes = has_feature("aes_ctr128", ct);
376 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN, use_aes);
377 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
378 SESSION_KEY_LEN, use_aes);
379 hash_to_asc(ct->challenge_hash, buf);
380 PARA_INFO_LOG("--> %s\n", buf);
381 ct->status = CL_RECEIVED_CHALLENGE;
384 case CL_RECEIVED_CHALLENGE:
385 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
386 SBD_CHALLENGE_RESPONSE, false);
388 ct->challenge_hash = NULL;
391 ct->status = CL_SENT_CH_RESPONSE;
393 case CL_SENT_CH_RESPONSE: /* read server response */
395 struct sb_buffer sbb;
396 ret = recv_sb(ct, &s->rfds, &sbb);
399 free(sbb.iov.iov_base);
400 if (sbb.band != SBD_PROCEED)
403 ct->status = CL_RECEIVED_PROCEED;
406 case CL_RECEIVED_PROCEED: /* concat args and send command */
408 if (!FD_ISSET(ct->scc.fd, &s->wfds))
410 ret = send_sb_command(ct);
413 ct->status = CL_EXECUTING;
420 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
421 if (ret == -E_BTR_EOF) {
422 /* empty blob data packet indicates EOF */
423 PARA_INFO_LOG("blob sent\n");
424 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
430 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
431 sz = btr_next_buffer(ct->btrn[1], &buf2);
433 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
437 btr_consume(ct->btrn[1], sz);
443 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
446 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
447 struct sb_buffer sbb;
448 ret = recv_sb(ct, &s->rfds, &sbb);
452 ret = dispatch_sbb(ct, &sbb);
462 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
463 btr_remove_node(&ct->btrn[1]);
468 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
469 btr_remove_node(&ct->btrn[0]);
470 if (ct->btrn[1] && ct->status == CL_SENDING)
475 btr_remove_node(&ct->btrn[0]);
476 btr_remove_node(&ct->btrn[1]);
477 if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
478 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
483 * Connect to para_server and register the client task.
485 * \param ct The initialized client task structure.
486 * \param s The scheduler instance to register the client task to.
487 * \param parent The parent node of the client btr node.
488 * \param child The child node of the client node.
490 * The client task structure given by \a ct must be allocated and initialized
491 * by \ref client_parse_config() before this function is called.
495 int client_connect(struct client_task *ct, struct sched *s,
496 struct btr_node *parent, struct btr_node *child)
500 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
501 ct->conf.server_port_arg);
503 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
504 ct->conf.server_port_arg);
508 ret = mark_fd_nonblocking(ct->scc.fd);
511 ct->status = CL_CONNECTED;
512 ct->btrn[0] = btr_new_node(&(struct btr_node_description)
513 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
514 ct->btrn[1] = btr_new_node(&(struct btr_node_description)
515 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
516 ct->task.pre_select = client_pre_select;
517 ct->task.post_select = client_post_select;
519 sprintf(ct->task.status, "client");
520 register_task(s, &ct->task);
528 __noreturn static void print_help_and_die(struct client_task *ct)
530 struct ggo_help h = DEFINE_GGO_HELP(client);
531 bool d = ct->conf.detailed_help_given;
533 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
538 * Parse a client configuration.
540 * \param argc Usual argument count.
541 * \param argv Usual argument vector.
542 * \param ct_ptr Filled in by this function.
543 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
545 * This checks the command line options given by \a argc and \a argv, sets
546 * default values for the user name and the name of the rsa key file and reads
547 * further options from the config file.
549 * Upon successful return, \a ct_ptr points to a dynamically allocated and
550 * initialized client task struct.
552 * \return The number of non-option arguments in \a argc/argv on success,
553 * negative on errors.
555 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
558 char *home = para_homedir();
560 struct client_task *ct = para_calloc(sizeof(struct client_task));
564 ret = -E_CLIENT_SYNTAX;
565 if (client_cmdline_parser(argc, argv, &ct->conf))
567 version_handle_flag("client", ct->conf.version_given);
568 if (ct->conf.help_given || ct->conf.detailed_help_given)
569 print_help_and_die(ct);
571 ct->config_file = ct->conf.config_file_given?
572 para_strdup(ct->conf.config_file_arg) :
573 make_message("%s/.paraslash/client.conf", home);
574 ret = file_exists(ct->config_file);
575 if (!ret && ct->conf.config_file_given) {
580 struct client_cmdline_parser_params params = {
584 .check_ambiguity = 0,
588 if (client_cmdline_parser_config_file(ct->config_file,
592 ct->user = ct->conf.user_given?
593 para_strdup(ct->conf.user_arg) : para_logname();
595 if (ct->conf.key_file_given)
596 ct->key_file = para_strdup(ct->conf.key_file_arg);
598 ct->key_file = make_message("%s/.paraslash/key.%s",
600 if (!file_exists(ct->key_file)) {
602 ct->key_file = make_message("%s/.ssh/id_rsa", home);
607 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
608 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
609 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
610 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
611 ret = ct->conf.inputs_num;
615 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
623 * Parse the client configuration and open a connection to para_server.
625 * \param argc See \ref client_parse_config.
626 * \param argv See \ref client_parse_config.
627 * \param ct_ptr See \ref client_parse_config.
628 * \param loglevel See \ref client_parse_config.
629 * \param parent See \ref client_connect().
630 * \param child See \ref client_connect().
631 * \param sched See \ref client_connect().
633 * This function combines client_parse_config() and client_connect(). It is
634 * considered a syntax error if no command was given, i.e. if the number
635 * of non-option arguments is zero.
639 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
640 int *loglevel, struct btr_node *parent, struct btr_node *child,
643 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
648 ret = -E_CLIENT_SYNTAX;
651 ret = client_connect(*ct_ptr, sched, parent, child);
656 client_close(*ct_ptr);