2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file command.c Client authentication and server commands. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
13 #include <sys/types.h>
15 #include <arpa/inet.h>
25 #include "server.cmdline.h"
38 #include "server_cmd.lsg.h"
39 #include "user_list.h"
43 #define SERVER_CMD_AUX_INFO(_arg) _arg,
44 static const unsigned server_command_perms
[] = {LSG_SERVER_CMD_AUX_INFOS
};
45 #undef SERVER_CMD_AUX_INFO
46 #define SERVER_CMD_AUX_INFO(_arg) #_arg,
47 static const char * const server_command_perms_txt
[] = {LSG_SERVER_CMD_AUX_INFOS
};
48 #undef SERVER_CMD_AUX_INFO
50 /** Commands including options must be shorter than this. */
51 #define MAX_COMMAND_LEN 32768
54 extern struct misc_meta_data
*mmd
;
55 extern struct sender senders
[];
56 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
58 static void dummy(__a_unused
int s
)
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 para_printf(). However, clang
111 * 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 *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
);
242 for (i
= 0; senders
[i
].name
; i
++)
243 if (!strcmp(senders
[i
].name
, arg
))
245 if (!senders
[i
].name
)
246 return -E_COMMAND_SYNTAX
;
248 arg
= lls_input(1, lpr
);
249 for (i
= 0; subcmds
[i
]; i
++)
250 if (!strcmp(subcmds
[i
], arg
))
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) {
337 for (i
= 0; senders
[i
].name
; i
++) {
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
,
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 * Write a list of audio-file related status items with empty values.
463 * This is used by vss when currently no audio file is open.
465 static unsigned empty_status_items(bool parser_friendly
, char **result
)
471 len
= xasprintf(&esi
,
472 #define ITEM(x) "0004 %02x:\n"
475 #define ITEM(x) , (unsigned) SI_ ## x
480 len
= xasprintf(&esi
,
481 #define ITEM(x) "%s:\n"
484 #define ITEM(x) ,status_item_list[SI_ ## x]
491 #undef EMPTY_STATUS_ITEMS
493 static int com_stat(struct command_context
*cc
, struct lls_parse_result
*lpr
)
496 struct misc_meta_data tmp
, *nmmd
= &tmp
;
498 bool parser_friendly
= SERVER_CMD_OPT_GIVEN(STAT
, PARSER_FRIENDLY
,
500 uint32_t num
= SERVER_CMD_UINT32_VAL(STAT
, NUM
, lpr
);
502 para_sigaction(SIGUSR1
, dummy
);
505 * Copy the mmd structure to minimize the time we hold the mmd
508 mutex_lock(mmd_mutex
);
510 mutex_unlock(mmd_mutex
);
511 ret
= get_status(nmmd
, parser_friendly
, &s
);
512 ret
= send_sb(&cc
->scc
, s
, ret
, SBD_OUTPUT
, false);
515 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
517 ret
= empty_status_items(parser_friendly
, &esi
);
518 ret
= send_sb(&cc
->scc
, esi
, ret
, SBD_OUTPUT
, false);
522 send_afs_status(cc
, parser_friendly
);
524 if (num
> 0 && !--num
)
527 ret
= -E_SERVER_CRASH
;
534 EXPORT_SERVER_CMD_HANDLER(stat
);
536 /* fixed-length, human readable permission string */
537 const char *server_cmd_perms_str(unsigned int perms
)
539 static char result
[5];
541 result
[0] = perms
& AFS_READ
? 'a' : '-';
542 result
[1] = perms
& AFS_WRITE
? 'A' : '-';
543 result
[2] = perms
& VSS_READ
? 'v' : '-';
544 result
[3] = perms
& VSS_WRITE
? 'V' : '-';
549 static int send_list_of_commands(struct command_context
*cc
)
552 const struct lls_command
*cmd
;
553 char *msg
= para_strdup("");
555 for (i
= 1; (cmd
= lls_cmd(i
, server_cmd_suite
)); i
++) {
556 const char *perms
= server_cmd_perms_str(server_command_perms
[i
]);
557 char *tmp
= make_message("%s%s\t%s\t%s\n", msg
,
558 lls_command_name(cmd
), perms
, lls_purpose(cmd
));
562 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
565 static int com_help(struct command_context
*cc
, struct lls_parse_result
*lpr
)
568 char *long_help
, *buf
, *errctx
;
570 const struct lls_command
*cmd
;
572 ret
= lls(lls_check_arg_count(lpr
, 0, 1, &errctx
));
574 send_errctx(cc
, errctx
);
577 if (lls_num_inputs(lpr
) == 0)
578 return send_list_of_commands(cc
);
579 /* argument given for help */
580 ret
= lls(lls_lookup_subcmd(lls_input(0, lpr
), server_cmd_suite
,
583 send_errctx(cc
, errctx
);
586 cmd
= lls_cmd(ret
, server_cmd_suite
);
587 perms
= server_command_perms_txt
[ret
];
588 long_help
= lls_long_help(cmd
);
590 ret
= xasprintf(&buf
, "%spermissions: %s\n", long_help
, perms
);
592 return send_sb(&cc
->scc
, buf
, ret
, SBD_OUTPUT
, false);
594 EXPORT_SERVER_CMD_HANDLER(help
);
596 static int com_hup(__a_unused
struct command_context
*cc
,
597 __a_unused
struct lls_parse_result
*lpr
)
599 kill(getppid(), SIGHUP
);
602 EXPORT_SERVER_CMD_HANDLER(hup
);
604 static int com_term(__a_unused
struct command_context
*cc
,
605 __a_unused
struct lls_parse_result
*lpr
)
607 kill(getppid(), SIGTERM
);
610 EXPORT_SERVER_CMD_HANDLER(term
);
612 static int com_play(__a_unused
struct command_context
*cc
,
613 __a_unused
struct lls_parse_result
*lpr
)
615 mutex_lock(mmd_mutex
);
616 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
617 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
618 mutex_unlock(mmd_mutex
);
621 EXPORT_SERVER_CMD_HANDLER(play
);
623 static int com_stop(__a_unused
struct command_context
*cc
,
624 __a_unused
struct lls_parse_result
*lpr
)
626 mutex_lock(mmd_mutex
);
627 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
628 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
629 mmd
->new_vss_status_flags
|= VSS_NEXT
;
630 mutex_unlock(mmd_mutex
);
633 EXPORT_SERVER_CMD_HANDLER(stop
);
635 static int com_pause(__a_unused
struct command_context
*cc
,
636 __a_unused
struct lls_parse_result
*lpr
)
638 mutex_lock(mmd_mutex
);
639 if (!vss_paused() && !vss_stopped()) {
641 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
642 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
644 mutex_unlock(mmd_mutex
);
647 EXPORT_SERVER_CMD_HANDLER(pause
);
649 static int com_next(__a_unused
struct command_context
*cc
,
650 __a_unused
struct lls_parse_result
*lpr
)
652 mutex_lock(mmd_mutex
);
654 mmd
->new_vss_status_flags
|= VSS_NEXT
;
655 mutex_unlock(mmd_mutex
);
658 EXPORT_SERVER_CMD_HANDLER(next
);
660 static int com_nomore(__a_unused
struct command_context
*cc
,
661 __a_unused
struct lls_parse_result
*lpr
)
663 mutex_lock(mmd_mutex
);
664 if (vss_playing() || vss_paused())
665 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
666 mutex_unlock(mmd_mutex
);
669 EXPORT_SERVER_CMD_HANDLER(nomore
);
671 static int com_ff(__a_unused
struct command_context
*cc
,
672 struct lls_parse_result
*lpr
)
675 int ret
, backwards
= 0;
679 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
681 send_errctx(cc
, errctx
);
684 if (!(ret
= sscanf(lls_input(0, lpr
), "%u%c", &i
, &c
)))
685 return -E_COMMAND_SYNTAX
;
686 if (ret
> 1 && c
== '-')
687 backwards
= 1; /* jmp backwards */
688 mutex_lock(mmd_mutex
);
689 ret
= -E_NO_AUDIO_FILE
;
690 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
692 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
694 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
696 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
699 if (promille
> 1000) {
700 mmd
->new_vss_status_flags
|= VSS_NEXT
;
703 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
704 mmd
->new_vss_status_flags
|= VSS_REPOS
;
705 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
709 mutex_unlock(mmd_mutex
);
712 EXPORT_SERVER_CMD_HANDLER(ff
);
714 static int com_jmp(__a_unused
struct command_context
*cc
,
715 struct lls_parse_result
*lpr
)
721 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
723 send_errctx(cc
, errctx
);
726 if (sscanf(lls_input(0, lpr
), "%lu", &i
) <= 0)
727 return -ERRNO_TO_PARA_ERROR(EINVAL
);
728 mutex_lock(mmd_mutex
);
729 ret
= -E_NO_AUDIO_FILE
;
730 if (!mmd
->afd
.afhi
.chunks_total
)
734 PARA_INFO_LOG("jumping to %lu%%\n", i
);
735 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50) / 100;
736 PARA_INFO_LOG("sent: %lu, offset before jmp: %li\n",
737 mmd
->chunks_sent
, mmd
->offset
);
738 mmd
->new_vss_status_flags
|= VSS_REPOS
;
739 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
743 mutex_unlock(mmd_mutex
);
746 EXPORT_SERVER_CMD_HANDLER(jmp
);
748 static int com_tasks(struct command_context
*cc
,
749 __a_unused
struct lls_parse_result
*lpr
)
751 char *tl
= server_get_tasks();
753 return send_sb(&cc
->scc
, tl
, strlen(tl
), SBD_OUTPUT
, false);
755 EXPORT_SERVER_CMD_HANDLER(tasks
);
757 static void reset_signals(void)
759 para_sigaction(SIGCHLD
, SIG_IGN
);
760 para_sigaction(SIGINT
, SIG_DFL
);
761 para_sigaction(SIGTERM
, SIG_DFL
);
762 para_sigaction(SIGHUP
, SIG_DFL
);
765 struct connection_features
{
766 bool aes_ctr128_requested
;
769 static int parse_auth_request(char *buf
, int len
, struct user
**u
,
770 struct connection_features
*cf
)
773 char *p
, *username
, **features
= NULL
;
774 size_t auth_rq_len
= strlen(AUTH_REQUEST_MSG
);
777 memset(cf
, 0, sizeof(*cf
));
778 if (len
< auth_rq_len
+ 2)
779 return -E_AUTH_REQUEST
;
780 if (strncmp(buf
, AUTH_REQUEST_MSG
, auth_rq_len
) != 0)
781 return -E_AUTH_REQUEST
;
782 username
= buf
+ auth_rq_len
;
783 p
= strchr(username
, ' ');
787 return -E_AUTH_REQUEST
;
790 create_argv(p
, ",", &features
);
791 for (i
= 0; features
[i
]; i
++) {
792 if (strcmp(features
[i
], "sideband") == 0)
794 if (strcmp(features
[i
], "aes_ctr128") == 0)
795 cf
->aes_ctr128_requested
= true;
797 ret
= -E_BAD_FEATURE
;
802 PARA_DEBUG_LOG("received auth request for user %s\n", username
);
803 *u
= lookup_user(username
);
810 #define HANDSHAKE_BUFSIZE 4096
812 static int run_command(struct command_context
*cc
, struct iovec
*iov
,
813 const char *peername
)
816 char *p
, *end
, **argv
;
817 const struct lls_command
*lcmd
= NULL
;
819 struct lls_parse_result
*lpr
;
822 if (iov
->iov_base
== NULL
|| iov
->iov_len
== 0)
823 return -ERRNO_TO_PARA_ERROR(EINVAL
);
825 p
[iov
->iov_len
- 1] = '\0'; /* just to be sure */
827 ret
= lls(lls_lookup_subcmd(p
, server_cmd_suite
, &errctx
));
829 send_errctx(cc
, errctx
);
832 perms
= server_command_perms
[ret
];
833 if ((perms
& cc
->u
->perms
) != perms
)
835 lcmd
= lls_cmd(ret
, server_cmd_suite
);
836 end
= iov
->iov_base
+ iov
->iov_len
;
837 for (i
= 0; p
< end
; i
++)
840 argv
= para_malloc((argc
+ 1) * sizeof(char *));
841 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++) {
842 argv
[i
] = para_strdup(p
);
846 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", lls_command_name(lcmd
),
847 cc
->u
->name
, peername
);
848 ret
= lls(lls_parse(argc
, argv
, lcmd
, &lpr
, &errctx
));
850 const struct server_cmd_user_data
*ud
= lls_user_data(lcmd
);
851 ret
= ud
->handler(cc
, lpr
);
852 lls_free_parse_result(lpr
, lcmd
);
854 send_errctx(cc
, errctx
);
856 mutex_lock(mmd_mutex
);
858 if (ret
>= 0 && (perms
& AFS_WRITE
))
860 mutex_unlock(mmd_mutex
);
865 * Perform user authentication and execute a command.
867 * \param fd The file descriptor to send output to.
868 * \param peername Identifies the connecting peer.
870 * Whenever para_server accepts an incoming tcp connection on the port it
871 * listens on, it forks and the resulting child calls this function.
873 * An RSA-based challenge/response is used to authenticate the peer. It that
874 * authentication succeeds, a random session key is generated and sent back to
875 * the peer, encrypted with its RSA public key. From this point on, all
876 * transfers are crypted with this session key.
878 * Next it is checked if the peer supplied a valid server command or a command
879 * for the audio file selector. If yes, and if the user has sufficient
880 * permissions to execute that command, the function calls the corresponding
881 * command handler which does argument checking and further processing.
883 * In order to cope with a DOS attacks, a timeout is set up which terminates
884 * the function if the connection was not authenticated when the timeout
887 * \sa alarm(2), crypt.c, crypt.h
889 __noreturn
void handle_connect(int fd
, const char *peername
)
892 unsigned char rand_buf
[CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
893 unsigned char challenge_hash
[HASH_SIZE
];
894 char *command
= NULL
, *buf
= para_malloc(HANDSHAKE_BUFSIZE
) /* must be on the heap */;
896 struct command_context cc_struct
= {.peer
= peername
}, *cc
= &cc_struct
;
898 struct connection_features cf
;
902 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
903 ret
= mark_fd_blocking(fd
);
906 /* send Welcome message */
907 ret
= write_va_buffer(fd
, "This is para_server, version "
908 PACKAGE_VERSION
".\n"
909 "Features: sideband,aes_ctr128\n"
913 /* recv auth request line */
914 ret
= recv_buffer(fd
, buf
, HANDSHAKE_BUFSIZE
);
917 ret
= parse_auth_request(buf
, ret
, &cc
->u
, &cf
);
921 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
922 ret
= pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
923 (unsigned char *)buf
);
929 * We don't want to reveal our user names, so we send a
930 * challenge to the client even if the user does not exist, and
931 * fail the authentication later.
934 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
936 PARA_DEBUG_LOG("sending %d byte challenge + session key (%zu bytes)\n",
937 CHALLENGE_SIZE
, numbytes
);
938 ret
= send_sb(&cc
->scc
, buf
, numbytes
, SBD_CHALLENGE
, false);
942 ret
= recv_sb(&cc
->scc
, SBD_CHALLENGE_RESPONSE
,
943 HANDSHAKE_BUFSIZE
, &iov
);
947 numbytes
= iov
.iov_len
;
948 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes
);
953 * The correct response is the hash of the first CHALLENGE_SIZE bytes
954 * of the random data.
957 if (numbytes
!= HASH_SIZE
)
959 hash_function((char *)rand_buf
, CHALLENGE_SIZE
, challenge_hash
);
960 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
962 /* auth successful */
964 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
965 /* init stream cipher keys with the second part of the random buffer */
966 cc
->scc
.recv
= sc_new(rand_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
,
967 cf
.aes_ctr128_requested
);
968 cc
->scc
.send
= sc_new(rand_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
969 SESSION_KEY_LEN
, cf
.aes_ctr128_requested
);
970 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_PROCEED
, false);
973 ret
= recv_sb(&cc
->scc
, SBD_COMMAND
, MAX_COMMAND_LEN
, &iov
);
976 ret
= run_command(cc
, &iov
, peername
);
983 if (send_strerror(cc
, -ret
) >= 0)
984 send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__FAILURE
, true);
986 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
990 mutex_lock(mmd_mutex
);
991 mmd
->active_connections
--;
992 mutex_unlock(mmd_mutex
);
994 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__SUCCESS
, true);
996 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
998 sc_free(cc
->scc
.recv
);
999 sc_free(cc
->scc
.send
);
1000 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);