2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file command.c does client authentication and executes server commands */
21 #include <sys/time.h> /* gettimeofday */
23 #include "server.cmdline.h"
29 #include <openssl/rc4.h>
35 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata,
36 unsigned char *outdata) = NULL;
37 void (*crypt_function_send)(unsigned long len, const unsigned char *indata,
38 unsigned char *outdata) = NULL;
39 static RC4_KEY rc4_recv_key;
40 static RC4_KEY rc4_send_key;
41 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
43 extern const char *status_item_list[NUM_STAT_ITEMS];
44 extern struct misc_meta_data *mmd;
45 extern struct gengetopt_args_info conf;
46 extern struct audio_file_selector selectors[];
47 extern struct sender senders[];
48 extern char *user_list;
49 struct sockaddr_in *in_addr;
51 static int com_si(int, int, char **);
52 static int com_version(int, int, char **);
53 static int com_sb(int, int, char **);
54 static int com_sc(int, int, char **);
55 static int com_stat(int, int, char **);
56 static int com_help(int, int, char **);
57 static int com_hup(int, int, char **);
58 static int com_term(int, int, char **);
59 static int com_play(int, int, char **);
60 static int com_stop(int, int, char **);
61 static int com_pause(int, int, char **);
62 static int com_next(int, int, char **);
63 static int com_nomore(int, int, char **);
64 static int com_chs(int, int, char **);
65 static int com_ff(int, int, char **);
66 static int com_jmp(int, int, char **);
67 static int com_sender(int, int, char **);
70 /* commands that are handled by the server itself */
71 static struct server_command cmd_struct[] = {
75 .perms = DB_READ | DB_WRITE,
76 .description = "change the current audio file selector",
77 .synopsis = "chs [new_selector]",
79 "Shutdown the current selector and activate new_selector. If no\n"
80 "argument was given, print the name of the current selector.\n"
86 .perms = AFS_READ | AFS_WRITE,
87 .description = "jmp amount of time forwards or backwards "
88 "in current audio file",
89 .synopsis = "ff n[-]",
92 "\tSet the 'R' (reposition request) bit of the afs status flags\n"
93 "\tand enqueue a request to jump n seconds forwards or backwards\n"
94 "\tin the current audio file.\n"
100 "\tjumps 30 seconds backwards.\n"
108 .description = "print help text",
109 .synopsis = "help [command]",
112 "Without any arguments, help prints a list of availible commands. When\n"
113 "issued with a command name as first argument, print out a description\n"
114 "for that command.\n"
122 .description = "force reload of config file and log file",
126 "After rereading the config file, a signal is sent to all children\n"
127 "which forces them to close/reopen the log file.\n"
134 .perms = AFS_READ | AFS_WRITE,
135 .description = "jmp to given position in current audio file",
136 .synopsis = "jmp [n]",
139 "\tSet the 'R' (reposition request) bit of the afs status flags\n"
140 "\tand enqueue a request to jump to n% of the current audio file,\n"
141 "\twhere 0 <= n <= 100.\n"
148 .perms = AFS_READ | AFS_WRITE,
149 .description = "skip rest of current audio file",
153 "\tSet the 'N' (next audio file) bit of the afs status flags. When\n"
154 "\tplaying, change audio file immediately. Equivalent to stop\n"
155 "\tif paused, NOP if stopped.\n"
162 .handler = com_nomore,
163 .perms = AFS_READ | AFS_WRITE,
164 .description = "stop playing after current audio file",
165 .synopsis = "nomore",
168 "Set the 'O' (no more) bit of the afs status flags. This instructs\n"
169 "para_server to clear the 'P' (playing) bit as soon as it encounters\n"
170 "the 'N' (next audio file) bit being set.\n"
172 "Use this command instead of stop if you don't like\n"
179 .handler = com_pause,
180 .perms = AFS_READ | AFS_WRITE,
181 .description = "pause current audio file",
185 "\tClear the 'P' (playing) bit of the afs status flags.\n"
192 .perms = AFS_READ | AFS_WRITE,
193 .description = "start playing or resume playing when paused",
197 "\tSet the 'P' (playing) bit of the afs status flags. This\n"
198 "\tresults in starting/continuing to stream.\n"
206 .description = "print status bar for current audio file",
207 .synopsis = "sb [n]",
210 "Without any arguments, sb continuously prints a status bar of the form\n"
212 " 12:34 [56:12] (56%) filename\n"
214 "indicating playing time, remaining time, percentage and the name of\n"
215 "the file beeing streamed. Use the optional number n to let stat exit\n"
216 "after having displayed the status bar n times.\n"
223 .description = "print name of audio file whenever it changes",
224 .synopsis = "sc [n]",
227 "\tsc prints exactly one line (the filename of the audio file\n"
228 "\tbeing played) whenever the audio file changes. Stops after\n"
229 "\tn iterations, or never if n is not specified.\n"
234 .handler = com_sender,
235 .perms = AFS_READ | AFS_WRITE,
236 .description = "control paraslash internal senders",
237 .synopsis = "sender [s cmd [arguments]]",
240 "send command cmd to sender s. cmd may be one of the following:\n"
241 "help, on, off, add, delete, allow, or deny. Note that not all senders\n"
242 "support each command. Try e.g. 'para_client sender http help' for\n"
243 "more information about the http sender. If no argument is given,\n"
244 "print out a list of all senders that are compiled in.\n"
251 .description = "print server info",
254 "Print server uptime and other information.\n"
261 .description = "print status info for current audio file",
262 .synopsis = "stat [n]",
265 "\tWithout any arguments, stat continuously prints status messages\n"
266 "\tof the audio file being streamed. Use the optional number n\n"
267 "\tto let stat exit after having displayed status n times.\n"
274 .perms = AFS_READ | AFS_WRITE,
275 .description = "stop playing",
279 "\tClear the 'P' (play) bit and set the 'N' bit of the afs status\n"
286 .perms = AFS_READ | AFS_WRITE,
287 .description = "terminate para_server",
291 "Shuts down the server. Instead of this command, you can also send\n"
292 "SIGINT or SIGTERM. It should never be necessary to send SIGKILL.\n"
297 .handler = com_version,
299 .description = "print server's version",
300 .synopsis = "version",
302 "Show version and other info\n"
304 /* this indicates the end of the list. Do not touch. */
310 static void dummy(__a_unused int s)
313 static void mmd_dup(struct misc_meta_data *new_mmd)
321 * compute human readable string containing
322 * afs_status for given integer value
324 static char *afs_status_tohuman(unsigned int flags)
326 if (flags & AFS_PLAYING)
327 return para_strdup("playing");
328 else if (flags & AFS_NEXT)
329 return para_strdup("stopped");
331 return para_strdup("paused");
335 * return human readable permission string. Never returns NULL.
337 char *cmd_perms_itohuman(unsigned int perms)
339 char *msg = para_malloc(7 * sizeof(char));
341 msg[0] = perms & DB_READ? 'd' : '-';
342 msg[1] = perms & DB_WRITE? 'D' : '-';
343 msg[2] = perms & AFS_READ? 'a' : '-';
344 msg[3] = perms & AFS_WRITE? 'A' : '-';
350 * Never returns NULL.
352 static char *afs_get_status_flags(unsigned int flags)
354 char *msg = para_malloc(5 * sizeof(char));
356 msg[0] = (flags & AFS_PLAYING)? 'P' : '_';
357 msg[1] = (flags & AFS_NOMORE)? 'O' : '_';
358 msg[2] = (flags & AFS_NEXT)? 'N' : '_';
359 msg[3] = (flags & AFS_REPOS)? 'R' : '_';
365 * compute status bar string. Never returns NULL
367 char *get_sb_string(struct misc_meta_data *nmmd)
370 long long unsigned secs = 0, rsecs = 0, percent = 0;
372 base = para_basename(nmmd->filename);
374 return para_strdup("");
377 if (nmmd->chunks_total) {
378 secs = (long long) nmmd->seconds_total * nmmd->chunks_sent
379 / nmmd->chunks_total;
380 rsecs = (long long) nmmd->seconds_total *
381 (nmmd->chunks_total - nmmd->chunks_sent)
382 / nmmd->chunks_total;
383 percent = 100 * ((nmmd->chunks_sent + 5) / 10)
384 / ((nmmd->chunks_total + 5) / 10);
386 ret = make_message("%llu:%02llu [%llu:%02llu] (%llu%%) %s",
387 secs / 60, secs % 60,
388 rsecs / 60, rsecs % 60,
396 static char *get_status(struct misc_meta_data *nmmd)
398 char *bar, *ret, mtime[30] = "";
399 char *status, *flags; /* afs status info */
400 char *ut = uptime_str();
401 long offset = (nmmd->offset + 500) / 1000;
405 if (nmmd->audio_format >= 0) {
406 localtime_r(&nmmd->mtime, &mtime_tm);
407 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
409 /* report real status */
410 status = afs_status_tohuman(nmmd->afs_status_flags);
411 flags = afs_get_status_flags(nmmd->afs_status_flags);
412 bar = para_basename(nmmd->filename);
413 gettimeofday(&now, NULL);
415 "%s:%lu\n" "%s:%s\n" "%s:%i\n" "%s:%u\n"
416 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%s\n"
417 "%s:%li\n" "%s:%s\n" "%s" "%s"
418 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n",
419 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
420 status_item_list[SI_MTIME], mtime,
421 status_item_list[SI_LENGTH], nmmd->seconds_total,
422 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
424 status_item_list[SI_STATUS_BAR], bar ? bar : "(none)",
425 status_item_list[SI_STATUS], status,
426 status_item_list[SI_STATUS_FLAGS], flags,
427 status_item_list[SI_SELECTOR], selectors[nmmd->selector_num].name,
429 status_item_list[SI_OFFSET], offset,
430 status_item_list[SI_FORMAT], audio_format_name(nmmd->audio_format),
432 nmmd->audio_file_info,
434 status_item_list[SI_UPTIME], ut,
435 status_item_list[SI_STREAM_START],
436 (long unsigned)nmmd->stream_start.tv_sec,
437 (long unsigned)nmmd->stream_start.tv_usec,
438 status_item_list[SI_CURRENT_TIME],
439 (long unsigned)now.tv_sec,
440 (long unsigned)now.tv_usec
450 static int check_sender_args(int argc, char **argv, struct sender_command_data *scd)
453 /* this has to match sender.h */
454 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
456 scd->sender_num = -1;
458 return -E_COMMAND_SYNTAX;
459 for (i = 0; senders[i].name; i++)
460 if (!strcmp(senders[i].name, argv[1]))
462 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
463 if (!senders[i].name)
464 return -E_COMMAND_SYNTAX;
466 for (i = 0; subcmds[i]; i++)
467 if (!strcmp(subcmds[i], argv[2]))
470 return -E_COMMAND_SYNTAX;
473 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
475 return -E_SENDER_CMD;
478 switch (scd->cmd_num) {
482 return -E_COMMAND_SYNTAX;
486 if (argc != 4 && argc != 5)
487 return -E_COMMAND_SYNTAX;
488 if (!inet_aton(argv[3], &scd->addr))
489 return -E_COMMAND_SYNTAX;
492 scd->netmask = atoi(argv[4]);
493 if (scd->netmask < 0 || scd->netmask > 32)
494 return -E_COMMAND_SYNTAX;
499 if (argc != 4 && argc != 5)
500 return -E_COMMAND_SYNTAX;
501 if (!inet_aton(argv[3], &scd->addr))
502 return -E_COMMAND_SYNTAX;
505 scd->port = atoi(argv[4]);
506 if (scd->port < 0 || scd->port > 65535)
507 return -E_COMMAND_SYNTAX;
511 return -E_COMMAND_SYNTAX;
516 static int com_sender(int fd, int argc, char **argv)
519 struct sender_command_data scd;
523 for (i = 0; senders[i].name; i++) {
524 char *tmp = make_message("%s%s\n",
525 msg? msg : "", senders[i].name);
529 ret = send_buffer(fd, msg);
533 ret = check_sender_args(argc, argv, &scd);
536 if (scd.sender_num < 0)
538 msg = senders[scd.sender_num].help();
539 send_buffer(fd, msg);
543 for (i = 0; i < 10; i++) {
545 if (mmd->sender_cmd_data.cmd_num >= 0) {
550 mmd->sender_cmd_data = scd;
554 return (i < 10)? 1 : -E_LOCK;
558 static int com_si(int fd, int argc, __a_unused char **argv)
562 char *selector_string = NULL, *sender_info = NULL, *sender_list = NULL;
565 return -E_COMMAND_SYNTAX;
567 for (i = 0; selectors[i].name; i++) {
568 selector_string = para_strcat(selector_string, selectors[i].name);
569 selector_string = para_strcat(selector_string, " ");
571 for (i = 0; senders[i].name; i++) {
572 char *info = senders[i].info();
573 sender_info = para_strcat(sender_info, info);
575 sender_list = para_strcat(sender_list, senders[i].name);
576 sender_list = para_strcat(sender_list, " ");
579 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
581 "connections (active/accepted/total): %u/%u/%u\n"
582 "current loglevel: %i\n"
583 "supported audio file selectors: %s\n"
584 "supported audio formats: %s\n"
585 "supported senders: %s\n"
589 mmd->active_connections,
594 supported_audio_formats(),
600 free(selector_string);
607 static int com_version(int socket_fd, int argc, __a_unused char **argv)
610 return -E_COMMAND_SYNTAX;
611 return send_buffer(socket_fd, "para_server-" VERSION ", \"" CODENAME "\"\n"
613 "built: " BUILD_DATE "\n"
614 SYSTEM ", " CC_VERSION "\n"
619 static int com_sc(int socket_fd, int argc, char **argv)
622 int ret, old = 0, count = -1; /* print af change forever */
625 count = atoi(argv[1]);
628 if (old != mmd->num_played) {
629 old = mmd->num_played;
630 name = para_strdup(mmd->filename);
634 ret = send_va_buffer(socket_fd, "%s\n", name);
639 if (argc > 1 && !--count)
647 static int com_sb(int socket_fd, int argc, char **argv)
650 int ret, nr = -1; /* status bar will be printed that many
651 * times. Negative value means: print
658 sb = get_sb_string(mmd);
660 ret = send_va_buffer(socket_fd, "%s\n", sb);
674 static int com_stat(int socket_fd, int argc, char **argv)
676 int ret, num = 0;/* status will be printed that many
677 * times. num <= 0 means: print forever
679 struct misc_meta_data tmp, *nmmd = &tmp;
682 signal(SIGUSR1, dummy);
689 s = get_status(nmmd);
690 ret = send_buffer(socket_fd, s);
697 usleep(500000 * 100);
703 static int send_description(int fd, struct server_command *cmd, const char *handler, int num)
707 for (i = 1; cmd->name && (!num || i <= num); cmd++, i++) {
708 char *perms = cmd_perms_itohuman(cmd->perms);
709 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
720 /* always returns string that must be freed by the caller in handler */
721 static struct server_command *get_cmd_ptr(char *name, char **handler)
723 struct server_command *cmd = cmd_struct;
725 for (cmd = cmd_struct; cmd->name; cmd++)
726 if (!strcmp(cmd->name, name)) {
728 *handler = para_strdup("para_server"); /* server commands */
731 /* not found, look for commands supported by the current selector */
734 *handler = make_message("the %s selector",
735 selectors[mmd->selector_num].name);
736 cmd = selectors[mmd->selector_num].cmd_list;
738 for (; cmd->name; cmd++)
739 if (!strcmp(cmd->name, name))
745 static int com_help(int fd, int argc, char **argv)
747 struct server_command *cmd;
748 char *perms, *handler;
752 /* no argument given, print list of commands */
753 if ((ret = send_description(fd, cmd_struct, "server", 0)) < 0)
756 handler = para_strdup(selectors[mmd->selector_num].name);
757 cmd = selectors[mmd->selector_num].cmd_list;
759 ret = send_description(fd, cmd, handler, 0);
763 /* argument given for help */
764 cmd = get_cmd_ptr(argv[1], &handler);
769 perms = cmd_perms_itohuman(cmd->perms);
770 ret = send_va_buffer(fd,
772 "SYNOPSIS\n\t para_client %s\n"
775 "This command is handled by %s.\n\n"
777 "Needed privileges for %s: %s\n",
792 static int com_hup(__a_unused int socket_fd, int argc, __a_unused char **argv)
795 return -E_COMMAND_SYNTAX;
796 kill(getppid(), SIGHUP);
801 static int com_term(__a_unused int socket_fd, int argc, __a_unused char **argv)
804 return -E_COMMAND_SYNTAX;
805 kill(getppid(), SIGTERM);
809 static int com_play(__a_unused int socket_fd, int argc, __a_unused char **argv)
812 return -E_COMMAND_SYNTAX;
814 mmd->new_afs_status_flags |= AFS_PLAYING;
815 mmd->new_afs_status_flags &= ~AFS_NOMORE;
822 static int com_stop(__a_unused int socket_fd, int argc, __a_unused char **argv)
825 return -E_COMMAND_SYNTAX;
827 mmd->new_afs_status_flags &= ~AFS_PLAYING;
828 mmd->new_afs_status_flags &= ~AFS_REPOS;
829 mmd->new_afs_status_flags |= AFS_NEXT;
835 static int com_pause(__a_unused int socket_fd, int argc, __a_unused char **argv)
838 return -E_COMMAND_SYNTAX;
842 mmd->new_afs_status_flags &= ~AFS_PLAYING;
843 mmd->new_afs_status_flags &= ~AFS_NEXT;
848 static int com_chs(int fd, int argc, char **argv)
855 selector = para_strdup(selectors[mmd->selector_num].name);
857 ret = send_va_buffer(fd, "%s\n", selector);
861 for (i = 0; selectors[i].name; i++) {
862 if (strcmp(selectors[i].name, argv[1]))
865 mmd->selector_change = i;
870 return -E_BAD_SELECTOR;
874 static int com_next(__a_unused int socket_fd, int argc, __a_unused char **argv)
877 return -E_COMMAND_SYNTAX;
880 mmd->new_afs_status_flags |= AFS_NEXT;
886 static int com_nomore(__a_unused int socket_fd, int argc, __a_unused char **argv)
889 return -E_COMMAND_SYNTAX;
891 if (afs_playing() || afs_paused())
892 mmd->new_afs_status_flags |= AFS_NOMORE;
898 static int com_ff(__a_unused int socket_fd, int argc, char **argv)
901 int ret, backwards = 0;
906 return -E_COMMAND_SYNTAX;
907 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
908 return -E_COMMAND_SYNTAX;
909 if (ret > 1 && c == '-')
910 backwards = 1; /* jmp backwards */
912 ret = -E_NO_AUDIO_FILE;
913 if (!mmd->chunks_total || !mmd->seconds_total)
915 promille = (1000 * mmd->current_chunk) / mmd->chunks_total;
917 promille -= 1000 * i / mmd->seconds_total;
919 promille += 1000 * i / mmd->seconds_total;
922 if (promille > 1000) {
923 mmd->new_afs_status_flags |= AFS_NEXT;
926 mmd->repos_request = (mmd->chunks_total * promille) / 1000;
927 mmd->new_afs_status_flags |= AFS_REPOS;
928 mmd->new_afs_status_flags &= ~AFS_NEXT;
937 static int com_jmp(__a_unused int socket_fd, int argc, char **argv)
943 return -E_COMMAND_SYNTAX;
944 if (sscanf(argv[1], "%lu", &i) <= 0)
945 return -E_COMMAND_SYNTAX;
947 ret = -E_NO_AUDIO_FILE;
948 if (!mmd->chunks_total)
952 PARA_INFO_LOG("jumping to %lu%%\n", i);
953 mmd->repos_request = (mmd->chunks_total * i + 50)/ 100;
954 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
955 mmd->chunks_sent, mmd->offset);
956 mmd->new_afs_status_flags |= AFS_REPOS;
957 mmd->new_afs_status_flags &= ~AFS_NEXT;
966 * check if perms are sufficient to exec a command having perms cmd_perms.
967 * Returns 0 if perms are sufficient, -E_PERM otherwise.
969 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
971 PARA_DEBUG_LOG("%s", "checking permissions\n");
972 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
976 * Parse first string from *cmd and lookup in table of valid commands.
977 * On error, NULL is returned.
979 static struct server_command *parse_cmd(const char *cmdstr)
984 sscanf(cmdstr, "%200s%n", buf, &n);
988 return get_cmd_ptr(buf, NULL);
991 long int para_rand(long unsigned max)
993 return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
996 /* Open user_list file, returns pointer to opened file on success,
999 static FILE *open_user_list(char *file)
1001 PARA_DEBUG_LOG("opening user list %s\n", file);
1002 return fopen(file, "r");
1006 * lookup user in user_list file. Fills in a user struct containing
1007 * filename of the user's public key as well as the permissions of that user.
1008 * Returns 1 on success, 0 if user does not exist and < 0 on errors.
1010 static int get_user(struct user *user) {
1014 /* keyword, user, key, perms */
1015 char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE];
1018 file_ptr = open_user_list(user_list);
1021 while (fgets(line, MAXLINE, file_ptr)) {
1022 // PARA_DEBUG_LOG("%s: Read line (%i bytes) "
1023 // "from config file\n", __func__, strlen(line));
1024 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
1026 if (!strcmp(w, "user") && !strcmp(user->name, n)) {
1027 PARA_DEBUG_LOG("found entry for %s\n", n);
1028 strcpy(user->name, n);
1029 strcpy(user->pubkey_file, k);
1032 num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
1033 tmp[0], tmp[1], tmp[2], tmp[3]);
1034 PARA_DEBUG_LOG("found %i perm entries\n",
1039 //PARA_DEBUG_LOG("%s: tmp[%i]=%s\n", __func__,
1041 if (!strcmp(tmp[num], "AFS_READ"))
1043 user->perms | AFS_READ;
1044 else if (!strcmp(tmp[num], "AFS_WRITE"))
1046 user->perms | AFS_WRITE;
1047 else if (!strcmp(tmp[num], "DB_READ"))
1048 user->perms = user->perms | DB_READ;
1049 else if (!strcmp(tmp[num], "DB_WRITE"))
1050 user->perms = user->perms | DB_WRITE;
1051 else /* unknown permission */
1052 PARA_WARNING_LOG("unknown permission:"
1063 static void init_rc4_keys(void)
1067 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
1068 rc4_buf[i] = para_rand(256);
1069 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
1070 (unsigned char) rc4_buf[0],
1071 (unsigned char) rc4_buf[RC4_KEY_LEN]);
1072 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
1073 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
1076 static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
1078 RC4(&rc4_recv_key, len, indata, outdata);
1081 static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
1083 RC4(&rc4_send_key, len, indata, outdata);
1086 int handle_connect(int fd, struct sockaddr_in *addr)
1088 int numbytes, ret, argc, use_rc4 = 0;
1089 char buf[STRINGSIZE];
1090 unsigned char crypt_buf[MAXLINE];
1092 struct server_command *cmd = NULL;
1093 long unsigned challenge_nr, chall_response;
1095 char *p, *command = NULL;
1097 signal(SIGCHLD, SIG_IGN);
1098 signal(SIGINT, SIG_DFL);
1099 signal(SIGTERM, SIG_DFL);
1100 signal(SIGHUP, SIG_DFL);
1101 signal(SIGUSR1, SIG_IGN);
1104 challenge_nr = random();
1105 /* send Welcome message */
1106 ret = send_va_buffer(fd, "This is para_server, version " VERSION ".\n" );
1109 /* recv auth request line */
1110 ret = recv_buffer(fd, buf, sizeof(buf));
1119 if (strncmp(buf, "auth ", 5))
1122 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
1123 strcpy(u.name, buf + 5); /* client version < 0.2.6 */
1125 strcpy(u.name, buf + 9); /* client version >= 0.2.6 */
1128 // strcpy(u.name, buf + 5); /* ok, but ugly */
1129 PARA_DEBUG_LOG("received %s request for user %s\n",
1130 use_rc4? "rc4" : "auth", u.name);
1131 /* lookup user in list file */
1132 if ((ret = get_user(&u)) < 0)
1134 if (!ret) { /* user not found */
1135 PARA_WARNING_LOG("auth request for unknown user %s\n", u.name);
1139 ret = para_encrypt_challenge(u.pubkey_file, challenge_nr, crypt_buf);
1143 PARA_DEBUG_LOG("sending %d byte challenge\n", numbytes);
1144 /* We can't use send_buffer here since buf may contain null bytes */
1145 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
1148 /* recv decrypted number */
1149 numbytes = recv_buffer(fd, buf, sizeof(buf));
1156 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
1157 || chall_response != challenge_nr)
1159 /* auth successful. Send 'Proceed' message */
1160 PARA_INFO_LOG("good auth for %s (%lu)\n", u.name, challenge_nr);
1161 sprintf(buf, "%s", PROCEED_MSG);
1164 ret = para_encrypt_buffer(u.pubkey_file, rc4_buf, 2 * RC4_KEY_LEN,
1165 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
1168 numbytes = ret + strlen(PROCEED_MSG) + 1;
1170 numbytes = strlen(buf);
1171 ret = send_bin_buffer(fd, buf, numbytes);
1175 crypt_function_recv = rc4_recv;
1176 crypt_function_send = rc4_send;
1177 PARA_INFO_LOG("%s", "rc4 encryption activated\n");
1180 while ((numbytes = recv_buffer(fd, buf, sizeof(buf))) > 0) {
1181 // PARA_INFO_LOG("recvd: %s (%d)\n", buf, numbytes);
1182 ret = -E_COMMAND_SYNTAX;
1183 if (command && numbytes + strlen(command) > STRINGSIZE) /* DOS */
1185 command = para_strcat(command, buf);
1186 if ((p = strstr(command, EOC_MSG))) {
1196 if (!(cmd = parse_cmd(command)))
1198 /* valid command, check permissions */
1199 ret = check_perms(u.perms, cmd);
1202 /* valid command and sufficient perms */
1204 argc = split_args(command, &argv, "\n");
1206 mmd->num_commands++;
1208 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u.name,
1209 inet_ntoa(addr->sin_addr));
1210 ret = cmd->handler(fd, argc, argv);
1216 if (ret != -E_SEND && ret != -E_RECV) {
1217 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
1218 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
1225 if (cmd && (cmd->perms & DB_WRITE) && ret >= 0)
1227 mmd->active_connections--;