Remove users of the 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 "afs_common.h"
19 #include "string.h"
20 #include "afh.h"
21 #include "afs.h"
22 #include "server.h"
23 #include "vss.h"
24 #include "send.h"
25 #include "rc4.h"
26 #include "net.h"
27 #include "daemon.h"
28 #include "fd.h"
29 #include "list.h"
30 #include "user_list.h"
31 #include "server_command_list.h"
32 #include "afs_command_list.h"
33
34 /** commands including options must be shorter than this */
35 #define MAX_COMMAND_LEN 32768
36
37 static RC4_KEY rc4_recv_key;
38 static RC4_KEY rc4_send_key;
39 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
40
41 extern const char *status_item_list[NUM_STAT_ITEMS];
42 extern struct misc_meta_data *mmd;
43 extern struct 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"
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], "FIXME",
163
164 status_item_list[SI_OFFSET], offset,
165 status_item_list[SI_FORMAT], audio_format_name(nmmd->audio_format),
166 nmmd->afi.info_string,
167
168 status_item_list[SI_UPTIME], ut,
169 status_item_list[SI_STREAM_START],
170 (long unsigned)nmmd->stream_start.tv_sec,
171 (long unsigned)nmmd->stream_start.tv_usec,
172 status_item_list[SI_CURRENT_TIME],
173 (long unsigned)now.tv_sec,
174 (long unsigned)now.tv_usec
175
176 );
177 free(bar);
178 free(flags);
179 free(status);
180 free(ut);
181 return ret;
182 }
183
184 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
185 {
186 int i;
187 /* this has to match sender.h */
188 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
189
190 scd->sender_num = -1;
191 if (argc < 2)
192 return -E_COMMAND_SYNTAX;
193 for (i = 0; senders[i].name; i++)
194 if (!strcmp(senders[i].name, argv[1]))
195 break;
196 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
197 if (!senders[i].name)
198 return -E_COMMAND_SYNTAX;
199 scd->sender_num = i;
200 for (i = 0; subcmds[i]; i++)
201 if (!strcmp(subcmds[i], argv[2]))
202 break;
203 if (!subcmds[i])
204 return -E_COMMAND_SYNTAX;
205 scd->cmd_num = i;
206 mmd_lock();
207 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
208 mmd_unlock();
209 return -E_SENDER_CMD;
210 }
211 mmd_unlock();
212 switch (scd->cmd_num) {
213 case SENDER_ON:
214 case SENDER_OFF:
215 if (argc != 3)
216 return -E_COMMAND_SYNTAX;
217 break;
218 case SENDER_DENY:
219 case SENDER_ALLOW:
220 if (argc != 4 && argc != 5)
221 return -E_COMMAND_SYNTAX;
222 if (!inet_pton(AF_INET, argv[3], &scd->addr))
223 return -E_COMMAND_SYNTAX;
224 scd->netmask = 32;
225 if (argc == 5) {
226 scd->netmask = atoi(argv[4]);
227 if (scd->netmask < 0 || scd->netmask > 32)
228 return -E_COMMAND_SYNTAX;
229 }
230 break;
231 case SENDER_ADD:
232 case SENDER_DELETE:
233 if (argc != 4 && argc != 5)
234 return -E_COMMAND_SYNTAX;
235 if (!inet_pton(AF_INET, argv[3], &scd->addr))
236 return -E_COMMAND_SYNTAX;
237 scd->port = -1;
238 if (argc == 5) {
239 scd->port = atoi(argv[4]);
240 if (scd->port < 0 || scd->port > 65535)
241 return -E_COMMAND_SYNTAX;
242 }
243 break;
244 default:
245 return -E_COMMAND_SYNTAX;
246 }
247 return 1;
248 }
249
250 int com_sender(int fd, int argc, char * const * argv)
251 {
252 int i, ret;
253 struct sender_command_data scd;
254
255 if (argc < 2) {
256 char *msg = NULL;
257 for (i = 0; senders[i].name; i++) {
258 char *tmp = make_message("%s%s\n",
259 msg? msg : "", senders[i].name);
260 free(msg);
261 msg = tmp;
262 }
263 ret = send_buffer(fd, msg);
264 free(msg);
265 return ret;
266 }
267 ret = check_sender_args(argc, argv, &scd);
268 if (ret < 0) {
269 char *msg;
270 if (scd.sender_num < 0)
271 return ret;
272 msg = senders[scd.sender_num].help();
273 send_buffer(fd, msg);
274 free(msg);
275 return 1;
276 }
277 for (i = 0; i < 10; i++) {
278 mmd_lock();
279 if (mmd->sender_cmd_data.cmd_num >= 0) {
280 mmd_unlock();
281 usleep(100 * 1000);
282 continue;
283 }
284 mmd->sender_cmd_data = scd;
285 mmd_unlock();
286 break;
287 }
288 return (i < 10)? 1 : -E_LOCK;
289 }
290
291 /* server info */
292 int com_si(int fd, int argc, __a_unused char * const * argv)
293 {
294 int i, ret;
295 char *ut;
296 char *sender_info = NULL, *sender_list = NULL;
297
298 if (argc != 1)
299 return -E_COMMAND_SYNTAX;
300 mmd_lock();
301 for (i = 0; senders[i].name; i++) {
302 char *info = senders[i].info();
303 sender_info = para_strcat(sender_info, info);
304 free(info);
305 sender_list = para_strcat(sender_list, senders[i].name);
306 sender_list = para_strcat(sender_list, " ");
307 }
308 ut = uptime_str();
309 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
310 "pid: %d\n"
311 "connections (active/accepted/total): %u/%u/%u\n"
312 "current loglevel: %i\n"
313 "supported audio formats: %s\n"
314 "supported senders: %s\n"
315 "%s",
316 ut, mmd->num_played,
317 getppid(),
318 mmd->active_connections,
319 mmd->num_commands,
320 mmd->num_connects,
321 conf.loglevel_arg,
322 supported_audio_formats(),
323 sender_list,
324 sender_info
325 );
326 mmd_unlock();
327 free(ut);
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 * const * 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 * const * 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 * const * 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 * const * 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(const 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 afs */
461 for (cmd = afs_cmds; cmd->name; cmd++)
462 if (!strcmp(cmd->name, name))
463 return cmd;
464 return NULL;
465 }
466
467 /* help */
468 int com_help(int fd, int argc, char * const * argv)
469 {
470 struct server_command *cmd;
471 char *perms, *handler;
472 int ret;
473
474 if (argc < 2) {
475 /* no argument given, print list of commands */
476 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
477 return ret;
478 return send_list_of_commands(fd, afs_cmds, "afs");
479 }
480 /* argument given for help */
481 cmd = get_cmd_ptr(argv[1], &handler);
482 if (!cmd) {
483 free(handler);
484 return -E_BAD_CMD;
485 }
486 perms = cmd_perms_itohuman(cmd->perms);
487 ret = send_va_buffer(fd,
488 "%s - %s\n\n"
489 "handler: %s\n"
490 "permissions: %s\n"
491 "usage: %s\n\n"
492 "%s\n",
493 argv[1],
494 cmd->description,
495 handler,
496 perms,
497 cmd->usage,
498 cmd->help
499 );
500 free(perms);
501 free(handler);
502 return ret;
503 }
504
505 /* hup */
506 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
507 {
508 if (argc != 1)
509 return -E_COMMAND_SYNTAX;
510 kill(getppid(), SIGHUP);
511 return 1;
512 }
513
514 /* term */
515 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
516 {
517 if (argc != 1)
518 return -E_COMMAND_SYNTAX;
519 kill(getppid(), SIGTERM);
520 return 1;
521 }
522
523 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
524 {
525 if (argc != 1)
526 return -E_COMMAND_SYNTAX;
527 mmd_lock();
528 mmd->new_vss_status_flags |= VSS_PLAYING;
529 mmd->new_vss_status_flags &= ~VSS_NOMORE;
530 mmd_unlock();
531 return 1;
532
533 }
534
535 /* stop */
536 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
537 {
538 if (argc != 1)
539 return -E_COMMAND_SYNTAX;
540 mmd_lock();
541 mmd->new_vss_status_flags &= ~VSS_PLAYING;
542 mmd->new_vss_status_flags &= ~VSS_REPOS;
543 mmd->new_vss_status_flags |= VSS_NEXT;
544 mmd_unlock();
545 return 1;
546 }
547
548 /* pause */
549 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
550 {
551 if (argc != 1)
552 return -E_COMMAND_SYNTAX;
553 mmd_lock();
554 if (!vss_paused())
555 mmd->events++;
556 mmd->new_vss_status_flags &= ~VSS_PLAYING;
557 mmd->new_vss_status_flags &= ~VSS_NEXT;
558 mmd_unlock();
559 return 1;
560 }
561
562 /* next */
563 int com_next(__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->events++;
569 mmd->new_vss_status_flags |= VSS_NEXT;
570 mmd_unlock();
571 return 1;
572 }
573
574 /* nomore */
575 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
576 {
577 if (argc != 1)
578 return -E_COMMAND_SYNTAX;
579 mmd_lock();
580 if (vss_playing() || vss_paused())
581 mmd->new_vss_status_flags |= VSS_NOMORE;
582 mmd_unlock();
583 return 1;
584 }
585
586 /* ff */
587 int com_ff(__a_unused int fd, int argc, char * const * argv)
588 {
589 long promille;
590 int ret, backwards = 0;
591 unsigned i;
592 char c;
593
594 if (argc != 2)
595 return -E_COMMAND_SYNTAX;
596 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
597 return -E_COMMAND_SYNTAX;
598 if (ret > 1 && c == '-')
599 backwards = 1; /* jmp backwards */
600 mmd_lock();
601 ret = -E_NO_AUDIO_FILE;
602 if (!mmd->afi.chunks_total || !mmd->afi.seconds_total)
603 goto out;
604 promille = (1000 * mmd->current_chunk) / mmd->afi.chunks_total;
605 if (backwards)
606 promille -= 1000 * i / mmd->afi.seconds_total;
607 else
608 promille += 1000 * i / mmd->afi.seconds_total;
609 if (promille < 0)
610 promille = 0;
611 if (promille > 1000) {
612 mmd->new_vss_status_flags |= VSS_NEXT;
613 goto out;
614 }
615 mmd->repos_request = (mmd->afi.chunks_total * promille) / 1000;
616 mmd->new_vss_status_flags |= VSS_REPOS;
617 mmd->new_vss_status_flags &= ~VSS_NEXT;
618 mmd->events++;
619 ret = 1;
620 out:
621 mmd_unlock();
622 return ret;
623 }
624
625 /* jmp */
626 int com_jmp(__a_unused int fd, int argc, char * const * argv)
627 {
628 long unsigned int i;
629 int ret;
630
631 if (argc != 2)
632 return -E_COMMAND_SYNTAX;
633 if (sscanf(argv[1], "%lu", &i) <= 0)
634 return -E_COMMAND_SYNTAX;
635 mmd_lock();
636 ret = -E_NO_AUDIO_FILE;
637 if (!mmd->afi.chunks_total)
638 goto out;
639 if (i > 100)
640 i = 100;
641 PARA_INFO_LOG("jumping to %lu%%\n", i);
642 mmd->repos_request = (mmd->afi.chunks_total * i + 50)/ 100;
643 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
644 mmd->chunks_sent, mmd->offset);
645 mmd->new_vss_status_flags |= VSS_REPOS;
646 mmd->new_vss_status_flags &= ~VSS_NEXT;
647 ret = 1;
648 mmd->events++;
649 out:
650 mmd_unlock();
651 return ret;
652 }
653
654 /*
655 * check if perms are sufficient to exec a command having perms cmd_perms.
656 * Returns 0 if perms are sufficient, -E_PERM otherwise.
657 */
658 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
659 {
660 PARA_DEBUG_LOG("%s", "checking permissions\n");
661 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
662 }
663
664 /*
665 * Parse first string from *cmd and lookup in table of valid commands.
666 * On error, NULL is returned.
667 */
668 static struct server_command *parse_cmd(const char *cmdstr)
669 {
670 char buf[255];
671 int n = 0;
672
673 sscanf(cmdstr, "%200s%n", buf, &n);
674 if (!n)
675 return NULL;
676 buf[n] = '\0';
677 return get_cmd_ptr(buf, NULL);
678 }
679
680 static long int para_rand(long unsigned max)
681 {
682 return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
683 }
684
685 static void init_rc4_keys(void)
686 {
687 int i;
688
689 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
690 rc4_buf[i] = para_rand(256);
691 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
692 (unsigned char) rc4_buf[0],
693 (unsigned char) rc4_buf[RC4_KEY_LEN]);
694 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
695 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
696 }
697
698 static void rc4_recv(unsigned long len, const unsigned char *indata,
699 unsigned char *outdata, __a_unused void *private_data)
700 {
701 RC4(&rc4_recv_key, len, indata, outdata);
702 }
703
704 static void rc4_send(unsigned long len, const unsigned char *indata,
705 unsigned char *outdata, __a_unused void *private_data)
706 {
707 RC4(&rc4_send_key, len, indata, outdata);
708 }
709
710 static int read_command(int fd, char **result)
711 {
712 int ret;
713 char buf[4096];
714 char *command = NULL;
715
716 for (;;) {
717 size_t numbytes;
718 char *p;
719
720 ret = recv_buffer(fd, buf, sizeof(buf));
721 if (ret < 0)
722 goto out;
723 if (!ret)
724 break;
725 numbytes = ret;
726 ret = -E_COMMAND_SYNTAX;
727 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
728 goto out;
729 command = para_strcat(command, buf);
730 p = strstr(command, EOC_MSG);
731 if (p) {
732 *p = '\0';
733 break;
734 }
735 }
736 ret = command? 1 : -E_COMMAND_SYNTAX;
737 out:
738 if (ret < 0)
739 free(command);
740 else
741 *result = command;
742 return ret;
743
744 }
745
746 /**
747 * perform user authentication and execute a command
748 *
749 * \param fd the file descriptor to send output to
750 * \param addr socket address info of peer
751 *
752 * \return EXIT_SUCCESS or EXIT_FAILURE
753 *
754 * Whenever para_server accepts an incoming tcp connection on
755 * the port it listens on, it forks and the resulting child
756 * calls this function.
757 *
758 * An RSA-based challenge/response is used to authenticate
759 * the peer. It that authentication succeeds, a random RC4
760 * session key is generated and sent back to the peer,
761 * encrypted with its RSA public key. From this point on,
762 * all transfers are crypted with this session key.
763 *
764 * Next it is checked if the peer supplied a valid server command or a command
765 * for the audio file selector. If yes, and if the user has sufficient
766 * permissions to execute that command, the function calls the corresponding
767 * command handler which does argument checking and further processing.
768 *
769 * In order to cope with a DOS attacks, a timeout is set up
770 * which terminates the function if the connection was not
771 * authenticated when the timeout expires.
772 *
773 * \sa alarm(2), rc4(3), crypt.c, crypt.h
774 */
775 int handle_connect(int fd, struct sockaddr_in *addr)
776 {
777 int ret, argc, use_rc4 = 0;
778 char buf[4096];
779 unsigned char crypt_buf[MAXLINE];
780 struct user *u;
781 struct server_command *cmd = NULL;
782 long unsigned challenge_nr, chall_response;
783 char **argv = NULL;
784 char *p, *command = NULL;
785 size_t numbytes;
786
787 signal(SIGCHLD, SIG_IGN);
788 signal(SIGINT, SIG_DFL);
789 signal(SIGTERM, SIG_DFL);
790 signal(SIGHUP, SIG_DFL);
791 signal(SIGUSR1, SIG_IGN);
792
793 challenge_nr = random();
794 /* send Welcome message */
795 ret = send_va_buffer(fd, "This is para_server, version "
796 PACKAGE_VERSION ".\n" );
797 if (ret < 0)
798 goto err_out;
799 /* recv auth request line */
800 ret = recv_buffer(fd, buf, sizeof(buf));
801 if (ret < 0)
802 goto err_out;
803 if (ret <= 6) {
804 ret = -E_AUTH;
805 goto err_out;
806 }
807 numbytes = ret;
808 ret = -E_AUTH;
809 if (strncmp(buf, "auth ", 5))
810 goto err_out;
811
812 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
813 p = buf + 5; /* client version < 0.2.6 */
814 else {
815 p = buf + 9; /* client version >= 0.2.6 */
816 use_rc4 = 1;
817 }
818 PARA_DEBUG_LOG("received %s request for user %s\n",
819 use_rc4? "rc4" : "auth", p);
820 ret = -E_BAD_USER;
821 u = lookup_user(p);
822 if (!u)
823 goto err_out;
824 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
825 if (ret <= 0)
826 goto err_out;
827 numbytes = ret;
828 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
829 /* We can't use send_buffer here since buf may contain null bytes */
830 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
831 if (ret < 0)
832 goto err_out;
833 /* recv decrypted number */
834 ret = recv_buffer(fd, buf, sizeof(buf));
835 if (ret < 0)
836 goto err_out;
837 numbytes = ret;
838 ret = -E_AUTH;
839 if (!numbytes)
840 goto err_out;
841 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
842 || chall_response != challenge_nr)
843 goto err_out;
844 /* auth successful, send 'Proceed' message */
845 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
846 sprintf(buf, "%s", PROCEED_MSG);
847 if (use_rc4) {
848 init_rc4_keys();
849 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
850 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
851 if (ret <= 0)
852 goto err_out;
853 numbytes = ret + strlen(PROCEED_MSG) + 1;
854 } else
855 numbytes = strlen(buf);
856 ret = send_bin_buffer(fd, buf, numbytes);
857 if (ret < 0)
858 goto err_out;
859 if (use_rc4)
860 enable_crypt(fd, rc4_recv, rc4_send, NULL);
861 ret = read_command(fd, &command);
862 if (ret < 0)
863 goto err_out;
864 ret = -E_BAD_CMD;
865 cmd = parse_cmd(command);
866 if (!cmd)
867 goto err_out;
868 /* valid command, check permissions */
869 ret = check_perms(u->perms, cmd);
870 if (ret < 0)
871 goto err_out;
872 /* valid command and sufficient perms */
873 alarm(0);
874 argc = split_args(command, &argv, "\n");
875 mmd_lock();
876 mmd->num_commands++;
877 mmd_unlock();
878 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
879 inet_ntoa(addr->sin_addr));
880 ret = cmd->handler(fd, argc, argv);
881 if (ret >= 0) {
882 ret = EXIT_SUCCESS;
883 goto out;
884 }
885 err_out:
886 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
887 if (ret != -E_SEND && ret != -E_RECV)
888 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
889 ret = EXIT_FAILURE;
890 out:
891 free(command);
892 free(argv);
893 mmd_lock();
894 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
895 mmd->events++;
896 mmd->active_connections--;
897 mmd_unlock();
898 return ret;
899 }