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 *basename
, *dirname
, *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 basename
= para_basename(nmmd
->filename
);
113 dirname
= para_dirname(nmmd
->filename
);
114 gettimeofday(&now
, NULL
);
116 "%s:%zu\n" "%s:%s\n" "%s:%lu\n" "%s:%u\n"
117 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%s\n"
118 "%s:%li\n" "%s:%s\n" "%s:%s\n" "%s"
119 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n" "%s:%s\n"
121 status_item_list
[SI_FILE_SIZE
], nmmd
->size
/ 1024,
122 status_item_list
[SI_MTIME
], mtime
,
123 status_item_list
[SI_LENGTH
], nmmd
->afd
.afhi
.seconds_total
,
124 status_item_list
[SI_NUM_PLAYED
], nmmd
->num_played
,
126 status_item_list
[SI_BASENAME
], basename
? basename
: "(none)",
127 status_item_list
[SI_STATUS
], status
,
128 status_item_list
[SI_STATUS_FLAGS
], flags
,
129 status_item_list
[SI_SELECTOR
], "FIXME",
131 status_item_list
[SI_OFFSET
], offset
,
132 status_item_list
[SI_FORMAT
], audio_format_name(nmmd
->afd
.afsi
.audio_format_id
),
133 status_item_list
[SI_AFS_MODE
], mmd
->afs_mode_string
,
134 nmmd
->afd
.afhi
.info_string
,
136 status_item_list
[SI_UPTIME
], ut
,
137 status_item_list
[SI_STREAM_START
],
138 (long unsigned)nmmd
->stream_start
.tv_sec
,
139 (long unsigned)nmmd
->stream_start
.tv_usec
,
140 status_item_list
[SI_CURRENT_TIME
],
141 (long unsigned)now
.tv_sec
,
142 (long unsigned)now
.tv_usec
,
143 status_item_list
[SI_ATTRIBUTES
], mmd
->afd
.attributes_string
,
145 status_item_list
[SI_DIRECTORY
], dirname
156 static int check_sender_args(int argc
, char * const * argv
, struct sender_command_data
*scd
)
159 /* this has to match sender.h */
160 const char *subcmds
[] = {"add", "delete", "allow", "deny", "on", "off", NULL
};
162 scd
->sender_num
= -1;
164 return -E_COMMAND_SYNTAX
;
165 for (i
= 0; senders
[i
].name
; i
++)
166 if (!strcmp(senders
[i
].name
, argv
[1]))
168 PARA_DEBUG_LOG("%d:%s\n", argc
, argv
[1]);
169 if (!senders
[i
].name
)
170 return -E_COMMAND_SYNTAX
;
172 for (i
= 0; subcmds
[i
]; i
++)
173 if (!strcmp(subcmds
[i
], argv
[2]))
176 return -E_COMMAND_SYNTAX
;
179 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
]) {
181 return -E_SENDER_CMD
;
184 switch (scd
->cmd_num
) {
188 return -E_COMMAND_SYNTAX
;
192 if (argc
!= 4 && argc
!= 5)
193 return -E_COMMAND_SYNTAX
;
194 if (!inet_pton(AF_INET
, argv
[3], &scd
->addr
))
195 return -E_COMMAND_SYNTAX
;
198 scd
->netmask
= atoi(argv
[4]);
199 if (scd
->netmask
< 0 || scd
->netmask
> 32)
200 return -E_COMMAND_SYNTAX
;
205 if (argc
!= 4 && argc
!= 5)
206 return -E_COMMAND_SYNTAX
;
207 if (!inet_pton(AF_INET
, argv
[3], &scd
->addr
))
208 return -E_COMMAND_SYNTAX
;
211 scd
->port
= atoi(argv
[4]);
212 if (scd
->port
< 0 || scd
->port
> 65535)
213 return -E_COMMAND_SYNTAX
;
217 return -E_COMMAND_SYNTAX
;
222 int com_sender(int fd
, int argc
, char * const * argv
)
225 struct sender_command_data scd
;
229 for (i
= 0; senders
[i
].name
; i
++) {
230 char *tmp
= make_message("%s%s\n",
231 msg
? msg
: "", senders
[i
].name
);
235 ret
= send_buffer(fd
, msg
);
239 ret
= check_sender_args(argc
, argv
, &scd
);
242 if (scd
.sender_num
< 0)
244 msg
= senders
[scd
.sender_num
].help();
245 send_buffer(fd
, msg
);
249 for (i
= 0; i
< 10; i
++) {
251 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
256 mmd
->sender_cmd_data
= scd
;
260 return (i
< 10)? 1 : -E_LOCK
;
264 int com_si(int fd
, int argc
, __a_unused
char * const * argv
)
268 char *sender_info
= NULL
, *sender_list
= NULL
;
271 return -E_COMMAND_SYNTAX
;
273 for (i
= 0; senders
[i
].name
; i
++) {
274 char *info
= senders
[i
].info();
275 sender_info
= para_strcat(sender_info
, info
);
277 sender_list
= para_strcat(sender_list
, senders
[i
].name
);
278 sender_list
= para_strcat(sender_list
, " ");
281 ret
= send_va_buffer(fd
, "up: %s\nplayed: %u\n"
283 "connections (active/accepted/total): %u/%u/%u\n"
284 "current loglevel: %i\n"
285 "supported audio formats: %s\n"
286 "supported senders: %s\n"
290 mmd
->active_connections
,
294 supported_audio_formats(),
306 int com_version(int fd
, int argc
, __a_unused
char * const * argv
)
309 return -E_COMMAND_SYNTAX
;
310 return send_buffer(fd
, VERSION_TEXT("server")
311 "built: " BUILD_DATE
"\n"
312 SYSTEM
", " CC_VERSION
"\n"
317 int com_stat(int fd
, int argc
, char * const * argv
)
319 int ret
, num
= 0;/* status will be printed that many
320 * times. num <= 0 means: print forever
322 struct misc_meta_data tmp
, *nmmd
= &tmp
;
325 signal(SIGUSR1
, dummy
);
332 s
= get_status(nmmd
);
333 ret
= send_buffer(fd
, s
);
342 return -E_SERVER_CRASH
;
348 static int send_list_of_commands(int fd
, struct server_command
*cmd
,
353 for (i
= 1; cmd
->name
; cmd
++, i
++) {
354 char *perms
= cmd_perms_itohuman(cmd
->perms
);
355 ret
= send_va_buffer(fd
, "%s\t%s\t%s\t%s\n", cmd
->name
,
366 /* returns string that must be freed by the caller */
367 static struct server_command
*get_cmd_ptr(const char *name
, char **handler
)
369 struct server_command
*cmd
;
371 for (cmd
= server_cmds
; cmd
->name
; cmd
++)
372 if (!strcmp(cmd
->name
, name
)) {
374 *handler
= para_strdup("para_server"); /* server commands */
377 /* not found, look for commands supported by afs */
378 for (cmd
= afs_cmds
; cmd
->name
; cmd
++)
379 if (!strcmp(cmd
->name
, name
))
385 int com_help(int fd
, int argc
, char * const * argv
)
387 struct server_command
*cmd
;
388 char *perms
, *handler
;
392 /* no argument given, print list of commands */
393 if ((ret
= send_list_of_commands(fd
, server_cmds
, "server")) < 0)
395 return send_list_of_commands(fd
, afs_cmds
, "afs");
397 /* argument given for help */
398 cmd
= get_cmd_ptr(argv
[1], &handler
);
403 perms
= cmd_perms_itohuman(cmd
->perms
);
404 ret
= send_va_buffer(fd
,
423 int com_hup(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
426 return -E_COMMAND_SYNTAX
;
427 kill(getppid(), SIGHUP
);
432 int com_term(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
435 return -E_COMMAND_SYNTAX
;
436 kill(getppid(), SIGTERM
);
440 int com_play(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
443 return -E_COMMAND_SYNTAX
;
445 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
446 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
453 int com_stop(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
456 return -E_COMMAND_SYNTAX
;
458 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
459 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
460 mmd
->new_vss_status_flags
|= VSS_NEXT
;
466 int com_pause(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
469 return -E_COMMAND_SYNTAX
;
473 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
474 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
480 int com_next(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
483 return -E_COMMAND_SYNTAX
;
486 mmd
->new_vss_status_flags
|= VSS_NEXT
;
492 int com_nomore(__a_unused
int fd
, int argc
, __a_unused
char * const * argv
)
495 return -E_COMMAND_SYNTAX
;
497 if (vss_playing() || vss_paused())
498 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
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 */
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
;
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
;
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
;
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("%s", "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 long int para_rand(long unsigned max
)
599 return (long int) ((max
+ 0.0) * (random() / (RAND_MAX
+ 1.0)));
602 static void init_rc4_keys(void)
606 for (i
= 0; i
< 2 * RC4_KEY_LEN
; i
++)
607 rc4_buf
[i
] = para_rand(256);
608 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
609 (unsigned char) rc4_buf
[0],
610 (unsigned char) rc4_buf
[RC4_KEY_LEN
]);
611 RC4_set_key(&rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
);
612 RC4_set_key(&rc4_send_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
615 static void rc4_recv(unsigned long len
, const unsigned char *indata
,
616 unsigned char *outdata
, __a_unused
void *private_data
)
618 RC4(&rc4_recv_key
, len
, indata
, outdata
);
621 static void rc4_send(unsigned long len
, const unsigned char *indata
,
622 unsigned char *outdata
, __a_unused
void *private_data
)
624 RC4(&rc4_send_key
, len
, indata
, outdata
);
627 static int read_command(int fd
, char **result
)
631 char *command
= NULL
;
637 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
643 ret
= -E_COMMAND_SYNTAX
;
644 if (command
&& numbytes
+ strlen(command
) > MAX_COMMAND_LEN
) /* DOS */
646 command
= para_strcat(command
, buf
);
647 p
= strstr(command
, EOC_MSG
);
653 ret
= command
? 1 : -E_COMMAND_SYNTAX
;
664 * perform user authentication and execute a command
666 * \param fd the file descriptor to send output to
667 * \param addr socket address info of peer
669 * \return EXIT_SUCCESS or EXIT_FAILURE
671 * Whenever para_server accepts an incoming tcp connection on
672 * the port it listens on, it forks and the resulting child
673 * calls this function.
675 * An RSA-based challenge/response is used to authenticate
676 * the peer. It that authentication succeeds, a random RC4
677 * session key is generated and sent back to the peer,
678 * encrypted with its RSA public key. From this point on,
679 * all transfers are crypted with this session key.
681 * Next it is checked if the peer supplied a valid server command or a command
682 * for the audio file selector. If yes, and if the user has sufficient
683 * permissions to execute that command, the function calls the corresponding
684 * command handler which does argument checking and further processing.
686 * In order to cope with a DOS attacks, a timeout is set up
687 * which terminates the function if the connection was not
688 * authenticated when the timeout expires.
690 * \sa alarm(2), rc4(3), crypt.c, crypt.h
692 int handle_connect(int fd
, struct sockaddr_in
*addr
)
694 int ret
, argc
, use_rc4
= 0;
696 unsigned char crypt_buf
[MAXLINE
];
698 struct server_command
*cmd
= NULL
;
699 long unsigned challenge_nr
, chall_response
;
701 char *p
, *command
= NULL
;
704 signal(SIGCHLD
, SIG_IGN
);
705 signal(SIGINT
, SIG_DFL
);
706 signal(SIGTERM
, SIG_DFL
);
707 signal(SIGHUP
, SIG_DFL
);
708 signal(SIGUSR1
, SIG_IGN
);
710 challenge_nr
= random();
711 /* send Welcome message */
712 ret
= send_va_buffer(fd
, "This is para_server, version "
713 PACKAGE_VERSION
".\n" );
716 /* recv auth request line */
717 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
726 if (strncmp(buf
, "auth ", 5))
729 if (numbytes
< 9 || strncmp(buf
, "auth rc4 ", 9))
730 p
= buf
+ 5; /* client version < 0.2.6 */
732 p
= buf
+ 9; /* client version >= 0.2.6 */
735 PARA_DEBUG_LOG("received %s request for user %s\n",
736 use_rc4
? "rc4" : "auth", p
);
741 ret
= para_encrypt_challenge(u
->rsa
, challenge_nr
, crypt_buf
);
745 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes
);
746 /* We can't use send_buffer here since buf may contain null bytes */
747 ret
= send_bin_buffer(fd
,(char *) crypt_buf
, numbytes
);
750 /* recv decrypted number */
751 ret
= recv_buffer(fd
, buf
, sizeof(buf
));
758 if (sscanf(buf
, CHALLENGE_RESPONSE_MSG
"%lu", &chall_response
) < 1
759 || chall_response
!= challenge_nr
)
761 /* auth successful, send 'Proceed' message */
762 PARA_INFO_LOG("good auth for %s (%lu)\n", u
->name
, challenge_nr
);
763 sprintf(buf
, "%s", PROCEED_MSG
);
766 ret
= para_encrypt_buffer(u
->rsa
, rc4_buf
, 2 * RC4_KEY_LEN
,
767 (unsigned char *)buf
+ PROCEED_MSG_LEN
+ 1);
770 numbytes
= ret
+ strlen(PROCEED_MSG
) + 1;
772 numbytes
= strlen(buf
);
773 ret
= send_bin_buffer(fd
, buf
, numbytes
);
777 enable_crypt(fd
, rc4_recv
, rc4_send
, NULL
);
778 ret
= read_command(fd
, &command
);
782 cmd
= parse_cmd(command
);
785 /* valid command, check permissions */
786 ret
= check_perms(u
->perms
, cmd
);
789 /* valid command and sufficient perms */
791 argc
= split_args(command
, &argv
, "\n");
795 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd
->name
, u
->name
,
796 inet_ntoa(addr
->sin_addr
));
797 ret
= cmd
->handler(fd
, argc
, argv
);
803 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret
));
804 if (ret
!= -E_SEND
&& ret
!= -E_RECV
)
805 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
811 if (cmd
&& (cmd
->perms
& AFS_WRITE
) && ret
>= 0)
813 mmd
->active_connections
--;