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 && argc != 5)
187 return -E_COMMAND_SYNTAX;
188 if (!is_valid_ipv4_address(argv[3]))
189 return -E_COMMAND_SYNTAX;
192 scd->netmask = atoi(argv[4]);
193 if (scd->netmask < 0 || scd->netmask > 32)
194 return -E_COMMAND_SYNTAX;
196 strncpy(scd->host, argv[3], sizeof(scd->host));
201 return -E_COMMAND_SYNTAX;
202 return parse_fec_url(argv[3], scd);
204 return -E_COMMAND_SYNTAX;
209 int com_sender(struct rc4_context *rc4c, int argc, char * const * argv)
212 struct sender_command_data scd;
216 for (i = 0; senders[i].name; i++) {
217 char *tmp = make_message("%s%s\n",
218 msg? msg : "", senders[i].name);
222 ret = rc4_send_buffer(rc4c, msg);
226 ret = check_sender_args(argc, argv, &scd);
229 if (scd.sender_num < 0)
231 msg = senders[scd.sender_num].help();
232 ret = rc4_send_buffer(rc4c, msg);
236 for (i = 0; i < 10; i++) {
237 mutex_lock(mmd_mutex);
238 if (mmd->sender_cmd_data.cmd_num >= 0) {
239 mutex_unlock(mmd_mutex);
243 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
244 mutex_unlock(mmd_mutex);
247 return (i < 10)? 1 : -E_LOCK;
251 int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
255 char *sender_info = NULL, *sender_list = NULL;
258 return -E_COMMAND_SYNTAX;
259 mutex_lock(mmd_mutex);
260 for (i = 0; senders[i].name; i++) {
261 char *info = senders[i].info();
262 sender_info = para_strcat(sender_info, info);
264 sender_list = para_strcat(sender_list, senders[i].name);
265 sender_list = para_strcat(sender_list, " ");
268 ret = rc4_send_va_buffer(rc4c, "up: %s\nplayed: %u\n"
271 "connections (active/accepted/total): %u/%u/%u\n"
272 "current loglevel: %s\n"
273 "supported audio formats: %s\n"
274 "supported senders: %s\n"
279 mmd->active_connections,
283 supported_audio_formats(),
287 mutex_unlock(mmd_mutex);
295 int com_version(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
298 return -E_COMMAND_SYNTAX;
299 return rc4_send_buffer(rc4c, VERSION_TEXT("server")
300 "built: " BUILD_DATE "\n"
301 UNAME_RS ", " CC_VERSION "\n"
306 int com_stat(struct rc4_context *rc4c, int argc, char * const * argv)
308 int ret, num = 0;/* status will be printed that many
309 * times. num <= 0 means: print forever
311 struct misc_meta_data tmp, *nmmd = &tmp;
314 para_sigaction(SIGUSR1, dummy);
321 s = get_status(nmmd);
322 ret = rc4_send_buffer(rc4c, s);
327 if (num > 0 && !--num)
331 return -E_SERVER_CRASH;
337 static int send_list_of_commands(struct rc4_context *rc4c, struct server_command *cmd,
342 for (i = 1; cmd->name; cmd++, i++) {
343 char *perms = cmd_perms_itohuman(cmd->perms);
344 ret = rc4_send_va_buffer(rc4c, "%s\t%s\t%s\t%s\n", cmd->name,
355 /* returns string that must be freed by the caller */
356 static struct server_command *get_cmd_ptr(const char *name, char **handler)
358 struct server_command *cmd;
360 for (cmd = server_cmds; cmd->name; cmd++)
361 if (!strcmp(cmd->name, name)) {
363 *handler = para_strdup("server"); /* server commands */
366 /* not found, look for commands supported by afs */
367 for (cmd = afs_cmds; cmd->name; cmd++)
368 if (!strcmp(cmd->name, name)) {
370 *handler = para_strdup("afs");
377 int com_help(struct rc4_context *rc4c, int argc, char * const * argv)
379 struct server_command *cmd;
380 char *perms, *handler;
384 /* no argument given, print list of commands */
385 if ((ret = send_list_of_commands(rc4c, server_cmds, "server")) < 0)
387 return send_list_of_commands(rc4c, afs_cmds, "afs");
389 /* argument given for help */
390 cmd = get_cmd_ptr(argv[1], &handler);
395 perms = cmd_perms_itohuman(cmd->perms);
396 ret = rc4_send_va_buffer(rc4c,
415 int com_hup(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
418 return -E_COMMAND_SYNTAX;
419 kill(getppid(), SIGHUP);
424 int com_term(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
427 return -E_COMMAND_SYNTAX;
428 kill(getppid(), SIGTERM);
432 int com_play(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
435 return -E_COMMAND_SYNTAX;
436 mutex_lock(mmd_mutex);
437 mmd->new_vss_status_flags |= VSS_PLAYING;
438 mmd->new_vss_status_flags &= ~VSS_NOMORE;
439 mutex_unlock(mmd_mutex);
445 int com_stop(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
448 return -E_COMMAND_SYNTAX;
449 mutex_lock(mmd_mutex);
450 mmd->new_vss_status_flags &= ~VSS_PLAYING;
451 mmd->new_vss_status_flags &= ~VSS_REPOS;
452 mmd->new_vss_status_flags |= VSS_NEXT;
453 mutex_unlock(mmd_mutex);
458 int com_pause(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
461 return -E_COMMAND_SYNTAX;
462 mutex_lock(mmd_mutex);
463 if (!vss_paused() && !vss_stopped()) {
465 mmd->new_vss_status_flags &= ~VSS_PLAYING;
466 mmd->new_vss_status_flags &= ~VSS_NEXT;
468 mutex_unlock(mmd_mutex);
473 int com_next(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
476 return -E_COMMAND_SYNTAX;
477 mutex_lock(mmd_mutex);
479 mmd->new_vss_status_flags |= VSS_NEXT;
480 mutex_unlock(mmd_mutex);
485 int com_nomore(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
488 return -E_COMMAND_SYNTAX;
489 mutex_lock(mmd_mutex);
490 if (vss_playing() || vss_paused())
491 mmd->new_vss_status_flags |= VSS_NOMORE;
492 mutex_unlock(mmd_mutex);
497 int com_ff(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
500 int ret, backwards = 0;
505 return -E_COMMAND_SYNTAX;
506 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
507 return -E_COMMAND_SYNTAX;
508 if (ret > 1 && c == '-')
509 backwards = 1; /* jmp backwards */
510 mutex_lock(mmd_mutex);
511 ret = -E_NO_AUDIO_FILE;
512 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
514 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
516 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
518 promille += 1000 * i / mmd->afd.afhi.seconds_total;
521 if (promille > 1000) {
522 mmd->new_vss_status_flags |= VSS_NEXT;
525 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
526 mmd->new_vss_status_flags |= VSS_REPOS;
527 mmd->new_vss_status_flags &= ~VSS_NEXT;
531 mutex_unlock(mmd_mutex);
536 int com_jmp(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
542 return -E_COMMAND_SYNTAX;
543 if (sscanf(argv[1], "%lu", &i) <= 0)
544 return -E_COMMAND_SYNTAX;
545 mutex_lock(mmd_mutex);
546 ret = -E_NO_AUDIO_FILE;
547 if (!mmd->afd.afhi.chunks_total)
551 PARA_INFO_LOG("jumping to %lu%%\n", i);
552 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
553 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
554 mmd->chunks_sent, mmd->offset);
555 mmd->new_vss_status_flags |= VSS_REPOS;
556 mmd->new_vss_status_flags &= ~VSS_NEXT;
560 mutex_unlock(mmd_mutex);
565 * check if perms are sufficient to exec a command having perms cmd_perms.
566 * Returns 0 if perms are sufficient, -E_PERM otherwise.
568 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
570 PARA_DEBUG_LOG("checking permissions\n");
571 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
575 * Parse first string from *cmd and lookup in table of valid commands.
576 * On error, NULL is returned.
578 static struct server_command *parse_cmd(const char *cmdstr)
583 sscanf(cmdstr, "%200s%n", buf, &n);
587 return get_cmd_ptr(buf, NULL);
590 static int read_command(struct rc4_context *rc4c, char **result)
594 char *command = NULL;
600 ret = rc4_recv_buffer(rc4c, buf, sizeof(buf));
606 ret = -E_COMMAND_SYNTAX;
607 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
609 command = para_strcat(command, buf);
610 p = strstr(command, EOC_MSG);
616 ret = command? 1 : -E_COMMAND_SYNTAX;
626 static void reset_signals(void)
628 para_sigaction(SIGCHLD, SIG_IGN);
629 para_sigaction(SIGINT, SIG_DFL);
630 para_sigaction(SIGTERM, SIG_DFL);
631 para_sigaction(SIGHUP, SIG_DFL);
635 * Perform user authentication and execute a command.
637 * \param fd The file descriptor to send output to.
638 * \param peername Identifies the connecting peer.
640 * Whenever para_server accepts an incoming tcp connection on
641 * the port it listens on, it forks and the resulting child
642 * calls this function.
644 * An RSA-based challenge/response is used to authenticate
645 * the peer. It that authentication succeeds, a random RC4
646 * session key is generated and sent back to the peer,
647 * encrypted with its RSA public key. From this point on,
648 * all transfers are crypted with this session key.
650 * Next it is checked if the peer supplied a valid server command or a command
651 * for the audio file selector. If yes, and if the user has sufficient
652 * permissions to execute that command, the function calls the corresponding
653 * command handler which does argument checking and further processing.
655 * In order to cope with a DOS attacks, a timeout is set up
656 * which terminates the function if the connection was not
657 * authenticated when the timeout expires.
659 * \sa alarm(2), rc4(3), crypt.c, crypt.h
661 __noreturn void handle_connect(int fd, const char *peername)
665 unsigned char rand_buf[CHALLENGE_SIZE + 2 * RC4_KEY_LEN];
666 unsigned char challenge_sha1[HASH_SIZE];
668 struct server_command *cmd = NULL;
670 char *p, *command = NULL;
672 struct rc4_context rc4c = {.fd = fd};
675 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
676 ret = mark_fd_blocking(fd);
679 /* send Welcome message */
680 ret = send_va_buffer(fd, "This is para_server, version "
681 PACKAGE_VERSION ".\n" );
684 /* recv auth request line */
685 ret = recv_buffer(fd, buf, sizeof(buf));
689 ret = -E_AUTH_REQUEST;
693 ret = -E_AUTH_REQUEST;
694 if (strncmp(buf, AUTH_REQUEST_MSG, strlen(AUTH_REQUEST_MSG)))
696 p = buf + strlen(AUTH_REQUEST_MSG);
697 PARA_DEBUG_LOG("received auth request for user %s\n", p);
701 get_random_bytes_or_die(rand_buf, sizeof(rand_buf));
702 ret = para_encrypt_buffer(u->rsa, rand_buf, sizeof(rand_buf),
703 (unsigned char *)buf);
709 * We don't want to reveal our user names, so we send a
710 * challenge to the client even if the user does not exist, and
711 * fail the authentication later.
714 get_random_bytes_or_die((unsigned char *)buf, numbytes);
716 PARA_DEBUG_LOG("sending %zu byte challenge + rc4 keys (%u bytes)\n",
717 CHALLENGE_SIZE, numbytes);
718 ret = send_bin_buffer(fd, buf, numbytes);
721 /* recv challenge response */
722 ret = recv_bin_buffer(fd, buf, HASH_SIZE);
726 PARA_DEBUG_LOG("received %zu bytes challenge response\n", ret);
731 * The correct response is the sha1 of the first CHALLENGE_SIZE bytes
732 * of the random data.
735 if (numbytes != HASH_SIZE)
737 sha1_hash((char *)rand_buf, CHALLENGE_SIZE, challenge_sha1);
738 if (memcmp(challenge_sha1, buf, HASH_SIZE))
740 /* auth successful */
742 PARA_INFO_LOG("good auth for %s\n", u->name);
743 /* init rc4 keys with the second part of the random buffer */
744 RC4_set_key(&rc4c.recv_key, RC4_KEY_LEN, rand_buf + CHALLENGE_SIZE);
745 RC4_set_key(&rc4c.send_key, RC4_KEY_LEN, rand_buf + CHALLENGE_SIZE
747 ret = rc4_send_buffer(&rc4c, PROCEED_MSG);
750 ret = read_command(&rc4c, &command);
751 if (ret == -E_COMMAND_SYNTAX)
756 cmd = parse_cmd(command);
759 /* valid command, check permissions */
760 ret = check_perms(u->perms, cmd);
763 /* valid command and sufficient perms */
764 argc = split_args(command, &argv, "\n");
765 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
767 ret = cmd->handler(&rc4c, argc, argv);
768 mutex_lock(mmd_mutex);
770 mutex_unlock(mmd_mutex);
774 rc4_send_va_buffer(&rc4c, "%s\n", para_strerror(-ret));
776 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
780 mutex_lock(mmd_mutex);
781 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
783 mmd->active_connections--;
784 mutex_unlock(mmd_mutex);
785 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);