2 * Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file command.c Client authentication and server commands. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
13 #include <sys/types.h>
15 #include <arpa/inet.h>
24 #include "server.cmdline.h"
37 #include "user_list.h"
38 #include "server.command_list.h"
39 #include "afs.command_list.h"
43 typedef int server_command_handler_t(struct command_context *);
44 static server_command_handler_t SERVER_COMMAND_HANDLERS;
45 server_command_handler_t AFS_COMMAND_HANDLERS;
47 /* Defines one command of para_server. */
48 struct server_command {
49 /* The name of the command. */
51 /* Pointer to the function that handles the command. */
52 server_command_handler_t *handler;
53 /* The privileges a user must have to execute this command. */
55 /* One-line description of the command. */
56 const char *description;
57 /* Summary of the command line options. */
59 /* The long help text. */
63 static struct server_command afs_cmds[] = {DEFINE_AFS_CMD_ARRAY};
64 static struct server_command server_cmds[] = {DEFINE_SERVER_CMD_ARRAY};
66 /** Commands including options must be shorter than this. */
67 #define MAX_COMMAND_LEN 32768
70 extern struct misc_meta_data *mmd;
71 extern struct sender senders[];
72 int send_afs_status(struct command_context *cc, int parser_friendly);
74 static void dummy(__a_unused int s)
78 static void mmd_dup(struct misc_meta_data *new_mmd)
80 mutex_lock(mmd_mutex);
82 mutex_unlock(mmd_mutex);
86 * Compute human readable string containing vss status for given integer value.
88 * We don't want to use vss_playing() and friends here because we take a
89 * snapshot of the mmd struct and use the copy for computing the state of the
90 * vss. If the real data were used, we would take the mmd lock for a rather
91 * long time or risk to get an inconsistent view.
93 static char *vss_status_tohuman(unsigned int flags)
95 if (flags & VSS_PLAYING)
96 return para_strdup("playing");
98 return para_strdup("stopped");
99 return para_strdup("paused");
103 * return human readable permission string. Never returns NULL.
105 static char *cmd_perms_itohuman(unsigned int perms)
107 char *msg = para_malloc(5 * sizeof(char));
109 msg[0] = perms & AFS_READ? 'a' : '-';
110 msg[1] = perms & AFS_WRITE? 'A' : '-';
111 msg[2] = perms & VSS_READ? 'v' : '-';
112 msg[3] = perms & VSS_WRITE? 'V' : '-';
118 * Never returns NULL.
120 static char *vss_get_status_flags(unsigned int flags)
122 char *msg = para_malloc(5 * sizeof(char));
124 msg[0] = (flags & VSS_PLAYING)? 'P' : '_';
125 msg[1] = (flags & VSS_NOMORE)? 'O' : '_';
126 msg[2] = (flags & VSS_NEXT)? 'N' : '_';
127 msg[3] = (flags & VSS_REPOS)? 'R' : '_';
132 static unsigned get_status(struct misc_meta_data *nmmd, int parser_friendly,
135 char *status, *flags; /* vss status info */
136 /* nobody updates our version of "now" */
137 long offset = (nmmd->offset + 500) / 1000;
138 struct timeval current_time;
139 struct para_buffer b = {.flags = parser_friendly? PBF_SIZE_PREFIX : 0};
141 /* report real status */
142 status = vss_status_tohuman(nmmd->vss_status_flags);
143 flags = vss_get_status_flags(nmmd->vss_status_flags);
144 clock_get_realtime(¤t_time);
146 * The calls to WRITE_STATUS_ITEM() below never fail because
147 * b->max_size is zero (unlimited), see para_printf(). However, clang
148 * is not smart enough to prove this and complains nevertheless.
149 * Casting the return value to void silences clang.
151 (void)WRITE_STATUS_ITEM(&b, SI_STATUS, "%s\n", status);
152 (void)WRITE_STATUS_ITEM(&b, SI_STATUS_FLAGS, "%s\n", flags);
153 (void)WRITE_STATUS_ITEM(&b, SI_OFFSET, "%li\n", offset);
154 (void)WRITE_STATUS_ITEM(&b, SI_AFS_MODE, "%s\n", mmd->afs_mode_string);
155 (void)WRITE_STATUS_ITEM(&b, SI_STREAM_START, "%lu.%lu\n",
156 (long unsigned)nmmd->stream_start.tv_sec,
157 (long unsigned)nmmd->stream_start.tv_usec);
158 (void)WRITE_STATUS_ITEM(&b, SI_CURRENT_TIME, "%lu.%lu\n",
159 (long unsigned)current_time.tv_sec,
160 (long unsigned)current_time.tv_usec);
167 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
170 /* this has to match sender.h */
171 const char *subcmds[] = {"add", "delete", "allow", "deny", "on", "off", NULL};
173 scd->sender_num = -1;
175 return -E_COMMAND_SYNTAX;
176 for (i = 0; senders[i].name; i++)
177 if (!strcmp(senders[i].name, argv[1]))
179 PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
180 if (!senders[i].name)
181 return -E_COMMAND_SYNTAX;
183 for (i = 0; subcmds[i]; i++)
184 if (!strcmp(subcmds[i], argv[2]))
187 return -E_COMMAND_SYNTAX;
189 if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
190 return -E_SENDER_CMD;
191 switch (scd->cmd_num) {
195 return -E_COMMAND_SYNTAX;
199 if (argc != 4 || parse_cidr(argv[3], scd->host,
200 sizeof(scd->host), &scd->netmask) == NULL)
201 return -E_COMMAND_SYNTAX;
206 return -E_COMMAND_SYNTAX;
207 return parse_fec_url(argv[3], scd);
209 return -E_COMMAND_SYNTAX;
215 * Send a sideband packet through a blocking file descriptor.
217 * \param scc fd and crypto keys.
218 * \param buf The buffer to send.
219 * \param numbytes The size of \a buf.
220 * \param band The sideband designator of this packet.
221 * \param dont_free If true, never deallocate \a buf.
223 * The nonblock flag must be disabled for the file descriptor given by \a scc.
225 * Stream cipher encryption is automatically activated if necessary via the
226 * sideband transformation, depending on the value of \a band.
230 * \sa \ref send_sb_va().
232 int send_sb(struct stream_cipher_context *scc, void *buf, size_t numbytes,
233 int band, bool dont_free)
236 struct sb_context *sbc;
238 sb_transformation trafo = band < SBD_PROCEED? NULL : sc_trafo;
239 struct sb_buffer sbb = SBB_INIT(band, buf, numbytes);
241 sbc = sb_new_send(&sbb, dont_free, trafo, scc->send);
243 ret = sb_get_send_buffers(sbc, iov);
244 ret = xwritev(scc->fd, iov, ret);
247 } while (sb_sent(sbc, ret) == false);
255 * Create a variable sized buffer and send it as a sideband packet.
257 * \param scc Passed to \ref send_sb.
258 * \param band See \ref send_sb.
259 * \param fmt The format string.
261 * \return The return value of the underlying call to \ref send_sb.
263 __printf_3_4 int send_sb_va(struct stream_cipher_context *scc, int band,
264 const char *fmt, ...)
271 ret = xvasprintf(&msg, fmt, ap);
273 return send_sb(scc, msg, ret, band, false);
277 * Send an error message to a client.
279 * \param cc Client info.
280 * \param err The (positive) error code.
282 * \return The return value of the underlying call to send_sb_va().
284 int send_strerror(struct command_context *cc, int err)
286 return send_sb_va(&cc->scc, SBD_ERROR_LOG, "%s\n", para_strerror(err));
290 * Send a sideband packet through a blocking file descriptor.
292 * \param scc fd and crypto keys.
293 * \param expected_band The expected band designator.
294 * \param max_size Passed to \ref sb_new_recv().
295 * \param result Body of the sideband packet is returned here.
297 * If \a expected_band is not \p SBD_ANY, the band designator of the received
298 * sideband packet is compared to \a expected_band and a mismatch is considered
303 int recv_sb(struct stream_cipher_context *scc,
304 enum sb_designator expected_band,
305 size_t max_size, struct iovec *result)
308 struct sb_context *sbc;
310 struct sb_buffer sbb;
311 sb_transformation trafo;
313 trafo = expected_band != SBD_ANY && expected_band < SBD_PROCEED?
315 sbc = sb_new_recv(max_size, trafo, scc->recv);
317 sb_get_recv_buffer(sbc, &iov);
318 ret = recv_bin_buffer(scc->fd, iov.iov_base, iov.iov_len);
323 ret = sb_received(sbc, ret, &sbb);
330 if (expected_band != SBD_ANY && sbb.band != expected_band)
339 static int com_sender(struct command_context *cc)
343 struct sender_command_data scd;
346 for (i = 0; senders[i].name; i++) {
348 ret = xasprintf(&tmp, "%s%s\n", msg? msg : "",
353 return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
355 ret = check_sender_args(cc->argc, cc->argv, &scd);
357 if (scd.sender_num < 0)
359 if (strcmp(cc->argv[2], "status") == 0)
360 msg = senders[scd.sender_num].status();
362 msg = senders[scd.sender_num].help();
363 return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT, false);
366 switch (scd.cmd_num) {
369 assert(senders[scd.sender_num].resolve_target);
370 ret = senders[scd.sender_num].resolve_target(cc->argv[3], &scd);
375 for (i = 0; i < 10; i++) {
376 mutex_lock(mmd_mutex);
377 if (mmd->sender_cmd_data.cmd_num >= 0) {
378 /* another sender command is active, retry in 100ms */
379 struct timespec ts = {.tv_nsec = 100 * 1000 * 1000};
380 mutex_unlock(mmd_mutex);
381 nanosleep(&ts, NULL);
384 mmd->sender_cmd_data = scd;
385 mutex_unlock(mmd_mutex);
388 return (i < 10)? 1 : -E_LOCK;
392 static int com_si(struct command_context *cc)
398 return -E_COMMAND_SYNTAX;
399 mutex_lock(mmd_mutex);
400 ut = daemon_get_uptime_str(now);
401 ret = xasprintf(&msg,
402 "up: %s\nplayed: %u\n"
405 "connections (active/accepted/total): %u/%u/%u\n"
406 "current loglevel: %s\n"
407 "supported audio formats: %s\n",
411 mmd->active_connections,
415 AUDIO_FORMAT_HANDLERS
417 mutex_unlock(mmd_mutex);
419 return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
423 static int com_version(struct command_context *cc)
428 if (cc->argc > 1 && strcmp(cc->argv[1], "-v") == 0)
429 len = xasprintf(&msg, "%s", version_text("server"));
431 len = xasprintf(&msg, "%s\n", version_single_line("server"));
432 return send_sb(&cc->scc, msg, len, SBD_OUTPUT, false);
435 /** These status items are cleared if no audio file is currently open. */
436 #define EMPTY_STATUS_ITEMS \
441 ITEM(ATTRIBUTES_BITMAP) \
442 ITEM(ATTRIBUTES_TXT) \
453 ITEM(SECONDS_TOTAL) \
466 ITEM(AMPLIFICATION) \
469 * Write a list of audio-file related status items with empty values.
471 * This is used by vss when currently no audio file is open.
473 static unsigned empty_status_items(int parser_friendly, char **result)
479 len = xasprintf(&esi,
480 #define ITEM(x) "0004 %02x:\n"
483 #define ITEM(x) , SI_ ## x
488 len = xasprintf(&esi,
489 #define ITEM(x) "%s:\n"
492 #define ITEM(x) ,status_item_list[SI_ ## x]
499 #undef EMPTY_STATUS_ITEMS
502 static int com_stat(struct command_context *cc)
505 struct misc_meta_data tmp, *nmmd = &tmp;
508 int parser_friendly = 0;
510 para_sigaction(SIGUSR1, dummy);
512 for (i = 1; i < cc->argc; i++) {
513 const char *arg = cc->argv[i];
516 if (!strcmp(arg, "--")) {
520 if (!strncmp(arg, "-n=", 3)) {
521 ret = para_atoi32(arg + 3, &num);
526 if (!strcmp(arg, "-p")) {
530 return -E_COMMAND_SYNTAX;
533 return -E_COMMAND_SYNTAX;
536 ret = get_status(nmmd, parser_friendly, &s);
537 ret = send_sb(&cc->scc, s, ret, SBD_OUTPUT, false);
540 if (nmmd->vss_status_flags & VSS_NEXT) {
542 ret = empty_status_items(parser_friendly, &esi);
543 ret = send_sb(&cc->scc, esi, ret, SBD_OUTPUT, false);
547 send_afs_status(cc, parser_friendly);
549 if (num > 0 && !--num)
552 ret = -E_SERVER_CRASH;
560 static int send_list_of_commands(struct command_context *cc, struct server_command *cmd,
565 for (; cmd->name; cmd++) {
566 char *tmp, *perms = cmd_perms_itohuman(cmd->perms);
567 tmp = make_message("%s\t%s\t%s\t%s\n", cmd->name, handler,
568 perms, cmd->description);
570 msg = para_strcat(msg, tmp);
574 return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT, false);
577 /* returns string that must be freed by the caller */
578 static struct server_command *get_cmd_ptr(const char *name, char **handler)
580 struct server_command *cmd;
582 for (cmd = server_cmds; cmd->name; cmd++)
583 if (!strcmp(cmd->name, name)) {
585 *handler = para_strdup("server"); /* server commands */
588 /* not found, look for commands supported by afs */
589 for (cmd = afs_cmds; cmd->name; cmd++)
590 if (!strcmp(cmd->name, name)) {
592 *handler = para_strdup("afs");
599 static int com_help(struct command_context *cc)
601 struct server_command *cmd;
602 char *perms, *handler, *buf;
606 /* no argument given, print list of commands */
607 if ((ret = send_list_of_commands(cc, server_cmds, "server")) < 0)
609 return send_list_of_commands(cc, afs_cmds, "afs");
611 /* argument given for help */
612 cmd = get_cmd_ptr(cc->argv[1], &handler);
615 perms = cmd_perms_itohuman(cmd->perms);
616 ret = xasprintf(&buf, "%s - %s\n\n"
630 return send_sb(&cc->scc, buf, ret, SBD_OUTPUT, false);
634 static int com_hup(struct command_context *cc)
637 return -E_COMMAND_SYNTAX;
638 kill(getppid(), SIGHUP);
643 static int com_term(struct command_context *cc)
646 return -E_COMMAND_SYNTAX;
647 kill(getppid(), SIGTERM);
651 static int com_play(struct command_context *cc)
654 return -E_COMMAND_SYNTAX;
655 mutex_lock(mmd_mutex);
656 mmd->new_vss_status_flags |= VSS_PLAYING;
657 mmd->new_vss_status_flags &= ~VSS_NOMORE;
658 mutex_unlock(mmd_mutex);
663 static int com_stop(struct command_context *cc)
666 return -E_COMMAND_SYNTAX;
667 mutex_lock(mmd_mutex);
668 mmd->new_vss_status_flags &= ~VSS_PLAYING;
669 mmd->new_vss_status_flags &= ~VSS_REPOS;
670 mmd->new_vss_status_flags |= VSS_NEXT;
671 mutex_unlock(mmd_mutex);
676 static int com_pause(struct command_context *cc)
679 return -E_COMMAND_SYNTAX;
680 mutex_lock(mmd_mutex);
681 if (!vss_paused() && !vss_stopped()) {
683 mmd->new_vss_status_flags &= ~VSS_PLAYING;
684 mmd->new_vss_status_flags &= ~VSS_NEXT;
686 mutex_unlock(mmd_mutex);
691 static int com_next(struct command_context *cc)
694 return -E_COMMAND_SYNTAX;
695 mutex_lock(mmd_mutex);
697 mmd->new_vss_status_flags |= VSS_NEXT;
698 mutex_unlock(mmd_mutex);
703 static int com_nomore(struct command_context *cc)
706 return -E_COMMAND_SYNTAX;
707 mutex_lock(mmd_mutex);
708 if (vss_playing() || vss_paused())
709 mmd->new_vss_status_flags |= VSS_NOMORE;
710 mutex_unlock(mmd_mutex);
715 static int com_ff(struct command_context *cc)
718 int ret, backwards = 0;
723 return -E_COMMAND_SYNTAX;
724 if (!(ret = sscanf(cc->argv[1], "%u%c", &i, &c)))
725 return -E_COMMAND_SYNTAX;
726 if (ret > 1 && c == '-')
727 backwards = 1; /* jmp backwards */
728 mutex_lock(mmd_mutex);
729 ret = -E_NO_AUDIO_FILE;
730 if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
732 promille = (1000 * mmd->current_chunk) / mmd->afd.afhi.chunks_total;
734 promille -= 1000 * i / mmd->afd.afhi.seconds_total;
736 promille += 1000 * i / mmd->afd.afhi.seconds_total;
739 if (promille > 1000) {
740 mmd->new_vss_status_flags |= VSS_NEXT;
743 mmd->repos_request = (mmd->afd.afhi.chunks_total * promille) / 1000;
744 mmd->new_vss_status_flags |= VSS_REPOS;
745 mmd->new_vss_status_flags &= ~VSS_NEXT;
749 mutex_unlock(mmd_mutex);
754 static int com_jmp(struct command_context *cc)
760 return -E_COMMAND_SYNTAX;
761 if (sscanf(cc->argv[1], "%lu", &i) <= 0)
762 return -E_COMMAND_SYNTAX;
763 mutex_lock(mmd_mutex);
764 ret = -E_NO_AUDIO_FILE;
765 if (!mmd->afd.afhi.chunks_total)
769 PARA_INFO_LOG("jumping to %lu%%\n", i);
770 mmd->repos_request = (mmd->afd.afhi.chunks_total * i + 50) / 100;
771 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
772 mmd->chunks_sent, mmd->offset);
773 mmd->new_vss_status_flags |= VSS_REPOS;
774 mmd->new_vss_status_flags &= ~VSS_NEXT;
778 mutex_unlock(mmd_mutex);
782 static int com_tasks(struct command_context *cc)
784 char *tl = server_get_tasks();
788 ret = send_sb(&cc->scc, tl, strlen(tl), SBD_OUTPUT, false);
793 * check if perms are sufficient to exec a command having perms cmd_perms.
794 * Returns 0 if perms are sufficient, -E_PERM otherwise.
796 static int check_perms(unsigned int perms, const struct server_command *cmd_ptr)
798 PARA_DEBUG_LOG("checking permissions\n");
799 return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0;
802 static void reset_signals(void)
804 para_sigaction(SIGCHLD, SIG_IGN);
805 para_sigaction(SIGINT, SIG_DFL);
806 para_sigaction(SIGTERM, SIG_DFL);
807 para_sigaction(SIGHUP, SIG_DFL);
810 struct connection_features {
811 bool sideband_requested;
812 bool aes_ctr128_requested;
815 static int parse_auth_request(char *buf, int len, struct user **u,
816 struct connection_features *cf)
819 char *p, *username, **features = NULL;
820 size_t auth_rq_len = strlen(AUTH_REQUEST_MSG);
823 memset(cf, 0, sizeof(*cf));
824 if (len < auth_rq_len + 2)
825 return -E_AUTH_REQUEST;
826 if (strncmp(buf, AUTH_REQUEST_MSG, auth_rq_len) != 0)
827 return -E_AUTH_REQUEST;
828 username = buf + auth_rq_len;
829 p = strchr(username, ' ');
833 return -E_AUTH_REQUEST;
836 create_argv(p, ",", &features);
837 for (i = 0; features[i]; i++) {
838 if (strcmp(features[i], "sideband") == 0)
839 cf->sideband_requested = true;
840 else if (strcmp(features[i], "aes_ctr128") == 0)
841 cf->aes_ctr128_requested = true;
843 ret = -E_BAD_FEATURE;
848 PARA_DEBUG_LOG("received auth request for user %s\n", username);
849 *u = lookup_user(username);
856 #define HANDSHAKE_BUFSIZE 4096
858 static int run_command(struct command_context *cc, struct iovec *iov,
859 const char *peername)
863 struct server_command *cmd;
865 if (iov->iov_base == NULL || iov->iov_len == 0)
868 p[iov->iov_len - 1] = '\0'; /* just to be sure */
869 cmd = get_cmd_ptr(p, NULL);
872 ret = check_perms(cc->u->perms, cmd);
875 end = iov->iov_base + iov->iov_len;
876 for (i = 0; p < end; i++)
879 cc->argv = para_malloc((cc->argc + 1) * sizeof(char *));
880 for (i = 0, p = iov->iov_base; p < end; i++) {
881 cc->argv[i] = para_strdup(p);
884 cc->argv[cc->argc] = NULL;
885 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name,
886 cc->u->name, peername);
887 ret = cmd->handler(cc);
889 mutex_lock(mmd_mutex);
891 if (ret >= 0 && (cmd->perms & AFS_WRITE))
893 mutex_unlock(mmd_mutex);
898 * Perform user authentication and execute a command.
900 * \param fd The file descriptor to send output to.
901 * \param peername Identifies the connecting peer.
903 * Whenever para_server accepts an incoming tcp connection on the port it
904 * listens on, it forks and the resulting child calls this function.
906 * An RSA-based challenge/response is used to authenticate the peer. It that
907 * authentication succeeds, a random session key is generated and sent back to
908 * the peer, encrypted with its RSA public key. From this point on, all
909 * transfers are crypted with this session key.
911 * Next it is checked if the peer supplied a valid server command or a command
912 * for the audio file selector. If yes, and if the user has sufficient
913 * permissions to execute that command, the function calls the corresponding
914 * command handler which does argument checking and further processing.
916 * In order to cope with a DOS attacks, a timeout is set up which terminates
917 * the function if the connection was not authenticated when the timeout
920 * \sa alarm(2), crypt.c, crypt.h
922 __noreturn void handle_connect(int fd, const char *peername)
925 unsigned char rand_buf[CHALLENGE_SIZE + 2 * SESSION_KEY_LEN];
926 unsigned char challenge_hash[HASH_SIZE];
927 char *command = NULL, *buf = para_malloc(HANDSHAKE_BUFSIZE) /* must be on the heap */;
929 struct command_context cc_struct = {.peer = peername}, *cc = &cc_struct;
931 struct connection_features cf;
935 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
936 ret = mark_fd_blocking(fd);
939 /* send Welcome message */
940 ret = write_va_buffer(fd, "This is para_server, version "
941 PACKAGE_VERSION ".\n"
942 "Features: sideband,aes_ctr128\n"
946 /* recv auth request line */
947 ret = recv_buffer(fd, buf, HANDSHAKE_BUFSIZE);
950 ret = parse_auth_request(buf, ret, &cc->u, &cf);
953 if (!cf.sideband_requested) { /* sideband is mandatory */
954 PARA_ERROR_LOG("client did not request sideband\n");
955 ret = -E_BAD_FEATURE;
959 get_random_bytes_or_die(rand_buf, sizeof(rand_buf));
960 ret = pub_encrypt(cc->u->pubkey, rand_buf, sizeof(rand_buf),
961 (unsigned char *)buf);
967 * We don't want to reveal our user names, so we send a
968 * challenge to the client even if the user does not exist, and
969 * fail the authentication later.
972 get_random_bytes_or_die((unsigned char *)buf, numbytes);
974 PARA_DEBUG_LOG("sending %u byte challenge + session key (%zu bytes)\n",
975 CHALLENGE_SIZE, numbytes);
976 ret = send_sb(&cc->scc, buf, numbytes, SBD_CHALLENGE, false);
980 ret = recv_sb(&cc->scc, SBD_CHALLENGE_RESPONSE,
981 HANDSHAKE_BUFSIZE, &iov);
985 numbytes = iov.iov_len;
986 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes);
991 * The correct response is the hash of the first CHALLENGE_SIZE bytes
992 * of the random data.
995 if (numbytes != HASH_SIZE)
997 hash_function((char *)rand_buf, CHALLENGE_SIZE, challenge_hash);
998 if (memcmp(challenge_hash, buf, HASH_SIZE))
1000 /* auth successful */
1002 PARA_INFO_LOG("good auth for %s\n", cc->u->name);
1003 /* init stream cipher keys with the second part of the random buffer */
1004 cc->scc.recv = sc_new(rand_buf + CHALLENGE_SIZE, SESSION_KEY_LEN,
1005 cf.aes_ctr128_requested);
1006 cc->scc.send = sc_new(rand_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
1007 SESSION_KEY_LEN, cf.aes_ctr128_requested);
1008 ret = send_sb(&cc->scc, NULL, 0, SBD_PROCEED, false);
1011 ret = recv_sb(&cc->scc, SBD_COMMAND, MAX_COMMAND_LEN, &iov);
1014 ret = run_command(cc, &iov, peername);
1021 if (send_strerror(cc, -ret) >= 0)
1022 send_sb(&cc->scc, NULL, 0, SBD_EXIT__FAILURE, true);
1024 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
1028 mutex_lock(mmd_mutex);
1029 mmd->active_connections--;
1030 mutex_unlock(mmd_mutex);
1032 ret = send_sb(&cc->scc, NULL, 0, SBD_EXIT__SUCCESS, true);
1034 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
1036 sc_free(cc->scc.recv);
1037 sc_free(cc->scc.send);
1038 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);