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