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