1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file client_common.c Common functions of para_client and para_audiod. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
14 #include "client.lsg.h"
26 #include "buffer_tree.h"
29 /** The size of the receiving buffer. */
30 #define CLIENT_BUFSIZE 4000
33 * Close the connection to para_server and free all resources.
35 * \param ct Pointer to the client data.
37 * \sa \ref client_open().
39 void client_close(struct client_task *ct)
45 lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
46 free(ct->challenge_hash);
53 * This function asks the scheduler to monitor a file descriptor which
54 * corresponds to an active connection. The descriptor is monitored for either
55 * reading or writing, depending on the state of the connection.
57 * The context pointer is assumed to refer to a client task structure that was
58 * initialized earlier by client_open().
60 static void client_pre_monitor(struct sched *s, void *context)
63 struct client_task *ct = context;
70 case CL_SENT_CH_RESPONSE:
71 sched_monitor_readfd(ct->scc.fd, s);
74 case CL_RECEIVED_WELCOME:
75 case CL_RECEIVED_PROCEED:
76 case CL_RECEIVED_CHALLENGE:
77 sched_monitor_writefd(ct->scc.fd, s);
82 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
86 sched_monitor_writefd(ct->scc.fd, s);
88 __attribute__ ((fallthrough));
91 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
95 sched_monitor_readfd(ct->scc.fd, s);
101 static int send_sb(struct client_task *ct, int channel, void *buf, size_t numbytes,
102 enum sb_designator band, bool dont_free)
104 int ret, fd = ct->scc.fd;
107 if (!ct->sbc[channel]) {
108 struct sb_buffer sbb;
109 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
111 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
112 ct->sbc[channel] = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
114 ret = sb_get_send_buffers(ct->sbc[channel], iov);
115 ret = xwritev(fd, iov, ret);
117 sb_free(ct->sbc[channel]);
118 ct->sbc[channel] = NULL;
121 if (sb_sent(ct->sbc[channel], ret)) {
122 ct->sbc[channel] = NULL;
128 static int recv_sb(struct client_task *ct, struct sb_buffer *result)
132 sb_transformation trafo;
136 if (ct->status < CL_SENT_CH_RESPONSE)
137 trafo = trafo_context = NULL;
140 trafo_context = ct->scc.recv;
143 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
145 sb_get_recv_buffer(ct->sbc[0], &iov);
146 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, &n);
154 ret = sb_received(ct->sbc[0], n, result);
164 static char **parse_features(char *buf)
167 const char id[] = "\nFeatures: ";
168 char *p, *q, **features;
178 create_argv(p, ",", &features);
179 for (i = 0; features[i]; i++)
180 PARA_INFO_LOG("server feature: %s\n", features[i]);
184 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
187 const char *designator[] = {SB_DESIGNATORS_ARRAY};
191 if (sbb->band < NUM_SB_DESIGNATORS)
192 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
195 case SBD_AWAITING_DATA:
196 ct->status = CL_SENDING;
200 if (iov_valid(&sbb->iov))
201 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
208 case SBD_WARNING_LOG:
212 if (iov_valid(&sbb->iov)) {
213 int ll = sbb->band - SBD_DEBUG_LOG;
214 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
218 case SBD_EXIT__SUCCESS:
219 ret = -E_SERVER_CMD_SUCCESS;
221 case SBD_EXIT__FAILURE:
222 ret = -E_SERVER_CMD_FAILURE;
225 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
230 free(sbb->iov.iov_base);
232 sbb->iov.iov_base = NULL;
236 static int send_sb_command(struct client_task *ct)
241 unsigned num_inputs = lls_num_inputs(ct->lpr);
244 return send_sb(ct, 0, NULL, 0, 0, false);
246 for (i = 0; i < num_inputs; i++)
247 len += strlen(lls_input(i, ct->lpr)) + 1;
248 p = command = alloc(len);
249 for (i = 0; i < num_inputs; i++) {
250 const char *str = lls_input(i, ct->lpr);
252 p += strlen(str) + 1;
254 PARA_DEBUG_LOG("--> %s\n", command);
255 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
258 /* Find out if the given string is contained in the features vector. */
259 static bool has_feature(const char *feature, struct client_task *ct)
263 for (int i = 0; ct->features[i]; i++)
264 if (strcmp(feature, ct->features[i]) == 0)
270 * This function reads or writes to the socket file descriptor which
271 * corresponds to an established connection between the client and the server.
272 * It depends on the current state of the connection and on the readiness of
273 * the socket file descriptor which type of I/O is going to be performed.
274 * Besides the initial handshake and authentication, the function sends the
275 * server command and receives the output from the server, if any.
277 * The context pointer refers to a client task structure that was initialized
278 * earlier by client_open().
280 static int client_post_monitor(struct sched *s, void *context)
282 struct client_task *ct = context;
285 char buf[CLIENT_BUFSIZE];
287 ret = task_get_notification(ct->task);
292 switch (ct->status) {
293 case CL_CONNECTED: /* receive welcome message */
294 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &n);
295 if (ret < 0 || n == 0)
297 ct->features = parse_features(buf);
298 ct->status = CL_RECEIVED_WELCOME;
300 case CL_RECEIVED_WELCOME: /* send auth command */
303 * Use sha256 iff the server announced the feature. After 0.7.0
304 * we may request and use the feature unconditionally. After
305 * 0.8.0 we no longer need to request the feature.
308 if (!sched_write_ok(ct->scc.fd, s))
310 has_sha256 = has_feature("sha256", ct);
311 sprintf(buf, AUTH_REQUEST_MSG "%s%s", ct->user, has_sha256?
313 PARA_INFO_LOG("--> %s\n", buf);
314 ret = write_buffer(ct->scc.fd, buf);
317 ct->status = CL_SENT_AUTH;
322 * Receive challenge and session keys, decrypt the challenge and
323 * send back the hash of the decrypted challenge.
326 /* decrypted challenge/session key buffer */
327 unsigned char crypt_buf[1024];
328 struct sb_buffer sbb;
330 ret = recv_sb(ct, &sbb);
333 if (sbb.band != SBD_CHALLENGE) {
335 free(sbb.iov.iov_base);
339 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
340 ret = apc_priv_decrypt(ct->key_file, crypt_buf,
341 sbb.iov.iov_base, n);
342 free(sbb.iov.iov_base);
345 ct->challenge_hash = alloc(HASH2_SIZE);
346 if (has_feature("sha256", ct)) {
347 hash2_function((char *)crypt_buf, APC_CHALLENGE_SIZE,
349 hash2_to_asc(ct->challenge_hash, buf);
351 hash_function((char *)crypt_buf, APC_CHALLENGE_SIZE,
353 hash_to_asc(ct->challenge_hash, buf);
355 ct->scc.send = sc_new(crypt_buf + APC_CHALLENGE_SIZE,
357 ct->scc.recv = sc_new(crypt_buf + APC_CHALLENGE_SIZE
358 + SESSION_KEY_LEN, SESSION_KEY_LEN);
359 PARA_INFO_LOG("--> %s\n", buf);
360 ct->status = CL_RECEIVED_CHALLENGE;
363 case CL_RECEIVED_CHALLENGE:
364 if (has_feature("sha256", ct))
365 ret = send_sb(ct, 0, ct->challenge_hash, HASH2_SIZE,
366 SBD_CHALLENGE_RESPONSE, false);
368 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
369 SBD_CHALLENGE_RESPONSE, false);
371 ct->challenge_hash = NULL;
374 ct->status = CL_SENT_CH_RESPONSE;
376 case CL_SENT_CH_RESPONSE: /* read server response */
378 struct sb_buffer sbb;
379 ret = recv_sb(ct, &sbb);
382 free(sbb.iov.iov_base);
383 if (sbb.band != SBD_PROCEED)
386 ct->status = CL_RECEIVED_PROCEED;
389 case CL_RECEIVED_PROCEED: /* concat args and send command */
391 if (!sched_write_ok(ct->scc.fd, s))
393 ret = send_sb_command(ct);
396 ct->status = CL_EXECUTING;
403 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
405 /* empty blob data packet indicates EOF */
406 PARA_INFO_LOG("blob sent\n");
407 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
413 if (ret > 0 && sched_write_ok(ct->scc.fd, s)) {
414 sz = btr_next_buffer(ct->btrn[1], &buf2);
416 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
420 btr_consume(ct->btrn[1], sz);
423 __attribute__ ((fallthrough));
426 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
429 if (ret > 0 && sched_read_ok(ct->scc.fd, s)) {
430 struct sb_buffer sbb;
431 ret = recv_sb(ct, &sbb);
435 ret = dispatch_sbb(ct, &sbb);
445 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
446 btr_remove_node(&ct->btrn[1]);
451 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
452 btr_remove_node(&ct->btrn[0]);
453 if (ct->btrn[1] && ct->status == CL_SENDING)
458 btr_remove_node(&ct->btrn[0]);
459 btr_remove_node(&ct->btrn[1]);
460 PARA_NOTICE_LOG("closing connection (%s)\n", para_strerror(-ret));
461 if (ct->scc.fd >= 0) {
465 free_argv(ct->features);
467 sc_free(ct->scc.recv);
469 sc_free(ct->scc.send);
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)
491 const char *host = CLIENT_OPT_STRING_VAL(HOSTNAME, ct->lpr);
492 uint32_t port = CLIENT_OPT_UINT32_VAL(SERVER_PORT, ct->lpr);
494 PARA_NOTICE_LOG("connecting %s:%u\n", host, port);
496 ret = para_connect(IPPROTO_TCP, host, port);
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));
509 ct->task = task_register(&(struct task_info) {
511 .pre_monitor = client_pre_monitor,
512 .post_monitor = client_post_monitor,
522 static void handle_help_flag(struct lls_parse_result *lpr)
526 if (CLIENT_OPT_GIVEN(DETAILED_HELP, lpr))
527 help = lls_long_help(CLIENT_CMD_PTR);
528 else if (CLIENT_OPT_GIVEN(HELP, lpr))
529 help = lls_short_help(CLIENT_CMD_PTR);
532 printf("%s\n", help);
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 const struct lls_command *cmd = CLIENT_CMD_PTR;
559 struct lls_parse_result *lpr;
561 struct client_task *ct;
562 char *kf = NULL, *user, *errctx, *home = para_homedir();
564 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
567 version_handle_flag("client", CLIENT_OPT_GIVEN(VERSION, lpr));
568 handle_help_flag(lpr);
570 ret = lsu_merge_config_file_options(CLIENT_OPT_STRING_VAL(CONFIG_FILE, lpr),
571 "client.conf", &lpr, cmd, client_suite, 0U /* default flags */);
575 ll = CLIENT_OPT_UINT32_VAL(LOGLEVEL, lpr);
578 user = CLIENT_OPT_GIVEN(USER, lpr)?
579 para_strdup(CLIENT_OPT_STRING_VAL(USER, lpr)) : para_logname();
581 if (CLIENT_OPT_GIVEN(KEY_FILE, lpr))
582 kf = para_strdup(CLIENT_OPT_STRING_VAL(KEY_FILE, lpr));
585 kf = make_message("%s/.paraslash/key.%s", home, user);
586 if (stat(kf, &statbuf) != 0) { /* assume file does not exist */
588 kf = make_message("%s/.ssh/id_rsa", home);
591 PARA_INFO_LOG("user: %s\n", user);
592 PARA_INFO_LOG("key file: %s\n", kf);
593 PARA_INFO_LOG("loglevel: %d\n", ll);
594 ct = zalloc(sizeof(*ct));
600 ret = lls_num_inputs(lpr);
605 PARA_ERROR_LOG("%s\n", errctx);
607 lls_free_parse_result(lpr, cmd);
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);