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