Merge branch 't/daemon_cleanups'
[paraslash.git] / client_common.c
1 /*
2  * Copyright (C) 1997-2014 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file client_common.c Common functions of para_client and para_audiod. */
8
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "list.h"
20 #include "sched.h"
21 #include "crypt.h"
22 #include "net.h"
23 #include "fd.h"
24 #include "sideband.h"
25 #include "string.h"
26 #include "client.cmdline.h"
27 #include "client.h"
28 #include "buffer_tree.h"
29 #include "version.h"
30 #include "ggo.h"
31
32 /** The size of the receiving buffer. */
33 #define CLIENT_BUFSIZE 4000
34
35 /**
36  * Close the connection to para_server and free all resources.
37  *
38  * \param ct Pointer to the client data.
39  *
40  * \sa \ref client_open().
41  */
42 void client_close(struct client_task *ct)
43 {
44         if (!ct)
45                 return;
46         free(ct->user);
47         free(ct->config_file);
48         free(ct->key_file);
49         client_cmdline_parser_free(&ct->conf);
50         free(ct->challenge_hash);
51         sb_free(ct->sbc[0]);
52         sb_free(ct->sbc[1]);
53         free(ct);
54 }
55
56 /*
57  * The preselect hook for server commands.
58  *
59  * The task pointer must contain a pointer to the initialized client data
60  * structure as it is returned by client_open().
61  *
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.
64  */
65 static void client_pre_select(struct sched *s, void *context)
66 {
67         int ret;
68         struct client_task *ct = context;
69
70         if (ct->scc.fd < 0)
71                 return;
72         switch (ct->status) {
73         case CL_CONNECTED:
74         case CL_SENT_AUTH:
75         case CL_SENT_CH_RESPONSE:
76                 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
77                 return;
78
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);
83                 return;
84
85         case CL_SENDING:
86                 if (ct->btrn[1]) {
87                         ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
88                         if (ret < 0)
89                                 sched_min_delay(s);
90                         else if (ret > 0)
91                                 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
92                 }
93                 /* fall though */
94         case CL_EXECUTING:
95                 if (ct->btrn[0]) {
96                         ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
97                         if (ret < 0)
98                                 sched_min_delay(s);
99                         else if (ret > 0)
100                                 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
101                 }
102                 return;
103         }
104 }
105
106 static int send_sb(struct client_task *ct, int channel, void *buf, size_t numbytes,
107                 enum sb_designator band, bool dont_free)
108 {
109         int ret, fd = ct->scc.fd;
110         struct iovec iov[2];
111
112         if (!ct->sbc[channel]) {
113                 struct sb_buffer sbb;
114                 sb_transformation trafo = ct->status < CL_RECEIVED_PROCEED?
115                         NULL : sc_trafo;
116                 sbb = (typeof(sbb))SBB_INIT(band, buf, numbytes);
117                 ct->sbc[channel] = sb_new_send(&sbb, dont_free, trafo, ct->scc.send);
118         }
119         ret = sb_get_send_buffers(ct->sbc[channel], iov);
120         ret = xwritev(fd, iov, ret);
121         if (ret < 0) {
122                 sb_free(ct->sbc[channel]);
123                 ct->sbc[channel] = NULL;
124                 return ret;
125         }
126         if (sb_sent(ct->sbc[channel], ret)) {
127                 ct->sbc[channel] = NULL;
128                 return 1;
129         }
130         return 0;
131 }
132
133 static int recv_sb(struct client_task *ct, fd_set *rfds,
134                 struct sb_buffer *result)
135 {
136         int ret;
137         size_t n;
138         sb_transformation trafo;
139         void *trafo_context;
140         struct iovec iov;
141
142         if (!FD_ISSET(ct->scc.fd, rfds))
143                 return 0;
144         if (ct->status < CL_SENT_CH_RESPONSE)
145                 trafo = trafo_context = NULL;
146         else {
147                 trafo = sc_trafo;
148                 trafo_context = ct->scc.recv;
149         }
150         if (!ct->sbc[0])
151                 ct->sbc[0] = sb_new_recv(0, trafo, trafo_context);
152 again:
153         sb_get_recv_buffer(ct->sbc[0], &iov);
154         ret = read_nonblock(ct->scc.fd, iov.iov_base, iov.iov_len, rfds, &n);
155         if (ret < 0) {
156                 sb_free(ct->sbc[0]);
157                 ct->sbc[0] = NULL;
158                 return ret;
159         }
160         if (n == 0)
161                 return 0;
162         if (!sb_received(ct->sbc[0], n, result))
163                 goto again;
164         ct->sbc[0] = NULL;
165         return 1;
166 }
167
168
169 static char **parse_features(char *buf)
170 {
171         int i;
172         const char id[] = "\nFeatures: ";
173         char *p, *q, **features;
174
175         p = strstr(buf, id);
176         if (!p)
177                 return NULL;
178         p += strlen(id);
179         q = strchr(p, '\n');
180         if (!q)
181                 return NULL;
182         *q = '\0';
183         create_argv(p, ",", &features);
184         for (i = 0; features[i]; i++)
185                 PARA_INFO_LOG("server feature: %s\n", features[i]);
186         return features;
187 }
188
189 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
190 {
191         int ret;
192         const char *designator[] = {SB_DESIGNATORS_ARRAY};
193
194         if (!sbb)
195                 return 0;
196         if (sbb->band < NUM_SB_DESIGNATORS)
197                 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
198
199         switch (sbb->band) {
200         case SBD_AWAITING_DATA:
201                 ct->status = CL_SENDING;
202                 ret = 1;
203                 goto out;
204         case SBD_OUTPUT:
205                 if (iov_valid(&sbb->iov))
206                         btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
207                                 ct->btrn[0]);
208                 ret = 1;
209                 goto out;
210         case SBD_DEBUG_LOG:
211         case SBD_INFO_LOG:
212         case SBD_NOTICE_LOG:
213         case SBD_WARNING_LOG:
214         case SBD_ERROR_LOG:
215         case SBD_CRIT_LOG:
216         case SBD_EMERG_LOG:
217                 if (iov_valid(&sbb->iov)) {
218                         int ll = sbb->band - SBD_DEBUG_LOG;
219                         para_log(ll, "remote: %s", (char *)sbb->iov.iov_base);
220                 }
221                 ret = 1;
222                 goto deallocate;
223         case SBD_EXIT__SUCCESS:
224                 ret = -E_SERVER_CMD_SUCCESS;
225                 goto deallocate;
226         case SBD_EXIT__FAILURE:
227                 ret = -E_SERVER_CMD_FAILURE;
228                 goto deallocate;
229         default:
230                 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
231                 ret = -E_BAD_BAND;
232                 goto deallocate;
233         }
234 deallocate:
235         free(sbb->iov.iov_base);
236 out:
237         sbb->iov.iov_base = NULL;
238         return ret;
239 }
240
241 static bool has_feature(const char *feature, struct client_task *ct)
242 {
243         return find_arg(feature, ct->features) >= 0? true : false;
244 }
245
246 static int send_sb_command(struct client_task *ct)
247 {
248         int i;
249         char *command, *p;
250         size_t len = 0;
251
252         if (ct->sbc[1])
253                 return send_sb(ct, 0, NULL, 0, 0, false);
254
255         for (i = 0; i < ct->conf.inputs_num; i++)
256                 len += strlen(ct->conf.inputs[i]) + 1;
257         p = command = para_malloc(len);
258         for (i = 0; i < ct->conf.inputs_num; i++) {
259                 strcpy(p, ct->conf.inputs[i]);
260                 p += strlen(ct->conf.inputs[i]) + 1;
261         }
262         PARA_DEBUG_LOG("--> %s\n", command);
263         return send_sb(ct, 0, command, len, SBD_COMMAND, false);
264 }
265
266 /*
267  * The post select hook for client commands.
268  *
269  * Depending on the current state of the connection and the status of the read
270  * and write fd sets of s, this function performs the necessary steps to
271  * authenticate the connection, to send the command given by t->private_data
272  * and to receive para_server's output, if any.
273  */
274 static int client_post_select(struct sched *s, void *context)
275 {
276         struct client_task *ct = context;
277         int ret = 0;
278         size_t n;
279         char buf[CLIENT_BUFSIZE];
280
281         ret = task_get_notification(ct->task);
282         if (ret < 0)
283                 goto out;
284         if (ct->scc.fd < 0)
285                 return 0;
286         switch (ct->status) {
287         case CL_CONNECTED: /* receive welcome message */
288                 ret = read_nonblock(ct->scc.fd, buf, sizeof(buf), &s->rfds, &n);
289                 if (ret < 0 || n == 0)
290                         goto out;
291                 ct->features = parse_features(buf);
292                 if (!has_feature("sideband", ct)) {
293                         PARA_ERROR_LOG("server has no sideband support\n");
294                         ret = -E_INCOMPAT_FEAT;
295                         goto out;
296                 }
297                 ct->status = CL_RECEIVED_WELCOME;
298                 return 0;
299         case CL_RECEIVED_WELCOME: /* send auth command */
300                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
301                         return 0;
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);
306                 if (ret < 0)
307                         goto out;
308                 ct->status = CL_SENT_AUTH;
309                 return 0;
310         case CL_SENT_AUTH:
311                 /*
312                  * Receive challenge and session keys, decrypt the challenge and
313                  * send back the hash of the decrypted challenge.
314                  */
315                 {
316                 /* decrypted challenge/session key buffer */
317                 unsigned char crypt_buf[1024];
318                 struct sb_buffer sbb;
319                 bool use_aes;
320
321                 ret = recv_sb(ct, &s->rfds, &sbb);
322                 if (ret <= 0)
323                         goto out;
324                 if (sbb.band != SBD_CHALLENGE) {
325                         ret = -E_BAD_BAND;
326                         free(sbb.iov.iov_base);
327                                 goto out;
328                 }
329                 n = sbb.iov.iov_len;
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);
334                 if (ret < 0)
335                         goto out;
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;
345                 return 0;
346                 }
347         case CL_RECEIVED_CHALLENGE:
348                 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
349                         SBD_CHALLENGE_RESPONSE, false);
350                 if (ret != 0)
351                         ct->challenge_hash = NULL;
352                 if (ret <= 0)
353                         goto out;
354                 ct->status = CL_SENT_CH_RESPONSE;
355                 goto out;
356         case CL_SENT_CH_RESPONSE: /* read server response */
357                 {
358                 struct sb_buffer sbb;
359                 ret = recv_sb(ct, &s->rfds, &sbb);
360                 if (ret <= 0)
361                         goto out;
362                 free(sbb.iov.iov_base);
363                 if (sbb.band != SBD_PROCEED)
364                         ret = -E_BAD_BAND;
365                 else
366                         ct->status = CL_RECEIVED_PROCEED;
367                 goto out;
368                 }
369         case CL_RECEIVED_PROCEED: /* concat args and send command */
370                 {
371                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
372                         return 0;
373                 ret = send_sb_command(ct);
374                 if (ret <= 0)
375                         goto out;
376                 ct->status = CL_EXECUTING;
377                 return 0;
378                 }
379         case CL_SENDING:
380                 if (ct->btrn[1]) {
381                         char *buf2;
382                         size_t sz;
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);
388                                 if (ret >= 0)
389                                         ret = -E_BTR_EOF;
390                         }
391                         if (ret < 0)
392                                 goto close1;
393                         if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
394                                 sz = btr_next_buffer(ct->btrn[1], &buf2);
395                                 assert(sz);
396                                 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
397                                 if (ret < 0)
398                                         goto close1;
399                                 if (ret > 0)
400                                         btr_consume(ct->btrn[1], sz);
401                         }
402                 }
403                 /* fall though */
404         case CL_EXECUTING:
405                 if (ct->btrn[0]) {
406                         ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
407                         if (ret < 0)
408                                 goto close0;
409                         if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
410                                 struct sb_buffer sbb;
411                                 ret = recv_sb(ct, &s->rfds, &sbb);
412                                 if (ret < 0)
413                                         goto close0;
414                                 if (ret > 0) {
415                                         ret = dispatch_sbb(ct, &sbb);
416                                         if (ret < 0)
417                                                 goto close0;
418                                 }
419                         }
420                 }
421                 ret = 0;
422                 goto out;
423         }
424 close1:
425         PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
426         btr_remove_node(&ct->btrn[1]);
427         if (ct->btrn[0])
428                 return 0;
429         goto out;
430 close0:
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)
434                 return 0;
435 out:
436         if (ret >= 0)
437                 return 0;
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) {
443                 close(ct->scc.fd);
444                 ct->scc.fd = -1;
445         }
446         free_argv(ct->features);
447         ct->features = NULL;
448         sc_free(ct->scc.recv);
449         ct->scc.recv = NULL;
450         sc_free(ct->scc.send);
451         ct->scc.send = NULL;
452         return ret;
453 }
454
455 /**
456  * Connect to para_server and register the client task.
457  *
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.
462  *
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.
465  *
466  * \return Standard.
467  */
468 int client_connect(struct client_task *ct, struct sched *s,
469                 struct btr_node *parent, struct btr_node *child)
470 {
471         int ret;
472
473         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
474                 ct->conf.server_port_arg);
475         ct->scc.fd = -1;
476         ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
477                                                ct->conf.server_port_arg);
478         if (ret < 0)
479                 return ret;
480         ct->scc.fd = ret;
481         ret = mark_fd_nonblocking(ct->scc.fd);
482         if (ret < 0)
483                 goto err_out;
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));
489
490         ct->task = task_register(&(struct task_info) {
491                 .name = "client",
492                 .pre_select = client_pre_select,
493                 .post_select = client_post_select,
494                 .context = ct,
495         }, s);
496         return 1;
497 err_out:
498         close(ct->scc.fd);
499         ct->scc.fd = -1;
500         return ret;
501 }
502
503 __noreturn static void print_help_and_die(struct client_task *ct)
504 {
505         struct ggo_help h = DEFINE_GGO_HELP(client);
506         bool d = ct->conf.detailed_help_given;
507
508         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
509         exit(0);
510 }
511
512 /**
513  * Parse a client configuration.
514  *
515  * \param argc Usual argument count.
516  * \param argv Usual argument vector.
517  * \param ct_ptr Filled in by this function.
518  * \param loglevel If not \p NULL, the number of the loglevel is stored here.
519  *
520  * This checks the command line options given by \a argc and \a argv, sets
521  * default values for the user name and the name of the rsa key file and reads
522  * further options from the config file.
523  *
524  * Upon successful return, \a ct_ptr points to a dynamically allocated and
525  * initialized client task struct.
526  *
527  * \return The number of non-option arguments in \a argc/argv on success,
528  * negative on errors.
529  */
530 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
531                 int *loglevel)
532 {
533         char *home = para_homedir();
534         int ret;
535         struct client_task *ct = para_calloc(sizeof(struct client_task));
536
537         *ct_ptr = ct;
538         ct->scc.fd = -1;
539         ret = -E_CLIENT_SYNTAX;
540         if (client_cmdline_parser(argc, argv, &ct->conf))
541                 goto out;
542         version_handle_flag("client", ct->conf.version_given);
543         if (ct->conf.help_given || ct->conf.detailed_help_given)
544                 print_help_and_die(ct);
545
546         ct->config_file = ct->conf.config_file_given?
547                 para_strdup(ct->conf.config_file_arg) :
548                 make_message("%s/.paraslash/client.conf", home);
549         ret = file_exists(ct->config_file);
550         if (!ret && ct->conf.config_file_given) {
551                 ret = -E_NO_CONFIG;
552                 goto out;
553         }
554         if (ret) {
555                 struct client_cmdline_parser_params params = {
556                         .override = 0,
557                         .initialize = 0,
558                         .check_required = 0,
559                         .check_ambiguity = 0,
560                         .print_errors = 0
561                 };
562                 ret = -E_BAD_CONFIG;
563                 if (client_cmdline_parser_config_file(ct->config_file,
564                         &ct->conf, &params))
565                         goto out;
566         }
567         ct->user = ct->conf.user_given?
568                 para_strdup(ct->conf.user_arg) : para_logname();
569
570         if (ct->conf.key_file_given)
571                 ct->key_file = para_strdup(ct->conf.key_file_arg);
572         else {
573                 ct->key_file = make_message("%s/.paraslash/key.%s",
574                         home, ct->user);
575                 if (!file_exists(ct->key_file)) {
576                         free(ct->key_file);
577                         ct->key_file = make_message("%s/.ssh/id_rsa", home);
578                 }
579         }
580
581         if (loglevel)
582                 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
583         PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
584         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
585         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
586         ret = ct->conf.inputs_num;
587 out:
588         free(home);
589         if (ret < 0) {
590                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
591                 client_close(ct);
592                 *ct_ptr = NULL;
593         }
594         return ret;
595 }
596
597 /**
598  * Parse the client configuration and open a connection to para_server.
599  *
600  * \param argc See \ref client_parse_config.
601  * \param argv See \ref client_parse_config.
602  * \param ct_ptr See \ref client_parse_config.
603  * \param loglevel See \ref client_parse_config.
604  * \param parent See \ref client_connect().
605  * \param child See \ref client_connect().
606  * \param sched See \ref client_connect().
607  *
608  * This function combines client_parse_config() and client_connect(). It is
609  * considered a syntax error if no command was given, i.e. if the number
610  * of non-option arguments is zero.
611  *
612  * \return Standard.
613  */
614 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
615                 int *loglevel, struct btr_node *parent, struct btr_node *child,
616                 struct sched *sched)
617 {
618         int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
619
620         if (ret < 0)
621                 return ret;
622         if (ret == 0) {
623                 ret = -E_CLIENT_SYNTAX;
624                 goto fail;
625         }
626         ret = client_connect(*ct_ptr, sched, parent, child);
627         if (ret < 0)
628                 goto fail;
629         return 1;
630 fail:
631         client_close(*ct_ptr);
632         *ct_ptr = NULL;
633         return ret;
634 }