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