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