Kill more duplicate status items.
[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" "%s:%u\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 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
127
128 status_item_list[SI_BASENAME], basename ? basename : "(none)",
129 status_item_list[SI_STATUS], status,
130 status_item_list[SI_STATUS_FLAGS], flags,
131
132 status_item_list[SI_OFFSET], offset,
133 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
134 nmmd->afd.afhi.info_string,
135
136 status_item_list[SI_UPTIME], ut,
137 status_item_list[SI_STREAM_START],
138 (long unsigned)nmmd->stream_start.tv_sec,
139 (long unsigned)nmmd->stream_start.tv_usec,
140 status_item_list[SI_CURRENT_TIME],
141 (long unsigned)now.tv_sec,
142 (long unsigned)now.tv_usec,
143
144 status_item_list[SI_DIRECTORY], dirname? dirname : "(none)",
145 nmmd->afd.afs_status_info
146
147 );
148 free(basename);
149 free(dirname);
150 free(flags);
151 free(status);
152 free(ut);
153 return ret;
154 }
155
156 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
157 {
158 int i;
159 /* this has to match sender.h */
160 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
161
162 scd->sender_num = -1;
163 if (argc < 2)
164 return -E_COMMAND_SYNTAX;
165 for (i = 0; senders[i].name; i++)
166 if (!strcmp(senders[i].name, argv[1]))
167 break;
168 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
169 if (!senders[i].name)
170 return -E_COMMAND_SYNTAX;
171 scd->sender_num = i;
172 for (i = 0; subcmds[i]; i++)
173 if (!strcmp(subcmds[i], argv[2]))
174 break;
175 if (!subcmds[i])
176 return -E_COMMAND_SYNTAX;
177 scd->cmd_num = i;
178 mmd_lock();
179 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
180 mmd_unlock();
181 return -E_SENDER_CMD;
182 }
183 mmd_unlock();
184 switch (scd->cmd_num) {
185 case SENDER_ON:
186 case SENDER_OFF:
187 if (argc != 3)
188 return -E_COMMAND_SYNTAX;
189 break;
190 case SENDER_DENY:
191 case SENDER_ALLOW:
192 if (argc != 4 && argc != 5)
193 return -E_COMMAND_SYNTAX;
194 if (!inet_pton(AF_INET, argv[3], &scd->addr))
195 return -E_COMMAND_SYNTAX;
196 scd->netmask = 32;
197 if (argc == 5) {
198 scd->netmask = atoi(argv[4]);
199 if (scd->netmask < 0 || scd->netmask > 32)
200 return -E_COMMAND_SYNTAX;
201 }
202 break;
203 case SENDER_ADD:
204 case SENDER_DELETE:
205 if (argc != 4 && argc != 5)
206 return -E_COMMAND_SYNTAX;
207 if (!inet_pton(AF_INET, argv[3], &scd->addr))
208 return -E_COMMAND_SYNTAX;
209 scd->port = -1;
210 if (argc == 5) {
211 scd->port = atoi(argv[4]);
212 if (scd->port < 0 || scd->port > 65535)
213 return -E_COMMAND_SYNTAX;
214 }
215 break;
216 default:
217 return -E_COMMAND_SYNTAX;
218 }
219 return 1;
220 }
221
222 int com_sender(int fd, int argc, char * const * argv)
223 {
224 int i, ret;
225 struct sender_command_data scd;
226
227 if (argc < 2) {
228 char *msg = NULL;
229 for (i = 0; senders[i].name; i++) {
230 char *tmp = make_message("%s%s\n",
231 msg? msg : "", senders[i].name);
232 free(msg);
233 msg = tmp;
234 }
235 ret = send_buffer(fd, msg);
236 free(msg);
237 return ret;
238 }
239 ret = check_sender_args(argc, argv, &scd);
240 if (ret < 0) {
241 char *msg;
242 if (scd.sender_num < 0)
243 return ret;
244 msg = senders[scd.sender_num].help();
245 send_buffer(fd, msg);
246 free(msg);
247 return 1;
248 }
249 for (i = 0; i < 10; i++) {
250 mmd_lock();
251 if (mmd->sender_cmd_data.cmd_num >= 0) {
252 mmd_unlock();
253 usleep(100 * 1000);
254 continue;
255 }
256 mmd->sender_cmd_data = scd;
257 mmd_unlock();
258 break;
259 }
260 return (i < 10)? 1 : -E_LOCK;
261 }
262
263 /* server info */
264 int com_si(int fd, int argc, __a_unused char * const * argv)
265 {
266 int i, ret;
267 char *ut;
268 char *sender_info = NULL, *sender_list = NULL;
269
270 if (argc != 1)
271 return -E_COMMAND_SYNTAX;
272 mmd_lock();
273 for (i = 0; senders[i].name; i++) {
274 char *info = senders[i].info();
275 sender_info = para_strcat(sender_info, info);
276 free(info);
277 sender_list = para_strcat(sender_list, senders[i].name);
278 sender_list = para_strcat(sender_list, " ");
279 }
280 ut = uptime_str();
281 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
282 "pid: %d\n"
283 "connections (active/accepted/total): %u/%u/%u\n"
284 "current loglevel: %i\n"
285 "supported audio formats: %s\n"
286 "supported senders: %s\n"
287 "%s",
288 ut, mmd->num_played,
289 getppid(),
290 mmd->active_connections,
291 mmd->num_commands,
292 mmd->num_connects,
293 conf.loglevel_arg,
294 supported_audio_formats(),
295 sender_list,
296 sender_info
297 );
298 mmd_unlock();
299 free(ut);
300 free(sender_list);
301 free(sender_info);
302 return ret;
303 }
304
305 /* version */
306 int com_version(int fd, int argc, __a_unused char * const * argv)
307 {
308 if (argc != 1)
309 return -E_COMMAND_SYNTAX;
310 return send_buffer(fd, VERSION_TEXT("server")
311 "built: " BUILD_DATE "\n"
312 SYSTEM ", " CC_VERSION "\n"
313 );
314 }
315
316 /* stat */
317 int com_stat(int fd, int argc, char * const * argv)
318 {
319 int ret, num = 0;/* status will be printed that many
320 * times. num <= 0 means: print forever
321 */
322 struct misc_meta_data tmp, *nmmd = &tmp;
323 char *s;
324
325 signal(SIGUSR1, dummy);
326
327 if (argc > 1)
328 num = atoi(argv[1]);
329 for (;;) {
330
331 mmd_dup(nmmd);
332 s = get_status(nmmd);
333 ret = send_buffer(fd, s);
334 free(s);
335 if (ret < 0)
336 goto out;
337 ret = 1;
338 if (num == 1)
339 goto out;
340 sleep(50);
341 if (getppid() == 1)
342 return -E_SERVER_CRASH;
343 }
344 out:
345 return ret;
346 }
347
348 static int send_list_of_commands(int fd, struct server_command *cmd,
349 const char *handler)
350 {
351 int ret, i;
352
353 for (i = 1; cmd->name; cmd++, i++) {
354 char *perms = cmd_perms_itohuman(cmd->perms);
355 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
356 handler,
357 perms,
358 cmd->description);
359 free(perms);
360 if (ret < 0)
361 return ret;
362 }
363 return 1;
364 }
365
366 /* returns string that must be freed by the caller */
367 static struct server_command *get_cmd_ptr(const char *name, char **handler)
368 {
369 struct server_command *cmd;
370
371 for (cmd = server_cmds; cmd->name; cmd++)
372 if (!strcmp(cmd->name, name)) {
373 if (handler)
374 *handler = para_strdup("server"); /* server commands */
375 return cmd;
376 }
377 /* not found, look for commands supported by afs */
378 for (cmd = afs_cmds; cmd->name; cmd++)
379 if (!strcmp(cmd->name, name)) {
380 if (handler)
381 *handler = para_strdup("afs");
382 return cmd;
383 }
384 return NULL;
385 }
386
387 /* help */
388 int com_help(int fd, int argc, char * const * argv)
389 {
390 struct server_command *cmd;
391 char *perms, *handler;
392 int ret;
393
394 if (argc < 2) {
395 /* no argument given, print list of commands */
396 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
397 return ret;
398 return send_list_of_commands(fd, afs_cmds, "afs");
399 }
400 /* argument given for help */
401 cmd = get_cmd_ptr(argv[1], &handler);
402 if (!cmd) {
403 free(handler);
404 return -E_BAD_CMD;
405 }
406 perms = cmd_perms_itohuman(cmd->perms);
407 ret = send_va_buffer(fd,
408 "%s - %s\n\n"
409 "handler: %s\n"
410 "permissions: %s\n"
411 "usage: %s\n\n"
412 "%s\n",
413 argv[1],
414 cmd->description,
415 handler,
416 perms,
417 cmd->usage,
418 cmd->help
419 );
420 free(perms);
421 free(handler);
422 return ret;
423 }
424
425 /* hup */
426 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
427 {
428 if (argc != 1)
429 return -E_COMMAND_SYNTAX;
430 kill(getppid(), SIGHUP);
431 return 1;
432 }
433
434 /* term */
435 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
436 {
437 if (argc != 1)
438 return -E_COMMAND_SYNTAX;
439 kill(getppid(), SIGTERM);
440 return 1;
441 }
442
443 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
444 {
445 if (argc != 1)
446 return -E_COMMAND_SYNTAX;
447 mmd_lock();
448 mmd->new_vss_status_flags |= VSS_PLAYING;
449 mmd->new_vss_status_flags &= ~VSS_NOMORE;
450 mmd_unlock();
451 return 1;
452
453 }
454
455 /* stop */
456 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
457 {
458 if (argc != 1)
459 return -E_COMMAND_SYNTAX;
460 mmd_lock();
461 mmd->new_vss_status_flags &= ~VSS_PLAYING;
462 mmd->new_vss_status_flags &= ~VSS_REPOS;
463 mmd->new_vss_status_flags |= VSS_NEXT;
464 mmd_unlock();
465 return 1;
466 }
467
468 /* pause */
469 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
470 {
471 if (argc != 1)
472 return -E_COMMAND_SYNTAX;
473 mmd_lock();
474 if (!vss_paused())
475 mmd->events++;
476 mmd->new_vss_status_flags &= ~VSS_PLAYING;
477 mmd->new_vss_status_flags &= ~VSS_NEXT;
478 mmd_unlock();
479 return 1;
480 }
481
482 /* next */
483 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
484 {
485 if (argc != 1)
486 return -E_COMMAND_SYNTAX;
487 mmd_lock();
488 mmd->events++;
489 mmd->new_vss_status_flags |= VSS_NEXT;
490 mmd_unlock();
491 return 1;
492 }
493
494 /* nomore */
495 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
496 {
497 if (argc != 1)
498 return -E_COMMAND_SYNTAX;
499 mmd_lock();
500 if (vss_playing() || vss_paused())
501 mmd->new_vss_status_flags |= VSS_NOMORE;
502 mmd_unlock();
503 return 1;
504 }
505
506 /* ff */
507 int com_ff(__a_unused int fd, int argc, char * const * argv)
508 {
509 long promille;
510 int ret, backwards = 0;
511 unsigned i;
512 char c;
513
514 if (argc != 2)
515 return -E_COMMAND_SYNTAX;
516 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
517 return -E_COMMAND_SYNTAX;
518 if (ret > 1 && c == '-')
519 backwards = 1; /* jmp backwards */
520 mmd_lock();
521 ret = -E_NO_AUDIO_FILE;
522 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
523 goto out;
524 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
525 if (backwards)
526 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
527 else
528 promille += 1000 * i / mmd->afd.afhi.seconds_total;
529 if (promille < 0)
530 promille = 0;
531 if (promille > 1000) {
532 mmd->new_vss_status_flags |= VSS_NEXT;
533 goto out;
534 }
535 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
536 mmd->new_vss_status_flags |= VSS_REPOS;
537 mmd->new_vss_status_flags &= ~VSS_NEXT;
538 mmd->events++;
539 ret = 1;
540 out:
541 mmd_unlock();
542 return ret;
543 }
544
545 /* jmp */
546 int com_jmp(__a_unused int fd, int argc, char * const * argv)
547 {
548 long unsigned int i;
549 int ret;
550
551 if (argc != 2)
552 return -E_COMMAND_SYNTAX;
553 if (sscanf(argv[1], "%lu", &i) <= 0)
554 return -E_COMMAND_SYNTAX;
555 mmd_lock();
556 ret = -E_NO_AUDIO_FILE;
557 if (!mmd->afd.afhi.chunks_total)
558 goto out;
559 if (i > 100)
560 i = 100;
561 PARA_INFO_LOG("jumping to %lu%%\n", i);
562 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
563 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
564 mmd->chunks_sent, mmd->offset);
565 mmd->new_vss_status_flags |= VSS_REPOS;
566 mmd->new_vss_status_flags &= ~VSS_NEXT;
567 ret = 1;
568 mmd->events++;
569 out:
570 mmd_unlock();
571 return ret;
572 }
573
574 /*
575 * check if perms are sufficient to exec a command having perms cmd_perms.
576 * Returns 0 if perms are sufficient, -E_PERM otherwise.
577 */
578 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
579 {
580 PARA_DEBUG_LOG("%s", "checking permissions\n");
581 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
582 }
583
584 /*
585 * Parse first string from *cmd and lookup in table of valid commands.
586 * On error, NULL is returned.
587 */
588 static struct server_command *parse_cmd(const char *cmdstr)
589 {
590 char buf[255];
591 int n = 0;
592
593 sscanf(cmdstr, "%200s%n", buf, &n);
594 if (!n)
595 return NULL;
596 buf[n] = '\0';
597 return get_cmd_ptr(buf, NULL);
598 }
599
600 static void init_rc4_keys(void)
601 {
602 int i;
603
604 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
605 rc4_buf[i] = para_random(256);
606 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
607 (unsigned char) rc4_buf[0],
608 (unsigned char) rc4_buf[RC4_KEY_LEN]);
609 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
610 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
611 }
612
613 static void rc4_recv(unsigned long len, const unsigned char *indata,
614 unsigned char *outdata, __a_unused void *private_data)
615 {
616 RC4(&rc4_recv_key, len, indata, outdata);
617 }
618
619 static void rc4_send(unsigned long len, const unsigned char *indata,
620 unsigned char *outdata, __a_unused void *private_data)
621 {
622 RC4(&rc4_send_key, len, indata, outdata);
623 }
624
625 static int read_command(int fd, char **result)
626 {
627 int ret;
628 char buf[4096];
629 char *command = NULL;
630
631 for (;;) {
632 size_t numbytes;
633 char *p;
634
635 ret = recv_buffer(fd, buf, sizeof(buf));
636 if (ret < 0)
637 goto out;
638 if (!ret)
639 break;
640 numbytes = ret;
641 ret = -E_COMMAND_SYNTAX;
642 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
643 goto out;
644 command = para_strcat(command, buf);
645 p = strstr(command, EOC_MSG);
646 if (p) {
647 *p = '\0';
648 break;
649 }
650 }
651 ret = command? 1 : -E_COMMAND_SYNTAX;
652 out:
653 if (ret < 0)
654 free(command);
655 else
656 *result = command;
657 return ret;
658
659 }
660
661 /**
662 * perform user authentication and execute a command
663 *
664 * \param fd the file descriptor to send output to
665 * \param addr socket address info of peer
666 *
667 * \return EXIT_SUCCESS or EXIT_FAILURE
668 *
669 * Whenever para_server accepts an incoming tcp connection on
670 * the port it listens on, it forks and the resulting child
671 * calls this function.
672 *
673 * An RSA-based challenge/response is used to authenticate
674 * the peer. It that authentication succeeds, a random RC4
675 * session key is generated and sent back to the peer,
676 * encrypted with its RSA public key. From this point on,
677 * all transfers are crypted with this session key.
678 *
679 * Next it is checked if the peer supplied a valid server command or a command
680 * for the audio file selector. If yes, and if the user has sufficient
681 * permissions to execute that command, the function calls the corresponding
682 * command handler which does argument checking and further processing.
683 *
684 * In order to cope with a DOS attacks, a timeout is set up
685 * which terminates the function if the connection was not
686 * authenticated when the timeout expires.
687 *
688 * \sa alarm(2), rc4(3), crypt.c, crypt.h
689 */
690 int handle_connect(int fd, struct sockaddr_in *addr)
691 {
692 int ret, argc, use_rc4 = 0;
693 char buf[4096];
694 unsigned char crypt_buf[MAXLINE];
695 struct user *u;
696 struct server_command *cmd = NULL;
697 long unsigned challenge_nr, chall_response;
698 char **argv = NULL;
699 char *p, *command = NULL;
700 size_t numbytes;
701
702 signal(SIGCHLD, SIG_IGN);
703 signal(SIGINT, SIG_DFL);
704 signal(SIGTERM, SIG_DFL);
705 signal(SIGHUP, SIG_DFL);
706 signal(SIGUSR1, SIG_IGN);
707
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 err_out;
748 /* recv decrypted number */
749 ret = recv_buffer(fd, buf, sizeof(buf));
750 if (ret < 0)
751 goto err_out;
752 numbytes = ret;
753 ret = -E_AUTH;
754 if (!numbytes)
755 goto err_out;
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 err_out;
774 if (use_rc4)
775 enable_crypt(fd, rc4_recv, rc4_send, NULL);
776 ret = read_command(fd, &command);
777 if (ret < 0)
778 goto err_out;
779 ret = -E_BAD_CMD;
780 cmd = parse_cmd(command);
781 if (!cmd)
782 goto err_out;
783 /* valid command, check permissions */
784 ret = check_perms(u->perms, cmd);
785 if (ret < 0)
786 goto err_out;
787 /* valid command and sufficient perms */
788 alarm(0);
789 argc = split_args(command, &argv, "\n");
790 mmd_lock();
791 mmd->num_commands++;
792 mmd_unlock();
793 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
794 inet_ntoa(addr->sin_addr));
795 ret = cmd->handler(fd, argc, argv);
796 if (ret >= 0) {
797 ret = EXIT_SUCCESS;
798 goto out;
799 }
800 err_out:
801 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
802 if (ret != -E_SEND && ret != -E_RECV)
803 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
804 ret = EXIT_FAILURE;
805 out:
806 free(command);
807 free(argv);
808 mmd_lock();
809 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
810 mmd->events++;
811 mmd->active_connections--;
812 mmd_unlock();
813 return ret;
814 }