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