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
42 extern struct misc_meta_data *mmd;
43 extern struct sender senders[];
45 static void dummy(__a_unused int s)
49 static void mmd_dup(struct misc_meta_data *new_mmd)
51 mutex_lock(mmd_mutex);
53 mutex_unlock(mmd_mutex);
57 * Compute human readable string containing vss status for given integer value.
59 * We don't want to use vss_playing() and friends here because we take a
60 * snapshot of the mmd struct and use the copy for computing the state of the
61 * vss. If the real data were used, we would take the mmd lock for a rather
62 * long time or risk to get an inconsistent view.
64 static char *vss_status_tohuman(unsigned int flags)
66 if (flags & VSS_PLAYING)
67 return para_strdup("playing");
69 return para_strdup("stopped");
70 return para_strdup("paused");
74 * return human readable permission string. Never returns NULL.
76 static char *cmd_perms_itohuman(unsigned int perms)
78 char *msg = para_malloc(5 * sizeof(char));
80 msg[0] = perms & AFS_READ? 'a' : '-';
81 msg[1] = perms & AFS_WRITE? 'A' : '-';
82 msg[2] = perms & VSS_READ? 'v' : '-';
83 msg[3] = perms & VSS_WRITE? 'V' : '-';
91 static char *vss_get_status_flags(unsigned int flags)
93 char *msg = para_malloc(5 * sizeof(char));
95 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
96 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
97 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
98 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
103 static char *get_status(struct misc_meta_data *nmmd)
105 char *ret, mtime[30] = "";
106 char *status, *flags; /* vss status info */
107 char *ut = uptime_str();
108 long offset = (nmmd->offset + 500) / 1000;
109 struct timeval current_time;
112 /* report real status */
113 status = vss_status_tohuman(nmmd->vss_status_flags);
114 flags = vss_get_status_flags(nmmd->vss_status_flags);
115 if (nmmd->size) { /* parent currently has an audio file open */
116 localtime_r(&nmmd->mtime, &mtime_tm);
117 strftime(mtime, 29, "%b %d %Y", &mtime_tm);
119 gettimeofday(¤t_time, NULL);
121 "%s: %zu\n" /* file size */
122 "%s: %s\n" /* mtime */
123 "%s: %s\n" /* status */
124 "%s: %s\n" /* status flags */
125 "%s: %li\n" /* offset */
126 "%s: %s\n" /* afs mode */
127 "%s: %lu.%lu\n" /* stream start */
128 "%s: %lu.%lu\n" /* current server time */
129 "%s", /* afs status info */
130 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
131 status_item_list[SI_MTIME], mtime,
132 status_item_list[SI_STATUS], status,
133 status_item_list[SI_STATUS_FLAGS], flags,
135 status_item_list[SI_OFFSET], offset,
136 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
138 status_item_list[SI_STREAM_START],
139 (long unsigned)nmmd->stream_start.tv_sec,
140 (long unsigned)nmmd->stream_start.tv_usec,
141 status_item_list[SI_CURRENT_TIME],
142 (long unsigned)current_time.tv_sec,
143 (long unsigned)current_time.tv_usec,
145 nmmd->afd.verbose_ls_output
154 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
157 /* this has to match sender.h */
158 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
160 scd->sender_num = -1;
162 return -E_COMMAND_SYNTAX;
163 for (i = 0; senders[i].name; i++)
164 if (!strcmp(senders[i].name, argv[1]))
166 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
167 if (!senders[i].name)
168 return -E_COMMAND_SYNTAX;
170 for (i = 0; subcmds[i]; i++)
171 if (!strcmp(subcmds[i], argv[2]))
174 return -E_COMMAND_SYNTAX;
176 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
177 return -E_SENDER_CMD;
178 switch (scd->cmd_num) {
182 return -E_COMMAND_SYNTAX;
186 if (argc != 4 || parse_cidr(argv[3], scd->host,
187 sizeof(scd->host), &scd->netmask) == NULL)
188 return -E_COMMAND_SYNTAX;
193 return -E_COMMAND_SYNTAX;
194 return parse_fec_url(argv[3], scd);
196 return -E_COMMAND_SYNTAX;
201 int com_sender(struct rc4_context *rc4c, int argc, char * const * argv)
204 struct sender_command_data scd;
208 for (i = 0; senders[i].name; i++) {
209 char *tmp = make_message("%s%s\n",
210 msg? msg : "", senders[i].name);
214 ret = rc4_send_buffer(rc4c, msg);
218 ret = check_sender_args(argc, argv, &scd);
221 if (scd.sender_num < 0)
223 msg = senders[scd.sender_num].help();
224 ret = rc4_send_buffer(rc4c, msg);
228 for (i = 0; i < 10; i++) {
229 mutex_lock(mmd_mutex);
230 if (mmd->sender_cmd_data.cmd_num >= 0) {
231 mutex_unlock(mmd_mutex);
235 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
236 mutex_unlock(mmd_mutex);
239 return (i < 10)? 1 : -E_LOCK;
243 int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
247 char *sender_info = NULL, *sender_list = NULL;
250 return -E_COMMAND_SYNTAX;
251 mutex_lock(mmd_mutex);
252 for (i = 0; senders[i].name; i++) {
253 char *info = senders[i].info();
254 sender_info = para_strcat(sender_info, info);
256 sender_list = para_strcat(sender_list, senders[i].name);
257 sender_list = para_strcat(sender_list, " ");
260 ret = rc4_send_va_buffer(rc4c, "up: %s\nplayed: %u\n"
263 "connections (active/accepted/total): %u/%u/%u\n"
264 "current loglevel: %s\n"
265 "supported audio formats: %s\n"
266 "supported senders: %s\n"
271 mmd->active_connections,
275 supported_audio_formats(),
279 mutex_unlock(mmd_mutex);
287 int com_version(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
290 return -E_COMMAND_SYNTAX;
291 return rc4_send_buffer(rc4c, VERSION_TEXT("server")
292 "built: " BUILD_DATE "\n"
293 UNAME_RS ", " CC_VERSION "\n"
298 int com_stat(struct rc4_context *rc4c, int argc, char * const * argv)
300 int ret, num = 0;/* status will be printed that many
301 * times. num <= 0 means: print forever
303 struct misc_meta_data tmp, *nmmd = &tmp;
306 para_sigaction(SIGUSR1, dummy);
313 s = get_status(nmmd);
314 ret = rc4_send_buffer(rc4c, s);
319 if (num > 0 && !--num)
323 return -E_SERVER_CRASH;
329 static int send_list_of_commands(struct rc4_context *rc4c, struct server_command *cmd,
334 for (i = 1; cmd->name; cmd++, i++) {
335 char *perms = cmd_perms_itohuman(cmd->perms);
336 ret = rc4_send_va_buffer(rc4c, "%s\t%s\t%s\t%s\n", cmd->name,
347 /* returns string that must be freed by the caller */
348 static struct server_command *get_cmd_ptr(const char *name, char **handler)
350 struct server_command *cmd;
352 for (cmd = server_cmds; cmd->name; cmd++)
353 if (!strcmp(cmd->name, name)) {
355 *handler = para_strdup("server"); /* server commands */
358 /* not found, look for commands supported by afs */
359 for (cmd = afs_cmds; cmd->name; cmd++)
360 if (!strcmp(cmd->name, name)) {
362 *handler = para_strdup("afs");
369 int com_help(struct rc4_context *rc4c, int argc, char * const * argv)
371 struct server_command *cmd;
372 char *perms, *handler;
376 /* no argument given, print list of commands */
377 if ((ret = send_list_of_commands(rc4c, server_cmds, "server")) < 0)
379 return send_list_of_commands(rc4c, afs_cmds, "afs");
381 /* argument given for help */
382 cmd = get_cmd_ptr(argv[1], &handler);
387 perms = cmd_perms_itohuman(cmd->perms);
388 ret = rc4_send_va_buffer(rc4c,
407 int com_hup(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
410 return -E_COMMAND_SYNTAX;
411 kill(getppid(), SIGHUP);
416 int com_term(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
419 return -E_COMMAND_SYNTAX;
420 kill(getppid(), SIGTERM);
424 int com_play(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
427 return -E_COMMAND_SYNTAX;
428 mutex_lock(mmd_mutex);
429 mmd->new_vss_status_flags |= VSS_PLAYING;
430 mmd->new_vss_status_flags &= ~VSS_NOMORE;
431 mutex_unlock(mmd_mutex);
437 int com_stop(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
440 return -E_COMMAND_SYNTAX;
441 mutex_lock(mmd_mutex);
442 mmd->new_vss_status_flags &= ~VSS_PLAYING;
443 mmd->new_vss_status_flags &= ~VSS_REPOS;
444 mmd->new_vss_status_flags |= VSS_NEXT;
445 mutex_unlock(mmd_mutex);
450 int com_pause(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
453 return -E_COMMAND_SYNTAX;
454 mutex_lock(mmd_mutex);
455 if (!vss_paused() && !vss_stopped()) {
457 mmd->new_vss_status_flags &= ~VSS_PLAYING;
458 mmd->new_vss_status_flags &= ~VSS_NEXT;
460 mutex_unlock(mmd_mutex);
465 int com_next(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
468 return -E_COMMAND_SYNTAX;
469 mutex_lock(mmd_mutex);
471 mmd->new_vss_status_flags |= VSS_NEXT;
472 mutex_unlock(mmd_mutex);
477 int com_nomore(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
480 return -E_COMMAND_SYNTAX;
481 mutex_lock(mmd_mutex);
482 if (vss_playing() || vss_paused())
483 mmd->new_vss_status_flags |= VSS_NOMORE;
484 mutex_unlock(mmd_mutex);
489 int com_ff(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
492 int ret, backwards = 0;
497 return -E_COMMAND_SYNTAX;
498 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
499 return -E_COMMAND_SYNTAX;
500 if (ret > 1 && c == '-')
501 backwards = 1; /* jmp backwards */
502 mutex_lock(mmd_mutex);
503 ret = -E_NO_AUDIO_FILE;
504 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
506 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
508 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
510 promille += 1000 * i / mmd->afd.afhi.seconds_total;
513 if (promille > 1000) {
514 mmd->new_vss_status_flags |= VSS_NEXT;
517 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
518 mmd->new_vss_status_flags |= VSS_REPOS;
519 mmd->new_vss_status_flags &= ~VSS_NEXT;
523 mutex_unlock(mmd_mutex);
528 int com_jmp(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
534 return -E_COMMAND_SYNTAX;
535 if (sscanf(argv[1], "%lu", &i) <= 0)
536 return -E_COMMAND_SYNTAX;
537 mutex_lock(mmd_mutex);
538 ret = -E_NO_AUDIO_FILE;
539 if (!mmd->afd.afhi.chunks_total)
543 PARA_INFO_LOG("jumping to %lu%%\n", i);
544 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
545 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
546 mmd->chunks_sent, mmd->offset);
547 mmd->new_vss_status_flags |= VSS_REPOS;
548 mmd->new_vss_status_flags &= ~VSS_NEXT;
552 mutex_unlock(mmd_mutex);
557 * check if perms are sufficient to exec a command having perms cmd_perms.
558 * Returns 0 if perms are sufficient, -E_PERM otherwise.
560 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
562 PARA_DEBUG_LOG("checking permissions\n");
563 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
567 * Parse first string from *cmd and lookup in table of valid commands.
568 * On error, NULL is returned.
570 static struct server_command *parse_cmd(const char *cmdstr)
575 sscanf(cmdstr, "%200s%n", buf, &n);
579 return get_cmd_ptr(buf, NULL);
582 static int read_command(struct rc4_context *rc4c, char **result)
586 char *command = NULL;
592 ret = rc4_recv_buffer(rc4c, buf, sizeof(buf));
598 ret = -E_COMMAND_SYNTAX;
599 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
601 command = para_strcat(command, buf);
602 p = strstr(command, EOC_MSG);
608 ret = command? 1 : -E_COMMAND_SYNTAX;
618 static void reset_signals(void)
620 para_sigaction(SIGCHLD, SIG_IGN);
621 para_sigaction(SIGINT, SIG_DFL);
622 para_sigaction(SIGTERM, SIG_DFL);
623 para_sigaction(SIGHUP, SIG_DFL);
627 * Perform user authentication and execute a command.
629 * \param fd The file descriptor to send output to.
630 * \param peername Identifies the connecting peer.
632 * Whenever para_server accepts an incoming tcp connection on
633 * the port it listens on, it forks and the resulting child
634 * calls this function.
636 * An RSA-based challenge/response is used to authenticate
637 * the peer. It that authentication succeeds, a random RC4
638 * session key is generated and sent back to the peer,
639 * encrypted with its RSA public key. From this point on,
640 * all transfers are crypted with this session key.
642 * Next it is checked if the peer supplied a valid server command or a command
643 * for the audio file selector. If yes, and if the user has sufficient
644 * permissions to execute that command, the function calls the corresponding
645 * command handler which does argument checking and further processing.
647 * In order to cope with a DOS attacks, a timeout is set up
648 * which terminates the function if the connection was not
649 * authenticated when the timeout expires.
651 * \sa alarm(2), rc4(3), crypt.c, crypt.h
653 __noreturn void handle_connect(int fd, const char *peername)
657 unsigned char rand_buf[CHALLENGE_SIZE + 2 * RC4_KEY_LEN];
658 unsigned char challenge_sha1[HASH_SIZE];
660 struct server_command *cmd = NULL;
662 char *p, *command = NULL;
664 struct rc4_context rc4c = {.fd = fd};
667 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
668 ret = mark_fd_blocking(fd);
671 /* send Welcome message */
672 ret = send_va_buffer(fd, "This is para_server, version "
673 PACKAGE_VERSION ".\n" );
676 /* recv auth request line */
677 ret = recv_buffer(fd, buf, sizeof(buf));
681 ret = -E_AUTH_REQUEST;
685 ret = -E_AUTH_REQUEST;
686 if (strncmp(buf, AUTH_REQUEST_MSG, strlen(AUTH_REQUEST_MSG)))
688 p = buf + strlen(AUTH_REQUEST_MSG);
689 PARA_DEBUG_LOG("received auth request for user %s\n", p);
693 get_random_bytes_or_die(rand_buf, sizeof(rand_buf));
694 ret = para_encrypt_buffer(u->rsa, rand_buf, sizeof(rand_buf),
695 (unsigned char *)buf);
701 * We don't want to reveal our user names, so we send a
702 * challenge to the client even if the user does not exist, and
703 * fail the authentication later.
706 get_random_bytes_or_die((unsigned char *)buf, numbytes);
708 PARA_DEBUG_LOG("sending %zu byte challenge + rc4 keys (%u bytes)\n",
709 CHALLENGE_SIZE, numbytes);
710 ret = send_bin_buffer(fd, buf, numbytes);
713 /* recv challenge response */
714 ret = recv_bin_buffer(fd, buf, HASH_SIZE);
718 PARA_DEBUG_LOG("received %zu bytes challenge response\n", ret);
723 * The correct response is the sha1 of the first CHALLENGE_SIZE bytes
724 * of the random data.
727 if (numbytes != HASH_SIZE)
729 sha1_hash((char *)rand_buf, CHALLENGE_SIZE, challenge_sha1);
730 if (memcmp(challenge_sha1, buf, HASH_SIZE))
732 /* auth successful */
734 PARA_INFO_LOG("good auth for %s\n", u->name);
735 /* init rc4 keys with the second part of the random buffer */
736 RC4_set_key(&rc4c.recv_key, RC4_KEY_LEN, rand_buf + CHALLENGE_SIZE);
737 RC4_set_key(&rc4c.send_key, RC4_KEY_LEN, rand_buf + CHALLENGE_SIZE
739 ret = rc4_send_buffer(&rc4c, PROCEED_MSG);
742 ret = read_command(&rc4c, &command);
743 if (ret == -E_COMMAND_SYNTAX)
748 cmd = parse_cmd(command);
751 /* valid command, check permissions */
752 ret = check_perms(u->perms, cmd);
755 /* valid command and sufficient perms */
756 argc = split_args(command, &argv, "\n");
757 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
759 ret = cmd->handler(&rc4c, argc, argv);
760 mutex_lock(mmd_mutex);
762 mutex_unlock(mmd_mutex);
766 rc4_send_va_buffer(&rc4c, "%s\n", para_strerror(-ret));
768 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
772 mutex_lock(mmd_mutex);
773 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
775 mmd->active_connections--;
776 mutex_unlock(mmd_mutex);
777 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);