2 * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file command.c Client authentication and server commands. */
12 #include <sys/types.h>
19 #include "server.cmdline.h"
32 #include "user_list.h"
33 #include "server_command_list.h"
34 #include "afs_command_list.h"
38 /** Commands including options must be shorter than this. */
39 #define MAX_COMMAND_LEN 32768
42 extern struct misc_meta_data
*mmd
;
43 extern struct sender senders
[];
44 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
46 static void dummy(__a_unused
int s
)
50 static void mmd_dup(struct misc_meta_data
*new_mmd
)
52 mutex_lock(mmd_mutex
);
54 mutex_unlock(mmd_mutex
);
58 * Compute human readable string containing vss status for given integer value.
60 * We don't want to use vss_playing() and friends here because we take a
61 * snapshot of the mmd struct and use the copy for computing the state of the
62 * vss. If the real data were used, we would take the mmd lock for a rather
63 * long time or risk to get an inconsistent view.
65 static char *vss_status_tohuman(unsigned int flags
)
67 if (flags
& VSS_PLAYING
)
68 return para_strdup("playing");
70 return para_strdup("stopped");
71 return para_strdup("paused");
75 * return human readable permission string. Never returns NULL.
77 static char *cmd_perms_itohuman(unsigned int perms
)
79 char *msg
= para_malloc(5 * sizeof(char));
81 msg
[0] = perms
& AFS_READ
? 'a' : '-';
82 msg
[1] = perms
& AFS_WRITE
? 'A' : '-';
83 msg
[2] = perms
& VSS_READ
? 'v' : '-';
84 msg
[3] = perms
& VSS_WRITE
? 'V' : '-';
92 static char *vss_get_status_flags(unsigned int flags
)
94 char *msg
= para_malloc(5 * sizeof(char));
96 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
97 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
98 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
99 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
104 static char *get_status(struct misc_meta_data
*nmmd
, int parser_friendly
)
107 char *status
, *flags
; /* vss status info */
108 /* nobody updates our version of "now" */
109 char *ut
= get_server_uptime_str(NULL
);
110 long offset
= (nmmd
->offset
+ 500) / 1000;
111 struct timeval current_time
;
113 struct para_buffer b
= {.flags
= parser_friendly
? PBF_SIZE_PREFIX
: 0};
115 /* report real status */
116 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
117 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
118 if (nmmd
->size
) { /* parent currently has an audio file open */
119 localtime_r(&nmmd
->mtime
, &mtime_tm
);
120 strftime(mtime
, 29, "%b %d %Y", &mtime_tm
);
122 gettimeofday(¤t_time
, NULL
);
124 * The calls to WRITE_STATUS_ITEM() below never fail because
125 * b->max_size is zero (unlimited), see para_printf(). However, clang
126 * is not smart enough to prove this and complains nevertheless.
127 * Casting the return value to void silences clang.
129 (void)WRITE_STATUS_ITEM(&b
, SI_FILE_SIZE
, "%zu\n", nmmd
->size
/ 1024);
130 (void)WRITE_STATUS_ITEM(&b
, SI_MTIME
, "%s\n", mtime
);
131 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS
, "%s\n", status
);
132 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS_FLAGS
, "%s\n", flags
);
133 (void)WRITE_STATUS_ITEM(&b
, SI_OFFSET
, "%li\n", offset
);
134 (void)WRITE_STATUS_ITEM(&b
, SI_AFS_MODE
, "%s\n", mmd
->afs_mode_string
);
135 (void)WRITE_STATUS_ITEM(&b
, SI_STREAM_START
, "%lu.%lu\n",
136 (long unsigned)nmmd
->stream_start
.tv_sec
,
137 (long unsigned)nmmd
->stream_start
.tv_usec
);
138 (void)WRITE_STATUS_ITEM(&b
, SI_CURRENT_TIME
, "%lu.%lu\n",
139 (long unsigned)current_time
.tv_sec
,
140 (long unsigned)current_time
.tv_usec
);
147 static int check_sender_args(int argc
, char * const * argv
, struct sender_command_data
*scd
)
150 /* this has to match sender.h */
151 const char *subcmds
[] = {"add", "delete", "allow", "deny", "on", "off", NULL
};
153 scd
->sender_num
= -1;
155 return -E_COMMAND_SYNTAX
;
156 for (i
= 0; senders
[i
].name
; i
++)
157 if (!strcmp(senders
[i
].name
, argv
[1]))
159 PARA_DEBUG_LOG("%d:%s\n", argc
, argv
[1]);
160 if (!senders
[i
].name
)
161 return -E_COMMAND_SYNTAX
;
163 for (i
= 0; subcmds
[i
]; i
++)
164 if (!strcmp(subcmds
[i
], argv
[2]))
167 return -E_COMMAND_SYNTAX
;
169 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
])
170 return -E_SENDER_CMD
;
171 switch (scd
->cmd_num
) {
175 return -E_COMMAND_SYNTAX
;
179 if (argc
!= 4 || parse_cidr(argv
[3], scd
->host
,
180 sizeof(scd
->host
), &scd
->netmask
) == NULL
)
181 return -E_COMMAND_SYNTAX
;
186 return -E_COMMAND_SYNTAX
;
187 return parse_fec_url(argv
[3], scd
);
189 return -E_COMMAND_SYNTAX
;
194 int com_sender(struct command_context
*cc
)
198 struct sender_command_data scd
;
201 for (i
= 0; senders
[i
].name
; i
++) {
202 char *tmp
= make_message("%s%s\n",
203 msg
? msg
: "", senders
[i
].name
);
207 ret
= sc_send_buffer(&cc
->scc
, msg
);
211 ret
= check_sender_args(cc
->argc
, cc
->argv
, &scd
);
213 if (scd
.sender_num
< 0)
215 msg
= senders
[scd
.sender_num
].help();
216 ret
= sc_send_buffer(&cc
->scc
, msg
);
221 switch (scd
.cmd_num
) {
224 assert(senders
[scd
.sender_num
].resolve_target
);
225 ret
= senders
[scd
.sender_num
].resolve_target(cc
->argv
[3], &scd
);
230 for (i
= 0; i
< 10; i
++) {
231 mutex_lock(mmd_mutex
);
232 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
233 mutex_unlock(mmd_mutex
);
237 memcpy(&mmd
->sender_cmd_data
, &scd
, sizeof(scd
));
238 mutex_unlock(mmd_mutex
);
241 return (i
< 10)? 1 : -E_LOCK
;
245 int com_si(struct command_context
*cc
)
249 char *sender_info
= NULL
;
252 return -E_COMMAND_SYNTAX
;
253 mutex_lock(mmd_mutex
);
254 for (i
= 0; senders
[i
].name
; i
++) {
255 char *info
= senders
[i
].info();
256 sender_info
= para_strcat(sender_info
, info
);
259 ut
= get_server_uptime_str(now
);
260 ret
= sc_send_va_buffer(&cc
->scc
, "version: " GIT_VERSION
"\n"
261 "up: %s\nplayed: %u\n"
264 "connections (active/accepted/total): %u/%u/%u\n"
265 "current loglevel: %s\n"
266 "supported audio formats: %s\n"
271 mmd
->active_connections
,
275 SERVER_AUDIO_FORMATS
,
278 mutex_unlock(mmd_mutex
);
285 int com_version(struct command_context
*cc
)
288 return -E_COMMAND_SYNTAX
;
289 return sc_send_buffer(&cc
->scc
, VERSION_TEXT("server")
290 "built: " BUILD_DATE
"\n"
291 UNAME_RS
", " CC_VERSION
"\n"
295 #define EMPTY_STATUS_ITEMS \
300 ITEM(ATTRIBUTES_BITMAP) \
301 ITEM(ATTRIBUTES_TXT) \
312 ITEM(SECONDS_TOTAL) \
324 * Write a list of audio-file related status items with empty values.
326 * This is used by vss when currently no audio file is open.
328 static char *empty_status_items(int parser_friendly
)
332 #define ITEM(x) "0004 %02x:\n"
335 #define ITEM(x) , SI_ ## x
340 #define ITEM(x) "%s:\n"
343 #define ITEM(x) ,status_item_list[SI_ ## x]
348 #undef EMPTY_STATUS_ITEMS
351 int com_stat(struct command_context
*cc
)
354 struct misc_meta_data tmp
, *nmmd
= &tmp
;
357 int parser_friendly
= 0;
359 para_sigaction(SIGUSR1
, dummy
);
361 for (i
= 1; i
< cc
->argc
; i
++) {
362 const char *arg
= cc
->argv
[i
];
365 if (!strcmp(arg
, "--")) {
369 if (!strncmp(arg
, "-n=", 3)) {
370 ret
= para_atoi32(arg
+ 3, &num
);
375 if (!strcmp(arg
, "-p")) {
379 return -E_COMMAND_SYNTAX
;
382 return -E_COMMAND_SYNTAX
;
385 s
= get_status(nmmd
, parser_friendly
);
386 ret
= sc_send_buffer(&cc
->scc
, s
);
390 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
393 esi
= empty_status_items(parser_friendly
);
394 ret
= sc_send_buffer(&cc
->scc
, esi
);
398 send_afs_status(cc
, parser_friendly
);
400 if (num
> 0 && !--num
)
404 return -E_SERVER_CRASH
;
410 static int send_list_of_commands(struct stream_cipher_context
*scc
, struct server_command
*cmd
,
415 for (i
= 1; cmd
->name
; cmd
++, i
++) {
416 char *perms
= cmd_perms_itohuman(cmd
->perms
);
417 ret
= sc_send_va_buffer(scc
, "%s\t%s\t%s\t%s\n", cmd
->name
,
428 /* returns string that must be freed by the caller */
429 static struct server_command
*get_cmd_ptr(const char *name
, char **handler
)
431 struct server_command
*cmd
;
433 for (cmd
= server_cmds
; cmd
->name
; cmd
++)
434 if (!strcmp(cmd
->name
, name
)) {
436 *handler
= para_strdup("server"); /* server commands */
439 /* not found, look for commands supported by afs */
440 for (cmd
= afs_cmds
; cmd
->name
; cmd
++)
441 if (!strcmp(cmd
->name
, name
)) {
443 *handler
= para_strdup("afs");
450 int com_help(struct command_context
*cc
)
452 struct server_command
*cmd
;
453 char *perms
, *handler
;
457 /* no argument given, print list of commands */
458 if ((ret
= send_list_of_commands(&cc
->scc
, server_cmds
, "server")) < 0)
460 return send_list_of_commands(&cc
->scc
, afs_cmds
, "afs");
462 /* argument given for help */
463 cmd
= get_cmd_ptr(cc
->argv
[1], &handler
);
466 perms
= cmd_perms_itohuman(cmd
->perms
);
467 ret
= sc_send_va_buffer(&cc
->scc
,
486 int com_hup(struct command_context
*cc
)
489 return -E_COMMAND_SYNTAX
;
490 kill(getppid(), SIGHUP
);
495 int com_term(struct command_context
*cc
)
498 return -E_COMMAND_SYNTAX
;
499 kill(getppid(), SIGTERM
);
503 int com_play(struct command_context
*cc
)
506 return -E_COMMAND_SYNTAX
;
507 mutex_lock(mmd_mutex
);
508 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
509 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
510 mutex_unlock(mmd_mutex
);
515 int com_stop(struct command_context
*cc
)
518 return -E_COMMAND_SYNTAX
;
519 mutex_lock(mmd_mutex
);
520 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
521 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
522 mmd
->new_vss_status_flags
|= VSS_NEXT
;
523 mutex_unlock(mmd_mutex
);
528 int com_pause(struct command_context
*cc
)
531 return -E_COMMAND_SYNTAX
;
532 mutex_lock(mmd_mutex
);
533 if (!vss_paused() && !vss_stopped()) {
535 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
536 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
538 mutex_unlock(mmd_mutex
);
543 int com_next(struct command_context
*cc
)
546 return -E_COMMAND_SYNTAX
;
547 mutex_lock(mmd_mutex
);
549 mmd
->new_vss_status_flags
|= VSS_NEXT
;
550 mutex_unlock(mmd_mutex
);
555 int com_nomore(struct command_context
*cc
)
558 return -E_COMMAND_SYNTAX
;
559 mutex_lock(mmd_mutex
);
560 if (vss_playing() || vss_paused())
561 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
562 mutex_unlock(mmd_mutex
);
567 int com_ff(struct command_context
*cc
)
570 int ret
, backwards
= 0;
575 return -E_COMMAND_SYNTAX
;
576 if (!(ret
= sscanf(cc
->argv
[1], "%u%c", &i
, &c
)))
577 return -E_COMMAND_SYNTAX
;
578 if (ret
> 1 && c
== '-')
579 backwards
= 1; /* jmp backwards */
580 mutex_lock(mmd_mutex
);
581 ret
= -E_NO_AUDIO_FILE
;
582 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
584 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
586 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
588 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
591 if (promille
> 1000) {
592 mmd
->new_vss_status_flags
|= VSS_NEXT
;
595 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
596 mmd
->new_vss_status_flags
|= VSS_REPOS
;
597 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
601 mutex_unlock(mmd_mutex
);
606 int com_jmp(struct command_context
*cc
)
612 return -E_COMMAND_SYNTAX
;
613 if (sscanf(cc
->argv
[1], "%lu", &i
) <= 0)
614 return -E_COMMAND_SYNTAX
;
615 mutex_lock(mmd_mutex
);
616 ret
= -E_NO_AUDIO_FILE
;
617 if (!mmd
->afd
.afhi
.chunks_total
)
621 PARA_INFO_LOG("jumping to %lu%%\n", i
);
622 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50)/ 100;
623 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
624 mmd
->chunks_sent
, mmd
->offset
);
625 mmd
->new_vss_status_flags
|= VSS_REPOS
;
626 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
630 mutex_unlock(mmd_mutex
);
635 * check if perms are sufficient to exec a command having perms cmd_perms.
636 * Returns 0 if perms are sufficient, -E_PERM otherwise.
638 static int check_perms(unsigned int perms
, struct server_command
*cmd_ptr
)
640 PARA_DEBUG_LOG("checking permissions\n");
641 return (cmd_ptr
->perms
& perms
) < cmd_ptr
->perms
? -E_PERM
: 0;
645 * Parse first string from *cmd and lookup in table of valid commands.
646 * On error, NULL is returned.
648 static struct server_command
*parse_cmd(const char *cmdstr
)
653 sscanf(cmdstr
, "%200s%n", buf
, &n
);
657 return get_cmd_ptr(buf
, NULL
);
660 static int read_command(struct stream_cipher_context
*scc
, char **result
)
664 char *command
= NULL
;
670 ret
= sc_recv_buffer(scc
, buf
, sizeof(buf
));
676 ret
= -E_COMMAND_SYNTAX
;
677 if (command
&& numbytes
+ strlen(command
) > MAX_COMMAND_LEN
) /* DOS */
679 command
= para_strcat(command
, buf
);
680 p
= strstr(command
, EOC_MSG
);
686 ret
= command
? 1 : -E_COMMAND_SYNTAX
;
696 static void reset_signals(void)
698 para_sigaction(SIGCHLD
, SIG_IGN
);
699 para_sigaction(SIGINT
, SIG_DFL
);
700 para_sigaction(SIGTERM
, SIG_DFL
);
701 para_sigaction(SIGHUP
, SIG_DFL
);
705 * Perform user authentication and execute a command.
707 * \param fd The file descriptor to send output to.
708 * \param peername Identifies the connecting peer.
710 * Whenever para_server accepts an incoming tcp connection on
711 * the port it listens on, it forks and the resulting child
712 * calls this function.
714 * An RSA-based challenge/response is used to authenticate
715 * the peer. It that authentication succeeds, a random
716 * session key is generated and sent back to the peer,
717 * encrypted with its RSA public key. From this point on,
718 * all transfers are crypted with this session key.
720 * Next it is checked if the peer supplied a valid server command or a command
721 * for the audio file selector. If yes, and if the user has sufficient
722 * permissions to execute that command, the function calls the corresponding
723 * command handler which does argument checking and further processing.
725 * In order to cope with a DOS attacks, a timeout is set up
726 * which terminates the function if the connection was not
727 * authenticated when the timeout expires.
729 * \sa alarm(2), crypt.c, crypt.h
731 __noreturn
void handle_connect(int fd
, const char *peername
)
735 unsigned char rand_buf
[CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
736 unsigned char challenge_hash
[HASH_SIZE
];
737 char *p
, *command
= NULL
;
739 struct command_context cc_struct
= {.peer
= peername
}, *cc
= &cc_struct
;
743 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
744 ret
= mark_fd_blocking(fd
);
747 /* send Welcome message */
748 ret
= write_va_buffer(fd
, "This is para_server, version "
749 PACKAGE_VERSION
".\n" );
752 /* recv auth request line */
753 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
757 ret
= -E_AUTH_REQUEST
;
760 ret
= -E_AUTH_REQUEST
;
761 if (strncmp(buf
, AUTH_REQUEST_MSG
, strlen(AUTH_REQUEST_MSG
)))
763 p
= buf
+ strlen(AUTH_REQUEST_MSG
);
764 PARA_DEBUG_LOG("received auth request for user %s\n", p
);
765 cc
->u
= lookup_user(p
);
767 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
768 ret
= pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
769 (unsigned char *)buf
);
775 * We don't want to reveal our user names, so we send a
776 * challenge to the client even if the user does not exist, and
777 * fail the authentication later.
780 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
782 PARA_DEBUG_LOG("sending %u byte challenge + rc4 keys (%zu bytes)\n",
783 CHALLENGE_SIZE
, numbytes
);
784 ret
= write_all(fd
, buf
, numbytes
);
787 /* recv challenge response */
788 ret
= recv_bin_buffer(fd
, buf
, HASH_SIZE
);
792 PARA_DEBUG_LOG("received %d bytes challenge response\n", ret
);
797 * The correct response is the hash of the first CHALLENGE_SIZE bytes
798 * of the random data.
801 if (numbytes
!= HASH_SIZE
)
803 hash_function((char *)rand_buf
, CHALLENGE_SIZE
, challenge_hash
);
804 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
806 /* auth successful */
808 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
809 /* init stream cipher keys with the second part of the random buffer */
810 cc
->scc
.recv
= sc_new(rand_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
);
811 cc
->scc
.send
= sc_new(rand_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
, SESSION_KEY_LEN
);
812 ret
= sc_send_buffer(&cc
->scc
, PROCEED_MSG
);
815 ret
= read_command(&cc
->scc
, &command
);
816 if (ret
== -E_COMMAND_SYNTAX
)
821 cc
->cmd
= parse_cmd(command
);
824 /* valid command, check permissions */
825 ret
= check_perms(cc
->u
->perms
, cc
->cmd
);
828 /* valid command and sufficient perms */
829 ret
= create_argv(command
, "\n", &cc
->argv
);
833 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cc
->cmd
->name
,
834 cc
->u
->name
, peername
);
835 ret
= cc
->cmd
->handler(cc
);
837 mutex_lock(mmd_mutex
);
839 mutex_unlock(mmd_mutex
);
843 sc_send_va_buffer(&cc
->scc
, "%s\n", para_strerror(-ret
));
845 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
848 sc_free(cc
->scc
.recv
);
849 sc_free(cc
->scc
.send
);
850 mutex_lock(mmd_mutex
);
851 if (cc
->cmd
&& (cc
->cmd
->perms
& AFS_WRITE
) && ret
>= 0)
853 mmd
->active_connections
--;
854 mutex_unlock(mmd_mutex
);
855 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);