Rename length status item to seconds_total and print it only once.
[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"
119 "%s:%s\n" "%s:%s\n" "%s:%s\n"
120 "%s:%li\n" "%s:%s\n"
121 "%s:%s\n" "%s:%lu.%lu\n" "%s:%lu.%lu\n"
122 "%s:%s\n" "%s\n",
123 status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
124 status_item_list[SI_MTIME], mtime,
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
130 status_item_list[SI_OFFSET], offset,
131 status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
132
133 status_item_list[SI_UPTIME], ut,
134 status_item_list[SI_STREAM_START],
135 (long unsigned)nmmd->stream_start.tv_sec,
136 (long unsigned)nmmd->stream_start.tv_usec,
137 status_item_list[SI_CURRENT_TIME],
138 (long unsigned)now.tv_sec,
139 (long unsigned)now.tv_usec,
140
141 status_item_list[SI_DIRECTORY], dirname? dirname : "(none)",
142 nmmd->afd.afs_status_info
143
144 );
145 free(basename);
146 free(dirname);
147 free(flags);
148 free(status);
149 free(ut);
150 return ret;
151 }
152
153 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
154 {
155 int i;
156 /* this has to match sender.h */
157 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
158
159 scd->sender_num = -1;
160 if (argc < 2)
161 return -E_COMMAND_SYNTAX;
162 for (i = 0; senders[i].name; i++)
163 if (!strcmp(senders[i].name, argv[1]))
164 break;
165 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
166 if (!senders[i].name)
167 return -E_COMMAND_SYNTAX;
168 scd->sender_num = i;
169 for (i = 0; subcmds[i]; i++)
170 if (!strcmp(subcmds[i], argv[2]))
171 break;
172 if (!subcmds[i])
173 return -E_COMMAND_SYNTAX;
174 scd->cmd_num = i;
175 mmd_lock();
176 if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
177 mmd_unlock();
178 return -E_SENDER_CMD;
179 }
180 mmd_unlock();
181 switch (scd->cmd_num) {
182 case SENDER_ON:
183 case SENDER_OFF:
184 if (argc != 3)
185 return -E_COMMAND_SYNTAX;
186 break;
187 case SENDER_DENY:
188 case SENDER_ALLOW:
189 if (argc != 4 && argc != 5)
190 return -E_COMMAND_SYNTAX;
191 if (!inet_pton(AF_INET, argv[3], &scd->addr))
192 return -E_COMMAND_SYNTAX;
193 scd->netmask = 32;
194 if (argc == 5) {
195 scd->netmask = atoi(argv[4]);
196 if (scd->netmask < 0 || scd->netmask > 32)
197 return -E_COMMAND_SYNTAX;
198 }
199 break;
200 case SENDER_ADD:
201 case SENDER_DELETE:
202 if (argc != 4 && argc != 5)
203 return -E_COMMAND_SYNTAX;
204 if (!inet_pton(AF_INET, argv[3], &scd->addr))
205 return -E_COMMAND_SYNTAX;
206 scd->port = -1;
207 if (argc == 5) {
208 scd->port = atoi(argv[4]);
209 if (scd->port < 0 || scd->port > 65535)
210 return -E_COMMAND_SYNTAX;
211 }
212 break;
213 default:
214 return -E_COMMAND_SYNTAX;
215 }
216 return 1;
217 }
218
219 int com_sender(int fd, int argc, char * const * argv)
220 {
221 int i, ret;
222 struct sender_command_data scd;
223
224 if (argc < 2) {
225 char *msg = NULL;
226 for (i = 0; senders[i].name; i++) {
227 char *tmp = make_message("%s%s\n",
228 msg? msg : "", senders[i].name);
229 free(msg);
230 msg = tmp;
231 }
232 ret = send_buffer(fd, msg);
233 free(msg);
234 return ret;
235 }
236 ret = check_sender_args(argc, argv, &scd);
237 if (ret < 0) {
238 char *msg;
239 if (scd.sender_num < 0)
240 return ret;
241 msg = senders[scd.sender_num].help();
242 send_buffer(fd, msg);
243 free(msg);
244 return 1;
245 }
246 for (i = 0; i < 10; i++) {
247 mmd_lock();
248 if (mmd->sender_cmd_data.cmd_num >= 0) {
249 mmd_unlock();
250 usleep(100 * 1000);
251 continue;
252 }
253 mmd->sender_cmd_data = scd;
254 mmd_unlock();
255 break;
256 }
257 return (i < 10)? 1 : -E_LOCK;
258 }
259
260 /* server info */
261 int com_si(int fd, int argc, __a_unused char * const * argv)
262 {
263 int i, ret;
264 char *ut;
265 char *sender_info = NULL, *sender_list = NULL;
266
267 if (argc != 1)
268 return -E_COMMAND_SYNTAX;
269 mmd_lock();
270 for (i = 0; senders[i].name; i++) {
271 char *info = senders[i].info();
272 sender_info = para_strcat(sender_info, info);
273 free(info);
274 sender_list = para_strcat(sender_list, senders[i].name);
275 sender_list = para_strcat(sender_list, " ");
276 }
277 ut = uptime_str();
278 ret = send_va_buffer(fd, "up: %s\nplayed: %u\n"
279 "pid: %d\n"
280 "connections (active/accepted/total): %u/%u/%u\n"
281 "current loglevel: %i\n"
282 "supported audio formats: %s\n"
283 "supported senders: %s\n"
284 "%s",
285 ut, mmd->num_played,
286 getppid(),
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 mmd_unlock();
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 SYSTEM ", " 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 == 1)
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 mmd_lock();
445 mmd->new_vss_status_flags |= VSS_PLAYING;
446 mmd->new_vss_status_flags &= ~VSS_NOMORE;
447 mmd_unlock();
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 mmd_lock();
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 mmd_unlock();
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 mmd_lock();
471 if (!vss_paused())
472 mmd->events++;
473 mmd->new_vss_status_flags &= ~VSS_PLAYING;
474 mmd->new_vss_status_flags &= ~VSS_NEXT;
475 mmd_unlock();
476 return 1;
477 }
478
479 /* next */
480 int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
481 {
482 if (argc != 1)
483 return -E_COMMAND_SYNTAX;
484 mmd_lock();
485 mmd->events++;
486 mmd->new_vss_status_flags |= VSS_NEXT;
487 mmd_unlock();
488 return 1;
489 }
490
491 /* nomore */
492 int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
493 {
494 if (argc != 1)
495 return -E_COMMAND_SYNTAX;
496 mmd_lock();
497 if (vss_playing() || vss_paused())
498 mmd->new_vss_status_flags |= VSS_NOMORE;
499 mmd_unlock();
500 return 1;
501 }
502
503 /* ff */
504 int com_ff(__a_unused int fd, int argc, char * const * argv)
505 {
506 long promille;
507 int ret, backwards = 0;
508 unsigned i;
509 char c;
510
511 if (argc != 2)
512 return -E_COMMAND_SYNTAX;
513 if (!(ret = sscanf(argv[1], "%u%c", &i, &c)))
514 return -E_COMMAND_SYNTAX;
515 if (ret > 1 && c == '-')
516 backwards = 1; /* jmp backwards */
517 mmd_lock();
518 ret = -E_NO_AUDIO_FILE;
519 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
520 goto out;
521 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
522 if (backwards)
523 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
524 else
525 promille += 1000 * i / mmd->afd.afhi.seconds_total;
526 if (promille < 0)
527 promille = 0;
528 if (promille > 1000) {
529 mmd->new_vss_status_flags |= VSS_NEXT;
530 goto out;
531 }
532 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
533 mmd->new_vss_status_flags |= VSS_REPOS;
534 mmd->new_vss_status_flags &= ~VSS_NEXT;
535 mmd->events++;
536 ret = 1;
537 out:
538 mmd_unlock();
539 return ret;
540 }
541
542 /* jmp */
543 int com_jmp(__a_unused int fd, int argc, char * const * argv)
544 {
545 long unsigned int i;
546 int ret;
547
548 if (argc != 2)
549 return -E_COMMAND_SYNTAX;
550 if (sscanf(argv[1], "%lu", &i) <= 0)
551 return -E_COMMAND_SYNTAX;
552 mmd_lock();
553 ret = -E_NO_AUDIO_FILE;
554 if (!mmd->afd.afhi.chunks_total)
555 goto out;
556 if (i > 100)
557 i = 100;
558 PARA_INFO_LOG("jumping to %lu%%\n", i);
559 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50)/ 100;
560 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
561 mmd->chunks_sent, mmd->offset);
562 mmd->new_vss_status_flags |= VSS_REPOS;
563 mmd->new_vss_status_flags &= ~VSS_NEXT;
564 ret = 1;
565 mmd->events++;
566 out:
567 mmd_unlock();
568 return ret;
569 }
570
571 /*
572 * check if perms are sufficient to exec a command having perms cmd_perms.
573 * Returns 0 if perms are sufficient, -E_PERM otherwise.
574 */
575 static int check_perms(unsigned int perms, struct server_command *cmd_ptr)
576 {
577 PARA_DEBUG_LOG("%s", "checking permissions\n");
578 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
579 }
580
581 /*
582 * Parse first string from *cmd and lookup in table of valid commands.
583 * On error, NULL is returned.
584 */
585 static struct server_command *parse_cmd(const char *cmdstr)
586 {
587 char buf[255];
588 int n = 0;
589
590 sscanf(cmdstr, "%200s%n", buf, &n);
591 if (!n)
592 return NULL;
593 buf[n] = '\0';
594 return get_cmd_ptr(buf, NULL);
595 }
596
597 static void init_rc4_keys(void)
598 {
599 int i;
600
601 for (i = 0; i < 2 * RC4_KEY_LEN; i++)
602 rc4_buf[i] = para_random(256);
603 PARA_DEBUG_LOG("rc4 keys initialized (%u:%u)\n",
604 (unsigned char) rc4_buf[0],
605 (unsigned char) rc4_buf[RC4_KEY_LEN]);
606 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf);
607 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
608 }
609
610 static void rc4_recv(unsigned long len, const unsigned char *indata,
611 unsigned char *outdata, __a_unused void *private_data)
612 {
613 RC4(&rc4_recv_key, len, indata, outdata);
614 }
615
616 static void rc4_send(unsigned long len, const unsigned char *indata,
617 unsigned char *outdata, __a_unused void *private_data)
618 {
619 RC4(&rc4_send_key, len, indata, outdata);
620 }
621
622 static int read_command(int fd, char **result)
623 {
624 int ret;
625 char buf[4096];
626 char *command = NULL;
627
628 for (;;) {
629 size_t numbytes;
630 char *p;
631
632 ret = recv_buffer(fd, buf, sizeof(buf));
633 if (ret < 0)
634 goto out;
635 if (!ret)
636 break;
637 numbytes = ret;
638 ret = -E_COMMAND_SYNTAX;
639 if (command && numbytes + strlen(command) > MAX_COMMAND_LEN) /* DOS */
640 goto out;
641 command = para_strcat(command, buf);
642 p = strstr(command, EOC_MSG);
643 if (p) {
644 *p = '\0';
645 break;
646 }
647 }
648 ret = command? 1 : -E_COMMAND_SYNTAX;
649 out:
650 if (ret < 0)
651 free(command);
652 else
653 *result = command;
654 return ret;
655
656 }
657
658 /**
659 * perform user authentication and execute a command
660 *
661 * \param fd the file descriptor to send output to
662 * \param addr socket address info of peer
663 *
664 * \return EXIT_SUCCESS or EXIT_FAILURE
665 *
666 * Whenever para_server accepts an incoming tcp connection on
667 * the port it listens on, it forks and the resulting child
668 * calls this function.
669 *
670 * An RSA-based challenge/response is used to authenticate
671 * the peer. It that authentication succeeds, a random RC4
672 * session key is generated and sent back to the peer,
673 * encrypted with its RSA public key. From this point on,
674 * all transfers are crypted with this session key.
675 *
676 * Next it is checked if the peer supplied a valid server command or a command
677 * for the audio file selector. If yes, and if the user has sufficient
678 * permissions to execute that command, the function calls the corresponding
679 * command handler which does argument checking and further processing.
680 *
681 * In order to cope with a DOS attacks, a timeout is set up
682 * which terminates the function if the connection was not
683 * authenticated when the timeout expires.
684 *
685 * \sa alarm(2), rc4(3), crypt.c, crypt.h
686 */
687 int handle_connect(int fd, struct sockaddr_in *addr)
688 {
689 int ret, argc, use_rc4 = 0;
690 char buf[4096];
691 unsigned char crypt_buf[MAXLINE];
692 struct user *u;
693 struct server_command *cmd = NULL;
694 long unsigned challenge_nr, chall_response;
695 char **argv = NULL;
696 char *p, *command = NULL;
697 size_t numbytes;
698
699 signal(SIGCHLD, SIG_IGN);
700 signal(SIGINT, SIG_DFL);
701 signal(SIGTERM, SIG_DFL);
702 signal(SIGHUP, SIG_DFL);
703 signal(SIGUSR1, SIG_IGN);
704
705 challenge_nr = random();
706 /* send Welcome message */
707 ret = send_va_buffer(fd, "This is para_server, version "
708 PACKAGE_VERSION ".\n" );
709 if (ret < 0)
710 goto err_out;
711 /* recv auth request line */
712 ret = recv_buffer(fd, buf, sizeof(buf));
713 if (ret < 0)
714 goto err_out;
715 if (ret <= 6) {
716 ret = -E_AUTH;
717 goto err_out;
718 }
719 numbytes = ret;
720 ret = -E_AUTH;
721 if (strncmp(buf, "auth ", 5))
722 goto err_out;
723
724 if (numbytes < 9 || strncmp(buf, "auth rc4 ", 9))
725 p = buf + 5; /* client version < 0.2.6 */
726 else {
727 p = buf + 9; /* client version >= 0.2.6 */
728 use_rc4 = 1;
729 }
730 PARA_DEBUG_LOG("received %s request for user %s\n",
731 use_rc4? "rc4" : "auth", p);
732 ret = -E_BAD_USER;
733 u = lookup_user(p);
734 if (!u)
735 goto err_out;
736 ret = para_encrypt_challenge(u->rsa, challenge_nr, crypt_buf);
737 if (ret <= 0)
738 goto err_out;
739 numbytes = ret;
740 PARA_DEBUG_LOG("sending %zu byte challenge\n", numbytes);
741 /* We can't use send_buffer here since buf may contain null bytes */
742 ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes);
743 if (ret < 0)
744 goto err_out;
745 /* recv decrypted number */
746 ret = recv_buffer(fd, buf, sizeof(buf));
747 if (ret < 0)
748 goto err_out;
749 numbytes = ret;
750 ret = -E_AUTH;
751 if (!numbytes)
752 goto err_out;
753 if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1
754 || chall_response != challenge_nr)
755 goto err_out;
756 /* auth successful, send 'Proceed' message */
757 PARA_INFO_LOG("good auth for %s (%lu)\n", u->name, challenge_nr);
758 sprintf(buf, "%s", PROCEED_MSG);
759 if (use_rc4) {
760 init_rc4_keys();
761 ret = para_encrypt_buffer(u->rsa, rc4_buf, 2 * RC4_KEY_LEN,
762 (unsigned char *)buf + PROCEED_MSG_LEN + 1);
763 if (ret <= 0)
764 goto err_out;
765 numbytes = ret + strlen(PROCEED_MSG) + 1;
766 } else
767 numbytes = strlen(buf);
768 ret = send_bin_buffer(fd, buf, numbytes);
769 if (ret < 0)
770 goto err_out;
771 if (use_rc4)
772 enable_crypt(fd, rc4_recv, rc4_send, NULL);
773 ret = read_command(fd, &command);
774 if (ret < 0)
775 goto err_out;
776 ret = -E_BAD_CMD;
777 cmd = parse_cmd(command);
778 if (!cmd)
779 goto err_out;
780 /* valid command, check permissions */
781 ret = check_perms(u->perms, cmd);
782 if (ret < 0)
783 goto err_out;
784 /* valid command and sufficient perms */
785 alarm(0);
786 argc = split_args(command, &argv, "\n");
787 mmd_lock();
788 mmd->num_commands++;
789 mmd_unlock();
790 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
791 inet_ntoa(addr->sin_addr));
792 ret = cmd->handler(fd, argc, argv);
793 if (ret >= 0) {
794 ret = EXIT_SUCCESS;
795 goto out;
796 }
797 err_out:
798 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
799 if (ret != -E_SEND && ret != -E_RECV)
800 send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret));
801 ret = EXIT_FAILURE;
802 out:
803 free(command);
804 free(argv);
805 mmd_lock();
806 if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
807 mmd->events++;
808 mmd->active_connections--;
809 mmd_unlock();
810 return ret;
811 }