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, int parser_friendly)
109 char *status, *flags; /* vss status info */
110 char *ut = uptime_str();
111 long offset = (nmmd->offset + 500) / 1000;
112 struct timeval current_time;
114 struct para_buffer b = {.flags = parser_friendly? PBF_SIZE_PREFIX : 0};
116 /* report real status */
117 status = vss_status_tohuman(nmmd->vss_status_flags);
118 flags = vss_get_status_flags(nmmd->vss_status_flags);
119 if (nmmd->size) { /* parent currently has an audio file open */
120 localtime_r(&nmmd->mtime, &mtime_tm);
121 strftime(mtime, 29, "%b %d %Y", &mtime_tm);
123 gettimeofday(¤t_time, NULL);
124 WRITE_STATUS_ITEM(&b, SI_FILE_SIZE, "%zu\n", nmmd->size / 1024);
125 WRITE_STATUS_ITEM(&b, SI_MTIME, "%s\n", mtime);
126 WRITE_STATUS_ITEM(&b, SI_STATUS, "%s\n", status);
127 WRITE_STATUS_ITEM(&b, SI_STATUS_FLAGS, "%s\n", flags);
128 WRITE_STATUS_ITEM(&b, SI_OFFSET, "%li\n", offset);
129 WRITE_STATUS_ITEM(&b, SI_AFS_MODE, "%s\n", mmd->afs_mode_string);
130 WRITE_STATUS_ITEM(&b, SI_STREAM_START, "%lu.%lu\n",
131 (long unsigned)nmmd->stream_start.tv_sec,
132 (long unsigned)nmmd->stream_start.tv_usec);
133 WRITE_STATUS_ITEM(&b, SI_CURRENT_TIME, "%lu.%lu\n",
134 (long unsigned)current_time.tv_sec,
135 (long unsigned)current_time.tv_usec);
142 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
145 /* this has to match sender.h */
146 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
148 scd->sender_num = -1;
150 return -E_COMMAND_SYNTAX;
151 for (i = 0; senders[i].name; i++)
152 if (!strcmp(senders[i].name, argv[1]))
154 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
155 if (!senders[i].name)
156 return -E_COMMAND_SYNTAX;
158 for (i = 0; subcmds[i]; i++)
159 if (!strcmp(subcmds[i], argv[2]))
162 return -E_COMMAND_SYNTAX;
164 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
165 return -E_SENDER_CMD;
166 switch (scd->cmd_num) {
170 return -E_COMMAND_SYNTAX;
174 if (argc != 4 && argc != 5)
175 return -E_COMMAND_SYNTAX;
176 if (!is_valid_ipv4_address(argv[3]))
177 return -E_COMMAND_SYNTAX;
180 scd->netmask = atoi(argv[4]);
181 if (scd->netmask < 0 || scd->netmask > 32)
182 return -E_COMMAND_SYNTAX;
184 strncpy(scd->host, argv[3], sizeof(scd->host));
189 return -E_COMMAND_SYNTAX;
190 return parse_fec_url(argv[3], scd);
192 return -E_COMMAND_SYNTAX;
197 int com_sender(int fd, int argc, char * const * argv)
200 struct sender_command_data scd;
204 for (i = 0; senders[i].name; i++) {
205 char *tmp = make_message("%s%s\n",
206 msg? msg : "", senders[i].name);
210 ret = send_buffer(fd, msg);
214 ret = check_sender_args(argc, argv, &scd);
217 if (scd.sender_num < 0)
219 msg = senders[scd.sender_num].help();
220 ret = send_buffer(fd, msg);
224 for (i = 0; i < 10; i++) {
225 mutex_lock(mmd_mutex);
226 if (mmd->sender_cmd_data.cmd_num >= 0) {
227 mutex_unlock(mmd_mutex);
231 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
232 mutex_unlock(mmd_mutex);
235 return (i < 10)? 1 : -E_LOCK;
239 int com_si(int fd, int argc, __a_unused char * const * argv)
243 char *sender_info = NULL, *sender_list = NULL;
246 return -E_COMMAND_SYNTAX;
247 mutex_lock(mmd_mutex);
248 for (i = 0; senders[i].name; i++) {
249 char *info = senders[i].info();
250 sender_info = para_strcat(sender_info, info);
252 sender_list = para_strcat(sender_list, senders[i].name);
253 sender_list = para_strcat(sender_list, " ");
256 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
259 "connections (active/accepted/total): %u/%u/%u\n"
260 "current loglevel: %s\n"
261 "supported audio formats: %s\n"
262 "supported senders: %s\n"
267 mmd->active_connections,
271 supported_audio_formats(),
275 mutex_unlock(mmd_mutex);
283 int com_version(int fd, int argc, __a_unused char * const * argv)
286 return -E_COMMAND_SYNTAX;
287 return send_buffer(fd, VERSION_TEXT("server")
288 "built: " BUILD_DATE "\n"
289 UNAME_RS ", " CC_VERSION "\n"
293 #define EMPTY_STATUS_ITEMS \
298 ITEM(ATTRIBUTES_BITMAP) \
299 ITEM(ATTRIBUTES_TXT) \
310 ITEM(SECONDS_TOTAL) \
322 * Write a list of audio-file related status items with empty values.
324 * This is used by vss when currently no audio file is open.
326 static char *empty_status_items(int parser_friendly)
330 #define ITEM(x) "0004 %02x:\n"
333 #define ITEM(x) , SI_ ## x
338 #define ITEM(x) "%s:\n"
341 #define ITEM(x) ,status_item_list[SI_ ## x]
346 #undef EMPTY_STATUS_ITEMS
349 int com_stat(int fd, int argc, char * const * argv)
352 struct misc_meta_data tmp, *nmmd = &tmp;
355 int parser_friendly = 0;
357 para_sigaction(SIGUSR1, dummy);
359 for (i = 1; i < argc; i++) {
360 const char *arg = argv[i];
363 if (!strcmp(arg, "--")) {
367 if (!strncmp(arg, "-n=", 3)) {
368 ret = para_atoi32(arg + 3, &num);
373 if (!strcmp(arg, "-p")) {
379 return -E_COMMAND_SYNTAX;
382 s = get_status(nmmd, parser_friendly);
383 ret = send_buffer(fd, s);
387 if (nmmd->vss_status_flags & VSS_NEXT) {
390 esi = empty_status_items(parser_friendly);
391 ret = send_buffer(fd, esi);
395 send_afs_status(fd, parser_friendly);
397 if (num > 0 && !--num)
401 return -E_SERVER_CRASH;
407 static int send_list_of_commands(int fd, struct server_command *cmd,
412 for (i = 1; cmd->name; cmd++, i++) {
413 char *perms = cmd_perms_itohuman(cmd->perms);
414 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
425 /* returns string that must be freed by the caller */
426 static struct server_command *get_cmd_ptr(const char *name, char **handler)
428 struct server_command *cmd;
430 for (cmd = server_cmds; cmd->name; cmd++)
431 if (!strcmp(cmd->name, name)) {
433 *handler = para_strdup("server"); /* server commands */
436 /* not found, look for commands supported by afs */
437 for (cmd = afs_cmds; cmd->name; cmd++)
438 if (!strcmp(cmd->name, name)) {
440 *handler = para_strdup("afs");
447 int com_help(int fd, int argc, char * const * argv)
449 struct server_command *cmd;
450 char *perms, *handler;
454 /* no argument given, print list of commands */
455 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
457 return send_list_of_commands(fd, afs_cmds, "afs");
459 /* argument given for help */
460 cmd = get_cmd_ptr(argv[1], &handler);
465 perms = cmd_perms_itohuman(cmd->perms);
466 ret = send_va_buffer(fd,
485 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
488 return -E_COMMAND_SYNTAX;
489 kill(getppid(), SIGHUP);
494 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
497 return -E_COMMAND_SYNTAX;
498 kill(getppid(), SIGTERM);
502 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
505 return -E_COMMAND_SYNTAX;
506 mutex_lock(mmd_mutex);
507 mmd->new_vss_status_flags |= VSS_PLAYING;
508 mmd->new_vss_status_flags &= ~VSS_NOMORE;
509 mutex_unlock(mmd_mutex);
515 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
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(__a_unused int fd, int argc, __a_unused char * const * argv)
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(__a_unused int fd, int argc, __a_unused char * const * argv)
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(__a_unused int fd, int argc, __a_unused char * const * argv)
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(__a_unused int fd, int argc, char * const * argv)
570 int ret, backwards = 0;
575 return -E_COMMAND_SYNTAX;
576 if (!(ret = sscanf(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(__a_unused int fd, int argc, char * const * argv)
612 return -E_COMMAND_SYNTAX;
613 if (sscanf(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 void init_rc4_keys(void)
664 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
665 rc4_buf[i] = para_random(256);
666 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
667 (unsigned char) rc4_buf[0],
668 (unsigned char) rc4_buf[RC4_KEY_LEN]);
669 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
670 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
673 static void rc4_recv(unsigned long len, const unsigned char *indata,
674 unsigned char *outdata, __a_unused void *private_data)
676 RC4(&rc4_recv_key, len, indata, outdata);
679 static void rc4_send(unsigned long len, const unsigned char *indata,
680 unsigned char *outdata, __a_unused void *private_data)
682 RC4(&rc4_send_key, len, indata, outdata);
685 static int read_command(int fd, char **result)
689 char *command = NULL;
695 ret = recv_buffer(fd, buf, sizeof(buf));
701 ret = -E_COMMAND_SYNTAX;
702 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
704 command = para_strcat(command, buf);
705 p = strstr(command, EOC_MSG);
711 ret = command? 1 : -E_COMMAND_SYNTAX;
721 static void reset_signals(void)
723 para_sigaction(SIGCHLD, SIG_IGN);
724 para_sigaction(SIGINT, SIG_DFL);
725 para_sigaction(SIGTERM, SIG_DFL);
726 para_sigaction(SIGHUP, SIG_DFL);
730 * Perform user authentication and execute a command.
732 * \param fd The file descriptor to send output to.
733 * \param peername Identifies the connecting peer.
735 * Whenever para_server accepts an incoming tcp connection on
736 * the port it listens on, it forks and the resulting child
737 * calls this function.
739 * An RSA-based challenge/response is used to authenticate
740 * the peer. It that authentication succeeds, a random RC4
741 * session key is generated and sent back to the peer,
742 * encrypted with its RSA public key. From this point on,
743 * all transfers are crypted with this session key.
745 * Next it is checked if the peer supplied a valid server command or a command
746 * for the audio file selector. If yes, and if the user has sufficient
747 * permissions to execute that command, the function calls the corresponding
748 * command handler which does argument checking and further processing.
750 * In order to cope with a DOS attacks, a timeout is set up
751 * which terminates the function if the connection was not
752 * authenticated when the timeout expires.
754 * \sa alarm(2), rc4(3), crypt.c, crypt.h
756 __noreturn void handle_connect(int fd, const char *peername)
758 int ret, argc, use_rc4 = 0;
760 unsigned char crypt_buf[MAXLINE];
762 struct server_command *cmd = NULL;
763 long unsigned challenge_nr, chall_response;
765 char *p, *command = NULL;
769 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
770 ret = mark_fd_blocking(fd);
773 challenge_nr = random();
774 /* send Welcome message */
775 ret = send_va_buffer(fd, "This is para_server, version "
776 PACKAGE_VERSION ".\n" );
779 /* recv auth request line */
780 ret = recv_buffer(fd, buf, sizeof(buf));
789 if (strncmp(buf, "auth ", 5))
792 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
793 p = buf + 5; /* client version < 0.2.6 */
795 p = buf + 9; /* client version >= 0.2.6 */
798 PARA_DEBUG_LOG("received %s request for user %s\n",
799 use_rc4? "rc4" : "auth", p);
804 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
808 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
809 /* We can't use send_buffer here since buf may contain null bytes */
810 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
813 /* recv decrypted number */
814 ret = recv_buffer(fd, buf, sizeof(buf));
821 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
822 || chall_response != challenge_nr)
824 /* auth successful, send 'Proceed' message */
825 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
826 sprintf(buf, "%s", PROCEED_MSG);
829 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
830 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
833 numbytes = ret + strlen(PROCEED_MSG) + 1;
835 numbytes = strlen(buf);
836 ret = send_bin_buffer(fd, buf, numbytes);
840 enable_crypt(fd, rc4_recv, rc4_send, NULL);
841 ret = read_command(fd, &command);
842 if (ret == -E_COMMAND_SYNTAX)
847 cmd = parse_cmd(command);
850 /* valid command, check permissions */
851 ret = check_perms(u->perms, cmd);
854 /* valid command and sufficient perms */
856 argc = split_args(command, &argv, "\n");
857 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
859 ret = cmd->handler(fd, argc, argv);
860 mutex_lock(mmd_mutex);
862 mutex_unlock(mmd_mutex);
866 send_va_buffer(fd, "%s\n", para_strerror(-ret));
868 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
872 mutex_lock(mmd_mutex);
873 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
875 mmd->active_connections--;
876 mutex_unlock(mmd_mutex);
877 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);