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