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