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 * The preselect hook for server commands.
55 * The task pointer must contain a pointer to the initialized client data
56 * structure as it is returned by client_open().
58 * This function checks the state of the connection and adds the file descriptor
59 * of the connection to the read or write fd set of s accordingly.
61 static void client_pre_select(struct sched *s, void *context)
64 struct client_task *ct = context;
71 case CL_SENT_CH_RESPONSE:
72 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
75 case CL_RECEIVED_WELCOME:
76 case CL_RECEIVED_PROCEED:
77 case CL_RECEIVED_CHALLENGE:
78 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
83 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
87 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
92 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
96 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
102 static int send_sb(struct client_task *ct, int channel, void *buf, size_t numbytes,
103 enum sb_designator band, bool dont_free)
105 int ret, fd = ct->scc.fd;
108 if (!ct->sbc[channel]) {
109 struct sb_buffer sbb;
110 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
112 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
113 ct->sbc[channel] = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
115 ret = sb_get_send_buffers(ct->sbc[channel], iov);
116 ret = xwritev(fd, iov, ret);
118 sb_free(ct->sbc[channel]);
119 ct->sbc[channel] = NULL;
122 if (sb_sent(ct->sbc[channel], ret)) {
123 ct->sbc[channel] = NULL;
129 static int recv_sb(struct client_task *ct, fd_set *rfds,
130 struct sb_buffer *result)
134 sb_transformation trafo;
138 if (!FD_ISSET(ct->scc.fd, rfds))
140 if (ct->status < CL_SENT_CH_RESPONSE)
141 trafo = trafo_context = NULL;
144 trafo_context = ct->scc.recv;
147 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
149 sb_get_recv_buffer(ct->sbc[0], &iov);
150 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
158 ret = sb_received(ct->sbc[0], n, result);
168 static char **parse_features(char *buf)
171 const char id[] = "\nFeatures: ";
172 char *p, *q, **features;
182 create_argv(p, ",", &features);
183 for (i = 0; features[i]; i++)
184 PARA_INFO_LOG("server feature: %s\n", features[i]);
188 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
191 const char *designator[] = {SB_DESIGNATORS_ARRAY};
195 if (sbb->band < NUM_SB_DESIGNATORS)
196 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
199 case SBD_AWAITING_DATA:
200 ct->status = CL_SENDING;
204 if (iov_valid(&sbb->iov))
205 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
212 case SBD_WARNING_LOG:
216 if (iov_valid(&sbb->iov)) {
217 int ll = sbb->band - SBD_DEBUG_LOG;
218 para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
222 case SBD_EXIT__SUCCESS:
223 ret = -E_SERVER_CMD_SUCCESS;
225 case SBD_EXIT__FAILURE:
226 ret = -E_SERVER_CMD_FAILURE;
229 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
234 free(sbb->iov.iov_base);
236 sbb->iov.iov_base = NULL;
240 static int send_sb_command(struct client_task *ct)
245 unsigned num_inputs = lls_num_inputs(ct->lpr);
248 return send_sb(ct, 0, NULL, 0, 0, false);
250 for (i = 0; i < num_inputs; i++)
251 len += strlen(lls_input(i, ct->lpr)) + 1;
252 p = command = para_malloc(len);
253 for (i = 0; i < num_inputs; i++) {
254 const char *str = lls_input(i, ct->lpr);
256 p += strlen(str) + 1;
258 PARA_DEBUG_LOG("--> %s\n", command);
259 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
263 * The post select hook for client commands.
265 * Depending on the current state of the connection and the status of the read
266 * and write fd sets of s, this function performs the necessary steps to
267 * authenticate the connection, to send the command given by t->private_data
268 * and to receive para_server's output, if any.
270 static int client_post_select(struct sched *s, void *context)
272 struct client_task *ct = context;
275 char buf[CLIENT_BUFSIZE];
277 ret = task_get_notification(ct->task);
282 switch (ct->status) {
283 case CL_CONNECTED: /* receive welcome message */
284 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
285 if (ret < 0 || n == 0)
287 ct->features = parse_features(buf);
288 ct->status = CL_RECEIVED_WELCOME;
290 case CL_RECEIVED_WELCOME: /* send auth command */
291 if (!FD_ISSET(ct->scc.fd, &s->wfds))
293 sprintf(buf, AUTH_REQUEST_MSG "%s sideband,aes_ctr128",
295 PARA_INFO_LOG("--> %s\n", buf);
296 ret = write_buffer(ct->scc.fd, buf);
299 ct->status = CL_SENT_AUTH;
303 * Receive challenge and session keys, decrypt the challenge and
304 * send back the hash of the decrypted challenge.
307 /* decrypted challenge/session key buffer */
308 unsigned char crypt_buf[1024];
309 struct sb_buffer sbb;
311 ret = recv_sb(ct, &s->rfds, &sbb);
314 if (sbb.band != SBD_CHALLENGE) {
316 free(sbb.iov.iov_base);
320 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
321 ret = apc_priv_decrypt(ct->key_file, crypt_buf,
322 sbb.iov.iov_base, n);
323 free(sbb.iov.iov_base);
326 ct->challenge_hash = para_malloc(HASH_SIZE);
327 hash_function((char *)crypt_buf, APC_CHALLENGE_SIZE, ct->challenge_hash);
328 ct->scc.send = sc_new(crypt_buf + APC_CHALLENGE_SIZE, SESSION_KEY_LEN);
329 ct->scc.recv = sc_new(crypt_buf + APC_CHALLENGE_SIZE + SESSION_KEY_LEN,
331 hash_to_asc(ct->challenge_hash, buf);
332 PARA_INFO_LOG("--> %s\n", buf);
333 ct->status = CL_RECEIVED_CHALLENGE;
336 case CL_RECEIVED_CHALLENGE:
337 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
338 SBD_CHALLENGE_RESPONSE, false);
340 ct->challenge_hash = NULL;
343 ct->status = CL_SENT_CH_RESPONSE;
345 case CL_SENT_CH_RESPONSE: /* read server response */
347 struct sb_buffer sbb;
348 ret = recv_sb(ct, &s->rfds, &sbb);
351 free(sbb.iov.iov_base);
352 if (sbb.band != SBD_PROCEED)
355 ct->status = CL_RECEIVED_PROCEED;
358 case CL_RECEIVED_PROCEED: /* concat args and send command */
360 if (!FD_ISSET(ct->scc.fd, &s->wfds))
362 ret = send_sb_command(ct);
365 ct->status = CL_EXECUTING;
372 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
373 if (ret == -E_BTR_EOF) {
374 /* empty blob data packet indicates EOF */
375 PARA_INFO_LOG("blob sent\n");
376 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
382 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
383 sz = btr_next_buffer(ct->btrn[1], &buf2);
385 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
389 btr_consume(ct->btrn[1], sz);
395 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
398 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
399 struct sb_buffer sbb;
400 ret = recv_sb(ct, &s->rfds, &sbb);
404 ret = dispatch_sbb(ct, &sbb);
414 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
415 btr_remove_node(&ct->btrn[1]);
420 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
421 btr_remove_node(&ct->btrn[0]);
422 if (ct->btrn[1] && ct->status == CL_SENDING)
427 btr_remove_node(&ct->btrn[0]);
428 btr_remove_node(&ct->btrn[1]);
429 if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
430 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
431 if (ct->scc.fd >= 0) {
435 free_argv(ct->features);
437 sc_free(ct->scc.recv);
439 sc_free(ct->scc.send);
445 * Connect to para_server and register the client task.
447 * \param ct The initialized client task structure.
448 * \param s The scheduler instance to register the client task to.
449 * \param parent The parent node of the client btr node.
450 * \param child The child node of the client node.
452 * The client task structure given by \a ct must be allocated and initialized
453 * by \ref client_parse_config() before this function is called.
457 int client_connect(struct client_task *ct, struct sched *s,
458 struct btr_node *parent, struct btr_node *child)
461 const char *host = CLIENT_OPT_STRING_VAL(HOSTNAME, ct->lpr);
462 uint32_t port = CLIENT_OPT_UINT32_VAL(SERVER_PORT, ct->lpr);
464 PARA_NOTICE_LOG("connecting %s:%u\n", host, port);
466 ret = para_connect_simple(IPPROTO_TCP, host, port);
470 ret = mark_fd_nonblocking(ct->scc.fd);
473 ct->status = CL_CONNECTED;
474 ct->btrn[0] = btr_new_node(&(struct btr_node_description)
475 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
476 ct->btrn[1] = btr_new_node(&(struct btr_node_description)
477 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
479 ct->task = task_register(&(struct task_info) {
481 .pre_select = client_pre_select,
482 .post_select = client_post_select,
492 static void handle_help_flag(struct lls_parse_result *lpr)
496 if (CLIENT_OPT_GIVEN(DETAILED_HELP, lpr))
497 help = lls_long_help(CLIENT_CMD_PTR);
498 else if (CLIENT_OPT_GIVEN(HELP, lpr))
499 help = lls_short_help(CLIENT_CMD_PTR);
502 printf("%s\n", help);
508 * Parse a client configuration.
510 * \param argc Usual argument count.
511 * \param argv Usual argument vector.
512 * \param ct_ptr Filled in by this function.
513 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
515 * This checks the command line options given by \a argc and \a argv, sets
516 * default values for the user name and the name of the rsa key file and reads
517 * further options from the config file.
519 * Upon successful return, \a ct_ptr points to a dynamically allocated and
520 * initialized client task struct.
522 * \return The number of non-option arguments in \a argc/argv on success,
523 * negative on errors.
525 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
528 const struct lls_command *cmd = CLIENT_CMD_PTR;
529 struct lls_parse_result *lpr;
531 struct client_task *ct;
532 char *kf = NULL, *user, *errctx, *home = para_homedir();
534 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
537 version_handle_flag("client", CLIENT_OPT_GIVEN(VERSION, lpr));
538 handle_help_flag(lpr);
540 ret = lsu_merge_config_file_options(CLIENT_OPT_STRING_VAL(CONFIG_FILE, lpr),
541 "client.conf", &lpr, cmd, client_suite, 0U /* default flags */);
545 ll = CLIENT_OPT_UINT32_VAL(LOGLEVEL, lpr);
548 user = CLIENT_OPT_GIVEN(USER, lpr)?
549 para_strdup(CLIENT_OPT_STRING_VAL(USER, lpr)) : para_logname();
551 if (CLIENT_OPT_GIVEN(KEY_FILE, lpr))
552 kf = para_strdup(CLIENT_OPT_STRING_VAL(KEY_FILE, lpr));
554 kf = make_message("%s/.paraslash/key.%s", home, user);
555 if (!file_exists(kf)) {
557 kf = make_message("%s/.ssh/id_rsa", home);
560 PARA_INFO_LOG("user: %s\n", user);
561 PARA_INFO_LOG("key file: %s\n", kf);
562 PARA_INFO_LOG("loglevel: %d\n", ll);
563 ct = para_calloc(sizeof(*ct));
569 ret = lls_num_inputs(lpr);
574 PARA_ERROR_LOG("%s\n", errctx);
576 lls_free_parse_result(lpr, cmd);
584 * Parse the client configuration and open a connection to para_server.
586 * \param argc See \ref client_parse_config.
587 * \param argv See \ref client_parse_config.
588 * \param ct_ptr See \ref client_parse_config.
589 * \param loglevel See \ref client_parse_config.
590 * \param parent See \ref client_connect().
591 * \param child See \ref client_connect().
592 * \param sched See \ref client_connect().
594 * This function combines client_parse_config() and client_connect(). It is
595 * considered a syntax error if no command was given, i.e. if the number
596 * of non-option arguments is zero.
600 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
601 int *loglevel, struct btr_node *parent, struct btr_node *child,
604 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
609 ret = -E_CLIENT_SYNTAX;
612 ret = client_connect(*ct_ptr, sched, parent, child);
617 client_close(*ct_ptr);