2 * Copyright (C) 1997-2011 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 free all resources.
32 * \param ct Pointer to the client data.
36 void client_close(struct client_task
*ct
)
42 sc_free(ct
->scc
.recv
);
43 sc_free(ct
->scc
.send
);
45 free(ct
->config_file
);
47 btr_free_node(ct
->btrn
);
48 client_cmdline_parser_free(&ct
->conf
);
53 * The preselect hook for server commands.
55 * \param s Pointer to the scheduler.
56 * \param t Pointer to the task struct for this command.
58 * The task pointer must contain a pointer to the initialized client data
59 * structure as it is returned by client_open().
61 * This function checks the state of the connection and adds the file descriptor
62 * of the connection to the read or write fd set of \a s accordingly.
64 * \sa register_task() client_open(), struct sched, struct task.
66 static void client_pre_select(struct sched
*s
, struct task
*t
)
69 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
70 struct btr_node
*btrn
= ct
->btrn
;
77 case CL_SENT_CH_RESPONSE
:
79 para_fd_set(ct
->scc
.fd
, &s
->rfds
, &s
->max_fileno
);
82 case CL_RECEIVED_WELCOME
:
83 case CL_RECEIVED_PROCEED
:
84 para_fd_set(ct
->scc
.fd
, &s
->wfds
, &s
->max_fileno
);
88 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
93 para_fd_set(ct
->scc
.fd
, &s
->rfds
,
98 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
103 para_fd_set(ct
->scc
.fd
, &s
->wfds
,
110 static int client_recv_buffer(struct client_task
*ct
, fd_set
*rfds
,
111 char *buf
, size_t sz
, size_t *n
)
115 if (ct
->status
< CL_SENT_CH_RESPONSE
)
116 return read_nonblock(ct
->scc
.fd
, buf
, sz
, rfds
, n
);
119 ret
= sc_recv_buffer(&ct
->scc
, buf
, sz
);
121 * sc_recv_buffer is used with blocking fds elsewhere, so it
122 * does not use the nonblock-API. Therefore we need to
123 * check for EOF and EAGAIN.
126 return -E_SERVER_EOF
;
127 if (ret
== -ERRNO_TO_PARA_ERROR(EAGAIN
))
136 * The post select hook for client commands.
138 * \param s Pointer to the scheduler.
139 * \param t Pointer to the task struct for this command.
141 * Depending on the current state of the connection and the status of the read
142 * and write fd sets of \a s, this function performs the necessary steps to
143 * authenticate the connection, to send the command given by \a t->private_data
144 * and to receive para_server's output, if any.
146 * \sa struct sched, struct task.
148 static void client_post_select(struct sched
*s
, struct task
*t
)
150 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
151 struct btr_node
*btrn
= ct
->btrn
;
154 char buf
[CLIENT_BUFSIZE
];
159 switch (ct
->status
) {
160 case CL_CONNECTED
: /* receive welcome message */
161 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
162 if (ret
< 0 || n
== 0)
164 ct
->status
= CL_RECEIVED_WELCOME
;
166 case CL_RECEIVED_WELCOME
: /* send auth command */
167 sprintf(buf
, AUTH_REQUEST_MSG
"%s", ct
->user
);
168 PARA_INFO_LOG("--> %s\n", buf
);
169 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
171 ret
= send_buffer(ct
->scc
.fd
, buf
);
174 ct
->status
= CL_SENT_AUTH
;
178 * Receive challenge and session keys, decrypt the challenge and
179 * send back the hash of the decrypted challenge.
182 /* decrypted challenge/session key buffer */
183 unsigned char crypt_buf
[1024];
184 /* the SHA1 of the decrypted challenge */
185 unsigned char challenge_hash
[HASH_SIZE
];
187 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
188 if (ret
< 0 || n
== 0)
190 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n
);
191 ret
= priv_decrypt(ct
->key_file
, crypt_buf
,
192 (unsigned char *)buf
, n
);
195 hash_function((char *)crypt_buf
, CHALLENGE_SIZE
, challenge_hash
);
196 ct
->scc
.send
= sc_new(crypt_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
);
197 ct
->scc
.recv
= sc_new(crypt_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
199 hash_to_asc(challenge_hash
, buf
);
200 PARA_INFO_LOG("--> %s\n", buf
);
201 ret
= send_bin_buffer(ct
->scc
.fd
, (char *)challenge_hash
,
205 ct
->status
= CL_SENT_CH_RESPONSE
;
208 case CL_SENT_CH_RESPONSE
: /* read server response */
210 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
211 if (ret
< 0 || n
== 0)
213 /* check if server has sent "Proceed" message */
214 ret
= -E_CLIENT_AUTH
;
215 if (n
< PROCEED_MSG_LEN
)
217 if (!strstr(buf
, PROCEED_MSG
))
219 ct
->status
= CL_RECEIVED_PROCEED
;
222 case CL_RECEIVED_PROCEED
: /* concat args and send command */
225 char *command
= NULL
;
226 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
228 for (i
= 0; i
< ct
->conf
.inputs_num
; i
++) {
230 command
= make_message("%s\n%s", command
?
231 command
: "", ct
->conf
.inputs
[i
]);
234 command
= para_strcat(command
, EOC_MSG
"\n");
235 PARA_DEBUG_LOG("--> %s\n", command
);
236 ret
= sc_send_buffer(&ct
->scc
, command
);
240 ct
->status
= CL_SENT_COMMAND
;
243 case CL_SENT_COMMAND
:
246 /* can not use "buf" here because we need a malloced buffer */
247 buf2
= para_malloc(CLIENT_BUFSIZE
);
248 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
250 if (strstr(buf2
, AWAITING_DATA_MSG
)) {
252 ct
->status
= CL_SENDING
;
255 ct
->status
= CL_RECEIVING
;
256 btr_add_output(buf2
, n
, btrn
);
265 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
270 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
272 sz
= btr_next_buffer(btrn
, &buf2
);
273 ret
= sc_send_bin_buffer(&ct
->scc
, buf2
, sz
);
276 btr_consume(btrn
, sz
);
282 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
288 * The FD_ISSET() is not strictly necessary, but is allows us
289 * to skip the malloc below if there is nothing to read anyway.
291 if (!FD_ISSET(ct
->scc
.fd
, &s
->rfds
))
293 buf2
= para_malloc(CLIENT_BUFSIZE
);
294 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
296 buf2
= para_realloc(buf2
, n
);
297 btr_add_output(buf2
, n
, btrn
);
306 if (ret
!= -E_SERVER_EOF
&& ret
!= -E_BTR_EOF
)
307 PARA_ERROR_LOG("%s\n", para_strerror(-t
->error
));
308 btr_remove_node(btrn
);
312 /* connect to para_server and register the client task */
313 static int client_connect(struct client_task
*ct
)
318 ret
= para_connect_simple(IPPROTO_TCP
, ct
->conf
.hostname_arg
,
319 ct
->conf
.server_port_arg
);
323 ct
->status
= CL_CONNECTED
;
324 ret
= mark_fd_nonblocking(ct
->scc
.fd
);
327 ct
->task
.pre_select
= client_pre_select
;
328 ct
->task
.post_select
= client_post_select
;
329 sprintf(ct
->task
.status
, "client");
330 register_task(&ct
->task
);
339 * Open connection to para_server.
341 * \param argc Usual argument count.
342 * \param argv Usual argument vector.
343 * \param ct_ptr Points to dynamically allocated and initialized client task
344 * struct upon successful return.
345 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
346 * \param parent Add the new buffer tree node as a child of this node.
347 * \param child Add the new buffer tree node as a parent of this node.
349 * Check the command line options given by \a argc and argv, set default values
350 * for user name and rsa key file, read further option from the config file.
351 * Finally, establish a connection to para_server.
355 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
356 int *loglevel
, struct btr_node
*parent
, struct btr_node
*child
)
358 char *home
= para_homedir();
360 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
362 ct
->btrn
= btr_new_node(&(struct btr_node_description
)
363 EMBRACE(.name
= "client", .parent
= parent
, .child
= child
));
366 ret
= -E_CLIENT_SYNTAX
;
367 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
369 HANDLE_VERSION_FLAG("client", ct
->conf
);
370 ret
= -E_CLIENT_SYNTAX
;
371 if (!ct
->conf
.inputs_num
)
374 ct
->config_file
= ct
->conf
.config_file_given
?
375 para_strdup(ct
->conf
.config_file_arg
) :
376 make_message("%s/.paraslash/client.conf", home
);
377 ret
= file_exists(ct
->config_file
);
378 if (!ret
&& ct
->conf
.config_file_given
) {
383 struct client_cmdline_parser_params params
= {
387 .check_ambiguity
= 0,
391 if (client_cmdline_parser_config_file(ct
->config_file
,
395 ct
->user
= ct
->conf
.user_given
?
396 para_strdup(ct
->conf
.user_arg
) : para_logname();
398 if (ct
->conf
.key_file_given
)
399 ct
->key_file
= para_strdup(ct
->conf
.key_file_arg
);
401 ct
->key_file
= make_message("%s/.paraslash/key.%s",
403 if (!file_exists(ct
->key_file
)) {
405 ct
->key_file
= make_message("%s/.ssh/id_rsa", home
);
410 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
411 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
412 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
413 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
414 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
415 ct
->conf
.server_port_arg
);
416 ret
= client_connect(ct
);
420 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
421 btr_remove_node(ct
->btrn
);