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