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