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