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