2 * Copyright (C) 1997-2007 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"
29 #include "user_list.h"
30 #include "server_command_list.h"
31 #include "afs_command_list.h"
33 /** Commands including options must be shorter than this. */
34 #define MAX_COMMAND_LEN 32768
36 static RC4_KEY rc4_recv_key;
37 static RC4_KEY rc4_send_key;
38 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
40 extern const char *status_item_list[NUM_STAT_ITEMS];
41 extern struct misc_meta_data *mmd;
42 extern struct sender senders[];
44 static void dummy(__a_unused int s)
47 static void mmd_dup(struct misc_meta_data *new_mmd)
55 * compute human readable string containing
56 * vss status for given integer value
58 static char *vss_status_tohuman(unsigned int flags)
60 if (flags & VSS_PLAYING)
61 return para_strdup("playing");
62 else if (flags & VSS_NEXT)
63 return para_strdup("stopped");
65 return para_strdup("paused");
69 * return human readable permission string. Never returns NULL.
71 static char *cmd_perms_itohuman(unsigned int perms)
73 char *msg = para_malloc(5 * sizeof(char));
75 msg[0] = perms & AFS_READ? 'a' : '-';
76 msg[1] = perms & AFS_WRITE? 'A' : '-';
77 msg[2] = perms & VSS_READ? 'v' : '-';
78 msg[3] = perms & VSS_WRITE? 'V' : '-';
86 static char *vss_get_status_flags(unsigned int flags)
88 char *msg = para_malloc(5 * sizeof(char));
90 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
91 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
92 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
93 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
98 static char *get_status(struct misc_meta_data *nmmd)
100 char *bar, *ret, mtime[30] = "";
101 char *status, *flags; /* vss status info */
102 char *ut = uptime_str();
103 long offset = (nmmd->offset + 500) / 1000;
107 localtime_r(&nmmd->mtime, &mtime_tm);
108 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
109 /* report real status */
110 status = vss_status_tohuman(nmmd->vss_status_flags);
111 flags = vss_get_status_flags(nmmd->vss_status_flags);
112 bar = para_basename(nmmd->filename);
113 gettimeofday(&now, NULL);
115 "%s:%zu\n" "%s:%s\n" "%s:%lu\n" "%s:%u\n"
116 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%s\n"
117 "%s:%li\n" "%s:%s\n" "%s:%s\n" "%s"
118 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n",
119 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
120 status_item_list[SI_MTIME], mtime,
121 status_item_list[SI_LENGTH], nmmd->afd.afhi.seconds_total,
122 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
124 status_item_list[SI_STATUS_BAR], bar ? bar : "(none)",
125 status_item_list[SI_STATUS], status,
126 status_item_list[SI_STATUS_FLAGS], flags,
127 status_item_list[SI_SELECTOR], "FIXME",
129 status_item_list[SI_OFFSET], offset,
130 status_item_list[SI_FORMAT], audio_format_name(nmmd->afd.afsi.audio_format_id),
131 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
132 nmmd->afd.afhi.info_string,
134 status_item_list[SI_UPTIME], ut,
135 status_item_list[SI_STREAM_START],
136 (long unsigned)nmmd->stream_start.tv_sec,
137 (long unsigned)nmmd->stream_start.tv_usec,
138 status_item_list[SI_CURRENT_TIME],
139 (long unsigned)now.tv_sec,
140 (long unsigned)now.tv_usec
150 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
153 /* this has to match sender.h */
154 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
156 scd->sender_num = -1;
158 return -E_COMMAND_SYNTAX;
159 for (i = 0; senders[i].name; i++)
160 if (!strcmp(senders[i].name, argv[1]))
162 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
163 if (!senders[i].name)
164 return -E_COMMAND_SYNTAX;
166 for (i = 0; subcmds[i]; i++)
167 if (!strcmp(subcmds[i], argv[2]))
170 return -E_COMMAND_SYNTAX;
173 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
175 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 (!inet_pton(AF_INET, argv[3], &scd->addr))
189 return -E_COMMAND_SYNTAX;
192 scd->netmask = atoi(argv[4]);
193 if (scd->netmask < 0 || scd->netmask > 32)
194 return -E_COMMAND_SYNTAX;
199 if (argc != 4 && argc != 5)
200 return -E_COMMAND_SYNTAX;
201 if (!inet_pton(AF_INET, argv[3], &scd->addr))
202 return -E_COMMAND_SYNTAX;
205 scd->port = atoi(argv[4]);
206 if (scd->port < 0 || scd->port > 65535)
207 return -E_COMMAND_SYNTAX;
211 return -E_COMMAND_SYNTAX;
216 int com_sender(int fd, int argc, char * const * argv)
219 struct sender_command_data scd;
223 for (i = 0; senders[i].name; i++) {
224 char *tmp = make_message("%s%s\n",
225 msg? msg : "", senders[i].name);
229 ret = send_buffer(fd, msg);
233 ret = check_sender_args(argc, argv, &scd);
236 if (scd.sender_num < 0)
238 msg = senders[scd.sender_num].help();
239 send_buffer(fd, msg);
243 for (i = 0; i < 10; i++) {
245 if (mmd->sender_cmd_data.cmd_num >= 0) {
250 mmd->sender_cmd_data = scd;
254 return (i < 10)? 1 : -E_LOCK;
258 int com_si(int fd, int argc, __a_unused char * const * argv)
262 char *sender_info = NULL, *sender_list = NULL;
265 return -E_COMMAND_SYNTAX;
267 for (i = 0; senders[i].name; i++) {
268 char *info = senders[i].info();
269 sender_info = para_strcat(sender_info, info);
271 sender_list = para_strcat(sender_list, senders[i].name);
272 sender_list = para_strcat(sender_list, " ");
275 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
277 "connections (active/accepted/total): %u/%u/%u\n"
278 "current loglevel: %i\n"
279 "supported audio formats: %s\n"
280 "supported senders: %s\n"
284 mmd->active_connections,
288 supported_audio_formats(),
300 int com_version(int fd, int argc, __a_unused char * const * argv)
303 return -E_COMMAND_SYNTAX;
304 return send_buffer(fd, VERSION_TEXT("server")
305 "built: " BUILD_DATE "\n"
306 SYSTEM ", " CC_VERSION "\n"
311 int com_stat(int fd, int argc, char * const * argv)
313 int ret, num = 0;/* status will be printed that many
314 * times. num <= 0 means: print forever
316 struct misc_meta_data tmp, *nmmd = &tmp;
319 signal(SIGUSR1, dummy);
326 s = get_status(nmmd);
327 ret = send_buffer(fd, s);
336 return -E_SERVER_CRASH;
342 static int send_list_of_commands(int fd, struct server_command *cmd,
347 for (i = 1; cmd->name; cmd++, i++) {
348 char *perms = cmd_perms_itohuman(cmd->perms);
349 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
360 /* returns string that must be freed by the caller */
361 static struct server_command *get_cmd_ptr(const char *name, char **handler)
363 struct server_command *cmd;
365 for (cmd = server_cmds; cmd->name; cmd++)
366 if (!strcmp(cmd->name, name)) {
368 *handler = para_strdup("para_server"); /* server commands */
371 /* not found, look for commands supported by afs */
372 for (cmd = afs_cmds; cmd->name; cmd++)
373 if (!strcmp(cmd->name, name))
379 int com_help(int fd, int argc, char * const * argv)
381 struct server_command *cmd;
382 char *perms, *handler;
386 /* no argument given, print list of commands */
387 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
389 return send_list_of_commands(fd, afs_cmds, "afs");
391 /* argument given for help */
392 cmd = get_cmd_ptr(argv[1], &handler);
397 perms = cmd_perms_itohuman(cmd->perms);
398 ret = send_va_buffer(fd,
417 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
420 return -E_COMMAND_SYNTAX;
421 kill(getppid(), SIGHUP);
426 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
429 return -E_COMMAND_SYNTAX;
430 kill(getppid(), SIGTERM);
434 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
437 return -E_COMMAND_SYNTAX;
439 mmd->new_vss_status_flags |= VSS_PLAYING;
440 mmd->new_vss_status_flags &= ~VSS_NOMORE;
447 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
450 return -E_COMMAND_SYNTAX;
452 mmd->new_vss_status_flags &= ~VSS_PLAYING;
453 mmd->new_vss_status_flags &= ~VSS_REPOS;
454 mmd->new_vss_status_flags |= VSS_NEXT;
460 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
463 return -E_COMMAND_SYNTAX;
467 mmd->new_vss_status_flags &= ~VSS_PLAYING;
468 mmd->new_vss_status_flags &= ~VSS_NEXT;
474 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
477 return -E_COMMAND_SYNTAX;
480 mmd->new_vss_status_flags |= VSS_NEXT;
486 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
489 return -E_COMMAND_SYNTAX;
491 if (vss_playing() || vss_paused())
492 mmd->new_vss_status_flags |= VSS_NOMORE;
498 int com_ff(__a_unused int fd, int argc, char * const * argv)
501 int ret, backwards = 0;
506 return -E_COMMAND_SYNTAX;
507 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
508 return -E_COMMAND_SYNTAX;
509 if (ret > 1 && c == '-')
510 backwards = 1; /* jmp backwards */
512 ret = -E_NO_AUDIO_FILE;
513 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
515 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
517 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
519 promille += 1000 * i / mmd->afd.afhi.seconds_total;
522 if (promille > 1000) {
523 mmd->new_vss_status_flags |= VSS_NEXT;
526 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
527 mmd->new_vss_status_flags |= VSS_REPOS;
528 mmd->new_vss_status_flags &= ~VSS_NEXT;
537 int com_jmp(__a_unused int fd, int argc, char * const * argv)
543 return -E_COMMAND_SYNTAX;
544 if (sscanf(argv[1], "%lu", &i) <= 0)
545 return -E_COMMAND_SYNTAX;
547 ret = -E_NO_AUDIO_FILE;
548 if (!mmd->afd.afhi.chunks_total)
552 PARA_INFO_LOG("jumping to %lu%%\n", i);
553 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
554 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
555 mmd->chunks_sent, mmd->offset);
556 mmd->new_vss_status_flags |= VSS_REPOS;
557 mmd->new_vss_status_flags &= ~VSS_NEXT;
566 * check if perms are sufficient to exec a command having perms cmd_perms.
567 * Returns 0 if perms are sufficient, -E_PERM otherwise.
569 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
571 PARA_DEBUG_LOG("%s", "checking permissions\n");
572 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
576 * Parse first string from *cmd and lookup in table of valid commands.
577 * On error, NULL is returned.
579 static struct server_command *parse_cmd(const char *cmdstr)
584 sscanf(cmdstr, "%200s%n", buf, &n);
588 return get_cmd_ptr(buf, NULL);
591 static long int para_rand(long unsigned max)
593 return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
596 static void init_rc4_keys(void)
600 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
601 rc4_buf[i] = para_rand(256);
602 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
603 (unsigned char) rc4_buf[0],
604 (unsigned char) rc4_buf[RC4_KEY_LEN]);
605 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
606 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
609 static void rc4_recv(unsigned long len, const unsigned char *indata,
610 unsigned char *outdata, __a_unused void *private_data)
612 RC4(&rc4_recv_key, len, indata, outdata);
615 static void rc4_send(unsigned long len, const unsigned char *indata,
616 unsigned char *outdata, __a_unused void *private_data)
618 RC4(&rc4_send_key, len, indata, outdata);
621 static int read_command(int fd, char **result)
625 char *command = NULL;
631 ret = recv_buffer(fd, buf, sizeof(buf));
637 ret = -E_COMMAND_SYNTAX;
638 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
640 command = para_strcat(command, buf);
641 p = strstr(command, EOC_MSG);
647 ret = command? 1 : -E_COMMAND_SYNTAX;
658 * perform user authentication and execute a command
660 * \param fd the file descriptor to send output to
661 * \param addr socket address info of peer
663 * \return EXIT_SUCCESS or EXIT_FAILURE
665 * Whenever para_server accepts an incoming tcp connection on
666 * the port it listens on, it forks and the resulting child
667 * calls this function.
669 * An RSA-based challenge/response is used to authenticate
670 * the peer. It that authentication succeeds, a random RC4
671 * session key is generated and sent back to the peer,
672 * encrypted with its RSA public key. From this point on,
673 * all transfers are crypted with this session key.
675 * Next it is checked if the peer supplied a valid server command or a command
676 * for the audio file selector. If yes, and if the user has sufficient
677 * permissions to execute that command, the function calls the corresponding
678 * command handler which does argument checking and further processing.
680 * In order to cope with a DOS attacks, a timeout is set up
681 * which terminates the function if the connection was not
682 * authenticated when the timeout expires.
684 * \sa alarm(2), rc4(3), crypt.c, crypt.h
686 int handle_connect(int fd, struct sockaddr_in *addr)
688 int ret, argc, use_rc4 = 0;
690 unsigned char crypt_buf[MAXLINE];
692 struct server_command *cmd = NULL;
693 long unsigned challenge_nr, chall_response;
695 char *p, *command = NULL;
698 signal(SIGCHLD, SIG_IGN);
699 signal(SIGINT, SIG_DFL);
700 signal(SIGTERM, SIG_DFL);
701 signal(SIGHUP, SIG_DFL);
702 signal(SIGUSR1, SIG_IGN);
704 challenge_nr = random();
705 /* send Welcome message */
706 ret = send_va_buffer(fd, "This is para_server, version "
707 PACKAGE_VERSION ".\n" );
710 /* recv auth request line */
711 ret = recv_buffer(fd, buf, sizeof(buf));
720 if (strncmp(buf, "auth ", 5))
723 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
724 p = buf + 5; /* client version < 0.2.6 */
726 p = buf + 9; /* client version >= 0.2.6 */
729 PARA_DEBUG_LOG("received %s request for user %s\n",
730 use_rc4? "rc4" : "auth", p);
735 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
739 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
740 /* We can't use send_buffer here since buf may contain null bytes */
741 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
744 /* recv decrypted number */
745 ret = recv_buffer(fd, buf, sizeof(buf));
752 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
753 || chall_response != challenge_nr)
755 /* auth successful, send 'Proceed' message */
756 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
757 sprintf(buf, "%s", PROCEED_MSG);
760 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
761 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
764 numbytes = ret + strlen(PROCEED_MSG) + 1;
766 numbytes = strlen(buf);
767 ret = send_bin_buffer(fd, buf, numbytes);
771 enable_crypt(fd, rc4_recv, rc4_send, NULL);
772 ret = read_command(fd, &command);
776 cmd = parse_cmd(command);
779 /* valid command, check permissions */
780 ret = check_perms(u->perms, cmd);
783 /* valid command and sufficient perms */
785 argc = split_args(command, &argv, "\n");
789 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
790 inet_ntoa(addr->sin_addr));
791 ret = cmd->handler(fd, argc, argv);
797 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
798 if (ret != -E_SEND && ret != -E_RECV)
799 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
805 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
807 mmd->active_connections--;