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