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>
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);
48 sc_free(ct->scc.recv);
50 sc_free(ct->scc.send);
52 btr_remove_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 send_sb(struct client_task *ct, void *buf, size_t numbytes,
136 enum sb_designator band, bool dont_free)
138 int ret, fd = ct->scc.fd;
142 struct sb_buffer sbb;
143 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
145 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
146 ct->sbc = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
148 ret = sb_get_send_buffers(ct->sbc, iov);
149 ret = xwritev(fd, iov, ret);
155 if (sb_sent(ct->sbc, ret)) {
162 static int recv_sb(struct client_task *ct, fd_set *rfds,
163 struct sb_buffer *result)
167 sb_transformation trafo;
171 if (!FD_ISSET(ct->scc.fd, rfds))
173 if (ct->status < CL_SENT_CH_RESPONSE)
174 trafo = trafo_context = NULL;
177 trafo_context = ct->scc.recv;
180 ct->sbc = sb_new_recv(0, trafo, trafo_context);
182 sb_get_recv_buffer(ct->sbc, &iov);
183 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
191 if (!sb_received(ct->sbc, n, result))
198 static char **parse_features(char *buf)
201 const char id[] = "\nFeatures: ";
202 char *p, *q, **features;
212 create_argv(p, ",", &features);
213 for (i = 0; features[i]; i++)
214 PARA_INFO_LOG("server feature: %s\n", features[i]);
218 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
221 const char *designator[] = {SB_DESIGNATORS_ARRAY};
225 if (sbb->band < NUM_SB_DESIGNATORS)
226 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
230 if (iov_valid(&sbb->iov))
231 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
238 case SBD_WARNING_LOG:
242 if (iov_valid(&sbb->iov)) {
243 int ll = sbb->band - SBD_DEBUG_LOG;
244 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
248 case SBD_EXIT__SUCCESS:
249 ret = -E_SERVER_CMD_SUCCESS;
251 case SBD_EXIT__FAILURE:
252 ret = -E_SERVER_CMD_FAILURE;
255 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
260 free(sbb->iov.iov_base);
262 sbb->iov.iov_base = NULL;
266 static bool has_feature(const char *feature, struct client_task *ct)
268 return find_arg(feature, ct->features) >= 0? true : false;
271 static int send_sb_command(struct client_task *ct)
278 return send_sb(ct, NULL, 0, 0, false);
280 for (i = 0; i < ct->conf.inputs_num; i++)
281 len += strlen(ct->conf.inputs[i]) + 1;
282 p = command = para_malloc(len);
283 for (i = 0; i < ct->conf.inputs_num; i++) {
284 strcpy(p, ct->conf.inputs[i]);
285 p += strlen(ct->conf.inputs[i]) + 1;
287 PARA_DEBUG_LOG("--> %s\n", command);
288 return send_sb(ct, command, len, SBD_COMMAND, false);
292 * The post select hook for client commands.
294 * \param s Pointer to the scheduler.
295 * \param t Pointer to the task struct for this command.
297 * Depending on the current state of the connection and the status of the read
298 * and write fd sets of \a s, this function performs the necessary steps to
299 * authenticate the connection, to send the command given by \a t->private_data
300 * and to receive para_server's output, if any.
302 * \sa struct sched, struct task.
304 static void client_post_select(struct sched *s, struct task *t)
306 struct client_task *ct = container_of(t, struct client_task, task);
307 struct btr_node *btrn = ct->btrn;
310 char buf[CLIENT_BUFSIZE];
312 ret = task_get_notification(t);
317 switch (ct->status) {
318 case CL_CONNECTED: /* receive welcome message */
319 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
320 if (ret < 0 || n == 0)
322 ct->features = parse_features(buf);
323 if (!has_feature("sideband", ct)) {
324 PARA_ERROR_LOG("server has no sideband support\n");
325 ret = -E_INCOMPAT_FEAT;
328 ct->status = CL_RECEIVED_WELCOME;
330 case CL_RECEIVED_WELCOME: /* send auth command */
331 if (!FD_ISSET(ct->scc.fd, &s->wfds))
333 sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user);
334 PARA_INFO_LOG("--> %s\n", buf);
335 ret = write_buffer(ct->scc.fd, buf);
338 ct->status = CL_SENT_AUTH;
342 * Receive challenge and session keys, decrypt the challenge and
343 * send back the hash of the decrypted challenge.
346 /* decrypted challenge/session key buffer */
347 unsigned char crypt_buf[1024];
348 struct sb_buffer sbb;
350 ret = recv_sb(ct, &s->rfds, &sbb);
353 if (sbb.band != SBD_CHALLENGE) {
355 free(sbb.iov.iov_base);
359 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
360 ret = priv_decrypt(ct->key_file, crypt_buf,
361 sbb.iov.iov_base, n);
362 free(sbb.iov.iov_base);
365 ct->challenge_hash = para_malloc(HASH_SIZE);
366 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
367 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
368 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
370 hash_to_asc(ct->challenge_hash, buf);
371 PARA_INFO_LOG("--> %s\n", buf);
372 ct->status = CL_RECEIVED_CHALLENGE;
375 case CL_RECEIVED_CHALLENGE:
376 ret = send_sb(ct, ct->challenge_hash, HASH_SIZE,
377 SBD_CHALLENGE_RESPONSE, false);
379 ct->challenge_hash = NULL;
382 ct->status = CL_SENT_CH_RESPONSE;
384 case CL_SENT_CH_RESPONSE: /* read server response */
386 struct sb_buffer sbb;
387 ret = recv_sb(ct, &s->rfds, &sbb);
390 free(sbb.iov.iov_base);
391 if (sbb.band != SBD_PROCEED)
394 ct->status = CL_RECEIVED_PROCEED;
397 case CL_RECEIVED_PROCEED: /* concat args and send command */
399 if (!FD_ISSET(ct->scc.fd, &s->wfds))
401 ret = send_sb_command(ct);
404 ct->status = CL_SENT_COMMAND;
407 case CL_SENT_COMMAND:
409 struct sb_buffer sbb;
410 ret = recv_sb(ct, &s->rfds, &sbb);
413 if (sbb.band == SBD_AWAITING_DATA) {
414 ct->status = CL_SENDING;
415 free(sbb.iov.iov_base);
418 ct->status = CL_RECEIVING;
419 ret = dispatch_sbb(ct, &sbb);
426 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
431 if (!FD_ISSET(ct->scc.fd, &s->wfds))
433 sz = btr_next_buffer(btrn, &buf2);
434 ret = sc_send_bin_buffer(&ct->scc, buf2, sz);
437 btr_consume(btrn, sz);
442 struct sb_buffer sbb;
443 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
449 * The FD_ISSET() is not strictly necessary, but is allows us
450 * to skip the malloc below if there is nothing to read anyway.
452 if (!FD_ISSET(ct->scc.fd, &s->rfds))
454 ret = recv_sb(ct, &s->rfds, &sbb);
456 ret = dispatch_sbb(ct, &sbb);
463 btr_remove_node(&ct->btrn);
467 * Connect to para_server and register the client task.
469 * \param ct The initialized client task structure.
470 * \param s The scheduler instance to register the client task to.
471 * \param parent The parent node of the client btr node.
472 * \param child The child node of the client node.
474 * The client task structure given by \a ct must be allocated and initialized
475 * by \ref client_parse_config() before this function is called.
479 int client_connect(struct client_task *ct, struct sched *s,
480 struct btr_node *parent, struct btr_node *child)
484 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
485 ct->conf.server_port_arg);
487 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
488 ct->conf.server_port_arg);
492 ret = mark_fd_nonblocking(ct->scc.fd);
495 ct->status = CL_CONNECTED;
496 ct->btrn = btr_new_node(&(struct btr_node_description)
497 EMBRACE(.name = "client", .parent = parent, .child = child));
498 ct->task.pre_select = client_pre_select;
499 ct->task.post_select = client_post_select;
501 sprintf(ct->task.status, "client");
502 register_task(s, &ct->task);
511 * Parse a client configuration.
513 * \param argc Usual argument count.
514 * \param argv Usual argument vector.
515 * \param ct_ptr Filled in by this function.
516 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
518 * This checks the command line options given by \a argc and \a argv, sets
519 * default values for the user name and the name of the rsa key file and reads
520 * further options from the config file.
522 * Upon successful return, \a ct_ptr points to a dynamically allocated and
523 * initialized client task struct.
525 * \return The number of non-option arguments in \a argc/argv on success,
526 * negative on errors.
528 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
531 char *home = para_homedir();
533 struct client_task *ct = para_calloc(sizeof(struct client_task));
537 ret = -E_CLIENT_SYNTAX;
538 if (client_cmdline_parser(argc, argv, &ct->conf))
540 HANDLE_VERSION_FLAG("client", ct->conf);
542 ct->config_file = ct->conf.config_file_given?
543 para_strdup(ct->conf.config_file_arg) :
544 make_message("%s/.paraslash/client.conf", home);
545 ret = file_exists(ct->config_file);
546 if (!ret && ct->conf.config_file_given) {
551 struct client_cmdline_parser_params params = {
555 .check_ambiguity = 0,
559 if (client_cmdline_parser_config_file(ct->config_file,
563 ct->user = ct->conf.user_given?
564 para_strdup(ct->conf.user_arg) : para_logname();
566 if (ct->conf.key_file_given)
567 ct->key_file = para_strdup(ct->conf.key_file_arg);
569 ct->key_file = make_message("%s/.paraslash/key.%s",
571 if (!file_exists(ct->key_file)) {
573 ct->key_file = make_message("%s/.ssh/id_rsa", home);
578 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
579 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
580 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
581 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
582 ret = ct->conf.inputs_num;
586 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
594 * Parse the client configuration and open a connection to para_server.
596 * \param argc See \ref client_parse_config.
597 * \param argv See \ref client_parse_config.
598 * \param ct_ptr See \ref client_parse_config.
599 * \param loglevel See \ref client_parse_config.
600 * \param parent See \ref client_connect().
601 * \param child See \ref client_connect().
602 * \param sched See \ref client_connect().
604 * This function combines client_parse_config() and client_connect(). It is
605 * considered a syntax error if no command was given, i.e. if the number
606 * of non-option arguments is zero.
610 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
611 int *loglevel, struct btr_node *parent, struct btr_node *child,
614 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
619 ret = -E_CLIENT_SYNTAX;
622 ret = client_connect(*ct_ptr, sched, parent, child);
627 client_close(*ct_ptr);