command.c: Increase maximal command length.
[paraslash.git] / command.c
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file command.c does client authentication and executes server commands */
8
9 #include <sys/time.h> /* gettimeofday */
10 #include "para.h" /* gettimeofday */
11
12 #include "server.cmdline.h"
13 #include "afs_common.h"
14 #include "afh.h"
15 #include "server.h"
16 #include "vss.h"
17 #include "send.h"
18 #include "rc4.h"
19 #include <openssl/rc4.h>
20 #include "error.h"
21 #include "net.h"
22 #include "daemon.h"
23 #include "string.h"
24 #include "fd.h"
25 #include "list.h"
26 #include "user_list.h"
27 #include "server_command_list.h"
28 #include "afs_command_list.h"
29
30 /** commands including options must be shorter than this */
31 #define MAX_COMMAND_LEN 32768
32
33 static RC4_KEY rc4_recv_key;
34 static RC4_KEY rc4_send_key;
35 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
36
37 extern const char *status_item_list[NUM_STAT_ITEMS];
38 extern struct misc_meta_data *mmd;
39 extern struct audio_file_selector selectors[];
40 extern struct sender senders[];
41
42 static void dummy(__a_unused int s)
43 {}
44
45 static void mmd_dup(struct misc_meta_data *new_mmd)
46 {
47 mmd_lock();
48 *new_mmd = *mmd;
49 mmd_unlock();
50 }
51
52 /*
53 * compute human readable string containing
54 * vss status for given integer value
55 */
56 static char *vss_status_tohuman(unsigned int flags)
57 {
58 if (flags & VSS_PLAYING)
59 return para_strdup("playing");
60 else if (flags & VSS_NEXT)
61 return para_strdup("stopped");
62 else
63 return para_strdup("paused");
64 }
65
66 /*
67 * return human readable permission string. Never returns NULL.
68 */
69 static char *cmd_perms_itohuman(unsigned int perms)
70 {
71 char *msg = para_malloc(5 * sizeof(char));
72
73 msg[0] = perms & AFS_READ? 'a' : '-';
74 msg[1] = perms & AFS_WRITE? 'A' : '-';
75 msg[2] = perms & VSS_READ? 'v' : '-';
76 msg[3] = perms & VSS_WRITE? 'V' : '-';
77 msg[4] = '\0';
78 return msg;
79 }
80
81 /*
82 * Never returns NULL.
83 */
84 static char *vss_get_status_flags(unsigned int flags)
85 {
86 char *msg = para_malloc(5 * sizeof(char));
87
88 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
89 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
90 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
91 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
92 msg[4] = '\0';
93 return msg;
94 }
95
96 /*
97 * compute status bar string. Never returns NULL
98 */
99 static char *get_sb_string(struct misc_meta_data *nmmd)
100 {
101 char *base, *ret;
102 long long unsigned secs = 0, rsecs = 0, percent = 0;
103
104 base = para_basename(nmmd->filename);
105 if (!base)
106 return para_strdup("");
107 if (!base[0])
108 return base;
109 if (nmmd->afi.chunks_total) {
110 secs = (long long) nmmd->afi.seconds_total * nmmd->chunks_sent
111 / nmmd->afi.chunks_total;
112 rsecs = (long long) nmmd->afi.seconds_total *
113 (nmmd->afi.chunks_total - nmmd->chunks_sent)
114 / nmmd->afi.chunks_total;
115 percent = 100 * ((nmmd->chunks_sent + 5) / 10)
116 / ((nmmd->afi.chunks_total + 5) / 10);
117 }
118 ret = make_message("%llu:%02llu [%llu:%02llu] (%llu%%) %s",
119 secs / 60, secs % 60,
120 rsecs / 60, rsecs % 60,
121 percent,
122 base
123 );
124 free(base);
125 return ret;
126 }
127
128 static char *get_status(struct misc_meta_data *nmmd)
129 {
130 char *bar, *ret, mtime[30] = "";
131 char *status, *flags; /* vss status info */
132 char *ut = uptime_str();
133 long offset = (nmmd->offset + 500) / 1000;
134 struct timeval now;
135 struct tm mtime_tm;
136
137 if (nmmd->audio_format >= 0) {
138 localtime_r(&nmmd->mtime, &mtime_tm);
139 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
140 }
141 /* report real status */
142 status = vss_status_tohuman(nmmd->vss_status_flags);
143 flags = vss_get_status_flags(nmmd->vss_status_flags);
144 bar = para_basename(nmmd->filename);
145 gettimeofday(&now, NULL);
146 ret = make_message(
147 "%s:%zu\n" "%s:%s\n" "%s:%lu\n" "%s:%u\n"
148 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%s\n"
149 "%s:%li\n" "%s:%s\n" "%s" "%s"
150 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n",
151 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
152 status_item_list[SI_MTIME], mtime,
153 status_item_list[SI_LENGTH], nmmd->afi.seconds_total,
154 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
155
156 status_item_list[SI_STATUS_BAR], bar ? bar : "(none)",
157 status_item_list[SI_STATUS], status,
158 status_item_list[SI_STATUS_FLAGS], flags,
159 status_item_list[SI_SELECTOR], selectors[nmmd->selector_num].name,
160
161 status_item_list[SI_OFFSET], offset,
162 status_item_list[SI_FORMAT], audio_format_name(nmmd->audio_format),
163 nmmd->selector_info,
164 nmmd->afi.info_string,
165
166 status_item_list[SI_UPTIME], ut,
167 status_item_list[SI_STREAM_START],
168 (long unsigned)nmmd->stream_start.tv_sec,
169 (long unsigned)nmmd->stream_start.tv_usec,
170 status_item_list[SI_CURRENT_TIME],
171 (long unsigned)now.tv_sec,
172 (long unsigned)now.tv_usec
173
174 );
175 free(bar);
176 free(flags);
177 free(status);
178 free(ut);
179 return ret;
180 }
181
182 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
183 {
184 int i;
185 /* this has to match sender.h */
186 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
187
188 scd->sender_num = -1;
189 if (argc < 2)
190 return -E_COMMAND_SYNTAX;
191 for (i = 0; senders[i].name; i++)
192 if (!strcmp(senders[i].name, argv[1]))
193 break;
194 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
195 if (!senders[i].name)
196 return -E_COMMAND_SYNTAX;
197 scd->sender_num = i;
198 for (i = 0; subcmds[i]; i++)
199 if (!strcmp(subcmds[i], argv[2]))
200 break;
201 if (!subcmds[i])
202 return -E_COMMAND_SYNTAX;
203 scd->cmd_num = i;
204 mmd_lock();
205 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
206 mmd_unlock();
207 return -E_SENDER_CMD;
208 }
209 mmd_unlock();
210 switch (scd->cmd_num) {
211 case SENDER_ON:
212 case SENDER_OFF:
213 if (argc != 3)
214 return -E_COMMAND_SYNTAX;
215 break;
216 case SENDER_DENY:
217 case SENDER_ALLOW:
218 if (argc != 4 && argc != 5)
219 return -E_COMMAND_SYNTAX;
220 if (!inet_aton(argv[3], &scd->addr))
221 return -E_COMMAND_SYNTAX;
222 scd->netmask = 32;
223 if (argc == 5) {
224 scd->netmask = atoi(argv[4]);
225 if (scd->netmask < 0 || scd->netmask > 32)
226 return -E_COMMAND_SYNTAX;
227 }
228 break;
229 case SENDER_ADD:
230 case SENDER_DELETE:
231 if (argc != 4 && argc != 5)
232 return -E_COMMAND_SYNTAX;
233 if (!inet_aton(argv[3], &scd->addr))
234 return -E_COMMAND_SYNTAX;
235 scd->port = -1;
236 if (argc == 5) {
237 scd->port = atoi(argv[4]);
238 if (scd->port < 0 || scd->port > 65535)
239 return -E_COMMAND_SYNTAX;
240 }
241 break;
242 default:
243 return -E_COMMAND_SYNTAX;
244 }
245 return 1;
246 }
247
248 int com_sender(int fd, int argc, char * const * argv)
249 {
250 int i, ret;
251 struct sender_command_data scd;
252
253 if (argc < 2) {
254 char *msg = NULL;
255 for (i = 0; senders[i].name; i++) {
256 char *tmp = make_message("%s%s\n",
257 msg? msg : "", senders[i].name);
258 free(msg);
259 msg = tmp;
260 }
261 ret = send_buffer(fd, msg);
262 free(msg);
263 return ret;
264 }
265 ret = check_sender_args(argc, argv, &scd);
266 if (ret < 0) {
267 char *msg;
268 if (scd.sender_num < 0)
269 return ret;
270 msg = senders[scd.sender_num].help();
271 send_buffer(fd, msg);
272 free(msg);
273 return 1;
274 }
275 for (i = 0; i < 10; i++) {
276 mmd_lock();
277 if (mmd->sender_cmd_data.cmd_num >= 0) {
278 mmd_unlock();
279 usleep(100 * 1000);
280 continue;
281 }
282 mmd->sender_cmd_data = scd;
283 mmd_unlock();
284 break;
285 }
286 return (i < 10)? 1 : -E_LOCK;
287 }
288
289 /* server info */
290 int com_si(int fd, int argc, __a_unused char * const * argv)
291 {
292 int i, ret;
293 char *ut;
294 char *selector_string = NULL, *sender_info = NULL, *sender_list = NULL;
295
296 if (argc != 1)
297 return -E_COMMAND_SYNTAX;
298 mmd_lock();
299 for (i = 0; selectors[i].name; i++) {
300 selector_string = para_strcat(selector_string, selectors[i].name);
301 selector_string = para_strcat(selector_string, " ");
302 }
303 for (i = 0; senders[i].name; i++) {
304 char *info = senders[i].info();
305 sender_info = para_strcat(sender_info, info);
306 free(info);
307 sender_list = para_strcat(sender_list, senders[i].name);
308 sender_list = para_strcat(sender_list, " ");
309 }
310 ut = uptime_str();
311 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
312 "pid: %d\n"
313 "connections (active/accepted/total): %u/%u/%u\n"
314 "current loglevel: %i\n"
315 "supported audio file selectors: %s\n"
316 "supported audio formats: %s\n"
317 "supported senders: %s\n"
318 "%s",
319 ut, mmd->num_played,
320 getppid(),
321 mmd->active_connections,
322 mmd->num_commands,
323 mmd->num_connects,
324 conf.loglevel_arg,
325 selector_string,
326 supported_audio_formats(),
327 sender_list,
328 sender_info
329 );
330 mmd_unlock();
331 free(ut);
332 free(selector_string);
333 free(sender_list);
334 free(sender_info);
335 return ret;
336 }
337
338 /* version */
339 int com_version(int fd, int argc, __a_unused char * const * argv)
340 {
341 if (argc != 1)
342 return -E_COMMAND_SYNTAX;
343 return send_buffer(fd, VERSION_TEXT("server")
344 "built: " BUILD_DATE "\n"
345 SYSTEM ", " CC_VERSION "\n"
346 );
347 }
348
349 /* sc */
350 int com_sc(int fd, int argc, char * const * argv)
351 {
352 char *name = NULL;
353 int ret, old = 0, count = -1; /* print af change forever */
354
355 if (argc > 1)
356 count = atoi(argv[1]);
357 repeat:
358 mmd_lock();
359 if (old != mmd->num_played) {
360 old = mmd->num_played;
361 name = para_strdup(mmd->filename);
362 }
363 mmd_unlock();
364 if (name) {
365 ret = send_va_buffer(fd, "%s\n", name);
366 free(name);
367 name = NULL;
368 if (ret < 0)
369 return ret;
370 if (argc > 1 && !--count)
371 return 1;
372 }
373 usleep(500000);
374 goto repeat;
375 }
376
377 /* sb */
378 int com_sb(int fd, int argc, char * const * argv)
379 {
380 char *sb;
381 int ret, nr = -1; /* status bar will be printed that many
382 * times. Negative value means: print
383 * forever
384 */
385 if (argc > 1)
386 nr = atoi(argv[1]);
387 while (nr) {
388 mmd_lock();
389 sb = get_sb_string(mmd);
390 mmd_unlock();
391 ret = send_va_buffer(fd, "%s\n", sb);
392 free(sb);
393 if (ret < 0)
394 return ret;
395 if (nr == 1)
396 return 1;
397 usleep(500000);
398 if (nr > 0)
399 nr--;
400 }
401 return 1;
402 }
403
404 /* stat */
405 int com_stat(int fd, int argc, char * const * argv)
406 {
407 int ret, num = 0;/* status will be printed that many
408 * times. num <= 0 means: print forever
409 */
410 struct misc_meta_data tmp, *nmmd = &tmp;
411 char *s;
412
413 signal(SIGUSR1, dummy);
414
415 if (argc > 1)
416 num = atoi(argv[1]);
417 for (;;) {
418
419 mmd_dup(nmmd);
420 s = get_status(nmmd);
421 ret = send_buffer(fd, s);
422 free(s);
423 if (ret < 0)
424 goto out;
425 ret = 1;
426 if (num == 1)
427 goto out;
428 sleep(50);
429 if (getppid() == 1)
430 return -E_SERVER_CRASH;
431 }
432 out:
433 return ret;
434 }
435
436 static int send_list_of_commands(int fd, struct server_command *cmd,
437 const char *handler)
438 {
439 int ret, i;
440
441 for (i = 1; cmd->name; cmd++, i++) {
442 char *perms = cmd_perms_itohuman(cmd->perms);
443 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
444 handler,
445 perms,
446 cmd->description);
447 free(perms);
448 if (ret < 0)
449 return ret;
450 }
451 return 1;
452 }
453
454 /* always returns string that must be freed by the caller in handler */
455 static struct server_command *get_cmd_ptr(const char *name, char **handler)
456 {
457 struct server_command *cmd;
458
459 for (cmd = server_cmds; cmd->name; cmd++)
460 if (!strcmp(cmd->name, name)) {
461 if (handler)
462 *handler = para_strdup("para_server"); /* server commands */
463 return cmd;
464 }
465 /* not found, look for commands supported by the current selector */
466 mmd_lock();
467 if (handler)
468 *handler = make_message("the %s selector",
469 selectors[mmd->selector_num].name);
470 cmd = selectors[mmd->selector_num].cmd_list;
471 mmd_unlock();
472 for (; cmd->name; cmd++)
473 if (!strcmp(cmd->name, name))
474 return cmd;
475 /* not found, look for commands supported by afs */
476 for (cmd = afs_cmds; cmd; cmd++)
477 if (!strcmp(cmd->name, name))
478 return cmd;
479 return NULL;
480 }
481
482 /* help */
483 int com_help(int fd, int argc, char * const * argv)
484 {
485 struct server_command *cmd;
486 char *perms, *handler;
487 int ret;
488
489 if (argc < 2) {
490 /* no argument given, print list of commands */
491 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
492 return ret;
493 mmd_lock();
494 handler = para_strdup(selectors[mmd->selector_num].name);
495 cmd = selectors[mmd->selector_num].cmd_list;
496 mmd_unlock();
497 ret = send_list_of_commands(fd, cmd, handler);
498 free(handler);
499 if (ret < 0)
500 return ret;
501 cmd = afs_cmds;
502 ret = send_list_of_commands(fd, cmd, "afs");
503 }
504 /* argument given for help */
505 cmd = get_cmd_ptr(argv[1], &handler);
506 if (!cmd) {
507 free(handler);
508 return -E_BAD_CMD;
509 }
510 perms = cmd_perms_itohuman(cmd->perms);
511 ret = send_va_buffer(fd,
512 "%s - %s\n\n"
513 "handler: %s\n"
514 "permissions: %s\n"
515 "usage: %s\n\n"
516 "%s\n",
517 argv[1],
518 cmd->description,
519 handler,
520 perms,
521 cmd->usage,
522 cmd->help
523 );
524 free(perms);
525 free(handler);
526 return ret;
527 }
528
529 /* hup */
530 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
531 {
532 if (argc != 1)
533 return -E_COMMAND_SYNTAX;
534 kill(getppid(), SIGHUP);
535 return 1;
536 }
537
538 /* term */
539 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
540 {
541 if (argc != 1)
542 return -E_COMMAND_SYNTAX;
543 kill(getppid(), SIGTERM);
544 return 1;
545 }
546
547 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
548 {
549 if (argc != 1)
550 return -E_COMMAND_SYNTAX;
551 mmd_lock();
552 mmd->new_vss_status_flags |= VSS_PLAYING;
553 mmd->new_vss_status_flags &= ~VSS_NOMORE;
554 mmd_unlock();
555 return 1;
556
557 }
558
559 /* stop */
560 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
561 {
562 if (argc != 1)
563 return -E_COMMAND_SYNTAX;
564 mmd_lock();
565 mmd->new_vss_status_flags &= ~VSS_PLAYING;
566 mmd->new_vss_status_flags &= ~VSS_REPOS;
567 mmd->new_vss_status_flags |= VSS_NEXT;
568 mmd_unlock();
569 return 1;
570 }
571
572 /* pause */
573 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
574 {
575 if (argc != 1)
576 return -E_COMMAND_SYNTAX;
577 mmd_lock();
578 if (!vss_paused())
579 mmd->events++;
580 mmd->new_vss_status_flags &= ~VSS_PLAYING;
581 mmd->new_vss_status_flags &= ~VSS_NEXT;
582 mmd_unlock();
583 return 1;
584 }
585
586 int com_chs(int fd, int argc, char * const * argv)
587 {
588 int i, ret;
589
590 if (argc == 1) {
591 char *selector;
592 mmd_lock();
593 selector = para_strdup(selectors[mmd->selector_num].name);
594 mmd_unlock();
595 ret = send_va_buffer(fd, "%s\n", selector);
596 free(selector);
597 return ret;
598 }
599 for (i = 0; selectors[i].name; i++) {
600 if (strcmp(selectors[i].name, argv[1]))
601 continue;
602 mmd_lock();
603 mmd->selector_change = i;
604 mmd->events++;
605 mmd_unlock();
606 return 1;
607 }
608 return -E_BAD_SELECTOR;
609 }
610
611 /* next */
612 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
613 {
614 if (argc != 1)
615 return -E_COMMAND_SYNTAX;
616 mmd_lock();
617 mmd->events++;
618 mmd->new_vss_status_flags |= VSS_NEXT;
619 mmd_unlock();
620 return 1;
621 }
622
623 /* nomore */
624 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
625 {
626 if (argc != 1)
627 return -E_COMMAND_SYNTAX;
628 mmd_lock();
629 if (vss_playing() || vss_paused())
630 mmd->new_vss_status_flags |= VSS_NOMORE;
631 mmd_unlock();
632 return 1;
633 }
634
635 /* ff */
636 int com_ff(__a_unused int fd, int argc, char * const * argv)
637 {
638 long promille;
639 int ret, backwards = 0;
640 unsigned i;
641 char c;
642
643 if (argc != 2)
644 return -E_COMMAND_SYNTAX;
645 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
646 return -E_COMMAND_SYNTAX;
647 if (ret > 1 && c == '-')
648 backwards = 1; /* jmp backwards */
649 mmd_lock();
650 ret = -E_NO_AUDIO_FILE;
651 if (!mmd->afi.chunks_total || !mmd->afi.seconds_total)
652 goto out;
653 promille = (1000 * mmd->current_chunk) / mmd->afi.chunks_total;
654 if (backwards)
655 promille -= 1000 * i / mmd->afi.seconds_total;
656 else
657 promille += 1000 * i / mmd->afi.seconds_total;
658 if (promille < 0)
659 promille = 0;
660 if (promille > 1000) {
661 mmd->new_vss_status_flags |= VSS_NEXT;
662 goto out;
663 }
664 mmd->repos_request = (mmd->afi.chunks_total * promille) / 1000;
665 mmd->new_vss_status_flags |= VSS_REPOS;
666 mmd->new_vss_status_flags &= ~VSS_NEXT;
667 mmd->events++;
668 ret = 1;
669 out:
670 mmd_unlock();
671 return ret;
672 }
673
674 /* jmp */
675 int com_jmp(__a_unused int fd, int argc, char * const * argv)
676 {
677 long unsigned int i;
678 int ret;
679
680 if (argc != 2)
681 return -E_COMMAND_SYNTAX;
682 if (sscanf(argv[1], "%lu", &i) <= 0)
683 return -E_COMMAND_SYNTAX;
684 mmd_lock();
685 ret = -E_NO_AUDIO_FILE;
686 if (!mmd->afi.chunks_total)
687 goto out;
688 if (i > 100)
689 i = 100;
690 PARA_INFO_LOG("jumping to %lu%%\n", i);
691 mmd->repos_request = (mmd->afi.chunks_total * i + 50)/ 100;
692 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
693 mmd->chunks_sent, mmd->offset);
694 mmd->new_vss_status_flags |= VSS_REPOS;
695 mmd->new_vss_status_flags &= ~VSS_NEXT;
696 ret = 1;
697 mmd->events++;
698 out:
699 mmd_unlock();
700 return ret;
701 }
702
703 /*
704 * check if perms are sufficient to exec a command having perms cmd_perms.
705 * Returns 0 if perms are sufficient, -E_PERM otherwise.
706 */
707 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
708 {
709 PARA_DEBUG_LOG("%s", "checking permissions\n");
710 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
711 }
712
713 /*
714 * Parse first string from *cmd and lookup in table of valid commands.
715 * On error, NULL is returned.
716 */
717 static struct server_command *parse_cmd(const char *cmdstr)
718 {
719 char buf[255];
720 int n = 0;
721
722 sscanf(cmdstr, "%200s%n", buf, &n);
723 if (!n)
724 return NULL;
725 buf[n] = '\0';
726 return get_cmd_ptr(buf, NULL);
727 }
728
729 static long int para_rand(long unsigned max)
730 {
731 return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
732 }
733
734 static void init_rc4_keys(void)
735 {
736 int i;
737
738 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
739 rc4_buf[i] = para_rand(256);
740 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
741 (unsigned char) rc4_buf[0],
742 (unsigned char) rc4_buf[RC4_KEY_LEN]);
743 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
744 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
745 }
746
747 static void rc4_recv(unsigned long len, const unsigned char *indata,
748 unsigned char *outdata, __a_unused void *private_data)
749 {
750 RC4(&rc4_recv_key, len, indata, outdata);
751 }
752
753 static void rc4_send(unsigned long len, const unsigned char *indata,
754 unsigned char *outdata, __a_unused void *private_data)
755 {
756 RC4(&rc4_send_key, len, indata, outdata);
757 }
758
759 static int read_command(int fd, char **result)
760 {
761 int ret;
762 char buf[4096];
763 char *command = NULL;
764
765 for (;;) {
766 size_t numbytes;
767 char *p;
768
769 ret = recv_buffer(fd, buf, sizeof(buf));
770 if (ret < 0)
771 goto out;
772 if (!ret)
773 break;
774 numbytes = ret;
775 ret = -E_COMMAND_SYNTAX;
776 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
777 goto out;
778 command = para_strcat(command, buf);
779 p = strstr(command, EOC_MSG);
780 if (p) {
781 *p = '\0';
782 break;
783 }
784 }
785 ret = command? 1 : -E_COMMAND_SYNTAX;
786 out:
787 if (ret < 0)
788 free(command);
789 else
790 *result = command;
791 return ret;
792
793 }
794
795 /**
796 * perform user authentication and execute a command
797 *
798 * \param fd the file descriptor to send output to
799 * \param addr socket address info of peer
800 *
801 * \return EXIT_SUCCESS or EXIT_FAILURE
802 *
803 * Whenever para_server accepts an incoming tcp connection on
804 * the port it listens on, it forks and the resulting child
805 * calls this function.
806 *
807 * An RSA-based challenge/response is used to authenticate
808 * the peer. It that authentication succeeds, a random RC4
809 * session key is generated and sent back to the peer,
810 * encrypted with its RSA public key. From this point on,
811 * all transfers are crypted with this session key.
812 *
813 * Next it is checked if the peer supplied a valid server
814 * command or a command for the audio file selector currently
815 * in use. If yes, and if the user has sufficient
816 * permissions to execute that command, the function calls
817 * the corresponding command handler which does argument
818 * checking and further processing.
819 *
820 * In order to cope with a DOS attacks, a timeout is set up
821 * which terminates the function if the connection was not
822 * authenticated when the timeout expires.
823 *
824 * \sa alarm(2), rc4(3), crypt.c, crypt.h
825 */
826 int handle_connect(int fd, struct sockaddr_in *addr)
827 {
828 int ret, argc, use_rc4 = 0;
829 char buf[4096];
830 unsigned char crypt_buf[MAXLINE];
831 struct user *u;
832 struct server_command *cmd = NULL;
833 long unsigned challenge_nr, chall_response;
834 char **argv = NULL;
835 char *p, *command = NULL;
836 size_t numbytes;
837
838 signal(SIGCHLD, SIG_IGN);
839 signal(SIGINT, SIG_DFL);
840 signal(SIGTERM, SIG_DFL);
841 signal(SIGHUP, SIG_DFL);
842 signal(SIGUSR1, SIG_IGN);
843
844 challenge_nr = random();
845 /* send Welcome message */
846 ret = send_va_buffer(fd, "This is para_server, version "
847 PACKAGE_VERSION ".\n" );
848 if (ret < 0)
849 goto err_out;
850 /* recv auth request line */
851 ret = recv_buffer(fd, buf, sizeof(buf));
852 if (ret < 0)
853 goto err_out;
854 if (ret <= 6) {
855 ret = -E_AUTH;
856 goto err_out;
857 }
858 numbytes = ret;
859 ret = -E_AUTH;
860 if (strncmp(buf, "auth ", 5))
861 goto err_out;
862
863 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
864 p = buf + 5; /* client version < 0.2.6 */
865 else {
866 p = buf + 9; /* client version >= 0.2.6 */
867 use_rc4 = 1;
868 }
869 PARA_DEBUG_LOG("received %s request for user %s\n",
870 use_rc4? "rc4" : "auth", p);
871 ret = -E_BAD_USER;
872 u = lookup_user(p);
873 if (!u)
874 goto err_out;
875 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
876 if (ret <= 0)
877 goto err_out;
878 numbytes = ret;
879 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
880 /* We can't use send_buffer here since buf may contain null bytes */
881 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
882 if (ret < 0)
883 goto err_out;
884 /* recv decrypted number */
885 ret = recv_buffer(fd, buf, sizeof(buf));
886 if (ret < 0)
887 goto err_out;
888 numbytes = ret;
889 ret = -E_AUTH;
890 if (!numbytes)
891 goto err_out;
892 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
893 || chall_response != challenge_nr)
894 goto err_out;
895 /* auth successful, send 'Proceed' message */
896 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
897 sprintf(buf, "%s", PROCEED_MSG);
898 if (use_rc4) {
899 init_rc4_keys();
900 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
901 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
902 if (ret <= 0)
903 goto err_out;
904 numbytes = ret + strlen(PROCEED_MSG) + 1;
905 } else
906 numbytes = strlen(buf);
907 ret = send_bin_buffer(fd, buf, numbytes);
908 if (ret < 0)
909 goto err_out;
910 if (use_rc4)
911 enable_crypt(fd, rc4_recv, rc4_send, NULL);
912 ret = read_command(fd, &command);
913 if (ret < 0)
914 goto err_out;
915 ret = -E_BAD_CMD;
916 cmd = parse_cmd(command);
917 if (!cmd)
918 goto err_out;
919 /* valid command, check permissions */
920 ret = check_perms(u->perms, cmd);
921 if (ret < 0)
922 goto err_out;
923 /* valid command and sufficient perms */
924 alarm(0);
925 argc = split_args(command, &argv, "\n");
926 mmd_lock();
927 mmd->num_commands++;
928 mmd_unlock();
929 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
930 inet_ntoa(addr->sin_addr));
931 ret = cmd->handler(fd, argc, argv);
932 if (ret >= 0) {
933 ret = EXIT_SUCCESS;
934 goto out;
935 }
936 err_out:
937 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
938 if (ret != -E_SEND && ret != -E_RECV)
939 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
940 ret = EXIT_FAILURE;
941 out:
942 free(command);
943 free(argv);
944 mmd_lock();
945 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
946 mmd->events++;
947 mmd->active_connections--;
948 mmd_unlock();
949 return ret;
950 }