7962cc79d99926c5731981c80636c145d59b7c10
[paraslash.git] / command.c
1 /*
2 * Copyright (C) 1997-2007 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 "send.h"
24 #include "rc4.h"
25 #include "net.h"
26 #include "daemon.h"
27 #include "fd.h"
28 #include "list.h"
29 #include "user_list.h"
30 #include "server_command_list.h"
31 #include "afs_command_list.h"
32
33 /** Commands including options must be shorter than this. */
34 #define MAX_COMMAND_LEN 32768
35
36 static RC4_KEY rc4_recv_key;
37 static RC4_KEY rc4_send_key;
38 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
39
40 extern const char *status_item_list[NUM_STAT_ITEMS];
41 extern struct misc_meta_data *mmd;
42 extern struct sender senders[];
43
44 static void dummy(__a_unused int s)
45 {}
46
47 static void mmd_dup(struct misc_meta_data *new_mmd)
48 {
49 mmd_lock();
50 *new_mmd = *mmd;
51 mmd_unlock();
52 }
53
54 /*
55 * compute human readable string containing
56 * vss status for given integer value
57 */
58 static char *vss_status_tohuman(unsigned int flags)
59 {
60 if (flags & VSS_PLAYING)
61 return para_strdup("playing");
62 else if (flags & VSS_NEXT)
63 return para_strdup("stopped");
64 else
65 return para_strdup("paused");
66 }
67
68 /*
69 * return human readable permission string. Never returns NULL.
70 */
71 static char *cmd_perms_itohuman(unsigned int perms)
72 {
73 char *msg = para_malloc(5 * sizeof(char));
74
75 msg[0] = perms & AFS_READ? 'a' : '-';
76 msg[1] = perms & AFS_WRITE? 'A' : '-';
77 msg[2] = perms & VSS_READ? 'v' : '-';
78 msg[3] = perms & VSS_WRITE? 'V' : '-';
79 msg[4] = '\0';
80 return msg;
81 }
82
83 /*
84 * Never returns NULL.
85 */
86 static char *vss_get_status_flags(unsigned int flags)
87 {
88 char *msg = para_malloc(5 * sizeof(char));
89
90 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
91 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
92 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
93 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
94 msg[4] = '\0';
95 return msg;
96 }
97
98 static char *get_status(struct misc_meta_data *nmmd)
99 {
100 char *basename, *dirname, *ret, mtime[30] = "";
101 char *status, *flags; /* vss status info */
102 char *ut = uptime_str();
103 long offset = (nmmd->offset + 500) / 1000;
104 struct timeval now;
105 struct tm mtime_tm;
106
107 localtime_r(&nmmd->mtime, &mtime_tm);
108 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
109 /* report real status */
110 status = vss_status_tohuman(nmmd->vss_status_flags);
111 flags = vss_get_status_flags(nmmd->vss_status_flags);
112 basename = para_basename(nmmd->filename);
113 dirname = para_dirname(nmmd->filename);
114 gettimeofday(&now, NULL);
115 ret = make_message(
116 "%s:%zu\n" "%s:%s\n" "%s:%lu\n" "%s:%u\n"
117 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%u\n"
118 "%s:%li\n" "%s:%s\n" "%s:%s\n" "%s"
119 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n" "%s:%s\n"
120 "%s:%s\n" "%s:%u\n",
121 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
122 status_item_list[SI_MTIME], mtime,
123 status_item_list[SI_LENGTH], nmmd->afd.afhi.seconds_total,
124 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
125
126 status_item_list[SI_BASENAME], basename ? basename : "(none)",
127 status_item_list[SI_STATUS], status,
128 status_item_list[SI_STATUS_FLAGS], flags,
129 status_item_list[SI_IMAGE_ID], nmmd->afd.afsi.image_id,
130
131 status_item_list[SI_OFFSET], offset,
132 status_item_list[SI_FORMAT], audio_format_name(nmmd->afd.afsi.audio_format_id),
133 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
134 nmmd->afd.afhi.info_string,
135
136 status_item_list[SI_UPTIME], ut,
137 status_item_list[SI_STREAM_START],
138 (long unsigned)nmmd->stream_start.tv_sec,
139 (long unsigned)nmmd->stream_start.tv_usec,
140 status_item_list[SI_CURRENT_TIME],
141 (long unsigned)now.tv_sec,
142 (long unsigned)now.tv_usec,
143 status_item_list[SI_ATTRIBUTES], mmd->afd.attributes_string,
144
145 status_item_list[SI_DIRECTORY], dirname,
146 status_item_list[SI_LYRICS_ID], nmmd->afd.afsi.lyrics_id
147
148 );
149 free(basename);
150 free(dirname);
151 free(flags);
152 free(status);
153 free(ut);
154 return ret;
155 }
156
157 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
158 {
159 int i;
160 /* this has to match sender.h */
161 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
162
163 scd->sender_num = -1;
164 if (argc < 2)
165 return -E_COMMAND_SYNTAX;
166 for (i = 0; senders[i].name; i++)
167 if (!strcmp(senders[i].name, argv[1]))
168 break;
169 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
170 if (!senders[i].name)
171 return -E_COMMAND_SYNTAX;
172 scd->sender_num = i;
173 for (i = 0; subcmds[i]; i++)
174 if (!strcmp(subcmds[i], argv[2]))
175 break;
176 if (!subcmds[i])
177 return -E_COMMAND_SYNTAX;
178 scd->cmd_num = i;
179 mmd_lock();
180 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
181 mmd_unlock();
182 return -E_SENDER_CMD;
183 }
184 mmd_unlock();
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 send_buffer(fd, msg);
247 free(msg);
248 return 1;
249 }
250 for (i = 0; i < 10; i++) {
251 mmd_lock();
252 if (mmd->sender_cmd_data.cmd_num >= 0) {
253 mmd_unlock();
254 usleep(100 * 1000);
255 continue;
256 }
257 mmd->sender_cmd_data = scd;
258 mmd_unlock();
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 mmd_lock();
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 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 mmd_unlock();
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 SYSTEM ", " 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 == 1)
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("para_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 return cmd;
382 return NULL;
383 }
384
385 /* help */
386 int com_help(int fd, int argc, char * const * argv)
387 {
388 struct server_command *cmd;
389 char *perms, *handler;
390 int ret;
391
392 if (argc < 2) {
393 /* no argument given, print list of commands */
394 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
395 return ret;
396 return send_list_of_commands(fd, afs_cmds, "afs");
397 }
398 /* argument given for help */
399 cmd = get_cmd_ptr(argv[1], &handler);
400 if (!cmd) {
401 free(handler);
402 return -E_BAD_CMD;
403 }
404 perms = cmd_perms_itohuman(cmd->perms);
405 ret = send_va_buffer(fd,
406 "%s - %s\n\n"
407 "handler: %s\n"
408 "permissions: %s\n"
409 "usage: %s\n\n"
410 "%s\n",
411 argv[1],
412 cmd->description,
413 handler,
414 perms,
415 cmd->usage,
416 cmd->help
417 );
418 free(perms);
419 free(handler);
420 return ret;
421 }
422
423 /* hup */
424 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
425 {
426 if (argc != 1)
427 return -E_COMMAND_SYNTAX;
428 kill(getppid(), SIGHUP);
429 return 1;
430 }
431
432 /* term */
433 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
434 {
435 if (argc != 1)
436 return -E_COMMAND_SYNTAX;
437 kill(getppid(), SIGTERM);
438 return 1;
439 }
440
441 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
442 {
443 if (argc != 1)
444 return -E_COMMAND_SYNTAX;
445 mmd_lock();
446 mmd->new_vss_status_flags |= VSS_PLAYING;
447 mmd->new_vss_status_flags &= ~VSS_NOMORE;
448 mmd_unlock();
449 return 1;
450
451 }
452
453 /* stop */
454 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
455 {
456 if (argc != 1)
457 return -E_COMMAND_SYNTAX;
458 mmd_lock();
459 mmd->new_vss_status_flags &= ~VSS_PLAYING;
460 mmd->new_vss_status_flags &= ~VSS_REPOS;
461 mmd->new_vss_status_flags |= VSS_NEXT;
462 mmd_unlock();
463 return 1;
464 }
465
466 /* pause */
467 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
468 {
469 if (argc != 1)
470 return -E_COMMAND_SYNTAX;
471 mmd_lock();
472 if (!vss_paused())
473 mmd->events++;
474 mmd->new_vss_status_flags &= ~VSS_PLAYING;
475 mmd->new_vss_status_flags &= ~VSS_NEXT;
476 mmd_unlock();
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 mmd_lock();
486 mmd->events++;
487 mmd->new_vss_status_flags |= VSS_NEXT;
488 mmd_unlock();
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 mmd_lock();
498 if (vss_playing() || vss_paused())
499 mmd->new_vss_status_flags |= VSS_NOMORE;
500 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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("%s", "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 long int para_rand(long unsigned max)
599 {
600 return (long int) ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
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_rand(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 addr socket address info of peer
669 *
670 * \return EXIT_SUCCESS or EXIT_FAILURE
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 int handle_connect(int fd, struct sockaddr_in *addr)
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 signal(SIGUSR1, SIG_IGN);
710
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 err_out;
751 /* recv decrypted number */
752 ret = recv_buffer(fd, buf, sizeof(buf));
753 if (ret < 0)
754 goto err_out;
755 numbytes = ret;
756 ret = -E_AUTH;
757 if (!numbytes)
758 goto err_out;
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 err_out;
777 if (use_rc4)
778 enable_crypt(fd, rc4_recv, rc4_send, NULL);
779 ret = read_command(fd, &command);
780 if (ret < 0)
781 goto err_out;
782 ret = -E_BAD_CMD;
783 cmd = parse_cmd(command);
784 if (!cmd)
785 goto err_out;
786 /* valid command, check permissions */
787 ret = check_perms(u->perms, cmd);
788 if (ret < 0)
789 goto err_out;
790 /* valid command and sufficient perms */
791 alarm(0);
792 argc = split_args(command, &argv, "\n");
793 mmd_lock();
794 mmd->num_commands++;
795 mmd_unlock();
796 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
797 inet_ntoa(addr->sin_addr));
798 ret = cmd->handler(fd, argc, argv);
799 if (ret >= 0) {
800 ret = EXIT_SUCCESS;
801 goto out;
802 }
803 err_out:
804 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
805 if (ret != -E_SEND && ret != -E_RECV)
806 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
807 ret = EXIT_FAILURE;
808 out:
809 free(command);
810 free(argv);
811 mmd_lock();
812 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
813 mmd->events++;
814 mmd->active_connections--;
815 mmd_unlock();
816 return ret;
817 }