Remove autogenerated doxygen comments for commands.
[paraslash.git] / command.c
1 /*
2 * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file command.c Client authentication and server commands. */
8
9 #include <regex.h>
10 #include <signal.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <dirent.h>
14 #include <osl.h>
15 #include <stdbool.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "crypt.h"
20 #include "command.h"
21 #include "server.cmdline.h"
22 #include "string.h"
23 #include "afh.h"
24 #include "afs.h"
25 #include "server.h"
26 #include "list.h"
27 #include "send.h"
28 #include "vss.h"
29 #include "rc4.h"
30 #include "net.h"
31 #include "daemon.h"
32 #include "fd.h"
33 #include "ipc.h"
34 #include "user_list.h"
35 #include "server_command_list.h"
36 #include "afs_command_list.h"
37 #include "sched.h"
38 #include "signal.h"
39
40 /** Commands including options must be shorter than this. */
41 #define MAX_COMMAND_LEN 32768
42
43 extern int mmd_mutex;
44 extern struct misc_meta_data *mmd;
45 extern struct sender senders[];
46 int send_afs_status(struct rc4_context *rc4c, int parser_friendly);
47
48 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
49
50 static void dummy(__a_unused int s)
51 {
52 }
53
54 static void mmd_dup(struct misc_meta_data *new_mmd)
55 {
56 mutex_lock(mmd_mutex);
57 *new_mmd = *mmd;
58 mutex_unlock(mmd_mutex);
59 }
60
61 /*
62 * Compute human readable string containing vss status for given integer value.
63 *
64 * We don't want to use vss_playing() and friends here because we take a
65 * snapshot of the mmd struct and use the copy for computing the state of the
66 * vss. If the real data were used, we would take the mmd lock for a rather
67 * long time or risk to get an inconsistent view.
68 */
69 static char *vss_status_tohuman(unsigned int flags)
70 {
71 if (flags & VSS_PLAYING)
72 return para_strdup("playing");
73 if (flags & VSS_NEXT)
74 return para_strdup("stopped");
75 return para_strdup("paused");
76 }
77
78 /*
79 * return human readable permission string. Never returns NULL.
80 */
81 static char *cmd_perms_itohuman(unsigned int perms)
82 {
83 char *msg = para_malloc(5 * sizeof(char));
84
85 msg[0] = perms & AFS_READ? 'a' : '-';
86 msg[1] = perms & AFS_WRITE? 'A' : '-';
87 msg[2] = perms & VSS_READ? 'v' : '-';
88 msg[3] = perms & VSS_WRITE? 'V' : '-';
89 msg[4] = '\0';
90 return msg;
91 }
92
93 /*
94 * Never returns NULL.
95 */
96 static char *vss_get_status_flags(unsigned int flags)
97 {
98 char *msg = para_malloc(5 * sizeof(char));
99
100 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
101 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
102 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
103 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
104 msg[4] = '\0';
105 return msg;
106 }
107
108 static char *get_status(struct misc_meta_data *nmmd, int parser_friendly)
109 {
110 char mtime[30] = "";
111 char *status, *flags; /* vss status info */
112 char *ut = uptime_str();
113 long offset = (nmmd->offset + 500) / 1000;
114 struct timeval current_time;
115 struct tm mtime_tm;
116 struct para_buffer b = {.flags = parser_friendly? PBF_SIZE_PREFIX : 0};
117
118 /* report real status */
119 status = vss_status_tohuman(nmmd->vss_status_flags);
120 flags = vss_get_status_flags(nmmd->vss_status_flags);
121 if (nmmd->size) { /* parent currently has an audio file open */
122 localtime_r(&nmmd->mtime, &mtime_tm);
123 strftime(mtime, 29, "%b %d %Y", &mtime_tm);
124 }
125 gettimeofday(&current_time, NULL);
126 WRITE_STATUS_ITEM(&b, SI_FILE_SIZE, "%zu\n", nmmd->size / 1024);
127 WRITE_STATUS_ITEM(&b, SI_MTIME, "%s\n", mtime);
128 WRITE_STATUS_ITEM(&b, SI_STATUS, "%s\n", status);
129 WRITE_STATUS_ITEM(&b, SI_STATUS_FLAGS, "%s\n", flags);
130 WRITE_STATUS_ITEM(&b, SI_OFFSET, "%li\n", offset);
131 WRITE_STATUS_ITEM(&b, SI_AFS_MODE, "%s\n", mmd->afs_mode_string);
132 WRITE_STATUS_ITEM(&b, SI_STREAM_START, "%lu.%lu\n",
133 (long unsigned)nmmd->stream_start.tv_sec,
134 (long unsigned)nmmd->stream_start.tv_usec);
135 WRITE_STATUS_ITEM(&b, SI_CURRENT_TIME, "%lu.%lu\n",
136 (long unsigned)current_time.tv_sec,
137 (long unsigned)current_time.tv_usec);
138 free(flags);
139 free(status);
140 free(ut);
141 return b.buf;
142 }
143
144 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
145 {
146 int i;
147 /* this has to match sender.h */
148 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
149
150 scd->sender_num = -1;
151 if (argc < 2)
152 return -E_COMMAND_SYNTAX;
153 for (i = 0; senders[i].name; i++)
154 if (!strcmp(senders[i].name, argv[1]))
155 break;
156 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
157 if (!senders[i].name)
158 return -E_COMMAND_SYNTAX;
159 scd->sender_num = i;
160 for (i = 0; subcmds[i]; i++)
161 if (!strcmp(subcmds[i], argv[2]))
162 break;
163 if (!subcmds[i])
164 return -E_COMMAND_SYNTAX;
165 scd->cmd_num = i;
166 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
167 return -E_SENDER_CMD;
168 switch (scd->cmd_num) {
169 case SENDER_ON:
170 case SENDER_OFF:
171 if (argc != 3)
172 return -E_COMMAND_SYNTAX;
173 break;
174 case SENDER_DENY:
175 case SENDER_ALLOW:
176 if (argc != 4 || parse_cidr(argv[3], scd->host,
177 sizeof(scd->host), &scd->netmask) == NULL)
178 return -E_COMMAND_SYNTAX;
179 break;
180 case SENDER_ADD:
181 case SENDER_DELETE:
182 if (argc != 4)
183 return -E_COMMAND_SYNTAX;
184 return parse_fec_url(argv[3], scd);
185 default:
186 return -E_COMMAND_SYNTAX;
187 }
188 return 1;
189 }
190
191 int com_sender(struct rc4_context *rc4c, int argc, char * const * argv)
192 {
193 int i, ret;
194 char *msg = NULL;
195 struct sender_command_data scd;
196
197 if (argc < 2) {
198 for (i = 0; senders[i].name; i++) {
199 char *tmp = make_message("%s%s\n",
200 msg? msg : "", senders[i].name);
201 free(msg);
202 msg = tmp;
203 }
204 ret = rc4_send_buffer(rc4c, msg);
205 free(msg);
206 return ret;
207 }
208 ret = check_sender_args(argc, argv, &scd);
209 if (ret < 0) {
210 if (scd.sender_num < 0)
211 return ret;
212 msg = senders[scd.sender_num].help();
213 ret = rc4_send_buffer(rc4c, msg);
214 free(msg);
215 return ret;
216 }
217
218 switch (scd.cmd_num) {
219 case SENDER_ADD:
220 case SENDER_DELETE:
221 assert(senders[scd.sender_num].resolve_target);
222 ret = senders[scd.sender_num].resolve_target(argv[3], &scd);
223 if (ret < 0)
224 return ret;
225 }
226
227 for (i = 0; i < 10; i++) {
228 mutex_lock(mmd_mutex);
229 if (mmd->sender_cmd_data.cmd_num >= 0) {
230 mutex_unlock(mmd_mutex);
231 usleep(100 * 1000);
232 continue;
233 }
234 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
235 mutex_unlock(mmd_mutex);
236 break;
237 }
238 return (i < 10)? 1 : -E_LOCK;
239 }
240
241 /* server info */
242 int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
243 {
244 int i, ret;
245 char *ut;
246 char *sender_info = NULL;
247
248 if (argc != 1)
249 return -E_COMMAND_SYNTAX;
250 mutex_lock(mmd_mutex);
251 for (i = 0; senders[i].name; i++) {
252 char *info = senders[i].info();
253 sender_info = para_strcat(sender_info, info);
254 free(info);
255 }
256 ut = uptime_str();
257 ret = rc4_send_va_buffer(rc4c, "version: " GIT_VERSION "\n"
258 "up: %s\nplayed: %u\n"
259 "server_pid: %d\n"
260 "afs_pid: %d\n"
261 "connections (active/accepted/total): %u/%u/%u\n"
262 "current loglevel: %s\n"
263 "supported audio formats: %s\n"
264 "%s",
265 ut, mmd->num_played,
266 (int)getppid(),
267 (int)mmd->afs_pid,
268 mmd->active_connections,
269 mmd->num_commands,
270 mmd->num_connects,
271 conf.loglevel_arg,
272 SERVER_AUDIO_FORMATS,
273 sender_info
274 );
275 mutex_unlock(mmd_mutex);
276 free(ut);
277 free(sender_info);
278 return ret;
279 }
280
281 /* version */
282 int com_version(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
283 {
284 if (argc != 1)
285 return -E_COMMAND_SYNTAX;
286 return rc4_send_buffer(rc4c, VERSION_TEXT("server")
287 "built: " BUILD_DATE "\n"
288 UNAME_RS ", " CC_VERSION "\n"
289 );
290 }
291
292 #define EMPTY_STATUS_ITEMS \
293 ITEM(PATH) \
294 ITEM(DIRECTORY) \
295 ITEM(BASENAME) \
296 ITEM(SCORE) \
297 ITEM(ATTRIBUTES_BITMAP) \
298 ITEM(ATTRIBUTES_TXT) \
299 ITEM(HASH) \
300 ITEM(IMAGE_ID) \
301 ITEM(IMAGE_NAME) \
302 ITEM(LYRICS_ID) \
303 ITEM(LYRICS_NAME) \
304 ITEM(BITRATE) \
305 ITEM(FORMAT) \
306 ITEM(FREQUENCY) \
307 ITEM(CHANNELS) \
308 ITEM(DURATION) \
309 ITEM(SECONDS_TOTAL) \
310 ITEM(NUM_PLAYED) \
311 ITEM(LAST_PLAYED) \
312 ITEM(TECHINFO) \
313 ITEM(ARTIST) \
314 ITEM(TITLE) \
315 ITEM(YEAR) \
316 ITEM(ALBUM) \
317 ITEM(COMMENT) \
318 ITEM(AMPLIFICATION)
319
320 /**
321 * Write a list of audio-file related status items with empty values.
322 *
323 * This is used by vss when currently no audio file is open.
324 */
325 static char *empty_status_items(int parser_friendly)
326 {
327 if (parser_friendly)
328 return make_message(
329 #define ITEM(x) "0004 %02x:\n"
330 EMPTY_STATUS_ITEMS
331 #undef ITEM
332 #define ITEM(x) , SI_ ## x
333 EMPTY_STATUS_ITEMS
334 #undef ITEM
335 );
336 return make_message(
337 #define ITEM(x) "%s:\n"
338 EMPTY_STATUS_ITEMS
339 #undef ITEM
340 #define ITEM(x) ,status_item_list[SI_ ## x]
341 EMPTY_STATUS_ITEMS
342 #undef ITEM
343 );
344 }
345 #undef EMPTY_STATUS_ITEMS
346
347 /* stat */
348 int com_stat(struct rc4_context *rc4c, int argc, char * const * argv)
349 {
350 int i, ret;
351 struct misc_meta_data tmp, *nmmd = &tmp;
352 char *s;
353 int32_t num = 0;
354 int parser_friendly = 0;
355
356 para_sigaction(SIGUSR1, dummy);
357
358 for (i = 1; i < argc; i++) {
359 const char *arg = argv[i];
360 if (arg[0] != '-')
361 break;
362 if (!strcmp(arg, "--")) {
363 i++;
364 break;
365 }
366 if (!strncmp(arg, "-n=", 3)) {
367 ret = para_atoi32(arg + 3, &num);
368 if (ret < 0)
369 return ret;
370 continue;
371 }
372 if (!strcmp(arg, "-p")) {
373 parser_friendly = 1;
374 continue;
375 }
376 return -E_COMMAND_SYNTAX;
377 }
378 if (i != argc)
379 return -E_COMMAND_SYNTAX;
380 for (;;) {
381 mmd_dup(nmmd);
382 s = get_status(nmmd, parser_friendly);
383 ret = rc4_send_buffer(rc4c, s);
384 free(s);
385 if (ret < 0)
386 goto out;
387 if (nmmd->vss_status_flags & VSS_NEXT) {
388 static char *esi;
389 if (!esi)
390 esi = empty_status_items(parser_friendly);
391 ret = rc4_send_buffer(rc4c, esi);
392 if (ret < 0)
393 goto out;
394 } else
395 send_afs_status(rc4c, parser_friendly);
396 ret = 1;
397 if (num > 0 && !--num)
398 goto out;
399 sleep(50);
400 if (getppid() == 1)
401 return -E_SERVER_CRASH;
402 }
403 out:
404 return ret;
405 }
406
407 static int send_list_of_commands(struct rc4_context *rc4c, struct server_command *cmd,
408 const char *handler)
409 {
410 int ret, i;
411
412 for (i = 1; cmd->name; cmd++, i++) {
413 char *perms = cmd_perms_itohuman(cmd->perms);
414 ret = rc4_send_va_buffer(rc4c, "%s\t%s\t%s\t%s\n", cmd->name,
415 handler,
416 perms,
417 cmd->description);
418 free(perms);
419 if (ret < 0)
420 return ret;
421 }
422 return 1;
423 }
424
425 /* returns string that must be freed by the caller */
426 static struct server_command *get_cmd_ptr(const char *name, char **handler)
427 {
428 struct server_command *cmd;
429
430 for (cmd = server_cmds; cmd->name; cmd++)
431 if (!strcmp(cmd->name, name)) {
432 if (handler)
433 *handler = para_strdup("server"); /* server commands */
434 return cmd;
435 }
436 /* not found, look for commands supported by afs */
437 for (cmd = afs_cmds; cmd->name; cmd++)
438 if (!strcmp(cmd->name, name)) {
439 if (handler)
440 *handler = para_strdup("afs");
441 return cmd;
442 }
443 return NULL;
444 }
445
446 /* help */
447 int com_help(struct rc4_context *rc4c, int argc, char * const * argv)
448 {
449 struct server_command *cmd;
450 char *perms, *handler;
451 int ret;
452
453 if (argc < 2) {
454 /* no argument given, print list of commands */
455 if ((ret = send_list_of_commands(rc4c, server_cmds, "server")) < 0)
456 return ret;
457 return send_list_of_commands(rc4c, afs_cmds, "afs");
458 }
459 /* argument given for help */
460 cmd = get_cmd_ptr(argv[1], &handler);
461 if (!cmd)
462 return -E_BAD_CMD;
463 perms = cmd_perms_itohuman(cmd->perms);
464 ret = rc4_send_va_buffer(rc4c,
465 "%s - %s\n\n"
466 "handler: %s\n"
467 "permissions: %s\n"
468 "usage: %s\n\n"
469 "%s\n",
470 argv[1],
471 cmd->description,
472 handler,
473 perms,
474 cmd->usage,
475 cmd->help
476 );
477 free(perms);
478 free(handler);
479 return ret;
480 }
481
482 /* hup */
483 int com_hup(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
484 {
485 if (argc != 1)
486 return -E_COMMAND_SYNTAX;
487 kill(getppid(), SIGHUP);
488 return 1;
489 }
490
491 /* term */
492 int com_term(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
493 {
494 if (argc != 1)
495 return -E_COMMAND_SYNTAX;
496 kill(getppid(), SIGTERM);
497 return 1;
498 }
499
500 int com_play(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
501 {
502 if (argc != 1)
503 return -E_COMMAND_SYNTAX;
504 mutex_lock(mmd_mutex);
505 mmd->new_vss_status_flags |= VSS_PLAYING;
506 mmd->new_vss_status_flags &= ~VSS_NOMORE;
507 mutex_unlock(mmd_mutex);
508 return 1;
509
510 }
511
512 /* stop */
513 int com_stop(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
514 {
515 if (argc != 1)
516 return -E_COMMAND_SYNTAX;
517 mutex_lock(mmd_mutex);
518 mmd->new_vss_status_flags &= ~VSS_PLAYING;
519 mmd->new_vss_status_flags &= ~VSS_REPOS;
520 mmd->new_vss_status_flags |= VSS_NEXT;
521 mutex_unlock(mmd_mutex);
522 return 1;
523 }
524
525 /* pause */
526 int com_pause(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
527 {
528 if (argc != 1)
529 return -E_COMMAND_SYNTAX;
530 mutex_lock(mmd_mutex);
531 if (!vss_paused() && !vss_stopped()) {
532 mmd->events++;
533 mmd->new_vss_status_flags &= ~VSS_PLAYING;
534 mmd->new_vss_status_flags &= ~VSS_NEXT;
535 }
536 mutex_unlock(mmd_mutex);
537 return 1;
538 }
539
540 /* next */
541 int com_next(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
542 {
543 if (argc != 1)
544 return -E_COMMAND_SYNTAX;
545 mutex_lock(mmd_mutex);
546 mmd->events++;
547 mmd->new_vss_status_flags |= VSS_NEXT;
548 mutex_unlock(mmd_mutex);
549 return 1;
550 }
551
552 /* nomore */
553 int com_nomore(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
554 {
555 if (argc != 1)
556 return -E_COMMAND_SYNTAX;
557 mutex_lock(mmd_mutex);
558 if (vss_playing() || vss_paused())
559 mmd->new_vss_status_flags |= VSS_NOMORE;
560 mutex_unlock(mmd_mutex);
561 return 1;
562 }
563
564 /* ff */
565 int com_ff(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
566 {
567 long promille;
568 int ret, backwards = 0;
569 unsigned i;
570 char c;
571
572 if (argc != 2)
573 return -E_COMMAND_SYNTAX;
574 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
575 return -E_COMMAND_SYNTAX;
576 if (ret > 1 && c == '-')
577 backwards = 1; /* jmp backwards */
578 mutex_lock(mmd_mutex);
579 ret = -E_NO_AUDIO_FILE;
580 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
581 goto out;
582 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
583 if (backwards)
584 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
585 else
586 promille += 1000 * i / mmd->afd.afhi.seconds_total;
587 if (promille < 0)
588 promille = 0;
589 if (promille > 1000) {
590 mmd->new_vss_status_flags |= VSS_NEXT;
591 goto out;
592 }
593 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
594 mmd->new_vss_status_flags |= VSS_REPOS;
595 mmd->new_vss_status_flags &= ~VSS_NEXT;
596 mmd->events++;
597 ret = 1;
598 out:
599 mutex_unlock(mmd_mutex);
600 return ret;
601 }
602
603 /* jmp */
604 int com_jmp(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
605 {
606 long unsigned int i;
607 int ret;
608
609 if (argc != 2)
610 return -E_COMMAND_SYNTAX;
611 if (sscanf(argv[1], "%lu", &i) <= 0)
612 return -E_COMMAND_SYNTAX;
613 mutex_lock(mmd_mutex);
614 ret = -E_NO_AUDIO_FILE;
615 if (!mmd->afd.afhi.chunks_total)
616 goto out;
617 if (i > 100)
618 i = 100;
619 PARA_INFO_LOG("jumping to %lu%%\n", i);
620 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
621 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
622 mmd->chunks_sent, mmd->offset);
623 mmd->new_vss_status_flags |= VSS_REPOS;
624 mmd->new_vss_status_flags &= ~VSS_NEXT;
625 ret = 1;
626 mmd->events++;
627 out:
628 mutex_unlock(mmd_mutex);
629 return ret;
630 }
631
632 /*
633 * check if perms are sufficient to exec a command having perms cmd_perms.
634 * Returns 0 if perms are sufficient, -E_PERM otherwise.
635 */
636 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
637 {
638 PARA_DEBUG_LOG("checking permissions\n");
639 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
640 }
641
642 /*
643 * Parse first string from *cmd and lookup in table of valid commands.
644 * On error, NULL is returned.
645 */
646 static struct server_command *parse_cmd(const char *cmdstr)
647 {
648 char buf[255];
649 int n = 0;
650
651 sscanf(cmdstr, "%200s%n", buf, &n);
652 if (!n)
653 return NULL;
654 buf[n] = '\0';
655 return get_cmd_ptr(buf, NULL);
656 }
657
658 static int read_command(struct rc4_context *rc4c, char **result)
659 {
660 int ret;
661 char buf[4096];
662 char *command = NULL;
663
664 for (;;) {
665 size_t numbytes;
666 char *p;
667
668 ret = rc4_recv_buffer(rc4c, buf, sizeof(buf));
669 if (ret < 0)
670 goto out;
671 if (!ret)
672 break;
673 numbytes = ret;
674 ret = -E_COMMAND_SYNTAX;
675 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
676 goto out;
677 command = para_strcat(command, buf);
678 p = strstr(command, EOC_MSG);
679 if (p) {
680 *p = '\0';
681 break;
682 }
683 }
684 ret = command? 1 : -E_COMMAND_SYNTAX;
685 out:
686 if (ret < 0)
687 free(command);
688 else
689 *result = command;
690 return ret;
691
692 }
693
694 static void reset_signals(void)
695 {
696 para_sigaction(SIGCHLD, SIG_IGN);
697 para_sigaction(SIGINT, SIG_DFL);
698 para_sigaction(SIGTERM, SIG_DFL);
699 para_sigaction(SIGHUP, SIG_DFL);
700 }
701
702 /**
703 * Perform user authentication and execute a command.
704 *
705 * \param fd The file descriptor to send output to.
706 * \param peername Identifies the connecting peer.
707 *
708 * Whenever para_server accepts an incoming tcp connection on
709 * the port it listens on, it forks and the resulting child
710 * calls this function.
711 *
712 * An RSA-based challenge/response is used to authenticate
713 * the peer. It that authentication succeeds, a random RC4
714 * session key is generated and sent back to the peer,
715 * encrypted with its RSA public key. From this point on,
716 * all transfers are crypted with this session key.
717 *
718 * Next it is checked if the peer supplied a valid server command or a command
719 * for the audio file selector. If yes, and if the user has sufficient
720 * permissions to execute that command, the function calls the corresponding
721 * command handler which does argument checking and further processing.
722 *
723 * In order to cope with a DOS attacks, a timeout is set up
724 * which terminates the function if the connection was not
725 * authenticated when the timeout expires.
726 *
727 * \sa alarm(2), rc4(3), crypt.c, crypt.h
728 */
729 __noreturn void handle_connect(int fd, const char *peername)
730 {
731 int ret, argc;
732 char buf[4096];
733 unsigned char rand_buf[CHALLENGE_SIZE + 2 * RC4_KEY_LEN];
734 unsigned char challenge_sha1[HASH_SIZE];
735 struct user *u;
736 struct server_command *cmd = NULL;
737 char **argv = NULL;
738 char *p, *command = NULL;
739 size_t numbytes;
740 struct rc4_context rc4c = {.fd = fd};
741
742 reset_signals();
743 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
744 ret = mark_fd_blocking(fd);
745 if (ret < 0)
746 goto net_err;
747 /* send Welcome message */
748 ret = send_va_buffer(fd, "This is para_server, version "
749 PACKAGE_VERSION ".\n" );
750 if (ret < 0)
751 goto net_err;
752 /* recv auth request line */
753 ret = recv_buffer(fd, buf, sizeof(buf));
754 if (ret < 0)
755 goto net_err;
756 if (ret < 10) {
757 ret = -E_AUTH_REQUEST;
758 goto net_err;
759 }
760 numbytes = ret;
761 ret = -E_AUTH_REQUEST;
762 if (strncmp(buf, AUTH_REQUEST_MSG, strlen(AUTH_REQUEST_MSG)))
763 goto net_err;
764 p = buf + strlen(AUTH_REQUEST_MSG);
765 PARA_DEBUG_LOG("received auth request for user %s\n", p);
766 ret = -E_BAD_USER;
767 u = lookup_user(p);
768 if (u) {
769 get_random_bytes_or_die(rand_buf, sizeof(rand_buf));
770 ret = pub_encrypt(u->pubkey, rand_buf, sizeof(rand_buf),
771 (unsigned char *)buf);
772 if (ret < 0)
773 goto net_err;
774 numbytes = ret;
775 } else {
776 /*
777 * We don't want to reveal our user names, so we send a
778 * challenge to the client even if the user does not exist, and
779 * fail the authentication later.
780 */
781 numbytes = 256;
782 get_random_bytes_or_die((unsigned char *)buf, numbytes);
783 }
784 PARA_DEBUG_LOG("sending %u byte challenge + rc4 keys (%zu bytes)\n",
785 CHALLENGE_SIZE, numbytes);
786 ret = send_bin_buffer(fd, buf, numbytes);
787 if (ret < 0)
788 goto net_err;
789 /* recv challenge response */
790 ret = recv_bin_buffer(fd, buf, HASH_SIZE);
791 if (ret < 0)
792 goto net_err;
793 numbytes = ret;
794 PARA_DEBUG_LOG("received %d bytes challenge response\n", ret);
795 ret = -E_BAD_USER;
796 if (!u)
797 goto net_err;
798 /*
799 * The correct response is the sha1 of the first CHALLENGE_SIZE bytes
800 * of the random data.
801 */
802 ret = -E_BAD_AUTH;
803 if (numbytes != HASH_SIZE)
804 goto net_err;
805 sha1_hash((char *)rand_buf, CHALLENGE_SIZE, challenge_sha1);
806 if (memcmp(challenge_sha1, buf, HASH_SIZE))
807 goto net_err;
808 /* auth successful */
809 alarm(0);
810 PARA_INFO_LOG("good auth for %s\n", u->name);
811 /* init stream cipher keys with the second part of the random buffer */
812 rc4c.recv = stream_cipher_new(rand_buf + CHALLENGE_SIZE, RC4_KEY_LEN);
813 rc4c.send = stream_cipher_new(rand_buf + CHALLENGE_SIZE + RC4_KEY_LEN,
814 RC4_KEY_LEN);
815 ret = rc4_send_buffer(&rc4c, PROCEED_MSG);
816 if (ret < 0)
817 goto net_err;
818 ret = read_command(&rc4c, &command);
819 if (ret == -E_COMMAND_SYNTAX)
820 goto err_out;
821 if (ret < 0)
822 goto net_err;
823 ret = -E_BAD_CMD;
824 cmd = parse_cmd(command);
825 if (!cmd)
826 goto err_out;
827 /* valid command, check permissions */
828 ret = check_perms(u->perms, cmd);
829 if (ret < 0)
830 goto err_out;
831 /* valid command and sufficient perms */
832 ret = create_argv(command, "\n", &argv);
833 if (ret < 0)
834 goto err_out;
835 argc = ret;
836 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
837 peername);
838 ret = cmd->handler(&rc4c, argc, argv);
839 free_argv(argv);
840 mutex_lock(mmd_mutex);
841 mmd->num_commands++;
842 mutex_unlock(mmd_mutex);
843 if (ret >= 0)
844 goto out;
845 err_out:
846 rc4_send_va_buffer(&rc4c, "%s\n", para_strerror(-ret));
847 net_err:
848 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
849 out:
850 free(command);
851 stream_cipher_free(rc4c.recv);
852 stream_cipher_free(rc4c.send);
853 mutex_lock(mmd_mutex);
854 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
855 mmd->events++;
856 mmd->active_connections--;
857 mutex_unlock(mmd_mutex);
858 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
859 }