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 sc_free(ct
->scc
.recv
);
47 sc_free(ct
->scc
.send
);
49 btr_free_node(ct
->btrn
);
54 * Close the connection to para_server and free all resources.
56 * \param ct Pointer to the client data.
58 * \sa \ref client_open(), \ref client_disconnect().
60 void client_close(struct client_task
*ct
)
64 client_disconnect(ct
);
66 free(ct
->config_file
);
68 client_cmdline_parser_free(&ct
->conf
);
73 * The preselect hook for server commands.
75 * \param s Pointer to the scheduler.
76 * \param t Pointer to the task struct for this command.
78 * The task pointer must contain a pointer to the initialized client data
79 * structure as it is returned by client_open().
81 * This function checks the state of the connection and adds the file descriptor
82 * of the connection to the read or write fd set of \a s accordingly.
84 * \sa register_task() client_open(), struct sched, struct task.
86 static void client_pre_select(struct sched
*s
, struct task
*t
)
89 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
90 struct btr_node
*btrn
= ct
->btrn
;
97 case CL_SENT_CH_RESPONSE
:
99 para_fd_set(ct
->scc
.fd
, &s
->rfds
, &s
->max_fileno
);
102 case CL_RECEIVED_WELCOME
:
103 case CL_RECEIVED_PROCEED
:
104 para_fd_set(ct
->scc
.fd
, &s
->wfds
, &s
->max_fileno
);
108 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
113 para_fd_set(ct
->scc
.fd
, &s
->rfds
,
118 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
123 para_fd_set(ct
->scc
.fd
, &s
->wfds
,
130 static int client_recv_buffer(struct client_task
*ct
, fd_set
*rfds
,
131 char *buf
, size_t sz
, size_t *n
)
135 if (ct
->status
< CL_SENT_CH_RESPONSE
)
136 return read_nonblock(ct
->scc
.fd
, buf
, sz
, rfds
, n
);
139 ret
= sc_recv_buffer(&ct
->scc
, buf
, sz
);
141 * sc_recv_buffer is used with blocking fds elsewhere, so it
142 * does not use the nonblock-API. Therefore we need to
143 * check for EOF and EAGAIN.
146 return -E_SERVER_EOF
;
147 if (ret
== -ERRNO_TO_PARA_ERROR(EAGAIN
))
156 * The post select hook for client commands.
158 * \param s Pointer to the scheduler.
159 * \param t Pointer to the task struct for this command.
161 * Depending on the current state of the connection and the status of the read
162 * and write fd sets of \a s, this function performs the necessary steps to
163 * authenticate the connection, to send the command given by \a t->private_data
164 * and to receive para_server's output, if any.
166 * \sa struct sched, struct task.
168 static void client_post_select(struct sched
*s
, struct task
*t
)
170 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
171 struct btr_node
*btrn
= ct
->btrn
;
174 char buf
[CLIENT_BUFSIZE
];
179 switch (ct
->status
) {
180 case CL_CONNECTED
: /* receive welcome message */
181 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
182 if (ret
< 0 || n
== 0)
184 ct
->status
= CL_RECEIVED_WELCOME
;
186 case CL_RECEIVED_WELCOME
: /* send auth command */
187 sprintf(buf
, AUTH_REQUEST_MSG
"%s", ct
->user
);
188 PARA_INFO_LOG("--> %s\n", buf
);
189 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
191 ret
= send_buffer(ct
->scc
.fd
, buf
);
194 ct
->status
= CL_SENT_AUTH
;
198 * Receive challenge and session keys, decrypt the challenge and
199 * send back the hash of the decrypted challenge.
202 /* decrypted challenge/session key buffer */
203 unsigned char crypt_buf
[1024];
204 /* the SHA1 of the decrypted challenge */
205 unsigned char challenge_hash
[HASH_SIZE
];
207 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
208 if (ret
< 0 || n
== 0)
210 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n
);
211 ret
= priv_decrypt(ct
->key_file
, crypt_buf
,
212 (unsigned char *)buf
, n
);
215 hash_function((char *)crypt_buf
, CHALLENGE_SIZE
, challenge_hash
);
216 ct
->scc
.send
= sc_new(crypt_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
);
217 ct
->scc
.recv
= sc_new(crypt_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
219 hash_to_asc(challenge_hash
, buf
);
220 PARA_INFO_LOG("--> %s\n", buf
);
221 ret
= send_bin_buffer(ct
->scc
.fd
, (char *)challenge_hash
,
225 ct
->status
= CL_SENT_CH_RESPONSE
;
228 case CL_SENT_CH_RESPONSE
: /* read server response */
230 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
231 if (ret
< 0 || n
== 0)
233 /* check if server has sent "Proceed" message */
234 ret
= -E_CLIENT_AUTH
;
235 if (n
< PROCEED_MSG_LEN
)
237 if (!strstr(buf
, PROCEED_MSG
))
239 ct
->status
= CL_RECEIVED_PROCEED
;
242 case CL_RECEIVED_PROCEED
: /* concat args and send command */
245 char *command
= NULL
;
246 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
248 for (i
= 0; i
< ct
->conf
.inputs_num
; i
++) {
250 command
= make_message("%s\n%s", command
?
251 command
: "", ct
->conf
.inputs
[i
]);
254 command
= para_strcat(command
, EOC_MSG
"\n");
255 PARA_DEBUG_LOG("--> %s\n", command
);
256 ret
= sc_send_buffer(&ct
->scc
, command
);
260 ct
->status
= CL_SENT_COMMAND
;
263 case CL_SENT_COMMAND
:
266 /* can not use "buf" here because we need a malloced buffer */
267 buf2
= para_malloc(CLIENT_BUFSIZE
);
268 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
270 if (strstr(buf2
, AWAITING_DATA_MSG
)) {
272 ct
->status
= CL_SENDING
;
275 ct
->status
= CL_RECEIVING
;
276 btr_add_output(buf2
, n
, btrn
);
285 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
290 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
292 sz
= btr_next_buffer(btrn
, &buf2
);
293 ret
= sc_send_bin_buffer(&ct
->scc
, buf2
, sz
);
296 btr_consume(btrn
, sz
);
302 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
308 * The FD_ISSET() is not strictly necessary, but is allows us
309 * to skip the malloc below if there is nothing to read anyway.
311 if (!FD_ISSET(ct
->scc
.fd
, &s
->rfds
))
313 buf2
= para_malloc(CLIENT_BUFSIZE
);
314 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
316 buf2
= para_realloc(buf2
, n
);
317 btr_add_output(buf2
, n
, btrn
);
326 if (ret
!= -E_SERVER_EOF
&& ret
!= -E_BTR_EOF
)
327 PARA_ERROR_LOG("%s\n", para_strerror(-t
->error
));
328 btr_remove_node(btrn
);
333 * Connect to para_server and register the client task.
335 * \param ct The initialized client task structure.
336 * \param s The scheduler instance to register the client task to.
337 * \param parent The parent node of the client btr node.
338 * \param child The child node of the client node.
340 * The client task structure given by \a ct must be allocated and initialized
341 * by \ref client_parse_config() before this function is called.
345 int client_connect(struct client_task
*ct
, struct sched
*s
,
346 struct btr_node
*parent
, struct btr_node
*child
)
350 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
351 ct
->conf
.server_port_arg
);
353 ret
= para_connect_simple(IPPROTO_TCP
, ct
->conf
.hostname_arg
,
354 ct
->conf
.server_port_arg
);
358 ret
= mark_fd_nonblocking(ct
->scc
.fd
);
361 ct
->status
= CL_CONNECTED
;
362 ct
->btrn
= btr_new_node(&(struct btr_node_description
)
363 EMBRACE(.name
= "client", .parent
= parent
, .child
= child
));
364 ct
->task
.pre_select
= client_pre_select
;
365 ct
->task
.post_select
= client_post_select
;
367 sprintf(ct
->task
.status
, "client");
368 register_task(s
, &ct
->task
);
377 * Parse a client configuration.
379 * \param argc Usual argument count.
380 * \param argv Usual argument vector.
381 * \param ct_ptr Filled in by this function.
382 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
384 * This checks the command line options given by \a argc and \a argv, sets
385 * default values for the user name and the name of the rsa key file and reads
386 * further options from the config file.
388 * Upon successful return, \a ct_ptr points to a dynamically allocated and
389 * initialized client task struct.
391 * \return The number of non-option arguments in \a argc/argv on success,
392 * negative on errors.
394 int client_parse_config(int argc
, char *argv
[], struct client_task
**ct_ptr
,
397 char *home
= para_homedir();
399 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
403 ret
= -E_CLIENT_SYNTAX
;
404 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
406 HANDLE_VERSION_FLAG("client", ct
->conf
);
408 ct
->config_file
= ct
->conf
.config_file_given
?
409 para_strdup(ct
->conf
.config_file_arg
) :
410 make_message("%s/.paraslash/client.conf", home
);
411 ret
= file_exists(ct
->config_file
);
412 if (!ret
&& ct
->conf
.config_file_given
) {
417 struct client_cmdline_parser_params params
= {
421 .check_ambiguity
= 0,
425 if (client_cmdline_parser_config_file(ct
->config_file
,
429 ct
->user
= ct
->conf
.user_given
?
430 para_strdup(ct
->conf
.user_arg
) : para_logname();
432 if (ct
->conf
.key_file_given
)
433 ct
->key_file
= para_strdup(ct
->conf
.key_file_arg
);
435 ct
->key_file
= make_message("%s/.paraslash/key.%s",
437 if (!file_exists(ct
->key_file
)) {
439 ct
->key_file
= make_message("%s/.ssh/id_rsa", home
);
444 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
445 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
446 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
447 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
448 ret
= ct
->conf
.inputs_num
;
452 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
460 * Parse the client configuration and open a connection to para_server.
462 * \param argc See \ref client_parse_config.
463 * \param argv See \ref client_parse_config.
464 * \param ct_ptr See \ref client_parse_config.
465 * \param loglevel See \ref client_parse_config.
466 * \param parent See \ref client_connect().
467 * \param child See \ref client_connect().
468 * \param sched See \ref client_connect().
470 * This function combines client_parse_config() and client_connect(). It is
471 * considered a syntax error if no command was given, i.e. if the number
472 * of non-option arguments is zero.
476 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
477 int *loglevel
, struct btr_node
*parent
, struct btr_node
*child
,
480 int ret
= client_parse_config(argc
, argv
, ct_ptr
, loglevel
);
485 ret
= -E_CLIENT_SYNTAX
;
488 ret
= client_connect(*ct_ptr
, sched
, parent
, child
);
493 client_close(*ct_ptr
);