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 extern struct sender senders
[];
52 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
54 static void dummy(__a_unused
int s
)
59 * Compute human readable vss status text.
61 * We can't call vss_playing() and friends here because those functions read
62 * the flags from the primary mmd structure, so calling them from command
63 * handler context would require to take the mmd lock. At the time the function
64 * is called we already took a copy of the mmd structure and want to use the
65 * flags value of the copy for computing the vss status text.
67 static char *vss_status_tohuman(unsigned int flags
)
69 if (flags
& VSS_PLAYING
)
70 return para_strdup("playing");
72 return para_strdup("stopped");
73 return para_strdup("paused");
79 static char *vss_get_status_flags(unsigned int flags
)
81 char *msg
= para_malloc(5 * sizeof(char));
83 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
84 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
85 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
86 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
91 static unsigned get_status(struct misc_meta_data
*nmmd
, bool parser_friendly
,
94 char *status
, *flags
; /* vss status info */
95 /* nobody updates our version of "now" */
96 long offset
= (nmmd
->offset
+ 500) / 1000;
97 struct timeval current_time
;
98 struct para_buffer b
= {.flags
= parser_friendly
? PBF_SIZE_PREFIX
: 0};
100 /* report real status */
101 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
102 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
103 clock_get_realtime(¤t_time
);
105 * The calls to WRITE_STATUS_ITEM() below never fail because
106 * b->max_size is zero (unlimited), see \ref para_printf(). However,
107 * clang is not smart enough to prove this and complains nevertheless.
108 * Casting the return value to void silences clang.
110 (void)WRITE_STATUS_ITEM(&b
, SI_status
, "%s\n", status
);
111 (void)WRITE_STATUS_ITEM(&b
, SI_status_flags
, "%s\n", flags
);
112 (void)WRITE_STATUS_ITEM(&b
, SI_offset
, "%li\n", offset
);
113 (void)WRITE_STATUS_ITEM(&b
, SI_afs_mode
, "%s\n", mmd
->afs_mode_string
);
114 (void)WRITE_STATUS_ITEM(&b
, SI_stream_start
, "%lu.%lu\n",
115 (long unsigned)nmmd
->stream_start
.tv_sec
,
116 (long unsigned)nmmd
->stream_start
.tv_usec
);
117 (void)WRITE_STATUS_ITEM(&b
, SI_current_time
, "%lu.%lu\n",
118 (long unsigned)current_time
.tv_sec
,
119 (long unsigned)current_time
.tv_usec
);
127 * Send a sideband packet through a blocking file descriptor.
129 * \param scc fd and crypto keys.
130 * \param buf The buffer to send.
131 * \param numbytes The size of \a buf.
132 * \param band The sideband designator of this packet.
133 * \param dont_free If true, never deallocate \a buf.
135 * The nonblock flag must be disabled for the file descriptor given by \a scc.
137 * Stream cipher encryption is automatically activated if necessary via the
138 * sideband transformation, depending on the value of \a band.
142 * \sa \ref send_sb_va().
144 int send_sb(struct stream_cipher_context
*scc
, void *buf
, size_t numbytes
,
145 int band
, bool dont_free
)
148 struct sb_context
*sbc
;
150 sb_transformation trafo
= band
< SBD_PROCEED
? NULL
: sc_trafo
;
151 struct sb_buffer sbb
= SBB_INIT(band
, buf
, numbytes
);
153 sbc
= sb_new_send(&sbb
, dont_free
, trafo
, scc
->send
);
155 ret
= sb_get_send_buffers(sbc
, iov
);
156 ret
= xwritev(scc
->fd
, iov
, ret
);
159 } while (sb_sent(sbc
, ret
) == false);
167 * Create a variable sized buffer and send it as a sideband packet.
169 * \param scc Passed to \ref send_sb.
170 * \param band See \ref send_sb.
171 * \param fmt The format string.
173 * \return The return value of the underlying call to \ref send_sb.
175 __printf_3_4
int send_sb_va(struct stream_cipher_context
*scc
, int band
,
176 const char *fmt
, ...)
183 ret
= xvasprintf(&msg
, fmt
, ap
);
185 return send_sb(scc
, msg
, ret
, band
, false);
189 * Send an error message to a client.
191 * \param cc Client info.
192 * \param err The (positive) error code.
194 * \return The return value of the underlying call to send_sb_va().
196 int send_strerror(struct command_context
*cc
, int err
)
198 return send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", para_strerror(err
));
202 * Send an error context to a client,
204 * \param cc Client info.
205 * \param errctx The error context string.
207 * \return The return value of the underlying call to send_sb_va().
209 * This function frees the error context string after it was sent.
211 int send_errctx(struct command_context
*cc
, char *errctx
)
217 ret
= send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", errctx
);
222 static int check_sender_args(struct command_context
*cc
,
223 struct lls_parse_result
*lpr
, struct sender_command_data
*scd
)
226 const char * const subcmds
[] = {SENDER_SUBCOMMANDS
};
229 unsigned num_inputs
= lls_num_inputs(lpr
);
231 scd
->sender_num
= -1;
232 ret
= lls(lls_check_arg_count(lpr
, 2, INT_MAX
, &errctx
));
234 send_errctx(cc
, errctx
);
237 arg
= lls_input(0, lpr
);
238 for (i
= 0; senders
[i
].name
; i
++)
239 if (!strcmp(senders
[i
].name
, arg
))
241 if (!senders
[i
].name
)
242 return -E_COMMAND_SYNTAX
;
244 arg
= lls_input(1, lpr
);
245 for (i
= 0; i
< NUM_SENDER_CMDS
; i
++)
246 if (!strcmp(subcmds
[i
], arg
))
248 if (i
== NUM_SENDER_CMDS
)
249 return -E_COMMAND_SYNTAX
;
251 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
])
252 return -E_SENDER_CMD
;
253 switch (scd
->cmd_num
) {
257 return -E_COMMAND_SYNTAX
;
261 if (num_inputs
!= 3 || parse_cidr(lls_input(2, lpr
), scd
->host
,
262 sizeof(scd
->host
), &scd
->netmask
) == NULL
)
263 return -E_COMMAND_SYNTAX
;
268 return -E_COMMAND_SYNTAX
;
269 return parse_fec_url(lls_input(2, lpr
), scd
);
271 return -E_COMMAND_SYNTAX
;
277 * Send a sideband packet through a blocking file descriptor.
279 * \param scc fd and crypto keys.
280 * \param expected_band The expected band designator.
281 * \param max_size Passed to \ref sb_new_recv().
282 * \param result Body of the sideband packet is returned here.
284 * If \a expected_band is not \p SBD_ANY, the band designator of the received
285 * sideband packet is compared to \a expected_band and a mismatch is considered
290 int recv_sb(struct stream_cipher_context
*scc
,
291 enum sb_designator expected_band
,
292 size_t max_size
, struct iovec
*result
)
295 struct sb_context
*sbc
;
297 struct sb_buffer sbb
;
298 sb_transformation trafo
;
300 trafo
= expected_band
!= SBD_ANY
&& expected_band
< SBD_PROCEED
?
302 sbc
= sb_new_recv(max_size
, trafo
, scc
->recv
);
304 sb_get_recv_buffer(sbc
, &iov
);
305 ret
= recv_bin_buffer(scc
->fd
, iov
.iov_base
, iov
.iov_len
);
310 ret
= sb_received(sbc
, ret
, &sbb
);
317 if (expected_band
!= SBD_ANY
&& sbb
.band
!= expected_band
)
326 static int com_sender(struct command_context
*cc
, struct lls_parse_result
*lpr
)
330 struct sender_command_data scd
;
332 if (lls_num_inputs(lpr
) == 0) {
333 for (i
= 0; senders
[i
].name
; i
++) {
335 ret
= xasprintf(&tmp
, "%s%s\n", msg
? msg
: "",
340 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
342 ret
= check_sender_args(cc
, lpr
, &scd
);
344 if (scd
.sender_num
< 0)
346 if (strcmp(lls_input(1, lpr
), "status") == 0)
347 msg
= senders
[scd
.sender_num
].status();
349 msg
= senders
[scd
.sender_num
].help();
350 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
353 switch (scd
.cmd_num
) {
356 assert(senders
[scd
.sender_num
].resolve_target
);
357 ret
= senders
[scd
.sender_num
].resolve_target(lls_input(2, lpr
),
363 for (i
= 0; i
< 10; i
++) {
364 mutex_lock(mmd_mutex
);
365 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
366 /* another sender command is active, retry in 100ms */
367 struct timespec ts
= {.tv_nsec
= 100 * 1000 * 1000};
368 mutex_unlock(mmd_mutex
);
369 nanosleep(&ts
, NULL
);
372 mmd
->sender_cmd_data
= scd
;
373 mutex_unlock(mmd_mutex
);
376 return (i
< 10)? 1 : -E_LOCK
;
378 EXPORT_SERVER_CMD_HANDLER(sender
);
380 static int com_si(struct command_context
*cc
,
381 __a_unused
struct lls_parse_result
*lpr
)
386 ut
= daemon_get_uptime_str(now
);
387 mutex_lock(mmd_mutex
);
388 ret
= xasprintf(&msg
,
389 "up: %s\nplayed: %u\n"
392 "connections (active/accepted/total): %u/%u/%u\n"
393 "current loglevel: %s\n"
394 "supported audio formats: %s\n",
398 mmd
->active_connections
,
401 ENUM_STRING_VAL(LOGLEVEL
),
402 AUDIO_FORMAT_HANDLERS
404 mutex_unlock(mmd_mutex
);
406 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
408 EXPORT_SERVER_CMD_HANDLER(si
);
410 static int com_version(struct command_context
*cc
, struct lls_parse_result
*lpr
)
415 if (SERVER_CMD_OPT_GIVEN(VERSION
, VERBOSE
, lpr
))
416 len
= xasprintf(&msg
, "%s", version_text("server"));
418 len
= xasprintf(&msg
, "%s\n", version_single_line("server"));
419 return send_sb(&cc
->scc
, msg
, len
, SBD_OUTPUT
, false);
421 EXPORT_SERVER_CMD_HANDLER(version
);
423 /** These status items are cleared if no audio file is currently open. */
424 #define EMPTY_STATUS_ITEMS \
429 ITEM(attributes_bitmap) \
430 ITEM(attributes_txt) \
441 ITEM(seconds_total) \
454 ITEM(amplification) \
457 * Create a set of audio-file related status items with empty values. These are
458 * written to stat clients when no audio file is open.
460 static unsigned empty_status_items(bool parser_friendly
, char **result
)
466 len
= xasprintf(&esi
,
467 #define ITEM(x) "0004 %02x:\n"
470 #define ITEM(x) , (unsigned) SI_ ## x
475 len
= xasprintf(&esi
,
476 #define ITEM(x) "%s:\n"
479 #define ITEM(x) ,status_item_list[SI_ ## x]
486 #undef EMPTY_STATUS_ITEMS
488 static int com_stat(struct command_context
*cc
, struct lls_parse_result
*lpr
)
491 struct misc_meta_data tmp
, *nmmd
= &tmp
;
493 bool parser_friendly
= SERVER_CMD_OPT_GIVEN(STAT
, PARSER_FRIENDLY
,
495 uint32_t num
= SERVER_CMD_UINT32_VAL(STAT
, NUM
, lpr
);
497 para_sigaction(SIGUSR1
, dummy
);
500 * Copy the mmd structure to minimize the time we hold the mmd
503 mutex_lock(mmd_mutex
);
505 mutex_unlock(mmd_mutex
);
506 ret
= get_status(nmmd
, parser_friendly
, &s
);
507 ret
= send_sb(&cc
->scc
, s
, ret
, SBD_OUTPUT
, false);
510 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
512 ret
= empty_status_items(parser_friendly
, &esi
);
513 ret
= send_sb(&cc
->scc
, esi
, ret
, SBD_OUTPUT
, false);
517 send_afs_status(cc
, parser_friendly
);
519 if (num
> 0 && !--num
)
522 ret
= -E_SERVER_CRASH
;
529 EXPORT_SERVER_CMD_HANDLER(stat
);
531 /* fixed-length, human readable permission string */
532 const char *server_cmd_perms_str(unsigned int perms
)
534 static char result
[5];
536 result
[0] = perms
& AFS_READ
? 'a' : '-';
537 result
[1] = perms
& AFS_WRITE
? 'A' : '-';
538 result
[2] = perms
& VSS_READ
? 'v' : '-';
539 result
[3] = perms
& VSS_WRITE
? 'V' : '-';
544 static int send_list_of_commands(struct command_context
*cc
)
547 const struct lls_command
*cmd
;
548 char *msg
= para_strdup("");
550 for (i
= 1; (cmd
= lls_cmd(i
, server_cmd_suite
)); i
++) {
551 const char *perms
= server_cmd_perms_str(server_command_perms
[i
]);
552 char *tmp
= make_message("%s%s\t%s\t%s\n", msg
,
553 lls_command_name(cmd
), perms
, lls_purpose(cmd
));
557 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
560 static int com_help(struct command_context
*cc
, struct lls_parse_result
*lpr
)
563 char *long_help
, *buf
, *errctx
;
565 const struct lls_command
*cmd
;
567 ret
= lls(lls_check_arg_count(lpr
, 0, 1, &errctx
));
569 send_errctx(cc
, errctx
);
572 if (lls_num_inputs(lpr
) == 0)
573 return send_list_of_commands(cc
);
574 /* argument given for help */
575 ret
= lls(lls_lookup_subcmd(lls_input(0, lpr
), server_cmd_suite
,
578 send_errctx(cc
, errctx
);
581 cmd
= lls_cmd(ret
, server_cmd_suite
);
582 perms
= server_command_perms_txt
[ret
];
583 long_help
= lls_long_help(cmd
);
585 ret
= xasprintf(&buf
, "%spermissions: %s\n", long_help
, perms
);
587 return send_sb(&cc
->scc
, buf
, ret
, SBD_OUTPUT
, false);
589 EXPORT_SERVER_CMD_HANDLER(help
);
591 static int com_hup(__a_unused
struct command_context
*cc
,
592 __a_unused
struct lls_parse_result
*lpr
)
594 kill(getppid(), SIGHUP
);
597 EXPORT_SERVER_CMD_HANDLER(hup
);
599 static int com_term(__a_unused
struct command_context
*cc
,
600 __a_unused
struct lls_parse_result
*lpr
)
602 kill(getppid(), SIGTERM
);
605 EXPORT_SERVER_CMD_HANDLER(term
);
607 static int com_play(__a_unused
struct command_context
*cc
,
608 __a_unused
struct lls_parse_result
*lpr
)
610 mutex_lock(mmd_mutex
);
611 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
612 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
613 mutex_unlock(mmd_mutex
);
616 EXPORT_SERVER_CMD_HANDLER(play
);
618 static int com_stop(__a_unused
struct command_context
*cc
,
619 __a_unused
struct lls_parse_result
*lpr
)
621 mutex_lock(mmd_mutex
);
622 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
623 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
624 mmd
->new_vss_status_flags
|= VSS_NEXT
;
625 mutex_unlock(mmd_mutex
);
628 EXPORT_SERVER_CMD_HANDLER(stop
);
630 static int com_pause(__a_unused
struct command_context
*cc
,
631 __a_unused
struct lls_parse_result
*lpr
)
633 mutex_lock(mmd_mutex
);
634 if (!vss_paused() && !vss_stopped()) {
636 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
637 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
639 mutex_unlock(mmd_mutex
);
642 EXPORT_SERVER_CMD_HANDLER(pause
);
644 static int com_next(__a_unused
struct command_context
*cc
,
645 __a_unused
struct lls_parse_result
*lpr
)
647 mutex_lock(mmd_mutex
);
649 mmd
->new_vss_status_flags
|= VSS_NEXT
;
650 mutex_unlock(mmd_mutex
);
653 EXPORT_SERVER_CMD_HANDLER(next
);
655 static int com_nomore(__a_unused
struct command_context
*cc
,
656 __a_unused
struct lls_parse_result
*lpr
)
658 mutex_lock(mmd_mutex
);
659 if (vss_playing() || vss_paused())
660 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
661 mutex_unlock(mmd_mutex
);
664 EXPORT_SERVER_CMD_HANDLER(nomore
);
666 static int com_ff(__a_unused
struct command_context
*cc
,
667 struct lls_parse_result
*lpr
)
670 int ret
, backwards
= 0;
674 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
676 send_errctx(cc
, errctx
);
679 if (!(ret
= sscanf(lls_input(0, lpr
), "%u%c", &i
, &c
)))
680 return -E_COMMAND_SYNTAX
;
681 if (ret
> 1 && c
== '-')
682 backwards
= 1; /* jmp backwards */
683 mutex_lock(mmd_mutex
);
684 ret
= -E_NO_AUDIO_FILE
;
685 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
687 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
689 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
691 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
694 if (promille
> 1000) {
695 mmd
->new_vss_status_flags
|= VSS_NEXT
;
698 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
699 mmd
->new_vss_status_flags
|= VSS_REPOS
;
700 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
704 mutex_unlock(mmd_mutex
);
707 EXPORT_SERVER_CMD_HANDLER(ff
);
709 static int com_jmp(__a_unused
struct command_context
*cc
,
710 struct lls_parse_result
*lpr
)
716 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
718 send_errctx(cc
, errctx
);
721 if (sscanf(lls_input(0, lpr
), "%lu", &i
) <= 0)
722 return -ERRNO_TO_PARA_ERROR(EINVAL
);
723 mutex_lock(mmd_mutex
);
724 ret
= -E_NO_AUDIO_FILE
;
725 if (!mmd
->afd
.afhi
.chunks_total
)
729 PARA_INFO_LOG("jumping to %lu%%\n", i
);
730 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50) / 100;
731 PARA_INFO_LOG("sent: %lu, offset before jmp: %li\n",
732 mmd
->chunks_sent
, mmd
->offset
);
733 mmd
->new_vss_status_flags
|= VSS_REPOS
;
734 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
738 mutex_unlock(mmd_mutex
);
741 EXPORT_SERVER_CMD_HANDLER(jmp
);
743 /* deprecated, does nothing */
744 static int com_tasks(__a_unused
struct command_context
*cc
,
745 __a_unused
struct lls_parse_result
*lpr
)
749 EXPORT_SERVER_CMD_HANDLER(tasks
);
751 static void reset_signals(void)
753 para_sigaction(SIGCHLD
, SIG_IGN
);
754 para_sigaction(SIGINT
, SIG_DFL
);
755 para_sigaction(SIGTERM
, SIG_DFL
);
756 para_sigaction(SIGHUP
, SIG_DFL
);
759 struct connection_features
{
760 int dummy
; /* none at the moment */
763 static int parse_auth_request(char *buf
, int len
, struct user
**u
,
764 struct connection_features
*cf
)
767 char *p
, *username
, **features
= NULL
;
768 size_t auth_rq_len
= strlen(AUTH_REQUEST_MSG
);
771 memset(cf
, 0, sizeof(*cf
));
772 if (len
< auth_rq_len
+ 2)
773 return -E_AUTH_REQUEST
;
774 if (strncmp(buf
, AUTH_REQUEST_MSG
, auth_rq_len
) != 0)
775 return -E_AUTH_REQUEST
;
776 username
= buf
+ auth_rq_len
;
777 p
= strchr(username
, ' ');
781 return -E_AUTH_REQUEST
;
784 create_argv(p
, ",", &features
);
785 for (i
= 0; features
[i
]; i
++) {
786 if (strcmp(features
[i
], "sideband") == 0)
788 if (strcmp(features
[i
], "aes_ctr128") == 0)
791 ret
= -E_BAD_FEATURE
;
796 PARA_DEBUG_LOG("received auth request for user %s\n", username
);
797 *u
= lookup_user(username
);
804 #define HANDSHAKE_BUFSIZE 4096
806 static int run_command(struct command_context
*cc
, struct iovec
*iov
)
809 char *p
, *end
, **argv
;
810 const struct lls_command
*lcmd
= NULL
;
812 struct lls_parse_result
*lpr
;
815 if (iov
->iov_base
== NULL
|| iov
->iov_len
== 0)
816 return -ERRNO_TO_PARA_ERROR(EINVAL
);
818 p
[iov
->iov_len
- 1] = '\0'; /* just to be sure */
820 ret
= lls(lls_lookup_subcmd(p
, server_cmd_suite
, &errctx
));
822 send_errctx(cc
, errctx
);
825 perms
= server_command_perms
[ret
];
826 if ((perms
& cc
->u
->perms
) != perms
)
828 lcmd
= lls_cmd(ret
, server_cmd_suite
);
829 end
= iov
->iov_base
+ iov
->iov_len
;
830 for (i
= 0; p
< end
; i
++)
833 argv
= para_malloc((argc
+ 1) * sizeof(char *));
834 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++) {
835 argv
[i
] = para_strdup(p
);
839 PARA_NOTICE_LOG("calling com_%s() for user %s\n",
840 lls_command_name(lcmd
), cc
->u
->name
);
841 ret
= lls(lls_parse(argc
, argv
, lcmd
, &lpr
, &errctx
));
843 const struct server_cmd_user_data
*ud
= lls_user_data(lcmd
);
844 ret
= ud
->handler(cc
, lpr
);
845 lls_free_parse_result(lpr
, lcmd
);
847 send_errctx(cc
, errctx
);
849 mutex_lock(mmd_mutex
);
851 if (ret
>= 0 && (perms
& AFS_WRITE
))
853 mutex_unlock(mmd_mutex
);
858 * Perform user authentication and execute a command.
860 * \param fd The file descriptor to send output to.
862 * Whenever para_server accepts an incoming tcp connection on the port it
863 * listens on, it forks and the resulting child calls this function.
865 * An RSA-based challenge/response is used to authenticate the peer. It that
866 * authentication succeeds, a random session key is generated and sent back to
867 * the peer, encrypted with its RSA public key. From this point on, all
868 * transfers are crypted with this session key.
870 * Next it is checked if the peer supplied a valid server command or a command
871 * for the audio file selector. If yes, and if the user has sufficient
872 * permissions to execute that command, the function calls the corresponding
873 * command handler which does argument checking and further processing.
875 * In order to cope with DOS attacks, a timeout is set up which terminates
876 * the function if the connection was not authenticated when the timeout
879 * \sa alarm(2), \ref openssl.c, \ref crypt.h.
881 __noreturn
void handle_connect(int fd
)
884 unsigned char rand_buf
[APC_CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
885 unsigned char challenge_hash
[HASH_SIZE
];
886 char *command
= NULL
, *buf
= para_malloc(HANDSHAKE_BUFSIZE
) /* must be on the heap */;
888 struct command_context cc_struct
= {.u
= NULL
}, *cc
= &cc_struct
;
890 struct connection_features cf
;
894 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
895 ret
= mark_fd_blocking(fd
);
898 /* send Welcome message */
899 ret
= write_va_buffer(fd
, "This is para_server, version "
900 PACKAGE_VERSION
".\n"
901 "Features: sideband,aes_ctr128\n"
905 /* recv auth request line */
906 ret
= recv_buffer(fd
, buf
, HANDSHAKE_BUFSIZE
);
909 ret
= parse_auth_request(buf
, ret
, &cc
->u
, &cf
);
913 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
914 ret
= apc_pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
915 (unsigned char *)buf
);
921 * We don't want to reveal our user names, so we send a
922 * challenge to the client even if the user does not exist, and
923 * fail the authentication later.
926 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
928 PARA_DEBUG_LOG("sending %d byte challenge + session key (%zu bytes)\n",
929 APC_CHALLENGE_SIZE
, numbytes
);
930 ret
= send_sb(&cc
->scc
, buf
, numbytes
, SBD_CHALLENGE
, false);
934 ret
= recv_sb(&cc
->scc
, SBD_CHALLENGE_RESPONSE
,
935 HANDSHAKE_BUFSIZE
, &iov
);
939 numbytes
= iov
.iov_len
;
940 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes
);
945 * The correct response is the hash of the first APC_CHALLENGE_SIZE bytes
946 * of the random data.
949 if (numbytes
!= HASH_SIZE
)
951 hash_function((char *)rand_buf
, APC_CHALLENGE_SIZE
, challenge_hash
);
952 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
954 /* auth successful */
956 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
957 /* init stream cipher keys with the second part of the random buffer */
958 cc
->scc
.recv
= sc_new(rand_buf
+ APC_CHALLENGE_SIZE
, SESSION_KEY_LEN
);
959 cc
->scc
.send
= sc_new(rand_buf
+ APC_CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
961 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_PROCEED
, false);
964 ret
= recv_sb(&cc
->scc
, SBD_COMMAND
, MAX_COMMAND_LEN
, &iov
);
967 ret
= run_command(cc
, &iov
);
974 if (send_strerror(cc
, -ret
) >= 0)
975 send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__FAILURE
, true);
977 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
981 mutex_lock(mmd_mutex
);
982 mmd
->active_connections
--;
983 mutex_unlock(mmd_mutex
);
985 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__SUCCESS
, true);
987 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
989 sc_free(cc
->scc
.recv
);
990 sc_free(cc
->scc
.send
);
991 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);