2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
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 free all resources.
38 * \param ct Pointer to the client data.
40 * \sa \ref client_open().
42 void client_close(struct client_task *ct)
47 free(ct->config_file);
49 client_cmdline_parser_free(&ct->conf);
50 free(ct->challenge_hash);
57 * The preselect hook for server commands.
59 * The task pointer must contain a pointer to the initialized client data
60 * structure as it is returned by client_open().
62 * This function checks the state of the connection and adds the file descriptor
63 * of the connection to the read or write fd set of s accordingly.
65 static void client_pre_select(struct sched *s, void *context)
68 struct client_task *ct = context;
75 case CL_SENT_CH_RESPONSE:
76 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
79 case CL_RECEIVED_WELCOME:
80 case CL_RECEIVED_PROCEED:
81 case CL_RECEIVED_CHALLENGE:
82 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
87 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
91 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
96 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
100 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
106 static int send_sb(struct client_task *ct, int channel, void *buf, size_t numbytes,
107 enum sb_designator band, bool dont_free)
109 int ret, fd = ct->scc.fd;
112 if (!ct->sbc[channel]) {
113 struct sb_buffer sbb;
114 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
116 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
117 ct->sbc[channel] = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
119 ret = sb_get_send_buffers(ct->sbc[channel], iov);
120 ret = xwritev(fd, iov, ret);
122 sb_free(ct->sbc[channel]);
123 ct->sbc[channel] = NULL;
126 if (sb_sent(ct->sbc[channel], ret)) {
127 ct->sbc[channel] = NULL;
133 static int recv_sb(struct client_task *ct, fd_set *rfds,
134 struct sb_buffer *result)
138 sb_transformation trafo;
142 if (!FD_ISSET(ct->scc.fd, rfds))
144 if (ct->status < CL_SENT_CH_RESPONSE)
145 trafo = trafo_context = NULL;
148 trafo_context = ct->scc.recv;
151 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
153 sb_get_recv_buffer(ct->sbc[0], &iov);
154 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
162 ret = sb_received(ct->sbc[0], n, result);
172 static char **parse_features(char *buf)
175 const char id[] = "\nFeatures: ";
176 char *p, *q, **features;
186 create_argv(p, ",", &features);
187 for (i = 0; features[i]; i++)
188 PARA_INFO_LOG("server feature: %s\n", features[i]);
192 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
195 const char *designator[] = {SB_DESIGNATORS_ARRAY};
199 if (sbb->band < NUM_SB_DESIGNATORS)
200 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
203 case SBD_AWAITING_DATA:
204 ct->status = CL_SENDING;
208 if (iov_valid(&sbb->iov))
209 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
216 case SBD_WARNING_LOG:
220 if (iov_valid(&sbb->iov)) {
221 int ll = sbb->band - SBD_DEBUG_LOG;
222 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
226 case SBD_EXIT__SUCCESS:
227 ret = -E_SERVER_CMD_SUCCESS;
229 case SBD_EXIT__FAILURE:
230 ret = -E_SERVER_CMD_FAILURE;
233 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
238 free(sbb->iov.iov_base);
240 sbb->iov.iov_base = NULL;
244 static int send_sb_command(struct client_task *ct)
251 return send_sb(ct, 0, NULL, 0, 0, false);
253 for (i = 0; i < ct->conf.inputs_num; i++)
254 len += strlen(ct->conf.inputs[i]) + 1;
255 p = command = para_malloc(len);
256 for (i = 0; i < ct->conf.inputs_num; i++) {
257 strcpy(p, ct->conf.inputs[i]);
258 p += strlen(ct->conf.inputs[i]) + 1;
260 PARA_DEBUG_LOG("--> %s\n", command);
261 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
265 * The post select hook for client commands.
267 * Depending on the current state of the connection and the status of the read
268 * and write fd sets of s, this function performs the necessary steps to
269 * authenticate the connection, to send the command given by t->private_data
270 * and to receive para_server's output, if any.
272 static int client_post_select(struct sched *s, void *context)
274 struct client_task *ct = context;
277 char buf[CLIENT_BUFSIZE];
279 ret = task_get_notification(ct->task);
284 switch (ct->status) {
285 case CL_CONNECTED: /* receive welcome message */
286 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
287 if (ret < 0 || n == 0)
289 ct->features = parse_features(buf);
290 ct->status = CL_RECEIVED_WELCOME;
292 case CL_RECEIVED_WELCOME: /* send auth command */
293 if (!FD_ISSET(ct->scc.fd, &s->wfds))
295 sprintf(buf, AUTH_REQUEST_MSG "%s sideband,aes_ctr128",
297 PARA_INFO_LOG("--> %s\n", buf);
298 ret = write_buffer(ct->scc.fd, buf);
301 ct->status = CL_SENT_AUTH;
305 * Receive challenge and session keys, decrypt the challenge and
306 * send back the hash of the decrypted challenge.
309 /* decrypted challenge/session key buffer */
310 unsigned char crypt_buf[1024];
311 struct sb_buffer sbb;
313 ret = recv_sb(ct, &s->rfds, &sbb);
316 if (sbb.band != SBD_CHALLENGE) {
318 free(sbb.iov.iov_base);
322 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
323 ret = priv_decrypt(ct->key_file, crypt_buf,
324 sbb.iov.iov_base, n);
325 free(sbb.iov.iov_base);
328 ct->challenge_hash = para_malloc(HASH_SIZE);
329 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
330 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
331 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
333 hash_to_asc(ct->challenge_hash, buf);
334 PARA_INFO_LOG("--> %s\n", buf);
335 ct->status = CL_RECEIVED_CHALLENGE;
338 case CL_RECEIVED_CHALLENGE:
339 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
340 SBD_CHALLENGE_RESPONSE, false);
342 ct->challenge_hash = NULL;
345 ct->status = CL_SENT_CH_RESPONSE;
347 case CL_SENT_CH_RESPONSE: /* read server response */
349 struct sb_buffer sbb;
350 ret = recv_sb(ct, &s->rfds, &sbb);
353 free(sbb.iov.iov_base);
354 if (sbb.band != SBD_PROCEED)
357 ct->status = CL_RECEIVED_PROCEED;
360 case CL_RECEIVED_PROCEED: /* concat args and send command */
362 if (!FD_ISSET(ct->scc.fd, &s->wfds))
364 ret = send_sb_command(ct);
367 ct->status = CL_EXECUTING;
374 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
375 if (ret == -E_BTR_EOF) {
376 /* empty blob data packet indicates EOF */
377 PARA_INFO_LOG("blob sent\n");
378 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
384 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
385 sz = btr_next_buffer(ct->btrn[1], &buf2);
387 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
391 btr_consume(ct->btrn[1], sz);
397 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
400 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
401 struct sb_buffer sbb;
402 ret = recv_sb(ct, &s->rfds, &sbb);
406 ret = dispatch_sbb(ct, &sbb);
416 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
417 btr_remove_node(&ct->btrn[1]);
422 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
423 btr_remove_node(&ct->btrn[0]);
424 if (ct->btrn[1] && ct->status == CL_SENDING)
429 btr_remove_node(&ct->btrn[0]);
430 btr_remove_node(&ct->btrn[1]);
431 if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
432 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
433 if (ct->scc.fd >= 0) {
437 free_argv(ct->features);
439 sc_free(ct->scc.recv);
441 sc_free(ct->scc.send);
447 * Connect to para_server and register the client task.
449 * \param ct The initialized client task structure.
450 * \param s The scheduler instance to register the client task to.
451 * \param parent The parent node of the client btr node.
452 * \param child The child node of the client node.
454 * The client task structure given by \a ct must be allocated and initialized
455 * by \ref client_parse_config() before this function is called.
459 int client_connect(struct client_task *ct, struct sched *s,
460 struct btr_node *parent, struct btr_node *child)
464 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
465 ct->conf.server_port_arg);
467 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
468 ct->conf.server_port_arg);
472 ret = mark_fd_nonblocking(ct->scc.fd);
475 ct->status = CL_CONNECTED;
476 ct->btrn[0] = btr_new_node(&(struct btr_node_description)
477 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
478 ct->btrn[1] = btr_new_node(&(struct btr_node_description)
479 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
481 ct->task = task_register(&(struct task_info) {
483 .pre_select = client_pre_select,
484 .post_select = client_post_select,
494 __noreturn static void print_help_and_die(struct client_task *ct)
496 struct ggo_help h = DEFINE_GGO_HELP(client);
497 bool d = ct->conf.detailed_help_given;
499 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
504 * Parse a client configuration.
506 * \param argc Usual argument count.
507 * \param argv Usual argument vector.
508 * \param ct_ptr Filled in by this function.
509 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
511 * This checks the command line options given by \a argc and \a argv, sets
512 * default values for the user name and the name of the rsa key file and reads
513 * further options from the config file.
515 * Upon successful return, \a ct_ptr points to a dynamically allocated and
516 * initialized client task struct.
518 * \return The number of non-option arguments in \a argc/argv on success,
519 * negative on errors.
521 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
524 char *home = para_homedir();
526 struct client_task *ct = para_calloc(sizeof(struct client_task));
530 ret = -E_CLIENT_SYNTAX;
531 if (client_cmdline_parser(argc, argv, &ct->conf))
533 version_handle_flag("client", ct->conf.version_given);
534 if (ct->conf.help_given || ct->conf.detailed_help_given)
535 print_help_and_die(ct);
537 ct->config_file = ct->conf.config_file_given?
538 para_strdup(ct->conf.config_file_arg) :
539 make_message("%s/.paraslash/client.conf", home);
540 ret = file_exists(ct->config_file);
541 if (!ret && ct->conf.config_file_given) {
546 struct client_cmdline_parser_params params = {
550 .check_ambiguity = 0,
554 if (client_cmdline_parser_config_file(ct->config_file,
558 ct->user = ct->conf.user_given?
559 para_strdup(ct->conf.user_arg) : para_logname();
561 if (ct->conf.key_file_given)
562 ct->key_file = para_strdup(ct->conf.key_file_arg);
564 ct->key_file = make_message("%s/.paraslash/key.%s",
566 if (!file_exists(ct->key_file)) {
568 ct->key_file = make_message("%s/.ssh/id_rsa", home);
573 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
574 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
575 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
576 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
577 ret = ct->conf.inputs_num;
581 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
589 * Parse the client configuration and open a connection to para_server.
591 * \param argc See \ref client_parse_config.
592 * \param argv See \ref client_parse_config.
593 * \param ct_ptr See \ref client_parse_config.
594 * \param loglevel See \ref client_parse_config.
595 * \param parent See \ref client_connect().
596 * \param child See \ref client_connect().
597 * \param sched See \ref client_connect().
599 * This function combines client_parse_config() and client_connect(). It is
600 * considered a syntax error if no command was given, i.e. if the number
601 * of non-option arguments is zero.
605 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
606 int *loglevel, struct btr_node *parent, struct btr_node *child,
609 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
614 ret = -E_CLIENT_SYNTAX;
617 ret = client_connect(*ct_ptr, sched, parent, child);
622 client_close(*ct_ptr);