Kill duplicate audio format handler info string in stat output.
[paraslash.git] / command.c
1 /*
2 * Copyright (C) 1997-2007 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 "send.h"
24 #include "rc4.h"
25 #include "net.h"
26 #include "daemon.h"
27 #include "fd.h"
28 #include "list.h"
29 #include "user_list.h"
30 #include "server_command_list.h"
31 #include "afs_command_list.h"
32
33 /** Commands including options must be shorter than this. */
34 #define MAX_COMMAND_LEN 32768
35
36 static RC4_KEY rc4_recv_key;
37 static RC4_KEY rc4_send_key;
38 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
39
40 extern const char *status_item_list[NUM_STAT_ITEMS];
41 extern struct misc_meta_data *mmd;
42 extern struct sender senders[];
43
44 static void dummy(__a_unused int s)
45 {}
46
47 static void mmd_dup(struct misc_meta_data *new_mmd)
48 {
49 mmd_lock();
50 *new_mmd = *mmd;
51 mmd_unlock();
52 }
53
54 /*
55 * compute human readable string containing
56 * vss status for given integer value
57 */
58 static char *vss_status_tohuman(unsigned int flags)
59 {
60 if (flags & VSS_PLAYING)
61 return para_strdup("playing");
62 else if (flags & VSS_NEXT)
63 return para_strdup("stopped");
64 else
65 return para_strdup("paused");
66 }
67
68 /*
69 * return human readable permission string. Never returns NULL.
70 */
71 static char *cmd_perms_itohuman(unsigned int perms)
72 {
73 char *msg = para_malloc(5 * sizeof(char));
74
75 msg[0] = perms & AFS_READ? 'a' : '-';
76 msg[1] = perms & AFS_WRITE? 'A' : '-';
77 msg[2] = perms & VSS_READ? 'v' : '-';
78 msg[3] = perms & VSS_WRITE? 'V' : '-';
79 msg[4] = '\0';
80 return msg;
81 }
82
83 /*
84 * Never returns NULL.
85 */
86 static char *vss_get_status_flags(unsigned int flags)
87 {
88 char *msg = para_malloc(5 * sizeof(char));
89
90 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
91 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
92 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
93 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
94 msg[4] = '\0';
95 return msg;
96 }
97
98 static char *get_status(struct misc_meta_data *nmmd)
99 {
100 char *basename, *dirname, *ret, mtime[30] = "";
101 char *status, *flags; /* vss status info */
102 char *ut = uptime_str();
103 long offset = (nmmd->offset + 500) / 1000;
104 struct timeval now;
105 struct tm mtime_tm;
106
107 /* report real status */
108 status = vss_status_tohuman(nmmd->vss_status_flags);
109 flags = vss_get_status_flags(nmmd->vss_status_flags);
110 basename = para_basename(nmmd->afd.path);
111 dirname = para_dirname(nmmd->afd.path);
112 if (basename) {
113 localtime_r(&nmmd->mtime, &mtime_tm);
114 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
115 }
116 gettimeofday(&now, NULL);
117 ret = make_message(
118 "%s:%zu\n" "%s:%s\n" "%s:%lu\n"
119 "%s:%s\n" "%s:%s\n" "%s:%s\n"
120 "%s:%li\n" "%s:%s\n"
121 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n"
122 "%s:%s\n" "%s\n",
123 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
124 status_item_list[SI_MTIME], mtime,
125 status_item_list[SI_LENGTH], nmmd->afd.afhi.seconds_total,
126
127 status_item_list[SI_BASENAME], basename ? basename : "(none)",
128 status_item_list[SI_STATUS], status,
129 status_item_list[SI_STATUS_FLAGS], flags,
130
131 status_item_list[SI_OFFSET], offset,
132 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
133
134 status_item_list[SI_UPTIME], ut,
135 status_item_list[SI_STREAM_START],
136 (long unsigned)nmmd->stream_start.tv_sec,
137 (long unsigned)nmmd->stream_start.tv_usec,
138 status_item_list[SI_CURRENT_TIME],
139 (long unsigned)now.tv_sec,
140 (long unsigned)now.tv_usec,
141
142 status_item_list[SI_DIRECTORY], dirname? dirname : "(none)",
143 nmmd->afd.afs_status_info
144
145 );
146 free(basename);
147 free(dirname);
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 mmd_lock();
177 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
178 mmd_unlock();
179 return -E_SENDER_CMD;
180 }
181 mmd_unlock();
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 send_buffer(fd, msg);
244 free(msg);
245 return 1;
246 }
247 for (i = 0; i < 10; i++) {
248 mmd_lock();
249 if (mmd->sender_cmd_data.cmd_num >= 0) {
250 mmd_unlock();
251 usleep(100 * 1000);
252 continue;
253 }
254 mmd->sender_cmd_data = scd;
255 mmd_unlock();
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 mmd_lock();
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 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 mmd_unlock();
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 SYSTEM ", " 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 == 1)
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 mmd_lock();
446 mmd->new_vss_status_flags |= VSS_PLAYING;
447 mmd->new_vss_status_flags &= ~VSS_NOMORE;
448 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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 mmd_lock();
472 if (!vss_paused())
473 mmd->events++;
474 mmd->new_vss_status_flags &= ~VSS_PLAYING;
475 mmd->new_vss_status_flags &= ~VSS_NEXT;
476 mmd_unlock();
477 return 1;
478 }
479
480 /* next */
481 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
482 {
483 if (argc != 1)
484 return -E_COMMAND_SYNTAX;
485 mmd_lock();
486 mmd->events++;
487 mmd->new_vss_status_flags |= VSS_NEXT;
488 mmd_unlock();
489 return 1;
490 }
491
492 /* nomore */
493 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
494 {
495 if (argc != 1)
496 return -E_COMMAND_SYNTAX;
497 mmd_lock();
498 if (vss_playing() || vss_paused())
499 mmd->new_vss_status_flags |= VSS_NOMORE;
500 mmd_unlock();
501 return 1;
502 }
503
504 /* ff */
505 int com_ff(__a_unused int fd, int argc, char * const * argv)
506 {
507 long promille;
508 int ret, backwards = 0;
509 unsigned i;
510 char c;
511
512 if (argc != 2)
513 return -E_COMMAND_SYNTAX;
514 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
515 return -E_COMMAND_SYNTAX;
516 if (ret > 1 && c == '-')
517 backwards = 1; /* jmp backwards */
518 mmd_lock();
519 ret = -E_NO_AUDIO_FILE;
520 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
521 goto out;
522 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
523 if (backwards)
524 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
525 else
526 promille += 1000 * i / mmd->afd.afhi.seconds_total;
527 if (promille < 0)
528 promille = 0;
529 if (promille > 1000) {
530 mmd->new_vss_status_flags |= VSS_NEXT;
531 goto out;
532 }
533 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
534 mmd->new_vss_status_flags |= VSS_REPOS;
535 mmd->new_vss_status_flags &= ~VSS_NEXT;
536 mmd->events++;
537 ret = 1;
538 out:
539 mmd_unlock();
540 return ret;
541 }
542
543 /* jmp */
544 int com_jmp(__a_unused int fd, int argc, char * const * argv)
545 {
546 long unsigned int i;
547 int ret;
548
549 if (argc != 2)
550 return -E_COMMAND_SYNTAX;
551 if (sscanf(argv[1], "%lu", &i) <= 0)
552 return -E_COMMAND_SYNTAX;
553 mmd_lock();
554 ret = -E_NO_AUDIO_FILE;
555 if (!mmd->afd.afhi.chunks_total)
556 goto out;
557 if (i > 100)
558 i = 100;
559 PARA_INFO_LOG("jumping to %lu%%\n", i);
560 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
561 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
562 mmd->chunks_sent, mmd->offset);
563 mmd->new_vss_status_flags |= VSS_REPOS;
564 mmd->new_vss_status_flags &= ~VSS_NEXT;
565 ret = 1;
566 mmd->events++;
567 out:
568 mmd_unlock();
569 return ret;
570 }
571
572 /*
573 * check if perms are sufficient to exec a command having perms cmd_perms.
574 * Returns 0 if perms are sufficient, -E_PERM otherwise.
575 */
576 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
577 {
578 PARA_DEBUG_LOG("%s", "checking permissions\n");
579 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
580 }
581
582 /*
583 * Parse first string from *cmd and lookup in table of valid commands.
584 * On error, NULL is returned.
585 */
586 static struct server_command *parse_cmd(const char *cmdstr)
587 {
588 char buf[255];
589 int n = 0;
590
591 sscanf(cmdstr, "%200s%n", buf, &n);
592 if (!n)
593 return NULL;
594 buf[n] = '\0';
595 return get_cmd_ptr(buf, NULL);
596 }
597
598 static void init_rc4_keys(void)
599 {
600 int i;
601
602 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
603 rc4_buf[i] = para_random(256);
604 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
605 (unsigned char) rc4_buf[0],
606 (unsigned char) rc4_buf[RC4_KEY_LEN]);
607 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
608 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
609 }
610
611 static void rc4_recv(unsigned long len, const unsigned char *indata,
612 unsigned char *outdata, __a_unused void *private_data)
613 {
614 RC4(&rc4_recv_key, len, indata, outdata);
615 }
616
617 static void rc4_send(unsigned long len, const unsigned char *indata,
618 unsigned char *outdata, __a_unused void *private_data)
619 {
620 RC4(&rc4_send_key, len, indata, outdata);
621 }
622
623 static int read_command(int fd, char **result)
624 {
625 int ret;
626 char buf[4096];
627 char *command = NULL;
628
629 for (;;) {
630 size_t numbytes;
631 char *p;
632
633 ret = recv_buffer(fd, buf, sizeof(buf));
634 if (ret < 0)
635 goto out;
636 if (!ret)
637 break;
638 numbytes = ret;
639 ret = -E_COMMAND_SYNTAX;
640 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
641 goto out;
642 command = para_strcat(command, buf);
643 p = strstr(command, EOC_MSG);
644 if (p) {
645 *p = '\0';
646 break;
647 }
648 }
649 ret = command? 1 : -E_COMMAND_SYNTAX;
650 out:
651 if (ret < 0)
652 free(command);
653 else
654 *result = command;
655 return ret;
656
657 }
658
659 /**
660 * perform user authentication and execute a command
661 *
662 * \param fd the file descriptor to send output to
663 * \param addr socket address info of peer
664 *
665 * \return EXIT_SUCCESS or EXIT_FAILURE
666 *
667 * Whenever para_server accepts an incoming tcp connection on
668 * the port it listens on, it forks and the resulting child
669 * calls this function.
670 *
671 * An RSA-based challenge/response is used to authenticate
672 * the peer. It that authentication succeeds, a random RC4
673 * session key is generated and sent back to the peer,
674 * encrypted with its RSA public key. From this point on,
675 * all transfers are crypted with this session key.
676 *
677 * Next it is checked if the peer supplied a valid server command or a command
678 * for the audio file selector. If yes, and if the user has sufficient
679 * permissions to execute that command, the function calls the corresponding
680 * command handler which does argument checking and further processing.
681 *
682 * In order to cope with a DOS attacks, a timeout is set up
683 * which terminates the function if the connection was not
684 * authenticated when the timeout expires.
685 *
686 * \sa alarm(2), rc4(3), crypt.c, crypt.h
687 */
688 int handle_connect(int fd, struct sockaddr_in *addr)
689 {
690 int ret, argc, use_rc4 = 0;
691 char buf[4096];
692 unsigned char crypt_buf[MAXLINE];
693 struct user *u;
694 struct server_command *cmd = NULL;
695 long unsigned challenge_nr, chall_response;
696 char **argv = NULL;
697 char *p, *command = NULL;
698 size_t numbytes;
699
700 signal(SIGCHLD, SIG_IGN);
701 signal(SIGINT, SIG_DFL);
702 signal(SIGTERM, SIG_DFL);
703 signal(SIGHUP, SIG_DFL);
704 signal(SIGUSR1, SIG_IGN);
705
706 challenge_nr = random();
707 /* send Welcome message */
708 ret = send_va_buffer(fd, "This is para_server, version "
709 PACKAGE_VERSION ".\n" );
710 if (ret < 0)
711 goto err_out;
712 /* recv auth request line */
713 ret = recv_buffer(fd, buf, sizeof(buf));
714 if (ret < 0)
715 goto err_out;
716 if (ret <= 6) {
717 ret = -E_AUTH;
718 goto err_out;
719 }
720 numbytes = ret;
721 ret = -E_AUTH;
722 if (strncmp(buf, "auth ", 5))
723 goto err_out;
724
725 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
726 p = buf + 5; /* client version < 0.2.6 */
727 else {
728 p = buf + 9; /* client version >= 0.2.6 */
729 use_rc4 = 1;
730 }
731 PARA_DEBUG_LOG("received %s request for user %s\n",
732 use_rc4? "rc4" : "auth", p);
733 ret = -E_BAD_USER;
734 u = lookup_user(p);
735 if (!u)
736 goto err_out;
737 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
738 if (ret <= 0)
739 goto err_out;
740 numbytes = ret;
741 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
742 /* We can't use send_buffer here since buf may contain null bytes */
743 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
744 if (ret < 0)
745 goto err_out;
746 /* recv decrypted number */
747 ret = recv_buffer(fd, buf, sizeof(buf));
748 if (ret < 0)
749 goto err_out;
750 numbytes = ret;
751 ret = -E_AUTH;
752 if (!numbytes)
753 goto err_out;
754 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
755 || chall_response != challenge_nr)
756 goto err_out;
757 /* auth successful, send 'Proceed' message */
758 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
759 sprintf(buf, "%s", PROCEED_MSG);
760 if (use_rc4) {
761 init_rc4_keys();
762 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
763 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
764 if (ret <= 0)
765 goto err_out;
766 numbytes = ret + strlen(PROCEED_MSG) + 1;
767 } else
768 numbytes = strlen(buf);
769 ret = send_bin_buffer(fd, buf, numbytes);
770 if (ret < 0)
771 goto err_out;
772 if (use_rc4)
773 enable_crypt(fd, rc4_recv, rc4_send, NULL);
774 ret = read_command(fd, &command);
775 if (ret < 0)
776 goto err_out;
777 ret = -E_BAD_CMD;
778 cmd = parse_cmd(command);
779 if (!cmd)
780 goto err_out;
781 /* valid command, check permissions */
782 ret = check_perms(u->perms, cmd);
783 if (ret < 0)
784 goto err_out;
785 /* valid command and sufficient perms */
786 alarm(0);
787 argc = split_args(command, &argv, "\n");
788 mmd_lock();
789 mmd->num_commands++;
790 mmd_unlock();
791 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
792 inet_ntoa(addr->sin_addr));
793 ret = cmd->handler(fd, argc, argv);
794 if (ret >= 0) {
795 ret = EXIT_SUCCESS;
796 goto out;
797 }
798 err_out:
799 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
800 if (ret != -E_SEND && ret != -E_RECV)
801 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
802 ret = EXIT_FAILURE;
803 out:
804 free(command);
805 free(argv);
806 mmd_lock();
807 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
808 mmd->events++;
809 mmd->active_connections--;
810 mmd_unlock();
811 return ret;
812 }