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);
71 free(ct->challenge_hash);
77 * The preselect hook for server commands.
79 * \param s Pointer to the scheduler.
80 * \param t Pointer to the task struct for this command.
82 * The task pointer must contain a pointer to the initialized client data
83 * structure as it is returned by client_open().
85 * This function checks the state of the connection and adds the file descriptor
86 * of the connection to the read or write fd set of \a s accordingly.
88 * \sa register_task() client_open(), struct sched, struct task.
90 static void client_pre_select(struct sched *s, struct task *t)
93 struct client_task *ct = container_of(t, struct client_task, task);
94 struct btr_node *btrn = ct->btrn;
101 case CL_SENT_CH_RESPONSE:
102 case CL_SENT_COMMAND:
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);
113 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
118 para_fd_set(ct->scc.fd, &s->rfds,
123 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
128 para_fd_set(ct->scc.fd, &s->wfds,
135 static int client_recv_buffer(struct client_task *ct, fd_set *rfds,
136 char *buf, size_t sz, size_t *n)
140 if (ct->status < CL_SENT_CH_RESPONSE)
141 return read_nonblock(ct->scc.fd, buf, sz, rfds, n);
144 ret = sc_recv_buffer(&ct->scc, buf, sz);
146 * sc_recv_buffer is used with blocking fds elsewhere, so it
147 * does not use the nonblock-API. Therefore we need to
148 * check for EOF and EAGAIN.
151 return -E_SERVER_EOF;
152 if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
160 static int send_sb(struct client_task *ct, void *buf, size_t numbytes,
161 enum sb_designator band, bool dont_free)
163 int ret, fd = ct->scc.fd;
167 struct sb_buffer sbb;
168 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
170 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
171 ct->sbc = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
173 ret = sb_get_send_buffers(ct->sbc, iov);
174 ret = xwritev(fd, iov, ret);
180 if (sb_sent(ct->sbc, ret)) {
187 static int recv_sb(struct client_task *ct, fd_set *rfds,
188 struct sb_buffer *result)
192 sb_transformation trafo;
196 if (!FD_ISSET(ct->scc.fd, rfds))
198 if (ct->status < CL_SENT_CH_RESPONSE)
199 trafo = trafo_context = NULL;
202 trafo_context = ct->scc.recv;
205 ct->sbc = sb_new_recv(0, trafo, trafo_context);
207 sb_get_recv_buffer(ct->sbc, &iov);
208 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
216 if (!sb_received(ct->sbc, n, result))
223 static char **parse_features(char *buf)
226 const char id[] = "\nFeatures: ";
227 char *p, *q, **features;
237 create_argv(p, ",", &features);
238 for (i = 0; features[i]; i++)
239 PARA_INFO_LOG("server feature: %s\n", features[i]);
243 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
246 const char *designator[] = {SB_DESIGNATORS_ARRAY};
250 if (sbb->band < NUM_SB_DESIGNATORS)
251 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
255 if (iov_valid(&sbb->iov))
256 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
263 case SBD_WARNING_LOG:
267 if (iov_valid(&sbb->iov)) {
268 int ll = sbb->band - SBD_DEBUG_LOG;
269 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
273 case SBD_EXIT__SUCCESS:
274 ret = -E_SERVER_CMD_SUCCESS;
276 case SBD_EXIT__FAILURE:
277 ret = -E_SERVER_CMD_FAILURE;
280 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
285 free(sbb->iov.iov_base);
287 sbb->iov.iov_base = NULL;
291 static bool has_feature(const char *feature, struct client_task *ct)
293 return find_arg(feature, ct->features) >= 0? true : false;
296 static int send_sb_command(struct client_task *ct)
303 return send_sb(ct, NULL, 0, 0, false);
305 for (i = 0; i < ct->conf.inputs_num; i++)
306 len += strlen(ct->conf.inputs[i]) + 1;
307 p = command = para_malloc(len);
308 for (i = 0; i < ct->conf.inputs_num; i++) {
309 strcpy(p, ct->conf.inputs[i]);
310 p += strlen(ct->conf.inputs[i]) + 1;
312 PARA_DEBUG_LOG("--> %s\n", command);
313 return send_sb(ct, command, len, SBD_COMMAND, false);
317 * The post select hook for client commands.
319 * \param s Pointer to the scheduler.
320 * \param t Pointer to the task struct for this command.
322 * Depending on the current state of the connection and the status of the read
323 * and write fd sets of \a s, this function performs the necessary steps to
324 * authenticate the connection, to send the command given by \a t->private_data
325 * and to receive para_server's output, if any.
327 * \sa struct sched, struct task.
329 static void client_post_select(struct sched *s, struct task *t)
331 struct client_task *ct = container_of(t, struct client_task, task);
332 struct btr_node *btrn = ct->btrn;
335 char buf[CLIENT_BUFSIZE];
340 switch (ct->status) {
341 case CL_CONNECTED: /* receive welcome message */
342 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
343 if (ret < 0 || n == 0)
345 ct->features = parse_features(buf);
346 ct->status = CL_RECEIVED_WELCOME;
348 case CL_RECEIVED_WELCOME: /* send auth command */
349 if (!FD_ISSET(ct->scc.fd, &s->wfds))
351 if (has_feature("sideband", ct)) {
352 ct->use_sideband = true;
353 sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user);
355 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
356 PARA_INFO_LOG("--> %s\n", buf);
357 ret = write_buffer(ct->scc.fd, buf);
360 ct->status = CL_SENT_AUTH;
364 * Receive challenge and session keys, decrypt the challenge and
365 * send back the hash of the decrypted challenge.
368 /* decrypted challenge/session key buffer */
369 unsigned char crypt_buf[1024];
370 /* the SHA1 of the decrypted challenge */
372 if (ct->use_sideband) {
373 struct sb_buffer sbb;
374 ret = recv_sb(ct, &s->rfds, &sbb);
377 if (sbb.band != SBD_CHALLENGE) {
379 free(sbb.iov.iov_base);
383 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
384 ret = priv_decrypt(ct->key_file, crypt_buf,
385 sbb.iov.iov_base, n);
386 free(sbb.iov.iov_base);
390 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
391 if (ret < 0 || n == 0)
393 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
394 ret = priv_decrypt(ct->key_file, crypt_buf,
395 (unsigned char *)buf, n);
399 ct->challenge_hash = para_malloc(HASH_SIZE);
400 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
401 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
402 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
404 hash_to_asc(ct->challenge_hash, buf);
405 PARA_INFO_LOG("--> %s\n", buf);
406 ct->status = CL_RECEIVED_CHALLENGE;
409 case CL_RECEIVED_CHALLENGE:
410 if (ct->use_sideband) {
411 ret = send_sb(ct, ct->challenge_hash, HASH_SIZE,
412 SBD_CHALLENGE_RESPONSE, false);
414 ct->challenge_hash = NULL;
418 ret = write_all(ct->scc.fd, (char *)ct->challenge_hash, HASH_SIZE);
422 ct->status = CL_SENT_CH_RESPONSE;
424 case CL_SENT_CH_RESPONSE: /* read server response */
426 if (ct->use_sideband) {
427 struct sb_buffer sbb;
428 ret = recv_sb(ct, &s->rfds, &sbb);
431 free(sbb.iov.iov_base);
432 if (sbb.band != SBD_PROCEED)
435 ct->status = CL_RECEIVED_PROCEED;
438 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
439 if (ret < 0 || n == 0)
441 /* check if server has sent "Proceed" message */
442 ret = -E_CLIENT_AUTH;
443 if (n < PROCEED_MSG_LEN)
445 if (!strstr(buf, PROCEED_MSG))
447 ct->status = CL_RECEIVED_PROCEED;
450 case CL_RECEIVED_PROCEED: /* concat args and send command */
453 char *command = NULL;
454 if (!FD_ISSET(ct->scc.fd, &s->wfds))
456 if (ct->use_sideband) {
457 ret = send_sb_command(ct);
460 ct->status = CL_SENT_COMMAND;
463 for (i = 0; i < ct->conf.inputs_num; i++) {
465 command = make_message("%s\n%s", command?
466 command : "", ct->conf.inputs[i]);
469 command = para_strcat(command, EOC_MSG "\n");
470 PARA_DEBUG_LOG("--> %s\n", command);
471 ret = sc_send_buffer(&ct->scc, command);
475 ct->status = CL_SENT_COMMAND;
478 case CL_SENT_COMMAND:
481 if (ct->use_sideband) {
482 struct sb_buffer sbb;
483 ret = recv_sb(ct, &s->rfds, &sbb);
486 if (sbb.band == SBD_AWAITING_DATA) {
487 ct->status = CL_SENDING;
488 free(sbb.iov.iov_base);
491 ct->status = CL_RECEIVING;
492 ret = dispatch_sbb(ct, &sbb);
495 /* can not use "buf" here because we need a malloced buffer */
496 buf2 = para_malloc(CLIENT_BUFSIZE);
497 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
499 if (strstr(buf2, AWAITING_DATA_MSG)) {
501 ct->status = CL_SENDING;
504 ct->status = CL_RECEIVING;
505 btr_add_output(buf2, n, btrn);
514 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
519 if (!FD_ISSET(ct->scc.fd, &s->wfds))
521 sz = btr_next_buffer(btrn, &buf2);
522 ret = sc_send_bin_buffer(&ct->scc, buf2, sz);
525 btr_consume(btrn, sz);
531 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
537 * The FD_ISSET() is not strictly necessary, but is allows us
538 * to skip the malloc below if there is nothing to read anyway.
540 if (!FD_ISSET(ct->scc.fd, &s->rfds))
542 if (ct->use_sideband) {
543 struct sb_buffer sbb;
544 ret = recv_sb(ct, &s->rfds, &sbb);
546 ret = dispatch_sbb(ct, &sbb);
549 buf2 = para_malloc(CLIENT_BUFSIZE);
550 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
552 buf2 = para_realloc(buf2, n);
553 btr_add_output(buf2, n, btrn);
562 if (!ct->use_sideband && ret != -E_SERVER_EOF &&
563 ret != -E_BTR_EOF && ret != -E_EOF)
564 PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
565 btr_remove_node(btrn);
570 * Connect to para_server and register the client task.
572 * \param ct The initialized client task structure.
573 * \param s The scheduler instance to register the client task to.
574 * \param parent The parent node of the client btr node.
575 * \param child The child node of the client node.
577 * The client task structure given by \a ct must be allocated and initialized
578 * by \ref client_parse_config() before this function is called.
582 int client_connect(struct client_task *ct, struct sched *s,
583 struct btr_node *parent, struct btr_node *child)
587 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
588 ct->conf.server_port_arg);
590 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
591 ct->conf.server_port_arg);
595 ret = mark_fd_nonblocking(ct->scc.fd);
598 ct->status = CL_CONNECTED;
599 ct->btrn = btr_new_node(&(struct btr_node_description)
600 EMBRACE(.name = "client", .parent = parent, .child = child));
601 ct->task.pre_select = client_pre_select;
602 ct->task.post_select = client_post_select;
604 sprintf(ct->task.status, "client");
605 register_task(s, &ct->task);
614 * Parse a client configuration.
616 * \param argc Usual argument count.
617 * \param argv Usual argument vector.
618 * \param ct_ptr Filled in by this function.
619 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
621 * This checks the command line options given by \a argc and \a argv, sets
622 * default values for the user name and the name of the rsa key file and reads
623 * further options from the config file.
625 * Upon successful return, \a ct_ptr points to a dynamically allocated and
626 * initialized client task struct.
628 * \return The number of non-option arguments in \a argc/argv on success,
629 * negative on errors.
631 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
634 char *home = para_homedir();
636 struct client_task *ct = para_calloc(sizeof(struct client_task));
640 ret = -E_CLIENT_SYNTAX;
641 if (client_cmdline_parser(argc, argv, &ct->conf))
643 HANDLE_VERSION_FLAG("client", ct->conf);
645 ct->config_file = ct->conf.config_file_given?
646 para_strdup(ct->conf.config_file_arg) :
647 make_message("%s/.paraslash/client.conf", home);
648 ret = file_exists(ct->config_file);
649 if (!ret && ct->conf.config_file_given) {
654 struct client_cmdline_parser_params params = {
658 .check_ambiguity = 0,
662 if (client_cmdline_parser_config_file(ct->config_file,
666 ct->user = ct->conf.user_given?
667 para_strdup(ct->conf.user_arg) : para_logname();
669 if (ct->conf.key_file_given)
670 ct->key_file = para_strdup(ct->conf.key_file_arg);
672 ct->key_file = make_message("%s/.paraslash/key.%s",
674 if (!file_exists(ct->key_file)) {
676 ct->key_file = make_message("%s/.ssh/id_rsa", home);
681 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
682 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
683 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
684 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
685 ret = ct->conf.inputs_num;
689 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
697 * Parse the client configuration and open a connection to para_server.
699 * \param argc See \ref client_parse_config.
700 * \param argv See \ref client_parse_config.
701 * \param ct_ptr See \ref client_parse_config.
702 * \param loglevel See \ref client_parse_config.
703 * \param parent See \ref client_connect().
704 * \param child See \ref client_connect().
705 * \param sched See \ref client_connect().
707 * This function combines client_parse_config() and client_connect(). It is
708 * considered a syntax error if no command was given, i.e. if the number
709 * of non-option arguments is zero.
713 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
714 int *loglevel, struct btr_node *parent, struct btr_node *child,
717 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
722 ret = -E_CLIENT_SYNTAX;
725 ret = client_connect(*ct_ptr, sched, parent, child);
730 client_close(*ct_ptr);