Fix osl() wrapper.
[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 #include <osl.h>
15
16 #include "para.h"
17 #include "error.h"
18 #include "server.cmdline.h"
19 #include "string.h"
20 #include "afh.h"
21 #include "afs.h"
22 #include "server.h"
23 #include "vss.h"
24 #include "list.h"
25 #include "send.h"
26 #include "rc4.h"
27 #include "net.h"
28 #include "daemon.h"
29 #include "fd.h"
30 #include "ipc.h"
31 #include "user_list.h"
32 #include "server_command_list.h"
33 #include "afs_command_list.h"
34 #include "sched.h"
35 #include "signal.h"
36
37 /** Commands including options must be shorter than this. */
38 #define MAX_COMMAND_LEN 32768
39
40 static RC4_KEY rc4_recv_key;
41 static RC4_KEY rc4_send_key;
42 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
43
44 extern int mmd_mutex;
45 extern struct misc_meta_data *mmd;
46 extern struct sender senders[];
47
48 static void dummy(__a_unused int s)
49 {
50 }
51
52 static void mmd_dup(struct misc_meta_data *new_mmd)
53 {
54 mutex_lock(mmd_mutex);
55 *new_mmd = *mmd;
56 mutex_unlock(mmd_mutex);
57 }
58
59 /*
60 * Compute human readable string containing vss status for given integer value.
61 *
62 * We don't want to use vss_playing() and friends here because we take a
63 * snapshot of the mmd struct and use the copy for computing the state of the
64 * vss. If the real data were used, we would take the mmd lock for a rather
65 * long time or risk to get an inconsistent view.
66 */
67 static char *vss_status_tohuman(unsigned int flags)
68 {
69 if (flags & VSS_PLAYING)
70 return para_strdup("playing");
71 if (flags & VSS_NEXT)
72 return para_strdup("stopped");
73 return para_strdup("paused");
74 }
75
76 /*
77 * return human readable permission string. Never returns NULL.
78 */
79 static char *cmd_perms_itohuman(unsigned int perms)
80 {
81 char *msg = para_malloc(5 * sizeof(char));
82
83 msg[0] = perms & AFS_READ? 'a' : '-';
84 msg[1] = perms & AFS_WRITE? 'A' : '-';
85 msg[2] = perms & VSS_READ? 'v' : '-';
86 msg[3] = perms & VSS_WRITE? 'V' : '-';
87 msg[4] = '\0';
88 return msg;
89 }
90
91 /*
92 * Never returns NULL.
93 */
94 static char *vss_get_status_flags(unsigned int flags)
95 {
96 char *msg = para_malloc(5 * sizeof(char));
97
98 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
99 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
100 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
101 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
102 msg[4] = '\0';
103 return msg;
104 }
105
106 static char *get_status(struct misc_meta_data *nmmd)
107 {
108 char *ret, mtime[30] = "";
109 char *status, *flags; /* vss status info */
110 char *ut = uptime_str();
111 long offset = (nmmd->offset + 500) / 1000;
112 struct timeval current_time;
113 struct tm mtime_tm;
114
115 /* report real status */
116 status = vss_status_tohuman(nmmd->vss_status_flags);
117 flags = vss_get_status_flags(nmmd->vss_status_flags);
118 if (nmmd->size) { /* parent currently has an audio file open */
119 localtime_r(&nmmd->mtime, &mtime_tm);
120 strftime(mtime, 29, "%b %d %Y", &mtime_tm);
121 }
122 gettimeofday(&current_time, NULL);
123 ret = make_message(
124 "%s: %zu\n" /* file size */
125 "%s: %s\n" /* mtime */
126 "%s: %s\n" /* status */
127 "%s: %s\n" /* status flags */
128 "%s: %li\n" /* offset */
129 "%s: %s\n" /* afs mode */
130 "%s: %lu.%lu\n" /* stream start */
131 "%s: %lu.%lu\n" /* current server time */
132 "%s", /* afs status info */
133 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
134 status_item_list[SI_MTIME], mtime,
135 status_item_list[SI_STATUS], status,
136 status_item_list[SI_STATUS_FLAGS], flags,
137
138 status_item_list[SI_OFFSET], offset,
139 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
140
141 status_item_list[SI_STREAM_START],
142 (long unsigned)nmmd->stream_start.tv_sec,
143 (long unsigned)nmmd->stream_start.tv_usec,
144 status_item_list[SI_CURRENT_TIME],
145 (long unsigned)current_time.tv_sec,
146 (long unsigned)current_time.tv_usec,
147
148 nmmd->afd.verbose_ls_output
149
150 );
151 free(flags);
152 free(status);
153 free(ut);
154 return ret;
155 }
156
157 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
158 {
159 int i;
160 /* this has to match sender.h */
161 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
162
163 scd->sender_num = -1;
164 if (argc < 2)
165 return -E_COMMAND_SYNTAX;
166 for (i = 0; senders[i].name; i++)
167 if (!strcmp(senders[i].name, argv[1]))
168 break;
169 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
170 if (!senders[i].name)
171 return -E_COMMAND_SYNTAX;
172 scd->sender_num = i;
173 for (i = 0; subcmds[i]; i++)
174 if (!strcmp(subcmds[i], argv[2]))
175 break;
176 if (!subcmds[i])
177 return -E_COMMAND_SYNTAX;
178 scd->cmd_num = i;
179 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
180 return -E_SENDER_CMD;
181 switch (scd->cmd_num) {
182 case SENDER_ON:
183 case SENDER_OFF:
184 if (argc != 3)
185 return -E_COMMAND_SYNTAX;
186 break;
187 case SENDER_DENY:
188 case SENDER_ALLOW:
189 if (argc != 4 && argc != 5)
190 return -E_COMMAND_SYNTAX;
191 if (!is_valid_ipv4_address(argv[3]))
192 return -E_COMMAND_SYNTAX;
193 scd->netmask = 32;
194 if (argc == 5) {
195 scd->netmask = atoi(argv[4]);
196 if (scd->netmask < 0 || scd->netmask > 32)
197 return -E_COMMAND_SYNTAX;
198 }
199 strncpy(scd->host, argv[3], sizeof(scd->host));
200 break;
201 case SENDER_ADD:
202 case SENDER_DELETE:
203 if (argc != 4)
204 return -E_COMMAND_SYNTAX;
205 return parse_fec_url(argv[3], scd);
206 default:
207 return -E_COMMAND_SYNTAX;
208 }
209 return 1;
210 }
211
212 int com_sender(int fd, int argc, char * const * argv)
213 {
214 int i, ret;
215 struct sender_command_data scd;
216
217 if (argc < 2) {
218 char *msg = NULL;
219 for (i = 0; senders[i].name; i++) {
220 char *tmp = make_message("%s%s\n",
221 msg? msg : "", senders[i].name);
222 free(msg);
223 msg = tmp;
224 }
225 ret = send_buffer(fd, msg);
226 free(msg);
227 return ret;
228 }
229 ret = check_sender_args(argc, argv, &scd);
230 if (ret < 0) {
231 char *msg;
232 if (scd.sender_num < 0)
233 return ret;
234 msg = senders[scd.sender_num].help();
235 ret = send_buffer(fd, msg);
236 free(msg);
237 return ret;
238 }
239 for (i = 0; i < 10; i++) {
240 mutex_lock(mmd_mutex);
241 if (mmd->sender_cmd_data.cmd_num >= 0) {
242 mutex_unlock(mmd_mutex);
243 usleep(100 * 1000);
244 continue;
245 }
246 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
247 mutex_unlock(mmd_mutex);
248 break;
249 }
250 return (i < 10)? 1 : -E_LOCK;
251 }
252
253 /* server info */
254 int com_si(int fd, int argc, __a_unused char * const * argv)
255 {
256 int i, ret;
257 char *ut;
258 char *sender_info = NULL, *sender_list = NULL;
259
260 if (argc != 1)
261 return -E_COMMAND_SYNTAX;
262 mutex_lock(mmd_mutex);
263 for (i = 0; senders[i].name; i++) {
264 char *info = senders[i].info();
265 sender_info = para_strcat(sender_info, info);
266 free(info);
267 sender_list = para_strcat(sender_list, senders[i].name);
268 sender_list = para_strcat(sender_list, " ");
269 }
270 ut = uptime_str();
271 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
272 "server_pid: %d\n"
273 "afs_pid: %d\n"
274 "connections (active/accepted/total): %u/%u/%u\n"
275 "current loglevel: %s\n"
276 "supported audio formats: %s\n"
277 "supported senders: %s\n"
278 "%s",
279 ut, mmd->num_played,
280 (int)getppid(),
281 (int)mmd->afs_pid,
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 mutex_unlock(mmd_mutex);
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 UNAME_RS ", " 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 para_sigaction(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 > 0 && !--num)
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 mutex_lock(mmd_mutex);
440 mmd->new_vss_status_flags |= VSS_PLAYING;
441 mmd->new_vss_status_flags &= ~VSS_NOMORE;
442 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
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 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
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 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
481 mmd->events++;
482 mmd->new_vss_status_flags |= VSS_NEXT;
483 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
493 if (vss_playing() || vss_paused())
494 mmd->new_vss_status_flags |= VSS_NOMORE;
495 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
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 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
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 mutex_unlock(mmd_mutex);
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("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 static void reset_signals(void)
655 {
656 para_sigaction(SIGCHLD, SIG_IGN);
657 para_sigaction(SIGINT, SIG_DFL);
658 para_sigaction(SIGTERM, SIG_DFL);
659 para_sigaction(SIGHUP, SIG_DFL);
660 }
661
662 /**
663 * Perform user authentication and execute a command.
664 *
665 * \param fd The file descriptor to send output to.
666 * \param peername Identifies the connecting peer.
667 *
668 * Whenever para_server accepts an incoming tcp connection on
669 * the port it listens on, it forks and the resulting child
670 * calls this function.
671 *
672 * An RSA-based challenge/response is used to authenticate
673 * the peer. It that authentication succeeds, a random RC4
674 * session key is generated and sent back to the peer,
675 * encrypted with its RSA public key. From this point on,
676 * all transfers are crypted with this session key.
677 *
678 * Next it is checked if the peer supplied a valid server command or a command
679 * for the audio file selector. If yes, and if the user has sufficient
680 * permissions to execute that command, the function calls the corresponding
681 * command handler which does argument checking and further processing.
682 *
683 * In order to cope with a DOS attacks, a timeout is set up
684 * which terminates the function if the connection was not
685 * authenticated when the timeout expires.
686 *
687 * \sa alarm(2), rc4(3), crypt.c, crypt.h
688 */
689 __noreturn void handle_connect(int fd, const char *peername)
690 {
691 int ret, argc, use_rc4 = 0;
692 char buf[4096];
693 unsigned char crypt_buf[MAXLINE];
694 struct user *u;
695 struct server_command *cmd = NULL;
696 long unsigned challenge_nr, chall_response;
697 char **argv = NULL;
698 char *p, *command = NULL;
699 size_t numbytes;
700
701 reset_signals();
702 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
703 ret = mark_fd_blocking(fd);
704 if (ret < 0)
705 goto err_out;
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 net_err;
746 /* recv decrypted number */
747 ret = recv_buffer(fd, buf, sizeof(buf));
748 if (ret < 0)
749 goto net_err;
750 numbytes = ret;
751 ret = -E_AUTH;
752 if (!numbytes)
753 goto net_err;
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 net_err;
772 if (use_rc4)
773 enable_crypt(fd, rc4_recv, rc4_send, NULL);
774 ret = read_command(fd, &command);
775 if (ret == -E_COMMAND_SYNTAX)
776 goto err_out;
777 if (ret < 0)
778 goto net_err;
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 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
791 peername);
792 ret = cmd->handler(fd, argc, argv);
793 mutex_lock(mmd_mutex);
794 mmd->num_commands++;
795 mutex_unlock(mmd_mutex);
796 if (ret >= 0)
797 goto out;
798 err_out:
799 send_va_buffer(fd, "%s\n", para_strerror(-ret));
800 net_err:
801 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
802 out:
803 free(command);
804 free(argv);
805 mutex_lock(mmd_mutex);
806 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
807 mmd->events++;
808 mmd->active_connections--;
809 mutex_unlock(mmd_mutex);
810 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
811 }