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>
13 #include "server.lsg.h"
31 #include "server_cmd.lsg.h"
32 #include "user_list.h"
36 #define SERVER_CMD_AUX_INFO(_arg) _arg,
37 static const unsigned server_command_perms
[] = {LSG_SERVER_CMD_AUX_INFOS
};
38 #undef SERVER_CMD_AUX_INFO
39 #define SERVER_CMD_AUX_INFO(_arg) #_arg,
40 static const char * const server_command_perms_txt
[] = {LSG_SERVER_CMD_AUX_INFOS
};
41 #undef SERVER_CMD_AUX_INFO
43 /** Commands including options must be shorter than this. */
44 #define MAX_COMMAND_LEN 32768
47 extern struct misc_meta_data
*mmd
;
48 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
49 static bool subcmd_should_die
;
51 static void command_handler_sighandler(int s
)
55 PARA_EMERG_LOG("terminating on signal %d\n", SIGTERM
);
56 subcmd_should_die
= true;
60 * Compute human readable vss status text.
62 * We can't call vss_playing() and friends here because those functions read
63 * the flags from the primary mmd structure, so calling them from command
64 * handler context would require to take the mmd lock. At the time the function
65 * is called we already took a copy of the mmd structure and want to use the
66 * flags value of the copy for computing the vss status text.
68 static char *vss_status_tohuman(unsigned int flags
)
70 if (flags
& VSS_PLAYING
)
71 return para_strdup("playing");
73 return para_strdup("stopped");
74 return para_strdup("paused");
80 static char *vss_get_status_flags(unsigned int flags
)
82 char *msg
= para_malloc(5 * sizeof(char));
84 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
85 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
86 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
87 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
92 static unsigned get_status(struct misc_meta_data
*nmmd
, bool parser_friendly
,
95 char *status
, *flags
; /* vss status info */
96 /* nobody updates our version of "now" */
97 long offset
= (nmmd
->offset
+ 500) / 1000;
98 struct timeval current_time
;
99 struct para_buffer b
= {.flags
= parser_friendly
? PBF_SIZE_PREFIX
: 0};
101 /* report real status */
102 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
103 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
104 clock_get_realtime(¤t_time
);
106 * The calls to WRITE_STATUS_ITEM() below never fail because
107 * b->max_size is zero (unlimited), see \ref para_printf(). However,
108 * clang is not smart enough to prove this and complains nevertheless.
109 * Casting the return value to void silences clang.
111 (void)WRITE_STATUS_ITEM(&b
, SI_status
, "%s\n", status
);
112 (void)WRITE_STATUS_ITEM(&b
, SI_status_flags
, "%s\n", flags
);
113 (void)WRITE_STATUS_ITEM(&b
, SI_offset
, "%li\n", offset
);
114 (void)WRITE_STATUS_ITEM(&b
, SI_afs_mode
, "%s\n", mmd
->afs_mode_string
);
115 (void)WRITE_STATUS_ITEM(&b
, SI_stream_start
, "%lu.%lu\n",
116 (long unsigned)nmmd
->stream_start
.tv_sec
,
117 (long unsigned)nmmd
->stream_start
.tv_usec
);
118 (void)WRITE_STATUS_ITEM(&b
, SI_current_time
, "%lu.%lu\n",
119 (long unsigned)current_time
.tv_sec
,
120 (long unsigned)current_time
.tv_usec
);
128 * Send a sideband packet through a blocking file descriptor.
130 * \param scc fd and crypto keys.
131 * \param buf The buffer to send.
132 * \param numbytes The size of \a buf.
133 * \param band The sideband designator of this packet.
134 * \param dont_free If true, never deallocate \a buf.
136 * The nonblock flag must be disabled for the file descriptor given by \a scc.
138 * Stream cipher encryption is automatically activated if necessary via the
139 * sideband transformation, depending on the value of \a band.
143 * \sa \ref send_sb_va().
145 int send_sb(struct stream_cipher_context
*scc
, void *buf
, size_t numbytes
,
146 int band
, bool dont_free
)
149 struct sb_context
*sbc
;
151 sb_transformation trafo
= band
< SBD_PROCEED
? NULL
: sc_trafo
;
152 struct sb_buffer sbb
= SBB_INIT(band
, buf
, numbytes
);
154 sbc
= sb_new_send(&sbb
, dont_free
, trafo
, scc
->send
);
156 ret
= sb_get_send_buffers(sbc
, iov
);
157 ret
= xwritev(scc
->fd
, iov
, ret
);
160 } while (sb_sent(sbc
, ret
) == false);
168 * Create a variable sized buffer and send it as a sideband packet.
170 * \param scc Passed to \ref send_sb.
171 * \param band See \ref send_sb.
172 * \param fmt The format string.
174 * \return The return value of the underlying call to \ref send_sb.
176 __printf_3_4
int send_sb_va(struct stream_cipher_context
*scc
, int band
,
177 const char *fmt
, ...)
184 ret
= xvasprintf(&msg
, fmt
, ap
);
186 return send_sb(scc
, msg
, ret
, band
, false);
190 * Send an error message to a client.
192 * \param cc Client info.
193 * \param err The (positive) error code.
195 * \return The return value of the underlying call to send_sb_va().
197 int send_strerror(struct command_context
*cc
, int err
)
199 return send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", para_strerror(err
));
203 * Send an error context to a client,
205 * \param cc Client info.
206 * \param errctx The error context string.
208 * \return The return value of the underlying call to send_sb_va().
210 * This function frees the error context string after it was sent.
212 int send_errctx(struct command_context
*cc
, char *errctx
)
218 ret
= send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", errctx
);
223 static int check_sender_args(struct command_context
*cc
,
224 struct lls_parse_result
*lpr
, struct sender_command_data
*scd
)
227 const char * const subcmds
[] = {SENDER_SUBCOMMANDS
};
230 unsigned num_inputs
= lls_num_inputs(lpr
);
232 scd
->sender_num
= -1;
233 ret
= lls(lls_check_arg_count(lpr
, 2, INT_MAX
, &errctx
));
235 send_errctx(cc
, errctx
);
238 arg
= lls_input(0, lpr
);
240 if (strcmp(senders
[i
]->name
, arg
) == 0)
243 return -E_COMMAND_SYNTAX
;
245 arg
= lls_input(1, lpr
);
246 for (i
= 0; i
< NUM_SENDER_CMDS
; i
++)
247 if (!strcmp(subcmds
[i
], arg
))
249 if (i
== NUM_SENDER_CMDS
)
250 return -E_COMMAND_SYNTAX
;
252 if (!senders
[scd
->sender_num
]->client_cmds
[scd
->cmd_num
])
253 return -E_SENDER_CMD
;
254 switch (scd
->cmd_num
) {
258 return -E_COMMAND_SYNTAX
;
262 if (num_inputs
!= 3 || parse_cidr(lls_input(2, lpr
), scd
->host
,
263 sizeof(scd
->host
), &scd
->netmask
) == NULL
)
264 return -E_COMMAND_SYNTAX
;
269 return -E_COMMAND_SYNTAX
;
270 return parse_fec_url(lls_input(2, lpr
), scd
);
272 return -E_COMMAND_SYNTAX
;
278 * Receive a sideband packet from a blocking file descriptor.
280 * \param scc fd and crypto keys.
281 * \param expected_band The expected band designator.
282 * \param max_size Passed to \ref sb_new_recv().
283 * \param result Body of the sideband packet is returned here.
285 * If \a expected_band is not \p SBD_ANY, the band designator of the received
286 * sideband packet is compared to \a expected_band and a mismatch is considered
291 int recv_sb(struct stream_cipher_context
*scc
,
292 enum sb_designator expected_band
,
293 size_t max_size
, struct iovec
*result
)
296 struct sb_context
*sbc
;
298 struct sb_buffer sbb
;
299 sb_transformation trafo
;
301 trafo
= expected_band
!= SBD_ANY
&& expected_band
< SBD_PROCEED
?
303 sbc
= sb_new_recv(max_size
, trafo
, scc
->recv
);
305 sb_get_recv_buffer(sbc
, &iov
);
306 ret
= recv_bin_buffer(scc
->fd
, iov
.iov_base
, iov
.iov_len
);
311 ret
= sb_received(sbc
, ret
, &sbb
);
318 if (expected_band
!= SBD_ANY
&& sbb
.band
!= expected_band
)
327 static int com_sender(struct command_context
*cc
, struct lls_parse_result
*lpr
)
331 struct sender_command_data scd
;
333 if (lls_num_inputs(lpr
) == 0) {
336 ret
= xasprintf(&tmp
, "%s%s\n", msg
? msg
: "",
341 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
343 ret
= check_sender_args(cc
, lpr
, &scd
);
345 if (scd
.sender_num
< 0)
347 if (strcmp(lls_input(1, lpr
), "status") == 0)
348 msg
= senders
[scd
.sender_num
]->status();
350 msg
= senders
[scd
.sender_num
]->help();
351 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
354 switch (scd
.cmd_num
) {
357 assert(senders
[scd
.sender_num
]->resolve_target
);
358 ret
= senders
[scd
.sender_num
]->resolve_target(lls_input(2, lpr
),
364 for (i
= 0; i
< 10; i
++) {
365 mutex_lock(mmd_mutex
);
366 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
367 /* another sender command is active, retry in 100ms */
368 struct timespec ts
= {.tv_nsec
= 100 * 1000 * 1000};
369 mutex_unlock(mmd_mutex
);
370 nanosleep(&ts
, NULL
);
373 mmd
->sender_cmd_data
= scd
;
374 mutex_unlock(mmd_mutex
);
377 return (i
< 10)? 1 : -E_LOCK
;
379 EXPORT_SERVER_CMD_HANDLER(sender
);
381 static int com_si(struct command_context
*cc
,
382 __a_unused
struct lls_parse_result
*lpr
)
387 ut
= daemon_get_uptime_str(now
);
388 mutex_lock(mmd_mutex
);
389 ret
= xasprintf(&msg
,
390 "up: %s\nplayed: %u\n"
393 "connections (active/accepted/total): %u/%u/%u\n"
394 "current loglevel: %s\n"
395 "supported audio formats: %s\n",
399 mmd
->active_connections
,
402 ENUM_STRING_VAL(LOGLEVEL
),
403 AUDIO_FORMAT_HANDLERS
405 mutex_unlock(mmd_mutex
);
407 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
409 EXPORT_SERVER_CMD_HANDLER(si
);
411 static int com_version(struct command_context
*cc
, struct lls_parse_result
*lpr
)
416 if (SERVER_CMD_OPT_GIVEN(VERSION
, VERBOSE
, lpr
))
417 len
= xasprintf(&msg
, "%s", version_text("server"));
419 len
= xasprintf(&msg
, "%s\n", version_single_line("server"));
420 return send_sb(&cc
->scc
, msg
, len
, SBD_OUTPUT
, false);
422 EXPORT_SERVER_CMD_HANDLER(version
);
424 /** These status items are cleared if no audio file is currently open. */
425 #define EMPTY_STATUS_ITEMS \
430 ITEM(attributes_bitmap) \
431 ITEM(attributes_txt) \
442 ITEM(seconds_total) \
455 ITEM(amplification) \
459 * Create a set of audio-file related status items with empty values. These are
460 * written to stat clients when no audio file is open.
462 static unsigned empty_status_items(bool parser_friendly
, char **result
)
468 len
= xasprintf(&esi
,
469 #define ITEM(x) "0004 %02x:\n"
472 #define ITEM(x) , (unsigned) SI_ ## x
477 len
= xasprintf(&esi
,
478 #define ITEM(x) "%s:\n"
481 #define ITEM(x) ,status_item_list[SI_ ## x]
488 #undef EMPTY_STATUS_ITEMS
490 static int com_stat(struct command_context
*cc
, struct lls_parse_result
*lpr
)
493 struct misc_meta_data tmp
, *nmmd
= &tmp
;
495 bool parser_friendly
= SERVER_CMD_OPT_GIVEN(STAT
, PARSER_FRIENDLY
,
497 uint32_t num
= SERVER_CMD_UINT32_VAL(STAT
, NUM
, lpr
);
498 const struct timespec ts
= {.tv_sec
= 50, .tv_nsec
= 0};
500 para_sigaction(SIGINT
, SIG_IGN
);
501 para_sigaction(SIGUSR1
, command_handler_sighandler
);
502 para_sigaction(SIGTERM
, command_handler_sighandler
);
504 * Simply checking subcmd_should_die is racy because a signal may
505 * arrive after the check but before the subsequent call to sleep(3).
506 * If this happens, sleep(3) would not be interrupted by the signal.
507 * To avoid this we block SIGTERM here and allow it to arrive only
510 para_block_signal(SIGTERM
);
514 * Copy the mmd structure to minimize the time we hold the mmd
517 mutex_lock(mmd_mutex
);
519 mutex_unlock(mmd_mutex
);
520 ret
= get_status(nmmd
, parser_friendly
, &s
);
521 ret
= send_sb(&cc
->scc
, s
, ret
, SBD_OUTPUT
, false);
524 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
526 ret
= empty_status_items(parser_friendly
, &esi
);
527 ret
= send_sb(&cc
->scc
, esi
, ret
, SBD_OUTPUT
, false);
531 send_afs_status(cc
, parser_friendly
);
533 if (num
> 0 && !--num
)
535 sigemptyset(&set
); /* empty set means: unblock all signals */
537 * pselect(2) allows to atomically unblock signals, then go to
538 * sleep. Calling sigprocmask(2) followed by sleep(3) would
539 * open a race window similar to the one described above.
541 pselect(1, NULL
, NULL
, NULL
, &ts
, &set
);
542 if (subcmd_should_die
)
544 ret
= -E_SERVER_CRASH
;
551 EXPORT_SERVER_CMD_HANDLER(stat
);
553 static const char *aux_info_cb(unsigned cmd_num
, bool verbose
)
555 static char result
[80];
556 unsigned perms
= server_command_perms
[cmd_num
];
559 /* permissions: VSS_READ | VSS_WRITE */
560 sprintf(result
, "permissions: %s",
561 server_command_perms_txt
[cmd_num
]);
563 result
[0] = perms
& AFS_READ
? 'a' : '-';
564 result
[1] = perms
& AFS_WRITE
? 'A' : '-';
565 result
[2] = perms
& VSS_READ
? 'v' : '-';
566 result
[3] = perms
& VSS_WRITE
? 'V' : '-';
572 static int com_help(struct command_context
*cc
, struct lls_parse_result
*lpr
)
577 bool long_help
= SERVER_CMD_OPT_GIVEN(HELP
, LONG
, lpr
);
579 lsu_com_help(long_help
, lpr
, server_cmd_suite
, aux_info_cb
, &buf
, &n
);
580 ret
= send_sb(&cc
->scc
, buf
, n
, SBD_OUTPUT
, false);
583 EXPORT_SERVER_CMD_HANDLER(help
);
585 static int com_hup(__a_unused
struct command_context
*cc
,
586 __a_unused
struct lls_parse_result
*lpr
)
588 kill(getppid(), SIGHUP
);
591 EXPORT_SERVER_CMD_HANDLER(hup
);
593 static int com_term(__a_unused
struct command_context
*cc
,
594 __a_unused
struct lls_parse_result
*lpr
)
596 kill(getppid(), SIGTERM
);
599 EXPORT_SERVER_CMD_HANDLER(term
);
601 static int com_play(__a_unused
struct command_context
*cc
,
602 __a_unused
struct lls_parse_result
*lpr
)
604 mutex_lock(mmd_mutex
);
605 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
606 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
607 mutex_unlock(mmd_mutex
);
610 EXPORT_SERVER_CMD_HANDLER(play
);
612 static int com_stop(__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_REPOS
;
618 mmd
->new_vss_status_flags
|= VSS_NEXT
;
619 mutex_unlock(mmd_mutex
);
622 EXPORT_SERVER_CMD_HANDLER(stop
);
624 static int com_pause(__a_unused
struct command_context
*cc
,
625 __a_unused
struct lls_parse_result
*lpr
)
627 mutex_lock(mmd_mutex
);
628 if (!vss_paused() && !vss_stopped()) {
630 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
631 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
633 mutex_unlock(mmd_mutex
);
636 EXPORT_SERVER_CMD_HANDLER(pause
);
638 static int com_next(__a_unused
struct command_context
*cc
,
639 __a_unused
struct lls_parse_result
*lpr
)
641 mutex_lock(mmd_mutex
);
643 mmd
->new_vss_status_flags
|= VSS_NEXT
;
644 mutex_unlock(mmd_mutex
);
647 EXPORT_SERVER_CMD_HANDLER(next
);
649 static int com_nomore(__a_unused
struct command_context
*cc
,
650 __a_unused
struct lls_parse_result
*lpr
)
652 mutex_lock(mmd_mutex
);
653 if (vss_playing() || vss_paused())
654 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
655 mutex_unlock(mmd_mutex
);
658 EXPORT_SERVER_CMD_HANDLER(nomore
);
660 static int com_ff(struct command_context
*cc
, struct lls_parse_result
*lpr
)
666 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
668 send_errctx(cc
, errctx
);
671 ret
= para_atoi32(lls_input(0, lpr
), &i
);
673 if (ret
!= -E_ATOI_JUNK_AT_END
)
676 * Compatibility code to keep the historic syntax (ff 30-)
677 * working. This can be removed after 0.7.0.
679 ret
= sscanf(lls_input(0, lpr
), "%i%c", &i
, &c
);
681 return -E_COMMAND_SYNTAX
;
682 if (ret
> 1 && c
== '-') {
683 PARA_WARNING_LOG("use of obsolete syntax\n");
687 mutex_lock(mmd_mutex
);
688 ret
= -E_NO_AUDIO_FILE
;
689 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
692 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
694 * We need this cast because without it the expression on the right
695 * hand side is of unsigned type.
697 promille
+= 1000 * i
/ (int)mmd
->afd
.afhi
.seconds_total
;
700 if (promille
> 1000) {
701 mmd
->new_vss_status_flags
|= VSS_NEXT
;
704 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
705 mmd
->new_vss_status_flags
|= VSS_REPOS
;
706 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
709 mutex_unlock(mmd_mutex
);
712 EXPORT_SERVER_CMD_HANDLER(ff
);
714 static int com_jmp(struct command_context
*cc
, struct lls_parse_result
*lpr
)
719 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
721 send_errctx(cc
, errctx
);
724 if (sscanf(lls_input(0, lpr
), "%d", &i
) <= 0)
725 return -ERRNO_TO_PARA_ERROR(EINVAL
);
726 if (i
< 0 || i
> 100)
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
)
732 PARA_INFO_LOG("jumping to %d%%\n", i
);
733 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50) / 100;
734 mmd
->new_vss_status_flags
|= VSS_REPOS
;
735 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
739 mutex_unlock(mmd_mutex
);
742 EXPORT_SERVER_CMD_HANDLER(jmp
);
744 static void reset_signals(void)
746 para_sigaction(SIGCHLD
, SIG_IGN
);
747 para_sigaction(SIGINT
, SIG_DFL
);
748 para_sigaction(SIGTERM
, SIG_DFL
);
749 para_sigaction(SIGHUP
, SIG_DFL
);
752 struct connection_features
{
753 bool sha256_requested
; /* can be removed after 0.7.0 */
756 static int parse_auth_request(char *buf
, int len
, const struct user
**u
,
757 struct connection_features
*cf
)
760 char *p
, *username
, **features
= NULL
;
761 size_t auth_rq_len
= strlen(AUTH_REQUEST_MSG
);
764 memset(cf
, 0, sizeof(*cf
));
765 if (len
< auth_rq_len
+ 2)
766 return -E_AUTH_REQUEST
;
767 if (strncmp(buf
, AUTH_REQUEST_MSG
, auth_rq_len
) != 0)
768 return -E_AUTH_REQUEST
;
769 username
= buf
+ auth_rq_len
;
770 p
= strchr(username
, ' ');
774 return -E_AUTH_REQUEST
;
777 create_argv(p
, ",", &features
);
779 * Still accept sideband and AES feature requests (as a no-op)
780 * because some 0.6.x clients request them. The two checks
781 * below may be removed after 0.7.1.
783 for (i
= 0; features
[i
]; i
++) {
784 if (strcmp(features
[i
], "sideband") == 0)
786 if (strcmp(features
[i
], "aes_ctr128") == 0)
789 * ->sha256_requested can go away after 0.7.0 but the
790 * check has to stay until 0.9.0.
792 if (strcmp(features
[i
], "sha256") == 0)
793 cf
->sha256_requested
= true;
795 ret
= -E_BAD_FEATURE
;
800 PARA_DEBUG_LOG("received auth request for user %s\n", username
);
801 *u
= user_list_lookup(username
);
808 #define HANDSHAKE_BUFSIZE 4096
810 static int run_command(struct command_context
*cc
, struct iovec
*iov
)
813 char *p
, *end
, **argv
;
814 const struct lls_command
*lcmd
= NULL
;
816 struct lls_parse_result
*lpr
;
819 if (iov
->iov_base
== NULL
|| iov
->iov_len
== 0)
820 return -ERRNO_TO_PARA_ERROR(EINVAL
);
822 p
[iov
->iov_len
- 1] = '\0'; /* just to be sure */
824 ret
= lls(lls_lookup_subcmd(p
, server_cmd_suite
, &errctx
));
826 send_errctx(cc
, errctx
);
829 perms
= server_command_perms
[ret
];
830 if ((perms
& cc
->u
->perms
) != perms
)
831 return -ERRNO_TO_PARA_ERROR(EPERM
);
832 lcmd
= lls_cmd(ret
, server_cmd_suite
);
833 end
= iov
->iov_base
+ iov
->iov_len
;
834 for (i
= 0; p
< end
; i
++)
837 argv
= para_malloc((argc
+ 1) * sizeof(char *));
838 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++) {
839 argv
[i
] = para_strdup(p
);
843 PARA_NOTICE_LOG("calling com_%s() for user %s\n",
844 lls_command_name(lcmd
), cc
->u
->name
);
845 ret
= lls(lls_parse(argc
, argv
, lcmd
, &lpr
, &errctx
));
847 const struct server_cmd_user_data
*ud
= lls_user_data(lcmd
);
848 ret
= ud
->handler(cc
, lpr
);
849 lls_free_parse_result(lpr
, lcmd
);
851 send_errctx(cc
, errctx
);
853 mutex_lock(mmd_mutex
);
855 if (ret
>= 0 && (perms
& AFS_WRITE
))
857 mutex_unlock(mmd_mutex
);
862 * Perform user authentication and execute a command.
864 * \param fd The file descriptor to send output to.
866 * Whenever para_server accepts an incoming tcp connection on the port it
867 * listens on, it forks and the resulting child calls this function.
869 * An RSA-based challenge/response is used to authenticate the peer. If the
870 * authentication succeeds, a random session key is generated and sent back to
871 * the peer, encrypted with its RSA public key. From this point on, all
872 * transfers are encrypted with this session key using a stream cipher.
874 * Next it is checked if the peer supplied a valid server command or a command
875 * for the audio file selector. If yes, and if the user has sufficient
876 * permissions to execute this command, the function calls the corresponding
877 * command handler which performs argument checking and further processing.
879 * To cope with DOS attacks, a timer is set up right after the fork. If the
880 * connection was still not authenticated when the timeout expires, the child
881 * process is terminated.
885 * \sa alarm(2), \ref openssl.c, \ref crypt.h.
887 int handle_connect(int fd
)
890 unsigned char rand_buf
[APC_CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
891 unsigned char challenge_hash
[HASH2_SIZE
];
892 char *command
= NULL
, *buf
= para_malloc(HANDSHAKE_BUFSIZE
) /* must be on the heap */;
894 struct command_context cc_struct
= {.u
= NULL
}, *cc
= &cc_struct
;
896 struct connection_features cf
;
900 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
901 ret
= mark_fd_blocking(fd
);
904 /* send Welcome message */
905 ret
= write_va_buffer(fd
, "This is para_server, version "
906 PACKAGE_VERSION
".\n"
907 "Features: sha256\n" /* no longer announce this after 0.8.0 */
911 /* recv auth request line */
912 ret
= recv_buffer(fd
, buf
, HANDSHAKE_BUFSIZE
);
915 ret
= parse_auth_request(buf
, ret
, &cc
->u
, &cf
);
919 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
920 ret
= apc_pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
921 (unsigned char *)buf
);
927 * We don't want to reveal our user names, so we send a
928 * challenge to the client even if the user does not exist, and
929 * fail the authentication later.
932 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
934 PARA_DEBUG_LOG("sending %d byte challenge + session key (%zu bytes)\n",
935 APC_CHALLENGE_SIZE
, numbytes
);
936 ret
= send_sb(&cc
->scc
, buf
, numbytes
, SBD_CHALLENGE
, false);
940 ret
= recv_sb(&cc
->scc
, SBD_CHALLENGE_RESPONSE
,
941 HANDSHAKE_BUFSIZE
, &iov
);
945 numbytes
= iov
.iov_len
;
946 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes
);
951 * The correct response is the hash of the first APC_CHALLENGE_SIZE bytes
952 * of the random data.
955 if (cf
.sha256_requested
) {
956 if (numbytes
!= HASH2_SIZE
)
958 hash2_function((char *)rand_buf
, APC_CHALLENGE_SIZE
, challenge_hash
);
959 if (memcmp(challenge_hash
, buf
, HASH2_SIZE
))
961 } else { /* old client. This can be removed after 0.7.0 */
962 if (numbytes
!= HASH_SIZE
)
964 hash_function((char *)rand_buf
, APC_CHALLENGE_SIZE
, challenge_hash
);
965 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
968 /* auth successful */
970 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
971 /* init stream cipher keys with the second part of the random buffer */
972 cc
->scc
.recv
= sc_new(rand_buf
+ APC_CHALLENGE_SIZE
, SESSION_KEY_LEN
);
973 cc
->scc
.send
= sc_new(rand_buf
+ APC_CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
975 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_PROCEED
, false);
978 ret
= recv_sb(&cc
->scc
, SBD_COMMAND
, MAX_COMMAND_LEN
, &iov
);
981 ret
= run_command(cc
, &iov
);
988 if (send_strerror(cc
, -ret
) >= 0)
989 send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__FAILURE
, true);
991 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
995 mutex_lock(mmd_mutex
);
996 mmd
->active_connections
--;
997 mutex_unlock(mmd_mutex
);
999 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__SUCCESS
, true);
1001 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1003 sc_free(cc
->scc
.recv
);
1004 sc_free(cc
->scc
.send
);