opus_common.c: Fix trival whitespace issue.
[paraslash.git] / client_common.c
1 /*
2  * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
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         ret = sb_received(ct->sbc[0], n, result);
163         if (ret < 0)
164                 return ret;
165         if (ret == 0)
166                 goto again;
167         ct->sbc[0] = NULL;
168         return 1;
169 }
170
171
172 static char **parse_features(char *buf)
173 {
174         int i;
175         const char id[] = "\nFeatures: ";
176         char *p, *q, **features;
177
178         p = strstr(buf, id);
179         if (!p)
180                 return NULL;
181         p += strlen(id);
182         q = strchr(p, '\n');
183         if (!q)
184                 return NULL;
185         *q = '\0';
186         create_argv(p, ",", &features);
187         for (i = 0; features[i]; i++)
188                 PARA_INFO_LOG("server feature: %s\n", features[i]);
189         return features;
190 }
191
192 static int dispatch_sbb(struct client_task *ct, struct sb_buffer *sbb)
193 {
194         int ret;
195         const char *designator[] = {SB_DESIGNATORS_ARRAY};
196
197         if (!sbb)
198                 return 0;
199         if (sbb->band < NUM_SB_DESIGNATORS)
200                 PARA_DEBUG_LOG("band: %s\n", designator[sbb->band]);
201
202         switch (sbb->band) {
203         case SBD_AWAITING_DATA:
204                 ct->status = CL_SENDING;
205                 ret = 1;
206                 goto out;
207         case SBD_OUTPUT:
208                 if (iov_valid(&sbb->iov))
209                         btr_add_output(sbb->iov.iov_base, sbb->iov.iov_len,
210                                 ct->btrn[0]);
211                 ret = 1;
212                 goto out;
213         case SBD_DEBUG_LOG:
214         case SBD_INFO_LOG:
215         case SBD_NOTICE_LOG:
216         case SBD_WARNING_LOG:
217         case SBD_ERROR_LOG:
218         case SBD_CRIT_LOG:
219         case SBD_EMERG_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);
223                 }
224                 ret = 1;
225                 goto deallocate;
226         case SBD_EXIT__SUCCESS:
227                 ret = -E_SERVER_CMD_SUCCESS;
228                 goto deallocate;
229         case SBD_EXIT__FAILURE:
230                 ret = -E_SERVER_CMD_FAILURE;
231                 goto deallocate;
232         default:
233                 PARA_ERROR_LOG("invalid band %d\n", sbb->band);
234                 ret = -E_BAD_BAND;
235                 goto deallocate;
236         }
237 deallocate:
238         free(sbb->iov.iov_base);
239 out:
240         sbb->iov.iov_base = NULL;
241         return ret;
242 }
243
244 static bool has_feature(const char *feature, struct client_task *ct)
245 {
246         return find_arg(feature, ct->features) >= 0? true : false;
247 }
248
249 static int send_sb_command(struct client_task *ct)
250 {
251         int i;
252         char *command, *p;
253         size_t len = 0;
254
255         if (ct->sbc[1])
256                 return send_sb(ct, 0, NULL, 0, 0, false);
257
258         for (i = 0; i < ct->conf.inputs_num; i++)
259                 len += strlen(ct->conf.inputs[i]) + 1;
260         p = command = para_malloc(len);
261         for (i = 0; i < ct->conf.inputs_num; i++) {
262                 strcpy(p, ct->conf.inputs[i]);
263                 p += strlen(ct->conf.inputs[i]) + 1;
264         }
265         PARA_DEBUG_LOG("--> %s\n", command);
266         return send_sb(ct, 0, command, len, SBD_COMMAND, false);
267 }
268
269 /*
270  * The post select hook for client commands.
271  *
272  * Depending on the current state of the connection and the status of the read
273  * and write fd sets of s, this function performs the necessary steps to
274  * authenticate the connection, to send the command given by t->private_data
275  * and to receive para_server's output, if any.
276  */
277 static int client_post_select(struct sched *s, void *context)
278 {
279         struct client_task *ct = context;
280         int ret = 0;
281         size_t n;
282         char buf[CLIENT_BUFSIZE];
283
284         ret = task_get_notification(ct->task);
285         if (ret < 0)
286                 goto out;
287         if (ct->scc.fd < 0)
288                 return 0;
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)
293                         goto out;
294                 ct->features = parse_features(buf);
295                 if (!has_feature("sideband", ct)) {
296                         PARA_ERROR_LOG("server has no sideband support\n");
297                         ret = -E_INCOMPAT_FEAT;
298                         goto out;
299                 }
300                 ct->status = CL_RECEIVED_WELCOME;
301                 return 0;
302         case CL_RECEIVED_WELCOME: /* send auth command */
303                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
304                         return 0;
305                 sprintf(buf, AUTH_REQUEST_MSG "%s sideband%s", ct->user,
306                         has_feature("aes_ctr128", ct)? ",aes_ctr128" : "");
307                 PARA_INFO_LOG("--> %s\n", buf);
308                 ret = write_buffer(ct->scc.fd, buf);
309                 if (ret < 0)
310                         goto out;
311                 ct->status = CL_SENT_AUTH;
312                 return 0;
313         case CL_SENT_AUTH:
314                 /*
315                  * Receive challenge and session keys, decrypt the challenge and
316                  * send back the hash of the decrypted challenge.
317                  */
318                 {
319                 /* decrypted challenge/session key buffer */
320                 unsigned char crypt_buf[1024];
321                 struct sb_buffer sbb;
322                 bool use_aes;
323
324                 ret = recv_sb(ct, &s->rfds, &sbb);
325                 if (ret <= 0)
326                         goto out;
327                 if (sbb.band != SBD_CHALLENGE) {
328                         ret = -E_BAD_BAND;
329                         free(sbb.iov.iov_base);
330                                 goto out;
331                 }
332                 n = sbb.iov.iov_len;
333                 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
334                 ret = priv_decrypt(ct->key_file, crypt_buf,
335                         sbb.iov.iov_base, n);
336                 free(sbb.iov.iov_base);
337                 if (ret < 0)
338                         goto out;
339                 ct->challenge_hash = para_malloc(HASH_SIZE);
340                 hash_function((char *)crypt_buf, CHALLENGE_SIZE, ct->challenge_hash);
341                 use_aes = has_feature("aes_ctr128", ct);
342                 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN, use_aes);
343                 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
344                         SESSION_KEY_LEN, use_aes);
345                 hash_to_asc(ct->challenge_hash, buf);
346                 PARA_INFO_LOG("--> %s\n", buf);
347                 ct->status = CL_RECEIVED_CHALLENGE;
348                 return 0;
349                 }
350         case CL_RECEIVED_CHALLENGE:
351                 ret = send_sb(ct, 0, ct->challenge_hash, HASH_SIZE,
352                         SBD_CHALLENGE_RESPONSE, false);
353                 if (ret != 0)
354                         ct->challenge_hash = NULL;
355                 if (ret <= 0)
356                         goto out;
357                 ct->status = CL_SENT_CH_RESPONSE;
358                 goto out;
359         case CL_SENT_CH_RESPONSE: /* read server response */
360                 {
361                 struct sb_buffer sbb;
362                 ret = recv_sb(ct, &s->rfds, &sbb);
363                 if (ret <= 0)
364                         goto out;
365                 free(sbb.iov.iov_base);
366                 if (sbb.band != SBD_PROCEED)
367                         ret = -E_BAD_BAND;
368                 else
369                         ct->status = CL_RECEIVED_PROCEED;
370                 goto out;
371                 }
372         case CL_RECEIVED_PROCEED: /* concat args and send command */
373                 {
374                 if (!FD_ISSET(ct->scc.fd, &s->wfds))
375                         return 0;
376                 ret = send_sb_command(ct);
377                 if (ret <= 0)
378                         goto out;
379                 ct->status = CL_EXECUTING;
380                 return 0;
381                 }
382         case CL_SENDING:
383                 if (ct->btrn[1]) {
384                         char *buf2;
385                         size_t sz;
386                         ret = btr_node_status(ct->btrn[1], 0, BTR_NT_LEAF);
387                         if (ret == -E_BTR_EOF) {
388                                 /* empty blob data packet indicates EOF */
389                                 PARA_INFO_LOG("blob sent\n");
390                                 ret = send_sb(ct, 1, NULL, 0, SBD_BLOB_DATA, true);
391                                 if (ret >= 0)
392                                         ret = -E_BTR_EOF;
393                         }
394                         if (ret < 0)
395                                 goto close1;
396                         if (ret > 0 && FD_ISSET(ct->scc.fd, &s->wfds)) {
397                                 sz = btr_next_buffer(ct->btrn[1], &buf2);
398                                 assert(sz);
399                                 ret = send_sb(ct, 1, buf2, sz, SBD_BLOB_DATA, true);
400                                 if (ret < 0)
401                                         goto close1;
402                                 if (ret > 0)
403                                         btr_consume(ct->btrn[1], sz);
404                         }
405                 }
406                 /* fall through */
407         case CL_EXECUTING:
408                 if (ct->btrn[0]) {
409                         ret = btr_node_status(ct->btrn[0], 0, BTR_NT_ROOT);
410                         if (ret < 0)
411                                 goto close0;
412                         if (ret > 0 && FD_ISSET(ct->scc.fd, &s->rfds)) {
413                                 struct sb_buffer sbb;
414                                 ret = recv_sb(ct, &s->rfds, &sbb);
415                                 if (ret < 0)
416                                         goto close0;
417                                 if (ret > 0) {
418                                         ret = dispatch_sbb(ct, &sbb);
419                                         if (ret < 0)
420                                                 goto close0;
421                                 }
422                         }
423                 }
424                 ret = 0;
425                 goto out;
426         }
427 close1:
428         PARA_INFO_LOG("channel 1: %s\n", para_strerror(-ret));
429         btr_remove_node(&ct->btrn[1]);
430         if (ct->btrn[0])
431                 return 0;
432         goto out;
433 close0:
434         PARA_INFO_LOG("channel 0: %s\n", para_strerror(-ret));
435         btr_remove_node(&ct->btrn[0]);
436         if (ct->btrn[1] && ct->status == CL_SENDING)
437                 return 0;
438 out:
439         if (ret >= 0)
440                 return 0;
441         btr_remove_node(&ct->btrn[0]);
442         btr_remove_node(&ct->btrn[1]);
443         if (ret != -E_SERVER_CMD_SUCCESS && ret != -E_SERVER_CMD_FAILURE)
444                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
445         if (ct->scc.fd >= 0) {
446                 close(ct->scc.fd);
447                 ct->scc.fd = -1;
448         }
449         free_argv(ct->features);
450         ct->features = NULL;
451         sc_free(ct->scc.recv);
452         ct->scc.recv = NULL;
453         sc_free(ct->scc.send);
454         ct->scc.send = NULL;
455         return ret;
456 }
457
458 /**
459  * Connect to para_server and register the client task.
460  *
461  * \param ct The initialized client task structure.
462  * \param s The scheduler instance to register the client task to.
463  * \param parent The parent node of the client btr node.
464  * \param child The child node of the client node.
465  *
466  * The client task structure given by \a ct  must be allocated and initialized
467  * by \ref client_parse_config() before this function is called.
468  *
469  * \return Standard.
470  */
471 int client_connect(struct client_task *ct, struct sched *s,
472                 struct btr_node *parent, struct btr_node *child)
473 {
474         int ret;
475
476         PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
477                 ct->conf.server_port_arg);
478         ct->scc.fd = -1;
479         ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
480                                                ct->conf.server_port_arg);
481         if (ret < 0)
482                 return ret;
483         ct->scc.fd = ret;
484         ret = mark_fd_nonblocking(ct->scc.fd);
485         if (ret < 0)
486                 goto err_out;
487         ct->status = CL_CONNECTED;
488         ct->btrn[0] = btr_new_node(&(struct btr_node_description)
489                 EMBRACE(.name = "client recv", .parent = NULL, .child = child));
490         ct->btrn[1] = btr_new_node(&(struct btr_node_description)
491                 EMBRACE(.name = "client send", .parent = parent, .child = NULL));
492
493         ct->task = task_register(&(struct task_info) {
494                 .name = "client",
495                 .pre_select = client_pre_select,
496                 .post_select = client_post_select,
497                 .context = ct,
498         }, s);
499         return 1;
500 err_out:
501         close(ct->scc.fd);
502         ct->scc.fd = -1;
503         return ret;
504 }
505
506 __noreturn static void print_help_and_die(struct client_task *ct)
507 {
508         struct ggo_help h = DEFINE_GGO_HELP(client);
509         bool d = ct->conf.detailed_help_given;
510
511         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
512         exit(0);
513 }
514
515 /**
516  * Parse a client configuration.
517  *
518  * \param argc Usual argument count.
519  * \param argv Usual argument vector.
520  * \param ct_ptr Filled in by this function.
521  * \param loglevel If not \p NULL, the number of the loglevel is stored here.
522  *
523  * This checks the command line options given by \a argc and \a argv, sets
524  * default values for the user name and the name of the rsa key file and reads
525  * further options from the config file.
526  *
527  * Upon successful return, \a ct_ptr points to a dynamically allocated and
528  * initialized client task struct.
529  *
530  * \return The number of non-option arguments in \a argc/argv on success,
531  * negative on errors.
532  */
533 int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
534                 int *loglevel)
535 {
536         char *home = para_homedir();
537         int ret;
538         struct client_task *ct = para_calloc(sizeof(struct client_task));
539
540         *ct_ptr = ct;
541         ct->scc.fd = -1;
542         ret = -E_CLIENT_SYNTAX;
543         if (client_cmdline_parser(argc, argv, &ct->conf))
544                 goto out;
545         version_handle_flag("client", ct->conf.version_given);
546         if (ct->conf.help_given || ct->conf.detailed_help_given)
547                 print_help_and_die(ct);
548
549         ct->config_file = ct->conf.config_file_given?
550                 para_strdup(ct->conf.config_file_arg) :
551                 make_message("%s/.paraslash/client.conf", home);
552         ret = file_exists(ct->config_file);
553         if (!ret && ct->conf.config_file_given) {
554                 ret = -E_NO_CONFIG;
555                 goto out;
556         }
557         if (ret) {
558                 struct client_cmdline_parser_params params = {
559                         .override = 0,
560                         .initialize = 0,
561                         .check_required = 0,
562                         .check_ambiguity = 0,
563                         .print_errors = 0
564                 };
565                 ret = -E_BAD_CONFIG;
566                 if (client_cmdline_parser_config_file(ct->config_file,
567                         &ct->conf, &params))
568                         goto out;
569         }
570         ct->user = ct->conf.user_given?
571                 para_strdup(ct->conf.user_arg) : para_logname();
572
573         if (ct->conf.key_file_given)
574                 ct->key_file = para_strdup(ct->conf.key_file_arg);
575         else {
576                 ct->key_file = make_message("%s/.paraslash/key.%s",
577                         home, ct->user);
578                 if (!file_exists(ct->key_file)) {
579                         free(ct->key_file);
580                         ct->key_file = make_message("%s/.ssh/id_rsa", home);
581                 }
582         }
583
584         if (loglevel)
585                 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
586         PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
587         PARA_INFO_LOG("config_file: %s\n", ct->config_file);
588         PARA_INFO_LOG("key_file: %s\n", ct->key_file);
589         ret = ct->conf.inputs_num;
590 out:
591         free(home);
592         if (ret < 0) {
593                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
594                 client_close(ct);
595                 *ct_ptr = NULL;
596         }
597         return ret;
598 }
599
600 /**
601  * Parse the client configuration and open a connection to para_server.
602  *
603  * \param argc See \ref client_parse_config.
604  * \param argv See \ref client_parse_config.
605  * \param ct_ptr See \ref client_parse_config.
606  * \param loglevel See \ref client_parse_config.
607  * \param parent See \ref client_connect().
608  * \param child See \ref client_connect().
609  * \param sched See \ref client_connect().
610  *
611  * This function combines client_parse_config() and client_connect(). It is
612  * considered a syntax error if no command was given, i.e. if the number
613  * of non-option arguments is zero.
614  *
615  * \return Standard.
616  */
617 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
618                 int *loglevel, struct btr_node *parent, struct btr_node *child,
619                 struct sched *sched)
620 {
621         int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
622
623         if (ret < 0)
624                 return ret;
625         if (ret == 0) {
626                 ret = -E_CLIENT_SYNTAX;
627                 goto fail;
628         }
629         ret = client_connect(*ct_ptr, sched, parent, child);
630         if (ret < 0)
631                 goto fail;
632         return 1;
633 fail:
634         client_close(*ct_ptr);
635         *ct_ptr = NULL;
636         return ret;
637 }