Kill duplicate num_played status item.
[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" "%s"
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 nmmd->afd.afhi.info_string,
134
135 status_item_list[SI_UPTIME], ut,
136 status_item_list[SI_STREAM_START],
137 (long unsigned)nmmd->stream_start.tv_sec,
138 (long unsigned)nmmd->stream_start.tv_usec,
139 status_item_list[SI_CURRENT_TIME],
140 (long unsigned)now.tv_sec,
141 (long unsigned)now.tv_usec,
142
143 status_item_list[SI_DIRECTORY], dirname? dirname : "(none)",
144 nmmd->afd.afs_status_info
145
146 );
147 free(basename);
148 free(dirname);
149 free(flags);
150 free(status);
151 free(ut);
152 return ret;
153 }
154
155 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
156 {
157 int i;
158 /* this has to match sender.h */
159 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
160
161 scd->sender_num = -1;
162 if (argc < 2)
163 return -E_COMMAND_SYNTAX;
164 for (i = 0; senders[i].name; i++)
165 if (!strcmp(senders[i].name, argv[1]))
166 break;
167 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
168 if (!senders[i].name)
169 return -E_COMMAND_SYNTAX;
170 scd->sender_num = i;
171 for (i = 0; subcmds[i]; i++)
172 if (!strcmp(subcmds[i], argv[2]))
173 break;
174 if (!subcmds[i])
175 return -E_COMMAND_SYNTAX;
176 scd->cmd_num = i;
177 mmd_lock();
178 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
179 mmd_unlock();
180 return -E_SENDER_CMD;
181 }
182 mmd_unlock();
183 switch (scd->cmd_num) {
184 case SENDER_ON:
185 case SENDER_OFF:
186 if (argc != 3)
187 return -E_COMMAND_SYNTAX;
188 break;
189 case SENDER_DENY:
190 case SENDER_ALLOW:
191 if (argc != 4 && argc != 5)
192 return -E_COMMAND_SYNTAX;
193 if (!inet_pton(AF_INET, argv[3], &scd->addr))
194 return -E_COMMAND_SYNTAX;
195 scd->netmask = 32;
196 if (argc == 5) {
197 scd->netmask = atoi(argv[4]);
198 if (scd->netmask < 0 || scd->netmask > 32)
199 return -E_COMMAND_SYNTAX;
200 }
201 break;
202 case SENDER_ADD:
203 case SENDER_DELETE:
204 if (argc != 4 && argc != 5)
205 return -E_COMMAND_SYNTAX;
206 if (!inet_pton(AF_INET, argv[3], &scd->addr))
207 return -E_COMMAND_SYNTAX;
208 scd->port = -1;
209 if (argc == 5) {
210 scd->port = atoi(argv[4]);
211 if (scd->port < 0 || scd->port > 65535)
212 return -E_COMMAND_SYNTAX;
213 }
214 break;
215 default:
216 return -E_COMMAND_SYNTAX;
217 }
218 return 1;
219 }
220
221 int com_sender(int fd, int argc, char * const * argv)
222 {
223 int i, ret;
224 struct sender_command_data scd;
225
226 if (argc < 2) {
227 char *msg = NULL;
228 for (i = 0; senders[i].name; i++) {
229 char *tmp = make_message("%s%s\n",
230 msg? msg : "", senders[i].name);
231 free(msg);
232 msg = tmp;
233 }
234 ret = send_buffer(fd, msg);
235 free(msg);
236 return ret;
237 }
238 ret = check_sender_args(argc, argv, &scd);
239 if (ret < 0) {
240 char *msg;
241 if (scd.sender_num < 0)
242 return ret;
243 msg = senders[scd.sender_num].help();
244 send_buffer(fd, msg);
245 free(msg);
246 return 1;
247 }
248 for (i = 0; i < 10; i++) {
249 mmd_lock();
250 if (mmd->sender_cmd_data.cmd_num >= 0) {
251 mmd_unlock();
252 usleep(100 * 1000);
253 continue;
254 }
255 mmd->sender_cmd_data = scd;
256 mmd_unlock();
257 break;
258 }
259 return (i < 10)? 1 : -E_LOCK;
260 }
261
262 /* server info */
263 int com_si(int fd, int argc, __a_unused char * const * argv)
264 {
265 int i, ret;
266 char *ut;
267 char *sender_info = NULL, *sender_list = NULL;
268
269 if (argc != 1)
270 return -E_COMMAND_SYNTAX;
271 mmd_lock();
272 for (i = 0; senders[i].name; i++) {
273 char *info = senders[i].info();
274 sender_info = para_strcat(sender_info, info);
275 free(info);
276 sender_list = para_strcat(sender_list, senders[i].name);
277 sender_list = para_strcat(sender_list, " ");
278 }
279 ut = uptime_str();
280 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
281 "pid: %d\n"
282 "connections (active/accepted/total): %u/%u/%u\n"
283 "current loglevel: %i\n"
284 "supported audio formats: %s\n"
285 "supported senders: %s\n"
286 "%s",
287 ut, mmd->num_played,
288 getppid(),
289 mmd->active_connections,
290 mmd->num_commands,
291 mmd->num_connects,
292 conf.loglevel_arg,
293 supported_audio_formats(),
294 sender_list,
295 sender_info
296 );
297 mmd_unlock();
298 free(ut);
299 free(sender_list);
300 free(sender_info);
301 return ret;
302 }
303
304 /* version */
305 int com_version(int fd, int argc, __a_unused char * const * argv)
306 {
307 if (argc != 1)
308 return -E_COMMAND_SYNTAX;
309 return send_buffer(fd, VERSION_TEXT("server")
310 "built: " BUILD_DATE "\n"
311 SYSTEM ", " CC_VERSION "\n"
312 );
313 }
314
315 /* stat */
316 int com_stat(int fd, int argc, char * const * argv)
317 {
318 int ret, num = 0;/* status will be printed that many
319 * times. num <= 0 means: print forever
320 */
321 struct misc_meta_data tmp, *nmmd = &tmp;
322 char *s;
323
324 signal(SIGUSR1, dummy);
325
326 if (argc > 1)
327 num = atoi(argv[1]);
328 for (;;) {
329
330 mmd_dup(nmmd);
331 s = get_status(nmmd);
332 ret = send_buffer(fd, s);
333 free(s);
334 if (ret < 0)
335 goto out;
336 ret = 1;
337 if (num == 1)
338 goto out;
339 sleep(50);
340 if (getppid() == 1)
341 return -E_SERVER_CRASH;
342 }
343 out:
344 return ret;
345 }
346
347 static int send_list_of_commands(int fd, struct server_command *cmd,
348 const char *handler)
349 {
350 int ret, i;
351
352 for (i = 1; cmd->name; cmd++, i++) {
353 char *perms = cmd_perms_itohuman(cmd->perms);
354 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
355 handler,
356 perms,
357 cmd->description);
358 free(perms);
359 if (ret < 0)
360 return ret;
361 }
362 return 1;
363 }
364
365 /* returns string that must be freed by the caller */
366 static struct server_command *get_cmd_ptr(const char *name, char **handler)
367 {
368 struct server_command *cmd;
369
370 for (cmd = server_cmds; cmd->name; cmd++)
371 if (!strcmp(cmd->name, name)) {
372 if (handler)
373 *handler = para_strdup("server"); /* server commands */
374 return cmd;
375 }
376 /* not found, look for commands supported by afs */
377 for (cmd = afs_cmds; cmd->name; cmd++)
378 if (!strcmp(cmd->name, name)) {
379 if (handler)
380 *handler = para_strdup("afs");
381 return cmd;
382 }
383 return NULL;
384 }
385
386 /* help */
387 int com_help(int fd, int argc, char * const * argv)
388 {
389 struct server_command *cmd;
390 char *perms, *handler;
391 int ret;
392
393 if (argc < 2) {
394 /* no argument given, print list of commands */
395 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
396 return ret;
397 return send_list_of_commands(fd, afs_cmds, "afs");
398 }
399 /* argument given for help */
400 cmd = get_cmd_ptr(argv[1], &handler);
401 if (!cmd) {
402 free(handler);
403 return -E_BAD_CMD;
404 }
405 perms = cmd_perms_itohuman(cmd->perms);
406 ret = send_va_buffer(fd,
407 "%s - %s\n\n"
408 "handler: %s\n"
409 "permissions: %s\n"
410 "usage: %s\n\n"
411 "%s\n",
412 argv[1],
413 cmd->description,
414 handler,
415 perms,
416 cmd->usage,
417 cmd->help
418 );
419 free(perms);
420 free(handler);
421 return ret;
422 }
423
424 /* hup */
425 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
426 {
427 if (argc != 1)
428 return -E_COMMAND_SYNTAX;
429 kill(getppid(), SIGHUP);
430 return 1;
431 }
432
433 /* term */
434 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
435 {
436 if (argc != 1)
437 return -E_COMMAND_SYNTAX;
438 kill(getppid(), SIGTERM);
439 return 1;
440 }
441
442 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
443 {
444 if (argc != 1)
445 return -E_COMMAND_SYNTAX;
446 mmd_lock();
447 mmd->new_vss_status_flags |= VSS_PLAYING;
448 mmd->new_vss_status_flags &= ~VSS_NOMORE;
449 mmd_unlock();
450 return 1;
451
452 }
453
454 /* stop */
455 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
456 {
457 if (argc != 1)
458 return -E_COMMAND_SYNTAX;
459 mmd_lock();
460 mmd->new_vss_status_flags &= ~VSS_PLAYING;
461 mmd->new_vss_status_flags &= ~VSS_REPOS;
462 mmd->new_vss_status_flags |= VSS_NEXT;
463 mmd_unlock();
464 return 1;
465 }
466
467 /* pause */
468 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
469 {
470 if (argc != 1)
471 return -E_COMMAND_SYNTAX;
472 mmd_lock();
473 if (!vss_paused())
474 mmd->events++;
475 mmd->new_vss_status_flags &= ~VSS_PLAYING;
476 mmd->new_vss_status_flags &= ~VSS_NEXT;
477 mmd_unlock();
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 mmd_lock();
487 mmd->events++;
488 mmd->new_vss_status_flags |= VSS_NEXT;
489 mmd_unlock();
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 mmd_lock();
499 if (vss_playing() || vss_paused())
500 mmd->new_vss_status_flags |= VSS_NOMORE;
501 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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("%s", "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 addr socket address info of peer
665 *
666 * \return EXIT_SUCCESS or EXIT_FAILURE
667 *
668 * Whenever para_server accepts an incoming tcp connection on
669 * the port it listens on, it forks and the resulting child
670 * calls this function.
671 *
672 * An RSA-based challenge/response is used to authenticate
673 * the peer. It that authentication succeeds, a random RC4
674 * session key is generated and sent back to the peer,
675 * encrypted with its RSA public key. From this point on,
676 * all transfers are crypted with this session key.
677 *
678 * Next it is checked if the peer supplied a valid server command or a command
679 * for the audio file selector. If yes, and if the user has sufficient
680 * permissions to execute that command, the function calls the corresponding
681 * command handler which does argument checking and further processing.
682 *
683 * In order to cope with a DOS attacks, a timeout is set up
684 * which terminates the function if the connection was not
685 * authenticated when the timeout expires.
686 *
687 * \sa alarm(2), rc4(3), crypt.c, crypt.h
688 */
689 int handle_connect(int fd, struct sockaddr_in *addr)
690 {
691 int ret, argc, use_rc4 = 0;
692 char buf[4096];
693 unsigned char crypt_buf[MAXLINE];
694 struct user *u;
695 struct server_command *cmd = NULL;
696 long unsigned challenge_nr, chall_response;
697 char **argv = NULL;
698 char *p, *command = NULL;
699 size_t numbytes;
700
701 signal(SIGCHLD, SIG_IGN);
702 signal(SIGINT, SIG_DFL);
703 signal(SIGTERM, SIG_DFL);
704 signal(SIGHUP, SIG_DFL);
705 signal(SIGUSR1, SIG_IGN);
706
707 challenge_nr = random();
708 /* send Welcome message */
709 ret = send_va_buffer(fd, "This is para_server, version "
710 PACKAGE_VERSION ".\n" );
711 if (ret < 0)
712 goto err_out;
713 /* recv auth request line */
714 ret = recv_buffer(fd, buf, sizeof(buf));
715 if (ret < 0)
716 goto err_out;
717 if (ret <= 6) {
718 ret = -E_AUTH;
719 goto err_out;
720 }
721 numbytes = ret;
722 ret = -E_AUTH;
723 if (strncmp(buf, "auth ", 5))
724 goto err_out;
725
726 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
727 p = buf + 5; /* client version < 0.2.6 */
728 else {
729 p = buf + 9; /* client version >= 0.2.6 */
730 use_rc4 = 1;
731 }
732 PARA_DEBUG_LOG("received %s request for user %s\n",
733 use_rc4? "rc4" : "auth", p);
734 ret = -E_BAD_USER;
735 u = lookup_user(p);
736 if (!u)
737 goto err_out;
738 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
739 if (ret <= 0)
740 goto err_out;
741 numbytes = ret;
742 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
743 /* We can't use send_buffer here since buf may contain null bytes */
744 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
745 if (ret < 0)
746 goto err_out;
747 /* recv decrypted number */
748 ret = recv_buffer(fd, buf, sizeof(buf));
749 if (ret < 0)
750 goto err_out;
751 numbytes = ret;
752 ret = -E_AUTH;
753 if (!numbytes)
754 goto err_out;
755 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
756 || chall_response != challenge_nr)
757 goto err_out;
758 /* auth successful, send 'Proceed' message */
759 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
760 sprintf(buf, "%s", PROCEED_MSG);
761 if (use_rc4) {
762 init_rc4_keys();
763 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
764 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
765 if (ret <= 0)
766 goto err_out;
767 numbytes = ret + strlen(PROCEED_MSG) + 1;
768 } else
769 numbytes = strlen(buf);
770 ret = send_bin_buffer(fd, buf, numbytes);
771 if (ret < 0)
772 goto err_out;
773 if (use_rc4)
774 enable_crypt(fd, rc4_recv, rc4_send, NULL);
775 ret = read_command(fd, &command);
776 if (ret < 0)
777 goto err_out;
778 ret = -E_BAD_CMD;
779 cmd = parse_cmd(command);
780 if (!cmd)
781 goto err_out;
782 /* valid command, check permissions */
783 ret = check_perms(u->perms, cmd);
784 if (ret < 0)
785 goto err_out;
786 /* valid command and sufficient perms */
787 alarm(0);
788 argc = split_args(command, &argv, "\n");
789 mmd_lock();
790 mmd->num_commands++;
791 mmd_unlock();
792 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
793 inet_ntoa(addr->sin_addr));
794 ret = cmd->handler(fd, argc, argv);
795 if (ret >= 0) {
796 ret = EXIT_SUCCESS;
797 goto out;
798 }
799 err_out:
800 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
801 if (ret != -E_SEND && ret != -E_RECV)
802 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
803 ret = EXIT_FAILURE;
804 out:
805 free(command);
806 free(argv);
807 mmd_lock();
808 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
809 mmd->events++;
810 mmd->active_connections--;
811 mmd_unlock();
812 return ret;
813 }