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>
18 #include "client.lsg.h"
29 #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 lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
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 bool has_feature(const char *feature, struct client_task *ct)
246 return find_arg(feature, ct->features) >= 0? true : false;
249 static int send_sb_command(struct client_task *ct)
254 unsigned num_inputs = lls_num_inputs(ct->lpr);
257 return send_sb(ct, 0, NULL, 0, 0, false);
259 for (i = 0; i < num_inputs; i++)
260 len += strlen(lls_input(i, ct->lpr)) + 1;
261 p = command = para_malloc(len);
262 for (i = 0; i < num_inputs; i++) {
263 const char *str = lls_input(i, ct->lpr);
265 p += strlen(str) + 1;
267 PARA_DEBUG_LOG("--> %s\n", command);
268 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
272 * The post select hook for client commands.
274 * Depending on the current state of the connection and the status of the read
275 * and write fd sets of s, this function performs the necessary steps to
276 * authenticate the connection, to send the command given by t->private_data
277 * and to receive para_server's output, if any.
279 static int client_post_select(struct sched *s, void *context)
281 struct client_task *ct = context;
284 char buf[CLIENT_BUFSIZE];
286 ret = task_get_notification(ct->task);
291 switch (ct->status) {
292 case CL_CONNECTED: /* receive welcome message */
293 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
294 if (ret < 0 || n == 0)
296 ct->features = parse_features(buf);
297 ct->status = CL_RECEIVED_WELCOME;
299 case CL_RECEIVED_WELCOME: /* send auth command */
300 if (!FD_ISSET(ct->scc.fd, &s->wfds))
302 sprintf(buf, AUTH_REQUEST_MSG "%s sideband%s", ct->user,
303 has_feature("aes_ctr128", ct)? ",aes_ctr128" : "");
304 PARA_INFO_LOG("--> %s\n", buf);
305 ret = write_buffer(ct->scc.fd, buf);
308 ct->status = CL_SENT_AUTH;
312 * Receive challenge and session keys, decrypt the challenge and
313 * send back the hash of the decrypted challenge.
316 /* decrypted challenge/session key buffer */
317 unsigned char crypt_buf[1024];
318 struct sb_buffer sbb;
321 ret = recv_sb(ct, &s->rfds, &sbb);
324 if (sbb.band != SBD_CHALLENGE) {
326 free(sbb.iov.iov_base);
330 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
331 ret = priv_decrypt(ct->key_file, crypt_buf,
332 sbb.iov.iov_base, n);
333 free(sbb.iov.iov_base);
336 ct->challenge_hash = para_malloc(HASH_SIZE);
337 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
338 use_aes = has_feature("aes_ctr128", ct);
339 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN, use_aes);
340 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
341 SESSION_KEY_LEN, use_aes);
342 hash_to_asc(ct->challenge_hash, buf);
343 PARA_INFO_LOG("--> %s\n", buf);
344 ct->status = CL_RECEIVED_CHALLENGE;
347 case CL_RECEIVED_CHALLENGE:
348 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
349 SBD_CHALLENGE_RESPONSE, false);
351 ct->challenge_hash = NULL;
354 ct->status = CL_SENT_CH_RESPONSE;
356 case CL_SENT_CH_RESPONSE: /* read server response */
358 struct sb_buffer sbb;
359 ret = recv_sb(ct, &s->rfds, &sbb);
362 free(sbb.iov.iov_base);
363 if (sbb.band != SBD_PROCEED)
366 ct->status = CL_RECEIVED_PROCEED;
369 case CL_RECEIVED_PROCEED: /* concat args and send command */
371 if (!FD_ISSET(ct->scc.fd, &s->wfds))
373 ret = send_sb_command(ct);
376 ct->status = CL_EXECUTING;
383 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
384 if (ret == -E_BTR_EOF) {
385 /* empty blob data packet indicates EOF */
386 PARA_INFO_LOG("blob sent\n");
387 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
393 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
394 sz = btr_next_buffer(ct->btrn[1], &buf2);
396 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
400 btr_consume(ct->btrn[1], sz);
406 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
409 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
410 struct sb_buffer sbb;
411 ret = recv_sb(ct, &s->rfds, &sbb);
415 ret = dispatch_sbb(ct, &sbb);
425 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
426 btr_remove_node(&ct->btrn[1]);
431 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
432 btr_remove_node(&ct->btrn[0]);
433 if (ct->btrn[1] && ct->status == CL_SENDING)
438 btr_remove_node(&ct->btrn[0]);
439 btr_remove_node(&ct->btrn[1]);
440 if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
441 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
442 if (ct->scc.fd >= 0) {
446 free_argv(ct->features);
448 sc_free(ct->scc.recv);
450 sc_free(ct->scc.send);
456 * Connect to para_server and register the client task.
458 * \param ct The initialized client task structure.
459 * \param s The scheduler instance to register the client task to.
460 * \param parent The parent node of the client btr node.
461 * \param child The child node of the client node.
463 * The client task structure given by \a ct must be allocated and initialized
464 * by \ref client_parse_config() before this function is called.
468 int client_connect(struct client_task *ct, struct sched *s,
469 struct btr_node *parent, struct btr_node *child)
472 const char *host = CLIENT_OPT_STRING_VAL(HOSTNAME, ct->lpr);
473 uint32_t port = CLIENT_OPT_UINT32_VAL(SERVER_PORT, ct->lpr);
475 PARA_NOTICE_LOG("connecting %s:%u\n", host, port);
477 ret = para_connect_simple(IPPROTO_TCP, host, port);
481 ret = mark_fd_nonblocking(ct->scc.fd);
484 ct->status = CL_CONNECTED;
485 ct->btrn[0] = btr_new_node(&(struct btr_node_description)
486 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
487 ct->btrn[1] = btr_new_node(&(struct btr_node_description)
488 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
490 ct->task = task_register(&(struct task_info) {
492 .pre_select = client_pre_select,
493 .post_select = client_post_select,
503 static void handle_help_flag(struct lls_parse_result *lpr)
507 if (CLIENT_OPT_GIVEN(DETAILED_HELP, lpr))
508 help = lls_long_help(CLIENT_CMD_PTR);
509 else if (CLIENT_OPT_GIVEN(HELP, lpr))
510 help = lls_short_help(CLIENT_CMD_PTR);
513 printf("%s\n", help);
519 * Parse a client configuration.
521 * \param argc Usual argument count.
522 * \param argv Usual argument vector.
523 * \param ct_ptr Filled in by this function.
524 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
526 * This checks the command line options given by \a argc and \a argv, sets
527 * default values for the user name and the name of the rsa key file and reads
528 * further options from the config file.
530 * Upon successful return, \a ct_ptr points to a dynamically allocated and
531 * initialized client task struct.
533 * \return The number of non-option arguments in \a argc/argv on success,
534 * negative on errors.
536 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
539 const struct lls_command *cmd = CLIENT_CMD_PTR;
542 struct lls_parse_result *lpr;
544 struct client_task *ct;
545 char *cf = NULL, *kf = NULL, *user, *errctx, *home = para_homedir();
547 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
550 ll = CLIENT_OPT_UINT32_VAL(LOGLEVEL, lpr);
551 version_handle_flag("client", CLIENT_OPT_GIVEN(VERSION, lpr));
552 handle_help_flag(lpr);
554 if (CLIENT_OPT_GIVEN(CONFIG_FILE, lpr))
555 cf = para_strdup(CLIENT_OPT_STRING_VAL(CONFIG_FILE, lpr));
557 cf = make_message("%s/.paraslash/client.conf", home);
558 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
560 if (ret != -E_EMPTY && ret != -ERRNO_TO_PARA_ERROR(ENOENT))
562 if (ret == -ERRNO_TO_PARA_ERROR(ENOENT) &&
563 CLIENT_OPT_GIVEN(CONFIG_FILE, lpr))
568 struct lls_parse_result *cf_lpr, *merged_lpr;
569 ret = lls(lls_convert_config(map, sz, NULL, &cf_argv, &errctx));
570 para_munmap(map, sz);
574 ret = lls(lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx));
575 lls_free_argv(cf_argv);
578 ret = lls(lls_merge(lpr, cf_lpr, cmd, &merged_lpr,
580 lls_free_parse_result(cf_lpr, cmd);
583 lls_free_parse_result(lpr, cmd);
587 user = CLIENT_OPT_GIVEN(USER, lpr)?
588 para_strdup(CLIENT_OPT_STRING_VAL(USER, lpr)) : para_logname();
590 if (CLIENT_OPT_GIVEN(KEY_FILE, lpr))
591 kf = para_strdup(CLIENT_OPT_STRING_VAL(KEY_FILE, lpr));
593 kf = make_message("%s/.paraslash/key.%s", home, user);
594 if (!file_exists(kf)) {
596 kf = make_message("%s/.ssh/id_rsa", home);
599 PARA_INFO_LOG("user: %s\n", user);
600 PARA_INFO_LOG("config file: %s\n", cf);
601 PARA_INFO_LOG("key file: %s\n", kf);
602 PARA_INFO_LOG("loglevel: %d\n", ll);
603 ct = para_calloc(sizeof(*ct));
607 ct->config_file = cf;
612 ret = lls_num_inputs(lpr);
617 PARA_ERROR_LOG("%s\n", errctx);
619 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
620 lls_free_parse_result(lpr, cmd);
629 * Parse the client configuration and open a connection to para_server.
631 * \param argc See \ref client_parse_config.
632 * \param argv See \ref client_parse_config.
633 * \param ct_ptr See \ref client_parse_config.
634 * \param loglevel See \ref client_parse_config.
635 * \param parent See \ref client_connect().
636 * \param child See \ref client_connect().
637 * \param sched See \ref client_connect().
639 * This function combines client_parse_config() and client_connect(). It is
640 * considered a syntax error if no command was given, i.e. if the number
641 * of non-option arguments is zero.
645 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
646 int *loglevel, struct btr_node *parent, struct btr_node *child,
649 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
654 ret = -E_CLIENT_SYNTAX;
657 ret = client_connect(*ct_ptr, sched, parent, child);
662 client_close(*ct_ptr);