command.c: Remove a useless use of the mmd lock.
[paraslash.git] / command.c
1 /*
2 * Copyright (C) 1997-2008 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 "server.cmdline.h"
18 #include "string.h"
19 #include "afh.h"
20 #include "afs.h"
21 #include "server.h"
22 #include "vss.h"
23 #include "list.h"
24 #include "send.h"
25 #include "rc4.h"
26 #include "net.h"
27 #include "daemon.h"
28 #include "fd.h"
29 #include "ipc.h"
30 #include "user_list.h"
31 #include "server_command_list.h"
32 #include "afs_command_list.h"
33
34 /** Commands including options must be shorter than this. */
35 #define MAX_COMMAND_LEN 32768
36
37 static RC4_KEY rc4_recv_key;
38 static RC4_KEY rc4_send_key;
39 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
40
41 extern int mmd_mutex;
42 extern struct misc_meta_data *mmd;
43 extern struct sender senders[];
44
45 static void dummy(int s)
46 {
47 /*
48 * At least on Solaris, SIGUSR1 is one-shot, i.e. the signal action is
49 * restored to the default state once the signal handler has been
50 * called.
51 */
52 if (s == SIGUSR1)
53 signal(SIGUSR1, dummy);
54 }
55
56 static void mmd_dup(struct misc_meta_data *new_mmd)
57 {
58 mutex_lock(mmd_mutex);
59 *new_mmd = *mmd;
60 mutex_unlock(mmd_mutex);
61 }
62
63 /*
64 * compute human readable string containing
65 * vss status for given integer value
66 */
67 static char *vss_status_tohuman(unsigned int flags)
68 {
69 if (flags & VSS_PLAYING)
70 return para_strdup("playing");
71 else if (flags & VSS_NEXT)
72 return para_strdup("stopped");
73 else
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)
108 {
109 char *ret, mtime[30] = "";
110 char *status, *flags; /* vss status info */
111 char *ut = uptime_str();
112 long offset = (nmmd->offset + 500) / 1000;
113 struct timeval now;
114 struct tm mtime_tm;
115
116 /* report real status */
117 status = vss_status_tohuman(nmmd->vss_status_flags);
118 flags = vss_get_status_flags(nmmd->vss_status_flags);
119 if (nmmd->size) { /* parent currently has an audio file open */
120 localtime_r(&nmmd->mtime, &mtime_tm);
121 strftime(mtime, 29, "%b %d %Y", &mtime_tm);
122 }
123 gettimeofday(&now, NULL);
124 ret = make_message(
125 "%s: %zu\n" /* file size */
126 "%s: %s\n" /* mtime */
127 "%s: %s\n" /* status */
128 "%s: %s\n" /* status flags */
129 "%s: %li\n" /* offset */
130 "%s: %s\n" /* afs mode */
131 "%s: %lu.%lu\n" /* stream start */
132 "%s: %lu.%lu\n" /* current server time */
133 "%s", /* afs status info */
134 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
135 status_item_list[SI_MTIME], mtime,
136 status_item_list[SI_STATUS], status,
137 status_item_list[SI_STATUS_FLAGS], flags,
138
139 status_item_list[SI_OFFSET], offset,
140 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
141
142 status_item_list[SI_STREAM_START],
143 (long unsigned)nmmd->stream_start.tv_sec,
144 (long unsigned)nmmd->stream_start.tv_usec,
145 status_item_list[SI_CURRENT_TIME],
146 (long unsigned)now.tv_sec,
147 (long unsigned)now.tv_usec,
148
149 nmmd->afd.verbose_ls_output
150
151 );
152 free(flags);
153 free(status);
154 free(ut);
155 return ret;
156 }
157
158 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
159 {
160 int i;
161 /* this has to match sender.h */
162 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
163
164 scd->sender_num = -1;
165 if (argc < 2)
166 return -E_COMMAND_SYNTAX;
167 for (i = 0; senders[i].name; i++)
168 if (!strcmp(senders[i].name, argv[1]))
169 break;
170 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
171 if (!senders[i].name)
172 return -E_COMMAND_SYNTAX;
173 scd->sender_num = i;
174 for (i = 0; subcmds[i]; i++)
175 if (!strcmp(subcmds[i], argv[2]))
176 break;
177 if (!subcmds[i])
178 return -E_COMMAND_SYNTAX;
179 scd->cmd_num = i;
180 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
181 return -E_SENDER_CMD;
182 switch (scd->cmd_num) {
183 case SENDER_ON:
184 case SENDER_OFF:
185 if (argc != 3)
186 return -E_COMMAND_SYNTAX;
187 break;
188 case SENDER_DENY:
189 case SENDER_ALLOW:
190 if (argc != 4 && argc != 5)
191 return -E_COMMAND_SYNTAX;
192 if (!inet_pton(AF_INET, argv[3], &scd->addr))
193 return -E_COMMAND_SYNTAX;
194 scd->netmask = 32;
195 if (argc == 5) {
196 scd->netmask = atoi(argv[4]);
197 if (scd->netmask < 0 || scd->netmask > 32)
198 return -E_COMMAND_SYNTAX;
199 }
200 break;
201 case SENDER_ADD:
202 case SENDER_DELETE:
203 if (argc != 4 && argc != 5)
204 return -E_COMMAND_SYNTAX;
205 if (!inet_pton(AF_INET, argv[3], &scd->addr))
206 return -E_COMMAND_SYNTAX;
207 scd->port = -1;
208 if (argc == 5) {
209 scd->port = atoi(argv[4]);
210 if (scd->port < 0 || scd->port > 65535)
211 return -E_COMMAND_SYNTAX;
212 }
213 break;
214 default:
215 return -E_COMMAND_SYNTAX;
216 }
217 return 1;
218 }
219
220 int com_sender(int fd, int argc, char * const * argv)
221 {
222 int i, ret;
223 struct sender_command_data scd;
224
225 if (argc < 2) {
226 char *msg = NULL;
227 for (i = 0; senders[i].name; i++) {
228 char *tmp = make_message("%s%s\n",
229 msg? msg : "", senders[i].name);
230 free(msg);
231 msg = tmp;
232 }
233 ret = send_buffer(fd, msg);
234 free(msg);
235 return ret;
236 }
237 ret = check_sender_args(argc, argv, &scd);
238 if (ret < 0) {
239 char *msg;
240 if (scd.sender_num < 0)
241 return ret;
242 msg = senders[scd.sender_num].help();
243 ret = send_buffer(fd, msg);
244 free(msg);
245 return ret;
246 }
247 for (i = 0; i < 10; i++) {
248 mutex_lock(mmd_mutex);
249 if (mmd->sender_cmd_data.cmd_num >= 0) {
250 mutex_unlock(mmd_mutex);
251 usleep(100 * 1000);
252 continue;
253 }
254 mmd->sender_cmd_data = scd;
255 mutex_unlock(mmd_mutex);
256 break;
257 }
258 return (i < 10)? 1 : -E_LOCK;
259 }
260
261 /* server info */
262 int com_si(int fd, int argc, __a_unused char * const * argv)
263 {
264 int i, ret;
265 char *ut;
266 char *sender_info = NULL, *sender_list = NULL;
267
268 if (argc != 1)
269 return -E_COMMAND_SYNTAX;
270 mutex_lock(mmd_mutex);
271 for (i = 0; senders[i].name; i++) {
272 char *info = senders[i].info();
273 sender_info = para_strcat(sender_info, info);
274 free(info);
275 sender_list = para_strcat(sender_list, senders[i].name);
276 sender_list = para_strcat(sender_list, " ");
277 }
278 ut = uptime_str();
279 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
280 "pid: %d\n"
281 "connections (active/accepted/total): %u/%u/%u\n"
282 "current loglevel: %i\n"
283 "supported audio formats: %s\n"
284 "supported senders: %s\n"
285 "%s",
286 ut, mmd->num_played,
287 (int)getppid(),
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(int fd, int argc, __a_unused char * const * argv)
305 {
306 if (argc != 1)
307 return -E_COMMAND_SYNTAX;
308 return send_buffer(fd, VERSION_TEXT("server")
309 "built: " BUILD_DATE "\n"
310 UNAME_RS ", " CC_VERSION "\n"
311 );
312 }
313
314 /* stat */
315 int com_stat(int fd, 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 signal(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 = send_buffer(fd, 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(int fd, 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 = send_va_buffer(fd, "%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(int fd, 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(fd, server_cmds, "server")) < 0)
395 return ret;
396 return send_list_of_commands(fd, 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 = send_va_buffer(fd,
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 int fd, 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 int fd, 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 int fd, 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 int fd, 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 int fd, 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 int fd, 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 int fd, 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 int fd, 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 int fd, 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 void init_rc4_keys(void)
600 {
601 int i;
602
603 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
604 rc4_buf[i] = para_random(256);
605 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
606 (unsigned char) rc4_buf[0],
607 (unsigned char) rc4_buf[RC4_KEY_LEN]);
608 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
609 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
610 }
611
612 static void rc4_recv(unsigned long len, const unsigned char *indata,
613 unsigned char *outdata, __a_unused void *private_data)
614 {
615 RC4(&rc4_recv_key, len, indata, outdata);
616 }
617
618 static void rc4_send(unsigned long len, const unsigned char *indata,
619 unsigned char *outdata, __a_unused void *private_data)
620 {
621 RC4(&rc4_send_key, len, indata, outdata);
622 }
623
624 static int read_command(int fd, char **result)
625 {
626 int ret;
627 char buf[4096];
628 char *command = NULL;
629
630 for (;;) {
631 size_t numbytes;
632 char *p;
633
634 ret = recv_buffer(fd, buf, sizeof(buf));
635 if (ret < 0)
636 goto out;
637 if (!ret)
638 break;
639 numbytes = ret;
640 ret = -E_COMMAND_SYNTAX;
641 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
642 goto out;
643 command = para_strcat(command, buf);
644 p = strstr(command, EOC_MSG);
645 if (p) {
646 *p = '\0';
647 break;
648 }
649 }
650 ret = command? 1 : -E_COMMAND_SYNTAX;
651 out:
652 if (ret < 0)
653 free(command);
654 else
655 *result = command;
656 return ret;
657
658 }
659
660 /**
661 * Perform user authentication and execute a command.
662 *
663 * \param fd The file descriptor to send output to.
664 * \param peername Identifies the connecting peer.
665 *
666 * Whenever para_server accepts an incoming tcp connection on
667 * the port it listens on, it forks and the resulting child
668 * calls this function.
669 *
670 * An RSA-based challenge/response is used to authenticate
671 * the peer. It that authentication succeeds, a random RC4
672 * session key is generated and sent back to the peer,
673 * encrypted with its RSA public key. From this point on,
674 * all transfers are crypted with this session key.
675 *
676 * Next it is checked if the peer supplied a valid server command or a command
677 * for the audio file selector. If yes, and if the user has sufficient
678 * permissions to execute that command, the function calls the corresponding
679 * command handler which does argument checking and further processing.
680 *
681 * In order to cope with a DOS attacks, a timeout is set up
682 * which terminates the function if the connection was not
683 * authenticated when the timeout expires.
684 *
685 * \sa alarm(2), rc4(3), crypt.c, crypt.h
686 */
687 __noreturn void handle_connect(int fd, const char *peername)
688 {
689 int ret, argc, use_rc4 = 0;
690 char buf[4096];
691 unsigned char crypt_buf[MAXLINE];
692 struct user *u;
693 struct server_command *cmd = NULL;
694 long unsigned challenge_nr, chall_response;
695 char **argv = NULL;
696 char *p, *command = NULL;
697 size_t numbytes;
698
699 signal(SIGCHLD, SIG_IGN);
700 signal(SIGINT, SIG_DFL);
701 signal(SIGTERM, SIG_DFL);
702 signal(SIGHUP, SIG_DFL);
703
704 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
705 ret = mark_fd_blocking(fd);
706 if (ret < 0)
707 goto err_out;
708 challenge_nr = random();
709 /* send Welcome message */
710 ret = send_va_buffer(fd, "This is para_server, version "
711 PACKAGE_VERSION ".\n" );
712 if (ret < 0)
713 goto err_out;
714 /* recv auth request line */
715 ret = recv_buffer(fd, buf, sizeof(buf));
716 if (ret < 0)
717 goto err_out;
718 if (ret <= 6) {
719 ret = -E_AUTH;
720 goto err_out;
721 }
722 numbytes = ret;
723 ret = -E_AUTH;
724 if (strncmp(buf, "auth ", 5))
725 goto err_out;
726
727 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
728 p = buf + 5; /* client version < 0.2.6 */
729 else {
730 p = buf + 9; /* client version >= 0.2.6 */
731 use_rc4 = 1;
732 }
733 PARA_DEBUG_LOG("received %s request for user %s\n",
734 use_rc4? "rc4" : "auth", p);
735 ret = -E_BAD_USER;
736 u = lookup_user(p);
737 if (!u)
738 goto err_out;
739 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
740 if (ret <= 0)
741 goto err_out;
742 numbytes = ret;
743 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
744 /* We can't use send_buffer here since buf may contain null bytes */
745 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
746 if (ret < 0)
747 goto net_err;
748 /* recv decrypted number */
749 ret = recv_buffer(fd, buf, sizeof(buf));
750 if (ret < 0)
751 goto net_err;
752 numbytes = ret;
753 ret = -E_AUTH;
754 if (!numbytes)
755 goto net_err;
756 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
757 || chall_response != challenge_nr)
758 goto err_out;
759 /* auth successful, send 'Proceed' message */
760 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
761 sprintf(buf, "%s", PROCEED_MSG);
762 if (use_rc4) {
763 init_rc4_keys();
764 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
765 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
766 if (ret <= 0)
767 goto err_out;
768 numbytes = ret + strlen(PROCEED_MSG) + 1;
769 } else
770 numbytes = strlen(buf);
771 ret = send_bin_buffer(fd, buf, numbytes);
772 if (ret < 0)
773 goto net_err;
774 if (use_rc4)
775 enable_crypt(fd, rc4_recv, rc4_send, NULL);
776 ret = read_command(fd, &command);
777 if (ret == -E_COMMAND_SYNTAX)
778 goto err_out;
779 if (ret < 0)
780 goto net_err;
781 ret = -E_BAD_CMD;
782 cmd = parse_cmd(command);
783 if (!cmd)
784 goto err_out;
785 /* valid command, check permissions */
786 ret = check_perms(u->perms, cmd);
787 if (ret < 0)
788 goto err_out;
789 /* valid command and sufficient perms */
790 alarm(0);
791 argc = split_args(command, &argv, "\n");
792 mutex_lock(mmd_mutex);
793 mmd->num_commands++;
794 mutex_unlock(mmd_mutex);
795 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
796 peername);
797 ret = cmd->handler(fd, argc, argv);
798 if (ret >= 0)
799 goto out;
800 err_out:
801 send_va_buffer(fd, "%s\n", para_strerror(-ret));
802 net_err:
803 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
804 out:
805 free(command);
806 free(argv);
807 mutex_lock(mmd_mutex);
808 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
809 mmd->events++;
810 mmd->active_connections--;
811 mutex_unlock(mmd_mutex);
812 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
813 }