Get rid of mmd->audio_format.
[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 localtime_r(&nmmd->mtime, &mtime_tm);
108 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
109 /* report real status */
110 status = vss_status_tohuman(nmmd->vss_status_flags);
111 flags = vss_get_status_flags(nmmd->vss_status_flags);
112 bar = para_basename(nmmd->filename);
113 gettimeofday(&now, NULL);
114 ret = make_message(
115 "%s:%zu\n" "%s:%s\n" "%s:%lu\n" "%s:%u\n"
116 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%s\n"
117 "%s:%li\n" "%s:%s\n" "%s:%s\n" "%s"
118 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n",
119 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
120 status_item_list[SI_MTIME], mtime,
121 status_item_list[SI_LENGTH], nmmd->afd.afhi.seconds_total,
122 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
123
124 status_item_list[SI_STATUS_BAR], bar ? bar : "(none)",
125 status_item_list[SI_STATUS], status,
126 status_item_list[SI_STATUS_FLAGS], flags,
127 status_item_list[SI_SELECTOR], "FIXME",
128
129 status_item_list[SI_OFFSET], offset,
130 status_item_list[SI_FORMAT], audio_format_name(nmmd->afd.afsi.audio_format_id),
131 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
132 nmmd->afd.afhi.info_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 );
143 free(bar);
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("para_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 return cmd;
375 return NULL;
376 }
377
378 /* help */
379 int com_help(int fd, int argc, char * const * argv)
380 {
381 struct server_command *cmd;
382 char *perms, *handler;
383 int ret;
384
385 if (argc < 2) {
386 /* no argument given, print list of commands */
387 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
388 return ret;
389 return send_list_of_commands(fd, afs_cmds, "afs");
390 }
391 /* argument given for help */
392 cmd = get_cmd_ptr(argv[1], &handler);
393 if (!cmd) {
394 free(handler);
395 return -E_BAD_CMD;
396 }
397 perms = cmd_perms_itohuman(cmd->perms);
398 ret = send_va_buffer(fd,
399 "%s - %s\n\n"
400 "handler: %s\n"
401 "permissions: %s\n"
402 "usage: %s\n\n"
403 "%s\n",
404 argv[1],
405 cmd->description,
406 handler,
407 perms,
408 cmd->usage,
409 cmd->help
410 );
411 free(perms);
412 free(handler);
413 return ret;
414 }
415
416 /* hup */
417 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
418 {
419 if (argc != 1)
420 return -E_COMMAND_SYNTAX;
421 kill(getppid(), SIGHUP);
422 return 1;
423 }
424
425 /* term */
426 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
427 {
428 if (argc != 1)
429 return -E_COMMAND_SYNTAX;
430 kill(getppid(), SIGTERM);
431 return 1;
432 }
433
434 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
435 {
436 if (argc != 1)
437 return -E_COMMAND_SYNTAX;
438 mmd_lock();
439 mmd->new_vss_status_flags |= VSS_PLAYING;
440 mmd->new_vss_status_flags &= ~VSS_NOMORE;
441 mmd_unlock();
442 return 1;
443
444 }
445
446 /* stop */
447 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
448 {
449 if (argc != 1)
450 return -E_COMMAND_SYNTAX;
451 mmd_lock();
452 mmd->new_vss_status_flags &= ~VSS_PLAYING;
453 mmd->new_vss_status_flags &= ~VSS_REPOS;
454 mmd->new_vss_status_flags |= VSS_NEXT;
455 mmd_unlock();
456 return 1;
457 }
458
459 /* pause */
460 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
461 {
462 if (argc != 1)
463 return -E_COMMAND_SYNTAX;
464 mmd_lock();
465 if (!vss_paused())
466 mmd->events++;
467 mmd->new_vss_status_flags &= ~VSS_PLAYING;
468 mmd->new_vss_status_flags &= ~VSS_NEXT;
469 mmd_unlock();
470 return 1;
471 }
472
473 /* next */
474 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
475 {
476 if (argc != 1)
477 return -E_COMMAND_SYNTAX;
478 mmd_lock();
479 mmd->events++;
480 mmd->new_vss_status_flags |= VSS_NEXT;
481 mmd_unlock();
482 return 1;
483 }
484
485 /* nomore */
486 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
487 {
488 if (argc != 1)
489 return -E_COMMAND_SYNTAX;
490 mmd_lock();
491 if (vss_playing() || vss_paused())
492 mmd->new_vss_status_flags |= VSS_NOMORE;
493 mmd_unlock();
494 return 1;
495 }
496
497 /* ff */
498 int com_ff(__a_unused int fd, int argc, char * const * argv)
499 {
500 long promille;
501 int ret, backwards = 0;
502 unsigned i;
503 char c;
504
505 if (argc != 2)
506 return -E_COMMAND_SYNTAX;
507 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
508 return -E_COMMAND_SYNTAX;
509 if (ret > 1 && c == '-')
510 backwards = 1; /* jmp backwards */
511 mmd_lock();
512 ret = -E_NO_AUDIO_FILE;
513 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
514 goto out;
515 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
516 if (backwards)
517 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
518 else
519 promille += 1000 * i / mmd->afd.afhi.seconds_total;
520 if (promille < 0)
521 promille = 0;
522 if (promille > 1000) {
523 mmd->new_vss_status_flags |= VSS_NEXT;
524 goto out;
525 }
526 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
527 mmd->new_vss_status_flags |= VSS_REPOS;
528 mmd->new_vss_status_flags &= ~VSS_NEXT;
529 mmd->events++;
530 ret = 1;
531 out:
532 mmd_unlock();
533 return ret;
534 }
535
536 /* jmp */
537 int com_jmp(__a_unused int fd, int argc, char * const * argv)
538 {
539 long unsigned int i;
540 int ret;
541
542 if (argc != 2)
543 return -E_COMMAND_SYNTAX;
544 if (sscanf(argv[1], "%lu", &i) <= 0)
545 return -E_COMMAND_SYNTAX;
546 mmd_lock();
547 ret = -E_NO_AUDIO_FILE;
548 if (!mmd->afd.afhi.chunks_total)
549 goto out;
550 if (i > 100)
551 i = 100;
552 PARA_INFO_LOG("jumping to %lu%%\n", i);
553 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
554 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
555 mmd->chunks_sent, mmd->offset);
556 mmd->new_vss_status_flags |= VSS_REPOS;
557 mmd->new_vss_status_flags &= ~VSS_NEXT;
558 ret = 1;
559 mmd->events++;
560 out:
561 mmd_unlock();
562 return ret;
563 }
564
565 /*
566 * check if perms are sufficient to exec a command having perms cmd_perms.
567 * Returns 0 if perms are sufficient, -E_PERM otherwise.
568 */
569 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
570 {
571 PARA_DEBUG_LOG("%s", "checking permissions\n");
572 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
573 }
574
575 /*
576 * Parse first string from *cmd and lookup in table of valid commands.
577 * On error, NULL is returned.
578 */
579 static struct server_command *parse_cmd(const char *cmdstr)
580 {
581 char buf[255];
582 int n = 0;
583
584 sscanf(cmdstr, "%200s%n", buf, &n);
585 if (!n)
586 return NULL;
587 buf[n] = '\0';
588 return get_cmd_ptr(buf, NULL);
589 }
590
591 static long int para_rand(long unsigned max)
592 {
593 return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
594 }
595
596 static void init_rc4_keys(void)
597 {
598 int i;
599
600 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
601 rc4_buf[i] = para_rand(256);
602 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
603 (unsigned char) rc4_buf[0],
604 (unsigned char) rc4_buf[RC4_KEY_LEN]);
605 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
606 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
607 }
608
609 static void rc4_recv(unsigned long len, const unsigned char *indata,
610 unsigned char *outdata, __a_unused void *private_data)
611 {
612 RC4(&rc4_recv_key, len, indata, outdata);
613 }
614
615 static void rc4_send(unsigned long len, const unsigned char *indata,
616 unsigned char *outdata, __a_unused void *private_data)
617 {
618 RC4(&rc4_send_key, len, indata, outdata);
619 }
620
621 static int read_command(int fd, char **result)
622 {
623 int ret;
624 char buf[4096];
625 char *command = NULL;
626
627 for (;;) {
628 size_t numbytes;
629 char *p;
630
631 ret = recv_buffer(fd, buf, sizeof(buf));
632 if (ret < 0)
633 goto out;
634 if (!ret)
635 break;
636 numbytes = ret;
637 ret = -E_COMMAND_SYNTAX;
638 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
639 goto out;
640 command = para_strcat(command, buf);
641 p = strstr(command, EOC_MSG);
642 if (p) {
643 *p = '\0';
644 break;
645 }
646 }
647 ret = command? 1 : -E_COMMAND_SYNTAX;
648 out:
649 if (ret < 0)
650 free(command);
651 else
652 *result = command;
653 return ret;
654
655 }
656
657 /**
658 * perform user authentication and execute a command
659 *
660 * \param fd the file descriptor to send output to
661 * \param addr socket address info of peer
662 *
663 * \return EXIT_SUCCESS or EXIT_FAILURE
664 *
665 * Whenever para_server accepts an incoming tcp connection on
666 * the port it listens on, it forks and the resulting child
667 * calls this function.
668 *
669 * An RSA-based challenge/response is used to authenticate
670 * the peer. It that authentication succeeds, a random RC4
671 * session key is generated and sent back to the peer,
672 * encrypted with its RSA public key. From this point on,
673 * all transfers are crypted with this session key.
674 *
675 * Next it is checked if the peer supplied a valid server command or a command
676 * for the audio file selector. If yes, and if the user has sufficient
677 * permissions to execute that command, the function calls the corresponding
678 * command handler which does argument checking and further processing.
679 *
680 * In order to cope with a DOS attacks, a timeout is set up
681 * which terminates the function if the connection was not
682 * authenticated when the timeout expires.
683 *
684 * \sa alarm(2), rc4(3), crypt.c, crypt.h
685 */
686 int handle_connect(int fd, struct sockaddr_in *addr)
687 {
688 int ret, argc, use_rc4 = 0;
689 char buf[4096];
690 unsigned char crypt_buf[MAXLINE];
691 struct user *u;
692 struct server_command *cmd = NULL;
693 long unsigned challenge_nr, chall_response;
694 char **argv = NULL;
695 char *p, *command = NULL;
696 size_t numbytes;
697
698 signal(SIGCHLD, SIG_IGN);
699 signal(SIGINT, SIG_DFL);
700 signal(SIGTERM, SIG_DFL);
701 signal(SIGHUP, SIG_DFL);
702 signal(SIGUSR1, SIG_IGN);
703
704 challenge_nr = random();
705 /* send Welcome message */
706 ret = send_va_buffer(fd, "This is para_server, version "
707 PACKAGE_VERSION ".\n" );
708 if (ret < 0)
709 goto err_out;
710 /* recv auth request line */
711 ret = recv_buffer(fd, buf, sizeof(buf));
712 if (ret < 0)
713 goto err_out;
714 if (ret <= 6) {
715 ret = -E_AUTH;
716 goto err_out;
717 }
718 numbytes = ret;
719 ret = -E_AUTH;
720 if (strncmp(buf, "auth ", 5))
721 goto err_out;
722
723 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
724 p = buf + 5; /* client version < 0.2.6 */
725 else {
726 p = buf + 9; /* client version >= 0.2.6 */
727 use_rc4 = 1;
728 }
729 PARA_DEBUG_LOG("received %s request for user %s\n",
730 use_rc4? "rc4" : "auth", p);
731 ret = -E_BAD_USER;
732 u = lookup_user(p);
733 if (!u)
734 goto err_out;
735 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
736 if (ret <= 0)
737 goto err_out;
738 numbytes = ret;
739 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
740 /* We can't use send_buffer here since buf may contain null bytes */
741 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
742 if (ret < 0)
743 goto err_out;
744 /* recv decrypted number */
745 ret = recv_buffer(fd, buf, sizeof(buf));
746 if (ret < 0)
747 goto err_out;
748 numbytes = ret;
749 ret = -E_AUTH;
750 if (!numbytes)
751 goto err_out;
752 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
753 || chall_response != challenge_nr)
754 goto err_out;
755 /* auth successful, send 'Proceed' message */
756 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
757 sprintf(buf, "%s", PROCEED_MSG);
758 if (use_rc4) {
759 init_rc4_keys();
760 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
761 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
762 if (ret <= 0)
763 goto err_out;
764 numbytes = ret + strlen(PROCEED_MSG) + 1;
765 } else
766 numbytes = strlen(buf);
767 ret = send_bin_buffer(fd, buf, numbytes);
768 if (ret < 0)
769 goto err_out;
770 if (use_rc4)
771 enable_crypt(fd, rc4_recv, rc4_send, NULL);
772 ret = read_command(fd, &command);
773 if (ret < 0)
774 goto err_out;
775 ret = -E_BAD_CMD;
776 cmd = parse_cmd(command);
777 if (!cmd)
778 goto err_out;
779 /* valid command, check permissions */
780 ret = check_perms(u->perms, cmd);
781 if (ret < 0)
782 goto err_out;
783 /* valid command and sufficient perms */
784 alarm(0);
785 argc = split_args(command, &argv, "\n");
786 mmd_lock();
787 mmd->num_commands++;
788 mmd_unlock();
789 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
790 inet_ntoa(addr->sin_addr));
791 ret = cmd->handler(fd, argc, argv);
792 if (ret >= 0) {
793 ret = EXIT_SUCCESS;
794 goto out;
795 }
796 err_out:
797 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
798 if (ret != -E_SEND && ret != -E_RECV)
799 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
800 ret = EXIT_FAILURE;
801 out:
802 free(command);
803 free(argv);
804 mmd_lock();
805 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
806 mmd->events++;
807 mmd->active_connections--;
808 mmd_unlock();
809 return ret;
810 }