Use para_sigaction() in command handlers.
[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 && argc != 5)
189 return -E_COMMAND_SYNTAX;
190 if (!is_valid_ipv4_address(argv[3]))
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 strncpy(scd->host, argv[3], sizeof(scd->host));
199 break;
200 case SENDER_ADD:
201 case SENDER_DELETE:
202 if (argc != 4)
203 return -E_COMMAND_SYNTAX;
204 return parse_fec_url(argv[3], scd);
205 default:
206 return -E_COMMAND_SYNTAX;
207 }
208 return 1;
209 }
210
211 int com_sender(int fd, int argc, char * const * argv)
212 {
213 int i, ret;
214 struct sender_command_data scd;
215
216 if (argc < 2) {
217 char *msg = NULL;
218 for (i = 0; senders[i].name; i++) {
219 char *tmp = make_message("%s%s\n",
220 msg? msg : "", senders[i].name);
221 free(msg);
222 msg = tmp;
223 }
224 ret = send_buffer(fd, msg);
225 free(msg);
226 return ret;
227 }
228 ret = check_sender_args(argc, argv, &scd);
229 if (ret < 0) {
230 char *msg;
231 if (scd.sender_num < 0)
232 return ret;
233 msg = senders[scd.sender_num].help();
234 ret = send_buffer(fd, msg);
235 free(msg);
236 return ret;
237 }
238 for (i = 0; i < 10; i++) {
239 mutex_lock(mmd_mutex);
240 if (mmd->sender_cmd_data.cmd_num >= 0) {
241 mutex_unlock(mmd_mutex);
242 usleep(100 * 1000);
243 continue;
244 }
245 memcpy(&mmd->sender_cmd_data, &scd, sizeof(scd));
246 mutex_unlock(mmd_mutex);
247 break;
248 }
249 return (i < 10)? 1 : -E_LOCK;
250 }
251
252 /* server info */
253 int com_si(int fd, int argc, __a_unused char * const * argv)
254 {
255 int i, ret;
256 char *ut;
257 char *sender_info = NULL, *sender_list = NULL;
258
259 if (argc != 1)
260 return -E_COMMAND_SYNTAX;
261 mutex_lock(mmd_mutex);
262 for (i = 0; senders[i].name; i++) {
263 char *info = senders[i].info();
264 sender_info = para_strcat(sender_info, info);
265 free(info);
266 sender_list = para_strcat(sender_list, senders[i].name);
267 sender_list = para_strcat(sender_list, " ");
268 }
269 ut = uptime_str();
270 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
271 "server_pid: %d\n"
272 "afs_pid: %d\n"
273 "connections (active/accepted/total): %u/%u/%u\n"
274 "current loglevel: %s\n"
275 "supported audio formats: %s\n"
276 "supported senders: %s\n"
277 "%s",
278 ut, mmd->num_played,
279 (int)getppid(),
280 (int)mmd->afs_pid,
281 mmd->active_connections,
282 mmd->num_commands,
283 mmd->num_connects,
284 conf.loglevel_arg,
285 supported_audio_formats(),
286 sender_list,
287 sender_info
288 );
289 mutex_unlock(mmd_mutex);
290 free(ut);
291 free(sender_list);
292 free(sender_info);
293 return ret;
294 }
295
296 /* version */
297 int com_version(int fd, int argc, __a_unused char * const * argv)
298 {
299 if (argc != 1)
300 return -E_COMMAND_SYNTAX;
301 return send_buffer(fd, VERSION_TEXT("server")
302 "built: " BUILD_DATE "\n"
303 UNAME_RS ", " CC_VERSION "\n"
304 );
305 }
306
307 /* stat */
308 int com_stat(int fd, int argc, char * const * argv)
309 {
310 int ret, num = 0;/* status will be printed that many
311 * times. num <= 0 means: print forever
312 */
313 struct misc_meta_data tmp, *nmmd = &tmp;
314 char *s;
315
316 ret = para_sigaction(SIGUSR1, dummy);
317 if (ret < 0)
318 return ret;
319
320 if (argc > 1)
321 num = atoi(argv[1]);
322 for (;;) {
323
324 mmd_dup(nmmd);
325 s = get_status(nmmd);
326 ret = send_buffer(fd, s);
327 free(s);
328 if (ret < 0)
329 goto out;
330 ret = 1;
331 if (num > 0 && !--num)
332 goto out;
333 sleep(50);
334 if (getppid() == 1)
335 return -E_SERVER_CRASH;
336 }
337 out:
338 return ret;
339 }
340
341 static int send_list_of_commands(int fd, struct server_command *cmd,
342 const char *handler)
343 {
344 int ret, i;
345
346 for (i = 1; cmd->name; cmd++, i++) {
347 char *perms = cmd_perms_itohuman(cmd->perms);
348 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
349 handler,
350 perms,
351 cmd->description);
352 free(perms);
353 if (ret < 0)
354 return ret;
355 }
356 return 1;
357 }
358
359 /* returns string that must be freed by the caller */
360 static struct server_command *get_cmd_ptr(const char *name, char **handler)
361 {
362 struct server_command *cmd;
363
364 for (cmd = server_cmds; cmd->name; cmd++)
365 if (!strcmp(cmd->name, name)) {
366 if (handler)
367 *handler = para_strdup("server"); /* server commands */
368 return cmd;
369 }
370 /* not found, look for commands supported by afs */
371 for (cmd = afs_cmds; cmd->name; cmd++)
372 if (!strcmp(cmd->name, name)) {
373 if (handler)
374 *handler = para_strdup("afs");
375 return cmd;
376 }
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 mutex_lock(mmd_mutex);
441 mmd->new_vss_status_flags |= VSS_PLAYING;
442 mmd->new_vss_status_flags &= ~VSS_NOMORE;
443 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
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 mutex_unlock(mmd_mutex);
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 mutex_lock(mmd_mutex);
467 if (!vss_paused() && !vss_stopped()) {
468 mmd->events++;
469 mmd->new_vss_status_flags &= ~VSS_PLAYING;
470 mmd->new_vss_status_flags &= ~VSS_NEXT;
471 }
472 mutex_unlock(mmd_mutex);
473 return 1;
474 }
475
476 /* next */
477 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
478 {
479 if (argc != 1)
480 return -E_COMMAND_SYNTAX;
481 mutex_lock(mmd_mutex);
482 mmd->events++;
483 mmd->new_vss_status_flags |= VSS_NEXT;
484 mutex_unlock(mmd_mutex);
485 return 1;
486 }
487
488 /* nomore */
489 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
490 {
491 if (argc != 1)
492 return -E_COMMAND_SYNTAX;
493 mutex_lock(mmd_mutex);
494 if (vss_playing() || vss_paused())
495 mmd->new_vss_status_flags |= VSS_NOMORE;
496 mutex_unlock(mmd_mutex);
497 return 1;
498 }
499
500 /* ff */
501 int com_ff(__a_unused int fd, int argc, char * const * argv)
502 {
503 long promille;
504 int ret, backwards = 0;
505 unsigned i;
506 char c;
507
508 if (argc != 2)
509 return -E_COMMAND_SYNTAX;
510 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
511 return -E_COMMAND_SYNTAX;
512 if (ret > 1 && c == '-')
513 backwards = 1; /* jmp backwards */
514 mutex_lock(mmd_mutex);
515 ret = -E_NO_AUDIO_FILE;
516 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
517 goto out;
518 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
519 if (backwards)
520 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
521 else
522 promille += 1000 * i / mmd->afd.afhi.seconds_total;
523 if (promille < 0)
524 promille = 0;
525 if (promille > 1000) {
526 mmd->new_vss_status_flags |= VSS_NEXT;
527 goto out;
528 }
529 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
530 mmd->new_vss_status_flags |= VSS_REPOS;
531 mmd->new_vss_status_flags &= ~VSS_NEXT;
532 mmd->events++;
533 ret = 1;
534 out:
535 mutex_unlock(mmd_mutex);
536 return ret;
537 }
538
539 /* jmp */
540 int com_jmp(__a_unused int fd, int argc, char * const * argv)
541 {
542 long unsigned int i;
543 int ret;
544
545 if (argc != 2)
546 return -E_COMMAND_SYNTAX;
547 if (sscanf(argv[1], "%lu", &i) <= 0)
548 return -E_COMMAND_SYNTAX;
549 mutex_lock(mmd_mutex);
550 ret = -E_NO_AUDIO_FILE;
551 if (!mmd->afd.afhi.chunks_total)
552 goto out;
553 if (i > 100)
554 i = 100;
555 PARA_INFO_LOG("jumping to %lu%%\n", i);
556 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
557 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
558 mmd->chunks_sent, mmd->offset);
559 mmd->new_vss_status_flags |= VSS_REPOS;
560 mmd->new_vss_status_flags &= ~VSS_NEXT;
561 ret = 1;
562 mmd->events++;
563 out:
564 mutex_unlock(mmd_mutex);
565 return ret;
566 }
567
568 /*
569 * check if perms are sufficient to exec a command having perms cmd_perms.
570 * Returns 0 if perms are sufficient, -E_PERM otherwise.
571 */
572 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
573 {
574 PARA_DEBUG_LOG("checking permissions\n");
575 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
576 }
577
578 /*
579 * Parse first string from *cmd and lookup in table of valid commands.
580 * On error, NULL is returned.
581 */
582 static struct server_command *parse_cmd(const char *cmdstr)
583 {
584 char buf[255];
585 int n = 0;
586
587 sscanf(cmdstr, "%200s%n", buf, &n);
588 if (!n)
589 return NULL;
590 buf[n] = '\0';
591 return get_cmd_ptr(buf, NULL);
592 }
593
594 static void init_rc4_keys(void)
595 {
596 int i;
597
598 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
599 rc4_buf[i] = para_random(256);
600 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
601 (unsigned char) rc4_buf[0],
602 (unsigned char) rc4_buf[RC4_KEY_LEN]);
603 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
604 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
605 }
606
607 static void rc4_recv(unsigned long len, const unsigned char *indata,
608 unsigned char *outdata, __a_unused void *private_data)
609 {
610 RC4(&rc4_recv_key, len, indata, outdata);
611 }
612
613 static void rc4_send(unsigned long len, const unsigned char *indata,
614 unsigned char *outdata, __a_unused void *private_data)
615 {
616 RC4(&rc4_send_key, len, indata, outdata);
617 }
618
619 static int read_command(int fd, char **result)
620 {
621 int ret;
622 char buf[4096];
623 char *command = NULL;
624
625 for (;;) {
626 size_t numbytes;
627 char *p;
628
629 ret = recv_buffer(fd, buf, sizeof(buf));
630 if (ret < 0)
631 goto out;
632 if (!ret)
633 break;
634 numbytes = ret;
635 ret = -E_COMMAND_SYNTAX;
636 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
637 goto out;
638 command = para_strcat(command, buf);
639 p = strstr(command, EOC_MSG);
640 if (p) {
641 *p = '\0';
642 break;
643 }
644 }
645 ret = command? 1 : -E_COMMAND_SYNTAX;
646 out:
647 if (ret < 0)
648 free(command);
649 else
650 *result = command;
651 return ret;
652
653 }
654
655 static int reset_signals(void)
656 {
657 int ret = para_sigaction(SIGCHLD, SIG_IGN);
658
659 if (ret < 0)
660 return ret;
661 ret = para_sigaction(SIGINT, SIG_DFL);
662 if (ret < 0)
663 return ret;
664 ret = para_sigaction(SIGTERM, SIG_DFL);
665 if (ret < 0)
666 return ret;
667 ret = para_sigaction(SIGHUP, SIG_DFL);
668 if (ret < 0)
669 return ret;
670 return 1;
671 }
672
673 /**
674 * Perform user authentication and execute a command.
675 *
676 * \param fd The file descriptor to send output to.
677 * \param peername Identifies the connecting peer.
678 *
679 * Whenever para_server accepts an incoming tcp connection on
680 * the port it listens on, it forks and the resulting child
681 * calls this function.
682 *
683 * An RSA-based challenge/response is used to authenticate
684 * the peer. It that authentication succeeds, a random RC4
685 * session key is generated and sent back to the peer,
686 * encrypted with its RSA public key. From this point on,
687 * all transfers are crypted with this session key.
688 *
689 * Next it is checked if the peer supplied a valid server command or a command
690 * for the audio file selector. If yes, and if the user has sufficient
691 * permissions to execute that command, the function calls the corresponding
692 * command handler which does argument checking and further processing.
693 *
694 * In order to cope with a DOS attacks, a timeout is set up
695 * which terminates the function if the connection was not
696 * authenticated when the timeout expires.
697 *
698 * \sa alarm(2), rc4(3), crypt.c, crypt.h
699 */
700 __noreturn void handle_connect(int fd, const char *peername)
701 {
702 int ret, argc, use_rc4 = 0;
703 char buf[4096];
704 unsigned char crypt_buf[MAXLINE];
705 struct user *u;
706 struct server_command *cmd = NULL;
707 long unsigned challenge_nr, chall_response;
708 char **argv = NULL;
709 char *p, *command = NULL;
710 size_t numbytes;
711
712 ret = reset_signals();
713 if (ret < 0)
714 goto err_out;
715 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
716 ret = mark_fd_blocking(fd);
717 if (ret < 0)
718 goto err_out;
719 challenge_nr = random();
720 /* send Welcome message */
721 ret = send_va_buffer(fd, "This is para_server, version "
722 PACKAGE_VERSION ".\n" );
723 if (ret < 0)
724 goto err_out;
725 /* recv auth request line */
726 ret = recv_buffer(fd, buf, sizeof(buf));
727 if (ret < 0)
728 goto err_out;
729 if (ret <= 6) {
730 ret = -E_AUTH;
731 goto err_out;
732 }
733 numbytes = ret;
734 ret = -E_AUTH;
735 if (strncmp(buf, "auth ", 5))
736 goto err_out;
737
738 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
739 p = buf + 5; /* client version < 0.2.6 */
740 else {
741 p = buf + 9; /* client version >= 0.2.6 */
742 use_rc4 = 1;
743 }
744 PARA_DEBUG_LOG("received %s request for user %s\n",
745 use_rc4? "rc4" : "auth", p);
746 ret = -E_BAD_USER;
747 u = lookup_user(p);
748 if (!u)
749 goto err_out;
750 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
751 if (ret <= 0)
752 goto err_out;
753 numbytes = ret;
754 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
755 /* We can't use send_buffer here since buf may contain null bytes */
756 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
757 if (ret < 0)
758 goto net_err;
759 /* recv decrypted number */
760 ret = recv_buffer(fd, buf, sizeof(buf));
761 if (ret < 0)
762 goto net_err;
763 numbytes = ret;
764 ret = -E_AUTH;
765 if (!numbytes)
766 goto net_err;
767 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
768 || chall_response != challenge_nr)
769 goto err_out;
770 /* auth successful, send 'Proceed' message */
771 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
772 sprintf(buf, "%s", PROCEED_MSG);
773 if (use_rc4) {
774 init_rc4_keys();
775 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
776 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
777 if (ret <= 0)
778 goto err_out;
779 numbytes = ret + strlen(PROCEED_MSG) + 1;
780 } else
781 numbytes = strlen(buf);
782 ret = send_bin_buffer(fd, buf, numbytes);
783 if (ret < 0)
784 goto net_err;
785 if (use_rc4)
786 enable_crypt(fd, rc4_recv, rc4_send, NULL);
787 ret = read_command(fd, &command);
788 if (ret == -E_COMMAND_SYNTAX)
789 goto err_out;
790 if (ret < 0)
791 goto net_err;
792 ret = -E_BAD_CMD;
793 cmd = parse_cmd(command);
794 if (!cmd)
795 goto err_out;
796 /* valid command, check permissions */
797 ret = check_perms(u->perms, cmd);
798 if (ret < 0)
799 goto err_out;
800 /* valid command and sufficient perms */
801 alarm(0);
802 argc = split_args(command, &argv, "\n");
803 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
804 peername);
805 ret = cmd->handler(fd, argc, argv);
806 mutex_lock(mmd_mutex);
807 mmd->num_commands++;
808 free(mmd->afd.afhi.info_string);
809 free(mmd->afd.afhi.chunk_table);
810 mutex_unlock(mmd_mutex);
811 if (ret >= 0)
812 goto out;
813 err_out:
814 send_va_buffer(fd, "%s\n", para_strerror(-ret));
815 net_err:
816 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
817 out:
818 free(command);
819 free(argv);
820 mutex_lock(mmd_mutex);
821 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
822 mmd->events++;
823 mmd->active_connections--;
824 mutex_unlock(mmd_mutex);
825 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
826 }