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>
17 #include "server.cmdline.h"
30 #include "user_list.h"
31 #include "server_command_list.h"
32 #include "afs_command_list.h"
34 /** Commands including options must be shorter than this. */
35 #define MAX_COMMAND_LEN 32768
37 static RC4_KEY rc4_recv_key
;
38 static RC4_KEY rc4_send_key
;
39 static unsigned char rc4_buf
[2 * RC4_KEY_LEN
];
42 extern struct misc_meta_data
*mmd
;
43 extern struct sender senders
[];
45 static void dummy(int s
)
48 * At least on Solaris, SIGUSR1 is one-shot, i.e. the signal action is
49 * restored to the default state once the signal handler has been
53 signal(SIGUSR1
, dummy
);
56 static void mmd_dup(struct misc_meta_data
*new_mmd
)
58 mutex_lock(mmd_mutex
);
60 mutex_unlock(mmd_mutex
);
64 * Compute human readable string containing vss status for given integer value.
66 * We don't want to use vss_playing() and friends here because we take a
67 * snapshot of the mmd struct and use the copy for computing the state of the
68 * vss. If the real data were used, we would take the mmd lock for a rather
69 * long time or risk to get an inconsistent view.
71 static char *vss_status_tohuman(unsigned int flags
)
73 if (flags
& VSS_PLAYING
)
74 return para_strdup("playing");
76 return para_strdup("stopped");
77 return para_strdup("paused");
81 * return human readable permission string. Never returns NULL.
83 static char *cmd_perms_itohuman(unsigned int perms
)
85 char *msg
= para_malloc(5 * sizeof(char));
87 msg
[0] = perms
& AFS_READ
? 'a' : '-';
88 msg
[1] = perms
& AFS_WRITE
? 'A' : '-';
89 msg
[2] = perms
& VSS_READ
? 'v' : '-';
90 msg
[3] = perms
& VSS_WRITE
? 'V' : '-';
98 static char *vss_get_status_flags(unsigned int flags
)
100 char *msg
= para_malloc(5 * sizeof(char));
102 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
103 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
104 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
105 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
110 static char *get_status(struct misc_meta_data
*nmmd
)
112 char *ret
, mtime
[30] = "";
113 char *status
, *flags
; /* vss status info */
114 char *ut
= uptime_str();
115 long offset
= (nmmd
->offset
+ 500) / 1000;
119 /* report real status */
120 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
121 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
122 if (nmmd
->size
) { /* parent currently has an audio file open */
123 localtime_r(&nmmd
->mtime
, &mtime_tm
);
124 strftime(mtime
, 29, "%b %d %Y", &mtime_tm
);
126 gettimeofday(&now
, NULL
);
128 "%s: %zu\n" /* file size */
129 "%s: %s\n" /* mtime */
130 "%s: %s\n" /* status */
131 "%s: %s\n" /* status flags */
132 "%s: %li\n" /* offset */
133 "%s: %s\n" /* afs mode */
134 "%s: %lu.%lu\n" /* stream start */
135 "%s: %lu.%lu\n" /* current server time */
136 "%s", /* afs status info */
137 status_item_list
[SI_FILE_SIZE
], nmmd
->size
/ 1024,
138 status_item_list
[SI_MTIME
], mtime
,
139 status_item_list
[SI_STATUS
], status
,
140 status_item_list
[SI_STATUS_FLAGS
], flags
,
142 status_item_list
[SI_OFFSET
], offset
,
143 status_item_list
[SI_AFS_MODE
], mmd
->afs_mode_string
,
145 status_item_list
[SI_STREAM_START
],
146 (long unsigned)nmmd
->stream_start
.tv_sec
,
147 (long unsigned)nmmd
->stream_start
.tv_usec
,
148 status_item_list
[SI_CURRENT_TIME
],
149 (long unsigned)now
.tv_sec
,
150 (long unsigned)now
.tv_usec
,
152 nmmd
->afd
.verbose_ls_output
161 static int check_sender_args(int argc
, char * const * argv
, struct sender_command_data
*scd
)
164 /* this has to match sender.h */
165 const char *subcmds
[] = {"add", "delete", "allow", "deny", "on", "off", NULL
};
167 scd
->sender_num
= -1;
169 return -E_COMMAND_SYNTAX
;
170 for (i
= 0; senders
[i
].name
; i
++)
171 if (!strcmp(senders
[i
].name
, argv
[1]))
173 PARA_DEBUG_LOG("%d:%s\n", argc
, argv
[1]);
174 if (!senders
[i
].name
)
175 return -E_COMMAND_SYNTAX
;
177 for (i
= 0; subcmds
[i
]; i
++)
178 if (!strcmp(subcmds
[i
], argv
[2]))
181 return -E_COMMAND_SYNTAX
;
183 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
])
184 return -E_SENDER_CMD
;
185 switch (scd
->cmd_num
) {
189 return -E_COMMAND_SYNTAX
;
193 if (argc
!= 4 && argc
!= 5)
194 return -E_COMMAND_SYNTAX
;
195 if (!is_valid_ipv4_address(argv
[3]))
196 return -E_COMMAND_SYNTAX
;
199 scd
->netmask
= atoi(argv
[4]);
200 if (scd
->netmask
< 0 || scd
->netmask
> 32)
201 return -E_COMMAND_SYNTAX
;
203 strncpy(scd
->host
, argv
[3], sizeof(scd
->host
));
208 return -E_COMMAND_SYNTAX
;
209 return parse_fec_url(argv
[3], scd
);
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 ret
= send_buffer(fd
, msg
);
243 for (i
= 0; i
< 10; i
++) {
244 mutex_lock(mmd_mutex
);
245 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
246 mutex_unlock(mmd_mutex
);
250 memcpy(&mmd
->sender_cmd_data
, &scd
, sizeof(scd
));
251 mutex_unlock(mmd_mutex
);
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
;
266 mutex_lock(mmd_mutex
);
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"
278 "connections (active/accepted/total): %u/%u/%u\n"
279 "current loglevel: %s\n"
280 "supported audio formats: %s\n"
281 "supported senders: %s\n"
286 mmd
->active_connections
,
290 supported_audio_formats(),
294 mutex_unlock(mmd_mutex
);
302 int com_version(int fd
, int argc
, __a_unused
char * const * argv
)
305 return -E_COMMAND_SYNTAX
;
306 return send_buffer(fd
, VERSION_TEXT("server")
307 "built: " BUILD_DATE
"\n"
308 UNAME_RS
", " CC_VERSION
"\n"
313 int com_stat(int fd
, int argc
, char * const * argv
)
315 int ret
, num
= 0;/* status will be printed that many
316 * times. num <= 0 means: print forever
318 struct misc_meta_data tmp
, *nmmd
= &tmp
;
321 signal(SIGUSR1
, dummy
);
328 s
= get_status(nmmd
);
329 ret
= send_buffer(fd
, s
);
334 if (num
> 0 && !--num
)
338 return -E_SERVER_CRASH
;
344 static int send_list_of_commands(int fd
, struct server_command
*cmd
,
349 for (i
= 1; cmd
->name
; cmd
++, i
++) {
350 char *perms
= cmd_perms_itohuman(cmd
->perms
);
351 ret
= send_va_buffer(fd
, "%s\t%s\t%s\t%s\n", cmd
->name
,
362 /* returns string that must be freed by the caller */
363 static struct server_command
*get_cmd_ptr(const char *name
, char **handler
)
365 struct server_command
*cmd
;
367 for (cmd
= server_cmds
; cmd
->name
; cmd
++)
368 if (!strcmp(cmd
->name
, name
)) {
370 *handler
= para_strdup("server"); /* server commands */
373 /* not found, look for commands supported by afs */
374 for (cmd
= afs_cmds
; cmd
->name
; cmd
++)
375 if (!strcmp(cmd
->name
, name
)) {
377 *handler
= para_strdup("afs");
384 int com_help(int fd
, int argc
, char * const * argv
)
386 struct server_command
*cmd
;
387 char *perms
, *handler
;
391 /* no argument given, print list of commands */
392 if ((ret
= send_list_of_commands(fd
, server_cmds
, "server")) < 0)
394 return send_list_of_commands(fd
, afs_cmds
, "afs");
396 /* argument given for help */
397 cmd
= get_cmd_ptr(argv
[1], &handler
);
402 perms
= cmd_perms_itohuman(cmd
->perms
);
403 ret
= send_va_buffer(fd
,
422 int com_hup(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
425 return -E_COMMAND_SYNTAX
;
426 kill(getppid(), SIGHUP
);
431 int com_term(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
434 return -E_COMMAND_SYNTAX
;
435 kill(getppid(), SIGTERM
);
439 int com_play(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
442 return -E_COMMAND_SYNTAX
;
443 mutex_lock(mmd_mutex
);
444 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
445 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
446 mutex_unlock(mmd_mutex
);
452 int com_stop(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
455 return -E_COMMAND_SYNTAX
;
456 mutex_lock(mmd_mutex
);
457 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
458 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
459 mmd
->new_vss_status_flags
|= VSS_NEXT
;
460 mutex_unlock(mmd_mutex
);
465 int com_pause(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
468 return -E_COMMAND_SYNTAX
;
469 mutex_lock(mmd_mutex
);
470 if (!vss_paused() && !vss_stopped()) {
472 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
473 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
475 mutex_unlock(mmd_mutex
);
480 int com_next(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
483 return -E_COMMAND_SYNTAX
;
484 mutex_lock(mmd_mutex
);
486 mmd
->new_vss_status_flags
|= VSS_NEXT
;
487 mutex_unlock(mmd_mutex
);
492 int com_nomore(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
495 return -E_COMMAND_SYNTAX
;
496 mutex_lock(mmd_mutex
);
497 if (vss_playing() || vss_paused())
498 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
499 mutex_unlock(mmd_mutex
);
504 int com_ff(__a_unused
int fd
, int argc
, char * const * argv
)
507 int ret
, backwards
= 0;
512 return -E_COMMAND_SYNTAX
;
513 if (!(ret
= sscanf(argv
[1], "%u%c", &i
, &c
)))
514 return -E_COMMAND_SYNTAX
;
515 if (ret
> 1 && c
== '-')
516 backwards
= 1; /* jmp backwards */
517 mutex_lock(mmd_mutex
);
518 ret
= -E_NO_AUDIO_FILE
;
519 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
521 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
523 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
525 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
528 if (promille
> 1000) {
529 mmd
->new_vss_status_flags
|= VSS_NEXT
;
532 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
533 mmd
->new_vss_status_flags
|= VSS_REPOS
;
534 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
538 mutex_unlock(mmd_mutex
);
543 int com_jmp(__a_unused
int fd
, int argc
, char * const * argv
)
549 return -E_COMMAND_SYNTAX
;
550 if (sscanf(argv
[1], "%lu", &i
) <= 0)
551 return -E_COMMAND_SYNTAX
;
552 mutex_lock(mmd_mutex
);
553 ret
= -E_NO_AUDIO_FILE
;
554 if (!mmd
->afd
.afhi
.chunks_total
)
558 PARA_INFO_LOG("jumping to %lu%%\n", i
);
559 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50)/ 100;
560 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
561 mmd
->chunks_sent
, mmd
->offset
);
562 mmd
->new_vss_status_flags
|= VSS_REPOS
;
563 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
567 mutex_unlock(mmd_mutex
);
572 * check if perms are sufficient to exec a command having perms cmd_perms.
573 * Returns 0 if perms are sufficient, -E_PERM otherwise.
575 static int check_perms(unsigned int perms
, struct server_command
*cmd_ptr
)
577 PARA_DEBUG_LOG("checking permissions\n");
578 return (cmd_ptr
->perms
& perms
) < cmd_ptr
->perms
? -E_PERM
: 0;
582 * Parse first string from *cmd and lookup in table of valid commands.
583 * On error, NULL is returned.
585 static struct server_command
*parse_cmd(const char *cmdstr
)
590 sscanf(cmdstr
, "%200s%n", buf
, &n
);
594 return get_cmd_ptr(buf
, NULL
);
597 static void init_rc4_keys(void)
601 for (i
= 0; i
< 2 * RC4_KEY_LEN
; i
++)
602 rc4_buf
[i
] = para_random(256);
603 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
604 (unsigned char) rc4_buf
[0],
605 (unsigned char) rc4_buf
[RC4_KEY_LEN
]);
606 RC4_set_key(&rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
);
607 RC4_set_key(&rc4_send_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
610 static void rc4_recv(unsigned long len
, const unsigned char *indata
,
611 unsigned char *outdata
, __a_unused
void *private_data
)
613 RC4(&rc4_recv_key
, len
, indata
, outdata
);
616 static void rc4_send(unsigned long len
, const unsigned char *indata
,
617 unsigned char *outdata
, __a_unused
void *private_data
)
619 RC4(&rc4_send_key
, len
, indata
, outdata
);
622 static int read_command(int fd
, char **result
)
626 char *command
= NULL
;
632 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
638 ret
= -E_COMMAND_SYNTAX
;
639 if (command
&& numbytes
+ strlen(command
) > MAX_COMMAND_LEN
) /* DOS */
641 command
= para_strcat(command
, buf
);
642 p
= strstr(command
, EOC_MSG
);
648 ret
= command
? 1 : -E_COMMAND_SYNTAX
;
659 * Perform user authentication and execute a command.
661 * \param fd The file descriptor to send output to.
662 * \param peername Identifies the connecting peer.
664 * Whenever para_server accepts an incoming tcp connection on
665 * the port it listens on, it forks and the resulting child
666 * calls this function.
668 * An RSA-based challenge/response is used to authenticate
669 * the peer. It that authentication succeeds, a random RC4
670 * session key is generated and sent back to the peer,
671 * encrypted with its RSA public key. From this point on,
672 * all transfers are crypted with this session key.
674 * Next it is checked if the peer supplied a valid server command or a command
675 * for the audio file selector. If yes, and if the user has sufficient
676 * permissions to execute that command, the function calls the corresponding
677 * command handler which does argument checking and further processing.
679 * In order to cope with a DOS attacks, a timeout is set up
680 * which terminates the function if the connection was not
681 * authenticated when the timeout expires.
683 * \sa alarm(2), rc4(3), crypt.c, crypt.h
685 __noreturn
void handle_connect(int fd
, const char *peername
)
687 int ret
, argc
, use_rc4
= 0;
689 unsigned char crypt_buf
[MAXLINE
];
691 struct server_command
*cmd
= NULL
;
692 long unsigned challenge_nr
, chall_response
;
694 char *p
, *command
= NULL
;
697 signal(SIGCHLD
, SIG_IGN
);
698 signal(SIGINT
, SIG_DFL
);
699 signal(SIGTERM
, SIG_DFL
);
700 signal(SIGHUP
, SIG_DFL
);
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 free(mmd
->afd
.afhi
.info_string
);
796 free(mmd
->afd
.afhi
.chunk_table
);
797 mutex_unlock(mmd_mutex
);
801 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
803 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
807 mutex_lock(mmd_mutex
);
808 if (cmd
&& (cmd
->perms
& AFS_WRITE
) && ret
>= 0)
810 mmd
->active_connections
--;
811 mutex_unlock(mmd_mutex
);
812 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);