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