52d56c9eba015880000db26e4183814434ad6f39
[paraslash.git] / command.c
1 /*
2  * Copyright (C) 1997-2006 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 "crypt.h"
23 #include "server.cmdline.h"
24 #include "db.h"
25 #include "server.h"
26 #include "afs.h"
27 #include "send.h"
28 #include "rc4.h"
29 #include <openssl/rc4.h>
30 #include "error.h"
31 #include "net.h"
32 #include "daemon.h"
33 #include "string.h"
34
35 static RC4_KEY rc4_recv_key;
36 static RC4_KEY rc4_send_key;
37 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
38
39 extern const char *status_item_list[NUM_STAT_ITEMS];
40 extern struct misc_meta_data *mmd;
41 extern struct audio_file_selector selectors[];
42 extern struct sender senders[];
43 extern char *user_list;
44 struct sockaddr_in *in_addr;
45
46 static int com_si(int, int, char **);
47 static int com_version(int, int, char **);
48 static int com_sb(int, int, char **);
49 static int com_sc(int, int, char **);
50 static int com_stat(int, int, char **);
51 static int com_help(int, int, char **);
52 static int com_hup(int, int, char **);
53 static int com_term(int, int, char **);
54 static int com_play(int, int, char **);
55 static int com_stop(int, int, char **);
56 static int com_pause(int, int, char **);
57 static int com_next(int, int, char **);
58 static int com_nomore(int, int, char **);
59 static int com_chs(int, int, char **);
60 static int com_ff(int, int, char **);
61 static int com_jmp(int, int, char **);
62 static int com_sender(int, int, char **);
63
64
65 /* commands that are handled by the server itself */
66 static struct server_command cmd_struct[] = {
67 {
68 .name = "chs",
69 .handler = com_chs,
70 .perms = DB_READ | DB_WRITE,
71 .description = "change the current audio file selector",
72 .synopsis = "chs [new_selector]",
73 .help =
74 "Shutdown the current selector and activate new_selector. If no\n"
75 "argument was given, print the name of the current selector.\n"
76 },
77
78 {
79 .name = "ff",
80 .handler = com_ff,
81 .perms = AFS_READ | AFS_WRITE,
82 .description = "jmp amount of time forwards or backwards "
83         "in current audio file",
84 .synopsis = "ff n[-]",
85 .help =
86
87 "\tSet the 'R' (reposition request) bit of the afs status flags\n"
88 "\tand enqueue a request to jump n seconds forwards or backwards\n"
89 "\tin the current audio file.\n"
90 "\n"
91 "EXAMPLE\n"
92 "\n"
93 "\t\tff 30-\n"
94 "\n"
95 "\tjumps 30 seconds backwards.\n"
96
97 },
98
99 {
100 .name = "help",
101 .handler = com_help,
102 .perms = 0,
103 .description = "print help text",
104 .synopsis = "help [command]",
105 .help =
106
107 "Without any arguments, help prints a list of availible commands. When\n"
108 "issued with a command name as first argument, print out a description\n"
109 "for that command.\n"
110
111 },
112
113 {
114 .name = "hup",
115 .handler = com_hup,
116 .perms = AFS_WRITE,
117 .description = "force reload of config file and log file",
118 .synopsis = "hup",
119 .help =
120
121 "After rereading the config file, a signal is sent to all children\n"
122 "which forces them to close/reopen the log file.\n"
123
124 },
125
126 {
127 .name = "jmp",
128 .handler = com_jmp,
129 .perms = AFS_READ | AFS_WRITE,
130 .description = "jmp to given position in current audio file",
131 .synopsis = "jmp [n]",
132 .help =
133
134 "\tSet the 'R' (reposition request) bit of the afs status flags\n"
135 "\tand enqueue a request to jump to n% of the current audio file,\n"
136 "\twhere 0 <= n <= 100.\n"
137
138 },
139
140 {
141 .name = "next",
142 .handler = com_next,
143 .perms = AFS_READ | AFS_WRITE,
144 .description = "skip rest of current audio file",
145 .synopsis = "next",
146 .help =
147
148 "\tSet the 'N' (next audio file) bit of the afs status flags. When\n"
149 "\tplaying, change audio file immediately. Equivalent to stop\n"
150 "\tif paused, NOP if stopped.\n"
151
152
153 },
154
155 {
156 .name = "nomore",
157 .handler = com_nomore,
158 .perms = AFS_READ | AFS_WRITE,
159 .description = "stop playing after current audio file",
160 .synopsis = "nomore",
161 .help =
162
163 "Set the 'O' (no more) bit of the afs status flags. This instructs\n"
164 "para_server to clear the 'P' (playing) bit as soon as it encounters\n"
165 "the 'N' (next audio file) bit being set.\n"
166 "\n"
167 "Use this command instead of stop if you don't like\n"
168 "sudden endings.\n"
169
170 },
171
172 {
173 .name ="pause",
174 .handler = com_pause,
175 .perms = AFS_READ | AFS_WRITE,
176 .description = "pause current audio file",
177 .synopsis = "pause",
178 .help =
179
180 "\tClear the 'P' (playing) bit of the afs status flags.\n"
181
182 },
183
184 {
185 .name = "play",
186 .handler = com_play,
187 .perms = AFS_READ | AFS_WRITE,
188 .description = "start playing or resume playing when paused",
189 .synopsis = "play",
190 .help =
191
192 "\tSet the 'P' (playing) bit of the afs status flags. This\n"
193 "\tresults in starting/continuing to stream.\n"
194
195 },
196
197 {
198 .name = "sb",
199 .handler = com_sb,
200 .perms = AFS_READ,
201 .description = "print status bar for current audio file",
202 .synopsis = "sb [n]",
203 .help =
204
205 "Without any arguments, sb continuously prints a status bar of the form\n"
206 "\n"
207 "       12:34 [56:12] (56%) filename\n"
208 "\n"
209 "indicating playing time, remaining time, percentage and the name of\n"
210 "the file beeing streamed. Use the optional number n to let stat exit\n"
211 "after having displayed the status bar n times.\n"
212
213 },
214 {
215 .name = "sc",
216 .handler = com_sc,
217 .perms = AFS_READ,
218 .description = "print name of audio file whenever it changes",
219 .synopsis = "sc [n]",
220 .help =
221
222 "\tsc prints exactly one line (the filename of the audio file\n"
223 "\tbeing played) whenever the audio file changes. Stops after\n"
224 "\tn iterations, or never if n is not specified.\n"
225
226 },
227 {
228 .name = "sender",
229 .handler = com_sender,
230 .perms = AFS_READ | AFS_WRITE,
231 .description = "control paraslash internal senders",
232 .synopsis = "sender [s cmd [arguments]]",
233 .help =
234
235 "send command cmd to sender s. cmd may be one of the following:\n"
236 "help, on, off, add, delete, allow, or deny. Note that not all senders\n"
237 "support each command. Try e.g. 'para_client sender http help' for\n"
238 "more information about the http sender. If no argument is given,\n"
239 "print out a list of all senders that are compiled in.\n"
240
241 },
242 {
243 .name = "si",
244 .handler = com_si,
245 .perms = 0,
246 .description = "print server info",
247 .synopsis = "si",
248 .help =
249 "Print server uptime and other information.\n"
250 },
251
252 {
253 .name = "stat",
254 .handler = com_stat,
255 .perms = AFS_READ,
256 .description = "print status info for current audio file",
257 .synopsis = "stat [n]",
258 .help =
259
260 "\tWithout any arguments, stat continuously prints status messages\n"
261 "\tof the audio file being streamed. Use the optional number n\n"
262 "\tto let stat exit after having displayed status n times.\n"
263
264 },
265
266 {
267 .name = "stop",
268 .handler = com_stop,
269 .perms = AFS_READ | AFS_WRITE,
270 .description = "stop playing",
271 .synopsis = "stop",
272 .help =
273
274 "\tClear the 'P' (play) bit and set the 'N' bit of the afs status\n"
275 "\tflags.\n"
276
277 },
278 {
279 .name = "term",
280 .handler = com_term,
281 .perms = AFS_READ | AFS_WRITE,
282 .description = "terminate para_server",
283 .synopsis = "term",
284 .help =
285
286 "Shuts down the server. Instead of this command, you can also send\n"
287 "SIGINT or SIGTERM. It should never be necessary to send SIGKILL.\n"
288
289 },
290 {
291 .name = "version",
292 .handler = com_version,
293 .perms = 0,
294 .description = "print server's version",
295 .synopsis = "version",
296 .help =
297 "Show version and other info\n"
298 },
299 /* this indicates the end of the list. Do not touch. */
300 {
301 .name = NULL,
302 }
303 };
304
305 static void dummy(__a_unused int s)
306 {}
307
308 static void mmd_dup(struct misc_meta_data *new_mmd)
309 {
310         mmd_lock();
311         *new_mmd = *mmd;
312         mmd_unlock();
313 }
314
315 /*
316  * compute human readable string containing
317  * afs_status for given integer value
318  */
319 static char *afs_status_tohuman(unsigned int flags)
320 {
321         if (flags & AFS_PLAYING)
322                 return para_strdup("playing");
323         else if (flags & AFS_NEXT)
324                 return para_strdup("stopped");
325         else
326                 return para_strdup("paused");
327 }
328
329 /*
330  * return human readable permission string. Never returns NULL.
331  */
332 char *cmd_perms_itohuman(unsigned int perms)
333 {
334         char *msg = para_malloc(7 * sizeof(char));
335
336         msg[0] = perms & DB_READ? 'd' : '-';
337         msg[1] = perms & DB_WRITE? 'D' : '-';
338         msg[2] = perms & AFS_READ? 'a' : '-';
339         msg[3] = perms & AFS_WRITE? 'A' : '-';
340         msg[4] = '\0';
341         return msg;
342 }
343
344 /*
345  * Never returns NULL.
346  */
347 static char *afs_get_status_flags(unsigned int flags)
348 {
349         char *msg = para_malloc(5 * sizeof(char));
350
351         msg[0] = (flags & AFS_PLAYING)? 'P' : '_';
352         msg[1] = (flags & AFS_NOMORE)? 'O' : '_';
353         msg[2] = (flags & AFS_NEXT)? 'N' : '_';
354         msg[3] = (flags & AFS_REPOS)? 'R' : '_';
355         msg[4] = '\0';
356         return msg;
357 }
358
359 /*
360  * compute status bar string. Never returns NULL
361  */
362 char *get_sb_string(struct misc_meta_data *nmmd)
363 {
364         char *base, *ret;
365         long long unsigned secs = 0, rsecs = 0, percent = 0;
366
367         base = para_basename(nmmd->filename);
368         if (!base)
369                 return para_strdup("");
370         if (!base[0])
371                 return base;
372         if (nmmd->chunks_total) {
373                 secs = (long long) nmmd->seconds_total * nmmd->chunks_sent
374                         / nmmd->chunks_total;
375                 rsecs = (long long) nmmd->seconds_total *
376                         (nmmd->chunks_total - nmmd->chunks_sent)
377                         / nmmd->chunks_total;
378                 percent = 100 * ((nmmd->chunks_sent + 5) / 10)
379                         / ((nmmd->chunks_total + 5) / 10);
380         }
381         ret = make_message("%llu:%02llu [%llu:%02llu] (%llu%%) %s",
382                 secs / 60, secs % 60,
383                 rsecs / 60, rsecs % 60,
384                 percent,
385                 base
386         );
387         free(base);
388         return ret;
389 }
390
391 static char *get_status(struct misc_meta_data *nmmd)
392 {
393         char *bar, *ret, mtime[30] = "";
394         char *status, *flags; /* afs status info */
395         char *ut = uptime_str();
396         long offset = (nmmd->offset + 500) / 1000;
397         struct timeval now;
398         struct tm mtime_tm;
399
400         if (nmmd->audio_format >= 0) {
401                 localtime_r(&nmmd->mtime, &mtime_tm);
402                 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
403         }
404         /* report real status */
405         status = afs_status_tohuman(nmmd->afs_status_flags);
406         flags = afs_get_status_flags(nmmd->afs_status_flags);
407         bar = para_basename(nmmd->filename);
408         gettimeofday(&now, NULL);
409         ret = make_message(
410                 "%s:%lu\n"      "%s:%s\n"               "%s:%i\n"       "%s:%u\n"
411                 "%s:%s\n"       "%s:%s\n"       "%s:%s\n"       "%s:%s\n"
412                 "%s:%li\n"      "%s:%s\n"       "%s"            "%s"
413                 "%s:%s\n"       "%s:%lu.%lu\n"  "%s:%lu.%lu\n",
414                 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
415                 status_item_list[SI_MTIME], mtime,
416                 status_item_list[SI_LENGTH], nmmd->seconds_total,
417                 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
418
419                 status_item_list[SI_STATUS_BAR], bar ? bar : "(none)",
420                 status_item_list[SI_STATUS], status,
421                 status_item_list[SI_STATUS_FLAGS], flags,
422                 status_item_list[SI_SELECTOR], selectors[nmmd->selector_num].name,
423
424                 status_item_list[SI_OFFSET], offset,
425                 status_item_list[SI_FORMAT], audio_format_name(nmmd->audio_format),
426                 nmmd->selector_info,
427                 nmmd->audio_file_info,
428
429                 status_item_list[SI_UPTIME], ut,
430                 status_item_list[SI_STREAM_START],
431                         (long unsigned)nmmd->stream_start.tv_sec,
432                         (long unsigned)nmmd->stream_start.tv_usec,
433                 status_item_list[SI_CURRENT_TIME],
434                         (long unsigned)now.tv_sec,
435                         (long unsigned)now.tv_usec
436
437         );
438         free(bar);
439         free(flags);
440         free(status);
441         free(ut);
442         return ret;
443 }
444
445 static int check_sender_args(int argc, char **argv, struct sender_command_data *scd)
446 {
447         int i;
448         /* this has to match sender.h */
449         const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
450
451         scd->sender_num = -1;
452         if (argc < 2)
453                 return -E_COMMAND_SYNTAX;
454         for (i = 0; senders[i].name; i++)
455                 if (!strcmp(senders[i].name, argv[1]))
456                         break;
457         PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
458         if (!senders[i].name)
459                 return -E_COMMAND_SYNTAX;
460         scd->sender_num = i;
461         for (i = 0; subcmds[i]; i++)
462                 if (!strcmp(subcmds[i], argv[2]))
463                         break;
464         if (!subcmds[i])
465                 return -E_COMMAND_SYNTAX;
466         scd->cmd_num = i;
467         mmd_lock();
468         if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
469                 mmd_unlock();
470                 return -E_SENDER_CMD;
471         }
472         mmd_unlock();
473         switch (scd->cmd_num) {
474         case SENDER_ON:
475         case SENDER_OFF:
476                 if (argc != 3)
477                         return -E_COMMAND_SYNTAX;
478                 break;
479         case SENDER_DENY:
480         case SENDER_ALLOW:
481                 if (argc != 4 && argc != 5)
482                         return -E_COMMAND_SYNTAX;
483                 if (!inet_aton(argv[3], &scd->addr))
484                         return -E_COMMAND_SYNTAX;
485                 scd->netmask = 32;
486                 if (argc == 5) {
487                         scd->netmask = atoi(argv[4]);
488                         if (scd->netmask < 0 || scd->netmask > 32)
489                                 return -E_COMMAND_SYNTAX;
490                 }
491                 break;
492         case SENDER_ADD:
493         case SENDER_DELETE:
494                 if (argc != 4 && argc != 5)
495                         return -E_COMMAND_SYNTAX;
496                 if (!inet_aton(argv[3], &scd->addr))
497                         return -E_COMMAND_SYNTAX;
498                 scd->port = -1;
499                 if (argc == 5) {
500                         scd->port = atoi(argv[4]);
501                         if (scd->port < 0 || scd->port > 65535)
502                                 return -E_COMMAND_SYNTAX;
503                 }
504                 break;
505         default:
506                 return -E_COMMAND_SYNTAX;
507         }
508         return 1;
509 }
510
511 static int com_sender(int fd, int argc, char **argv)
512 {
513         int i, ret;
514         struct sender_command_data scd;
515
516         if (argc < 2) {
517                 char *msg = NULL;
518                 for (i = 0; senders[i].name; i++) {
519                         char *tmp = make_message("%s%s\n",
520                                 msg? msg : "", senders[i].name);
521                         free(msg);
522                         msg = tmp;
523                 }
524                 ret = send_buffer(fd, msg);
525                 free(msg);
526                 return ret;
527         }
528         ret = check_sender_args(argc, argv, &scd);
529         if (ret < 0) {
530                 char *msg;
531                 if (scd.sender_num < 0)
532                         return ret;
533                 msg = senders[scd.sender_num].help();
534                 send_buffer(fd, msg);
535                 free(msg);
536                 return 1;
537         }
538         for (i = 0; i < 10; i++) {
539                 mmd_lock();
540                 if (mmd->sender_cmd_data.cmd_num >= 0) {
541                         mmd_unlock();
542                         usleep(100 * 1000);
543                         continue;
544                 }
545                 mmd->sender_cmd_data = scd;
546                 mmd_unlock();
547                 break;
548         }
549         return (i < 10)? 1 : -E_LOCK;
550 }
551
552 /* server info */
553 static int com_si(int fd, int argc, __a_unused char **argv)
554 {
555         int i, ret;
556         char *ut;
557         char *selector_string = NULL, *sender_info = NULL, *sender_list = NULL;
558
559         if (argc != 1)
560                 return -E_COMMAND_SYNTAX;
561         mmd_lock();
562         for (i = 0; selectors[i].name; i++) {
563                 selector_string = para_strcat(selector_string, selectors[i].name);
564                 selector_string = para_strcat(selector_string, " ");
565         }
566         for (i = 0; senders[i].name; i++) {
567                 char *info = senders[i].info();
568                 sender_info = para_strcat(sender_info, info);
569                 free(info);
570                 sender_list = para_strcat(sender_list, senders[i].name);
571                 sender_list = para_strcat(sender_list, " ");
572         }
573         ut = uptime_str();
574         ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
575                 "pid: %d\n"
576                 "connections (active/accepted/total): %u/%u/%u\n"
577                 "current loglevel: %i\n"
578                 "supported audio file selectors: %s\n"
579                 "supported audio formats: %s\n"
580                 "supported senders: %s\n"
581                 "%s",
582                 ut, mmd->num_played,
583                 getppid(),
584                 mmd->active_connections,
585                 mmd->num_commands,
586                 mmd->num_connects,
587                 conf.loglevel_arg,
588                 selector_string,
589                 supported_audio_formats(),
590                 sender_list,
591                 sender_info
592         );
593         mmd_unlock();
594         free(ut);
595         free(selector_string);
596         free(sender_list);
597         free(sender_info);
598         return ret;
599 }
600
601 /* version */
602 static int com_version(int socket_fd, int argc, __a_unused char **argv)
603 {
604         if (argc != 1)
605                 return -E_COMMAND_SYNTAX;
606         return send_buffer(socket_fd, "para_server-" PACKAGE_VERSION ", \""
607                         CODENAME "\"\n"
608                         COPYRIGHT "\n"
609                         "built: " BUILD_DATE "\n"
610                         SYSTEM ", " CC_VERSION "\n"
611                 );
612 }
613
614 /* sc */
615 static int com_sc(int socket_fd, int argc, char **argv)
616 {
617         char *name = NULL;
618         int ret, old = 0, count = -1; /* print af change forever */
619
620         if (argc > 1)
621                 count = atoi(argv[1]);
622 repeat:
623         mmd_lock();
624         if (old != mmd->num_played) {
625                 old = mmd->num_played;
626                 name = para_strdup(mmd->filename);
627         }
628         mmd_unlock();
629         if (name) {
630                 ret = send_va_buffer(socket_fd, "%s\n", name);
631                 free(name);
632                 name = NULL;
633                 if (ret < 0)
634                         return ret;
635                 if (argc > 1 && !--count)
636                         return 1;
637         }
638         usleep(500000);
639         goto repeat;
640 }
641
642 /* sb */
643 static int com_sb(int socket_fd, int argc, char **argv)
644 {
645         char *sb;
646         int ret, nr = -1;       /* status bar will be printed that many
647                                  * times. Negative value means: print
648                                  * forever
649                                  */
650         if (argc > 1)
651                 nr = atoi(argv[1]);
652         while (nr) {
653                 mmd_lock();
654                 sb = get_sb_string(mmd);
655                 mmd_unlock();
656                 ret = send_va_buffer(socket_fd, "%s\n", sb);
657                 free(sb);
658                 if (ret < 0)
659                         return ret;
660                 if (nr == 1)
661                         return 1;
662                 usleep(500000);
663                 if (nr > 0)
664                         nr--;
665         }
666         return 1;
667 }
668
669 /* stat */
670 static int com_stat(int socket_fd, int argc, char **argv)
671 {
672         int ret, num = 0;/* status will be printed that many
673                           * times. num <= 0 means: print forever
674                           */
675         struct misc_meta_data tmp, *nmmd = &tmp;
676         char *s;
677
678         signal(SIGUSR1, dummy);
679
680         if (argc > 1)
681                 num = atoi(argv[1]);
682         for (;;) {
683
684                 mmd_dup(nmmd);
685                 s = get_status(nmmd);
686                 ret = send_buffer(socket_fd, s);
687                 free(s);
688                 if (ret < 0)
689                         goto out;
690                 ret = 1;
691                 if (num == 1)
692                         goto out;
693                 usleep(500000 * 100);
694         }
695 out:
696         return ret;
697 }
698
699 static int send_description(int fd, struct server_command *cmd, const char *handler, int num)
700 {
701         int ret, i;
702
703         for (i = 1; cmd->name && (!num || i <= num); cmd++, i++) {
704                 char *perms = cmd_perms_itohuman(cmd->perms);
705                 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
706                         handler,
707                         perms,
708                         cmd->description);
709                 free(perms);
710                 if (ret < 0)
711                         return ret;
712         }
713         return 1;
714 }
715
716 /* always returns string that must be freed by the caller in handler */
717 static struct server_command *get_cmd_ptr(char *name, char **handler)
718 {
719         struct server_command *cmd = cmd_struct;
720
721         for (cmd = cmd_struct; cmd->name; cmd++)
722                 if (!strcmp(cmd->name, name)) {
723                         if (handler)
724                                 *handler = para_strdup("para_server"); /* server commands */
725                         return cmd;
726                 }
727         /* not found, look for commands supported by the current selector */
728         mmd_lock();
729         if (handler)
730                 *handler = make_message("the %s selector",
731                         selectors[mmd->selector_num].name);
732         cmd = selectors[mmd->selector_num].cmd_list;
733         mmd_unlock();
734         for (; cmd->name; cmd++)
735                 if (!strcmp(cmd->name, name))
736                         return cmd;
737         return NULL;
738 }
739
740 /* help */
741 static int com_help(int fd, int argc, char **argv)
742 {
743         struct server_command *cmd;
744         char *perms, *handler;
745         int ret;
746
747         if (argc < 2) {
748                 /* no argument given, print list of commands */
749                 if ((ret = send_description(fd, cmd_struct, "server", 0)) < 0)
750                         return ret;
751                 mmd_lock();
752                 handler = para_strdup(selectors[mmd->selector_num].name);
753                 cmd = selectors[mmd->selector_num].cmd_list;
754                 mmd_unlock();
755                 ret = send_description(fd, cmd, handler, 0);
756                 free(handler);
757                 return ret;
758         }
759         /* argument given for help */
760         cmd = get_cmd_ptr(argv[1], &handler);
761         if (!cmd) {
762                 free(handler);
763                 return -E_BAD_CMD;
764         }
765         perms = cmd_perms_itohuman(cmd->perms);
766         ret = send_va_buffer(fd,
767                 "NAME\n\t%s - %s\n"
768                 "SYNOPSIS\n\t para_client %s\n"
769                 "DESCRIPTION\n%s\n"
770                 "HANDLER\n"
771                 "This command is handled by %s.\n\n"
772                 "PERMISSIONS\n"
773                 "Needed privileges for %s: %s\n",
774                 argv[1],
775                 cmd->description,
776                 cmd->synopsis,
777                 cmd->help,
778                 handler,
779                 argv[1],
780                 perms
781         );
782         free(perms);
783         free(handler);
784         return ret;
785 }
786
787 /* hup */
788 static int com_hup(__a_unused int socket_fd, int argc, __a_unused char **argv)
789 {
790         if (argc != 1)
791                 return -E_COMMAND_SYNTAX;
792         kill(getppid(), SIGHUP);
793         return 1;
794 }
795
796 /* term */
797 static int com_term(__a_unused int socket_fd, int argc, __a_unused char **argv)
798 {
799         if (argc != 1)
800                 return -E_COMMAND_SYNTAX;
801         kill(getppid(), SIGTERM);
802         return 1;
803 }
804
805 static int com_play(__a_unused int socket_fd, int argc, __a_unused char **argv)
806 {
807         if (argc != 1)
808                 return -E_COMMAND_SYNTAX;
809         mmd_lock();
810         mmd->new_afs_status_flags |= AFS_PLAYING;
811         mmd->new_afs_status_flags &= ~AFS_NOMORE;
812         mmd_unlock();
813         return 1;
814
815 }
816
817 /* stop */
818 static int com_stop(__a_unused int socket_fd, int argc, __a_unused char **argv)
819 {
820         if (argc != 1)
821                 return -E_COMMAND_SYNTAX;
822         mmd_lock();
823         mmd->new_afs_status_flags &= ~AFS_PLAYING;
824         mmd->new_afs_status_flags &= ~AFS_REPOS;
825         mmd->new_afs_status_flags |= AFS_NEXT;
826         mmd_unlock();
827         return 1;
828 }
829
830 /* pause */
831 static int com_pause(__a_unused int socket_fd, int argc, __a_unused char **argv)
832 {
833         if (argc != 1)
834                 return -E_COMMAND_SYNTAX;
835         mmd_lock();
836         if (!afs_paused())
837                 mmd->events++;
838         mmd->new_afs_status_flags &= ~AFS_PLAYING;
839         mmd->new_afs_status_flags &= ~AFS_NEXT;
840         mmd_unlock();
841         return 1;
842 }
843
844 static int com_chs(int fd, int argc, char **argv)
845 {
846         int i, ret;
847
848         if (argc == 1) {
849                 char *selector;
850                 mmd_lock();
851                 selector = para_strdup(selectors[mmd->selector_num].name);
852                 mmd_unlock();
853                 ret = send_va_buffer(fd, "%s\n", selector);
854                 free(selector);
855                 return ret;
856         }
857         for (i = 0; selectors[i].name; i++) {
858                 if (strcmp(selectors[i].name, argv[1]))
859                         continue;
860                 mmd_lock();
861                 mmd->selector_change = i;
862                 mmd->events++;
863                 mmd_unlock();
864                 return 1;
865         }
866         return -E_BAD_SELECTOR;
867 }
868
869 /* next */
870 static int com_next(__a_unused int socket_fd, int argc, __a_unused char **argv)
871 {
872         if (argc != 1)
873                 return -E_COMMAND_SYNTAX;
874         mmd_lock();
875         mmd->events++;
876         mmd->new_afs_status_flags |= AFS_NEXT;
877         mmd_unlock();
878         return 1;
879 }
880
881 /* nomore */
882 static int com_nomore(__a_unused int socket_fd, int argc, __a_unused char **argv)
883 {
884         if (argc != 1)
885                 return -E_COMMAND_SYNTAX;
886         mmd_lock();
887         if (afs_playing() || afs_paused())
888                 mmd->new_afs_status_flags |= AFS_NOMORE;
889         mmd_unlock();
890         return 1;
891 }
892
893 /* ff */
894 static int com_ff(__a_unused int socket_fd, int argc, char **argv)
895 {
896         long promille;
897         int ret, backwards = 0;
898         unsigned i;
899         char c;
900
901         if (argc != 2)
902                 return -E_COMMAND_SYNTAX;
903         if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
904                 return -E_COMMAND_SYNTAX;
905         if (ret > 1 && c == '-')
906                 backwards = 1; /* jmp backwards */
907         mmd_lock();
908         ret = -E_NO_AUDIO_FILE;
909         if (!mmd->chunks_total || !mmd->seconds_total)
910                 goto out;
911         promille = (1000 * mmd->current_chunk) / mmd->chunks_total;
912         if (backwards)
913                 promille -= 1000 * i / mmd->seconds_total;
914         else
915                 promille += 1000 * i / mmd->seconds_total;
916         if (promille < 0)
917                 promille = 0;
918         if (promille >  1000) {
919                 mmd->new_afs_status_flags |= AFS_NEXT;
920                 goto out;
921         }
922         mmd->repos_request = (mmd->chunks_total * promille) / 1000;
923         mmd->new_afs_status_flags |= AFS_REPOS;
924         mmd->new_afs_status_flags &= ~AFS_NEXT;
925         mmd->events++;
926         ret = 1;
927 out:
928         mmd_unlock();
929         return ret;
930 }
931
932 /* jmp */
933 static int com_jmp(__a_unused int socket_fd, int argc, char **argv)
934 {
935         long unsigned int i;
936         int ret;
937
938         if (argc != 2)
939                 return -E_COMMAND_SYNTAX;
940         if (sscanf(argv[1], "%lu", &i) <= 0)
941                 return -E_COMMAND_SYNTAX;
942         mmd_lock();
943         ret = -E_NO_AUDIO_FILE;
944         if (!mmd->chunks_total)
945                 goto out;
946         if (i > 100)
947                 i = 100;
948         PARA_INFO_LOG("jumping to %lu%%\n", i);
949         mmd->repos_request = (mmd->chunks_total * i + 50)/ 100;
950         PARA_INFO_LOG("sent: %lu,  offset before jmp: %lu\n",
951                 mmd->chunks_sent, mmd->offset);
952         mmd->new_afs_status_flags |= AFS_REPOS;
953         mmd->new_afs_status_flags &= ~AFS_NEXT;
954         ret = 1;
955         mmd->events++;
956 out:
957         mmd_unlock();
958         return ret;
959 }
960
961 /*
962  * check if perms are sufficient to exec a command having perms cmd_perms.
963  * Returns 0 if perms are sufficient, -E_PERM otherwise.
964  */
965 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
966 {
967         PARA_DEBUG_LOG("%s", "checking permissions\n");
968         return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
969 }
970
971 /*
972  * Parse first string from *cmd and lookup in table of valid commands.
973  * On error, NULL is returned.
974  */
975 static struct server_command *parse_cmd(const char *cmdstr)
976 {
977         char buf[255];
978         int n = 0;
979
980         sscanf(cmdstr, "%200s%n", buf, &n);
981         if (!n)
982                 return NULL;
983         buf[n] = '\0';
984         return get_cmd_ptr(buf, NULL);
985 }
986
987 long int para_rand(long unsigned max)
988 {
989         return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
990 }
991
992 /* Open user_list file, returns pointer to opened file on success,
993  * NULL on errors
994  */
995 static FILE *open_user_list(char *file)
996 {
997         PARA_DEBUG_LOG("opening user list %s\n", file);
998         return fopen(file, "r");
999 }
1000
1001 /*
1002  * lookup user in user_list file. Fills in a user struct containing
1003  * filename of the user's public key as well as the permissions of that user.
1004  * Returns 1 on success, 0 if user does not exist and < 0 on errors.
1005  */
1006 static int get_user(struct user *user) {
1007         FILE *file_ptr;
1008         char *char_ptr;
1009         char line[MAXLINE];
1010         /* keyword, user, key, perms */
1011         char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE];
1012         int num;
1013
1014         file_ptr = open_user_list(user_list);
1015         if (!file_ptr)
1016                 return -E_USERLIST;
1017         while (fgets(line, MAXLINE, file_ptr)) {
1018 //              PARA_DEBUG_LOG("%s: Read line (%i bytes) "
1019 //                      "from config file\n", __func__, strlen(line));
1020                 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
1021                         continue;
1022                 if (!strcmp(w, "user") && !strcmp(user->name, n)) {
1023                         PARA_DEBUG_LOG("found entry for %s\n", n);
1024                         strcpy(user->name, n);
1025                         strcpy(user->pubkey_file, k);
1026                         user->perms = 0;
1027                         char_ptr = p;
1028                         num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
1029                                 tmp[0], tmp[1], tmp[2], tmp[3]);
1030                         PARA_DEBUG_LOG("found %i perm entries\n",
1031                                 num);
1032                         user->perms = 0;
1033                         while (num > 0) {
1034                                 num--;
1035                                 //PARA_DEBUG_LOG("%s: tmp[%i]=%s\n", __func__,
1036                                 //      num, tmp[num]);
1037                                 if (!strcmp(tmp[num], "AFS_READ"))
1038                                         user->perms =
1039                                                 user->perms | AFS_READ;
1040                                 else if (!strcmp(tmp[num], "AFS_WRITE"))
1041                                         user->perms =
1042                                                 user->perms | AFS_WRITE;
1043                                 else if (!strcmp(tmp[num], "DB_READ"))
1044                                         user->perms = user->perms | DB_READ;
1045                                 else if (!strcmp(tmp[num], "DB_WRITE"))
1046                                         user->perms = user->perms | DB_WRITE;
1047                                 else /* unknown permission */
1048                                         PARA_WARNING_LOG("unknown permission:"
1049                                                 "%s\n", tmp[num]);
1050                         }
1051                         fclose(file_ptr);
1052                         return 1;
1053                 }
1054         }
1055         fclose(file_ptr);
1056         return 0;
1057 }
1058
1059 static void init_rc4_keys(void)
1060 {
1061         int i;
1062
1063         for (i = 0; i < 2 * RC4_KEY_LEN; i++)
1064                 rc4_buf[i] = para_rand(256);
1065         PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
1066                 (unsigned char) rc4_buf[0],
1067                 (unsigned char) rc4_buf[RC4_KEY_LEN]);
1068         RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
1069         RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
1070 }
1071
1072 static void rc4_recv(unsigned long len, const unsigned char *indata,
1073                 unsigned char *outdata, __a_unused void *private_data)
1074 {
1075         RC4(&rc4_recv_key, len, indata, outdata);
1076 }
1077
1078 static void rc4_send(unsigned long len, const unsigned char *indata,
1079                 unsigned char *outdata, __a_unused void *private_data)
1080 {
1081         RC4(&rc4_send_key, len, indata, outdata);
1082 }
1083
1084 int handle_connect(int fd, struct sockaddr_in *addr)
1085 {
1086         int numbytes, ret, argc, use_rc4 = 0;
1087         char buf[STRINGSIZE];
1088         unsigned char crypt_buf[MAXLINE];
1089         struct user u;
1090         struct server_command *cmd = NULL;
1091         long unsigned challenge_nr, chall_response;
1092         char **argv = NULL;
1093         char *p, *command = NULL;
1094
1095         signal(SIGCHLD, SIG_IGN);
1096         signal(SIGINT, SIG_DFL);
1097         signal(SIGTERM, SIG_DFL);
1098         signal(SIGHUP, SIG_DFL);
1099         signal(SIGUSR1, SIG_IGN);
1100
1101         in_addr = addr;
1102         challenge_nr = random();
1103         /* send Welcome message */
1104         ret = send_va_buffer(fd, "This is para_server, version "
1105                 PACKAGE_VERSION  ".\n" );
1106         if (ret < 0)
1107                 goto err_out;
1108         /* recv auth request line */
1109         ret = recv_buffer(fd, buf, sizeof(buf));
1110         if (ret < 0)
1111                 goto err_out;
1112         if (ret <= 6) {
1113                 ret = -E_AUTH;
1114                 goto err_out;
1115         }
1116         numbytes = ret;
1117         ret = -E_AUTH;
1118         if (strncmp(buf, "auth ", 5))
1119                 goto err_out;
1120
1121         if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
1122                 strcpy(u.name, buf + 5); /* client version < 0.2.6 */
1123         else {
1124                 strcpy(u.name, buf + 9); /* client version >= 0.2.6 */
1125                 use_rc4 = 1;
1126         }
1127 //      strcpy(u.name, buf + 5); /* ok, but ugly */
1128         PARA_DEBUG_LOG("received %s request for user %s\n",
1129                 use_rc4? "rc4" : "auth", u.name);
1130         /* lookup user in list file */
1131         if ((ret = get_user(&u)) < 0)
1132                 goto err_out;
1133         if (!ret) { /* user not found */
1134                 PARA_WARNING_LOG("auth request for unknown user %s\n", u.name);
1135                 ret = -E_BAD_USER;
1136                 goto err_out;
1137         }
1138         ret = para_encrypt_challenge(u.pubkey_file, challenge_nr, crypt_buf);
1139         if (ret <= 0)
1140                 goto err_out;
1141         numbytes = ret;
1142         PARA_DEBUG_LOG("sending %d byte challenge\n", numbytes);
1143         /* We can't use send_buffer here since buf may contain null bytes */
1144         ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
1145         if (ret < 0)
1146                 goto err_out;
1147         /* recv decrypted number */
1148         numbytes = recv_buffer(fd, buf, sizeof(buf));
1149         ret = numbytes;
1150         if (ret < 0)
1151                 goto err_out;
1152         ret = -E_AUTH;
1153         if (!numbytes)
1154                 goto err_out;
1155         if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
1156                         || chall_response != challenge_nr)
1157                 goto err_out;
1158         /* auth successful. Send 'Proceed' message */
1159         PARA_INFO_LOG("good auth for %s (%lu)\n", u.name, challenge_nr);
1160         sprintf(buf, "%s", PROCEED_MSG);
1161         if (use_rc4) {
1162                 init_rc4_keys();
1163                 ret = para_encrypt_buffer(u.pubkey_file, rc4_buf, 2 * RC4_KEY_LEN,
1164                         (unsigned char *)buf + PROCEED_MSG_LEN + 1);
1165                 if (ret <= 0)
1166                         goto err_out;
1167                 numbytes = ret + strlen(PROCEED_MSG) + 1;
1168         } else
1169                 numbytes = strlen(buf);
1170         ret = send_bin_buffer(fd, buf, numbytes);
1171         if (ret < 0)
1172                 goto err_out;
1173         if (use_rc4)
1174                 enable_crypt(fd, rc4_recv, rc4_send, NULL);
1175         /* read command */
1176         while ((numbytes = recv_buffer(fd, buf, sizeof(buf))) > 0) {
1177 //              PARA_INFO_LOG("recvd: %s (%d)\n", buf, numbytes);
1178                 ret = -E_COMMAND_SYNTAX;
1179                 if (command && numbytes + strlen(command) > STRINGSIZE) /* DOS */
1180                         goto err_out;
1181                 command = para_strcat(command, buf);
1182                 if ((p = strstr(command, EOC_MSG))) {
1183                         *p = '\0';
1184                         break;
1185                 }
1186         }
1187         ret = numbytes;
1188         if (ret < 0)
1189                 goto err_out;
1190         ret = -E_BAD_CMD;
1191         /* parse command */
1192         if (!(cmd = parse_cmd(command)))
1193                 goto err_out;
1194         /* valid command, check permissions */
1195         ret = check_perms(u.perms, cmd);
1196         if (ret < 0)
1197                 goto err_out;
1198         /* valid command and sufficient perms */
1199         alarm(0);
1200         argc = split_args(command, &argv, "\n");
1201         mmd_lock();
1202         mmd->num_commands++;
1203         mmd_unlock();
1204         PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u.name,
1205                 inet_ntoa(addr->sin_addr));
1206         ret = cmd->handler(fd, argc, argv);
1207         if (ret >= 0) {
1208                 ret = EXIT_SUCCESS;
1209                 goto out;
1210         }
1211 err_out:
1212         if (ret != -E_SEND && ret != -E_RECV) {
1213                 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
1214                 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
1215         }
1216         ret = EXIT_FAILURE;
1217 out:
1218         free(command);
1219         free(argv);
1220         mmd_lock();
1221         if (cmd && (cmd->perms & DB_WRITE) && ret >= 0)
1222                 mmd->events++;
1223         mmd->active_connections--;
1224         mmd_unlock();
1225         return ret;
1226 }