2 * Copyright (C) 1997-2009 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. */
11 #include <sys/types.h>
13 #include <openssl/rc4.h>
18 #include "server.cmdline.h"
31 #include "user_list.h"
32 #include "server_command_list.h"
33 #include "afs_command_list.h"
37 /** Commands including options must be shorter than this. */
38 #define MAX_COMMAND_LEN 32768
40 static RC4_KEY rc4_recv_key
;
41 static RC4_KEY rc4_send_key
;
42 static unsigned char rc4_buf
[2 * RC4_KEY_LEN
];
45 extern struct misc_meta_data
*mmd
;
46 extern struct sender senders
[];
48 static void dummy(__a_unused
int s
)
52 static void mmd_dup(struct misc_meta_data
*new_mmd
)
54 mutex_lock(mmd_mutex
);
56 mutex_unlock(mmd_mutex
);
60 * Compute human readable string containing vss status for given integer value.
62 * We don't want to use vss_playing() and friends here because we take a
63 * snapshot of the mmd struct and use the copy for computing the state of the
64 * vss. If the real data were used, we would take the mmd lock for a rather
65 * long time or risk to get an inconsistent view.
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");
77 * return human readable permission string. Never returns NULL.
79 static char *cmd_perms_itohuman(unsigned int perms
)
81 char *msg
= para_malloc(5 * sizeof(char));
83 msg
[0] = perms
& AFS_READ
? 'a' : '-';
84 msg
[1] = perms
& AFS_WRITE
? 'A' : '-';
85 msg
[2] = perms
& VSS_READ
? 'v' : '-';
86 msg
[3] = perms
& VSS_WRITE
? 'V' : '-';
94 static char *vss_get_status_flags(unsigned int flags
)
96 char *msg
= para_malloc(5 * sizeof(char));
98 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
99 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
100 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
101 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
106 static char *get_status(struct misc_meta_data
*nmmd
)
108 char *ret
, mtime
[30] = "";
109 char *status
, *flags
; /* vss status info */
110 char *ut
= uptime_str();
111 long offset
= (nmmd
->offset
+ 500) / 1000;
112 struct timeval current_time
;
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 "%s: %zu\n" /* file size */
125 "%s: %s\n" /* mtime */
126 "%s: %s\n" /* status */
127 "%s: %s\n" /* status flags */
128 "%s: %li\n" /* offset */
129 "%s: %s\n" /* afs mode */
130 "%s: %lu.%lu\n" /* stream start */
131 "%s: %lu.%lu\n", /* current server time */
132 status_item_list
[SI_FILE_SIZE
], nmmd
->size
/ 1024,
133 status_item_list
[SI_MTIME
], mtime
,
134 status_item_list
[SI_STATUS
], status
,
135 status_item_list
[SI_STATUS_FLAGS
], flags
,
137 status_item_list
[SI_OFFSET
], offset
,
138 status_item_list
[SI_AFS_MODE
], mmd
->afs_mode_string
,
140 status_item_list
[SI_STREAM_START
],
141 (long unsigned)nmmd
->stream_start
.tv_sec
,
142 (long unsigned)nmmd
->stream_start
.tv_usec
,
143 status_item_list
[SI_CURRENT_TIME
],
144 (long unsigned)current_time
.tv_sec
,
145 (long unsigned)current_time
.tv_usec
153 static int check_sender_args(int argc
, char * const * argv
, struct sender_command_data
*scd
)
156 /* this has to match sender.h */
157 const char *subcmds
[] = {"add", "delete", "allow", "deny", "on", "off", NULL
};
159 scd
->sender_num
= -1;
161 return -E_COMMAND_SYNTAX
;
162 for (i
= 0; senders
[i
].name
; i
++)
163 if (!strcmp(senders
[i
].name
, argv
[1]))
165 PARA_DEBUG_LOG("%d:%s\n", argc
, argv
[1]);
166 if (!senders
[i
].name
)
167 return -E_COMMAND_SYNTAX
;
169 for (i
= 0; subcmds
[i
]; i
++)
170 if (!strcmp(subcmds
[i
], argv
[2]))
173 return -E_COMMAND_SYNTAX
;
175 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
])
176 return -E_SENDER_CMD
;
177 switch (scd
->cmd_num
) {
181 return -E_COMMAND_SYNTAX
;
185 if (argc
!= 4 && argc
!= 5)
186 return -E_COMMAND_SYNTAX
;
187 if (!is_valid_ipv4_address(argv
[3]))
188 return -E_COMMAND_SYNTAX
;
191 scd
->netmask
= atoi(argv
[4]);
192 if (scd
->netmask
< 0 || scd
->netmask
> 32)
193 return -E_COMMAND_SYNTAX
;
195 strncpy(scd
->host
, argv
[3], sizeof(scd
->host
));
200 return -E_COMMAND_SYNTAX
;
201 return parse_fec_url(argv
[3], scd
);
203 return -E_COMMAND_SYNTAX
;
208 int com_sender(int fd
, int argc
, char * const * argv
)
211 struct sender_command_data scd
;
215 for (i
= 0; senders
[i
].name
; i
++) {
216 char *tmp
= make_message("%s%s\n",
217 msg
? msg
: "", senders
[i
].name
);
221 ret
= send_buffer(fd
, msg
);
225 ret
= check_sender_args(argc
, argv
, &scd
);
228 if (scd
.sender_num
< 0)
230 msg
= senders
[scd
.sender_num
].help();
231 ret
= send_buffer(fd
, msg
);
235 for (i
= 0; i
< 10; i
++) {
236 mutex_lock(mmd_mutex
);
237 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
238 mutex_unlock(mmd_mutex
);
242 memcpy(&mmd
->sender_cmd_data
, &scd
, sizeof(scd
));
243 mutex_unlock(mmd_mutex
);
246 return (i
< 10)? 1 : -E_LOCK
;
250 int com_si(int fd
, int argc
, __a_unused
char * const * argv
)
254 char *sender_info
= NULL
, *sender_list
= NULL
;
257 return -E_COMMAND_SYNTAX
;
258 mutex_lock(mmd_mutex
);
259 for (i
= 0; senders
[i
].name
; i
++) {
260 char *info
= senders
[i
].info();
261 sender_info
= para_strcat(sender_info
, info
);
263 sender_list
= para_strcat(sender_list
, senders
[i
].name
);
264 sender_list
= para_strcat(sender_list
, " ");
267 ret
= send_va_buffer(fd
, "up: %s\nplayed: %u\n"
270 "connections (active/accepted/total): %u/%u/%u\n"
271 "current loglevel: %s\n"
272 "supported audio formats: %s\n"
273 "supported senders: %s\n"
278 mmd
->active_connections
,
282 supported_audio_formats(),
286 mutex_unlock(mmd_mutex
);
294 int com_version(int fd
, int argc
, __a_unused
char * const * argv
)
297 return -E_COMMAND_SYNTAX
;
298 return send_buffer(fd
, VERSION_TEXT("server")
299 "built: " BUILD_DATE
"\n"
300 UNAME_RS
", " CC_VERSION
"\n"
305 * Write a list of audio-file related status items with empty values.
307 * This is used by vss when currently no audio file is open.
309 static char *empty_status_items(void)
313 "%s: \n" /* dirname */
314 "%s: \n" /* basename */
316 "%s: \n" /* attributes bitmap */
317 "%s: \n" /* attributes txt */
319 "%s: \n" /* image id */
320 "%s: \n" /* image name */
321 "%s: \n" /* lyrics id */
322 "%s: \n" /* lyrics name */
323 "%s: \n" /* bitrate */
324 "%s: \n" /* format */
325 "%s: \n" /* frequency */
326 "%s: \n" /* channels */
327 "%s: \n" /* duration */
328 "%s: \n" /* seconds total */
329 "%s: \n" /* num played */
330 "%s: \n" /* last played */
331 "%s: \n" /* techinfo */
332 "%s: \n" /* artist */
336 "%s: \n" /* comment */
337 "%s: \n" /* amplification */
339 status_item_list
[SI_PATH
],
340 status_item_list
[SI_DIRECTORY
],
341 status_item_list
[SI_BASENAME
],
342 status_item_list
[SI_SCORE
],
343 status_item_list
[SI_ATTRIBUTES_BITMAP
],
344 status_item_list
[SI_ATTRIBUTES_TXT
],
345 status_item_list
[SI_HASH
],
346 status_item_list
[SI_IMAGE_ID
],
347 status_item_list
[SI_IMAGE_NAME
],
348 status_item_list
[SI_LYRICS_ID
],
349 status_item_list
[SI_LYRICS_NAME
],
350 status_item_list
[SI_BITRATE
],
351 status_item_list
[SI_FORMAT
],
352 status_item_list
[SI_FREQUENCY
],
353 status_item_list
[SI_CHANNELS
],
354 status_item_list
[SI_DURATION
],
355 status_item_list
[SI_SECONDS_TOTAL
],
356 status_item_list
[SI_NUM_PLAYED
],
357 status_item_list
[SI_LAST_PLAYED
],
358 status_item_list
[SI_TECHINFO
],
359 status_item_list
[SI_ARTIST
],
360 status_item_list
[SI_TITLE
],
361 status_item_list
[SI_YEAR
],
362 status_item_list
[SI_ALBUM
],
363 status_item_list
[SI_COMMENT
],
364 status_item_list
[SI_AMPLIFICATION
]
369 int com_stat(int fd
, int argc
, char * const * argv
)
371 int ret
, num
= 0;/* status will be printed that many
372 * times. num <= 0 means: print forever
374 struct misc_meta_data tmp
, *nmmd
= &tmp
;
377 para_sigaction(SIGUSR1
, dummy
);
384 s
= get_status(nmmd
);
385 ret
= send_buffer(fd
, s
);
389 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
392 esi
= empty_status_items();
393 ret
= send_buffer(fd
, esi
);
399 if (num
> 0 && !--num
)
403 return -E_SERVER_CRASH
;
409 static int send_list_of_commands(int fd
, struct server_command
*cmd
,
414 for (i
= 1; cmd
->name
; cmd
++, i
++) {
415 char *perms
= cmd_perms_itohuman(cmd
->perms
);
416 ret
= send_va_buffer(fd
, "%s\t%s\t%s\t%s\n", cmd
->name
,
427 /* returns string that must be freed by the caller */
428 static struct server_command
*get_cmd_ptr(const char *name
, char **handler
)
430 struct server_command
*cmd
;
432 for (cmd
= server_cmds
; cmd
->name
; cmd
++)
433 if (!strcmp(cmd
->name
, name
)) {
435 *handler
= para_strdup("server"); /* server commands */
438 /* not found, look for commands supported by afs */
439 for (cmd
= afs_cmds
; cmd
->name
; cmd
++)
440 if (!strcmp(cmd
->name
, name
)) {
442 *handler
= para_strdup("afs");
449 int com_help(int fd
, int argc
, char * const * argv
)
451 struct server_command
*cmd
;
452 char *perms
, *handler
;
456 /* no argument given, print list of commands */
457 if ((ret
= send_list_of_commands(fd
, server_cmds
, "server")) < 0)
459 return send_list_of_commands(fd
, afs_cmds
, "afs");
461 /* argument given for help */
462 cmd
= get_cmd_ptr(argv
[1], &handler
);
467 perms
= cmd_perms_itohuman(cmd
->perms
);
468 ret
= send_va_buffer(fd
,
487 int com_hup(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
490 return -E_COMMAND_SYNTAX
;
491 kill(getppid(), SIGHUP
);
496 int com_term(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
499 return -E_COMMAND_SYNTAX
;
500 kill(getppid(), SIGTERM
);
504 int com_play(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
507 return -E_COMMAND_SYNTAX
;
508 mutex_lock(mmd_mutex
);
509 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
510 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
511 mutex_unlock(mmd_mutex
);
517 int com_stop(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
520 return -E_COMMAND_SYNTAX
;
521 mutex_lock(mmd_mutex
);
522 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
523 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
524 mmd
->new_vss_status_flags
|= VSS_NEXT
;
525 mutex_unlock(mmd_mutex
);
530 int com_pause(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
533 return -E_COMMAND_SYNTAX
;
534 mutex_lock(mmd_mutex
);
535 if (!vss_paused() && !vss_stopped()) {
537 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
538 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
540 mutex_unlock(mmd_mutex
);
545 int com_next(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
548 return -E_COMMAND_SYNTAX
;
549 mutex_lock(mmd_mutex
);
551 mmd
->new_vss_status_flags
|= VSS_NEXT
;
552 mutex_unlock(mmd_mutex
);
557 int com_nomore(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
560 return -E_COMMAND_SYNTAX
;
561 mutex_lock(mmd_mutex
);
562 if (vss_playing() || vss_paused())
563 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
564 mutex_unlock(mmd_mutex
);
569 int com_ff(__a_unused
int fd
, int argc
, char * const * argv
)
572 int ret
, backwards
= 0;
577 return -E_COMMAND_SYNTAX
;
578 if (!(ret
= sscanf(argv
[1], "%u%c", &i
, &c
)))
579 return -E_COMMAND_SYNTAX
;
580 if (ret
> 1 && c
== '-')
581 backwards
= 1; /* jmp backwards */
582 mutex_lock(mmd_mutex
);
583 ret
= -E_NO_AUDIO_FILE
;
584 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
586 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
588 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
590 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
593 if (promille
> 1000) {
594 mmd
->new_vss_status_flags
|= VSS_NEXT
;
597 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
598 mmd
->new_vss_status_flags
|= VSS_REPOS
;
599 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
603 mutex_unlock(mmd_mutex
);
608 int com_jmp(__a_unused
int fd
, int argc
, char * const * argv
)
614 return -E_COMMAND_SYNTAX
;
615 if (sscanf(argv
[1], "%lu", &i
) <= 0)
616 return -E_COMMAND_SYNTAX
;
617 mutex_lock(mmd_mutex
);
618 ret
= -E_NO_AUDIO_FILE
;
619 if (!mmd
->afd
.afhi
.chunks_total
)
623 PARA_INFO_LOG("jumping to %lu%%\n", i
);
624 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50)/ 100;
625 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
626 mmd
->chunks_sent
, mmd
->offset
);
627 mmd
->new_vss_status_flags
|= VSS_REPOS
;
628 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
632 mutex_unlock(mmd_mutex
);
637 * check if perms are sufficient to exec a command having perms cmd_perms.
638 * Returns 0 if perms are sufficient, -E_PERM otherwise.
640 static int check_perms(unsigned int perms
, struct server_command
*cmd_ptr
)
642 PARA_DEBUG_LOG("checking permissions\n");
643 return (cmd_ptr
->perms
& perms
) < cmd_ptr
->perms
? -E_PERM
: 0;
647 * Parse first string from *cmd and lookup in table of valid commands.
648 * On error, NULL is returned.
650 static struct server_command
*parse_cmd(const char *cmdstr
)
655 sscanf(cmdstr
, "%200s%n", buf
, &n
);
659 return get_cmd_ptr(buf
, NULL
);
662 static void init_rc4_keys(void)
666 for (i
= 0; i
< 2 * RC4_KEY_LEN
; i
++)
667 rc4_buf
[i
] = para_random(256);
668 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
669 (unsigned char) rc4_buf
[0],
670 (unsigned char) rc4_buf
[RC4_KEY_LEN
]);
671 RC4_set_key(&rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
);
672 RC4_set_key(&rc4_send_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
675 static void rc4_recv(unsigned long len
, const unsigned char *indata
,
676 unsigned char *outdata
, __a_unused
void *private_data
)
678 RC4(&rc4_recv_key
, len
, indata
, outdata
);
681 static void rc4_send(unsigned long len
, const unsigned char *indata
,
682 unsigned char *outdata
, __a_unused
void *private_data
)
684 RC4(&rc4_send_key
, len
, indata
, outdata
);
687 static int read_command(int fd
, char **result
)
691 char *command
= NULL
;
697 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
703 ret
= -E_COMMAND_SYNTAX
;
704 if (command
&& numbytes
+ strlen(command
) > MAX_COMMAND_LEN
) /* DOS */
706 command
= para_strcat(command
, buf
);
707 p
= strstr(command
, EOC_MSG
);
713 ret
= command
? 1 : -E_COMMAND_SYNTAX
;
723 static void reset_signals(void)
725 para_sigaction(SIGCHLD
, SIG_IGN
);
726 para_sigaction(SIGINT
, SIG_DFL
);
727 para_sigaction(SIGTERM
, SIG_DFL
);
728 para_sigaction(SIGHUP
, SIG_DFL
);
732 * Perform user authentication and execute a command.
734 * \param fd The file descriptor to send output to.
735 * \param peername Identifies the connecting peer.
737 * Whenever para_server accepts an incoming tcp connection on
738 * the port it listens on, it forks and the resulting child
739 * calls this function.
741 * An RSA-based challenge/response is used to authenticate
742 * the peer. It that authentication succeeds, a random RC4
743 * session key is generated and sent back to the peer,
744 * encrypted with its RSA public key. From this point on,
745 * all transfers are crypted with this session key.
747 * Next it is checked if the peer supplied a valid server command or a command
748 * for the audio file selector. If yes, and if the user has sufficient
749 * permissions to execute that command, the function calls the corresponding
750 * command handler which does argument checking and further processing.
752 * In order to cope with a DOS attacks, a timeout is set up
753 * which terminates the function if the connection was not
754 * authenticated when the timeout expires.
756 * \sa alarm(2), rc4(3), crypt.c, crypt.h
758 __noreturn
void handle_connect(int fd
, const char *peername
)
760 int ret
, argc
, use_rc4
= 0;
762 unsigned char crypt_buf
[MAXLINE
];
764 struct server_command
*cmd
= NULL
;
765 long unsigned challenge_nr
, chall_response
;
767 char *p
, *command
= NULL
;
771 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
772 ret
= mark_fd_blocking(fd
);
775 challenge_nr
= random();
776 /* send Welcome message */
777 ret
= send_va_buffer(fd
, "This is para_server, version "
778 PACKAGE_VERSION
".\n" );
781 /* recv auth request line */
782 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
791 if (strncmp(buf
, "auth ", 5))
794 if (numbytes
< 9 || strncmp(buf
, "auth rc4 ", 9))
795 p
= buf
+ 5; /* client version < 0.2.6 */
797 p
= buf
+ 9; /* client version >= 0.2.6 */
800 PARA_DEBUG_LOG("received %s request for user %s\n",
801 use_rc4
? "rc4" : "auth", p
);
806 ret
= para_encrypt_challenge(u
->rsa
, challenge_nr
, crypt_buf
);
810 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes
);
811 /* We can't use send_buffer here since buf may contain null bytes */
812 ret
= send_bin_buffer(fd
,(char *) crypt_buf
, numbytes
);
815 /* recv decrypted number */
816 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
823 if (sscanf(buf
, CHALLENGE_RESPONSE_MSG
"%lu", &chall_response
) < 1
824 || chall_response
!= challenge_nr
)
826 /* auth successful, send 'Proceed' message */
827 PARA_INFO_LOG("good auth for %s (%lu)\n", u
->name
, challenge_nr
);
828 sprintf(buf
, "%s", PROCEED_MSG
);
831 ret
= para_encrypt_buffer(u
->rsa
, rc4_buf
, 2 * RC4_KEY_LEN
,
832 (unsigned char *)buf
+ PROCEED_MSG_LEN
+ 1);
835 numbytes
= ret
+ strlen(PROCEED_MSG
) + 1;
837 numbytes
= strlen(buf
);
838 ret
= send_bin_buffer(fd
, buf
, numbytes
);
842 enable_crypt(fd
, rc4_recv
, rc4_send
, NULL
);
843 ret
= read_command(fd
, &command
);
844 if (ret
== -E_COMMAND_SYNTAX
)
849 cmd
= parse_cmd(command
);
852 /* valid command, check permissions */
853 ret
= check_perms(u
->perms
, cmd
);
856 /* valid command and sufficient perms */
858 argc
= split_args(command
, &argv
, "\n");
859 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd
->name
, u
->name
,
861 ret
= cmd
->handler(fd
, argc
, argv
);
862 mutex_lock(mmd_mutex
);
864 mutex_unlock(mmd_mutex
);
868 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
870 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
874 mutex_lock(mmd_mutex
);
875 if (cmd
&& (cmd
->perms
& AFS_WRITE
) && ret
>= 0)
877 mmd
->active_connections
--;
878 mutex_unlock(mmd_mutex
);
879 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);