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 /* Defines one command of para_server. */
44 struct server_command
{
45 /* The name of the command. */
47 /* Pointer to the function that handles the command. */
48 int (*handler
)(struct command_context
*);
49 /* The privileges a user must have to execute this command. */
51 /* One-line description of the command. */
52 const char *description
;
53 /* Summary of the command line options. */
55 /* The long help text. */
59 static struct server_command afs_cmds
[] = {DEFINE_AFS_CMD_ARRAY
};
60 static struct server_command server_cmds
[] = {DEFINE_SERVER_CMD_ARRAY
};
62 /** Commands including options must be shorter than this. */
63 #define MAX_COMMAND_LEN 32768
66 extern struct misc_meta_data
*mmd
;
67 extern struct sender senders
[];
68 int send_afs_status(struct command_context
*cc
, int parser_friendly
);
70 static void dummy(__a_unused
int s
)
74 static void mmd_dup(struct misc_meta_data
*new_mmd
)
76 mutex_lock(mmd_mutex
);
78 mutex_unlock(mmd_mutex
);
82 * Compute human readable string containing vss status for given integer value.
84 * We don't want to use vss_playing() and friends here because we take a
85 * snapshot of the mmd struct and use the copy for computing the state of the
86 * vss. If the real data were used, we would take the mmd lock for a rather
87 * long time or risk to get an inconsistent view.
89 static char *vss_status_tohuman(unsigned int flags
)
91 if (flags
& VSS_PLAYING
)
92 return para_strdup("playing");
94 return para_strdup("stopped");
95 return para_strdup("paused");
99 * return human readable permission string. Never returns NULL.
101 static char *cmd_perms_itohuman(unsigned int perms
)
103 char *msg
= para_malloc(5 * sizeof(char));
105 msg
[0] = perms
& AFS_READ
? 'a' : '-';
106 msg
[1] = perms
& AFS_WRITE
? 'A' : '-';
107 msg
[2] = perms
& VSS_READ
? 'v' : '-';
108 msg
[3] = perms
& VSS_WRITE
? 'V' : '-';
114 * Never returns NULL.
116 static char *vss_get_status_flags(unsigned int flags
)
118 char *msg
= para_malloc(5 * sizeof(char));
120 msg
[0] = (flags
& VSS_PLAYING
)? 'P' : '_';
121 msg
[1] = (flags
& VSS_NOMORE
)? 'O' : '_';
122 msg
[2] = (flags
& VSS_NEXT
)? 'N' : '_';
123 msg
[3] = (flags
& VSS_REPOS
)? 'R' : '_';
128 static unsigned get_status(struct misc_meta_data
*nmmd
, int parser_friendly
,
131 char *status
, *flags
; /* vss status info */
132 /* nobody updates our version of "now" */
133 long offset
= (nmmd
->offset
+ 500) / 1000;
134 struct timeval current_time
;
135 struct para_buffer b
= {.flags
= parser_friendly
? PBF_SIZE_PREFIX
: 0};
137 /* report real status */
138 status
= vss_status_tohuman(nmmd
->vss_status_flags
);
139 flags
= vss_get_status_flags(nmmd
->vss_status_flags
);
140 clock_get_realtime(¤t_time
);
142 * The calls to WRITE_STATUS_ITEM() below never fail because
143 * b->max_size is zero (unlimited), see para_printf(). However, clang
144 * is not smart enough to prove this and complains nevertheless.
145 * Casting the return value to void silences clang.
147 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS
, "%s\n", status
);
148 (void)WRITE_STATUS_ITEM(&b
, SI_STATUS_FLAGS
, "%s\n", flags
);
149 (void)WRITE_STATUS_ITEM(&b
, SI_OFFSET
, "%li\n", offset
);
150 (void)WRITE_STATUS_ITEM(&b
, SI_AFS_MODE
, "%s\n", mmd
->afs_mode_string
);
151 (void)WRITE_STATUS_ITEM(&b
, SI_STREAM_START
, "%lu.%lu\n",
152 (long unsigned)nmmd
->stream_start
.tv_sec
,
153 (long unsigned)nmmd
->stream_start
.tv_usec
);
154 (void)WRITE_STATUS_ITEM(&b
, SI_CURRENT_TIME
, "%lu.%lu\n",
155 (long unsigned)current_time
.tv_sec
,
156 (long unsigned)current_time
.tv_usec
);
163 static int check_sender_args(int argc
, char * const * argv
, struct sender_command_data
*scd
)
166 /* this has to match sender.h */
167 const char *subcmds
[] = {"add", "delete", "allow", "deny", "on", "off", NULL
};
169 scd
->sender_num
= -1;
171 return -E_COMMAND_SYNTAX
;
172 for (i
= 0; senders
[i
].name
; i
++)
173 if (!strcmp(senders
[i
].name
, argv
[1]))
175 PARA_DEBUG_LOG("%d:%s\n", argc
, argv
[1]);
176 if (!senders
[i
].name
)
177 return -E_COMMAND_SYNTAX
;
179 for (i
= 0; subcmds
[i
]; i
++)
180 if (!strcmp(subcmds
[i
], argv
[2]))
183 return -E_COMMAND_SYNTAX
;
185 if (!senders
[scd
->sender_num
].client_cmds
[scd
->cmd_num
])
186 return -E_SENDER_CMD
;
187 switch (scd
->cmd_num
) {
191 return -E_COMMAND_SYNTAX
;
195 if (argc
!= 4 || parse_cidr(argv
[3], scd
->host
,
196 sizeof(scd
->host
), &scd
->netmask
) == NULL
)
197 return -E_COMMAND_SYNTAX
;
202 return -E_COMMAND_SYNTAX
;
203 return parse_fec_url(argv
[3], scd
);
205 return -E_COMMAND_SYNTAX
;
211 * Send a sideband packet through a blocking file descriptor.
213 * \param scc fd and crypto keys.
214 * \param buf The buffer to send.
215 * \param numbytes The size of \a buf.
216 * \param band The sideband designator of this packet.
217 * \param dont_free If true, never deallocate \a buf.
219 * The nonblock flag must be disabled for the file descriptor given by \a scc.
221 * Stream cipher encryption is automatically activated if necessary via the
222 * sideband transformation, depending on the value of \a band.
226 * \sa \ref send_sb_va().
228 int send_sb(struct stream_cipher_context
*scc
, void *buf
, size_t numbytes
,
229 int band
, bool dont_free
)
232 struct sb_context
*sbc
;
234 sb_transformation trafo
= band
< SBD_PROCEED
? NULL
: sc_trafo
;
235 struct sb_buffer sbb
= SBB_INIT(band
, buf
, numbytes
);
237 sbc
= sb_new_send(&sbb
, dont_free
, trafo
, scc
->send
);
239 ret
= sb_get_send_buffers(sbc
, iov
);
240 ret
= xwritev(scc
->fd
, iov
, ret
);
243 } while (sb_sent(sbc
, ret
) == false);
251 * Create a variable sized buffer and send it as a sideband packet.
253 * \param scc Passed to \ref send_sb.
254 * \param band See \ref send_sb.
255 * \param fmt The format string.
257 * \return The return value of the underlying call to \ref send_sb.
259 __printf_3_4
int send_sb_va(struct stream_cipher_context
*scc
, int band
,
260 const char *fmt
, ...)
267 ret
= xvasprintf(&msg
, fmt
, ap
);
269 return send_sb(scc
, msg
, ret
, band
, false);
273 * Send an error message to a client.
275 * \param cc Client info.
276 * \param err The (positive) error code.
278 * \return The return value of the underlying call to send_sb_va().
280 int send_strerror(struct command_context
*cc
, int err
)
282 return send_sb_va(&cc
->scc
, SBD_ERROR_LOG
, "%s\n", para_strerror(err
));
286 * Send a sideband packet through a blocking file descriptor.
288 * \param scc fd and crypto keys.
289 * \param expected_band The expected band designator.
290 * \param max_size Passed to \ref sb_new_recv().
291 * \param result Body of the sideband packet is returned here.
293 * If \a expected_band is not \p SBD_ANY, the band designator of the received
294 * sideband packet is compared to \a expected_band and a mismatch is considered
299 int recv_sb(struct stream_cipher_context
*scc
,
300 enum sb_designator expected_band
,
301 size_t max_size
, struct iovec
*result
)
304 struct sb_context
*sbc
;
306 struct sb_buffer sbb
;
307 sb_transformation trafo
;
309 trafo
= expected_band
!= SBD_ANY
&& expected_band
< SBD_PROCEED
?
311 sbc
= sb_new_recv(max_size
, trafo
, scc
->recv
);
313 sb_get_recv_buffer(sbc
, &iov
);
314 ret
= recv_bin_buffer(scc
->fd
, iov
.iov_base
, iov
.iov_len
);
319 ret
= sb_received(sbc
, ret
, &sbb
);
326 if (expected_band
!= SBD_ANY
&& sbb
.band
!= expected_band
)
335 static int com_sender(struct command_context
*cc
)
339 struct sender_command_data scd
;
342 for (i
= 0; senders
[i
].name
; i
++) {
344 ret
= xasprintf(&tmp
, "%s%s\n", msg
? msg
: "",
349 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
351 ret
= check_sender_args(cc
->argc
, cc
->argv
, &scd
);
353 if (scd
.sender_num
< 0)
355 if (strcmp(cc
->argv
[2], "status") == 0)
356 msg
= senders
[scd
.sender_num
].status();
358 msg
= senders
[scd
.sender_num
].help();
359 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
362 switch (scd
.cmd_num
) {
365 assert(senders
[scd
.sender_num
].resolve_target
);
366 ret
= senders
[scd
.sender_num
].resolve_target(cc
->argv
[3], &scd
);
371 for (i
= 0; i
< 10; i
++) {
372 mutex_lock(mmd_mutex
);
373 if (mmd
->sender_cmd_data
.cmd_num
>= 0) {
374 /* another sender command is active, retry in 100ms */
375 struct timespec ts
= {.tv_nsec
= 100 * 1000 * 1000};
376 mutex_unlock(mmd_mutex
);
377 nanosleep(&ts
, NULL
);
380 mmd
->sender_cmd_data
= scd
;
381 mutex_unlock(mmd_mutex
);
384 return (i
< 10)? 1 : -E_LOCK
;
388 static int com_si(struct command_context
*cc
)
394 return -E_COMMAND_SYNTAX
;
395 mutex_lock(mmd_mutex
);
396 ut
= daemon_get_uptime_str(now
);
397 ret
= xasprintf(&msg
,
398 "up: %s\nplayed: %u\n"
401 "connections (active/accepted/total): %u/%u/%u\n"
402 "current loglevel: %s\n"
403 "supported audio formats: %s\n",
407 mmd
->active_connections
,
411 AUDIO_FORMAT_HANDLERS
413 mutex_unlock(mmd_mutex
);
415 return send_sb(&cc
->scc
, msg
, ret
, SBD_OUTPUT
, false);
419 static int com_version(struct command_context
*cc
)
425 return -E_COMMAND_SYNTAX
;
426 len
= xasprintf(&msg
, "%s", version_text("server"));
427 return send_sb(&cc
->scc
, msg
, len
, SBD_OUTPUT
, false);
430 /** These status items are cleared if no audio file is currently open. */
431 #define EMPTY_STATUS_ITEMS \
436 ITEM(ATTRIBUTES_BITMAP) \
437 ITEM(ATTRIBUTES_TXT) \
448 ITEM(SECONDS_TOTAL) \
461 ITEM(AMPLIFICATION) \
464 * Write a list of audio-file related status items with empty values.
466 * This is used by vss when currently no audio file is open.
468 static unsigned empty_status_items(int parser_friendly
, char **result
)
474 len
= xasprintf(&esi
,
475 #define ITEM(x) "0004 %02x:\n"
478 #define ITEM(x) , SI_ ## x
483 len
= xasprintf(&esi
,
484 #define ITEM(x) "%s:\n"
487 #define ITEM(x) ,status_item_list[SI_ ## x]
494 #undef EMPTY_STATUS_ITEMS
497 static int com_stat(struct command_context
*cc
)
500 struct misc_meta_data tmp
, *nmmd
= &tmp
;
503 int parser_friendly
= 0;
505 para_sigaction(SIGUSR1
, dummy
);
507 for (i
= 1; i
< cc
->argc
; i
++) {
508 const char *arg
= cc
->argv
[i
];
511 if (!strcmp(arg
, "--")) {
515 if (!strncmp(arg
, "-n=", 3)) {
516 ret
= para_atoi32(arg
+ 3, &num
);
521 if (!strcmp(arg
, "-p")) {
525 return -E_COMMAND_SYNTAX
;
528 return -E_COMMAND_SYNTAX
;
531 ret
= get_status(nmmd
, parser_friendly
, &s
);
532 ret
= send_sb(&cc
->scc
, s
, ret
, SBD_OUTPUT
, false);
535 if (nmmd
->vss_status_flags
& VSS_NEXT
) {
537 ret
= empty_status_items(parser_friendly
, &esi
);
538 ret
= send_sb(&cc
->scc
, esi
, ret
, SBD_OUTPUT
, false);
542 send_afs_status(cc
, parser_friendly
);
544 if (num
> 0 && !--num
)
547 ret
= -E_SERVER_CRASH
;
555 static int send_list_of_commands(struct command_context
*cc
, struct server_command
*cmd
,
560 for (; cmd
->name
; cmd
++) {
561 char *tmp
, *perms
= cmd_perms_itohuman(cmd
->perms
);
562 tmp
= make_message("%s\t%s\t%s\t%s\n", cmd
->name
, handler
,
563 perms
, cmd
->description
);
565 msg
= para_strcat(msg
, tmp
);
569 return send_sb(&cc
->scc
, msg
, strlen(msg
), SBD_OUTPUT
, false);
572 /* returns string that must be freed by the caller */
573 static struct server_command
*get_cmd_ptr(const char *name
, char **handler
)
575 struct server_command
*cmd
;
577 for (cmd
= server_cmds
; cmd
->name
; cmd
++)
578 if (!strcmp(cmd
->name
, name
)) {
580 *handler
= para_strdup("server"); /* server commands */
583 /* not found, look for commands supported by afs */
584 for (cmd
= afs_cmds
; cmd
->name
; cmd
++)
585 if (!strcmp(cmd
->name
, name
)) {
587 *handler
= para_strdup("afs");
594 static int com_help(struct command_context
*cc
)
596 struct server_command
*cmd
;
597 char *perms
, *handler
, *buf
;
601 /* no argument given, print list of commands */
602 if ((ret
= send_list_of_commands(cc
, server_cmds
, "server")) < 0)
604 return send_list_of_commands(cc
, afs_cmds
, "afs");
606 /* argument given for help */
607 cmd
= get_cmd_ptr(cc
->argv
[1], &handler
);
610 perms
= cmd_perms_itohuman(cmd
->perms
);
611 ret
= xasprintf(&buf
, "%s - %s\n\n"
625 return send_sb(&cc
->scc
, buf
, ret
, SBD_OUTPUT
, false);
629 static int com_hup(struct command_context
*cc
)
632 return -E_COMMAND_SYNTAX
;
633 kill(getppid(), SIGHUP
);
638 static int com_term(struct command_context
*cc
)
641 return -E_COMMAND_SYNTAX
;
642 kill(getppid(), SIGTERM
);
646 static int com_play(struct command_context
*cc
)
649 return -E_COMMAND_SYNTAX
;
650 mutex_lock(mmd_mutex
);
651 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
652 mmd
->new_vss_status_flags
&= ~VSS_NOMORE
;
653 mutex_unlock(mmd_mutex
);
658 static int com_stop(struct command_context
*cc
)
661 return -E_COMMAND_SYNTAX
;
662 mutex_lock(mmd_mutex
);
663 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
664 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
665 mmd
->new_vss_status_flags
|= VSS_NEXT
;
666 mutex_unlock(mmd_mutex
);
671 static int com_pause(struct command_context
*cc
)
674 return -E_COMMAND_SYNTAX
;
675 mutex_lock(mmd_mutex
);
676 if (!vss_paused() && !vss_stopped()) {
678 mmd
->new_vss_status_flags
&= ~VSS_PLAYING
;
679 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
681 mutex_unlock(mmd_mutex
);
686 static int com_next(struct command_context
*cc
)
689 return -E_COMMAND_SYNTAX
;
690 mutex_lock(mmd_mutex
);
692 mmd
->new_vss_status_flags
|= VSS_NEXT
;
693 mutex_unlock(mmd_mutex
);
698 static int com_nomore(struct command_context
*cc
)
701 return -E_COMMAND_SYNTAX
;
702 mutex_lock(mmd_mutex
);
703 if (vss_playing() || vss_paused())
704 mmd
->new_vss_status_flags
|= VSS_NOMORE
;
705 mutex_unlock(mmd_mutex
);
710 static int com_ff(struct command_context
*cc
)
713 int ret
, backwards
= 0;
718 return -E_COMMAND_SYNTAX
;
719 if (!(ret
= sscanf(cc
->argv
[1], "%u%c", &i
, &c
)))
720 return -E_COMMAND_SYNTAX
;
721 if (ret
> 1 && c
== '-')
722 backwards
= 1; /* jmp backwards */
723 mutex_lock(mmd_mutex
);
724 ret
= -E_NO_AUDIO_FILE
;
725 if (!mmd
->afd
.afhi
.chunks_total
|| !mmd
->afd
.afhi
.seconds_total
)
727 promille
= (1000 * mmd
->current_chunk
) / mmd
->afd
.afhi
.chunks_total
;
729 promille
-= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
731 promille
+= 1000 * i
/ mmd
->afd
.afhi
.seconds_total
;
734 if (promille
> 1000) {
735 mmd
->new_vss_status_flags
|= VSS_NEXT
;
738 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* promille
) / 1000;
739 mmd
->new_vss_status_flags
|= VSS_REPOS
;
740 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
744 mutex_unlock(mmd_mutex
);
749 static int com_jmp(struct command_context
*cc
)
755 return -E_COMMAND_SYNTAX
;
756 if (sscanf(cc
->argv
[1], "%lu", &i
) <= 0)
757 return -E_COMMAND_SYNTAX
;
758 mutex_lock(mmd_mutex
);
759 ret
= -E_NO_AUDIO_FILE
;
760 if (!mmd
->afd
.afhi
.chunks_total
)
764 PARA_INFO_LOG("jumping to %lu%%\n", i
);
765 mmd
->repos_request
= (mmd
->afd
.afhi
.chunks_total
* i
+ 50) / 100;
766 PARA_INFO_LOG("sent: %lu, offset before jmp: %lu\n",
767 mmd
->chunks_sent
, mmd
->offset
);
768 mmd
->new_vss_status_flags
|= VSS_REPOS
;
769 mmd
->new_vss_status_flags
&= ~VSS_NEXT
;
773 mutex_unlock(mmd_mutex
);
777 static int com_tasks(struct command_context
*cc
)
779 char *tl
= server_get_tasks();
783 ret
= send_sb(&cc
->scc
, tl
, strlen(tl
), SBD_OUTPUT
, false);
788 * check if perms are sufficient to exec a command having perms cmd_perms.
789 * Returns 0 if perms are sufficient, -E_PERM otherwise.
791 static int check_perms(unsigned int perms
, const struct server_command
*cmd_ptr
)
793 PARA_DEBUG_LOG("checking permissions\n");
794 return (cmd_ptr
->perms
& perms
) < cmd_ptr
->perms
? -E_PERM
: 0;
797 static void reset_signals(void)
799 para_sigaction(SIGCHLD
, SIG_IGN
);
800 para_sigaction(SIGINT
, SIG_DFL
);
801 para_sigaction(SIGTERM
, SIG_DFL
);
802 para_sigaction(SIGHUP
, SIG_DFL
);
805 struct connection_features
{
806 bool sideband_requested
;
807 bool aes_ctr128_requested
;
810 static int parse_auth_request(char *buf
, int len
, struct user
**u
,
811 struct connection_features
*cf
)
814 char *p
, *username
, **features
= NULL
;
815 size_t auth_rq_len
= strlen(AUTH_REQUEST_MSG
);
818 memset(cf
, 0, sizeof(*cf
));
819 if (len
< auth_rq_len
+ 2)
820 return -E_AUTH_REQUEST
;
821 if (strncmp(buf
, AUTH_REQUEST_MSG
, auth_rq_len
) != 0)
822 return -E_AUTH_REQUEST
;
823 username
= buf
+ auth_rq_len
;
824 p
= strchr(username
, ' ');
828 return -E_AUTH_REQUEST
;
831 create_argv(p
, ",", &features
);
832 for (i
= 0; features
[i
]; i
++) {
833 if (strcmp(features
[i
], "sideband") == 0)
834 cf
->sideband_requested
= true;
835 else if (strcmp(features
[i
], "aes_ctr128") == 0)
836 cf
->aes_ctr128_requested
= true;
838 ret
= -E_BAD_FEATURE
;
843 PARA_DEBUG_LOG("received auth request for user %s\n", username
);
844 *u
= lookup_user(username
);
851 #define HANDSHAKE_BUFSIZE 4096
853 static int parse_sb_command(struct command_context
*cc
, struct iovec
*iov
)
859 if (iov
->iov_base
== NULL
|| iov
->iov_len
== 0)
862 p
[iov
->iov_len
- 1] = '\0'; /* just to be sure */
863 cc
->cmd
= get_cmd_ptr(p
, NULL
);
866 ret
= check_perms(cc
->u
->perms
, cc
->cmd
);
869 end
= iov
->iov_base
+ iov
->iov_len
;
870 for (i
= 0; p
< end
; i
++)
873 cc
->argv
= para_malloc((cc
->argc
+ 1) * sizeof(char *));
874 for (i
= 0, p
= iov
->iov_base
; p
< end
; i
++) {
875 cc
->argv
[i
] = para_strdup(p
);
878 cc
->argv
[cc
->argc
] = NULL
;
886 * Perform user authentication and execute a command.
888 * \param fd The file descriptor to send output to.
889 * \param peername Identifies the connecting peer.
891 * Whenever para_server accepts an incoming tcp connection on the port it
892 * listens on, it forks and the resulting child calls this function.
894 * An RSA-based challenge/response is used to authenticate the peer. It that
895 * authentication succeeds, a random session key is generated and sent back to
896 * the peer, encrypted with its RSA public key. From this point on, all
897 * transfers are crypted with this session key.
899 * Next it is checked if the peer supplied a valid server command or a command
900 * for the audio file selector. If yes, and if the user has sufficient
901 * permissions to execute that command, the function calls the corresponding
902 * command handler which does argument checking and further processing.
904 * In order to cope with a DOS attacks, a timeout is set up which terminates
905 * the function if the connection was not authenticated when the timeout
908 * \sa alarm(2), crypt.c, crypt.h
910 __noreturn
void handle_connect(int fd
, const char *peername
)
913 unsigned char rand_buf
[CHALLENGE_SIZE
+ 2 * SESSION_KEY_LEN
];
914 unsigned char challenge_hash
[HASH_SIZE
];
915 char *command
= NULL
, *buf
= para_malloc(HANDSHAKE_BUFSIZE
) /* must be on the heap */;
917 struct command_context cc_struct
= {.peer
= peername
}, *cc
= &cc_struct
;
919 struct connection_features cf
;
923 /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
924 ret
= mark_fd_blocking(fd
);
927 /* send Welcome message */
928 ret
= write_va_buffer(fd
, "This is para_server, version "
929 PACKAGE_VERSION
".\n"
930 "Features: sideband,aes_ctr128\n"
934 /* recv auth request line */
935 ret
= recv_buffer(fd
, buf
, HANDSHAKE_BUFSIZE
);
938 ret
= parse_auth_request(buf
, ret
, &cc
->u
, &cf
);
941 if (!cf
.sideband_requested
) { /* sideband is mandatory */
942 PARA_ERROR_LOG("client did not request sideband\n");
943 ret
= -E_BAD_FEATURE
;
947 get_random_bytes_or_die(rand_buf
, sizeof(rand_buf
));
948 ret
= pub_encrypt(cc
->u
->pubkey
, rand_buf
, sizeof(rand_buf
),
949 (unsigned char *)buf
);
955 * We don't want to reveal our user names, so we send a
956 * challenge to the client even if the user does not exist, and
957 * fail the authentication later.
960 get_random_bytes_or_die((unsigned char *)buf
, numbytes
);
962 PARA_DEBUG_LOG("sending %u byte challenge + session key (%zu bytes)\n",
963 CHALLENGE_SIZE
, numbytes
);
964 ret
= send_sb(&cc
->scc
, buf
, numbytes
, SBD_CHALLENGE
, false);
968 ret
= recv_sb(&cc
->scc
, SBD_CHALLENGE_RESPONSE
,
969 HANDSHAKE_BUFSIZE
, &iov
);
973 numbytes
= iov
.iov_len
;
974 PARA_DEBUG_LOG("received %zu bytes challenge response\n", numbytes
);
979 * The correct response is the hash of the first CHALLENGE_SIZE bytes
980 * of the random data.
983 if (numbytes
!= HASH_SIZE
)
985 hash_function((char *)rand_buf
, CHALLENGE_SIZE
, challenge_hash
);
986 if (memcmp(challenge_hash
, buf
, HASH_SIZE
))
988 /* auth successful */
990 PARA_INFO_LOG("good auth for %s\n", cc
->u
->name
);
991 /* init stream cipher keys with the second part of the random buffer */
992 cc
->scc
.recv
= sc_new(rand_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
,
993 cf
.aes_ctr128_requested
);
994 cc
->scc
.send
= sc_new(rand_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
995 SESSION_KEY_LEN
, cf
.aes_ctr128_requested
);
996 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_PROCEED
, false);
999 ret
= recv_sb(&cc
->scc
, SBD_COMMAND
, MAX_COMMAND_LEN
, &iov
);
1002 ret
= parse_sb_command(cc
, &iov
);
1006 PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cc
->cmd
->name
,
1007 cc
->u
->name
, peername
);
1008 ret
= cc
->cmd
->handler(cc
);
1009 free_argv(cc
->argv
);
1010 mutex_lock(mmd_mutex
);
1011 mmd
->num_commands
++;
1012 mutex_unlock(mmd_mutex
);
1016 if (send_strerror(cc
, -ret
) >= 0)
1017 send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__FAILURE
, true);
1019 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1023 mutex_lock(mmd_mutex
);
1024 if (cc
->cmd
&& (cc
->cmd
->perms
& AFS_WRITE
) && ret
>= 0)
1026 mmd
->active_connections
--;
1027 mutex_unlock(mmd_mutex
);
1029 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_EXIT__SUCCESS
, true);
1031 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
1033 sc_free(cc
->scc
.recv
);
1034 sc_free(cc
->scc
.send
);
1035 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);