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>
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
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 init_rc4_keys(struct rc4_context *rcc)
49 get_random_bytes_or_die(rc4_buf, 2 * RC4_KEY_LEN);
50 RC4_set_key(&rcc->recv_key, RC4_KEY_LEN, rc4_buf);
51 RC4_set_key(&rcc->send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
54 static void dummy(__a_unused int s)
58 static void mmd_dup(struct misc_meta_data *new_mmd)
60 mutex_lock(mmd_mutex);
62 mutex_unlock(mmd_mutex);
66 * Compute human readable string containing vss status for given integer value.
68 * We don't want to use vss_playing() and friends here because we take a
69 * snapshot of the mmd struct and use the copy for computing the state of the
70 * vss. If the real data were used, we would take the mmd lock for a rather
71 * long time or risk to get an inconsistent view.
73 static char *vss_status_tohuman(unsigned int flags)
75 if (flags & VSS_PLAYING)
76 return para_strdup("playing");
78 return para_strdup("stopped");
79 return para_strdup("paused");
83 * return human readable permission string. Never returns NULL.
85 static char *cmd_perms_itohuman(unsigned int perms)
87 char *msg = para_malloc(5 * sizeof(char));
89 msg[0] = perms & AFS_READ? 'a' : '-';
90 msg[1] = perms & AFS_WRITE? 'A' : '-';
91 msg[2] = perms & VSS_READ? 'v' : '-';
92 msg[3] = perms & VSS_WRITE? 'V' : '-';
100 static char *vss_get_status_flags(unsigned int flags)
102 char *msg = para_malloc(5 * sizeof(char));
104 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
105 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
106 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
107 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
112 static char *get_status(struct misc_meta_data *nmmd)
114 char *ret, mtime[30] = "";
115 char *status, *flags; /* vss status info */
116 char *ut = uptime_str();
117 long offset = (nmmd->offset + 500) / 1000;
118 struct timeval current_time;
121 /* report real status */
122 status = vss_status_tohuman(nmmd->vss_status_flags);
123 flags = vss_get_status_flags(nmmd->vss_status_flags);
124 if (nmmd->size) { /* parent currently has an audio file open */
125 localtime_r(&nmmd->mtime, &mtime_tm);
126 strftime(mtime, 29, "%b %d %Y", &mtime_tm);
128 gettimeofday(¤t_time, NULL);
130 "%s: %zu\n" /* file size */
131 "%s: %s\n" /* mtime */
132 "%s: %s\n" /* status */
133 "%s: %s\n" /* status flags */
134 "%s: %li\n" /* offset */
135 "%s: %s\n" /* afs mode */
136 "%s: %lu.%lu\n" /* stream start */
137 "%s: %lu.%lu\n" /* current server time */
138 "%s", /* afs status info */
139 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
140 status_item_list[SI_MTIME], mtime,
141 status_item_list[SI_STATUS], status,
142 status_item_list[SI_STATUS_FLAGS], flags,
144 status_item_list[SI_OFFSET], offset,
145 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
147 status_item_list[SI_STREAM_START],
148 (long unsigned)nmmd->stream_start.tv_sec,
149 (long unsigned)nmmd->stream_start.tv_usec,
150 status_item_list[SI_CURRENT_TIME],
151 (long unsigned)current_time.tv_sec,
152 (long unsigned)current_time.tv_usec,
154 nmmd->afd.verbose_ls_output
163 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
166 /* this has to match sender.h */
167 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
169 scd->sender_num = -1;
171 return -E_COMMAND_SYNTAX;
172 for (i = 0; senders[i].name; i++)
173 if (!strcmp(senders[i].name, argv[1]))
175 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
176 if (!senders[i].name)
177 return -E_COMMAND_SYNTAX;
179 for (i = 0; subcmds[i]; i++)
180 if (!strcmp(subcmds[i], argv[2]))
183 return -E_COMMAND_SYNTAX;
185 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
186 return -E_SENDER_CMD;
187 switch (scd->cmd_num) {
191 return -E_COMMAND_SYNTAX;
195 if (argc != 4 && argc != 5)
196 return -E_COMMAND_SYNTAX;
197 if (!is_valid_ipv4_address(argv[3]))
198 return -E_COMMAND_SYNTAX;
201 scd->netmask = atoi(argv[4]);
202 if (scd->netmask < 0 || scd->netmask > 32)
203 return -E_COMMAND_SYNTAX;
205 strncpy(scd->host, argv[3], sizeof(scd->host));
210 return -E_COMMAND_SYNTAX;
211 return parse_fec_url(argv[3], scd);
213 return -E_COMMAND_SYNTAX;
218 int com_sender(struct rc4_context *rc4c, int argc, char * const * argv)
221 struct sender_command_data scd;
225 for (i = 0; senders[i].name; i++) {
226 char *tmp = make_message("%s%s\n",
227 msg? msg : "", senders[i].name);
231 ret = rc4_send_buffer(rc4c, msg);
235 ret = check_sender_args(argc, argv, &scd);
238 if (scd.sender_num < 0)
240 msg = senders[scd.sender_num].help();
241 ret = rc4_send_buffer(rc4c, msg);
245 for (i = 0; i < 10; i++) {
246 mutex_lock(mmd_mutex);
247 if (mmd->sender_cmd_data.cmd_num >= 0) {
248 mutex_unlock(mmd_mutex);
252 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
253 mutex_unlock(mmd_mutex);
256 return (i < 10)? 1 : -E_LOCK;
260 int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
264 char *sender_info = NULL, *sender_list = NULL;
267 return -E_COMMAND_SYNTAX;
268 mutex_lock(mmd_mutex);
269 for (i = 0; senders[i].name; i++) {
270 char *info = senders[i].info();
271 sender_info = para_strcat(sender_info, info);
273 sender_list = para_strcat(sender_list, senders[i].name);
274 sender_list = para_strcat(sender_list, " ");
277 ret = rc4_send_va_buffer(rc4c, "up: %s\nplayed: %u\n"
280 "connections (active/accepted/total): %u/%u/%u\n"
281 "current loglevel: %s\n"
282 "supported audio formats: %s\n"
283 "supported senders: %s\n"
288 mmd->active_connections,
292 supported_audio_formats(),
296 mutex_unlock(mmd_mutex);
304 int com_version(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
307 return -E_COMMAND_SYNTAX;
308 return rc4_send_buffer(rc4c, VERSION_TEXT("server")
309 "built: " BUILD_DATE "\n"
310 UNAME_RS ", " CC_VERSION "\n"
315 int com_stat(struct rc4_context *rc4c, int argc, char * const * argv)
317 int ret, num = 0;/* status will be printed that many
318 * times. num <= 0 means: print forever
320 struct misc_meta_data tmp, *nmmd = &tmp;
323 para_sigaction(SIGUSR1, dummy);
330 s = get_status(nmmd);
331 ret = rc4_send_buffer(rc4c, s);
336 if (num > 0 && !--num)
340 return -E_SERVER_CRASH;
346 static int send_list_of_commands(struct rc4_context *rc4c, struct server_command *cmd,
351 for (i = 1; cmd->name; cmd++, i++) {
352 char *perms = cmd_perms_itohuman(cmd->perms);
353 ret = rc4_send_va_buffer(rc4c, "%s\t%s\t%s\t%s\n", cmd->name,
364 /* returns string that must be freed by the caller */
365 static struct server_command *get_cmd_ptr(const char *name, char **handler)
367 struct server_command *cmd;
369 for (cmd = server_cmds; cmd->name; cmd++)
370 if (!strcmp(cmd->name, name)) {
372 *handler = para_strdup("server"); /* server commands */
375 /* not found, look for commands supported by afs */
376 for (cmd = afs_cmds; cmd->name; cmd++)
377 if (!strcmp(cmd->name, name)) {
379 *handler = para_strdup("afs");
386 int com_help(struct rc4_context *rc4c, int argc, char * const * argv)
388 struct server_command *cmd;
389 char *perms, *handler;
393 /* no argument given, print list of commands */
394 if ((ret = send_list_of_commands(rc4c, server_cmds, "server")) < 0)
396 return send_list_of_commands(rc4c, afs_cmds, "afs");
398 /* argument given for help */
399 cmd = get_cmd_ptr(argv[1], &handler);
404 perms = cmd_perms_itohuman(cmd->perms);
405 ret = rc4_send_va_buffer(rc4c,
424 int com_hup(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
427 return -E_COMMAND_SYNTAX;
428 kill(getppid(), SIGHUP);
433 int com_term(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
436 return -E_COMMAND_SYNTAX;
437 kill(getppid(), SIGTERM);
441 int com_play(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
444 return -E_COMMAND_SYNTAX;
445 mutex_lock(mmd_mutex);
446 mmd->new_vss_status_flags |= VSS_PLAYING;
447 mmd->new_vss_status_flags &= ~VSS_NOMORE;
448 mutex_unlock(mmd_mutex);
454 int com_stop(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
457 return -E_COMMAND_SYNTAX;
458 mutex_lock(mmd_mutex);
459 mmd->new_vss_status_flags &= ~VSS_PLAYING;
460 mmd->new_vss_status_flags &= ~VSS_REPOS;
461 mmd->new_vss_status_flags |= VSS_NEXT;
462 mutex_unlock(mmd_mutex);
467 int com_pause(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
470 return -E_COMMAND_SYNTAX;
471 mutex_lock(mmd_mutex);
472 if (!vss_paused() && !vss_stopped()) {
474 mmd->new_vss_status_flags &= ~VSS_PLAYING;
475 mmd->new_vss_status_flags &= ~VSS_NEXT;
477 mutex_unlock(mmd_mutex);
482 int com_next(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
485 return -E_COMMAND_SYNTAX;
486 mutex_lock(mmd_mutex);
488 mmd->new_vss_status_flags |= VSS_NEXT;
489 mutex_unlock(mmd_mutex);
494 int com_nomore(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
497 return -E_COMMAND_SYNTAX;
498 mutex_lock(mmd_mutex);
499 if (vss_playing() || vss_paused())
500 mmd->new_vss_status_flags |= VSS_NOMORE;
501 mutex_unlock(mmd_mutex);
506 int com_ff(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
509 int ret, backwards = 0;
514 return -E_COMMAND_SYNTAX;
515 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
516 return -E_COMMAND_SYNTAX;
517 if (ret > 1 && c == '-')
518 backwards = 1; /* jmp backwards */
519 mutex_lock(mmd_mutex);
520 ret = -E_NO_AUDIO_FILE;
521 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
523 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
525 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
527 promille += 1000 * i / mmd->afd.afhi.seconds_total;
530 if (promille > 1000) {
531 mmd->new_vss_status_flags |= VSS_NEXT;
534 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
535 mmd->new_vss_status_flags |= VSS_REPOS;
536 mmd->new_vss_status_flags &= ~VSS_NEXT;
540 mutex_unlock(mmd_mutex);
545 int com_jmp(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
551 return -E_COMMAND_SYNTAX;
552 if (sscanf(argv[1], "%lu", &i) <= 0)
553 return -E_COMMAND_SYNTAX;
554 mutex_lock(mmd_mutex);
555 ret = -E_NO_AUDIO_FILE;
556 if (!mmd->afd.afhi.chunks_total)
560 PARA_INFO_LOG("jumping to %lu%%\n", i);
561 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
562 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
563 mmd->chunks_sent, mmd->offset);
564 mmd->new_vss_status_flags |= VSS_REPOS;
565 mmd->new_vss_status_flags &= ~VSS_NEXT;
569 mutex_unlock(mmd_mutex);
574 * check if perms are sufficient to exec a command having perms cmd_perms.
575 * Returns 0 if perms are sufficient, -E_PERM otherwise.
577 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
579 PARA_DEBUG_LOG("checking permissions\n");
580 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
584 * Parse first string from *cmd and lookup in table of valid commands.
585 * On error, NULL is returned.
587 static struct server_command *parse_cmd(const char *cmdstr)
592 sscanf(cmdstr, "%200s%n", buf, &n);
596 return get_cmd_ptr(buf, NULL);
599 static int read_command(struct rc4_context *rc4c, char **result)
603 char *command = NULL;
609 ret = rc4_recv_buffer(rc4c, buf, sizeof(buf));
615 ret = -E_COMMAND_SYNTAX;
616 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
618 command = para_strcat(command, buf);
619 p = strstr(command, EOC_MSG);
625 ret = command? 1 : -E_COMMAND_SYNTAX;
635 static void reset_signals(void)
637 para_sigaction(SIGCHLD, SIG_IGN);
638 para_sigaction(SIGINT, SIG_DFL);
639 para_sigaction(SIGTERM, SIG_DFL);
640 para_sigaction(SIGHUP, SIG_DFL);
644 * Perform user authentication and execute a command.
646 * \param fd The file descriptor to send output to.
647 * \param peername Identifies the connecting peer.
649 * Whenever para_server accepts an incoming tcp connection on
650 * the port it listens on, it forks and the resulting child
651 * calls this function.
653 * An RSA-based challenge/response is used to authenticate
654 * the peer. It that authentication succeeds, a random RC4
655 * session key is generated and sent back to the peer,
656 * encrypted with its RSA public key. From this point on,
657 * all transfers are crypted with this session key.
659 * Next it is checked if the peer supplied a valid server command or a command
660 * for the audio file selector. If yes, and if the user has sufficient
661 * permissions to execute that command, the function calls the corresponding
662 * command handler which does argument checking and further processing.
664 * In order to cope with a DOS attacks, a timeout is set up
665 * which terminates the function if the connection was not
666 * authenticated when the timeout expires.
668 * \sa alarm(2), rc4(3), crypt.c, crypt.h
670 __noreturn void handle_connect(int fd, const char *peername)
674 unsigned char crypt_buf[MAXLINE];
676 struct server_command *cmd = NULL;
677 long unsigned challenge_nr, chall_response;
679 char *p, *command = NULL;
681 struct rc4_context rc4c = {.fd = fd};
684 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
685 ret = mark_fd_blocking(fd);
688 /* send Welcome message */
689 ret = send_va_buffer(fd, "This is para_server, version "
690 PACKAGE_VERSION ".\n" );
693 /* recv auth request line */
694 ret = recv_buffer(fd, buf, sizeof(buf));
703 if (strncmp(buf, "auth rc4 ", 9))
706 PARA_DEBUG_LOG("received auth request for user %s\n", p);
711 get_random_bytes_or_die((unsigned char *)&challenge_nr,
712 sizeof(challenge_nr));
713 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
717 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
718 /* We can't use send_buffer here since buf may contain null bytes */
719 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
722 /* recv decrypted number */
723 ret = recv_buffer(fd, buf, sizeof(buf));
730 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
731 || chall_response != challenge_nr)
733 /* auth successful, send 'Proceed' message */
734 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
735 sprintf(buf, "%s", PROCEED_MSG);
736 init_rc4_keys(&rc4c);
737 /* Should we also encrypt the proceed message? */
738 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
739 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
742 numbytes = ret + strlen(PROCEED_MSG) + 1;
743 ret = send_bin_buffer(fd, buf, numbytes);
746 ret = read_command(&rc4c, &command);
747 if (ret == -E_COMMAND_SYNTAX)
752 cmd = parse_cmd(command);
755 /* valid command, check permissions */
756 ret = check_perms(u->perms, cmd);
759 /* valid command and sufficient perms */
761 argc = split_args(command, &argv, "\n");
762 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
764 ret = cmd->handler(&rc4c, argc, argv);
765 mutex_lock(mmd_mutex);
767 mutex_unlock(mmd_mutex);
771 rc4_send_va_buffer(&rc4c, "%s\n", para_strerror(-ret));
773 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
777 mutex_lock(mmd_mutex);
778 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
780 mmd->active_connections--;
781 mutex_unlock(mmd_mutex);
782 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);