1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file command.c Client authentication and server commands. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
11 #include <arpa/inet.h>
16 #include "server.lsg.h"
34 #include "server_cmd.lsg.h"
35 #include "user_list.h"
39 #define SERVER_CMD_AUX_INFO(_arg) _arg,
40 static const unsigned server_command_perms
[] = {LSG_SERVER_CMD_AUX_INFOS
};
41 #undef SERVER_CMD_AUX_INFO
42 #define SERVER_CMD_AUX_INFO(_arg) #_arg,
43 static const char * const server_command_perms_txt
[] = {LSG_SERVER_CMD_AUX_INFOS
};
44 #undef SERVER_CMD_AUX_INFO
46 /** Commands including options must be shorter than this. */
47 #define MAX_COMMAND_LEN 32768
50 extern struct misc_meta_data
*mmd
;
51 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
52 static bool subcmd_should_die
;
54 static void command_handler_sighandler(int s
)
58 PARA_EMERG_LOG("terminating on signal %d\n", SIGTERM
);
59 subcmd_should_die
= true;
63 * Compute human readable vss status text.
65 * We can't call vss_playing() and friends here because those functions read
66 * the flags from the primary mmd structure, so calling them from command
67 * handler context would require to take the mmd lock. At the time the function
68 * is called we already took a copy of the mmd structure and want to use the
69 * flags value of the copy for computing the vss status text.
71 static char *vss_status_tohuman(unsigned int flags
)
73 if (flags
& VSS_PLAYING
)
74 return para_strdup("playing");
76 return para_strdup("stopped");
77 return para_strdup("paused");
83 static char *vss_get_status_flags(unsigned int flags
)
85 char *msg
= para_malloc(5 * sizeof(char));
87 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
88 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
89 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
90 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
95 static unsigned get_status(struct misc_meta_data
*nmmd
, bool parser_friendly
,
98 char *status
, *flags
; /* vss status info */
99 /* nobody updates our version of "now" */
100 long offset
= (nmmd
->offset
+ 500) / 1000;
101 struct timeval current_time
;
102 struct para_buffer b
= {.flags
= parser_friendly
? PBF_SIZE_PREFIX
: 0};
104 /* report real status */
105 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
106 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
107 clock_get_realtime(¤t_time
);
109 * The calls to WRITE_STATUS_ITEM() below never fail because
110 * b->max_size is zero (unlimited), see \ref para_printf(). However,
111 * clang is not smart enough to prove this and complains nevertheless.
112 * Casting the return value to void silences clang.
114 (void)WRITE_STATUS_ITEM(&b
, SI_status
, "%s\n", status
);
115 (void)WRITE_STATUS_ITEM(&b
, SI_status_flags
, "%s\n", flags
);
116 (void)WRITE_STATUS_ITEM(&b
, SI_offset
, "%li\n", offset
);
117 (void)WRITE_STATUS_ITEM(&b
, SI_afs_mode
, "%s\n", mmd
->afs_mode_string
);
118 (void)WRITE_STATUS_ITEM(&b
, SI_stream_start
, "%lu.%lu\n",
119 (long unsigned)nmmd
->stream_start
.tv_sec
,
120 (long unsigned)nmmd
->stream_start
.tv_usec
);
121 (void)WRITE_STATUS_ITEM(&b
, SI_current_time
, "%lu.%lu\n",
122 (long unsigned)current_time
.tv_sec
,
123 (long unsigned)current_time
.tv_usec
);
131 * Send a sideband packet through a blocking file descriptor.
133 * \param scc fd and crypto keys.
134 * \param buf The buffer to send.
135 * \param numbytes The size of \a buf.
136 * \param band The sideband designator of this packet.
137 * \param dont_free If true, never deallocate \a buf.
139 * The nonblock flag must be disabled for the file descriptor given by \a scc.
141 * Stream cipher encryption is automatically activated if necessary via the
142 * sideband transformation, depending on the value of \a band.
146 * \sa \ref send_sb_va().
148 int send_sb(struct stream_cipher_context
*scc
, void *buf
, size_t numbytes
,
149 int band
, bool dont_free
)
152 struct sb_context
*sbc
;
154 sb_transformation trafo
= band
< SBD_PROCEED
? NULL
: sc_trafo
;
155 struct sb_buffer sbb
= SBB_INIT(band
, buf
, numbytes
);
157 sbc
= sb_new_send(&sbb
, dont_free
, trafo
, scc
->send
);
159 ret
= sb_get_send_buffers(sbc
, iov
);
160 ret
= xwritev(scc
->fd
, iov
, ret
);
163 } while (sb_sent(sbc
, ret
) == false);
171 * Create a variable sized buffer and send it as a sideband packet.
173 * \param scc Passed to \ref send_sb.
174 * \param band See \ref send_sb.
175 * \param fmt The format string.
177 * \return The return value of the underlying call to \ref send_sb.
179 __printf_3_4
int send_sb_va(struct stream_cipher_context
*scc
, int band
,
180 const char *fmt
, ...)
187 ret
= xvasprintf(&msg
, fmt
, ap
);
189 return send_sb(scc
, msg
, ret
, band
, false);
193 * Send an error message to a client.
195 * \param cc Client info.
196 * \param err The (positive) error code.
198 * \return The return value of the underlying call to send_sb_va().
200 int send_strerror(struct command_context
*cc
, int err
)
202 return send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", para_strerror(err
));
206 * Send an error context to a client,
208 * \param cc Client info.
209 * \param errctx The error context string.
211 * \return The return value of the underlying call to send_sb_va().
213 * This function frees the error context string after it was sent.
215 int send_errctx(struct command_context
*cc
, char *errctx
)
221 ret
= send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", errctx
);
226 static int check_sender_args(struct command_context
*cc
,
227 struct lls_parse_result
*lpr
, struct sender_command_data
*scd
)
230 const char * const subcmds
[] = {SENDER_SUBCOMMANDS
};
233 unsigned num_inputs
= lls_num_inputs(lpr
);
235 scd
->sender_num
= -1;
236 ret
= lls(lls_check_arg_count(lpr
, 2, INT_MAX
, &errctx
));
238 send_errctx(cc
, errctx
);
241 arg
= lls_input(0, lpr
);
243 if (strcmp(senders
[i
]->name
, arg
) == 0)
246 return -E_COMMAND_SYNTAX
;
248 arg
= lls_input(1, lpr
);
249 for (i
= 0; i
< NUM_SENDER_CMDS
; i
++)
250 if (!strcmp(subcmds
[i
], arg
))
252 if (i
== NUM_SENDER_CMDS
)
253 return -E_COMMAND_SYNTAX
;
255 if (!senders
[scd
->sender_num
]->client_cmds
[scd
->cmd_num
])
256 return -E_SENDER_CMD
;
257 switch (scd
->cmd_num
) {
261 return -E_COMMAND_SYNTAX
;
265 if (num_inputs
!= 3 || parse_cidr(lls_input(2, lpr
), scd
->host
,
266 sizeof(scd
->host
), &scd
->netmask
) == NULL
)
267 return -E_COMMAND_SYNTAX
;
272 return -E_COMMAND_SYNTAX
;
273 return parse_fec_url(lls_input(2, lpr
), scd
);
275 return -E_COMMAND_SYNTAX
;
281 * Send a sideband packet through a blocking file descriptor.
283 * \param scc fd and crypto keys.
284 * \param expected_band The expected band designator.
285 * \param max_size Passed to \ref sb_new_recv().
286 * \param result Body of the sideband packet is returned here.
288 * If \a expected_band is not \p SBD_ANY, the band designator of the received
289 * sideband packet is compared to \a expected_band and a mismatch is considered
294 int recv_sb(struct stream_cipher_context
*scc
,
295 enum sb_designator expected_band
,
296 size_t max_size
, struct iovec
*result
)
299 struct sb_context
*sbc
;
301 struct sb_buffer sbb
;
302 sb_transformation trafo
;
304 trafo
= expected_band
!= SBD_ANY
&& expected_band
< SBD_PROCEED
?
306 sbc
= sb_new_recv(max_size
, trafo
, scc
->recv
);
308 sb_get_recv_buffer(sbc
, &iov
);
309 ret
= recv_bin_buffer(scc
->fd
, iov
.iov_base
, iov
.iov_len
);
314 ret
= sb_received(sbc
, ret
, &sbb
);
321 if (expected_band
!= SBD_ANY
&& sbb
.band
!= expected_band
)
330 static int com_sender(struct command_context
*cc
, struct lls_parse_result
*lpr
)
334 struct sender_command_data scd
;
336 if (lls_num_inputs(lpr
) == 0) {
339 ret
= xasprintf(&tmp
, "%s%s\n", msg
? msg
: "",
344 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
346 ret
= check_sender_args(cc
, lpr
, &scd
);
348 if (scd
.sender_num
< 0)
350 if (strcmp(lls_input(1, lpr
), "status") == 0)
351 msg
= senders
[scd
.sender_num
]->status();
353 msg
= senders
[scd
.sender_num
]->help();
354 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
357 switch (scd
.cmd_num
) {
360 assert(senders
[scd
.sender_num
]->resolve_target
);
361 ret
= senders
[scd
.sender_num
]->resolve_target(lls_input(2, lpr
),
367 for (i
= 0; i
< 10; i
++) {
368 mutex_lock(mmd_mutex
);
369 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
370 /* another sender command is active, retry in 100ms */
371 struct timespec ts
= {.tv_nsec
= 100 * 1000 * 1000};
372 mutex_unlock(mmd_mutex
);
373 nanosleep(&ts
, NULL
);
376 mmd
->sender_cmd_data
= scd
;
377 mutex_unlock(mmd_mutex
);
380 return (i
< 10)? 1 : -E_LOCK
;
382 EXPORT_SERVER_CMD_HANDLER(sender
);
384 static int com_si(struct command_context
*cc
,
385 __a_unused
struct lls_parse_result
*lpr
)
390 ut
= daemon_get_uptime_str(now
);
391 mutex_lock(mmd_mutex
);
392 ret
= xasprintf(&msg
,
393 "up: %s\nplayed: %u\n"
396 "connections (active/accepted/total): %u/%u/%u\n"
397 "current loglevel: %s\n"
398 "supported audio formats: %s\n",
402 mmd
->active_connections
,
405 ENUM_STRING_VAL(LOGLEVEL
),
406 AUDIO_FORMAT_HANDLERS
408 mutex_unlock(mmd_mutex
);
410 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
412 EXPORT_SERVER_CMD_HANDLER(si
);
414 static int com_version(struct command_context
*cc
, struct lls_parse_result
*lpr
)
419 if (SERVER_CMD_OPT_GIVEN(VERSION
, VERBOSE
, lpr
))
420 len
= xasprintf(&msg
, "%s", version_text("server"));
422 len
= xasprintf(&msg
, "%s\n", version_single_line("server"));
423 return send_sb(&cc
->scc
, msg
, len
, SBD_OUTPUT
, false);
425 EXPORT_SERVER_CMD_HANDLER(version
);
427 /** These status items are cleared if no audio file is currently open. */
428 #define EMPTY_STATUS_ITEMS \
433 ITEM(attributes_bitmap) \
434 ITEM(attributes_txt) \
445 ITEM(seconds_total) \
458 ITEM(amplification) \
461 * Create a set of audio-file related status items with empty values. These are
462 * written to stat clients when no audio file is open.
464 static unsigned empty_status_items(bool parser_friendly
, char **result
)
470 len
= xasprintf(&esi
,
471 #define ITEM(x) "0004 %02x:\n"
474 #define ITEM(x) , (unsigned) SI_ ## x
479 len
= xasprintf(&esi
,
480 #define ITEM(x) "%s:\n"
483 #define ITEM(x) ,status_item_list[SI_ ## x]
490 #undef EMPTY_STATUS_ITEMS
492 static int com_stat(struct command_context
*cc
, struct lls_parse_result
*lpr
)
495 struct misc_meta_data tmp
, *nmmd
= &tmp
;
497 bool parser_friendly
= SERVER_CMD_OPT_GIVEN(STAT
, PARSER_FRIENDLY
,
499 uint32_t num
= SERVER_CMD_UINT32_VAL(STAT
, NUM
, lpr
);
500 const struct timespec ts
= {.tv_sec
= 50, .tv_nsec
= 0};
502 para_sigaction(SIGINT
, SIG_IGN
);
503 para_sigaction(SIGUSR1
, command_handler_sighandler
);
504 para_sigaction(SIGTERM
, command_handler_sighandler
);
506 * Simply checking subcmd_should_die is racy because a signal may
507 * arrive after the check but before the subsequent call to sleep(3).
508 * If this happens, sleep(3) would not be interrupted by the signal.
509 * To avoid this we block SIGTERM here and allow it to arrive only
512 para_block_signal(SIGTERM
);
516 * Copy the mmd structure to minimize the time we hold the mmd
519 mutex_lock(mmd_mutex
);
521 mutex_unlock(mmd_mutex
);
522 ret
= get_status(nmmd
, parser_friendly
, &s
);
523 ret
= send_sb(&cc
->scc
, s
, ret
, SBD_OUTPUT
, false);
526 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
528 ret
= empty_status_items(parser_friendly
, &esi
);
529 ret
= send_sb(&cc
->scc
, esi
, ret
, SBD_OUTPUT
, false);
533 send_afs_status(cc
, parser_friendly
);
535 if (num
> 0 && !--num
)
537 sigemptyset(&set
); /* empty set means: unblock all signals */
539 * pselect(2) allows to atomically unblock signals, then go to
540 * sleep. Calling sigprocmask(2) followed by sleep(3) would
541 * open a race window similar to the one described above.
543 pselect(1, NULL
, NULL
, NULL
, &ts
, &set
);
544 if (subcmd_should_die
)
546 ret
= -E_SERVER_CRASH
;
553 EXPORT_SERVER_CMD_HANDLER(stat
);
555 /* fixed-length, human readable permission string */
556 const char *server_cmd_perms_str(unsigned int perms
)
558 static char result
[5];
560 result
[0] = perms
& AFS_READ
? 'a' : '-';
561 result
[1] = perms
& AFS_WRITE
? 'A' : '-';
562 result
[2] = perms
& VSS_READ
? 'v' : '-';
563 result
[3] = perms
& VSS_WRITE
? 'V' : '-';
568 static int send_list_of_commands(struct command_context
*cc
)
571 const struct lls_command
*cmd
;
572 char *msg
= para_strdup("");
574 for (i
= 1; (cmd
= lls_cmd(i
, server_cmd_suite
)); i
++) {
575 const char *perms
= server_cmd_perms_str(server_command_perms
[i
]);
576 char *tmp
= make_message("%s%s\t%s\t%s\n", msg
,
577 lls_command_name(cmd
), perms
, lls_purpose(cmd
));
581 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
584 static int com_help(struct command_context
*cc
, struct lls_parse_result
*lpr
)
587 char *long_help
, *buf
, *errctx
;
589 const struct lls_command
*cmd
;
591 ret
= lls(lls_check_arg_count(lpr
, 0, 1, &errctx
));
593 send_errctx(cc
, errctx
);
596 if (lls_num_inputs(lpr
) == 0)
597 return send_list_of_commands(cc
);
598 /* argument given for help */
599 ret
= lls(lls_lookup_subcmd(lls_input(0, lpr
), server_cmd_suite
,
602 send_errctx(cc
, errctx
);
605 cmd
= lls_cmd(ret
, server_cmd_suite
);
606 perms
= server_command_perms_txt
[ret
];
607 long_help
= lls_long_help(cmd
);
609 ret
= xasprintf(&buf
, "%spermissions: %s\n", long_help
, perms
);
611 return send_sb(&cc
->scc
, buf
, ret
, SBD_OUTPUT
, false);
613 EXPORT_SERVER_CMD_HANDLER(help
);
615 static int com_hup(__a_unused
struct command_context
*cc
,
616 __a_unused
struct lls_parse_result
*lpr
)
618 kill(getppid(), SIGHUP
);
621 EXPORT_SERVER_CMD_HANDLER(hup
);
623 static int com_term(__a_unused
struct command_context
*cc
,
624 __a_unused
struct lls_parse_result
*lpr
)
626 kill(getppid(), SIGTERM
);
629 EXPORT_SERVER_CMD_HANDLER(term
);
631 static int com_play(__a_unused
struct command_context
*cc
,
632 __a_unused
struct lls_parse_result
*lpr
)
634 mutex_lock(mmd_mutex
);
635 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
636 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
637 mutex_unlock(mmd_mutex
);
640 EXPORT_SERVER_CMD_HANDLER(play
);
642 static int com_stop(__a_unused
struct command_context
*cc
,
643 __a_unused
struct lls_parse_result
*lpr
)
645 mutex_lock(mmd_mutex
);
646 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
647 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
648 mmd
->new_vss_status_flags
|= VSS_NEXT
;
649 mutex_unlock(mmd_mutex
);
652 EXPORT_SERVER_CMD_HANDLER(stop
);
654 static int com_pause(__a_unused
struct command_context
*cc
,
655 __a_unused
struct lls_parse_result
*lpr
)
657 mutex_lock(mmd_mutex
);
658 if (!vss_paused() && !vss_stopped()) {
660 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
661 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
663 mutex_unlock(mmd_mutex
);
666 EXPORT_SERVER_CMD_HANDLER(pause
);
668 static int com_next(__a_unused
struct command_context
*cc
,
669 __a_unused
struct lls_parse_result
*lpr
)
671 mutex_lock(mmd_mutex
);
673 mmd
->new_vss_status_flags
|= VSS_NEXT
;
674 mutex_unlock(mmd_mutex
);
677 EXPORT_SERVER_CMD_HANDLER(next
);
679 static int com_nomore(__a_unused
struct command_context
*cc
,
680 __a_unused
struct lls_parse_result
*lpr
)
682 mutex_lock(mmd_mutex
);
683 if (vss_playing() || vss_paused())
684 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
685 mutex_unlock(mmd_mutex
);
688 EXPORT_SERVER_CMD_HANDLER(nomore
);
690 static int com_ff(__a_unused
struct command_context
*cc
,
691 struct lls_parse_result
*lpr
)
694 int ret
, backwards
= 0;
698 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
700 send_errctx(cc
, errctx
);
703 if (!(ret
= sscanf(lls_input(0, lpr
), "%u%c", &i
, &c
)))
704 return -E_COMMAND_SYNTAX
;
705 if (ret
> 1 && c
== '-')
706 backwards
= 1; /* jmp backwards */
707 mutex_lock(mmd_mutex
);
708 ret
= -E_NO_AUDIO_FILE
;
709 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
711 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
713 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
715 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
718 if (promille
> 1000) {
719 mmd
->new_vss_status_flags
|= VSS_NEXT
;
722 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
723 mmd
->new_vss_status_flags
|= VSS_REPOS
;
724 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
728 mutex_unlock(mmd_mutex
);
731 EXPORT_SERVER_CMD_HANDLER(ff
);
733 static int com_jmp(__a_unused
struct command_context
*cc
,
734 struct lls_parse_result
*lpr
)
740 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
742 send_errctx(cc
, errctx
);
745 if (sscanf(lls_input(0, lpr
), "%lu", &i
) <= 0)
746 return -ERRNO_TO_PARA_ERROR(EINVAL
);
747 mutex_lock(mmd_mutex
);
748 ret
= -E_NO_AUDIO_FILE
;
749 if (!mmd
->afd
.afhi
.chunks_total
)
753 PARA_INFO_LOG("jumping to %lu%%\n", i
);
754 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50) / 100;
755 PARA_INFO_LOG("sent: %lu, offset before jmp: %li\n",
756 mmd
->chunks_sent
, mmd
->offset
);
757 mmd
->new_vss_status_flags
|= VSS_REPOS
;
758 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
762 mutex_unlock(mmd_mutex
);
765 EXPORT_SERVER_CMD_HANDLER(jmp
);
767 /* deprecated, does nothing */
768 static int com_tasks(__a_unused
struct command_context
*cc
,
769 __a_unused
struct lls_parse_result
*lpr
)
773 EXPORT_SERVER_CMD_HANDLER(tasks
);
775 static void reset_signals(void)
777 para_sigaction(SIGCHLD
, SIG_IGN
);
778 para_sigaction(SIGINT
, SIG_DFL
);
779 para_sigaction(SIGTERM
, SIG_DFL
);
780 para_sigaction(SIGHUP
, SIG_DFL
);
783 struct connection_features
{
784 int dummy
; /* none at the moment */
787 static int parse_auth_request(char *buf
, int len
, const struct user
**u
,
788 struct connection_features
*cf
)
791 char *p
, *username
, **features
= NULL
;
792 size_t auth_rq_len
= strlen(AUTH_REQUEST_MSG
);
795 memset(cf
, 0, sizeof(*cf
));
796 if (len
< auth_rq_len
+ 2)
797 return -E_AUTH_REQUEST
;
798 if (strncmp(buf
, AUTH_REQUEST_MSG
, auth_rq_len
) != 0)
799 return -E_AUTH_REQUEST
;
800 username
= buf
+ auth_rq_len
;
801 p
= strchr(username
, ' ');
805 return -E_AUTH_REQUEST
;
808 create_argv(p
, ",", &features
);
809 for (i
= 0; features
[i
]; i
++) {
810 if (strcmp(features
[i
], "sideband") == 0)
812 if (strcmp(features
[i
], "aes_ctr128") == 0)
815 ret
= -E_BAD_FEATURE
;
820 PARA_DEBUG_LOG("received auth request for user %s\n", username
);
821 *u
= user_list_lookup(username
);
828 #define HANDSHAKE_BUFSIZE 4096
830 static int run_command(struct command_context
*cc
, struct iovec
*iov
)
833 char *p
, *end
, **argv
;
834 const struct lls_command
*lcmd
= NULL
;
836 struct lls_parse_result
*lpr
;
839 if (iov
->iov_base
== NULL
|| iov
->iov_len
== 0)
840 return -ERRNO_TO_PARA_ERROR(EINVAL
);
842 p
[iov
->iov_len
- 1] = '\0'; /* just to be sure */
844 ret
= lls(lls_lookup_subcmd(p
, server_cmd_suite
, &errctx
));
846 send_errctx(cc
, errctx
);
849 perms
= server_command_perms
[ret
];
850 if ((perms
& cc
->u
->perms
) != perms
)
852 lcmd
= lls_cmd(ret
, server_cmd_suite
);
853 end
= iov
->iov_base
+ iov
->iov_len
;
854 for (i
= 0; p
< end
; i
++)
857 argv
= para_malloc((argc
+ 1) * sizeof(char *));
858 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++) {
859 argv
[i
] = para_strdup(p
);
863 PARA_NOTICE_LOG("calling com_%s() for user %s\n",
864 lls_command_name(lcmd
), cc
->u
->name
);
865 ret
= lls(lls_parse(argc
, argv
, lcmd
, &lpr
, &errctx
));
867 const struct server_cmd_user_data
*ud
= lls_user_data(lcmd
);
868 ret
= ud
->handler(cc
, lpr
);
869 lls_free_parse_result(lpr
, lcmd
);
871 send_errctx(cc
, errctx
);
873 mutex_lock(mmd_mutex
);
875 if (ret
>= 0 && (perms
& AFS_WRITE
))
877 mutex_unlock(mmd_mutex
);
882 * Perform user authentication and execute a command.
884 * \param fd The file descriptor to send output to.
886 * Whenever para_server accepts an incoming tcp connection on the port it
887 * listens on, it forks and the resulting child calls this function.
889 * An RSA-based challenge/response is used to authenticate the peer. It that
890 * authentication succeeds, a random session key is generated and sent back to
891 * the peer, encrypted with its RSA public key. From this point on, all
892 * transfers are crypted with this session key.
894 * Next it is checked if the peer supplied a valid server command or a command
895 * for the audio file selector. If yes, and if the user has sufficient
896 * permissions to execute that command, the function calls the corresponding
897 * command handler which does argument checking and further processing.
899 * In order to cope with DOS attacks, a timeout is set up which terminates
900 * the function if the connection was not authenticated when the timeout
905 * \sa alarm(2), \ref crypt.c, \ref crypt.h.
907 int handle_connect(int fd
)
910 unsigned char rand_buf
[CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
911 unsigned char challenge_hash
[HASH_SIZE
];
912 char *command
= NULL
, *buf
= para_malloc(HANDSHAKE_BUFSIZE
) /* must be on the heap */;
914 struct command_context cc_struct
= {.u
= NULL
}, *cc
= &cc_struct
;
916 struct connection_features cf
;
920 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
921 ret
= mark_fd_blocking(fd
);
924 /* send Welcome message */
925 ret
= write_va_buffer(fd
, "This is para_server, version "
926 PACKAGE_VERSION
".\n"
927 "Features: sideband,aes_ctr128\n"
931 /* recv auth request line */
932 ret
= recv_buffer(fd
, buf
, HANDSHAKE_BUFSIZE
);
935 ret
= parse_auth_request(buf
, ret
, &cc
->u
, &cf
);
939 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
940 ret
= pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
941 (unsigned char *)buf
);
947 * We don't want to reveal our user names, so we send a
948 * challenge to the client even if the user does not exist, and
949 * fail the authentication later.
952 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
954 PARA_DEBUG_LOG("sending %d byte challenge + session key (%zu bytes)\n",
955 CHALLENGE_SIZE
, numbytes
);
956 ret
= send_sb(&cc
->scc
, buf
, numbytes
, SBD_CHALLENGE
, false);
960 ret
= recv_sb(&cc
->scc
, SBD_CHALLENGE_RESPONSE
,
961 HANDSHAKE_BUFSIZE
, &iov
);
965 numbytes
= iov
.iov_len
;
966 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes
);
971 * The correct response is the hash of the first CHALLENGE_SIZE bytes
972 * of the random data.
975 if (numbytes
!= HASH_SIZE
)
977 hash_function((char *)rand_buf
, CHALLENGE_SIZE
, challenge_hash
);
978 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
980 /* auth successful */
982 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
983 /* init stream cipher keys with the second part of the random buffer */
984 cc
->scc
.recv
= sc_new(rand_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
);
985 cc
->scc
.send
= sc_new(rand_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
987 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_PROCEED
, false);
990 ret
= recv_sb(&cc
->scc
, SBD_COMMAND
, MAX_COMMAND_LEN
, &iov
);
993 ret
= run_command(cc
, &iov
);
1000 if (send_strerror(cc
, -ret
) >= 0)
1001 send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__FAILURE
, true);
1003 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1007 mutex_lock(mmd_mutex
);
1008 mmd
->active_connections
--;
1009 mutex_unlock(mmd_mutex
);
1011 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__SUCCESS
, true);
1013 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1015 sc_free(cc
->scc
.recv
);
1016 sc_free(cc
->scc
.send
);