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"
40 #include "afs.command_list.h"
44 #define SERVER_CMD_AUX_INFO(_arg) _arg,
45 static const unsigned server_command_perms
[] = {LSG_SERVER_CMD_AUX_INFOS
};
46 #undef SERVER_CMD_AUX_INFO
47 #define SERVER_CMD_AUX_INFO(_arg) #_arg,
48 static const char * const server_command_perms_txt
[] = {LSG_SERVER_CMD_AUX_INFOS
};
49 #undef SERVER_CMD_AUX_INFO
51 typedef int server_command_handler_t(struct command_context
*);
52 server_command_handler_t AFS_COMMAND_HANDLERS
;
54 /* Defines one command of para_server. */
55 struct server_command
{
56 /* The name of the command. */
58 /* Pointer to the function that handles the command. */
59 server_command_handler_t
*handler
;
60 /* The privileges a user must have to execute this command. */
62 /* One-line description of the command. */
63 const char *description
;
64 /* Summary of the command line options. */
66 /* The long help text. */
70 static struct server_command afs_cmds
[] = {DEFINE_AFS_CMD_ARRAY
};
72 /** Commands including options must be shorter than this. */
73 #define MAX_COMMAND_LEN 32768
76 extern struct misc_meta_data
*mmd
;
77 extern struct sender senders
[];
78 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
80 static void dummy(__a_unused
int s
)
85 * Compute human readable vss status text.
87 * We can't call vss_playing() and friends here because those functions read
88 * the flags from the primary mmd structure, so calling them from command
89 * handler context would require to take the mmd lock. At the time the function
90 * is called we already took a copy of the mmd structure and want to use the
91 * flags value of the copy for computing the vss status text.
93 static char *vss_status_tohuman(unsigned int flags
)
95 if (flags
& VSS_PLAYING
)
96 return para_strdup("playing");
98 return para_strdup("stopped");
99 return para_strdup("paused");
103 * Never returns NULL.
105 static char *vss_get_status_flags(unsigned int flags
)
107 char *msg
= para_malloc(5 * sizeof(char));
109 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
110 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
111 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
112 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
117 static unsigned get_status(struct misc_meta_data
*nmmd
, bool parser_friendly
,
120 char *status
, *flags
; /* vss status info */
121 /* nobody updates our version of "now" */
122 long offset
= (nmmd
->offset
+ 500) / 1000;
123 struct timeval current_time
;
124 struct para_buffer b
= {.flags
= parser_friendly
? PBF_SIZE_PREFIX
: 0};
126 /* report real status */
127 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
128 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
129 clock_get_realtime(¤t_time
);
131 * The calls to WRITE_STATUS_ITEM() below never fail because
132 * b->max_size is zero (unlimited), see para_printf(). However, clang
133 * is not smart enough to prove this and complains nevertheless.
134 * Casting the return value to void silences clang.
136 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS
, "%s\n", status
);
137 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS_FLAGS
, "%s\n", flags
);
138 (void)WRITE_STATUS_ITEM(&b
, SI_OFFSET
, "%li\n", offset
);
139 (void)WRITE_STATUS_ITEM(&b
, SI_AFS_MODE
, "%s\n", mmd
->afs_mode_string
);
140 (void)WRITE_STATUS_ITEM(&b
, SI_STREAM_START
, "%lu.%lu\n",
141 (long unsigned)nmmd
->stream_start
.tv_sec
,
142 (long unsigned)nmmd
->stream_start
.tv_usec
);
143 (void)WRITE_STATUS_ITEM(&b
, SI_CURRENT_TIME
, "%lu.%lu\n",
144 (long unsigned)current_time
.tv_sec
,
145 (long unsigned)current_time
.tv_usec
);
153 * Send a sideband packet through a blocking file descriptor.
155 * \param scc fd and crypto keys.
156 * \param buf The buffer to send.
157 * \param numbytes The size of \a buf.
158 * \param band The sideband designator of this packet.
159 * \param dont_free If true, never deallocate \a buf.
161 * The nonblock flag must be disabled for the file descriptor given by \a scc.
163 * Stream cipher encryption is automatically activated if necessary via the
164 * sideband transformation, depending on the value of \a band.
168 * \sa \ref send_sb_va().
170 int send_sb(struct stream_cipher_context
*scc
, void *buf
, size_t numbytes
,
171 int band
, bool dont_free
)
174 struct sb_context
*sbc
;
176 sb_transformation trafo
= band
< SBD_PROCEED
? NULL
: sc_trafo
;
177 struct sb_buffer sbb
= SBB_INIT(band
, buf
, numbytes
);
179 sbc
= sb_new_send(&sbb
, dont_free
, trafo
, scc
->send
);
181 ret
= sb_get_send_buffers(sbc
, iov
);
182 ret
= xwritev(scc
->fd
, iov
, ret
);
185 } while (sb_sent(sbc
, ret
) == false);
193 * Create a variable sized buffer and send it as a sideband packet.
195 * \param scc Passed to \ref send_sb.
196 * \param band See \ref send_sb.
197 * \param fmt The format string.
199 * \return The return value of the underlying call to \ref send_sb.
201 __printf_3_4
int send_sb_va(struct stream_cipher_context
*scc
, int band
,
202 const char *fmt
, ...)
209 ret
= xvasprintf(&msg
, fmt
, ap
);
211 return send_sb(scc
, msg
, ret
, band
, false);
215 * Send an error message to a client.
217 * \param cc Client info.
218 * \param err The (positive) error code.
220 * \return The return value of the underlying call to send_sb_va().
222 int send_strerror(struct command_context
*cc
, int err
)
224 return send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", para_strerror(err
));
228 * Send an error context to a client,
230 * \param cc Client info.
231 * \param errctx The error context string.
233 * \return The return value of the underlying call to send_sb_va().
235 * This function frees the error context string after it was sent.
237 int send_errctx(struct command_context
*cc
, char *errctx
)
243 ret
= send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", errctx
);
248 static int check_sender_args(struct command_context
*cc
,
249 struct lls_parse_result
*lpr
, struct sender_command_data
*scd
)
252 const char *subcmds
[] = {SENDER_SUBCOMMANDS
};
255 unsigned num_inputs
= lls_num_inputs(lpr
);
257 scd
->sender_num
= -1;
258 ret
= lls(lls_check_arg_count(lpr
, 2, INT_MAX
, &errctx
));
260 send_errctx(cc
, errctx
);
263 arg
= lls_input(0, lpr
);
264 for (i
= 0; senders
[i
].name
; i
++)
265 if (!strcmp(senders
[i
].name
, arg
))
267 if (!senders
[i
].name
)
268 return -E_COMMAND_SYNTAX
;
270 arg
= lls_input(1, lpr
);
271 for (i
= 0; subcmds
[i
]; i
++)
272 if (!strcmp(subcmds
[i
], arg
))
275 return -E_COMMAND_SYNTAX
;
277 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
])
278 return -E_SENDER_CMD
;
279 switch (scd
->cmd_num
) {
283 return -E_COMMAND_SYNTAX
;
287 if (num_inputs
!= 3 || parse_cidr(lls_input(2, lpr
), scd
->host
,
288 sizeof(scd
->host
), &scd
->netmask
) == NULL
)
289 return -E_COMMAND_SYNTAX
;
294 return -E_COMMAND_SYNTAX
;
295 return parse_fec_url(lls_input(2, lpr
), scd
);
297 return -E_COMMAND_SYNTAX
;
303 * Send a sideband packet through a blocking file descriptor.
305 * \param scc fd and crypto keys.
306 * \param expected_band The expected band designator.
307 * \param max_size Passed to \ref sb_new_recv().
308 * \param result Body of the sideband packet is returned here.
310 * If \a expected_band is not \p SBD_ANY, the band designator of the received
311 * sideband packet is compared to \a expected_band and a mismatch is considered
316 int recv_sb(struct stream_cipher_context
*scc
,
317 enum sb_designator expected_band
,
318 size_t max_size
, struct iovec
*result
)
321 struct sb_context
*sbc
;
323 struct sb_buffer sbb
;
324 sb_transformation trafo
;
326 trafo
= expected_band
!= SBD_ANY
&& expected_band
< SBD_PROCEED
?
328 sbc
= sb_new_recv(max_size
, trafo
, scc
->recv
);
330 sb_get_recv_buffer(sbc
, &iov
);
331 ret
= recv_bin_buffer(scc
->fd
, iov
.iov_base
, iov
.iov_len
);
336 ret
= sb_received(sbc
, ret
, &sbb
);
343 if (expected_band
!= SBD_ANY
&& sbb
.band
!= expected_band
)
352 static int com_sender(struct command_context
*cc
, struct lls_parse_result
*lpr
)
356 struct sender_command_data scd
;
358 if (lls_num_inputs(lpr
) == 0) {
359 for (i
= 0; senders
[i
].name
; i
++) {
361 ret
= xasprintf(&tmp
, "%s%s\n", msg
? msg
: "",
366 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
368 ret
= check_sender_args(cc
, lpr
, &scd
);
370 if (scd
.sender_num
< 0)
372 if (strcmp(lls_input(1, lpr
), "status") == 0)
373 msg
= senders
[scd
.sender_num
].status();
375 msg
= senders
[scd
.sender_num
].help();
376 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
379 switch (scd
.cmd_num
) {
382 assert(senders
[scd
.sender_num
].resolve_target
);
383 ret
= senders
[scd
.sender_num
].resolve_target(lls_input(2, lpr
),
389 for (i
= 0; i
< 10; i
++) {
390 mutex_lock(mmd_mutex
);
391 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
392 /* another sender command is active, retry in 100ms */
393 struct timespec ts
= {.tv_nsec
= 100 * 1000 * 1000};
394 mutex_unlock(mmd_mutex
);
395 nanosleep(&ts
, NULL
);
398 mmd
->sender_cmd_data
= scd
;
399 mutex_unlock(mmd_mutex
);
402 return (i
< 10)? 1 : -E_LOCK
;
404 EXPORT_SERVER_CMD_HANDLER(sender
);
406 static int com_si(struct command_context
*cc
,
407 __a_unused
struct lls_parse_result
*lpr
)
412 ut
= daemon_get_uptime_str(now
);
413 mutex_lock(mmd_mutex
);
414 ret
= xasprintf(&msg
,
415 "up: %s\nplayed: %u\n"
418 "connections (active/accepted/total): %u/%u/%u\n"
419 "current loglevel: %s\n"
420 "supported audio formats: %s\n",
424 mmd
->active_connections
,
428 AUDIO_FORMAT_HANDLERS
430 mutex_unlock(mmd_mutex
);
432 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
434 EXPORT_SERVER_CMD_HANDLER(si
);
436 static int com_version(struct command_context
*cc
, struct lls_parse_result
*lpr
)
441 if (SERVER_CMD_OPT_GIVEN(VERSION
, VERBOSE
, lpr
))
442 len
= xasprintf(&msg
, "%s", version_text("server"));
444 len
= xasprintf(&msg
, "%s\n", version_single_line("server"));
445 return send_sb(&cc
->scc
, msg
, len
, SBD_OUTPUT
, false);
447 EXPORT_SERVER_CMD_HANDLER(version
);
449 /** These status items are cleared if no audio file is currently open. */
450 #define EMPTY_STATUS_ITEMS \
455 ITEM(ATTRIBUTES_BITMAP) \
456 ITEM(ATTRIBUTES_TXT) \
467 ITEM(SECONDS_TOTAL) \
480 ITEM(AMPLIFICATION) \
483 * Write a list of audio-file related status items with empty values.
485 * This is used by vss when currently no audio file is open.
487 static unsigned empty_status_items(bool parser_friendly
, char **result
)
493 len
= xasprintf(&esi
,
494 #define ITEM(x) "0004 %02x:\n"
497 #define ITEM(x) , (unsigned) SI_ ## x
502 len
= xasprintf(&esi
,
503 #define ITEM(x) "%s:\n"
506 #define ITEM(x) ,status_item_list[SI_ ## x]
513 #undef EMPTY_STATUS_ITEMS
515 static int com_stat(struct command_context
*cc
, struct lls_parse_result
*lpr
)
518 struct misc_meta_data tmp
, *nmmd
= &tmp
;
520 bool parser_friendly
= SERVER_CMD_OPT_GIVEN(STAT
, PARSER_FRIENDLY
,
522 uint32_t num
= SERVER_CMD_UINT32_VAL(STAT
, NUM
, lpr
);
524 para_sigaction(SIGUSR1
, dummy
);
527 * Copy the mmd structure to minimize the time we hold the mmd
530 mutex_lock(mmd_mutex
);
532 mutex_unlock(mmd_mutex
);
533 ret
= get_status(nmmd
, parser_friendly
, &s
);
534 ret
= send_sb(&cc
->scc
, s
, ret
, SBD_OUTPUT
, false);
537 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
539 ret
= empty_status_items(parser_friendly
, &esi
);
540 ret
= send_sb(&cc
->scc
, esi
, ret
, SBD_OUTPUT
, false);
544 send_afs_status(cc
, parser_friendly
);
546 if (num
> 0 && !--num
)
549 ret
= -E_SERVER_CRASH
;
556 EXPORT_SERVER_CMD_HANDLER(stat
);
558 /* fixed-length, human readable permission string */
559 const char *server_cmd_perms_str(unsigned int perms
)
561 static char result
[5];
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' : '-';
571 static int send_list_of_commands(struct command_context
*cc
)
574 const struct lls_command
*cmd
;
575 char *msg
= para_strdup("");
577 for (i
= 1; (cmd
= lls_cmd(i
, server_cmd_suite
)); i
++) {
578 const char *perms
= server_cmd_perms_str(server_command_perms
[i
]);
579 char *tmp
= make_message("%s%s\t%s\t%s\n", msg
,
580 lls_command_name(cmd
), perms
, lls_purpose(cmd
));
584 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
587 /* returns string that must be freed by the caller */
588 static struct server_command
*get_cmd_ptr(const char *name
, char **handler
)
590 struct server_command
*cmd
;
592 /* not found, look for commands supported by afs */
593 for (cmd
= afs_cmds
; cmd
->name
; cmd
++)
594 if (!strcmp(cmd
->name
, name
)) {
596 *handler
= para_strdup("afs");
602 static int com_help(struct command_context
*cc
, struct lls_parse_result
*lpr
)
605 char *long_help
, *buf
, *errctx
;
607 const struct lls_command
*cmd
;
609 ret
= lls(lls_check_arg_count(lpr
, 0, 1, &errctx
));
611 send_errctx(cc
, errctx
);
614 if (lls_num_inputs(lpr
) == 0)
615 return send_list_of_commands(cc
);
616 /* argument given for help */
617 ret
= lls(lls_lookup_subcmd(lls_input(0, lpr
), server_cmd_suite
,
620 send_errctx(cc
, errctx
);
623 cmd
= lls_cmd(ret
, server_cmd_suite
);
624 perms
= server_command_perms_txt
[ret
];
625 long_help
= lls_long_help(cmd
);
627 ret
= xasprintf(&buf
, "%spermissions: %s\n", long_help
, perms
);
629 return send_sb(&cc
->scc
, buf
, ret
, SBD_OUTPUT
, false);
631 EXPORT_SERVER_CMD_HANDLER(help
);
633 static int com_hup(__a_unused
struct command_context
*cc
,
634 __a_unused
struct lls_parse_result
*lpr
)
636 kill(getppid(), SIGHUP
);
639 EXPORT_SERVER_CMD_HANDLER(hup
);
641 static int com_term(__a_unused
struct command_context
*cc
,
642 __a_unused
struct lls_parse_result
*lpr
)
644 kill(getppid(), SIGTERM
);
647 EXPORT_SERVER_CMD_HANDLER(term
);
649 static int com_play(__a_unused
struct command_context
*cc
,
650 __a_unused
struct lls_parse_result
*lpr
)
652 mutex_lock(mmd_mutex
);
653 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
654 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
655 mutex_unlock(mmd_mutex
);
658 EXPORT_SERVER_CMD_HANDLER(play
);
660 static int com_stop(__a_unused
struct command_context
*cc
,
661 __a_unused
struct lls_parse_result
*lpr
)
663 mutex_lock(mmd_mutex
);
664 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
665 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
666 mmd
->new_vss_status_flags
|= VSS_NEXT
;
667 mutex_unlock(mmd_mutex
);
670 EXPORT_SERVER_CMD_HANDLER(stop
);
672 static int com_pause(__a_unused
struct command_context
*cc
,
673 __a_unused
struct lls_parse_result
*lpr
)
675 mutex_lock(mmd_mutex
);
676 if (!vss_paused() && !vss_stopped()) {
678 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
679 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
681 mutex_unlock(mmd_mutex
);
684 EXPORT_SERVER_CMD_HANDLER(pause
);
686 static int com_next(__a_unused
struct command_context
*cc
,
687 __a_unused
struct lls_parse_result
*lpr
)
689 mutex_lock(mmd_mutex
);
691 mmd
->new_vss_status_flags
|= VSS_NEXT
;
692 mutex_unlock(mmd_mutex
);
695 EXPORT_SERVER_CMD_HANDLER(next
);
697 static int com_nomore(__a_unused
struct command_context
*cc
,
698 __a_unused
struct lls_parse_result
*lpr
)
700 mutex_lock(mmd_mutex
);
701 if (vss_playing() || vss_paused())
702 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
703 mutex_unlock(mmd_mutex
);
706 EXPORT_SERVER_CMD_HANDLER(nomore
);
708 static int com_ff(__a_unused
struct command_context
*cc
,
709 struct lls_parse_result
*lpr
)
712 int ret
, backwards
= 0;
716 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
718 send_errctx(cc
, errctx
);
721 if (!(ret
= sscanf(lls_input(0, lpr
), "%u%c", &i
, &c
)))
722 return -E_COMMAND_SYNTAX
;
723 if (ret
> 1 && c
== '-')
724 backwards
= 1; /* jmp backwards */
725 mutex_lock(mmd_mutex
);
726 ret
= -E_NO_AUDIO_FILE
;
727 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
729 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
731 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
733 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
736 if (promille
> 1000) {
737 mmd
->new_vss_status_flags
|= VSS_NEXT
;
740 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
741 mmd
->new_vss_status_flags
|= VSS_REPOS
;
742 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
746 mutex_unlock(mmd_mutex
);
749 EXPORT_SERVER_CMD_HANDLER(ff
);
751 static int com_jmp(__a_unused
struct command_context
*cc
,
752 struct lls_parse_result
*lpr
)
758 ret
= lls(lls_check_arg_count(lpr
, 1, 1, &errctx
));
760 send_errctx(cc
, errctx
);
763 if (sscanf(lls_input(0, lpr
), "%lu", &i
) <= 0)
764 return -ERRNO_TO_PARA_ERROR(EINVAL
);
765 mutex_lock(mmd_mutex
);
766 ret
= -E_NO_AUDIO_FILE
;
767 if (!mmd
->afd
.afhi
.chunks_total
)
771 PARA_INFO_LOG("jumping to %lu%%\n", i
);
772 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50) / 100;
773 PARA_INFO_LOG("sent: %lu, offset before jmp: %li\n",
774 mmd
->chunks_sent
, mmd
->offset
);
775 mmd
->new_vss_status_flags
|= VSS_REPOS
;
776 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
780 mutex_unlock(mmd_mutex
);
783 EXPORT_SERVER_CMD_HANDLER(jmp
);
785 static int com_tasks(struct command_context
*cc
,
786 __a_unused
struct lls_parse_result
*lpr
)
788 char *tl
= server_get_tasks();
790 return send_sb(&cc
->scc
, tl
, strlen(tl
), SBD_OUTPUT
, false);
792 EXPORT_SERVER_CMD_HANDLER(tasks
);
795 * check if perms are sufficient to exec a command having perms cmd_perms.
796 * Returns 0 if perms are sufficient, -E_PERM otherwise.
798 static int check_perms(unsigned int perms
, const struct server_command
*cmd_ptr
)
800 PARA_DEBUG_LOG("checking permissions\n");
801 return (cmd_ptr
->perms
& perms
) < cmd_ptr
->perms
? -E_PERM
: 0;
804 static void reset_signals(void)
806 para_sigaction(SIGCHLD
, SIG_IGN
);
807 para_sigaction(SIGINT
, SIG_DFL
);
808 para_sigaction(SIGTERM
, SIG_DFL
);
809 para_sigaction(SIGHUP
, SIG_DFL
);
812 struct connection_features
{
813 bool aes_ctr128_requested
;
816 static int parse_auth_request(char *buf
, int len
, struct user
**u
,
817 struct connection_features
*cf
)
820 char *p
, *username
, **features
= NULL
;
821 size_t auth_rq_len
= strlen(AUTH_REQUEST_MSG
);
824 memset(cf
, 0, sizeof(*cf
));
825 if (len
< auth_rq_len
+ 2)
826 return -E_AUTH_REQUEST
;
827 if (strncmp(buf
, AUTH_REQUEST_MSG
, auth_rq_len
) != 0)
828 return -E_AUTH_REQUEST
;
829 username
= buf
+ auth_rq_len
;
830 p
= strchr(username
, ' ');
834 return -E_AUTH_REQUEST
;
837 create_argv(p
, ",", &features
);
838 for (i
= 0; features
[i
]; i
++) {
839 if (strcmp(features
[i
], "sideband") == 0)
841 if (strcmp(features
[i
], "aes_ctr128") == 0)
842 cf
->aes_ctr128_requested
= true;
844 ret
= -E_BAD_FEATURE
;
849 PARA_DEBUG_LOG("received auth request for user %s\n", username
);
850 *u
= lookup_user(username
);
857 #define HANDSHAKE_BUFSIZE 4096
859 static int run_command(struct command_context
*cc
, struct iovec
*iov
,
860 const char *peername
)
864 struct server_command
*cmd
= NULL
;
865 const struct lls_command
*lcmd
= NULL
;
867 struct lls_parse_result
*lpr
;
870 if (iov
->iov_base
== NULL
|| iov
->iov_len
== 0)
871 return -ERRNO_TO_PARA_ERROR(EINVAL
);
873 p
[iov
->iov_len
- 1] = '\0'; /* just to be sure */
875 ret
= lls(lls_lookup_subcmd(p
, server_cmd_suite
, &errctx
));
876 if (ret
>= 0 && !strcmp(p
, lls_command_name(lls_cmd(ret
,
877 server_cmd_suite
)))) {
878 perms
= server_command_perms
[ret
];
879 if ((perms
& cc
->u
->perms
) != perms
)
881 lcmd
= lls_cmd(ret
, server_cmd_suite
);
883 cmd
= get_cmd_ptr(p
, NULL
);
885 send_errctx(cc
, errctx
);
889 ret
= check_perms(cc
->u
->perms
, cmd
);
893 end
= iov
->iov_base
+ iov
->iov_len
;
894 for (i
= 0; p
< end
; i
++)
897 cc
->argv
= para_malloc((cc
->argc
+ 1) * sizeof(char *));
898 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++) {
899 cc
->argv
[i
] = para_strdup(p
);
902 cc
->argv
[cc
->argc
] = NULL
;
903 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", lcmd
?
904 lls_command_name(lcmd
) : cmd
->name
, cc
->u
->name
, peername
);
906 ret
= lls(lls_parse(cc
->argc
, cc
->argv
, lcmd
, &lpr
, &errctx
));
908 const struct server_cmd_user_data
*ud
= lls_user_data(lcmd
);
909 ret
= ud
->handler(cc
, lpr
);
910 lls_free_parse_result(lpr
, lcmd
);
912 send_errctx(cc
, errctx
);
914 ret
= cmd
->handler(cc
);
917 mutex_lock(mmd_mutex
);
919 if (ret
>= 0 && (perms
& AFS_WRITE
))
921 mutex_unlock(mmd_mutex
);
926 * Perform user authentication and execute a command.
928 * \param fd The file descriptor to send output to.
929 * \param peername Identifies the connecting peer.
931 * Whenever para_server accepts an incoming tcp connection on the port it
932 * listens on, it forks and the resulting child calls this function.
934 * An RSA-based challenge/response is used to authenticate the peer. It that
935 * authentication succeeds, a random session key is generated and sent back to
936 * the peer, encrypted with its RSA public key. From this point on, all
937 * transfers are crypted with this session key.
939 * Next it is checked if the peer supplied a valid server command or a command
940 * for the audio file selector. If yes, and if the user has sufficient
941 * permissions to execute that command, the function calls the corresponding
942 * command handler which does argument checking and further processing.
944 * In order to cope with a DOS attacks, a timeout is set up which terminates
945 * the function if the connection was not authenticated when the timeout
948 * \sa alarm(2), crypt.c, crypt.h
950 __noreturn
void handle_connect(int fd
, const char *peername
)
953 unsigned char rand_buf
[CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
954 unsigned char challenge_hash
[HASH_SIZE
];
955 char *command
= NULL
, *buf
= para_malloc(HANDSHAKE_BUFSIZE
) /* must be on the heap */;
957 struct command_context cc_struct
= {.peer
= peername
}, *cc
= &cc_struct
;
959 struct connection_features cf
;
963 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
964 ret
= mark_fd_blocking(fd
);
967 /* send Welcome message */
968 ret
= write_va_buffer(fd
, "This is para_server, version "
969 PACKAGE_VERSION
".\n"
970 "Features: sideband,aes_ctr128\n"
974 /* recv auth request line */
975 ret
= recv_buffer(fd
, buf
, HANDSHAKE_BUFSIZE
);
978 ret
= parse_auth_request(buf
, ret
, &cc
->u
, &cf
);
982 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
983 ret
= pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
984 (unsigned char *)buf
);
990 * We don't want to reveal our user names, so we send a
991 * challenge to the client even if the user does not exist, and
992 * fail the authentication later.
995 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
997 PARA_DEBUG_LOG("sending %d byte challenge + session key (%zu bytes)\n",
998 CHALLENGE_SIZE
, numbytes
);
999 ret
= send_sb(&cc
->scc
, buf
, numbytes
, SBD_CHALLENGE
, false);
1003 ret
= recv_sb(&cc
->scc
, SBD_CHALLENGE_RESPONSE
,
1004 HANDSHAKE_BUFSIZE
, &iov
);
1008 numbytes
= iov
.iov_len
;
1009 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes
);
1014 * The correct response is the hash of the first CHALLENGE_SIZE bytes
1015 * of the random data.
1018 if (numbytes
!= HASH_SIZE
)
1020 hash_function((char *)rand_buf
, CHALLENGE_SIZE
, challenge_hash
);
1021 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
1023 /* auth successful */
1025 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
1026 /* init stream cipher keys with the second part of the random buffer */
1027 cc
->scc
.recv
= sc_new(rand_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
,
1028 cf
.aes_ctr128_requested
);
1029 cc
->scc
.send
= sc_new(rand_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
1030 SESSION_KEY_LEN
, cf
.aes_ctr128_requested
);
1031 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_PROCEED
, false);
1034 ret
= recv_sb(&cc
->scc
, SBD_COMMAND
, MAX_COMMAND_LEN
, &iov
);
1037 ret
= run_command(cc
, &iov
, peername
);
1044 if (send_strerror(cc
, -ret
) >= 0)
1045 send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__FAILURE
, true);
1047 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1051 mutex_lock(mmd_mutex
);
1052 mmd
->active_connections
--;
1053 mutex_unlock(mmd_mutex
);
1055 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__SUCCESS
, true);
1057 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1059 sc_free(cc
->scc
.recv
);
1060 sc_free(cc
->scc
.send
);
1061 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);