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