2 * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file client_common.c Common functions of para_client and para_audiod. */
10 #include <sys/types.h>
16 #include "client.cmdline.h"
21 #include "client.cmdline.h"
23 #include "buffer_tree.h"
26 /** The size of the receiving buffer. */
27 #define CLIENT_BUFSIZE 4000
30 * Close the connection to para_server and deallocate per-command ressources.
32 * \param ct The client task.
34 * This frees all ressources of the current command but keeps the configuration
37 * \sa \ref client_close().
39 void client_disconnect(struct client_task
*ct
)
45 free_argv(ct
->features
);
46 sc_free(ct
->scc
.recv
);
48 sc_free(ct
->scc
.send
);
50 btr_free_node(ct
->btrn
);
55 * Close the connection to para_server and free all resources.
57 * \param ct Pointer to the client data.
59 * \sa \ref client_open(), \ref client_disconnect().
61 void client_close(struct client_task
*ct
)
65 client_disconnect(ct
);
67 free(ct
->config_file
);
69 client_cmdline_parser_free(&ct
->conf
);
74 * The preselect hook for server commands.
76 * \param s Pointer to the scheduler.
77 * \param t Pointer to the task struct for this command.
79 * The task pointer must contain a pointer to the initialized client data
80 * structure as it is returned by client_open().
82 * This function checks the state of the connection and adds the file descriptor
83 * of the connection to the read or write fd set of \a s accordingly.
85 * \sa register_task() client_open(), struct sched, struct task.
87 static void client_pre_select(struct sched
*s
, struct task
*t
)
90 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
91 struct btr_node
*btrn
= ct
->btrn
;
98 case CL_SENT_CH_RESPONSE
:
100 para_fd_set(ct
->scc
.fd
, &s
->rfds
, &s
->max_fileno
);
103 case CL_RECEIVED_WELCOME
:
104 case CL_RECEIVED_PROCEED
:
105 para_fd_set(ct
->scc
.fd
, &s
->wfds
, &s
->max_fileno
);
109 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
114 para_fd_set(ct
->scc
.fd
, &s
->rfds
,
119 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
124 para_fd_set(ct
->scc
.fd
, &s
->wfds
,
131 static int client_recv_buffer(struct client_task
*ct
, fd_set
*rfds
,
132 char *buf
, size_t sz
, size_t *n
)
136 if (ct
->status
< CL_SENT_CH_RESPONSE
)
137 return read_nonblock(ct
->scc
.fd
, buf
, sz
, rfds
, n
);
140 ret
= sc_recv_buffer(&ct
->scc
, buf
, sz
);
142 * sc_recv_buffer is used with blocking fds elsewhere, so it
143 * does not use the nonblock-API. Therefore we need to
144 * check for EOF and EAGAIN.
147 return -E_SERVER_EOF
;
148 if (ret
== -ERRNO_TO_PARA_ERROR(EAGAIN
))
156 static char **parse_features(char *buf
)
159 const char id
[] = "\nFeatures: ";
160 char *p
, *q
, **features
;
170 create_argv(p
, ",", &features
);
171 for (i
= 0; features
[i
]; i
++)
172 PARA_INFO_LOG("server feature: %s\n", features
[i
]);
176 static bool has_feature(const char *feature
, struct client_task
*ct
)
178 return find_arg(feature
, ct
->features
) >= 0? true : false;
182 * The post select hook for client commands.
184 * \param s Pointer to the scheduler.
185 * \param t Pointer to the task struct for this command.
187 * Depending on the current state of the connection and the status of the read
188 * and write fd sets of \a s, this function performs the necessary steps to
189 * authenticate the connection, to send the command given by \a t->private_data
190 * and to receive para_server's output, if any.
192 * \sa struct sched, struct task.
194 static void client_post_select(struct sched
*s
, struct task
*t
)
196 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
197 struct btr_node
*btrn
= ct
->btrn
;
200 char buf
[CLIENT_BUFSIZE
];
205 switch (ct
->status
) {
206 case CL_CONNECTED
: /* receive welcome message */
207 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
208 if (ret
< 0 || n
== 0)
210 ct
->features
= parse_features(buf
);
211 ct
->status
= CL_RECEIVED_WELCOME
;
213 case CL_RECEIVED_WELCOME
: /* send auth command */
214 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
216 if (has_feature("sideband", ct
)) {
217 ct
->use_sideband
= true;
218 sprintf(buf
, AUTH_REQUEST_MSG
"%s sideband", ct
->user
);
220 sprintf(buf
, AUTH_REQUEST_MSG
"%s", ct
->user
);
221 PARA_INFO_LOG("--> %s\n", buf
);
222 ret
= write_buffer(ct
->scc
.fd
, buf
);
225 ct
->status
= CL_SENT_AUTH
;
229 * Receive challenge and session keys, decrypt the challenge and
230 * send back the hash of the decrypted challenge.
233 /* decrypted challenge/session key buffer */
234 unsigned char crypt_buf
[1024];
235 /* the SHA1 of the decrypted challenge */
236 unsigned char challenge_hash
[HASH_SIZE
];
238 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
239 if (ret
< 0 || n
== 0)
241 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n
);
242 ret
= priv_decrypt(ct
->key_file
, crypt_buf
,
243 (unsigned char *)buf
, n
);
246 hash_function((char *)crypt_buf
, CHALLENGE_SIZE
, challenge_hash
);
247 ct
->scc
.send
= sc_new(crypt_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
);
248 ct
->scc
.recv
= sc_new(crypt_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
250 hash_to_asc(challenge_hash
, buf
);
251 PARA_INFO_LOG("--> %s\n", buf
);
252 ret
= write_all(ct
->scc
.fd
, (char *)challenge_hash
, HASH_SIZE
);
255 ct
->status
= CL_SENT_CH_RESPONSE
;
258 case CL_SENT_CH_RESPONSE
: /* read server response */
260 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
261 if (ret
< 0 || n
== 0)
263 /* check if server has sent "Proceed" message */
264 ret
= -E_CLIENT_AUTH
;
265 if (n
< PROCEED_MSG_LEN
)
267 if (!strstr(buf
, PROCEED_MSG
))
269 ct
->status
= CL_RECEIVED_PROCEED
;
272 case CL_RECEIVED_PROCEED
: /* concat args and send command */
275 char *command
= NULL
;
276 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
278 for (i
= 0; i
< ct
->conf
.inputs_num
; i
++) {
280 command
= make_message("%s\n%s", command
?
281 command
: "", ct
->conf
.inputs
[i
]);
284 command
= para_strcat(command
, EOC_MSG
"\n");
285 PARA_DEBUG_LOG("--> %s\n", command
);
286 ret
= sc_send_buffer(&ct
->scc
, command
);
290 ct
->status
= CL_SENT_COMMAND
;
293 case CL_SENT_COMMAND
:
296 /* can not use "buf" here because we need a malloced buffer */
297 buf2
= para_malloc(CLIENT_BUFSIZE
);
298 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
300 if (strstr(buf2
, AWAITING_DATA_MSG
)) {
302 ct
->status
= CL_SENDING
;
305 ct
->status
= CL_RECEIVING
;
306 btr_add_output(buf2
, n
, btrn
);
315 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
320 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
322 sz
= btr_next_buffer(btrn
, &buf2
);
323 ret
= sc_send_bin_buffer(&ct
->scc
, buf2
, sz
);
326 btr_consume(btrn
, sz
);
332 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
338 * The FD_ISSET() is not strictly necessary, but is allows us
339 * to skip the malloc below if there is nothing to read anyway.
341 if (!FD_ISSET(ct
->scc
.fd
, &s
->rfds
))
343 buf2
= para_malloc(CLIENT_BUFSIZE
);
344 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
346 buf2
= para_realloc(buf2
, n
);
347 btr_add_output(buf2
, n
, btrn
);
356 if (ret
!= -E_SERVER_EOF
&& ret
!= -E_BTR_EOF
)
357 PARA_ERROR_LOG("%s\n", para_strerror(-t
->error
));
358 btr_remove_node(btrn
);
363 * Connect to para_server and register the client task.
365 * \param ct The initialized client task structure.
366 * \param s The scheduler instance to register the client task to.
367 * \param parent The parent node of the client btr node.
368 * \param child The child node of the client node.
370 * The client task structure given by \a ct must be allocated and initialized
371 * by \ref client_parse_config() before this function is called.
375 int client_connect(struct client_task
*ct
, struct sched
*s
,
376 struct btr_node
*parent
, struct btr_node
*child
)
380 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
381 ct
->conf
.server_port_arg
);
383 ret
= para_connect_simple(IPPROTO_TCP
, ct
->conf
.hostname_arg
,
384 ct
->conf
.server_port_arg
);
388 ret
= mark_fd_nonblocking(ct
->scc
.fd
);
391 ct
->status
= CL_CONNECTED
;
392 ct
->btrn
= btr_new_node(&(struct btr_node_description
)
393 EMBRACE(.name
= "client", .parent
= parent
, .child
= child
));
394 ct
->task
.pre_select
= client_pre_select
;
395 ct
->task
.post_select
= client_post_select
;
397 sprintf(ct
->task
.status
, "client");
398 register_task(s
, &ct
->task
);
407 * Parse a client configuration.
409 * \param argc Usual argument count.
410 * \param argv Usual argument vector.
411 * \param ct_ptr Filled in by this function.
412 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
414 * This checks the command line options given by \a argc and \a argv, sets
415 * default values for the user name and the name of the rsa key file and reads
416 * further options from the config file.
418 * Upon successful return, \a ct_ptr points to a dynamically allocated and
419 * initialized client task struct.
421 * \return The number of non-option arguments in \a argc/argv on success,
422 * negative on errors.
424 int client_parse_config(int argc
, char *argv
[], struct client_task
**ct_ptr
,
427 char *home
= para_homedir();
429 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
433 ret
= -E_CLIENT_SYNTAX
;
434 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
436 HANDLE_VERSION_FLAG("client", ct
->conf
);
438 ct
->config_file
= ct
->conf
.config_file_given
?
439 para_strdup(ct
->conf
.config_file_arg
) :
440 make_message("%s/.paraslash/client.conf", home
);
441 ret
= file_exists(ct
->config_file
);
442 if (!ret
&& ct
->conf
.config_file_given
) {
447 struct client_cmdline_parser_params params
= {
451 .check_ambiguity
= 0,
455 if (client_cmdline_parser_config_file(ct
->config_file
,
459 ct
->user
= ct
->conf
.user_given
?
460 para_strdup(ct
->conf
.user_arg
) : para_logname();
462 if (ct
->conf
.key_file_given
)
463 ct
->key_file
= para_strdup(ct
->conf
.key_file_arg
);
465 ct
->key_file
= make_message("%s/.paraslash/key.%s",
467 if (!file_exists(ct
->key_file
)) {
469 ct
->key_file
= make_message("%s/.ssh/id_rsa", home
);
474 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
475 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
476 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
477 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
478 ret
= ct
->conf
.inputs_num
;
482 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
490 * Parse the client configuration and open a connection to para_server.
492 * \param argc See \ref client_parse_config.
493 * \param argv See \ref client_parse_config.
494 * \param ct_ptr See \ref client_parse_config.
495 * \param loglevel See \ref client_parse_config.
496 * \param parent See \ref client_connect().
497 * \param child See \ref client_connect().
498 * \param sched See \ref client_connect().
500 * This function combines client_parse_config() and client_connect(). It is
501 * considered a syntax error if no command was given, i.e. if the number
502 * of non-option arguments is zero.
506 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
507 int *loglevel
, struct btr_node
*parent
, struct btr_node
*child
,
510 int ret
= client_parse_config(argc
, argv
, ct_ptr
, loglevel
);
515 ret
= -E_CLIENT_SYNTAX
;
518 ret
= client_connect(*ct_ptr
, sched
, parent
, child
);
523 client_close(*ct_ptr
);