Remove some unused error codes.
[paraslash.git] / command.c
1 /*
2 * Copyright (C) 1997-2010 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 <openssl/rc4.h>
15 #include <osl.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, "up: %s\nplayed: %u\n"
258 "server_pid: %d\n"
259 "afs_pid: %d\n"
260 "connections (active/accepted/total): %u/%u/%u\n"
261 "current loglevel: %s\n"
262 "supported audio formats: %s\n"
263 "%s",
264 ut, mmd->num_played,
265 (int)getppid(),
266 (int)mmd->afs_pid,
267 mmd->active_connections,
268 mmd->num_commands,
269 mmd->num_connects,
270 conf.loglevel_arg,
271 supported_audio_formats(),
272 sender_info
273 );
274 mutex_unlock(mmd_mutex);
275 free(ut);
276 free(sender_info);
277 return ret;
278 }
279
280 /* version */
281 int com_version(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
282 {
283 if (argc != 1)
284 return -E_COMMAND_SYNTAX;
285 return rc4_send_buffer(rc4c, VERSION_TEXT("server")
286 "built: " BUILD_DATE "\n"
287 UNAME_RS ", " CC_VERSION "\n"
288 );
289 }
290
291 #define EMPTY_STATUS_ITEMS \
292 ITEM(PATH) \
293 ITEM(DIRECTORY) \
294 ITEM(BASENAME) \
295 ITEM(SCORE) \
296 ITEM(ATTRIBUTES_BITMAP) \
297 ITEM(ATTRIBUTES_TXT) \
298 ITEM(HASH) \
299 ITEM(IMAGE_ID) \
300 ITEM(IMAGE_NAME) \
301 ITEM(LYRICS_ID) \
302 ITEM(LYRICS_NAME) \
303 ITEM(BITRATE) \
304 ITEM(FORMAT) \
305 ITEM(FREQUENCY) \
306 ITEM(CHANNELS) \
307 ITEM(DURATION) \
308 ITEM(SECONDS_TOTAL) \
309 ITEM(NUM_PLAYED) \
310 ITEM(LAST_PLAYED) \
311 ITEM(TECHINFO) \
312 ITEM(ARTIST) \
313 ITEM(TITLE) \
314 ITEM(YEAR) \
315 ITEM(ALBUM) \
316 ITEM(COMMENT) \
317 ITEM(AMPLIFICATION)
318
319 /**
320 * Write a list of audio-file related status items with empty values.
321 *
322 * This is used by vss when currently no audio file is open.
323 */
324 static char *empty_status_items(int parser_friendly)
325 {
326 if (parser_friendly)
327 return make_message(
328 #define ITEM(x) "0004 %02x:\n"
329 EMPTY_STATUS_ITEMS
330 #undef ITEM
331 #define ITEM(x) , SI_ ## x
332 EMPTY_STATUS_ITEMS
333 #undef ITEM
334 );
335 return make_message(
336 #define ITEM(x) "%s:\n"
337 EMPTY_STATUS_ITEMS
338 #undef ITEM
339 #define ITEM(x) ,status_item_list[SI_ ## x]
340 EMPTY_STATUS_ITEMS
341 #undef ITEM
342 );
343 }
344 #undef EMPTY_STATUS_ITEMS
345
346 /* stat */
347 int com_stat(struct rc4_context *rc4c, int argc, char * const * argv)
348 {
349 int i, ret;
350 struct misc_meta_data tmp, *nmmd = &tmp;
351 char *s;
352 int32_t num = 0;
353 int parser_friendly = 0;
354
355 para_sigaction(SIGUSR1, dummy);
356
357 for (i = 1; i < argc; i++) {
358 const char *arg = argv[i];
359 if (arg[0] != '-')
360 break;
361 if (!strcmp(arg, "--")) {
362 i++;
363 break;
364 }
365 if (!strncmp(arg, "-n=", 3)) {
366 ret = para_atoi32(arg + 3, &num);
367 if (ret < 0)
368 return ret;
369 continue;
370 }
371 if (!strcmp(arg, "-p")) {
372 parser_friendly = 1;
373 continue;
374 }
375 return -E_COMMAND_SYNTAX;
376 }
377 if (i != argc)
378 return -E_COMMAND_SYNTAX;
379 for (;;) {
380 mmd_dup(nmmd);
381 s = get_status(nmmd, parser_friendly);
382 ret = rc4_send_buffer(rc4c, s);
383 free(s);
384 if (ret < 0)
385 goto out;
386 if (nmmd->vss_status_flags & VSS_NEXT) {
387 static char *esi;
388 if (!esi)
389 esi = empty_status_items(parser_friendly);
390 ret = rc4_send_buffer(rc4c, esi);
391 if (ret < 0)
392 goto out;
393 } else
394 send_afs_status(rc4c, parser_friendly);
395 ret = 1;
396 if (num > 0 && !--num)
397 goto out;
398 sleep(50);
399 if (getppid() == 1)
400 return -E_SERVER_CRASH;
401 }
402 out:
403 return ret;
404 }
405
406 static int send_list_of_commands(struct rc4_context *rc4c, struct server_command *cmd,
407 const char *handler)
408 {
409 int ret, i;
410
411 for (i = 1; cmd->name; cmd++, i++) {
412 char *perms = cmd_perms_itohuman(cmd->perms);
413 ret = rc4_send_va_buffer(rc4c, "%s\t%s\t%s\t%s\n", cmd->name,
414 handler,
415 perms,
416 cmd->description);
417 free(perms);
418 if (ret < 0)
419 return ret;
420 }
421 return 1;
422 }
423
424 /* returns string that must be freed by the caller */
425 static struct server_command *get_cmd_ptr(const char *name, char **handler)
426 {
427 struct server_command *cmd;
428
429 for (cmd = server_cmds; cmd->name; cmd++)
430 if (!strcmp(cmd->name, name)) {
431 if (handler)
432 *handler = para_strdup("server"); /* server commands */
433 return cmd;
434 }
435 /* not found, look for commands supported by afs */
436 for (cmd = afs_cmds; cmd->name; cmd++)
437 if (!strcmp(cmd->name, name)) {
438 if (handler)
439 *handler = para_strdup("afs");
440 return cmd;
441 }
442 return NULL;
443 }
444
445 /* help */
446 int com_help(struct rc4_context *rc4c, int argc, char * const * argv)
447 {
448 struct server_command *cmd;
449 char *perms, *handler;
450 int ret;
451
452 if (argc < 2) {
453 /* no argument given, print list of commands */
454 if ((ret = send_list_of_commands(rc4c, server_cmds, "server")) < 0)
455 return ret;
456 return send_list_of_commands(rc4c, afs_cmds, "afs");
457 }
458 /* argument given for help */
459 cmd = get_cmd_ptr(argv[1], &handler);
460 if (!cmd) {
461 free(handler);
462 return -E_BAD_CMD;
463 }
464 perms = cmd_perms_itohuman(cmd->perms);
465 ret = rc4_send_va_buffer(rc4c,
466 "%s - %s\n\n"
467 "handler: %s\n"
468 "permissions: %s\n"
469 "usage: %s\n\n"
470 "%s\n",
471 argv[1],
472 cmd->description,
473 handler,
474 perms,
475 cmd->usage,
476 cmd->help
477 );
478 free(perms);
479 free(handler);
480 return ret;
481 }
482
483 /* hup */
484 int com_hup(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
485 {
486 if (argc != 1)
487 return -E_COMMAND_SYNTAX;
488 kill(getppid(), SIGHUP);
489 return 1;
490 }
491
492 /* term */
493 int com_term(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
494 {
495 if (argc != 1)
496 return -E_COMMAND_SYNTAX;
497 kill(getppid(), SIGTERM);
498 return 1;
499 }
500
501 int com_play(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
502 {
503 if (argc != 1)
504 return -E_COMMAND_SYNTAX;
505 mutex_lock(mmd_mutex);
506 mmd->new_vss_status_flags |= VSS_PLAYING;
507 mmd->new_vss_status_flags &= ~VSS_NOMORE;
508 mutex_unlock(mmd_mutex);
509 return 1;
510
511 }
512
513 /* stop */
514 int com_stop(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
515 {
516 if (argc != 1)
517 return -E_COMMAND_SYNTAX;
518 mutex_lock(mmd_mutex);
519 mmd->new_vss_status_flags &= ~VSS_PLAYING;
520 mmd->new_vss_status_flags &= ~VSS_REPOS;
521 mmd->new_vss_status_flags |= VSS_NEXT;
522 mutex_unlock(mmd_mutex);
523 return 1;
524 }
525
526 /* pause */
527 int com_pause(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
528 {
529 if (argc != 1)
530 return -E_COMMAND_SYNTAX;
531 mutex_lock(mmd_mutex);
532 if (!vss_paused() && !vss_stopped()) {
533 mmd->events++;
534 mmd->new_vss_status_flags &= ~VSS_PLAYING;
535 mmd->new_vss_status_flags &= ~VSS_NEXT;
536 }
537 mutex_unlock(mmd_mutex);
538 return 1;
539 }
540
541 /* next */
542 int com_next(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
543 {
544 if (argc != 1)
545 return -E_COMMAND_SYNTAX;
546 mutex_lock(mmd_mutex);
547 mmd->events++;
548 mmd->new_vss_status_flags |= VSS_NEXT;
549 mutex_unlock(mmd_mutex);
550 return 1;
551 }
552
553 /* nomore */
554 int com_nomore(__a_unused struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
555 {
556 if (argc != 1)
557 return -E_COMMAND_SYNTAX;
558 mutex_lock(mmd_mutex);
559 if (vss_playing() || vss_paused())
560 mmd->new_vss_status_flags |= VSS_NOMORE;
561 mutex_unlock(mmd_mutex);
562 return 1;
563 }
564
565 /* ff */
566 int com_ff(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
567 {
568 long promille;
569 int ret, backwards = 0;
570 unsigned i;
571 char c;
572
573 if (argc != 2)
574 return -E_COMMAND_SYNTAX;
575 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
576 return -E_COMMAND_SYNTAX;
577 if (ret > 1 && c == '-')
578 backwards = 1; /* jmp backwards */
579 mutex_lock(mmd_mutex);
580 ret = -E_NO_AUDIO_FILE;
581 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
582 goto out;
583 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
584 if (backwards)
585 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
586 else
587 promille += 1000 * i / mmd->afd.afhi.seconds_total;
588 if (promille < 0)
589 promille = 0;
590 if (promille > 1000) {
591 mmd->new_vss_status_flags |= VSS_NEXT;
592 goto out;
593 }
594 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
595 mmd->new_vss_status_flags |= VSS_REPOS;
596 mmd->new_vss_status_flags &= ~VSS_NEXT;
597 mmd->events++;
598 ret = 1;
599 out:
600 mutex_unlock(mmd_mutex);
601 return ret;
602 }
603
604 /* jmp */
605 int com_jmp(__a_unused struct rc4_context *rc4c, int argc, char * const * argv)
606 {
607 long unsigned int i;
608 int ret;
609
610 if (argc != 2)
611 return -E_COMMAND_SYNTAX;
612 if (sscanf(argv[1], "%lu", &i) <= 0)
613 return -E_COMMAND_SYNTAX;
614 mutex_lock(mmd_mutex);
615 ret = -E_NO_AUDIO_FILE;
616 if (!mmd->afd.afhi.chunks_total)
617 goto out;
618 if (i > 100)
619 i = 100;
620 PARA_INFO_LOG("jumping to %lu%%\n", i);
621 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
622 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
623 mmd->chunks_sent, mmd->offset);
624 mmd->new_vss_status_flags |= VSS_REPOS;
625 mmd->new_vss_status_flags &= ~VSS_NEXT;
626 ret = 1;
627 mmd->events++;
628 out:
629 mutex_unlock(mmd_mutex);
630 return ret;
631 }
632
633 /*
634 * check if perms are sufficient to exec a command having perms cmd_perms.
635 * Returns 0 if perms are sufficient, -E_PERM otherwise.
636 */
637 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
638 {
639 PARA_DEBUG_LOG("checking permissions\n");
640 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
641 }
642
643 /*
644 * Parse first string from *cmd and lookup in table of valid commands.
645 * On error, NULL is returned.
646 */
647 static struct server_command *parse_cmd(const char *cmdstr)
648 {
649 char buf[255];
650 int n = 0;
651
652 sscanf(cmdstr, "%200s%n", buf, &n);
653 if (!n)
654 return NULL;
655 buf[n] = '\0';
656 return get_cmd_ptr(buf, NULL);
657 }
658
659 static int read_command(struct rc4_context *rc4c, char **result)
660 {
661 int ret;
662 char buf[4096];
663 char *command = NULL;
664
665 for (;;) {
666 size_t numbytes;
667 char *p;
668
669 ret = rc4_recv_buffer(rc4c, buf, sizeof(buf));
670 if (ret < 0)
671 goto out;
672 if (!ret)
673 break;
674 numbytes = ret;
675 ret = -E_COMMAND_SYNTAX;
676 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
677 goto out;
678 command = para_strcat(command, buf);
679 p = strstr(command, EOC_MSG);
680 if (p) {
681 *p = '\0';
682 break;
683 }
684 }
685 ret = command? 1 : -E_COMMAND_SYNTAX;
686 out:
687 if (ret < 0)
688 free(command);
689 else
690 *result = command;
691 return ret;
692
693 }
694
695 static void reset_signals(void)
696 {
697 para_sigaction(SIGCHLD, SIG_IGN);
698 para_sigaction(SIGINT, SIG_DFL);
699 para_sigaction(SIGTERM, SIG_DFL);
700 para_sigaction(SIGHUP, SIG_DFL);
701 }
702
703 /**
704 * Perform user authentication and execute a command.
705 *
706 * \param fd The file descriptor to send output to.
707 * \param peername Identifies the connecting peer.
708 *
709 * Whenever para_server accepts an incoming tcp connection on
710 * the port it listens on, it forks and the resulting child
711 * calls this function.
712 *
713 * An RSA-based challenge/response is used to authenticate
714 * the peer. It that authentication succeeds, a random RC4
715 * session key is generated and sent back to the peer,
716 * encrypted with its RSA public key. From this point on,
717 * all transfers are crypted with this session key.
718 *
719 * Next it is checked if the peer supplied a valid server command or a command
720 * for the audio file selector. If yes, and if the user has sufficient
721 * permissions to execute that command, the function calls the corresponding
722 * command handler which does argument checking and further processing.
723 *
724 * In order to cope with a DOS attacks, a timeout is set up
725 * which terminates the function if the connection was not
726 * authenticated when the timeout expires.
727 *
728 * \sa alarm(2), rc4(3), crypt.c, crypt.h
729 */
730 __noreturn void handle_connect(int fd, const char *peername)
731 {
732 int ret, argc;
733 char buf[4096];
734 unsigned char rand_buf[CHALLENGE_SIZE + 2 * RC4_KEY_LEN];
735 unsigned char challenge_sha1[HASH_SIZE];
736 struct user *u;
737 struct server_command *cmd = NULL;
738 char **argv = NULL;
739 char *p, *command = NULL;
740 size_t numbytes;
741 struct rc4_context rc4c = {.fd = fd};
742
743 reset_signals();
744 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
745 ret = mark_fd_blocking(fd);
746 if (ret < 0)
747 goto err_out;
748 /* send Welcome message */
749 ret = send_va_buffer(fd, "This is para_server, version "
750 PACKAGE_VERSION ".\n" );
751 if (ret < 0)
752 goto err_out;
753 /* recv auth request line */
754 ret = recv_buffer(fd, buf, sizeof(buf));
755 if (ret < 0)
756 goto err_out;
757 if (ret < 10) {
758 ret = -E_AUTH_REQUEST;
759 goto err_out;
760 }
761 numbytes = ret;
762 ret = -E_AUTH_REQUEST;
763 if (strncmp(buf, AUTH_REQUEST_MSG, strlen(AUTH_REQUEST_MSG)))
764 goto err_out;
765 p = buf + strlen(AUTH_REQUEST_MSG);
766 PARA_DEBUG_LOG("received auth request for user %s\n", p);
767 ret = -E_BAD_USER;
768 u = lookup_user(p);
769 if (u) {
770 get_random_bytes_or_die(rand_buf, sizeof(rand_buf));
771 ret = para_encrypt_buffer(u->rsa, rand_buf, sizeof(rand_buf),
772 (unsigned char *)buf);
773 if (ret < 0)
774 goto err_out;
775 numbytes = ret;
776 } else {
777 /*
778 * We don't want to reveal our user names, so we send a
779 * challenge to the client even if the user does not exist, and
780 * fail the authentication later.
781 */
782 numbytes = 256;
783 get_random_bytes_or_die((unsigned char *)buf, numbytes);
784 }
785 PARA_DEBUG_LOG("sending %u byte challenge + rc4 keys (%zu bytes)\n",
786 CHALLENGE_SIZE, numbytes);
787 ret = send_bin_buffer(fd, buf, numbytes);
788 if (ret < 0)
789 goto net_err;
790 /* recv challenge response */
791 ret = recv_bin_buffer(fd, buf, HASH_SIZE);
792 if (ret < 0)
793 goto net_err;
794 numbytes = ret;
795 PARA_DEBUG_LOG("received %d bytes challenge response\n", ret);
796 ret = -E_BAD_USER;
797 if (!u)
798 goto net_err;
799 /*
800 * The correct response is the sha1 of the first CHALLENGE_SIZE bytes
801 * of the random data.
802 */
803 ret = -E_BAD_AUTH;
804 if (numbytes != HASH_SIZE)
805 goto net_err;
806 sha1_hash((char *)rand_buf, CHALLENGE_SIZE, challenge_sha1);
807 if (memcmp(challenge_sha1, buf, HASH_SIZE))
808 goto net_err;
809 /* auth successful */
810 alarm(0);
811 PARA_INFO_LOG("good auth for %s\n", u->name);
812 /* init rc4 keys with the second part of the random buffer */
813 RC4_set_key(&rc4c.recv_key, RC4_KEY_LEN, rand_buf + CHALLENGE_SIZE);
814 RC4_set_key(&rc4c.send_key, RC4_KEY_LEN, rand_buf + CHALLENGE_SIZE
815 + RC4_KEY_LEN);
816 ret = rc4_send_buffer(&rc4c, PROCEED_MSG);
817 if (ret < 0)
818 goto net_err;
819 ret = read_command(&rc4c, &command);
820 if (ret == -E_COMMAND_SYNTAX)
821 goto err_out;
822 if (ret < 0)
823 goto net_err;
824 ret = -E_BAD_CMD;
825 cmd = parse_cmd(command);
826 if (!cmd)
827 goto err_out;
828 /* valid command, check permissions */
829 ret = check_perms(u->perms, cmd);
830 if (ret < 0)
831 goto err_out;
832 /* valid command and sufficient perms */
833 ret = create_argv(command, "\n", &argv);
834 if (ret < 0)
835 goto err_out;
836 argc = ret;
837 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
838 peername);
839 ret = cmd->handler(&rc4c, argc, argv);
840 free_argv(argv);
841 mutex_lock(mmd_mutex);
842 mmd->num_commands++;
843 mutex_unlock(mmd_mutex);
844 if (ret >= 0)
845 goto out;
846 err_out:
847 rc4_send_va_buffer(&rc4c, "%s\n", para_strerror(-ret));
848 net_err:
849 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
850 out:
851 free(command);
852 mutex_lock(mmd_mutex);
853 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
854 mmd->events++;
855 mmd->active_connections--;
856 mutex_unlock(mmd_mutex);
857 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
858 }