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