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