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