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