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 "%s", /* afs status info */
133 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
134 status_item_list[SI_MTIME], mtime,
135 status_item_list[SI_STATUS], status,
136 status_item_list[SI_STATUS_FLAGS], flags,
138 status_item_list[SI_OFFSET], offset,
139 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
141 status_item_list[SI_STREAM_START],
142 (long unsigned)nmmd->stream_start.tv_sec,
143 (long unsigned)nmmd->stream_start.tv_usec,
144 status_item_list[SI_CURRENT_TIME],
145 (long unsigned)current_time.tv_sec,
146 (long unsigned)current_time.tv_usec,
148 nmmd->afd.verbose_ls_output
157 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
160 /* this has to match sender.h */
161 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
163 scd->sender_num = -1;
165 return -E_COMMAND_SYNTAX;
166 for (i = 0; senders[i].name; i++)
167 if (!strcmp(senders[i].name, argv[1]))
169 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
170 if (!senders[i].name)
171 return -E_COMMAND_SYNTAX;
173 for (i = 0; subcmds[i]; i++)
174 if (!strcmp(subcmds[i], argv[2]))
177 return -E_COMMAND_SYNTAX;
179 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
180 return -E_SENDER_CMD;
181 switch (scd->cmd_num) {
185 return -E_COMMAND_SYNTAX;
189 if (argc != 4 && argc != 5)
190 return -E_COMMAND_SYNTAX;
191 if (!is_valid_ipv4_address(argv[3]))
192 return -E_COMMAND_SYNTAX;
195 scd->netmask = atoi(argv[4]);
196 if (scd->netmask < 0 || scd->netmask > 32)
197 return -E_COMMAND_SYNTAX;
199 strncpy(scd->host, argv[3], sizeof(scd->host));
204 return -E_COMMAND_SYNTAX;
205 return parse_fec_url(argv[3], scd);
207 return -E_COMMAND_SYNTAX;
212 int com_sender(int fd, int argc, char * const * argv)
215 struct sender_command_data scd;
219 for (i = 0; senders[i].name; i++) {
220 char *tmp = make_message("%s%s\n",
221 msg? msg : "", senders[i].name);
225 ret = send_buffer(fd, msg);
229 ret = check_sender_args(argc, argv, &scd);
232 if (scd.sender_num < 0)
234 msg = senders[scd.sender_num].help();
235 ret = send_buffer(fd, msg);
239 for (i = 0; i < 10; i++) {
240 mutex_lock(mmd_mutex);
241 if (mmd->sender_cmd_data.cmd_num >= 0) {
242 mutex_unlock(mmd_mutex);
246 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
247 mutex_unlock(mmd_mutex);
250 return (i < 10)? 1 : -E_LOCK;
254 int com_si(int fd, int argc, __a_unused char * const * argv)
258 char *sender_info = NULL, *sender_list = NULL;
261 return -E_COMMAND_SYNTAX;
262 mutex_lock(mmd_mutex);
263 for (i = 0; senders[i].name; i++) {
264 char *info = senders[i].info();
265 sender_info = para_strcat(sender_info, info);
267 sender_list = para_strcat(sender_list, senders[i].name);
268 sender_list = para_strcat(sender_list, " ");
271 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
274 "connections (active/accepted/total): %u/%u/%u\n"
275 "current loglevel: %s\n"
276 "supported audio formats: %s\n"
277 "supported senders: %s\n"
282 mmd->active_connections,
286 supported_audio_formats(),
290 mutex_unlock(mmd_mutex);
298 int com_version(int fd, int argc, __a_unused char * const * argv)
301 return -E_COMMAND_SYNTAX;
302 return send_buffer(fd, VERSION_TEXT("server")
303 "built: " BUILD_DATE "\n"
304 UNAME_RS ", " CC_VERSION "\n"
309 int com_stat(int fd, int argc, char * const * argv)
311 int ret, num = 0;/* status will be printed that many
312 * times. num <= 0 means: print forever
314 struct misc_meta_data tmp, *nmmd = &tmp;
317 para_sigaction(SIGUSR1, dummy);
324 s = get_status(nmmd);
325 ret = send_buffer(fd, s);
330 if (num > 0 && !--num)
334 return -E_SERVER_CRASH;
340 static int send_list_of_commands(int fd, struct server_command *cmd,
345 for (i = 1; cmd->name; cmd++, i++) {
346 char *perms = cmd_perms_itohuman(cmd->perms);
347 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
358 /* returns string that must be freed by the caller */
359 static struct server_command *get_cmd_ptr(const char *name, char **handler)
361 struct server_command *cmd;
363 for (cmd = server_cmds; cmd->name; cmd++)
364 if (!strcmp(cmd->name, name)) {
366 *handler = para_strdup("server"); /* server commands */
369 /* not found, look for commands supported by afs */
370 for (cmd = afs_cmds; cmd->name; cmd++)
371 if (!strcmp(cmd->name, name)) {
373 *handler = para_strdup("afs");
380 int com_help(int fd, int argc, char * const * argv)
382 struct server_command *cmd;
383 char *perms, *handler;
387 /* no argument given, print list of commands */
388 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
390 return send_list_of_commands(fd, afs_cmds, "afs");
392 /* argument given for help */
393 cmd = get_cmd_ptr(argv[1], &handler);
398 perms = cmd_perms_itohuman(cmd->perms);
399 ret = send_va_buffer(fd,
418 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
421 return -E_COMMAND_SYNTAX;
422 kill(getppid(), SIGHUP);
427 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
430 return -E_COMMAND_SYNTAX;
431 kill(getppid(), SIGTERM);
435 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
438 return -E_COMMAND_SYNTAX;
439 mutex_lock(mmd_mutex);
440 mmd->new_vss_status_flags |= VSS_PLAYING;
441 mmd->new_vss_status_flags &= ~VSS_NOMORE;
442 mutex_unlock(mmd_mutex);
448 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
451 return -E_COMMAND_SYNTAX;
452 mutex_lock(mmd_mutex);
453 mmd->new_vss_status_flags &= ~VSS_PLAYING;
454 mmd->new_vss_status_flags &= ~VSS_REPOS;
455 mmd->new_vss_status_flags |= VSS_NEXT;
456 mutex_unlock(mmd_mutex);
461 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
464 return -E_COMMAND_SYNTAX;
465 mutex_lock(mmd_mutex);
466 if (!vss_paused() && !vss_stopped()) {
468 mmd->new_vss_status_flags &= ~VSS_PLAYING;
469 mmd->new_vss_status_flags &= ~VSS_NEXT;
471 mutex_unlock(mmd_mutex);
476 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
479 return -E_COMMAND_SYNTAX;
480 mutex_lock(mmd_mutex);
482 mmd->new_vss_status_flags |= VSS_NEXT;
483 mutex_unlock(mmd_mutex);
488 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
491 return -E_COMMAND_SYNTAX;
492 mutex_lock(mmd_mutex);
493 if (vss_playing() || vss_paused())
494 mmd->new_vss_status_flags |= VSS_NOMORE;
495 mutex_unlock(mmd_mutex);
500 int com_ff(__a_unused int fd, int argc, char * const * argv)
503 int ret, backwards = 0;
508 return -E_COMMAND_SYNTAX;
509 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
510 return -E_COMMAND_SYNTAX;
511 if (ret > 1 && c == '-')
512 backwards = 1; /* jmp backwards */
513 mutex_lock(mmd_mutex);
514 ret = -E_NO_AUDIO_FILE;
515 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
517 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
519 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
521 promille += 1000 * i / mmd->afd.afhi.seconds_total;
524 if (promille > 1000) {
525 mmd->new_vss_status_flags |= VSS_NEXT;
528 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
529 mmd->new_vss_status_flags |= VSS_REPOS;
530 mmd->new_vss_status_flags &= ~VSS_NEXT;
534 mutex_unlock(mmd_mutex);
539 int com_jmp(__a_unused int fd, int argc, char * const * argv)
545 return -E_COMMAND_SYNTAX;
546 if (sscanf(argv[1], "%lu", &i) <= 0)
547 return -E_COMMAND_SYNTAX;
548 mutex_lock(mmd_mutex);
549 ret = -E_NO_AUDIO_FILE;
550 if (!mmd->afd.afhi.chunks_total)
554 PARA_INFO_LOG("jumping to %lu%%\n", i);
555 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
556 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
557 mmd->chunks_sent, mmd->offset);
558 mmd->new_vss_status_flags |= VSS_REPOS;
559 mmd->new_vss_status_flags &= ~VSS_NEXT;
563 mutex_unlock(mmd_mutex);
568 * check if perms are sufficient to exec a command having perms cmd_perms.
569 * Returns 0 if perms are sufficient, -E_PERM otherwise.
571 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
573 PARA_DEBUG_LOG("checking permissions\n");
574 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
578 * Parse first string from *cmd and lookup in table of valid commands.
579 * On error, NULL is returned.
581 static struct server_command *parse_cmd(const char *cmdstr)
586 sscanf(cmdstr, "%200s%n", buf, &n);
590 return get_cmd_ptr(buf, NULL);
593 static void init_rc4_keys(void)
597 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
598 rc4_buf[i] = para_random(256);
599 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
600 (unsigned char) rc4_buf[0],
601 (unsigned char) rc4_buf[RC4_KEY_LEN]);
602 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
603 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
606 static void rc4_recv(unsigned long len, const unsigned char *indata,
607 unsigned char *outdata, __a_unused void *private_data)
609 RC4(&rc4_recv_key, len, indata, outdata);
612 static void rc4_send(unsigned long len, const unsigned char *indata,
613 unsigned char *outdata, __a_unused void *private_data)
615 RC4(&rc4_send_key, len, indata, outdata);
618 static int read_command(int fd, char **result)
622 char *command = NULL;
628 ret = recv_buffer(fd, buf, sizeof(buf));
634 ret = -E_COMMAND_SYNTAX;
635 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
637 command = para_strcat(command, buf);
638 p = strstr(command, EOC_MSG);
644 ret = command? 1 : -E_COMMAND_SYNTAX;
654 static void reset_signals(void)
656 para_sigaction(SIGCHLD, SIG_IGN);
657 para_sigaction(SIGINT, SIG_DFL);
658 para_sigaction(SIGTERM, SIG_DFL);
659 para_sigaction(SIGHUP, SIG_DFL);
663 * Perform user authentication and execute a command.
665 * \param fd The file descriptor to send output to.
666 * \param peername Identifies the connecting peer.
668 * Whenever para_server accepts an incoming tcp connection on
669 * the port it listens on, it forks and the resulting child
670 * calls this function.
672 * An RSA-based challenge/response is used to authenticate
673 * the peer. It that authentication succeeds, a random RC4
674 * session key is generated and sent back to the peer,
675 * encrypted with its RSA public key. From this point on,
676 * all transfers are crypted with this session key.
678 * Next it is checked if the peer supplied a valid server command or a command
679 * for the audio file selector. If yes, and if the user has sufficient
680 * permissions to execute that command, the function calls the corresponding
681 * command handler which does argument checking and further processing.
683 * In order to cope with a DOS attacks, a timeout is set up
684 * which terminates the function if the connection was not
685 * authenticated when the timeout expires.
687 * \sa alarm(2), rc4(3), crypt.c, crypt.h
689 __noreturn void handle_connect(int fd, const char *peername)
691 int ret, argc, use_rc4 = 0;
693 unsigned char crypt_buf[MAXLINE];
695 struct server_command *cmd = NULL;
696 long unsigned challenge_nr, chall_response;
698 char *p, *command = NULL;
702 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
703 ret = mark_fd_blocking(fd);
706 challenge_nr = random();
707 /* send Welcome message */
708 ret = send_va_buffer(fd, "This is para_server, version "
709 PACKAGE_VERSION ".\n" );
712 /* recv auth request line */
713 ret = recv_buffer(fd, buf, sizeof(buf));
722 if (strncmp(buf, "auth ", 5))
725 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
726 p = buf + 5; /* client version < 0.2.6 */
728 p = buf + 9; /* client version >= 0.2.6 */
731 PARA_DEBUG_LOG("received %s request for user %s\n",
732 use_rc4? "rc4" : "auth", p);
737 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
741 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
742 /* We can't use send_buffer here since buf may contain null bytes */
743 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
746 /* recv decrypted number */
747 ret = recv_buffer(fd, buf, sizeof(buf));
754 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
755 || chall_response != challenge_nr)
757 /* auth successful, send 'Proceed' message */
758 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
759 sprintf(buf, "%s", PROCEED_MSG);
762 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
763 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
766 numbytes = ret + strlen(PROCEED_MSG) + 1;
768 numbytes = strlen(buf);
769 ret = send_bin_buffer(fd, buf, numbytes);
773 enable_crypt(fd, rc4_recv, rc4_send, NULL);
774 ret = read_command(fd, &command);
775 if (ret == -E_COMMAND_SYNTAX)
780 cmd = parse_cmd(command);
783 /* valid command, check permissions */
784 ret = check_perms(u->perms, cmd);
787 /* valid command and sufficient perms */
789 argc = split_args(command, &argv, "\n");
790 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
792 ret = cmd->handler(fd, argc, argv);
793 mutex_lock(mmd_mutex);
795 mutex_unlock(mmd_mutex);
799 send_va_buffer(fd, "%s\n", para_strerror(-ret));
801 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
805 mutex_lock(mmd_mutex);
806 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
808 mmd->active_connections--;
809 mutex_unlock(mmd_mutex);
810 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);