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_select(struct sched *s, void *context)
63 struct client_task *ct = context;
70 case CL_SENT_CH_RESPONSE:
71 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
74 case CL_RECEIVED_WELCOME:
75 case CL_RECEIVED_PROCEED:
76 case CL_RECEIVED_CHALLENGE:
77 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
82 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
86 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
88 __attribute__ ((fallthrough));
91 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
95 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
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, fd_set *rfds,
129 struct sb_buffer *result)
133 sb_transformation trafo;
137 if (!FD_ISSET(ct->scc.fd, rfds))
139 if (ct->status < CL_SENT_CH_RESPONSE)
140 trafo = trafo_context = NULL;
143 trafo_context = ct->scc.recv;
146 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
148 sb_get_recv_buffer(ct->sbc[0], &iov);
149 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
157 ret = sb_received(ct->sbc[0], n, result);
167 static char **parse_features(char *buf)
170 const char id[] = "\nFeatures: ";
171 char *p, *q, **features;
181 create_argv(p, ",", &features);
182 for (i = 0; features[i]; i++)
183 PARA_INFO_LOG("server feature: %s\n", features[i]);
187 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
190 const char *designator[] = {SB_DESIGNATORS_ARRAY};
194 if (sbb->band < NUM_SB_DESIGNATORS)
195 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
198 case SBD_AWAITING_DATA:
199 ct->status = CL_SENDING;
203 if (iov_valid(&sbb->iov))
204 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
211 case SBD_WARNING_LOG:
215 if (iov_valid(&sbb->iov)) {
216 int ll = sbb->band - SBD_DEBUG_LOG;
217 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
221 case SBD_EXIT__SUCCESS:
222 ret = -E_SERVER_CMD_SUCCESS;
224 case SBD_EXIT__FAILURE:
225 ret = -E_SERVER_CMD_FAILURE;
228 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
233 free(sbb->iov.iov_base);
235 sbb->iov.iov_base = NULL;
239 static int send_sb_command(struct client_task *ct)
244 unsigned num_inputs = lls_num_inputs(ct->lpr);
247 return send_sb(ct, 0, NULL, 0, 0, false);
249 for (i = 0; i < num_inputs; i++)
250 len += strlen(lls_input(i, ct->lpr)) + 1;
251 p = command = para_malloc(len);
252 for (i = 0; i < num_inputs; i++) {
253 const char *str = lls_input(i, ct->lpr);
255 p += strlen(str) + 1;
257 PARA_DEBUG_LOG("--> %s\n", command);
258 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
261 static bool has_feature(const char *feature, struct client_task *ct)
263 return find_arg(feature, ct->features) >= 0? true : false;
267 * This function reads or writes to the socket file descriptor which
268 * corresponds to an established connection between the client and the server.
269 * It depends on the current state of the connection and on the readiness of
270 * the socket file descriptor which type of I/O is going to be performed.
271 * Besides the initial handshake and authentication, the function sends the
272 * server command and receives the output from the server, if any.
274 * The context pointer refers to a client task structure that was initialized
275 * earlier by client_open().
277 static int client_post_select(struct sched *s, void *context)
279 struct client_task *ct = context;
282 char buf[CLIENT_BUFSIZE];
284 ret = task_get_notification(ct->task);
289 switch (ct->status) {
290 case CL_CONNECTED: /* receive welcome message */
291 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
292 if (ret < 0 || n == 0)
294 ct->features = parse_features(buf);
295 ct->status = CL_RECEIVED_WELCOME;
297 case CL_RECEIVED_WELCOME: /* send auth command */
300 * Use sha256 iff the server announced the feature. After 0.7.0
301 * we may request and use the feature unconditionally. After
302 * 0.8.0 we no longer need to request the feature.
305 if (!FD_ISSET(ct->scc.fd, &s->wfds))
307 has_sha256 = has_feature("sha256", ct);
308 sprintf(buf, AUTH_REQUEST_MSG "%s%s", ct->user, has_sha256?
310 PARA_INFO_LOG("--> %s\n", buf);
311 ret = write_buffer(ct->scc.fd, buf);
314 ct->status = CL_SENT_AUTH;
319 * Receive challenge and session keys, decrypt the challenge and
320 * send back the hash of the decrypted challenge.
323 /* decrypted challenge/session key buffer */
324 unsigned char crypt_buf[1024];
325 struct sb_buffer sbb;
327 ret = recv_sb(ct, &s->rfds, &sbb);
330 if (sbb.band != SBD_CHALLENGE) {
332 free(sbb.iov.iov_base);
336 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
337 ret = apc_priv_decrypt(ct->key_file, crypt_buf,
338 sbb.iov.iov_base, n);
339 free(sbb.iov.iov_base);
342 ct->challenge_hash = para_malloc(HASH2_SIZE);
344 if (has_feature("sha256", ct)) {
345 hash2_function((char *)crypt_buf, APC_CHALLENGE_SIZE, ct->challenge_hash);
346 hash2_to_asc(ct->challenge_hash, buf);
348 hash_function((char *)crypt_buf, APC_CHALLENGE_SIZE, ct->challenge_hash);
349 hash_to_asc(ct->challenge_hash, buf);
351 ct->scc.send = sc_new(crypt_buf + APC_CHALLENGE_SIZE, SESSION_KEY_LEN);
352 ct->scc.recv = sc_new(crypt_buf + APC_CHALLENGE_SIZE + SESSION_KEY_LEN,
354 PARA_INFO_LOG("--> %s\n", buf);
355 ct->status = CL_RECEIVED_CHALLENGE;
358 case CL_RECEIVED_CHALLENGE:
359 if (has_feature("sha256", ct))
360 ret = send_sb(ct, 0, ct->challenge_hash, HASH2_SIZE,
361 SBD_CHALLENGE_RESPONSE, false);
363 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
364 SBD_CHALLENGE_RESPONSE, false);
366 ct->challenge_hash = NULL;
369 ct->status = CL_SENT_CH_RESPONSE;
371 case CL_SENT_CH_RESPONSE: /* read server response */
373 struct sb_buffer sbb;
374 ret = recv_sb(ct, &s->rfds, &sbb);
377 free(sbb.iov.iov_base);
378 if (sbb.band != SBD_PROCEED)
381 ct->status = CL_RECEIVED_PROCEED;
384 case CL_RECEIVED_PROCEED: /* concat args and send command */
386 if (!FD_ISSET(ct->scc.fd, &s->wfds))
388 ret = send_sb_command(ct);
391 ct->status = CL_EXECUTING;
398 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
399 if (ret == -E_BTR_EOF) {
400 /* empty blob data packet indicates EOF */
401 PARA_INFO_LOG("blob sent\n");
402 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
408 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
409 sz = btr_next_buffer(ct->btrn[1], &buf2);
411 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
415 btr_consume(ct->btrn[1], sz);
418 __attribute__ ((fallthrough));
421 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
424 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
425 struct sb_buffer sbb;
426 ret = recv_sb(ct, &s->rfds, &sbb);
430 ret = dispatch_sbb(ct, &sbb);
440 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
441 btr_remove_node(&ct->btrn[1]);
446 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
447 btr_remove_node(&ct->btrn[0]);
448 if (ct->btrn[1] && ct->status == CL_SENDING)
453 btr_remove_node(&ct->btrn[0]);
454 btr_remove_node(&ct->btrn[1]);
455 if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
456 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
457 if (ct->scc.fd >= 0) {
461 free_argv(ct->features);
463 sc_free(ct->scc.recv);
465 sc_free(ct->scc.send);
471 * Connect to para_server and register the client task.
473 * \param ct The initialized client task structure.
474 * \param s The scheduler instance to register the client task to.
475 * \param parent The parent node of the client btr node.
476 * \param child The child node of the client node.
478 * The client task structure given by \a ct must be allocated and initialized
479 * by \ref client_parse_config() before this function is called.
483 int client_connect(struct client_task *ct, struct sched *s,
484 struct btr_node *parent, struct btr_node *child)
487 const char *host = CLIENT_OPT_STRING_VAL(HOSTNAME, ct->lpr);
488 uint32_t port = CLIENT_OPT_UINT32_VAL(SERVER_PORT, ct->lpr);
490 PARA_NOTICE_LOG("connecting %s:%u\n", host, port);
492 ret = para_connect_simple(IPPROTO_TCP, host, port);
496 ret = mark_fd_nonblocking(ct->scc.fd);
499 ct->status = CL_CONNECTED;
500 ct->btrn[0] = btr_new_node(&(struct btr_node_description)
501 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
502 ct->btrn[1] = btr_new_node(&(struct btr_node_description)
503 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
505 ct->task = task_register(&(struct task_info) {
507 .pre_select = client_pre_select,
508 .post_select = client_post_select,
518 static void handle_help_flag(struct lls_parse_result *lpr)
522 if (CLIENT_OPT_GIVEN(DETAILED_HELP, lpr))
523 help = lls_long_help(CLIENT_CMD_PTR);
524 else if (CLIENT_OPT_GIVEN(HELP, lpr))
525 help = lls_short_help(CLIENT_CMD_PTR);
528 printf("%s\n", help);
534 * Parse a client configuration.
536 * \param argc Usual argument count.
537 * \param argv Usual argument vector.
538 * \param ct_ptr Filled in by this function.
539 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
541 * This checks the command line options given by \a argc and \a argv, sets
542 * default values for the user name and the name of the rsa key file and reads
543 * further options from the config file.
545 * Upon successful return, \a ct_ptr points to a dynamically allocated and
546 * initialized client task struct.
548 * \return The number of non-option arguments in \a argc/argv on success,
549 * negative on errors.
551 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
554 const struct lls_command *cmd = CLIENT_CMD_PTR;
555 struct lls_parse_result *lpr;
557 struct client_task *ct;
558 char *kf = NULL, *user, *errctx, *home = para_homedir();
560 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
563 version_handle_flag("client", CLIENT_OPT_GIVEN(VERSION, lpr));
564 handle_help_flag(lpr);
566 ret = lsu_merge_config_file_options(CLIENT_OPT_STRING_VAL(CONFIG_FILE, lpr),
567 "client.conf", &lpr, cmd, client_suite, 0U /* default flags */);
571 ll = CLIENT_OPT_UINT32_VAL(LOGLEVEL, lpr);
574 user = CLIENT_OPT_GIVEN(USER, lpr)?
575 para_strdup(CLIENT_OPT_STRING_VAL(USER, lpr)) : para_logname();
577 if (CLIENT_OPT_GIVEN(KEY_FILE, lpr))
578 kf = para_strdup(CLIENT_OPT_STRING_VAL(KEY_FILE, lpr));
580 kf = make_message("%s/.paraslash/key.%s", home, user);
581 if (!file_exists(kf)) {
583 kf = make_message("%s/.ssh/id_rsa", home);
586 PARA_INFO_LOG("user: %s\n", user);
587 PARA_INFO_LOG("key file: %s\n", kf);
588 PARA_INFO_LOG("loglevel: %d\n", ll);
589 ct = para_calloc(sizeof(*ct));
595 ret = lls_num_inputs(lpr);
600 PARA_ERROR_LOG("%s\n", errctx);
602 lls_free_parse_result(lpr, cmd);
610 * Parse the client configuration and open a connection to para_server.
612 * \param argc See \ref client_parse_config.
613 * \param argv See \ref client_parse_config.
614 * \param ct_ptr See \ref client_parse_config.
615 * \param loglevel See \ref client_parse_config.
616 * \param parent See \ref client_connect().
617 * \param child See \ref client_connect().
618 * \param sched See \ref client_connect().
620 * This function combines client_parse_config() and client_connect(). It is
621 * considered a syntax error if no command was given, i.e. if the number
622 * of non-option arguments is zero.
626 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
627 int *loglevel, struct btr_node *parent, struct btr_node *child,
630 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
635 ret = -E_CLIENT_SYNTAX;
638 ret = client_connect(*ct_ptr, sched, parent, child);
643 client_close(*ct_ptr);