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