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