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>
17 #include "server.cmdline.h"
30 #include "user_list.h"
31 #include "server_command_list.h"
32 #include "afs_command_list.h"
36 /** Commands including options must be shorter than this. */
37 #define MAX_COMMAND_LEN 32768
39 static RC4_KEY rc4_recv_key;
40 static RC4_KEY rc4_send_key;
41 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
44 extern struct misc_meta_data *mmd;
45 extern struct sender senders[];
47 static void dummy(__a_unused int s)
51 static void mmd_dup(struct misc_meta_data *new_mmd)
53 mutex_lock(mmd_mutex);
55 mutex_unlock(mmd_mutex);
59 * Compute human readable string containing vss status for given integer value.
61 * We don't want to use vss_playing() and friends here because we take a
62 * snapshot of the mmd struct and use the copy for computing the state of the
63 * vss. If the real data were used, we would take the mmd lock for a rather
64 * long time or risk to get an inconsistent view.
66 static char *vss_status_tohuman(unsigned int flags)
68 if (flags & VSS_PLAYING)
69 return para_strdup("playing");
71 return para_strdup("stopped");
72 return para_strdup("paused");
76 * return human readable permission string. Never returns NULL.
78 static char *cmd_perms_itohuman(unsigned int perms)
80 char *msg = para_malloc(5 * sizeof(char));
82 msg[0] = perms & AFS_READ? 'a' : '-';
83 msg[1] = perms & AFS_WRITE? 'A' : '-';
84 msg[2] = perms & VSS_READ? 'v' : '-';
85 msg[3] = perms & VSS_WRITE? 'V' : '-';
93 static char *vss_get_status_flags(unsigned int flags)
95 char *msg = para_malloc(5 * sizeof(char));
97 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
98 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
99 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
100 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
105 static char *get_status(struct misc_meta_data *nmmd)
107 char *ret, mtime[30] = "";
108 char *status, *flags; /* vss status info */
109 char *ut = uptime_str();
110 long offset = (nmmd->offset + 500) / 1000;
111 struct timeval current_time;
114 /* report real status */
115 status = vss_status_tohuman(nmmd->vss_status_flags);
116 flags = vss_get_status_flags(nmmd->vss_status_flags);
117 if (nmmd->size) { /* parent currently has an audio file open */
118 localtime_r(&nmmd->mtime, &mtime_tm);
119 strftime(mtime, 29, "%b %d %Y", &mtime_tm);
121 gettimeofday(¤t_time, NULL);
123 "%s: %zu\n" /* file size */
124 "%s: %s\n" /* mtime */
125 "%s: %s\n" /* status */
126 "%s: %s\n" /* status flags */
127 "%s: %li\n" /* offset */
128 "%s: %s\n" /* afs mode */
129 "%s: %lu.%lu\n" /* stream start */
130 "%s: %lu.%lu\n" /* current server time */
131 "%s", /* afs status info */
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,
147 nmmd->afd.verbose_ls_output
156 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
159 /* this has to match sender.h */
160 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
162 scd->sender_num = -1;
164 return -E_COMMAND_SYNTAX;
165 for (i = 0; senders[i].name; i++)
166 if (!strcmp(senders[i].name, argv[1]))
168 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
169 if (!senders[i].name)
170 return -E_COMMAND_SYNTAX;
172 for (i = 0; subcmds[i]; i++)
173 if (!strcmp(subcmds[i], argv[2]))
176 return -E_COMMAND_SYNTAX;
178 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
179 return -E_SENDER_CMD;
180 switch (scd->cmd_num) {
184 return -E_COMMAND_SYNTAX;
188 if (argc != 4 || parse_cidr(argv[3], scd->host,
189 sizeof(scd->host), &scd->netmask) == NULL)
190 return -E_COMMAND_SYNTAX;
195 return -E_COMMAND_SYNTAX;
196 return parse_fec_url(argv[3], scd);
198 return -E_COMMAND_SYNTAX;
203 int com_sender(int fd, int argc, char * const * argv)
206 struct sender_command_data scd;
210 for (i = 0; senders[i].name; i++) {
211 char *tmp = make_message("%s%s\n",
212 msg? msg : "", senders[i].name);
216 ret = send_buffer(fd, msg);
220 ret = check_sender_args(argc, argv, &scd);
223 if (scd.sender_num < 0)
225 msg = senders[scd.sender_num].help();
226 ret = send_buffer(fd, msg);
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(int fd, int argc, __a_unused char * const * argv)
249 char *sender_info = NULL, *sender_list = 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);
258 sender_list = para_strcat(sender_list, senders[i].name);
259 sender_list = para_strcat(sender_list, " ");
262 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
265 "connections (active/accepted/total): %u/%u/%u\n"
266 "current loglevel: %s\n"
267 "supported audio formats: %s\n"
268 "supported senders: %s\n"
273 mmd->active_connections,
277 supported_audio_formats(),
281 mutex_unlock(mmd_mutex);
289 int com_version(int fd, int argc, __a_unused char * const * argv)
292 return -E_COMMAND_SYNTAX;
293 return send_buffer(fd, VERSION_TEXT("server")
294 "built: " BUILD_DATE "\n"
295 UNAME_RS ", " CC_VERSION "\n"
300 int com_stat(int fd, int argc, char * const * argv)
302 int ret, num = 0;/* status will be printed that many
303 * times. num <= 0 means: print forever
305 struct misc_meta_data tmp, *nmmd = &tmp;
308 para_sigaction(SIGUSR1, dummy);
311 return -E_COMMAND_SYNTAX;
313 ret = para_atoi32(argv[1], &num);
320 s = get_status(nmmd);
321 ret = send_buffer(fd, s);
326 if (num > 0 && !--num)
330 return -E_SERVER_CRASH;
336 static int send_list_of_commands(int fd, struct server_command *cmd,
341 for (i = 1; cmd->name; cmd++, i++) {
342 char *perms = cmd_perms_itohuman(cmd->perms);
343 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
354 /* returns string that must be freed by the caller */
355 static struct server_command *get_cmd_ptr(const char *name, char **handler)
357 struct server_command *cmd;
359 for (cmd = server_cmds; cmd->name; cmd++)
360 if (!strcmp(cmd->name, name)) {
362 *handler = para_strdup("server"); /* server commands */
365 /* not found, look for commands supported by afs */
366 for (cmd = afs_cmds; cmd->name; cmd++)
367 if (!strcmp(cmd->name, name)) {
369 *handler = para_strdup("afs");
376 int com_help(int fd, int argc, char * const * argv)
378 struct server_command *cmd;
379 char *perms, *handler;
383 /* no argument given, print list of commands */
384 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
386 return send_list_of_commands(fd, afs_cmds, "afs");
388 /* argument given for help */
389 cmd = get_cmd_ptr(argv[1], &handler);
392 perms = cmd_perms_itohuman(cmd->perms);
393 ret = send_va_buffer(fd,
412 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
415 return -E_COMMAND_SYNTAX;
416 kill(getppid(), SIGHUP);
421 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
424 return -E_COMMAND_SYNTAX;
425 kill(getppid(), SIGTERM);
429 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
432 return -E_COMMAND_SYNTAX;
433 mutex_lock(mmd_mutex);
434 mmd->new_vss_status_flags |= VSS_PLAYING;
435 mmd->new_vss_status_flags &= ~VSS_NOMORE;
436 mutex_unlock(mmd_mutex);
442 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
445 return -E_COMMAND_SYNTAX;
446 mutex_lock(mmd_mutex);
447 mmd->new_vss_status_flags &= ~VSS_PLAYING;
448 mmd->new_vss_status_flags &= ~VSS_REPOS;
449 mmd->new_vss_status_flags |= VSS_NEXT;
450 mutex_unlock(mmd_mutex);
455 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
458 return -E_COMMAND_SYNTAX;
459 mutex_lock(mmd_mutex);
460 if (!vss_paused() && !vss_stopped()) {
462 mmd->new_vss_status_flags &= ~VSS_PLAYING;
463 mmd->new_vss_status_flags &= ~VSS_NEXT;
465 mutex_unlock(mmd_mutex);
470 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
473 return -E_COMMAND_SYNTAX;
474 mutex_lock(mmd_mutex);
476 mmd->new_vss_status_flags |= VSS_NEXT;
477 mutex_unlock(mmd_mutex);
482 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
485 return -E_COMMAND_SYNTAX;
486 mutex_lock(mmd_mutex);
487 if (vss_playing() || vss_paused())
488 mmd->new_vss_status_flags |= VSS_NOMORE;
489 mutex_unlock(mmd_mutex);
494 int com_ff(__a_unused int fd, int argc, char * const * argv)
497 int ret, backwards = 0;
502 return -E_COMMAND_SYNTAX;
503 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
504 return -E_COMMAND_SYNTAX;
505 if (ret > 1 && c == '-')
506 backwards = 1; /* jmp backwards */
507 mutex_lock(mmd_mutex);
508 ret = -E_NO_AUDIO_FILE;
509 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
511 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
513 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
515 promille += 1000 * i / mmd->afd.afhi.seconds_total;
518 if (promille > 1000) {
519 mmd->new_vss_status_flags |= VSS_NEXT;
522 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
523 mmd->new_vss_status_flags |= VSS_REPOS;
524 mmd->new_vss_status_flags &= ~VSS_NEXT;
528 mutex_unlock(mmd_mutex);
533 int com_jmp(__a_unused int fd, int argc, char * const * argv)
539 return -E_COMMAND_SYNTAX;
540 if (sscanf(argv[1], "%lu", &i) <= 0)
541 return -E_COMMAND_SYNTAX;
542 mutex_lock(mmd_mutex);
543 ret = -E_NO_AUDIO_FILE;
544 if (!mmd->afd.afhi.chunks_total)
548 PARA_INFO_LOG("jumping to %lu%%\n", i);
549 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
550 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
551 mmd->chunks_sent, mmd->offset);
552 mmd->new_vss_status_flags |= VSS_REPOS;
553 mmd->new_vss_status_flags &= ~VSS_NEXT;
557 mutex_unlock(mmd_mutex);
562 * check if perms are sufficient to exec a command having perms cmd_perms.
563 * Returns 0 if perms are sufficient, -E_PERM otherwise.
565 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
567 PARA_DEBUG_LOG("checking permissions\n");
568 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
572 * Parse first string from *cmd and lookup in table of valid commands.
573 * On error, NULL is returned.
575 static struct server_command *parse_cmd(const char *cmdstr)
580 sscanf(cmdstr, "%200s%n", buf, &n);
584 return get_cmd_ptr(buf, NULL);
587 static void init_rc4_keys(void)
591 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
592 rc4_buf[i] = para_random(256);
593 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
594 (unsigned char) rc4_buf[0],
595 (unsigned char) rc4_buf[RC4_KEY_LEN]);
596 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
597 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
600 static void rc4_recv(unsigned long len, const unsigned char *indata,
601 unsigned char *outdata, __a_unused void *private_data)
603 RC4(&rc4_recv_key, len, indata, outdata);
606 static void rc4_send(unsigned long len, const unsigned char *indata,
607 unsigned char *outdata, __a_unused void *private_data)
609 RC4(&rc4_send_key, len, indata, outdata);
612 static int read_command(int fd, char **result)
616 char *command = NULL;
622 ret = recv_buffer(fd, buf, sizeof(buf));
628 ret = -E_COMMAND_SYNTAX;
629 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
631 command = para_strcat(command, buf);
632 p = strstr(command, EOC_MSG);
638 ret = command? 1 : -E_COMMAND_SYNTAX;
648 static void reset_signals(void)
650 para_sigaction(SIGCHLD, SIG_IGN);
651 para_sigaction(SIGINT, SIG_DFL);
652 para_sigaction(SIGTERM, SIG_DFL);
653 para_sigaction(SIGHUP, SIG_DFL);
657 * Perform user authentication and execute a command.
659 * \param fd The file descriptor to send output to.
660 * \param peername Identifies the connecting peer.
662 * Whenever para_server accepts an incoming tcp connection on
663 * the port it listens on, it forks and the resulting child
664 * calls this function.
666 * An RSA-based challenge/response is used to authenticate
667 * the peer. It that authentication succeeds, a random RC4
668 * session key is generated and sent back to the peer,
669 * encrypted with its RSA public key. From this point on,
670 * all transfers are crypted with this session key.
672 * Next it is checked if the peer supplied a valid server command or a command
673 * for the audio file selector. If yes, and if the user has sufficient
674 * permissions to execute that command, the function calls the corresponding
675 * command handler which does argument checking and further processing.
677 * In order to cope with a DOS attacks, a timeout is set up
678 * which terminates the function if the connection was not
679 * authenticated when the timeout expires.
681 * \sa alarm(2), rc4(3), crypt.c, crypt.h
683 __noreturn void handle_connect(int fd, const char *peername)
685 int ret, argc, use_rc4 = 0;
687 unsigned char crypt_buf[MAXLINE];
689 struct server_command *cmd = NULL;
690 long unsigned challenge_nr, chall_response;
692 char *p, *command = NULL;
696 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
697 ret = mark_fd_blocking(fd);
700 challenge_nr = random();
701 /* send Welcome message */
702 ret = send_va_buffer(fd, "This is para_server, version "
703 PACKAGE_VERSION ".\n" );
706 /* recv auth request line */
707 ret = recv_buffer(fd, buf, sizeof(buf));
716 if (strncmp(buf, "auth ", 5))
719 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
720 p = buf + 5; /* client version < 0.2.6 */
722 p = buf + 9; /* client version >= 0.2.6 */
725 PARA_DEBUG_LOG("received %s request for user %s\n",
726 use_rc4? "rc4" : "auth", p);
731 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
735 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
736 /* We can't use send_buffer here since buf may contain null bytes */
737 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
740 /* recv decrypted number */
741 ret = recv_buffer(fd, buf, sizeof(buf));
748 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
749 || chall_response != challenge_nr)
751 /* auth successful, send 'Proceed' message */
752 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
753 sprintf(buf, "%s", PROCEED_MSG);
756 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
757 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
760 numbytes = ret + strlen(PROCEED_MSG) + 1;
762 numbytes = strlen(buf);
763 ret = send_bin_buffer(fd, buf, numbytes);
767 enable_crypt(fd, rc4_recv, rc4_send, NULL);
768 ret = read_command(fd, &command);
769 if (ret == -E_COMMAND_SYNTAX)
774 cmd = parse_cmd(command);
777 /* valid command, check permissions */
778 ret = check_perms(u->perms, cmd);
781 /* valid command and sufficient perms */
783 argc = split_args(command, &argv, "\n");
784 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
786 ret = cmd->handler(fd, argc, argv);
787 mutex_lock(mmd_mutex);
789 mutex_unlock(mmd_mutex);
793 send_va_buffer(fd, "%s\n", para_strerror(-ret));
795 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
799 mutex_lock(mmd_mutex);
800 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
802 mmd->active_connections--;
803 mutex_unlock(mmd_mutex);
804 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);