e5e8e6841e86d4b70150aef46c7d7c1e8a04ec72
[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 /* report real status */
108 status = vss_status_tohuman(nmmd->vss_status_flags);
109 flags = vss_get_status_flags(nmmd->vss_status_flags);
110 basename = para_basename(nmmd->afd.path);
111 dirname = para_dirname(nmmd->afd.path);
112 if (basename) {
113 localtime_r(&nmmd->mtime, &mtime_tm);
114 strftime(mtime, 29, "%a %b %d %Y", &mtime_tm);
115 }
116 gettimeofday(&now, NULL);
117 ret = make_message(
118 "%s:%zu\n" "%s:%s\n" "%s:%lu\n" "%s:%u\n"
119 "%s:%s\n" "%s:%s\n" "%s:%s\n" "%s:%u\n"
120 "%s:%li\n" "%s:%s\n" "%s:%s\n" "%s"
121 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n"
122 "%s:%s\n" "%s:%u\n" "%s\n",
123 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
124 status_item_list[SI_MTIME], mtime,
125 status_item_list[SI_LENGTH], nmmd->afd.afhi.seconds_total,
126 status_item_list[SI_NUM_PLAYED], nmmd->num_played,
127
128 status_item_list[SI_BASENAME], basename ? basename : "(none)",
129 status_item_list[SI_STATUS], status,
130 status_item_list[SI_STATUS_FLAGS], flags,
131 status_item_list[SI_IMAGE_ID], nmmd->afd.afsi.image_id,
132
133 status_item_list[SI_OFFSET], offset,
134 status_item_list[SI_FORMAT], basename?
135 audio_format_name(nmmd->afd.afsi.audio_format_id)
136 : "",
137 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
138 nmmd->afd.afhi.info_string,
139
140 status_item_list[SI_UPTIME], ut,
141 status_item_list[SI_STREAM_START],
142 (long unsigned)nmmd->stream_start.tv_sec,
143 (long unsigned)nmmd->stream_start.tv_usec,
144 status_item_list[SI_CURRENT_TIME],
145 (long unsigned)now.tv_sec,
146 (long unsigned)now.tv_usec,
147
148 status_item_list[SI_DIRECTORY], dirname? dirname : "(none)",
149 status_item_list[SI_LYRICS_ID], nmmd->afd.afsi.lyrics_id,
150 nmmd->afd.afs_status_info
151
152 );
153 free(basename);
154 free(dirname);
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 mmd_lock();
184 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
185 mmd_unlock();
186 return -E_SENDER_CMD;
187 }
188 mmd_unlock();
189 switch (scd->cmd_num) {
190 case SENDER_ON:
191 case SENDER_OFF:
192 if (argc != 3)
193 return -E_COMMAND_SYNTAX;
194 break;
195 case SENDER_DENY:
196 case SENDER_ALLOW:
197 if (argc != 4 && argc != 5)
198 return -E_COMMAND_SYNTAX;
199 if (!inet_pton(AF_INET, argv[3], &scd->addr))
200 return -E_COMMAND_SYNTAX;
201 scd->netmask = 32;
202 if (argc == 5) {
203 scd->netmask = atoi(argv[4]);
204 if (scd->netmask < 0 || scd->netmask > 32)
205 return -E_COMMAND_SYNTAX;
206 }
207 break;
208 case SENDER_ADD:
209 case SENDER_DELETE:
210 if (argc != 4 && argc != 5)
211 return -E_COMMAND_SYNTAX;
212 if (!inet_pton(AF_INET, argv[3], &scd->addr))
213 return -E_COMMAND_SYNTAX;
214 scd->port = -1;
215 if (argc == 5) {
216 scd->port = atoi(argv[4]);
217 if (scd->port < 0 || scd->port > 65535)
218 return -E_COMMAND_SYNTAX;
219 }
220 break;
221 default:
222 return -E_COMMAND_SYNTAX;
223 }
224 return 1;
225 }
226
227 int com_sender(int fd, int argc, char * const * argv)
228 {
229 int i, ret;
230 struct sender_command_data scd;
231
232 if (argc < 2) {
233 char *msg = NULL;
234 for (i = 0; senders[i].name; i++) {
235 char *tmp = make_message("%s%s\n",
236 msg? msg : "", senders[i].name);
237 free(msg);
238 msg = tmp;
239 }
240 ret = send_buffer(fd, msg);
241 free(msg);
242 return ret;
243 }
244 ret = check_sender_args(argc, argv, &scd);
245 if (ret < 0) {
246 char *msg;
247 if (scd.sender_num < 0)
248 return ret;
249 msg = senders[scd.sender_num].help();
250 send_buffer(fd, msg);
251 free(msg);
252 return 1;
253 }
254 for (i = 0; i < 10; i++) {
255 mmd_lock();
256 if (mmd->sender_cmd_data.cmd_num >= 0) {
257 mmd_unlock();
258 usleep(100 * 1000);
259 continue;
260 }
261 mmd->sender_cmd_data = scd;
262 mmd_unlock();
263 break;
264 }
265 return (i < 10)? 1 : -E_LOCK;
266 }
267
268 /* server info */
269 int com_si(int fd, int argc, __a_unused char * const * argv)
270 {
271 int i, ret;
272 char *ut;
273 char *sender_info = NULL, *sender_list = NULL;
274
275 if (argc != 1)
276 return -E_COMMAND_SYNTAX;
277 mmd_lock();
278 for (i = 0; senders[i].name; i++) {
279 char *info = senders[i].info();
280 sender_info = para_strcat(sender_info, info);
281 free(info);
282 sender_list = para_strcat(sender_list, senders[i].name);
283 sender_list = para_strcat(sender_list, " ");
284 }
285 ut = uptime_str();
286 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
287 "pid: %d\n"
288 "connections (active/accepted/total): %u/%u/%u\n"
289 "current loglevel: %i\n"
290 "supported audio formats: %s\n"
291 "supported senders: %s\n"
292 "%s",
293 ut, mmd->num_played,
294 getppid(),
295 mmd->active_connections,
296 mmd->num_commands,
297 mmd->num_connects,
298 conf.loglevel_arg,
299 supported_audio_formats(),
300 sender_list,
301 sender_info
302 );
303 mmd_unlock();
304 free(ut);
305 free(sender_list);
306 free(sender_info);
307 return ret;
308 }
309
310 /* version */
311 int com_version(int fd, int argc, __a_unused char * const * argv)
312 {
313 if (argc != 1)
314 return -E_COMMAND_SYNTAX;
315 return send_buffer(fd, VERSION_TEXT("server")
316 "built: " BUILD_DATE "\n"
317 SYSTEM ", " CC_VERSION "\n"
318 );
319 }
320
321 /* stat */
322 int com_stat(int fd, int argc, char * const * argv)
323 {
324 int ret, num = 0;/* status will be printed that many
325 * times. num <= 0 means: print forever
326 */
327 struct misc_meta_data tmp, *nmmd = &tmp;
328 char *s;
329
330 signal(SIGUSR1, dummy);
331
332 if (argc > 1)
333 num = atoi(argv[1]);
334 for (;;) {
335
336 mmd_dup(nmmd);
337 s = get_status(nmmd);
338 ret = send_buffer(fd, s);
339 free(s);
340 if (ret < 0)
341 goto out;
342 ret = 1;
343 if (num == 1)
344 goto out;
345 sleep(50);
346 if (getppid() == 1)
347 return -E_SERVER_CRASH;
348 }
349 out:
350 return ret;
351 }
352
353 static int send_list_of_commands(int fd, struct server_command *cmd,
354 const char *handler)
355 {
356 int ret, i;
357
358 for (i = 1; cmd->name; cmd++, i++) {
359 char *perms = cmd_perms_itohuman(cmd->perms);
360 ret = send_va_buffer(fd, "%s\t%s\t%s\t%s\n", cmd->name,
361 handler,
362 perms,
363 cmd->description);
364 free(perms);
365 if (ret < 0)
366 return ret;
367 }
368 return 1;
369 }
370
371 /* returns string that must be freed by the caller */
372 static struct server_command *get_cmd_ptr(const char *name, char **handler)
373 {
374 struct server_command *cmd;
375
376 for (cmd = server_cmds; cmd->name; cmd++)
377 if (!strcmp(cmd->name, name)) {
378 if (handler)
379 *handler = para_strdup("server"); /* server commands */
380 return cmd;
381 }
382 /* not found, look for commands supported by afs */
383 for (cmd = afs_cmds; cmd->name; cmd++)
384 if (!strcmp(cmd->name, name)) {
385 if (handler)
386 *handler = para_strdup("afs");
387 return cmd;
388 }
389 return NULL;
390 }
391
392 /* help */
393 int com_help(int fd, int argc, char * const * argv)
394 {
395 struct server_command *cmd;
396 char *perms, *handler;
397 int ret;
398
399 if (argc < 2) {
400 /* no argument given, print list of commands */
401 if ((ret = send_list_of_commands(fd, server_cmds, "server")) < 0)
402 return ret;
403 return send_list_of_commands(fd, afs_cmds, "afs");
404 }
405 /* argument given for help */
406 cmd = get_cmd_ptr(argv[1], &handler);
407 if (!cmd) {
408 free(handler);
409 return -E_BAD_CMD;
410 }
411 perms = cmd_perms_itohuman(cmd->perms);
412 ret = send_va_buffer(fd,
413 "%s - %s\n\n"
414 "handler: %s\n"
415 "permissions: %s\n"
416 "usage: %s\n\n"
417 "%s\n",
418 argv[1],
419 cmd->description,
420 handler,
421 perms,
422 cmd->usage,
423 cmd->help
424 );
425 free(perms);
426 free(handler);
427 return ret;
428 }
429
430 /* hup */
431 int com_hup(__a_unused int fd, int argc, __a_unused char * const * argv)
432 {
433 if (argc != 1)
434 return -E_COMMAND_SYNTAX;
435 kill(getppid(), SIGHUP);
436 return 1;
437 }
438
439 /* term */
440 int com_term(__a_unused int fd, int argc, __a_unused char * const * argv)
441 {
442 if (argc != 1)
443 return -E_COMMAND_SYNTAX;
444 kill(getppid(), SIGTERM);
445 return 1;
446 }
447
448 int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
449 {
450 if (argc != 1)
451 return -E_COMMAND_SYNTAX;
452 mmd_lock();
453 mmd->new_vss_status_flags |= VSS_PLAYING;
454 mmd->new_vss_status_flags &= ~VSS_NOMORE;
455 mmd_unlock();
456 return 1;
457
458 }
459
460 /* stop */
461 int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
462 {
463 if (argc != 1)
464 return -E_COMMAND_SYNTAX;
465 mmd_lock();
466 mmd->new_vss_status_flags &= ~VSS_PLAYING;
467 mmd->new_vss_status_flags &= ~VSS_REPOS;
468 mmd->new_vss_status_flags |= VSS_NEXT;
469 mmd_unlock();
470 return 1;
471 }
472
473 /* pause */
474 int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
475 {
476 if (argc != 1)
477 return -E_COMMAND_SYNTAX;
478 mmd_lock();
479 if (!vss_paused())
480 mmd->events++;
481 mmd->new_vss_status_flags &= ~VSS_PLAYING;
482 mmd->new_vss_status_flags &= ~VSS_NEXT;
483 mmd_unlock();
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 mmd_lock();
493 mmd->events++;
494 mmd->new_vss_status_flags |= VSS_NEXT;
495 mmd_unlock();
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 mmd_lock();
505 if (vss_playing() || vss_paused())
506 mmd->new_vss_status_flags |= VSS_NOMORE;
507 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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("%s", "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 addr socket address info of peer
671 *
672 * \return EXIT_SUCCESS or EXIT_FAILURE
673 *
674 * Whenever para_server accepts an incoming tcp connection on
675 * the port it listens on, it forks and the resulting child
676 * calls this function.
677 *
678 * An RSA-based challenge/response is used to authenticate
679 * the peer. It that authentication succeeds, a random RC4
680 * session key is generated and sent back to the peer,
681 * encrypted with its RSA public key. From this point on,
682 * all transfers are crypted with this session key.
683 *
684 * Next it is checked if the peer supplied a valid server command or a command
685 * for the audio file selector. If yes, and if the user has sufficient
686 * permissions to execute that command, the function calls the corresponding
687 * command handler which does argument checking and further processing.
688 *
689 * In order to cope with a DOS attacks, a timeout is set up
690 * which terminates the function if the connection was not
691 * authenticated when the timeout expires.
692 *
693 * \sa alarm(2), rc4(3), crypt.c, crypt.h
694 */
695 int handle_connect(int fd, struct sockaddr_in *addr)
696 {
697 int ret, argc, use_rc4 = 0;
698 char buf[4096];
699 unsigned char crypt_buf[MAXLINE];
700 struct user *u;
701 struct server_command *cmd = NULL;
702 long unsigned challenge_nr, chall_response;
703 char **argv = NULL;
704 char *p, *command = NULL;
705 size_t numbytes;
706
707 signal(SIGCHLD, SIG_IGN);
708 signal(SIGINT, SIG_DFL);
709 signal(SIGTERM, SIG_DFL);
710 signal(SIGHUP, SIG_DFL);
711 signal(SIGUSR1, SIG_IGN);
712
713 challenge_nr = random();
714 /* send Welcome message */
715 ret = send_va_buffer(fd, "This is para_server, version "
716 PACKAGE_VERSION ".\n" );
717 if (ret < 0)
718 goto err_out;
719 /* recv auth request line */
720 ret = recv_buffer(fd, buf, sizeof(buf));
721 if (ret < 0)
722 goto err_out;
723 if (ret <= 6) {
724 ret = -E_AUTH;
725 goto err_out;
726 }
727 numbytes = ret;
728 ret = -E_AUTH;
729 if (strncmp(buf, "auth ", 5))
730 goto err_out;
731
732 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
733 p = buf + 5; /* client version < 0.2.6 */
734 else {
735 p = buf + 9; /* client version >= 0.2.6 */
736 use_rc4 = 1;
737 }
738 PARA_DEBUG_LOG("received %s request for user %s\n",
739 use_rc4? "rc4" : "auth", p);
740 ret = -E_BAD_USER;
741 u = lookup_user(p);
742 if (!u)
743 goto err_out;
744 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
745 if (ret <= 0)
746 goto err_out;
747 numbytes = ret;
748 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
749 /* We can't use send_buffer here since buf may contain null bytes */
750 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
751 if (ret < 0)
752 goto err_out;
753 /* recv decrypted number */
754 ret = recv_buffer(fd, buf, sizeof(buf));
755 if (ret < 0)
756 goto err_out;
757 numbytes = ret;
758 ret = -E_AUTH;
759 if (!numbytes)
760 goto err_out;
761 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
762 || chall_response != challenge_nr)
763 goto err_out;
764 /* auth successful, send 'Proceed' message */
765 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
766 sprintf(buf, "%s", PROCEED_MSG);
767 if (use_rc4) {
768 init_rc4_keys();
769 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
770 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
771 if (ret <= 0)
772 goto err_out;
773 numbytes = ret + strlen(PROCEED_MSG) + 1;
774 } else
775 numbytes = strlen(buf);
776 ret = send_bin_buffer(fd, buf, numbytes);
777 if (ret < 0)
778 goto err_out;
779 if (use_rc4)
780 enable_crypt(fd, rc4_recv, rc4_send, NULL);
781 ret = read_command(fd, &command);
782 if (ret < 0)
783 goto err_out;
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 mmd_lock();
796 mmd->num_commands++;
797 mmd_unlock();
798 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
799 inet_ntoa(addr->sin_addr));
800 ret = cmd->handler(fd, argc, argv);
801 if (ret >= 0) {
802 ret = EXIT_SUCCESS;
803 goto out;
804 }
805 err_out:
806 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
807 if (ret != -E_SEND && ret != -E_RECV)
808 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
809 ret = EXIT_FAILURE;
810 out:
811 free(command);
812 free(argv);
813 mmd_lock();
814 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
815 mmd->events++;
816 mmd->active_connections--;
817 mmd_unlock();
818 return ret;
819 }