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