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