2 * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file command.c Client authentication and server commands. */
12 #include <sys/types.h>
20 #include "server.cmdline.h"
33 #include "user_list.h"
34 #include "server_command_list.h"
35 #include "afs_command_list.h"
39 /** Commands including options must be shorter than this. */
40 #define MAX_COMMAND_LEN 32768
43 extern struct misc_meta_data
*mmd
;
44 extern struct sender senders
[];
45 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
47 static void dummy(__a_unused
int s
)
51 static void mmd_dup(struct misc_meta_data
*new_mmd
)
53 mutex_lock(mmd_mutex
);
55 mutex_unlock(mmd_mutex
);
59 * Compute human readable string containing vss status for given integer value.
61 * We don't want to use vss_playing() and friends here because we take a
62 * snapshot of the mmd struct and use the copy for computing the state of the
63 * vss. If the real data were used, we would take the mmd lock for a rather
64 * long time or risk to get an inconsistent view.
66 static char *vss_status_tohuman(unsigned int flags
)
68 if (flags
& VSS_PLAYING
)
69 return para_strdup("playing");
71 return para_strdup("stopped");
72 return para_strdup("paused");
76 * return human readable permission string. Never returns NULL.
78 static char *cmd_perms_itohuman(unsigned int perms
)
80 char *msg
= para_malloc(5 * sizeof(char));
82 msg
[0] = perms
& AFS_READ
? 'a' : '-';
83 msg
[1] = perms
& AFS_WRITE
? 'A' : '-';
84 msg
[2] = perms
& VSS_READ
? 'v' : '-';
85 msg
[3] = perms
& VSS_WRITE
? 'V' : '-';
93 static char *vss_get_status_flags(unsigned int flags
)
95 char *msg
= para_malloc(5 * sizeof(char));
97 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
98 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
99 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
100 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
105 static unsigned get_status(struct misc_meta_data
*nmmd
, int parser_friendly
,
109 char *status
, *flags
; /* vss status info */
110 /* nobody updates our version of "now" */
111 char *ut
= get_server_uptime_str(NULL
);
112 long offset
= (nmmd
->offset
+ 500) / 1000;
113 struct timeval current_time
;
115 struct para_buffer b
= {.flags
= parser_friendly
? PBF_SIZE_PREFIX
: 0};
117 /* report real status */
118 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
119 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
120 if (nmmd
->size
) { /* parent currently has an audio file open */
121 localtime_r(&nmmd
->mtime
, &mtime_tm
);
122 strftime(mtime
, 29, "%b %d %Y", &mtime_tm
);
124 gettimeofday(¤t_time
, NULL
);
126 * The calls to WRITE_STATUS_ITEM() below never fail because
127 * b->max_size is zero (unlimited), see para_printf(). However, clang
128 * is not smart enough to prove this and complains nevertheless.
129 * Casting the return value to void silences clang.
131 (void)WRITE_STATUS_ITEM(&b
, SI_FILE_SIZE
, "%zu\n", nmmd
->size
/ 1024);
132 (void)WRITE_STATUS_ITEM(&b
, SI_MTIME
, "%s\n", mtime
);
133 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS
, "%s\n", status
);
134 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS_FLAGS
, "%s\n", flags
);
135 (void)WRITE_STATUS_ITEM(&b
, SI_OFFSET
, "%li\n", offset
);
136 (void)WRITE_STATUS_ITEM(&b
, SI_AFS_MODE
, "%s\n", mmd
->afs_mode_string
);
137 (void)WRITE_STATUS_ITEM(&b
, SI_STREAM_START
, "%lu.%lu\n",
138 (long unsigned)nmmd
->stream_start
.tv_sec
,
139 (long unsigned)nmmd
->stream_start
.tv_usec
);
140 (void)WRITE_STATUS_ITEM(&b
, SI_CURRENT_TIME
, "%lu.%lu\n",
141 (long unsigned)current_time
.tv_sec
,
142 (long unsigned)current_time
.tv_usec
);
150 static int check_sender_args(int argc
, char * const * argv
, struct sender_command_data
*scd
)
153 /* this has to match sender.h */
154 const char *subcmds
[] = {"add", "delete", "allow", "deny", "on", "off", NULL
};
156 scd
->sender_num
= -1;
158 return -E_COMMAND_SYNTAX
;
159 for (i
= 0; senders
[i
].name
; i
++)
160 if (!strcmp(senders
[i
].name
, argv
[1]))
162 PARA_DEBUG_LOG("%d:%s\n", argc
, argv
[1]);
163 if (!senders
[i
].name
)
164 return -E_COMMAND_SYNTAX
;
166 for (i
= 0; subcmds
[i
]; i
++)
167 if (!strcmp(subcmds
[i
], argv
[2]))
170 return -E_COMMAND_SYNTAX
;
172 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
])
173 return -E_SENDER_CMD
;
174 switch (scd
->cmd_num
) {
178 return -E_COMMAND_SYNTAX
;
182 if (argc
!= 4 || parse_cidr(argv
[3], scd
->host
,
183 sizeof(scd
->host
), &scd
->netmask
) == NULL
)
184 return -E_COMMAND_SYNTAX
;
189 return -E_COMMAND_SYNTAX
;
190 return parse_fec_url(argv
[3], scd
);
192 return -E_COMMAND_SYNTAX
;
198 * Send a sideband packet through a blocking file descriptor.
200 * \param scc fd and crypto keys.
201 * \param buf The buffer to send.
202 * \param numbytes The size of \a buf.
203 * \param band The sideband designator of this packet.
204 * \param dont_free If true, never deallocate \a buf.
206 * The nonblock flag must be disabled for the file descriptor given by \a scc.
208 * Stream cipher encryption is automatically activated if neccessary via the
209 * sideband transformation, depending on the value of \a band.
213 * \sa \ref send_sb_va().
215 int send_sb(struct stream_cipher_context
*scc
, void *buf
, size_t numbytes
,
216 int band
, bool dont_free
)
219 struct sb_context
*sbc
;
221 struct sb_buffer sbb
= SBB_INIT(band
, buf
, numbytes
);
222 sb_transformation trafo
= band
< SBD_PROCEED
? NULL
: sc_trafo
;
224 sbc
= sb_new_send(&sbb
, dont_free
, trafo
, scc
->send
);
226 ret
= sb_get_send_buffers(sbc
, iov
);
227 ret
= xwritev(scc
->fd
, iov
, ret
);
230 } while (sb_sent(sbc
, ret
) == false);
238 * Create a variable sized buffer and send it as a sideband packet.
240 * \param scc Passed to \ref send_sb.
241 * \param band See \ref send_sb.
242 * \param fmt The format string.
244 * \return The return value of the underlying call to \ref send_sb.
246 __printf_3_4
int send_sb_va(struct stream_cipher_context
*scc
, int band
,
247 const char *fmt
, ...)
254 ret
= xvasprintf(&msg
, fmt
, ap
);
256 return send_sb(scc
, msg
, ret
, band
, false);
260 * Send a sideband packet through a blocking file descriptor.
262 * \param scc fd and crypto keys.
263 * \param expected_band The expected band designator.
264 * \param max_size Passed to \ref sb_new_recv().
265 * \param result Body of the sideband packet is returned here.
267 * If \a expected_band is not \p SBD_ANY, the band designator of the received
268 * sideband packet is compared to \a expected_band and a mismatch is considered
273 int recv_sb(struct stream_cipher_context
*scc
,
274 enum sb_designator expected_band
,
275 size_t max_size
, struct iovec
*result
)
278 struct sb_context
*sbc
;
280 struct sb_buffer sbb
;
281 sb_transformation trafo
;
283 trafo
= expected_band
!= SBD_ANY
&& expected_band
< SBD_PROCEED
?
285 sbc
= sb_new_recv(max_size
, trafo
, scc
->recv
);
287 sb_get_recv_buffer(sbc
, &iov
);
288 ret
= recv_bin_buffer(scc
->fd
, iov
.iov_base
, iov
.iov_len
);
293 ret
= sb_received(sbc
, ret
, &sbb
);
300 if (expected_band
!= SBD_ANY
&& sbb
.band
!= expected_band
)
309 int com_sender(struct command_context
*cc
)
313 struct sender_command_data scd
;
316 for (i
= 0; senders
[i
].name
; i
++) {
317 char *tmp
= make_message("%s%s\n",
318 msg
? msg
: "", senders
[i
].name
);
322 ret
= sc_send_buffer(&cc
->scc
, msg
);
326 ret
= check_sender_args(cc
->argc
, cc
->argv
, &scd
);
328 if (scd
.sender_num
< 0)
330 msg
= senders
[scd
.sender_num
].help();
331 ret
= sc_send_buffer(&cc
->scc
, msg
);
336 switch (scd
.cmd_num
) {
339 assert(senders
[scd
.sender_num
].resolve_target
);
340 ret
= senders
[scd
.sender_num
].resolve_target(cc
->argv
[3], &scd
);
345 for (i
= 0; i
< 10; i
++) {
346 mutex_lock(mmd_mutex
);
347 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
348 mutex_unlock(mmd_mutex
);
352 memcpy(&mmd
->sender_cmd_data
, &scd
, sizeof(scd
));
353 mutex_unlock(mmd_mutex
);
356 return (i
< 10)? 1 : -E_LOCK
;
360 int com_si(struct command_context
*cc
)
364 char *sender_info
= NULL
;
367 return -E_COMMAND_SYNTAX
;
368 mutex_lock(mmd_mutex
);
369 for (i
= 0; senders
[i
].name
; i
++) {
370 char *info
= senders
[i
].info();
371 sender_info
= para_strcat(sender_info
, info
);
374 ut
= get_server_uptime_str(now
);
375 ret
= sc_send_va_buffer(&cc
->scc
, "version: " GIT_VERSION
"\n"
376 "up: %s\nplayed: %u\n"
379 "connections (active/accepted/total): %u/%u/%u\n"
380 "current loglevel: %s\n"
381 "supported audio formats: %s\n"
386 mmd
->active_connections
,
390 SERVER_AUDIO_FORMATS
,
393 mutex_unlock(mmd_mutex
);
400 int com_version(struct command_context
*cc
)
403 return -E_COMMAND_SYNTAX
;
404 return sc_send_buffer(&cc
->scc
, VERSION_TEXT("server")
405 "built: " BUILD_DATE
"\n"
406 UNAME_RS
", " CC_VERSION
"\n"
410 #define EMPTY_STATUS_ITEMS \
415 ITEM(ATTRIBUTES_BITMAP) \
416 ITEM(ATTRIBUTES_TXT) \
427 ITEM(SECONDS_TOTAL) \
439 * Write a list of audio-file related status items with empty values.
441 * This is used by vss when currently no audio file is open.
443 static char *empty_status_items(int parser_friendly
)
447 #define ITEM(x) "0004 %02x:\n"
450 #define ITEM(x) , SI_ ## x
455 #define ITEM(x) "%s:\n"
458 #define ITEM(x) ,status_item_list[SI_ ## x]
463 #undef EMPTY_STATUS_ITEMS
466 int com_stat(struct command_context
*cc
)
469 struct misc_meta_data tmp
, *nmmd
= &tmp
;
472 int parser_friendly
= 0;
474 para_sigaction(SIGUSR1
, dummy
);
476 for (i
= 1; i
< cc
->argc
; i
++) {
477 const char *arg
= cc
->argv
[i
];
480 if (!strcmp(arg
, "--")) {
484 if (!strncmp(arg
, "-n=", 3)) {
485 ret
= para_atoi32(arg
+ 3, &num
);
490 if (!strcmp(arg
, "-p")) {
494 return -E_COMMAND_SYNTAX
;
497 return -E_COMMAND_SYNTAX
;
500 ret
= get_status(nmmd
, parser_friendly
, &s
);
501 ret
= sc_send_bin_buffer(&cc
->scc
, s
, ret
);
505 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
508 esi
= empty_status_items(parser_friendly
);
509 ret
= sc_send_buffer(&cc
->scc
, esi
);
513 send_afs_status(cc
, parser_friendly
);
515 if (num
> 0 && !--num
)
519 return -E_SERVER_CRASH
;
525 static int send_list_of_commands(struct stream_cipher_context
*scc
, struct server_command
*cmd
,
530 for (i
= 1; cmd
->name
; cmd
++, i
++) {
531 char *perms
= cmd_perms_itohuman(cmd
->perms
);
532 ret
= sc_send_va_buffer(scc
, "%s\t%s\t%s\t%s\n", cmd
->name
,
543 /* returns string that must be freed by the caller */
544 static struct server_command
*get_cmd_ptr(const char *name
, char **handler
)
546 struct server_command
*cmd
;
548 for (cmd
= server_cmds
; cmd
->name
; cmd
++)
549 if (!strcmp(cmd
->name
, name
)) {
551 *handler
= para_strdup("server"); /* server commands */
554 /* not found, look for commands supported by afs */
555 for (cmd
= afs_cmds
; cmd
->name
; cmd
++)
556 if (!strcmp(cmd
->name
, name
)) {
558 *handler
= para_strdup("afs");
565 int com_help(struct command_context
*cc
)
567 struct server_command
*cmd
;
568 char *perms
, *handler
;
572 /* no argument given, print list of commands */
573 if ((ret
= send_list_of_commands(&cc
->scc
, server_cmds
, "server")) < 0)
575 return send_list_of_commands(&cc
->scc
, afs_cmds
, "afs");
577 /* argument given for help */
578 cmd
= get_cmd_ptr(cc
->argv
[1], &handler
);
581 perms
= cmd_perms_itohuman(cmd
->perms
);
582 ret
= sc_send_va_buffer(&cc
->scc
,
601 int com_hup(struct command_context
*cc
)
604 return -E_COMMAND_SYNTAX
;
605 kill(getppid(), SIGHUP
);
610 int com_term(struct command_context
*cc
)
613 return -E_COMMAND_SYNTAX
;
614 kill(getppid(), SIGTERM
);
618 int com_play(struct command_context
*cc
)
621 return -E_COMMAND_SYNTAX
;
622 mutex_lock(mmd_mutex
);
623 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
624 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
625 mutex_unlock(mmd_mutex
);
630 int com_stop(struct command_context
*cc
)
633 return -E_COMMAND_SYNTAX
;
634 mutex_lock(mmd_mutex
);
635 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
636 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
637 mmd
->new_vss_status_flags
|= VSS_NEXT
;
638 mutex_unlock(mmd_mutex
);
643 int com_pause(struct command_context
*cc
)
646 return -E_COMMAND_SYNTAX
;
647 mutex_lock(mmd_mutex
);
648 if (!vss_paused() && !vss_stopped()) {
650 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
651 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
653 mutex_unlock(mmd_mutex
);
658 int com_next(struct command_context
*cc
)
661 return -E_COMMAND_SYNTAX
;
662 mutex_lock(mmd_mutex
);
664 mmd
->new_vss_status_flags
|= VSS_NEXT
;
665 mutex_unlock(mmd_mutex
);
670 int com_nomore(struct command_context
*cc
)
673 return -E_COMMAND_SYNTAX
;
674 mutex_lock(mmd_mutex
);
675 if (vss_playing() || vss_paused())
676 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
677 mutex_unlock(mmd_mutex
);
682 int com_ff(struct command_context
*cc
)
685 int ret
, backwards
= 0;
690 return -E_COMMAND_SYNTAX
;
691 if (!(ret
= sscanf(cc
->argv
[1], "%u%c", &i
, &c
)))
692 return -E_COMMAND_SYNTAX
;
693 if (ret
> 1 && c
== '-')
694 backwards
= 1; /* jmp backwards */
695 mutex_lock(mmd_mutex
);
696 ret
= -E_NO_AUDIO_FILE
;
697 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
699 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
701 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
703 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
706 if (promille
> 1000) {
707 mmd
->new_vss_status_flags
|= VSS_NEXT
;
710 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
711 mmd
->new_vss_status_flags
|= VSS_REPOS
;
712 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
716 mutex_unlock(mmd_mutex
);
721 int com_jmp(struct command_context
*cc
)
727 return -E_COMMAND_SYNTAX
;
728 if (sscanf(cc
->argv
[1], "%lu", &i
) <= 0)
729 return -E_COMMAND_SYNTAX
;
730 mutex_lock(mmd_mutex
);
731 ret
= -E_NO_AUDIO_FILE
;
732 if (!mmd
->afd
.afhi
.chunks_total
)
736 PARA_INFO_LOG("jumping to %lu%%\n", i
);
737 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50)/ 100;
738 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
739 mmd
->chunks_sent
, mmd
->offset
);
740 mmd
->new_vss_status_flags
|= VSS_REPOS
;
741 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
745 mutex_unlock(mmd_mutex
);
750 * check if perms are sufficient to exec a command having perms cmd_perms.
751 * Returns 0 if perms are sufficient, -E_PERM otherwise.
753 static int check_perms(unsigned int perms
, struct server_command
*cmd_ptr
)
755 PARA_DEBUG_LOG("checking permissions\n");
756 return (cmd_ptr
->perms
& perms
) < cmd_ptr
->perms
? -E_PERM
: 0;
760 * Parse first string from *cmd and lookup in table of valid commands.
761 * On error, NULL is returned.
763 static struct server_command
*parse_cmd(const char *cmdstr
)
768 sscanf(cmdstr
, "%200s%n", buf
, &n
);
772 return get_cmd_ptr(buf
, NULL
);
775 static int read_command(struct stream_cipher_context
*scc
, char **result
)
779 char *command
= NULL
;
785 ret
= sc_recv_buffer(scc
, buf
, sizeof(buf
));
791 ret
= -E_COMMAND_SYNTAX
;
792 if (command
&& numbytes
+ strlen(command
) > MAX_COMMAND_LEN
) /* DOS */
794 command
= para_strcat(command
, buf
);
795 p
= strstr(command
, EOC_MSG
);
801 ret
= command
? 1 : -E_COMMAND_SYNTAX
;
811 static void reset_signals(void)
813 para_sigaction(SIGCHLD
, SIG_IGN
);
814 para_sigaction(SIGINT
, SIG_DFL
);
815 para_sigaction(SIGTERM
, SIG_DFL
);
816 para_sigaction(SIGHUP
, SIG_DFL
);
819 static int parse_auth_request(char *buf
, int len
, struct user
**u
,
823 char *p
, *username
, **features
= NULL
;
824 size_t auth_rq_len
= strlen(AUTH_REQUEST_MSG
);
827 *use_sideband
= false;
828 if (len
< auth_rq_len
+ 2)
829 return -E_AUTH_REQUEST
;
830 if (strncmp(buf
, AUTH_REQUEST_MSG
, auth_rq_len
) != 0)
831 return -E_AUTH_REQUEST
;
832 username
= buf
+ auth_rq_len
;
833 p
= strchr(username
, ' ');
837 return -E_AUTH_REQUEST
;
840 create_argv(p
, ",", &features
);
841 for (i
= 0; features
[i
]; i
++) {
842 if (strcmp(features
[i
], "sideband") == 0)
843 *use_sideband
= true;
845 ret
= -E_BAD_FEATURE
;
850 PARA_DEBUG_LOG("received auth request for user %s (sideband = %s)\n",
851 username
, *use_sideband
? "true" : "false");
852 *u
= lookup_user(username
);
859 #define HANDSHAKE_BUFSIZE 4096
861 static int parse_sb_command(struct command_context
*cc
, struct iovec
*iov
)
867 if (iov
->iov_base
== NULL
|| iov
->iov_len
== 0)
870 p
[iov
->iov_len
- 1] = '\0'; /* just to be sure */
871 cc
->cmd
= get_cmd_ptr(p
, NULL
);
874 ret
= check_perms(cc
->u
->perms
, cc
->cmd
);
877 end
= iov
->iov_base
+ iov
->iov_len
;
878 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++)
881 cc
->argv
= para_malloc((cc
->argc
+ 1) * sizeof(char *));
882 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++) {
883 cc
->argv
[i
] = para_strdup(p
);
886 cc
->argv
[cc
->argc
] = NULL
;
894 * Perform user authentication and execute a command.
896 * \param fd The file descriptor to send output to.
897 * \param peername Identifies the connecting peer.
899 * Whenever para_server accepts an incoming tcp connection on
900 * the port it listens on, it forks and the resulting child
901 * calls this function.
903 * An RSA-based challenge/response is used to authenticate
904 * the peer. It that authentication succeeds, a random
905 * session key is generated and sent back to the peer,
906 * encrypted with its RSA public key. From this point on,
907 * all transfers are crypted with this session key.
909 * Next it is checked if the peer supplied a valid server command or a command
910 * for the audio file selector. If yes, and if the user has sufficient
911 * permissions to execute that command, the function calls the corresponding
912 * command handler which does argument checking and further processing.
914 * In order to cope with a DOS attacks, a timeout is set up
915 * which terminates the function if the connection was not
916 * authenticated when the timeout expires.
918 * \sa alarm(2), crypt.c, crypt.h
920 __noreturn
void handle_connect(int fd
, const char *peername
)
923 unsigned char rand_buf
[CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
924 unsigned char challenge_hash
[HASH_SIZE
];
925 char *p
, *command
= NULL
, *buf
= para_malloc(HANDSHAKE_BUFSIZE
) /* must be on the heap */;
927 struct command_context cc_struct
= {.peer
= peername
}, *cc
= &cc_struct
;
931 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
932 ret
= mark_fd_blocking(fd
);
935 /* send Welcome message */
936 ret
= write_va_buffer(fd
, "This is para_server, version "
937 PACKAGE_VERSION
".\n"
938 "Features: sideband,foo\n"
942 /* recv auth request line */
943 ret
= recv_buffer(fd
, buf
, HANDSHAKE_BUFSIZE
);
946 ret
= parse_auth_request(buf
, ret
, &cc
->u
, &cc
->use_sideband
);
949 p
= buf
+ strlen(AUTH_REQUEST_MSG
);
950 PARA_DEBUG_LOG("received auth request for user %s\n", p
);
951 cc
->u
= lookup_user(p
);
953 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
954 ret
= pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
955 (unsigned char *)buf
);
961 * We don't want to reveal our user names, so we send a
962 * challenge to the client even if the user does not exist, and
963 * fail the authentication later.
966 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
968 PARA_DEBUG_LOG("sending %u byte challenge + rc4 keys (%zu bytes)\n",
969 CHALLENGE_SIZE
, numbytes
);
970 if (cc
->use_sideband
) {
972 ret
= send_sb(&cc
->scc
, buf
, numbytes
, SBD_CHALLENGE
, false);
976 ret
= recv_sb(&cc
->scc
, SBD_CHALLENGE_RESPONSE
,
977 HANDSHAKE_BUFSIZE
, &iov
);
981 numbytes
= iov
.iov_len
;
983 ret
= write_all(fd
, buf
, numbytes
);
986 /* recv challenge response */
987 ret
= recv_bin_buffer(fd
, buf
, HASH_SIZE
);
992 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes
);
997 * The correct response is the hash of the first CHALLENGE_SIZE bytes
998 * of the random data.
1001 if (numbytes
!= HASH_SIZE
)
1003 hash_function((char *)rand_buf
, CHALLENGE_SIZE
, challenge_hash
);
1004 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
1006 /* auth successful */
1008 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
1009 /* init stream cipher keys with the second part of the random buffer */
1010 cc
->scc
.recv
= sc_new(rand_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
);
1011 cc
->scc
.send
= sc_new(rand_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
, SESSION_KEY_LEN
);
1012 if (cc
->use_sideband
)
1013 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_PROCEED
, false);
1015 ret
= sc_send_buffer(&cc
->scc
, PROCEED_MSG
);
1018 if (cc
->use_sideband
) {
1020 ret
= recv_sb(&cc
->scc
, SBD_COMMAND
, MAX_COMMAND_LEN
, &iov
);
1023 ret
= parse_sb_command(cc
, &iov
);
1028 ret
= read_command(&cc
->scc
, &command
);
1029 if (ret
== -E_COMMAND_SYNTAX
)
1034 cc
->cmd
= parse_cmd(command
);
1037 /* valid command, check permissions */
1038 ret
= check_perms(cc
->u
->perms
, cc
->cmd
);
1041 /* valid command and sufficient perms */
1042 ret
= create_argv(command
, "\n", &cc
->argv
);
1047 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cc
->cmd
->name
,
1048 cc
->u
->name
, peername
);
1049 ret
= cc
->cmd
->handler(cc
);
1050 free_argv(cc
->argv
);
1051 mutex_lock(mmd_mutex
);
1052 mmd
->num_commands
++;
1053 mutex_unlock(mmd_mutex
);
1057 sc_send_va_buffer(&cc
->scc
, "%s\n", para_strerror(-ret
));
1059 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1063 sc_free(cc
->scc
.recv
);
1064 sc_free(cc
->scc
.send
);
1065 mutex_lock(mmd_mutex
);
1066 if (cc
->cmd
&& (cc
->cmd
->perms
& AFS_WRITE
) && ret
>= 0)
1068 mmd
->active_connections
--;
1069 mutex_unlock(mmd_mutex
);
1070 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);