client: Remove unused ->config_file member.
[paraslash.git] / client_common.c
1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file client_common.c Common functions of para_client and para_audiod. */
4
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <regex.h>
8 #include <sys/types.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "client.lsg.h"
15 #include "para.h"
16 #include "error.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "crypt.h"
20 #include "net.h"
21 #include "fd.h"
22 #include "sideband.h"
23 #include "string.h"
24 #include "client.h"
25 #include "buffer_tree.h"
26 #include "version.h"
27
28 /** The size of the receiving buffer. */
29 #define CLIENT_BUFSIZE 4000
30
31 /**
32 * Close the connection to para_server and free all resources.
33 *
34 * \param ct Pointer to the client data.
35 *
36 * \sa \ref client_open().
37 */
38 void client_close(struct client_task *ct)
39 {
40 if (!ct)
41 return;
42 free(ct->user);
43 free(ct->key_file);
44 lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
45 free(ct->challenge_hash);
46 sb_free(ct->sbc[0]);
47 sb_free(ct->sbc[1]);
48 free(ct);
49 }
50
51 /*
52 * The preselect hook for server commands.
53 *
54 * The task pointer must contain a pointer to the initialized client data
55 * structure as it is returned by client_open().
56 *
57 * This function checks the state of the connection and adds the file descriptor
58 * of the connection to the read or write fd set of s accordingly.
59 */
60 static void client_pre_select(struct sched *s, void *context)
61 {
62 int ret;
63 struct client_task *ct = context;
64
65 if (ct->scc.fd < 0)
66 return;
67 switch (ct->status) {
68 case CL_CONNECTED:
69 case CL_SENT_AUTH:
70 case CL_SENT_CH_RESPONSE:
71 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
72 return;
73
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);
78 return;
79
80 case CL_SENDING:
81 if (ct->btrn[1]) {
82 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
83 if (ret < 0)
84 sched_min_delay(s);
85 else if (ret > 0)
86 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
87 }
88 /* fallthrough */
89 case CL_EXECUTING:
90 if (ct->btrn[0]) {
91 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
92 if (ret < 0)
93 sched_min_delay(s);
94 else if (ret > 0)
95 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
96 }
97 return;
98 }
99 }
100
101 static int send_sb(struct client_task *ct, int channel, void *buf, size_t numbytes,
102 enum sb_designator band, bool dont_free)
103 {
104 int ret, fd = ct->scc.fd;
105 struct iovec iov[2];
106
107 if (!ct->sbc[channel]) {
108 struct sb_buffer sbb;
109 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
110 NULL : sc_trafo;
111 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
112 ct->sbc[channel] = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
113 }
114 ret = sb_get_send_buffers(ct->sbc[channel], iov);
115 ret = xwritev(fd, iov, ret);
116 if (ret < 0) {
117 sb_free(ct->sbc[channel]);
118 ct->sbc[channel] = NULL;
119 return ret;
120 }
121 if (sb_sent(ct->sbc[channel], ret)) {
122 ct->sbc[channel] = NULL;
123 return 1;
124 }
125 return 0;
126 }
127
128 static int recv_sb(struct client_task *ct, fd_set *rfds,
129 struct sb_buffer *result)
130 {
131 int ret;
132 size_t n;
133 sb_transformation trafo;
134 void *trafo_context;
135 struct iovec iov;
136
137 if (!FD_ISSET(ct->scc.fd, rfds))
138 return 0;
139 if (ct->status < CL_SENT_CH_RESPONSE)
140 trafo = trafo_context = NULL;
141 else {
142 trafo = sc_trafo;
143 trafo_context = ct->scc.recv;
144 }
145 if (!ct->sbc[0])
146 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
147 again:
148 sb_get_recv_buffer(ct->sbc[0], &iov);
149 ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
150 if (ret < 0) {
151 sb_free(ct->sbc[0]);
152 ct->sbc[0] = NULL;
153 return ret;
154 }
155 if (n == 0)
156 return 0;
157 ret = sb_received(ct->sbc[0], n, result);
158 if (ret < 0)
159 return ret;
160 if (ret == 0)
161 goto again;
162 ct->sbc[0] = NULL;
163 return 1;
164 }
165
166
167 static char **parse_features(char *buf)
168 {
169 int i;
170 const char id[] = "\nFeatures: ";
171 char *p, *q, **features;
172
173 p = strstr(buf, id);
174 if (!p)
175 return NULL;
176 p += strlen(id);
177 q = strchr(p, '\n');
178 if (!q)
179 return NULL;
180 *q = '\0';
181 create_argv(p, ",", &features);
182 for (i = 0; features[i]; i++)
183 PARA_INFO_LOG("server feature: %s\n", features[i]);
184 return features;
185 }
186
187 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
188 {
189 int ret;
190 const char *designator[] = {SB_DESIGNATORS_ARRAY};
191
192 if (!sbb)
193 return 0;
194 if (sbb->band < NUM_SB_DESIGNATORS)
195 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
196
197 switch (sbb->band) {
198 case SBD_AWAITING_DATA:
199 ct->status = CL_SENDING;
200 ret = 1;
201 goto out;
202 case SBD_OUTPUT:
203 if (iov_valid(&sbb->iov))
204 btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
205 ct->btrn[0]);
206 ret = 1;
207 goto out;
208 case SBD_DEBUG_LOG:
209 case SBD_INFO_LOG:
210 case SBD_NOTICE_LOG:
211 case SBD_WARNING_LOG:
212 case SBD_ERROR_LOG:
213 case SBD_CRIT_LOG:
214 case SBD_EMERG_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);
218 }
219 ret = 1;
220 goto deallocate;
221 case SBD_EXIT__SUCCESS:
222 ret = -E_SERVER_CMD_SUCCESS;
223 goto deallocate;
224 case SBD_EXIT__FAILURE:
225 ret = -E_SERVER_CMD_FAILURE;
226 goto deallocate;
227 default:
228 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
229 ret = -E_BAD_BAND;
230 goto deallocate;
231 }
232 deallocate:
233 free(sbb->iov.iov_base);
234 out:
235 sbb->iov.iov_base = NULL;
236 return ret;
237 }
238
239 static int send_sb_command(struct client_task *ct)
240 {
241 int i;
242 char *command, *p;
243 size_t len = 0;
244 unsigned num_inputs = lls_num_inputs(ct->lpr);
245
246 if (ct->sbc[1])
247 return send_sb(ct, 0, NULL, 0, 0, false);
248
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);
254 strcpy(p, str);
255 p += strlen(str) + 1;
256 }
257 PARA_DEBUG_LOG("--> %s\n", command);
258 return send_sb(ct, 0, command, len, SBD_COMMAND, false);
259 }
260
261 /*
262 * The post select hook for client commands.
263 *
264 * Depending on the current state of the connection and the status of the read
265 * and write fd sets of s, this function performs the necessary steps to
266 * authenticate the connection, to send the command given by t->private_data
267 * and to receive para_server's output, if any.
268 */
269 static int client_post_select(struct sched *s, void *context)
270 {
271 struct client_task *ct = context;
272 int ret = 0;
273 size_t n;
274 char buf[CLIENT_BUFSIZE];
275
276 ret = task_get_notification(ct->task);
277 if (ret < 0)
278 goto out;
279 if (ct->scc.fd < 0)
280 return 0;
281 switch (ct->status) {
282 case CL_CONNECTED: /* receive welcome message */
283 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
284 if (ret < 0 || n == 0)
285 goto out;
286 ct->features = parse_features(buf);
287 ct->status = CL_RECEIVED_WELCOME;
288 return 0;
289 case CL_RECEIVED_WELCOME: /* send auth command */
290 if (!FD_ISSET(ct->scc.fd, &s->wfds))
291 return 0;
292 sprintf(buf, AUTH_REQUEST_MSG "%s sideband,aes_ctr128",
293 ct->user);
294 PARA_INFO_LOG("--> %s\n", buf);
295 ret = write_buffer(ct->scc.fd, buf);
296 if (ret < 0)
297 goto out;
298 ct->status = CL_SENT_AUTH;
299 return 0;
300 case CL_SENT_AUTH:
301 /*
302 * Receive challenge and session keys, decrypt the challenge and
303 * send back the hash of the decrypted challenge.
304 */
305 {
306 /* decrypted challenge/session key buffer */
307 unsigned char crypt_buf[1024];
308 struct sb_buffer sbb;
309
310 ret = recv_sb(ct, &s->rfds, &sbb);
311 if (ret <= 0)
312 goto out;
313 if (sbb.band != SBD_CHALLENGE) {
314 ret = -E_BAD_BAND;
315 free(sbb.iov.iov_base);
316 goto out;
317 }
318 n = sbb.iov.iov_len;
319 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
320 ret = priv_decrypt(ct->key_file, crypt_buf,
321 sbb.iov.iov_base, n);
322 free(sbb.iov.iov_base);
323 if (ret < 0)
324 goto out;
325 ct->challenge_hash = para_malloc(HASH_SIZE);
326 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
327 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
328 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
329 SESSION_KEY_LEN);
330 hash_to_asc(ct->challenge_hash, buf);
331 PARA_INFO_LOG("--> %s\n", buf);
332 ct->status = CL_RECEIVED_CHALLENGE;
333 return 0;
334 }
335 case CL_RECEIVED_CHALLENGE:
336 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
337 SBD_CHALLENGE_RESPONSE, false);
338 if (ret != 0)
339 ct->challenge_hash = NULL;
340 if (ret <= 0)
341 goto out;
342 ct->status = CL_SENT_CH_RESPONSE;
343 goto out;
344 case CL_SENT_CH_RESPONSE: /* read server response */
345 {
346 struct sb_buffer sbb;
347 ret = recv_sb(ct, &s->rfds, &sbb);
348 if (ret <= 0)
349 goto out;
350 free(sbb.iov.iov_base);
351 if (sbb.band != SBD_PROCEED)
352 ret = -E_BAD_BAND;
353 else
354 ct->status = CL_RECEIVED_PROCEED;
355 goto out;
356 }
357 case CL_RECEIVED_PROCEED: /* concat args and send command */
358 {
359 if (!FD_ISSET(ct->scc.fd, &s->wfds))
360 return 0;
361 ret = send_sb_command(ct);
362 if (ret <= 0)
363 goto out;
364 ct->status = CL_EXECUTING;
365 return 0;
366 }
367 case CL_SENDING:
368 if (ct->btrn[1]) {
369 char *buf2;
370 size_t sz;
371 ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
372 if (ret == -E_BTR_EOF) {
373 /* empty blob data packet indicates EOF */
374 PARA_INFO_LOG("blob sent\n");
375 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
376 if (ret >= 0)
377 ret = -E_BTR_EOF;
378 }
379 if (ret < 0)
380 goto close1;
381 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
382 sz = btr_next_buffer(ct->btrn[1], &buf2);
383 assert(sz);
384 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
385 if (ret < 0)
386 goto close1;
387 if (ret > 0)
388 btr_consume(ct->btrn[1], sz);
389 }
390 }
391 /* fall through */
392 case CL_EXECUTING:
393 if (ct->btrn[0]) {
394 ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
395 if (ret < 0)
396 goto close0;
397 if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
398 struct sb_buffer sbb;
399 ret = recv_sb(ct, &s->rfds, &sbb);
400 if (ret < 0)
401 goto close0;
402 if (ret > 0) {
403 ret = dispatch_sbb(ct, &sbb);
404 if (ret < 0)
405 goto close0;
406 }
407 }
408 }
409 ret = 0;
410 goto out;
411 }
412 close1:
413 PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
414 btr_remove_node(&ct->btrn[1]);
415 if (ct->btrn[0])
416 return 0;
417 goto out;
418 close0:
419 PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
420 btr_remove_node(&ct->btrn[0]);
421 if (ct->btrn[1] && ct->status == CL_SENDING)
422 return 0;
423 out:
424 if (ret >= 0)
425 return 0;
426 btr_remove_node(&ct->btrn[0]);
427 btr_remove_node(&ct->btrn[1]);
428 if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
429 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
430 if (ct->scc.fd >= 0) {
431 close(ct->scc.fd);
432 ct->scc.fd = -1;
433 }
434 free_argv(ct->features);
435 ct->features = NULL;
436 sc_free(ct->scc.recv);
437 ct->scc.recv = NULL;
438 sc_free(ct->scc.send);
439 ct->scc.send = NULL;
440 return ret;
441 }
442
443 /**
444 * Connect to para_server and register the client task.
445 *
446 * \param ct The initialized client task structure.
447 * \param s The scheduler instance to register the client task to.
448 * \param parent The parent node of the client btr node.
449 * \param child The child node of the client node.
450 *
451 * The client task structure given by \a ct must be allocated and initialized
452 * by \ref client_parse_config() before this function is called.
453 *
454 * \return Standard.
455 */
456 int client_connect(struct client_task *ct, struct sched *s,
457 struct btr_node *parent, struct btr_node *child)
458 {
459 int ret;
460 const char *host = CLIENT_OPT_STRING_VAL(HOSTNAME, ct->lpr);
461 uint32_t port = CLIENT_OPT_UINT32_VAL(SERVER_PORT, ct->lpr);
462
463 PARA_NOTICE_LOG("connecting %s:%u\n", host, port);
464 ct->scc.fd = -1;
465 ret = para_connect_simple(IPPROTO_TCP, host, port);
466 if (ret < 0)
467 return ret;
468 ct->scc.fd = ret;
469 ret = mark_fd_nonblocking(ct->scc.fd);
470 if (ret < 0)
471 goto err_out;
472 ct->status = CL_CONNECTED;
473 ct->btrn[0] = btr_new_node(&(struct btr_node_description)
474 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
475 ct->btrn[1] = btr_new_node(&(struct btr_node_description)
476 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
477
478 ct->task = task_register(&(struct task_info) {
479 .name = "client",
480 .pre_select = client_pre_select,
481 .post_select = client_post_select,
482 .context = ct,
483 }, s);
484 return 1;
485 err_out:
486 close(ct->scc.fd);
487 ct->scc.fd = -1;
488 return ret;
489 }
490
491 static void handle_help_flag(struct lls_parse_result *lpr)
492 {
493 char *help;
494
495 if (CLIENT_OPT_GIVEN(DETAILED_HELP, lpr))
496 help = lls_long_help(CLIENT_CMD_PTR);
497 else if (CLIENT_OPT_GIVEN(HELP, lpr))
498 help = lls_short_help(CLIENT_CMD_PTR);
499 else
500 return;
501 printf("%s\n", help);
502 free(help);
503 exit(EXIT_SUCCESS);
504 }
505
506 /**
507 * Parse a client configuration.
508 *
509 * \param argc Usual argument count.
510 * \param argv Usual argument vector.
511 * \param ct_ptr Filled in by this function.
512 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
513 *
514 * This checks the command line options given by \a argc and \a argv, sets
515 * default values for the user name and the name of the rsa key file and reads
516 * further options from the config file.
517 *
518 * Upon successful return, \a ct_ptr points to a dynamically allocated and
519 * initialized client task struct.
520 *
521 * \return The number of non-option arguments in \a argc/argv on success,
522 * negative on errors.
523 */
524 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
525 int *loglevel)
526 {
527 const struct lls_command *cmd = CLIENT_CMD_PTR;
528 void *map;
529 size_t sz;
530 struct lls_parse_result *lpr;
531 int ret, ll;
532 struct client_task *ct;
533 char *cf = NULL, *kf = NULL, *user, *errctx, *home = para_homedir();
534
535 ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
536 if (ret < 0)
537 goto out;
538 version_handle_flag("client", CLIENT_OPT_GIVEN(VERSION, lpr));
539 handle_help_flag(lpr);
540
541 if (CLIENT_OPT_GIVEN(CONFIG_FILE, lpr))
542 cf = para_strdup(CLIENT_OPT_STRING_VAL(CONFIG_FILE, lpr));
543 else
544 cf = make_message("%s/.paraslash/client.conf", home);
545 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
546 if (ret < 0) {
547 if (ret != -E_EMPTY && ret != -ERRNO_TO_PARA_ERROR(ENOENT))
548 goto out;
549 if (ret == -ERRNO_TO_PARA_ERROR(ENOENT) &&
550 CLIENT_OPT_GIVEN(CONFIG_FILE, lpr))
551 goto out;
552 } else {
553 int cf_argc;
554 char **cf_argv;
555 struct lls_parse_result *cf_lpr, *merged_lpr;
556 ret = lls(lls_convert_config(map, sz, NULL, &cf_argv, &errctx));
557 para_munmap(map, sz);
558 if (ret < 0)
559 goto out;
560 cf_argc = ret;
561 ret = lls(lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx));
562 lls_free_argv(cf_argv);
563 if (ret < 0)
564 goto out;
565 ret = lls(lls_merge(lpr, cf_lpr, cmd, &merged_lpr,
566 &errctx));
567 lls_free_parse_result(cf_lpr, cmd);
568 if (ret < 0)
569 goto out;
570 lls_free_parse_result(lpr, cmd);
571 lpr = merged_lpr;
572 }
573 /* success */
574 ll = CLIENT_OPT_UINT32_VAL(LOGLEVEL, lpr);
575 if (loglevel)
576 *loglevel = ll;
577 user = CLIENT_OPT_GIVEN(USER, lpr)?
578 para_strdup(CLIENT_OPT_STRING_VAL(USER, lpr)) : para_logname();
579
580 if (CLIENT_OPT_GIVEN(KEY_FILE, lpr))
581 kf = para_strdup(CLIENT_OPT_STRING_VAL(KEY_FILE, lpr));
582 else {
583 kf = make_message("%s/.paraslash/key.%s", home, user);
584 if (!file_exists(kf)) {
585 free(kf);
586 kf = make_message("%s/.ssh/id_rsa", home);
587 }
588 }
589 PARA_INFO_LOG("user: %s\n", user);
590 PARA_INFO_LOG("config file: %s\n", cf);
591 PARA_INFO_LOG("key file: %s\n", kf);
592 PARA_INFO_LOG("loglevel: %d\n", ll);
593 ct = para_calloc(sizeof(*ct));
594 ct->scc.fd = -1;
595 ct->lpr = lpr;
596 ct->key_file = kf;
597 ct->user = user;
598 *ct_ptr = ct;
599 ret = lls_num_inputs(lpr);
600 out:
601 free(home);
602 free(cf);
603 if (ret < 0) {
604 if (errctx)
605 PARA_ERROR_LOG("%s\n", errctx);
606 free(errctx);
607 lls_free_parse_result(lpr, cmd);
608 free(kf);
609 *ct_ptr = NULL;
610 }
611 return ret;
612 }
613
614 /**
615 * Parse the client configuration and open a connection to para_server.
616 *
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().
624 *
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.
628 *
629 * \return Standard.
630 */
631 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
632 int *loglevel, struct btr_node *parent, struct btr_node *child,
633 struct sched *sched)
634 {
635 int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
636
637 if (ret < 0)
638 return ret;
639 if (ret == 0) {
640 ret = -E_CLIENT_SYNTAX;
641 goto fail;
642 }
643 ret = client_connect(*ct_ptr, sched, parent, child);
644 if (ret < 0)
645 goto fail;
646 return 1;
647 fail:
648 client_close(*ct_ptr);
649 *ct_ptr = NULL;
650 return ret;
651 }