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