Get rid of mmd->afi.
[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 *bar, *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 if (nmmd->audio_format >= 0) {
108 localtime_r(&nmmd->mtime, &mtime_tm);
109 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
110 }
111 /* report real status */
112 status = vss_status_tohuman(nmmd->vss_status_flags);
113 flags = vss_get_status_flags(nmmd->vss_status_flags);
114 bar = para_basename(nmmd->filename);
115 gettimeofday(&now, NULL);
116 ret = make_message(
117 "%s:%zu\n" "%s:%s\n" "%s:%lu\n" "%s:%u\n"
118 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%s\n"
119 "%s:%li\n" "%s:%s\n" "%s:%s\n" "%s"
120 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n",
121 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
122 status_item_list[SI_MTIME], mtime,
123 status_item_list[SI_LENGTH], nmmd->afd.afhi.seconds_total,
124 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
125
126 status_item_list[SI_STATUS_BAR], bar ? bar : "(none)",
127 status_item_list[SI_STATUS], status,
128 status_item_list[SI_STATUS_FLAGS], flags,
129 status_item_list[SI_SELECTOR], "FIXME",
130
131 status_item_list[SI_OFFSET], offset,
132 status_item_list[SI_FORMAT], audio_format_name(nmmd->audio_format),
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 );
145 free(bar);
146 free(flags);
147 free(status);
148 free(ut);
149 return ret;
150 }
151
152 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
153 {
154 int i;
155 /* this has to match sender.h */
156 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
157
158 scd->sender_num = -1;
159 if (argc < 2)
160 return -E_COMMAND_SYNTAX;
161 for (i = 0; senders[i].name; i++)
162 if (!strcmp(senders[i].name, argv[1]))
163 break;
164 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
165 if (!senders[i].name)
166 return -E_COMMAND_SYNTAX;
167 scd->sender_num = i;
168 for (i = 0; subcmds[i]; i++)
169 if (!strcmp(subcmds[i], argv[2]))
170 break;
171 if (!subcmds[i])
172 return -E_COMMAND_SYNTAX;
173 scd->cmd_num = i;
174 mmd_lock();
175 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
176 mmd_unlock();
177 return -E_SENDER_CMD;
178 }
179 mmd_unlock();
180 switch (scd->cmd_num) {
181 case SENDER_ON:
182 case SENDER_OFF:
183 if (argc != 3)
184 return -E_COMMAND_SYNTAX;
185 break;
186 case SENDER_DENY:
187 case SENDER_ALLOW:
188 if (argc != 4 && argc != 5)
189 return -E_COMMAND_SYNTAX;
190 if (!inet_pton(AF_INET, argv[3], &scd->addr))
191 return -E_COMMAND_SYNTAX;
192 scd->netmask = 32;
193 if (argc == 5) {
194 scd->netmask = atoi(argv[4]);
195 if (scd->netmask < 0 || scd->netmask > 32)
196 return -E_COMMAND_SYNTAX;
197 }
198 break;
199 case SENDER_ADD:
200 case SENDER_DELETE:
201 if (argc != 4 && argc != 5)
202 return -E_COMMAND_SYNTAX;
203 if (!inet_pton(AF_INET, argv[3], &scd->addr))
204 return -E_COMMAND_SYNTAX;
205 scd->port = -1;
206 if (argc == 5) {
207 scd->port = atoi(argv[4]);
208 if (scd->port < 0 || scd->port > 65535)
209 return -E_COMMAND_SYNTAX;
210 }
211 break;
212 default:
213 return -E_COMMAND_SYNTAX;
214 }
215 return 1;
216 }
217
218 int com_sender(int fd, int argc, char * const * argv)
219 {
220 int i, ret;
221 struct sender_command_data scd;
222
223 if (argc < 2) {
224 char *msg = NULL;
225 for (i = 0; senders[i].name; i++) {
226 char *tmp = make_message("%s%s\n",
227 msg? msg : "", senders[i].name);
228 free(msg);
229 msg = tmp;
230 }
231 ret = send_buffer(fd, msg);
232 free(msg);
233 return ret;
234 }
235 ret = check_sender_args(argc, argv, &scd);
236 if (ret < 0) {
237 char *msg;
238 if (scd.sender_num < 0)
239 return ret;
240 msg = senders[scd.sender_num].help();
241 send_buffer(fd, msg);
242 free(msg);
243 return 1;
244 }
245 for (i = 0; i < 10; i++) {
246 mmd_lock();
247 if (mmd->sender_cmd_data.cmd_num >= 0) {
248 mmd_unlock();
249 usleep(100 * 1000);
250 continue;
251 }
252 mmd->sender_cmd_data = scd;
253 mmd_unlock();
254 break;
255 }
256 return (i < 10)? 1 : -E_LOCK;
257 }
258
259 /* server info */
260 int com_si(int fd, int argc, __a_unused char * const * argv)
261 {
262 int i, ret;
263 char *ut;
264 char *sender_info = NULL, *sender_list = NULL;
265
266 if (argc != 1)
267 return -E_COMMAND_SYNTAX;
268 mmd_lock();
269 for (i = 0; senders[i].name; i++) {
270 char *info = senders[i].info();
271 sender_info = para_strcat(sender_info, info);
272 free(info);
273 sender_list = para_strcat(sender_list, senders[i].name);
274 sender_list = para_strcat(sender_list, " ");
275 }
276 ut = uptime_str();
277 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
278 "pid: %d\n"
279 "connections (active/accepted/total): %u/%u/%u\n"
280 "current loglevel: %i\n"
281 "supported audio formats: %s\n"
282 "supported senders: %s\n"
283 "%s",
284 ut, mmd->num_played,
285 getppid(),
286 mmd->active_connections,
287 mmd->num_commands,
288 mmd->num_connects,
289 conf.loglevel_arg,
290 supported_audio_formats(),
291 sender_list,
292 sender_info
293 );
294 mmd_unlock();
295 free(ut);
296 free(sender_list);
297 free(sender_info);
298 return ret;
299 }
300
301 /* version */
302 int com_version(int fd, int argc, __a_unused char * const * argv)
303 {
304 if (argc != 1)
305 return -E_COMMAND_SYNTAX;
306 return send_buffer(fd, VERSION_TEXT("server")
307 "built: " BUILD_DATE "\n"
308 SYSTEM ", " CC_VERSION "\n"
309 );
310 }
311
312 /* stat */
313 int com_stat(int fd, int argc, char * const * argv)
314 {
315 int ret, num = 0;/* status will be printed that many
316 * times. num <= 0 means: print forever
317 */
318 struct misc_meta_data tmp, *nmmd = &tmp;
319 char *s;
320
321 signal(SIGUSR1, dummy);
322
323 if (argc > 1)
324 num = atoi(argv[1]);
325 for (;;) {
326
327 mmd_dup(nmmd);
328 s = get_status(nmmd);
329 ret = send_buffer(fd, s);
330 free(s);
331 if (ret < 0)
332 goto out;
333 ret = 1;
334 if (num == 1)
335 goto out;
336 sleep(50);
337 if (getppid() == 1)
338 return -E_SERVER_CRASH;
339 }
340 out:
341 return ret;
342 }
343
344 static int send_list_of_commands(int fd, struct server_command *cmd,
345 const char *handler)
346 {
347 int ret, i;
348
349 for (i = 1; cmd->name; cmd++, i++) {
350 char *perms = cmd_perms_itohuman(cmd->perms);
351 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
352 handler,
353 perms,
354 cmd->description);
355 free(perms);
356 if (ret < 0)
357 return ret;
358 }
359 return 1;
360 }
361
362 /* returns string that must be freed by the caller */
363 static struct server_command *get_cmd_ptr(const char *name, char **handler)
364 {
365 struct server_command *cmd;
366
367 for (cmd = server_cmds; cmd->name; cmd++)
368 if (!strcmp(cmd->name, name)) {
369 if (handler)
370 *handler = para_strdup("para_server"); /* server commands */
371 return cmd;
372 }
373 /* not found, look for commands supported by afs */
374 for (cmd = afs_cmds; cmd->name; cmd++)
375 if (!strcmp(cmd->name, name))
376 return cmd;
377 return NULL;
378 }
379
380 /* help */
381 int com_help(int fd, int argc, char * const * argv)
382 {
383 struct server_command *cmd;
384 char *perms, *handler;
385 int ret;
386
387 if (argc < 2) {
388 /* no argument given, print list of commands */
389 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
390 return ret;
391 return send_list_of_commands(fd, afs_cmds, "afs");
392 }
393 /* argument given for help */
394 cmd = get_cmd_ptr(argv[1], &handler);
395 if (!cmd) {
396 free(handler);
397 return -E_BAD_CMD;
398 }
399 perms = cmd_perms_itohuman(cmd->perms);
400 ret = send_va_buffer(fd,
401 "%s - %s\n\n"
402 "handler: %s\n"
403 "permissions: %s\n"
404 "usage: %s\n\n"
405 "%s\n",
406 argv[1],
407 cmd->description,
408 handler,
409 perms,
410 cmd->usage,
411 cmd->help
412 );
413 free(perms);
414 free(handler);
415 return ret;
416 }
417
418 /* hup */
419 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
420 {
421 if (argc != 1)
422 return -E_COMMAND_SYNTAX;
423 kill(getppid(), SIGHUP);
424 return 1;
425 }
426
427 /* term */
428 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
429 {
430 if (argc != 1)
431 return -E_COMMAND_SYNTAX;
432 kill(getppid(), SIGTERM);
433 return 1;
434 }
435
436 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
437 {
438 if (argc != 1)
439 return -E_COMMAND_SYNTAX;
440 mmd_lock();
441 mmd->new_vss_status_flags |= VSS_PLAYING;
442 mmd->new_vss_status_flags &= ~VSS_NOMORE;
443 mmd_unlock();
444 return 1;
445
446 }
447
448 /* stop */
449 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
450 {
451 if (argc != 1)
452 return -E_COMMAND_SYNTAX;
453 mmd_lock();
454 mmd->new_vss_status_flags &= ~VSS_PLAYING;
455 mmd->new_vss_status_flags &= ~VSS_REPOS;
456 mmd->new_vss_status_flags |= VSS_NEXT;
457 mmd_unlock();
458 return 1;
459 }
460
461 /* pause */
462 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
463 {
464 if (argc != 1)
465 return -E_COMMAND_SYNTAX;
466 mmd_lock();
467 if (!vss_paused())
468 mmd->events++;
469 mmd->new_vss_status_flags &= ~VSS_PLAYING;
470 mmd->new_vss_status_flags &= ~VSS_NEXT;
471 mmd_unlock();
472 return 1;
473 }
474
475 /* next */
476 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
477 {
478 if (argc != 1)
479 return -E_COMMAND_SYNTAX;
480 mmd_lock();
481 mmd->events++;
482 mmd->new_vss_status_flags |= VSS_NEXT;
483 mmd_unlock();
484 return 1;
485 }
486
487 /* nomore */
488 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
489 {
490 if (argc != 1)
491 return -E_COMMAND_SYNTAX;
492 mmd_lock();
493 if (vss_playing() || vss_paused())
494 mmd->new_vss_status_flags |= VSS_NOMORE;
495 mmd_unlock();
496 return 1;
497 }
498
499 /* ff */
500 int com_ff(__a_unused int fd, int argc, char * const * argv)
501 {
502 long promille;
503 int ret, backwards = 0;
504 unsigned i;
505 char c;
506
507 if (argc != 2)
508 return -E_COMMAND_SYNTAX;
509 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
510 return -E_COMMAND_SYNTAX;
511 if (ret > 1 && c == '-')
512 backwards = 1; /* jmp backwards */
513 mmd_lock();
514 ret = -E_NO_AUDIO_FILE;
515 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
516 goto out;
517 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
518 if (backwards)
519 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
520 else
521 promille += 1000 * i / mmd->afd.afhi.seconds_total;
522 if (promille < 0)
523 promille = 0;
524 if (promille > 1000) {
525 mmd->new_vss_status_flags |= VSS_NEXT;
526 goto out;
527 }
528 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
529 mmd->new_vss_status_flags |= VSS_REPOS;
530 mmd->new_vss_status_flags &= ~VSS_NEXT;
531 mmd->events++;
532 ret = 1;
533 out:
534 mmd_unlock();
535 return ret;
536 }
537
538 /* jmp */
539 int com_jmp(__a_unused int fd, int argc, char * const * argv)
540 {
541 long unsigned int i;
542 int ret;
543
544 if (argc != 2)
545 return -E_COMMAND_SYNTAX;
546 if (sscanf(argv[1], "%lu", &i) <= 0)
547 return -E_COMMAND_SYNTAX;
548 mmd_lock();
549 ret = -E_NO_AUDIO_FILE;
550 if (!mmd->afd.afhi.chunks_total)
551 goto out;
552 if (i > 100)
553 i = 100;
554 PARA_INFO_LOG("jumping to %lu%%\n", i);
555 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
556 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
557 mmd->chunks_sent, mmd->offset);
558 mmd->new_vss_status_flags |= VSS_REPOS;
559 mmd->new_vss_status_flags &= ~VSS_NEXT;
560 ret = 1;
561 mmd->events++;
562 out:
563 mmd_unlock();
564 return ret;
565 }
566
567 /*
568 * check if perms are sufficient to exec a command having perms cmd_perms.
569 * Returns 0 if perms are sufficient, -E_PERM otherwise.
570 */
571 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
572 {
573 PARA_DEBUG_LOG("%s", "checking permissions\n");
574 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
575 }
576
577 /*
578 * Parse first string from *cmd and lookup in table of valid commands.
579 * On error, NULL is returned.
580 */
581 static struct server_command *parse_cmd(const char *cmdstr)
582 {
583 char buf[255];
584 int n = 0;
585
586 sscanf(cmdstr, "%200s%n", buf, &n);
587 if (!n)
588 return NULL;
589 buf[n] = '\0';
590 return get_cmd_ptr(buf, NULL);
591 }
592
593 static long int para_rand(long unsigned max)
594 {
595 return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
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_rand(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 }